Tuesday, November 25, 2008

Automatic rotation on its way

I got a new laptop in January 2008, an HP 2150p. It has a great feature: hard disk protection when the laptop falls. Well, by itself I don't think it's a great feature at all, I'd rather stay very careful and never let the laptop fall at all. However, what this meant is that it contains an accelerometer. Since I discovered this, I've wanted to have a great feature, the same one which is available on the expensive phones (like the iPhone) and in some photo cameras (like my Canon A530), the screen automatically rotates when you physically rotate the device. It's especially useful when reading articles or books: you open the file, rotate the computer and suddenly one page = one screen.

Before going further, let's recap why an accelerometer is useful for this. An accelerometer measures the acceleration, so shouldn't it detect something only when it moves, and not when it is standing rotated or not? Well, all the trick is that we can assume the user is on earth (or a planet more generally), and that the user keeps his fits on the ground. The beauty of the accelerometer is that it detect all the accelerations, including G, the gravity! So, assuming that you are on earth and that you are not taking off in a rocket plane, the main acceleration vector indicates the direction of the ground... so convenient!

Getting the functionality working in Linux was not so hard. Mainly because Yan Burman had already tackled the main problem of writing a driver for the accelerometer. However, the real fun is to have it as a default feature for anyone using Linux. Having the ad-hoc solution is fine, but the generic solution is worth thousand times more! For this, I ended up becoming the maintainer of the driver (now named lis3lv02d) and it got recently accepted in Linus' tree. It will be part of 2.6.28...

But that's merely half the journey. We need also a daemon which observes the data obtained by the accelerometer and rotates the screen accordingly. For now I have my own little script which does this. On the long term it would be good to have it integrated with the normal desktop. Let's say at least in Gnome, I would like to get this feature integrated with the new xrandr plugin.

Last but not least, after some usage I realised that there is an annoying problem when rotating the screen with the laptop: the touchpad stays not rotated. Moving the cursor requires some special mental training! So, the obvious question is how to do that. There is unfortunately no directly available solutions. However, I'm hopeful that with the Input Device Properties that Peter Hutterer added to Xorg, plus the work by Mildred Ki'Lya for changing the rotation of the synaptic touchpad driver. To be totally generic, it would be better if any input device could be rotated (the same way), especially the evdev driver should support this as well...

So the complete ad-hoc solution is nearly there, but not yet. And the generic and upstream solution still needs lots of integration at various levels... but it's on its way! Whoever is also interested is welcome to try to create patchs (for Gnome, for Xorg) and share them.

Saturday, November 8, 2008

Xorg documentation: what a pain

X.Org provides the official reference implementation of the X Window System. Being the reference, one can expect it to find a default implementation as well as the description of its behaviour and the behaviour of the client applications with the server. Unfortunately, this is done extremely badly, amazingly badly. Up to the point that today, when looking for the description of the XV protocol (you know, the technology that allows you to watch video without overheating your processor), I got lost in their minimalistic wiki, found nothing in google, passed via wikipedia, and ended up in the cvs of the old xfree which has an update in a different file. In the X.Org website all I could find was this page about the conversion of all the documentation to DocBook which doesn't even mention where are the converted documentations!

It's really a pity, because it's make it even harder to code or to debug X. I wish they had something organized as well as the gnome/glib documentation. Actually, on the book shelves of the university, there are a couple of huge official old books on the X reference, so at least an outdated documentation must be exist, it's just impossible to find. If you know, please update the X.Org wiki!

Sunday, September 7, 2008

C magics

Recently Linus Torvalds has sent a patch for the e1000 driver. Looking at it, I'm not sure if I should be amazed by the talent of Linus, or by the fact that even after 10 years of using C there are still constructs which I don't know.

It does things like:

- struct e1000_opt_list speed_list[] = {
+ static const struct e1000_opt_list speed_list[] = {


- struct e1000_option opt = {
+ opt = (struct e1000_option) {

Not only I didn't even know this last construct was possible, but Linus reports that overall his patch reduces the stack usage from 1152 bytes to 192 bytes (the total stack is 4K)! Ahhh... how much more to learn about C?

Sunday, June 15, 2008

Spell-checking using spam

I was doing spell checking over a OCR'd version of The Long Dark Tea Time Of The Soul (Douglas Adam). The problem with OCRs errors is that they are different from the human mistakes, and the spell checker (hunspell) is not trained for these. For instance: cl instead of d, or l instead of I. So far one could suggest to write a different suggestion algorithm for hunspell (or your preferred open source spell checker). However, I was not in the mood for that, and something very interesting turned out of this.

I thought I would use the power of the web by finding the correct text as quotations of the book spread everywhere. Although it seems that people don't quote much The Long Dark Tea Time Of The Soul, nearly every sentence is available! It seems that some kind of spammer has decided to use this book as carpet text for fake websites linking to its products. There are literally thousands of pages hanging around, each of them containing a tiny fragment of the book. The book is entirely available on internet but as unordered sentences mixed with links to commercial products!

Ok, where it comes really interesting is that they seem to have used a much better OCR algorithm than me (or they spent a lot of time fixing the errors) so the sentences are near perfect. So you can use this spam as spell checker. For instance "And where there is something which is not dealt with properly in your world," the old lady pranled on. "pranled on"? What the heck is this verb? You go on google with the previous sentence, and, there, you immediately find out that the correct word is "prattled on". Thank you spammers!