Compare commits
3 Commits
8fb822aa91
...
6ed1eb428f
Author | SHA1 | Date | |
---|---|---|---|
6ed1eb428f | |||
f4090c1f07 | |||
3918d2f30e |
258
main.go
258
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,19 +66,19 @@ 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
|
||||||
file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
|
}
|
||||||
if err == nil {
|
}
|
||||||
cmd := string(file)
|
if playerName == "Browser" {
|
||||||
for k, v := range knownBrowsers {
|
file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
|
||||||
if strings.Contains(cmd, k) {
|
if err == nil {
|
||||||
playerName = v
|
cmd := string(file)
|
||||||
break
|
for key, val := range knownBrowsers {
|
||||||
}
|
if strings.Contains(cmd, key) {
|
||||||
}
|
playerName = val
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p = &Player{
|
p = &Player{
|
||||||
@ -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,126 +391,153 @@ func main() {
|
|||||||
fmt.Println("Response:")
|
fmt.Println("Response:")
|
||||||
fmt.Printf(response)
|
fmt.Printf(response)
|
||||||
}
|
}
|
||||||
} else {
|
os.Exit(0)
|
||||||
conn, err := dbus.SessionBus()
|
}
|
||||||
if err != nil {
|
conn, err := dbus.SessionBus()
|
||||||
panic(err)
|
if err != nil {
|
||||||
}
|
log.Fatalln("Error connecting to DBus:", err)
|
||||||
players := &PlayerList{
|
}
|
||||||
conn: conn,
|
players := &PlayerList{
|
||||||
}
|
conn: conn,
|
||||||
players.Reload()
|
}
|
||||||
players.Sort()
|
players.Reload()
|
||||||
players.Refresh()
|
players.Sort()
|
||||||
fmt.Println(players.JSON())
|
players.Refresh()
|
||||||
lastLine := ""
|
fmt.Println(players.JSON())
|
||||||
// fmt.Println("New array", players)
|
lastLine := ""
|
||||||
// Start command listener
|
// 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() {
|
go func() {
|
||||||
os.Remove(SOCK)
|
fmt.Scanln(&input)
|
||||||
listener, err := net.Listen("unix", SOCK)
|
if strings.Contains(input, "y") && !ignoreChoice {
|
||||||
if err != nil {
|
os.Remove(SOCK)
|
||||||
log.Fatalln("Couldn't establish socket connection at", SOCK)
|
|
||||||
}
|
}
|
||||||
defer listener.Close()
|
}()
|
||||||
for {
|
time.Sleep(5 * time.Second)
|
||||||
con, err := listener.Accept()
|
if input == "" {
|
||||||
if err != nil {
|
fmt.Printf("\nRemoving due to lack of input.\n")
|
||||||
log.Println("Couldn't accept:", err)
|
ignoreChoice = true
|
||||||
continue
|
os.Remove(SOCK)
|
||||||
}
|
}
|
||||||
buf := make([]byte, 512)
|
|
||||||
nr, err := con.Read(buf)
|
}
|
||||||
if err != nil {
|
go func() {
|
||||||
log.Println("Couldn't read:", err)
|
listener, err := net.Listen("unix", SOCK)
|
||||||
continue
|
if err != nil {
|
||||||
}
|
log.Fatalln("Couldn't establish socket connection at", SOCK)
|
||||||
command := string(buf[0:nr])
|
}
|
||||||
if command == "player-next" {
|
defer func() {
|
||||||
if players.current < uint(len(players.list)-1) {
|
listener.Close()
|
||||||
|
os.Remove(SOCK)
|
||||||
|
}()
|
||||||
|
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
|
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" {
|
}
|
||||||
players.Next()
|
} else if command == "next" {
|
||||||
} else if command == "prev" {
|
players.Next()
|
||||||
players.Prev()
|
} else if command == "prev" {
|
||||||
} else if command == "toggle" {
|
players.Prev()
|
||||||
players.Toggle()
|
} else if command == "toggle" {
|
||||||
} else if command == "list" {
|
players.Toggle()
|
||||||
resp := ""
|
} else if command == "list" {
|
||||||
pad := 0
|
resp := ""
|
||||||
i := len(players.list)
|
pad := 0
|
||||||
for i != 0 {
|
i := len(players.list)
|
||||||
i /= 10
|
for i != 0 {
|
||||||
pad++
|
i /= 10
|
||||||
|
pad++
|
||||||
|
}
|
||||||
|
for i, p := range players.list {
|
||||||
|
symbol := ""
|
||||||
|
if uint(i) == players.current {
|
||||||
|
symbol = "*"
|
||||||
}
|
}
|
||||||
for i, p := range players.list {
|
resp += fmt.Sprintf("%0"+strconv.Itoa(pad)+"d%s: Name: %s; Playing: %t; PID: %d\n", i, symbol, p.fullName, p.playing, p.pid)
|
||||||
symbol := ""
|
}
|
||||||
if uint(i) == players.current {
|
con.Write([]byte(resp))
|
||||||
symbol = "*"
|
} 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)
|
c := make(chan *dbus.Signal, 10)
|
||||||
conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, MATCH_PC)
|
conn.Signal(c)
|
||||||
if SHOW_POS {
|
for v := range c {
|
||||||
go func() {
|
// fmt.Printf("SIGNAL: Sender %s, Path %s, Name %s, Body %s\n", v.Sender, v.Path, v.Name, v.Body)
|
||||||
for {
|
if strings.Contains(v.Name, "NameOwnerChanged") {
|
||||||
time.Sleep(1000 * time.Millisecond)
|
switch name := v.Body[0].(type) {
|
||||||
if players.list[players.current].playing {
|
case string:
|
||||||
go fmt.Println(players.JSON())
|
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") {
|
||||||
c := make(chan *dbus.Signal, 10)
|
players.Refresh()
|
||||||
conn.Signal(c)
|
if AUTOFOCUS {
|
||||||
for v := range c {
|
players.Sort()
|
||||||
// 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") {
|
if l := players.JSON(); l != lastLine {
|
||||||
switch name := v.Body[0].(type) {
|
lastLine = l
|
||||||
case string:
|
fmt.Println(l)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BIN
waybar-mpris
BIN
waybar-mpris
Binary file not shown.
Loading…
Reference in New Issue
Block a user