Compare commits

..

No commits in common. "6ed1eb428f2941b6a5ea603781f6406c221a8087" and "8fb822aa919e119f3e167faa4f7ce026cc917e1b" have entirely different histories.

2 changed files with 108 additions and 144 deletions

62
main.go
View File

@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
@ -45,7 +44,6 @@ const (
// PropertiesChanged
MATCH_PC = "type='signal',path='/org/mpris/MediaPlayer2',interface='org.freedesktop.DBus.Properties'"
SOCK = "/tmp/waybar-mpris.sock"
LOGFILE = "/tmp/waybar-mpris.log"
)
var (
@ -66,21 +64,21 @@ func NewPlayer(conn *dbus.Conn, name string) (p *Player) {
for key, val := range knownPlayers {
if strings.Contains(name, key) {
playerName = val
break
}
}
if playerName == "Browser" {
if val == "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
for k, v := range knownBrowsers {
if strings.Contains(cmd, k) {
playerName = v
break
}
}
}
}
break
}
}
p = &Player{
player: conn.Object(name, PATH),
conn: conn,
@ -354,13 +352,6 @@ func (pl *PlayerList) Toggle() {
}
func main() {
logfile, err := os.OpenFile(LOGFILE, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Fatalf("Couldn't open %s for writing: %s", LOGFILE, err)
}
mw := io.MultiWriter(logfile, os.Stdout)
log.SetOutput(mw)
os.Stderr = logfile
flag.StringVar(&PLAY, "play", PLAY, "Play symbol/text to use.")
flag.StringVar(&PAUSE, "pause", PAUSE, "Pause symbol/text to use.")
flag.StringVar(&SEP, "separator", SEP, "Separator string to use between artist, album, and title.")
@ -391,11 +382,10 @@ func main() {
fmt.Println("Response:")
fmt.Printf(response)
}
os.Exit(0)
}
} else {
conn, err := dbus.SessionBus()
if err != nil {
log.Fatalln("Error connecting to DBus:", err)
panic(err)
}
players := &PlayerList{
conn: conn,
@ -407,34 +397,13 @@ func main() {
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)
}
}
go func() {
listener, err := net.Listen("unix", SOCK)
if err != nil {
log.Fatalln("Couldn't establish socket connection at", SOCK)
}
defer func() {
listener.Close()
os.Remove(SOCK)
}()
defer listener.Close()
for {
con, err := listener.Accept()
if err != nil {
@ -449,27 +418,21 @@ func main() {
}
command := string(buf[0:nr])
if command == "player-next" {
length := len(players.list)
if length != 1 {
if players.current < uint(length-1) {
if players.current < uint(len(players.list)-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.current = uint(len(players.list) - 1)
}
players.Refresh()
fmt.Println(players.JSON())
}
} else if command == "next" {
players.Next()
} else if command == "prev" {
@ -542,3 +505,4 @@ func main() {
}
}
}
}

Binary file not shown.