Skip to content

Instantly share code, notes, and snippets.

@emiller
Last active April 22, 2024 15:44
Show Gist options
  • Save emiller/6488449 to your computer and use it in GitHub Desktop.
Save emiller/6488449 to your computer and use it in GitHub Desktop.
Lenovo Yoga 13 tablet mode / auto rotation script (ubuntu/debian)
#!/bin/bash
#
# yoga-auto-rotate -- ghetto-style tablet mode, with keyboard and all.
#
# Simple little script that will detect an orientation change for a
# Lenovo Yoga 13 (very hackily) and adjust the active display's
# orientation and disable/enable the touchpad as necessary.
#
# The Yoga 13 will emit keycode `e03e` at one second intervals
# when the screen is flipped into tablet mode. Since this keycode
# isn't actually mapped to anything by default, this script will
# use those events to determine if it should rotate the display or
# not.
#
# To make this work, the keycode that the system emits needs to be
# mapped first. You can pick a keycode to map it to via the `xmodmap -pk`
# command. The keycode mapping I used personally was `231`, for example:
#
# setkeycodes e03e 231
#
# I run the above `setkeycodes` mapping command on start-up, and
# set an additional hotkey binding in my window manager to execute the
# `yoga-auto-rotate` script whenever the hotkey is pressed.
#
# It may be necessary to add the following commands to `/etc/sudoers`
# for your user or group, as `setkeycodes` and `input-events` may not
# allow it by default:
#
# %users ALL=NOPASSWD: /usr/bin/setkeycodes, /usr/bin/input-events
#
# @author emiller
# @date 2013-09-08
input="Virtual core keyboard"
output="LVDS1"
touchpad="SynPS/2 Synaptics TouchPad"
stub="/tmp/.yoga-tablet-watcher"
interval=2
function timestamp() {
seconds=-1
if [ -f $stub ]; then
filemtime=`stat -c %Y $stub`
currtime=`date +%s`
seconds=$(( ($currtime - $filemtime) ))
fi
echo $seconds
}
function toggle_tablet() {
orientation=`xrandr --properties | grep $output | cut -d ' ' -f4 | sed 's/(//g'`
case $1 in
enable)
xrandr --output $output --rotate right
xinput disable "$touchpad"
which onboard && nohup onboard >/dev/null 2>&1 &
which easystroke && nohup easystroke >/dev/null 2>&1 &
;;
disable)
xrandr --output $output --rotate normal
which onboard && pkill -9 -f onboard
which easystroke && pkill -9 -f easystroke
xinput enable "$touchpad"
;;
esac
}
function update_timer() {
echo $(timestamp) > $stub
}
function clear_timer() {
rm -f $stub
}
function watcher() {
toggle_tablet "enable"
update_timer
sudo input-events -t $interval `xinput --list "$input" | head -n 1 | cut -d= -f2 | sed 's/\S*\[.*//g'`
toggle_tablet "disable"
clear_timer
}
test -f $stub || { watcher & }
Copy link

ghost commented Jan 4, 2014

Hi,
your script sounds lovely but I unfortunately cannot make it work.
I run at startup "sudo setkeycodes e03e 231" and then "sudo yoga-autorotate"; the screen changes to 90 ° for 1 second but then the screen return to his original position.
After that; whenever I flip the screen nothing happens.

I actualy run Debian Wheezy 7.3 on a Lenovo Yogo 13; input-utils is installed.

This is what the console returns when I enter "xmodmap -pk | grep 231"
231 0xff69 (Cancel) 0x0000 (NoSymbol) 0xff69 (Cancel)

And when I flip the screen "showkey -s" returns me: 0xe0 0x5a 0xe0 0xda

Any idea?
Thank you in advance for your answer. :)
(Sorry for my scholar english...)

@tjsquared
Copy link

Hey, does this script align the touchscreen input with the display rotation?

@emiller
Copy link
Author

emiller commented Apr 16, 2014

It sure does!

@rubo77
Copy link

rubo77 commented Jun 20, 2014

I created a similar script rotate-screen.sh for the Yoga 2 Pro: https://gist.github.com/rubo77/daa262e0229f6e398766#file-rotate-screen-sh

@F4FXL
Copy link

F4FXL commented Oct 28, 2015

Hi,

I Just installed Debian Sid on a Lenovo Yoga 2 Pro 13. Whilst the Keyboard is disabled by default the touchpad remains active. I will try to see if I can tweak your script to achieve this.

@vak
Copy link

vak commented Nov 22, 2016

the device ID fed to the input-events via $(xinput --list ...) is not matching at least for Ubuntu 16.04.

@chuugar, btw, this mismatch seems to be the reason why it all returns to original position -- because now events are being catched from the wrong /dev/input/event* and input-events just exits

@johnnemo
Copy link

Hello,

I own an hp spectre x360 (late 2017) laptop. This is the only script I find promising so far. But I get "output LVDS1 not found". Note that both the input and the touchpad names are the same with yours (xinput list). So I wonder, what is this output variable correspond to? And, if this script is not suitable for me are you aware of another one for my model. I am using Ubuntu 16.04

@tindzk
Copy link

tindzk commented Mar 14, 2018

@johnnemo LVDS1 is your display's identifier. It may be a different one on your HP laptop. You can get a listing of all displays using the xrandr command. In my case, it is called eDP-1.

You might also want to check out this program: https://github.com/mrquincle/yoga-900-auto-rotate/

@Rvillarreal2
Copy link

I just got a Lenovo IdeaPad Flex 14API 81ss
How would i determine the input/ouput or find device names like "touchpad"

@wolfgang-noichl
Copy link

I just wanted to bring this tool to your attention: https://github.com/wolfgang-noichl/magick-rotation
I more or less blindly forked it from an orphaned launchpad project, to keep my Fujitsu T901 running. But I would be very happy for contributors who want to support their convertible as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment