bumblebee-status/bumblebee/modules/caffeine.py

103 lines
2.9 KiB
Python
Raw Permalink Normal View History

#pylint: disable=C0111,R0903,W0212
"""Enable/disable automatic screen locking.
Requires the following executables:
2019-08-25 20:53:42 +02:00
* xdg-screensaver
* xdotool
2019-09-01 22:38:22 +02:00
* xprop (as dependency for xdotool)
* notify-send
"""
2019-09-01 22:38:22 +02:00
import logging
2019-09-01 22:54:38 +02:00
import os
import psutil
import bumblebee.input
import bumblebee.output
import bumblebee.engine
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
super(Module, self).__init__(engine, config,
2019-08-25 20:53:42 +02:00
bumblebee.output.Widget(full_text="")
)
2019-08-25 20:53:42 +02:00
self._active = False
2019-09-01 22:38:22 +02:00
self._xid = None
2019-09-02 16:42:25 +02:00
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd=self._toggle
)
2019-09-01 22:38:22 +02:00
def _check_requirements(self):
2019-09-02 16:42:25 +02:00
requirements = ['xdotool', 'xprop', 'xdg-screensaver']
2019-09-01 22:38:22 +02:00
missing = []
for tool in requirements:
if not bumblebee.util.which(tool):
missing.append(tool)
return missing
def _get_i3bar_xid(self):
xid = bumblebee.util.execute("xdotool search --class \"i3bar\"").partition('\n')[0].strip()
if xid.isdigit():
return xid
logging.warning("Module caffeine: xdotool couldn't get X window ID of \"i3bar\".")
2019-09-01 22:38:22 +02:00
return None
def _notify(self):
if not bumblebee.util.which('notify-send'):
return
if self._active:
bumblebee.util.execute("notify-send \"Consuming caffeine\"")
else:
bumblebee.util.execute("notify-send \"Out of coffee\"")
def _suspend_screensaver(self):
self._xid = self._get_i3bar_xid()
2019-09-01 22:54:38 +02:00
if self._xid is None:
return False
2019-09-02 16:42:25 +02:00
2019-09-01 22:38:22 +02:00
pid = os.fork()
if pid == 0:
os.setsid()
bumblebee.util.execute("xdg-screensaver suspend {}".format(self._xid))
os._exit(0)
else:
2019-09-02 16:42:25 +02:00
os.waitpid(pid, 0)
return True
2019-09-02 16:42:25 +02:00
2019-09-01 22:38:22 +02:00
def _resume_screensaver(self):
2019-09-05 20:48:19 +02:00
success = True
2019-09-05 21:08:05 +02:00
xprop_path = bumblebee.util.which('xprop')
2019-09-05 21:57:32 +02:00
pids = [ p.pid for p in psutil.process_iter() if p.cmdline() == [xprop_path, '-id', str(self._xid), '-spy'] ]
2019-09-05 20:48:19 +02:00
for pid in pids:
try:
2019-09-05 21:57:32 +02:00
os.kill(pid, 9)
2019-09-05 20:48:19 +02:00
except OSError:
success = False
return success
2019-09-01 22:38:22 +02:00
2019-09-02 16:42:25 +02:00
def state(self, _):
2019-08-25 20:53:42 +02:00
if self._active:
return "activated"
return "deactivated"
2019-09-02 16:42:25 +02:00
def _toggle(self, _):
2019-09-01 22:38:22 +02:00
missing = self._check_requirements()
if missing:
logging.warning('Could not run caffeine - missing %s!', ", ".join(missing))
2019-09-01 22:38:22 +02:00
return
2019-08-25 20:53:42 +02:00
self._active = not self._active
2019-09-01 22:38:22 +02:00
if self._active:
success = self._suspend_screensaver()
else:
success = self._resume_screensaver()
if success:
self._notify()
2019-09-01 22:38:22 +02:00
else:
self._active = not self._active
2019-08-25 20:53:42 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4