#!/bin/bash
###########################################################################
#                                                                         #
#                       Thinkpad event script                             #
#                                                                         #
#          Copyright (C) 2005,2006,2007 SUSE Linux Products GmbH          #
#                                                                         #
# Author(s): Based on code by Stefan Seyfried                             #
#            Hotkey support by Alex Solovey, solovey@us.ibm.com           #
#            Enhancements by Holger Macht                                 #
#                                                                         #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the   #
# Free Software Foundation; either version 2 of the License, or (at you   #
# option) any later version.                                              #
#                                                                         #
# This program is distributed in the hope that it will be useful, but     #
# WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       #
# General Public License for more details.                                #
#                                                                         #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., #
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                  #
#                                                                         #
###########################################################################
#
# thinkpad_acpi_events - process ThinkPad specific ACPI events generated by
# thinkpad_acpi driver and log them to syslog
#
# Configuration changes required for the script to work:
#  - Issue echo 0xffff > /proc/acpi/ibm/hotkey to enable thinkpad_acpi event on FnF5
#
PATH=/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin  # be paranoid, we're running as root.

WTTYHX=/usr/lib/powersave/wttyhx

function DEBUG(){
    logger "thinkpad_acpi_events $1"
}

get_x_user(){
    [ -n "$X_USER" -a -n "$DISP" ] && return 0
    local DUMMY
    read DUMMY X_USER DISP DUMMY < <($WTTYHX -v)
    return 0
}

run_on_xserver() {
    get_x_user
    DEBUG "User $X_USER display $DISP $1 " INFO
    su $X_USER -c "DISPLAY=$DISP $1"
}

kde_running() {
    get_x_user
    su - $X_USER -c "DISPLAY=$DISP /opt/kde3/bin/dcopfind kdesktop >/dev/null 2>&1" && \
            KDE_RUNNING=true
}

HOTKEY=$1

DEBUG "Custom event script for ThinkPad thinkpad_acpi driver" INFO

set $HOTKEY
EVENT=$1   # "ibm/hotkey"
ACPI=$2    # "HOTK"
WHAT=$3    # "00000080"
SERIAL=$4  # "0000100c" Fn+F12

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

if [ "$EVENT" = "ibm/hotkey" ]; then
    ACTION="log event"
    if [ "$VAL" -eq 128 ]; then
        case $SER in
            4097)   HOTKEY="Fn+F1" ;;
            4098)   HOTKEY="Fn+F2"
		ACTION="lock screen"
		kde_running
		if [ -n "$KDE_RUNNING" ]; then
		    get_x_user
		    DEBUG "User $X_USER display $DISP" INFO
		    su $X_USER -c "DISPLAY=$DISP /opt/kde3/bin/dcop --all-users --all-sessions kdesktop KScreensaverIface lock"
		fi
		;;
            4099)   HOTKEY="Fn+F3"
                if [ -x /opt/thinkpad/pm/onscreen_pm.sh ] ; then
                    run_on_xserver "/opt/thinkpad/pm/onscreen_pm.sh start" &
                    ACTION="start onscreen_pm applet"
                else
                    run_on_xserver "xset dpms force off" &
                    ACTION="blank screen"
                fi
                ;;
            4100)   HOTKEY="Fn+F4"
                ;;
            4101)   HOTKEY="Fn+F5" # Bluetooth
                if [ -x /opt/thinkpad/ac/onscreen_ac.sh ] ; then
                    run_on_xserver "/opt/thinkpad/ac/onscreen_ac.sh start" &
                    ACTION="start onscreen_ac applet"
                elif grep -q "status.*disabled" /proc/acpi/ibm/bluetooth ; then
                    echo enable > /proc/acpi/ibm/bluetooth
                    ACTION="enable blooetooth"
                else
                    echo disable > /proc/acpi/ibm/bluetooth
                    ACTION="disable blooetooth"
                fi
                ;;
            4102)   HOTKEY="Fn+F6" ;;
            4103)   HOTKEY="Fn+F7"
                ACTION="swap monitors"
                echo video_switch > /proc/acpi/ibm/video
                ;;
            4104)   HOTKEY="Fn+F8" ;;
                # ACTION="expand screen"
                # echo expand_toggle > /proc/acpi/ibm/video
            4105)   HOTKEY="Fn+F9"
                # ACTION="undock"
		# handled by dockutils
                ;;
            4106)   HOTKEY="Fn+F10" ;;
            4107)   HOTKEY="Fn+F11" ;;
            4108)   HOTKEY="Fn+F12" ;;
            4109)   HOTKEY="Fn+Backspace" ;;
            4110)   HOTKEY="Fn+Insert" ;;
            4111)   HOTKEY="Fn+Delete" ;;
            4112)   HOTKEY="Fn+Home"
                    ACTION="brighter display" ;;
            *)      HOTKEY="Unidentified" ;;
        esac
    else
        HOTKEY="Unidentified"
    fi	
    DEBUG "$HOTKEY hotkey: keycode $VAL serial $SER. action: $ACTION " INFO
elif [ "$EVENT" = "ibm/bay" ]; then
    case $VAL in
        1)  HOTKEY="Eject lever inserted" ;;
        3)  HOTKEY="Eject Request" ;;
        *)  HOTKEY="Unidentified" ;;
    esac
    DEBUG "$HOTKEY UltraBay event: $EVENT " INFO
else
    DEBUG "Unidentified event: $EVENT $ACPI $WHAT $SERIAL" INFO
fi

exit 0
