summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Lowther <victor.lowther@gmail.com>2010-06-18 11:00:05 -0500
committerVictor Lowther <victor.lowther@gmail.com>2010-06-25 23:28:22 -0500
commit0ca325b97e76ae6cead3cf8ebc6a3a70a9ab2559 (patch)
tree14da9adcc134ed9f7ea8ac1b85431682b96b4655
parentd936c6d4efba8945419ac26b604922467d0299ee (diff)
Add support for powersave policies.powersave-policies
A powersave policy is a pm-utils configuration file that the pm-powersave command treats specially. You can use it to define various levels of power savings available to a system by changing the environment variables the power.d hooks run with, and by blacklisting any hooks you do not want to use at any given level. pm-powersave will look in /etc/pm/powersave and /usr/lib/pm-utils/powersave in that order, and policy settings override any other variables or hook overrides set via other methods. If no policies are in use, pm-powersave considers itself to be in the default policy, and will fall back to the usual settings for the powersave hooks. You can set a powersave policy with pm-powersave --(ac|battery)-policy=<policy> or pm-powersave --(ac|battery)-policy <policy> Setting a policy in this fashion does not actually apply the policy. You can get the current powersave policy by running pm-powersave --(ac|battery)-policy You can list all the policies with pm-powersave --list-policies Policies should not know or care about the actual power source the system is using -- it is expected that some users will want to use the same policy no matter what power source is being used. The "default" policy is shorthand for the way pm-powersave will behave in the absence of a defined policy for either the ac or battery power states. When on AC power, it will set everything back to the kernel defaults. On battery power, it will tune things according to the powersave hook defaults. If you want to apply a powersave policy directly, just run pm-powersave <policyname> and the policy you passed will be immediatly applied without changing the preferred policy for the current power state.
-rw-r--r--src/pm-powersave.in126
1 files changed, 120 insertions, 6 deletions
diff --git a/src/pm-powersave.in b/src/pm-powersave.in
index 7399788..752a6e6 100644
--- a/src/pm-powersave.in
+++ b/src/pm-powersave.in
@@ -34,9 +34,16 @@ remove_powersave_lock() {
help() {
cat <<EOF
$0: Valid options are:
-false or ac = turn powersaving features off.
-true or battery = turn powersaving features on.
-help = get detailed help.
+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
@@ -65,10 +72,117 @@ lock_and_load() {
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) lock_and_load && run_hooks power true;;
- false|ac) lock_and_load && run_hooks power false;;
+ true|battery) do_battery;;
+ false|ac) do_ac;;
--help) help && run_hooks power help;;
- '') lock_and_load; on_ac_power && run_hooks power false || run_hooks power true;;
+ --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