omap3-pandora-kernel: add a few more USB TV card modules
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_power.sh
1 #!/bin/bash
2
3 ( test -e /tmp/op_power.lock && exit 2 ) || touch "/tmp/op_power.lock"
4
5 debug(){
6         return 1 # 0 when debugging, 1 when not
7 }
8
9 test -e $(grep /etc/passwd -e $(ps u -C xfce4-session | tail -n1 | awk '{print $1}')| cut -f 6 -d ":")/.lidconfig && lidconfig=$(cat $(grep /etc/passwd -e $(ps u -C xfce4-session | tail -n1 | awk '{print $1}')| cut -f 6 -d ":")/.lidconfig) # read lid conf. file if it exists
10
11 #powerbuttonconfig=$(cat $(grep /etc/passwd -e $(ps u -C xfce4-session | tail -n1 | awk '{print $1}')| cut -f 6 -d ":")/.powerbuttonconfig)
12
13 if [ -e /tmp/powerstate ]; then 
14         powerstate="$(cat /tmp/powerstate)"
15 else
16         powerstate="on"
17 fi
18
19 debug && echo "powerstate=$powerstate"
20
21 lowPowerOn(){ #switch from normal to lowpower mode
22         cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
23         cat /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/brightness > /tmp/oldbright
24         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
25         for PID in $pidlist
26         do
27                 kill -19 $PID #send SIGSTOP
28         done
29         rm /tmp/hcistate
30         hcistate=$(hciconfig hci0 | grep DOWN)
31         if [ $hcistate ]; then
32                 echo "down" > /tmp/hcistate
33         else
34                 hciconfig hci0 down
35         fi
36         rm /tmp/wlstate
37         wlstate=$(lsmod | grep -m1 wl1251)
38         if [ ! $wlstate ]; then
39                 echo "down" > /tmp/wlstate
40         else
41                 ifconfig wlan0 down
42                 rmmod board_omap3pandora_wifi wl1251_sdio wl1251
43         fi
44         echo 0 > /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/brightness
45         echo 1 > /sys/devices/platform/omapfb/graphics/fb0/blank
46         echo 16 > /sys/class/leds/pandora\:\:power/brightness #dim power LED
47         /usr/pandora/scripts/op_cpuspeed.sh 125
48 }
49
50 lowPowerOff(){ # switch from lowpower to normal mode
51         oldspeed=$(cat /tmp/oldspeed)
52         /usr/pandora/scripts/op_cpuspeed.sh $oldspeed
53         oldbright=$(cat /tmp/oldbright)
54         maxbright=$(cat /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/max_brightness)
55         echo 0 > /sys/devices/platform/omapfb/graphics/fb0/blank
56         sleep 0.1s # looks cleaner, could flicker without
57         oldspeed=$(cat /tmp/oldspeed)
58         if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then 
59                 /usr/pandora/scripts/op_bright.sh $oldbright 
60         else
61                 /usr/pandora/scripts/op_bright.sh $maxbright
62         fi
63         hcistate=$(cat /tmp/hcistate)
64         if [ ! $hcistate ]; then
65                 hciconfig hci0 up pscan
66         fi
67         wlstate=$(cat /tmp/wlstate)
68         if [ ! $wlstate ]; then
69                 /etc/init.d/wl1251-init start
70         fi
71         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
72         for PID in $pidlist
73         do
74                 kill -18 $PID #send SIGCONT
75         done
76         echo 255 > /sys/class/leds/pandora\:\:power/brightness #power LED bright
77 }
78
79 shutdown(){ # warns the user and shuts the pandora down
80         xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
81         time=5
82         countdown () {
83                 for i in $(seq $time); do
84                         precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
85                         echo $precentage
86                         echo "# Shutdown in $(($time-$i))"
87                         sleep 1
88                 done
89         }
90         countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
91         if [ $? -eq 0 ]; then
92         shutdown -h now
93         else
94         su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
95         fi
96 }
97
98 displayOn(){ # turns the display on
99         #echo 0 > /sys/devices/platform/omapfb/graphics/fb0/blank
100         #sleep 0.1s # looks cleaner, could flicker without
101         maxbright=$(cat /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/max_brightness)
102         oldbright=0
103         if [ -f /tmp/oldbright ]; then
104                 oldbright=$(cat /tmp/oldbright)
105         fi
106         if [ $oldbright -eq 0 ]; then
107                 oldbright=$(cat /etc/pandora/conf/brightness.state)
108         fi
109         if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then 
110                 /usr/pandora/scripts/op_bright.sh $oldbright 
111         else
112                 /usr/pandora/scripts/op_bright.sh $maxbright
113         fi
114 }
115
116 displayOff(){ # turns the display off
117         brightness=$(cat /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/brightness)
118         if [ $brightness -gt 0 ]; then
119                 echo $brightness > /tmp/oldbright
120         fi
121         echo 0 > /sys/devices/platform/twl4030-pwm0-bl/backlight/twl4030-pwm0-bl/brightness
122         #echo 1 > /sys/devices/platform/omapfb/graphics/fb0/blank
123 }
124
125 if [[ "$2" == "" ]]; then
126         if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
127                 if [[ "$powerstate" == "buttonlowpower" ]]; then
128                         (debug && echo "lowPowerOff") || lowPowerOff
129                         powerstate="on"
130                 elif [[ "$powerstate" == "on" ]]; then
131                         (debug && echo "lowPowerOn") || lowPowerOn
132                         powerstate="buttonlowpower"
133                 fi
134         elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
135                 if [[ "$powerstate" == "on" ]]; then
136                         (debug && echo "shutdown") || shutdown
137                 fi
138         fi
139 elif [[ "$2" == "lid" ]]; then
140         if [[ "$1" == 0 ]]; then # lid was opened
141                 if [[ "$powerstate" == lid* ]]; then
142                         case "$lidconfig" in
143                                 "lowpower")
144                                         (debug && echo "lowPowerOff") || lowPowerOff
145                                         powerstate="on"
146                                 ;;
147                                 *)
148                                         (debug && echo "displayOn") || displayOn
149                                         powerstate="on"
150                                 ;;
151                         esac
152                 fi
153         elif [[ "$1" == 1 ]]; then # lid was closed
154                 if [[ "$powerstate" == "on" ]]; then
155                         case "$lidconfig" in
156                                 "shutdown")
157                                         (debug && echo "shutdown") || shutdown
158                                 ;;
159                                 "lowpower")
160                                         (debug && echo "lowPowerOn") || lowPowerOn
161                                         powerstate="lidlowpower"
162                                 ;;
163                                 *)
164                                         (debug && echo "displayOff") || displayOff
165                                         powerstate="liddisplayoff"
166                                 ;;
167                         esac
168                 fi
169         fi
170  fi
171 debug && echo "powerstate=$powerstate"
172 echo "$powerstate" > /tmp/powerstate
173 fi
174