[Pkg-wmaker-commits] [wmbattery] 32/241: * Improved acpi interface (also used for procmeter3 module now). * Fixed segfault on non-ACPI systems.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Mon Aug 24 23:37:25 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch master
in repository wmbattery.

commit ef0104fff3db5f9a2a4c120b96e0e5b91ff31664
Author: joey <joey at a4a2c43b-8ac3-0310-8836-e0e880c912e2>
Date:   Mon Apr 8 02:05:28 2002 +0000

       * Improved acpi interface (also used for procmeter3 module now).
       * Fixed segfault on non-ACPI systems.
---
 acpi.c               | 136 ++++++++++++++++++++++++++++++---------------------
 acpi.h               |  44 +++++++++++++++--
 autoconf/makeinfo.in |   2 +-
 debian/changelog     |   7 +++
 wmbattery.c          |  10 ++--
 5 files changed, 134 insertions(+), 65 deletions(-)

diff --git a/acpi.c b/acpi.c
index eeac37a..8770594 100644
--- a/acpi.c
+++ b/acpi.c
@@ -1,29 +1,41 @@
+/* 
+ * A not-yet-general-purpose ACPI library, by Joey Hess <joey at kitenet.net>
+ */
+
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <dirent.h>
 #include <string.h>
+#ifdef ACPI_APM
 #include <apm.h>
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 
 #include "acpi.h"
 
-#define MAXITEM 8
+#define ACPI_MAXITEM 8
 
-int batt_count = 0;
+int acpi_batt_count = 0;
 /* Filenames of the battery info files for each system battery. */
-char battery_info[MAXITEM][128];
+char acpi_batt_info[ACPI_MAXITEM][128];
 /* Filenames of the battery status files for each system battery. */
-char battery_status[MAXITEM][128];
+char acpi_batt_status[ACPI_MAXITEM][128];
 /* Stores battery capacity, or 0 if the battery is absent. */
-int battery_capacity[MAXITEM];
+int acpi_batt_capacity[ACPI_MAXITEM];
+
+int acpi_ac_count = 0;
+char acpi_ac_adapter_info[ACPI_MAXITEM][128];
+char acpi_ac_adapter_status[ACPI_MAXITEM][128];
 
-int ac_count = 0;
-char ac_adapter_info[MAXITEM][128];
-char ac_adapter_status[MAXITEM][128];
+#if ACPI_THERMAL
+int acpi_thermal_count = 0;
+char acpi_thermal_info[ACPI_MAXITEM][128];
+char acpi_thermal_status[ACPI_MAXITEM][128];
+#endif
 
 /* Read in an entire ACPI proc file (well, the first 1024 bytes anyway), and
  * return a statically allocated array containing it. */
@@ -73,9 +85,9 @@ char *get_acpi_value (const char *file, const char *key) {
 }
 
 /* Returns the last full capacity of a battery. */
