op_power.sh: Fixed a bug which crashed the script when going to low-power mode while...
[openpandora.oe.git] / recipes / pandora-system / pandora-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 "2" ]; then # button was pressed 1-2sec, "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     #switch from lowpower to normal mode
13     oldspeed=$(cat /tmp/oldspeed)
14     /usr/pandora/scripts/op_cpuspeed.sh $oldspeed
15     echo 0 > /tmp/powerstate
16     oldbright=$(cat /tmp/oldbright)
17     maxbright=$(cat /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/max_brightness)
18     echo 0 > /sys/devices/platform/omapfb/graphics/fb0/blank
19     sleep 0.1s # looks cleaner, could flicker without
20     oldspeed=$(cat /tmp/oldspeed)
21     if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then 
22       /usr/pandora/scripts/op_bright.sh $oldbright 
23     else
24       /usr/pandora/scripts/op_bright.sh $maxbright
25     fi
26     hcistate=$(cat /tmp/hcistate)
27     if [ ! $hcistate ]; then
28       hciconfig hci0 up pscan
29     fi
30     wlstate=$(cat /tmp/wlstate)
31     if [ ! $wlstate ]; then
32       /etc/init.d/wl1251-init start
33     fi
34     pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
35     for PID in $pidlist
36     do
37       kill -18 $PID #send SIGCONT
38     done
39     echo 255 > /sys/class/leds/pandora\:\:power/brightness #power LED bright
40     rm /tmp/powerstate
41   else
42     #switch from normal to lowpower mode
43     echo 1 > /tmp/powerstate
44     cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
45     cat /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/brightness > /tmp/oldbright
46     pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
47     for PID in $pidlist
48     do
49       kill -19 $PID #send SIGSTOP
50     done
51     rm /tmp/hcistate
52     hcistate=$(hciconfig hci0 | grep DOWN)
53     if [ $hcistate ]; then
54         echo "down" > /tmp/hcistate
55     else
56         hciconfig hci0 down
57     fi
58     rm /tmp/wlstate
59     wlstate=$(lsmod | grep -m1 wl1251)
60     if [ ! $wlstate ]; then
61         echo "down" > /tmp/wlstate
62     else
63             ifconfig wlan0 down
64             rmmod board_omap3pandora_wifi wl1251_sdio wl1251
65     fi
66     echo 0 > /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/brightness
67     echo 1 > /sys/devices/platform/omapfb/graphics/fb0/blank
68     echo 16 > /sys/class/leds/pandora\:\:power/brightness #dim power LED
69     /usr/pandora/scripts/op_cpuspeed.sh 125
70   fi
71 elif [ "$1" -ge "3" ]; then #button was pressed 3 sec or longer, shutdown
72   xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
73   time=5
74   countdown () {
75     for i in $(seq $time); do
76       precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
77       echo $precentage
78       echo "# Shutdown in $(($time-$i))"
79       sleep 1
80     done
81   }
82   countdown  | su -c 'DISPLAY=:0.0  zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
83   if [ $? -eq 0 ]; then
84   shutdown -h now
85   else
86   su -c 'DISPLAY=:0.0  zenity --error --text "Shutdown aborted!"' $xfceuser
87   fi
88 fi
89