Monday, July 1, 2013

Sound Volume Management Through Multimedia Keys in Openbox

My laptop has those multimedia keys which don't work automatically in Openbox. This is how I fix it. You can use the same method for LXDE, just take note of the different config file location.
A lot of the Openbox stuff I use is from here.

Open the Openbox main config file. Check here for a preliminary reading on how to edit the bugger. It's XML, so it looks like a mess, but if you use a nice editor that has coloured mark up, like vim or nano, it will be easier.
The standard location for the config file in Openbox should be:



 .  
 ~/.config/openbox/rc.xml  



and for LXDE:



 .  
 ~/.config/openbox/lxde-rc.xml  



In the section called
 <keyboard> 
 you can add all your keyboard shortcuts.
In most cases, the kernel will automatically recognise your multimedia key input. However, in some cases you might have to manually assign those keys. In case these instructions don't work, this might be the culprit. See here for more help.

Make sure that amixer is available on your machine (in Debian, that would be the package alsa-utils). Even though this is an ALSA utility, this works perfectly well with Pulseaudio, too.

We will add the following code so the Volume Up button works:



 .  
   <keybind key="XF86AudioRaiseVolume">  
    <action name="Execute">  
     <startupnotify>  
      <enabled>true</enabled>  
      <name>volumeup</name>  
     </startupnotify>  
     <command>amixer -c 0 set Master 2%+ unmute</command>  
    </action>  
   </keybind>  



I've rendered the amixer command in bold letters. I've also set the command to raise the volume by 2% each time I hit the key. If that is too finely grained for you, just pick a higher percentage.

Here's the same example for a command to lower the volume:



 .  
   <keybind key="XF86AudioLowerVolume">  
    <action name="Execute">  
     <startupnotify>  
      <enabled>true</enabled>  
      <name>volumedown</name>  
     </startupnotify>  
     <command>amixer -c 0 set Master 2%- unmute</command>  
    </action>  
   </keybind>  



The only difference here is a minus instead of a plus sign.
Finally, we need a way to mute/unmute sound.
There is a little issue here which makes unmuting the sound on Pulseaudio not work, which is why I will present 2 separate configurations for ALSA and Pulseaudio, respectively.  I've fixed this issue based on this post.
For ALSA:


 .  
   <keybind key="XF86AudioMute">  
    <action name="Execute">  
     <startupnotify>  
      <enabled>true</enabled>  
      <name>volumemute</name>  
     </startupnotify>  
     <command>amixer -c 0 set Master toggle</command>  
    </action>  
   </keybind>  



For Pulseaudio:



 .  
   <keybind key="XF86AudioMute">  
    <action name="Execute">  
     <startupnotify>  
      <enabled>true</enabled>  
      <name>volumemute</name>  
     </startupnotify>  
     <command>amixer -D pulse set Master 1+ toggle</command>  
    </action>  
   </keybind>  



I hope you get the idea behind all this. The multimedia keys mostly already have a certain denominator in X.org, making it easier to assign anything to these keys. You could use xev as described in the preliminary link above on the Arch Wiki to find out what the other keys have as denominators. You can thus set up the rest of the bunch, such as the calculator or the play/pause/skip buttons and you could assign any command to them you like.

However, there is just one issue with all this - we don't get any visual feedback as to whether anything is happening, since amixer is a command line programme run in the background.
Fortunately, there is a nifty tool from the Xfce desktop environment that only pulls in gstreamer and a few basic libs you have on any Linux installation anyway. The tool is called Xfce Volume Daemon. In Debian, the package is xfce4-volumed.
You have to run the command in your .xinitrc / .xsession or Openbox autostart file. Just put this among your other commands:



 .  
 xfce4-volumed &  



You will get a libnotify pop-up window that actually follows your GTK theme.
Of course, in order for this to work, you need a notification daemon. I suggest xfce4-notifyd. It even comes with a very nifty tool to customise the looks of the notification bubble:



 .  
 xfce4-notifyd-config &  



That's all.

1 comment:

  1. Tried with Fedora 21 and Openbox, works like a charm.

    ReplyDelete