You are not logged in.

#1 2012-12-05 14:14:49

Erhan
Member
Registered: 2012-12-05
Posts: 12

Autorotate script for Thinkpad Tablets (x200 Tablet)

Hey everyone!

I am using an x200 tablet at the moment and I wanted to get autorotate to work including the digitizer but I coulnd't find something that satisfied me. So with the help of the internet I made this small script. It works on my tablet and checks every second if the laptop is in tablet and rotates per xrandr and xsetwacom.
I put the file in /bin/ and run it with my .xinitrc as autorotate &

Well here is the most important thing:

#!/bin/bash
# Working autorotate on thinkpad x200 tablet
rstate="0"
while [ true ] 
do
	react=`grep 0 -c /sys/devices/platform/thinkpad_acpi/hotkey_tablet_mode`
	if (( ($react == "1") && ($rstate=="1") )); then
	 /usr/bin/xrandr -o normal
	 xsetwacom set "Serial Wacom Tablet WACf008 stylus" Rotate none
 	 xsetwacom set "Serial Wacom Tablet WACf008 eraser" Rotate none
	 xsetwacom set "Serial Wacom Tablet WACf008 touch" Rotate none
	 rstate="0"
	fi
	if (( ($react != "1") && (rstate == "0") )); then
	 /usr/bin/xrandr -o right                                 
	 xsetwacom set "Serial Wacom Tablet WACf008 stylus" Rotate cw
	 xsetwacom set "Serial Wacom Tablet WACf008 eraser" Rotate cw
	 xsetwacom set "Serial Wacom Tablet WACf008 touch" Rotate cw
	 rstate="1"
	fi
 sleep 1
done

Last edited by Erhan (2012-12-05 15:03:54)

Offline

#2 2012-12-05 14:36:59

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,485
Website

Re: Autorotate script for Thinkpad Tablets (x200 Tablet)

This could be greatly simplified with inotify which would have the added benefit of removing the potential 2 second delay.

At the very least you could replace the useless cat and variable with the following:

if [[ $(grep -q 0 /sys/devices/restofpath) ]]; then

Last edited by Trilby (2012-12-05 14:37:51)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#3 2012-12-05 15:04:55

Erhan
Member
Registered: 2012-12-05
Posts: 12

Re: Autorotate script for Thinkpad Tablets (x200 Tablet)

Thank you very much. I optimized the code a little more now and it doesn't consume as much resources as before.
I just wonder if using grep is less resource hungry then cat.
What do you think?

Offline

#4 2012-12-05 15:41:11

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,485
Website

Re: Autorotate script for Thinkpad Tablets (x200 Tablet)

The way you have implemented it, I suspect it is more resource hungry.  I wasn't suggesting using grep to extract the number from the file, just as a conditional test of whether the file stores a 0 or not.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#5 2012-12-05 16:01:20

Erhan
Member
Registered: 2012-12-05
Posts: 12

Re: Autorotate script for Thinkpad Tablets (x200 Tablet)

I have two if statements, would that mean that i would have to grep two  times as a conditional test instead of using the output once?

My idea was that it reacts on the fact if the lid is open or in tablet mode. Moreover I want it to check if it is ALREADY in tablet mode so that it doesn't run  xrandr and xsetwacom when there is no need to it.

Last edited by Erhan (2012-12-05 16:08:16)

Offline

#6 2012-12-05 16:36:25

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,485
Website

Re: Autorotate script for Thinkpad Tablets (x200 Tablet)

Sorry, I think I have provided more confusion than assistance.  These are really trivial issues when it is still looping continuously when it could use inotify instead.  Try something like the following:

#!/bin/bash
# depends: inotify-tools

file="/sys/devices/platform/thinkpad_acpi/hotkey_tablet_mode"

while inotifywait -qq -e modify $file; do
	if [[ "$(cat $file)" == "0" ]]; then
		xrandr -o normal
		xsetwacom set "Serial Wacom Tablet WACf008 stylus" Rotate none
		xsetwacom set "Serial Wacom Tablet WACf008 eraser" Rotate none
		xsetwacom set "Serial Wacom Tablet WACf008 touch" Rotate none
	else
		xrandr -o right
		xsetwacom set "Serial Wacom Tablet WACf008 stylus" Rotate cw
		xsetwacom set "Serial Wacom Tablet WACf008 eraser" Rotate cw
		xsetwacom set "Serial Wacom Tablet WACf008 touch" Rotate cw
	fi
done

Last edited by Trilby (2012-12-05 16:45:07)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#7 2012-12-05 17:23:55

Erhan
Member
Registered: 2012-12-05
Posts: 12

Re: Autorotate script for Thinkpad Tablets (x200 Tablet)

Thank you very much for your answer.

-e modify doesn't seem to react to the modifications. I can cat and proof that the file was modified but inotifywait stays quiet. I tried it with -e access and it reacted to cat at least. I'm trying to find out now why "modify" doesn't react to the modification.

But I like your idea because it would just go to the if statements if the file was changed. So less resources would be used this way.
Only drawback is the extra dependency but that's okay.

Last edited by Erhan (2012-12-05 17:25:46)

Offline

#8 2013-04-07 19:57:10

slosd
Member
Registered: 2011-11-23
Posts: 22
Website

Re: Autorotate script for Thinkpad Tablets (x200 Tablet)

This thread is a little old, but this might help somebody else. You cannot use inotifywait in this case because the file you want to watch is never modified. Its content is basically generated when you read it, which, if you use inotifywait, you never do.

An alternative is to listen for acpi events. That's what I did in this script: https://github.com/slosd/bin/blob/maste … ate-screen
Instead of inotifywait I use acpi_listen:

acpi_listen | while read event; do
if [[
        $event == "ibm/hotkey HKEY 00000080 00005009" # tablet
     || $event == "ibm/hotkey HKEY 00000080 0000500a" # laptop
     ]]; then
    autorotate "$rotation"
  fi
done

Last edited by slosd (2013-04-07 20:07:56)

Offline

Board footer

Powered by FluxBB