bumblebee-status/bumblebee/modules/caffeine.py

57 lines
1.7 KiB
Python
Raw Normal View History

2019-08-25 20:53:42 +02:00
#pylint: disable=C0111,R0903
"""Enable/disable automatic screen locking.
Requires the following executables:
2019-08-25 20:53:42 +02:00
* xdg-screensaver
* xdotool
* notify-send
"""
import bumblebee.input
import bumblebee.output
import bumblebee.engine
import psutil
import os
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
self._xid = 0
2019-08-25 20:53:42 +02:00
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
cmd=self._toggle
)
def state(self, widget):
2019-08-25 20:53:42 +02:00
if self._active:
return "activated"
return "deactivated"
2019-08-25 20:53:42 +02:00
def _toggle(self, event):
self._active = not self._active
try:
if self._active:
self._xid = bumblebee.util.execute("xdotool search --class \"i3bar\"").strip()
2019-08-28 00:09:04 +02:00
pid = os.fork()
if pid == 0:
os.setsid()
bumblebee.util.execute("xdg-screensaver suspend {}".format(self._xid))
os._exit(0)
else:
os.waitpid(pid,0)
bumblebee.util.execute("notify-send \"Consuming caffeine\"")
else:
for process in psutil.process_iter():
if process.cmdline() == ['/usr/bin/xprop','-id',str(self._xid),'-spy']:
pid = process.pid
os.kill(pid,9)
bumblebee.util.execute("notify-send \"Out of coffee\"")
except:
self._active = not self._active
2019-08-25 20:53:42 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4