Mapping multimedia keys in Linux
By default no “extra” keys have ever worked on my Arch Linux installs. Today I made an effort for solving that and I succeeded primarily on the basis of this forum thread, this blog post, this article and Extra Keyboard Keys in Xorg on the ArchWiki. The task is simple:
Adjust the output volume on my computer when I press the multimedia keys (play, mute, volume up, volume down) on my keyboard.
How key presses are registered and handled (simplified)
So what happens on the inside when you press the keys on my keyboard? Well, each key press is recognized by the X server as an X event. When an event is registered the X server executes a routine assigned to that specific event. E.g. if the letter “k” is pressed it will simply echo a “k” to the application that is listening for keyboard input. This “application” is usually the window manager running on top of the X server and when the window manager registers the keyboard input it will pass it on to the application which has focus right now. Could be Firefox. Let’s have a small ASCII flowchart to sum up the example. Firefox has focus, runs inside the window manager and the window manager runs on top of the X server.
The user presses the key "k" inside Firefox -> The X server registers the "k-key-pressed"-event -> The X server passes the event to the window manager -> The window manager passes the event to Firefox which has focus -> The letter "k" is written into the address bar, a textarea, whatever.
Brilliant. Now, what happens if there’s no X server routine assigned to a specific X event? Nothing. This is exactly the reason why the multimedia keys does not work in my X by default on Arch Linux, thus the problem we are going to address in the following. Before that I should say that the problem is not distribution nor window manager related. You’ll encounter it on any installation where the X server is not set up properly or there’s no dummy application taking care of the specific event.
Identify keycodes for multimedia keys
Each key on your keyboard has a keycode assigned within the X server whether there’s a routine assigned for it or not. First we need to find out which keycodes Play, Mute, Volume up and Volume down have. We can do this by running xev inside a terminal instance running in X. You’ll most likely have xev already, if not then install it. When you have it running, press the four keys shortly right after each other. Look for output similar to this:
KeyRelease event, serial 28, synthetic NO, window 0x2800001, root 0x253, subw 0x0, time 28502634, (263,0), root:(3169,410), state 0x0, keycode 172 (keysym 0x1008ff14, XF86AudioPlay), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False KeyRelease event, serial 28, synthetic NO, window 0x2200001, root 0x253, subw 0x0, time 21416440, (235,1), root:(3141,411), state 0x0, keycode 121 (keysym 0x1008ff12, XF86AudioMute), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False KeyRelease event, serial 28, synthetic NO, window 0x2200001, root 0x253, subw 0x0, time 21417224, (235,1), root:(3141,411), state 0x0, keycode 122 (keysym 0x1008ff11, XF86AudioLowerVolume), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False KeyRelease event, serial 28, synthetic NO, window 0x2200001, root 0x253, subw 0x0, time 21417968, (235,1), root:(3141,411), state 0x0, keycode 123 (keysym 0x1008ff13, XF86AudioRaiseVolume), same_screen YES, XLookupString gives 0 bytes: XFilterEvent returns: False
These are the outputs of the four key presses Play, Mute, Volume up and Volume down and I’ve marked the parts relevant to us bold.
Map keycodes to routines for multimedia control in X
Routines for volume adjustments in X is defined by the “labels” XF86Audio*. xmodmap is a small utility that will bind any of your keys to labels by referencing them to keycodes. The procedure for setting it up is simple. Install it if necessary, then create a file .Xmodmap in your home directory and put the keycodes and labels in it. In my case (and probably also yours) the content will look like this.
keycode 172 = XF86AudioPlay keycode 121 = XF86AudioMute keycode 122 = XF86AudioLowerVolume keycode 123 = XF86AudioRaiseVolume
Run
xmodmap ~/.Xmodmap
to activate it in the current session. To make it permanently start when you start X, put the command in your ~/.xinitrc also.
Bind volume multimedia keys
All of the four keys actually works now when multimedia applications like mplayer, VLC and Rhythmbox have focus in your window manager. But the volume keys currently do not control the ALSA sound system directly (I assume that’s what your using for sound). By adjusting the volume directly in ALSA rather than your player you adjust it globally.
ALSA can be controlled by amixer (you already have amixer if you have ALSA installed), so what we want to do is bind an amixer shell command to our volume keys.
This can be done in many ways and I’m going to share three of them with you here. Two window manager (Xmonad, Awesome) specific ways and one standalone utility (xbindkeys) for doing it. The thing is, if you have key binding support in your window manager already there’s no reason to use a standalone utility for it, but if your window manager does not support or handle key bindings very well, it’s nice to have an alternative.
Xmonad window manager
The Xmonad window manager provides key binding support configurable in the Haskell configuration file ~/.xmonad/xmonad.hs. I’m going to assume that you have one of these already and just tell you what you need to add. Xmonad does not have key names for the ones we’re going to add so we have to refer to their hex value which can be found in the file /usr/share/X11/XKeysymDB. Find the three relevant volume labels and remember their value (yours are most likely identical to mine). Now, add them to the key bindings section in your Xmonad config file. I’ve added the following to my config file.
-- Alsa keyboard control -- XF86AudioMute , ((0 , 0x1008ff12), spawn "amixer -q set PCM toggle") -- XF86AudioLowerVolume , ((0 , 0x1008ff11), spawn "amixer -q set PCM 2- unmute") -- XF86AudioRaiseVolume , ((0 , 0x1008ff13), spawn "amixer -q set PCM 2+ unmute")If you want you can safely change the PCM channel to ie. Master and/or maybe the lower/raise values. Reload the config file by pressing Mod+q.
Awesome window manager
Accoring to this thread, the following three key bindings should work. Add them to your ~/.config/awesome/rc.lua configuration file.
keybinding({}, "#122", function () awful.util.spawn("amixer -q sset PCM 2dB-") end):add() keybinding({}, "#123", function () awful.util.spawn("amixer -q sset PCM 2dB+") end):add() keybinding({}, "XF86AudioMute", function () awful.util.spawn("amixer -q sset Master toggle") end):add()Again, feel free to change PCM and/or the lower/raise values. I haven’t tried that method myself but they look easier than what I had luck on my laptop in my Awesome days. I used these lines my Awesome config file.
table.insert(globalkeys, key({ }, "XF86AudioRaiseVolume", function () volume("up", tb_volume) end)) table.insert(globalkeys, key({ }, "XF86AudioLowerVolume", function () volume("down", tb_volume) end)) table.insert(globalkeys, key({ }, "XF86AudioMute", function () volume("mute", tb_volume) end))They assume that you have the volume() function defined somewhere else in the config file. This function is available my Awesome config file.
xbindkeys
In fact, xkeybinds can bind your keycodes directly to your amixer commands, thus skipping the label mumbo jumbo. Very convenient. Another advantage is that it’ll work on any window manager. You only have to install it, refer it to your config file, add it to your ~/.xinitrc and you’re good to go. For now, create the file ~/.xbindkeysrc and put the following in it.
# Vol down "amixer -q set PCM 2- unmute" XF86AudioLowerVolume # Vol up "amixer -q set PCM 2+ unmute" XF86AudioRaiseVolume # Vol mute/unmute "amixer -q set Master toggle" XF86AudioMuteI think there’s no reason to explain that, but still, feel free to change PCM and/or the lower/raise values.
Wrapping up
Hopefully you got it working. The same methods can be used to enable other multimedia keys. This article “Enabling the Multimedia Keys on your Keyboard” is very useful for that. Like that article I’d probably recommend the xkeybinds solution in most cases as it is very consistent, feature-full and portable. That said, Xmonad as I use for it now also does a fine job. I’ve had no problems with it, it’s easy and I like the facts that it’s integrated in my window manager. Matter of taste.
Thank you for reading.
X crashing with Arch, dual monitor and xorg-server 1.6.1
After my recent 64-bit install I experienced some instability with X. I narrowed the problem down to: X crashes in a dual monitor setup when I hold down backspace, the arrows, maybe other keys inside any application.
The problem was a little hard to google so I threw the question at the Arch Linux Forum.
It turns out you have to install a patched version of the xorg server. Because I had some trouble figuring out dependencies, etc., here is the procedure I got working:
- If you don’t have yaourt installed, install it. If you don’t know what yaourt is, it is a wrapper around pacman that allows you to easily install user contributed packages from AUR. So, go ahead and install it.
- You may want to log of X.
- Perform the following and final steps. The yaourt command will take a while to execute but keep an eye on it anyway to discover it if any errors occur.
# pacman -R nvidia xorg-server # pacman -S libgl # yaourt -S xorg-server-warnaud # pacman -R libgl # pacman -S nvidia
To you with a similar problem, I hope you will find this helpful.
More on the Web 2.0
In relation to my last post on “Operating systems and web applications” I’d like to share with you an article I just read.
http://www.guardian.co.uk/technology/2008/sep/29/cloud.computing.richard.stallman
I can’t help but completely agree and realize that I have to go cold turkey on the web applications. Some may even say “go back in time”. Soon!
Another status
Ok, sorry for my inconsistency, but I’m returning to WordPress again. I’ll import my tumblr post now.
Highlights from the Free and Open Source Software conference in Aalborg
Today I went to the Free and Open Source Software (FOSS) conference in Aalborg. It concerned two main topics, secure programming and real-time/embedded systems. Here are some highlights and reasons I was glad that I went. While it was incredibly relevant for my education, I didn’t find the real-time/embedded part very interesting. It was plain boring, so I won’t comment on that.
Development tools
During the day several helpful tools for software development were introduced and recommended. That included tools for bug tracking, documentation, etc. Especially interesting ones were doxygen, doxygen-gui, graphviz, valgrind and splint.
Git eye-opener
A guy named Esben Haabendal talked about git through practical examples. As opposed to some documentation about git I’ve read online which I found a bit arbitrary and complex, this talk was really an eye-opener for me. Even though the presentation itself wasn’t particularly good, the talk provided an introduction to some terminology within git, basic commands/operations and more importantly the context in which they are used. Also the brief explanation of distributed version control systems and how they implement various workflow models was very interesting. I can definitely see why git outrules subversion or other centralized version control systems in larger projects.
The OpenBSD guys
There were two german OpenBSD developers, Henning Brauer and Sven Dehmlow, present. Two funny but also extremely OpenBSD religious characters. I really enjoyed their presentation about code security and rightness. Particularly, I found the part about chroot and privilege seperation interesting. But also their slides. They were different with colors, beers as progressbars, legos as flowchart, etc.
Book recommendations
Henrik Lund Kramshøj talked about security tools, mainly for programming also. His talk was alright and he recommended a couple of books. They were:
- Secure Coding: Principles & Practices – Principles and Practices by Mark G. Graff and Kenneth R. Van Wyk
- 19 Deadly Sins Of Software Security – Programming Flaws And How To Fix Them by Michael Howard and David LeBlanc
Wrapping up
There are several other things from the conference on my list that I plan to look more into, so I guess in all it was 200 dkr well spent. The food there was also much better than expected, but I was disappointed that we didn’t get a t-shirt.
To zoom out a bit, the conference reminded me what great community exists around the open source idea. Once again I’m really motivated to contribute.
Finally, all talks were video recorded, so they should become available online together with slides etc. at some point. The talks from last year’s conference are available at http://mirrors.dotsrc.org/blivklogere/foss_aalborg/2008/.