-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreensaver-inhibit
executable file
·187 lines (155 loc) · 5.33 KB
/
screensaver-inhibit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
# Author: Jan Larres <[email protected]>
# License: MIT/X11
#
# References:
# https://specifications.freedesktop.org/idle-inhibit-spec/latest/
# https://gabmus.org/posts/making-a-dbus-daemon/
# https://github.com/loops/idlehack/blob/fd73c76c2d289f9eb9ad9b0695fa9e9f151be22f/idlehack.c
# mypy: disable-error-code="misc, import-untyped"
# pyright: basic
# ruff: noqa: BLE001, N802, S311, S603, S607, ERA001, TRY400
import argparse
import logging
import random
import signal
import subprocess
import sys
from dataclasses import dataclass
from functools import partial
from typing import Any
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib # pyright: ignore
from systemd.journal import JournalHandler
BUS_NAME = "org.freedesktop.ScreenSaver"
OBJECT_PATH = "/org/freedesktop/ScreenSaver"
OBJECT_PATH_OLD = "/ScreenSaver"
INTERFACE = "org.freedesktop.ScreenSaver"
logging.basicConfig(
format="%(message)s",
level=logging.INFO,
handlers=[
JournalHandler(
UNIT="screensaver-inhibit.service",
SYSLOG_IDENTIFIER="screensaver-inhibit",
)
],
)
log = logging.getLogger(__name__)
@dataclass
class InhibitingApp:
peer: str
name: str
reason: str
cookie: int
INHIBITORS: dict[int, InhibitingApp] = {}
class ScreensaverInhibitor(dbus.service.Object):
def __init__(self, bus_name: dbus.service.BusName):
self.SUPPORTS_MULTIPLE_OBJECT_PATHS = True
super().__init__(bus_name, OBJECT_PATH)
self.conn = bus_name.get_bus()
self.add_to_connection(self.conn, OBJECT_PATH_OLD)
@dbus.service.method(
dbus_interface=INTERFACE,
in_signature="ss",
out_signature="u",
sender_keyword="sender",
)
def Inhibit(self, app_name: str, reason: str, sender: str) -> int:
while (cookie := random.randint(0, 2**32 - 1)) in INHIBITORS:
pass
app = InhibitingApp(sender, app_name, reason, cookie)
log.debug("Inhibiting screensaver for app %s", app)
self.conn.watch_name_owner(sender, partial(watch_callback, sender))
inhibit()
INHIBITORS[cookie] = app
return cookie
@dbus.service.method(dbus_interface=INTERFACE, in_signature="u")
def UnInhibit(self, cookie: int) -> None:
app = INHIBITORS.pop(cookie, None)
if app is None:
log.debug("Received uninhibit request for unknown cookie %s", cookie)
else:
log.debug("Uninhibiting screensaver for app %s", app)
uninhibit()
@dbus.service.method(
dbus_interface=dbus.PROPERTIES_IFACE, in_signature="s", out_signature="a{sv}"
)
def GetAll(self, interface: dbus.String) -> dict[dbus.String, Any]:
log.debug("%s.GetAll(%s) called", dbus.PROPERTIES_IFACE, interface)
return {}
def main(args: argparse.Namespace) -> int:
if args.verbose:
log.setLevel(logging.DEBUG)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGUSR1, signal_handler)
DBusGMainLoop(set_as_default=True)
try:
bus_name = dbus.service.BusName(
BUS_NAME, bus=dbus.SessionBus(), do_not_queue=True
)
except dbus.exceptions.NameExistsException:
log.warning("Service with id %s is already running", BUS_NAME)
return 1
loop = GLib.MainLoop()
_ = ScreensaverInhibitor(bus_name)
try:
loop.run()
except KeyboardInterrupt:
log.info("KeyboardInterrupt received")
except Exception as e:
log.info("Caught exception: %s", e)
finally:
uninhibit(True)
loop.quit()
return 0
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="run basic screensaver inhibitor")
parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=False,
help="increase output verbosity",
)
return parser.parse_args()
def inhibit() -> None:
log.debug("inhibit() called")
if not INHIBITORS:
log.debug("inhibiting screensaver")
run_command(["/usr/bin/xset", "s", "off"])
run_command(["/usr/bin/xset", "-dpms"])
def uninhibit(force: bool = False) -> None:
log.debug("uninhibit(%s) called", force)
if force or not INHIBITORS:
log.debug("uninhibiting screensaver")
run_command(["/usr/bin/xset", "s", "on"])
run_command(["/usr/bin/xset", "+dpms"])
def watch_callback(peer: str, arg: str | None) -> None:
log.debug("watch_callback called for peer %s with arg %s", peer, repr(arg))
if arg:
return
to_remove = {cookie for cookie, app in INHIBITORS.items() if app.peer == peer}
if not INHIBITORS or not to_remove:
return
for cookie in to_remove:
INHIBITORS.pop(cookie, None)
uninhibit()
def run_command(args: list[str]) -> None:
log.debug("Running command: %s", args)
try:
subprocess.run(args, check=True)
except Exception as e:
log.error("Error running command %s: %s", args, e)
def signal_handler(signum: int, _frame: Any) -> None:
signame = signal.Signals(signum).name
if signame == "SIGUSR1":
log.info("Inhibitors: %s", INHIBITORS)
return
log.info("Caught signal %s; exiting", signame)
uninhibit(True)
sys.exit(0)
if __name__ == "__main__":
sys.exit(main(parse_args()))