[modules/caffeine] update to latest API

This commit is contained in:
tobi-wan-kenobi 2020-04-21 20:27:50 +02:00
parent 6ff8932c99
commit 04487d0764

View file

@ -11,65 +11,68 @@ Requires the following executables:
import logging import logging
import os import os
import shutil
import psutil import psutil
import bumblebee.input
import bumblebee.output
import bumblebee.engine
class Module(bumblebee.engine.Module): import core.module
def __init__(self, engine, config): import core.widget
super(Module, self).__init__(engine, config, import core.input
bumblebee.output.Widget(full_text='') import core.decorators
)
self._active = False
self._xid = None
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, import util.cli
cmd=self._toggle
)
def _check_requirements(self): class Module(core.module.Module):
@core.decorators.every(minutes=10)
def __init__(self, config):
super().__init__(config, 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'] requirements = ['xdotool', 'xprop', 'xdg-screensaver']
missing = [] missing = []
for tool in requirements: for tool in requirements:
if not bumblebee.util.which(tool): if not shutil.which(tool):
missing.append(tool) missing.append(tool)
return missing return missing
def _get_i3bar_xid(self): def __get_i3bar_xid(self):
xid = bumblebee.util.execute('xdotool search --class \'i3bar\'').partition('\n')[0].strip() xid = util.cli.execute('xdotool search --class \'i3bar\'').partition('\n')[0].strip()
if xid.isdigit(): if xid.isdigit():
return xid return xid
logging.warning('Module caffeine: xdotool couldn't get X window ID of \'i3bar\'.') logging.warning('Module caffeine: xdotool couldn\'t get X window ID of \'i3bar\'.')
return None return None
def _notify(self): def __notify(self):
if not bumblebee.util.which('notify-send'): if not shutil.which('notify-send'):
return return
if self._active: if self.__active:
bumblebee.util.execute('notify-send \'Consuming caffeine\'') util.cli.execute('notify-send \'Consuming caffeine\'')
else: else:
bumblebee.util.execute('notify-send \'Out of coffee\'') util.cli.execute('notify-send \'Out of coffee\'')
def _suspend_screensaver(self): def _suspend_screensaver(self):
self._xid = self._get_i3bar_xid() self.__xid = self.__get_i3bar_xid()
if self._xid is None: if self.__xid is None:
return False return False
pid = os.fork() pid = os.fork()
if pid == 0: if pid == 0:
os.setsid() os.setsid()
bumblebee.util.execute('xdg-screensaver suspend {}'.format(self._xid)) util.cli.execute('xdg-screensaver suspend {}'.format(self.__xid))
os._exit(0) os._exit(0)
else: else:
os.waitpid(pid, 0) os.waitpid(pid, 0)
return True return True
def _resume_screensaver(self): def __resume_screensaver(self):
success = True success = True
xprop_path = bumblebee.util.which('xprop') xprop_path = shutil.which('xprop')
pids = [ p.pid for p in psutil.process_iter() if p.cmdline() == [xprop_path, '-id', str(self._xid), '-spy'] ] pids = [ p.pid for p in psutil.process_iter() if p.cmdline() == [xprop_path, '-id', str(self.__xid), '-spy'] ]
for pid in pids: for pid in pids:
try: try:
os.kill(pid, 9) os.kill(pid, 9)
@ -78,25 +81,25 @@ class Module(bumblebee.engine.Module):
return success return success
def state(self, _): def state(self, _):
if self._active: if self.__active:
return 'activated' return 'activated'
return 'deactivated' return 'deactivated'
def _toggle(self, _): def __toggle(self, _):
missing = self._check_requirements() missing = self.__check_requirements()
if missing: if missing:
logging.warning('Could not run caffeine - missing %s!', ', '.join(missing)) logging.warning('Could not run caffeine - missing %s!', ', '.join(missing))
return return
self._active = not self._active self.__active = not self.__active
if self._active: if self.__active:
success = self._suspend_screensaver() success = self._suspend_screensaver()
else: else:
success = self._resume_screensaver() success = self.__resume_screensaver()
if success: if success:
self._notify() self.__notify()
else: else:
self._active = not self._active self.__active = not self.__active
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4