diff --git a/pmos/monitor.sh b/pmos/monitor.sh new file mode 100755 index 0000000..edcdb9f --- /dev/null +++ b/pmos/monitor.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +set -e + +# command to make rotation +# normal, left, inverted or right will be passed as the first arg +COMMAND="$(dirname "$0")/rotate.sh" + +# paths to sensor files +DEVICE=/sys/bus/iio/devices/iio:device0 +DEVICE_X="$DEVICE/in_accel_x_raw" +DEVICE_Y="$DEVICE/in_accel_y_raw" + +# min sensor value to call rotation +THRESHOLD=10000 +# time interval in seconds +INTERVAL=2 + + + +PREV_ROT= +while true; do + X="$(cat "$DEVICE/in_accel_x_raw")" + Y="$(cat "$DEVICE/in_accel_y_raw")" + + ROT="$PREV_ROT" + if [ "$X" -gt "$THRESHOLD" ]; then + ROT="normal" + elif [ "$X" -lt "-$THRESHOLD" ]; then + ROT="inverted" + elif [ "$Y" -gt "$THRESHOLD" ]; then + ROT="right" + elif [ "$Y" -lt "-$THRESHOLD" ]; then + ROT="left" + fi + + if [ "$ROT" != "$PREV_ROT" ]; then + "$COMMAND" "$ROT" + PREV_ROT="$ROT" + # double wait immediatelly after rotation + sleep "$INTERVAL" + fi + + sleep "$INTERVAL" +done + diff --git a/pmos/rotate.sh b/pmos/rotate.sh index c38b716..0d5289d 100755 --- a/pmos/rotate.sh +++ b/pmos/rotate.sh @@ -2,14 +2,25 @@ set -e +# directory to store pids of background processes +PIDS=/dev/shm/cool-rotate + # command to run a screen keyboard KEYBOARD="coolkbd" # arguments for the screen keyboard KEYBOARD_ARGS="/dev/input/event1" -# name of an input device to configure (from xinput) -INPUTDEV="Elan Touchscreen" +# command to run sensors monitor (which do the screen auto-rotation) +MONITOR="$(dirname "$0")/monitor.sh" +# arguments for the sensors monitor +MONITOR_ARGS= + +# screen notify command, just comment out if it is not need +NOTIFY="notify-send" + +# name of an input device to configure (from xinput) +INPUTDEV="Elan Touchscreen" # name of a transformation matrix property of the device MATRIX_PROP="Coordinate Transformation Matrix" # name of a dead edges sizes property of the device @@ -30,6 +41,10 @@ function usage() { echo " kbon - open screen keyboard" echo " kboff - close screen keyboard" echo " kbtoggle - toggle screen keyboard" + echo " aron - enable auto-rotation" + echo " aroff - disable auto-rotation" + echo " artoggle - toggle auto-rotation" + echo " quiet - disable screen notify" echo " normal,left,inverted,right" echo " - comma separated looped sequence of orientations" echo " screen will be turned to the next state relative to current" @@ -62,8 +77,6 @@ function fix_rotation_name() { *) echo "normal";; esac } - - # check if the first arg is a comma separated sequence of valid rotation names function is_rotation_sequence() { local s="$(echo "$1" | tr -s , ' ')" @@ -78,8 +91,6 @@ function is_rotation_sequence() { function current_rotation() { fix_rotation_name "$(xrandr --screen 0 --current --query | head -2 | tail -1 | cut -d' ' -f5)" } - - # turn screen to chosen rotation # possible values: normal, left, inverted and right function rotate_screen() { @@ -87,22 +98,35 @@ function rotate_screen() { } -# keyboard functions -function is_keyboard_run() { - [ -n "$KEYBOARD" ] - pidof "$KEYBOARD" > /dev/null +# check if our serivice is running +function is_run() { + [ -f "$PIDS/$1.pid" ] } -function run_keyboard() { - if ! is_keyboard_run; then - nohup $KEYBOARD $KEYBOARD_ARGS &> /dev/null & - fi +# run our service if it is not running +# args: service-name command command-args... +function run() { + local svc="$1" + shift + ( ! is_run "$svc" ) || return 0 + [ -n "$1" ] || return 0 + nohup "$@" &> /dev/null & + local p="$!" + mkdir -p "$PIDS" + echo "$p" > "$PIDS/$svc.pid" + [ "$svc" != "monitor" ] || [ -z "$NOTIFY" ] || "$NOTIFY" "rotation ON" } -function stop_keyboard() { - [ -z "$KEYBOARD" ] || killall -q "$KEYBOARD" || true +# stop our service +function stop() { + is_run "$1" || return 0 + kill "$(cat "$PIDS/$1.pid")" || true + rm "$PIDS/$1.pid" + [ "$1" != "monitor" ] || [ -z "$NOTIFY" ] || "$NOTIFY" "rotation OFF" } -function toggle_keyboard() { - [ -n "$KEYBOARD" ] || return - if is_keyboard_run; then stop_keyboard; else run_keyboard; fi +# stop our service if it is running or run it in other case +# args: service-name command command-args... +function toggle() { + [ -n "$1" ] || return 0 + if is_run "$1"; then stop "$1"; else run "$@"; fi } @@ -112,7 +136,7 @@ function configure_input() { local matrix= local edges= local area=0 - if is_keyboard_run; then area="$KEYAREA"; fi + if is_run keyboard; then area="$KEYAREA"; fi echo "configure the input device for orientation: $rot" case "$rot" in @@ -149,13 +173,24 @@ while [ "$#" != "0" ]; do IS_CMD=1 if [ "$1" = "kbon" ]; then echo "open screen keyboard" - run_keyboard + run keyboard "$KEYBOARD" $KEYBOARD_ARGS elif [ "$1" = "kboff" ]; then echo "close screen keyboard" - stop_keyboard + stop keyboard elif [ "$1" = "kbtoggle" ]; then echo "toggle screen keyboard" - toggle_keyboard + toggle keyboard "$KEYBOARD" $KEYBOARD_ARGS + elif [ "$1" = "aron" ]; then + echo "run auto-rotation sensor monitor" + run monitor "$MONITOR" $MONITOR_ARGS + elif [ "$1" = "aroff" ]; then + echo "stop auto-rotation sensor monitor" + stop monitor + elif [ "$1" = "artoggle" ]; then + echo "toggle auto-rotation sensor monitor" + toggle monitor "$MONITOR" $MONITOR_ARGS + elif [ "$1" = "quiet" ]; then + NOTIFY= elif is_rotation_sequence "$1"; then ROT="$(current_rotation)" ROT="$(next "$1" "$ROT")" @@ -172,4 +207,5 @@ done [ -z "$SHOW_USAGE" ] || usage ( [ -n "$SHOW_USAGE" ] && [ -z "$CHANGED" ] ) || configure_input - +[ -z "$SHOW_USAGE" ] +echo success