Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatic Stylus (Pen) Rotate Orientation
#!/bin/sh
# Automatic stylus (pen) orientation for HP Spectre x360
# adapted by Efrem Rensi
# This is a fix for an apparent bug in Cinnamon whereby System Settings >> General >> Automatic Screen Rotation
# Rotates the screen and touch functionality, but the pen orientation does not rotate
# Based on chadm's script at https://linuxappfinder.com/blog/auto_screen_rotation_in_ubuntu.
# Receives input from monitor-sensor (part of iio-sensor-proxy package) and sets the touchscreen
# orientation based on the accellerometer positionn. We assume that the display rotation is
# handled by Linux Mint 18.1, Cinnamon 3.2.7. If this is not the case, add the appropriate
# xrandr command into each case block.
# This script should be added to startup applications for the user.
PEN=18
# The device number PEN=18 is obtained by running
# xinput
# and reading the output, which for my HP Spectre x360 is:
# ⎡ Virtual core pointer id=2 [master pointer (3)]
# ⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
# ⎜ ↳ ELAN2514:00 04F3:2592 id=11 [slave pointer (2)]
# ⎜ ↳ ELAN2514:00 04F3:2592 Pen Pen (0) id=18 [slave pointer (2)]
# ⎜ ↳ SynPS/2 Synaptics TouchPad id=15 [slave pointer (2)]
# + other lines
# Kill any existing monitor-sensor instance, for example if manually invoking
# from a terminal for testing.
killall monitor-sensor
# Launch monitor-sensor and store the output in a RAM based file that can be checked by the rest of the script.
# We use the RAM based file system to save wear where an SSD is being used.
monitor-sensor > /dev/shm/sensor.log 2>&1 &
# Parse output of monitor sensor to get the new orientation whenever the log file is updated
# Possibles are: normal, bottom-up, right-up, left-up
# Light data will be ignored
while inotifywait -e modify /dev/shm/sensor.log; do
# Read the last few lines that were added to the file and get the last orientation line.
ORIENTATION=$(tail /dev/shm/sensor.log | grep 'orientation' | tail -1 | grep -oE '[^ ]+$')
echo "$ORIENTATION"
# Set the actions to be taken for each possible orientation
case "$ORIENTATION" in
bottom-up)
xinput set-prop $PEN 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1;;
normal)
xinput set-prop $PEN 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1;;
right-up)
xinput set-prop $PEN 'Coordinate Transformation Matrix' 0 1 0 -1 0 1 0 0 1;;
left-up)
xinput set-prop $PEN 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1;;
esac
done
# On stopping this script, don't forget that "monitor-sensor" is still running - hence the "killall" !
@arneko

This comment has been minimized.

Copy link

@arneko arneko commented May 3, 2019

Thanks for sharing this. Haven't tried it yet, because I am still trying to find out why it isn't working out of the box, which is my ultimate goal ;-)

I found these in my xsession-errors:

(csd-xrandr:6337): common-plugin-WARNING **: 13:50:46.162: Error in setting "Coordinate Transformation Matrix" for "ELAN0732:00 04F3:24DB"

Not sure if they're only there, because I activated some additional debug output. I haven't found the origin of that output yet though. I would also expect this to include some reference to the "Pen", because only the Stylus part is not rotating properly, the above device (which is normally finger touch) seems to work fine.

I also haven't quite figured out the relationship between libinput and libwacom - and who'd be responsible for our devices. Any idea?

@ebrensi

This comment has been minimized.

Copy link
Owner Author

@ebrensi ebrensi commented Mar 28, 2020

hey @arneko I just now saw your comment. I guess my notifications are off or something. I ended up not using auto-rotate anyway because I wanted to use it to read and code while lying sideways in bed. Rather than auto rotate I opted to have it be manual so I could keep it in one orientation. I have switched over to Manjaro XCFE anyway, which I like better. I hope you managed to work out whatever the problem was!

@wizzlestyx

This comment has been minimized.

Copy link

@wizzlestyx wizzlestyx commented Jul 9, 2020

Thank you so much! I barely have any coding knowledge and I was able to read through your instructions and get this working. I second the recommendation of running the code through the terminal first. Just copy/paste the code directly into the terminal all at once, but be sure to change the PEN ID at the very beginning to whatever corresponds on your computer like the instructions say. This way you can test it out, and make sure that you aren't missing any packages. My terminal notified me when I first ran that I was missing inotifywait, but that was a quick and easy install.

Thanks again, this was my first GitHub code package that I've ever used!

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