On Feb 2, 2008 4:32 PM, Victor Lowther <[EMAIL PROTECTED]> wrote:
> Hmmm... how about something along these lines:
>
> @PM-UTILS-LIBDIR@/methods.d/, which is populated by files named
>   suspend_method-suspend_type (kernel-suspend, kernel-hibernate,
> tuxonice-suspend-hybrid, etc.)
>   These files accept a single argument, which (right now) can be
> either "inhibited" or "", and output either of "resume" or "thaw" on
> stdout, depending on whether the system is resuming from suspend or
> hibernate.
>
> All the do_suspend_type functions then collapse into a single
> do_sleep() function, which is passed either "hibernate", "suspend", or
> "suspend-hybrid".  The do_sleep method then calls
> @PM-UTILS-LIBDIR@/method.d/${SUSPEND_METHOD}-$1 with the appropriate
> value of inhibit
>
> That way, there is no patching of case statements needed to add a new
> suspend method -- just drop an appropriatly-named script in the
> method.d directory, and let it handle doing The Right Thing.

Test patch attached.  It may not actually run, and there are a few
niceties to add, but it is a first stab at things.  Should apply
cleanly to current git head, and is also available as
vlowther-pluggable-sleep-methods in my git repo.
diff --git a/pm/Makefile.am b/pm/Makefile.am
index a063dbb..486c713 100644
--- a/pm/Makefile.am
+++ b/pm/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS =  sleep.d power.d
+SUBDIRS =  sleep.d power.d method.d
 
 extradir = $(libdir)/pm-utils
 
diff --git a/pm/functions b/pm/functions
index 76aafd1..a0f6515 100755
--- a/pm/functions
+++ b/pm/functions
@@ -18,6 +18,7 @@ SUSPEND_MODULES=""
 TEMPORARY_CPUFREQ_GOVERNOR="performance"
 LOCK="/var/run/pm-utils/lock"
 STORAGEDIR="/var/run/pm-utils/storage"
+SUSPEND_METHOD="kernel"
 
 # Use c sort order
 export LC_COLLATE=C
@@ -124,7 +125,8 @@ run_hooks() {
 	# $2 = parameters to pass to hooks
 	# $3 = if $3 = "reverse", then also run the hooks that ran sucessfully
 	#      backwards with parameters in $4
-        # $4 = parameters to pass to scripts when running backwards	
+        # $4 = Function (with optional arguments) to run in between running
+	# forwards and backwards.
 	echo "$(date): running $1 hooks."
 	local hooks="$1"
 	local params="$2"
@@ -135,14 +137,16 @@ run_hooks() {
 		echo "===== $(date): running hook: $file $params ====="
 		"$file" $params && revhooks="${file}${revhooks:+" ${revhooks}"}"
 	done
-	echo "$(date): done running $hooks:$params hooks."
+	echo "$(date): done running $hooks hooks."
 	if [ "x$1" = "xreverse" ]; then
+		# the lack of quotes around $4 is quite deliberate
+		local revparam="$($4)"
 		echo "$(date): running $hooks hooks backwards."
 		for file in $revhooks; do
-			echo "===== $(date): running hook :$file $2 ====="
-			"${file}" $2
+			echo "===== $(date): running hook :$file $revparam ====="
+			"${file}" $revparam
 		done
-		echo "$(date): done running $hooks:$2 backwards"
+		echo "$(date): done running $hooks backwards"
 	fi
 }
 
@@ -165,20 +169,22 @@ get_power_status()
 	return $RETVAL
 }
 
