Debian Codegraph @

powerd-config

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #!/bin/bash
2 #
3 # primitive configurator for powerd.
4 #
5 # can be used from the commandline to activate a profile,
6 # otherwise, will bring up a series of dialog-based config
7 # screens to let you select and edit a profile.
8 #
9 #
10 # Copyright (C) 2009, Paul G Fox
11 #
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License as
14 # published by the Free Software Foundation; either version 2 of
15 # the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be
18 # useful, but WITHOUT ANY WARRANTY; without even the implied
19 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 # PURPOSE. See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public
23 # License along with this program; if not, write to the Free
24 # Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
25 # USA.
26 #
27
28
29 INHIBITDIR=/var/run/powerd-inhibit-suspend
30 INHIBIT_BY_TOUCH=$INHIBITDIR/.fake_activity
31 CONFIGDIR=/etc/powerd
32 CONFIGFILE=$CONFIGDIR/powerd.conf
33 TMPCONFIG=$CONFIGFILE.tmp
34 NEWCONFIG=$TMPCONFIG.new
35 EVENTFIFO=/var/run/powerevents
36
37 TMP=/tmp/powerdconfig.$$
38
39
40 #exec 2>pc.log
41 #set -x
42
43 me=${0##*/}
44
45 usage()
46 {
47 cat <<EOF >&2
48 usage: $me [ -a | =<eventname> | configname ]"
49 -a Will cause pseudo-activity, to keep the laptop awake"
50 =foo Will send event 'foo' to powerd"
51 configname If $CONFIGFILE is a symlink,"
52 specifying the name of a file in $CONFIGDIR will"
53 repoint the symlink and cause powerd to reconfigure itself."
54 Otherwise, giving no arguments will bring up a configuration"
55 editor on the current $CONFIGFILE"
56
57 EOF
58 exit 1
59 }
60
61
62 trap "rm -f $TMP $TMPCONFIG $NEWCONFIG" 0
63
64 backtitle="powerd configuration"
65
66
67 yes_no()
68 {
69 case $1 in
70 1|[yYtT]*) echo yes ;;
71 *) echo no ;;
72 esac
73 }
74
75 setvar()
76 {
77 varname=$1
78 val=$2
79
80 grep -v "^$varname=" $TMPCONFIG >$NEWCONFIG
81 echo $varname="\"$val\"" >>$NEWCONFIG
82 mv $NEWCONFIG $TMPCONFIG
83 }
84
85 edit_config()
86 {
87
88 cfg=$1
89
90 ro=;
91 test "$readonly" && ro=2
92
93 # suck in the config
94 . $cfg
95
96 bat_dim=${config_BATTERY_TIME_DIM:-120}
97 bat_sleep=${config_BATTERY_TIME_SLEEP:-240}
98 bat_blank=${config_BATTERY_TIME_BLANK:-480}
99
100 # force numeric, and reasonable defaults
101 test "$bat_dim" -eq "$bat_dim" || bat_dim=120
102 test "$bat_sleep" -eq "$bat_sleep" || bat_sleep=$(( bat_dim + 120 ))
103 test "$bat_blank" -eq "$bat_blank" || bat_blank=$(( bat_dim + 240 ))
104
105
106 ebook_dim=${config_EBOOK_TIME_DIM:-120}
107 ebook_sleep=${config_EBOOK_TIME_SLEEP:-240}
108 ebook_blank=${config_EBOOK_TIME_BLANK:-480}
109
110 # force numeric, and reasonable defaults
111 test "$ebook_dim" -eq "$ebook_dim" || ebook_dim=120
112 test "$ebook_sleep" -eq "$ebook_sleep" || ebook_sleep=$(( ebook_dim + 120 ))
113 test "$ebook_blank" -eq "$ebook_blank" || ebook_blank=$(( ebook_dim + 240 ))
114
115 plug_dim=${config_PLUGGED_TIME_DIM:-120}
116 plug_sleep=${config_PLUGGED_TIME_SLEEP:-240}
117 plug_blank=${config_PLUGGED_TIME_BLANK:-480}
118
119 # force numeric, and reasonable defaults
120 test "$plug_dim" -eq "$plug_dim" || plug_dim=120
121 test "$plug_sleep" -eq "$plug_sleep" || plug_sleep=$(( plug_dim + 120 ))
122 test "$plug_blank" -eq "$plug_blank" || plug_blank=$(( plug_dim + 240 ))
123
124 idledim=${config_IDLE_DIM_LEVEL:-5}
125 test "$idledim" -eq "$idledim" || idledim=5
126
127 wlanpower=${config_MESH_DURING_SUSPEND:-no}
128 wlanwake=${config_WAKE_ON_WLAN:-yes}
129 maxsleep=${config_MAX_SLEEP_BEFORE_SHUTDOWN:-3600}
130 test "$maxsleep" -eq "$maxsleep" || maxsleep=5
131 allowshut=${config_ALLOW_SHUTDOWN_WHEN_PLUGGED:-yes}
132 lidsleep=${config_SLEEP_WHEN_LID_CLOSED:-yes}
133
134 splashdelay=${config_CONFIRM_SECONDS:-8}
135
136
137 # Careful! adding fields means adjusting numbers in many places
138 # below the new field (at least two per line), and _also_ means
139 # the results need to be assigned differently after return. see below.
140 row=1
141 if ! dialog \
142 --backtitle "$backtitle" --mixedform \
143 "${editing:-}Dim, Sleep and Blank may be scheduled in any order." \
144 22 67 16 \
145 \
146 "Battery: Dim" 1 1 "$bat_dim" 1 17 9 9 "$ro" \
147 "Sleep" 1 28 "$bat_sleep" 1 34 9 9 "$ro" \
148 "Blank" 1 45 "$bat_blank" 1 51 9 9 "$ro" \
149 \
150 "Ebook: Dim" 2 1 "$ebook_dim" 2 17 9 9 "$ro" \
151 "Sleep" 2 28 "$ebook_sleep" 2 34 9 9 "$ro" \
152 "Blank" 2 45 "$ebook_blank" 2 51 9 9 "$ro" \
153 \
154 "Plugged: Dim" 3 1 "$plug_dim" 3 17 9 9 "$ro" \
155 "Sleep" 3 28 "$plug_sleep" 3 34 9 9 "$ro" \
156 "Blank" 3 45 "$plug_blank" 3 51 9 9 "$ro" \
157 \
158 "Screen brightness (0-15) when dimmed:" 5 1 "$idledim" 5 43 3 3 "$ro" \
159 \
160 "Keep wlan powered in suspend? (XO-1)" 7 1 "$wlanpower" 7 41 5 5 "$ro" \
161 "Wake on wireless activity?" 8 1 "$wlanwake" 8 41 5 5 "$ro" \
162 \
163 "Max blanked sleep before shutdown:" 10 1 "$maxsleep" 10 37 9 9 "$ro" \
164 "Allow shutdown when plugged in?" 11 1 "$allowshut" 11 41 5 5 "$ro" \
165 "Always sleep when lid closed?" 12 1 "$lidsleep" 12 41 5 5 "$ro" \
166 \
167 "Power button splash screen timeout:" 14 1 "$splashdelay" 14 43 3 3 "$ro" \
168 2>$TMP
169 then
170 return 1
171 fi
172
173 if [ ! "$readonly" ]
174 then
175 # assign results to to the right variables here:
176 set -- $(cat $TMP)
177 bat_dim=$1
178 bat_sleep=$2
179 bat_blank=$3
180 ebook_dim=$4
181 ebook_sleep=$5
182 ebook_blank=$6
183 plug_dim=$7
184 plug_sleep=$8
185 plug_blank=$9
186 idledim=${10}
187 wlanpower=${11}
188 wlanwake=${12}
189 maxsleep=${13}
190 allowshut=${14}
191 lidsleep=${15}
192 splashdelay=${16}
193
194 setvar config_BATTERY_TIME_DIM $bat_dim
195 setvar config_BATTERY_TIME_SLEEP $bat_sleep
196 setvar config_BATTERY_TIME_BLANK $bat_blank
197 setvar config_EBOOK_TIME_DIM $ebook_dim
198 setvar config_EBOOK_TIME_SLEEP $ebook_sleep
199 setvar config_EBOOK_TIME_BLANK $ebook_blank
200 setvar config_PLUGGED_TIME_DIM $plug_dim
201 setvar config_PLUGGED_TIME_SLEEP $plug_sleep
202 setvar config_PLUGGED_TIME_BLANK $plug_blank
203
204 setvar config_IDLE_DIM_LEVEL $idledim
205 setvar config_MESH_DURING_SUSPEND $(yes_no $wlanpower)
206 setvar config_WAKE_ON_WLAN $(yes_no $wlanwake)
207 setvar config_MAX_SLEEP_BEFORE_SHUTDOWN $maxsleep
208 setvar config_ALLOW_SHUTDOWN_WHEN_PLUGGED $(yes_no $allowshut)
209 setvar config_SLEEP_WHEN_LID_CLOSED $(yes_no $lidsleep)
210 setvar config_CONFIRM_SECONDS $splashdelay
211 fi
212
213 return 0
214
215 }
216
217 activate_config()
218 {
219 echo reconfig >$EVENTFIFO
220 }
221
222 newconfig()
223 {
224 new="$1"
225
226 if [ ! -L $CONFIGFILE ]
227 then
228 echo "$me: $CONFIGFILE isn't a symlink." >&2
229 echo " configuration switching unavailable." >&2
230 exit 1
231 fi
232
233 # make sure it points to a file in $CONFIGDIR, otherwise this
234 # program can't restore it. maybe overkill.
235 l=$(readlink $CONFIGFILE)
236 case $l in
237 */*)
238 echo "$me: $CONFIGFILE must be a local symlink." >&2
239 echo " (currently points to '$l')" >&2
240 exit 1
241 ;;
242 esac
243
244 if [ ! -s $CONFIGDIR/$new ]
245 then
246 echo "$me: $CONFIGDIR/$new doesn't exist or is empty." >&2
247 exit 1
248 fi
249
250 ln -sf $new $CONFIGFILE && activate_config
251 }
252
253 o_check_root()
254 {
255 if [ "$(whoami)" != root ]
256 then
257 dialog \
258 --backtitle "$backtitle" \
259 --msgbox \
260 "You must be root to run this configuration program" 0 0
261 exit 1
262 fi
263 }
264
265 check_root()
266 {
267 am_root=;
268 test "$(whoami)" = root && am_root=yes
269 }
270
271 main()
272 {
273
274 set -u
275
276 arg1="${1:-}"
277
278 case $arg1 in
279 -a) # force pseudo-activity by touching the magic file
280 if [ -d $INHIBITDIR ]
281 then
282 touch $INHIBIT_BY_TOUCH
283 else
284 echo "$me: no powerd running?" >&2
285 fi
286 exit
287 ;;
288 -*) # we have no other -options
289 usage
290 ;;
291 esac
292
293 check_root
294
295 if [ "$arg1" -a ! "$am_root" ]
296 then
297 echo "You must be root to control powerd" >&2
298 exit 1
299 fi
300
301 case $arg1 in
302 "")
303 ;;
304 =*) # =foo is just a direct event to the daemon,
305 # with the '=' removed.
306 if [ ! -p $EVENTFIFO ]
307 then
308 echo $me: problem with $EVENTFIFO >&2
309 exit 1
310 fi
311 echo ${arg1#=} >$EVENTFIFO
312 exit
313 ;;
314 *)
315 newconfig $1
316 exit
317 ;;
318 esac
319
320 if ! type dialog >/dev/null 2>&1
321 then
322 (
323 echo "${0##*/}: the 'dialog' program isn't available, quitting."
324 echo " Use a text editor to edit profiles under $CONFIGDIR,"
325 echo " and (re)activate them with: '${0##*/} profilename'"
326 echo " (Or use 'sudo yum install dialog' to install the package.)"
327 ) >&2
328 exit 1
329 fi
330
331 descr=;
332 if [ -L $CONFIGFILE ]
333 then
334 cfg_file=$(readlink -e $CONFIGFILE 2>/dev/null)
335 test -r "$cfg_file"
336 r=$?
337 descr=" linked profile"
338 elif [ -s $CONFIGFILE ]
339 then
340 cfg_file=$CONFIGFILE
341 r=0
342 else # no config file -- (try to) create one
343 > $CONFIGFILE
344 cfg_file=$CONFIGFILE
345 descr=" new"
346 r=$?
347 fi
348
349 if [ $r != 0 ]
350 then
351 echo $me: problem with config file >&2
352 exit 1
353 fi
354
355 readonly=;
356 if [ ! "$am_root" ]
357 then
358 test -w "$cfg_file" || readonly=yes
359 test -w $(dirname "$cfg_file") || readonly=yes
360 fi
361
362 if [ $readonly ]
363 then
364 editing="Showing read-only contents of$descr '$cfg_file'.\n"
365 edit_config $CONFIGFILE
366 else
367 editing="Editing$descr '$cfg_file'.\n"
368 cp "$cfg_file" $TMPCONFIG
369 if edit_config $TMPCONFIG
370 then
371 cp $CONFIGFILE $CONFIGFILE.bak
372 # don't disturb possible symlink
373 cat $TMPCONFIG >$CONFIGFILE
374 activate_config
375 else
376 rm $TMPCONFIG
377 echo No changes made.
378 fi
379 fi
380 }
381
382
383 main "$@"