Compare commits
3 Commits
8fb822aa91
...
6ed1eb428f
Author | SHA1 | Date | |
---|---|---|---|
6ed1eb428f | |||
f4090c1f07 | |||
3918d2f30e |
62
main.go
62
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -44,6 +45,7 @@ const (
|
|||||||
// PropertiesChanged
|
// PropertiesChanged
|
||||||
MATCH_PC = "type='signal',path='/org/mpris/MediaPlayer2',interface='org.freedesktop.DBus.Properties'"
|
MATCH_PC = "type='signal',path='/org/mpris/MediaPlayer2',interface='org.freedesktop.DBus.Properties'"
|
||||||
SOCK = "/tmp/waybar-mpris.sock"
|
SOCK = "/tmp/waybar-mpris.sock"
|
||||||
|
LOGFILE = "/tmp/waybar-mpris.log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -64,21 +66,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,
|
||||||
@ -352,6 +354,13 @@ func (pl *PlayerList) Toggle() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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(&PLAY, "play", PLAY, "Play symbol/text to use.")
|
||||||
flag.StringVar(&PAUSE, "pause", PAUSE, "Pause 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.")
|
flag.StringVar(&SEP, "separator", SEP, "Separator string to use between artist, album, and title.")
|
||||||
@ -382,10 +391,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 +407,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 {
|
||||||
@ -418,21 +449,27 @@ func main() {
|
|||||||
}
|
}
|
||||||
command := string(buf[0:nr])
|
command := string(buf[0:nr])
|
||||||
if command == "player-next" {
|
if command == "player-next" {
|
||||||
if players.current < uint(len(players.list)-1) {
|
length := len(players.list)
|
||||||
|
if length != 1 {
|
||||||
|
if players.current < uint(length-1) {
|
||||||
players.current += 1
|
players.current += 1
|
||||||
} else {
|
} else {
|
||||||
players.current = 0
|
players.current = 0
|
||||||
}
|
}
|
||||||
players.Refresh()
|
players.Refresh()
|
||||||
fmt.Println(players.JSON())
|
fmt.Println(players.JSON())
|
||||||
|
}
|
||||||
} else if command == "player-prev" {
|
} else if command == "player-prev" {
|
||||||
|
length := len(players.list)
|
||||||
|
if length != 1 {
|
||||||
if players.current != 0 {
|
if players.current != 0 {
|
||||||
players.current -= 1
|
players.current -= 1
|
||||||
} else {
|
} else {
|
||||||
players.current = uint(len(players.list) - 1)
|
players.current = uint(length - 1)
|
||||||
}
|
}
|
||||||
players.Refresh()
|
players.Refresh()
|
||||||
fmt.Println(players.JSON())
|
fmt.Println(players.JSON())
|
||||||
|
}
|
||||||
} else if command == "next" {
|
} else if command == "next" {
|
||||||
players.Next()
|
players.Next()
|
||||||
} else if command == "prev" {
|
} else if command == "prev" {
|
||||||
@ -505,4 +542,3 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
BIN
waybar-mpris
BIN
waybar-mpris
Binary file not shown.
Loading…
Reference in New Issue
Block a user