Skip to content
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
56 changes: 48 additions & 8 deletions MediaController.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class MediaController:
def __init__(self):
self.session_bus = dbus.SessionBus()
self.last_active_bus_name: str = None

self.update_players()

Expand Down Expand Up @@ -55,25 +56,64 @@ def get_matching_ifaces(self, player_name: str = None) -> list[dbus.Interface]:
Returns:
list[dbus.Interface]: A list of dbus interfaces that match the given player name.
"""
ifaces = []
matching = []
for player in self.mpris_players:
properties = dbus.Interface(player, 'org.freedesktop.DBus.Properties')
try:
if player_name in [None, "", properties.Get('org.mpris.MediaPlayer2', 'Identity')]:
iface = dbus.Interface(player, 'org.mpris.MediaPlayer2.Player')
ifaces.append(iface)
matching.append((player, properties))
except dbus.exceptions.DBusException as e:
if e.get_dbus_name() != "com.github.altdesktop.playerctld.NoActivePlayer":
log.warning(e)
return ifaces

if player_name not in [None, ""]:
players = [player for player, _ in matching]
else:
players = self.get_active_players(matching)

return [dbus.Interface(player, 'org.mpris.MediaPlayer2.Player') for player in players]

def get_active_players(self, matching: list) -> list:
"""Narrows the players to act on when no player was picked in the settings.

Whatever is playing is the active player, and when nothing is playing it
is whatever played last. Acting on every player instead means one press
of a play/pause key resumes several of them at once: pause a video while
music is paused too, press play, and both start.

Args:
matching (list): (player, properties) pairs that match the settings.

Returns:
list: the players to act on, at most one unless several are playing.
"""
playing = []
for player, properties in matching:
try:
if properties.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus') == "Playing":
playing.append(player)
except dbus.exceptions.DBusException as e:
if e.get_dbus_name() != "com.github.altdesktop.playerctld.NoActivePlayer":
log.warning(e)

if playing:
self.last_active_bus_name = str(playing[0].bus_name)
return playing

for player, _ in matching:
if str(player.bus_name) == self.last_active_bus_name:
return [player]

# Nothing has played yet this session, so there is nothing better to go on.
return matching[:1] and [matching[0][0]]

def pause(self, player_name: str = None):
"""
Pauses the media player specified by the `player_name` parameter.

Args:
player_name (str, optional): The name of the media player to pause.
If not provided, all media players will be paused.
If not provided, the active player is used.

Returns:
None
Expand All @@ -95,7 +135,7 @@ def play(self, player_name: str = None):

Args:
player_name (str, optional): The name of the media player to play.
If not provided, all media players will be played.
If not provided, the active player is used.

Returns:
None
Expand All @@ -117,7 +157,7 @@ def toggle(self, player_name: str = None):

Args:
player_name (str, optional): The name of the media player to toggle.
If not provided, all media players will be toggled.
If not provided, the active player is used.

Returns:
None
Expand All @@ -139,7 +179,7 @@ def stop(self, player_name: str = None):

Args:
player_name (str, optional): The name of the media player to stop.
If not provided, all media players will be stopped.
If not provided, the active player is used.

Returns:
None
Expand Down