Compare commits

...

16 Commits
v0.1.0 ... main

Author SHA1 Message Date
Harvey Tindall 485ec0ec0a
escape double quotes
continuous-integration/drone/push Build is failing Details
2022-01-27 18:00:49 +00:00
Harvey Tindall 39f84a94ac
write connection refused error to stderr
continuous-integration/drone/push Build is passing Details
2022-01-24 14:30:38 +00:00
Harvey Tindall 4b71fa248a
Truncate file when directly sharing
continuous-integration/drone/push Build is passing Details
Fixes bug where recipient instance would have a bit of the previous
track data on the end of the output if the previous track data was
longer, which effectively froze the output on waybar as it was no longer
valid JSON.
2021-10-29 15:10:53 +01:00
Harvey Tindall c26c13e984
upload to buildrone before apt
continuous-integration/drone/push Build is passing Details
2021-06-25 01:57:29 +01:00
Harvey Tindall 174d243f15
fix apt versioning
continuous-integration/drone/push Build is failing Details
2021-05-31 23:19:31 +01:00
Harvey Tindall 6cab493189
mention repo in README
continuous-integration/drone/push Build is passing Details
2021-05-24 22:44:07 +01:00
Harvey Tindall 69be0dcf8a
fix typo
continuous-integration/drone/push Build is passing Details
2021-05-24 21:48:44 +01:00
Harvey Tindall 22ad0e29cb
make debs/rpms, upload .debs to repo
continuous-integration/drone/push Build is failing Details
2021-05-24 20:27:45 +01:00
Harvey Tindall 8340ef1f20
always return the expected number of bytes written
continuous-integration/drone/push Build is passing Details
emptyEveryWrite was returning the wrong number, so it ignores the real
value and just returns the expected value (len(bytes in)). For #4 again.
2021-05-24 14:51:21 +01:00
Harvey Tindall fe851278f4
change order of writers in io.Multiwriter
continuous-integration/drone/push Build is passing Details
io.MultiWriter (i think) thinks dataWrite.Write fails, and so doesn't
continue writing to all other outputs. This just makes it the last one,
should fix #4.
2021-05-21 16:03:33 +01:00
Harvey Tindall 49b3212c48
support multiple instances with different layouts
continuous-integration/drone/push Build is passing Details
if specified layout for nth instance is different than 1st, the player data is
    shared instead of the waybar output.
2021-05-17 16:10:14 +01:00
Harvey Tindall ea4f47a1b1
only share output when args are identical; use fixed length socket
commands

fixed length commands were originally a guess for an issue I was having,
but it's cleaner this way anyway.
2021-05-17 13:44:24 +01:00
Harvey Tindall 51c7a983a0
add PLAYER to --order, remove binary from repo
continuous-integration/drone/push Build is passing Details
also added more prominent download links to the readme.
2021-05-16 23:28:23 +01:00
Harvey Tindall 3aec82c40c
fix upload.py args for new version
continuous-integration/drone/push Build is passing Details
2021-04-24 18:03:03 +01:00
Harvey Tindall a2164b0173
build JSON string instead of json.Marshal-ing
continuous-integration/drone/push Build is failing Details
not that this was a problem, but the output is simple enough to
just create it by hand.
2021-04-24 16:37:44 +01:00
Harvey Tindall c235dd60dd
overwrite previous content when sharing output
this way /tmp/waybar-mpris.out won't get filled up.
2021-04-24 16:02:39 +01:00
17 changed files with 526 additions and 277 deletions

View File

