Skip to content

Commit

Permalink
chore: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
TorchedSammy committed Apr 16, 2022
2 parents 6f54835 + 6ff1bf5 commit cbe74da
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ It does not create a default config file, but for reference these are the config
{
"vars": [""], // define a list of vars for the presence from the metadata (reference from `playerctl metadata`)
"blacklist": [""], // list of blacklisted clients
"whitelist": [""], // list of whitelisted clients (ignored if empty)
"useIdentifiers": false, // use mpris player identifiers (as listed by `playerctl -l`) instead of names
"logLevel": "info", // level for log (accepts info, debug and error)
"presence": {
"details": "{title}" // the top text in the presence
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

type config struct {
Blacklist []string `json:"blacklist"`
Whitelist []string `json:"whitelist"`
UseIdentifiers bool `json:"useIdentifiers"`
Presence presenceConfig `json:"presence"`
PlayerPresence map[string]presenceConfig `json:"playerPresence"`
Vars []string `json:"vars"`
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func main() {
playerName, err := getPlayerName(infoConn, conf)
if err == errNoPlayers {
logger.Fatal("No MPRIS players found.")
} else if err == errAllBlacklisted {
logger.Fatal("Could not find any player that is not blacklisted.")
} else if err == errAllExcluded {
logger.Fatal("Could not find any player that is not excluded (blacklisted or non-whitelisted).")
}

player := mpris.New(infoConn, playerName)
Expand Down
28 changes: 18 additions & 10 deletions player.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"errors"
"strings"

"github.com/Pauloo27/go-mpris"
"github.com/godbus/dbus/v5"
)

var (
errAllBlacklisted = errors.New("All players are blacklisted")
errAllExcluded = errors.New("All players are excluded (blacklisted or non-whitelisted)")
errNoPlayers = errors.New("No players found")
)

Expand All @@ -22,24 +23,31 @@ func getPlayerName(conn *dbus.Conn, conf config) (string, error) {
return "", errNoPlayers
}

playerName := ""
// get first player name, unless it's in the blacklist
// get first player name, unless it's excluded
for _, propName := range names {
// get identity of each player
player := mpris.New(conn, propName)
identity, err := player.GetIdentity()
identity, err := getIdentity(conn, propName, conf.UseIdentifiers)
if err != nil {
panic(err)
}

if !contains(conf.Blacklist, identity) {
isBlacklisted := contains(conf.Blacklist, identity)
isWhitelisted := contains(conf.Whitelist, identity)
whitelistDisabled := conf.Whitelist == nil || len(conf.Whitelist) == 0

if !isBlacklisted && (whitelistDisabled || isWhitelisted) {
return propName, nil
}
}

if playerName == "" {
return "", errAllBlacklisted
}
return "", errAllExcluded
}

return "", nil // unreachable
func getIdentity(conn *dbus.Conn, propName string, useId bool) (string, error) {
if useId {
return strings.TrimPrefix(propName, "org.mpris.MediaPlayer2."), nil
} else {
player := mpris.New(conn, propName)
return player.GetIdentity()
}
}

0 comments on commit cbe74da

Please sign in to comment.