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