pandora-scripts: try to fix currentuser/home mess
[openpandora.oe.git] / recipes / pandora-system / pandora-scripts / op_power.sh
1 #!/bin/bash
2
3 . /usr/pandora/scripts/op_common.sh
4 xfceuser=$(get_user)
5 xfceuser_home=$(get_user_home)
6
7 # XXX: better use lockfile (or something), but it's not in current firmware
8 test -e /tmp/op_power.lock && exit 2
9 touch /tmp/op_power.lock
10 highpow="$(cat /etc/pandora/conf/led.conf | grep HighPowerLED: | awk -F\: '{print $2}')"
11 lowpow="$(cat /etc/pandora/conf/led.conf | grep LowPowerLED: | awk -F\: '{print $2}')"
12
13 debug(){
14         return 1 # 0 when debugging, 1 when not
15 }
16
17 test -e $xfceuser_home/.lidconfig && lidconfig=$(cat $xfceuser_home/.lidconfig) # read lid conf. file if it exists
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         cmd="DISPLAY=:0.0 zenity --info --text \"$1\" --timeout 10"
142         su -c "$cmd" $xfceuser
143 }
144
145 suspend_real() {
146         delay=0
147
148         if ! [ -e /sys/power/state ]; then
149                 # no kernel suspend support
150                 return 1
151         fi
152
153         current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
154  
155         if [ $current_now -gt 0 ]; then 
156                 return 1 
157                 #don't suspend while unit is charging
158         fi
159
160         # can't suspend while SGX is in use due to bugs
161         # (prevents low power states and potential lockup)
162         if lsof -t /dev/pvrsrvkm > /dev/null; then
163                 return 1
164         fi
165
166         if ! grep -q 'mmc_core.removable=0' /proc/cmdline; then
167                 # must unmount cards because they will be "ejected" on suspend
168                 # (some filesystems may even deadlock if we don't do this due to bugs)
169                 mounts="$(grep "/dev/mmcblk" /proc/mounts | awk '{print $1}' | xargs echo)"
170                 for mnt in $mounts; do
171                         if ! umount $mnt; then
172                                 show_message "Could not unmount $mnt, using partial suspend only"
173                                 return 1
174                         fi
175                 done
176                 swaps="$(grep "/dev/mmcblk" /proc/swaps | awk '{print $1}' | xargs echo)"
177                 for swp in $swaps; do
178                         if ! swapoff $swp; then
179                                 show_message "Could not unmount $swp, using partial suspend only"
180                                 return 1
181                         fi
182                 done
183         else
184                 if [ ! -e /etc/pandora/suspend-warned ]; then
185                         show_message "Pandora will now suspend.\n\n\
186 Please do not remove SD cards while pandora is suspended, doing so will corrupt them."
187                         touch /etc/pandora/suspend-warned
188                 fi
189         fi
190
191         # FIXME: fix the kernel and get rid of this
192         suspend_net
193
194         # get rid of modules that prevent suspend due to bugs
195         modules="$(lsmod | awk '{print $1}' | xargs echo)"
196         blacklist="g_zero g_audio g_ether g_serial g_midi gadgetfs g_file_storage
197                 g_mass_storage g_printer g_cdc g_multi g_hid g_dbgp g_nokia g_webcam g_ncm g_acm_ms
198                 ehci_hcd bridgedriver"
199         restore_list=""
200         for mod in $modules; do
201                 if echo $blacklist | grep -q "\<$mod\>"; then
202                         restore_list="$restore_list $mod"
203                         rmmod $mod
204                         delay=1 # enough?
205                 fi
206         done
207
208         sleep $delay
209         sync
210         echo mem > /sys/power/state
211
212         # if we are here, either we already resumed or the suspend failed
213         if [ -n "$restore_list" ]; then
214                 for module in $restore_list; do
215                         modprobe $module
216                 done
217         fi
218
219         # it has been reported that time sometimes drifts during suspend
220         # so we reload hwclock here (WizardStan)
221         hwclock -u -s
222
223         display_on
224         resume_net
225         echo $highpow > /sys/class/leds/pandora\:\:power/brightness
226
227         # wait here a bit to prevent this script from running again (keep op_power.lock)
228         # in case user did resume using the power switch.
229         sleep 2
230
231         return 0
232 }
233
234 suspend_() {
235         # dim power LED
236         echo $lowpow > /sys/class/leds/pandora\:\:power/brightness
237
238         if suspend_real; then
239                 # resumed already
240                 powerstate="on"
241         else
242                 lowPowerOn
243         fi
244 }
245
246 resume() {
247         if [ "$powerstate" = "on" ]; then
248                 # nothing to do
249                 echo "resume called unexpectedly" >&2
250         else
251                 lowPowerOff
252         fi
253 }
254
255 shutdown(){ # warns the user and shuts the pandora down
256         time=5
257         countdown () {
258                 for i in $(seq $time); do
259                         precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
260                         echo $precentage
261                         echo "# Shutdown in $(($time-$i))"
262                         sleep 1
263                 done
264         }
265         countdown | su -c 'DISPLAY=:0.0 zenity --progress --auto-close --text "Shutdown in X" --title "Shutdown"' $xfceuser
266         if [ $? -eq 0 ]; then
267         /sbin/shutdown -h now
268         else
269         su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
270         fi
271 }
272
273 if [[ "$2" == "" ]]; then
274         if [[ "$1" -le 2 ]]; then # power button was pressed 1-2sec, "suspend"
275                 if [[ "$powerstate" == "buttonlowpower" ]]; then
276                         (debug && echo "resume") || resume
277                         powerstate="on"
278                 elif [[ "$powerstate" == "on" ]]; then
279                         powerstate="buttonlowpower"
280                         (debug && echo "suspend") || suspend_
281                 elif [[ "$powerstate" == "liddisplayoff" ]]; then
282                         powerstate="buttonlowpower"
283                         (debug && echo "suspend") || suspend_
284                 fi
285         elif [[ "$1" -ge 3 ]]; then # power button was pressed 3 sec or longer, shutdown
286                 if [[ "$powerstate" == "on" ]]; then
287                         (debug && echo "shutdown") || shutdown
288                 fi
289         fi
290 elif [[ "$2" == "lid" ]]; then
291         if [[ "$1" == 0 ]]; then # lid was opened
292                 if [[ "$powerstate" == lid* ]]; then
293                         case "$lidconfig" in
294                                 "lowpower")
295                                         (debug && echo "resume") || resume
296                                         powerstate="on"
297                                 ;;
298                                 *)
299                                         (debug && echo "display_on") || display_on_with_checks
300                                         powerstate="on"
301                                 ;;
302                         esac
303                 fi
304         elif [[ "$1" == 1 ]]; then # lid was closed
305                 if [[ "$powerstate" == "on" ]]; then
306                         case "$lidconfig" in
307                                 "shutdown")
308                                         (debug && echo "shutdown") || shutdown
309                                 ;;
310                                 "lowpower")
311                                         powerstate="lidlowpower"
312                                         (debug && echo "suspend") || suspend_
313                                 ;;
314                                 *)
315                                         (debug && echo "display_off") || display_off
316                                         powerstate="liddisplayoff"
317                                 ;;
318                         esac
319                 fi
320         fi
321 elif [[ "$2" == "screensaver" ]]; then
322         # warning: don't try to interact with X or do real suspend here -
323         # will cause various deadlocks
324         unset DISPLAY
325
326         if [[ "$1" == 0 ]]; then # deactivate screensaver
327                 display_on_with_checks
328                 powerstate="on"
329         elif [[ "$1" == 1 ]]; then # activate screensaver
330                 display_off
331         fi
332 fi
333
334 debug && echo "powerstate=$powerstate"
335 echo "$powerstate" > /tmp/powerstate
336
337 rm -f /tmp/op_power.lock