@ -5,20 +5,29 @@ type: docker
steps:
- name: build
image: golang:latest
volumes:
- name: ssh_key
path: /id_rsa
commands:
- apt update -y
- apt install -y python3-pip curl
- go mod download
- curl -sL https://git.io/goreleaser > goreleaser.sh
- chmod +x goreleaser.sh
- ./goreleaser.sh --snapshot --skip-publish --rm-dist
- ./version.sh ./goreleaser.sh --snapshot --skip-publish --rm-dist
- wget https://builds.hrfee.pw/upload.py
- pip3 install requests
- bash -c 'python3 upload.py https://builds2.hrfee.pw hrfee waybar-mpris ./dist/*.tar.gz'
- bash -c 'python3 upload.py https://builds2.hrfee.pw hrfee waybar-mpris --upload ./dist/*.tar.gz ./dist/*.rpm'
- bash -c 'sftp -P 2022 -i /id_rsa -o StrictHostKeyChecking=no root@161.97.102.153:/repo/incoming <<< $"put dist/*.deb"'
- bash -c 'ssh -i /id_rsa root@161.97.102.153 -p 2022 "repo-process-deb trusty-unstable"'
environment:
BUILDRONE_KEY:
from_secret: BUILDRONE_KEY
volumes:
- name: ssh_key
host:
path: /root/.ssh/id_rsa_packaging
trigger:
branch:
- main

2
.gitignore vendored
View File

