diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 61 |
1 files changed, 29 insertions, 32 deletions
@@ -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) - } |