Monday, 18 August 2008

Controlling MPD with a media keyboard in Ubuntu

I've switched to using MPD as my music-player under Ubuntu. It's very lightweight, and allows me to control the playing music from my desktop on the Ubuntu box, or from my laptop (using the superb Minion firefox addon). Note that MPD is just a daemon that sits in the background playing music, you need something like Minion to control it.

My keyboard is a wireless S510 keyboard, with accompanying wireless mouse and 'media remote'. I'd like to use this to control MPD, so the 'media buttons' i.e. the play/pause, stop, previous and next buttons, all control MPD.

The easiest way to achieve this is to use xbindkeys along with mpc, a command-line mpd client. With mpc you can control mpd by issuing command like mpc next or mpc stop. Xbindkeys allows you to fire off custom commands when you hit certain buttons.

To use xbindkeys you need to find the 'key codes' that correspond to your media keys. Hopefully the codes I show below should work for you, but if not try running the xev command from a terminal, press the media keys and look at the output. Looking below you can see that key-code 153 corresponds to the 'next track' button, for example. Once you have the key-codes enter the following into your ~/.xbindkeysrc file.

"/usr/bin/mpc toggle"
c:162

"/usr/bin/mpc next"
c:153

"/usr/bin/mpc prev"
c:144

"/usr/bin/mpc stop"
c:164

Note that "mpc toggle" toggles the play/pause state of mpd. Also note that I've left Gnome controlling the volume since it does a fine job already.

The last job to do (if you're using Gnome, as is the default in Ubuntu) is to stop Gnome from grabbing the keypresses from the media buttons. Otherwise xbindkeys will never see the keypresses. Do this using the following 4 gconftool commands (the word-wrap looks screwed up, copy & paste should work?):

gconftool-2 --set /apps/gnome_settings_daemon/keybindings/next --type string disabled

gconftool-2 --set /apps/gnome_settings_daemon/keybindings/previous --type string disabled

gconftool-2 --set /apps/gnome_settings_daemon/keybindings/play --type string disabled

gconftool-2 --set /apps/gnome_settings_daemon/keybindings/stop --type string disabled

So given you have your ~/.xbindkeysrc file set-up, run xbindkeys and everything should work. Note that by default xbindkeys runs as a daemon in the background, so for debugging try xbindkeys -n -v to allow you to see the debugging messages.

Once it's all working you should add xbindkeys to start automatically when you log in. In Gnome go to System -> Preferences -> Sessions -> Startup Programs. Add an entry for xbindkeys.

A final note concerning using passwords, mpd and mpc. I use a password with my mpd (very weak security but still...). For mpd to use passwords you need to set the MPD_HOST env variable. Do this in your ~/.bashrc and your ~/.gnomerc files by adding the following command (it's likely you won't have a .gnomerc file so create one). The .bashrc entry ensures mpc works from terminal and ssh sessions, the latter ensures that mpc can see the env variable when it is kicked off by xbindkeys. This is the line to add to those two files:
export MPD_HOST=MyPassword@localhost

No comments: