git.slaskete.net
/
einar-bin
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
d903416
)
Be more smarter when deciding when to lock screen
author
Einar Jørgen Haraldseid
<einar.haraldseid@ntnu.no>
Wed, 8 Feb 2023 15:31:12 +0000
(16:31 +0100)
committer
Einar Jørgen Haraldseid
<einar.haraldseid@ntnu.no>
Wed, 8 Feb 2023 15:31:12 +0000
(16:31 +0100)
autolock.py
patch
|
blob
|
history
diff --git
a/autolock.py
b/autolock.py
index 0fddaad3dcfcbe90e51c395b40723146eff729df..b02642b4e2c612cd1814b3fb2e48e074743ba6cc 100644
(file)
--- a/
autolock.py
+++ b/
autolock.py
@@
-1,12
+1,14
@@
+#!/usr/bin/env python
# Automatically lock the screen when bluetooth beacon moves out of range
# Automatically lock the screen when bluetooth beacon moves out of range
-# Requires GNOME Shell
-from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement
import time
import time
+from beacontools import BeaconScanner, IBeaconFilter, IBeaconAdvertisement
import signal
import dbus
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()
def handler(signum, frame):
scanner.stop()
@@
-17,12
+19,16
@@
def callback(bt_addr, rssi, packet, additional_info):
isHere = True
sessionBus = dbus.SessionBus()
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")
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
# 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)
signal.signal(signal.SIGINT, handler)
+beaconTimeout=0
+
while True:
isHere = False
scanner = BeaconScanner(callback,
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()
)
scanner.start()
- time.sleep(
45
)
+ time.sleep(
10
)
scanner.stop()
scanner.stop()
+ idleTime = sessionIdleTime() / 1000
if isHere:
if isHere:
- print("Beacon is here")
+ #print("Beacon observed")
+ beaconTimeout = 0
else:
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)