-int get_batt_capacity(int battery) {
+int get_acpi_batt_capacity(int battery) {
 	int cap;
-	cap = atoi(get_acpi_value(battery_info[battery], "Last Full Capacity:"));
+	cap = atoi(get_acpi_value(acpi_batt_info[battery], "Last Full Capacity:"));
 	/* This is ACPI's broken way of saying that there is no battery. */
 	if (cap == 655350)
 		return 0;
@@ -85,8 +97,8 @@ int get_batt_capacity(int battery) {
 /* Find something (batteries, ac adpaters, etc), and set up a string array
  * to hold the paths to info and status files of the things found. Must be 
  * in /proc/acpi to call this. Returns the number of items found. */
-int find_items (char *itemname, char infoarray[MAXITEM][128],
-		                char statusarray[MAXITEM][128]) {
+int find_items (char *itemname, char infoarray[ACPI_MAXITEM][128],
+		                char statusarray[ACPI_MAXITEM][128]) {
 	DIR *dir;
 	struct dirent *ent;
 	int count = 0;
@@ -103,7 +115,7 @@ int find_items (char *itemname, char infoarray[MAXITEM][128],
 		sprintf(infoarray[count], "%s/%s/%s", itemname, ent->d_name, "info");
 		sprintf(statusarray[count], "%s/%s/%s", itemname, ent->d_name, "status");
 		count++;
-		if (count > MAXITEM)
+		if (count > ACPI_MAXITEM)
 			break;
 	}
 
@@ -111,28 +123,37 @@ int find_items (char *itemname, char infoarray[MAXITEM][128],
 	return count;
 }
 
-/* Find batteries, return the number, and set batt_count to it as well. */
+/* Find batteries, return the number, and set acpi_batt_count to it as well. */
 int find_batteries(void) {
 	int i;
-	batt_count = find_items("battery", battery_info, battery_status);
+	acpi_batt_count = find_items("battery", acpi_batt_info, acpi_batt_status);
 	/* Read in the last charged capacity of the batteries. */
-	for (i = 0; i < batt_count; i++)
-		battery_capacity[i] = get_batt_capacity(i);
-	return batt_count;
+	for (i = 0; i < acpi_batt_count; i++)
+		acpi_batt_capacity[i] = get_acpi_batt_capacity(i);
+	return acpi_batt_count;
 }
 
-/* Find AC power adapters, return the number found, and set ac_count to it
+/* Find AC power adapters, return the number found, and set acpi_ac_count to it
  * as well. */
 int find_ac_adapters(void) {
-	ac_count = find_items("ac_adapter", ac_adapter_info, ac_adapter_status);
-	return ac_count;
+	acpi_ac_count = find_items("ac_adapter", acpi_ac_adapter_info, acpi_ac_adapter_status);
+	return acpi_ac_count;
 }
 
+#if ACPI_THERMAL
+/* Find thermal information sources, return the number found, and set
+ * thermal_count to it as well. */
+int find_thermal(void) {
+	acpi_thermal_count = find_items("thermal", acpi_thermal_info, acpi_thermal_status);
+	return acpi_thermal_count;
+}
+#endif
+
 /* Returns true if the system is on ac power. Call find_ac_adapters first. */
 int on_ac_power (void) {
 	int i;
-	for (i = 0; i < ac_count; i++) {
-		if (strcmp("on-line", get_acpi_value(ac_adapter_status[i], "Status:")))
+	for (i = 0; i < acpi_ac_count; i++) {
+		if (strcmp("on-line", get_acpi_value(acpi_ac_adapter_status[i], "Status:")))
 			return 1;
 		else
 			return 0;
@@ -170,40 +191,42 @@ int acpi_supported (void) {
 		fprintf(stderr, "No ACPI power adapter found!");
 	}
 
+#if ACPI_THERMAL
+	if (! find_thermal()) {
+		fprintf(stderr, "No ACPI thermal information found.");
+	}
+#endif
+	
 	return 1;
 }
 
+#ifdef ACPI_APM
 /* Read ACPI info on a given power adapter and battery, and fill the passed
  * apm_info struct. */
 int acpi_read (int battery, apm_info *info) {
 	char *buf, *state;
-	char *rate_s;
 	
 	/* Internally it's zero indexed. */
 	battery--;
 	
-	buf = get_acpi_file(battery_status[battery]);
+	buf = get_acpi_file(acpi_batt_status[battery]);
 
 	info->ac_line_status = 0;
 	info->battery_flags = 0;
 	info->using_minutes = 1;
 	
-	/* Work out if the battery is present, and what percentage of full
+	/* Work out if the battery is present, and what pqercentage of full
 	 * it is and how much time is left. */
 	if (strcmp(scan_acpi_value(buf, "Present:"), "yes") == 0) {
-		int pcap, rate;
-		
-		pcap = scan_acpi_num(buf, "Remaining Capacity:");
-		rate = scan_acpi_num(buf, "Present Rate:");
+		int pcap = scan_acpi_num(buf, "Remaining Capacity:");
+		int rate = scan_acpi_num(buf, "Present Rate:");
 
 		if (rate) {
 			/* time remaining = (current_capacity / discharge rate) */
 			info->battery_time = (float) pcap / (float) rate * 60;
-			if (info->battery_time <= 0)
-				info->battery_time = 0;
 		}
 		else {
-			rate_s = scan_acpi_value(buf, "Present Rate:");
+			char *rate_s = scan_acpi_value(buf, "Present Rate:");
 			if (! rate_s) {
 				/* Time remaining unknown. */
 				info->battery_time = 0;
@@ -214,25 +237,10 @@ int acpi_read (int battery, apm_info *info) {
 			 * unknown. I don't know if this is the
 			 * correct way to do it. */
 			else if (strcmp(rate_s, "unknown") == 0) {
-				goto NOBAT;
+				goto NOBATT;
 			}
 		}
 
-		if (battery_capacity[battery] == 0) {
-			/* The battery was absent, and now is present.
-			 * Well, it might be a different battery. So
-			 * re-probe the battery. */
-			battery_capacity[battery] = get_batt_capacity(battery);
-		}
-		
-		if (pcap) {
-			/* percentage = (current_capacity / last_full_capacity) * 100 */
-			info->battery_percentage = (float) pcap / (float) battery_capacity[battery] * 100;
-		}
-		else {
-			info->battery_percentage = -1;
-		}
-
 		state = scan_acpi_value(buf, "State:");
 		if (state) {
 			if (state[0] == 'd') { /* discharging */
@@ -242,9 +250,7 @@ int acpi_read (int battery, apm_info *info) {
 				info->battery_status = BATTERY_STATUS_CHARGING;
 				info->ac_line_status = 1;
 				info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
-
-				/*   */
-				info->battery_time = -1 * (float) (battery_capacity[battery] - pcap) / (float) rate * 60;
+				info->battery_time = -1 * (float) (acpi_batt_capacity[battery] - pcap) / (float) rate * 60;
 			}
 			else if (state[0] == 'o') { /* ok */
 				/* charged, on ac power */
@@ -261,15 +267,32 @@ int acpi_read (int battery, apm_info *info) {
 			/* Battery state unknown. */
 			info->battery_status = BATTERY_STATUS_ABSENT;
 		}
+		
+		if (acpi_batt_capacity[battery] == 0) {
+			/* The battery was absent, and now is present.
+			 * Well, it might be a different battery. So
+			 * re-probe the battery. */
+			/* NOTE that this invalidates buf. No accesses of
+			 * buf below this point! */
+			acpi_batt_capacity[battery] = get_acpi_batt_capacity(battery);
+		}
+		
+		if (pcap) {
+			/* percentage = (current_capacity / last_full_capacity) * 100 */
+			info->battery_percentage = (float) pcap / (float) acpi_batt_capacity[battery] * 100;
+		}
+		else {
+			info->battery_percentage = -1;
+		}
+
 	}
 	else {
-NOBAT:
-		/* No battery. */
+NOBATT:
 		info->battery_percentage = 0;
 		info->battery_time = 0;
 		info->battery_status = BATTERY_STATUS_ABSENT;
-		battery_capacity[battery] = 0;
-		if (batt_count == 1) {
+		acpi_batt_capacity[battery] = 0;
+		if (acpi_batt_count == 1) {
 			/* Where else would the power come from, eh? ;-) */
 			info->ac_line_status = 1;
 		}
@@ -281,3 +304,4 @@ NOBAT:
 	
 	return 0;
 }
+#endif
diff --git a/acpi.h b/acpi.h
index 2996f9e..5d93ac1 100644
--- a/acpi.h
+++ b/acpi.h
@@ -1,7 +1,45 @@
-int acpi_supported (void);
-int acpi_read (int battery, apm_info *info);
-extern int batt_count;
+/* 
+ * A not-yet-general-purpose ACPI library, by Joey Hess <joey at kitenet.net>
+ */
+
+/* Define ACPI_THERMAL to make the library support finding info about thermal
+ * sources. */
+//#define ACPI_THERMAL 1
+
+/* Define ACPI_APM to get the acpi_read function, which is like apm_read. */
+//#define ACPI_APM 1
 
 /* The lowest version of ACPI proc files supported. */
 #define ACPI_VERSION 20011018
 
+/* The number of acpi items of each class supported. */
+#define ACPI_MAXITEM 8
+
+int acpi_supported (void);
+#ifdef ACPI_APM
+int acpi_read (int battery, apm_info *info);
+#endif
+char *get_acpi_file (const char *file);
+int scan_acpi_num (const char *buf, const char *key);
+char *scan_acpi_value (const char *buf, const char *key);
+char *get_acpi_value (const char *file, const char *key);
+int get_acpi_batt_capacity(int battery);
+
+extern int acpi_batt_count;
+/* Filenames of the battery info files for each system battery. */
+extern char acpi_batt_info[ACPI_MAXITEM][128];
+/* Filenames of the battery status files for each system battery. */
+extern char acpi_batt_status[ACPI_MAXITEM][128];
+/* Stores battery capacity, or 0 if the battery is absent. */
+extern int acpi_batt_capacity[ACPI_MAXITEM];
+
+extern int acpi_ac_count;
+extern char acpi_ac_adapter_info[ACPI_MAXITEM][128];
+extern char acpi_ac_adapter_status[ACPI_MAXITEM][128];
+
+#if ACPI_THERMAL
+extern int acpi_thermal_count;
+extern char acpi_thermal_info[ACPI_MAXITEM][128];
+extern char acpi_thermal_status[ACPI_MAXITEM][128];
+#endif
+
diff --git a/autoconf/makeinfo.in b/autoconf/makeinfo.in
index cc17c33..2584096 100644
--- a/autoconf/makeinfo.in
+++ b/autoconf/makeinfo.in
@@ -6,7 +6,7 @@ prefix=@prefix@
 srcdir=@srcdir@
 
 CC=@CC@
-CFLAGS=@CFLAGS@ -Wall
+CFLAGS=@CFLAGS@ -Wall -DACPI_APM
 CPPFLAGS=@CPPFLAGS@ -DICONDIR=\"$(icondir)\"
 INSTALL = @INSTALL@
 INSTALL_DATA = @INSTALL_DATA@
diff --git a/debian/changelog b/debian/changelog
index 22cd73b..6e64fea 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+wmbattery (2.02) unstable; urgency=medium
+
+  * Improved acpi interface (also used for procmeter3 module now).
+  * Fixed segfault on non-ACPI systems.
+
+ -- Joey Hess <joeyh at debian.org>  Sun,  7 Apr 2002 17:48:41 -0400
+
 wmbattery (2.01) unstable; urgency=low
 
   * Corrected minus sign in bigfont to proper faux-lcd appearance. Also fixed
diff --git a/wmbattery.c b/wmbattery.c
index c3f070f..e5c4d98 100644
--- a/wmbattery.c
+++ b/wmbattery.c
@@ -28,7 +28,7 @@ int pos[2] = {0, 0};
 
 int battnum = 1;
 int use_sonypi = 0;
-int use_acpi = 1;
+int use_acpi = 0;
 int delay = 0;
 
 signed int low_pct = -1;
@@ -413,11 +413,11 @@ int main(int argc, char *argv[]) {
 	}
 	/* Check for ACPI support. */
 	else if (acpi_supported()) {
-		if (battnum > batt_count || battnum < 1) {
+		if (battnum > acpi_batt_count || battnum < 1) {
 			error("There %s only %i batter%s, and you asked for number %i.",
-					batt_count == 1 ? "is" : "are",
-					batt_count,
-					batt_count == 1 ? "y" : "ies",
+					acpi_batt_count == 1 ? "is" : "are",
+					acpi_batt_count,
+					acpi_batt_count == 1 ? "y" : "ies",
 					battnum);
 		}
 		use_acpi = 1;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-wmaker/wmbattery.git



More information about the Pkg-wmaker-commits mailing list