-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9df63a1
commit 5b3a44b
Showing
8 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from sdbus import DbusInterfaceCommonAsync, dbus_property_async, dbus_method_async | ||
from sdbus_async.dbus_daemon import FreedesktopDbus | ||
|
||
|
||
class MPRISPProxy(DbusInterfaceCommonAsync, interface_name="org.mpris.MediaPlayer2"): | ||
def __init__(self, service_name): | ||
super().__init__() | ||
self._proxify(service_name, "/org/mpris/MediaPlayer2") | ||
self.Player = MPRISPlayer2Proxy.new_proxy(service_name, "/org/mpris/MediaPlayer2") | ||
|
||
@staticmethod | ||
async def get_all(): | ||
items: list[MPRISPProxy] = [] | ||
bus = FreedesktopDbus() | ||
for name in await bus.list_names(): | ||
if name.startswith("org.mpris.MediaPlayer2"): | ||
items.append(MPRISPProxy(name)) | ||
return items | ||
|
||
@dbus_property_async("s") | ||
def Identity(self) -> str: | ||
pass | ||
|
||
|
||
class MPRISPlayer2Proxy(DbusInterfaceCommonAsync, interface_name="org.mpris.MediaPlayer2.Player"): | ||
@dbus_property_async("s") | ||
def PlaybackStatus(self) -> str: | ||
pass | ||
|
||
@dbus_method_async() | ||
async def Pause(self): | ||
pass | ||
|
||
@dbus_method_async() | ||
async def Play(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import asyncio | ||
from contextlib import suppress | ||
from typing import Optional | ||
|
||
from openfreebuds import IOpenFreebuds, OfbEventKind | ||
from openfreebuds.utils.logger import create_logger | ||
from openfreebuds_backend.linux.dbus.mpris import MPRISPProxy | ||
from openfreebuds_qt.config import OfbQtConfigParser | ||
from openfreebuds_qt.utils import OfbCoreEvent | ||
|
||
log = create_logger("OfbQtMPRISHelperService") | ||
|
||
|
||
class OfbQtMPRISHelperService: | ||
instance = None | ||
|
||
def __init__(self, ofb: IOpenFreebuds): | ||
self.ofb = ofb | ||
self.config = OfbQtConfigParser.get_instance() | ||
|
||
self._task: Optional[asyncio.Task] = None | ||
self.paused_players: list[MPRISPProxy] = [] | ||
self.last_in_ear: bool = True | ||
|
||
async def _trigger(self): | ||
in_ear = await self.ofb.get_property("state", "in_ear", "false") == "true" | ||
enabled = await self.ofb.get_property("config", "auto_pause", "false") == "true" | ||
|
||
if in_ear != self.last_in_ear and enabled: | ||
if self.last_in_ear is True and in_ear is False: | ||
# Pause all | ||
self.paused_players = [] | ||
for service in await MPRISPProxy.get_all(): | ||
if await service.Player.PlaybackStatus == "Playing": | ||
log.info(f"Pause {service.Identity}") | ||
await service.Player.Pause() | ||
self.paused_players.append(service) | ||
elif self.last_in_ear is False and in_ear is True: | ||
for service in self.paused_players: | ||
log.info(f"Resume {service.Identity}") | ||
await service.Player.Play() | ||
self.paused_players = [] | ||
self.last_in_ear = in_ear | ||
|
||
@staticmethod | ||
def get_instance(ofb: IOpenFreebuds): | ||
if OfbQtMPRISHelperService.instance is None: | ||
OfbQtMPRISHelperService.instance = OfbQtMPRISHelperService(ofb) | ||
return OfbQtMPRISHelperService.instance | ||
|
||
async def stop(self): | ||
if self._task is not None: | ||
with suppress(Exception): | ||
self._task.cancel() | ||
await self._task | ||
self._task = None | ||
|
||
async def start(self): | ||
await self.stop() | ||
if not self.config.get("mpris", "enabled", False): | ||
return | ||
|
||
self._task = asyncio.create_task(self._main()) | ||
|
||
async def _main(self): | ||
log.info("Started") | ||
member_id = await self.ofb.subscribe(kind_filters=[OfbEventKind.PROPERTY_CHANGED]) | ||
|
||
while True: | ||
try: | ||
event = OfbCoreEvent(*await self.ofb.wait_for_event(member_id)) | ||
if event.is_changed("state", "in_ear"): | ||
await self._trigger() | ||
except Exception: | ||
log.exception("Failure") |