-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChangeDisplayResolution
executable file
·131 lines (118 loc) · 4.21 KB
/
ChangeDisplayResolution
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
#!/bin/bash
# Usage: ChangeDisplayResolution [resolution]
source /usr/share/endeavouros/scripts/eos-script-lib-yad || {
echo "Error: ${BASH_SOURCE[1]}, line $LINENO: cannot find /usr/share/endeavouros/scripts/eos-script-lib-yad" >&2
exit 1
}
export -f eos_yad
DIE() {
echo "$progname: error: $1" >&2
exit 1
}
Restart_me() {
local tmpfile=$(mktemp)
cat <<EOF > $tmpfile
#!/bin/bash
pkill "$progname"
sleep 0.1
"$progname" &
EOF
bash $tmpfile
sleep 2
rm -f $tmpfile
}
export -f Restart_me
yad_ChangeDisplayResolution() {
local reso="$1"
local progname=${0##*/}
eos_assert_deps $progname xorg-xrandr yad || return 1
local query="$(xrandr --query)"
local output="$(echo "$query" | grep -m1 " connected " | awk '{print $1}')"
local xrandr="xrandr --output $output --mode"
local resos="$(echo "$query" | grep "^ [ ]*[0-9][0-9]*x[0-9][0-9]* " | awk '{print $1}')"
local resosarr
local current_reso="$(echo "$query" | grep "^ [ ]*[0-9][0-9]*x[0-9][0-9]* " | grep "*" | awk '{print $1}')"
local retval
local result
local txt mark="*"
local impl=list # list or form
local butt_set_quit="Set and quit"
local butt_set_stay="Set and stay"
# yad window return values must be different:
local b_ok=0 # button "Set and quit" clicked
local b_quit=1 # button "Quit" clicked (button currently not visible!)
local b_refresh=10 # button "Set and stay" clicked
local b_exit=252 # (X) clicked in the upper right corner
readarray -t resosarr <<< $(echo "$resos")
if [ -n "$reso" ] ; then
if [ -n "$(echo "$resos" | grep "$reso")" ] ; then
$xrandr "$reso"
else
echo "Error: $progname: given resolution '$reso' is not supported." >&2
echo "Supported values:" >&2
for reso in "${resosarr[@]}" ; do
if [ "$reso" = "$current_reso" ] ; then
echo " * $reso"
else
echo " $reso"
fi
done
fi
else
txt="Select new display resolution from the list below.\n"
txt+="- Current value is marked with: <b>$mark</b>\n"
txt+="- Double clicking a value does <b>$butt_set_quit</b>\n"
local cmd=(
eos_yad
--image=preferences-desktop-display
--width=400
--title="Change display resolution"
--text="$txt"
)
case "$impl" in
form) cmd+=(--form --columns=2 --button=yad-quit:0) ;;
list) cmd+=(--list --height=500 --no-click --grid-lines=both
--column="Available resolution values:"
# --button="yad-quit!!Change nothing, just quit":$b_quit
--button="$butt_set_stay!view-refresh!Use selected resolution but don't quit":$b_refresh
--button="$butt_set_quit!gtk-ok!Use selected resolution and quit":$b_ok
) ;;
esac
for reso in "${resosarr[@]}" ; do
if [ "$reso" = "$current_reso" ] ; then
case "$impl" in
form) cmd+=(--field="* $reso":fbtn "$xrandr $reso") ;;
list) cmd+=("$reso $mark") ;;
esac
else
case "$impl" in
form) cmd+=(--field=" $reso":fbtn "$xrandr $reso") ;;
list) cmd+=("$reso") ;;
esac
fi
done
result="$("${cmd[@]}")"
retval=$?
if [ -z "$result" ] ; then
return
fi
case "$retval" in
$b_quit | $b_exit) return ;;
esac
case "$impl" in
form) ;; # does not support refresh...
list)
reso="$(echo "$result" | cut -d '|' -f 1 | awk '{print $1}')"
if [ -n "$(echo "$reso" | tr -d '0-9x')" ] ; then
echo "Invalid resolution value '$reso'" >&2
return 1
fi
$xrandr $reso
case "$retval" in
$b_refresh) Restart_me ;;
esac
;;
esac
fi
}
yad_ChangeDisplayResolution "$@"