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