-
Notifications
You must be signed in to change notification settings - Fork 24
/
auto-rotate
executable file
·96 lines (86 loc) · 3.77 KB
/
auto-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
#!/bin/sh
#
# auto-rotate the screen and pointers
# copyright 2018 Don Bowman ([email protected])
# Licensed under Apache License, Version 2.0
DISPLAY=:0
export DISPLAY
install_background() {
export BACKDROP=~/.config/auto-rotate/${1}.jpg
if [ -f "$BACKDROP" ]
then
cat << EOF | sh
dbus-send --session --dest=org.kde.plasmashell --type=method_call /PlasmaShell org.kde.PlasmaShell.evaluateScript 'string:
var Desktops = desktops();
for (i=0;i<Desktops.length;i++) {
d = Desktops[i];
d.wallpaperPlugin = "org.kde.image";
d.currentConfigGroup = Array("Wallpaper",
"org.kde.image",
"General");
d.writeConfig("Image", "file://$BACKDROP");
}'
EOF
fi
}
rotate_cursor() {
pointers=$(xinput list | mawk -W interactive '/Virtual core pointer/ { printing=1 } /Virtual core keyboard/ { printing=0} { if (printing && (match($0, /ELAN/) || match($0, /Pen/) || match($0, /Finger/) || match($0, /Touch/) )) { gsub(".*id=",""); print $1 } }')
echo "rotate -- pointers: <<$pointers>> matrix: <<$*>>"
for p in $pointers
do
xinput set-prop "$p" "Coordinate Transformation Matrix" "$@"
done
}
remove_keyboard(){
keyboard=$(xinput list | mawk -W interactive '/Virtual core pointer/ { printing=0 } /Virtual core keyboard/ { printing=1} { if (printing && (match($0, /AT Translated Set 2 keyboard/))) { gsub(".*id=",""); print $1 } }')
xinput ${1} $keyboard
echo Keyboard ${1}"d"
}
MAIN_SCREEN=$(xrandr --current | grep connected | mawk -W interactive '/primary/ {print $1}')
echo "$MAIN_SCREEN"
monitor-sensor | mawk -W interactive '/Accelerometer orientation changed:/ { print $NF; fflush();}' | while read -r line
do
echo "$line"
# If we have external monitor connected, do not rotate
nscreens=$(xrandr | grep -c " connected")
if [ "$nscreens" != 1 ]
then
line=normal
fi
# Set the actions to be taken for each possible orientation
case "$line" in
normal)
# re-enable backlight
dbus-send --print-reply=literal --type=method_call --system --dest=org.freedesktop.UPower /org/freedesktop/UPower/KbdBacklight org.freedesktop.UPower.KbdBacklight.SetBrightness int32:1
remove_keyboard enable
xrandr --output "$MAIN_SCREEN" --rotate normal
rotate_cursor 1 0 0 0 1 0 0 0 1
install_background normal
;;
bottom-up)
# disable backlight
dbus-send --print-reply=literal --type=method_call --system --dest=org.freedesktop.UPower /org/freedesktop/UPower/KbdBacklight org.freedesktop.UPower.KbdBacklight.SetBrightness int32:0
remove_keyboard disable
xrandr --output "$MAIN_SCREEN" --rotate inverted
rotate_cursor -1 0 1 0 -1 1 0 0 1
install_background bottom-up
;;
right-up)
# disable backlight
dbus-send --print-reply=literal --type=method_call --system --dest=org.freedesktop.UPower /org/freedesktop/UPower/KbdBacklight org.freedesktop.UPower.KbdBacklight.SetBrightness int32:0
remove_keyboard disable
xrandr --output "$MAIN_SCREEN" --rotate right
rotate_cursor 0 1 0 -1 0 1 0 0 1
install_background right-up
;;
left-up)
# disable backlight
dbus-send --print-reply=literal --type=method_call --system --dest=org.freedesktop.UPower /org/freedesktop/UPower/KbdBacklight org.freedesktop.UPower.KbdBacklight.SetBrightness int32:0
remove_keyboard disable
xrandr --output "$MAIN_SCREEN" --rotate left
rotate_cursor 0 -1 1 1 0 0 0 0 1
install_background left-up
;;
esac
done
exit 0