op_xfcemenu.sh: Changed to bsp's script (slightly modified) which works with the...
[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 highpow="$(cat /etc/pandora/conf/led.conf | grep HighPowerLED: | awk -F\: '{print $2}')"
9 lowpow="$(cat /etc/pandora/conf/led.conf | grep LowPowerLED: | awk -F\: '{print $2}')"
10
11 debug(){
12         return 1 # 0 when debugging, 1 when not
13 }
14
15 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
16
17 #powerbuttonconfig=$(cat $(grep /etc/passwd -e $(ps u -C xfce4-session | tail -n1 | awk '{print $1}')| cut -f 6 -d ":")/.powerbuttonconfig)
18
19 if [ -e /tmp/powerstate ]; then 
20         powerstate="$(cat /tmp/powerstate)"
21 else
22         powerstate="on"
23 fi
24
25 debug && echo "powerstate=$powerstate"
26
27 suspend_net() {
28         hcistate=$(hciconfig hci0 | grep DOWN)
29         if [ $hcistate ]; then
30                 echo "down" > /tmp/hcistate
31         else
32                 echo "up" > /tmp/hcistate
33                 hciconfig hci0 down
34         fi
35         wlstate=$(lsmod | grep -m1 wl1251)
36         if [ -z "$wlstate" ]; then
37                 echo "down" > /tmp/wlstate
38         else
39                 echo "up" > /tmp/wlstate
40                 ifconfig wlan0 down
41                 rmmod board_omap3pandora_wifi 2> /dev/null
42                 rmmod wl1251_sdio wl1251
43         fi
44 }
45
46 resume_net() {
47         hcistate=$(cat /tmp/hcistate)
48         if [ "$hcistate" = "up" ]; then
49                 hciconfig hci0 up pscan
50         fi
51         wlstate=$(cat /tmp/wlstate)
52         if [ "$wlstate" = "up" ]; then
53                 /etc/init.d/wl1251-init start
54         fi
55         rm -f /tmp/hcistate /tmp/wlstate
56 }
57
58 display_on() {
59         echo 0 > /sys/class/graphics/fb0/blank
60
61         # only bother restoring brightness if it's 0
62         # (old kernel or user messed it up somehow)
63         brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
64         if [ $brightness -gt 0 ]; then
65                 return 0
66         fi
67
68         maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
69         oldbright=0
70         if [ -f /tmp/oldbright ]; then
71                 oldbright=$(cat /tmp/oldbright)
72         fi
73         if [ $oldbright -eq 0 ]; then
74                 oldbright=$(cat /etc/pandora/conf/brightness.state)
75         fi
76         if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then 
77                 /usr/pandora/scripts/op_bright.sh $oldbright 
78         else
79                 /usr/pandora/scripts/op_bright.sh $maxbright
80         fi
81 }
82
83 display_off() {
84         brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
85         if [ $brightness -gt 0 ]; then
86                 echo $brightness > /tmp/oldbright
87         fi
88         kernel_major=`uname -r | cut -c 1`
89         if [ "$kernel_major" = "2" ]; then
90                 echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
91         fi
92
93         echo 1 > /sys/class/graphics/fb0/blank
94 }
95
96 lowPowerOn(){ #switch from normal to lowpower mode
97         display_off
98
99         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
100         for PID in $pidlist
101         do
102                 kill -STOP $PID
103         done
104
105         suspend_net
106
107         cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
108         /usr/pandora/scripts/op_cpuspeed.sh -n 125
109 }
110
111 lowPowerOff(){ # switch from lowpower to normal mode
112         oldspeed=$(cat /tmp/oldspeed)
113         /usr/pandora/scripts/op_cpuspeed.sh -n $oldspeed
114         rm -f /tmp/oldspeed
115
116         display_on
117         resume_net
118
119         pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
120         for PID in $pidlist
121         do
122                 kill -CONT $PID
123         done
124         echo $highpow > /sys/class/leds/pandora\:\:power/brightness #power LED bright
125 }
126
127 display_on_with_checks() {
128         # after turning on the display, we don't want lowpower state
129         # (which could be active because of some races)
130         if [ "$powerstate" = "buttonlowpower" -o "$powerstate" = "lidlowpower" -o \
131              -e /tmp/oldspeed ]
132         then
133                 lowPowerOff
134         else
135                 display_on
136         fi
137 }
138
139 show_message() {
140         # TODO: check if desktop is visible; maybe use layer3?
141         xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
142         cmd="DISPLAY=:0.0 zenity --info --text \"$1\" --timeout 10"
143         su -c "$cmd" $xfceuser
144 }
145
146 suspend_real() {
147         delay=0
148
149         if ! [ -e /sys/power/state ]; then
150                 # no kernel suspend support
151                 return 1
152         fi
153
154         current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
155  
156         if [ $current_now -gt 0 ]; then 
157                 return 1 
158                 #don't suspend while unit is charging
159         fi
160
161         # can't suspend while SGX is in use due to bugs
162         # (prevents low power states and potential lockup)
163         if lsof -t /dev/pvrsrvkm > /dev/null; then
164                 return 1
165         fi
166
167         if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
168                 # must unmount cards because they will be "ejected" on suspend
169                 # (some filesystems may even deadlock if we don't do this due to bugs)
170                 mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
171                 for mnt in $mounts; do
172                         if ! umount $mnt; then
173                                 show_message "Could not unmount $mnt, using partial suspend only"
174                                 return 1
175                         fi
176                 done
177                 swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
178                 for swp in $swaps; do
179                         if ! swapoff $swp; then
180                                 show_message "Could not unmount $swp, using partial suspend only"
181                                 return 1
182                         fi
183                 done
184         else
185                 if [ ! -e /etc/pandora/suspend-warned ]; then
186                         show_message "Pandora will now suspend.\n\n\
187 Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
188                         touch /etc/pandora/suspend-warned
189                 fi
190         fi
191
192         # FIXME: fix the kernel and get rid of this
193         suspend_net
194
195         # get rid of modules that prevent suspend due to bugs
196         modules="$(lsmod | awk '{print $1}' | xargs echo)"
197         blacklist="g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
198                 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms
199                 ehci_hcd bridgedriver"
200         restore_list=""
201         for mod in $modules; do
202                 if echo $blacklist | grep -q "\<$mod\>"; then
203                         restore_list="$restore_list $mod"
204                         rmmod $mod
205                         delay=1 # enough?
206                 fi
207         done
208
209         sleep $delay
210         sync
211         echo mem > /sys/power/state
212
213         # if we are here, either we already resumed or the suspend failed
214         if [ -n "$restore_list" ]; then
215                 for module in $restore_list; do
216                         modprobe $module
217                 done
218         fi
219
220         # it has been reported that time sometimes drifts during suspend
221         # so we reload hwclock here (WizardStan)
222         hwclock -u -s
223
224         display_on
225         resume_net
226         echo $highpow > /sys/class/leds/pandora\:\:power/brightness
227
228         # wait here a bit to prevent this script from running again (keep op_power.lock)
229         # in case user did resume using the power switch.
230         sleep 2
231
232         return 0
233 }
234
235 suspend_() {
236         # dim power LED
237         echo $lowpow > /sys/class/leds/pandora\:\:power/brightness
238
239         if suspend_real; then
240                 # resumed already
241                 powerstate="on"
242         else
243                 lowPowerOn
244         fi
245 }
246
247 resume() {
248         if [ "$powerstate" = "on" ]; then
249                 # nothing to do
250                 echo "resume called unexpectedly" >&2
251         else
252                 lowPowerOff
253         fi
254 }
255
256 shutdown(){ # warns the user and shuts the pandora down
257         xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
258         time=5
259         countdown () {
260                 for i in $(seq $time); do
261                         precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
262                         echo $precentage
263                         echo "# Shutdown in $(($time-$i))"
264                         sleep 1
265                 done
266         }
267         countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
268         if [ $? -eq 0 ]; then
269         /sbin/shutdown -h now
270         else
271         su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
272         fi
273 }
274
275 if [[ "$2" == "" ]]; then
276         if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
277                 if [[ "$powerstate" == "buttonlowpower" ]]; then
278                         (debug && echo "resume") || resume
279                         powerstate="on"
280                 elif [[ "$powerstate" == "on" ]]; then
281                         powerstate="buttonlowpower"
282                         (debug && echo "suspend") || suspend_
283                 elif [[ "$powerstate" == "liddisplayoff" ]]; then
284                         powerstate="buttonlowpower"
285                         (debug && echo "suspend") || suspend_
286                 fi
287         elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
288                 if [[ "$powerstate" == "on" ]]; then
289                         (debug && echo "shutdown") || shutdown
290                 fi
291         fi
292 elif [[ "$2" == "lid" ]]; then
293         if [[ "$1" == 0 ]]; then # lid was opened
294                 if [[ "$powerstate" == lid* ]]; then
295                         case "$lidconfig" in
296                                 "lowpower")
297                                         (debug && echo "resume") || resume
298                                         powerstate="on"
299                                 ;;
300                                 *)
301                                         (debug && echo "display_on") || display_on_with_checks
302                                         powerstate="on"
303                                 ;;
304                         esac
305                 fi
306         elif [[ "$1" == 1 ]]; then # lid was closed
307                 if [[ "$powerstate" == "on" ]]; then
308                         case "$lidconfig" in
309                                 "shutdown")
310                                         (debug && echo "shutdown") || shutdown
311                                 ;;
312                                 "lowpower")
313                                         powerstate="lidlowpower"
314                                         (debug && echo "suspend") || suspend_
315                                 ;;
316                                 *)
317                                         (debug && echo "display_off") || display_off
318                                         powerstate="liddisplayoff"
319                                 ;;
320                         esac
321                 fi
322         fi
323 elif [[ "$2" == "screensaver" ]]; then
324         # warning: don't try to interact with X or do real suspend here -
325         # will cause various deadlocks
326         unset DISPLAY
327
328         if [[ "$1" == 0 ]]; then # deactivate screensaver
329                 display_on_with_checks
330                 powerstate="on"
331         elif [[ "$1" == 1 ]]; then # activate screensaver
332                 display_off
333         fi
334 fi
335
336 debug && echo "powerstate=$powerstate"
337 echo "$powerstate" > /tmp/powerstate
338
339 rm -f /tmp/op_power.lock