Skip to content

Instantly share code, notes, and snippets.

@emiller
Last active April 22, 2024 15:44
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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 & }
@ozydingo
Copy link

I'm trying to learn from this, but am stopped with two questions. Some difference may be because I'm using a Yoga 2.

  1. I can't see any e03e keycode being emitted. I'm using showkey -s, and see nothing when I flip the screen to tablet position. Should I be expecting something here? How else could I monitor the keycode?

  2. After using setkeycode e03e 247, which does not return any error, I still see nothing in the 247 row (same for 230) when I type xmodmap -pk. Do you know of any reason this command would silently fail?

thanks!!

@emiller
Copy link
Author

emiller commented Dec 11, 2013

It's possible your kernel actually recognizes the keycodes properly already, so you're lucky. You can use a utility called xev or evtest to determine what keycode those actions map to. Just run xev and then flip your yoga into tablet mode and back, rinse repeat, and you'll see what keycodes are getting emited.

@jans23
Copy link

jans23 commented Dec 13, 2013

You describe "I [...] set an additional hotkey binding in my window manager to execute theyoga-auto-rotate script whenever the hotkey is pressed." I didn't manage to do so with Ubuntu's Unity. Could you elaborate how to setup the hotkey binding, please?

@emiller
Copy link
Author

emiller commented Dec 14, 2013

Open gnome-control-center and go into the keyboard area, in there will be an area to set hot keys that will run a command.

@mehanzhi
Copy link

Hello,

This sounds really great, but I cannot find any package that provides binary or script with name input-events.
I am using Fedora and I searched the repositories with yum provides and yum search, but nothing came up.
Could you please give me a tip, what is this binary and what package is it coming from, how can I get it.
Please also give a note about any other software requirements that we need to install like xrandr or xinput.
Otherwise, the script looks great. I am looking forward to test it.

Thank you.

@emiller
Copy link
Author

emiller commented Dec 18, 2013

@mehanzhi The package for debian is input-utils, and I think is the same for Fedora too:

http://pkgs.org/download/input-utils

xrandr and xinput should come standard I think with any xorg/desktop installation on most distros. To find out what provides what, you can run yum whatprovides <name>. For example:

$ yum whatprovides \*/xrandr
xorg-x11-server-utils-7.5-13.7.amzn1.x86_64 : X.Org X11 X server utilities
Repo        : amzn-main
Matched from:
Filename    : /usr/bin/xrandr

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