summaryrefslogtreecommitdiff
path: root/autorotate.sh
blob: 4bf4074701fa872cfb49440ec6b24e166b2ff0d0 (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
#!/bin/bash

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
}

process_orientation () {
    case $1 in
        "normal") rotate_screen 0 ;;
        "right-up") rotate_screen 90 ;;
        "bottom-up") rotate_screen 180 ;;
        "left-up") rotate_screen 270 ;;
    esac
}

stdbuf -oL monitor-sensor | while IFS= read -r line; do
    rotation="$(echo $line | sed -En "s/^.*orientation changed: (.*)/\1/p")"
    [[ -n $rotation ]] && process_orientation $rotation
done