#!/bin/bash
# acpi_hotkeys_ASUS_L2400D
# Part of the powersave package. Stefan Seyfried 2004, 2005
# this is a quick hack and only intended as an example. Yes, one could do better.
# For a more sophisticated solution, look for Manfred Tremmels
# acpi_hotkeys_ASUS_M6842NW.
#
# needs "aumix" to change volume
#

PATH=/bin:/usr/bin # be paranoid, we're running as root.
export PATH

# look into the example_event_script for an explanation
. ${0%/*}/helper_functions # `dirname $0`/helper_functions

run_on_xserver() {
  # this is ugly.
  local DUMMY XUSER DISP
  read DUMMY XUSER DISP DUMMY < <(wttyhx -v)
  su $XUSER -c "DISPLAY=$DISP $1"
}

TYPE=$1
# we discard $2 which is the name of the current scheme.
set $3  # powersaved gives us "other '<current_scheme_name>' '<content of /proc/acpi/event>'"
        # so we must split $3 to get the contents of /proc/acpi/event.
EVENT=$1   # "hotkey"
ACPI=$2    # "HOTK"
WHAT=$3    # "00000052"
SERIAL=$4  # "0000001c"

# it is easier to deal with numerical values (for me :-)
declare -i VAL
VAL=0x$WHAT # hex -> decimal

if [ "$EVENT" != "hotkey" ]; then
  logger -t $MYNAME "non-hotkey-event: $TYPE $EVENT $ACPI $WHAT $SERIAL"
elif [ $VAL -gt 16 -a $VAL -lt 48 ]; then # brightness up/down
  : # do nothing
elif [ $VAL -eq 51 -o $VAL -eq 52 ]; then # LCD on/off
  : # do nothing
elif [ $VAL -ge 97 -a $VAL -le 99 ]; then # internal/external/both
  : # do nothing
elif [ $VAL -eq 50 ]; then # Fn-F10 -> mute
  mute        &            # from the aumix package
elif [ $VAL -eq 49 ]; then # Fn-F11 -> volume down
  aumix -w -3 &            # needs aumix, does "pcm"
elif [ $VAL -eq 48 ]; then # Fn-F12 -> volume up
  aumix -w +3
elif [ $VAL -eq 80 ]; then # first "silver key" right from power
  run_on_xserver "xmms --rew" &
  : # do something
elif [ $VAL -eq 81 ]; then # second "silver key" right from power
  run_on_xserver "xmms --fwd" &
  : # do something
elif [ $VAL -eq 82 ]; then # third "silver key" right from power
  aumix -w -3 &
  : # do something
elif [ $VAL -eq 83 ]; then # fourth "silver key" right from power
  aumix -w +3 &
  : # do something
elif [ $VAL -eq 64 ]; then # CD <<
  run_on_xserver "xmms --rew" &
  : # do something
elif [ $VAL -eq 65 ]; then # CD >>
  run_on_xserver "xmms --fwd" &
  : # do something
elif [ $VAL -eq 67 ]; then # CD stop
  run_on_xserver "xmms --stop" &
  : # do something
elif [ $VAL -eq 69 ]; then # CD play/pause
  run_on_xserver "xmms --play-pause" &
  : # do something
else
  logger -t $MYNAME "undefined hotkey: keycode '$VAL'. $TYPE $EVENT $ACPI $WHAT $SERIAL"
fi
#
# see example_event_script for an explanation.
$SCRIPT_RETURN $EV_ID 0 "$MYNAME finished"
EXIT 0
