summaryrefslogtreecommitdiff
path: root/autorotate.sh
blob: bf69b7361de685681908c8e5e66b6f552b7a0e08 (plain)
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
#!/bin/sh

### TODO: These should be automatically identified
touchpad='PS/2 Synaptics TouchPad'
touchdev='SYNAPTICS Synaptics Touch Digitizer V04'

# Global variables
CURRENT='normal'
GRAV=$(echo 7.0 / $(head -n1 /sys/bus/iio/devices/iio:device*/in_accel_scale) | bc)

rotatetouch() {
	# For some reason, xrandr causes the touch device to disappear briefly.
	# This loop waits for it to come back.
	while ! xinput --list | grep -q "$touchdev"; do :; done
	xinput set-float-prop "$touchdev" 'Coordinate Transformation Matrix' $*
}

rotatewacom() {
	### FIXME: the byte range (38-40) will probably break at some point
	for dev in $(xsetwacom --list devices | cut -b 38-40); do
		xsetwacom set "$dev" rotate $1
	done
}

check_orientation() {
	XRAW=$(head -n1 /sys/bus/iio/devices/iio:device*/in_accel_x_raw)
	YRAW=$(head -n1 /sys/bus/iio/devices/iio:device*/in_accel_y_raw)

	if [ $CURRENT != 'normal' ] && [ $YRAW -le -$GRAV ]; then
		CURRENT='normal'
		xrandr -o normal
		rotatetouch 1 0 0 0 1 0 0 0 1
		rotatewacom none
		### FIXME: There should be a separate script to enable/disable touchpad on folding events
		xinput enable "$touchpad"
	fi

	if [ $CURRENT != 'inverse' ] && [ $YRAW -ge $GRAV ]; then
		CURRENT='inverse'
		xrandr -o inverted
		rotatetouch -1 0 1 0 -1 1 0 0 1
		rotatewacom half
		xinput disable "$touchpad"
	fi

	if [ $CURRENT != 'left' ] && [ $XRAW -ge $GRAV ]; then
		CURRENT='left'
		xrandr -o left
		rotatetouch 0 -1 1 1 0 0 0 0 1
		rotatewacom ccw
		xinput disable "$touchpad"
	fi

	if [ $CURRENT != 'right' ] && [ $XRAW -le -$GRAV ]; then
		CURRENT='right'
		xrandr -o right
		rotatetouch 0 1 0 -1 0 1 0 0 1
		rotatewacom cw
		xinput disable "$touchpad"
	fi
}

while true; do
	check_orientation
	sleep 1
done