x220-acpi: Add acpi event scripts for Lenovo X220
[shutils.git] / lenovo-x220-linuxacpi / etc / acpi / actions / powerbtn.sh
1 #!/bin/sh
2 # /etc/acpi/powerbtn.sh
3 # Taken from Debian's 2.0.4-1 diff file. This version handles KDE4.
4 # Power Button event handler.
5 # Checks to see if gnome or KDE are already handling the power button.
6 # If not, initiates a plain shutdown.
7
8 # getXuser gets the X user belonging to the display in $displaynum.
9 # If you want the foreground X user, use getXconsole!
10 # Input:
11 # displaynum - X display number
12 # Output:
13 # XUSER - the name of the user
14 # XAUTHORITY - full pathname of the user's .Xauthority file
15 getXuser() {
16 user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
17 if [ x"$user" = x"" ]; then
18 startx=`pgrep -n startx`
19 if [ x"$startx" != x"" ]; then
20 user=`ps -o user --no-headers $startx`
21 fi
22 fi
23 if [ x"$user" != x"" ]; then
24 userhome=`getent passwd $user | cut -d: -f6`
25 export XAUTHORITY=$userhome/.Xauthority
26 else
27 export XAUTHORITY=""
28 fi
29 export XUSER=$user
30 }
31
32 # Gets the X display number for the active virtual terminal.
33 # Output:
34 # DISPLAY - the X display number
35 # See getXuser()'s output.
36 getXconsole() {
37 console=`fgconsole`;
38 displaynum=`ps t tty$console | sed -n -re 's,.*/X .*:([0-9]+).*,\1,p'`
39 if [ x"$displaynum" != x"" ]; then
40 export DISPLAY=":$displaynum"
41 getXuser
42 fi
43 }
44
45 # Skip if we are just in the middle of resuming.
46 test -f /var/lock/acpisleep && exit 0
47
48 # If the current X console user is running a power management daemon that
49 # handles suspend/resume requests, let them handle policy.
50
51 getXconsole
52
53 # A list of power management system process names.
54 PMS="gnome-power-manager kpowersave xfce4-power-manager"
55 PMS="$PMS guidance-power-manager.py dalston-power-applet"
56
57 # If one of those is running or any of several others,
58 if pidof x $PMS > /dev/null ||
59 ( test "$XUSER" != "" && pidof dcopserver > /dev/null && test -x /usr/bin/dcop && /usr/bin/dcop --user $XUSER kded kded loadedModules | grep -q klaptopdaemon) ||
60 ( test "$XUSER" != "" && test -x /usr/bin/qdbus && test -r /proc/$(pidof kded4)/environ && su - $XUSER -c "eval $(echo -n 'export '; cat /proc/$(pidof kded4)/environ |tr '\0' '\n'|grep DBUS_SESSION_BUS_ADDRESS); qdbus org.kde.kded" | grep -q powerdevil) ; then
61 # Get out as the power manager that is running will take care of things.
62 exit
63 fi
64
65 # No power managment system appears to be running. Just initiate a plain
66 # shutdown.
67 /sbin/shutdown -h now "Power button pressed"
68