@ -1 +1,3 @@
./main
waybar-mpris
dist/*

View File

@ -8,7 +8,8 @@ before:
hooks:
- go mod download
builds:
- dir: ./
- id: main
dir: ./
env:
- CGO_ENABLED=0
goos:
@ -18,16 +19,37 @@ builds:
- arm
- arm64
archives:
- replacements:
- id: main
builds:
- main
replacements:
linux: Linux
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "git-{{.ShortCommit}}"
name_template: "0.0.0-{{.Env.NFPM_EPOCH}}"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
nfpms:
- id: main
file_name_template: '{{ .ProjectName }}-git-{{ .ShortCommit }}_{{ .Arch }}'
package_name: waybar-mpris
homepage: https://git.hrfee.pw/hrfee/waybar-mpris
description: MPRIS2 waybar component
maintainer: Harvey Tindall <hrfee@hrfee.dev>
license: MIT
vendor: hrfee.dev
version_metadata: git
builds:
- main
contents:
- src: ./LICENSE
dst: /usr/share/licenses/waybar-mpris
formats:
- deb
- rpm

View File

@ -3,6 +3,9 @@
<img src="images/cropped.gif" style="width: 100%;" alt="bar gif"></img>
</p>
##### Downloads:
##### [binary](https://builds2.hrfee.pw/view/hrfee/waybar-mpris) | [aur](https://aur.archlinux.org/packages/waybar-mpris-git/)
a waybar component/utility for displaying and controlling MPRIS2 compliant media players individually, inspired by [waybar-media](https://github.com/yurihs/waybar-media).
MPRIS2 is widely supported, so this component should work with:
@ -18,9 +21,18 @@ MPRIS2 is widely supported, so this component should work with:
## Install
Available on the AUR as [waybar-mpris-git](https://aur.archlinux.org/packages/waybar-mpris-git/) (Thanks @nichobi!)
Available on my Debian repo as well:
```sh
sudo apt-get update && sudo apt-get install curl apt-transport-https gnupg
curl https://apt.hrfee.dev/hrfee.pubkey.gpg | sudo apt-key add -
echo "deb https://apt.hrfee.dev trusty-unstable main" | sudo tee /etc/apt/sources.list.d/hrfee.list
sudo apt-get update
sudo apt-get install waybar-mpris
```
`go get git.hrfee.pw/hrfee/waybar-mpris` will compile from source and install.
You can also download a precompiled binaries from [here](https://builds2.hrfee.pw/view/hrfee/waybar-mpris).
You can also download a precompiled binary from [here](https://builds2.hrfee.pw/view/hrfee/waybar-mpris).
## Issues
Stick them on [mpris2client](https://github.com/hrfee/mpris2client) or the [og](https://github.com/hrfee/waybar-mpris) repository (both on github) if you can't make an account here.

3
dist/checksums.txt vendored
View File

@ -1,3 +0,0 @@
57ca953082ee67820539a128d6c285b1bc1e4e917e1376c86cbbc2436880330a waybar-mpris_git-750317a_Linux_x86_64.tar.gz
6e638c4c4e7b1aa324abbbbedda193600390988001c9d4c8d5ed4700f1f6ca0a waybar-mpris_git-750317a_Linux_armv6.tar.gz
c710c2cd4e4a3ed3c4d5e2b8f2a86077904c26365102496ea9b429e5ad2204a6 waybar-mpris_git-750317a_Linux_arm64.tar.gz

88
dist/config.yaml vendored
View File

@ -1,88 +0,0 @@
project_name: waybar-mpris
release:
github:
owner: hrfee
name: waybar-mpris
gitea:
owner: hrfee
name: waybar-mpris
name_template: v{{.Version}}
milestones:
- repo:
owner: hrfee
name: waybar-mpris
name_template: '{{ .Tag }}'
scoop:
name: waybar-mpris
commit_author:
name: goreleaserbot
email: goreleaser@carlosbecker.com
commit_msg_template: Scoop update for {{ .ProjectName }} version {{ .Tag }}
builds:
- id: waybar-mpris
goos:
- linux
goarch:
- amd64
- arm
- arm64
goarm:
- "6"
targets:
- linux_amd64
- linux_arm_6
- linux_arm64
dir: ./
main: .
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser
binary: waybar-mpris
env:
- CGO_ENABLED=0
lang: go
gobinary: go
archives:
- id: default
builds:
- waybar-mpris
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}'
replacements:
amd64: x86_64
linux: Linux
format: tar.gz
files:
- licence*
- LICENCE*
- license*
- LICENSE*
- readme*
- README*
- changelog*
- CHANGELOG*
allow_different_binary_count: false
snapshot:
name_template: git-{{.ShortCommit}}
checksum:
name_template: checksums.txt
algorithm: sha256
changelog:
filters:
exclude:
- '^docs:'
- '^test:'
sort: asc
dist: dist
env_files:
github_token: ~/.config/goreleaser/github_token
gitlab_token: ~/.config/goreleaser/gitlab_token
gitea_token: ~/.config/goreleaser/gitea_token
before:
hooks:
- go mod download
source:
name_template: '{{ .ProjectName }}-{{ .Version }}'
format: tar.gz
github_urls:
download: https://github.com
gitlab_urls:
download: https://gitlab.com

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

5
go.mod
View File

@ -3,12 +3,9 @@ module git.hrfee.pw/hrfee/waybar-mpris
go 1.15
require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fsnotify/fsnotify v1.4.9
github.com/godbus/dbus/v5 v5.0.3
github.com/hpcloud/tail v1.0.0
github.com/hrfee/mpris2client v0.0.5
github.com/spf13/pflag v1.0.5
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
View File

@ -2,28 +2,10 @@ 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/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
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/go.mod h1:tVpzzAlsljmQevNA4mJwUy1onUUiaQcRAa4gdl37okY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
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-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/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=

630
main.go
View File

@ -1,28 +1,29 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"strconv"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/godbus/dbus/v5"
"github.com/hpcloud/tail"
mpris2 "github.com/hrfee/mpris2client"
flag "github.com/spf13/pflag"
)
// Various paths and values to use elsewhere.
const (
SOCK = "/tmp/waybar-mpris.sock"
LOGFILE = "/tmp/waybar-mpris.log"
OUTFILE = "/tmp/waybar-mpris.out"
POLL = 1
SOCK = "/tmp/waybar-mpris.sock"
LOGFILE = "/tmp/waybar-mpris.log"
OUTFILE = "/tmp/waybar-mpris.out" // Used for sharing waybar output when args are the same.
DATAFILE = "/tmp/waybar-mpris.data.out" // Used for sharing "\n"-separated player data between instances when args are different.
POLL = 1
)
// Mostly default values for flag options.
@ -33,27 +34,144 @@ var (
ORDER = "SYMBOL:ARTIST:ALBUM:TITLE:POSITION"
AUTOFOCUS = false
// Available commands that can be sent to running instances.
COMMANDS = []string{"player-next", "player-prev", "next", "prev", "toggle", "list"}
SHOW_POS = false
INTERPOLATE = false
REPLACE = false
WRITER io.Writer = os.Stdout
COMMANDS = []string{"player-next", "player-prev", "next", "prev", "toggle", "list"}
SHOW_POS = false
INTERPOLATE = false
REPLACE = false
isSharing = false
isDataSharing = false
WRITER io.Writer = os.Stdout
SHAREWRITER, DATAWRITER io.Writer
)
const (
cPlayerNext = "pn"
cPlayerPrev = "pp"
cNext = "mn"
cPrev = "mp"
cToggle = "mt"
cList = "ls"
cShare = "sh"
cPreShare = "ps"
cDataShare = "ds"
rSuccess = "sc"
rInvalidCommand = "iv"
rFailed = "fa"
)
func stringToCmd(str string) string {
switch str {
case "player-next":
return cPlayerNext
case "player-prev":
return cPlayerPrev
case "next":
return cNext
case "prev":
return cPrev
case "toggle":
return cToggle
case "list":
return cList
case "share":
return cShare
case "data-share":
return cDataShare
case "pre-share":
return cPreShare
}
return ""
}
// length-µS\nposition-µS\nplaying (0 or 1)\nartist\nalbum\ntitle\nplayer\n
func fromData(p *player, cmd string) {
p.Duplicate = true
values := make([]string, 7)
prev := 0
current := 0
for i := range cmd {
if current == len(values) {
break
}
if cmd[i] == '\n' {
values[current] = cmd[prev:i]
prev = i + 1
current++
}
}
l, err := strconv.ParseInt(values[0], 10, 64)
if err != nil {
l = -1
}
p.Length = int(l) / 1000000
pos, err := strconv.ParseInt(values[1], 10, 64)
if err != nil {
pos = -1
}
p.Position = pos
if values[2] == "1" {
p.Playing = true
} else {
p.Playing = false
}
p.Artist = values[3]
p.Album = values[4]
p.Title = values[5]
p.Name = values[6]
}
func toData(p *player) (cmd string) {
cmd += strconv.FormatInt(int64(p.Length*1000000), 10) + "\n"
cmd += strconv.FormatInt(p.Position, 10) + "\n"
if p.Playing {
cmd += "1"
} else {
cmd += "0"
}
cmd += "\n"
cmd += p.Artist + "\n"
cmd += p.Album + "\n"
cmd += p.Title + "\n"
cmd += p.Name + "\n"
return
}
type player struct {
*mpris2.Player
Duplicate bool
}
func secondsToString(seconds int) string {
minutes := int(seconds / 60)
seconds -= int(minutes * 60)
return fmt.Sprintf("%02d:%02d", minutes, seconds)
}
// JSON returns json for waybar to consume.
func playerJSON(p *mpris2.Player) string {
data := map[string]string{}
func playerJSON(p *player) string {
artist := strings.ReplaceAll(p.Artist, "\"", "\\\"")
album := strings.ReplaceAll(p.Album, "\"", "\\\"")
title := strings.ReplaceAll(p.Title, "\"", "\\\"")
name := strings.ReplaceAll(p.Name, "\"", "\\\"")
symbol := PLAY
data["class"] = "paused"
out := "{\"class\": \""
if p.Playing {
symbol = PAUSE
data["class"] = "playing"
out += "playing"
} else {
out += "paused"
}
var pos string
if SHOW_POS {
pos = p.StringPosition()
if pos != "" {
pos = "(" + pos + ")"
if !p.Duplicate {
pos = p.StringPosition()
if pos != "" {
pos = "(" + pos + ")"
}
} else {
pos = "(" + secondsToString(int(p.Position/1000000)) + "/" + secondsToString(p.Length) + ")"
}
}
var items []string
@ -63,21 +181,25 @@ func playerJSON(p *mpris2.Player) string {
case "SYMBOL":
items = append(items, symbol)
case "ARTIST":
if p.Artist != "" {
items = append(items, p.Artist)
if artist != "" {
items = append(items, artist)
}
case "ALBUM":
if p.Album != "" {
items = append(items, p.Album)
if album != "" {
items = append(items, album)
}
case "TITLE":
if p.Title != "" {
items = append(items, p.Title)
if title != "" {
items = append(items, title)
}
case "POSITION":
if pos != "" && SHOW_POS {
items = append(items, pos)
}
case "PLAYER":
if name != "" {
items = append(items, name)
}
}
}
if len(items) == 0 {
@ -95,21 +217,22 @@ func playerJSON(p *mpris2.Player) string {
}
text += v + right
}
data["tooltip"] = fmt.Sprintf(
"%s\nby %s\n",
strings.ReplaceAll(p.Title, "&", "&amp;"),
strings.ReplaceAll(p.Artist, "&", "&amp;"))
if p.Album != "" {
data["tooltip"] += "from " + strings.ReplaceAll(p.Album, "&", "&amp;") + "\n"
out += "\",\"text\":\"" + text + "\","
out += "\"tooltip\":\"" + strings.ReplaceAll(title, "&", "&amp;") + "\\n"
if artist != "" {
out += "by " + strings.ReplaceAll(artist, "&", "&amp;") + "\\n"
}
data["tooltip"] += "(" + p.Name + ")"
data["text"] = text
out, err := json.Marshal(data)
if err != nil {
return "{}"
if album != "" {
out += "from " + strings.ReplaceAll(album, "&", "&amp;") + "\\n"
}
return string(out)
out += "(" + name + ")\"}"
return out
// return fmt.Sprintf("{\"class\":\"%s\",\"text\":\"%s\",\"tooltip\":\"%s\"}", data["class"], data["text"], data["tooltip"])
// out, err := json.Marshal(data)
// if err != nil {
// return "{}"
// }
// return string(out)
}
type players struct {
@ -118,7 +241,7 @@ type players struct {
func (pl *players) JSON() string {
if len(pl.mpris2.List) != 0 {
return playerJSON(pl.mpris2.List[pl.mpris2.Current])
return playerJSON(&player{pl.mpris2.List[pl.mpris2.Current], false})
}
return "{}"
}
@ -129,6 +252,312 @@ func (pl *players) Prev() { pl.mpris2.List[pl.mpris2.Current].Previous() }
func (pl *players) Toggle() { pl.mpris2.List[pl.mpris2.Current].Toggle() }
func execCommand(cmd string) {
conn, err := net.Dial("unix", SOCK)
if err != nil {
log.Fatalln("Couldn't dial:", err)
}
shortCmd := stringToCmd(cmd)
_, err = conn.Write([]byte(shortCmd))
if err != nil {
log.Fatalln("Couldn't send command")
}
fmt.Println("Sent.")
if cmd == "list" {
buf := make([]byte, 512)
nr, err := conn.Read(buf)
if err != nil {
log.Fatalln("Couldn't read response.")
}
response := string(buf[0:nr])
fmt.Println("Response:")
fmt.Printf(response)
}
os.Exit(0)
}
func duplicateOutput() error {
// Print to stderr to avoid errors from waybar
os.Stderr.WriteString("waybar-mpris is already running. This instance will clone its output.")
conn, err := net.Dial("unix", SOCK)
if err != nil {
return err
}
_, err = conn.Write([]byte(cPreShare))
if err != nil {
log.Fatalf("Couldn't send command: %v", err)
return err
}
buf := make([]byte, 512)
nr, err := conn.Read(buf)
if err != nil {
log.Fatalf("Couldn't read response: %v", err)
return err
}
argString := ""
for _, arg := range os.Args {
argString += arg + "|"
}
conn.Close()
conn, err = net.Dial("unix", SOCK)
if err != nil {
return err
}
if string(buf[0:nr]) == argString {
// Tell other instance to share output in OUTFILE
_, err := conn.Write([]byte(cShare))
if err != nil {
log.Fatalf("Couldn't send command: %v", err)
}
buf = make([]byte, 2)
nr, err := conn.Read(buf)
if err != nil {
log.Fatalf("Couldn't read response: %v", err)
}
if resp := string(buf[0:nr]); resp == rSuccess {
// t, err := tail.TailFile(OUTFILE, tail.Config{
// Follow: true,
// MustExist: true,
// Logger: tail.DiscardingLogger,
// })
// if err == nil {
// for line := range t.Lines {
// 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 err
}
if event.Op&fsnotify.Write == fsnotify.Write {
l, err := io.ReadAll(f)
if err != nil {
log.Printf("Failed to read file: %v", err)
return err
}
str := string(l)
// Trim extra newline is necessary
if str[len(str)-2:] == "\n\n" {
fmt.Print(str[:len(str)-1])
} else {
fmt.Print(str)
}
f.Seek(0, 0)
}
}
}
}
} else {
_, err := conn.Write([]byte(cDataShare))
if err != nil {
log.Fatalf("Couldn't send command: %v", err)
}
buf = make([]byte, 2)
nr, err := conn.Read(buf)
if err != nil {
log.Fatalf("Couldn't read response: %v", err)
}
if resp := string(buf[0:nr]); resp == rSuccess {
f, err := os.Open(DATAFILE)
if err != nil {
log.Fatalf("Failed to open \"%s\": %v", DATAFILE, err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Failed to start watcher: %v", err)
}
defer watcher.Close()
err = watcher.Add(DATAFILE)
if err != nil {
log.Fatalf("Failed to watch file: %v", err)
}
p := &player{
&mpris2.Player{},
true,
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
log.Printf("Watcher failed: %v", err)
return err
}
if event.Op&fsnotify.Write == fsnotify.Write {
l, err := io.ReadAll(f)
if err != nil {
log.Printf("Failed to read file: %v", err)
return err
}
str := string(l)
fromData(p, str)
fmt.Fprintln(WRITER, playerJSON(p))
f.Seek(0, 0)
}
}
}
}
}
return nil
}
func listenForCommands(players *players) {
listener, err := net.Listen("unix", SOCK)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
os.Remove(OUTFILE)
os.Remove(SOCK)
os.Exit(1)
}()
if err != nil {
log.Fatalf("Couldn't establish socket connection at %s (error %s)\n", SOCK, err)
}
defer func() {
listener.Close()
os.Remove(SOCK)
}()
for {
con, err := listener.Accept()
if err != nil {
log.Println("Couldn't accept:", err)
continue
}
buf := make([]byte, 2)
nr, err := con.Read(buf)
if err != nil {
log.Println("Couldn't read:", err)
continue
}
command := string(buf[0:nr])
switch command {
case cPlayerNext:
length := len(players.mpris2.List)
if length != 1 {
if players.mpris2.Current < uint(length-1) {
players.mpris2.Current++
} else {
players.mpris2.Current = 0
}
players.mpris2.Refresh()
}
case cPlayerPrev:
length := len(players.mpris2.List)
if length != 1 {
if players.mpris2.Current != 0 {
players.mpris2.Current--
} else {
players.mpris2.Current = uint(length - 1)
}
players.mpris2.Refresh()
}
case cNext:
players.Next()
case cPrev:
players.Prev()
case cToggle:
players.Toggle()
case cList:
con.Write([]byte(players.mpris2.String()))
case cDataShare:
if !isDataSharing {
f, err := os.OpenFile(DATAFILE, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
defer f.Close()
if err != nil {
fmt.Fprintf(con, "Failed: %v", err)
}
DATAWRITER = dataWrite{
emptyEveryWrite{file: f},
players,
}
if isSharing {
WRITER = io.MultiWriter(os.Stdout, SHAREWRITER, DATAWRITER)
} else {
WRITER = io.MultiWriter(os.Stdout, DATAWRITER)
}
isDataSharing = true
}
fmt.Fprint(con, rSuccess)
case cShare:
if !isSharing {
f, err := os.OpenFile(OUTFILE, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
defer f.Close()
if err != nil {
fmt.Fprintf(con, "Failed: %v", err)
}
SHAREWRITER = emptyEveryWrite{file: f}
if isDataSharing {
WRITER = io.MultiWriter(SHAREWRITER, DATAWRITER, os.Stdout)
} else {
WRITER = io.MultiWriter(SHAREWRITER, os.Stdout)
}
isSharing = true
}
fmt.Fprint(con, rSuccess)
/* Prior to sharing, the first instance sends its os.Args.
If the second instances args are different, the first sends the raw data (artist, album, etc.)
If they are the same, the first instance just sends its output and the second prints it. */
case cPreShare:
out := ""
for _, arg := range os.Args {
out += arg + "|"
}
con.Write([]byte(out))
default:
fmt.Println("Invalid command")
}
con.Close()
}
}
type dataWrite struct {
emptyEveryWrite
Players *players
}
func (w dataWrite) Write(p []byte) (n int, err error) {
line := toData(&player{w.Players.mpris2.List[w.Players.mpris2.Current], true})
_, err = w.emptyEveryWrite.Write([]byte(line))
n = len(p)
return
}
type emptyEveryWrite struct {
file *os.File
}
func (w emptyEveryWrite) Write(p []byte) (n int, err error) {
n = len(p)
// Set new size in case previous data was longer and would leave garbage at the end of the file.
err = w.file.Truncate(int64(n))
if err != nil {
return 0, err
}
offset, err := w.file.Seek(0, 0)
if err != nil {
return 0, err
}
_, err = w.file.WriteAt(p, offset)
return
}
func main() {
logfile, err := os.OpenFile(LOGFILE, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
@ -139,7 +568,7 @@ func main() {
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.StringVar(&ORDER, "order", ORDER, "Element order. An extra \"PLAYER\" element is also available.")
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)")
@ -151,26 +580,7 @@ func main() {
os.Stderr = logfile
if command != "" {
conn, err := net.Dial("unix", SOCK)
if err != nil {
log.Fatalln("Couldn't dial:", err)
}
_, err = conn.Write([]byte(command))
if err != nil {
log.Fatalln("Couldn't send command")
}
fmt.Println("Sent.")
if command == "list" {
buf := make([]byte, 512)
nr, err := conn.Read(buf)
if err != nil {
log.Fatalln("Couldn't read response.")
}
response := string(buf[0:nr])
fmt.Println("Response:")
fmt.Printf(response)
}
os.Exit(0)
execCommand(command)
}
// fmt.Println("New array", players)
// Start command listener
@ -192,35 +602,9 @@ func main() {
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
os.Stderr.WriteString("waybar-mpris is already running. This instance will clone its output.")
if err != nil {
log.Fatalln("Couldn't dial:", err)
}
_, err = conn.Write([]byte("share"))
if err != nil {
log.Fatalln("Couldn't send command")
}
buf := make([]byte, 512)
nr, err := conn.Read(buf)
if err != nil {
log.Fatalln("Couldn't read response.")
}
if string(buf[0:nr]) == "success" {
t, err := tail.TailFile(OUTFILE, tail.Config{
Follow: true,
MustExist: true,
Logger: tail.DiscardingLogger,
})
if err == nil {
for line := range t.Lines {
fmt.Println(line.Text)
}
}
}
} else {
} else if err := duplicateOutput(); err != nil {
os.Stderr.WriteString("Couldn't dial socket, deleting instead: " + err.Error())
os.Remove(SOCK)
os.Remove(OUTFILE)
}
@ -235,77 +619,7 @@ func main() {
players.mpris2.Reload()
players.mpris2.Sort()
lastLine := ""
go func() {
listener, err := net.Listen("unix", SOCK)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
os.Remove(OUTFILE)
os.Remove(SOCK)
os.Exit(1)
}()
if err != nil {
log.Fatalf("Couldn't establish socket connection at %s (error %s)\n", SOCK, err)
}
defer func() {
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])
switch command {
case "player-next":
length := len(players.mpris2.List)
if length != 1 {
if players.mpris2.Current < uint(length-1) {
players.mpris2.Current++
} else {
players.mpris2.Current = 0
}
players.mpris2.Refresh()
}
case "player-prev":
length := len(players.mpris2.List)
if length != 1 {
if players.mpris2.Current != 0 {
players.mpris2.Current--
} else {
players.mpris2.Current = uint(length - 1)
}
players.mpris2.Refresh()
}
case "next":
players.Next()
case "prev":
players.Prev()
case "toggle":
players.Toggle()
case "list":
con.Write([]byte(players.mpris2.String()))
case "share":
out, err := os.Create(OUTFILE)
if err != nil {
con.Write([]byte(fmt.Sprintf("Failed: %s", err)))
}
WRITER = io.MultiWriter(os.Stdout, out)
con.Write([]byte("success"))
default:
fmt.Println("Invalid command")
}
}
}()
go listenForCommands(players)
go players.mpris2.Listen()
if SHOW_POS {
go func() {

2
version.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
NFPM_EPOCH=$(git rev-list --all --count) $@

Binary file not shown.