Files
ThinkpadX1Yoga_files/usr/local/bin/thinkpad-rotate.rb
T
2018-10-02 20:14:20 +02:00

117 lines
3.0 KiB
Ruby
Executable File

#!/usr/bin/ruby
# ./grox.rb <dir>
# +left, +right, flip: relative movement
# left, right, normal, inverted: absolute movement
# devices to manipulate
$screen = 'eDP-1'
$touchscreen1 = 'Wacom Pen and multitouch sensor Pen stylus'
$touchscreen2 = 'Wacom Pen and multitouch sensor Pen eraser'
$touchscreen3 = 'Wacom Pen and multitouch sensor Finger touch'
$touchpad = ''
$keyboard = 'AT Translated Set 2 keyboard'
# disable keypad and touchpad on all but normal orientation
#$controlKeys = true
$controlKeys = false
# runs cmd and greps output to find orientation
$orientationCmd = 'xrandr'
$orientationRE = /\s*#{$screen}\s+\w+\s+\w+\s+\d+x\d+\+\d+\+\d+\s+(|left|right|inverted)\s*\(/
# default direction
$defaultDirection = '+right'
# CODE
def main()
direction = $defaultDirection
if ARGV.length > 0
direction = ARGV[0]
end
doOrientate(getNewOrientation(direction))
end
def orientateCmd(orientation, transform)
rotateScreen = "xrandr --output #{$screen}" +
" --rotate #{orientation}";
rotateTouchscreen1 = "xsetwacom --set '#{$touchscreen1}' Rotate #{transform}"
rotateTouchscreen2 = "xsetwacom --set '#{$touchscreen2}' Rotate #{transform}"
rotateTouchscreen3 = "xsetwacom --set '#{$touchscreen3}' Rotate #{transform}"
controlKeys = ""
if $controlKeys
setCmd = orientation == 'normal' ? 'xinput --enable '
: 'xinput --disable '
controlKeys = "#{setCmd} '#{$touchpad}'; #{setCmd} '#{$keyboard}';"
end
return controlKeys +
rotateScreen + ';' +
rotateTouchscreen1 + ';' +
rotateTouchscreen2 + ';' +
rotateTouchscreen3 + ';'
end
def doOrientate(orientation)
case orientation
when 'normal'
`#{orientateCmd('normal', 'none')}`
when 'left'
`#{orientateCmd('left', 'ccw')}`
when 'right'
`#{orientateCmd('right', 'cw')}`
when 'inverted'
`#{orientateCmd('inverted', 'half')}`
else
raise "Don't know how to orientate to #{orientation}"
end
end
# returns direction of $screen: left, right, normal or invert
def getOrientation()
if `#{$orientationCmd}` =~ $orientationRE
return $1 == '' ? 'normal' : $1
else
raise "Could not determine orientation of #{$screen} from #{$orientationCmd}"
end
end
# direction should be +left, +right, flip, left, right, normal, or inverted
def getNewOrientation(direction)
clockwise = ['normal', 'right', 'inverted', 'left']
if clockwise.include?(direction)
return direction
else
curdir = clockwise.find_index(getOrientation())
shift = case direction
when '+left' then -1
when '+right' then 1
when 'flip' then 2
else
raise "Unrecognised rotate direction #{direction}"
end
newdir = (curdir + shift) % 4
return clockwise[newdir]
end
end
# DO
main()