Sunday 15 June 2008

Getting a Tablet PC Touchscreen working under Ubuntu

These are instructions I figured out for a Fujitsu-Siemens Lifebook P1510 but they should be applicable to most tablet PCs. I will note the values to change for different touchscreen resolutions.
This tutorial will also cover getting the touchscreen to map correctly when the screen is rotated for tablet mode.

My screen rotation script uses information found here with additions for correctly killing a previous instance (the touchscreen gets rather crazy when there are multiple copies of the script running for different screen rotations and they are all trying to pull the cursor in different directions).
My touchscreen script is based on one found here but with modifications for rotation.

First you need to install Perl and the required libraries:

sudo apt-get install wacom-tools
sudo apt-get install libx11-dev
sudo apt-get install libxtst-dev
sudo apt-get install x11-common
sudo apt-get install libxtest-dev


download X11::GuiTest, unzip it and install with

perl Makefile.PL
make
make test
make install


Now you need two scripts:
One to kill any previous instance of the touchscreen script, rotate the screen and fire a new instance of the touchscreen script
One to control the touchscreen - a modification of 'tablet5.en.pl' from here

I named my first script 'rotate.sh' and used this code:

#!/bin/bash

#Get the requested rotation angle from the args
angle=$1

#Kill off any previous instance of the touchscreen script
prs=`ps aux | grep tablet.pl | grep -v grep`

if [ "$prs" != "" ] ; then
read -r -a Words <<< $prs
kill -9 ${Words[1]}
fi

#Rotate the screen and fire off the appropriate Perl touchscreen script
case $angle in
normal | 0 )
xrandr -o normal
perl /drivers/tablet.pl &
;;
right | 90 )
xrandr -o right
perl /drivers/tablet.pl 90 &
;;
inverted | 180 )
xrandr -o inverted
perl /drivers/tablet.pl 180 &
;;
left | 270 )
xrandr -o left
perl /drivers/tablet.pl 270 &
;;
esac


You can replace '/drivers/tablet.pl' with the location of your touchscreen script.

Now take 'tablet5.en.pl' from here and modify is as follows:

Add the following code after the line 'use constant DIGITIZER=>(30.78,30.06);'

my $angle = '0';

if (not undef($ARGV)) {
$angle = $ARGV[0];
}


Replace the body of 'sub movemouse(@)' with:

    (my $x,my $y)=@_;
if (($x ne $prevx)||($y ne $prevy)) {

if ($angle eq '90' || $angle eq 'right') {
my $tmpx = $x;
$x = $y;
$y = (SCREEN)[0] - $tmpx;
}
elsif ($angle eq '180' || $angle eq 'inverted') {
$x = (SCREEN)[0] - $x;
$y = (SCREEN)[1] - $y;
}
elsif ($angle eq '270' || $angle eq 'left') {
my $tmpx = $x;
$x = (SCREEN)[1] - $y;
$y = $tmpx;
}

$prevx=$x;
$prevy=$y;
MoveMouseAbs($x,$y);
}


And you're done!

Now to get the touchscreen working at startup you need to add the line 'perl <path to touchscreen script> &' at the end of the file '/etc/gdm/Init/Default' (but before the exit command).

I would recommend putting links to the different screen rotations on your taskbar (there are some nice icons here) and also one to 'onboard', the very good onscreen keyboard that comes with Ubuntu.

1 comment:

aul said...

btw,, is that really work ? i also have a (semi) tablet notebook with intel atom processor that could rotate without a digitize pen.. but i dont know how to make the touchscreen thing work..
btw, mail me at freaks.guitar@gmail.com i think i will need your further advice :p