-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor-toggle
More file actions
executable file
·121 lines (96 loc) · 2.84 KB
/
Copy pathmonitor-toggle
File metadata and controls
executable file
·121 lines (96 loc) · 2.84 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
internalMonitor="eDP1"
externalPosition="above"
function externalMonitor() {
xrandr | grep " connected" | sed -Ee "s/^(\w+) connected .*/\1/gm" -e '2!d'
}
function monitorIsEnabled() {
[[ -n "$(xrandr | grep "$1" | grep "+")" ]]
}
function monitorIsConnected() {
[[ -n "$(xrandr | grep "$1" | grep " connected")" ]]
}
function requireMonitor() {
if ! monitorIsConnected "$1"; then
echo "Monitor $1 is not connected"
return 1;
fi
}
function enableProfile() {
profile=${1:-"default"}
# Values: left-of, right-of, above, below or same-as
position="${2:-"$externalPosition"}"
case "$profile" in
one)
if requireMonitor $externalMonitor; then
xrandr --output $internalMonitor --off \
--output $externalMonitor --auto #--scale 0.8x0.8
fi
;;
two)
if requireMonitor $externalMonitor; then
xrandr --output "$internalMonitor" --off \
--output "$externalMonitor" --auto
xrandr --output "$internalMonitor" --auto \
--output "$externalMonitor" --$position "$internalMonitor" --auto
fi
;;
default|*) # and fallback
xrandr --output $externalMonitor --off \
--output $internalMonitor --auto
;;
esac
afterProfileChange
}
function afterProfileChange() {
# this was needed to fix i3 bug, maybe no longer needed
# if [ -n "$(ps -A | grep i3)" ]; then
# i3-msg restart
# fi
nitrogen --restore # re-adjust wallpaper
}
function detectCurrentProfile() {
currentProfile="none"
if monitorIsConnected "$externalMonitor" && monitorIsEnabled "$externalMonitor"; then
currentProfile="one"
fi
if monitorIsEnabled "$internalMonitor"; then
if [ "$currentProfile" == "none" ]; then
currentProfile="default"
else
currentProfile="two"
fi
fi
echo $currentProfile
}
function toggleMonitors() {
profile="$1"
currentProfile="$(detectCurrentProfile)"
if [[ "$currentProfile" =~ ^($profile|none)$ ]]; then
profile="default"
fi
enableProfile $profile $2
}
function hotplugMonitors() {
currentProfile="$(detectCurrentProfile)"
if monitorIsConnected "$externalMonitor"; then
if [[ "$currentProfile" =~ ^(default|none)$ ]]; then
enableProfile one
fi
elif [ "$currentProfile" != "default" ]; then
enableProfile default
fi
}
profile=${1:-"one"}
externalMonitor="$(externalMonitor)" # "HDMI1" or "DP1" or "DP2"
if [ "$profile" == "hotplug" ]; then
while true; do
hotplugMonitors
sleep 5
done
elif [ "$profile" == "auto" ]; then
hotplugMonitors
afterProfileChange
else
toggleMonitors $profile $2
fi