Be more smarter when deciding when to lock screen
[einar-bin] / autolock.py
index 0fddaad3dcfcbe90e51c395b40723146eff729df..b02642b4e2c612cd1814b3fb2e48e074743ba6cc 100644 (file)
@@ -1,12 +1,14 @@
+#!/usr/bin/env python
 # Automatically lock the screen when bluetooth beacon moves out of range
-# Requires GNOME Shell
 
-from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement
 import time
+from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement
 import signal
 import dbus
+from gi.repository import Gio
 
-global isHere, screenSaverSetActive, screenSaverGetActive
+global isHere, beaconTimeout
+global screenSaverSetActive, screenSaverGetActive, sessionIdleTime
 
 def handler(signum, frame):
     scanner.stop()
@@ -17,12 +19,16 @@ def callback(bt_addr, rssi, packet, additional_info):
     isHere = True
 
 sessionBus = dbus.SessionBus()
+
 screenSaver = sessionBus.get_object("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver")
 screenSaverIface = dbus.Interface(screenSaver, 'org.gnome.ScreenSaver')
 screenSaverSetActive = screenSaverIface.get_dbus_method("SetActive")
 screenSaverGetActive = screenSaverIface.get_dbus_method("GetActive")
 
-# To find beacon:
+mutter = sessionBus.get_object('org.gnome.Mutter.IdleMonitor', '/org/gnome/Mutter/IdleMonitor/Core')
+mutterIface = dbus.Interface(mutter, 'org.gnome.Mutter.IdleMonitor')
+sessionIdleTime = mutterIface.get_dbus_method("GetIdletime")
+
 # scan for all iBeacon advertisements regardless from which beacon
 #scanner = BeaconScanner(callback,
 #    packet_filter=IBeaconAdvertisement
@@ -30,19 +36,25 @@ screenSaverGetActive = screenSaverIface.get_dbus_method("GetActive")
 
 signal.signal(signal.SIGINT, handler)
 
+beaconTimeout=0
+
 while True:
     isHere = False
     scanner = BeaconScanner(callback,
-        device_filter=IBeaconFilter(uuid="INSERT YOUR UUID HERE")
+        device_filter=IBeaconFilter(uuid="INSERT BEACON UUID HERE")
     )
     scanner.start()
-    time.sleep(45)
+    time.sleep(10)
     scanner.stop()
+    idleTime = sessionIdleTime() / 1000
 
     if isHere:
-        print("Beacon is here")
+        #print("Beacon observed")
+        beaconTimeout = 0
     else:
-        print("Beacon is gone")
-        if not screenSaverGetActive():
-            print("Locking screen")
-            screenSaverSetActive(True)
+        beaconTimeout += 10
+        #print("Beacon not observed,", idleTime, "seconds since last activity,", beaconTimeout, "seconds since beacon seen")
+        if idleTime >= 60 and beaconTimeout >= 60:
+            if not screenSaverGetActive():
+                print("No activity and not seen for at least 60 seconds, locking screen")
+                screenSaverSetActive(True)