[Pm-utils] pm-on-ac

Stefan Seyfried seife at suse.de
Fri Apr 28 09:49:02 PDT 2006


On Fri, Apr 28, 2006 at 05:04:05PM +0100, Richard Hughes wrote:
> In the spirit of making things simple, I'm suggesting porting
> on_ac_power (written in C) to bash, so the whole of pm-utils can be arch
> independent (no compiling).
> 
> The direct line-for-line conversion is attached, but please could
> someone review and correct my primitive sh.
> 
> Thanks.
> 
> Richard.

> #!/bin/sh
> 
> # Test if the computer is running on line power
> # Exit status:
> # - 0 (true)  System is on AC power
> # - 1 (false) System is not on AC power
> #
> # NOTE: Absence of batteries is taken to mean that the system is on ac power.
> 
> # Check for AC/DC/etc adapters
> devices=`hal-find-by-capability --capability ac_adapter`
> for device in $devices
> do
> 	present=`hal-get-property --udi $devices --key ac_adapter.present`
                                        ^^^^^^^^
$device (without 's')

Usually it does not matter, since you only have one AC adapter, but it is more
correct this way ;-)

> #If there are no ac_adapters, check for batteries.
> devices=`hal-find-by-capability --capability battery`
> if [ -n "$devices" ]; then
> 	exit 1
> fi

This does not find batteries, but battery slots (at least on my machine,
hp nx5000):
seife at strolchi:~> hal-find-by-capability --capability battery
/org/freedesktop/Hal/devices/acpi_C13C
/org/freedesktop/Hal/devices/acpi_C13B

but there is only one battery present now.
So i don't think this check for "--capability battery" is too useful here.
I might be overlooking something of course.

I have attached my version, without the battery check.
-- 
Stefan Seyfried                  \ "I didn't want to write for pay. I
QA / R&D Team Mobile Devices      \ wanted to be paid for what I write."
SUSE LINUX Products GmbH, Nürnberg \                    -- Leonard Cohen
-------------- next part --------------
#!/bin/sh

# Test if the computer is running on line power
# Exit status:
# - 0 (true)  System is on AC power
# - 1 (false) System is not on AC power
#
# NOTE: Absence of batteries is taken to mean that the system is on ac power.

# Check for AC/DC/etc adapters
devices=`hal-find-by-capability --capability ac_adapter`

RET=0
for device in $devices
do
	present=`hal-get-property --udi $device --key ac_adapter.present`
	if [ "$present" == "false" ]; then
		RET=1
	else
		# "$present" == true => on AC power, no need to look further
		RET=0
		break
	fi
done

exit $RET


More information about the Pm-utils mailing list