summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--autorotate.sh69
1 files changed, 23 insertions, 46 deletions
diff --git a/autorotate.sh b/autorotate.sh
index c4b0c2b..4bf4074 100644
--- a/autorotate.sh
+++ b/autorotate.sh
@@ -1,54 +1,31 @@
#!/bin/bash
-# This script handles rotation of the screen and related input devices automatically
-# using the output of the monitor-sensor command (part of the iio-sensor-proxy package)
-# for sway.
-# The target screen and input device names should be configured in the below variables.
-# Note: input devices using the libinput driver (e.g. touchscreens) should be included
-# in the WAYLANDINPUT array.
-#
-# You can get a list of input devices with the `swaymsg -t output` command.
-#
-# This scritp was frok from https://gitlab.com/snippets/1793649 by Fishonadish
-
-
-SCREEN="eDP-1"
-WAYLANDINPUT=("1118:2485:Microsoft_Surface_Keyboard_Touchpad" "1267:10780:ELAN9038:00_04F3:2A1C")
+declare -r SCREEN=eDP-1
+declare -r WAYLANDINPUT=(
+ '1118:2485:Microsoft_Surface_Keyboard_Touchpad'
+ '1267:10780:ELAN9038:00_04F3:2A1C'
+)
+
+rotate_screen () {
+ local -r orientation=$1
+ echo "Rotating to $orientation"
+ swaymsg output $SCREEN transform $orientation
+
+ for input_device in "${WAYLANDINPUT[@]}"; do
+ swaymsg input "$input_device" map_to_output "$SCREEN"
+ done
+}
-function rotate_ms {
+process_orientation () {
case $1 in
- "normal")
- rotate 0
- ;;
- "right-up")
- rotate 90
- ;;
- "bottom-up")
- rotate 180
- ;;
- "left-up")
- rotate 270
- ;;
+ "normal") rotate_screen 0 ;;
+ "right-up") rotate_screen 90 ;;
+ "bottom-up") rotate_screen 180 ;;
+ "left-up") rotate_screen 270 ;;
esac
}
-function rotate {
-
- TARGET_ORIENTATION=$1
-
- echo "Rotating to" $TARGET_ORIENTATION
-
- swaymsg output $SCREEN transform $TARGET_ORIENTATION
-
- for i in "${WAYLANDINPUT[@]}"
- do
- swaymsg input "$i" map_to_output "$SCREEN"
- done
-
-}
-
-while IFS='$\n' read -r line; do
+stdbuf -oL monitor-sensor | while IFS= read -r line; do
rotation="$(echo $line | sed -En "s/^.*orientation changed: (.*)/\1/p")"
- [[ ! -z $rotation ]] && rotate_ms $rotation
-done < <(stdbuf -oL monitor-sensor)
-
+ [[ -n $rotation ]] && process_orientation $rotation
+done