bumblebee-status/bumblebee_status/modules/contrib/caffeine.py

118 lines
3 KiB
Python
Raw Permalink Normal View History

# pylint: disable=C0111,R0903,W0212
2020-04-21 20:22:49 +02:00
"""Enable/disable automatic screen locking.
Requires the following executables:
* xdg-screensaver
* xdotool
* xprop (as dependency for xdotool)
* notify-send
contributed by `TheEdgeOfRage <https://github.com/TheEdgeOfRage>`_ - many thanks!
2020-04-21 20:22:49 +02:00
"""
import logging
import os
import shutil
2020-04-21 20:22:49 +02:00
import psutil
import core.module
import core.widget
import core.input
import core.decorators
import util.cli
class Module(core.module.Module):
@core.decorators.every(minutes=10)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(""))
self.__active = False
self.__xid = None
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__toggle)
def __check_requirements(self):
requirements = ["xdotool", "xprop", "xdg-screensaver"]
2020-04-21 20:22:49 +02:00
missing = []
for tool in requirements:
if not shutil.which(tool):
2020-04-21 20:22:49 +02:00
missing.append(tool)
return missing
def __get_i3bar_xid(self):
xid = (
util.cli.execute("xdotool search --class 'i3bar'")
.partition("\n")[0]
.strip()
)
2020-04-21 20:22:49 +02:00
if xid.isdigit():
return xid
logging.warning("Module caffeine: xdotool couldn't get X window ID of 'i3bar'.")
2020-04-21 20:22:49 +02:00
return None
def __notify(self):
if not shutil.which("notify-send"):
2020-04-21 20:22:49 +02:00
return
if self.__active:
util.cli.execute("notify-send 'Consuming caffeine'")
2020-04-21 20:22:49 +02:00
else:
util.cli.execute("notify-send 'Out of coffee'")
2020-04-21 20:22:49 +02:00
def _suspend_screensaver(self):
self.__xid = self.__get_i3bar_xid()
if self.__xid is None:
2020-04-21 20:22:49 +02:00
return False
pid = os.fork()
if pid == 0:
os.setsid()
util.cli.execute("xdg-screensaver suspend {}".format(self.__xid))
2020-04-21 20:22:49 +02:00
os._exit(0)
else:
os.waitpid(pid, 0)
return True
def __resume_screensaver(self):
2020-04-21 20:22:49 +02:00
success = True
xprop_path = shutil.which("xprop")
pids = [
p.pid
for p in psutil.process_iter()
if p.cmdline() == [xprop_path, "-id", str(self.__xid), "-spy"]
]
2020-04-21 20:22:49 +02:00
for pid in pids:
try:
os.kill(pid, 9)
except OSError:
success = False
return success
def state(self, _):
if self.__active:
return "activated"
return "deactivated"
2020-04-21 20:22:49 +02:00
def __toggle(self, _):
missing = self.__check_requirements()
2020-04-21 20:22:49 +02:00
if missing:
logging.warning("Could not run caffeine - missing %s!", ", ".join(missing))
2020-04-21 20:22:49 +02:00
return
self.__active = not self.__active
if self.__active:
2020-04-21 20:22:49 +02:00
success = self._suspend_screensaver()
else:
success = self.__resume_screensaver()
2020-04-21 20:22:49 +02:00
if success:
self.__notify()
2020-04-21 20:22:49 +02:00
else:
self.__active = not self.__active
2020-04-21 20:22:49 +02:00
2020-04-21 20:22:49 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4