Saturday, February 6, 2010

Logitech MX Revolution in Arch Linux

There is a multitude of guides out there describing various ways of getting the Logitech MX Revolution mouse working in various flavours of Linux. I figured one more guide wouldn't hurt.

Here, I describe one method using xbindkeys and xmacro.

xbindkeys

From the man page: XbindKeys is a program that grab keys and mouse button events in X and starts associated shell command.

In other words, xbindkeys lets you map keystrokes and mouse buttons to shell commands. What is less obvious is the fact that there are programs that can emulate keystrokes and mouse buttons as well; we will cover this later. What we want to do now is to create a file that defines this mapping.

Begin by installing xbindkeys. In Arch Linux, this is done with the following command:
pacman -S xbindkeys

Next, we create a sample xbindkeys configuration file in your home directory, and open it.
xbindkeys --defaults > ~/.xbindkeysrc

This is what it looks like:
# For the benefit of emacs users: -*- shell-script -*-
###########################
# xbindkeys configuration #
###########################
#
# Version: 1.8.3
#
# If you edit this file, do not forget to uncomment any lines
# that you change.
# The pound(#) symbol may be used anywhere for comments.
#
# To specify a key, you can use 'xbindkeys --key' or
# 'xbindkeys --multikey' and put one of the two lines in this file.
#
# The format of a command line is:
# "command to start"
# associated key
#
#
# A list of keys is in /usr/include/X11/keysym.h and in
# /usr/include/X11/keysymdef.h
# The XK_ is not needed.
#
# List of modifier:
# Release, Control, Shift, Mod1 (Alt), Mod2 (NumLock),
# Mod3 (CapsLock), Mod4, Mod5 (Scroll).
#

# The release modifier is not a standard X modifier, but you can
# use it if you want to catch release events instead of press events

# By defaults, xbindkeys does not pay attention with the modifiers
# NumLock, CapsLock and ScrollLock.
# Uncomment the lines above if you want to pay attention to them.

#keystate_numlock = enable
#keystate_capslock = enable
#keystate_scrolllock= enable

# Examples of commands:

"xbindkeys_show"
control+shift + q

# set directly keycode (here control + f with my keyboard)
"xterm"
c:41 + m:0x4

# specify a mouse button
"xterm"
control + b:2

#"xterm -geom 50x20+20+20"
# Shift+Mod2+alt + s
#
## set directly keycode (here control+alt+mod2 + f with my keyboard)
#"xterm"
# alt + c:0x29 + m:4 + mod2
#
## Control+Shift+a release event starts rxvt
#"rxvt"
# release+control+shift + a
#
## Control + mouse button 2 release event starts rxvt
#"rxvt"
# Control + b:2 + Release

##################################
# End of xbindkeys configuration #
##################################

It is clear that the key bindings follow the following format:
"[shell command to run]"
[keystroke to capture]
[keysym (alternate key descriptor)]

An example:
#Middle-click
"echo 'ButtonPress 2 ButtonRelease 2' | xmacroplay :0"
m:0x0 + c:225
XF86Search

In the above example, key 225 (the search key, which can be found on some multimedia keyboards with more than the standard 108 keys) is also known by the key symbol 0x1008ff1b, or XF86Search. "m" stands for modifier, and is used it you want to add a modifier key (Ctrl, Alt, Shift, and so on) to the keystroke.

You can safely do without the key symbol in the above key binding definitions as well, for example:
#Ctrl-Alt-PgUp
"echo 'KeyStrPress Control_L KeyStrPress Alt_L KeyStrPress Page_Down KeyStrRelease Page_Down KeyStrRelease Alt_L KeyStrRelease Control_L' | xmacroplay :0"
m:0x0 + b:6

Here, "b" stands for button, and we'll be using this to capture mouse button input. For defined keys, use the "c" descriptor instead, as in the earlier example. For moer key binding definitions, use the command xmodmap -pk (you will need to install xmodmap first.)



In contrast with the key bindings, the mouse bindings are much simpler. A list of mouse button bindings for the Logitech MX Revolution is shown below.
1: Left click
2: Scrollwheel click (middle-click)
3: Right click
4: Scrollwheel up
5: Scrollwheel down
6: Scrollwheel tilt left
7: Scrollwheel tilt right
8: Thumb button down
9: Thumb button up
13: Thumbwheel down
15: Thumbwheel up
17: Thumbwheel click

To find out how mouse buttons for other mice are mapped, install and run xev. On execution, xev creates a window with a black-bordered box. Move your mouse cursor inside the box and press one of the buttons. In the console output, look for a section like
ButtonPress event, serial 34, synthetic NO, window 0x3a00001,
root 0x142, subw 0x0, time 12161652, (111,59), root:(1851,1005),
state 0x0, button 5, same_screen YES

ButtonRelease event, serial 34, synthetic NO, window 0x3a00001,
root 0x142, subw 0x0, time 12161652, (111,59), root:(1851,1005),
state 0x1000, button 5, same_screen YES

That tells you which number the button is binded to.

With the above information, it is easy to create key bindings for xbindkeys to capture. Next, we figure out how to run shell commands that will emulate key presses. We will do this with xmacro.

xmacro

To simulate key presses or mouse clicks with xmacro, we use commands in the following format:

echo 'KeyStrPress KeyStrPress KeyStrRelease KeyStrRelease ' | xmacroplay :0

The section between the single-quotes (' ') describes the command which is sent to xmacro to be run.

I used the following key mappings in my .xbindkeysrc:
#Middle-click
"echo 'ButtonPress 2 ButtonRelease 2' | xmacroplay :0"
m:0x0 + c:225
XF86Search

#Ctrl-Alt-PgUp
"echo 'KeyStrPress Control_L KeyStrPress Alt_L KeyStrPress Page_Down KeyStrRelease Page_Down KeyStrRelease Alt_L KeyStrRelease Control_L' | xmacroplay :0"
m:0x0 + b:6

#Ctrl-Alt-PgDn
"echo 'KeyStrPress Control_L KeyStrPress Alt_L KeyStrPress Page_Up KeyStrRelease Page_Up KeyStrRelease Alt_L KeyStrRelease Control_L' | xmacroplay :0"
m:0x0 + b:7

#Ctrl-PgDn
"echo 'KeyStrPress Control_L KeyStrPress Page_Down KeyStrRelease Page_Down KeyStrRelease Control_L' | xmacroplay :0"
m:0x0 + b:13

#Ctrl-PgUp
"echo 'KeyStrPress Control_L KeyStrPress Page_Up KeyStrRelease Page_Up KeyStrRelease Control_L' | xmacroplay :0"
m:0x0 + b:15

#Ctrl-F4
"echo 'KeyStrPress Control_L KeyStrPress F4 KeyStrRelease F4 KeyStrRelease Control_L' | xmacroplay :0"
m:0x0 + b:17

Modify the above code to suit your own purposes, and insert it into .xbindkeysrc in your home folder.

Now run xbindkeys and check that the key bindings work. Add xbindkeys to your list of autostart applications, and enjoy using your properly mapped Logitech MX Revolution.

0 comments:

Tags

linux (4) software (4) hardware (3) musings (3) osmium (3) projects (3) science (1) typography (1)