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
* 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);
@ -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.
* Returns the number of items found. */
int find_items(char *itemname, char infoarray[ACPI_MAXITEM][128],
char statusarray[ACPI_MAXITEM][128]) {
char statusarray[ACPI_MAXITEM][128])
{
DIR *dir;
struct dirent *ent;
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. */
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,14 +251,16 @@ 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]);
@ -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) {
/* 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();
}

View file

@ -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

View file

@ -9,15 +9,16 @@
#include <libhal.h>
#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,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;
DBusError error;
if (! hal_ready()) {
if (!hal_ready())
return -1;
}
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)) {
return ret;
}
else {
} else {
if (!optional) {
fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n",
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;
DBusError error;
if (! hal_ready()) {
if (!hal_ready())
return -1;
}
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)) {
return ret;
}
else {
} else {
if (!optional) {
fprintf(stderr, "error: libhal_device_get_property_bool: %s: %s\n",
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;
dbus_error_init(&error);
@ -146,41 +147,39 @@ void find_devices (void) {
}
}
int simplehal_supported (void) {
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++) {
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 {
} else {
device = batteries[battery-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 &&
} 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");
}

View file

@ -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,16 +28,16 @@ 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) {
@ -43,8 +46,7 @@ int sonypi_read (apm_info *info) {
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))
@ -52,8 +54,7 @@ int sonypi_read (apm_info *info) {
if (!sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem))
return 1;
havebatt = 1;
}
else {
} else {
info->battery_percentage = 0;
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) {
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) {
ctx->ac |= online;
}
}
int upower_supported (void) {
int upower_supported(void)
{
UpClient *up;
up = up_client_new();
if (!up) {
return 0;
}
else {
} else {
GPtrArray *devices = up_client_get_devices(up);
if (!devices) {
@ -75,15 +74,15 @@ int upower_supported (void) {
}
/* Fill the passed apm_info struct. */
int upower_read(int battery, apm_info *info) {
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");
}

View file

@ -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;
@ -167,7 +170,8 @@ estim_values:
}
/* Load up the images this program uses. */
void load_images() {
void load_images(void)
{
int x;
char fn[128]; /* enough? */
@ -176,25 +180,23 @@ void load_images() {
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)) {
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,11 +285,11 @@ 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[]) {
char *parse_commandline(int argc, char *argv[])
{
int c = 0;
char *ret = NULL;
char *s;
extern char *optarg;
while (c != -1) {
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, "+");
if (s) {
pos[0] = atoi(s);
if ((s = strtok(NULL, "+")) != NULL) {
s = strtok(NULL, "+");
if (s)
pos[1] = atoi(s);
}
else {
else
pos[0] = 0;
}
}
break;
case 'i':
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. */
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;
@ -367,7 +371,8 @@ void make_window(char *display_name, int argc, char *argv[]) {
Pixel back_pix, fore_pix;
Pixmap pixmask;
if (!(display = XOpenDisplay(display_name)))
display = XOpenDisplay(display_name);
if (!display)
error("can't open display %s", XDisplayName(display_name));
screen = DefaultScreen(display);
@ -438,13 +443,16 @@ 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);
flush_expose(iconwin);
@ -458,7 +466,8 @@ 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);
}
@ -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
* 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) {
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) {
@ -515,12 +527,10 @@ void recalc_window(apm_info cur_info) {
}
/* 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);
}
else {
else
draw_image(NOCHARGING);
}
/*
* 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);
digit = cur_info.battery_percentage % 10;
draw_letter(digit, SMALLFONT, ONES_OFFSET);
}
else {
} else {
/* There is no battery, so we need to dim out the
* percent sign that is normally bright. */
draw_letter(10, SMALLFONT, PERCENT_OFFSET);
@ -586,22 +595,23 @@ void recalc_window(apm_info cur_info) {
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
@ -626,8 +635,7 @@ void alarmhandler(int sig) {
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,7 +676,8 @@ 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",
@ -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);
/* Check for APM support (returns 0 on success). */
@ -696,9 +705,8 @@ int main(int argc, char *argv[]) {
}
#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) {
@ -706,15 +714,13 @@ int main(int argc, char *argv[]) {
use_acpi = 1;
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)
delay = 1;
}
else {
} else {
error("No APM, ACPI, UPOWER, HAL or SPIC support detected.");
}