3 . /usr/pandora/scripts/op_paths.sh
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}')"
12 return 1 # 0 when debugging, 1 when not
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
17 #powerbuttonconfig=$(cat $(grep /etc/passwd -e $(ps u -C xfce4-session | tail -n1 | awk '{print $1}')| cut -f 6 -d ":")/.powerbuttonconfig)
19 if [ -e /tmp/powerstate ]; then
20 powerstate="$(cat /tmp/powerstate)"
25 debug && echo "powerstate=$powerstate"
28 hcistate=$(hciconfig hci0 | grep DOWN)
29 if [ $hcistate ]; then
30 echo "down" > /tmp/hcistate
32 echo "up" > /tmp/hcistate
35 wlstate=$(lsmod | grep -m1 wl1251)
36 if [ -z "$wlstate" ]; then
37 echo "down" > /tmp/wlstate
39 echo "up" > /tmp/wlstate
41 rmmod board_omap3pandora_wifi 2> /dev/null
42 rmmod wl1251_sdio wl1251
47 hcistate=$(cat /tmp/hcistate)
48 if [ "$hcistate" = "up" ]; then
49 hciconfig hci0 up pscan
51 wlstate=$(cat /tmp/wlstate)
52 if [ "$wlstate" = "up" ]; then
53 /etc/init.d/wl1251-init start
55 rm -f /tmp/hcistate /tmp/wlstate
59 echo 0 > /sys/class/graphics/fb0/blank
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
68 maxbright=$(cat $SYSFS_BACKLIGHT/max_brightness)
70 if [ -f /tmp/oldbright ]; then
71 oldbright=$(cat /tmp/oldbright)
73 if [ $oldbright -eq 0 ]; then
74 oldbright=$(cat /etc/pandora/conf/brightness.state)
76 if [ $oldbright -ge 3 ] && [ $oldbright -le $maxbright ]; then
77 /usr/pandora/scripts/op_bright.sh $oldbright
79 /usr/pandora/scripts/op_bright.sh $maxbright
84 brightness=$(cat $SYSFS_BACKLIGHT_BRIGHTNESS)
85 if [ $brightness -gt 0 ]; then
86 echo $brightness > /tmp/oldbright
88 kernel_major=`uname -r | cut -c 1`
89 if [ "$kernel_major" = "2" ]; then
90 echo 0 > $SYSFS_BACKLIGHT_BRIGHTNESS
93 echo 1 > /sys/class/graphics/fb0/blank
96 lowPowerOn(){ #switch from normal to lowpower mode
99 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
107 cat /proc/pandora/cpu_mhz_max > /tmp/oldspeed
108 /usr/pandora/scripts/op_cpuspeed.sh -n 125
111 lowPowerOff(){ # switch from lowpower to normal mode
112 oldspeed=$(cat /tmp/oldspeed)
113 /usr/pandora/scripts/op_cpuspeed.sh -n $oldspeed
119 pidlist=$(pstree -lpA | grep pnd_run.sh | sed -ne 's/.*(\([0-9]\+\))/\1/p')
124 echo $highpow > /sys/class/leds/pandora\:\:power/brightness #power LED bright
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 \
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
149 if ! [ -e /sys/power/state ]; then
150 # no kernel suspend support
154 current_now="$(cat /sys/class/power_supply/bq27500-0/current_now)"
156 if [ $current_now -gt 0 ]; then
158 #don't suspend while unit is charging
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
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"
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"
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
192 # FIXME: fix the kernel and get rid of this
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"
201 for mod in $modules; do
202 if echo $blacklist | grep -q "\<$mod\>"; then
203 restore_list="$restore_list $mod"
211 echo mem > /sys/power/state
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
220 # it has been reported that time sometimes drifts during suspend
221 # so we reload hwclock here (WizardStan)
226 echo $highpow > /sys/class/leds/pandora\:\:power/brightness
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.
237 echo $lowpow > /sys/class/leds/pandora\:\:power/brightness
239 if suspend_real; then
248 if [ "$powerstate" = "on" ]; then
250 echo "resume called unexpectedly" >&2
256 shutdown(){ # warns the user and shuts the pandora down
257 xfceuser=$(ps u -C xfce4-session | tail -n1 | awk '{print $1}')
260 for i in $(seq $time); do
261 precentage=$(echo $i $time | awk '{ printf("%f\n", $1/$2*100) }')
263 echo "# Shutdown in $(($time-$i))"
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
271 su -c 'DISPLAY=:0.0 zenity --error --text "Shutdown aborted!"' $xfceuser
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
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_
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
292 elif [[ "$2" == "lid" ]]; then
293 if [[ "$1" == 0 ]]; then # lid was opened
294 if [[ "$powerstate" == lid* ]]; then
297 (debug && echo "resume") || resume
301 (debug && echo "display_on") || display_on_with_checks
306 elif [[ "$1" == 1 ]]; then # lid was closed
307 if [[ "$powerstate" == "on" ]]; then
310 (debug && echo "shutdown") || shutdown
313 powerstate="lidlowpower"
314 (debug && echo "suspend") || suspend_
317 (debug && echo "display_off") || display_off
318 powerstate="liddisplayoff"
323 elif [[ "$2" == "screensaver" ]]; then
324 # warning: don't try to interact with X or do real suspend here -
325 # will cause various deadlocks
328 if [[ "$1" == 0 ]]; then # deactivate screensaver
329 display_on_with_checks
331 elif [[ "$1" == 1 ]]; then # activate screensaver
336 debug && echo "powerstate=$powerstate"
337 echo "$powerstate" > /tmp/powerstate
339 rm -f /tmp/op_power.lock