aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarvey Tindall <hrfee@protonmail.ch>2020-08-26 22:10:36 +0100
committerHarvey Tindall <hrfee@protonmail.ch>2020-08-26 22:10:36 +0100
commit8177e5dec9a272c607be90fb1bf02f7f0eb0b64e (patch)
tree69431b18d5d502fcbf24c3d3701e7d59aa8305f5
parent08ebaa5525e4ca01ac19b8c1ce0fa7e481986321 (diff)
downloadwaybar-mpris-8177e5dec9a272c607be90fb1bf02f7f0eb0b64e.tar.gz
waybar-mpris-8177e5dec9a272c607be90fb1bf02f7f0eb0b64e.zip
optionally display track position
with the --position flag, the position is grabbed every second and outputted. Nothing else is refreshed.
-rw-r--r--go.sum1
-rw-r--r--main.go57
-rwxr-xr-xwaybar-mprisbin950288 -> 952016 bytes
3 files changed, 54 insertions, 4 deletions
diff --git a/go.sum b/go.sum
index b9efde1..9ae06e1 100644
--- a/go.sum
+++ b/go.sum
@@ -1,4 +1,3 @@
-github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
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/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
diff --git a/main.go b/main.go
index 77b5da2..073366d 100644
--- a/main.go
+++ b/main.go
@@ -9,6 +9,7 @@ import (
"os"
"sort"
"strings"
+ "time"
"github.com/godbus/dbus/v5"
flag "github.com/spf13/pflag"
@@ -47,9 +48,10 @@ var (
PLAY = "▶"
PAUSE = ""
SEP = " - "
- ORDER = "SYMBOL:ARTIST:ALBUM:TITLE"
+ ORDER = "SYMBOL:ARTIST:ALBUM:TITLE:POSITION"
AUTOFOCUS = false
COMMANDS = []string{"player-next", "player-prev", "next", "prev", "toggle"}
+ SHOW_POS = false
)
// NewPlayer returns a new player object.
@@ -140,6 +142,31 @@ func (p *Player) Refresh() (err error) {
return nil
}
+func µsToString(µs int64) string {
+ seconds := int(µs / 1e6)
+ minutes := int(seconds / 60)
+ seconds -= minutes * 60
+ return fmt.Sprintf("%02d:%02d", minutes, seconds)
+}
+
+func (p *Player) Position() string {
+ // position is in microseconds so we prob need int64 to be safe
+ l := p.metadata["mpris:length"].Value().(int64)
+ length := µsToString(l)
+ if length == "" {
+ return ""
+ }
+ pos, err := p.player.GetProperty(INTERFACE + ".Player.Position")
+ if err != nil {
+ return ""
+ }
+ position := µsToString(pos.Value().(int64))
+ if position == "" {
+ return ""
+ }
+ return position + "/" + length
+}
+
func (p *Player) JSON() string {
data := map[string]string{}
symbol := PLAY
@@ -148,6 +175,13 @@ func (p *Player) JSON() string {
symbol = PAUSE
data["class"] = "playing"
}
+ var pos string
+ if SHOW_POS {
+ pos = p.Position()
+ if pos != "" {
+ pos = "(" + pos + ")"
+ }
+ }
var items []string
order := strings.Split(ORDER, ":")
for _, v := range order {
@@ -165,6 +199,10 @@ func (p *Player) JSON() string {
if p.title != "" {
items = append(items, p.title)
}
+ } else if v == "POSITION" && SHOW_POS {
+ if pos != "" {
+ items = append(items, pos)
+ }
}
}
if len(items) == 0 {
@@ -173,9 +211,9 @@ func (p *Player) JSON() string {
text := ""
for i, v := range items {
right := ""
- if v == symbol && i != len(items)-1 {
+ if (v == symbol || v == pos) && i != len(items)-1 {
right = " "
- } else if i != len(items)-1 && items[i+1] != symbol {
+ } else if i != len(items)-1 && items[i+1] != symbol && items[i+1] != pos {
right = SEP
} else {
right = " "
@@ -316,6 +354,7 @@ func main() {
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)")
var command string
flag.StringVar(&command, "send", "", "send command to already runnning waybar-mpris instance. (options: "+strings.Join(COMMANDS, "/")+")")
flag.Parse()
@@ -344,6 +383,7 @@ func main() {
fmt.Println(players.JSON())
lastLine := ""
// fmt.Println("New array", players)
+ // Start command listener
go func() {
os.Remove(SOCK)
listener, err := net.Listen("unix", SOCK)
@@ -391,8 +431,19 @@ func main() {
}
}
}()
+
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())
+ }
+ }
+ }()
+ }
c := make(chan *dbus.Signal, 10)
conn.Signal(c)
for v := range c {
diff --git a/waybar-mpris b/waybar-mpris
index e0b5ba9..09e062e 100755
--- a/waybar-mpris
+++ b/waybar-mpris
Binary files differ