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