forked from MestreLion/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor-rotate
executable file
·172 lines (148 loc) · 4.99 KB
/
monitor-rotate
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
#!/bin/bash
#
# rotate-monitor - rotate primary monitor, adjusting other monitors to it
#
# Copyright (C) 2012 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. See <http://www.gnu.org/licenses/gpl.html>
#
# Uses xandr for its magic.The key is not the rotation itself, but the
# repositioning of all other monitors relative to the new height x width
# of the primary monitor
# FIXME: currently layout_list is setting positions relative to *previous*
# output (starting with primary). This should apply only to --position, while
# --layout list should be always relative to primary.
# TODO: There could be 2 distinct layout lists: one always relative to primary
# outout, the other relative to previous output. Or maybe a single list, which
# defaults "to relative to primary", and a "--previous" flag to set list as
# "relative to previous"
# TODO: make said "notification bubbles", *then* document -q :)
# -q|--no-notify - do not show notify "bubble"
myname="${0##*/}"
verbose=0
notify=1
rotate=normal
layout_list=
position="right-of"
primary=
message() { printf "%s\n" "$1" >&2 ; }
fatal() { [[ "$1" ]] && message "$myname: error: $1" ; exit ${2:-1} ; }
argerr() { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
invalid() { argerr "invalid argument: $1" ; }
missing() { argerr "missing ${2:+$2 }operand${1:+ from $1}." ; }
usage() {
cat <<-USAGE
Usage: $myname [options]
USAGE
if [[ "$1" ]] ; then
cat >&2 <<- USAGE
Try '$myname --help' for more information.
USAGE
exit 1
fi
cat <<-USAGE
Rotate the primary monitor using xrandr, re-positioning the others
monitors (if any) to $position the previous monitor, starting with the
primary one.
Options:
-h|--help - show this page.
-v|--verbose - print in terminal the full xrandr command executed
--primary OUTPUT
- select which output will be set as primary.
Default is the first connected monitor in xrandr list
Example: --primary DFP10
--rotate ORIENTATION
- how to rotate the primary monitor. ORIENTATION must
be one of normal, left, right
--left
--portrait
--counterclockwise
- same as --rotate left
--right
--clockwise
- same as --rotate right
--normal
--landscape
- same as --rotate normal
Copyright (C) 2012 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
USAGE
exit 0
}
# Option handling
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage ; done
while (( $# )); do
case "$1" in
-v|--verbose) verbose=1 ;;
-q|--no-notify) notify=0 ;;
--primary=*) primary="${1#*=}" ;;
--layout=* ) layout_list="${1#*=}" ;;
--rotate=* ) rotate="${1#*=}" ;;
--primary ) shift ; primary="$1" ;;
--layout ) shift ; layout_list="$1" ;;
--rotate ) shift ; rotate="$1" ;;
--normal|--landscape) rotate="normal" ;;
--left|--portrait|--counterclockwise) rotate="left" ;;
--right|--clockwise) rotate="right" ;;
*) invalid "$1" ;;
esac
shift
done
# Parse rotate
[[ "$rotate" ]] || missing "--rotate" "ORIENTATION"
case "${rotate,,}" in
normal|left|right)
rotate="${rotate,,}"
if [[ "$rotate" == normal ]]; then
label="Landscape (Normal)"
else
label="Portrait"
fi
;;
*) argerr "invalid orientation argument for --rotate: $rotate" ;;
esac
# Parse layout
IFS=, read -a layout <<<"$layout_list"
for pos in "${layout[@]}"; do
case "${pos,,}" in
right-of|left-of|above|below|same-as) continue ;;
*) argerr "invalid position argument for --layout: $pos" ;;
esac
done
# Parse default position
[[ "$position" ]] || missing "--position" "DEFAULT_POSITION"
case "${position,,}" in
right-of|left-of|above|below|same-as) ;;
*) argerr "invalid position argument for --position: $position" ;;
esac
# Read outputs (monitors)
{
# Set primary
if [[ -z "$primary" ]]; then
read -r primary
fi
xrandropts+=(--output "$primary" --primary --rotate "$rotate" --pos 0x0)
previous="$primary"
# Loop the others
i=0
while ((i++)); read -r output; do
if [[ "$output" != "$primary" ]]; then
[[ "${layout[i-1]}" ]] || layout[i-1]="$position"
xrandropts+=(--output "$output" "--${layout[i-1],,}" "$previous")
previous="$output"
fi
done
} < <(xrandr | awk '$2 ~/^c/{print $1}')
((verbose)) && message "$myname: executing xrandr ${xrandropts[*]}"
xrandr "${xrandropts[@]}"