Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 131 lines (110 sloc) 2.96 KB
#!/bin/bash
print_usage() {
sed -re '1d;s/^#\s?//g' <<METASCRIPT
#?
# Name: Thinkpad autorotate screen
# Version: 0.1
# Author: Thomas Leberbauer
# References: https://bbs.archlinux.org/viewtopic.php?id=154147 https://github.com/martin-ueding/think-rotate
# Uses: acpid x11-xserver-utils xserver-xorg-input-wacom
#
# Usage: thinkpad-autorotate-screen OPTIONS
#
# Rotates the screen automatically on Thinkpad tablets based on the
# display position.
#
# Options:
# -h Print this usage description.
# -r Set type of rotation (landscape or portrait)
METASCRIPT
}
CONF_SCREEN_KEYBOARD_ENABLED="/org/gnome/desktop/a11y/applications/screen-keyboard-enabled"
is_in_tablet_mode() {
local mode="`cat /sys/devices/platform/thinkpad_acpi/hotkey_tablet_mode`"
if [[ "$mode" == "1" ]]; then
return 0
else
return 1
fi
}
set_randr() {
local rotation="$1"
xrandr -o "$rotation"
}
set_wacom() {
local rotation="$1"
xsetwacom list devices | sed 's/id: .*//' | while read device; do
xsetwacom set "$device" rotate "$rotation"
xsetwacom set "$device" MapToOutput "LVDS1"
done
}
set_input() {
local mode="$1"
local trackpoint="`xinput list | grep TrackPoint | grep -Eo 'id=([0-9]*)' | cut -d'=' -f2`"
local touchpad="`xinput list | grep TouchPad | grep -Eo 'id=([0-9]*)' | cut -d'=' -f2`"
for input in "$trackpoint" "$touchpad"; do
if [[ $mode == "enable" ]]; then
xinput set-prop "$input" "Device Enabled" "1"
elif [[ $mode == "disable" ]]; then
xinput set-prop "$input" "Device Enabled" "0"
else
echo "ERROR [set_input]" 1>&2
fi
done
}
set_on_screen_keyboard() {
case "$1" in
enable) dconf write "$CONF_SCREEN_KEYBOARD_ENABLED" true ;;
disable) dconf write "$CONF_SCREEN_KEYBOARD_ENABLED" false ;;
esac
}
rotate_normal() {
set_randr normal
set_wacom none
}
rotate_portrait() {
set_randr right
set_wacom cw
}
rotate_landscape() {
set_randr inverted
set_wacom half
}
autorotate() {
local rotation="$1"
if is_in_tablet_mode; then
if [[ $rotation == "portrait" ]]; then
rotate_portrait
else
rotate_landscape
fi
set_input disable
set_on_screen_keyboard enable
else
rotate_normal
set_input enable
set_on_screen_keyboard disable
fi
}
while getopts :r:h opt; do
case "$opt" in
r) rotation="$OPTARG" ;;
h) metascript "$0" 2>/dev/null || print_usage; exit ;;
:) echo "Option $OPTARG requires an argument" 1>&2; exit ;;
\?) echo "Unknown option $OPTARG" 1>&2; exit ;;
*) echo "Unknown option $opt" 1>&2; exit ;;
esac
done
# adjust for current setting
autorotate "$rotation"
# listen for changes
acpi_listen | while read event; do
if [[
$event == "ibm/hotkey HKEY 00000080 00005009" # tablet
|| $event == "ibm/hotkey HKEY 00000080 0000500a" # laptop
|| $event == "video/tabletmode TBLT 0000008A 00000000"
|| $event == "video/tabletmode TBLT 0000008A 00000001"
]]; then
autorotate "$rotation"
fi
done
Something went wrong with that request. Please try again.