summaryrefslogtreecommitdiff
path: root/autorotate.sh
blob: 0c9e58ac6f4ce5ad986361b4ae28e67ca4ca22a5 (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
32
33
34
35
36
37
38
#!/bin/bash

set -euf -o pipefail

declare -r WAYLANDINPUT=(
    '1118:2485:Microsoft_Surface_Keyboard_Touchpad'
    '1267:10780:ELAN9038:00_04F3:2A1C'
)
declare -r SCREEN=eDP-1

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
}

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

main