diff --git a/aw_watcher_afk/afk.py b/aw_watcher_afk/afk.py index e188fd7..04a860e 100755 --- a/aw_watcher_afk/afk.py +++ b/aw_watcher_afk/afk.py @@ -13,13 +13,13 @@ if system == "Windows": # noreorder - from .windows import seconds_since_last_input # fmt: skip + from .windows import seconds_since_last_input, lock_screen_shown # fmt: skip elif system == "Darwin": # noreorder - from .macos import seconds_since_last_input # fmt: skip + from .macos import seconds_since_last_input, lock_screen_shown # fmt: skip elif system == "Linux": # noreorder - from .unix import seconds_since_last_input # fmt: skip + from .unix import seconds_since_last_input, lock_screen_shown # fmt: skip else: raise Exception(f"Unsupported platform: {system}") @@ -84,8 +84,10 @@ def heartbeat_loop(self): now = datetime.now(timezone.utc) seconds_since_input = seconds_since_last_input() + showing_lock_screen = lock_screen_shown() last_input = now - timedelta(seconds=seconds_since_input) logger.debug(f"Seconds since last input: {seconds_since_input}") + logger.debug(f"Lock screen shown: {showing_lock_screen}") # If no longer AFK if afk and seconds_since_input < self.settings.timeout: @@ -95,7 +97,7 @@ def heartbeat_loop(self): # ping with timestamp+1ms with the next event (to ensure the latest event gets retrieved by get_event) self.ping(afk, timestamp=last_input + td1ms) # If becomes AFK - elif not afk and seconds_since_input >= self.settings.timeout: + elif not afk and (seconds_since_input >= self.settings.timeout or (showing_lock_screen and seconds_since_input >= 5)): logger.info("Became AFK") self.ping(afk, timestamp=last_input) afk = True diff --git a/aw_watcher_afk/macos.py b/aw_watcher_afk/macos.py index 5fd2717..5bf8bdd 100755 --- a/aw_watcher_afk/macos.py +++ b/aw_watcher_afk/macos.py @@ -1,6 +1,14 @@ from Quartz.CoreGraphics import (CGEventSourceSecondsSinceLastEventType, kCGEventSourceStateHIDSystemState, - kCGAnyInputEventType) + kCGAnyInputEventType, + CGSessionCopyCurrentDictionary) + + +def lock_screen_shown() -> bool: + session = CGSessionCopyCurrentDictionary() + if "CGSSessionScreenIsLocked" in session: + return session["CGSSessionScreenIsLocked"] == True + return False def seconds_since_last_input() -> float: @@ -11,4 +19,4 @@ def seconds_since_last_input() -> float: from time import sleep while True: sleep(1) - print(seconds_since_last_input()) + print(seconds_since_last_input(), lock_screen_shown()) diff --git a/aw_watcher_afk/unix.py b/aw_watcher_afk/unix.py index 42a7b9f..582d739 100755 --- a/aw_watcher_afk/unix.py +++ b/aw_watcher_afk/unix.py @@ -43,7 +43,12 @@ def seconds_since_last_input(): return _last_input_unix.seconds_since_last_input() +def lock_screen_shown() -> bool: + # TODO: Linux implementation + return False + + if __name__ == "__main__": while True: sleep(1) - print(seconds_since_last_input()) + print(seconds_since_last_input(), lock_screen_shown()) diff --git a/aw_watcher_afk/windows.py b/aw_watcher_afk/windows.py index a55f7cb..106958a 100644 --- a/aw_watcher_afk/windows.py +++ b/aw_watcher_afk/windows.py @@ -26,6 +26,11 @@ def _getTickCount() -> int: return c_GetTickCount() +def lock_screen_shown() -> bool: + # TODO: Windows implementation + return False + + def seconds_since_last_input(): seconds_since_input = (_getTickCount() - _getLastInputTick()) / 1000 return seconds_since_input @@ -34,4 +39,4 @@ def seconds_since_last_input(): if __name__ == "__main__": while True: time.sleep(1) - print(seconds_since_last_input()) + print(seconds_since_last_input(), lock_screen_shown())