From 3cf3f910caea1fe0f3f7da08ca3bc20ba2f44d30 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Fri, 21 Aug 2020 15:44:59 +0100 Subject: add players as detected, switch to currently playing player on state change. --- .gitignore | 1 + main | Bin 4170927 -> 0 bytes main.go | 61 +++++++++++++++++++++++++++++-------------------------------- 3 files changed, 30 insertions(+), 32 deletions(-) create mode 100644 .gitignore delete mode 100755 main diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b81853 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +./main diff --git a/main b/main deleted file mode 100755 index 08d9118..0000000 Binary files a/main and /dev/null differ diff --git a/main.go b/main.go index 455fa79..9f2b249 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "github.com/godbus/dbus/v5" "sort" "strings" + // "time" ) type Player struct { @@ -85,8 +86,6 @@ func (s Players) Len() int { } func (s Players) Less(i, j int) bool { - // x, err := s[i].GetProperty("org.mpris.MediaPlayer2.Player.PlaybackStatus") - // y, err := s[j].GetProperty("org.mpris.MediaPlayer2.Player.PlaybackStatus") s[i].Refresh() s[j].Refresh() var states [2]int @@ -96,13 +95,6 @@ func (s Players) Less(i, j int) bool { if s[j].playing { states[1] = 1 } - // if strings.Contains(x.String(), "Playing") { - // states[0] = 1 - // } - // if strings.Contains(y.String(), "Playing") { - // states[1] = 1 - // } - // fmt.Println(states[i]) // reverse return states[0] > states[1] } @@ -116,39 +108,44 @@ func main() { if err != nil { panic(err) } - var fd []string - err = conn.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&fd) - if err != nil { - panic(err) - } var players Players - for _, name := range fd { - if strings.HasPrefix(name, "org.mpris.MediaPlayer2") { - //players = append(players, conn.Object(name, "/org/mpris/MediaPlayer2")) - players = append(players, NewPlayer(conn, name)) + getPlayers := func() { + var fd []string + err = conn.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&fd) + if err != nil { + panic(err) + } + for _, name := range fd { + if strings.HasPrefix(name, "org.mpris.MediaPlayer2") { + players = append(players, NewPlayer(conn, name)) + } } + sort.Sort(players) } - sort.Sort(players) + getPlayers() + go func() { + conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, + "type='signal',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'") + c := make(chan *dbus.Signal, 10) + conn.Signal(c) + for v := range c { + switch name := v.Body[0].(type) { + case string: + if strings.Contains(name, "org.mpris.MediaPlayer2") && name != "org.mpris.MediaPlayer2.Player" { + players = append(players, NewPlayer(conn, name)) + sort.Sort(players) + } + } + } + }() conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, "type='signal',path='/org/mpris/MediaPlayer2',interface='org.freedesktop.DBus.Properties'") c := make(chan *dbus.Signal, 10) conn.Signal(c) fmt.Println(players[0].JSON()) for range c { + sort.Sort(players) players[0].Refresh() fmt.Println(players[0].JSON()) } - // fmt.Printf("%d: %s: %t\n", i, s.playerName, s.playing) - // for key, val := range s.metadata { - // fmt.Println(key, val) - // } - - // fmt.Println(fd) - // node, err := introspect.Call(conn.Object("org.mpris.MediaPlayer2", "/org/mpris/MediaPlayer2")) - // if err != nil { - // panic(err) - // } - // data, _ := json.MarshalIndent(node, "", " ") - // fmt.Println(data) - } -- cgit v1.2.3