Toggling Drivers With Keyboard Shortcuts

I have a Macbook Pro which has a large touchpad that is really sensitive so its always clicking on stuff while I am typing, which is extremely annoying. I have tried going the syndaemon route to disable the touchpad while typing, but it is very unreliable in my experience and I’d rather go about doing this without another process running in the background. This is a Linux specific tutorial, so if you want to know how to do this on Windows or OS X I apologize as this will not work for you. I do most everything in a terminal except web browsing (and even then I use elinks unless I need flash or fancy javascript), DWM is my window manager and I use vimperator with Firefox so I hardly use my mouse as it is.  Who needs a mouse when I already have 100 buttons at my disposal?  Anyway, I was looking for a way to quickly toggle my touchpad driver on and off so I can use it when I need to and then quickly get rid of it again.  While this guide is specifically for DWM and my Macbook touchpad, if you alter the scripts for your particular drivers and set up the keyboard shortcuts with whatever window manager you use, this guide should be fairly universal.

Here is a simple bash script that will determine whether a module is loaded and insert/remove accordingly:

#!/bin/bash
if [ $(lsmod | grep bcm5974 | wc -l) = "0" ]
then
# mouse driver not found, need to load
modprobe bcm5974
swarp 512 300
else
# mouse driver found, need to remove
swarp 9999 9999
rmmod bcm5974
fi

I have this script located in /usr/local/bin/toggletp.  bcm5974 is the name of the macbook’s touchpad driver and swarp is a program that will move your cursor to specific coordinates on the screen.  Unfortunately there is no way to disable the mouse cursor in X so my solution was to move the cursor to the very bottom right corner of the screen where it can’t be seen when the module is disabled.
Now we are going to need a simple sudo wrapper since modprobe/rmmod require root access:

#!/bin/sh
sudo /usr/local/bin/toggletp

Now in order for you to be able to execute this script requiring root access without a password, you will need to edit the sudoers file to allow this. I tried using setuid originally until I realized that setuid doesn’t work on shell scripts :(

Here is the line I use in my sudoers file:

%wheel ALL=(root) NOPASSWD: /usr/local/bin/toggletp

Wheel is just a group that my user belongs to. This is just the bsd style way of managing root access. This line allows you to execute the toggletp script as root without a password. Make sure that you set the executable bit on these scripts and lock down access since they can execute commands as root without passwords.

Now, these are the relevant lines from my DWM config.h so set the keyboard shortcuts for my scripts:

/* commands */
static const char *dmenucmd[] = { "dmenu_run", "-fn", font, "-nb", normbgcolor, "-nf", normfgcolor, "-sb", selbgcolor, "-sf", selfgcolor, NULL };
static const char *termcmd[]  = { "urxvt", NULL };
static const char *tpcmd[] = { "/home/al3k/build/toggletp", NULL };
static const char *mpdpause[] = { "mpc", "toggle",NULL };
static const char *mpdnext[] = { "mpc", "next", NULL };
static const char *mpdprev[] = { "mpc", "prev", NULL };

static Key keys[] = {
/* modifier                     key        function        argument */
{ MODKEY,                       XK_p,      spawn,          {.v = dmenucmd } },
{ MODKEY,                       XK_Return, spawn,          {.v = termcmd } },
{ MODKEY,                       XK_z,      spawn,          {.v = tpcmd } },

The third line and the last line are the chunks relevant to this guide. Copying these lines into your config.h will make it so that every time you press MODKEY+Z, it will execute the sudo wrapper for toggling your driver (mine is located in the build directory in my home folder). Now recompile dwm, reload, and , if everything worked out correctly, you should be able to toggle your laptop touch pad on and off at will with a simple keyboard shortcut!

Leave comments if you have any questions, I’m happy to help. Also, I cannot take credit for the touchpad driver bash script, a friend of mine came up with it and I modifed it for my use.

  1. No comments yet.

  1. No trackbacks yet.