Hello,

I am sad to say that the patch relayed by Stefan Lippers-Hollman breaks things
for me. This is because my AC adapter has the name "ADP0". (This is a Toshiba
Satellite A100 series laptop running Debian Sid with Linux 2.6.25.15.) Here's a
demonstration:

=== 8< ===
$ cd /sys/class/power_supply/
$ ls
ADP0  BAT0
$ cd ADP0
$ ls
device  online  power  subsystem  type  uevent
$ cat type
Mains
$ cat online
1
$
=== >8 ===

As you can see, my power supply has the name "ADP0" which
does not start with "AC".

Now let's take a look at the relevant part of the patch relayed by
Stefan Lippers-Hollman:

=== 8< ===
+    if [ -d /sys/class/power_supply/ ]; then
+       for FN in /sys/class/power_supply/AC*; do
+           local online
+           if [ -r "${FN}/online" ]; then
+               online="$(cat ${FN}/online)"
+               [ "$online" = 1 ] && exit 0
+               [ "$online" = 0 ] && OFF_LINE_P=yes
=== >8 ===

As you can see, the patch assumes that the power supply names are always
prefixed with the string "AC". However, as I have shown above, this is not the
case with all laptops.

Hence, here's an improved patch which makes "on_ac_power check" whether each
power supply is an AC adapter by looking at its type:

=== 8< ===

--- a/src/on_ac_power.orig.debian       2008-08-09 14:44:48.000000000 +0300
+++ b/src/on_ac_power   2008-08-09 14:19:24.000000000 +0300
@@ -20,19 +20,34 @@
 # that have known states.  If any adapter is on-line, we return 0.  If
 # no adapters are on-line but one or more are off-line, we return 1.
 #
-if /sbin/acpi_available && [ -d /proc/acpi/ac_adapter ]; then
+if /sbin/acpi_available; then
     OFF_LINE_P=no
-    for FN in /proc/acpi/ac_adapter/*; do
-       if [ -d "${FN}" ]; then
-           if [ -r "${FN}/state" ]; then
-               grep --quiet on-line "${FN}/state" && exit 0
-               grep --quiet off-line "${FN}/state" && OFF_LINE_P=yes
-           elif [ -r "${FN}/status" ]; then
-               grep --quiet on-line "${FN}/status" && exit 0
-               grep --quiet off-line "${FN}/status" && OFF_LINE_P=yes
+    if [ -d /sys/class/power_supply/ ]; then
+        for FN in /sys/class/power_supply/*; do
+           if test -d "${FN}" && test -r "${FN}/type"; then
+               type="$(cat ${FN}/type)"
+               if test "x${type}" = "xMains"; then
+                   if [ -r "${FN}/online" ]; then
+                       online="$(cat ${FN}/online)"
+                       [ "$online" = 1 ] && exit 0
+                       [ "$online" = 0 ] && OFF_LINE_P=yes
+                   fi
+               fi
            fi
-       fi
-    done
+       done
+    elif [ -d /proc/acpi/ac_adapter ]; then
+       for FN in /proc/acpi/ac_adapter/*; do
+           if [ -d "${FN}" ]; then
+               if [ -r "${FN}/state" ]; then
+                   grep --quiet on-line "${FN}/state" && exit 0
+                   grep --quiet off-line "${FN}/state" && OFF_LINE_P=yes
+               elif [ -r "${FN}/status" ]; then
+                   grep --quiet on-line "${FN}/status" && exit 0
+                   grep --quiet off-line "${FN}/status" && OFF_LINE_P=yes
+               fi
+           fi
+       done
+    fi
     [ "${OFF_LINE_P}" = "yes" ] && exit 1
 fi


=== >8 ===

This patch also fixes an issue in the patch relayed by Stefan Lippers-Hollman
where the variable named "online" is declared as a "local" variable even though
it is not in a function.

Regards,

M. Vefa Bıçakcı

Note: I am attaching the patch as well. (Just in case my e-mail program messes
up the patch.)
--- a/src/on_ac_power.orig.debian	2008-08-09 14:44:48.000000000 +0300
+++ b/src/on_ac_power	2008-08-09 14:19:24.000000000 +0300
@@ -20,19 +20,34 @@
 # that have known states.  If any adapter is on-line, we return 0.  If
 # no adapters are on-line but one or more are off-line, we return 1.
 #
-if /sbin/acpi_available && [ -d /proc/acpi/ac_adapter ]; then
+if /sbin/acpi_available; then
     OFF_LINE_P=no
-    for FN in /proc/acpi/ac_adapter/*; do
-	if [ -d "${FN}" ]; then
-	    if [ -r "${FN}/state" ]; then
-		grep --quiet on-line "${FN}/state" && exit 0
-		grep --quiet off-line "${FN}/state" && OFF_LINE_P=yes
-	    elif [ -r "${FN}/status" ]; then
-		grep --quiet on-line "${FN}/status" && exit 0
-		grep --quiet off-line "${FN}/status" && OFF_LINE_P=yes
+    if [ -d /sys/class/power_supply/ ]; then
+        for FN in /sys/class/power_supply/*; do
+	    if test -d "${FN}" && test -r "${FN}/type"; then
+	        type="$(cat ${FN}/type)"
+	        if test "x${type}" = "xMains"; then
+	            if [ -r "${FN}/online" ]; then
+		        online="$(cat ${FN}/online)"
+		        [ "$online" = 1 ] && exit 0
+		        [ "$online" = 0 ] && OFF_LINE_P=yes
+		    fi
+		fi
 	    fi
-	fi
-    done
+	done
+    elif [ -d /proc/acpi/ac_adapter ]; then
+	for FN in /proc/acpi/ac_adapter/*; do
+	    if [ -d "${FN}" ]; then
+		if [ -r "${FN}/state" ]; then
+		    grep --quiet on-line "${FN}/state" && exit 0
+		    grep --quiet off-line "${FN}/state" && OFF_LINE_P=yes
+		elif [ -r "${FN}/status" ]; then
+		    grep --quiet on-line "${FN}/status" && exit 0
+		    grep --quiet off-line "${FN}/status" && OFF_LINE_P=yes
+		fi
+	    fi
+	done
+    fi
     [ "${OFF_LINE_P}" = "yes" ] && exit 1
 fi
 

Reply via email to