-
Notifications
You must be signed in to change notification settings - Fork 21
/
mpris2.go
172 lines (165 loc) · 4.48 KB
/
mpris2.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
package main
import (
"fmt"
"strings"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
"github.com/godbus/dbus/v5/prop"
)
type MprisPlayer struct {
conn *dbus.Conn
player *Player
logger Logger
}
// Mandatory functions
func (mpp MprisPlayer) Stop() {
if err := mpp.player.Stop(); err != nil {
mpp.logger.Printf(err.Error())
}
}
func (mpp MprisPlayer) Next() {
mpp.player.PlayNextTrack()
}
func (mpp MprisPlayer) Pause() {
psd, err := mpp.player.IsPaused()
if err != nil {
mpp.logger.Printf(err.Error())
return
}
if !psd {
if _, err = mpp.player.Pause(); err != nil {
mpp.logger.Printf(err.Error())
}
}
}
func (mpp MprisPlayer) Play() {
psd, err := mpp.player.IsPaused()
if err != nil {
mpp.logger.Printf(err.Error())
return
}
if psd {
if _, err = mpp.player.Pause(); err != nil {
mpp.logger.Printf(err.Error())
}
}
}
func (mpp MprisPlayer) PlayPause() {
mpp.player.Pause()
}
func (mpp MprisPlayer) OpenUri(string) {
// TODO not implemented
}
func (mpp MprisPlayer) Previous() {
// TODO not implemented
}
func (mpp MprisPlayer) Seek(int) {
// TODO not implemented
}
func (mpp MprisPlayer) Seeked(int) {
// TODO not implemented
}
func (mpp MprisPlayer) SetPosition(string, int) {
// TODO not implemented
}
func RegisterPlayer(p *Player, l Logger) (MprisPlayer, error) {
conn, err := dbus.ConnectSessionBus()
if err != nil {
return MprisPlayer{}, err
}
parts := []string{"", "org", "mpris", "MediaPlayer2", "Player"}
name := strings.Join(parts[1:], ".")
mpp := MprisPlayer{
conn: conn,
player: p,
logger: l,
}
err = conn.ExportAll(mpp, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player")
if err != nil {
return MprisPlayer{}, err
}
/*
func (mpp MprisPlayer) Metadata() string {
if len(mpp.player.Queue) == 0 {
return ""
}
playing := mpp.player.Queue[0]
return fmt.Sprintf("%s - %s", playing.Artist, playing.Title)
}
Shuffle true/false
LoopStatus "Noneon, "Track", "Playlist"
Position time_in_us
MaximumRate, Rate, MinimumRate (float 0-1, x speed)
*/
metadata := map[string]interface{}{
"mpris:trackid": "",
"mpris:length": int64(0),
"xesam:album": "",
"xesam:albumArtist": "",
"xesam:artist": []string{},
"xesam:composer": []string{},
"xesam:genre": []string{},
"xesam:title": "",
"xesam:trackNumber": int(0),
}
propSpec := map[string]map[string]*prop.Prop{
"org.mpris.MediaPlayer2.Player": {
"CanControl": {Value: true, Writable: false, Emit: prop.EmitFalse, Callback: nil},
"CanGoNext": {Value: true, Writable: false, Emit: prop.EmitFalse, Callback: nil},
"CanPause": {Value: true, Writable: false, Emit: prop.EmitFalse, Callback: nil},
"CanPlay": {Value: true, Writable: false, Emit: prop.EmitFalse, Callback: nil},
"CanSeek": {Value: false, Writable: false, Emit: prop.EmitFalse, Callback: nil},
"CanGoPrevious": {Value: false, Writable: false, Emit: prop.EmitFalse, Callback: nil},
"Metadata": {Value: metadata, Writable: false, Emit: prop.EmitTrue, Callback: nil},
"Volume": {Value: float64(0.0), Writable: true, Emit: prop.EmitTrue, Callback: func(c *prop.Change) *dbus.Error {
oldVolume, err := mpp.player.Volume()
if err != nil {
mpp.logger.Printf(err.Error())
return nil
}
fvol := c.Value.(float64)
if fvol < 0 {
mpp.player.AdjustVolume(-oldVolume)
return nil
}
vol := int64(fvol * 100)
volDiff := vol - oldVolume
mpp.player.AdjustVolume(volDiff)
return nil
},
},
"PlaybackStatus": {Value: "", Writable: false, Emit: prop.EmitFalse, Callback: nil},
},
}
props, err := prop.Export(conn, "/org/mpris/MediaPlayer2", propSpec)
if err != nil {
return MprisPlayer{}, err
}
n := &introspect.Node{
Name: "/org/mpris/MediaPlayer2",
Interfaces: []introspect.Interface{
introspect.IntrospectData,
prop.IntrospectData,
{
Name: "org.mpris.MediaPlayer2.Player",
Methods: introspect.Methods(mpp),
Properties: props.Introspection("org.mpris.MediaPlayer2.Player"),
},
},
}
err = conn.Export(introspect.NewIntrospectable(n), "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Introspectable")
if err != nil {
return MprisPlayer{}, err
}
reply, err := conn.RequestName(name, dbus.NameFlagDoNotQueue)
if err != nil {
return MprisPlayer{}, err
}
if reply != dbus.RequestNameReplyPrimaryOwner {
return MprisPlayer{}, fmt.Errorf("name already owned")
}
return mpp, nil
}
func (m MprisPlayer) Close() {
m.conn.Close()
}