Thursday, August 11, 2005

amixer is afriend

For whatever reason, I have to adjust the PCM track in order to change volume in GNOME; the Master track does nothing. I suspect it's something specific to my sound card. It isn't especially inconvenient as long as I want to use the volume control built into the GNOME panel. I just right-clicked on the speaker icon, selected Preferences and told it to control the PCM track. Unfortunately adjusting the volume using multimedia keys has proven more difficult.

GNOME 2 provides a utility to bind an action to a key or keys. It can be accessed from the terminal with the command gnome-keybinding-properties or from the GNOME Desktop (System -> Preferences -> Keyboard Shortcuts). However, if you want to perform an unlisted action, you'll have to use the Configuration Editor. In my case, I wanted to adjust the PCM track volume.

The GNOME Configuration Editor can run from the terminal with the command gconf-editor or from the GNOME Desktop (Applications -> System Tools -> Configuration Editor). I used it to set key bindings for Metacity.

Using Xev and Editing Xmodmap

Before I could begin I needed to know if the keys to which I wanted to bind actions already had keysyms—symbolic names for keys on the keyboard—assigned to them. I opened the terminal and entered xev.

Xev prints the contents of X events. According to its manual, "Xev creates a window and then asks the X server to send it events whenever anything happens to the window…" This includes typing. So pressing a multimedia key should return something like this.

KeyPress event, serial 29, synthetic NO, window 0x3000001,
root 0x8e, subw 0x0, time 412303, (-458,206), root:(527,303),
state 0x10, keycode 160 (keysym 0x0, NoSymbol), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False


We're interested in the third line. "NoSymbol" indicates that there isn't a keysym assigned to keycode 160. Therefore I had to add them to my X keyboard map. First I exported the existing map to a file in my home directory (/home/josh on my system).

xmodmap -pke > xmodmap.conf

I added the missing keysyms for the corresponding keycodes. There's a specific set of names you must use, listed in /usr/lib/X11/XKeysymDB. I saved and closed the file. Then I applied the changes.

xmodmap xmodmap.conf

At this point, you could actually read ahead and bind actions to the keysyms to see if they work. Of course, the new keyboard map isn't just for this session. We want to load it when X starts. Following the advice of Corey Burger, I added it to the PostLogin script of gdm.

sudo cp xmodmap.conf /etc/xmodmap.conf
cd /etc/X11/gdm/PostLogin
sudo cp Default.sample Default
sudo gedit Default


We need to add the line xmodmap /etc/xmodmap.conf to the end of the file. Now we're ready to use the Configuration Editor.

GNOME Configuration Editor

Run the Configuration Editor and navigate to /apps/metacity/global_keybindings. There should be 12 keys with "run_command" in their names. Right-click on any of them and select "Edit Key…" from the popup menu. Enter the keysym of the desired multimedia key in the "Value" field. Repeat these steps for each multimedia key you want to use. Next we'll tell Metacity which command to run when these keys are pressed.



The ALSA mixer can be controlled from the command line with amixer. To mute/unmute the PCM track, you would enter amixer -q sset PCM,0 toggle. To lower or raise the PCM track volume by 10 percent, you would enter amixer -q sset PCM,0 10%-,10%- unmute or amixer -q sset PCM,0 10%+,10%+ unmute respectively. These commands become the values for the appropriate keys. The changes take effect immediately, so you can verify that the commands work as expected before you close the Configuration Editor. If you used the values I did, you should now be able to control the PCM track volume from your keyboard.

5 comments:

Christy said...

A geeky rockstar? A rockstar geek? l337 h4xx0rz by day and rockstar by night? I'd wondered where you'd disappeared to lately. :D

travisfisher21033769 said...
This comment has been removed by a blog administrator.
J said...

Thanks for the post - it was very helpful! Just one comment - running Ubuntu Dapper I have to put single quotation marks (') around the channel when using the amixer command: ie amixer -q sset 'PCM',0 toggle rather than amixer -q sset PCM,0 toggle. Other than that, a great guide! Thanks!

Anonymous said...

this seems helpful but I've got an issue that requires further info. My bluetooth wireless MX5000 causes a different kind of event (not a keypress) when I use its volume slider and I don't see a key code anywhere in the output. does that mean this method won't work for me?

Josh Zenker said...

No, unfortunately this method probably doesn't apply to the MX5000—or at least not to the slider controls.

Which distro are you using? I can give you more specific advice if I know that. Here's what I found on the Ubuntu Forums:

HOW TO: Logitech MX5000 via (bluetooth)

I also saw a project called mx5000tools, which might help:

mx5000tools - Control your Logitech MX5000 keybord [sic]