-
Notifications
You must be signed in to change notification settings - Fork 37
/
mons.sh
executable file
·489 lines (436 loc) · 15.8 KB
/
mons.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/bin/sh
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2019 Thomas "Ventto" Venriès <thomas.venries@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
usage() {
echo 'Usage: mons [OPTION]...
Without argument, it prints connected monitors list with their names and ids.
Options are exclusive and can be used in conjunction with extra options.
Information:
-h Prints this help and exits.
-v Prints version and exits.
Two monitors:
-o Primary monitor only.
-s Second monitor only.
-d Duplicates the primary monitor.
-m Mirrors the primary monitor.
-e <side>
Extends the primary monitor to the selected side
[ top | left | right | bottom ].
-n <side>
This mode selects the previous ones, one after another. The argument
sets the side for the extend mode.
More monitors:
-O <mon>
Only enables the monitor with a specified id.
-S <mon1>,<mon2>:<pos>
Only enables two monitors with specified ids. The specified position
places the second monitor on the right (R) or at the top (T).
Daemon mode:
-a Performs an automatic display if it detects only one monitor.
-x <script>
Must be used in conjunction with the -a option. Every time the number
of connected monitors changes, mons calls the given script with the
MONS_NUMBER environment variable.
Extra (in-conjunction or alone):
--dpi <dpi>
Set the DPI, a strictly positive value within the range [0 ; 27432].
--primary <mon_name>
Select a connected monitor as the primary output. Run the script
without argument to print monitors information, the names are in the
second column between ids and status. The primary monitor is marked
by an asterisk.
'
}
version() {
echo 'Mons 0.8.2
Copyright (C) 2017 Thomas "Ventto" Venries.
License MIT: <https://opensource.org/licenses/MIT>.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
'
}
# Helps to generate manpage with help2man before installing the library
[ "$1" = '-h' ] && { usage; exit; }
[ "$1" = '-v' ] && { version; exit; }
lib='%LIBDIR%/liblist.sh'
[ ! -r "$lib" ] && { "$lib: library not found."; exit 1; }
. "$lib"
arg_err() {
usage ; exit 2
}
enable_mon() {
"$XRANDR" --output "$1" --auto
}
disable_mons() {
for mon in $@; do "$XRANDR" --output "$mon" --off ; done
}
arg2xrandr() {
case $1 in
left) echo '--left-of' ;;
right) echo '--right-of' ;;
bottom) echo '--below' ;;
top) echo '--above' ;;
esac
}
whichmode() {
if [ "$(list_size "${disp_mons}")" -eq 1 ]; then
if echo "${enabled_out}" | grep prima > /dev/null 2>&1; then
echo 'primary'
else
echo 'second'
fi
else
if [ "$(list_size "${plug_mons}")" -gt 2 ] ; then
echo 'selection'; return 0
fi
enabled_out="$(echo "${enabled_out}" | \
sed 's/^.*\( [0-9]\+\x[0-9]\++[0-9]\++[0-9]\+\).*/\1/')"
echo "${enabled_out}" | head -n1 | sed -e 's/+/ /g' | \
while read -r trash x1 y1; do
echo "${enabled_out}" | tail -n1 | sed -e 's/x/ /' -e 's/+/ /g' | \
while read -r w2 h2 x2 y2; do
echo "${xrandr_out}" | \
awk "/^$(list_get 1 "${plug_mons}")/{nr[NR+1]}; NR in nr" | \
awk '{print $1;}' | sed -e 's/x/ /' | \
while read -r wi2 hi2; do
if [ "$x1" = "$x2" ] && [ "$y1" = "$y2" ]; then
if [ "$w2" != "$wi2" ] || [ "$h2" != "$hi2" ]; then
echo 'mirror'
else
echo 'duplicate'
fi
else
echo 'extend'
fi
done
done
done
fi
}
main() {
# =============================
# Requirements
# =============================
[ -z "$DISPLAY" ] && { echo 'DISPLAY: no variable set.'; exit 1; }
command -vp xrandr >/dev/null 2>&1 || { echo 'xrandr: command not found.'; exit 1; }
XRANDR="$(command -pv xrandr)"
# =============================
# Argument Checking
# =============================
aFlag=false
dFlag=false
eFlag=false
mFlag=false
nFlag=false
oFlag=false
sFlag=false
OFlag=false
SFlag=false
pFlag=false
iFlag=false
xFlag=false
is_flag=false
# X has assumed 96 DPI and this is fine for many traditional monitors.
primary=
# getopts does not support long options. We convert them to short one.
for arg in "$@"; do
shift
case "$arg" in
--dpi) set -- "$@" '-i' ;;
--primary) set -- "$@" '-p' ;;
*) set -- "$@" "$arg"
esac
done
while getopts 'hvamosde:n:O:S:i:p:x:' opt; do
case $opt in
# Long options
i)
if ! echo "$OPTARG" | \
grep -E '^[1-9][0-9]*$' > /dev/null 2>&1; then
arg_err
fi
iFlag=true; iArg="$OPTARG"
;;
p) if ! echo "$OPTARG" | \
grep -E '^[a-zA-Z][a-zA-Z0-9\-]+' > /dev/null 2>&1; then
arg_err
fi
pFlag=true; primary="$OPTARG"
;;
# Short options
a) $is_flag && arg_err
aFlag=true ; is_flag=true
;;
m) $is_flag && arg_err
mFlag=true ; is_flag=true
;;
o) $is_flag && arg_err
oFlag=true ; is_flag=true
;;
s) $is_flag && arg_err
sFlag=true ; is_flag=true
;;
d) $is_flag && arg_err
dFlag=true ; is_flag=true
;;
e|n) $is_flag && arg_err
case ${OPTARG} in
left | right | bottom | top) ;;
*) arg_err ;;
esac
eArg=$OPTARG
[ "$opt" = "e" ] && eFlag=true || nFlag=true ; is_flag=true
;;
O) $is_flag && arg_err
! echo "$OPTARG" | grep -E '^[0-9]+$' > /dev/null && arg_err
OArg=$OPTARG
OFlag=true ; is_flag=true
;;
S) $is_flag && arg_err
idx1="$(echo "$OPTARG" | cut -d',' -f1)"
idx2="$(echo "$OPTARG" | cut -d',' -f2)"
area="$(echo "$idx2" | cut -d ':' -f2)"
idx2="$(echo "$idx2" | cut -d ':' -f1)"
! echo "$idx1" | grep -E '^[0-9]+$' > /dev/null && arg_err
! echo "$idx2" | grep -E '^[0-9]+$' > /dev/null && arg_err
! echo "$area" | grep -E '^[RT]$' > /dev/null && arg_err
[ "$idx1" = "$idx2" ] && arg_err
SFlag=true ; is_flag=true
;;
x) xFlag=true;
if [ ! -x "$OPTARG" ]; then
echo "${OPTARG}: file cannot be executed or does not exist"
exit 2
fi
xArg="$OPTARG"
;;
h) usage ; exit ;;
v) version ; exit ;;
\?) arg_err ;;
:) arg_err ;;
esac
done
if $xFlag; then
if ! $aFlag; then
echo '-x: option can only be used in conjunction with -a'
exit 2
fi
fi
# =============================
# Daemon Mode
# =============================
if $aFlag ; then
prev=0; i=0
while true; do
for status in /sys/class/drm/*/status; do
[ "$(<"$status")" = 'connected' ] && i=$((i+1))
done
if [ "$i" != "$prev" ]; then
if $xFlag; then
MONS_NUMBER="$i" sh "$xArg"
else
[ "$i" -eq 1 ] && "$XRANDR" --auto
fi
fi
prev="$i"; i=0
sleep 2
done
fi
# List all outputs (except primary one)
xrandr_out="$("$XRANDR")"
enabled_out="$(echo "$xrandr_out" | grep 'connect')"
[ -z "$enabled_out" ] && { echo 'No monitor output detected.'; exit; }
mons="$(echo "$enabled_out" | cut -d' ' -f1)"
# List plugged-in and turned-on outputs
enabled_out="$(echo "$enabled_out" | grep ' connect')"
[ -z "$enabled_out" ] && { echo 'No plugged-in monitor detected.'; exit 1; }
plug_mons="$(echo "$enabled_out" | cut -d' ' -f1)"
if [ "$(list_size "$plug_mons")" -eq 0 ]; then
echo "No monitor plugged-in."
exit 0
fi
# =============================
# Conjunction Options
# =============================
# Set DPI
$iFlag && "$XRANDR" --dpi "$iArg"
# Set primary output
if $pFlag; then
if ! list_contains "$primary" "$plug_mons"; then
echo "${primary}: output not connected."
exit 1
fi
"$XRANDR" --output "$primary" --primary
[ "$#" -eq 2 ] && exit
else
primary="$(echo "$enabled_out" | grep 'primary' | cut -d' ' -f1)"
fi
# Move the primary monitor to the head if connected otherwise the first
# connected monitor that appears in the xrandr output is considerate as
# the primary one.
if [ -n "$primary" ]; then
plug_mons="$(list_erase "$primary" "$plug_mons")"
plug_mons="$(list_insert "$primary" 0 "$plug_mons")"
fi
enabled_out="$(echo "$enabled_out" | grep -E '\+[0-9]{1,4}\+[0-9]{1,4}')"
disp_mons="$(echo "$enabled_out" | cut -d' ' -f1)"
if [ "$#" -eq 0 ]; then
echo "Monitors: $(list_size "$plug_mons")"
echo "Mode: $(whichmode)"
i=0
for mon in $mons; do
if echo "$plug_mons" | grep "^${mon}$" > /dev/null; then
if echo "$disp_mons" | grep "^${mon}$" > /dev/null; then
state='(enabled)'
fi
if [ "$mon" = "$primary" ]; then
printf '%-4s %-8s %-8s %-8s\n' "${i}:*" "$mon" "$state"
else
printf '%-4s %-8s %-8s\n' "${i}:" "$mon" "$state"
fi
fi
i=$((i+1))
state=
done
exit
fi
if $nFlag ; then
case "$(whichmode)" in
primary) sFlag=true;;
second) eFlag=true;;
extend) mFlag=true;;
mirror) dFlag=true;;
duplicate) oFlag=true;;
esac
fi
if [ "$(list_size "$plug_mons")" -eq 1 ] ; then
if $oFlag ; then
# After unplugging each monitor, the last preferred one might be
# still turned off or the window manager might need the monitor
# reset to cause the reconfiguration of the layout placement.
"$XRANDR" --auto
else
echo 'Only one monitor detected.'
fi
exit
fi
if $oFlag ; then
if [ "$(list_size "${disp_mons}")" -eq 1 ]; then
if [ "$(list_front "${disp_mons}")" = "$(list_front "${plug_mons}")" ]; then
exit
fi
fi
disp_mons="$(list_erase "$(list_front "${plug_mons}")" "$disp_mons")"
disable_mons "${disp_mons}"
enable_mon "$(list_front "${plug_mons}")"
exit
fi
if $OFlag ; then
if [ "$OArg" -ge "$(list_size "$mons")" ] ; then
echo "Monitor ID '${OArg}' does not exist."
echo 'Try without option to get monitor ID list.'
exit 2
fi
mons_elt="$(list_get "$OArg" "$mons")"
if ! list_contains "${mons_elt}" "${plug_mons}"; then
echo "Monitor ID '${OArg}' not plugged in."
echo 'Try without option to get monitor ID list.'
exit 2
fi
disp_mons="$(list_erase "${mons_elt}" "${disp_mons}")"
disable_mons "${disp_mons}"
enable_mon "${mons_elt}"
exit
fi
if $SFlag ; then
if [ "$idx1" -ge "$(list_size "$mons")" ] || \
[ "$idx2" -ge "$(list_size "$mons")" ]; then
echo 'One or both monitor IDs do not exist.'
echo 'Try without option to get monitor ID list.'
exit 2
fi
if ! list_contains "$(list_get "$idx1" "$mons")" "$plug_mons" || \
! list_contains "$(list_get "$idx2" "$mons")" "$plug_mons" ; then
echo 'One or both monitor IDs are not plugged in.'
echo 'Try without option to get monitor ID list.'
exit 2
fi
[ "$area" = 'R' ] && area="--right-of" || area="--above"
mon1="$(list_get "$idx1" "$mons")"
mon2="$(list_get "$idx2" "$mons")"
disp_mons="$(list_erase "$mon1" "$disp_mons")"
disp_mons="$(list_erase "$mon2" "$disp_mons")"
disable_mons "$disp_mons"
enable_mon "$mon1"
enable_mon "$mon2"
"$XRANDR" --output "$mon2" "$area" "$mon1"
exit
fi
# =============================
# 2-Monitors Options
# =============================
if [ "$(list_size "$plug_mons")" -eq 2 ]; then
if $sFlag ; then
if [ "$(list_size "$disp_mons")" -eq 1 ] ; then
if [ "$(list_front "$disp_mons")" = "$(list_get 1 "$plug_mons")" ] ; then
enable_mon "$(list_get 1 "$plug_mons")"
exit
fi
fi
enable_mon "$(list_get 1 "$plug_mons")"
disable_mons "$(list_front "$disp_mons")"
exit
fi
# Resets the screen configuration
disable_mons "$(list_get 1 "$plug_mons")"
"$XRANDR" --auto
if $dFlag ; then
"$XRANDR" --output "$(list_get 1 "$plug_mons")" \
--same-as "$(list_front "$plug_mons")"
exit $?
fi
if $mFlag ; then
xrandr_out="$(echo "$xrandr_out" | \
awk "/primary/{nr[NR]; nr[NR+1]}; NR in nr")"
if echo "$xrandr_out" | \
grep -E 'primary [0-9]+x[0-9]+' >/dev/null 2>&1; then
size="$(echo "$xrandr_out" | head -n1 | cut -d' ' -f4 | \
cut -d'+' -f1)"
else
size="$(echo "$xrandr_out" | tail -n1 | awk '{ print $1 }')"
fi
"$XRANDR" --output "$(list_get 1 "$plug_mons")" \
--auto --scale-from "$size" \
--output "$(list_front "$plug_mons")"
exit $?
fi
if $eFlag ; then
"$XRANDR" --output "$(list_get 1 "$plug_mons")" \
"$(arg2xrandr "$eArg")" "$(list_front "$plug_mons")"
exit $?
fi
else
echo 'At most two plugged monitors for this option.'
fi
}
main "$@"