#!/bin/bash

change_resolution() {
        if [ x"$XAUTHORITY" != x"" ]; then
                if [ x"`xrandr -q | grep $1[[:space:]]x[[:space:]]$2 | cut -b -1`" != x"*" ]; then
                        xrandr -d $DISPLAY -s $1x$2
                fi
        fi
}

change_monitor() {
        if [ x"$XAUTHORITY" != x"" ]; then
                su $XUSER -c "$ATICONFIG --enable-monitor=$1" &>/dev/null
        fi
}


echo "IBM ACPI event script running"

export DISPLAY=":0.0"
export XUSER="galambos"
export XAUTHORITY=/home/$XUSER/.Xauthority
export ATICONFIG=/opt/bin/aticonfig

case "$1" in
    

    #FnF1
    "ibm/hotkey HKEY 00000080 00001001")
	modprobe -r ipw3945
	modprobe ipw3945
     ;;


    #FnF2 
    "ibm/hotkey HKEY 00000080 00001002")
      su $XUSER -c "/usr/kde/3.5/bin/dcop kdesktop KScreensaverIface lock && xset dpms force off"
    ;;

    #FnF3 	 
    "ibm/hotkey HKEY 00000080 00001003")
      su $XUSER -c "/usr/kde/3.5/bin/dcop kdesktop KScreensaverIface save && xset dpms force off"
      
    ;;


    #FnF4 	 
    "ibm/hotkey HKEY 00000080 00001004")
    #suspend-ram
	su $XUSER -c "/usr/kde/3.5/bin/dcop kdesktop KScreensaverIface lock"
	echo -n mem > /sys/power/state
#	su $XUSER -c "xscreensaver-command -deactivate"
     ;;

    #FnF5 	 
    "ibm/hotkey HKEY 00000080 00001005")
    ;;


#FnF6 	 
    "ibm/hotkey HKEY 00000080 00001006")
    ;;


#FnF7 	 
    "ibm/hotkey HKEY 00000080 00001007")

    if [ x"$XAUTHORITY" != x"" ]; then

        CURRENT=`$ATICONFIG --query-monitor | grep Enabled | cut -b 21- | \
                        sed -e 's/lvds/LCD/g' -e 's/crt1/CRT/g' -e 's/tv/TV-Out/g' -e 's/,/ and/g'`

        CHOICE=`su $XUSER -c "zenity --width=600 --height=350 --window-icon=/usr/share/pixmaps/monitor.png \
                --title 'Video output switcher -- choose the output' \
                --text='Current setting: $CURRENT\n' \
                --list --radiolist --column '' --column 'video output' \
                true 'LCD' \
                false 'CRT' \
                false 'TV-Out' \
                false 'LCD and CRT' \
                false 'LCD and TV-Out' \
                false 'CRT and TV-Out'"`

    fi

    case $CHOICE in
        LCD)
                change_monitor lvds
                change_resolution 1400 1050
                ;;
        CRT)
                change_monitor crt1
                change_resolution 1280 1024
                ;;
        TV-Out)
                change_monitor tv
                change_resolution 1024 768
                ;;
        LCD\ and\ CRT)
                change_monitor lvds,crt1
                change_resolution 1024 768
                ;;
        LCD\ and\ TV-Out)
                change_monitor lvds,tv
                change_resolution 1024 768
                ;;
        CRT\ and\ TV-Out)
                change_monitor crt1,tv
                change_resolution 1024 768
                ;;
    esac

    ;;


#FnF8 	 
    "ibm/hotkey HKEY 00000080 00001008")
    ;;


#FnF9 	 
    "ibm/hotkey HKEY 00000080 00001009")
    ;;


#FnF10 	 
    "ibm/hotkey HKEY 00000080 0000100a")
    ;;


#FnF11 	 
    "ibm/hotkey HKEY 00000080 0000100b")
    ;;


    #FnF12 	 
    "ibm/hotkey HKEY 00000080 0000100c")
    # suspend-disk
	su $XUSER -c "/usr/kde/3.5/bin/dcop kdesktop KScreensaverIface lock"
	echo -n disk > /sys/power/state
    ;;


    #FnBackspace 	 
    "ibm/hotkey HKEY 00000080 0000100d")
    ;;


#FnIns 	 
    "ibm/hotkey HKEY 00000080 0000100e")
    ;;


#FnDel 	 
    "ibm/hotkey HKEY 00000080 0000100f")
    ;;


#FnPos1 	 
    "ibm/hotkey HKEY 00000080 00001010")
    ;;


#Power 	 
#    "button/power PWRF 00000080 xxxxxxxx")
#    ;;


#Display lid 	 
#    "button/lid LID 00000080 xxxxxxxx")
#    ;;


#Ultrabay eject 	 
    "ibm/bay MSTR 00000003 00000000")
    ;;


#Ultrabay inserted 	 
    "ibm/bay MSTR 00000001 00000000")
    ;;


#Dock eject 	 
    "ibm/dock GDCK 00000003 00000001")
    ;;


#Wireless switch 	 
    "ibm/hotkey HKEY 00000080 00007000")
    ;;

        # Fn+F4
#        "ibm/hotkey HKEY 00000080 00001004")
#                /usr/sbin/hibernate -F /etc/hibernate/hibernate.conf.ram
#                ;;
        # Fn+F6
#        "ibm/hotkey HKEY 00000080 00001006")
 #               /root/bin/toggle_wlan
  #              ;;
        # Fn+F8
   #     "ibm/hotkey HKEY 00000080 00001008")
    #            /root/bin/toggle_irda
     #           ;;
        # Fn+F3
#        "ibm/hotkey HKEY 00000080 00001003")
#
 #               ;;
        # log everything else to syslog
  #      *)
   #             /usr/bin/logger -i -t "IBM Special Button" $1
    #            ;;
esac
