forked from trav/lintunes
Snapshot of the existing codebase before working through the TASKS.md backlog. Real library data (data/) and the iTunes import fixture (itunes-test-library/) are gitignored.
46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
"""Keep the machine awake while music is playing.
|
|
|
|
Uses the GNOME SessionManager D-Bus interface (the same session bus PyQt6's
|
|
QtDBus already talks to for MPRIS) to register a *suspend* inhibitor. GNOME's
|
|
critical-battery action overrides this inhibitor, so a near-dead laptop still
|
|
suspends as expected — we only block idle/automatic suspend while playing.
|
|
|
|
On non-GNOME desktops the interface won't be available; every method then
|
|
no-ops silently rather than failing.
|
|
"""
|
|
from PyQt6.QtDBus import QDBusConnection, QDBusInterface, QDBusReply
|
|
|
|
|
|
SERVICE = "org.gnome.SessionManager"
|
|
PATH = "/org/gnome/SessionManager"
|
|
INHIBIT_SUSPEND = 4 # GsmInhibitorFlag: inhibit suspending the session/computer
|
|
APP_ID = "org.lintunes.LinTunes"
|
|
REASON = "Playing music"
|
|
|
|
|
|
class SleepInhibitor:
|
|
def __init__(self):
|
|
self._cookie: int | None = None
|
|
bus = QDBusConnection.sessionBus()
|
|
self._iface = QDBusInterface(SERVICE, PATH, SERVICE, bus)
|
|
|
|
def _available(self) -> bool:
|
|
return self._iface.isValid()
|
|
|
|
def inhibit(self):
|
|
"""Block automatic suspend. No-op if already inhibiting or unavailable."""
|
|
if self._cookie is not None or not self._available():
|
|
return
|
|
reply = QDBusReply(self._iface.call(
|
|
"Inhibit", APP_ID, 0, REASON, INHIBIT_SUSPEND))
|
|
if reply.isValid():
|
|
self._cookie = int(reply.value())
|
|
|
|
def release(self):
|
|
"""Release a held inhibitor, if any."""
|
|
if self._cookie is None:
|
|
return
|
|
if self._available():
|
|
self._iface.call("Uninhibit", self._cookie)
|
|
self._cookie = None
|