Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added dbus handler to update current player #2157

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions resources/custom_modules/mediaplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import signal
import gi
import json
import dbus
import dbus.service
gi.require_version('Playerctl', '2.0')
from gi.repository import Playerctl, GLib
from dbus.mainloop.glib import DBusGMainLoop

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -55,6 +58,10 @@ def on_player_appeared(manager, player, selected_player=None):

def on_player_vanished(manager, player):
logger.info('Player has vanished')
if len(manager.props.players) > 0:
player = manager.props.players[0]
on_metadata(player, player.props.metadata, manager)
return
sys.stdout.write('\n')
sys.stdout.flush()

Expand All @@ -76,6 +83,17 @@ def signal_handler(sig, frame):
sys.exit(0)


def dbus_signal_handler(*args, manager, **kwargs):
# there is no implicit way to get current player,
# so we need to create a new manager to get the right order of players
new_manager = Playerctl.PlayerManager()
if len(new_manager.props.player_names) > 1:
current_player_name = new_manager.props.player_names[0].name
for player in manager.props.players:
if player.props.player_name == current_player_name:
on_metadata(player, player.props.metadata, manager)


def parse_arguments():
parser = argparse.ArgumentParser()

Expand Down Expand Up @@ -112,6 +130,14 @@ def main():
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGPIPE, signal.SIG_DFL)

# use dbus to shift player (e.g. after playerctld shift)
bus = dbus.SessionBus(mainloop=DBusGMainLoop())
bus.add_signal_receiver(lambda *args, **kwargs: dbus_signal_handler(*args, **kwargs, manager=manager),
signal_name='Shift',
dbus_interface='org.waybar.Player')
# register well-known bus name
bus.request_name('org.waybar.Player')

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'
Expand Down