From 539ef9f52cdde803eb67f015c64792bce1656594 Mon Sep 17 00:00:00 2001 From: Doug Torrance Date: Sun, 5 Oct 2014 10:30:02 -0500 Subject: [PATCH] wmbattery: Consistent formatting in source. Modified source files to minimize warnings from checkpatch.pl in Window Maker source tree. --- wmbattery/acpi.c | 120 ++++++++------- wmbattery/acpi.h | 16 +- wmbattery/simplehal.c | 102 ++++++------- wmbattery/simplehal.h | 4 +- wmbattery/sonypi.c | 29 ++-- wmbattery/sonypi.h | 4 +- wmbattery/upower.c | 45 +++--- wmbattery/wmbattery.c | 348 +++++++++++++++++++++--------------------- wmbattery/wmbattery.h | 42 ++--- 9 files changed, 353 insertions(+), 357 deletions(-) diff --git a/wmbattery/acpi.c b/wmbattery/acpi.c index 39b662e..1e837d2 100644 --- a/wmbattery/acpi.c +++ b/wmbattery/acpi.c @@ -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 * 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 end; static char buf[1024]; fd = open(file, O_RDONLY); - if (fd == -1) return NULL; + if (fd == -1) + return NULL; end = read(fd, buf, sizeof(buf)); buf[end-1] = '\0'; 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, * 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; int ret = 0; do { ptr = strchr(buf, '\n'); if (!strmcmp(buf, key)) { - if ((ptr = strchr(buf, '='))) { + ptr = strchr(buf, '='); + if (ptr) { sscanf(ptr + 1, "%d", &ret); return ret; } 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, * 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; static char ret[256]; do { ptr = strchr(buf, '\n'); if (!strmcmp(buf, key)) { - if ((ptr = strchr(buf, '='))) { - if (sscanf(ptr + 1, "%255s", ret) == 1) { + ptr = strchr(buf, '='); + if (ptr) { + if (sscanf(ptr + 1, "%255s", ret) == 1) return ret; - } else { + else return NULL; - } } else { 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 * the slow, dumb way, fine for initialization or if only one value is needed * 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); - if (! buf) return NULL; + if (!buf) + return NULL; return scan_acpi_value(buf, key); } /* 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; s = get_acpi_value(acpi_batt_info[battery], acpi_labels[label_last_full_capacity]); - if (s == NULL) { + if (s == NULL) return 0; - } else { + else return atoi(s); - } } /* 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 **pb = (const char **)b; return strcasecmp((const char *)*pa, (const char *)*pb); @@ -166,11 +174,12 @@ int _acpi_compare_strings (const void *a, const void *b) { /* 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. * Returns the number of items found. */ -int find_items (char *itemname, char infoarray[ACPI_MAXITEM][128], - char statusarray[ACPI_MAXITEM][128]) { +int find_items(char *itemname, char infoarray[ACPI_MAXITEM][128], + char statusarray[ACPI_MAXITEM][128]) +{ DIR *dir; struct dirent *ent; - int num_devices=0; + int num_devices = 0; int i; char **devices = malloc(ACPI_MAXITEM * sizeof(char *)); @@ -199,7 +208,7 @@ int find_items (char *itemname, char infoarray[ACPI_MAXITEM][128], continue; } - devices[num_devices]=strdup(ent->d_name); + devices[num_devices] = strdup(ent->d_name); num_devices++; if (num_devices >= ACPI_MAXITEM) break; @@ -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. */ -int find_batteries(void) { +int find_batteries(void) +{ int i; acpi_batt_count = find_items(acpi_labels[label_battery], acpi_batt_info, acpi_batt_status); 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 * 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); return acpi_ac_count; } @@ -240,17 +251,19 @@ int find_ac_adapters(void) { #if ACPI_THERMAL /* Find thermal information sources, return the number found, and set * 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); 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 on_ac_power(void) +{ int 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]); if (online && atoi(online)) return 1; else @@ -261,23 +274,23 @@ int on_ac_power (void) { /* See if we have ACPI support and check version. Also find batteries and * ac power adapters. */ -int acpi_supported (void) { +int acpi_supported(void) +{ char *version; DIR *dir; int num; - if (!(dir = opendir(SYSFS_PATH))) { + dir = opendir(SYSFS_PATH); + if (!dir) return 0; - } closedir(dir); /* If kernel is 2.6.21 or newer, version is in /sys/module/acpi/parameters/acpica_version */ version = get_acpi_file("/sys/module/acpi/parameters/acpica_version"); - if (version == NULL) { + if (version == NULL) return 0; - } num = atoi(version); if (num < ACPI_VERSION) { fprintf(stderr, "ACPI subsystem %s too is old, consider upgrading to %i.\n", @@ -297,7 +310,8 @@ int acpi_supported (void) { #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) { +int acpi_read(int battery, apm_info *info) +{ char *buf, *state; if (acpi_batt_count == 0) { @@ -332,14 +346,12 @@ int acpi_read (int battery, apm_info *info) { if (rate) { /* time remaining = (current_capacity / discharge rate) */ info->battery_time = (float) pcap / (float) rate * 60; - } - else { + } else { char *rate_s = scan_acpi_value(buf, acpi_labels[label_present_rate]); - if (! rate_s) { + if (!rate_s) { /* Time remaining unknown. */ info->battery_time = 0; - } - else { + } else { /* a zero or unknown in the file; time * unknown so use a negative one to * indicate this */ @@ -355,31 +367,28 @@ int acpi_read (int battery, apm_info *info) { * because AC power might be on even if a * battery is discharging in some cases. */ 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->ac_line_status = 1; info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING; 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 info->battery_time = 0; if (abs(info->battery_time) < 0.5) info->battery_time = 0; - } - else if (state[0] == 'F') { /* full */ + } else if (state[0] == 'F') { /* full */ /* charged, on ac power */ info->battery_status = BATTERY_STATUS_HIGH; 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; /* Expensive ac power check used here * because AC power might be on even if a * battery is critical in some cases. */ 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(); int current = scan_acpi_num(buf, acpi_labels[label_present_rate]); if (info->ac_line_status) { @@ -387,16 +396,13 @@ int acpi_read (int battery, apm_info *info) { info->battery_status = BATTERY_STATUS_HIGH; else info->battery_status = BATTERY_STATUS_CHARGING; - } - else { + } else { info->battery_status = BATTERY_STATUS_CHARGING; } - } - else { + } else { fprintf(stderr, "unknown battery state: %s\n", state); } - } - else { + } else { /* Battery state unknown. */ 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 * buf below this point! */ 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 * capacity. Rescan for a new max capacity. */ find_batteries(); @@ -419,13 +424,11 @@ int acpi_read (int battery, apm_info *info) { info->battery_percentage = 100 * pcap / acpi_batt_capacity[battery]; if (info->battery_percentage > 100) info->battery_percentage = 100; - } - else { + } else { info->battery_percentage = -1; } - } - else { + } else { info->battery_percentage = 0; info->battery_time = 0; info->battery_status = BATTERY_STATUS_ABSENT; @@ -433,8 +436,7 @@ int acpi_read (int battery, apm_info *info) { if (acpi_batt_count == 0) { /* Where else would the power come from, eh? ;-) */ info->ac_line_status = 1; - } - else { + } else { /* Expensive ac power check. */ info->ac_line_status = on_ac_power(); } diff --git a/wmbattery/acpi.h b/wmbattery/acpi.h index 44e5ed6..ffe4278 100644 --- a/wmbattery/acpi.h +++ b/wmbattery/acpi.h @@ -4,10 +4,10 @@ /* Define ACPI_THERMAL to make the library support finding info about thermal * 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 1 +/* #define ACPI_APM 1 */ /* The lowest version of ACPI proc files supported. */ #define ACPI_VERSION 20011018 @@ -15,14 +15,14 @@ /* The number of acpi items of each class supported. */ #define ACPI_MAXITEM 8 -int acpi_supported (void); +int acpi_supported(void); #ifdef ACPI_APM -int acpi_read (int battery, apm_info *info); +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); +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; diff --git a/wmbattery/simplehal.c b/wmbattery/simplehal.c index b107a37..8647e4e 100644 --- a/wmbattery/simplehal.c +++ b/wmbattery/simplehal.c @@ -9,15 +9,16 @@ #include #include "apm.h" -static DBusConnection *dbus_ctx = NULL; -static LibHalContext *hal_ctx = NULL; +static DBusConnection *dbus_ctx; +static LibHalContext *hal_ctx; int num_ac_adapters = 0; int num_batteries = 0; char **ac_adapters = NULL; char **batteries = NULL; -int connect_hal (void) { +int connect_hal(void) +{ DBusError error; dbus_error_init(&error); @@ -28,7 +29,8 @@ int connect_hal (void) { LIBHAL_FREE_DBUS_ERROR(&error); 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"); LIBHAL_FREE_DBUS_ERROR(&error); return 0; @@ -52,11 +54,11 @@ int connect_hal (void) { return 1; } -int hal_ready (void) { +int hal_ready(void) +{ if (hal_ctx && dbus_connection_get_is_connected(dbus_ctx)) { return 1; - } - else { + } else { /* The messy business of reconnecting. * dbus's design is crap when it comes to reconnecting. * If dbus is down, can't actually close the connection to hal, @@ -72,57 +74,56 @@ 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; DBusError error; - if (! hal_ready()) { + if (!hal_ready()) return -1; - } dbus_error_init(&error); ret = libhal_device_get_property_int (hal_ctx, udi, key, &error); - if (! dbus_error_is_set (&error)) { + if (!dbus_error_is_set(&error)) { return ret; - } - else { - if (! optional) { + } else { + if (!optional) { fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n", error.name, error.message); } - dbus_error_free (&error); + dbus_error_free(&error); return -1; } } -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; DBusError error; - if (! hal_ready()) { + if (!hal_ready()) return -1; - } dbus_error_init(&error); - ret = libhal_device_get_property_bool (hal_ctx, udi, key, &error); + ret = libhal_device_get_property_bool(hal_ctx, udi, key, &error); - if (! dbus_error_is_set (&error)) { + if (!dbus_error_is_set(&error)) { return ret; - } - else { - if (! optional) { + } else { + if (!optional) { fprintf(stderr, "error: libhal_device_get_property_bool: %s: %s\n", error.name, error.message); } - dbus_error_free (&error); + dbus_error_free(&error); return -1; } } -void find_devices (void) { +void find_devices(void) +{ DBusError error; dbus_error_init(&error); @@ -131,57 +132,55 @@ void find_devices (void) { libhal_free_string_array(ac_adapters); ac_adapters = libhal_find_device_by_capability(hal_ctx, "ac_adapter", &num_ac_adapters, &error); - if (dbus_error_is_set (&error)) { - fprintf (stderr, "error: %s: %s\n", error.name, error.message); - LIBHAL_FREE_DBUS_ERROR (&error); + if (dbus_error_is_set(&error)) { + fprintf(stderr, "error: %s: %s\n", error.name, error.message); + LIBHAL_FREE_DBUS_ERROR(&error); } if (batteries) libhal_free_string_array(batteries); batteries = libhal_find_device_by_capability(hal_ctx, "battery", &num_batteries, &error); - if (dbus_error_is_set (&error)) { - fprintf (stderr, "error: %s: %s\n", error.name, error.message); - LIBHAL_FREE_DBUS_ERROR (&error); + if (dbus_error_is_set(&error)) { + fprintf(stderr, "error: %s: %s\n", error.name, error.message); + LIBHAL_FREE_DBUS_ERROR(&error); } } -int simplehal_supported (void) { - if (! connect_hal()) { +int simplehal_supported(void) +{ + if (!connect_hal()) { return 0; - } - else { + } else { find_devices(); return 1; } } /* Fill the passed apm_info struct. */ -int simplehal_read (int battery, apm_info *info) { +int simplehal_read(int battery, apm_info *info) +{ char *device; int i; /* Allow a battery that was not present before to appear. */ - if (battery > num_batteries) { + if (battery > num_batteries) find_devices(); - } info->battery_flags = 0; info->using_minutes = 0; - info->ac_line_status=0; - for (i = 0 ; i < num_ac_adapters && ! info->ac_line_status ; i++) { + info->ac_line_status = 0; + 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); - } if (battery > num_batteries) { info->battery_percentage = 0; info->battery_time = 0; info->battery_status = BATTERY_STATUS_ABSENT; return 0; - } - else { - device=batteries[battery-1]; + } else { + device = batteries[battery-1]; } if (get_hal_bool(device, "battery.present", 0) != 1) { @@ -199,23 +198,18 @@ int simplehal_read (int battery, apm_info *info) { info->battery_status = BATTERY_STATUS_CHARGING; /* charge_level.warning and charge_level.low are not * required to be available; this is good enough */ - if (info->battery_percentage < 1) { + if (info->battery_percentage < 1) info->battery_status = BATTERY_STATUS_CRITICAL; - } else if (info->battery_percentage < 10) { info->battery_status = BATTERY_STATUS_LOW; - } - } - else if (info->ac_line_status && - get_hal_bool(device, "battery.rechargeable.is_charging", 0) == 1) { + } else if (info->ac_line_status && + get_hal_bool(device, "battery.rechargeable.is_charging", 0) == 1) { info->battery_status = BATTERY_STATUS_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. */ info->battery_status = BATTERY_STATUS_HIGH; - } - else { + } else { fprintf(stderr, "unknown battery state\n"); } diff --git a/wmbattery/simplehal.h b/wmbattery/simplehal.h index 1f4cd2a..2a5b0ac 100644 --- a/wmbattery/simplehal.h +++ b/wmbattery/simplehal.h @@ -1,2 +1,2 @@ -int simplehal_supported (void); -int simplehal_read (int battery, apm_info *info); +int simplehal_supported(void); +int simplehal_read(int battery, apm_info *info); diff --git a/wmbattery/sonypi.c b/wmbattery/sonypi.c index ab9922f..c3190be 100644 --- a/wmbattery/sonypi.c +++ b/wmbattery/sonypi.c @@ -9,14 +9,17 @@ signed int spicfd = -1; -int sonypi_supported (void) { - if ((spicfd = open("/dev/sonypi", O_RDWR)) == -1) +int sonypi_supported(void) +{ + spicfd = open("/dev/sonypi", O_RDWR); + if (spicfd == -1) return 0; else return 1; } -inline int sonypi_ioctl(int ioctlno, void *param) { +inline int sonypi_ioctl(int ioctlno, void *param) +{ if (ioctl(spicfd, ioctlno, param) < 0) return 0; else @@ -25,35 +28,33 @@ inline int sonypi_ioctl(int ioctlno, void *param) { /* Read battery info from sonypi device and shove it into an apm_info * struct. */ -int sonypi_read (apm_info *info) { +int sonypi_read(apm_info *info) +{ __u8 batflags; __u16 cap, rem; int havebatt = 0; info->using_minutes = info->battery_flags = 0; - if (! sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags)) { + if (!sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags)) return 1; - } info->ac_line_status = (batflags & SONYPI_BFLAGS_AC) != 0; if (batflags & SONYPI_BFLAGS_B1) { - if (! sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap)) + if (!sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap)) return 1; - if (! sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem)) + if (!sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem)) return 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 * probably merge the two somehow.. */ - if (! sonypi_ioctl(SONYPI_IOCGBAT2CAP, &cap)) + if (!sonypi_ioctl(SONYPI_IOCGBAT2CAP, &cap)) return 1; - if (! sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem)) + if (!sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem)) return 1; havebatt = 1; - } - else { + } else { info->battery_percentage = 0; info->battery_status = BATTERY_STATUS_ABSENT; } diff --git a/wmbattery/sonypi.h b/wmbattery/sonypi.h index 03650af..9e132e6 100644 --- a/wmbattery/sonypi.h +++ b/wmbattery/sonypi.h @@ -1,5 +1,5 @@ -int sonypi_supported (void); -int sonypi_read (apm_info *info); +int sonypi_supported(void); +int sonypi_read(apm_info *info); /* There's no good place to get these constants, so I must define them * myself. */ diff --git a/wmbattery/upower.c b/wmbattery/upower.c index 0890908..857c7f4 100644 --- a/wmbattery/upower.c +++ b/wmbattery/upower.c @@ -28,7 +28,7 @@ static void get_devinfo(gpointer device, gpointer result) guint kind; gint64 time_to_empty; gint64 time_to_full; - struct context * ctx = result; + struct context *ctx = result; g_object_get(G_OBJECT(device), "percentage", &percentage, "online", &online, @@ -41,11 +41,10 @@ static void get_devinfo(gpointer device, gpointer result) if (ctx->current == ctx->needed) { ctx->percentage = (int)percentage; ctx->state = state; - if (time_to_empty) { + if (time_to_empty) ctx->time = time_to_empty; - } else { + else ctx->time = time_to_full; - } } ctx->current++; } else if (kind == UP_DEVICE_KIND_LINE_POWER) { @@ -53,15 +52,15 @@ static void get_devinfo(gpointer device, gpointer result) } } -int upower_supported (void) { - UpClient * up; +int upower_supported(void) +{ + UpClient *up; up = up_client_new(); if (!up) { return 0; - } - else { - GPtrArray * devices = up_client_get_devices(up); + } else { + GPtrArray *devices = up_client_get_devices(up); if (!devices) { g_object_unref(up); @@ -75,15 +74,15 @@ int upower_supported (void) { } /* Fill the passed apm_info struct. */ -int upower_read(int battery, apm_info *info) { - UpClient * up; - GPtrArray * devices = NULL; +int upower_read(int battery, apm_info *info) +{ + UpClient *up; + GPtrArray *devices = NULL; up = up_client_new(); - if (!up) { + if (!up) return -1; - } #if !UP_CHECK_VERSION(0, 9, 99) /* 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); - if (!devices) { + if (!devices) return -1; - } info->battery_flags = 0; info->using_minutes = 0; @@ -120,22 +118,17 @@ int upower_read(int battery, apm_info *info) { info->battery_status = BATTERY_STATUS_CHARGING; /* charge_level.warning and charge_level.low are not * required to be available; this is good enough */ - if (info->battery_percentage < 1) { + if (info->battery_percentage < 1) info->battery_status = BATTERY_STATUS_CRITICAL; - } - else if (info->battery_percentage < 10) { + else if (info->battery_percentage < 10) 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_flags = info->battery_flags | BATTERY_FLAGS_CHARGING; - } - else if (info->ac_line_status) { + } else if (info->ac_line_status) { /* Must be fully charged. */ info->battery_status = BATTERY_STATUS_HIGH; - } - else { + } else { fprintf(stderr, "unknown battery state\n"); } diff --git a/wmbattery/wmbattery.c b/wmbattery/wmbattery.c index 82ba4fe..ad99a07 100644 --- a/wmbattery/wmbattery.c +++ b/wmbattery/wmbattery.c @@ -58,7 +58,8 @@ int initial_state = WithdrawnState; signed int low_pct = -1; signed int critical_pct = -1; -void error(const char *fmt, ...) { +void error(const char *fmt, ...) +{ va_list arglist; va_start(arglist, fmt); @@ -70,7 +71,8 @@ void error(const char *fmt, ...) { 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, battery_percentage = 0, battery_time = 0, using_minutes = 0; @@ -92,21 +94,22 @@ int apm_change(apm_info *cur) { } /* Calculate battery estimate */ -void estimate_timeleft(apm_info *cur_info) { +void estimate_timeleft(apm_info *cur_info) +{ /* Time of the last estimate */ - static time_t estimate_time = 0; + static time_t estimate_time; /* Estimated time left */ - static time_t estimate = 0; + static time_t estimate; /* 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 */ - static time_t prev_estimate = 0; + static time_t prev_estimate; /* Percentage at the last estimate */ - static short percent = 0; + static short percent; /* Where we charging or discharging the last time we were called? */ static short was_charging = 1; /* Have we made a guess lately? */ - static short guessed_lately = 0; + static short guessed_lately; time_t t; int interval; @@ -117,13 +120,13 @@ void estimate_timeleft(apm_info *cur_info) { goto estim_values; if (( - /* AC is on and battery is not charging anymore or ... */ - (cur_info->ac_line_status == AC_LINE_STATUS_ON) && !is_charging - ) || + /* AC is on and battery is not charging anymore or ... */ + (cur_info->ac_line_status == AC_LINE_STATUS_ON) && !is_charging + ) || ( - /* ... the charging state has changed */ - is_charging ^ was_charging - )) { + /* ... the charging state has changed */ + is_charging ^ was_charging + )) { /* Reset counters */ battery_change_time = t; estimate = -1; @@ -167,34 +170,33 @@ estim_values: } /* Load up the images this program uses. */ -void load_images() { - int x; +void load_images(void) +{ + int x; char fn[128]; /* enough? */ - for(x=0; x < NUM_IMAGES; x++) { - sprintf(fn, "%s/%s.xpm", ICONDIR, image_info[x].filename); - if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) { - /* Check in current direcotry for fallback. */ - sprintf(fn, "%s.xpm", image_info[x].filename); - if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) { - error("Failed to load %s\n",fn); + for (x = 0; x < NUM_IMAGES; x++) { + sprintf(fn, "%s/%s.xpm", ICONDIR, image_info[x].filename); + if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) { + /* Check in current direcotry for fallback. */ + sprintf(fn, "%s.xpm", image_info[x].filename); + if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) + error("Failed to load %s\n", fn); } } - } } -void load_audio() { +void load_audio(void) +{ int fd; struct stat s; crit_audio = NULL; - if (crit_audio_fn == NULL) { + if (crit_audio_fn == NULL) return; - } fd = open(crit_audio_fn, 0); - if (fd == -1) { + if (fd == -1) error("unable to open audio file"); - } if (fstat(fd, &s) == 0) { crit_audio_size = s.st_size; crit_audio = malloc(crit_audio_size); @@ -225,7 +227,8 @@ char *replace_str(const char *str, const char *old, const char *new) } else retlen = strlen(str); - if ((ret = malloc(retlen + 1)) == NULL) + ret = malloc(retlen + 1); + if (!ret) return NULL; 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; } -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 min_str[255] = ""; char sec_str[255] = ""; @@ -281,21 +285,21 @@ void cmd_crit(const char *cmd, int percentage, int time) { } /* Returns the display to run on (or NULL for default). */ -char *parse_commandline(int argc, char *argv[]) { - int c=0; - char *ret=NULL; - char *s; - extern char *optarg; +char *parse_commandline(int argc, char *argv[]) +{ + int c = 0; + char *ret = NULL; + char *s; - while (c != -1) { - c=getopt(argc, argv, "hd:g:if:b:w:c:l:es:a:x:"); + while (c != -1) { + c = getopt(argc, argv, "hd:g:if:b:w:c:l:es:a:x:"); switch (c) { - case 'h': + case 'h': printf("Usage: wmbattery [options]\n"); - printf("\t-d \tselects target display\n"); - printf("\t-h\t\tdisplay this help\n"); - printf("\t-g +x+y\t\tposition of the window\n"); - printf("\t-i start \n"); + printf("\t-d \tselects target display\n"); + printf("\t-h\t\tdisplay this help\n"); + printf("\t-g +x+y\t\tposition of the window\n"); + printf("\t-i start\n"); printf("\t-b num\t\tnumber of battery to display\n"); printf("\t-w secs\t\tseconds between updates\n"); printf("\t-l percent\tlow percentage\n"); @@ -304,49 +308,48 @@ char *parse_commandline(int argc, char *argv[]) { printf("\t-s granularity\tignore fluctuations less than granularity%% (implies -e)\n"); printf("\t-a file\t\twhen critical send file to /dev/audio\n"); printf("\t-x command\twhen critical execute this command\n"); - exit(0); - break; - case 'd': - ret=strdup(optarg); - break; - case 'g': + exit(0); + break; + case 'd': + ret = strdup(optarg); + break; + case 'g': s = strtok(optarg, "+"); if (s) { - pos[0]=atoi(s); - if ((s = strtok(NULL, "+")) != NULL) { - pos[1]=atoi(s); - } - else { - pos[0]=0; - } + pos[0] = atoi(s); + s = strtok(NULL, "+"); + if (s) + pos[1] = atoi(s); + else + pos[0] = 0; } break; - case 'i': + case 'i': initial_state = IconicState; break; - case 'b': + case 'b': battnum = atoi(optarg); break; - case 'w': + case 'w': delay = atoi(optarg); break; - case 'l': + case 'l': low_pct = atoi(optarg); break; - case 'c': + case 'c': critical_pct = atoi(optarg); break; - case 'e': + case 'e': always_estimate_remaining = 1; break; - case 's': + case 's': always_estimate_remaining = 1; granularity_estimate_remaining = atoi(optarg); break; - case 'a': + case 'a': crit_audio_fn = strdup(optarg); break; - case 'x': + case 'x': crit_command = strdup(optarg); break; } @@ -356,22 +359,24 @@ char *parse_commandline(int argc, char *argv[]) { } /* 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; char *wname = argv[0]; XTextProperty name; XGCValues gcv; - int dummy=0, borderwidth = 1; + int dummy = 0, borderwidth = 1; XSizeHints sizehints; XWMHints wmhints; Pixel back_pix, fore_pix; Pixmap pixmask; - if (!(display = XOpenDisplay(display_name))) - error("can't open display %s",XDisplayName(display_name)); + display = XOpenDisplay(display_name); + if (!display) + error("can't open display %s", XDisplayName(display_name)); - screen=DefaultScreen(display); - root=RootWindow(display, screen); + screen = DefaultScreen(display); + root = RootWindow(display, screen); /* Create window. */ sizehints.flags = USSize | USPosition; @@ -401,7 +406,7 @@ void make_window(char *display_name, int argc, char *argv[]) { classhint.res_class = wname; XSetClassHint(display, win, &classhint); - if (! XStringListToTextProperty(&wname, 1, &name)) + if (!XStringListToTextProperty(&wname, 1, &name)) error("Can't allocate window name."); XSetWMName(display, win, &name); @@ -415,7 +420,7 @@ void make_window(char *display_name, int argc, char *argv[]) { &gcv); pixmask = XCreateBitmapFromData(display, win, mask_bits, - mask_width,mask_height); + mask_width, mask_height); XShapeCombineMask(display, win, ShapeBounding, 0, 0, pixmask, ShapeSet); XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0, @@ -427,7 +432,7 @@ void make_window(char *display_name, int argc, char *argv[]) { wmhints.icon_y = sizehints.y; wmhints.window_group = win; wmhints.flags = StateHint | IconWindowHint | - IconPositionHint | WindowGroupHint; + IconPositionHint | WindowGroupHint; XSetWMHints(display, win, &wmhints); XSetCommand(display, win, argv, argc); @@ -438,18 +443,21 @@ void make_window(char *display_name, int argc, char *argv[]) { XMapWindow(display, win); } -void flush_expose(Window w) { +void flush_expose(Window w) +{ 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, - image_info[FACE].width, image_info[FACE].height, 0,0); + image_info[FACE].width, image_info[FACE].height, 0, 0); flush_expose(iconwin); XCopyArea(display, images[FACE], win, 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(win); } @@ -458,9 +466,10 @@ void redraw_window() { * located anywhere. */ 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, - xoffset, yoffset, width, height, x, y); + xoffset, yoffset, width, height, x, y); } /* @@ -468,98 +477,98 @@ void copy_image(int image, int xoffset, int yoffset, * Note that 10 is passed for special characters `:' or `1' at the * 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, image_info[font].charwidth, image_info[font].height, x, image_info[font].y); } /* Display an image at its normal location. */ -void draw_image(int image) { - copy_image(image, 0, 0, +void draw_image(int image) +{ + copy_image(image, 0, 0, image_info[image].width, image_info[image].height, 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; - static int blinked = 0; + static int blinked; /* Display if it's plugged in. */ - switch (cur_info.ac_line_status) { - case AC_LINE_STATUS_ON: + switch (cur_info.ac_line_status) { + case AC_LINE_STATUS_ON: draw_image(PLUGGED); break; - default: + default: draw_image(UNPLUGGED); } - /* Display the appropriate color battery. */ - switch (cur_info.battery_status) { - case BATTERY_STATUS_HIGH: - case BATTERY_STATUS_CHARGING: + /* Display the appropriate color battery. */ + switch (cur_info.battery_status) { + case BATTERY_STATUS_HIGH: + case BATTERY_STATUS_CHARGING: draw_image(BATTERY_HIGH); break; - case BATTERY_STATUS_LOW: + case BATTERY_STATUS_LOW: draw_image(BATTERY_LOW); break; - case BATTERY_STATUS_CRITICAL: /* blinking red battery */ + case BATTERY_STATUS_CRITICAL: /* blinking red battery */ if (blinked) draw_image(BATTERY_CRITICAL); else draw_image(BATTERY_BLINK); - blinked=!blinked; + blinked = !blinked; break; - default: + default: draw_image(BATTERY_NONE); - } - - /* Show if the battery is charging. */ - if (cur_info.battery_flags & BATTERY_FLAGS_CHARGING) { - draw_image(CHARGING); } - else { - draw_image(NOCHARGING); - } - /* - * Display the percent left dial. This has the side effect of - * clearing the time left field. - */ - x=DIAL_MULTIPLIER * cur_info.battery_percentage; - if (x >= 0) { + /* Show if the battery is charging. */ + if (cur_info.battery_flags & BATTERY_FLAGS_CHARGING) + draw_image(CHARGING); + else + draw_image(NOCHARGING); + + /* + * Display the percent left dial. This has the side effect of + * clearing the time left field. + */ + x = DIAL_MULTIPLIER * cur_info.battery_percentage; + if (x >= 0) { /* Start by displaying bright on the dial. */ copy_image(DIAL_BRIGHT, 0, 0, x, image_info[DIAL_BRIGHT].height, image_info[DIAL_BRIGHT].x, image_info[DIAL_BRIGHT].y); - } - /* Now display dim on the remainder of the dial. */ - copy_image(DIAL_DIM, x, 0, + } + /* Now display dim on the remainder of the dial. */ + copy_image(DIAL_DIM, x, 0, image_info[DIAL_DIM].width - x, image_info[DIAL_DIM].height, image_info[DIAL_DIM].x + x, image_info[DIAL_DIM].y); /* Show percent remaining */ - if (cur_info.battery_percentage >= 0) { - digit = cur_info.battery_percentage / 10; - if (digit == 10) { - /* 11 is the `1' for the hundreds place. */ - draw_letter(11,SMALLFONT,HUNDREDS_OFFSET); - digit=0; + if (cur_info.battery_percentage >= 0) { + digit = cur_info.battery_percentage / 10; + if (digit == 10) { + /* 11 is the `1' for the hundreds place. */ + draw_letter(11, SMALLFONT, HUNDREDS_OFFSET); + digit = 0; } - draw_letter(digit,SMALLFONT,TENS_OFFSET); + draw_letter(digit, SMALLFONT, TENS_OFFSET); digit = cur_info.battery_percentage % 10; - draw_letter(digit,SMALLFONT,ONES_OFFSET); - } - else { - /* There is no battery, so we need to dim out the + draw_letter(digit, SMALLFONT, ONES_OFFSET); + } else { + /* There is no battery, so we need to dim out the * percent sign that is normally bright. */ - draw_letter(10,SMALLFONT,PERCENT_OFFSET); + draw_letter(10, SMALLFONT, PERCENT_OFFSET); } - /* Show time left */ + /* Show time left */ /* A negative number means that it is unknown. Dim the field. */ if (cur_info.battery_time < 0) { @@ -568,40 +577,41 @@ void recalc_window(apm_info cur_info) { return; } - if (cur_info.using_minutes) - time_left = cur_info.battery_time; - else - time_left = cur_info.battery_time / 60; - hour_left = time_left / 60; - min_left = time_left % 60; - digit = hour_left / 10; - draw_letter(digit,BIGFONT,HOURS_TENS_OFFSET); - digit = hour_left % 10; - draw_letter(digit,BIGFONT,HOURS_ONES_OFFSET); - digit = min_left / 10; - draw_letter(digit,BIGFONT,MINUTES_TENS_OFFSET); - digit = min_left % 10; - draw_letter(digit,BIGFONT,MINUTES_ONES_OFFSET); + if (cur_info.using_minutes) + time_left = cur_info.battery_time; + else + time_left = cur_info.battery_time / 60; + hour_left = time_left / 60; + min_left = time_left % 60; + digit = hour_left / 10; + draw_letter(digit, BIGFONT, HOURS_TENS_OFFSET); + digit = hour_left % 10; + draw_letter(digit, BIGFONT, HOURS_ONES_OFFSET); + digit = min_left / 10; + draw_letter(digit, BIGFONT, MINUTES_TENS_OFFSET); + digit = min_left % 10; + draw_letter(digit, BIGFONT, MINUTES_ONES_OFFSET); redraw_window(); } -void snd_crit() { +void snd_crit(void) +{ int audio, n; if (crit_audio) { audio = open("/dev/audio", O_WRONLY); if (audio >= 0) { 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); - } close(audio); } } } -void alarmhandler(int sig) { +void alarmhandler(int sig) +{ apm_info cur_info; int old_status; @@ -609,8 +619,7 @@ void alarmhandler(int sig) { if (use_upower) { if (upower_read(1, &cur_info) != 0) error("Cannot read upower information."); - } - else if (use_acpi) { + } else if (use_acpi) { #else if (use_acpi) { #endif @@ -623,11 +632,10 @@ void alarmhandler(int sig) { error("Cannot read HAL information."); } #endif - else if (! use_sonypi) { + else if (!use_sonypi) { if (apm_read(&cur_info) != 0) error("Cannot read APM information."); - } - else { + } else { if (sonypi_read(&cur_info) != 0) error("Cannot read sonypi information."); } @@ -659,8 +667,7 @@ void alarmhandler(int sig) { if ((old_status == BATTERY_STATUS_HIGH) && (cur_info.battery_status == BATTERY_STATUS_LOW)) { snd_crit(); - } - else if (cur_info.battery_status == BATTERY_STATUS_CRITICAL) { + } else if (cur_info.battery_status == BATTERY_STATUS_CRITICAL) { snd_crit(); cmd_crit(crit_command, cur_info.battery_percentage, cur_info.battery_time); @@ -669,57 +676,56 @@ void alarmhandler(int sig) { alarm(delay); } -void check_battery_num(int real, int requested) { +void check_battery_num(int real, int requested) +{ if (requested > real || requested < 1) { error("There %s only %i batter%s, and you asked for number %i.", - real == 1 ? "is" : "are", - real, - real == 1 ? "y" : "ies", - requested); + real == 1 ? "is" : "are", + real, + real == 1 ? "y" : "ies", + requested); } } -int main(int argc, char *argv[]) { - make_window(parse_commandline(argc, argv), argc ,argv); +int main(int argc, char *argv[]) +{ + make_window(parse_commandline(argc, argv), argc, argv); /* Check for APM support (returns 0 on success). */ if (apm_exists() == 0) { - if (! delay) + if (!delay) delay = 1; } #ifdef HAL /* Check for hal support. */ else if (simplehal_supported()) { use_simplehal = 1; - if (! delay) + if (!delay) delay = 2; } #endif #ifdef UPOWER - else if (upower_supported()) { + else if (upower_supported()) use_upower = 1; - } #endif /* Check for ACPI support. */ else if (acpi_supported() && acpi_batt_count > 0) { check_battery_num(acpi_batt_count, battnum); use_acpi = 1; - if (! delay) + if (!delay) delay = 3; /* slow interface! */ - } - else if (sonypi_supported()) { + } else if (sonypi_supported()) { use_sonypi = 1; low_pct = 10; critical_pct = 5; - if (! delay) + if (!delay) delay = 1; - } - else { + } else { error("No APM, ACPI, UPOWER, HAL or SPIC support detected."); } load_images(); - load_audio(); + load_audio(); signal(SIGALRM, alarmhandler); alarmhandler(SIGALRM); diff --git a/wmbattery/wmbattery.h b/wmbattery/wmbattery.h index 1453462..82369b4 100644 --- a/wmbattery/wmbattery.h +++ b/wmbattery/wmbattery.h @@ -3,16 +3,16 @@ typedef struct { Pixmap pixmap; Pixmap mask; - XpmAttributes attributes; + XpmAttributes attributes; } XpmIcon; typedef struct image_info_type { - const char* filename; - const int width; - const int height; - const int x; - const int y; - const int charwidth; + const char *filename; + const int width; + const int height; + const int x; + const int y; + const int charwidth; } image_info_type; /* Assign reference numbers to all images that are loaded. */ @@ -38,20 +38,20 @@ typedef struct image_info_type { * plus the size of the image, where to draw it on the icon, etc */ static struct image_info_type image_info[] = { - {"smallfont",7,67,0,45,6}, - {"bigfont",9,73,0,23,7}, - {"battery_high",25,13,33,42,0}, - {"battery_medium",25,13,33,42,0}, - {"battery_low",25,13,33,42,0}, - {"battery_none",25,13,33,42,0}, - {"battery_blink",25,13,33,42,0}, - {"unplugged",10,8,6,45,0}, - {"plugged",10,8,6,45,0}, - {"nocharging",15,9,17,43,0}, - {"charging",15,9,17,43,0}, - {"dial_bright",56,31,4,4,0}, - {"dial_dim",56,31,4,4,0}, - {"face",64,64,0,0,0}, + {"smallfont", 7, 67, 0, 45, 6}, + {"bigfont", 9, 73, 0, 23, 7}, + {"battery_high", 25, 13, 33, 42, 0}, + {"battery_medium", 25, 13, 33, 42, 0}, + {"battery_low", 25, 13, 33, 42, 0}, + {"battery_none", 25, 13, 33, 42, 0}, + {"battery_blink", 25, 13, 33, 42, 0}, + {"unplugged", 10, 8, 6, 45, 0}, + {"plugged", 10, 8, 6, 45, 0}, + {"nocharging", 15, 9, 17, 43, 0}, + {"charging", 15, 9, 17, 43, 0}, + {"dial_bright", 56, 31, 4, 4, 0}, + {"dial_dim", 56, 31, 4, 4, 0}, + {"face", 64, 64, 0, 0, 0}, }; #define DIAL_MULTIPLIER 0.56