summaryrefslogtreecommitdiff
path: root/src/pm-powersave.in
blob: 752a6e60646bd11477afe5d3f454ec46e746d6d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/sh
# vim:noexpandtab
# Simple powersave script
#
# Copyright 2006 Red Hat, Inc.
#
# Based on work from:
#    Bill Nottingham <notting@redhat.com>
#    Peter Jones <pjones@redhat.com>
#    David Zeuthen <davidz@redhat.com>
#    Richard Hughes <richard@hughsie.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

export STASHNAME=pm-powersave
. "@PM-UTILS-LIBDIR@/pm-functions"

remove_powersave_lock() {
    release_lock "${STASHNAME}.lock"
}

help() {
    cat <<EOF
$0: Valid options are:
false or ac = Tune powersaving features for the currently selected AC profile.
true or battery = Tune powersaving features for the currently selected 
                  battery profile.
<policyname> = Immediatly switch to the passed powersave policy, but do not
               change the preferred policy for the current power state.
--help = get detailed help.
--(ac|battery)-policy <policy> or --(ac|battery)policy=<policy>
   set powersave for ac or battery policy to <policy>
--(ac|battery)-policy = show the current powersave policy
--list-policies = show the available powersave policies.

EOF
if [ "$1" = "--help" ]; then
    cat <<EOF
The rest of this help message displays the variables
that can be used to tune the powersave hooks.  
You can change these variables using pm-utils config files.
(see the pm-utils README for more information)

EOF
else
    echo "You can get more detailed information by running pm-powersave --help"
fi
}

lock_and_load() {
# take the powersave lock.
# ensure it gets released no matter how we exit.
    try_lock "${STASHNAME}.lock" || exit 1
    trap remove_powersave_lock 0
    mkdir -p "${STORAGEDIR}"
    rm -f "${INHIBIT}"
    
    load_hook_blacklist
    
    init_logfile "${PM_LOGFILE}"
}

set_policy_vars() {
    current_powersave_policy="$PM_UTILS_RUNDIR/$STASHNAME/current_$1_policy"
    unset powersave_polfile
    powersave_policy=default
    if [ -L "$current_powersave_policy" ]; then
        powersave_polfile="$(readlink -f "$current_powersave_policy")"
        [ -f "$powersave_polfile" ] && {
            powersave_policy="${powersave_polfile##*/}"
            powersave_policy="${powersave_policy%.policy}"
        }
    fi
}

show_policy() { echo "$powersave_policy"; }

find_policy() {
    local pol
    for pol in "$PM_UTILS_ETCDIR/$STASHNAME/$1.policy" \
        "$PM_UTILS_LIBDIR/$STASHNAME/$1.policy" ''; do
        [ "$pol" -a -f "$pol" ] || continue
    done
    [ "$pol" ] && echo "$pol"
}

maybe_set_policy() {
    local pol
    if [ "$1" = "default" ]; then
        rm -f "$current_powersave_policy"
        return
    elif pol="$(find_policy "$1")"; then
        ln -sf "$pol" "$current_powersave_policy"
        return
    else
        echo "Could not find policy $1, aborting."
        exit 1
    fi
}

load_policy() {
    local p
    if [ ! "$1" ]; then
        [ -L "$current_powersave_policy" ] && . "$current_powersave_policy"
        return 0
    elif p="$(find_policy "$1")"; then
        . "$p"
    else
        return 1
    fi  
}

process_policy() {
    set_policy_vars "$1"
    shift
    case $1 in
        --*=*) maybe_set_policy "${1#*=}";;
        --*) if [ "$2" ]; then
            maybe_set_policy "$2"
            else
            show_policy
            fi;;
    esac
}

list_policies() {
    (for pol in "$PM_UTILS_ETCDIR/$STASHNAME/"*".policy" \
        "$PM_UTILS_LIBDIR/$STASHNAME/"*".policy"; do
        [ -f "$pol" ] || continue
        pol="${pol##*/}"
        pol="${pol%.policy}"
        echo "$pol"
    done; echo default;) |sort |uniq
}

do_battery() {
    lock_and_load
    set_policy_vars battery
    load_policy
    run_hooks power true
}

do_ac() {
    lock_and_load
    set_policy_vars ac
    if [ "$powersave_policy" != "default" ]; then
        load_policy
        run_hooks power true
    else
        run_hooks power false
    fi
}

case $1 in
    true|battery) do_battery;; 
    false|ac) do_ac;;
    --help) help && run_hooks power help;;
    --ac-policy*) process_policy ac "$@";;
    --batt-policy*) process_policy battery "$@";;
    --list-policies) list_policies;;
    default) lock_and_load
        if on_ac_power; then
            run_hooks power false
        else
            run_hooks power true
        fi;;
    '') if load_policy "$1"; then
        lock_and_load
        run_hooks power true
        elif on_ac_power; then
        do_ac
        else
        do_battery
        fi;;
    *) help && exit 1;;
esac