Make sure state is reverted in case of error

This commit is contained in:
mw 2019-09-02 17:26:01 +02:00
parent 43b29eedd2
commit 43988db4cc

View file

@ -55,7 +55,7 @@ class Module(bumblebee.engine.Module):
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 return False
pid = os.fork() pid = os.fork()
if pid == 0: if pid == 0:
@ -64,12 +64,16 @@ class Module(bumblebee.engine.Module):
os._exit(0) os._exit(0)
else: else:
os.waitpid(pid, 0) os.waitpid(pid, 0)
return True
def _resume_screensaver(self): def _resume_screensaver(self):
for process in psutil.process_iter(): for process in psutil.process_iter():
if process.cmdline() == [bumblebee.util.which('xprop'), '-id', str(self._xid), '-spy']: if process.cmdline() == [bumblebee.util.which('xprop'), '-id', str(self._xid), '-spy']:
pid = process.pid try:
os.kill(pid, 9) os.kill(process.pid, 9)
except OSError:
return False
return True
def state(self, _): def state(self, _):
if self._active: if self._active:
@ -84,9 +88,13 @@ class Module(bumblebee.engine.Module):
self._active = not self._active self._active = not self._active
if self._active: if self._active:
self._suspend_screensaver() success = self._suspend_screensaver()
else: else:
self._resume_screensaver() success = self._resume_screensaver()
self._notify()
if success:
self._notify()
else:
self._active = not self._active
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4