Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Tablet Rotation Script for Arch Linux
- # This script automatically rotates the screen and input devices based on the tablet's orientation.
- # Dependencies:
- # - inotify-tools: for monitoring sensor changes
- # - xorg-xrandr: for rotating the display
- # - xorg-xinput: for rotating input devices
- # - iio-sensor-proxy: for the monitor-sensor command
- # - A wallpaper restoration tool (e.g., nitrogen, feh)
- # Install dependencies on Arch Linux:
- # sudo pacman -S inotify-tools xorg-xrandr xorg-xinput iio-sensor-proxy
- ######################### ADJUST THESE SETTINGS ############################
- # Use 'xinput list' command to find your device names
- main_device="GXTP7386:00 27C6:0111"
- stylus_device="GXTP7386:00 27C6:0111 Stylus"
- touchpad_device="GXTP7386:00 27C6:0111 Touchpad"
- # Use 'xrandr' command to find your display name
- display="eDP-1"
- # Set your preferred wallpaper restoration command
- # Examples:
- # wallpaper_command="nitrogen --restore"
- # wallpaper_command="feh --bg-fill /path/to/wallpaper.jpg"
- wallpaper_command="nitrogen --restore"
- ######################### END OF SETTINGS ##################################
- # Parse command line arguments
- VERBOSE=0
- while [[ "$#" -gt 0 ]]; do
- case $1 in
- -v|--verbose) VERBOSE=1 ;;
- *) echo "Unknown parameter passed: $1"; exit 1 ;;
- esac
- shift
- done
- # Debug function
- debug() {
- if [ $VERBOSE -eq 1 ]; then
- echo "DEBUG: $1" >&2
- fi
- }
- # Function to set rotation for a device
- set_rotation() {
- local device="$1"
- local matrix="$2"
- debug "Attempting to rotate device: $device"
- if xinput list --id-only "$device" &>/dev/null; then
- debug "Device found, setting rotation"
- if [ "$device" = "$stylus_device" ]; then
- if ! xinput set-prop "$device" "libinput Calibration Matrix" $matrix; then
- debug "Failed to set libinput Calibration Matrix for $device"
- else
- debug "Successfully set libinput Calibration Matrix for $device"
- fi
- else
- if ! xinput set-prop "$device" "Coordinate Transformation Matrix" $matrix; then
- debug "Failed to set Coordinate Transformation Matrix for $device"
- else
- debug "Successfully set Coordinate Transformation Matrix for $device"
- fi
- fi
- else
- debug "Device not found: $device"
- fi
- }
- # Function to rotate all devices and screen
- rotate_all() {
- local matrix="$1"
- local xrandr_rotation="$2"
- debug "Rotating all devices: $xrandr_rotation"
- set_rotation "$stylus_device" "$matrix"
- set_rotation "$main_device" "$matrix"
- # Only rotate touchpad if it's connected
- if xinput list --id-only "$touchpad_device" &>/dev/null; then
- set_rotation "$touchpad_device" "$matrix"
- fi
- debug "Rotating display: $display to $xrandr_rotation"
- if ! xrandr --output "$display" --rotate "$xrandr_rotation"; then
- debug "Failed to rotate display. xrandr command output:"
- xrandr --output "$display" --rotate "$xrandr_rotation" 2>&1 | while read -r line; do debug "$line"; done
- else
- debug "Successfully rotated display"
- fi
- if [ -n "$wallpaper_command" ]; then
- debug "Restoring wallpaper with command: $wallpaper_command"
- eval "$wallpaper_command" &>/dev/null
- fi
- }
- # Initial setup
- debug "Performing initial setup"
- rotate_all "1 0 0 0 1 0 0 0 1" "normal"
- # Kill any existing monitor-sensor processes
- debug "Killing existing monitor-sensor processes"
- killall monitor-sensor &>/dev/null
- # Start the monitor-sensor and redirect output
- debug "Starting monitor-sensor"
- monitor-sensor > /dev/shm/sensor.log 2>&1 &
- echo "Tablet rotation script is running. Ctrl+C to stop."
- # Main loop to monitor sensor changes
- last_orient=""
- while inotifywait -q -e modify /dev/shm/sensor.log &>/dev/null; do
- orient=$(tail -n1 /dev/shm/sensor.log | grep 'orientation' | grep -oE '[^ ]+$')
- if [[ -n "$orient" && "$orient" != "$last_orient" ]]; then
- debug "Detected orientation change: $orient"
- case "$orient" in
- bottom-up)
- rotate_all "-1 0 1 0 -1 1 0 0 1" "inverted"
- ;;
- normal)
- rotate_all "1 0 0 0 1 0 0 0 1" "normal"
- ;;
- right-up)
- rotate_all "0 1 0 -1 0 1 0 0 1" "right"
- ;;
- left-up)
- rotate_all "0 -1 1 1 0 0 0 0 1" "left"
- ;;
- esac
- last_orient="$orient"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement