-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplaymgt.sh
executable file
·61 lines (50 loc) · 1.83 KB
/
displaymgt.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
#!/usr/bin/env bash
################################################################
#
# Simple Display Management
#
# Usage: ./displaymgt.sh # extended mode
# ./displaymgt.sh --mirror # mirror mode
# ./displaymgt.sh --noext # turn off external
#
# Licence: GNU GPL v3.0
#
# Author: Xiaokui Shu
#
################################################################
# all connected displays
DISPCONN=($(xrandr -q | awk '$2 == "connected" {print $1}'))
# all disconnected displays
DISPDISCONN=($(xrandr -q | awk '$2 == "disconnected" {print $1}'))
# laptop display
DISPLP=${DISPCONN[0]}
# potential external display
DISPEXT=${DISPCONN[1]}
if [ -n "$DISPEXT" ]; then
if [[ $1 = "--mirror" ]]; then
# laptop resolution
RESLP=$(xrandr -q | sed -n "/$DISPLP/{n;p;}" | awk '{print $1}')
# external display resolution
RESEXT=$(xrandr -q | sed -n "/$DISPEXT/{n;p;}" | awk '{print $1}')
# find the smaller resolution and assign it to $RESF
RESLPs=(${RESLP//x/ })
RESEXTs=(${RESEXT//x/ })
if [ "${RESLPs[0]}" -gt "${RESEXTs[0]}" ]; then
RESF=$RESEXT
else
RESF=$RESLP
fi
# mirror the laptop display using the smaller resolution
xrandr --output $DISPLP --mode $RESF --output $DISPEXT --mode $RESF --same-as $DISPLP
elif [[ $1 = "--noext" ]]; then
# turn the external screen off
xrandr --output $DISPLP --auto --primary --output $DISPEXT --off
else
# extended mode
xrandr --output $DISPLP --auto --primary --output $DISPEXT --auto --right-of $DISPLP
fi
else
# no external display: remove all display spaces by using "--auto"
DISPALL=("${DISPCONN[@]}" "${DISPDISCONN[@]}")
xrandr $(printf " --output %s --auto" ${DISPALL[@]})
fi