Closing the mpv window now disconnects the client

This commit is contained in:
Christoph Stahl 2024-10-10 13:33:18 +02:00
parent d4cf649735
commit bf2e854cdd
3 changed files with 17 additions and 13 deletions

View file

@ -119,21 +119,16 @@ class State:
class Client: class Client:
def __init__(self, config: dict[str, Any]): def __init__(self, config: dict[str, Any]):
self.sio = socketio.AsyncClient(json=jsonencoder) self.sio = socketio.AsyncClient(json=jsonencoder)
self.loop: Optional[asyncio.AbstractEventLoop] = None
self.skipped = [] self.skipped = []
self.sources = configure_sources(config["sources"]) self.sources = configure_sources(config["sources"])
self.state = State() self.state = State()
self.currentLock = asyncio.Semaphore(0) self.currentLock = asyncio.Semaphore(0)
self.player = Player(f"{config['config']['server']}/{config['config']['room']}", self.quit) self.player = Player(
f"{config['config']['server']}/{config['config']['room']}", self.quit_callback
)
self.register_handlers() self.register_handlers()
def quit(self, event):
e = event.as_dict()
if e["event"] == b"shutdown":
quit()
# if value is None:
# return
# print(value)
def register_handlers(self) -> None: def register_handlers(self) -> None:
self.sio.on("update_config", self.handle_update_config) self.sio.on("update_config", self.handle_update_config)
self.sio.on("skip-current", self.handle_skip_current) self.sio.on("skip-current", self.handle_skip_current)
@ -445,6 +440,10 @@ class Client:
if self.player is not None: if self.player is not None:
self.player.mpv.terminate() self.player.mpv.terminate()
def quit_callback(self):
if self.loop is not None:
asyncio.run_coroutine_threadsafe(self.sio.disconnect(), self.loop)
async def start_client(self, config: dict[str, Any]) -> None: async def start_client(self, config: dict[str, Any]) -> None:
""" """
Initialize the client and connect to the server. Initialize the client and connect to the server.
@ -454,6 +453,8 @@ class Client:
:rtype: None :rtype: None
""" """
self.loop = asyncio.get_running_loop()
self.sources.update(configure_sources(config["sources"])) self.sources.update(configure_sources(config["sources"]))
if "config" in config: if "config" in config:

View file

@ -30,14 +30,16 @@ class Player:
self.default_options = { self.default_options = {
"scale": "bilinear", "scale": "bilinear",
} }
self.quit_callback = quit_callback
self.mpv.observe_property("osd-width", self.osd_size_handler) self.mpv.observe_property("osd-width", self.osd_size_handler)
self.mpv.observe_property("osd-height", self.osd_size_handler) self.mpv.observe_property("osd-height", self.osd_size_handler)
# self.mpv.register_event_callback(quit_callback) self.mpv.register_event_callback(self.event_callback)
# self.mpv.observe_property("exit-handler", quit_callback)
def event_printer(self, *args): def event_callback(self, event):
print(args) e = event.as_dict()
if e["event"] == b"shutdown":
self.quit_callback()
def update_qr(self, qr_string: str) -> None: def update_qr(self, qr_string: str) -> None:
qr = QRCode(box_size=5, border=1) qr = QRCode(box_size=5, border=1)

View file

@ -38,5 +38,6 @@ class MPV:
def loadfile( def loadfile(
self, file: str, audio_file: Optional[str] = None, sub_file: Optional[str] = None self, file: str, audio_file: Optional[str] = None, sub_file: Optional[str] = None
) -> None: ... ) -> None: ...
def register_event_callback(self, callback: Callable[..., Any]) -> None: ...
def __setitem__(self, key: str, value: str) -> None: ... def __setitem__(self, key: str, value: str) -> None: ...
def __getitem__(self, key: str) -> str: ... def __getitem__(self, key: str) -> str: ...