#!/bin/bash
###########################################################################
#                                                                         #
#                         Powersave Daemon                                #
#                                                                         #
#          Copyright (C) 2005,2006 SUSE Linux Products GmbH               #
#                                                                         #
# Author(s): Based on code by Stefan Seyfried                             #
#            Hotkey support by Alex Solovey, solovey@us.ibm.com           #
#            Enhancements (docking station support) 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
# ibm_acpi driver and log them to syslog
#
# Configuration changes required for the script to work with powersave package:
# 1) Set EVENT_OTHER="thinkpad_acpi_events" in /etc/sysconfig/powersave/events
# 2) Place this script into /usr/lib/powersave/scripts directory
# 3) Issue echo 0xffff > /proc/acpi/ibm/hotkey to enable ibm_acpi event on FnF5
#
PATH=/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin  # be paranoid, we're running as root.

# First, we pull in the helper functions.
. ${0%/*}/helper_functions # `dirname $0`/helper_functions
# get_x_user comes from here...
. ${0%/*}/x_helper_functions # `dirname $0`/x_helper_functions
export PATH

ME=${0##*/} # basename $0

# argument $4 is set to $EV_ID in helper_functions which is included above
if [ -z "$EV_ID" ]; then
    DEBUG "$ME 'Sorry, not enough arguments: $4 is empty.'" WARN
    $SCRIPT_RETURN $EV_ID 1 "$ME finished unsuccessful."
    exit 1
fi

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

HOTKEY=$3

TYPE=$1

DEBUG "Custom event script for ThinkPad ibm_acpi driver" INFO

# we discard $2 which is the name of the current scheme.
set $HOTKEY  # 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   # "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" ;;
            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"
                ACTION="suspend-to-ram"
                . $SYSCONF_DIR/sleep
                [ "$DISABLE_USER_SUSPEND2RAM" != "yes" ] && powersave "--$ACTION"
                ;;
            4101)   HOTKEY="Fn+F5" # Wireles LAN
                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"
                echo undock > /proc/acpi/ibm/dock
                ;;
            4106)   HOTKEY="Fn+F10" ;;
            4107)   HOTKEY="Fn+F11" ;;
            4108)   HOTKEY="Fn+F12"
                ACTION="suspend-to-disk"
                . $SYSCONF_DIR/sleep
                [ "$DISABLE_USER_SUSPEND2DISK" != "yes" ] && powersave "--$ACTION"
                ;;
            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
elif [ "$EVENT" = "ibm/dock" ]; then
    case $VAL in
        0)  if [ $SER -eq 3 ]; then	# X32 has strange dock code
                HOTKEY="Dock requested"
                ACTION="Docking"
                echo dock > /proc/acpi/ibm/dock 2>&1
            fi
            ;;
        1)  HOTKEY="Dock requested"
            ACTION="Docking"
            echo dock > /proc/acpi/ibm/dock 2>&1
            ;;
        3)  HOTKEY="Undock requested"
            ACTION="Undocking"
            echo undock > /proc/acpi/ibm/dock 2>&1
            ;;
    esac
    DEBUG "$HOTKEY: keycode $VAL serial $SER. action: $ACTION " INFO
else
    DEBUG "Unidentified event: $EVENT $ACPI $WHAT $SERIAL" INFO
fi

$SCRIPT_RETURN $EV_ID 0 "$ME finished"
EXIT 0
