aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: dcde8a38f9013d4ab800743fd799ee1e09433497 (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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package main

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

	"github.com/fsnotify/fsnotify"
	"github.com/godbus/dbus/v5"
	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"      // 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     = 900
)

// Mostly default values for flag options.
var (
	playIcon      = "▶"
	pauseIcon     = ""
	textFormat    = "%i %a - %t (%p/%d)"
	tooltipFormat = "%t by %a from %A\n(%P)"
	autofocus     = false
	// Available commands that can be sent to running instances.
	commands                          = []string{"player-next", "player-prev", "next", "prev", "toggle", "list"}
	interpolate                       = false
	replace                           = false
	isSharing                         = false
	isDataSharing                     = false
	maxTitleLen                       = 50
	writer                  io.Writer = os.Stdout
	shareWriter, dataWriter io.Writer
)

// Format tokens `%<character>`
const (
	tokIcon     = 'i'
	tokArtist   = 'a'
	tokAlbum    = 'A'
	tokTitle    = 't'
	tokPosition = 'p'
	tokLength   = 'd'
	tokPlayer   = 'P'
)

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
}

// This represents the output in JSON format that waybar is expecting
type waybarData struct {
	Class   string `json:"class"`
	Text    string `json:"text"`
	Tooltip string `json:"tooltip"`
}

func secondsToString(seconds int) string {
	minutes := int(seconds / 60)
	seconds -= int(minutes * 60)
	return fmt.Sprintf("%02d:%02d", minutes, seconds)
}

// Function to format the output as requested by the user preferences of icon,
// format specifier string and maximum title length
func formatOutput(p *player, icon string, fstr string, maxTitleLen int) string {
	var buf []byte
	for i := 0; i < len(fstr); i++ {
		if fstr[i] == '%' {
			i++
			if i >= len(fstr) {
				break
			}
			switch fstr[i] {
			case tokIcon:
				buf = append(buf, []byte(icon)...)
			case tokArtist:
				artist := strings.ReplaceAll(p.Artist, "&", "&amp;")
				buf = append(buf, []byte(artist)...)
			case tokAlbum:
				album := strings.ReplaceAll(p.Album, "&", "&amp;")
				buf = append(buf, []byte(album)...)
			case tokTitle:
				title := p.Title
				if maxTitleLen > 0 && len([]rune(title)) > maxTitleLen {
					// Probably not the most effective thing to do, but this way
					// we don't slice the string in the middle of a UTF-8
					// character it happens to be more than 1 byte, and the
					// maximum length actually correlates to the displayed
					// charaacters instead of the bytes.
					title = string([]rune(title)[:maxTitleLen-3]) + "..."
				}
				title = strings.ReplaceAll(title, "&", "&amp;")
				buf = append(buf, []byte(title)...)
			case tokPosition:
				var position string
				if p.Length > 0 {
					position = secondsToString(int(p.Position / 1000000))
				} else {
					position = secondsToString(0)
				}
				buf = append(buf, []byte(position)...)
			case tokLength:
				length := secondsToString(p.Length)
				buf = append(buf, []byte(length)...)
			case tokPlayer:
				buf = append(buf, []byte(p.Name)...)
			default:
				buf = append(buf, fstr[i])
			}
		} else {
			buf = append(buf, fstr[i])
		}
	}

	return string(buf)
}

// Returns JSON for waybar to consume.
func playerJSON(p *player) string {
	data := waybarData{}

	icon := playIcon
	if p.Playing {
		icon = pauseIcon
		data.Class = "playing"
	} else {
		data.Class = "paused"
	}

	data.Text = formatOutput(p, icon, textFormat, maxTitleLen)
	// There's no point in limiting the length of the output/title in the
	// tooltip, so we set it to 0
	data.Tooltip = formatOutput(p, icon, tooltipFormat, 0)

	out, err := json.Marshal(data)
	if err != nil {
		return "{}"
	}
	return string(out)
}

type players struct {
	mpris2 *mpris2.Mpris2
}

func (pl *players) JSON() string {
	if len(pl.mpris2.List) != 0 {
		return playerJSON(&player{pl.mpris2.List[pl.mpris2.Current], false})
	}
	return "{}"
}

func (pl *players) Next() { pl.mpris2.List[pl.mpris2.Current].Next() }

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.\n")

	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 {
		log.Fatalf("Couldn't open %s for writing: %s", LOGFILE, err)
	}
	mw := io.MultiWriter(logfile, os.Stdout)
	log.SetOutput(mw)
	flag.StringVar(&playIcon, "play", playIcon, "Play symbol/text to use.")
	flag.StringVar(&pauseIcon, "pause", pauseIcon, "Pause symbol/text to use.")
	flag.StringVar(&textFormat, "text-format", textFormat,
		"Format of the waybar module text.")
	flag.StringVar(&tooltipFormat, "tooltip-format", tooltipFormat,
		"Format of the waybar module tooltip.")
	flag.BoolVar(&autofocus, "autofocus", autofocus,
		"Auto switch to currently playing music players.")
	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.")
	flag.IntVar(&maxTitleLen, "max-title", maxTitleLen,
		"Maximum length of title. If the title's longer N-3 characters will be printed and ... will be printed at the end.")
	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)
			}
			// When waybar-mpris is already running, we attach to its output instead of launching a whole new instance.
		} else if err := duplicateOutput(); err != nil {
			os.Stderr.WriteString("Couldn't dial socket, deleting instead: " + err.Error())
			os.Remove(SOCK)
			os.Remove(OUTFILE)
		}
	} else {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
	}
	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 func() {
		for {
			time.Sleep(POLL * time.Millisecond)
			if len(players.mpris2.List) != 0 {
				if players.mpris2.List[players.mpris2.Current].Playing {
					players.mpris2.List[players.mpris2.Current].GetPosition()
					go fmt.Fprintln(writer, players.JSON())
				}
			}
		}
	}()
	fmt.Fprintln(writer, players.JSON())
	for v := range players.mpris2.Messages {
		if v.Name == "refresh" {
			if autofocus {
				players.mpris2.Sort()
			}
			if l := players.JSON(); l != lastLine {
				lastLine = l
				fmt.Fprintln(writer, l)
			}
		}
	}
	players.mpris2.Refresh()
}