ask before removing existing socket

automatically removes the socket after 5 seconds of no input so that its
functions headlessly.
This commit is contained in:
Harvey Tindall 2020-08-31 20:14:14 +01:00
parent 3918d2f30e
commit f4090c1f07
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
2 changed files with 150 additions and 129 deletions

43
main.go
View File

@ -64,21 +64,21 @@ func NewPlayer(conn *dbus.Conn, name string) (p *Player) {
for key, val := range knownPlayers { for key, val := range knownPlayers {
if strings.Contains(name, key) { if strings.Contains(name, key) {
playerName = val playerName = val
if val == "Browser" { break
}
}
if playerName == "Browser" {
file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid)) file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
if err == nil { if err == nil {
cmd := string(file) cmd := string(file)
for k, v := range knownBrowsers { for key, val := range knownBrowsers {
if strings.Contains(cmd, k) { if strings.Contains(cmd, key) {
playerName = v playerName = val
break break
} }
} }
} }
} }
break
}
}
p = &Player{ p = &Player{
player: conn.Object(name, PATH), player: conn.Object(name, PATH),
conn: conn, conn: conn,
@ -382,10 +382,11 @@ func main() {
fmt.Println("Response:") fmt.Println("Response:")
fmt.Printf(response) fmt.Printf(response)
} }
} else { os.Exit(0)
}
conn, err := dbus.SessionBus() conn, err := dbus.SessionBus()
if err != nil { if err != nil {
panic(err) log.Fatalln("Error connecting to DBus:", err)
} }
players := &PlayerList{ players := &PlayerList{
conn: conn, conn: conn,
@ -397,13 +398,34 @@ func main() {
lastLine := "" lastLine := ""
// fmt.Println("New array", players) // fmt.Println("New array", players)
// Start command listener // 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() { go func() {
fmt.Scanln(&input)
if strings.Contains(input, "y") && !ignoreChoice {
os.Remove(SOCK) os.Remove(SOCK)
}
}()
time.Sleep(5 * time.Second)
if input == "" {
fmt.Printf("\nRemoving due to lack of input.\n")
ignoreChoice = true
os.Remove(SOCK)
}
}
go func() {
listener, err := net.Listen("unix", SOCK) listener, err := net.Listen("unix", SOCK)
if err != nil { if err != nil {
log.Fatalln("Couldn't establish socket connection at", SOCK) log.Fatalln("Couldn't establish socket connection at", SOCK)
} }
defer listener.Close() defer func() {
listener.Close()
os.Remove(SOCK)
}()
for { for {
con, err := listener.Accept() con, err := listener.Accept()
if err != nil { if err != nil {
@ -510,5 +532,4 @@ func main() {
} }
} }
} }
}
} }

Binary file not shown.