rotate.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env bash
  2. #
  3. # rotate.sh: Rotates the screen by 90 degrees counterclockwise and/or
  4. # gives the Wacom stylus input and/or TrackPoint the same orientation.
  5. #
  6. # Copyright (C) 2021 Emilia L.K. Blåsten
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. #
  22. #
  23. # Usage: You can run this file with the command line option -s, -p or
  24. # -t, or any combination, for example -sp, -ps, -tp, -tps. The
  25. # behaviour is as follows.
  26. #
  27. # -s: Rotates the screen by 90 degrees. Depending on your desktop
  28. # environment, the stylus input might get rotated or not.
  29. #
  30. # -p: Makes the stylus have the same rotation as the the screen.
  31. #
  32. # -t: Makes the trackpoint have the same rotation as the the screen.
  33. #
  34. # The script requires xrandr and xsetwacom.
  35. rotScreen=0
  36. rotPen=0
  37. rotTP=0
  38. while getopts ":spt" option; do
  39. case $option in
  40. s)
  41. rotScreen=1
  42. ;;
  43. p)
  44. rotPen=1
  45. ;;
  46. t)
  47. rotTP=1
  48. ;;
  49. \?)
  50. echo "Invalid option: -$OPTARG" >&2
  51. ;;
  52. esac
  53. done
  54. # Find the display name. On some kernels it's LVDS1, on some others it's LVDS-1
  55. display="$(xrandr | grep LVDS | sed 's/1.*/1/')"
  56. # Find the line in "xrandr -q --verbose" output that contains current
  57. # screen orientation and "strip" out current orientation.
  58. getScreenOrientation() {
  59. rotation="$(xrandr -q --verbose | grep "$display" | egrep -o '\) (normal|left|inverted|right) \(' | egrep -o '(normal|left|inverted|right)')"
  60. echo $rotation
  61. }
  62. # Find the device name from the STYLUS-entry in "xsetwacom list"
  63. device="$(xsetwacom list | grep STYLUS | sed -E "s/( |\t)*id:.*//")"
  64. # Function to set the stylus orientation to the same as the screen
  65. setStylusToScreenOrientation() {
  66. screenOrientation=$(getScreenOrientation)
  67. case "$screenOrientation" in
  68. normal)
  69. # rotate to the normal
  70. xsetwacom set "$device" rotate none
  71. ;;
  72. left)
  73. # rotate to left (counterclockwise)
  74. xsetwacom set "$device" rotate ccw
  75. ;;
  76. inverted)
  77. # rotate to inverted
  78. xsetwacom set "$device" rotate half
  79. ;;
  80. right)
  81. # rotate to right
  82. xsetwacom set "$device" rotate cw
  83. ;;
  84. esac
  85. }
  86. # Find the trackpoint device number
  87. trackpointNbr="$(xinput list | grep TrackPoint | sed -E "s/^.*id=//" | sed -E "s/( |\t).*$//")"
  88. # Set the trackpoint rotation to the same as the screen rotation
  89. setTrackpointToScreenOrientation() {
  90. screenOrientation=$(getScreenOrientation)
  91. case "$screenOrientation" in
  92. normal)
  93. # rotate to the normal
  94. xinput set-prop "$trackpointNbr" "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
  95. ;;
  96. left)
  97. # rotate to left (counterclockwise)
  98. xinput set-prop "$trackpointNbr" "Coordinate Transformation Matrix" 0 -1 1 1 0 0 0 0 1
  99. ;;
  100. inverted)
  101. # rotate to inverted
  102. xinput set-prop "$trackpointNbr" "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
  103. ;;
  104. right)
  105. # rotate to right
  106. xinput set-prop "$trackpointNbr" "Coordinate Transformation Matrix" 0 1 0 -1 0 1 0 0 1
  107. ;;
  108. esac
  109. }
  110. # Rotate the screen 90 degrees counterclockwise
  111. rotateScreen() {
  112. screenOrientation=$(getScreenOrientation)
  113. case "$screenOrientation" in
  114. normal)
  115. # rotate to left (counterclockwise)
  116. xrandr --output "$display" --rotate left
  117. ;;
  118. left)
  119. # rotate to inverted
  120. xrandr --output "$display" --rotate inverted
  121. ;;
  122. inverted)
  123. # rotate to right
  124. xrandr --output "$display" --rotate right
  125. ;;
  126. right)
  127. # rotate to the normal orientation
  128. xrandr --output "$display" --rotate normal
  129. ;;
  130. esac
  131. }
  132. if (($rotScreen)); then
  133. rotateScreen
  134. fi
  135. if (($rotPen)); then
  136. setStylusToScreenOrientation
  137. fi
  138. if (($rotTP)); then
  139. setTrackpointToScreenOrientation
  140. fi
  141. # Make sure the stylus acts on the internal display, not an external display
  142. xsetwacom set "$device" MapToOutput "$display"