2020-04-21 20:22:49 +02:00
|
|
|
#pylint: disable=C0111,R0903,W0212
|
|
|
|
|
|
|
|
"""Enable/disable automatic screen locking.
|
|
|
|
|
|
|
|
Requires the following executables:
|
|
|
|
* xdg-screensaver
|
|
|
|
* xdotool
|
|
|
|
* xprop (as dependency for xdotool)
|
|
|
|
* notify-send
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
2020-04-21 20:27:50 +02:00
|
|
|
import shutil
|
2020-04-21 20:22:49 +02:00
|
|
|
import psutil
|
2020-04-21 20:27:50 +02:00
|
|
|
|
|
|
|
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)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(''))
|
2020-04-21 20:27:50 +02:00
|
|
|
|
|
|
|
self.__active = False
|
|
|
|
self.__xid = None
|
|
|
|
|
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__toggle)
|
|
|
|
|
|
|
|
def __check_requirements(self):
|
2020-04-21 20:22:49 +02:00
|
|
|
requirements = ['xdotool', 'xprop', 'xdg-screensaver']
|
|
|
|
missing = []
|
|
|
|
for tool in requirements:
|
2020-04-21 20:27:50 +02:00
|
|
|
if not shutil.which(tool):
|
2020-04-21 20:22:49 +02:00
|
|
|
missing.append(tool)
|
|
|
|
return missing
|
|
|
|
|
2020-04-21 20:27:50 +02:00
|
|
|
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
|
2020-04-21 20:27:50 +02:00
|
|
|
logging.warning('Module caffeine: xdotool couldn\'t get X window ID of \'i3bar\'.')
|
2020-04-21 20:22:49 +02:00
|
|
|
return None
|
|
|
|
|
2020-04-21 20:27:50 +02:00
|
|
|
def __notify(self):
|
|
|
|
if not shutil.which('notify-send'):
|
2020-04-21 20:22:49 +02:00
|
|
|
return
|
|
|
|
|
2020-04-21 20:27:50 +02:00
|
|
|
if self.__active:
|
|
|
|
util.cli.execute('notify-send \'Consuming caffeine\'')
|
2020-04-21 20:22:49 +02:00
|
|
|
else:
|
2020-04-21 20:27:50 +02:00
|
|
|
util.cli.execute('notify-send \'Out of coffee\'')
|
2020-04-21 20:22:49 +02:00
|
|
|
|
|
|
|
def _suspend_screensaver(self):
|
2020-04-21 20:27:50 +02:00
|
|
|
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()
|
2020-04-21 20:27:50 +02:00
|
|
|
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
|
|
|
|
|
2020-04-21 20:27:50 +02:00
|
|
|
def __resume_screensaver(self):
|
2020-04-21 20:22:49 +02:00
|
|
|
success = True
|
2020-04-21 20:27:50 +02:00
|
|
|
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, _):
|
2020-04-21 20:27:50 +02:00
|
|
|
if self.__active:
|
2020-04-21 20:23:08 +02:00
|
|
|
return 'activated'
|
|
|
|
return 'deactivated'
|
2020-04-21 20:22:49 +02:00
|
|
|
|
2020-04-21 20:27:50 +02:00
|
|
|
def __toggle(self, _):
|
|
|
|
missing = self.__check_requirements()
|
2020-04-21 20:22:49 +02:00
|
|
|
if missing:
|
2020-04-21 20:23:08 +02:00
|
|
|
logging.warning('Could not run caffeine - missing %s!', ', '.join(missing))
|
2020-04-21 20:22:49 +02:00
|
|
|
return
|
|
|
|
|
2020-04-21 20:27:50 +02:00
|
|
|
self.__active = not self.__active
|
|
|
|
if self.__active:
|
2020-04-21 20:22:49 +02:00
|
|
|
success = self._suspend_screensaver()
|
|
|
|
else:
|
2020-04-21 20:27:50 +02:00
|
|
|
success = self.__resume_screensaver()
|
2020-04-21 20:22:49 +02:00
|
|
|
|
|
|
|
if success:
|
2020-04-21 20:27:50 +02:00
|
|
|
self.__notify()
|
2020-04-21 20:22:49 +02:00
|
|
|
else:
|
2020-04-21 20:27:50 +02:00
|
|
|
self.__active = not self.__active
|
2020-04-21 20:22:49 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|