-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpris.go
181 lines (160 loc) · 4.21 KB
/
mpris.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package blocks
import (
"context"
"strings"
"github.com/godbus/dbus/v5"
. "github.com/kraftwerk28/gost/core"
"github.com/kraftwerk28/gost/core/formatting"
)
type MprisBlockConfig struct {
Icons map[playbackStatus]string `yaml:"icons"`
PlayerFormat *ConfigFormat `yaml:"player_format"`
Separator string `yaml:"separator"`
}
type playbackStatus string
const (
playbackStatusPlaying playbackStatus = "Playing"
playbackStatusPaused playbackStatus = "Paused"
playbackStatusStopped playbackStatus = "Stopped"
)
type MprisPlayer struct {
dbusName string
nameOwner string
metadata map[string]dbus.Variant
playbackStatus playbackStatus
}
type MprisBlock struct {
MprisBlockConfig
dbus *dbus.Conn
players []MprisPlayer
}
func NewMprisBlock() I3barBlocklet {
b := MprisBlock{}
b.PlayerFormat = NewConfigFormatFromString("{icon$}{title^10}")
b.Icons = make(map[playbackStatus]string)
return &b
}
func (s *MprisBlock) GetConfig() interface{} {
return &s.MprisBlockConfig
}
const mprisPath dbus.ObjectPath = "/org/mpris/MediaPlayer2"
const mprisPlayerIface = "org.mpris.MediaPlayer2.Player"
func (b *MprisBlock) fetchPlayers() (err error) {
var names []string
if err = b.dbus.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&names); err != nil {
return
}
b.players = make([]MprisPlayer, 0)
for _, name := range names {
if strings.Contains(name, "org.mpris.MediaPlayer2") &&
!strings.Contains(name, "playerctld") {
p := b.dbus.Object(name, "/org/mpris/MediaPlayer2")
player := MprisPlayer{}
player.dbusName = name
b.dbus.BusObject().Call(
"org.freedesktop.DBus.GetNameOwner", 0, name,
).Store(&player.nameOwner)
p.Call(dbusGetProperty, 0, mprisPlayerIface, "PlaybackStatus").Store(&player.playbackStatus)
p.Call(dbusGetProperty, 0, mprisPlayerIface, "Metadata").Store(&player.metadata)
b.players = append(b.players, player)
}
}
return
}
func (b *MprisBlock) Run(ch UpdateChan, ctx context.Context) {
if _, ok := b.Icons[playbackStatusPaused]; !ok {
b.Icons[playbackStatusPaused] = " "
}
if _, ok := b.Icons[playbackStatusPlaying]; !ok {
b.Icons[playbackStatusPlaying] = " "
}
if _, ok := b.Icons[playbackStatusStopped]; !ok {
b.Icons[playbackStatusStopped] = " "
}
var err error
b.dbus, err = dbus.ConnectSessionBus()
if err != nil {
Log.Print(err)
return
}
defer b.dbus.Close()
if err = b.fetchPlayers(); err != nil {
Log.Print(err)
return
}
ch.SendUpdate()
err = b.dbus.AddMatchSignal(
dbus.WithMatchObjectPath(mprisPath),
dbus.WithMatchInterface("org.freedesktop.DBus.Properties"),
dbus.WithMatchMember("PropertiesChanged"),
)
err = b.dbus.AddMatchSignal(
dbus.WithMatchObjectPath(dbusObjectPath),
dbus.WithMatchInterface("org.freedesktop.DBus"),
)
if err != nil {
Log.Print(err)
return
}
c := make(chan *dbus.Signal)
b.dbus.Signal(c)
for {
select {
case sig := <-c:
switch sig.Path {
case dbusObjectPath:
b.fetchPlayers()
ch.SendUpdate()
case mprisPath:
var player *MprisPlayer
shouldUpdate := false
for i := range b.players {
player = &b.players[i]
if player.dbusName == sig.Sender ||
player.nameOwner == sig.Sender {
break
}
}
if player == nil {
break
}
chProps := sig.Body[1].(map[string]dbus.Variant)
if p, ok := chProps["PlaybackStatus"]; ok {
p.Store(&player.playbackStatus)
shouldUpdate = true
}
if p, ok := chProps["Metadata"]; ok {
p.Store(&player.metadata)
shouldUpdate = true
}
if shouldUpdate {
ch.SendUpdate()
}
}
case <-ctx.Done():
return
}
}
}
func (t *MprisBlock) OnEvent(e *I3barClickEvent, ctx context.Context) {
}
func (b *MprisBlock) Render(cfg *AppConfig) []I3barBlock {
if len(b.players) == 0 {
return nil
}
parts := make([]string, len(b.players))
for i, pl := range b.players {
var title string
if titleVar, ok := pl.metadata["xesam:title"]; ok {
titleVar.Store(&title)
}
parts[i] = b.PlayerFormat.Expand(formatting.NamedArgs{
"icon": b.Icons[pl.playbackStatus],
"title": title,
})
}
return []I3barBlock{{FullText: strings.Join(parts, b.Separator)}}
}
func init() {
RegisterBlocklet("mpris", NewMprisBlock)
}