Directly track running swayidle process (#470)

We actually don't need to use a separate variable to track the status of
the spawned swayidle process, since we can poll the status directly from
the process object. This increases robustness against e.g. a killed
swayidle process, which would cause safeeyes to be permanently disabled
until a restart.
This commit is contained in:
Ankit Raj Pandey 2021-11-16 07:52:12 -06:00 committed by GitHub
parent 2e7df5c880
commit ed60dd1970
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 9 deletions

View File

@ -47,12 +47,15 @@ interpret_idle_as_break = False
is_wayland_and_gnome = False
use_swayidle = False
swayidle_running = False
swayidle_process = None
swayidle_lock = threading.Lock()
swayidle_idle = 0
swayidle_active = 0
def __swayidle_running():
return (swayidle_process is not None and
swayidle_process.poll() is None)
def __start_swayidle_monitor():
global swayidle_process
global swayidle_start
@ -72,20 +75,14 @@ def __start_swayidle_monitor():
swayidle_active = timestamp
def __stop_swayidle_monitor():
global swayidle_running
swayidle_lock.acquire()
if swayidle_running:
if __swayidle_running():
logging.debug('Stopping swayidle subprocess')
swayidle_process.terminate()
swayidle_running = False
swayidle_lock.release()
def __swayidle_idle_time():
global swayidle_running
with swayidle_lock:
if not swayidle_running:
if not __swayidle_running():
utility.start_thread(__start_swayidle_monitor)
swayidle_running = True
# Idle more recently than active, meaning idle time isn't stale.
if swayidle_idle > swayidle_active:
idle_time = int(datetime.datetime.now().timestamp()) - swayidle_idle