-do_suspend()
-{
-	pm-pmu --suspend || echo -n "mem" > /sys/power/state
-}
-
-do_hibernate()
-{
-	echo -n "${HIBERNATE_MODE}" > /sys/power/disk
-	echo -n "disk" > /sys/power/state
+# check to make sure we can actually do the requested type of sleep.
+check_sleep() {
+	# $1 = sleep type
+	local hook="/usr/lib/pm-utils/method.d/${SUSPEND_METHOD}-$1"
+	[ -O "${hook}" -a -x "${hook}" ] && return 0 || return 1
 }
 
-do_suspend_hybrid()
-{
-	return 1
+do_sleep() {
+	# $1 = type of sleep to perform.
+	# Currently supported are suspend, hibernate, and suspend-hybrid
+	local hook="/usr/lib/pm-utils/method.d/${SUSPEND_METHOD}-$1"
+	if [ -e "${INHIBIT}" ]; then
+		"${hook}" inhibit
+	else
+		"${hook}"
+	fi
 }
 
 init_logfile() {
@@ -194,10 +200,11 @@ pm_main()
 
 	# make sure that our locks are unlocked no matter how the script exits
 	trap remove_suspend_lock 0
+	check_sleep "$1" || exit 1
 
 	rm -f "$INHIBIT"
 
-	run_hooks sleep "$1" reverse "$2"
+	run_hooks sleep "$1" reverse "do_sleep $1"
 
 	return 0
 }
diff --git a/pm/method.d/Makefile.am b/pm/method.d/Makefile.am
new file mode 100644
index 0000000..e9b3793
--- /dev/null
+++ b/pm/method.d/Makefile.am
@@ -0,0 +1,10 @@
+method_dir = $(libdir)/pm-utils/config.d
+
+method_DATA = kernel-suspend		\
+	      kernel-hibernate		\
+	      kernel-suspend-hybrid
+
+EXTRA_DIST=$(config_DATA)
+
+clean-local :
+	rm -f *~
diff --git a/pm/method.d/kernel-hibernate b/pm/method.d/kernel-hibernate
new file mode 100755
index 0000000..a123138
--- /dev/null
+++ b/pm/method.d/kernel-hibernate
@@ -0,0 +1,7 @@
+#!/bin/sh
+if [ "$1" = "inhibit" ]; then :;
+else
+	echo -n "${HIBERNATE_MODE}" >/sys/power/disk
+	echo -n "disk" >/sys/power/state
+fi
+echo thaw
diff --git a/pm/method.d/kernel-suspend b/pm/method.d/kernel-suspend
new file mode 100755
index 0000000..842759e
--- /dev/null
+++ b/pm/method.d/kernel-suspend
@@ -0,0 +1,6 @@
+#!/bin/sh
+if [ "$1" = "inhibit" ]; then :;
+else
+	echo -n "mem" > /sys/power/state
+fi
+echo resume
diff --git a/pm/method.d/kernel-suspend-hybrid b/pm/method.d/kernel-suspend-hybrid
new file mode 100755
index 0000000..9b1a16d
--- /dev/null
+++ b/pm/method.d/kernel-suspend-hybrid
@@ -0,0 +1,2 @@
+#!/bin/sh
+echo resume
diff --git a/pm/sleep.d/zzz b/pm/sleep.d/zzz
deleted file mode 100755
index a7ae472..0000000
--- a/pm/sleep.d/zzz
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/bin/sh
-. /usr/lib/pm-utils/functions
-[ -e "${INHIBIT}" ] && return 1
-sync;sync;sync;
-case $1 in
-	suspend|hibernate|suspend_hybrid) "do_$1" ;;
-	resume|thaw) exit 0 ;;
-	*) exit 1 ;;
-esac
-exit $?
diff --git a/src/pm-action b/src/pm-action
index 3a1e6a5..4e28301 100755
--- a/src/pm-action
+++ b/src/pm-action
@@ -120,6 +120,6 @@ esac
 
 export PM_CMDLINE="$@"
 
-pm_main "$(echo $ACTION | tr - _)" "$REVERSE"
+pm_main "$ACTION" "$REVERSE"
 
 exit $?
_______________________________________________
Pm-utils mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/pm-utils

Reply via email to