[waybar] fix: Replace mediaplayer with mpris

This commit is contained in:
Tibo De Peuter 2024-06-02 19:51:04 +02:00
parent 19affd8b0e
commit 386d79432b
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
5 changed files with 35 additions and 159 deletions

View file

@ -1,7 +1,7 @@
{
"modules-left": [
"idle_inhibitor",
"custom/media"
"mpris"
],
"modules-center": [
"sway/workspaces"

View file

@ -1,21 +1,22 @@
{
"modules-left": [
"modules-left": [
"sway/workspaces",
"idle_inhibitor",
"custom/light-dark-toggle",
"custom/night-light-toggle",
"custom/toggle-notifications",
"custom/media"
"mpris"
],
"modules-right": [
"memory",
"cpu",
"temperature",
"modules-right": [
"memory",
"cpu",
"temperature",
"custom/sep",
"bluetooth",
"network",
"pulseaudio",
"battery",
"bluetooth",
"network",
"pulseaudio",
"battery",
"custom/sep",
"tray"
]

View file

@ -1,131 +0,0 @@
#!/usr/bin/env python3
# From:
# https://github.com/Alexays/Waybar/blob/master/resources/custom_modules/mediaplayer.py
import argparse
import logging
import sys
import signal
import gi
import json
gi.require_version('Playerctl', '2.0')
from gi.repository import Playerctl, GLib
logger = logging.getLogger(__name__)
def write_output(text, player):
logger.info('Writing output')
output = {'text': text,
'class': 'custom-' + player.props.player_name,
'alt': player.props.player_name}
sys.stdout.write(json.dumps(output) + '\n')
sys.stdout.flush()
def on_play(player, status, manager):
logger.info('Received new playback status')
on_metadata(player, player.props.metadata, manager)
def on_metadata(player, metadata, manager):
logger.info('Received new metadata')
track_info = ''
if player.props.player_name == 'spotify' and \
'mpris:trackid' in metadata.keys() and \
':ad:' in player.props.metadata['mpris:trackid']:
track_info = 'AD PLAYING'
elif player.get_artist() != '' and player.get_title() != '':
track_info = '{artist} - {title}'.format(artist=player.get_artist(),
title=player.get_title())
else:
track_info = player.get_title()
if player.props.status != 'Playing' and track_info:
track_info = '' + track_info
write_output(track_info, player)
def on_player_appeared(manager, player, selected_player=None):
if player is not None and (selected_player is None or player.name == selected_player):
init_player(manager, player)
else:
logger.debug("New player appeared, but it's not the selected player, skipping")
def on_player_vanished(manager, player):
logger.info('Player has vanished')
sys.stdout.write('\n')
sys.stdout.flush()
def init_player(manager, name):
logger.debug('Initialize player: {player}'.format(player=name.name))
player = Playerctl.Player.new_from_name(name)
player.connect('playback-status', on_play, manager)
player.connect('metadata', on_metadata, manager)
manager.manage_player(player)
on_metadata(player, player.props.metadata, manager)
def signal_handler(sig, frame):
logger.debug('Received signal to stop, exiting')
sys.stdout.write('\n')
sys.stdout.flush()
# loop.quit()
sys.exit(0)
def parse_arguments():
parser = argparse.ArgumentParser()
# Increase verbosity with every occurrence of -v
parser.add_argument('-v', '--verbose', action='count', default=0)
# Define for which player we're listening
parser.add_argument('--player')
return parser.parse_args()
def main():
arguments = parse_arguments()
# Initialize logging
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG,
format='%(name)s %(levelname)s %(message)s')
# Logging is set by default to WARN and higher.
# With every occurrence of -v it's lowered by one
logger.setLevel(max((3 - arguments.verbose) * 10, 0))
# Log the sent command line arguments
logger.debug('Arguments received {}'.format(vars(arguments)))
manager = Playerctl.PlayerManager()
loop = GLib.MainLoop()
manager.connect('name-appeared', lambda *args: on_player_appeared(*args, arguments.player))
manager.connect('player-vanished', on_player_vanished)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
for player in manager.props.player_names:
if arguments.player is not None and arguments.player != player.name:
logger.debug('{player} is not the filtered player, skipping it'
.format(player=player.name)
)
continue
init_player(manager, player)
loop.run()
if __name__ == '__main__':
main()

View file

@ -47,19 +47,6 @@
"tooltip-format": "Launch an application"
},
"custom/media": {
"escape": true,
"exec": "~/.config/waybar/mediaplayer.py 2> /dev/null",
"format": "{icon} {}",
"format-icons": {
"default": "\uf51f",
"spotify": "\uf1bc"
},
"max-length": 40,
"on-click": "playerctl play-pause",
"return-type": "json"
},
"custom/light-dark-toggle": {
"on-click": "bash ${SCRIPT_DIR}/toggle-light-dark.sh",
"tooltip-format": "Toggle between light and dark mode",
@ -101,6 +88,26 @@
"on-click": "kitty -e zenith"
},
"mpris": {
// "ignored-players": ["firefox"]
"format": "{player_icon} {dynamic}",
"format-paused": "{status_icon} <i>{dynamic}</i>",
"tooltip-format": "{player} ({status}) {title} - {artist} - {album}",
"player-icons": {
"default": "\uf51f",
"spotify": "\uf1bc",
"mpv": "🎵"
},
"status-icons": {
"paused": "⏸"
},
"dynamic-order": [
"title",
"artist"
],
"dynamic-len": 40,
},
"network": {
"format-disconnected": "\uf127",
"format-ethernet": "\uf6ff {ifname}: {ipadds}/{cidr}",

View file

@ -101,17 +101,16 @@ label:focus {
opacity: 0.6;
}
#custom-media {
#mpris {
color: #2a5c45;
background-color: #66cc99;
min-width: 100px;
}
#custom-media.custom-spotify {
#mpris.spotify {
background-color: #66cc99;
}
#custom-media.custom-vlc {
#mpris.vlc {
background-color: #ffa000;
}