-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·96 lines (77 loc) · 1.79 KB
/
main.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
package main
import (
"errors"
"os"
"github.com/godbus/dbus/v5"
"github.com/jessevdk/go-flags"
)
var opts struct {
}
var (
flagParser = flags.NewParser(&opts, flags.Default)
)
func main() {
if _, err := flagParser.Parse(); err != nil {
var e *flags.Error
if errors.As(err, &e) {
/* If it's flags' error type, the error message has already been printed */
if e.Type == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
}
panic(err)
}
}
func getDBusSessionConnection() *dbus.Conn {
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}
// defer conn.Close() - static lifetime
return conn
}
func getDBusObj(conn *dbus.Conn) dbus.BusObject {
obj := conn.Object(
"org.mpris.MediaPlayer2.spotify", // bus address
"/org/mpris/MediaPlayer2", // object path
)
return obj
}
func getMetadata(obj dbus.BusObject) map[string]interface{} {
var metadata map[string]interface{}
variant, err := obj.GetProperty(
"org.mpris.MediaPlayer2.Player.Metadata", // Interface.Property
)
if err != nil {
panic(err)
}
err = dbus.Store([]interface{}{variant}, &metadata)
if err != nil {
panic(err)
}
return metadata
}
func getPlaybackStatus(obj dbus.BusObject) string {
var status string
variant, err := obj.GetProperty(
"org.mpris.MediaPlayer2.Player.PlaybackStatus", // Interface.Property
)
if err != nil {
panic(err)
}
err = dbus.Store([]interface{}{variant}, &status)
if err != nil {
panic(err)
}
return status
}
func watchPropertiesChanged(conn *dbus.Conn, obj dbus.BusObject) <-chan *dbus.Signal {
obj.AddMatchSignal( // Makes a call under the hood, to add a Match rule to this object
"org.freedesktop.DBus.Properties", // Interface
"PropertiesChanged", // Member, ie Signal
)
ch := make(chan *dbus.Signal, 10)
conn.Signal(ch)
return ch
}