fixed check for running x
[pandora-libraries.git] / testdata / scripts / op_power.sh
1 #!/bin/bash
2 #actions done when the power button is pressed
3 #only argument is the time the button was pressed in  seconds
4
5 if [ "$1" -le "3" ]; then # button was pressed 1-3sec, "suspend"
6   if [ -e /tmp/powerstate ]; then 
7     powerstate=$(cat /tmp/powerstate)
8   else
9     powerstate=0
10   fi
11   if [ $powerstate -eq "1" ]; then
12     #in lowpower mode
13     echo 0 > /tmp/powerstate
14     oldbright=$(cat /tmp/oldbright)
15     maxbright=$(cat /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/max_brightness)
16     oldspeed=$(cat /tmp/oldspeed)
17     if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then 
18       /usr/pandora/scripts/op_bright.sh $oldbright 
19     else
20       /usr/pandora/scripts/op_bright.sh $maxbright
21     fi
22     if [ $oldspeed -gt 14 ] && [ $oldspeed -le 1000 ]; then 
23      echo $oldspeed > /proc/pandora/cpu_mhz_max 
24     else
25       echo 500 > /proc/pandora/cpu_mhz_max
26     fi
27     hciconfig hci0 up
28     /etc/init.d/wl1251-init start
29     pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
30     for PID in $pidlist
31     do
32       kill -18 $PID #send SIGCONT
33     done
34   else
35     #in normal mode
36     echo 1 > /tmp/powerstate
37     cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
38     cat /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/brightness > /tmp/oldbright
39     pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
40     for PID in $pidlist
41     do
42       kill -19 $PID #send SIGSTOP
43     done
44     hciconfig hci0 down
45     /etc/init.d/wl1251-init stop
46     echo 0 > /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/brightness
47     echo 14 > /proc/pandora/cpu_mhz_max
48   fi
49 elif [ "$1" -ge "4" ]; then #button was pressed 4 sec or longer, shutdown
50   xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
51   time=10
52   countdown () {
53     for i in $(seq $time); do
54       precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
55       echo $precentage
56       echo "# Shutdown in $i"
57       sleep 1
58     done
59   }
60   countdown  | su -c 'DISPLAY=:0.0  zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
61   if [ $? -eq 0 ]; then
62   shutdown -h now
63   else
64   su -c 'DISPLAY=:0.0  zenity --error --text "Shutdown aborted!"' $xfceuser
65   fi
66 fi
67