code.fastix.org

Dateiansicht:

Datei:Projekte -> Linux:Convertibles:Bildschirm drehen -> rotate-screen.sh
md5:7cb7b300a022d7eb3c8a2feca35e8b79
sha1:db2a4333797a43776193f7f71dcfd76463b22bad
Download-Link:Download
  1. #!/bin/bash
  2. # This script rotates the screen and touchscreen input 90 degrees each time it is called,
  3. # also disables the touchpad, and enables the virtual keyboard accordingly
  4. # Original by Ruben Barkow: https://gist.github.com/rubo77/daa262e0229f6e398766
  5.  
  6. ## Changings/Adds by Jörg Reinholz, Kassel (Germany)
  7. # Disable keyboard and/or touchpad in 'upside down'- and '90°'-modes
  8. # Added debug option
  9. # Added settings for Lenovo YOGA 300-11IBY
  10. # Prepared for other machines
  11. # the screen for xrandr ist now defined (its a litte faster)
  12.  
  13. #########################
  14. #     DEBUG-Option      #
  15. #########################
  16.  
  17. debug=0; # set only to 1 for developing
  18. if [ 1 -eq $debug ];
  19.         then set -x;
  20. else
  21.         set +x;
  22. fi
  23.  
  24. #########################
  25. #     Configuration     #
  26. #########################
  27.  
  28. #### At least one (or all) of the next 3 option should by enabbled! 1 = on / 0 = off
  29. manageOnboard=1;   # Set to 1 to start the program `onboard` in 'upside down' and '90°'-modes (and stop in normal-modus)
  30. manageKeyboard=1;  # Set to 1 to disable the keyboard in 'upside down'- and '90°'-modes (and enable in normal-modus)
  31. manageTouchpad=1;  # Set to 1 to disable the touchpad in 'upside down'- and '90°'-modes  (and enable in normal-modus)
  32.  
  33. ### You can safe your XFCE4-Panel-settings with `xfpanel-settings` and use them:
  34. manageXFCE4Panel=0; # 1 = on / 0 = off
  35. # Edit the path to your files with XFCE-Settings
  36. normalPanel='/path/to/normal.panel.tar.bz2';
  37. invertedPanel='/path/to/touch.panel.tar.bz2';
  38. leftPanel='/path/to/touch.panel.tar.bz2';
  39. ###
  40.  
  41. ### show xrandr for your screen, but in most cases is this the first (and only) screen:
  42. screen=0;
  43.  
  44. #### Settings for the machine(s) - show `xinput list` to find your settings.
  45. # You can use Regexes for grep -Pi
  46. touchscreensRegex='Touchscreen|ATML';                                     # Extended Regex! Show `man grep`.
  47. touchpadsRegex='Touchpad|Synaptics TM|Wacom.*Finger.*touch';              # Extended Regex! Show `man grep` .
  48. keyboardDevices=('AT Translated Set 2 keyboard' 'Ideapad extra buttons'); # Array! see `man bash`.
  49.  
  50. ########################################
  51. #             Program                  #
  52. ########################################
  53.  
  54. LANG="c"; # Hint: Do not translate outputs if you "parse" thiss stuff!
  55.  
  56. if [ 1 -eq $manageXFCE4Panel ]; then
  57.         if [ ! -x '/usr/bin/xfpanel-switch' ] || [ ! -r "$invertedPanel" ] || [ ! -r "$normalPanel" ] || [ ! -r "$leftPanel" ] || [ ! -r "$rightPanel" ] ; then
  58.                 manageXFCE4Panel=0;
  59.         fi
  60. fi
  61.  
  62. if [ "$1" = '--help'  ] || [ "$1" = '-h'  ] ; then
  63.         echo 'Usage: rotate-screen.sh [OPTION]';
  64.         echo ;
  65.         echo 'This script rotates the screen and touchscreen input 90 degrees each time it is called,';
  66.         echo 'also disables the touchpad, and enables the virtual keyboard accordingly';
  67.         echo ;
  68.         echo 'Usage:';
  69.         echo ' -h --help display this help';
  70.         echo ' -j (just horizontal) rotates the screen and touchscreen input only 180 degrees';
  71.         echo ' -n always rotates the screen back to normal';
  72.         exit 0
  73. fi
  74.  
  75. t=$(xinput list | grep -Pi "$touchscreensRegex" | tr -d '[[⎜↳]]'| sed -Ee 's/[ \t]+/ /g' | sed -Ee 's/id=.*$//');
  76. TouchscreenDevice=$(echo -n $t); #trim
  77.  
  78. if [ 1 -eq $manageTouchpad ]; then
  79.         t=$(xinput list | grep -Pi "$touchpadsRegex" | tr -d '[[⎜↳]]' | sed -Ee 's/[ \t]+/ /g' | sed -Ee 's/id=.*$//');
  80.         TouchpadDevice=$(echo -n $t); # trim
  81. fi
  82.  
  83. if [ 1 -eq $manageOnboard ]; then
  84.         if [ 1 -lt $(ps ax | grep 'onboard' | wc -l) ]; then
  85.                 onboardRunning=1;
  86.         else
  87.                 onboardRunning=0;
  88.         fi
  89. fi
  90.  
  91. touchpadEnabled=$(xinput --list-props "$TouchpadDevice" | awk '/Device Enabled/{print $NF}');
  92. screenMatrix=$(xinput --list-props "$TouchscreenDevice" | awk '/Coordinate Transformation Matrix/{print $5$6$7$8$9$10$11$12$NF}' | sed -Ee 's/\.[0-9]+//g' -e 's/,/ /g');
  93.  
  94. normal='1 0 0 0 1 0 0 0 1';
  95. inverted='-1 0 1 0 -1 1 0 0 1';
  96. left='0 -1 1 1 0 0 0 0 1';
  97. right='0 1 0 -1 0 1 0 0 1';
  98.  
  99. if [ "$screenMatrix" = "$normal" ] && [ "$1" != '-n' ]; then
  100.         if [ 1 -eq $debug ];
  101.                 then echo 'Upside down:';
  102.         fi
  103.         xrandr --screen $screen -o inverted;
  104.         xinput set-prop "$TouchscreenDevice" 'Coordinate Transformation Matrix' $inverted;
  105.         xinput disable "$TouchpadDevice";
  106.         if [ 0 -eq $onboardRunning ]; then
  107.                 onboard &
  108.         fi
  109.         if [ 1 -eq $manageOnboard ] && [ 1 -eq $manageKeyboard ]; then
  110.                 for device in "${keyboardDevices[@]}"; do
  111.                         xinput --set-prop "$device" 'Device Enabled' 0;
  112.                 done
  113.         fi
  114.         if [ 1 -eq $manageXFCE4Panel ]; then
  115.                 xfpanel-switch load "$invertedPanel";
  116.         fi
  117. elif [ "$screenMatrix" = "$inverted" ] && [ "$1" != '-j' ] && [ "$1" != '-n' ]; then
  118.         if [ 1 -eq $debug ]; then
  119.                 echo '90° to the left';
  120.         fi
  121.         xrandr --screen $screen -o left;
  122.         xinput set-prop "$TouchscreenDevice" 'Coordinate Transformation Matrix' $left;
  123.         xinput disable "$TouchpadDevice";
  124.         if [ 1 -eq $manageOnboard ] && [ 0 -eq $onboardRunning ]; then
  125.                 onboard &
  126.         fi
  127.         if [ 1 -eq $manageKeyboard ]; then
  128.                 for device in "${keyboardDevices[@]}"; do
  129.                         xinput --set-prop "$device" 'Device Enabled' 0;
  130.                 done
  131.         fi
  132.         if [ 1 -eq $manageXFCE4Panel ]; then
  133.                 xfpanel-switch load "$leftPanel";
  134.         fi
  135. else
  136.     if [ 1 -eq $debug ]; then
  137.                 echo "Back to normal";
  138.         fi
  139.         xrandr --screen $screen -o normal;
  140.         xinput set-prop "$TouchscreenDevice" 'Coordinate Transformation Matrix' $normal;
  141.         xinput enable "$TouchpadDevice";
  142.         if [ 1 -eq $manageOnboard ] &&  [ 1 -eq $onboardRunning ]; then
  143.                 killall onboard;
  144.         fi
  145.         if [ 1 -eq $manageKeyboard ]; then
  146.                 for device in "${keyboardDevices[@]}"; do
  147.                         xinput --set-prop "$device" 'Device Enabled' 1;
  148.                 done
  149.         fi
  150.         if [ 1 -eq $manageXFCE4Panel ]; then
  151.                 xfpanel-switch load "$normalPanel";
  152.         fi
  153. fi
  154.