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

Advertisements




BASH script to add GUI to cp

21 07 2011

Hi folks!

Got an interesting BASH script for you this time. I regularly use a BASH script to clone a HDD using cp, and other than perhaps using “-v”, I couldn’t think of a way to give it a GUI. A little research and thought into it came up with the following result:

#!/bin/bash
SRC="/mnt"
TGT="/mnt2"
echo "Copying $SRC to $TGT"
[[ -w /tmp/fifo ]] && rm /tmp/fifo
mkfifo /tmp/fifo
exec 5<>/tmp/fifo
(cat <&5 | dialog --guage "Copying OS and data files from source to target HDD" 7 30) &
CPPID=$!
TOTAL=$(df | grep $SRC | awk '{print $3}' | tr '\n' '+' | sed 's/.$/\n/' | bc)
while true; do
    COPIED=$(df | grep $TGT | awk '{print $3}' | tr '\n' '+' | sed 's/.$/\n/' | bc)
    echo 100*$COPIED/$TOTAL | bc >&5
    if [ ! "$(ps aux | awk '{print $2}' | grep $CPPID)" ]; then
        break
    fi
    sleep 10
done
5<&-
reset

Apologies for the truly horrible formatting. WordPress’s code tags aren’t the best! Now I’ll do my best to explain each stage…

First up, the script sets variables for the locations to copy from and to. These must be mounted volumes in this example, as we use `df` to detect them. This wouldn’t be too difficult to convert to use some other method of working out the size, such as `du`, if you aren’t using a mounted volume.

We then create a pipe called fifo in /tmp. This is how we will feed data to `dialog`.

Now we connect file descriptor 5 to the pipe, to facilitate the connection to our ncurses dialog, then pipe FD 5 into the `dialog` program, yet again forked into the background.

Next, we find the total size of the source partition, and set it to the variable $TOTAL.

Here we set the copy process off, and fork it into the background, so the script will jump through to the next section, which regularly updates our instance of `dialog`.

Here’s the interesting bit… We run a while true loop, which every 10 seconds, checks the cumulative size of the target partition ($COPIED), and compares it to $TOTAL by converting it to a percentage, using bc. This number is then fed into the pipe, which we have connected to dialog. Each loop, we also check to see if the copy process is still running, and if not, we break out of the loop.

To clean up, we close the file descriptor (5<&-), and use `reset`, as dialog has a nasty habit of breaking the terminal, and `clear` doesn't fix it.

As always, if you have any questions, tips, improvements or corrections, just direct them straight to the comments!

n00b