wmbattery: Consistent formatting in source.

Modified source files to minimize warnings from checkpatch.pl in Window Maker
source tree.
This commit is contained in:
Doug Torrance 2014-10-05 10:30:02 -05:00 committed by Carlos R. Mafra
parent 8e5aa5c7e4
commit 539ef9f52c
9 changed files with 353 additions and 357 deletions

View file

@ -60,12 +60,14 @@ char acpi_thermal_status[ACPI_MAXITEM][128];
/* Read in an entire ACPI proc file (well, the first 1024 bytes anyway), and /* Read in an entire ACPI proc file (well, the first 1024 bytes anyway), and
* return a statically allocated array containing it. */ * return a statically allocated array containing it. */
inline char *get_acpi_file (const char *file) { inline char *get_acpi_file(const char *file)
{
int fd; int fd;
int end; int end;
static char buf[1024]; static char buf[1024];
fd = open(file, O_RDONLY); fd = open(file, O_RDONLY);
if (fd == -1) return NULL; if (fd == -1)
return NULL;
end = read(fd, buf, sizeof(buf)); end = read(fd, buf, sizeof(buf));
buf[end-1] = '\0'; buf[end-1] = '\0';
close(fd); close(fd);
@ -86,14 +88,16 @@ int strmcmp(const char *s1, const char *s2)
/* Given a buffer holding an acpi file, searches for the given key in it, /* Given a buffer holding an acpi file, searches for the given key in it,
* and returns the numeric value. 0 is returned on failure. */ * and returns the numeric value. 0 is returned on failure. */
inline int scan_acpi_num (const char *buf, const char *key) { inline int scan_acpi_num(const char *buf, const char *key)
{
char *ptr; char *ptr;
int ret = 0; int ret = 0;
do { do {
ptr = strchr(buf, '\n'); ptr = strchr(buf, '\n');
if (!strmcmp(buf, key)) { if (!strmcmp(buf, key)) {
if ((ptr = strchr(buf, '='))) { ptr = strchr(buf, '=');
if (ptr) {
sscanf(ptr + 1, "%d", &ret); sscanf(ptr + 1, "%d", &ret);
return ret; return ret;
} else { } else {
@ -109,19 +113,20 @@ inline int scan_acpi_num (const char *buf, const char *key) {
/* Given a buffer holding an acpi file, searches for the given key in it, /* Given a buffer holding an acpi file, searches for the given key in it,
* and returns its value in a statically allocated string. */ * and returns its value in a statically allocated string. */
inline char *scan_acpi_value (const char *buf, const char *key) { inline char *scan_acpi_value(const char *buf, const char *key)
{
char *ptr; char *ptr;
static char ret[256]; static char ret[256];
do { do {
ptr = strchr(buf, '\n'); ptr = strchr(buf, '\n');
if (!strmcmp(buf, key)) { if (!strmcmp(buf, key)) {
if ((ptr = strchr(buf, '='))) { ptr = strchr(buf, '=');
if (sscanf(ptr + 1, "%255s", ret) == 1) { if (ptr) {
if (sscanf(ptr + 1, "%255s", ret) == 1)
return ret; return ret;
} else { else
return NULL; return NULL;
}
} else { } else {
return NULL; return NULL;
} }
@ -137,27 +142,30 @@ inline char *scan_acpi_value (const char *buf, const char *key) {
* return it (statically allocated string). Returns NULL on error, This is * return it (statically allocated string). Returns NULL on error, This is
* the slow, dumb way, fine for initialization or if only one value is needed * the slow, dumb way, fine for initialization or if only one value is needed
* from a file, slow if called many times. */ * from a file, slow if called many times. */
char *get_acpi_value (const char *file, const char *key) { char *get_acpi_value(const char *file, const char *key)
{
char *buf = get_acpi_file(file); char *buf = get_acpi_file(file);
if (! buf) return NULL; if (!buf)
return NULL;
return scan_acpi_value(buf, key); return scan_acpi_value(buf, key);
} }
/* Returns the last full charge capacity of a battery. /* Returns the last full charge capacity of a battery.
*/ */
int get_acpi_batt_capacity(int battery) { int get_acpi_batt_capacity(int battery)
{
char *s; char *s;
s = get_acpi_value(acpi_batt_info[battery], acpi_labels[label_last_full_capacity]); s = get_acpi_value(acpi_batt_info[battery], acpi_labels[label_last_full_capacity]);
if (s == NULL) { if (s == NULL)
return 0; return 0;
} else { else
return atoi(s); return atoi(s);
} }
}
/* Comparison function for qsort. */ /* Comparison function for qsort. */
int _acpi_compare_strings (const void *a, const void *b) { int _acpi_compare_strings(const void *a, const void *b)
{
const char **pa = (const char **)a; const char **pa = (const char **)a;
const char **pb = (const char **)b; const char **pb = (const char **)b;
return strcasecmp((const char *)*pa, (const char *)*pb); return strcasecmp((const char *)*pa, (const char *)*pb);
@ -167,7 +175,8 @@ int _acpi_compare_strings (const void *a, const void *b) {
* to hold the paths to info and status files of the things found. * to hold the paths to info and status files of the things found.
* Returns the number of items found. */ * Returns the number of items found. */
int find_items(char *itemname, char infoarray[ACPI_MAXITEM][128], int find_items(char *itemname, char infoarray[ACPI_MAXITEM][128],
char statusarray[ACPI_MAXITEM][128]) { char statusarray[ACPI_MAXITEM][128])
{
DIR *dir; DIR *dir;
struct dirent *ent; struct dirent *ent;
int num_devices = 0; int num_devices = 0;
@ -222,7 +231,8 @@ int find_items (char *itemname, char infoarray[ACPI_MAXITEM][128],
} }
/* Find batteries, return the number, and set acpi_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 find_batteries(void)
{
int i; int i;
acpi_batt_count = find_items(acpi_labels[label_battery], acpi_batt_info, acpi_batt_status); acpi_batt_count = find_items(acpi_labels[label_battery], acpi_batt_info, acpi_batt_status);
for (i = 0; i < acpi_batt_count; i++) for (i = 0; i < acpi_batt_count; i++)
@ -232,7 +242,8 @@ int find_batteries(void) {
/* Find AC power adapters, return the number found, and set acpi_ac_count to it /* Find AC power adapters, return the number found, and set acpi_ac_count to it
* as well. */ * as well. */
int find_ac_adapters(void) { int find_ac_adapters(void)
{
acpi_ac_count = find_items(acpi_labels[label_ac_adapter], acpi_ac_adapter_info, acpi_ac_adapter_status); acpi_ac_count = find_items(acpi_labels[label_ac_adapter], acpi_ac_adapter_info, acpi_ac_adapter_status);
return acpi_ac_count; return acpi_ac_count;
} }
@ -240,14 +251,16 @@ int find_ac_adapters(void) {
#if ACPI_THERMAL #if ACPI_THERMAL
/* Find thermal information sources, return the number found, and set /* Find thermal information sources, return the number found, and set
* thermal_count to it as well. */ * thermal_count to it as well. */
int find_thermal(void) { int find_thermal(void)
{
acpi_thermal_count = find_items(acpi_labels[label_thermal], acpi_thermal_info, acpi_thermal_status); acpi_thermal_count = find_items(acpi_labels[label_thermal], acpi_thermal_info, acpi_thermal_status);
return acpi_thermal_count; return acpi_thermal_count;
} }
#endif #endif
/* Returns true if the system is on ac power. Call find_ac_adapters first. */ /* Returns true if the system is on ac power. Call find_ac_adapters first. */
int on_ac_power (void) { int on_ac_power(void)
{
int i; int i;
for (i = 0; i < acpi_ac_count; i++) { for (i = 0; i < acpi_ac_count; i++) {
char *online = get_acpi_value(acpi_ac_adapter_info[i], acpi_labels[label_ac_state]); char *online = get_acpi_value(acpi_ac_adapter_info[i], acpi_labels[label_ac_state]);
@ -261,23 +274,23 @@ int on_ac_power (void) {
/* See if we have ACPI support and check version. Also find batteries and /* See if we have ACPI support and check version. Also find batteries and
* ac power adapters. */ * ac power adapters. */
int acpi_supported (void) { int acpi_supported(void)
{
char *version; char *version;
DIR *dir; DIR *dir;
int num; int num;
if (!(dir = opendir(SYSFS_PATH))) { dir = opendir(SYSFS_PATH);
if (!dir)
return 0; return 0;
}
closedir(dir); closedir(dir);
/* If kernel is 2.6.21 or newer, version is in /* If kernel is 2.6.21 or newer, version is in
/sys/module/acpi/parameters/acpica_version */ /sys/module/acpi/parameters/acpica_version */
version = get_acpi_file("/sys/module/acpi/parameters/acpica_version"); version = get_acpi_file("/sys/module/acpi/parameters/acpica_version");
if (version == NULL) { if (version == NULL)
return 0; return 0;
}
num = atoi(version); num = atoi(version);
if (num < ACPI_VERSION) { if (num < ACPI_VERSION) {
fprintf(stderr, "ACPI subsystem %s too is old, consider upgrading to %i.\n", fprintf(stderr, "ACPI subsystem %s too is old, consider upgrading to %i.\n",
@ -297,7 +310,8 @@ int acpi_supported (void) {
#ifdef ACPI_APM #ifdef ACPI_APM
/* Read ACPI info on a given power adapter and battery, and fill the passed /* Read ACPI info on a given power adapter and battery, and fill the passed
* apm_info struct. */ * apm_info struct. */
int acpi_read (int battery, apm_info *info) { int acpi_read(int battery, apm_info *info)
{
char *buf, *state; char *buf, *state;
if (acpi_batt_count == 0) { if (acpi_batt_count == 0) {
@ -332,14 +346,12 @@ int acpi_read (int battery, apm_info *info) {
if (rate) { if (rate) {
/* time remaining = (current_capacity / discharge rate) */ /* time remaining = (current_capacity / discharge rate) */
info->battery_time = (float) pcap / (float) rate * 60; info->battery_time = (float) pcap / (float) rate * 60;
} } else {
else {
char *rate_s = scan_acpi_value(buf, acpi_labels[label_present_rate]); char *rate_s = scan_acpi_value(buf, acpi_labels[label_present_rate]);
if (!rate_s) { if (!rate_s) {
/* Time remaining unknown. */ /* Time remaining unknown. */
info->battery_time = 0; info->battery_time = 0;
} } else {
else {
/* a zero or unknown in the file; time /* a zero or unknown in the file; time
* unknown so use a negative one to * unknown so use a negative one to
* indicate this */ * indicate this */
@ -355,31 +367,28 @@ int acpi_read (int battery, apm_info *info) {
* because AC power might be on even if a * because AC power might be on even if a
* battery is discharging in some cases. */ * battery is discharging in some cases. */
info->ac_line_status = on_ac_power(); info->ac_line_status = on_ac_power();
} } else if (state[0] == 'C' && state[1] == 'h') { /* charging */
else if (state[0] == 'C' && state[1] == 'h') { /* charging */
info->battery_status = BATTERY_STATUS_CHARGING; info->battery_status = BATTERY_STATUS_CHARGING;
info->ac_line_status = 1; info->ac_line_status = 1;
info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING; info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
if (rate) if (rate)
info->battery_time = -1 * (float) (acpi_batt_capacity[battery] - pcap) / (float) rate * 60; info->battery_time = -1 * (float) (acpi_batt_capacity[battery] - pcap) /
(float) rate * 60;
else else
info->battery_time = 0; info->battery_time = 0;
if (abs(info->battery_time) < 0.5) if (abs(info->battery_time) < 0.5)
info->battery_time = 0; info->battery_time = 0;
} } else if (state[0] == 'F') { /* full */
else if (state[0] == 'F') { /* full */
/* charged, on ac power */ /* charged, on ac power */
info->battery_status = BATTERY_STATUS_HIGH; info->battery_status = BATTERY_STATUS_HIGH;
info->ac_line_status = 1; info->ac_line_status = 1;
} } else if (state[0] == 'C') { /* not charging, so must be critical */
else if (state[0] == 'C') { /* not charging, so must be critical */
info->battery_status = BATTERY_STATUS_CRITICAL; info->battery_status = BATTERY_STATUS_CRITICAL;
/* Expensive ac power check used here /* Expensive ac power check used here
* because AC power might be on even if a * because AC power might be on even if a
* battery is critical in some cases. */ * battery is critical in some cases. */
info->ac_line_status = on_ac_power(); info->ac_line_status = on_ac_power();
} } else if (state[0] == 'U') { /* unknown */
else if (state[0] == 'U') { /* unknown */
info->ac_line_status = on_ac_power(); info->ac_line_status = on_ac_power();
int current = scan_acpi_num(buf, acpi_labels[label_present_rate]); int current = scan_acpi_num(buf, acpi_labels[label_present_rate]);
if (info->ac_line_status) { if (info->ac_line_status) {
@ -387,16 +396,13 @@ int acpi_read (int battery, apm_info *info) {
info->battery_status = BATTERY_STATUS_HIGH; info->battery_status = BATTERY_STATUS_HIGH;
else else
info->battery_status = BATTERY_STATUS_CHARGING; info->battery_status = BATTERY_STATUS_CHARGING;
} } else {
else {
info->battery_status = BATTERY_STATUS_CHARGING; info->battery_status = BATTERY_STATUS_CHARGING;
} }
} } else {
else {
fprintf(stderr, "unknown battery state: %s\n", state); fprintf(stderr, "unknown battery state: %s\n", state);
} }
} } else {
else {
/* Battery state unknown. */ /* Battery state unknown. */
info->battery_status = BATTERY_STATUS_ABSENT; info->battery_status = BATTERY_STATUS_ABSENT;
} }
@ -408,8 +414,7 @@ int acpi_read (int battery, apm_info *info) {
/* NOTE that this invalidates buf. No accesses of /* NOTE that this invalidates buf. No accesses of
* buf below this point! */ * buf below this point! */
acpi_batt_capacity[battery] = get_acpi_batt_capacity(battery); acpi_batt_capacity[battery] = get_acpi_batt_capacity(battery);
} } else if (pcap > acpi_batt_capacity[battery]) {
else if (pcap > acpi_batt_capacity[battery]) {
/* Battery is somehow charged to greater than max /* Battery is somehow charged to greater than max
* capacity. Rescan for a new max capacity. */ * capacity. Rescan for a new max capacity. */
find_batteries(); find_batteries();
@ -419,13 +424,11 @@ int acpi_read (int battery, apm_info *info) {
info->battery_percentage = 100 * pcap / acpi_batt_capacity[battery]; info->battery_percentage = 100 * pcap / acpi_batt_capacity[battery];
if (info->battery_percentage > 100) if (info->battery_percentage > 100)
info->battery_percentage = 100; info->battery_percentage = 100;
} } else {
else {
info->battery_percentage = -1; info->battery_percentage = -1;
} }
} } else {
else {
info->battery_percentage = 0; info->battery_percentage = 0;
info->battery_time = 0; info->battery_time = 0;
info->battery_status = BATTERY_STATUS_ABSENT; info->battery_status = BATTERY_STATUS_ABSENT;
@ -433,8 +436,7 @@ int acpi_read (int battery, apm_info *info) {
if (acpi_batt_count == 0) { if (acpi_batt_count == 0) {
/* Where else would the power come from, eh? ;-) */ /* Where else would the power come from, eh? ;-) */
info->ac_line_status = 1; info->ac_line_status = 1;
} } else {
else {
/* Expensive ac power check. */ /* Expensive ac power check. */
info->ac_line_status = on_ac_power(); info->ac_line_status = on_ac_power();
} }

View file

@ -4,10 +4,10 @@
/* Define ACPI_THERMAL to make the library support finding info about thermal /* Define ACPI_THERMAL to make the library support finding info about thermal
* sources. */ * sources. */
//#define ACPI_THERMAL 1 /* #define ACPI_THERMAL 1 */
/* Define ACPI_APM to get the acpi_read function, which is like apm_read. */ /* Define ACPI_APM to get the acpi_read function, which is like apm_read. */
//#define ACPI_APM 1 /* #define ACPI_APM 1 */
/* The lowest version of ACPI proc files supported. */ /* The lowest version of ACPI proc files supported. */
#define ACPI_VERSION 20011018 #define ACPI_VERSION 20011018

View file

@ -9,15 +9,16 @@
#include <libhal.h> #include <libhal.h>
#include "apm.h" #include "apm.h"
static DBusConnection *dbus_ctx = NULL; static DBusConnection *dbus_ctx;
static LibHalContext *hal_ctx = NULL; static LibHalContext *hal_ctx;
int num_ac_adapters = 0; int num_ac_adapters = 0;
int num_batteries = 0; int num_batteries = 0;
char **ac_adapters = NULL; char **ac_adapters = NULL;
char **batteries = NULL; char **batteries = NULL;
int connect_hal (void) { int connect_hal(void)
{
DBusError error; DBusError error;
dbus_error_init(&error); dbus_error_init(&error);
@ -28,7 +29,8 @@ int connect_hal (void) {
LIBHAL_FREE_DBUS_ERROR(&error); LIBHAL_FREE_DBUS_ERROR(&error);
return 0; return 0;
} }
if ((hal_ctx = libhal_ctx_new()) == NULL) { hal_ctx = libhal_ctx_new();
if (hal_ctx == NULL) {
fprintf(stderr, "error: libhal_ctx_new\n"); fprintf(stderr, "error: libhal_ctx_new\n");
LIBHAL_FREE_DBUS_ERROR(&error); LIBHAL_FREE_DBUS_ERROR(&error);
return 0; return 0;
@ -52,11 +54,11 @@ int connect_hal (void) {
return 1; return 1;
} }
int hal_ready (void) { int hal_ready(void)
{
if (hal_ctx && dbus_connection_get_is_connected(dbus_ctx)) { if (hal_ctx && dbus_connection_get_is_connected(dbus_ctx)) {
return 1; return 1;
} } else {
else {
/* The messy business of reconnecting. /* The messy business of reconnecting.
* dbus's design is crap when it comes to reconnecting. * dbus's design is crap when it comes to reconnecting.
* If dbus is down, can't actually close the connection to hal, * If dbus is down, can't actually close the connection to hal,
@ -72,13 +74,13 @@ int hal_ready (void) {
} }
} }
signed int get_hal_int (const char *udi, const char *key, int optional) { signed int get_hal_int(const char *udi, const char *key, int optional)
{
int ret; int ret;
DBusError error; DBusError error;
if (! hal_ready()) { if (!hal_ready())
return -1; return -1;
}
dbus_error_init(&error); dbus_error_init(&error);
@ -86,8 +88,7 @@ signed int get_hal_int (const char *udi, const char *key, int optional) {
if (!dbus_error_is_set(&error)) { if (!dbus_error_is_set(&error)) {
return ret; return ret;
} } else {
else {
if (!optional) { if (!optional) {
fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n", fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n",
error.name, error.message); error.name, error.message);
@ -97,13 +98,13 @@ signed int get_hal_int (const char *udi, const char *key, int optional) {
} }
} }
signed int get_hal_bool (const char *udi, const char *key, int optional) { signed int get_hal_bool(const char *udi, const char *key, int optional)
{
int ret; int ret;
DBusError error; DBusError error;
if (! hal_ready()) { if (!hal_ready())
return -1; return -1;
}
dbus_error_init(&error); dbus_error_init(&error);
@ -111,8 +112,7 @@ signed int get_hal_bool (const char *udi, const char *key, int optional) {
if (!dbus_error_is_set(&error)) { if (!dbus_error_is_set(&error)) {
return ret; return ret;
} } else {
else {
if (!optional) { if (!optional) {
fprintf(stderr, "error: libhal_device_get_property_bool: %s: %s\n", fprintf(stderr, "error: libhal_device_get_property_bool: %s: %s\n",
error.name, error.message); error.name, error.message);
@ -122,7 +122,8 @@ signed int get_hal_bool (const char *udi, const char *key, int optional) {
} }
} }
void find_devices (void) { void find_devices(void)
{
DBusError error; DBusError error;
dbus_error_init(&error); dbus_error_init(&error);
@ -146,41 +147,39 @@ void find_devices (void) {
} }
} }
int simplehal_supported (void) { int simplehal_supported(void)
{
if (!connect_hal()) { if (!connect_hal()) {
return 0; return 0;
} } else {
else {
find_devices(); find_devices();
return 1; return 1;
} }
} }
/* Fill the passed apm_info struct. */ /* Fill the passed apm_info struct. */
int simplehal_read (int battery, apm_info *info) { int simplehal_read(int battery, apm_info *info)
{
char *device; char *device;
int i; int i;
/* Allow a battery that was not present before to appear. */ /* Allow a battery that was not present before to appear. */
if (battery > num_batteries) { if (battery > num_batteries)
find_devices(); find_devices();
}
info->battery_flags = 0; info->battery_flags = 0;
info->using_minutes = 0; info->using_minutes = 0;
info->ac_line_status = 0; info->ac_line_status = 0;
for (i = 0 ; i < num_ac_adapters && ! info->ac_line_status ; i++) { for (i = 0 ; i < num_ac_adapters && !info->ac_line_status ; i++)
info->ac_line_status = (get_hal_bool(ac_adapters[i], "ac_adapter.present", 0) == 1); info->ac_line_status = (get_hal_bool(ac_adapters[i], "ac_adapter.present", 0) == 1);
}
if (battery > num_batteries) { if (battery > num_batteries) {
info->battery_percentage = 0; info->battery_percentage = 0;
info->battery_time = 0; info->battery_time = 0;
info->battery_status = BATTERY_STATUS_ABSENT; info->battery_status = BATTERY_STATUS_ABSENT;
return 0; return 0;
} } else {
else {
device = batteries[battery-1]; device = batteries[battery-1];
} }
@ -199,23 +198,18 @@ int simplehal_read (int battery, apm_info *info) {
info->battery_status = BATTERY_STATUS_CHARGING; info->battery_status = BATTERY_STATUS_CHARGING;
/* charge_level.warning and charge_level.low are not /* charge_level.warning and charge_level.low are not
* required to be available; this is good enough */ * required to be available; this is good enough */
if (info->battery_percentage < 1) { if (info->battery_percentage < 1)
info->battery_status = BATTERY_STATUS_CRITICAL; info->battery_status = BATTERY_STATUS_CRITICAL;
}
else if (info->battery_percentage < 10) { else if (info->battery_percentage < 10) {
info->battery_status = BATTERY_STATUS_LOW; info->battery_status = BATTERY_STATUS_LOW;
} } else if (info->ac_line_status &&
}
else if (info->ac_line_status &&
get_hal_bool(device, "battery.rechargeable.is_charging", 0) == 1) { get_hal_bool(device, "battery.rechargeable.is_charging", 0) == 1) {
info->battery_status = BATTERY_STATUS_CHARGING; info->battery_status = BATTERY_STATUS_CHARGING;
info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING; info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
} } else if (info->ac_line_status) {
else if (info->ac_line_status) {
/* Must be fully charged. */ /* Must be fully charged. */
info->battery_status = BATTERY_STATUS_HIGH; info->battery_status = BATTERY_STATUS_HIGH;
} } else {
else {
fprintf(stderr, "unknown battery state\n"); fprintf(stderr, "unknown battery state\n");
} }

View file

@ -9,14 +9,17 @@
signed int spicfd = -1; signed int spicfd = -1;
int sonypi_supported (void) { int sonypi_supported(void)
if ((spicfd = open("/dev/sonypi", O_RDWR)) == -1) {
spicfd = open("/dev/sonypi", O_RDWR);
if (spicfd == -1)
return 0; return 0;
else else
return 1; return 1;
} }
inline int sonypi_ioctl(int ioctlno, void *param) { inline int sonypi_ioctl(int ioctlno, void *param)
{
if (ioctl(spicfd, ioctlno, param) < 0) if (ioctl(spicfd, ioctlno, param) < 0)
return 0; return 0;
else else
@ -25,16 +28,16 @@ inline int sonypi_ioctl(int ioctlno, void *param) {
/* Read battery info from sonypi device and shove it into an apm_info /* Read battery info from sonypi device and shove it into an apm_info
* struct. */ * struct. */
int sonypi_read (apm_info *info) { int sonypi_read(apm_info *info)
{
__u8 batflags; __u8 batflags;
__u16 cap, rem; __u16 cap, rem;
int havebatt = 0; int havebatt = 0;
info->using_minutes = info->battery_flags = 0; info->using_minutes = info->battery_flags = 0;
if (! sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags)) { if (!sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags))
return 1; return 1;
}
info->ac_line_status = (batflags & SONYPI_BFLAGS_AC) != 0; info->ac_line_status = (batflags & SONYPI_BFLAGS_AC) != 0;
if (batflags & SONYPI_BFLAGS_B1) { if (batflags & SONYPI_BFLAGS_B1) {
@ -43,8 +46,7 @@ int sonypi_read (apm_info *info) {
if (!sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem)) if (!sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem))
return 1; return 1;
havebatt = 1; havebatt = 1;
} } else if (batflags & SONYPI_BFLAGS_B2) {
else if (batflags & SONYPI_BFLAGS_B2) {
/* Not quite right, if there is a second battery I should /* Not quite right, if there is a second battery I should
* probably merge the two somehow.. */ * probably merge the two somehow.. */
if (!sonypi_ioctl(SONYPI_IOCGBAT2CAP, &cap)) if (!sonypi_ioctl(SONYPI_IOCGBAT2CAP, &cap))
@ -52,8 +54,7 @@ int sonypi_read (apm_info *info) {
if (!sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem)) if (!sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem))
return 1; return 1;
havebatt = 1; havebatt = 1;
} } else {
else {
info->battery_percentage = 0; info->battery_percentage = 0;
info->battery_status = BATTERY_STATUS_ABSENT; info->battery_status = BATTERY_STATUS_ABSENT;
} }

View file

@ -41,26 +41,25 @@ static void get_devinfo(gpointer device, gpointer result)
if (ctx->current == ctx->needed) { if (ctx->current == ctx->needed) {
ctx->percentage = (int)percentage; ctx->percentage = (int)percentage;
ctx->state = state; ctx->state = state;
if (time_to_empty) { if (time_to_empty)
ctx->time = time_to_empty; ctx->time = time_to_empty;
} else { else
ctx->time = time_to_full; ctx->time = time_to_full;
} }
}
ctx->current++; ctx->current++;
} else if (kind == UP_DEVICE_KIND_LINE_POWER) { } else if (kind == UP_DEVICE_KIND_LINE_POWER) {
ctx->ac |= online; ctx->ac |= online;
} }
} }
int upower_supported (void) { int upower_supported(void)
{
UpClient *up; UpClient *up;
up = up_client_new(); up = up_client_new();
if (!up) { if (!up) {
return 0; return 0;
} } else {
else {
GPtrArray *devices = up_client_get_devices(up); GPtrArray *devices = up_client_get_devices(up);
if (!devices) { if (!devices) {
@ -75,15 +74,15 @@ int upower_supported (void) {
} }
/* Fill the passed apm_info struct. */ /* Fill the passed apm_info struct. */
int upower_read(int battery, apm_info *info) { int upower_read(int battery, apm_info *info)
{
UpClient *up; UpClient *up;
GPtrArray *devices = NULL; GPtrArray *devices = NULL;
up = up_client_new(); up = up_client_new();
if (!up) { if (!up)
return -1; return -1;
}
#if !UP_CHECK_VERSION(0, 9, 99) #if !UP_CHECK_VERSION(0, 9, 99)
/* Allow a battery that was not present before to appear. */ /* Allow a battery that was not present before to appear. */
@ -92,9 +91,8 @@ int upower_read(int battery, apm_info *info) {
devices = up_client_get_devices(up); devices = up_client_get_devices(up);
if (!devices) { if (!devices)
return -1; return -1;
}
info->battery_flags = 0; info->battery_flags = 0;
info->using_minutes = 0; info->using_minutes = 0;
@ -120,22 +118,17 @@ int upower_read(int battery, apm_info *info) {
info->battery_status = BATTERY_STATUS_CHARGING; info->battery_status = BATTERY_STATUS_CHARGING;
/* charge_level.warning and charge_level.low are not /* charge_level.warning and charge_level.low are not
* required to be available; this is good enough */ * required to be available; this is good enough */
if (info->battery_percentage < 1) { if (info->battery_percentage < 1)
info->battery_status = BATTERY_STATUS_CRITICAL; info->battery_status = BATTERY_STATUS_CRITICAL;
} else if (info->battery_percentage < 10)
else if (info->battery_percentage < 10) {
info->battery_status = BATTERY_STATUS_LOW; info->battery_status = BATTERY_STATUS_LOW;
} } else if (info->ac_line_status && ctx.state == UP_DEVICE_STATE_CHARGING) {
}
else if (info->ac_line_status && ctx.state == UP_DEVICE_STATE_CHARGING) {
info->battery_status = BATTERY_STATUS_CHARGING; info->battery_status = BATTERY_STATUS_CHARGING;
info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING; info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
} } else if (info->ac_line_status) {
else if (info->ac_line_status) {
/* Must be fully charged. */ /* Must be fully charged. */
info->battery_status = BATTERY_STATUS_HIGH; info->battery_status = BATTERY_STATUS_HIGH;
} } else {
else {
fprintf(stderr, "unknown battery state\n"); fprintf(stderr, "unknown battery state\n");
} }

View file

@ -58,7 +58,8 @@ int initial_state = WithdrawnState;
signed int low_pct = -1; signed int low_pct = -1;
signed int critical_pct = -1; signed int critical_pct = -1;
void error(const char *fmt, ...) { void error(const char *fmt, ...)
{
va_list arglist; va_list arglist;
va_start(arglist, fmt); va_start(arglist, fmt);
@ -70,7 +71,8 @@ void error(const char *fmt, ...) {
exit(1); exit(1);
} }
int apm_change(apm_info *cur) { int apm_change(apm_info *cur)
{
static int ac_line_status = 0, battery_status = 0, battery_flags = 0, static int ac_line_status = 0, battery_status = 0, battery_flags = 0,
battery_percentage = 0, battery_time = 0, using_minutes = 0; battery_percentage = 0, battery_time = 0, using_minutes = 0;
@ -92,21 +94,22 @@ int apm_change(apm_info *cur) {
} }
/* Calculate battery estimate */ /* Calculate battery estimate */
void estimate_timeleft(apm_info *cur_info) { void estimate_timeleft(apm_info *cur_info)
{
/* Time of the last estimate */ /* Time of the last estimate */
static time_t estimate_time = 0; static time_t estimate_time;
/* Estimated time left */ /* Estimated time left */
static time_t estimate = 0; static time_t estimate;
/* Time when we last noticed a battery level change */ /* Time when we last noticed a battery level change */
static time_t battery_change_time = 0; static time_t battery_change_time;
/* The previous estimation we had before the battery level changed */ /* The previous estimation we had before the battery level changed */
static time_t prev_estimate = 0; static time_t prev_estimate;
/* Percentage at the last estimate */ /* Percentage at the last estimate */
static short percent = 0; static short percent;
/* Where we charging or discharging the last time we were called? */ /* Where we charging or discharging the last time we were called? */
static short was_charging = 1; static short was_charging = 1;
/* Have we made a guess lately? */ /* Have we made a guess lately? */
static short guessed_lately = 0; static short guessed_lately;
time_t t; time_t t;
int interval; int interval;
@ -167,7 +170,8 @@ estim_values:
} }
/* Load up the images this program uses. */ /* Load up the images this program uses. */
void load_images() { void load_images(void)
{
int x; int x;
char fn[128]; /* enough? */ char fn[128]; /* enough? */
@ -176,25 +180,23 @@ void load_images() {
if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) { if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) {
/* Check in current direcotry for fallback. */ /* Check in current direcotry for fallback. */
sprintf(fn, "%s.xpm", image_info[x].filename); sprintf(fn, "%s.xpm", image_info[x].filename);
if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) { if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL))
error("Failed to load %s\n", fn); error("Failed to load %s\n", fn);
} }
} }
} }
}
void load_audio() { void load_audio(void)
{
int fd; int fd;
struct stat s; struct stat s;
crit_audio = NULL; crit_audio = NULL;
if (crit_audio_fn == NULL) { if (crit_audio_fn == NULL)
return; return;
}
fd = open(crit_audio_fn, 0); fd = open(crit_audio_fn, 0);
if (fd == -1) { if (fd == -1)
error("unable to open audio file"); error("unable to open audio file");
}
if (fstat(fd, &s) == 0) { if (fstat(fd, &s) == 0) {
crit_audio_size = s.st_size; crit_audio_size = s.st_size;
crit_audio = malloc(crit_audio_size); crit_audio = malloc(crit_audio_size);
@ -225,7 +227,8 @@ char *replace_str(const char *str, const char *old, const char *new)
} else } else
retlen = strlen(str); retlen = strlen(str);
if ((ret = malloc(retlen + 1)) == NULL) ret = malloc(retlen + 1);
if (!ret)
return NULL; return NULL;
for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) { for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) {
@ -241,7 +244,8 @@ char *replace_str(const char *str, const char *old, const char *new)
return ret; return ret;
} }
void cmd_crit(const char *cmd, int percentage, int time) { void cmd_crit(const char *cmd, int percentage, int time)
{
char prc_str[255] = ""; char prc_str[255] = "";
char min_str[255] = ""; char min_str[255] = "";
char sec_str[255] = ""; char sec_str[255] = "";
@ -281,11 +285,11 @@ void cmd_crit(const char *cmd, int percentage, int time) {
} }
/* Returns the display to run on (or NULL for default). */ /* Returns the display to run on (or NULL for default). */
char *parse_commandline(int argc, char *argv[]) { char *parse_commandline(int argc, char *argv[])
{
int c = 0; int c = 0;
char *ret = NULL; char *ret = NULL;
char *s; char *s;
extern char *optarg;
while (c != -1) { while (c != -1) {
c = getopt(argc, argv, "hd:g:if:b:w:c:l:es:a:x:"); c = getopt(argc, argv, "hd:g:if:b:w:c:l:es:a:x:");
@ -313,13 +317,12 @@ char *parse_commandline(int argc, char *argv[]) {
s = strtok(optarg, "+"); s = strtok(optarg, "+");
if (s) { if (s) {
pos[0] = atoi(s); pos[0] = atoi(s);
if ((s = strtok(NULL, "+")) != NULL) { s = strtok(NULL, "+");
if (s)
pos[1] = atoi(s); pos[1] = atoi(s);
} else
else {
pos[0] = 0; pos[0] = 0;
} }
}
break; break;
case 'i': case 'i':
initial_state = IconicState; initial_state = IconicState;
@ -356,7 +359,8 @@ char *parse_commandline(int argc, char *argv[]) {
} }
/* Sets up the window and icon and all the nasty X stuff. */ /* Sets up the window and icon and all the nasty X stuff. */
void make_window(char *display_name, int argc, char *argv[]) { void make_window(char *display_name, int argc, char *argv[])
{
XClassHint classhint; XClassHint classhint;
char *wname = argv[0]; char *wname = argv[0];
XTextProperty name; XTextProperty name;
@ -367,7 +371,8 @@ void make_window(char *display_name, int argc, char *argv[]) {
Pixel back_pix, fore_pix; Pixel back_pix, fore_pix;
Pixmap pixmask; Pixmap pixmask;
if (!(display = XOpenDisplay(display_name))) display = XOpenDisplay(display_name);
if (!display)
error("can't open display %s", XDisplayName(display_name)); error("can't open display %s", XDisplayName(display_name));
screen = DefaultScreen(display); screen = DefaultScreen(display);
@ -438,13 +443,16 @@ void make_window(char *display_name, int argc, char *argv[]) {
XMapWindow(display, win); XMapWindow(display, win);
} }
void flush_expose(Window w) { void flush_expose(Window w)
{
XEvent dummy; XEvent dummy;
while (XCheckTypedWindowEvent(display, w, Expose, &dummy)); while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
;
} }
void redraw_window() { void redraw_window(void)
{
XCopyArea(display, images[FACE], iconwin, NormalGC, 0, 0, XCopyArea(display, images[FACE], iconwin, NormalGC, 0, 0,
image_info[FACE].width, image_info[FACE].height, 0, 0); image_info[FACE].width, image_info[FACE].height, 0, 0);
flush_expose(iconwin); flush_expose(iconwin);
@ -458,7 +466,8 @@ void redraw_window() {
* located anywhere. * located anywhere.
*/ */
void copy_image(int image, int xoffset, int yoffset, void copy_image(int image, int xoffset, int yoffset,
int width, int height, int x, int y) { int width, int height, int x, int y)
{
XCopyArea(display, images[image], images[FACE], NormalGC, XCopyArea(display, images[image], images[FACE], NormalGC,
xoffset, yoffset, width, height, x, y); xoffset, yoffset, width, height, x, y);
} }
@ -468,22 +477,25 @@ void copy_image(int image, int xoffset, int yoffset,
* Note that 10 is passed for special characters `:' or `1' at the * Note that 10 is passed for special characters `:' or `1' at the
* end of the font. * end of the font.
*/ */
void draw_letter(int letter, int font, int x) { void draw_letter(int letter, int font, int x)
{
copy_image(font, image_info[font].charwidth * letter, 0, copy_image(font, image_info[font].charwidth * letter, 0,
image_info[font].charwidth, image_info[font].height, image_info[font].charwidth, image_info[font].height,
x, image_info[font].y); x, image_info[font].y);
} }
/* Display an image at its normal location. */ /* Display an image at its normal location. */
void draw_image(int image) { void draw_image(int image)
{
copy_image(image, 0, 0, copy_image(image, 0, 0,
image_info[image].width, image_info[image].height, image_info[image].width, image_info[image].height,
image_info[image].x, image_info[image].y); image_info[image].x, image_info[image].y);
} }
void recalc_window(apm_info cur_info) { void recalc_window(apm_info cur_info)
{
int time_left, hour_left, min_left, digit, x; int time_left, hour_left, min_left, digit, x;
static int blinked = 0; static int blinked;
/* Display if it's plugged in. */ /* Display if it's plugged in. */
switch (cur_info.ac_line_status) { switch (cur_info.ac_line_status) {
@ -515,12 +527,10 @@ void recalc_window(apm_info cur_info) {
} }
/* Show if the battery is charging. */ /* Show if the battery is charging. */
if (cur_info.battery_flags & BATTERY_FLAGS_CHARGING) { if (cur_info.battery_flags & BATTERY_FLAGS_CHARGING)
draw_image(CHARGING); draw_image(CHARGING);
} else
else {
draw_image(NOCHARGING); draw_image(NOCHARGING);
}
/* /*
* Display the percent left dial. This has the side effect of * Display the percent left dial. This has the side effect of
@ -552,8 +562,7 @@ void recalc_window(apm_info cur_info) {
draw_letter(digit, SMALLFONT, TENS_OFFSET); draw_letter(digit, SMALLFONT, TENS_OFFSET);
digit = cur_info.battery_percentage % 10; digit = cur_info.battery_percentage % 10;
draw_letter(digit, SMALLFONT, ONES_OFFSET); draw_letter(digit, SMALLFONT, ONES_OFFSET);
} } else {
else {
/* There is no battery, so we need to dim out the /* There is no battery, so we need to dim out the
* percent sign that is normally bright. */ * percent sign that is normally bright. */
draw_letter(10, SMALLFONT, PERCENT_OFFSET); draw_letter(10, SMALLFONT, PERCENT_OFFSET);
@ -586,22 +595,23 @@ void recalc_window(apm_info cur_info) {
redraw_window(); redraw_window();
} }
void snd_crit() { void snd_crit(void)
{
int audio, n; int audio, n;
if (crit_audio) { if (crit_audio) {
audio = open("/dev/audio", O_WRONLY); audio = open("/dev/audio", O_WRONLY);
if (audio >= 0) { if (audio >= 0) {
n = write(audio, crit_audio, crit_audio_size); n = write(audio, crit_audio, crit_audio_size);
if (n != crit_audio_size) { if (n != crit_audio_size)
fprintf(stderr, "write failed (%d/%d bytes)\n", n, crit_audio_size); fprintf(stderr, "write failed (%d/%d bytes)\n", n, crit_audio_size);
}
close(audio); close(audio);
} }
} }
} }
void alarmhandler(int sig) { void alarmhandler(int sig)
{
apm_info cur_info; apm_info cur_info;
int old_status; int old_status;
@ -609,8 +619,7 @@ void alarmhandler(int sig) {
if (use_upower) { if (use_upower) {
if (upower_read(1, &cur_info) != 0) if (upower_read(1, &cur_info) != 0)
error("Cannot read upower information."); error("Cannot read upower information.");
} } else if (use_acpi) {
else if (use_acpi) {
#else #else
if (use_acpi) { if (use_acpi) {
#endif #endif
@ -626,8 +635,7 @@ void alarmhandler(int sig) {
else if (!use_sonypi) { else if (!use_sonypi) {
if (apm_read(&cur_info) != 0) if (apm_read(&cur_info) != 0)
error("Cannot read APM information."); error("Cannot read APM information.");
} } else {
else {
if (sonypi_read(&cur_info) != 0) if (sonypi_read(&cur_info) != 0)
error("Cannot read sonypi information."); error("Cannot read sonypi information.");
} }
@ -659,8 +667,7 @@ void alarmhandler(int sig) {
if ((old_status == BATTERY_STATUS_HIGH) && if ((old_status == BATTERY_STATUS_HIGH) &&
(cur_info.battery_status == BATTERY_STATUS_LOW)) { (cur_info.battery_status == BATTERY_STATUS_LOW)) {
snd_crit(); snd_crit();
} } else if (cur_info.battery_status == BATTERY_STATUS_CRITICAL) {
else if (cur_info.battery_status == BATTERY_STATUS_CRITICAL) {
snd_crit(); snd_crit();
cmd_crit(crit_command, cur_info.battery_percentage, cmd_crit(crit_command, cur_info.battery_percentage,
cur_info.battery_time); cur_info.battery_time);
@ -669,7 +676,8 @@ void alarmhandler(int sig) {
alarm(delay); alarm(delay);
} }
void check_battery_num(int real, int requested) { void check_battery_num(int real, int requested)
{
if (requested > real || requested < 1) { if (requested > real || requested < 1) {
error("There %s only %i batter%s, and you asked for number %i.", error("There %s only %i batter%s, and you asked for number %i.",
real == 1 ? "is" : "are", real == 1 ? "is" : "are",
@ -679,7 +687,8 @@ void check_battery_num(int real, int requested) {
} }
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
make_window(parse_commandline(argc, argv), argc, argv); make_window(parse_commandline(argc, argv), argc, argv);
/* Check for APM support (returns 0 on success). */ /* Check for APM support (returns 0 on success). */
@ -696,9 +705,8 @@ int main(int argc, char *argv[]) {
} }
#endif #endif
#ifdef UPOWER #ifdef UPOWER
else if (upower_supported()) { else if (upower_supported())
use_upower = 1; use_upower = 1;
}
#endif #endif
/* Check for ACPI support. */ /* Check for ACPI support. */
else if (acpi_supported() && acpi_batt_count > 0) { else if (acpi_supported() && acpi_batt_count > 0) {
@ -706,15 +714,13 @@ int main(int argc, char *argv[]) {
use_acpi = 1; use_acpi = 1;
if (!delay) if (!delay)
delay = 3; /* slow interface! */ delay = 3; /* slow interface! */
} } else if (sonypi_supported()) {
else if (sonypi_supported()) {
use_sonypi = 1; use_sonypi = 1;
low_pct = 10; low_pct = 10;
critical_pct = 5; critical_pct = 5;
if (!delay) if (!delay)
delay = 1; delay = 1;
} } else {
else {
error("No APM, ACPI, UPOWER, HAL or SPIC support detected."); error("No APM, ACPI, UPOWER, HAL or SPIC support detected.");
} }