/
setcustomres.sh
executable file
·190 lines (166 loc) · 6.27 KB
/
setcustomres.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
red=$'\e[31m'
yellow=$'\e[93m'
blue=$'\e[34m'
dull_blue=$'\e[94m'
magenta=$'\e[35m'
bold=$'\e[1m'
underline=$'\e[4m'
reset=$'\e[0m'
number='^[0-9]+$'
version="2.3"
help()
{
echo "${bold}${yellow}Description:${reset} Set custom resolution to a display using ${bold}xrandr${reset}"
echo "${bold}${yellow}Usage:${reset} $0 [OPTIONS]..."
echo "${bold}${yellow}Version:${reset} $version"
echo ""
echo "${magenta}-w${reset} | ${magenta}-width${reset} <width> ${bold}Mandatory:${reset} Width of resolution"
echo "${magenta}-h${reset} | ${magenta}-height${reset} <height> ${bold}Mandatory:${reset} Hight of resolution"
echo "${magenta}-o${reset} | ${magenta}-output${reset} <output> ${bold}Mandatory:${reset} Display output"
echo "${magenta}-r${reset} | ${magenta}-refresh-rate${reset} <refresh rate> Custom refresh rate (Default 60Hz)"
echo "${magenta}-p${reset} | ${magenta}-param${reset} ${dull_blue}\"<parameter> [parameter]...\"${reset} ${underline}xrandr${reset} parameters - wrap with double quotes"
echo "${magenta}-u${reset} | ${magenta}-update${reset} <path> ${bold}Standalone:${reset} Update setcustomres repo"
echo "${magenta}-v${reset} | ${magenta}-version${reset} ${bold}Standalone:${reset} Print version"
echo "${magenta}-help${reset} ${bold}Standalone:${reset} Print this help message"
}
printMessage()
{
echo -e "\n ${bold}${blue}[+]${reset} ${bold}$1${reset} \n"
}
printError()
{
echo -e "${red}ERROR:${reset} ${bold}$1${reset}"
exit 1
}
checkMonitorStatus()
{
output="$1"
mode="$2"
monitor_set="false"
reached_output="false"
while read -r line
do
if [[ $(echo "$line" | awk '{print $1}') = "$output" ]]; then
reached_output="true"
elif [[ $reached_output = "true" ]] && [[ $(echo "$line" | cut -d 'x' -f1) =~ $number ]]; then
[[ $(echo $line | awk '{print $1}') = "$mode" ]] && monitor_set="true"
elif ! [[ $(echo "$line" | cut -d ' ' -f1) = "" ]]; then
reached_output="false"
fi
done < <(xrandr)
echo $monitor_set
}
set_resolution()
{
width=$1
height=$2
output=$3
param=$4
refresh=$5
res="${width}x${height}$([[ "${refresh}" ]] && echo "_${refresh}")"
printMessage "Setting custom resolution of ${width}x${height} to output $output $([[ "$refresh" ]] && echo "at a refresh rate of ${refresh}Hz") $([[ "$param" ]] && echo "\n With flags: ${reset}${magenta}$param")"
monitor_connected=$(xrandr --listactivemonitors | grep " $output")
if [[ $monitor_connected = "" ]]; then
printError "Monitor $output is not active!"
fi
[[ "$refresh" ]] && cvt="$(echo $res $(cvt "$width" "$height" "$refresh" | tail -1 | cut -d ' ' -f3-))" || cvt="$(echo $res $(cvt "$width" "$height" | tail -1 | cut -d ' ' -f3-))"
mode=$(echo $cvt | cut -d ' ' -f1)
status=$(checkMonitorStatus $output $res)
if [[ $status = "false" ]]; then
xrandr --newmode $(echo $cvt) 2> /dev/null
xrandr --addmode "$output" "$mode"
fi
xrandr --output "$output" --mode "$mode" $(echo $param)
}
flags() {
if [[ "$#" -eq 0 ]]; then
printError "Missing arguments, parse \"-help\" for more information"
fi
while [[ "$1" != "" ]]
do
case $1 in
-v|-version)
echo "setcustomres v$version"
exit
;;
-help)
help
exit
;;
-w|-width)
if [[ ! $2 =~ $number ]]; then
printError "Invalid value parsed! Only numbers are applicable."
elif [ "$2" ]; then
shift
width="$1"
else
printError "\"-w|-width\" requires a non-empty argument"
fi
;;
-h|-height)
if [[ ! $2 =~ $number ]]; then
printError "Invalid value parsed! Only numbers are applicable."
elif [ "$2" ]; then
shift
height="$1"
else
printError "\"-h|-height\" requires a non-empty argument"
fi
;;
-r|-refresh-rate)
if [[ ! $2 =~ $number ]]; then
printError "Invalid value parsed! Only numbers are applicable."
elif [ "$2" ]; then
shift
refresh="$1"
else
printError "\"-r|-refresh-rate\" requires a non-empty argument"
fi
;;
-o|-output)
if [ "$2" ]; then
shift
output="$1"
else
printError "\"-o|-output\" requires a non empty arguments"
fi
;;
-p|-param)
if [ "$2" ]; then
shift
param="$1"
else
printError "\"-p|-param\" requires a non empty arguments"
fi
;;
-u|-update)
if [[ "$2" ]]; then
shift
if [[ -d "$1" && $(basename $1) = *"setcustomres"* ]]; then
cd $1
printMessage "Updating 'setcustomres'"
sudo ./setup.sh install
cd - &> /dev/null
shift
else
printError "\"-u|-update\" requires a path to the local setcustomres repository"
fi
fi
exit
;;
-?*)
printError "Unknown option: $1"
;;
*)
break
;;
esac
shift
done
if [[ ! "$width" ]] || [[ ! "$height" ]] || [[ ! "$output" ]]; then
printError "Missing arguments, parse \"-help\" for more information"
fi
}
flags "$@" # Deal with flags
set_resolution "$width" "$height" "$output" "$param" "$refresh" # Call set_resolution