-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbluetooth-lock.py
46 lines (40 loc) · 1.69 KB
/
bluetooth-lock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python
import sys
import os
import shutil
from optparse import OptionParser
import subprocess
import time
ENV = "KDE" # Can be 'KDE' or 'GNOME'
DEVICEADDR = "AA:BB:CC:DD:EE:FF" # bluetooth device address
CHECKINTERVAL = 60 # device pinged at this interval (seconds) when screen is unlocked
CHECKREPEAT = 2 # device must be unreachable this many times to lock
mode = 'unlocked'
if __name__ == "__main__":
while True:
tries = 0
while tries < CHECKREPEAT:
process = subprocess.Popen(['sudo', '/usr/bin/l2ping', DEVICEADDR, '-t', '1', '-c', '1'], shell=False, stdout=subprocess.PIPE)
process.wait()
if process.returncode == 0:
print("ping OK")
break
print("ping response code: %d" % (process.returncode))
time.sleep(1)
tries = tries + 1
if process.returncode == 0 and mode == 'locked':
mode = 'unlocked'
if ENV == "KDE":
subprocess.Popen(['loginctl', 'unlock-session', '1'], shell=False, stdout=subprocess.PIPE)
elif ENV == "GNOME":
subprocess.Popen(['gnome-screensaver-command', '--deactivate'], shell=False, stdout=subprocess.PIPE)
if process.returncode == 1 and mode == 'unlocked':
mode = 'locked'
if ENV == "KDE":
subprocess.Popen(['loginctl', 'lock-session', '1'], shell=False, stdout=subprocess.PIPE)
elif ENV == "GNOME":
subprocess.Popen(['gnome-screensaver-command', '--lock'], shell=False, stdout=subprocess.PIPE)
if mode == 'locked':
time.sleep(1)
else:
time.sleep(CHECKINTERVAL)