This repository has been archived by the owner on Dec 25, 2021. It is now read-only.
/
mpris.go
177 lines (141 loc) · 4 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
package mpris
import (
"log"
"strings"
"github.com/godbus/dbus"
)
const (
dbusObjectPath = "/org/mpris/MediaPlayer2"
propertiesChangedSignal = "org.freedesktop.DBus.Properties.PropertiesChanged"
baseInterface = "org.mpris.MediaPlayer2"
playerInterface = "org.mpris.MediaPlayer2.Player"
trackListInterface = "org.mpris.MediaPlayer2.TrackList"
playlistsInterface = "org.mpris.MediaPlayer2.Playlists"
getPropertyMethod = "org.freedesktop.DBus.Properties.Get"
setPropertyMethod = "org.freedesktop.DBus.Properties.Set"
)
func getProperty(obj *dbus.Object, iface string, prop string) dbus.Variant {
result := dbus.Variant{}
err := obj.Call(getPropertyMethod, 0, iface, prop).Store(&result)
if err != nil {
log.Println("Warning: could not get dbus property:", iface, prop, err)
return dbus.Variant{}
}
return result
}
func setProperty(obj *dbus.Object, iface string, prop string, val interface{}) {
call := obj.Call(setPropertyMethod, 0, prop, val)
if call.Err != nil {
log.Println("Warning: could not set dbus property:", iface, prop, call.Err)
}
}
func List(conn *dbus.Conn) ([]string, error) {
var names []string
err := conn.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&names)
if err != nil {
return nil, err
}
var mprisNames []string
for _, name := range names {
if strings.HasPrefix(name, baseInterface) {
mprisNames = append(mprisNames, name)
}
}
return mprisNames, nil
}
type Player struct {
*base
*player
}
type base struct {
obj *dbus.Object
}
func (i *base) Raise() {
i.obj.Call(baseInterface+".Raise", 0)
}
func (i *base) Quit() {
i.obj.Call(baseInterface+".Quit", 0)
}
func (i *base) GetIdentity() string {
return getProperty(i.obj, baseInterface, "Identity").String()
}
type player struct {
obj *dbus.Object
}
func (i *player) Next() {
i.obj.Call(playerInterface+".Next", 0)
}
func (i *player) Previous() {
i.obj.Call(playerInterface+".Previous", 0)
}
func (i *player) Pause() {
i.obj.Call(playerInterface+".Pause", 0)
}
func (i *player) PlayPause() {
i.obj.Call(playerInterface+".PlayPause", 0)
}
func (i *player) Stop() {
i.obj.Call(playerInterface+".Stop", 0)
}
func (i *player) Play() {
i.obj.Call(playerInterface+".Play", 0)
}
func (i *player) Seek(offset int64) {
i.obj.Call(playerInterface+".Seek", 0, offset)
}
func (i *player) SetPosition(trackId *dbus.ObjectPath, position int64) {
i.obj.Call(playerInterface+".SetPosition", 0, trackId, position)
}
func (i *player) OpenUri(uri string) {
i.obj.Call(playerInterface+".OpenUri", 0, uri)
}
type PlaybackStatus string
const (
PlaybackPlaying PlaybackStatus = "Playing"
PlaybackPaused = "Paused"
PlaybackStopped = "Stopped"
)
func (i *player) GetPlaybackStatus() PlaybackStatus {
variant, err := i.obj.GetProperty(playerInterface+".PlaybackStatus")
if err != nil {
return ""
}
return PlaybackStatus(variant.String())
}
type LoopStatus string
const (
LoopNone LoopStatus = "None"
LoopTrack = "Track"
LoopPlaylist = "Playlist"
)
func (i *player) GetLoopStatus() LoopStatus {
return LoopStatus(getProperty(i.obj, playerInterface, "LoopStatus").String())
}
func (i *player) GetRate() float64 {
return getProperty(i.obj, playerInterface, "Rate").Value().(float64)
}
func (i *player) GetShuffle() bool {
return getProperty(i.obj, playerInterface, "Shuffle").Value().(bool)
}
func (i *player) GetMetadata() map[string]dbus.Variant {
return getProperty(i.obj, playerInterface, "Metadata").Value().(map[string]dbus.Variant)
}
func (i *player) GetVolume() float64 {
return getProperty(i.obj, playerInterface, "Volume").Value().(float64)
}
func (i *player) SetVolume(volume float64) {
setProperty(i.obj, playerInterface, "Volume", volume)
}
func (i *player) GetPosition() int64 {
return getProperty(i.obj, playerInterface, "Position").Value().(int64)
}
func (i *player) SetPosition(position float64) {
setProperty(i.obj, playerInterface, "Position", position)
}
func New(conn *dbus.Conn, name string) *Player {
obj := conn.Object(name, dbusObjectPath).(*dbus.Object)
return &Player{
&base{obj},
&player{obj},
}
}