Advertisement
Guest User

starlite5-rotate-script

a guest
Jul 13th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.61 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Tablet Rotation Script for Arch Linux
  4. # This script automatically rotates the screen and input devices based on the tablet's orientation.
  5.  
  6. # Dependencies:
  7. # - inotify-tools: for monitoring sensor changes
  8. # - xorg-xrandr: for rotating the display
  9. # - xorg-xinput: for rotating input devices
  10. # - iio-sensor-proxy: for the monitor-sensor command
  11. # - A wallpaper restoration tool (e.g., nitrogen, feh)
  12.  
  13. # Install dependencies on Arch Linux:
  14. # sudo pacman -S inotify-tools xorg-xrandr xorg-xinput iio-sensor-proxy
  15.  
  16. ######################### ADJUST THESE SETTINGS ############################
  17.  
  18. # Use 'xinput list' command to find your device names
  19. main_device="GXTP7386:00 27C6:0111"
  20. stylus_device="GXTP7386:00 27C6:0111 Stylus"
  21. touchpad_device="GXTP7386:00 27C6:0111 Touchpad"
  22.  
  23. # Use 'xrandr' command to find your display name
  24. display="eDP-1"
  25.  
  26. # Set your preferred wallpaper restoration command
  27. # Examples:
  28. # wallpaper_command="nitrogen --restore"
  29. # wallpaper_command="feh --bg-fill /path/to/wallpaper.jpg"
  30. wallpaper_command="nitrogen --restore"
  31.  
  32. ######################### END OF SETTINGS ##################################
  33.  
  34. # Parse command line arguments
  35. VERBOSE=0
  36. while [[ "$#" -gt 0 ]]; do
  37.     case $1 in
  38.         -v|--verbose) VERBOSE=1 ;;
  39.         *) echo "Unknown parameter passed: $1"; exit 1 ;;
  40.     esac
  41.     shift
  42. done
  43.  
  44. # Debug function
  45. debug() {
  46.     if [ $VERBOSE -eq 1 ]; then
  47.         echo "DEBUG: $1" >&2
  48.     fi
  49. }
  50.  
  51. # Function to set rotation for a device
  52. set_rotation() {
  53.     local device="$1"
  54.     local matrix="$2"
  55.    
  56.     debug "Attempting to rotate device: $device"
  57.    
  58.     if xinput list --id-only "$device" &>/dev/null; then
  59.         debug "Device found, setting rotation"
  60.         if [ "$device" = "$stylus_device" ]; then
  61.             if ! xinput set-prop "$device" "libinput Calibration Matrix" $matrix; then
  62.                 debug "Failed to set libinput Calibration Matrix for $device"
  63.             else
  64.                 debug "Successfully set libinput Calibration Matrix for $device"
  65.             fi
  66.         else
  67.             if ! xinput set-prop "$device" "Coordinate Transformation Matrix" $matrix; then
  68.                 debug "Failed to set Coordinate Transformation Matrix for $device"
  69.             else
  70.                 debug "Successfully set Coordinate Transformation Matrix for $device"
  71.             fi
  72.         fi
  73.     else
  74.         debug "Device not found: $device"
  75.     fi
  76. }
  77.  
  78. # Function to rotate all devices and screen
  79. rotate_all() {
  80.     local matrix="$1"
  81.     local xrandr_rotation="$2"
  82.    
  83.     debug "Rotating all devices: $xrandr_rotation"
  84.    
  85.     set_rotation "$stylus_device" "$matrix"
  86.     set_rotation "$main_device" "$matrix"
  87.    
  88.     # Only rotate touchpad if it's connected
  89.     if xinput list --id-only "$touchpad_device" &>/dev/null; then
  90.         set_rotation "$touchpad_device" "$matrix"
  91.     fi
  92.    
  93.     debug "Rotating display: $display to $xrandr_rotation"
  94.     if ! xrandr --output "$display" --rotate "$xrandr_rotation"; then
  95.         debug "Failed to rotate display. xrandr command output:"
  96.         xrandr --output "$display" --rotate "$xrandr_rotation" 2>&1 | while read -r line; do debug "$line"; done
  97.     else
  98.         debug "Successfully rotated display"
  99.     fi
  100.    
  101.     if [ -n "$wallpaper_command" ]; then
  102.         debug "Restoring wallpaper with command: $wallpaper_command"
  103.         eval "$wallpaper_command" &>/dev/null
  104.     fi
  105. }
  106.  
  107. # Initial setup
  108. debug "Performing initial setup"
  109. rotate_all "1 0 0 0 1 0 0 0 1" "normal"
  110.  
  111. # Kill any existing monitor-sensor processes
  112. debug "Killing existing monitor-sensor processes"
  113. killall monitor-sensor &>/dev/null
  114.  
  115. # Start the monitor-sensor and redirect output
  116. debug "Starting monitor-sensor"
  117. monitor-sensor > /dev/shm/sensor.log 2>&1 &
  118.  
  119. echo "Tablet rotation script is running. Ctrl+C to stop."
  120.  
  121. # Main loop to monitor sensor changes
  122. last_orient=""
  123. while inotifywait -q -e modify /dev/shm/sensor.log &>/dev/null; do
  124.     orient=$(tail -n1 /dev/shm/sensor.log | grep 'orientation' | grep -oE '[^ ]+$')
  125.     if [[ -n "$orient" && "$orient" != "$last_orient" ]]; then
  126.         debug "Detected orientation change: $orient"
  127.         case "$orient" in
  128.             bottom-up)
  129.                 rotate_all "-1 0 1 0 -1 1 0 0 1" "inverted"
  130.                 ;;
  131.             normal)
  132.                 rotate_all "1 0 0 0 1 0 0 0 1" "normal"
  133.                 ;;
  134.             right-up)
  135.                 rotate_all "0 1 0 -1 0 1 0 0 1" "right"
  136.                 ;;
  137.             left-up)
  138.                 rotate_all "0 -1 1 1 0 0 0 0 1" "left"
  139.                 ;;
  140.         esac
  141.         last_orient="$orient"
  142.     fi
  143. done
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement