Reverse Xorg scrolling in Linux (‘Natural Scrolling’).

26 07 2011

Hey guys,

I found an interesting snippet of information today. In OSX Lion, scrolling is reversed, like we have seen on a lot of touch applications. I for one, find this form of scrolling much more natural than the standard method, particularly when using a laptop touchpad.

It is also, really easy to implement on any Linux system. I can’t claim credit for this entirely. I have paraphrased the necessary Linux terminal commands from this python app.

The only commands you will need are `xinput`, `grep` and `sed`. The original code used gawk to separate text fields, but I thought I’d use sed, as it gives me a chance to show you some more regex pattern matching, which I have decided will be a running theme through as many of my posts as possible.

The first thing to do is to extract the slave input device’s xinput id. This is done using `xinput list`, and grep/sed to extract the exact number we want. Here’s the command:
xinput list | egrep "slave.*pointer" | grep -v XTEST | sed -e 's/^.*id=//' -e 's/\s.*$//'
This extracts the id number by first matching the regex “slave.*pointer”, which means match the letters “slave”, then anything, then the letters “pointer”. It then makes sure that our output ignores any line which contains the characters “XTEST”. Now it has the line it wants, it needs to extract the correct field. As I mentioned, I’ve used sed instead of awk (which is possibly slightly more readable – I never said my option was the better one). I use sed -e, so I can search and replace multiple regex in the order I specify. The first sed expression is to match anything from the start of the line (^.*), until it reaches the end of the characters “id=”, and replace them with nothing (//). The second sed expression starts matching at the first whitespace found (\s), and continues on until the end of the line ($). It is not necessary to include the $, as sed will default to matching until the end of the line anyway. I have included it only for the purposes of explanation.

Now we have our id number (in my case “11”), we can find out what the current input order is. For this, we use the following command, replacing {idnum} with the number we extracted before:
xinput get-button-map {idnum}
This will most likely output a sequence of numbers, in order. If the numbers are in order, this means that the scrolling is not reversed. What we need to do to reverse the scrolling, is reverse numbers 4 and 5, which represent the scroll wheel. We do this by using a very similar command to the one we just used. In this, replace {order} with the same sequence you saw in the output of `xinput get-button-map`, with 4 and 5’s order reversed, and {idnum} with our extracted input id again:
xinput set-button-map {idnum} {order}
That’s it done! Go to any application with a scroll-wheel activated scroll function, and watch it in action! To switch back, just repeat the last command with 4 and 5 back in the correct order!

To simplify this whole process, check out the Python code at the link I mentioned previously. You may or may not be able to use it as it is, because it uses PyGTK, and therefore KDE users will have to port it to PyKDE in order to use it. It’s pretty simple as long as you have any programming experience. Even if not, I encourage you to have a look – Python’s an amazing language!

Anything you want to know in more detail, or any corrections/improvements you would like to suggest, please direct it to the comments section, and I’ll do my best to get back to you as soon as I can!

n00b

About these ads

Actions

Information

8 responses

26 08 2011
eMBee

why bother with grep and set, just run xinput list; and pick the right id using your brain. (this is not intending to say that you are not using your brain, but rather trying to point out that our brain is sometimes more efficient than giving complex instructions to a computer.)

greetings, eMBee.

26 08 2011
n00bsys0p

Hi eMBee,

The idea behind using a piped command, is that it can then be used in a shell script, and run during startup, rather than at the user’s request. Also, no matter how you use Linux, it helps to have a good grasp of the various terminal commands you have at your disposal. I fully agree with your point, though – for the matter of just trying to do this on a one-off basis, just using your brain would be a far more efficient method! Thanks for your input! :-)

n00b

29 08 2011
Jordan

Thanks for the tutorial, I didn’t know about xinput, and this is much better than fiddling with xorg.conf, which has a nasty habit of crashing X whenever there is a syntax error.

29 08 2011
n00bsys0p

Glad to be able to help, Jordan. To follow newer posts, check my new blog found at http://n00bsys0p.co.uk. It’s got all the content from this site, and more!

n00b

11 10 2011
beeman

What about this?

xmodmap -e “pointer = 1 2 3 5 4 6 7 8 9 10 11 12″
;)

11 10 2011
n00bsys0p

Hi beeman,

That would work too! I’ll update the article on my new site to reflect that. I’ve discontinued this blog, so I’ll not be making any changes to it. Please check out my new blog and see what you think. There’s all the content from here, plus a whole bunch of new stuff.

n00b

27 07 2012
ouaibe

if you want to exchange also right and left
xinput set-button-map 11 1 2 3 5 4 7 6 9 8 10 11 12

27 07 2012
n00bsys0p

Thanks for the tip!

n00b

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: