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