aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 436e65dd8e78d3c7ee452caab2ce88377fc47c48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/godbus/dbus/v5"
	flag "github.com/spf13/pflag"
)

var knownPlayers = map[string]string{
	"plasma-browser-integration": "Browser",
	"noson":                      "Noson",
}

var knownBrowsers = map[string]string{
	"mozilla":  "Firefox",
	"chrome":   "Chrome",
	"chromium": "Chromium",
}

type Player struct {
	player                               dbus.BusObject
	fullName, name, title, artist, album string
	pid                                  uint32
	playing, stopped                     bool
	metadata                             map[string]dbus.Variant
	conn                                 *dbus.Conn
}

const (
	INTERFACE = "org.mpris.MediaPlayer2"
	PATH      = "/org/mpris/MediaPlayer2"
	// NameOwnerChanged
	MATCH_NOC = "type='signal',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
	// PropertiesChanged
	MATCH_PC = "type='signal',path='/org/mpris/MediaPlayer2',interface='org.freedesktop.DBus.Properties'"
	SOCK     = "/tmp/waybar-mpris.sock"
)

var (
	PLAY      = "▶"
	PAUSE     = ""
	SEP       = " - "
	ORDER     = "SYMBOL:ARTIST:ALBUM:TITLE:POSITION"
	AUTOFOCUS = false
	COMMANDS  = []string{"player-next", "player-prev", "next", "prev", "toggle", "list"}
	SHOW_POS  = false
)

// NewPlayer returns a new player object.
func NewPlayer(conn *dbus.Conn, name string) (p *Player) {
	playerName := strings.ReplaceAll(name, INTERFACE+".", "")
	var pid uint32
	conn.BusObject().Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, name).Store(&pid)
	for key, val := range knownPlayers {
		if strings.Contains(name, key) {
			playerName = val
			break
		}
	}
	if playerName == "Browser" {
		file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
		if err == nil {
			cmd := string(file)
			for key, val := range knownBrowsers {
				if strings.Contains(cmd, key) {
					playerName = val
					break
				}
			}
		}
	}
	p = &Player{
		player:   conn.Object(name, PATH),
		conn:     conn,
		name:     playerName,
		fullName: name,
		pid:      pid,
	}
	p.Refresh()
	return
}

