Saturday, April 27, 2013

Volume OSD with Openbox and Riitek Micro Keyboard

With this small bash script you can show a nice progress bar on the screen when changing the volume in a TV-like fashion.
The script uses osd_cat provided by the xosd package under Arch Linux (Please refer to your system specific package manager for install this package under other distributions).

In order to make this script work with Openbox you will have to edit the Openbox configuration file rc.xml file normally placed under /home/<user>/.config/openbox/rc.xml. I will show how I did it later on this post.

OSD Bash Script

A few function definitions make the main part simpler. They are mainly responsible of increasing/decreasing the volume and showing the on-screen message.
There are also some constants responsible for placing the message on a certain screen position, setting the font, etc.
test.sh
 1 #!/bin/bash
 2 
 3 
 4 
 5 # constants
 6 FONT='-*-fixed-*-*-*-*-100-*-*-*-*-*-*-*'
 7 COLOR="green"
 8 DELAY=4
 9 POS="bottom"
10 ALIGN="center"
11 BARMOD="percentage"
12 VOLTXT="Volume"
13 VOLMUTEDTXT="Muted"
14 VOLSTEP=2
15 
16 
17 
18 # kills an existing osd_cat process
19 # needed when holding down a key to force repaint of the onscreen message
20 preKill() {
21     killall osd_cat
22 }
23 
24 # gets the actual volume value
25 getVol() {
26     VOL="$(amixer sget Master | grep "Mono:" | awk '{ print $4 }' | sed -e 's/\[//' -
           e 's/\]//' -e 's/\%//')"
27 }
28 
29 # gets the actual volume value and prints is on the screen
30 # with a percent bar + a percent number
31 showVol() {
32     getVol
33     if [[ $VOL == 0 ]]; then
34         osd_cat --pos="$POS" --align="$ALIGN" --delay="$DELAY" --colour="$COLOR" --
                         font="$FONT" --barmode="$BARMOD" --text="$VOLMUTEDTXT $VOL%" --
                         percentage="$VOL"
35     else
36         osd_cat --pos="$POS" --align="$ALIGN" --delay="$DELAY" --colour="$COLOR" --
                         font="$FONT" --barmode="$BARMOD" --text="$VOLTXT $VOL%" --
                         percentage="$VOL"
37     fi
38 }
39 
40 # reises the master channel by "VOLSTEP"
41 volUp() {
42     amixer sset Master "$VOLSTEP+"
43 }
44 
45 # decreases the master channel by "VOLSTEP"
46 volDown() {
47     amixer sset Master "$VOLSTEP-"
48 }
49 
50 # mutes the master channel
51 volMute() {
52     amixer sset Master 0
53 }
54 
55 
56 # main part
57 preKill
58 
59 case "$1" in
60     "volup")
61             volUp
62             showVol
63             ;;
64     "voldown")
65             volDown
66             showVol
67             ;;
68     "mute")
69             volMute
70             showVol
71             ;;
72     *)
73             ;;
74 esac

Openbox rc.xml File

The rc.xml file has a keyboard section where custom keyboard shortcuts can be defined.

x.xml
 1     <keybind key="W-KP_Add">
 2       <action name="execute">
 3         <execute>/home/adrian/scripts/osd.sh volup</execute>
 4       </action>
 5     </keybind>
 6     <keybind key="W-KP_Subtract">
 7       <action name="execute">
 8         <execute>/home/adrian/scripts/osd.sh voldown</execute>
 9       </action>
10     </keybind>
11     <keybind key="W-m">
12       <action name="execute">
13         <execute>/home/adrian/scripts/osd.sh mute</execute>
14       </action>
15     </keybind>
Riitek Micro Keyboard

Besides the keyboard shortcuts, I wanted to use my Riitek Micro Keyboard RT-MWK03. When pressing the Vulume Up/Down/Mute key combination on the keyboard an X event is generated on the X server.
These X events can be used in the rc.xml file just the same way as any other keyboard shortcut. I used Xev to find out what events are generated when pressing the volume key combinations.
y.xml
 1     <keybind key="XF86AudioRaiseVolume">
 2       <action name="execute">
 3         <execute>/home/adrian/scripts/osd.sh volup</execute>
 4       </action>
 5     </keybind>
 6     <keybind key="XF86AudioLowerVolume">
 7       <action name="execute">
 8         <execute>/home/adrian/scripts/osd.sh voldown</execute>
 9       </action>
10     </keybind>
11     <keybind key="XF86AudioMute">
12       <action name="execute">
13         <execute>/home/adrian/scripts/osd.sh mute</execute>
14       </action>
15     </keybind>


Screenshots


Openbox OSD Script Volume 59%

Openbox OSD Script Muted Volume


2 comments:

  1. This is helpful but I may have trouble with the configuration. May be I should just ask my IT guy to fix it for me.

    ReplyDelete
  2. some scripts using osd_cat, (xosd) some i put in crontab
    https://gist.github.com/cirrusUK/6832961e7b16badddbceea01f655bfad

    ReplyDelete