/
spinnr.sh
executable file
·123 lines (112 loc) · 2.55 KB
/
spinnr.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
#!/bin/bash
function usage {
cat - <<EOF
This is spinnr.sh, the client daemon of the linux screen autorotation daemons.
Usage: $0 spinfile [OUTPUT] [-o OUTPUT | -t TOUCHSCREEN]...
-h, --help Display this help and exit.
-t TOUCHSCREEN,
--touchscreen=TOUCHSCREEN Add the name of a touchscreen to rotate.
-o OUTPUT,
--output=OUTPUT Add the name of an output to rotate.
-q, --quiet Don't output text.
EOF
}
selfdir=$(dirname "$(readlink $0 -m)")
if [[ "$selfdir" == "." ]]; then
selfdir=$(pwd)
fi
quiet=0
outputs=( )
touchscreennames=( )
shortopts="hqt:o:"
longopts="help,quiet,touchscreen:,output:"
getopterrname="$0->getopt.sh"
ARGS=$(getopt -o $shortopts -l "$longopts" -n "$getopterrname" -- "$@")
if (($?)); then exit 1; fi
eval set -- "$ARGS"
while (($#)); do
case "$1" in
-h|--help)
usage
exit 0
;;
-o|--output)
outputs+=( "$2" )
shift 2
;;
-q|--quiet)
quiet=1
shift
;;
-t|--touchscreen)
touchscreennames+=( "$2" )
shift 2
;;
--)
shift
break
;;
*)
echo "$1 is not a recognized option! ($0)" >&2
usage
exit 1
;;
esac
done
spinfile="$1"
if ! [[ "$spinfile" ]]; then
echo This script needs a spinfile to function! >&2
exit 2
elif ! [[ -f "$spinfile" ]]; then
echo "'$spinfile' doesn't exist!" >&2
exit 2
fi
if [[ "$2" ]]; then
outputs+=( "$2" )
fi
if ! (( ${#outputs} )); then
outputs=( $(xrandr -q | grep -Pom1 '\w+(?= connected)') )
echo "Using autodetected output $outputs"
fi
declare -A rotmap
rotmap[normal]=" \
1 0 0 \
0 1 0 \
0 0 1 "
rotmap[left]=" \
0 -1 1 \
1 0 0 \
0 0 1 "
rotmap[inverted]=" \
-1 0 1 \
0 -1 1 \
0 0 1 "
rotmap[right]=" \
0 1 0 \
-1 0 1 \
0 0 1 "
# Convert touchscreen names to touchscreen ids
if (( ${#touchscreennames[@]} )); then
# inputs="$(xinput --list)"
tsc=( )
for tsname in "${touchscreennames[@]}"; do
tsc+=( -e )
tsc+=( "$tsname" )
# touchscreens+=( $(echo "$inputs" | grep -i "$tsname" | grep -Po "(id=)\d+") )
done
touchscreens=( $(xinput --list | grep -i "${tsc[@]}" | grep -Po '(?<=id=)\d+') )
fi
#TODO: proper option parsing
#FEEP: interactive output choice
#TODO: additional xrandr args(?) (e.g. quiet)
while inotifywait -qqe close_write "$spinfile"; do
rotation=$(cat "$spinfile")
for output in "${outputs[@]}"; do
xrandr --output "$output" --rotate $rotation;
done
if (( ${#touchscreens} )); then
for touchscreen in "${touchscreens[@]}"; do
xinput set-prop "$touchscreen" 'Coordinate Transformation Matrix' ${rotmap[$rotation]}
done
fi
done