// Refresh grabs playback info.
func (p *Player) Refresh() (err error) {
	val, err := p.player.GetProperty(INTERFACE + ".Player.PlaybackStatus")
	if err != nil {
		p.playing = false
		p.stopped = false
		p.metadata = map[string]dbus.Variant{}
		p.title = ""
		p.artist = ""
		p.album = ""
		return
	}
	strVal := val.String()
	if strings.Contains(strVal, "Playing") {
		p.playing = true
		p.stopped = false
	} else if strings.Contains(strVal, "Paused") {
		p.playing = false
		p.stopped = false
	} else {
		p.playing = false
		p.stopped = true
	}
	metadata, err := p.player.GetProperty(INTERFACE + ".Player.Metadata")
	if err != nil {
		p.metadata = map[string]dbus.Variant{}
		p.title = ""
		p.artist = ""
		p.album = ""
		return
	}
	p.metadata = metadata.Value().(map[string]dbus.Variant)
	switch artist := p.metadata["xesam:artist"].Value().(type) {
	case []string:
		p.artist = strings.Join(artist, ", ")
	case string:
		p.artist = artist
	default:
		p.artist = ""
	}
	switch title := p.metadata["xesam:title"].Value().(type) {
	case string:
		p.title = title
	default:
		p.title = ""
	}
	switch album := p.metadata["xesam:album"].Value().(type) {
	case string:
		p.album = album
	default:
		p.album = ""
	}
	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
	data["class"] = "paused"
	if p.playing {
		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 {
		if v == "SYMBOL" {
			items = append(items, symbol)
		} else if v == "ARTIST" {
			if p.artist != "" {
				items = append(items, p.artist)
			}
		} else if v == "ALBUM" {
			if p.album != "" {
				items = append(items, p.album)
			}
		} else if v == "TITLE" {
			if p.title != "" {
				items = append(items, p.title)
			}
		} else if v == "POSITION" && SHOW_POS {
			if pos != "" {
				items = append(items, pos)
			}
		}
	}
	if len(items) == 0 {
		return "{}"
	}
	text := ""
	for i, v := range items {
		right := ""
		if (v == symbol || v == pos) && i != len(items)-1 {
			right = " "
		} else if i != len(items)-1 && items[i+1] != symbol && items[i+1] != pos {
			right = SEP
		} else {
			right = " "
		}
		text += v + right
	}

	data["tooltip"] = fmt.Sprintf(
		"%s\nby %s\n",
		p.title,
		p.artist)
	if p.album != "" {
		data["tooltip"] += "from " + p.album + "\n"
	}
	data["tooltip"] += "(" + p.name + ")"
	data["text"] = text
	out, err := json.Marshal(data)
	if err != nil {
		return "{}"
	}
	return string(out)
}

type PlayerList struct {
	list    List
	current uint
	conn    *dbus.Conn
}

type List []*Player

func (ls List) Len() int {
	return len(ls)
}

func (ls List) Less(i, j int) bool {
	var states [2]uint8
	for i, p := range []bool{ls[i].playing, ls[j].playing} {
		if p {
			states[i] = 1
		}
	}
	// Reverse order
	return states[0] > states[1]
}

func (ls List) Swap(i, j int) {
	ls[i], ls[j] = ls[j], ls[i]
}

func (pl *PlayerList) Remove(fullName string) {
	currentName := pl.list[pl.current].fullName
	var i int
	found := false
	for ind, p := range pl.list {
		if p.fullName == fullName {
			i = ind
			found = true
			break
		}
	}
	if found {
		pl.list[0], pl.list[i] = pl.list[i], pl.list[0]
		pl.list = pl.list[1:]
		found = false
		for ind, p := range pl.list {
			if p.fullName == currentName {
				pl.current = uint(ind)
				found = true
				break
			}
		}
		if !found {
			pl.current = 0
			pl.Refresh()
			fmt.Println(pl.JSON())
		}
	}
	// ls[len(ls)-1], ls[i] = ls[i], ls[len(ls)-1]
	// ls = ls[:len(ls)-1]
}

func (pl *PlayerList) Reload() error {
	var buses []string
	err := pl.conn.BusObject().Call("org.freedesktop.DBus.ListNames", 0).Store(&buses)
	if err != nil {
		return err
	}
	for _, name := range buses {
		// Don't add playerctld, it just duplicates other players
		if strings.HasPrefix(name, INTERFACE) && !strings.Contains(name, "playerctld") {
			pl.New(name)
		}
	}
	return nil
}

func (pl *PlayerList) New(name string) {
	pl.list = append(pl.list, NewPlayer(pl.conn, name))
	if AUTOFOCUS {
		pl.current = uint(len(pl.list) - 1)
	}
}

func (pl *PlayerList) Sort() {
	sort.Sort(pl.list)
	pl.current = 0
}

func (pl *PlayerList) Refresh() {
	for i := range pl.list {
		pl.list[i].Refresh()
	}
}

func (pl *PlayerList) JSON() string {
	if len(pl.list) != 0 {
		return pl.list[pl.current].JSON()
	}
	return "{}"
}

func (pl *PlayerList) Next() {
	pl.list[pl.current].player.Call(INTERFACE+".Player.Next", 0)
}

func (pl *PlayerList) Prev() {
	pl.list[pl.current].player.Call(INTERFACE+".Player.Previous", 0)
}

func (pl *PlayerList) Toggle() {
	pl.list[pl.current].player.Call(INTERFACE+".Player.PlayPause", 0)
}

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.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()

	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.Fatalf("Couldn't read response.")
			}
			response := string(buf[0:nr])
			fmt.Println("Response:")
			fmt.Printf(response)
		}
		os.Exit(0)
	}
	conn, err := dbus.SessionBus()
	if err != nil {
		log.Fatalln("Error connecting to DBus:", err)
	}
	players := &PlayerList{
		conn: conn,
	}
	players.Reload()
	players.Sort()
	players.Refresh()
	fmt.Println(players.JSON())
	lastLine := ""
	// 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() {
			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)
		}

	}
	go func() {
		listener, err := net.Listen("unix", SOCK)
		if err != nil {
			log.Fatalln("Couldn't establish socket connection at", SOCK)
		}
		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])
			if command == "player-next" {
				length := len(players.list)
				if length != 1 {
					if players.current < uint(length-1) {
						players.current += 1
					} else {
						players.current = 0
					}
					players.Refresh()
					fmt.Println(players.JSON())
				}
			} else if command == "player-prev" {
				length := len(players.list)
				if length != 1 {
					if players.current != 0 {
						players.current -= 1
					} else {
						players.current = uint(length - 1)
					}
					players.Refresh()
					fmt.Println(players.JSON())
				}
			} else if command == "next" {
				players.Next()
			} else if command == "prev" {
				players.Prev()
			} else if command == "toggle" {
				players.Toggle()
			} else if command == "list" {
				resp := ""
				pad := 0
				i := len(players.list)
				for i != 0 {
					i /= 10
					pad++
				}
				for i, p := range players.list {
					symbol := ""
					if uint(i) == players.current {
						symbol = "*"
					}
					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())
				}
			}
		}()
	}
	c := make(chan *dbus.Signal, 10)
	conn.Signal(c)
	for v := range c {
		// 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") {
			switch name := v.Body[0].(type) {
			case string:
				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)
			}
		}
	}
}