. /usr/share/acpi-support/power-funcs

CheckUPowerPolicy() {
	if pidof upowerd > /dev/null; then
		return 0;
	else
		return 1;
	fi
}

# The (not very aptly named) function CheckPolicy checks if the current X
# console user is running a power management daemon that handles suspend/resume
# requests. This is used in various places to determine if we need to handle
# something ourselves or if we need to pass the info on to a power management
# daemon (e.g. through a fake key press).

CheckPolicy() {
	local PMS

	getXconsole
	PMS="/usr/bin/gnome-power-manager /usr/bin/kpowersave /usr/bin/xfce4-power-manager"
	PMS="$PMS /usr/bin/guidance-power-manager /usr/lib/dalston/dalston-power-applet"
	if pidof -x $PMS > /dev/null ||
	   (test "$XUSER" != "" && pidof dcopserver > /dev/null && test -x /usr/bin/dcop && /usr/bin/dcop --user "$XUSER" kded kded loadedModules | grep -q klaptopdaemon) ||
	   PowerDevilRunning ; then
		echo 0;
	else
		echo 1;
	fi
}

# Find a DBUS session bus, by looking at the given process' environment and
# then send a message to that bus. If multiple processes match the given name,
# the message is sent to all of the buses (and output is simply concatenated.
DBusSend() {
	# The process to get the DBUS session address from
	local PROC_NAME="$1"

	# Parameters for dbus-send
	local DEST="$2"
	local DBUS_PATH="$3"
	local METHOD="$4"

	local DBUS_SESS	
	getXuser
	if test "$XUSER" != "" && test -x /usr/bin/dbus-send; then
		serverpid=$(pgrep -n -u "$XUSER" "$PROC_NAME")
		if test "$serverpid" != ""; then
			DBUS_SESS=$(su - "$XUSER" -c "grep -a -z DBUS_SESSION_BUS_ADDRESS /proc/$serverpid/environ || :")
			if test "$DBUS_SESS" != "" && su - "$XUSER" -s /bin/sh -c "export \"$DBUS_SESS\"; $DBUS_SESS dbus-send --print-reply --dest='$DEST' '$DBUS_PATH' '$METHOD'"; then
				return 0
			fi
		fi
	fi
}

PowerDevilRunning() {
	DBusSend kded4 org.kde.kded /kded org.kde.kded.loadedModules | grep -q powerdevil
}
