-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen_monitor.py
executable file
·57 lines (43 loc) · 1.31 KB
/
screen_monitor.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
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import subprocess
import time
# --- set both commands (connect / disconnect) below
connect_command = "bash ~/.screenlayout/office_docked.sh"
disconnect_command = "bash ~/.screenlayout/laptop.sh"
# ---
# Wait for xrandr to startup before executing the changes.
while True:
time.sleep(5)
try:
subprocess.Popen(["xrandr"])
except:
pass
else:
break
def get(cmd):
return subprocess.check_output(cmd).decode("utf-8")
# - to count the occurrenc of " connected "
def count_screens(xr):
return xr.count(" connected ")
# - to run the connect / disconnect command(s)
def run_command(cmd):
subprocess.Popen(["/bin/bash", "-c", cmd])
# first count
xr1 = None
while True:
time.sleep(5)
# second count
xr2 = count_screens(get(["xrandr"]))
# check if there is a change in the screen state
if xr2 != xr1:
if xr2 == 2:
# command to run if connected (two screens)
run_command(connect_command)
elif xr2 == 1:
# command to run if disconnected (one screen)
# uncomment run_command(disconnect_command) to enable,
# then also comment out pass
# pass
run_command(disconnect_command)
# set the second count as initial state for the next loop
xr1 = xr2