From f4090c1f0711b211ab54ba7eb2e43b3e4ea5e0ca Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Mon, 31 Aug 2020 20:14:14 +0100 Subject: ask before removing existing socket automatically removes the socket after 5 seconds of no input so that its functions headlessly. --- main.go | 267 ++++++++++++++++++++++++++++++++--------------------------- waybar-mpris | Bin 953088 -> 968532 bytes 2 files changed, 144 insertions(+), 123 deletions(-) diff --git a/main.go b/main.go index 10ada9e..436e65d 100644 --- a/main.go +++ b/main.go @@ -64,19 +64,19 @@ func NewPlayer(conn *dbus.Conn, name string) (p *Player) { for key, val := range knownPlayers { if strings.Contains(name, key) { playerName = val - if val == "Browser" { - file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid)) - if err == nil { - cmd := string(file) - for k, v := range knownBrowsers { - if strings.Contains(cmd, k) { - playerName = v - break - } - } + break + } + } + if playerName == "Browser" { + file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid)) + if err == nil { + cmd := string(file) + for key, val := range knownBrowsers { + if strings.Contains(cmd, key) { + playerName = val + break } } - break } } p = &Player{ @@ -382,132 +382,153 @@ func main() { fmt.Println("Response:") fmt.Printf(response) } - } else { - conn, err := dbus.SessionBus() - if err != nil { - panic(err) + os.Exit(0) + } + conn, err := dbus.SessionBus() + if err != nil { + log.Fatalln("Error connecting to DBus:", err) + } + players := &PlayerList{ + conn: conn, + } + players.Reload() + players.Sort() + players.Refresh() + fmt.Println(players.JSON()) + lastLine := "" + // fmt.Println("New array", players) + // Start command listener + if _, err := os.Stat(SOCK); err == nil { + fmt.Printf("Socket %s already exists, this could mean waybar-mpris is already running.\nStarting this instance will overwrite the file, possibly stopping other instances from accepting commands.\n", SOCK) + var input string + ignoreChoice := false + fmt.Printf("Continue? [y/n]: ") + go func() { + fmt.Scanln(&input) + if strings.Contains(input, "y") && !ignoreChoice { + os.Remove(SOCK) + } + }() + time.Sleep(5 * time.Second) + if input == "" { + fmt.Printf("\nRemoving due to lack of input.\n") + ignoreChoice = true + os.Remove(SOCK) } - players := &PlayerList{ - conn: conn, + + } + go func() { + listener, err := net.Listen("unix", SOCK) + if err != nil { + log.Fatalln("Couldn't establish socket connection at", SOCK) } - players.Reload() - players.Sort() - players.Refresh() - fmt.Println(players.JSON()) - lastLine := "" - // fmt.Println("New array", players) - // Start command listener - go func() { + defer func() { + listener.Close() os.Remove(SOCK) - listener, err := net.Listen("unix", SOCK) + }() + for { + con, err := listener.Accept() if err != nil { - log.Fatalln("Couldn't establish socket connection at", SOCK) + log.Println("Couldn't accept:", err) + continue } - defer listener.Close() - for { - con, err := listener.Accept() - if err != nil { - log.Println("Couldn't accept:", err) - continue - } - buf := make([]byte, 512) - nr, err := con.Read(buf) - if err != nil { - log.Println("Couldn't read:", err) - continue - } - command := string(buf[0:nr]) - if command == "player-next" { - length := len(players.list) - if length != 1 { - if players.current < uint(length-1) { - players.current += 1 - } else { - players.current = 0 - } - players.Refresh() - fmt.Println(players.JSON()) - } - } else if command == "player-prev" { - length := len(players.list) - if length != 1 { - if players.current != 0 { - players.current -= 1 - } else { - players.current = uint(length - 1) - } - players.Refresh() - fmt.Println(players.JSON()) + buf := make([]byte, 512) + nr, err := con.Read(buf) + if err != nil { + log.Println("Couldn't read:", err) + continue + } + command := string(buf[0:nr]) + if command == "player-next" { + length := len(players.list) + if length != 1 { + if players.current < uint(length-1) { + players.current += 1 + } else { + players.current = 0 } - } else if command == "next" { - players.Next() - } else if command == "prev" { - players.Prev() - } else if command == "toggle" { - players.Toggle() - } else if command == "list" { - resp := "" - pad := 0 - i := len(players.list) - for i != 0 { - i /= 10 - pad++ + players.Refresh() + fmt.Println(players.JSON()) + } + } else if command == "player-prev" { + length := len(players.list) + if length != 1 { + if players.current != 0 { + players.current -= 1 + } else { + players.current = uint(length - 1) } - for i, p := range players.list { - symbol := "" - if uint(i) == players.current { - symbol = "*" - } - resp += fmt.Sprintf("%0"+strconv.Itoa(pad)+"d%s: Name: %s; Playing: %t; PID: %d\n", i, symbol, p.fullName, p.playing, p.pid) + players.Refresh() + fmt.Println(players.JSON()) + } + } else if command == "next" { + players.Next() + } else if command == "prev" { + players.Prev() + } else if command == "toggle" { + players.Toggle() + } else if command == "list" { + resp := "" + pad := 0 + i := len(players.list) + for i != 0 { + i /= 10 + pad++ + } + for i, p := range players.list { + symbol := "" + if uint(i) == players.current { + symbol = "*" } - con.Write([]byte(resp)) - } else { - fmt.Println("Invalid command") + resp += fmt.Sprintf("%0"+strconv.Itoa(pad)+"d%s: Name: %s; Playing: %t; PID: %d\n", i, symbol, p.fullName, p.playing, p.pid) } + con.Write([]byte(resp)) + } else { + fmt.Println("Invalid command") } - }() + } + }() - conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, MATCH_NOC) - conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, MATCH_PC) - if SHOW_POS { - go func() { - for { - time.Sleep(1000 * time.Millisecond) - if players.list[players.current].playing { - go fmt.Println(players.JSON()) - } + conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, MATCH_NOC) + conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, MATCH_PC) + if SHOW_POS { + go func() { + for { + time.Sleep(1000 * time.Millisecond) + if players.list[players.current].playing { + go fmt.Println(players.JSON()) } - }() - } - c := make(chan *dbus.Signal, 10) - conn.Signal(c) - for v := range c { - // fmt.Printf("SIGNAL: Sender %s, Path %s, Name %s, Body %s\n", v.Sender, v.Path, v.Name, v.Body) - if strings.Contains(v.Name, "NameOwnerChanged") { - switch name := v.Body[0].(type) { - case string: - var pid uint32 - conn.BusObject().Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, name).Store(&pid) - // Ignore playerctld again - if strings.Contains(name, INTERFACE) && !strings.Contains(name, "playerctld") { - if pid == 0 { - // fmt.Println("Removing", name) - players.Remove(name) - } else { - // fmt.Println("Adding", name) - players.New(name) - } + } + }() + } + c := make(chan *dbus.Signal, 10) + conn.Signal(c) + for v := range c { + // fmt.Printf("SIGNAL: Sender %s, Path %s, Name %s, Body %s\n", v.Sender, v.Path, v.Name, v.Body) + if strings.Contains(v.Name, "NameOwnerChanged") { + switch name := v.Body[0].(type) { + case string: + var pid uint32 + conn.BusObject().Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, name).Store(&pid) + // Ignore playerctld again + if strings.Contains(name, INTERFACE) && !strings.Contains(name, "playerctld") { + if pid == 0 { + // fmt.Println("Removing", name) + players.Remove(name) + } else { + // fmt.Println("Adding", name) + players.New(name) } } - } else if strings.Contains(v.Name, "PropertiesChanged") && strings.Contains(v.Body[0].(string), INTERFACE+".Player") { - players.Refresh() - if AUTOFOCUS { - players.Sort() - } - if l := players.JSON(); l != lastLine { - lastLine = l - fmt.Println(l) - } + } + } else if strings.Contains(v.Name, "PropertiesChanged") && strings.Contains(v.Body[0].(string), INTERFACE+".Player") { + players.Refresh() + if AUTOFOCUS { + players.Sort() + } + if l := players.JSON(); l != lastLine { + lastLine = l + fmt.Println(l) } } } diff --git a/waybar-mpris b/waybar-mpris index 69c4d88..9f338fc 100755 Binary files a/waybar-mpris and b/waybar-mpris differ -- cgit v1.2.3