-
Notifications
You must be signed in to change notification settings - Fork 2
/
autorotate-gtk.sh
executable file
·143 lines (130 loc) · 5.34 KB
/
autorotate-gtk.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#! /bin/bash
#--button="Enter" --button="unlock" --button="exit" \
#https://sourceforge.net/p/yad-dialog/wiki/Examples/
# Source: https://linuxappfinder.com/blog/auto_screen_rotation_in_ubuntu
# Tweaks: https://gist.github.com/mildmojo/48e9025070a2ba40795c
#TOUCHSCREEN='ELAN Touchscreen' #as reported by evtest
TOUCHSCREEN="$(xinput list --name-only |grep -i -e 'touchscreen' -e 'touch[- ]screen')"
TRANSFORM='Coordinate Transformation Matrix'
function gv {
> sensor.log
monitor-sensor >> sensor.log 2>&1 &
exec >> autorotate.log 2>&1
#TOUCHSCREEN='ELAN Touchscreen' #as reported by evtest
TOUCHSCREEN="$(xinput list --name-only |grep -i -e 'touchscreen' -e 'touch[- ]screen')"
TRANSFORM='Coordinate Transformation Matrix'
printf '%s ' $(date) && echo 'Start of Function gv. Parameters Received:' $1 $2 $3 #Parameters inside function get a different number / name.
screen=$(xrandr | grep 'connected' |grep -v 'disconnected' | grep -oE '[a-zA-Z]+[\-]+[^ ]')
printf '%s ' $(date) && echo 'Auto Recognize screen {alt}:' $screen
while inotifywait -e modify sensor.log 2>&1 |ts;
#ts is a utility of pkg moreutils and automatically time stamps output of any command. Using 2>&1 stderr is also time stamped
do
ORIENTATION=$(tail -n 1 sensor.log | grep 'orientation' | grep -oE '[^ ]+$')
case "$ORIENTATION" in
normal)
printf '%s ' $(date) && echo 'xrandr rotate normal'
xrandr --output $screen --rotate normal
xinput set-prop "$TOUCHSCREEN" "$TRANSFORM" 1 0 0 0 1 0 0 0 1
#gsettings set com.canonical.Unity.Launcher launcher-position Left
;;
bottom-up)
printf '%s ' $(date) && echo 'xrandr rotate bottom-up'
xrandr --output $screen --rotate inverted
xinput set-prop "$TOUCHSCREEN" "$TRANSFORM" -1 0 1 0 -1 1 0 0 1
#gsettings set com.canonical.Unity.Launcher launcher-position Left
;;
right-up)
printf '%s ' $(date) && echo 'xrandr rotate right-up'
xrandr --output $screen --rotate right
xinput set-prop "$TOUCHSCREEN" "$TRANSFORM" 0 1 0 -1 0 1 0 0 1
#gsettings set com.canonical.Unity.Launcher launcher-position Bottom
;;
left-up)
printf '%s ' $(date) && echo 'xrandr rotate left-up'
xrandr --output $screen --rotate left
xinput set-prop "$TOUCHSCREEN" "$TRANSFORM" 0 -1 1 1 0 0 0 0 1
#gsettings set com.canonical.Unity.Launcher launcher-position Bottom
;;
esac
done
printf '%s ' $(date) && echo 'End of Function gv'
exit 1
}
export -f gv
function notifystop {
exec >> autorotate.log 2>&1
printf '%s ' $(date) && echo 'Notify Stop'
#exec 2> /dev/null
#pidof monitor-sensor | xargs kill -9
#pidof inotifywait | xargs kill -9
pkill -f "monitor-sensor" 2>&1
pkill -f "monitor-sensor >> sensor.log 2>&1" 2>&1
pkill -f "inotifywait -e modify sensor.log" 2>&1
exit
}
export -f notifystop
function byebye {
exec >> autorotate.log 2>&1
#exec 2> /dev/null
printf '%s ' $(date) && echo 'Bye Bye to all!'
#pidof monitor-sensor | xargs kill -9
#pidof inotifywait | xargs kill -9
#pidof yad | xargs kill -9
pkill -f "monitor-sensor" 2>&1
pkill -f "monitor-sensor >> sensor.log 2>&1" 2>&1
pkill -f "inotifywait -e modify sensor.log" 2>&1
pkill -f "autorot.sh" 2>&1
pkill -f "yad --notification --command=bash -c autorot >>autorotate.log 2>&1" 2>&1
#rm -f sensor.log autorotate.log
exit
}
export -f byebye
function autorot {
exec >> autorotate.log 2>&1
# ------------------------------------------------------- The main function ----------------------------- #
printf '%s ' $(date) && echo 'Main Programm Starting'
action=$(yad --width 300 --entry --title "AutoRotate" \
--image=gnome-shutdown \
--button="gtk-ok:0" --button="gtk-cancel:1" \
--text "Choose action:" \
--entry-text "UnLock" "Lock" "Exit" )
pof=$(pidof monitor-sensor) && printf '%s ' $(date) && echo 'monitor-sensor pid =' $pof
if [[ $pof -eq "" ]]; then
running="notrunning"
else
running="running"
fi
case $action in
UnLock*)
printf '%s ' $(date) && echo 'Unlock Selected'
# gv $locked $running
#Check if monitor-sensor and inotify is already running to avoid creating a new instance.
if [[ $pof -eq "" ]]; then
printf '%s ' $(date) && echo "notrunning - starting now"
gv
else
printf '%s ' $(date) && echo "already running , skipping command"
fi
;;
Lock*)
printf '%s ' $(date) && echo 'Locked Selected'
notifystop;;
Exit*)
printf '%s ' $(date) && echo 'Exit Selected'
byebye;;
*) exit 0;;
esac
printf '%s ' $(date) && echo 'Main Programm End'
exit 0
}
export -f autorot
yad --notification --command="bash -c autorot >>autorotate.log 2>&1" >>autorotate.log 2>&1
#yad --notification --image=gtk-help --command="bash -c autorot >>autorotate.log 2>&1" >>autorotate.log 2>&1
exit
# I tried to use a kind of flags passing to the functions in order to avoid pkill for lock feature.
# But is seems that once inotifywait runs once, will be always running as process.
# As a result if you try to "skip" the inotify section with some "already running" logic checks, they do not work.
# It seems that the most easy way when you want to lock the autorotation is just to kill the inotify and monitor-sensor.
# To verify if the process is killed : #ps aux |grep inotify or grep monitor-sensor
# To simulate the monitor-write, open a second terminal and # echo " Accelerometer orientation changed: bottom-up" >>sensor.log
# It has been observed with ps aux that the monitor sensor process some times appear just as "monitor-sensor" and some times with full comannd line.