diff options
Diffstat (limited to 'deckit/sleep.py')
| -rw-r--r-- | deckit/sleep.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/deckit/sleep.py b/deckit/sleep.py new file mode 100644 index 0000000..69b5a05 --- /dev/null +++ b/deckit/sleep.py @@ -0,0 +1,82 @@ +# deck-it - telling the tablet the PC is going to sleep +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Tells the tablet the PC is about to sleep. + +elogind announces a suspend with PrepareForSleep on the system bus, and +waits for it only while someone holds a delay inhibitor (at most +InhibitDelayMaxSec, 5 s by default). deck-it holds one; on the announcement +the server marks every answer for SHOW seconds, so the tablet's next poll +sees it, then lets the inhibitor go and the PC sleeps. On resume it takes +the inhibitor again. +""" + +import ctypes +import os +import signal +import subprocess +import threading +import time + +SHOW = 2.0 # the tablet polls every second (0.5 s on the assistant tab) +sleeping = threading.Event() + + +def _die_with_parent(): + # PR_SET_PDEATHSIG: a server that dies takes dbus-monitor with it. + ctypes.CDLL(None, use_errno=True).prctl(1, signal.SIGTERM) + + +def _inhibit(): + # elogind-inhibit holds the lock while its command runs. The command + # ends with the server (tail --pid), so a crashed server never leaves a + # lock delaying every suspend; its own session, so letting go on purpose + # kills the tail too. + return subprocess.Popen( + ["elogind-inhibit", "--what=sleep", "--mode=delay", "--who=deck-it", + "--why=Tell the tablet the PC is going to sleep", + "tail", f"--pid={os.getpid()}", "-f", "/dev/null"], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + + +def _release(p): + try: + os.killpg(p.pid, signal.SIGTERM) + except ProcessLookupError: + pass + p.wait() + + +def watch(): + """Runs for the server's life; without elogind or dbus-monitor it does nothing.""" + try: + mon = subprocess.Popen( + ["dbus-monitor", "--system", + "type='signal',interface='org.freedesktop.login1.Manager',member='PrepareForSleep'"], + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, + preexec_fn=_die_with_parent) + lock = _inhibit() + except OSError: + return + for line in mon.stdout: + line = line.strip() + if line == "boolean true": + sleeping.set() + time.sleep(SHOW) + _release(lock) + elif line == "boolean false": + sleeping.clear() + lock = _inhibit() |
