wmbattery: Consistent formatting in source.
Modified source files to minimize warnings from checkpatch.pl in Window Maker source tree.
This commit is contained in:
parent
8e5aa5c7e4
commit
539ef9f52c
120
wmbattery/acpi.c
120
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
|
/* 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);
|
||||||
|
@ -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
|
/* 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.
|
* 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;
|
||||||
int i;
|
int i;
|
||||||
char **devices = malloc(ACPI_MAXITEM * sizeof(char *));
|
char **devices = malloc(ACPI_MAXITEM * sizeof(char *));
|
||||||
|
|
||||||
|
@ -199,7 +208,7 @@ int find_items (char *itemname, char infoarray[ACPI_MAXITEM][128],
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
devices[num_devices]=strdup(ent->d_name);
|
devices[num_devices] = strdup(ent->d_name);
|
||||||
num_devices++;
|
num_devices++;
|
||||||
if (num_devices >= ACPI_MAXITEM)
|
if (num_devices >= ACPI_MAXITEM)
|
||||||
break;
|
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. */
|
/* 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,17 +251,19 @@ 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]);
|
||||||
if (online && atoi(online))
|
if (online && atoi(online))
|
||||||
return 1;
|
return 1;
|
||||||
else
|
else
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
@ -15,14 +15,14 @@
|
||||||
/* The number of acpi items of each class supported. */
|
/* The number of acpi items of each class supported. */
|
||||||
#define ACPI_MAXITEM 8
|
#define ACPI_MAXITEM 8
|
||||||
|
|
||||||
int acpi_supported (void);
|
int acpi_supported(void);
|
||||||
#ifdef ACPI_APM
|
#ifdef ACPI_APM
|
||||||
int acpi_read (int battery, apm_info *info);
|
int acpi_read(int battery, apm_info *info);
|
||||||
#endif
|
#endif
|
||||||
char *get_acpi_file (const char *file);
|
char *get_acpi_file(const char *file);
|
||||||
int scan_acpi_num (const char *buf, const char *key);
|
int scan_acpi_num(const char *buf, const char *key);
|
||||||
char *scan_acpi_value (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_value(const char *file, const char *key);
|
||||||
int get_acpi_batt_capacity(int battery);
|
int get_acpi_batt_capacity(int battery);
|
||||||
|
|
||||||
extern int acpi_batt_count;
|
extern int acpi_batt_count;
|
||||||
|
|
|
@ -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,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;
|
int ret;
|
||||||
DBusError error;
|
DBusError error;
|
||||||
|
|
||||||
if (! hal_ready()) {
|
if (!hal_ready())
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
dbus_error_init(&error);
|
dbus_error_init(&error);
|
||||||
|
|
||||||
ret = libhal_device_get_property_int (hal_ctx, udi, key, &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;
|
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);
|
||||||
}
|
}
|
||||||
dbus_error_free (&error);
|
dbus_error_free(&error);
|
||||||
return -1;
|
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;
|
int ret;
|
||||||
DBusError error;
|
DBusError error;
|
||||||
|
|
||||||
if (! hal_ready()) {
|
if (!hal_ready())
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
dbus_error_init(&error);
|
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;
|
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);
|
||||||
}
|
}
|
||||||
dbus_error_free (&error);
|
dbus_error_free(&error);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void find_devices (void) {
|
void find_devices(void)
|
||||||
|
{
|
||||||
DBusError error;
|
DBusError error;
|
||||||
|
|
||||||
dbus_error_init(&error);
|
dbus_error_init(&error);
|
||||||
|
@ -131,57 +132,55 @@ void find_devices (void) {
|
||||||
libhal_free_string_array(ac_adapters);
|
libhal_free_string_array(ac_adapters);
|
||||||
ac_adapters = libhal_find_device_by_capability(hal_ctx, "ac_adapter",
|
ac_adapters = libhal_find_device_by_capability(hal_ctx, "ac_adapter",
|
||||||
&num_ac_adapters, &error);
|
&num_ac_adapters, &error);
|
||||||
if (dbus_error_is_set (&error)) {
|
if (dbus_error_is_set(&error)) {
|
||||||
fprintf (stderr, "error: %s: %s\n", error.name, error.message);
|
fprintf(stderr, "error: %s: %s\n", error.name, error.message);
|
||||||
LIBHAL_FREE_DBUS_ERROR (&error);
|
LIBHAL_FREE_DBUS_ERROR(&error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (batteries)
|
if (batteries)
|
||||||
libhal_free_string_array(batteries);
|
libhal_free_string_array(batteries);
|
||||||
batteries = libhal_find_device_by_capability(hal_ctx, "battery",
|
batteries = libhal_find_device_by_capability(hal_ctx, "battery",
|
||||||
&num_batteries, &error);
|
&num_batteries, &error);
|
||||||
if (dbus_error_is_set (&error)) {
|
if (dbus_error_is_set(&error)) {
|
||||||
fprintf (stderr, "error: %s: %s\n", error.name, error.message);
|
fprintf(stderr, "error: %s: %s\n", error.name, error.message);
|
||||||
LIBHAL_FREE_DBUS_ERROR (&error);
|
LIBHAL_FREE_DBUS_ERROR(&error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (get_hal_bool(device, "battery.present", 0) != 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;
|
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 &&
|
||||||
}
|
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_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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
int simplehal_supported (void);
|
int simplehal_supported(void);
|
||||||
int simplehal_read (int battery, apm_info *info);
|
int simplehal_read(int battery, apm_info *info);
|
||||||
|
|
|
@ -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,35 +28,33 @@ 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) {
|
||||||
if (! sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap))
|
if (!sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap))
|
||||||
return 1;
|
return 1;
|
||||||
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))
|
||||||
return 1;
|
return 1;
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
int sonypi_supported (void);
|
int sonypi_supported(void);
|
||||||
int sonypi_read (apm_info *info);
|
int sonypi_read(apm_info *info);
|
||||||
|
|
||||||
/* There's no good place to get these constants, so I must define them
|
/* There's no good place to get these constants, so I must define them
|
||||||
* myself. */
|
* myself. */
|
||||||
|
|
|
@ -28,7 +28,7 @@ static void get_devinfo(gpointer device, gpointer result)
|
||||||
guint kind;
|
guint kind;
|
||||||
gint64 time_to_empty;
|
gint64 time_to_empty;
|
||||||
gint64 time_to_full;
|
gint64 time_to_full;
|
||||||
struct context * ctx = result;
|
struct context *ctx = result;
|
||||||
|
|
||||||
g_object_get(G_OBJECT(device), "percentage", &percentage,
|
g_object_get(G_OBJECT(device), "percentage", &percentage,
|
||||||
"online", &online,
|
"online", &online,
|
||||||
|
@ -41,11 +41,10 @@ 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) {
|
||||||
|
@ -53,15 +52,15 @@ static void get_devinfo(gpointer device, gpointer result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
g_object_unref(up);
|
g_object_unref(up);
|
||||||
|
@ -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;
|
{
|
||||||
GPtrArray * devices = NULL;
|
UpClient *up;
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -117,13 +120,13 @@ void estimate_timeleft(apm_info *cur_info) {
|
||||||
goto estim_values;
|
goto estim_values;
|
||||||
|
|
||||||
if ((
|
if ((
|
||||||
/* AC is on and battery is not charging anymore or ... */
|
/* AC is on and battery is not charging anymore or ... */
|
||||||
(cur_info->ac_line_status == AC_LINE_STATUS_ON) && !is_charging
|
(cur_info->ac_line_status == AC_LINE_STATUS_ON) && !is_charging
|
||||||
) ||
|
) ||
|
||||||
(
|
(
|
||||||
/* ... the charging state has changed */
|
/* ... the charging state has changed */
|
||||||
is_charging ^ was_charging
|
is_charging ^ was_charging
|
||||||
)) {
|
)) {
|
||||||
/* Reset counters */
|
/* Reset counters */
|
||||||
battery_change_time = t;
|
battery_change_time = t;
|
||||||
estimate = -1;
|
estimate = -1;
|
||||||
|
@ -167,34 +170,33 @@ 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? */
|
||||||
|
|
||||||
for(x=0; x < NUM_IMAGES; x++) {
|
for (x = 0; x < NUM_IMAGES; x++) {
|
||||||
sprintf(fn, "%s/%s.xpm", ICONDIR, image_info[x].filename);
|
sprintf(fn, "%s/%s.xpm", ICONDIR, image_info[x].filename);
|
||||||
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,21 +285,21 @@ 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;
|
{
|
||||||
char *ret=NULL;
|
int c = 0;
|
||||||
char *s;
|
char *ret = NULL;
|
||||||
extern char *optarg;
|
char *s;
|
||||||
|
|
||||||
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:");
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'h':
|
case 'h':
|
||||||
printf("Usage: wmbattery [options]\n");
|
printf("Usage: wmbattery [options]\n");
|
||||||
printf("\t-d <display>\tselects target display\n");
|
printf("\t-d <display>\tselects target display\n");
|
||||||
printf("\t-h\t\tdisplay this help\n");
|
printf("\t-h\t\tdisplay this help\n");
|
||||||
printf("\t-g +x+y\t\tposition of the window\n");
|
printf("\t-g +x+y\t\tposition of the window\n");
|
||||||
printf("\t-i start \n");
|
printf("\t-i start\n");
|
||||||
printf("\t-b num\t\tnumber of battery to display\n");
|
printf("\t-b num\t\tnumber of battery to display\n");
|
||||||
printf("\t-w secs\t\tseconds between updates\n");
|
printf("\t-w secs\t\tseconds between updates\n");
|
||||||
printf("\t-l percent\tlow percentage\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-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-a file\t\twhen critical send file to /dev/audio\n");
|
||||||
printf("\t-x command\twhen critical execute this command\n");
|
printf("\t-x command\twhen critical execute this command\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
ret=strdup(optarg);
|
ret = strdup(optarg);
|
||||||
break;
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
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, "+");
|
||||||
pos[1]=atoi(s);
|
if (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;
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
battnum = atoi(optarg);
|
battnum = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'w':
|
case 'w':
|
||||||
delay = atoi(optarg);
|
delay = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
low_pct = atoi(optarg);
|
low_pct = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
critical_pct = atoi(optarg);
|
critical_pct = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
always_estimate_remaining = 1;
|
always_estimate_remaining = 1;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
always_estimate_remaining = 1;
|
always_estimate_remaining = 1;
|
||||||
granularity_estimate_remaining = atoi(optarg);
|
granularity_estimate_remaining = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
crit_audio_fn = strdup(optarg);
|
crit_audio_fn = strdup(optarg);
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
crit_command = strdup(optarg);
|
crit_command = strdup(optarg);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -356,22 +359,24 @@ 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;
|
||||||
XGCValues gcv;
|
XGCValues gcv;
|
||||||
int dummy=0, borderwidth = 1;
|
int dummy = 0, borderwidth = 1;
|
||||||
XSizeHints sizehints;
|
XSizeHints sizehints;
|
||||||
XWMHints wmhints;
|
XWMHints wmhints;
|
||||||
Pixel back_pix, fore_pix;
|
Pixel back_pix, fore_pix;
|
||||||
Pixmap pixmask;
|
Pixmap pixmask;
|
||||||
|
|
||||||
if (!(display = XOpenDisplay(display_name)))
|
display = XOpenDisplay(display_name);
|
||||||
error("can't open display %s",XDisplayName(display_name));
|
if (!display)
|
||||||
|
error("can't open display %s", XDisplayName(display_name));
|
||||||
|
|
||||||
screen=DefaultScreen(display);
|
screen = DefaultScreen(display);
|
||||||
root=RootWindow(display, screen);
|
root = RootWindow(display, screen);
|
||||||
|
|
||||||
/* Create window. */
|
/* Create window. */
|
||||||
sizehints.flags = USSize | USPosition;
|
sizehints.flags = USSize | USPosition;
|
||||||
|
@ -401,7 +406,7 @@ void make_window(char *display_name, int argc, char *argv[]) {
|
||||||
classhint.res_class = wname;
|
classhint.res_class = wname;
|
||||||
XSetClassHint(display, win, &classhint);
|
XSetClassHint(display, win, &classhint);
|
||||||
|
|
||||||
if (! XStringListToTextProperty(&wname, 1, &name))
|
if (!XStringListToTextProperty(&wname, 1, &name))
|
||||||
error("Can't allocate window name.");
|
error("Can't allocate window name.");
|
||||||
|
|
||||||
XSetWMName(display, win, &name);
|
XSetWMName(display, win, &name);
|
||||||
|
@ -415,7 +420,7 @@ void make_window(char *display_name, int argc, char *argv[]) {
|
||||||
&gcv);
|
&gcv);
|
||||||
|
|
||||||
pixmask = XCreateBitmapFromData(display, win, mask_bits,
|
pixmask = XCreateBitmapFromData(display, win, mask_bits,
|
||||||
mask_width,mask_height);
|
mask_width, mask_height);
|
||||||
XShapeCombineMask(display, win, ShapeBounding, 0, 0,
|
XShapeCombineMask(display, win, ShapeBounding, 0, 0,
|
||||||
pixmask, ShapeSet);
|
pixmask, ShapeSet);
|
||||||
XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0,
|
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.icon_y = sizehints.y;
|
||||||
wmhints.window_group = win;
|
wmhints.window_group = win;
|
||||||
wmhints.flags = StateHint | IconWindowHint |
|
wmhints.flags = StateHint | IconWindowHint |
|
||||||
IconPositionHint | WindowGroupHint;
|
IconPositionHint | WindowGroupHint;
|
||||||
|
|
||||||
XSetWMHints(display, win, &wmhints);
|
XSetWMHints(display, win, &wmhints);
|
||||||
XSetCommand(display, win, argv, argc);
|
XSetCommand(display, win, argv, argc);
|
||||||
|
@ -438,18 +443,21 @@ 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);
|
||||||
XCopyArea(display, images[FACE], win, NormalGC, 0, 0,
|
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);
|
flush_expose(win);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -458,9 +466,10 @@ 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,98 +477,98 @@ 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) {
|
||||||
case AC_LINE_STATUS_ON:
|
case AC_LINE_STATUS_ON:
|
||||||
draw_image(PLUGGED);
|
draw_image(PLUGGED);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
draw_image(UNPLUGGED);
|
draw_image(UNPLUGGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Display the appropriate color battery. */
|
/* Display the appropriate color battery. */
|
||||||
switch (cur_info.battery_status) {
|
switch (cur_info.battery_status) {
|
||||||
case BATTERY_STATUS_HIGH:
|
case BATTERY_STATUS_HIGH:
|
||||||
case BATTERY_STATUS_CHARGING:
|
case BATTERY_STATUS_CHARGING:
|
||||||
draw_image(BATTERY_HIGH);
|
draw_image(BATTERY_HIGH);
|
||||||
break;
|
break;
|
||||||
case BATTERY_STATUS_LOW:
|
case BATTERY_STATUS_LOW:
|
||||||
draw_image(BATTERY_LOW);
|
draw_image(BATTERY_LOW);
|
||||||
break;
|
break;
|
||||||
case BATTERY_STATUS_CRITICAL: /* blinking red battery */
|
case BATTERY_STATUS_CRITICAL: /* blinking red battery */
|
||||||
if (blinked)
|
if (blinked)
|
||||||
draw_image(BATTERY_CRITICAL);
|
draw_image(BATTERY_CRITICAL);
|
||||||
else
|
else
|
||||||
draw_image(BATTERY_BLINK);
|
draw_image(BATTERY_BLINK);
|
||||||
blinked=!blinked;
|
blinked = !blinked;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
draw_image(BATTERY_NONE);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/* Show if the battery is charging. */
|
||||||
* Display the percent left dial. This has the side effect of
|
if (cur_info.battery_flags & BATTERY_FLAGS_CHARGING)
|
||||||
* clearing the time left field.
|
draw_image(CHARGING);
|
||||||
*/
|
else
|
||||||
x=DIAL_MULTIPLIER * cur_info.battery_percentage;
|
draw_image(NOCHARGING);
|
||||||
if (x >= 0) {
|
|
||||||
|
/*
|
||||||
|
* 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. */
|
/* Start by displaying bright on the dial. */
|
||||||
copy_image(DIAL_BRIGHT, 0, 0,
|
copy_image(DIAL_BRIGHT, 0, 0,
|
||||||
x, image_info[DIAL_BRIGHT].height,
|
x, image_info[DIAL_BRIGHT].height,
|
||||||
image_info[DIAL_BRIGHT].x,
|
image_info[DIAL_BRIGHT].x,
|
||||||
image_info[DIAL_BRIGHT].y);
|
image_info[DIAL_BRIGHT].y);
|
||||||
}
|
}
|
||||||
/* Now display dim on the remainder of the dial. */
|
/* Now display dim on the remainder of the dial. */
|
||||||
copy_image(DIAL_DIM, x, 0,
|
copy_image(DIAL_DIM, x, 0,
|
||||||
image_info[DIAL_DIM].width - x,
|
image_info[DIAL_DIM].width - x,
|
||||||
image_info[DIAL_DIM].height,
|
image_info[DIAL_DIM].height,
|
||||||
image_info[DIAL_DIM].x + x,
|
image_info[DIAL_DIM].x + x,
|
||||||
image_info[DIAL_DIM].y);
|
image_info[DIAL_DIM].y);
|
||||||
|
|
||||||
/* Show percent remaining */
|
/* Show percent remaining */
|
||||||
if (cur_info.battery_percentage >= 0) {
|
if (cur_info.battery_percentage >= 0) {
|
||||||
digit = cur_info.battery_percentage / 10;
|
digit = cur_info.battery_percentage / 10;
|
||||||
if (digit == 10) {
|
if (digit == 10) {
|
||||||
/* 11 is the `1' for the hundreds place. */
|
/* 11 is the `1' for the hundreds place. */
|
||||||
draw_letter(11,SMALLFONT,HUNDREDS_OFFSET);
|
draw_letter(11, SMALLFONT, HUNDREDS_OFFSET);
|
||||||
digit=0;
|
digit = 0;
|
||||||
}
|
}
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show time left */
|
/* Show time left */
|
||||||
|
|
||||||
/* A negative number means that it is unknown. Dim the field. */
|
/* A negative number means that it is unknown. Dim the field. */
|
||||||
if (cur_info.battery_time < 0) {
|
if (cur_info.battery_time < 0) {
|
||||||
|
@ -568,40 +577,41 @@ void recalc_window(apm_info cur_info) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cur_info.using_minutes)
|
if (cur_info.using_minutes)
|
||||||
time_left = cur_info.battery_time;
|
time_left = cur_info.battery_time;
|
||||||
else
|
else
|
||||||
time_left = cur_info.battery_time / 60;
|
time_left = cur_info.battery_time / 60;
|
||||||
hour_left = time_left / 60;
|
hour_left = time_left / 60;
|
||||||
min_left = time_left % 60;
|
min_left = time_left % 60;
|
||||||
digit = hour_left / 10;
|
digit = hour_left / 10;
|
||||||
draw_letter(digit,BIGFONT,HOURS_TENS_OFFSET);
|
draw_letter(digit, BIGFONT, HOURS_TENS_OFFSET);
|
||||||
digit = hour_left % 10;
|
digit = hour_left % 10;
|
||||||
draw_letter(digit,BIGFONT,HOURS_ONES_OFFSET);
|
draw_letter(digit, BIGFONT, HOURS_ONES_OFFSET);
|
||||||
digit = min_left / 10;
|
digit = min_left / 10;
|
||||||
draw_letter(digit,BIGFONT,MINUTES_TENS_OFFSET);
|
draw_letter(digit, BIGFONT, MINUTES_TENS_OFFSET);
|
||||||
digit = min_left % 10;
|
digit = min_left % 10;
|
||||||
draw_letter(digit,BIGFONT,MINUTES_ONES_OFFSET);
|
draw_letter(digit, BIGFONT, MINUTES_ONES_OFFSET);
|
||||||
|
|
||||||
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
|
||||||
|
@ -623,11 +632,10 @@ void alarmhandler(int sig) {
|
||||||
error("Cannot read HAL information.");
|
error("Cannot read HAL information.");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
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,57 +676,56 @@ 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",
|
||||||
real,
|
real,
|
||||||
real == 1 ? "y" : "ies",
|
real == 1 ? "y" : "ies",
|
||||||
requested);
|
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). */
|
||||||
if (apm_exists() == 0) {
|
if (apm_exists() == 0) {
|
||||||
if (! delay)
|
if (!delay)
|
||||||
delay = 1;
|
delay = 1;
|
||||||
}
|
}
|
||||||
#ifdef HAL
|
#ifdef HAL
|
||||||
/* Check for hal support. */
|
/* Check for hal support. */
|
||||||
else if (simplehal_supported()) {
|
else if (simplehal_supported()) {
|
||||||
use_simplehal = 1;
|
use_simplehal = 1;
|
||||||
if (! delay)
|
if (!delay)
|
||||||
delay = 2;
|
delay = 2;
|
||||||
}
|
}
|
||||||
#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) {
|
||||||
check_battery_num(acpi_batt_count, battnum);
|
check_battery_num(acpi_batt_count, battnum);
|
||||||
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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
load_images();
|
load_images();
|
||||||
load_audio();
|
load_audio();
|
||||||
|
|
||||||
signal(SIGALRM, alarmhandler);
|
signal(SIGALRM, alarmhandler);
|
||||||
alarmhandler(SIGALRM);
|
alarmhandler(SIGALRM);
|
||||||
|
|
|
@ -3,16 +3,16 @@
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Pixmap pixmap;
|
Pixmap pixmap;
|
||||||
Pixmap mask;
|
Pixmap mask;
|
||||||
XpmAttributes attributes;
|
XpmAttributes attributes;
|
||||||
} XpmIcon;
|
} XpmIcon;
|
||||||
|
|
||||||
typedef struct image_info_type {
|
typedef struct image_info_type {
|
||||||
const char* filename;
|
const char *filename;
|
||||||
const int width;
|
const int width;
|
||||||
const int height;
|
const int height;
|
||||||
const int x;
|
const int x;
|
||||||
const int y;
|
const int y;
|
||||||
const int charwidth;
|
const int charwidth;
|
||||||
} image_info_type;
|
} image_info_type;
|
||||||
|
|
||||||
/* Assign reference numbers to all images that are loaded. */
|
/* 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
|
* plus the size of the image, where to draw it on the icon, etc
|
||||||
*/
|
*/
|
||||||
static struct image_info_type image_info[] = {
|
static struct image_info_type image_info[] = {
|
||||||
{"smallfont",7,67,0,45,6},
|
{"smallfont", 7, 67, 0, 45, 6},
|
||||||
{"bigfont",9,73,0,23,7},
|
{"bigfont", 9, 73, 0, 23, 7},
|
||||||
{"battery_high",25,13,33,42,0},
|
{"battery_high", 25, 13, 33, 42, 0},
|
||||||
{"battery_medium",25,13,33,42,0},
|
{"battery_medium", 25, 13, 33, 42, 0},
|
||||||
{"battery_low",25,13,33,42,0},
|
{"battery_low", 25, 13, 33, 42, 0},
|
||||||
{"battery_none",25,13,33,42,0},
|
{"battery_none", 25, 13, 33, 42, 0},
|
||||||
{"battery_blink",25,13,33,42,0},
|
{"battery_blink", 25, 13, 33, 42, 0},
|
||||||
{"unplugged",10,8,6,45,0},
|
{"unplugged", 10, 8, 6, 45, 0},
|
||||||
{"plugged",10,8,6,45,0},
|
{"plugged", 10, 8, 6, 45, 0},
|
||||||
{"nocharging",15,9,17,43,0},
|
{"nocharging", 15, 9, 17, 43, 0},
|
||||||
{"charging",15,9,17,43,0},
|
{"charging", 15, 9, 17, 43, 0},
|
||||||
{"dial_bright",56,31,4,4,0},
|
{"dial_bright", 56, 31, 4, 4, 0},
|
||||||
{"dial_dim",56,31,4,4,0},
|
{"dial_dim", 56, 31, 4, 4, 0},
|
||||||
{"face",64,64,0,0,0},
|
{"face", 64, 64, 0, 0, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
#define DIAL_MULTIPLIER 0.56
|
#define DIAL_MULTIPLIER 0.56
|
||||||
|
|
Loading…
Reference in a new issue