Compare commits
No commits in common. "a2164b0173c5ef052ec7758ab4ddb4e301fd9c0d" and "16465c44a2d76879d3fd08ef2de701ee6151bb31" have entirely different histories.
a2164b0173
...
16465c44a2
5
go.mod
5
go.mod
@ -3,9 +3,12 @@ module git.hrfee.pw/hrfee/waybar-mpris
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/fsnotify/fsnotify v1.4.9
|
github.com/fsnotify/fsnotify v1.4.9 // indirect
|
||||||
github.com/godbus/dbus/v5 v5.0.3
|
github.com/godbus/dbus/v5 v5.0.3
|
||||||
|
github.com/hpcloud/tail v1.0.0
|
||||||
github.com/hrfee/mpris2client v0.0.5
|
github.com/hrfee/mpris2client v0.0.5
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 // indirect
|
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 // indirect
|
||||||
|
gopkg.in/fsnotify.v1 v1.4.7 // indirect
|
||||||
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
||||||
)
|
)
|
||||||
|
18
go.sum
18
go.sum
@ -2,10 +2,28 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo
|
|||||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||||
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
|
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
|
||||||
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
|
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||||
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
|
github.com/hrfee/mpris2client v0.0.0-20210108004725-d2a36745cc4a h1:3H1awMdF3eJBCg0QPpDtNW+ujQPvUCuLsqlMRoODMA4=
|
||||||
|
github.com/hrfee/mpris2client v0.0.0-20210108004725-d2a36745cc4a/go.mod h1:tVpzzAlsljmQevNA4mJwUy1onUUiaQcRAa4gdl37okY=
|
||||||
|
github.com/hrfee/mpris2client v0.0.0-20210115213010-244c4cd6a569 h1:aCHzdXajnRUeaaW9FHzunrjGcRqmyFedwO0ODJRAdBk=
|
||||||
|
github.com/hrfee/mpris2client v0.0.0-20210115213010-244c4cd6a569/go.mod h1:tVpzzAlsljmQevNA4mJwUy1onUUiaQcRAa4gdl37okY=
|
||||||
|
github.com/hrfee/mpris2client v0.0.2 h1:AqsDoJCKyKjSGwVk3sJ5fjx+7M0tsSueZbVUIDxS1xI=
|
||||||
|
github.com/hrfee/mpris2client v0.0.2/go.mod h1:tVpzzAlsljmQevNA4mJwUy1onUUiaQcRAa4gdl37okY=
|
||||||
|
github.com/hrfee/mpris2client v0.0.4 h1:2WnCkTWGBr1eg3qHFjxDo7hIeCeQWh3jJ0hFJSts9Ks=
|
||||||
|
github.com/hrfee/mpris2client v0.0.4/go.mod h1:tVpzzAlsljmQevNA4mJwUy1onUUiaQcRAa4gdl37okY=
|
||||||
github.com/hrfee/mpris2client v0.0.5 h1:h3gcb8Q68i6SfcEgOxL9cXcGee9ekvxXltrUxGwRLDU=
|
github.com/hrfee/mpris2client v0.0.5 h1:h3gcb8Q68i6SfcEgOxL9cXcGee9ekvxXltrUxGwRLDU=
|
||||||
github.com/hrfee/mpris2client v0.0.5/go.mod h1:tVpzzAlsljmQevNA4mJwUy1onUUiaQcRAa4gdl37okY=
|
github.com/hrfee/mpris2client v0.0.5/go.mod h1:tVpzzAlsljmQevNA4mJwUy1onUUiaQcRAa4gdl37okY=
|
||||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200928205150-006507a75852 h1:sXxgOAXy8JwHhZnPuItAlUtwIlxrlEqi28mKhUR+zZY=
|
||||||
|
golang.org/x/sys v0.0.0-20200928205150-006507a75852/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201116194326-cc9327a14d48 h1:AYCWBZhgIw6XobZ5CibNJr0Rc4ZofGGKvWa1vcx2IGk=
|
||||||
|
golang.org/x/sys v0.0.0-20201116194326-cc9327a14d48/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 h1:nVuTkr9L6Bq62qpUqKo/RnZCFfzDBL0bYo6w9OJUqZY=
|
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 h1:nVuTkr9L6Bq62qpUqKo/RnZCFfzDBL0bYo6w9OJUqZY=
|
||||||
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||||
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||||
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
|
253
main.go
253
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -10,8 +11,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
|
||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
|
"github.com/hpcloud/tail"
|
||||||
mpris2 "github.com/hrfee/mpris2client"
|
mpris2 "github.com/hrfee/mpris2client"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
@ -36,19 +37,17 @@ var (
|
|||||||
SHOW_POS = false
|
SHOW_POS = false
|
||||||
INTERPOLATE = false
|
INTERPOLATE = false
|
||||||
REPLACE = false
|
REPLACE = false
|
||||||
isSharing = false
|
|
||||||
WRITER io.Writer = os.Stdout
|
WRITER io.Writer = os.Stdout
|
||||||
)
|
)
|
||||||
|
|
||||||
// JSON returns json for waybar to consume.
|
// JSON returns json for waybar to consume.
|
||||||
func playerJSON(p *mpris2.Player) string {
|
func playerJSON(p *mpris2.Player) string {
|
||||||
|
data := map[string]string{}
|
||||||
symbol := PLAY
|
symbol := PLAY
|
||||||
out := "{\"class\": \""
|
data["class"] = "paused"
|
||||||
if p.Playing {
|
if p.Playing {
|
||||||
symbol = PAUSE
|
symbol = PAUSE
|
||||||
out += "playing"
|
data["class"] = "playing"
|
||||||
} else {
|
|
||||||
out += "paused"
|
|
||||||
}
|
}
|
||||||
var pos string
|
var pos string
|
||||||
if SHOW_POS {
|
if SHOW_POS {
|
||||||
@ -96,23 +95,21 @@ func playerJSON(p *mpris2.Player) string {
|
|||||||
}
|
}
|
||||||
text += v + right
|
text += v + right
|
||||||
}
|
}
|
||||||
out += "\",\"text\":\"" + text + "\","
|
|
||||||
out += "\"tooltip\":\"" + fmt.Sprintf(
|
data["tooltip"] = fmt.Sprintf(
|
||||||
"%s\\nby %s\\n",
|
"%s\nby %s\n",
|
||||||
strings.ReplaceAll(p.Title, "&", "&"),
|
strings.ReplaceAll(p.Title, "&", "&"),
|
||||||
strings.ReplaceAll(p.Artist, "&", "&"),
|
strings.ReplaceAll(p.Artist, "&", "&"))
|
||||||
)
|
|
||||||
if p.Album != "" {
|
if p.Album != "" {
|
||||||
out += "from " + strings.ReplaceAll(p.Album, "&", "&") + "\\n"
|
data["tooltip"] += "from " + strings.ReplaceAll(p.Album, "&", "&") + "\n"
|
||||||
}
|
}
|
||||||
out += "(" + p.Name + ")\"}"
|
data["tooltip"] += "(" + p.Name + ")"
|
||||||
return out
|
data["text"] = text
|
||||||
// return fmt.Sprintf("{\"class\":\"%s\",\"text\":\"%s\",\"tooltip\":\"%s\"}", data["class"], data["text"], data["tooltip"])
|
out, err := json.Marshal(data)
|
||||||
// out, err := json.Marshal(data)
|
if err != nil {
|
||||||
// if err != nil {
|
return "{}"
|
||||||
// return "{}"
|
}
|
||||||
// }
|
return string(out)
|
||||||
// return string(out)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type players struct {
|
type players struct {
|
||||||
@ -132,17 +129,38 @@ func (pl *players) Prev() { pl.mpris2.List[pl.mpris2.Current].Previous() }
|
|||||||
|
|
||||||
func (pl *players) Toggle() { pl.mpris2.List[pl.mpris2.Current].Toggle() }
|
func (pl *players) Toggle() { pl.mpris2.List[pl.mpris2.Current].Toggle() }
|
||||||
|
|
||||||
func execCommand(cmd string) {
|
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)
|
||||||
|
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.")
|
||||||
|
flag.StringVar(&ORDER, "order", ORDER, "Element order.")
|
||||||
|
flag.BoolVar(&AUTOFOCUS, "autofocus", AUTOFOCUS, "Auto switch to currently playing music players.")
|
||||||
|
flag.BoolVar(&SHOW_POS, "position", SHOW_POS, "Show current position between brackets, e.g (04:50/05:00)")
|
||||||
|
flag.BoolVar(&INTERPOLATE, "interpolate", INTERPOLATE, "Interpolate track position (helpful for players that don't update regularly, e.g mpDris2)")
|
||||||
|
flag.BoolVar(&REPLACE, "replace", REPLACE, "replace existing waybar-mpris if found. When false, new instance will clone the original instances output.")
|
||||||
|
var command string
|
||||||
|
flag.StringVar(&command, "send", "", "send command to already runnning waybar-mpris instance. (options: "+strings.Join(COMMANDS, "/")+")")
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
os.Stderr = logfile
|
||||||
|
|
||||||
|
if command != "" {
|
||||||
conn, err := net.Dial("unix", SOCK)
|
conn, err := net.Dial("unix", SOCK)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Couldn't dial:", err)
|
log.Fatalln("Couldn't dial:", err)
|
||||||
}
|
}
|
||||||
_, err = conn.Write([]byte(cmd))
|
_, err = conn.Write([]byte(command))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Couldn't send command")
|
log.Fatalln("Couldn't send command")
|
||||||
}
|
}
|
||||||
fmt.Println("Sent.")
|
fmt.Println("Sent.")
|
||||||
if cmd == "list" {
|
if command == "list" {
|
||||||
buf := make([]byte, 512)
|
buf := make([]byte, 512)
|
||||||
nr, err := conn.Read(buf)
|
nr, err := conn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -154,73 +172,70 @@ func execCommand(cmd string) {
|
|||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
// fmt.Println("New array", players)
|
||||||
func duplicateOutput(conn net.Conn) {
|
// Start command listener
|
||||||
|
if _, err := os.Stat(SOCK); err == nil {
|
||||||
|
if REPLACE {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
} else if conn, err := net.Dial("unix", SOCK); err == nil {
|
||||||
|
// When waybar-mpris is already running, we attach to its output instead of launching a whole new instance.
|
||||||
// Print to stderr to avoid errors from waybar
|
// Print to stderr to avoid errors from waybar
|
||||||
os.Stderr.WriteString("waybar-mpris is already running. This instance will clone its output.")
|
os.Stderr.WriteString("waybar-mpris is already running. This instance will clone its output.")
|
||||||
// Tell other instance to share output in OUTFILE
|
|
||||||
_, err := conn.Write([]byte("share"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Couldn't send command: %v", err)
|
log.Fatalln("Couldn't dial:", err)
|
||||||
|
}
|
||||||
|
_, err = conn.Write([]byte("share"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Couldn't send command")
|
||||||
}
|
}
|
||||||
buf := make([]byte, 512)
|
buf := make([]byte, 512)
|
||||||
nr, err := conn.Read(buf)
|
nr, err := conn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Couldn't read response: %v", err)
|
log.Fatalln("Couldn't read response.")
|
||||||
}
|
}
|
||||||
if resp := string(buf[0:nr]); resp == "success" {
|
if string(buf[0:nr]) == "success" {
|
||||||
// t, err := tail.TailFile(OUTFILE, tail.Config{
|
t, err := tail.TailFile(OUTFILE, tail.Config{
|
||||||
// Follow: true,
|
Follow: true,
|
||||||
// MustExist: true,
|
MustExist: true,
|
||||||
// Logger: tail.DiscardingLogger,
|
Logger: tail.DiscardingLogger,
|
||||||
// })
|
})
|
||||||
// if err == nil {
|
if err == nil {
|
||||||
// for line := range t.Lines {
|
for line := range t.Lines {
|
||||||
// fmt.Println(line.Text)
|
fmt.Println(line.Text)
|
||||||
// }
|
|
||||||
// }
|
|
||||||
f, err := os.Open(OUTFILE)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to open \"%s\": %v", OUTFILE, err)
|
|
||||||
}
|
}
|
||||||
watcher, err := fsnotify.NewWatcher()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to start watcher: %v", err)
|
|
||||||
}
|
}
|
||||||
defer watcher.Close()
|
|
||||||
err = watcher.Add(OUTFILE)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to watch file: %v", err)
|
|
||||||
}
|
}
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case event, ok := <-watcher.Events:
|
|
||||||
if !ok {
|
|
||||||
log.Printf("Watcher failed: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
|
||||||
l, err := io.ReadAll(f)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to read file: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
str := string(l)
|
|
||||||
// Trim extra newline is necessary
|
|
||||||
if str[len(str)-2:] == "\n\n" {
|
|
||||||
fmt.Print(str[:len(str)-1])
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Print(str)
|
os.Remove(SOCK)
|
||||||
}
|
os.Remove(OUTFILE)
|
||||||
f.Seek(0, 0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
conn, err := dbus.SessionBus()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Error connecting to DBus:", err)
|
||||||
}
|
}
|
||||||
|
players := &players{
|
||||||
|
mpris2: mpris2.NewMpris2(conn, INTERPOLATE, POLL, AUTOFOCUS),
|
||||||
}
|
}
|
||||||
|
players.mpris2.Reload()
|
||||||
}
|
players.mpris2.Sort()
|
||||||
|
lastLine := ""
|
||||||
func listenForCommands(players *players) {
|
go func() {
|
||||||
listener, err := net.Listen("unix", SOCK)
|
listener, err := net.Listen("unix", SOCK)
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, os.Interrupt)
|
signal.Notify(c, os.Interrupt)
|
||||||
@ -280,101 +295,17 @@ func listenForCommands(players *players) {
|
|||||||
case "list":
|
case "list":
|
||||||
con.Write([]byte(players.mpris2.String()))
|
con.Write([]byte(players.mpris2.String()))
|
||||||
case "share":
|
case "share":
|
||||||
if !isSharing {
|
out, err := os.Create(OUTFILE)
|
||||||
f, err := os.OpenFile(OUTFILE, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
|
||||||
defer f.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(con, "Failed: %v", err)
|
con.Write([]byte(fmt.Sprintf("Failed: %s", err)))
|
||||||
}
|
}
|
||||||
var out io.Writer = emptyEveryWrite{file: f}
|
|
||||||
WRITER = io.MultiWriter(os.Stdout, out)
|
WRITER = io.MultiWriter(os.Stdout, out)
|
||||||
isSharing = true
|
con.Write([]byte("success"))
|
||||||
}
|
|
||||||
fmt.Fprint(con, "success")
|
|
||||||
default:
|
default:
|
||||||
fmt.Println("Invalid command")
|
fmt.Println("Invalid command")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
type emptyEveryWrite struct {
|
|
||||||
file *os.File
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w emptyEveryWrite) Write(p []byte) (n int, err error) {
|
|
||||||
offset, err := w.file.Seek(0, 0)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return w.file.WriteAt(p, offset)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
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.")
|
|
||||||
flag.StringVar(&ORDER, "order", ORDER, "Element order.")
|
|
||||||
flag.BoolVar(&AUTOFOCUS, "autofocus", AUTOFOCUS, "Auto switch to currently playing music players.")
|
|
||||||
flag.BoolVar(&SHOW_POS, "position", SHOW_POS, "Show current position between brackets, e.g (04:50/05:00)")
|
|
||||||
flag.BoolVar(&INTERPOLATE, "interpolate", INTERPOLATE, "Interpolate track position (helpful for players that don't update regularly, e.g mpDris2)")
|
|
||||||
flag.BoolVar(&REPLACE, "replace", REPLACE, "replace existing waybar-mpris if found. When false, new instance will clone the original instances output.")
|
|
||||||
var command string
|
|
||||||
flag.StringVar(&command, "send", "", "send command to already runnning waybar-mpris instance. (options: "+strings.Join(COMMANDS, "/")+")")
|
|
||||||
|
|
||||||
flag.Parse()
|
|
||||||
os.Stderr = logfile
|
|
||||||
|
|
||||||
if command != "" {
|
|
||||||
execCommand(command)
|
|
||||||
}
|
|
||||||
// fmt.Println("New array", players)
|
|
||||||
// Start command listener
|
|
||||||
if _, err := os.Stat(SOCK); err == nil {
|
|
||||||
if REPLACE {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
} else if conn, err := net.Dial("unix", SOCK); err == nil {
|
|
||||||
// When waybar-mpris is already running, we attach to its output instead of launching a whole new instance.
|
|
||||||
duplicateOutput(conn)
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
os.Stdout.WriteString("Couldn't dial socket, deleting instead: " + err.Error())
|
|
||||||
}
|
|
||||||
os.Remove(SOCK)
|
|
||||||
os.Remove(OUTFILE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
conn, err := dbus.SessionBus()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Error connecting to DBus:", err)
|
|
||||||
}
|
|
||||||
players := &players{
|
|
||||||
mpris2: mpris2.NewMpris2(conn, INTERPOLATE, POLL, AUTOFOCUS),
|
|
||||||
}
|
|
||||||
players.mpris2.Reload()
|
|
||||||
players.mpris2.Sort()
|
|
||||||
lastLine := ""
|
|
||||||
go listenForCommands(players)
|
|
||||||
go players.mpris2.Listen()
|
go players.mpris2.Listen()
|
||||||
if SHOW_POS {
|
if SHOW_POS {
|
||||||
go func() {
|
go func() {
|
||||||
|
BIN
waybar-mpris
BIN
waybar-mpris
Binary file not shown.
Loading…
Reference in New Issue
Block a user