Skip to content

fix(macos): treat screen lock as AFK - #84

Open
0xbrayo wants to merge 1 commit into
ActivityWatch:masterfrom
0xbrayo:fix/macos-afk-on-screen-lock
Open

fix(macos): treat screen lock as AFK#84
0xbrayo wants to merge 1 commit into
ActivityWatch:masterfrom
0xbrayo:fix/macos-afk-on-screen-lock

Conversation

@0xbrayo

@0xbrayo 0xbrayo commented Jul 29, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #66 — when the macOS screen is locked, aw-watcher-afk now reports AFK even if HID idle time is still low (e.g. mouse jiggle or background audio).

Previously, idle detection only used CGEventSourceSecondsSinceLastEventType, which has no awareness of lock state. Locking the screen therefore often left the user as not-afk.

Changes

  • macOS: detect lock via CGSessionCopyCurrentDictionary / CGSSessionScreenIsLocked (as suggested in the issue thread)
  • AFK loop: become AFK immediately when the screen is locked; stay AFK while locked even if synthetic input keeps HID idle low
  • Windows / Linux: stub is_screen_locked() returning False (same shape as macOS for a shared call site; real implementations left as TODO)

Unlock handling is unchanged: once unlocked, normal idle timeout decides when to leave AFK (typing/Touch ID counts as input).

Note on existing PR

Related to #69, which takes a similar approach but still allows leaving AFK while locked if HID idle resets, and adds a fixed 5s delay before lock counts as AFK. This PR forces AFK for the whole lock period with no extra delay.

Test plan

  • Verified is_screen_locked() returns False when unlocked and that CGSSessionScreenIsLocked is absent from the session dict when unlocked
  • Run aw-watcher-afk --testing --verbose, lock screen (Ctrl-Cmd-Q), confirm logs show Became AFK (screen locked) / Screen locked: True
  • With screen locked, confirm status stays afk even if idle would otherwise reset
  • Unlock and confirm watcher returns to not-afk after activity
  • On Linux/Windows, confirm no behavior change (stub returns False)

HID idle time alone misses lock-screen sessions when input still occurs
(mouse jiggle, background audio, etc.). Detect lock via
CGSessionCopyCurrentDictionary / CGSSessionScreenIsLocked and force AFK
while locked; do not return to not-afk until the screen is unlocked.

Fixes ActivityWatch#66
@greptile-apps

greptile-apps Bot commented Jul 29, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds macOS screen-lock detection to AFK classification.

  • Reads CGSSessionScreenIsLocked from the current CoreGraphics session.
  • Keeps the watcher AFK while macOS reports the screen as locked.
  • Adds matching no-op lock-state functions for Linux and Windows.

Confidence Score: 3/5

The PR should not merge until lock-triggered AFK transitions use the lock-detection time rather than backdating the interval to the last HID input.

The new lock condition reaches the existing idle-time transition code, causing AFK events to begin before the screen was locked whenever the latest input predates lock detection.

Files Needing Attention: aw_watcher_afk/afk.py

Important Files Changed

Filename Overview
aw_watcher_afk/afk.py Adds lock-aware transitions, but incorrectly timestamps a lock-triggered AFK interval from the last HID input.
aw_watcher_afk/macos.py Adds CoreGraphics session-dictionary lock detection with a safe false result when no dictionary is available.
aw_watcher_afk/unix.py Adds a Linux lock-state stub that preserves existing behavior.
aw_watcher_afk/windows.py Adds a Windows lock-state stub that preserves existing behavior.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Poll idle and lock state] --> B{Currently AFK?}
    B -->|Yes| C{Unlocked and input below timeout?}
    C -->|Yes| D[Transition to not-afk]
    C -->|No| E[Continue AFK]
    B -->|No| F{Locked or idle above timeout?}
    F -->|Yes| G[Transition to AFK]
    F -->|No| H[Continue not-afk]
Loading

Comments Outside Diff (1)

  1. aw_watcher_afk/afk.py, line 103-111 (link)

    P1 Lock transition uses input time

    When the screen is locked after a nonzero HID-idle interval, this branch timestamps the transition from last_input rather than the lock-detection time, causing ActivityWatch to record unlocked time before the lock as AFK.

Reviews (1): Last reviewed commit: "fix(macos): treat screen lock as AFK" | Re-trigger Greptile

@0xbrayo
0xbrayo marked this pull request as draft July 29, 2026 08:37
@0xbrayo
0xbrayo marked this pull request as ready for review August 17, 2026 13:25
@0xbrayo

0xbrayo commented Aug 17, 2026

Copy link
Copy Markdown
Member Author

Tested on MacOS

@0xbrayo

0xbrayo commented Aug 22, 2026

Copy link
Copy Markdown
Member Author

@TimeToBuildBob review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[macOS] Set state to AFK when screen is locked or computer is asleep

1 participant