add list command for debug
responds with the current list of players and some info, to be used for debugging purposes
This commit is contained in:
		
							parent
							
								
									f9b38e5d1d
								
							
						
					
					
						commit
						315b3df656
					
				
							
								
								
									
										35
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								main.go
									
									
									
									
									
								
							@ -8,6 +8,7 @@ import (
 | 
				
			|||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -29,6 +30,7 @@ var knownBrowsers = map[string]string{
 | 
				
			|||||||
type Player struct {
 | 
					type Player struct {
 | 
				
			||||||
	player                               dbus.BusObject
 | 
						player                               dbus.BusObject
 | 
				
			||||||
	fullName, name, title, artist, album string
 | 
						fullName, name, title, artist, album string
 | 
				
			||||||
 | 
						pid                                  uint32
 | 
				
			||||||
	playing, stopped                     bool
 | 
						playing, stopped                     bool
 | 
				
			||||||
	metadata                             map[string]dbus.Variant
 | 
						metadata                             map[string]dbus.Variant
 | 
				
			||||||
	conn                                 *dbus.Conn
 | 
						conn                                 *dbus.Conn
 | 
				
			||||||
@ -50,19 +52,19 @@ var (
 | 
				
			|||||||
	SEP       = " - "
 | 
						SEP       = " - "
 | 
				
			||||||
	ORDER     = "SYMBOL:ARTIST:ALBUM:TITLE:POSITION"
 | 
						ORDER     = "SYMBOL:ARTIST:ALBUM:TITLE:POSITION"
 | 
				
			||||||
	AUTOFOCUS = false
 | 
						AUTOFOCUS = false
 | 
				
			||||||
	COMMANDS  = []string{"player-next", "player-prev", "next", "prev", "toggle"}
 | 
						COMMANDS  = []string{"player-next", "player-prev", "next", "prev", "toggle", "list"}
 | 
				
			||||||
	SHOW_POS  = false
 | 
						SHOW_POS  = false
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewPlayer returns a new player object.
 | 
					// NewPlayer returns a new player object.
 | 
				
			||||||
func NewPlayer(conn *dbus.Conn, name string) (p *Player) {
 | 
					func NewPlayer(conn *dbus.Conn, name string) (p *Player) {
 | 
				
			||||||
	playerName := strings.ReplaceAll(name, INTERFACE+".", "")
 | 
						playerName := strings.ReplaceAll(name, INTERFACE+".", "")
 | 
				
			||||||
 | 
						var pid uint32
 | 
				
			||||||
 | 
						conn.BusObject().Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, name).Store(&pid)
 | 
				
			||||||
	for key, val := range knownPlayers {
 | 
						for key, val := range knownPlayers {
 | 
				
			||||||
		if strings.Contains(name, key) {
 | 
							if strings.Contains(name, key) {
 | 
				
			||||||
			playerName = val
 | 
								playerName = val
 | 
				
			||||||
			if val == "Browser" {
 | 
								if val == "Browser" {
 | 
				
			||||||
				var pid uint32
 | 
					 | 
				
			||||||
				conn.BusObject().Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, name).Store(&pid)
 | 
					 | 
				
			||||||
				file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
 | 
									file, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
 | 
				
			||||||
				if err == nil {
 | 
									if err == nil {
 | 
				
			||||||
					cmd := string(file)
 | 
										cmd := string(file)
 | 
				
			||||||
@ -82,6 +84,7 @@ func NewPlayer(conn *dbus.Conn, name string) (p *Player) {
 | 
				
			|||||||
		conn:     conn,
 | 
							conn:     conn,
 | 
				
			||||||
		name:     playerName,
 | 
							name:     playerName,
 | 
				
			||||||
		fullName: name,
 | 
							fullName: name,
 | 
				
			||||||
 | 
							pid:      pid,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	p.Refresh()
 | 
						p.Refresh()
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
@ -369,6 +372,16 @@ func main() {
 | 
				
			|||||||
			log.Fatalln("Couldn't send command")
 | 
								log.Fatalln("Couldn't send command")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		fmt.Println("Sent.")
 | 
							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)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		conn, err := dbus.SessionBus()
 | 
							conn, err := dbus.SessionBus()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
@ -426,6 +439,22 @@ func main() {
 | 
				
			|||||||
					players.Prev()
 | 
										players.Prev()
 | 
				
			||||||
				} else if command == "toggle" {
 | 
									} else if command == "toggle" {
 | 
				
			||||||
					players.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 {
 | 
									} else {
 | 
				
			||||||
					fmt.Println("Invalid command")
 | 
										fmt.Println("Invalid command")
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										
											BIN
										
									
								
								waybar-mpris
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								waybar-mpris
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
		Loading…
	
		Reference in New Issue
	
	Block a user