Monday, December 31, 2012

Running commands on resume on a Linux laptop

I recently installed Ubuntu 12.04 on a MacBook Air. There are some great instructions on how to do this on the Ubuntu wiki and everything basically went smoothly (of course, my desire to install ZFS on Linux added many complications). However, I quickly ran into a problem that I've experienced on numerous Linux laptops in the past: how to middle click.

Of course, on a normal Linux machine, middle click is an invaluable copy and paste tool. Unfortunately, modern laptops don't have any buttons, much less a middle click. Generally speaking, button clicks are simulated via multitouch capabilities of trackpads, and they do not, by default simulate middle clicks. Fortunately, it turns out that if you're using the synaptic trackpad driver (the default if you follow the instructions on the MacBook Air install wiki page), there's an easy command to turn on middle click simulation with a three-fingered click:

synclient TapButton3=2 ClickFinger3=2 PalmDetect=1

Great! Works perfectly! Except, it resets every time you suspend and resume your laptop. Apparently, when you resume from a suspend, the trackpad disappears and then reappears so the options to the synaptic driver are reset. Very frustrating. This brings us to the second problem that I've run into in the past: how to run a script on resume, which seems to be the only way to reset these settings.

How to do this varies from setup to setup, but in my case (Ubuntu on a Macbook Air), it seems that the Gnome Power Management (pm) module controls these things. In fact, it turns out that one can add a new script to run in the /etc/pm/sleep.d directory that will get run on suspend and resume.

Unfortunately, solving our particular problem is not as straightforward as we'd like. When we resume, you have to (a) wait for the X server to start up again, and (b) select what display you want to do this on. It took a while, but I was able to find a good suggestion on how to do this on a web form that you can find here.

I created the following script that solves my problems. Don't forget, you'll also need to run the command on login, but that generally is much easier. Of course, you can pretty easily generalize this script to run just about anything you need on resume.
 echo "updating middle click..."
 sleep 5
 DISPLAY=:0.0 su jww -c "synclient TapButton3=2 ClickFinger3=2 PalmDetect=1"
 echo "middle click update succeeded!"
case "${1}" in
 resume|thaw) resume_middle_click & ;;