From db07043d7c3dcf21ba6c8d0649305fc9a7597a79 Mon Sep 17 00:00:00 2001 From: "Ian W. Remmel" <1182361+ianwremmel@users.noreply.github.com> Date: Thu, 22 Mar 2018 09:03:11 -0700 Subject: [PATCH] augment macos screensaver detection with screen lock detection Fixes #168 --- ntfy/screensaver.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ntfy/screensaver.py b/ntfy/screensaver.py index 9674010..e6decb4 100644 --- a/ntfy/screensaver.py +++ b/ntfy/screensaver.py @@ -97,12 +97,24 @@ def macos_detect(): def macos_is_locked(): - # Strictly-speaking, this detects whether or not the screensaver is running. The screensaver + # Strictly-speaking, this detects whether or not the screensaver is running. The screensaver # may or may not be locked. cmd = '''tell application "System Events" get running of screen saver preferences end tell''' - return check_output([ 'osascript', '-e', cmd ]) == b'true\n' + screensaver_is_running = check_output( + ['osascript', '-e', cmd]) == b'true\n' + if screensaver_is_running: + return True + + # The screen may be locked even if the scrensaver is not running. This + # *should* cover that scenario. + # https: // stackoverflow.com/questions/11505255/osx-check-if-the-screen-is-locked + import Quartz + d = Quartz.CGSessionCopyCurrentDictionary() + screen_is_locked = d.get("CGSSessionScreenIsLocked", 0) == 1 + + return screen_is_locked def is_locked():