Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Mark as AFK when lock screen is shown #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions aw_watcher_afk/afk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions aw_watcher_afk/macos.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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())
7 changes: 6 additions & 1 deletion aw_watcher_afk/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
7 changes: 6 additions & 1 deletion aw_watcher_afk/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def _getTickCount() -> int:
return c_GetTickCount()


def lock_screen_shown() -> bool:
# TODO: Windows implementation
return False
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock_screen_shown function is not implemented for Windows. This will cause the feature to not work on Windows systems. Consider implementing this function to detect if the lock screen is shown on Windows.



def seconds_since_last_input():
seconds_since_input = (_getTickCount() - _getLastInputTick()) / 1000
return seconds_since_input
Expand All @@ -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())