This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
forked from Tasssadar/bugfree-tribble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_screen.sh
executable file
·137 lines (118 loc) · 3.68 KB
/
set_screen.sh
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh
if [ -z "$DISPLAY" ]; then
export DISPLAY=:0
fi
OUTPUTS="LVDS1 VGA1 HDMI1"
OUTPUTS_CNT="3"
# args: [xrandr line] [output]
grep_for_active() {
echo $1 | grep -E -q "^$2 connected .*[0-9]{1,4}x[0-9]{1,4}\\+.*\\(.*\\).*$" && echo "true" || echo "false"
}
# args: [xrandr line]
grep_for_resolution() {
echo $1 | grep -E -o "[0-9]{1,4}x[0-9]{1,4}" || echo "0x0"
}
# args: [array] [output] [print to stdout]
search_output_array() {
array="$1"
for o in $OUTPUTS; do
if [ "$2" = "$o" ]; then
([ "$3" = "true" ]) && echo ${array%% *}
([ ${array%% *} = "true" ]) && return 0 || return 1
else
array=${array#* }
fi
done
return 1
}
# args: [output]
is_connected() {
search_output_array "$connected" "$1"
return $?
}
# args: [output]
is_active() {
search_output_array "$active" "$1"
return $?
}
# args: [output]
get_resolution() {
echo $(search_output_array "$resolution" "$1" "true")
}
print_vals() {
echo "connected: $connected"
echo "active: $active"
echo "resolutions: $resolution"
}
counter=0
connected=""
active=""
resolution=""
tmp_file=$(mktemp)
xrandr > "$tmp_file"
while read line; do
for o in $OUTPUTS; do
# check if line starts with watched output
([ "${line##$o*}" ]) && continue
counter=$((counter+1))
if [ ! "${line##$o connected*}" ]; then
connected="${connected}true "
else
connected="${connected}false "
fi
active="${active}$(grep_for_active "$line" "$o") "
resolution="${resolution}$(grep_for_resolution "$line") "
done
done < "$tmp_file"
rm "$tmp_file"
if [ "$counter" != "$OUTPUTS_CNT" ]; then
echo "counter != OUTPUTS_CNT, something went wrong when reading xrandr!"
exit 1
fi
print_vals
if is_active "LVDS1"; then
echo "- LVDS1 active"
# If VGA1 is connected, try to clone the screen to it, in 1024x768
if is_connected "VGA1"; then
if ! is_active "VGA1"; then
echo "-- Start VGA1"
xrandr --output LVDS1 --mode "1024x768" --output VGA1 --mode "1024x768"
else
echo "-- Stop VGA1"
xrandr --output LVDS1 --auto --output VGA1 --off
fi
# If HDMI is connected, try to switch to it
elif is_connected "HDMI1"; then
if ! is_active "HDMI1"; then
echo "-- Start HDMI1"
xrandr --output LVDS1 --off --output HDMI1 --auto
elif [ "$(get_resolution "HDMI1")" = "1024x768" ] && [ "$(get_resolution "LVDS1")" = "1024x768" ]; then
echo "-- Stop LVDS1"
echo "-- Set HDMI1 to auto"
xrandr --output LVDS1 --off --output HDMI1 --auto
else
echo "-- Stop LVDS1"
xrandr --output LVDS1 --off
fi
# If nothing is connected, turn everything else than LVDS1 off and switch to 1024x768 and back
else
if [ "$(get_resolution "LVDS1")" = "1366x768" ]; then
echo "-- setting LVDS1 to 1024x768"
xrandr --output LVDS1 --mode "1024x768" --output VGA1 --off --output HDMI1 --off
else
echo "-- setting LVDS1 to auto"
xrandr --output LVDS1 --auto --output VGA1 --off --output HDMI1 --off
fi
fi
else
echo "- LVDS1 inactive"
# If HDMI1 is active, stop it and switch to LVDS1
if is_active "HDMI1"; then
echo "-- Stop HDMI1"
xrandr --output LVDS1 --auto --output HDMI1 --off
# If everything is inactive, turn on LVDS1 and switch everything else off
elif ! is_active "HDMI1" && ! is_active "VGA1"; then
echo "-- Activating LVDS1"
xrandr --output LVDS1 --auto --output VGA1 --off --output HDMI1 --off
fi
fi