-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.go
53 lines (43 loc) · 1.18 KB
/
player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"errors"
"strings"
"github.com/Pauloo27/go-mpris"
"github.com/godbus/dbus/v5"
)
var (
errAllExcluded = errors.New("All players are excluded (blacklisted or non-whitelisted)")
errNoPlayers = errors.New("No players found")
)
func getPlayerName(conn *dbus.Conn, conf config) (string, error) {
names, err := mpris.List(conn)
if err != nil {
panic(err)
}
if len(names) == 0 {
return "", errNoPlayers
}
// get first player name, unless it's excluded
for _, propName := range names {
// get identity of each player
identity, err := getIdentity(conn, propName, conf.UseIdentifiers)
if err != nil {
panic(err)
}
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
}
}
return "", errAllExcluded
}
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()
}
}