From 1de2a9e6c5c6743663b9ac9a33df3d5f3203cfc4 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 24 Jan 2010 15:34:03 +0100 Subject: [PATCH] Add optional argument to $acpiacadapter The exact location of the state information seems hardware dependent, so this adds the option to specify it manually. (linux only) --- doc/variables.xml | 5 ++++- src/common.cc | 3 +-- src/common.h | 2 +- src/core.cc | 18 ++++++++++++++++++ src/freebsd.c | 4 +++- src/linux.cc | 9 ++++++--- src/netbsd.c | 4 +++- 7 files changed, 36 insertions(+), 9 deletions(-) diff --git a/doc/variables.xml b/doc/variables.xml index 8b438b8d..6c53a2c2 100644 --- a/doc/variables.xml +++ b/doc/variables.xml @@ -4,8 +4,11 @@ + - ACPI ac adapter state. + ACPI ac adapter state. On linux, the adapter option specifies the + subfolder of /sys/class/power_supply containing the state information (defaults + to "AC"). Other systems ignore it. diff --git a/src/common.cc b/src/common.cc index d4339efa..dc1cfa8e 100644 --- a/src/common.cc +++ b/src/common.cc @@ -734,8 +734,7 @@ void print_acpifan(struct text_object *obj, char *p, int p_max_size) void print_acpiacadapter(struct text_object *obj, char *p, int p_max_size) { - (void)obj; - get_acpi_ac_adapter(p, p_max_size); + get_acpi_ac_adapter(p, p_max_size, (const char *)obj->data.opaque); } void print_battery(struct text_object *obj, char *p, int p_max_size) diff --git a/src/common.h b/src/common.h index daa81009..aa6519e6 100644 --- a/src/common.h +++ b/src/common.h @@ -87,7 +87,7 @@ extern int no_buffers; int open_acpi_temperature(const char *name); double get_acpi_temperature(int fd); -void get_acpi_ac_adapter(char *, size_t); +void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter); void get_acpi_fan(char *, size_t); void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item); int get_battery_perct(const char *bat); diff --git a/src/core.cc b/src/core.cc index eb1aa1ad..d7703ff4 100644 --- a/src/core.cc +++ b/src/core.cc @@ -169,7 +169,25 @@ struct text_object *construct_text_object(const char *s, const char *arg, long obj->callbacks.print = &print_acpitemp; obj->callbacks.free = &free_acpitemp; END OBJ(acpiacadapter, 0) + if(arg) { +#ifdef __linux__ + if(strpbrk(arg, "/.") != NULL) { + /* + * a bit of paranoia. screen out funky paths + * i hope no device will have a '.' in its name + */ + NORM_ERR("acpiacadapter: arg must not contain '/' or '.'"); + } else + obj->data.opaque = strdup(arg); +#else + NORM_ERR("acpiacadapter: arg is only used on linux"); +#endif + } + if(! obj->data.opaque) + obj->data.opaque = strdup("AC"); + obj->callbacks.print = &print_acpiacadapter; + obj->callbacks.free = &gen_free_opaque; #endif /* !__OpenBSD__ */ END OBJ(freq, 0) get_cpu_count(); diff --git a/src/freebsd.c b/src/freebsd.c index 117d4f76..9edc8cd1 100644 --- a/src/freebsd.c +++ b/src/freebsd.c @@ -522,10 +522,12 @@ int open_acpi_temperature(const char *name) return 0; } -void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size) +void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter) { int state; + (void) adapter; // only linux uses this + if (!p_client_buffer || client_buffer_size <= 0) { return; } diff --git a/src/linux.cc b/src/linux.cc index 59ae392b..a4388bb3 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -1342,7 +1342,7 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) snprintf(p_client_buffer, client_buffer_size, "%s", buf); } -#define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply/AC" +#define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply" #define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/" /* Linux 2.6.25 onwards ac adapter info is in /sys/class/power_supply/AC/ @@ -1354,9 +1354,12 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) POWER_SUPPLY_NAME=AC POWER_SUPPLY_TYPE=Mains POWER_SUPPLY_ONLINE=1 + + Update: it seems the folder name is hardware-dependent. We add an aditional adapter + argument, specifying the folder name. */ -void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size) +void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter) { static int rep = 0; @@ -1368,7 +1371,7 @@ void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size) return; } - snprintf(buf2, sizeof(buf2), "%s/uevent", SYSFS_AC_ADAPTER_DIR); + snprintf(buf2, sizeof(buf2), "%s/%s/uevent", SYSFS_AC_ADAPTER_DIR, adapter); fp = open_file(buf2, &rep); if (fp) { /* sysfs processing */ diff --git a/src/netbsd.c b/src/netbsd.c index dff16cbb..5645df1a 100644 --- a/src/netbsd.c +++ b/src/netbsd.c @@ -333,8 +333,10 @@ int open_acpi_temperature(const char *name) return -1; } -void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size) +void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter) { + (void) adapter; // only linux uses this + if (!p_client_buffer || client_buffer_size <= 0) { return; }