Haiku volume shortcuts

Speaker

AFAIK almost all portable computers (eg. laptops) have volume and brightness control shortcuts, you can find the self-explaining symbols on your keyboard, mostly you have to use them with the Fn key together.

The signals will be sent to the EC which will notify some methods in the ACPI tables. The problem is: the most OEM ACPI implementations are utterly garbage. It could be useful to control some features without an assisting OS, but ACPI queries the OS string and the DSDT have own codeapths for the most important OS families (Windows and Linux) and every other OS have to lie to get it somewhat work.

The predefined keyboard shortcuts aren’t working on Haiku so i always had to go to the system mixer to change the output volume. I even placed a volume replicant on the Desktop, but it have little use if a window covers it.

So i just seen, Haiku provides a setvolume binary in /system/bin which accepts an integer. But what if i want to increase or decrease the volume? There is currently no way to do that, so i wrote a small bash script to set the volume. Let’s see, how it works:

#! /bin/sh

SETVOLUME=setvolume
GETVOLUME=`$SETVOLUME | cut -d " " -f 3`
CLI="$*"
  
case "$CLI" in
  lower*)
    (( GETVOLUME-- ))
    $SETVOLUME $GETVOLUME
  	;;
  higher*)
    (( GETVOLUME++ ))
    $SETVOLUME $GETVOLUME
  	;;
   *) echo "Usage: setvol.sh {lower/higher}"
    ;;
esac

What does it do? It runs setvolume which prints the current volume like:

Current volume: -32 (min = -60, max = 18)

I cut the actual volume (-32 in this case) with cut and store it in a variable. Then i check for the command line arguments. If the passed argument contains the string “lower” it substracts one from the current volume and sets it. If the args contains the “upper” string, it will increase the volume with one. If no arguments are passed it will show a little help.

Don’t we have to check for min/max volume? No, the setvolume command does it for us.

Now we can save this file as setvol.sh in /boot/home/config/non-packaged/bin folder and create 2 shortcuts in the Shortcuts pref-panel like:

Option + PgDn : /boot/home/config/non-packaged/bin/setvol.sh lower
Option + PgUp : /boot/home/config/non-packaged/bin/setvol.sh upper

While this folder happened to be in the standard $PATH we still have to provide full path in the Shortcuts preflet.

Save and Apply and you are good to go. Of course you can use other shortcut key-combo too. Be careful not overide some standard shortcuts. Note: you cannot use the Fn key in the Shortcuts preflet, as this scancode won’t get sent to the OS.