Sunday, May 25, 2014

Configure the Logitech MX Revolution extra buttons in Ubuntu

I love my Logitech MX Revolution mouse. It has been a part of my desktop setup for over 5 years. Its ridiculously quiet, scrolls super smooth, has just the right amount of buttons, and feels great in my hand.

Getting all the buttons on the the Logitech MX Revolution mouse working in Ubuntu 14.04 is easy and straightforward. It involves generating and editing your xorg.conf.

Static xorgs are not used or needed in Ubuntu and other modern Linux distributions anymore, but using one correctly will not harm your system. First you will need to generate an xorg configuration file if you don't have one already.

I have an Nvidia card in my desktop and use the nvidia-settings tool to automatically generate my xorg.conf.

To have the nvidia-settings tool generate an xorg.conf file
sudo nvidia-xconfig

You can also generate a xorg.conf file my hand. By switching to a virtual console using the key combination: CTRL + ALT + F1

Now execute the following commands:
sudo service gdm stop
- This command will stop the X.

sudo Xorg -configure
- this will generate the xorg.conf file in your home folder

sudo mv ~/ /etc/X11/xorg.conf
- move the newly created xorg file to the correct location

sudo reboot
- to reboot the machine, and allow the changes to take effect.

Install the needed software
sudo apt-get install xbindkeys xvkbd xautomation

- xbindkeys is a program that allows you to launch shell commands with your keyboard or your mouse under X Window. It links commands to keys or mouse buttons, using a configuration file.

- xvkbd is a virtual (graphical) keyboard program for X Window System which provides facility to enter characters onto other clients (softwares) by clicking on a keyboard displayed on the screen.

- xautomation Control X from the command line for scripts, and do "visual scraping" to find things on the screen. The control interface allows mouse movement, clicking, button up/down, key up/down, etc,

Edit you xorg.conf file using your preferred editor, gedit, mousepad, leafpad, nano
sudo mousepad /etc/X11/xorg.conf

Insert this text in the Mouse section of Input Device
Option     "Buttons"    "17"

Your file should look similar to this
Section "InputDevice"
    # generated from default
    Identifier     "Mouse0"
    Driver         "mouse"
    Option         "Protocol" "auto"
    Option         "Device" "/dev/psaux"
    Option         "Emulate3Buttons" "no"
    Option         "ZAxisMapping" "4 5"
    Option         "Buttons" "17"

Create your xbindkeys configuration file
touch ~/.xbindkeysrc

Edit your xbindkeys configuration file
mousepad ~/.xbindkeysrc

Here is my file:
# forward
"/usr/bin/xvkbd -text "\[Alt_L]\[Left]""
m:0x0 + b:8

# back
"/usr/bin/xvkbd -text "\[Alt_L]\[Right]""
m:0x0 + b:9

# next tab
"/usr/bin/xvkbd -text "\[Control_L]\[Page_Up]""
m:0x0 + b:13

# previous tab
"/usr/bin/xvkbd -text "\[Control_L]\[Page_Down]""
 m:0x0 + b:15

# undo closed tab
"/usr/bin/xvkbd -text "\[Control_L]\[Shift]\[t]""
 m:0x0 + b:16

# mouse wheel left middle click
"xte 'mouseclick 2'"

# mouse wheel right middle click
"xte 'mouseclick 2'"

- I used xvkbd to emulate key combinations on my controller
 and xte to emulate middle click

I didn't enjoy the search button so I turned it into a middle click button, by adding the following to my /etc/sysctl.conf file
sudo mousepad /etc/sysctl.conf

# Enable mouse button emulation
dev.mac_hid.mouse_button_emulation = 1
# Set 2nd button to 217 - the middle button of MX Revolution mouse.
dev.mac_hid.mouse_button2_keycode = 217

Here is a reference for all the MX Revolution buttons
   # b:1    -    left mouse button
   # b:2    -    left and right mouse button together
   # b:3     -    right mouse button
   # b:4    -    mouse wheel up
   # b:5    -    mouse wheel down
   # b:6    -    mouse wheel left
   # b:7    -    mouse wheel right
   # b:8    -    back button
   # b:9    -    forward button
   # b:10    -    -none-
   # b:11    -    -none-
   # b:12    -    -none-
   # b:13    -    media wheel up
   # b:14    -    -none-
   # b:15    -    media wheel down
   # b:16    -    -none-
   # b:17    -    media wheel press