diff options
author | raveit65 <[email protected]> | 2020-09-01 15:27:15 +0200 |
---|---|---|
committer | raveit65 <[email protected]> | 2020-09-03 22:08:43 +0200 |
commit | 02954e103de5b722c99407c68d04f77acd9e15e0 (patch) | |
tree | 818f8ee6496c9a639bdd092143a05416efcd095e /battstat | |
parent | 1fc3cf73ca2e390bdb0ede31390139bde33b26cf (diff) | |
download | mate-applets-02954e103de5b722c99407c68d04f77acd9e15e0.tar.bz2 mate-applets-02954e103de5b722c99407c68d04f77acd9e15e0.tar.xz |
battstat: improve code-style
- tab to spaces
- fix indents
- add a space before brackets
Diffstat (limited to 'battstat')
-rw-r--r-- | battstat/acpi-linux.c | 632 | ||||
-rw-r--r-- | battstat/acpi-linux.h | 22 | ||||
-rw-r--r-- | battstat/apmlib/apm.h | 34 | ||||
-rw-r--r-- | battstat/apmlib/apmlib.c | 439 | ||||
-rw-r--r-- | battstat/battstat-upower.c | 82 | ||||
-rw-r--r-- | battstat/battstat.h | 10 | ||||
-rw-r--r-- | battstat/battstat_applet.c | 1716 | ||||
-rw-r--r-- | battstat/power-management.c | 180 |
8 files changed, 1580 insertions, 1535 deletions
diff --git a/battstat/acpi-linux.c b/battstat/acpi-linux.c index afda8fd7..c383bd2e 100644 --- a/battstat/acpi-linux.c +++ b/battstat/acpi-linux.c @@ -24,7 +24,7 @@ */ #ifdef HAVE_CONFIG_H - #include <config.h> + #include <config.h> #endif #ifdef __linux__ @@ -44,91 +44,92 @@ #include <dirent.h> #include "acpi-linux.h" -static GHashTable* read_file(const char* file, char* buf, size_t bufsize) +static GHashTable* +read_file (const char* file, char* buf, size_t bufsize) { - GHashTable* hash = NULL; - - int fd, len, i; - char* key; - char* value; - gboolean reading_key; - - fd = open(file, O_RDONLY); - - if (fd == -1) - { - return hash; - } - - len = read(fd, buf, bufsize); - - close (fd); - - if (len < 0) - { - if (getenv("BATTSTAT_DEBUG")) - { - g_message("Error reading %s: %s", file, g_strerror(errno)); - } - - return hash; - } - - hash = g_hash_table_new(g_str_hash, g_str_equal); - - for (i = 0, value = key = buf, reading_key = TRUE; i < len; i++) - { - if (buf[i] == ':' && reading_key) - { - reading_key = FALSE; - buf[i] = '\0'; - value = buf + i + 1; - } - else if (buf[i] == '\n') - { - reading_key = TRUE; - buf[i] = '\0'; - /* g_message ("Read: %s => %s\n", key, value); */ - g_hash_table_insert(hash, key, g_strstrip(value)); - key = buf + i + 1; - } - else if (reading_key) - { - /* in acpi 20020214 it switched to lower-case proc - * entries. fixing this up here simplifies the - * code. - */ - buf[i] = g_ascii_tolower(buf[i]); - } - } - - return hash; + GHashTable* hash = NULL; + + int fd, len, i; + char* key; + char* value; + gboolean reading_key; + + fd = open (file, O_RDONLY); + + if (fd == -1) + { + return hash; + } + + len = read (fd, buf, bufsize); + + close (fd); + + if (len < 0) + { + if (getenv ("BATTSTAT_DEBUG")) + { + g_message ("Error reading %s: %s", file, g_strerror (errno)); + } + + return hash; + } + + hash = g_hash_table_new (g_str_hash, g_str_equal); + + for (i = 0, value = key = buf, reading_key = TRUE; i < len; i++) + { + if (buf[i] == ':' && reading_key) + { + reading_key = FALSE; + buf[i] = '\0'; + value = buf + i + 1; + } + else if (buf[i] == '\n') + { + reading_key = TRUE; + buf[i] = '\0'; + /* g_message ("Read: %s => %s\n", key, value); */ + g_hash_table_insert (hash, key, g_strstrip (value)); + key = buf + i + 1; + } + else if (reading_key) + { + /* in acpi 20020214 it switched to lower-case proc + * entries. fixing this up here simplifies the + * code. + */ + buf[i] = g_ascii_tolower (buf[i]); + } + } + + return hash; } #if 0 static gboolean read_bool (GHashTable *hash, const char *key) { - char *s; + char *s; - g_return_val_if_fail (hash, FALSE); - g_return_val_if_fail (key, FALSE); + g_return_val_if_fail (hash, FALSE); + g_return_val_if_fail (key, FALSE); - s = g_hash_table_lookup (hash, key); - return s && (*s == 'y'); + s = g_hash_table_lookup (hash, key); + return s && (*s == 'y'); } #endif static long read_long (GHashTable *hash, const char *key) { - char* s; + char* s; - g_return_val_if_fail(hash, 0); - g_return_val_if_fail(key, 0); + g_return_val_if_fail (hash, 0); + g_return_val_if_fail (key, 0); - s = g_hash_table_lookup(hash, key); - return s ? strtol(s, NULL, 10) : 0; + s = g_hash_table_lookup (hash, key); + return s ? strtol (s, NULL, 10) : 0; } static gulong @@ -136,112 +137,114 @@ read_ulong (GHashTable *hash, const char *key) { char *s; - g_return_val_if_fail (hash, 0); - g_return_val_if_fail (key, 0); + g_return_val_if_fail (hash, 0); + g_return_val_if_fail (key, 0); - s = g_hash_table_lookup (hash, key); - return s ? strtoul (s, NULL, 10) : 0; + s = g_hash_table_lookup (hash, key); + return s ? strtoul (s, NULL, 10) : 0; } static const char * read_string (GHashTable *hash, const char *key) { - return g_hash_table_lookup (hash, key); + return g_hash_table_lookup (hash, key); } /* Reads the current status of the AC adapter and stores the * result in acpiinfo->ac_online. */ -static gboolean update_ac_info(struct acpi_info * acpiinfo) +static gboolean +update_ac_info (struct acpi_info * acpiinfo) { - gchar *ac_state = NULL; - DIR * procdir; - struct dirent * procdirentry; - char buf[BUFSIZ]; - GHashTable *hash; - gboolean have_adaptor = FALSE; - - acpiinfo->ac_online = FALSE; - - procdir=opendir("/proc/acpi/ac_adapter/"); - if (!procdir) - return FALSE; - - while ((procdirentry=readdir(procdir))) - { - if (procdirentry->d_name[0]!='.') - { - have_adaptor = TRUE; - ac_state = g_strconcat("/proc/acpi/ac_adapter/", - procdirentry->d_name, - "/", - acpiinfo->ac_state_state, - NULL); - hash = read_file (ac_state, buf, sizeof (buf)); - if (hash && !acpiinfo->ac_online) - { - const char *s; - s = read_string (hash, acpiinfo->ac_state_state); - acpiinfo->ac_online = s ? (strcmp (s, "on-line") == 0) : 0; - g_hash_table_destroy (hash); - } - g_free(ac_state); - } - } + gchar *ac_state = NULL; + DIR * procdir; + struct dirent * procdirentry; + char buf[BUFSIZ]; + GHashTable *hash; + gboolean have_adaptor = FALSE; + + acpiinfo->ac_online = FALSE; + + procdir=opendir ("/proc/acpi/ac_adapter/"); + if (!procdir) + return FALSE; + + while ((procdirentry=readdir (procdir))) + { + if (procdirentry->d_name[0]!='.') + { + have_adaptor = TRUE; + ac_state = g_strconcat ("/proc/acpi/ac_adapter/", + procdirentry->d_name, + "/", + acpiinfo->ac_state_state, + NULL); + hash = read_file (ac_state, buf, sizeof (buf)); + if (hash && !acpiinfo->ac_online) + { + const char *s; + s = read_string (hash, acpiinfo->ac_state_state); + acpiinfo->ac_online = s ? (strcmp (s, "on-line") == 0) : 0; + g_hash_table_destroy (hash); + } + g_free (ac_state); + } + } /* If there are no AC adaptors registered in the system, then we're probably on a desktop (and therefore, on AC power). */ - if (have_adaptor == FALSE) - acpiinfo->ac_online = 1; + if (have_adaptor == FALSE) + acpiinfo->ac_online = 1; - closedir(procdir); + closedir (procdir); - return TRUE; + return TRUE; } /* Reads the ACPI info for the system batteries, and finds * the total capacity, which is stored in acpiinfo. */ -static gboolean update_battery_info(struct acpi_info* acpiinfo) +static gboolean +update_battery_info (struct acpi_info* acpiinfo) { - gchar* batt_info = NULL; - GHashTable* hash; - DIR* procdir; - struct dirent* procdirentry; - char buf[BUFSIZ]; - - acpiinfo->max_capacity = 0; - acpiinfo->low_capacity = 0; - acpiinfo->critical_capacity = 0; - - procdir = opendir("/proc/acpi/battery/"); - - if (!procdir) - { - return FALSE; - } - - while ((procdirentry = readdir(procdir))) - { - if (procdirentry->d_name[0] != '.') - { - batt_info = g_strconcat("/proc/acpi/battery/", procdirentry->d_name, "/info", NULL); - hash = read_file(batt_info, buf, sizeof(buf)); - - if (hash) - { - acpiinfo->max_capacity += read_long(hash, "last full capacity"); - acpiinfo->low_capacity += read_long(hash, "design capacity warning"); - acpiinfo->critical_capacity += read_long(hash, "design capacity low"); - - g_hash_table_destroy(hash); - } - g_free(batt_info); - } - } - - closedir(procdir); - - return TRUE; + gchar* batt_info = NULL; + GHashTable* hash; + DIR* procdir; + struct dirent* procdirentry; + char buf[BUFSIZ]; + + acpiinfo->max_capacity = 0; + acpiinfo->low_capacity = 0; + acpiinfo->critical_capacity = 0; + + procdir = opendir ("/proc/acpi/battery/"); + + if (!procdir) + { + return FALSE; + } + + while ((procdirentry = readdir (procdir))) + { + if (procdirentry->d_name[0] != '.') + { + batt_info = g_strconcat ("/proc/acpi/battery/", procdirentry->d_name, "/info", NULL); + hash = read_file (batt_info, buf, sizeof (buf)); + + if (hash) + { + acpiinfo->max_capacity += read_long (hash, "last full capacity"); + acpiinfo->low_capacity += read_long (hash, "design capacity warning"); + acpiinfo->critical_capacity += read_long (hash, "design capacity low"); + + g_hash_table_destroy (hash); + } + g_free (batt_info); + } + } + + closedir (procdir); + + return TRUE; } @@ -250,72 +253,74 @@ static gboolean update_battery_info(struct acpi_info* acpiinfo) * /proc/acpi/event exported by the kernel, or if it's already * in use, the /var/run/acpid.socket maintained by acpid. Also * initializes the stored battery and AC adapter information. */ -gboolean acpi_linux_init(struct acpi_info * acpiinfo) +gboolean +acpi_linux_init (struct acpi_info * acpiinfo) { - GHashTable *hash; - char buf[BUFSIZ]; - gchar *pbuf; - gulong acpi_ver; - int fd; - - g_assert(acpiinfo); - - if (g_file_get_contents ("/sys/module/acpi/parameters/acpica_version", &pbuf, NULL, NULL)) { - acpi_ver = strtoul (pbuf, NULL, 10); - g_free (pbuf); - } else if ((hash = read_file ("/proc/acpi/info", buf, sizeof (buf)))) { - acpi_ver = read_ulong (hash, "version"); - g_hash_table_destroy (hash); - } else - return FALSE; - - if (acpi_ver < (gulong)20020208) { - acpiinfo->ac_state_state = "status"; - acpiinfo->batt_state_state = "status"; - acpiinfo->charging_state = "state"; - } else { - acpiinfo->ac_state_state = "state"; - acpiinfo->batt_state_state = "state"; - acpiinfo->charging_state = "charging state"; - } - - if (!update_battery_info(acpiinfo) || !update_ac_info(acpiinfo)) - return FALSE; + GHashTable *hash; + char buf[BUFSIZ]; + gchar *pbuf; + gulong acpi_ver; + int fd; + + g_assert (acpiinfo); + + if (g_file_get_contents ("/sys/module/acpi/parameters/acpica_version", &pbuf, NULL, NULL)) { + acpi_ver = strtoul (pbuf, NULL, 10); + g_free (pbuf); + } else if ((hash = read_file ("/proc/acpi/info", buf, sizeof (buf)))) { + acpi_ver = read_ulong (hash, "version"); + g_hash_table_destroy (hash); + } else + return FALSE; + + if (acpi_ver < (gulong)20020208) { + acpiinfo->ac_state_state = "status"; + acpiinfo->batt_state_state = "status"; + acpiinfo->charging_state = "state"; + } else { + acpiinfo->ac_state_state = "state"; + acpiinfo->batt_state_state = "state"; + acpiinfo->charging_state = "charging state"; + } - fd = open("/proc/acpi/event", 0); - if (fd >= 0) { - acpiinfo->event_fd = fd; - acpiinfo->channel = g_io_channel_unix_new(fd); - return TRUE; - } - - fd = socket(PF_UNIX, SOCK_STREAM, 0); - if (fd >= 0) { - struct sockaddr_un addr; - addr.sun_family = AF_UNIX; - strcpy(addr.sun_path, "/var/run/acpid.socket"); - if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == 0) { - acpiinfo->event_fd = fd; - acpiinfo->channel = g_io_channel_unix_new(fd); - return TRUE; + if (!update_battery_info (acpiinfo) || !update_ac_info (acpiinfo)) + return FALSE; + + fd = open ("/proc/acpi/event", 0); + if (fd >= 0) { + acpiinfo->event_fd = fd; + acpiinfo->channel = g_io_channel_unix_new (fd); + return TRUE; + } + + fd = socket (PF_UNIX, SOCK_STREAM, 0); + if (fd >= 0) { + struct sockaddr_un addr; + addr.sun_family = AF_UNIX; + strcpy (addr.sun_path, "/var/run/acpid.socket"); + if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) == 0) { + acpiinfo->event_fd = fd; + acpiinfo->channel = g_io_channel_unix_new (fd); + return TRUE; + } } - } - close(fd); - acpiinfo->event_fd = -1; - return FALSE; + close (fd); + acpiinfo->event_fd = -1; + return FALSE; } /* Cleans up ACPI */ -void acpi_linux_cleanup(struct acpi_info * acpiinfo) +void +acpi_linux_cleanup (struct acpi_info * acpiinfo) { - g_assert(acpiinfo); + g_assert (acpiinfo); - if (acpiinfo->event_fd >= 0) { - g_io_channel_unref(acpiinfo->channel); - close(acpiinfo->event_fd); - acpiinfo->event_fd = -1; - } + if (acpiinfo->event_fd >= 0) { + g_io_channel_unref (acpiinfo->channel); + close (acpiinfo->event_fd); + acpiinfo->event_fd = -1; + } } #define ACPI_EVENT_IGNORE 0 @@ -326,56 +331,60 @@ void acpi_linux_cleanup(struct acpi_info * acpiinfo) /* Given a string event from the ACPI system, returns the type * of event if we're interested in it. str is updated to point * to the next event. */ -static int parse_acpi_event(GString *buffer) +static int +parse_acpi_event (GString *buffer) { - if (strstr(buffer->str, "ac_adapter")) - return ACPI_EVENT_AC; - if (strstr(buffer->str, "battery") ) - return ACPI_EVENT_BATTERY_INFO; + if (strstr (buffer->str, "ac_adapter")) + return ACPI_EVENT_AC; + if (strstr (buffer->str, "battery")) + return ACPI_EVENT_BATTERY_INFO; - return ACPI_EVENT_IGNORE; + return ACPI_EVENT_IGNORE; } /* Handles a new ACPI event by reading it from the event file * and calling any handlers. Since this does a blocking read, * it should only be called when there is a new event as indicated - * by select(). */ -gboolean acpi_process_event(struct acpi_info * acpiinfo) + * by select (). */ +gboolean acpi_process_event (struct acpi_info * acpiinfo) { gsize i; int evt; GString *buffer; GError *gerror=NULL; - buffer=g_string_new(NULL); - g_io_channel_read_line_string ( acpiinfo->channel,buffer,&i,&gerror); + buffer=g_string_new (NULL); + g_io_channel_read_line_string (acpiinfo->channel, + buffer, + &i, + &gerror); if (gerror != NULL) { - g_warning ("%s", gerror->message); - g_error_free (gerror); + g_warning ("%s", gerror->message); + g_error_free (gerror); } gboolean result; - evt = parse_acpi_event(buffer); - switch (evt) { + evt = parse_acpi_event (buffer); + switch (evt) { case ACPI_EVENT_AC: - result = update_ac_info(acpiinfo); - break; - case ACPI_EVENT_BATTERY_INFO: - if (update_battery_info(acpiinfo)) { - /* Update AC info on battery info updates. This works around - * a bug in ACPI (as per bug #163013). - */ - result = update_ac_info(acpiinfo); + result = update_ac_info (acpiinfo); break; - } - /* fall-through */ + case ACPI_EVENT_BATTERY_INFO: + if (update_battery_info (acpiinfo)) { + /* Update AC info on battery info updates. This works around + * a bug in ACPI (as per bug #163013). + */ + result = update_ac_info (acpiinfo); + break; + } + /* fall-through */ default: - result = FALSE; - } + result = FALSE; + } - g_string_free(buffer, FALSE); + g_string_free (buffer, FALSE); return result; } @@ -383,82 +392,83 @@ gboolean acpi_process_event(struct acpi_info * acpiinfo) * Fills out a classic apm_info structure with the data gathered from * the ACPI kernel interface in /proc */ -gboolean acpi_linux_read(struct apm_info *apminfo, struct acpi_info * acpiinfo) +gboolean +acpi_linux_read (struct apm_info *apminfo, struct acpi_info * acpiinfo) { - guint32 remain; - guint32 rate; - gboolean charging; - GHashTable *hash; - gchar* batt_state = NULL; - DIR * procdir; - struct dirent * procdirentry; - char buf[BUFSIZ]; - - g_assert(acpiinfo); - - /* - * apminfo.ac_line_status must be one when on ac power - * apminfo.battery_status must be 0 for high, 1 for low, 2 for critical, 3 for charging - * apminfo.battery_percentage must contain batter charge percentage - * apminfo.battery_flags & 0x8 must be nonzero when charging - */ - - g_assert(apminfo); - - charging = FALSE; - remain = 0; - rate = 0; - - procdir=opendir("/proc/acpi/battery/"); - if (!procdir) - return FALSE; + guint32 remain; + guint32 rate; + gboolean charging; + GHashTable *hash; + gchar* batt_state = NULL; + DIR * procdir; + struct dirent * procdirentry; + char buf[BUFSIZ]; + + g_assert (acpiinfo); + + /* + * apminfo.ac_line_status must be one when on ac power + * apminfo.battery_status must be 0 for high, 1 for low, 2 for critical, 3 for charging + * apminfo.battery_percentage must contain batter charge percentage + * apminfo.battery_flags & 0x8 must be nonzero when charging + */ + + g_assert (apminfo); + + charging = FALSE; + remain = 0; + rate = 0; + + procdir=opendir ("/proc/acpi/battery/"); + if (!procdir) + return FALSE; + + /* Get the remaining capacity for the batteries. Other information + * such as AC state and battery max capacity are read only when they + * change using acpi_process_event (). */ + while ((procdirentry=readdir (procdir))) + { + if (procdirentry->d_name[0]!='.') + { + batt_state = g_strconcat ("/proc/acpi/battery/", + procdirentry->d_name, + "/", + acpiinfo->batt_state_state, + NULL); + hash = read_file (batt_state, buf, sizeof (buf)); + if (hash) + { + const char *s; + if (!charging) + { + s = read_string (hash, acpiinfo->charging_state); + charging = s ? (strcmp (s, "charging") == 0) : 0; + } + remain += read_long (hash, "remaining capacity"); + rate += read_long (hash, "present rate"); + g_hash_table_destroy (hash); + } + g_free (batt_state); + } + } + closedir (procdir); + + apminfo->ac_line_status = acpiinfo->ac_online ? 1 : 0; + apminfo->battery_status = + remain < acpiinfo->low_capacity ? 1 : remain < acpiinfo->critical_capacity ? 2 : 0; + if (!acpiinfo->max_capacity) + apminfo->battery_percentage = -1; + else + apminfo->battery_percentage = (int) (remain/(float)acpiinfo->max_capacity*100); + apminfo->battery_flags = charging ? 0x8 : 0; + if (rate && !charging) + apminfo->battery_time = (int) (remain/(float)rate * 60); + else if (rate && charging) + apminfo->battery_time = (int) ((acpiinfo->max_capacity-remain)/(float)rate * 60); + else + apminfo->battery_time = -1; - /* Get the remaining capacity for the batteries. Other information - * such as AC state and battery max capacity are read only when they - * change using acpi_process_event(). */ - while ((procdirentry=readdir(procdir))) - { - if (procdirentry->d_name[0]!='.') - { - batt_state = g_strconcat("/proc/acpi/battery/", - procdirentry->d_name, - "/", - acpiinfo->batt_state_state, - NULL); - hash = read_file (batt_state, buf, sizeof (buf)); - if (hash) - { - const char *s; - if (!charging) - { - s = read_string (hash, acpiinfo->charging_state); - charging = s ? (strcmp (s, "charging") == 0) : 0; - } - remain += read_long (hash, "remaining capacity"); - rate += read_long (hash, "present rate"); - g_hash_table_destroy (hash); - } - g_free(batt_state); - } - } - closedir(procdir); - - apminfo->ac_line_status = acpiinfo->ac_online ? 1 : 0; - apminfo->battery_status = remain < acpiinfo->low_capacity ? 1 : remain < acpiinfo->critical_capacity ? 2 : 0; - if (!acpiinfo->max_capacity) - apminfo->battery_percentage = -1; - else - apminfo->battery_percentage = (int) (remain/(float)acpiinfo->max_capacity*100); - apminfo->battery_flags = charging ? 0x8 : 0; - if (rate && !charging) - apminfo->battery_time = (int) (remain/(float)rate * 60); - else if (rate && charging) - apminfo->battery_time = (int) ((acpiinfo->max_capacity-remain)/(float)rate * 60); - else - apminfo->battery_time = -1; - - return TRUE; + return TRUE; } - #endif /* __linux__ */ diff --git a/battstat/acpi-linux.h b/battstat/acpi-linux.h index 3550935d..572bf0e5 100644 --- a/battstat/acpi-linux.h +++ b/battstat/acpi-linux.h @@ -22,18 +22,18 @@ #define __ACPI_LINUX_H__ struct acpi_info { - const char *ac_state_state, *batt_state_state, *charging_state; - gboolean ac_online; - int event_fd; - int max_capacity; - int low_capacity; - int critical_capacity; - GIOChannel * channel; + const char *ac_state_state, *batt_state_state, *charging_state; + gboolean ac_online; + int event_fd; + int max_capacity; + int low_capacity; + int critical_capacity; + GIOChannel * channel; }; -gboolean acpi_linux_read(struct apm_info *apminfo, struct acpi_info * acpiinfo); -gboolean acpi_process_event(struct acpi_info * acpiinfo); -gboolean acpi_linux_init(struct acpi_info * acpiinfo); -void acpi_linux_cleanup(struct acpi_info * acpiinfo); +gboolean acpi_linux_read (struct apm_info *apminfo, struct acpi_info * acpiinfo); +gboolean acpi_process_event (struct acpi_info * acpiinfo); +gboolean acpi_linux_init (struct acpi_info * acpiinfo); +void acpi_linux_cleanup (struct acpi_info * acpiinfo); #endif /* __ACPI_LINUX_H__ */ diff --git a/battstat/apmlib/apm.h b/battstat/apmlib/apm.h index 5b44ab71..619c704d 100644 --- a/battstat/apmlib/apm.h +++ b/battstat/apmlib/apm.h @@ -51,22 +51,22 @@ typedef struct apm_info } apm_info; -extern int apm_exists(void); -extern int apm_read(apm_info * i); -extern dev_t apm_dev(void); -extern int apm_open(void); -extern int apm_close(int fd); -extern int apm_get_events(int fd, int timeout, apm_event_t * events, int n); -extern int apm_suspend(int fd); -extern int apm_standby(int fd); -extern int apm_set_ignore(int fd, int mode); -extern unsigned int apm_last_error(int fd); -extern const char *apm_error_name( unsigned int err ); -extern int apm_reject(int fd); -extern const char *apm_event_name(apm_event_t event); -extern const char *apm_time(time_t t); -extern const char *apm_delta_time(time_t then, time_t now); -extern const char *apm_time_nosec(time_t t); +extern int apm_exists (void); +extern int apm_read (apm_info * i); +extern dev_t apm_dev (void); +extern int apm_open (void); +extern int apm_close (int fd); +extern int apm_get_events (int fd, int timeout, apm_event_t * events, int n); +extern int apm_suspend (int fd); +extern int apm_standby (int fd); +extern int apm_set_ignore (int fd, int mode); +extern unsigned int apm_last_error (int fd); +extern const char *apm_error_name (unsigned int err); +extern int apm_reject (int fd); +extern const char *apm_event_name (apm_event_t event); +extern const char *apm_time (time_t t); +extern const char *apm_delta_time (time_t then, time_t now); +extern const char *apm_time_nosec (time_t t); #define SUSPEND 0 #define STANDBY 1 @@ -79,4 +79,4 @@ extern const char *apm_time_nosec(time_t t); #define APM_REJECT_ENABLED #endif -#endif /* __APM_H__ */ +#endif /* __APM_H__ */ diff --git a/battstat/apmlib/apmlib.c b/battstat/apmlib/apmlib.c index e15bb541..d7125f74 100644 --- a/battstat/apmlib/apmlib.c +++ b/battstat/apmlib/apmlib.c @@ -37,121 +37,123 @@ * Otherwise, return 1 if no support exists, or 2 if it is the wrong * version. *NOTE* The sense of the return value is not intuitive. */ -int apm_exists(void) +int +apm_exists (void) { apm_info i; - if (access(APM_PROC, R_OK)) - return 1; - return apm_read(&i); + if (access (APM_PROC, R_OK)) + return 1; + return apm_read (&i); } /* Read information from /proc/apm. Return 0 on success, 1 if APM not * installed, 2 if APM installed, but old version. */ -int apm_read(apm_info * i) +int +apm_read (apm_info * i) { FILE *str; char units[10]; char buffer[100]; int retcode = 0; - if (!(str = fopen(APM_PROC, "r"))) - return 1; + if (!(str = fopen (APM_PROC, "r"))) + return 1; - if (fgets(buffer, sizeof(buffer) - 1, str) == NULL) - printf("fgets error\n"); + if (fgets (buffer, sizeof (buffer) - 1, str) == NULL) + printf ("fgets error\n"); - buffer[sizeof(buffer) - 1] = '\0'; + buffer[sizeof (buffer) - 1] = '\0'; /* Should check for other driver versions; driver 1.9 (and some * others) uses this format, which doesn't expose # batteries. */ - sscanf(buffer, "%s %d.%d %hx %hx %hx %hx %d%% %d %s\n", - (char *) i->driver_version, - &i->apm_version_major, - &i->apm_version_minor, - &i->apm_flags, - &i->ac_line_status, - &i->battery_status, - &i->battery_flags, - &i->battery_percentage, - &i->battery_time, - units); - i->using_minutes = !strncmp(units, "min", 3) ? 1 : 0; + sscanf (buffer, "%s %d.%d %hx %hx %hx %hx %d%% %d %s\n", + (char *) i->driver_version, + &i->apm_version_major, + &i->apm_version_minor, + &i->apm_flags, + &i->ac_line_status, + &i->battery_status, + &i->battery_flags, + &i->battery_percentage, + &i->battery_time, + units); + i->using_minutes = !strncmp (units, "min", 3) ? 1 : 0; if (i->driver_version[0] == 'B') - { /* old style. argh. */ + { /* old style. argh. */ #if !BACKWARD_COMPAT - retcode = 2; + retcode = 2; #else - strcpy((char *) i->driver_version, "pre-0.7"); - i->apm_version_major = 0; - i->apm_version_minor = 0; - i->apm_flags = 0; - i->ac_line_status = 0xff; - i->battery_status = 0xff; - i->battery_flags = 0xff; - i->battery_percentage = -1; - i->battery_time = -1; - i->using_minutes = 1; - - sscanf(buffer, "BIOS version: %d.%d", - &i->apm_version_major, &i->apm_version_minor); - - if (fgets(buffer, sizeof(buffer) - 1, str) == NULL) - printf("fgets error\n"); - - sscanf(buffer, "Flags: 0x%02hx", &i->apm_flags); - if (i->apm_flags & APM_32_BIT_SUPPORT) - { - if (fgets(buffer, sizeof(buffer) - 1, str) == NULL) - printf("fgets error\n"); - - if (fgets(buffer, sizeof(buffer) - 1, str) == NULL) - printf("fgets error\n"); - - if (buffer[0] != 'P') - { - if (!strncmp(buffer + 4, "off line", 8)) - i->ac_line_status = 0; - else if (!strncmp(buffer + 4, "on line", 7)) - i->ac_line_status = 1; - else if (!strncmp(buffer + 4, "on back", 7)) - i->ac_line_status = 2; - - if (fgets(buffer, sizeof(buffer) - 1, str) == NULL) - printf("fgets error\n"); - - if (!strncmp(buffer + 16, "high", 4)) - i->battery_status = 0; - else if (!strncmp(buffer + 16, "low", 3)) - i->battery_status = 1; - else if (!strncmp(buffer + 16, "crit", 4)) - i->battery_status = 2; - else if (!strncmp(buffer + 16, "charg", 5)) - i->battery_status = 3; - - if (fgets(buffer, sizeof(buffer) - 1, str) == NULL) - printf("fgets error\n"); - - if (strncmp(buffer + 14, "unknown", 7)) - i->battery_percentage = atoi(buffer + 14); - if (i->apm_version_major >= 1 && i->apm_version_minor >= 1) - { - if (fgets(buffer, sizeof(buffer) - 1, str) == NULL) - printf("fgets error\n"); - - sscanf(buffer, "Battery flag: 0x%02hx", &i->battery_flags); - - if (fgets(buffer, sizeof(buffer) - 1, str) == NULL) - printf("fgets error\n"); - - if (strncmp(buffer + 14, "unknown", 7)) - i->battery_time = atoi(buffer + 14); - } - } - } + strcpy ((char *) i->driver_version, "pre-0.7"); + i->apm_version_major = 0; + i->apm_version_minor = 0; + i->apm_flags = 0; + i->ac_line_status = 0xff; + i->battery_status = 0xff; + i->battery_flags = 0xff; + i->battery_percentage = -1; + i->battery_time = -1; + i->using_minutes = 1; + + sscanf (buffer, "BIOS version: %d.%d", + &i->apm_version_major, &i->apm_version_minor); + + if (fgets (buffer, sizeof (buffer) - 1, str) == NULL) + printf ("fgets error\n"); + + sscanf (buffer, "Flags: 0x%02hx", &i->apm_flags); + if (i->apm_flags & APM_32_BIT_SUPPORT) + { + if (fgets (buffer, sizeof (buffer) - 1, str) == NULL) + printf ("fgets error\n"); + + if (fgets (buffer, sizeof (buffer) - 1, str) == NULL) + printf ("fgets error\n"); + + if (buffer[0] != 'P') + { + if (!strncmp (buffer + 4, "off line", 8)) + i->ac_line_status = 0; + else if (!strncmp (buffer + 4, "on line", 7)) + i->ac_line_status = 1; + else if (!strncmp (buffer + 4, "on back", 7)) + i->ac_line_status = 2; + + if (fgets (buffer, sizeof (buffer) - 1, str) == NULL) + printf ("fgets error\n"); + + if (!strncmp (buffer + 16, "high", 4)) + i->battery_status = 0; + else if (!strncmp (buffer + 16, "low", 3)) + i->battery_status = 1; + else if (!strncmp (buffer + 16, "crit", 4)) + i->battery_status = 2; + else if (!strncmp (buffer + 16, "charg", 5)) + i->battery_status = 3; + + if (fgets (buffer, sizeof (buffer) - 1, str) == NULL) + printf ("fgets error\n"); + + if (strncmp (buffer + 14, "unknown", 7)) + i->battery_percentage = atoi (buffer + 14); + if (i->apm_version_major >= 1 && i->apm_version_minor >= 1) + { + if (fgets (buffer, sizeof (buffer) - 1, str) == NULL) + printf ("fgets error\n"); + + sscanf (buffer, "Battery flag: 0x%02hx", &i->battery_flags); + + if (fgets (buffer, sizeof (buffer) - 1, str) == NULL) + printf ("fgets error\n"); + + if (strncmp (buffer + 14, "unknown", 7)) + i->battery_time = atoi (buffer + 14); + } + } + } #endif } @@ -159,15 +161,15 @@ int apm_read(apm_info * i) * set to 0xff (==255) instead of -1. */ if (i->battery_percentage > 100) - i->battery_percentage = -1; + i->battery_percentage = -1; - fclose(str); + fclose (str); return retcode; } /* Lookup the device number for the apm_bios device. */ -dev_t apm_dev(void) +dev_t apm_dev (void) { FILE *str; static int cached = -1; @@ -176,32 +178,32 @@ dev_t apm_dev(void) apm_info i; if (cached >= 0) - return cached; + return cached; - if (access(APM_PROC, R_OK) || apm_read(&i) == 1) - return cached = -1; + if (access (APM_PROC, R_OK) || apm_read (&i) == 1) + return cached = -1; if (i.driver_version[0] == '1') - return cached = makedev(10, 134); + return cached = makedev (10, 134); - if (!(str = fopen(APM_DEV, "r"))) - return -1; - while (fgets(buf, sizeof(buf) - 1, str)) + if (!(str = fopen (APM_DEV, "r"))) + return -1; + while (fgets (buf, sizeof (buf) - 1, str)) { - buf[sizeof(buf) - 1] = '\0'; - for (pt = buf; *pt && isspace(*pt); ++pt); /* skip leading spaces */ - for (; *pt && !isspace(*pt); ++pt); /* find next space */ - if (isspace(*pt)) - { - *pt++ = '\0'; - pt[strlen(pt) - 1] = '\0'; /* get rid of newline */ - if (!strcmp(pt, APM_NAME)) - { - fclose(str); - return cached = makedev(atoi(buf), 0); - } - } + buf[sizeof (buf) - 1] = '\0'; + for (pt = buf; *pt && isspace (*pt); ++pt); /* skip leading spaces */ + for (; *pt && !isspace (*pt); ++pt); /* find next space */ + if (isspace (*pt)) + { + *pt++ = '\0'; + pt[strlen (pt) - 1] = '\0'; /* get rid of newline */ + if (!strcmp (pt, APM_NAME)) + { + fclose (str); + return cached = makedev (atoi (buf), 0); + } + } } - fclose(str); + fclose (str); return cached = -1; } @@ -211,54 +213,55 @@ dev_t apm_dev(void) * instead of /tmp? * * apenwarr 2001/05/11: just throw out the weird temporary device file stuff. - * It was only for ancient kernel versions anyway. + * It was only for ancient kernel versions anyway. */ -int apm_open(void) +int +apm_open (void) { int fd; apm_info i; - if (access(APM_PROC, R_OK) || apm_read(&i) == 1) - return -1; + if (access (APM_PROC, R_OK) || apm_read (&i) == 1) + return -1; if (i.driver_version[0] >= '1') { - if ((fd = open(APM_DEVICE, O_RDWR)) < 0) - { - /* Try to create it. This is reasonable - * for backward compatibility. - */ - if (mknod(APM_DEVICE, S_IFCHR | S_IRUSR | S_IWUSR, apm_dev())) - { - unlink(APM_DEVICE); - return -1; - } - fd = open(APM_DEVICE, O_RDWR); - } - - return fd; + if ((fd = open (APM_DEVICE, O_RDWR)) < 0) + { + /* Try to create it. This is reasonable + * for backward compatibility. + */ + if (mknod (APM_DEVICE, S_IFCHR | S_IRUSR | S_IWUSR, apm_dev ())) + { + unlink (APM_DEVICE); + return -1; + } + fd = open (APM_DEVICE, O_RDWR); + } + return fd; } - return -1; } /* Given a file descriptor for the apm_bios device, close it. */ -int apm_close(int fd) +int +apm_close (int fd) { - return close(fd); + return close (fd); } /* Given a file descriptor for the apm_bios device, this routine will wait * timeout seconds for APM events. Up to n events will be placed in the * events queue. The return code will indicate the number of events - * stored. Since this routine uses select(2), it will return if an + * stored. Since this routine uses select (2), it will return if an * unblocked signal is caught. A timeout < 0 means to block indefinately. * * Note that if you read a request to standby or to suspend, the kernel - * will be waiting for you to respond to it with a call to apm_suspend() - * or to apm_standby() ! + * will be waiting for you to respond to it with a call to apm_suspend () + * or to apm_standby () ! */ -int apm_get_events(int fd, int timeout, apm_event_t * events, int n) +int +apm_get_events (int fd, int timeout, apm_event_t * events, int n) { int retcode; fd_set fds; @@ -267,110 +270,116 @@ int apm_get_events(int fd, int timeout, apm_event_t * events, int n) t.tv_sec = timeout; t.tv_usec = 0; - FD_ZERO(&fds); - FD_SET(fd, &fds); - retcode = select(fd + 1, &fds, NULL, NULL, timeout < 0 ? NULL : &t); + FD_ZERO (&fds); + FD_SET (fd, &fds); + retcode = select (fd + 1, &fds, NULL, NULL, timeout < 0 ? NULL : &t); if (retcode <= 0) - return 0; - return read(fd, events, n * sizeof(apm_event_t)) / sizeof(apm_event_t); + return 0; + return read (fd, events, n * sizeof (apm_event_t)) / sizeof (apm_event_t); } /* Try to set the Power State to Suspend. */ -int apm_suspend(int fd) +int +apm_suspend (int fd) { - sync(); - return ioctl(fd, APM_IOC_SUSPEND, NULL); + sync (); + return ioctl (fd, APM_IOC_SUSPEND, NULL); } /* Try to set the Power State to Standby. */ -int apm_standby(int fd) +int +apm_standby (int fd) { - sync(); - return ioctl(fd, APM_IOC_STANDBY, NULL); + sync (); + return ioctl (fd, APM_IOC_STANDBY, NULL); } /* Return the last error code generated by the kernel APM driver */ -unsigned int apm_last_error( int fd ) +unsigned int +apm_last_error ( int fd ) { int err = 0; #ifdef APM_IOC_LAST_ERROR int ierr = 0; - if ( (ierr = ioctl( fd, APM_IOC_LAST_ERROR, &err)) ) - return ierr; + if ((ierr = ioctl (fd, APM_IOC_LAST_ERROR, &err))) + return ierr; #endif return err; } /* Define lookup table for error messages */ typedef struct lookup_t { - int key; - char * msg; + int key; + char* msg; } lookup_t; /* APM error messages, arranged by error code */ static const lookup_t error_table[] = { -/* N/A { APM_SUCCESS, "Operation succeeded" }, */ - { APM_DISABLED, "Power management disabled" }, - { APM_CONNECTED, "Real mode interface already connected" }, - { APM_NOT_CONNECTED, "Interface not connected" }, - { APM_16_CONNECTED, "16 bit interface already connected" }, -/* N/A { APM_16_UNSUPPORTED, "16 bit interface not supported" }, */ - { APM_32_CONNECTED, "32 bit interface already connected" }, - { APM_32_UNSUPPORTED, "32 bit interface not supported" }, - { APM_BAD_DEVICE, "Unrecognized device ID" }, - { APM_BAD_PARAM, "Parameter out of range" }, - { APM_NOT_ENGAGED, "Interface not engaged" }, +/* N/A { APM_SUCCESS, "Operation succeeded" }, */ + { APM_DISABLED, "Power management disabled" }, + { APM_CONNECTED, "Real mode interface already connected" }, + { APM_NOT_CONNECTED, "Interface not connected" }, + { APM_16_CONNECTED, "16 bit interface already connected" }, +/* N/A { APM_16_UNSUPPORTED, "16 bit interface not supported" }, */ + { APM_32_CONNECTED, "32 bit interface already connected" }, + { APM_32_UNSUPPORTED, "32 bit interface not supported" }, + { APM_BAD_DEVICE, "Unrecognized device ID" }, + { APM_BAD_PARAM, "Parameter out of range" }, + { APM_NOT_ENGAGED, "Interface not engaged" }, #ifdef APM_BAD_FUNCTION - { APM_BAD_FUNCTION, "Function not supported" }, + { APM_BAD_FUNCTION, "Function not supported" }, #endif #ifdef APM_RESUME_DISABLED - { APM_RESUME_DISABLED, "Resume timer disabled" }, + { APM_RESUME_DISABLED, "Resume timer disabled" }, #endif - { APM_BAD_STATE, "Unable to enter requested state" }, -/* N/A { APM_NO_EVENTS, "No events pending" }, */ - { APM_NOT_PRESENT, "No APM present" } + { APM_BAD_STATE, "Unable to enter requested state" }, +/* N/A { APM_NO_EVENTS, "No events pending" }, */ + { APM_NOT_PRESENT, "No APM present" } }; -#define ERROR_COUNT (sizeof(error_table)/sizeof(lookup_t)) +#define ERROR_COUNT (sizeof(error_table)/sizeof(lookup_t)) /* Return character string describing error messages from APM kernel */ -const char *apm_error_name( unsigned int err ) +const char * +apm_error_name (unsigned int err) { int i; - for(i=0; i<ERROR_COUNT; i++) - if(err == error_table[i].key) return(error_table[i].msg); + for (i=0; i<ERROR_COUNT; i++) + if (err == error_table[i].key) return (error_table[i].msg); return "Unknown error"; } -int apm_reject( int fd ) +int +apm_reject (int fd) { #ifdef APM_IOC_REJECT - if ( ioctl( fd, APM_IOC_REJECT, NULL ) ) - return apm_last_error( fd ); + if (ioctl fd, APM_IOC_REJECT, NULL )) + return apm_last_error (fd); else #endif - return 0; + return 0; } -#ifdef APM_IOC_IGNORE /* detect kernel support of IGNORE/NOIGNORE functions */ -int apm_set_ignore(int fd, int mode) +#ifdef APM_IOC_IGNORE /* detect kernel support of IGNORE/NOIGNORE functions */ +int +apm_set_ignore (int fd, int mode) /* Ignore Standby. */ { if (mode == IGNORE) { - printf("Telling kernel to ignore system standby/suspend mode\n"); - return ioctl(fd, APM_IOC_IGNORE, NULL); + printf ("Telling kernel to ignore system standby/suspend mode\n"); + return ioctl (fd, APM_IOC_IGNORE, NULL); } else { - printf("Telling kernel not to ignore system standby/suspend mode\n"); - return ioctl(fd, APM_IOC_NOIGNORE, NULL); + printf ("Telling kernel not to ignore system standby/suspend mode\n"); + return ioctl (fd, APM_IOC_NOIGNORE, NULL); } - printf("NOTE: User-generated suspend/standby requests are not ignored\n"); + printf ("NOTE: User-generated suspend/standby requests are not ignored\n"); } #endif @@ -381,35 +390,36 @@ int apm_set_ignore(int fd, int mode) * * Updated to APM BIOS 1.2 spec (February 1996). Available on-line. */ -const char *apm_event_name(apm_event_t event) +const char * +apm_event_name (apm_event_t event) { switch (event) { case APM_SYS_STANDBY: - return "System Standby Request"; + return "System Standby Request"; case APM_SYS_SUSPEND: - return "System Suspend Request"; + return "System Suspend Request"; case APM_NORMAL_RESUME: - return "Normal Resume System"; + return "Normal Resume System"; case APM_CRITICAL_RESUME: - return "Critical Resume System"; + return "Critical Resume System"; case APM_LOW_BATTERY: - return "Battery Low"; + return "Battery Low"; case APM_POWER_STATUS_CHANGE: - return "Power Status Change"; + return "Power Status Change"; case APM_UPDATE_TIME: - return "Update Time"; + return "Update Time"; case APM_CRITICAL_SUSPEND: - return "Critical Suspend"; + return "Critical Suspend"; case APM_USER_STANDBY: - return "User System Standby Request"; + return "User System Standby Request"; case APM_USER_SUSPEND: - return "User System Suspend Request"; + return "User System Suspend Request"; case APM_STANDBY_RESUME: - return "System Standby Resume"; + return "System Standby Resume"; #ifdef APM_CAPABILITY_CHANGE case APM_CAPABILITY_CHANGE: - return "Capability Change"; + return "Capability Change"; #endif } return "Unknown"; @@ -424,12 +434,14 @@ const char *apm_event_name(apm_event_t event) #define SEC_PER_HOUR (60*60) #define SEC_PER_MIN (60) -const char *apm_delta_time(time_t then, time_t now) +const char * +apm_delta_time (time_t then, time_t now) { - return apm_time(now - then); + return apm_time (now - then); } -const char *apm_time(time_t t) +const char * +apm_time (time_t t) { static char buffer[128]; unsigned long s, m, h, d; @@ -443,18 +455,19 @@ const char *apm_time(time_t t) s = t; if (d) - sprintf(buffer, "%lu day%s, %02lu:%02lu:%02lu", - d, d > 1 ? "s" : "", h, m, s); + sprintf (buffer, "%lu day%s, %02lu:%02lu:%02lu", + d, d > 1 ? "s" : "", h, m, s); else - sprintf(buffer, "%02lu:%02lu:%02lu", h, m, s); + sprintf (buffer, "%02lu:%02lu:%02lu", h, m, s); if (t == -1) - sprintf(buffer, "unknown"); + sprintf (buffer, "unknown"); return buffer; } -const char *apm_time_nosec(time_t t) +const char * +apm_time_nosec (time_t t) { static char buffer[128]; unsigned long s, m, h, d; @@ -468,16 +481,16 @@ const char *apm_time_nosec(time_t t) s = t; if (s > 30) - ++m; + ++m; if (d) - sprintf(buffer, "%lu day%s, %lu:%02lu", - d, d > 1 ? "s" : "", h, m); + sprintf (buffer, "%lu day%s, %lu:%02lu", + d, d > 1 ? "s" : "", h, m); else - sprintf(buffer, "%lu:%02lu", h, m); + sprintf (buffer, "%lu:%02lu", h, m); if (t == -1) - sprintf(buffer, "unknown"); + sprintf (buffer, "unknown"); return buffer; } diff --git a/battstat/battstat-upower.c b/battstat/battstat-upower.c index 2a3c6541..788adfd4 100644 --- a/battstat/battstat-upower.c +++ b/battstat/battstat-upower.c @@ -34,7 +34,7 @@ static UpClient *upc; static void (*status_updated_callback) (void); -/* status_updated_callback() can not be called directly because at the time of +/* status_updated_callback () can not be called directly because at the time of * the device-remove signal, the device is not actually removed from the list * of devices known to the up_client object (see libupower-glib/up-client.c in * upower). Waiting for the next idle timer works around this issue and has has @@ -64,12 +64,12 @@ schedule_status_callback (void) static void device_cb (UpClient *client, UpDevice *device, gpointer user_data) { - schedule_status_callback(); + schedule_status_callback (); } static void device_removed_cb (UpClient *client, const gchar *object_path, gpointer user_data) { - schedule_status_callback(); + schedule_status_callback (); } /* ---- public functions ---- */ @@ -79,10 +79,10 @@ battstat_upower_initialise (void (*callback) (void)) { status_updated_callback = callback; - if( upc != NULL ) - return g_strdup( "Already initialised!" ); + if (upc != NULL) + return g_strdup ("Already initialised!"); - if( (upc = up_client_new() ) == NULL ) + if ((upc = up_client_new ()) == NULL) goto error_out; GPtrArray *devices; @@ -90,15 +90,15 @@ battstat_upower_initialise (void (*callback) (void)) if (!devices) { goto error_shutdownclient; } - g_ptr_array_unref(devices); + g_ptr_array_unref (devices); - g_signal_connect_after( upc, "device-added", G_CALLBACK (device_cb), NULL ); - g_signal_connect_after( upc, "device-removed", G_CALLBACK (device_removed_cb), NULL ); + g_signal_connect_after (upc, "device-added", G_CALLBACK (device_cb), NULL); + g_signal_connect_after (upc, "device-removed", G_CALLBACK (device_removed_cb), NULL); return NULL; error_shutdownclient: - g_object_unref( upc ); + g_object_unref (upc); upc = NULL; error_out: @@ -106,12 +106,12 @@ error_out: } void -battstat_upower_cleanup( void ) +battstat_upower_cleanup (void) { - if( upc == NULL ) + if (upc == NULL) return; - g_object_unref( upc ); + g_object_unref (upc); upc = NULL; } @@ -130,14 +130,14 @@ battstat_upower_cleanup( void ) * http://lists.freedesktop.org/archives/hal/2005-July/002841.html */ void -battstat_upower_get_battery_info( BatteryStatus *status ) +battstat_upower_get_battery_info (BatteryStatus *status) { GPtrArray *devices = up_client_get_devices2 (upc); /* The calculation to get overall percentage power remaining is as follows: * - * Sum( Current charges ) / Sum( Full Capacities ) + * Sum (Current charges) / Sum (Full Capacities) * * We can't just take an average of all of the percentages since this * doesn't deal with the case that one battery might have a larger @@ -179,15 +179,15 @@ battstat_upower_get_battery_info( BatteryStatus *status ) /* For each physical battery bay... */ int i; - for( i = 0; i < devices->len; i++ ) + for (i = 0; i < devices->len; i++) { - UpDevice *upd = g_ptr_array_index( devices, i ); + UpDevice *upd = g_ptr_array_index (devices, i); int type, state; double current_charge, full_capacity, rate; gint64 time_to_full, time_to_empty; - g_object_get( upd, + g_object_get (upd, "kind", &type, "state", &state, "energy", ¤t_charge, @@ -195,7 +195,7 @@ battstat_upower_get_battery_info( BatteryStatus *status ) "energy-rate", &rate, "time-to-full", &time_to_full, "time-to-empty", &time_to_empty, - NULL ); + NULL); /* Only count batteries here */ @@ -206,11 +206,11 @@ battstat_upower_get_battery_info( BatteryStatus *status ) present++; /* At least one battery charging -> composite battery is charging. */ - if( state == UP_DEVICE_STATE_CHARGING ) + if (state == UP_DEVICE_STATE_CHARGING) charging = 1; /* At least one battery is discharging -> we're not on AC. */ - if( state == UP_DEVICE_STATE_DISCHARGING ) + if (state == UP_DEVICE_STATE_DISCHARGING) on_ac_power = 0; /* Sum the totals for current charge, design capacity, (dis)charge rate. */ @@ -222,7 +222,7 @@ battstat_upower_get_battery_info( BatteryStatus *status ) remaining_time = (state == UP_DEVICE_STATE_DISCHARGING ? time_to_empty : time_to_full); } - if( !present || full_capacity_total <= 0 || (charging && !on_ac_power) ) + if (!present || full_capacity_total <= 0 || (charging && !on_ac_power)) { /* Either no battery is present or something has gone horribly wrong. * In either case we must return that the composite battery is not @@ -234,7 +234,7 @@ battstat_upower_get_battery_info( BatteryStatus *status ) status->on_ac_power = TRUE; status->charging = FALSE; - g_ptr_array_unref( devices ); + g_ptr_array_unref (devices); return; } @@ -243,11 +243,11 @@ battstat_upower_get_battery_info( BatteryStatus *status ) /* As per above, overall charge is: * - * Sum( Current charges ) / Sum( Full Capacities ) + * Sum (Current charges) / Sum (Full Capacities) */ - status->percent = ( current_charge_total / full_capacity_total ) * 100.0 + 0.5; + status->percent = (current_charge_total / full_capacity_total) * 100.0 + 0.5; - if( present == 1 ) + if (present == 1) { /* In the case of exactly one battery, report the time remaining figure * from upower directly since it might have come from an authorative source @@ -258,13 +258,13 @@ battstat_upower_get_battery_info( BatteryStatus *status ) * unknown time remaining. */ - if( remaining_time == 0 ) + if (remaining_time == 0) status->minutes = -1; else status->minutes = (remaining_time + 30) / 60; } /* Rest of cases to deal with multiple battery systems... */ - else if( !on_ac_power && rate_total != 0 ) + else if (!on_ac_power && rate_total != 0) { /* Then we're discharging. Calculate time remaining until at zero. */ @@ -272,9 +272,9 @@ battstat_upower_get_battery_info( BatteryStatus *status ) remaining = current_charge_total; remaining /= rate_total; - status->minutes = (int) floor( remaining * 60.0 + 0.5 ); + status->minutes = (int) floor (remaining * 60.0 + 0.5); } - else if( charging && rate_total != 0 ) + else if (charging && rate_total != 0) { /* Calculate time remaining until charged. For systems with more than * one battery, this code is very approximate. The assumption is that if @@ -286,11 +286,11 @@ battstat_upower_get_battery_info( BatteryStatus *status ) double remaining; remaining = full_capacity_total - current_charge_total; - if( remaining < 0 ) + if (remaining < 0) remaining = 0; remaining /= rate_total; - status->minutes = (int) floor( remaining * 60.0 + 0.5 ); + status->minutes = (int) floor (remaining * 60.0 + 0.5); } else { @@ -302,27 +302,27 @@ battstat_upower_get_battery_info( BatteryStatus *status ) status->charging = charging; status->on_ac_power = on_ac_power; - g_ptr_array_unref( devices ); + g_ptr_array_unref (devices); } void -error_dialog( const char *fmt , ...) +error_dialog (const char *fmt , ...) { va_list ap; - va_start(ap, fmt); + va_start (ap, fmt); char str[1000]; - vsprintf(str, fmt, ap); - va_end(ap); + vsprintf (str, fmt, ap); + va_end (ap); GtkWidget *dialog; - dialog = gtk_message_dialog_new( NULL, 0, GTK_MESSAGE_ERROR, + dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", str); - g_signal_connect_swapped( G_OBJECT (dialog), "response", + g_signal_connect_swapped (G_OBJECT (dialog), "response", G_CALLBACK (gtk_widget_destroy), - G_OBJECT (dialog) ); + G_OBJECT (dialog)); - gtk_widget_show_all( dialog ); + gtk_widget_show_all (dialog); } #endif /* HAVE_UPOWER */ diff --git a/battstat/battstat.h b/battstat/battstat.h index d0d4e402..77ff8f10 100644 --- a/battstat/battstat.h +++ b/battstat/battstat.h @@ -142,15 +142,15 @@ typedef struct _ProgressData { } ProgressData; /* battstat_applet.c */ -void reconfigure_layout( ProgressData *battstat ); -void battstat_show_help( ProgressData *battstat, const char *section ); +void reconfigure_layout (ProgressData *battstat); +void battstat_show_help (ProgressData *battstat, const char *section); void prop_cb (GtkAction *, ProgressData *); /* power-management.c */ -const char *power_management_getinfo( BatteryStatus *status ); +const char *power_management_getinfo (BatteryStatus *status); const char *power_management_initialise (void (*callback) (void)); -void power_management_cleanup( void ); +void power_management_cleanup (void); -int power_management_using_upower( void ); +int power_management_using_upower (void); #endif /* _battstat_h_ */ diff --git a/battstat/battstat_applet.c b/battstat/battstat_applet.c index 7f3838ed..a369612c 100644 --- a/battstat/battstat_applet.c +++ b/battstat/battstat_applet.c @@ -50,19 +50,21 @@ #define BATTSTAT_SCHEMA "org.mate.panel.applet.battstat" static gboolean check_for_updates (gpointer data); -static void about_cb( GtkAction *, ProgressData * ); -static void help_cb( GtkAction *, ProgressData * ); + +static void about_cb (GtkAction *, ProgressData *); + +static void help_cb (GtkAction *, ProgressData *); static const GtkActionEntry battstat_menu_actions [] = { - { "BattstatProperties", "document-properties", N_("_Preferences"), - NULL, NULL, - G_CALLBACK (prop_cb) }, - { "BattstatHelp", "help-browser", N_("_Help"), - NULL, NULL, - G_CALLBACK (help_cb) }, - { "BattstatAbout", "help-about", N_("_About"), - NULL, NULL, - G_CALLBACK (about_cb) } + { "BattstatProperties", "document-properties", N_("_Preferences"), + NULL, NULL, + G_CALLBACK (prop_cb) }, + { "BattstatHelp", "help-browser", N_("_Help"), + NULL, NULL, + G_CALLBACK (help_cb) }, + { "BattstatAbout", "help-about", N_("_About"), + NULL, NULL, + G_CALLBACK (about_cb) } }; #define AC_POWER_STRING _("System is running on AC power") @@ -78,22 +80,22 @@ static GSList *instances; static void status_change_callback (void) { - GSList *instance; + GSList *instance; - for (instance = instances; instance; instance = instance->next) - { - ProgressData *battstat = instance->data; - - if (battstat->timeout_id) + for (instance = instances; instance; instance = instance->next) { - g_source_remove (battstat->timeout_id); - battstat->timeout_id = 0; - } + ProgressData *battstat = instance->data; - check_for_updates (battstat); - } + if (battstat->timeout_id) + { + g_source_remove (battstat->timeout_id); + battstat->timeout_id = 0; + } + + check_for_updates (battstat); + } - event_driven = TRUE; + event_driven = TRUE; } /* The following two functions keep track of how many instances of the applet @@ -108,214 +110,215 @@ status_change_callback (void) static const char * static_global_initialisation (ProgressData *battstat) { - gboolean first_time; - const char *err; + gboolean first_time; + const char *err; - first_time = !instances; + first_time = !instances; - instances = g_slist_prepend (instances, battstat); + instances = g_slist_prepend (instances, battstat); - if (!first_time) - return NULL; + if (!first_time) + return NULL; - err = power_management_initialise (status_change_callback); + err = power_management_initialise (status_change_callback); - return err; + return err; } static void static_global_teardown (ProgressData *battstat) { - instances = g_slist_remove (instances, battstat); + instances = g_slist_remove (instances, battstat); - /* remaining instances... */ - if (instances) - return; + /* remaining instances... */ + if (instances) + return; - /* instances == 0 */ + /* instances == 0 */ - power_management_cleanup(); + power_management_cleanup (); } /* Pop up an error dialog on the same screen as 'applet' saying 'msg'. */ static void -battstat_error_dialog( GtkWidget *applet, const char *msg ) +battstat_error_dialog (GtkWidget *applet, + const char *msg) { - GtkWidget *dialog; + GtkWidget *dialog; - dialog = gtk_message_dialog_new( NULL, 0, GTK_MESSAGE_ERROR, - GTK_BUTTONS_OK, "%s", msg); + dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR, + GTK_BUTTONS_OK, "%s", msg); - gtk_window_set_screen( GTK_WINDOW (dialog), - gtk_widget_get_screen (GTK_WIDGET (applet)) ); + gtk_window_set_screen (GTK_WINDOW (dialog), + gtk_widget_get_screen (GTK_WIDGET (applet))); - g_signal_connect_swapped( G_OBJECT (dialog), "response", - G_CALLBACK (gtk_widget_destroy), - G_OBJECT (dialog) ); + g_signal_connect_swapped (G_OBJECT (dialog), "response", + G_CALLBACK (gtk_widget_destroy), + G_OBJECT (dialog)); - gtk_widget_show_all( dialog ); + gtk_widget_show_all (dialog); } /* Format a string describing how much time is left to fully (dis)charge - the battery. The return value must be g_free()d. + the battery. The return value must be g_free ()d. */ static char * get_remaining (BatteryStatus *info) { - int hours; - int mins; - - hours = info->minutes / 60; - mins = info->minutes % 60; - - if (info->on_ac_power && !info->charging) - return g_strdup_printf (_("Battery charged (%d%%)"), info->percent); - else if (info->minutes < 0 && !info->on_ac_power) - return g_strdup_printf (_("Unknown time (%d%%) remaining"), info->percent); - else if (info->minutes < 0 && info->on_ac_power) - return g_strdup_printf (_("Unknown time (%d%%) until charged"), info->percent); - else - if (hours == 0) - if (!info->on_ac_power) - return g_strdup_printf (ngettext ( - "%d minute (%d%%) remaining", - "%d minutes (%d%%) remaining", - mins), mins, info->percent); - else - return g_strdup_printf (ngettext ( - "%d minute until charged (%d%%)", - "%d minutes until charged (%d%%)", - mins), mins, info->percent); - else if (mins == 0) - if (!info->on_ac_power) - return g_strdup_printf (ngettext ( - "%d hour (%d%%) remaining", - "%d hours (%d%%) remaining", - hours), hours, info->percent); - else - return g_strdup_printf (ngettext ( - "%d hour until charged (%d%%)", - "%d hours until charged (%d%%)", - hours), hours, info->percent); - else - if (!info->on_ac_power) - /* TRANSLATOR: "%d %s %d %s" are "%d hours %d minutes" - * Swap order with "%2$s %2$d %1$s %1$d if needed */ - return g_strdup_printf (_("%d %s %d %s (%d%%) remaining"), - hours, ngettext ("hour", "hours", hours), - mins, ngettext ("minute", "minutes", mins), - info->percent); - else - /* TRANSLATOR: "%d %s %d %s" are "%d hours %d minutes" - * Swap order with "%2$s %2$d %1$s %1$d if needed */ - return g_strdup_printf (_("%d %s %d %s until charged (%d%%)"), - hours, ngettext ("hour", "hours", hours), - mins, ngettext ("minute", "minutes", mins), - info->percent); + int hours; + int mins; + + hours = info->minutes / 60; + mins = info->minutes % 60; + + if (info->on_ac_power && !info->charging) + return g_strdup_printf (_("Battery charged (%d%%)"), info->percent); + else if (info->minutes < 0 && !info->on_ac_power) + return g_strdup_printf (_("Unknown time (%d%%) remaining"), info->percent); + else if (info->minutes < 0 && info->on_ac_power) + return g_strdup_printf (_("Unknown time (%d%%) until charged"), info->percent); + else + if (hours == 0) + if (!info->on_ac_power) + return g_strdup_printf (ngettext ( + "%d minute (%d%%) remaining", + "%d minutes (%d%%) remaining", + mins), mins, info->percent); + else + return g_strdup_printf (ngettext ( + "%d minute until charged (%d%%)", + "%d minutes until charged (%d%%)", + mins), mins, info->percent); + else if (mins == 0) + if (!info->on_ac_power) + return g_strdup_printf (ngettext ( + "%d hour (%d%%) remaining", + "%d hours (%d%%) remaining", + hours), hours, info->percent); + else + return g_strdup_printf (ngettext ( + "%d hour until charged (%d%%)", + "%d hours until charged (%d%%)", + hours), hours, info->percent); + else + if (!info->on_ac_power) + /* TRANSLATOR: "%d %s %d %s" are "%d hours %d minutes" + * Swap order with "%2$s %2$d %1$s %1$d if needed */ + return g_strdup_printf (_("%d %s %d %s (%d%%) remaining"), + hours, ngettext ("hour", "hours", hours), + mins, ngettext ("minute", "minutes", mins), + info->percent); + else + /* TRANSLATOR: "%d %s %d %s" are "%d hours %d minutes" + * Swap order with "%2$s %2$d %1$s %1$d if needed */ + return g_strdup_printf (_("%d %s %d %s until charged (%d%%)"), + hours, ngettext ("hour", "hours", hours), + mins, ngettext ("minute", "minutes", mins), + info->percent); } static gboolean battery_full_notify (GtkWidget *applet) { #ifdef HAVE_LIBNOTIFY - GError *error = NULL; - GdkPixbuf *icon; - gboolean result; + GError *error = NULL; + GdkPixbuf *icon; + gboolean result; - if (!notify_is_initted () && !notify_init (_("Battery Monitor"))) - return FALSE; + if (!notify_is_initted () && !notify_init (_("Battery Monitor"))) + return FALSE; - icon = gtk_icon_theme_load_icon_for_scale ( - gtk_icon_theme_get_default (), - "battery", - 48, - gtk_widget_get_scale_factor (applet), - GTK_ICON_LOOKUP_USE_BUILTIN, - NULL); + icon = gtk_icon_theme_load_icon_for_scale (gtk_icon_theme_get_default (), + "battery", + 48, + gtk_widget_get_scale_factor (applet), + GTK_ICON_LOOKUP_USE_BUILTIN, + NULL); - NotifyNotification *n = notify_notification_new (_("Your battery is now fully recharged"), "", /* "battery" */ NULL); + NotifyNotification *n = notify_notification_new (_("Your battery is now fully recharged"), + "", /* "battery" */ NULL); - notify_notification_set_image_from_pixbuf (n, icon); - g_object_unref (icon); + notify_notification_set_image_from_pixbuf (n, icon); + g_object_unref (icon); - result = notify_notification_show (n, &error); + result = notify_notification_show (n, &error); - if (error) - { - g_warning ("%s", error->message); - g_error_free (error); - } + if (error) + { + g_warning ("%s", error->message); + g_error_free (error); + } - g_object_unref (G_OBJECT (n)); + g_object_unref (G_OBJECT (n)); - return result; + return result; #else - return FALSE; + return FALSE; #endif } -/* Show a dialog notifying the user that their battery is done charging. - */ +/* Show a dialog notifying the user that their battery is done charging. */ static void battery_full_dialog (GtkWidget *applet) { /* first attempt to use libnotify */ - if (battery_full_notify (applet)) - return; - - GtkWidget *dialog, *hbox, *image, *label; - cairo_surface_t *surface; - - gchar *new_label; - dialog = gtk_dialog_new_with_buttons ( - _("Battery Notice"), - NULL, - GTK_DIALOG_DESTROY_WITH_PARENT, - "gtk-ok", - GTK_RESPONSE_ACCEPT, - NULL); - g_signal_connect_swapped (G_OBJECT (dialog), "response", - G_CALLBACK (gtk_widget_destroy), - G_OBJECT (dialog)); - - gtk_container_set_border_width (GTK_CONTAINER (dialog), 6); - hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); - surface = gtk_icon_theme_load_surface ( - gtk_icon_theme_get_default (), - "battery", - 48, - gtk_widget_get_scale_factor (applet), - NULL, - GTK_ICON_LOOKUP_USE_BUILTIN, - NULL); - image = gtk_image_new_from_surface (surface); - cairo_surface_destroy (surface); - gtk_box_pack_start (GTK_BOX (hbox), image, TRUE, TRUE, 6); - new_label = g_strdup_printf ( - "<span weight=\"bold\" size=\"larger\">%s</span>", - _("Your battery is now fully recharged")); - label = gtk_label_new (new_label); - g_free (new_label); - gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); - gtk_label_set_use_markup (GTK_LABEL (label), TRUE); - gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 6); - gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox); - gtk_window_set_keep_above (GTK_WINDOW (dialog), TRUE); - gtk_window_stick (GTK_WINDOW (dialog)); - gtk_window_set_skip_pager_hint (GTK_WINDOW (dialog), TRUE); - gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE); - gtk_widget_show_all (dialog); + if (battery_full_notify (applet)) + return; + + GtkWidget *dialog, *hbox, *image, *label; + cairo_surface_t *surface; + + gchar *new_label; + dialog = gtk_dialog_new_with_buttons (_("Battery Notice"), + NULL, + GTK_DIALOG_DESTROY_WITH_PARENT, + "gtk-ok", + GTK_RESPONSE_ACCEPT, + NULL); + g_signal_connect_swapped (G_OBJECT (dialog), + "response", + G_CALLBACK (gtk_widget_destroy), + G_OBJECT (dialog)); + + gtk_container_set_border_width (GTK_CONTAINER (dialog), 6); + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); + surface = gtk_icon_theme_load_surface (gtk_icon_theme_get_default (), + "battery", + 48, + gtk_widget_get_scale_factor (applet), + NULL, + GTK_ICON_LOOKUP_USE_BUILTIN, + NULL); + + image = gtk_image_new_from_surface (surface); + cairo_surface_destroy (surface); + gtk_box_pack_start (GTK_BOX (hbox), image, TRUE, TRUE, 6); + new_label = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>", + _("Your battery is now fully recharged")); + + label = gtk_label_new (new_label); + g_free (new_label); + gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); + gtk_label_set_use_markup (GTK_LABEL (label), TRUE); + gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 6); + gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), + hbox); + gtk_window_set_keep_above (GTK_WINDOW (dialog), TRUE); + gtk_window_stick (GTK_WINDOW (dialog)); + gtk_window_set_skip_pager_hint (GTK_WINDOW (dialog), TRUE); + gtk_window_set_focus_on_map (GTK_WINDOW (dialog), FALSE); + gtk_widget_show_all (dialog); } /* Destroy the low battery notification dialog and mark it as such. */ static void -battery_low_dialog_destroy( ProgressData *battstat ) +battery_low_dialog_destroy (ProgressData *battstat) { - gtk_widget_destroy( battstat->battery_low_dialog ); - battstat->battery_low_dialog = NULL; - battstat->battery_low_label = NULL; + gtk_widget_destroy (battstat->battery_low_dialog); + battstat->battery_low_dialog = NULL; + battstat->battery_low_label = NULL; } /* Determine if suspend is unsupported. For the time being this involves @@ -323,221 +326,226 @@ battery_low_dialog_destroy( ProgressData *battstat ) */ /* #define HAVE_PMI */ static gboolean -is_suspend_unavailable( void ) +is_suspend_unavailable (void) { #ifdef HAVE_PMI - int status; + int status; - status = system( "pmi query suspend" ); + status = system ("pmi query suspend"); - /* -1 - fail (pmi unavailable?). return 'false' since we don't know. - * 0 - success (can suspend). return 'false' since not unavailable. - * 1 - success (cannot suspend). return 'true' since unavailable. - */ - if( WEXITSTATUS( status ) == 1 ) - return TRUE; - else - return FALSE; + /* -1 - fail (pmi unavailable?). return 'false' since we don't know. + * 0 - success (can suspend). return 'false' since not unavailable. + * 1 - success (cannot suspend). return 'true' since unavailable. + */ + if (WEXITSTATUS (status) == 1 ) + return TRUE; + else + return FALSE; #else - return FALSE; /* return 'false' since we don't know. */ + return FALSE; /* return 'false' since we don't know. */ #endif } /* Update the text label in the battery low dialog. */ static void -battery_low_update_text( ProgressData *battstat, BatteryStatus *info ) +battery_low_update_text (ProgressData *battstat, + BatteryStatus *info) { - const char *suggest; - gchar *remaining, *new_label; - GtkRequisition size; - - /* If we're not displaying the dialog then don't update it. */ - if( battstat->battery_low_label == NULL || - battstat->battery_low_dialog == NULL ) - return; - - gtk_widget_get_preferred_size (GTK_WIDGET (battstat->battery_low_label), NULL, &size); - - /* If the label has never been set before, the width will be 0. If it - has been set before (width > 0) then we want to keep the size of - the old widget (to keep the dialog from changing sizes) so we set it - explicitly here. - */ - if( size.width > 0 ) - gtk_widget_set_size_request( GTK_WIDGET( battstat->battery_low_label ), - size.width, size.height ); - - if (info->minutes < 0 && !info->on_ac_power) - { - /* we don't know the remaining time */ - remaining = g_strdup_printf (_("You have %d%% of your total battery " - "capacity remaining."), info->percent); - } - else - { - remaining = g_strdup_printf( ngettext( - "You have %d minute of battery power " - "remaining (%d%% of the total capacity).", - "You have %d minutes of battery power " - "remaining (%d%% of the total capacity).", - info->minutes ), - info->minutes,info->percent ); - } - - if( is_suspend_unavailable() ) + const char *suggest; + gchar *remaining, *new_label; + GtkRequisition size; + + /* If we're not displaying the dialog then don't update it. */ + if (battstat->battery_low_label == NULL || + battstat->battery_low_dialog == NULL) + return; + + gtk_widget_get_preferred_size (GTK_WIDGET (battstat->battery_low_label), NULL, &size); + + /* If the label has never been set before, the width will be 0. If it + has been set before (width > 0) then we want to keep the size of + the old widget (to keep the dialog from changing sizes) so we set it + explicitly here. + */ + if (size.width > 0) + gtk_widget_set_size_request (GTK_WIDGET (battstat->battery_low_label), + size.width, size.height); + + if (info->minutes < 0 && !info->on_ac_power) + { + /* we don't know the remaining time */ + remaining = g_strdup_printf (_("You have %d%% of your total battery" + "capacity remaining."), info->percent); + } + else + { + remaining = g_strdup_printf (ngettext ("You have %d minute of battery power " + "remaining (%d%% of the total capacity).", + "You have %d minutes of battery power " + "remaining (%d%% of the total capacity).", + info->minutes), + info->minutes, + info->percent); + } + + if (is_suspend_unavailable ()) /* TRANSLATORS: this is a list, it is left as a single string * to allow you to make it appear like a list would in your * locale. This is if the laptop does not support suspend. */ - suggest = _("To avoid losing your work:\n" - " \xE2\x80\xA2 plug your laptop into external power, or\n" - " \xE2\x80\xA2 save open documents and shut your laptop down." - ); - else + suggest = _("To avoid losing your work:\n" + " \xE2\x80\xA2 plug your laptop into external power, or\n" + " \xE2\x80\xA2 save open documents and shut your laptop down."); + else /* TRANSLATORS: this is a list, it is left as a single string * to allow you to make it appear like a list would in your * locale. This is if the laptop supports suspend. */ - suggest = _("To avoid losing your work:\n" - " \xE2\x80\xA2 suspend your laptop to save power,\n" - " \xE2\x80\xA2 plug your laptop into external power, or\n" - " \xE2\x80\xA2 save open documents and shut your laptop down." - ); - - new_label = g_strdup_printf( - "<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n\n%s", - _("Your battery is running low"), remaining, suggest ); - - gtk_label_set_markup( battstat->battery_low_label, new_label ); - g_free( remaining ); - g_free( new_label ); + suggest = _("To avoid losing your work:\n" + " \xE2\x80\xA2 suspend your laptop to save power,\n" + " \xE2\x80\xA2 plug your laptop into external power, or\n" + " \xE2\x80\xA2 save open documents and shut your laptop down."); + + new_label = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n\n%s", + _("Your battery is running low"), remaining, suggest); + + gtk_label_set_markup (battstat->battery_low_label, new_label); + g_free (remaining); + g_free (new_label); } /* Show a dialog notifying the user that their battery is running low. */ static void -battery_low_dialog( ProgressData *battery, BatteryStatus *info ) +battery_low_dialog (ProgressData *battery, + BatteryStatus *info) { - GtkWidget *hbox, *image, *label; - GtkWidget *vbox; - cairo_surface_t *surface; - - /* If the dialog is already displayed then don't display it again. */ - if( battery->battery_low_dialog != NULL ) - return; - - battery->battery_low_dialog = gtk_dialog_new_with_buttons ( - _("Battery Notice"), - NULL, - GTK_DIALOG_DESTROY_WITH_PARENT, - "gtk-ok", - GTK_RESPONSE_ACCEPT, - NULL); - gtk_dialog_set_default_response( GTK_DIALOG (battery->battery_low_dialog), - GTK_RESPONSE_ACCEPT ); - - g_signal_connect_swapped( G_OBJECT (battery->battery_low_dialog), - "response", - G_CALLBACK (battery_low_dialog_destroy), - battery ); - - gtk_container_set_border_width (GTK_CONTAINER (battery->battery_low_dialog), - 6); - hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); - gtk_container_set_border_width (GTK_CONTAINER (hbox), 6); - surface = gtk_icon_theme_load_surface (gtk_icon_theme_get_default (), - "battery", - 48, - gtk_widget_get_scale_factor (GTK_WIDGET (hbox)), - NULL, - GTK_ICON_LOOKUP_USE_BUILTIN, - NULL); - image = gtk_image_new_from_surface (surface); - cairo_surface_destroy (surface); - vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); - gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 6); - gtk_box_pack_start (GTK_BOX (vbox), image, FALSE, FALSE, 0); - label = gtk_label_new (""); - battery->battery_low_label = GTK_LABEL( label ); - gtk_label_set_line_wrap( battery->battery_low_label, TRUE ); - gtk_label_set_selectable( battery->battery_low_label, TRUE ); - gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 6); - gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (battery->battery_low_dialog))), hbox); - - gtk_window_set_keep_above (GTK_WINDOW (battery->battery_low_dialog), TRUE); - gtk_window_stick (GTK_WINDOW (battery->battery_low_dialog)); - gtk_window_set_focus_on_map (GTK_WINDOW (battery->battery_low_dialog), - FALSE); - gtk_window_set_skip_pager_hint (GTK_WINDOW (battery->battery_low_dialog), - TRUE); - - battery_low_update_text( battery, info ); - - gtk_window_set_position (GTK_WINDOW (battery->battery_low_dialog), - GTK_WIN_POS_CENTER); - gtk_widget_show_all (battery->battery_low_dialog); + GtkWidget *hbox, *image, *label; + GtkWidget *vbox; + cairo_surface_t *surface; + + /* If the dialog is already displayed then don't display it again. */ + if (battery->battery_low_dialog != NULL) + return; + + battery->battery_low_dialog = + gtk_dialog_new_with_buttons (_("Battery Notice"), + NULL, + GTK_DIALOG_DESTROY_WITH_PARENT, + "gtk-ok", + GTK_RESPONSE_ACCEPT, + NULL); + + gtk_dialog_set_default_response (GTK_DIALOG (battery->battery_low_dialog), + GTK_RESPONSE_ACCEPT); + + g_signal_connect_swapped (G_OBJECT (battery->battery_low_dialog), + "response", + G_CALLBACK (battery_low_dialog_destroy), + battery); + + gtk_container_set_border_width (GTK_CONTAINER (battery->battery_low_dialog), + 6); + + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); + gtk_container_set_border_width (GTK_CONTAINER (hbox), 6); + surface = gtk_icon_theme_load_surface (gtk_icon_theme_get_default (), + "battery", + 48, + gtk_widget_get_scale_factor (GTK_WIDGET (hbox)), + NULL, + GTK_ICON_LOOKUP_USE_BUILTIN, + NULL); + + image = gtk_image_new_from_surface (surface); + cairo_surface_destroy (surface); + vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); + gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 6); + gtk_box_pack_start (GTK_BOX (vbox), image, FALSE, FALSE, 0); + label = gtk_label_new (""); + battery->battery_low_label = GTK_LABEL (label); + gtk_label_set_line_wrap (battery->battery_low_label, TRUE); + gtk_label_set_selectable (battery->battery_low_label, TRUE); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 6); + gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (battery->battery_low_dialog))), + hbox); + + gtk_window_set_keep_above (GTK_WINDOW (battery->battery_low_dialog), TRUE); + gtk_window_stick (GTK_WINDOW (battery->battery_low_dialog)); + gtk_window_set_focus_on_map (GTK_WINDOW (battery->battery_low_dialog), + FALSE); + gtk_window_set_skip_pager_hint (GTK_WINDOW (battery->battery_low_dialog), + TRUE); + + battery_low_update_text (battery, info); + + gtk_window_set_position (GTK_WINDOW (battery->battery_low_dialog), + GTK_WIN_POS_CENTER); + gtk_widget_show_all (battery->battery_low_dialog); } /* Update the text of the tooltip from the provided info. */ static void -update_tooltip( ProgressData *battstat, BatteryStatus *info ) +update_tooltip (ProgressData *battstat, + BatteryStatus *info) { - gchar *powerstring; - gchar *remaining; - gchar *tiptext; - - if (info->present) - { - if (info->on_ac_power) - powerstring = AC_POWER_STRING; - else - powerstring = DC_POWER_STRING; + gchar *powerstring; + gchar *remaining; + gchar *tiptext; - remaining = get_remaining (info); + if (info->present) + { + if (info->on_ac_power) + powerstring = AC_POWER_STRING; + else + powerstring = DC_POWER_STRING; - tiptext = g_strdup_printf ("%s\n%s", powerstring, remaining); - g_free (remaining); - } - else - { - if (info->on_ac_power) - tiptext = g_strdup_printf ("%s\n%s", AC_POWER_STRING, - _("No battery present")); + remaining = get_remaining (info); + + tiptext = g_strdup_printf ("%s\n%s", powerstring, remaining); + g_free (remaining); + } else - tiptext = g_strdup_printf ("%s\n%s", DC_POWER_STRING, - _("Battery status unknown")); - } + { + if (info->on_ac_power) + tiptext = g_strdup_printf ("%s\n%s", AC_POWER_STRING, + _("No battery present")); + else + tiptext = g_strdup_printf ("%s\n%s", DC_POWER_STRING, + _("Battery status unknown")); + } - gtk_widget_set_tooltip_text (battstat->applet, tiptext); - g_free (tiptext); + gtk_widget_set_tooltip_text (battstat->applet, tiptext); + g_free (tiptext); } /* Update the text label that either shows the percentage of time left. */ static void -update_percent_label( ProgressData *battstat, BatteryStatus *info ) +update_percent_label (ProgressData *battstat, + BatteryStatus *info) { - gchar *new_label; - - if (info->present && battstat->showtext == APPLET_SHOW_PERCENT) - new_label = g_strdup_printf ("%d%%", info->percent); - else if (info->present && battstat->showtext == APPLET_SHOW_TIME) - { - /* Fully charged or unknown (-1) time remaining display none */ - if ((info->on_ac_power && info->percent == 100) || info->minutes < 0) - new_label = g_strdup (""); - else + gchar *new_label; + + if (info->present && battstat->showtext == APPLET_SHOW_PERCENT) + new_label = g_strdup_printf ("%d%%", info->percent); + else if (info->present && battstat->showtext == APPLET_SHOW_TIME) { - int time; - time = info->minutes; - new_label = g_strdup_printf ("%d:%02d", time/60, time%60); + /* Fully charged or unknown (-1) time remaining display none */ + if ((info->on_ac_power && info->percent == 100) || info->minutes < 0) + new_label = g_strdup (""); + else + { + int time; + time = info->minutes; + new_label = g_strdup_printf ("%d:%02d", time/60, time%60); + } } - } - else - new_label = g_strdup (_("N/A")); + else + new_label = g_strdup (_("N/A")); - gtk_label_set_text (GTK_LABEL (battstat->percent), new_label); - g_free (new_label); + gtk_label_set_text (GTK_LABEL (battstat->percent), new_label); + g_free (new_label); } /* Determine what status icon we ought to be displaying and change the @@ -545,184 +553,189 @@ update_percent_label( ProgressData *battstat, BatteryStatus *info ) showing. */ static void -possibly_update_status_icon( ProgressData *battstat, BatteryStatus *info ) +possibly_update_status_icon (ProgressData *battstat, + BatteryStatus *info ) { - GtkIconTheme *theme; - cairo_surface_t *surface; - gint icon_size, icon_scale; - gchar *icon_name; - int batt_life; - - batt_life = !battstat->red_value_is_time ? info->percent : info->minutes; - - if (batt_life <= battstat->red_val) - { - if (info->charging) - icon_name = "battery-caution-charging"; - else - icon_name = "battery-caution"; - } - else if (batt_life <= battstat->orange_val) - { - if (info->charging) - icon_name = "battery-low-charging"; - else - icon_name = "battery-low"; - } - else if (batt_life <= battstat->yellow_val) - { - if (info->charging) - icon_name = "battery-good-charging"; - else - icon_name = "battery-good"; - } - else if (info->on_ac_power) - { - if (info->charging) - icon_name = "battery-full-charging"; + GtkIconTheme *theme; + cairo_surface_t *surface; + gint icon_size, icon_scale; + gchar *icon_name; + int batt_life; + + batt_life = !battstat->red_value_is_time ? info->percent : info->minutes; + + if (batt_life <= battstat->red_val) + { + if (info->charging) + icon_name = "battery-caution-charging"; + else + icon_name = "battery-caution"; + } + else if (batt_life <= battstat->orange_val) + { + if (info->charging) + icon_name = "battery-low-charging"; + else + icon_name = "battery-low"; + } + else if (batt_life <= battstat->yellow_val) + { + if (info->charging) + icon_name = "battery-good-charging"; + else + icon_name = "battery-good"; + } + else if (info->on_ac_power) + { + if (info->charging) + icon_name = "battery-full-charging"; + else + icon_name = "battery-full-charged"; + } else - icon_name = "battery-full-charged"; - } - else - { - icon_name = "battery-full"; - } + { + icon_name = "battery-full"; + } - theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (battstat->applet))); + theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (battstat->applet))); - icon_size = mate_panel_applet_get_size (MATE_PANEL_APPLET (battstat->applet)); - icon_scale = gtk_widget_get_scale_factor (GTK_WIDGET (battstat->applet)); + icon_size = mate_panel_applet_get_size (MATE_PANEL_APPLET (battstat->applet)); + icon_scale = gtk_widget_get_scale_factor (GTK_WIDGET (battstat->applet)); - surface = gtk_icon_theme_load_surface (theme, icon_name, icon_size, icon_scale, NULL, 0, NULL); + surface = gtk_icon_theme_load_surface (theme, icon_name, + icon_size, + icon_scale, + NULL, 0, NULL); - gtk_image_set_from_surface (GTK_IMAGE(battstat->status), surface); - cairo_surface_destroy (surface); + gtk_image_set_from_surface (GTK_IMAGE (battstat->status), + surface); + cairo_surface_destroy (surface); } /* Gets called as a gtk_timeout once per second. Checks for updates and makes any changes as appropriate. */ static gboolean -check_for_updates( gpointer data ) +check_for_updates (gpointer data) { - ProgressData *battstat = data; - BatteryStatus info; - const char *err; + ProgressData *battstat = data; + BatteryStatus info; + const char *err; - if (DEBUG) g_print("check_for_updates()\n"); + if (DEBUG) g_print ("check_for_updates ()\n"); - if( (err = power_management_getinfo( &info )) ) - battstat_error_dialog( battstat->applet, err ); + if ((err = power_management_getinfo (&info))) + battstat_error_dialog (battstat->applet, err); - if (!event_driven) - { - int timeout; + if (!event_driven) + { + int timeout; - /* if on AC and not event driven scale back the polls to once every 10 */ - if (info.on_ac_power) - timeout = 10; - else - timeout = 2; + /* if on AC and not event driven scale back the polls to once every 10 */ + if (info.on_ac_power) + timeout = 10; + else + timeout = 2; - if (timeout != battstat->timeout) - { - battstat->timeout = timeout; + if (timeout != battstat->timeout) + { + battstat->timeout = timeout; - if (battstat->timeout_id) - g_source_remove (battstat->timeout_id); + if (battstat->timeout_id) + g_source_remove (battstat->timeout_id); + + battstat->timeout_id = g_timeout_add_seconds (battstat->timeout, + check_for_updates, + battstat); + } + } - battstat->timeout_id = g_timeout_add_seconds (battstat->timeout, - check_for_updates, - battstat); + + possibly_update_status_icon (battstat, &info); + + if (!info.on_ac_power && + battstat->last_batt_life != 1000 && + ( + /* if percentage drops below red_val */ + (!battstat->red_value_is_time && + battstat->last_batt_life > battstat->red_val && + info.percent <= battstat->red_val) || + /* if time drops below red_val */ + (battstat->red_value_is_time && + battstat->last_minutes > battstat->red_val && + info.minutes <= battstat->red_val) + ) + && info.present) + { + /* Warn that battery dropped below red_val */ + if (battstat->lowbattnotification) + { + battery_low_dialog (battstat, &info); + + if (battstat->beep) + gdk_display_beep (gdk_display_get_default ()); + } + } + + if (battstat->last_charging && + battstat->last_acline_status && + battstat->last_acline_status!=1000 && + !info.charging && + info.on_ac_power && + info.present && + info.percent > 99) + { + /* Inform that battery now fully charged */ + if (battstat->fullbattnot) + { + battery_full_dialog (battstat->applet); + + if (battstat->beep) + gdk_display_beep (gdk_display_get_default ()); + } } - } - - - possibly_update_status_icon( battstat, &info ); - - if (!info.on_ac_power && - battstat->last_batt_life != 1000 && - ( - /* if percentage drops below red_val */ - (!battstat->red_value_is_time && - battstat->last_batt_life > battstat->red_val && - info.percent <= battstat->red_val) || - /* if time drops below red_val */ - (battstat->red_value_is_time && - battstat->last_minutes > battstat->red_val && - info.minutes <= battstat->red_val) - ) && - info.present) - { - /* Warn that battery dropped below red_val */ - if(battstat->lowbattnotification) + + /* If the warning dialog is displayed and we just got plugged in then + stop displaying it. + */ + if (battstat->battery_low_dialog && info.on_ac_power) + battery_low_dialog_destroy (battstat); + + if (info.on_ac_power != battstat->last_acline_status || + info.percent != battstat->last_batt_life || + info.minutes != battstat->last_minutes || + info.charging != battstat->last_charging) { - battery_low_dialog(battstat, &info); + /* Update the tooltip */ + update_tooltip (battstat, &info); - if(battstat->beep) - gdk_display_beep (gdk_display_get_default ()); + /* If the warning dialog box is currently displayed, update that too. */ + if (battstat->battery_low_dialog != NULL) + battery_low_update_text (battstat, &info); } - } - - if( battstat->last_charging && - battstat->last_acline_status && - battstat->last_acline_status!=1000 && - !info.charging && - info.on_ac_power && - info.present && - info.percent > 99) - { - /* Inform that battery now fully charged */ - if(battstat->fullbattnot) + + if ((battstat->showtext == APPLET_SHOW_PERCENT && + battstat->last_batt_life != info.percent) || + (battstat->showtext == APPLET_SHOW_TIME && + battstat->last_minutes != info.minutes) || + battstat->last_acline_status != info.on_ac_power || + battstat->last_present != info.present || + battstat->refresh_label) /* set by properties dialog */ { - battery_full_dialog (battstat->applet); + /* Update the label */ + update_percent_label (battstat, &info); - if (battstat->beep) - gdk_display_beep (gdk_display_get_default ()); + /* done */ + battstat->refresh_label = FALSE; } - } - - /* If the warning dialog is displayed and we just got plugged in then - stop displaying it. - */ - if( battstat->battery_low_dialog && info.on_ac_power ) - battery_low_dialog_destroy( battstat ); - - if( info.on_ac_power != battstat->last_acline_status || - info.percent != battstat->last_batt_life || - info.minutes != battstat->last_minutes || - info.charging != battstat->last_charging ) - { - /* Update the tooltip */ - update_tooltip( battstat, &info ); - - /* If the warning dialog box is currently displayed, update that too. */ - if( battstat->battery_low_dialog != NULL ) - battery_low_update_text( battstat, &info ); - } - - if( (battstat->showtext == APPLET_SHOW_PERCENT && - battstat->last_batt_life != info.percent) || - (battstat->showtext == APPLET_SHOW_TIME && - battstat->last_minutes != info.minutes) || - battstat->last_acline_status != info.on_ac_power || - battstat->last_present != info.present || - battstat->refresh_label ) /* set by properties dialog */ - { - /* Update the label */ - update_percent_label( battstat, &info ); - - /* done */ - battstat->refresh_label = FALSE; - } - - battstat->last_charging = info.charging; - battstat->last_batt_life = info.percent; - battstat->last_minutes = info.minutes; - battstat->last_acline_status = info.on_ac_power; - battstat->last_present = info.present; - - return TRUE; + + battstat->last_charging = info.charging; + battstat->last_batt_life = info.percent; + battstat->last_minutes = info.minutes; + battstat->last_acline_status = info.on_ac_power; + battstat->last_present = info.present; + + return TRUE; } /* Gets called when the user removes the applet from the panel. Clean up @@ -730,26 +743,27 @@ check_for_updates( gpointer data ) decrease our applet count (and possibly perform global cleanup) */ static void -destroy_applet( GtkWidget *widget, ProgressData *battstat ) +destroy_applet (GtkWidget *widget, + ProgressData *battstat) { - if (DEBUG) g_print("destroy_applet()\n"); + if (DEBUG) g_print ("destroy_applet ()\n"); - if (battstat->prop_win) - gtk_widget_destroy (GTK_WIDGET (battstat->prop_win)); + if (battstat->prop_win) + gtk_widget_destroy (GTK_WIDGET (battstat->prop_win)); - if( battstat->battery_low_dialog ) - battery_low_dialog_destroy( battstat ); + if (battstat->battery_low_dialog) + battery_low_dialog_destroy (battstat); - if (battstat->timeout_id) - g_source_remove (battstat->timeout_id); + if (battstat->timeout_id) + g_source_remove (battstat->timeout_id); - g_object_unref( G_OBJECT(battstat->status) ); - g_object_unref( G_OBJECT(battstat->percent) ); - g_object_unref (battstat->settings); + g_object_unref (G_OBJECT (battstat->status)); + g_object_unref (G_OBJECT (battstat->percent)); + g_object_unref (battstat->settings); - static_global_teardown (battstat); + static_global_teardown (battstat); - g_free (battstat); + g_free (battstat); } /* Common function invoked by the 'Help' context menu item and the 'Help' @@ -759,91 +773,92 @@ void battstat_show_help (ProgressData *battstat, const char *section) { - GError *error = NULL; - char *uri; - - if (section) - uri = g_strdup_printf ("help:mate-battstat/%s", section); - else - uri = g_strdup ("help:mate-battstat"); - - gtk_show_uri_on_window (NULL, - uri, - gtk_get_current_event_time (), - &error); - - g_free (uri); - - if (error) - { - char *message; - - message = g_strdup_printf (_("There was an error displaying help: %s"), - error->message ); - battstat_error_dialog (battstat->applet, message); - g_error_free (error); - g_free (message); - } + GError *error = NULL; + char *uri; + + if (section) + uri = g_strdup_printf ("help:mate-battstat/%s", section); + else + uri = g_strdup ("help:mate-battstat"); + + gtk_show_uri_on_window (NULL, + uri, + gtk_get_current_event_time (), + &error); + + g_free (uri); + + if (error) + { + char *message; + + message = g_strdup_printf (_("There was an error displaying help: %s"), + error->message ); + battstat_error_dialog (battstat->applet, message); + g_error_free (error); + g_free (message); + } } /* Called when the user selects the 'help' menu item. */ static void -help_cb( GtkAction *action, ProgressData *battstat ) +help_cb (GtkAction *action, + ProgressData *battstat) { - battstat_show_help( battstat, NULL ); + battstat_show_help (battstat, NULL); } /* Called when the user selects the 'about' menu item. */ static void -about_cb( GtkAction *action, ProgressData *battstat ) +about_cb (GtkAction *action, ProgressData *battstat) { - const gchar *authors[] = { - "J\xC3\xB6rgen Pehrson <[email protected]>", - "Lennart Poettering <[email protected]> (Linux ACPI support)", - "Seth Nickell <[email protected]> (GNOME2 port)", - "Davyd Madeley <[email protected]>", - "Ryan Lortie <[email protected]>", - "Joe Marcus Clarke <[email protected]> (FreeBSD ACPI support)", - NULL - }; - - const gchar *documenters[] = { - "J\xC3\xB6rgen Pehrson <[email protected]>", - "Trevor Curtis <[email protected]>", - "Davyd Madeley <[email protected]>", - N_("MATE Documentation Team"), - NULL - }; - - char *comments = g_strdup_printf ("%s\n\n%s", - _("This utility shows the status of your laptop battery."), - power_management_using_upower () ? - /* true */ _("upower backend enabled.") : - /* false */ _("Legacy backend enabled.")); + const gchar *authors[] = { + "J\xC3\xB6rgen Pehrson <[email protected]>", + "Lennart Poettering <[email protected]> (Linux ACPI support)", + "Seth Nickell <[email protected]> (GNOME2 port)", + "Davyd Madeley <[email protected]>", + "Ryan Lortie <[email protected]>", + "Joe Marcus Clarke <[email protected]> (FreeBSD ACPI support)", + NULL + }; + + const gchar *documenters[] = { + "J\xC3\xB6rgen Pehrson <[email protected]>", + "Trevor Curtis <[email protected]>", + "Davyd Madeley <[email protected]>", + N_("MATE Documentation Team"), + NULL + }; + + char *comments = g_strdup_printf ("%s\n\n%s", + _("This utility shows the status of your laptop battery."), + power_management_using_upower () ? + /* true */ _("upower backend enabled.") : + /* false */ _("Legacy backend enabled.")); #ifdef ENABLE_NLS - const char **p; - for (p = documenters; *p; ++p) - *p = _(*p); + const char **p; + for (p = documenters; *p; ++p) + *p = _(*p); #endif - gtk_show_about_dialog( NULL, - "title", _("About Battery Charge Monitor"), - "version", VERSION, - "copyright", _("Copyright \xc2\xa9 2000 The Gnulix Society\n" - "Copyright \xc2\xa9 2002-2005 Free Software Foundation and others\n" - "Copyright \xc2\xa9 2012-2020 MATE developers"), - "comments", comments, - "authors", authors, - "documenters", documenters, - "translator-credits", _("translator-credits"), - "logo-icon-name", "battery", - NULL ); - - g_free (comments); + gtk_show_about_dialog (NULL, + "title", _("About Battery Charge Monitor"), + "version", VERSION, + "copyright", _("Copyright \xc2\xa9 2000 The Gnulix Society\n" + "Copyright \xc2\xa9 2002-2005 Free Software Foundation and others\n" + "Copyright \xc2\xa9 2012-2020 MATE developers"), + "comments", comments, + "authors", authors, + "documenters", documenters, + "translator-credits", _("translator-credits"), + "logo-icon-name", "battery", + NULL); + + g_free (comments); } /* Rotate text on side panels. Called on initial startup and when the @@ -851,17 +866,16 @@ about_cb( GtkAction *action, ProgressData *battstat ) * another panel). */ static void -setup_text_orientation( ProgressData *battstat ) +setup_text_orientation (ProgressData *battstat) { - if( battstat->orienttype == MATE_PANEL_APPLET_ORIENT_RIGHT ) - gtk_label_set_angle( GTK_LABEL( battstat->percent ), 90 ); - else if( battstat->orienttype == MATE_PANEL_APPLET_ORIENT_LEFT ) - gtk_label_set_angle( GTK_LABEL( battstat->percent ), 270 ); - else - gtk_label_set_angle( GTK_LABEL( battstat->percent ), 0 ); + if (battstat->orienttype == MATE_PANEL_APPLET_ORIENT_RIGHT) + gtk_label_set_angle (GTK_LABEL (battstat->percent), 90); + else if (battstat->orienttype == MATE_PANEL_APPLET_ORIENT_LEFT) + gtk_label_set_angle (GTK_LABEL (battstat->percent), 270); + else + gtk_label_set_angle (GTK_LABEL (battstat->percent), 0); } - /* This signal is delivered by the panel when the orientation of the applet has changed. This is either because the applet has just been created, has just been moved to a new panel or the panel that the applet was on @@ -869,69 +883,70 @@ setup_text_orientation( ProgressData *battstat ) */ static void change_orient (MatePanelApplet *applet, - MatePanelAppletOrient orient, - ProgressData *battstat) + MatePanelAppletOrient orient, + ProgressData *battstat) { - if (DEBUG) g_print("change_orient()\n"); + if (DEBUG) g_print ("change_orient ()\n"); - /* Ignore the update if we already know. */ - if( orient != battstat->orienttype ) - { - battstat->orienttype = orient; + /* Ignore the update if we already know. */ + if (orient != battstat->orienttype) + { + battstat->orienttype = orient; - /* The applet changing orientation very likely involves the layout - being changed to better fit the new shape of the panel. - */ - setup_text_orientation( battstat ); - reconfigure_layout( battstat ); - } + /* The applet changing orientation very likely involves the layout + being changed to better fit the new shape of the panel. + */ + setup_text_orientation (battstat); + reconfigure_layout (battstat); + } } /* This is delivered when our size has changed. This happens when the applet is just created or if the size of the panel has changed. */ static void -size_allocate( MatePanelApplet *applet, GtkAllocation *allocation, - ProgressData *battstat) +size_allocate (MatePanelApplet *applet, + GtkAllocation *allocation, + ProgressData *battstat) { - if (DEBUG) g_print("applet_change_pixel_size()\n"); + if (DEBUG) g_print ("applet_change_pixel_size ()\n"); - /* Ignore the update if we already know. */ - if( battstat->width == allocation->width && - battstat->height == allocation->height ) - return; + /* Ignore the update if we already know. */ + if (battstat->width == allocation->width && + battstat->height == allocation->height) + return; - battstat->width = allocation->width; - battstat->height = allocation->height; + battstat->width = allocation->width; + battstat->height = allocation->height; - /* The applet changing size could result in the layout changing. */ - reconfigure_layout( battstat ); + /* The applet changing size could result in the layout changing. */ + reconfigure_layout (battstat); } /* Get our settings out of gsettings. */ static void -load_preferences(ProgressData *battstat) +load_preferences (ProgressData *battstat) { - GSettings *settings = battstat->settings; + GSettings *settings = battstat->settings; - if (DEBUG) g_print("load_preferences()\n"); + if (DEBUG) g_print ("load_preferences ()\n"); - battstat->red_val = g_settings_get_int (settings, "red-value"); - battstat->red_val = MIN (battstat->red_val, 100); - battstat->red_value_is_time = g_settings_get_boolean (settings, "red-value-is-time"); + battstat->red_val = g_settings_get_int (settings, "red-value"); + battstat->red_val = MIN (battstat->red_val, 100); + battstat->red_value_is_time = g_settings_get_boolean (settings, "red-value-is-time"); - /* automatically calculate orangle and yellow values from the red value */ - battstat->orange_val = battstat->red_val * ORANGE_MULTIPLIER; - battstat->orange_val = MIN (battstat->orange_val, 100); + /* automatically calculate orangle and yellow values from the red value */ + battstat->orange_val = battstat->red_val * ORANGE_MULTIPLIER; + battstat->orange_val = MIN (battstat->orange_val, 100); - battstat->yellow_val = battstat->red_val * YELLOW_MULTIPLIER; - battstat->yellow_val = MIN (battstat->yellow_val, 100); + battstat->yellow_val = battstat->red_val * YELLOW_MULTIPLIER; + battstat->yellow_val = MIN (battstat->yellow_val, 100); - battstat->lowbattnotification = g_settings_get_boolean (settings, "low-battery-notification"); - battstat->fullbattnot = g_settings_get_boolean (settings, "full-battery-notification"); - battstat->beep = g_settings_get_boolean (settings, "beep"); - battstat->showtext = g_settings_get_int (settings, "show-text"); + battstat->lowbattnotification = g_settings_get_boolean (settings, "low-battery-notification"); + battstat->fullbattnot = g_settings_get_boolean (settings, "full-battery-notification"); + battstat->beep = g_settings_get_boolean (settings, "beep"); + battstat->showtext = g_settings_get_int (settings, "show-text"); } /* Convenience function to attach a child widget to a GtkGrid in the @@ -939,41 +954,43 @@ load_preferences(ProgressData *battstat) gridss and only supports positions that are used in this applet. */ static void -grid_layout_attach (GtkGrid *grid, LayoutLocation loc, GtkWidget *child) +grid_layout_attach (GtkGrid *grid, + LayoutLocation loc, + GtkWidget *child) { - switch( loc ) - { - case LAYOUT_LONG: - gtk_grid_attach (grid, child, 1, 0, 1, 2); - break; - - case LAYOUT_TOPLEFT: - gtk_grid_attach (grid, child, 0, 0, 1, 1); - break; - - case LAYOUT_TOP: - gtk_grid_attach (grid, child, 1, 0, 1, 1); - break; - - case LAYOUT_LEFT: - gtk_grid_attach (grid, child, 0, 1, 1, 1); - break; - - case LAYOUT_CENTRE: - gtk_grid_attach (grid, child, 1, 1, 1, 1); - break; - - case LAYOUT_RIGHT: - gtk_grid_attach (grid, child, 2, 1, 1, 1); - break; - - case LAYOUT_BOTTOM: - gtk_grid_attach (grid, child, 1, 2, 1, 1); - break; - - default: - break; - } + switch (loc) + { + case LAYOUT_LONG: + gtk_grid_attach (grid, child, 1, 0, 1, 2); + break; + + case LAYOUT_TOPLEFT: + gtk_grid_attach (grid, child, 0, 0, 1, 1); + break; + + case LAYOUT_TOP: + gtk_grid_attach (grid, child, 1, 0, 1, 1); + break; + + case LAYOUT_LEFT: + gtk_grid_attach (grid, child, 0, 1, 1, 1); + break; + + case LAYOUT_CENTRE: + gtk_grid_attach (grid, child, 1, 1, 1, 1); + break; + + case LAYOUT_RIGHT: + gtk_grid_attach (grid, child, 2, 1, 1, 1); + break; + + case LAYOUT_BOTTOM: + gtk_grid_attach (grid, child, 1, 2, 1, 1); + break; + + default: + break; + } } /* The layout has (maybe) changed. Calculate what layout we ought to be @@ -982,132 +999,132 @@ grid_layout_attach (GtkGrid *grid, LayoutLocation loc, GtkWidget *child) when elements get added or removed. */ void -reconfigure_layout( ProgressData *battstat ) +reconfigure_layout (ProgressData *battstat) { - LayoutConfiguration c; - - /* Default to no elements being displayed. */ - c.status = c.text = LAYOUT_NONE; - - switch( battstat->orienttype ) - { - case MATE_PANEL_APPLET_ORIENT_UP: - case MATE_PANEL_APPLET_ORIENT_DOWN: - /* Stack horizontally for top and bottom panels. */ - c.status = LAYOUT_LEFT; - if( battstat->showtext ) - c.text = LAYOUT_RIGHT; - break; - - case MATE_PANEL_APPLET_ORIENT_LEFT: - case MATE_PANEL_APPLET_ORIENT_RIGHT: - /* Stack vertically for left and right panels. */ - c.status = LAYOUT_TOP; - if( battstat->showtext ) - c.text = LAYOUT_BOTTOM; - break; - } - - if( memcmp( &c, &battstat->layout, sizeof (LayoutConfiguration) ) ) - { - /* Something in the layout has changed. Rebuild. */ - - /* Start by removing any elements in the grid from the grid. */ - if( battstat->layout.text ) - gtk_container_remove( GTK_CONTAINER( battstat->grid ), - battstat->percent ); - if( battstat->layout.status ) - gtk_container_remove( GTK_CONTAINER( battstat->grid ), - battstat->status ); - - /* Attach the elements to their new locations. */ - grid_layout_attach( GTK_GRID(battstat->grid), - c.status, battstat->status ); - grid_layout_attach( GTK_GRID(battstat->grid), - c.text, battstat->percent ); - - gtk_widget_show_all( battstat->applet ); - } - - battstat->layout = c; - - /* Check for generic updates. This is required, for example, to make sure - the text label is immediately updated to show the time remaining or - percentage. - */ - check_for_updates( battstat ); + LayoutConfiguration c; + + /* Default to no elements being displayed. */ + c.status = c.text = LAYOUT_NONE; + + switch (battstat->orienttype) + { + case MATE_PANEL_APPLET_ORIENT_UP: + case MATE_PANEL_APPLET_ORIENT_DOWN: + /* Stack horizontally for top and bottom panels. */ + c.status = LAYOUT_LEFT; + if (battstat->showtext) + c.text = LAYOUT_RIGHT; + break; + + case MATE_PANEL_APPLET_ORIENT_LEFT: + case MATE_PANEL_APPLET_ORIENT_RIGHT: + /* Stack vertically for left and right panels. */ + c.status = LAYOUT_TOP; + if (battstat->showtext) + c.text = LAYOUT_BOTTOM; + break; + } + + if (memcmp (&c, &battstat->layout, sizeof (LayoutConfiguration))) + { + /* Something in the layout has changed. Rebuild. */ + + /* Start by removing any elements in the grid from the grid. */ + if (battstat->layout.text) + gtk_container_remove (GTK_CONTAINER (battstat->grid), + battstat->percent); + if (battstat->layout.status) + gtk_container_remove (GTK_CONTAINER (battstat->grid), + battstat->status); + + /* Attach the elements to their new locations. */ + grid_layout_attach (GTK_GRID (battstat->grid), + c.status, battstat->status); + grid_layout_attach (GTK_GRID (battstat->grid), + c.text, battstat->percent); + + gtk_widget_show_all (battstat->applet); + } + + battstat->layout = c; + + /* Check for generic updates. This is required, for example, to make sure + the text label is immediately updated to show the time remaining or + percentage. + */ + check_for_updates (battstat); } /* Allocate the widgets for the applet and connect our signals. */ static gint -create_layout(ProgressData *battstat) +create_layout (ProgressData *battstat) { - if (DEBUG) g_print("create_layout()\n"); - - /* Have our background automatically painted. */ - mate_panel_applet_set_background_widget( MATE_PANEL_APPLET( battstat->applet ), - GTK_WIDGET( battstat->applet ) ); - - /* Allocate the four widgets that we need. */ - battstat->grid = gtk_grid_new (); - battstat->percent = gtk_label_new( "" ); - battstat->status = gtk_image_new(); - - /* When you first get a pointer to a newly created GtkWidget it has one - 'floating' reference. When you first add this widget to a container - the container adds a real reference and removes the floating reference - if one exists. Since we insert/remove these widgets from the table - when our layout is reconfigured, we need to keep our own 'real' - reference to each widget. This adds a real reference to each widget - and "sinks" the floating reference. - */ - g_object_ref( battstat->status ); - g_object_ref( battstat->percent ); - g_object_ref_sink( G_OBJECT( battstat->status ) ); - g_object_ref_sink( G_OBJECT( battstat->percent ) ); - - /* Let reconfigure_layout know that the grid is currently empty. */ - battstat->layout.status = LAYOUT_NONE; - battstat->layout.text = LAYOUT_NONE; - - /* Put the grid directly inside the applet and show everything. */ - gtk_widget_set_halign (battstat->grid, GTK_ALIGN_CENTER); - gtk_widget_set_valign (battstat->grid, GTK_ALIGN_CENTER); - gtk_container_add (GTK_CONTAINER (battstat->applet), battstat->grid); - gtk_widget_show_all (battstat->applet); - - /* Attach all sorts of signals to the applet. */ - g_signal_connect(G_OBJECT(battstat->applet), - "destroy", - G_CALLBACK(destroy_applet), - battstat); - - g_signal_connect (battstat->applet, - "change_orient", - G_CALLBACK(change_orient), - battstat); - - g_signal_connect (battstat->applet, - "size_allocate", - G_CALLBACK (size_allocate), - battstat); - - return FALSE; + if (DEBUG) g_print ("create_layout ()\n"); + + /* Have our background automatically painted. */ + mate_panel_applet_set_background_widget (MATE_PANEL_APPLET (battstat->applet), + GTK_WIDGET (battstat->applet)); + + /* Allocate the four widgets that we need. */ + battstat->grid = gtk_grid_new (); + battstat->percent = gtk_label_new (""); + battstat->status = gtk_image_new (); + + /* When you first get a pointer to a newly created GtkWidget it has one + 'floating' reference. When you first add this widget to a container + the container adds a real reference and removes the floating reference + if one exists. Since we insert/remove these widgets from the table + when our layout is reconfigured, we need to keep our own 'real' + reference to each widget. This adds a real reference to each widget + and "sinks" the floating reference. + */ + g_object_ref (battstat->status); + g_object_ref (battstat->percent); + g_object_ref_sink (G_OBJECT (battstat->status)); + g_object_ref_sink (G_OBJECT (battstat->percent)); + + /* Let reconfigure_layout know that the grid is currently empty. */ + battstat->layout.status = LAYOUT_NONE; + battstat->layout.text = LAYOUT_NONE; + + /* Put the grid directly inside the applet and show everything. */ + gtk_widget_set_halign (battstat->grid, GTK_ALIGN_CENTER); + gtk_widget_set_valign (battstat->grid, GTK_ALIGN_CENTER); + gtk_container_add (GTK_CONTAINER (battstat->applet), battstat->grid); + gtk_widget_show_all (battstat->applet); + + /* Attach all sorts of signals to the applet. */ + g_signal_connect (G_OBJECT (battstat->applet), + "destroy", + G_CALLBACK (destroy_applet), + battstat); + + g_signal_connect (battstat->applet, + "change_orient", + G_CALLBACK (change_orient), + battstat); + + g_signal_connect (battstat->applet, + "size_allocate", + G_CALLBACK (size_allocate), + battstat); + + return FALSE; } void prop_cb (GtkAction *action, ProgressData *battstat) { - if (battstat->prop_win) { - gtk_window_set_screen (GTK_WINDOW (battstat->prop_win), - gtk_widget_get_screen (battstat->applet)); - gtk_window_present (GTK_WINDOW (battstat->prop_win)); - } else { - battstat->prop_win = battstat_preferences_new (battstat); - gtk_widget_show_all (GTK_WIDGET (battstat->prop_win)); - } + if (battstat->prop_win) { + gtk_window_set_screen (GTK_WINDOW (battstat->prop_win), + gtk_widget_get_screen (battstat->applet)); + gtk_window_present (GTK_WINDOW (battstat->prop_win)); + } else { + battstat->prop_win = battstat_preferences_new (battstat); + gtk_widget_show_all (GTK_WIDGET (battstat->prop_win)); + } } /* Called by the factory to fill in the fields for the applet. @@ -1115,92 +1132,97 @@ prop_cb (GtkAction *action, static gboolean battstat_applet_fill (MatePanelApplet *applet) { - ProgressData *battstat; - AtkObject *atk_widget; - GtkActionGroup *action_group; - const char *err; - - if (DEBUG) g_print("main()\n"); - - g_set_application_name (_("Battery Charge Monitor")); - - gtk_window_set_default_icon_name ("battery"); - - mate_panel_applet_set_flags (applet, MATE_PANEL_APPLET_EXPAND_MINOR); - - battstat = g_new0 (ProgressData, 1); - battstat->settings = mate_panel_applet_settings_new (applet, BATTSTAT_SCHEMA); - - /* Some starting values... */ - battstat->applet = GTK_WIDGET (applet); - battstat->refresh_label = TRUE; - battstat->last_batt_life = 1000; - battstat->last_acline_status = 1000; - battstat->last_charging = 1000; - battstat->orienttype = mate_panel_applet_get_orient (applet); - battstat->battery_low_dialog = NULL; - battstat->battery_low_label = NULL; - battstat->timeout = -1; - battstat->timeout_id = 0; - - /* The first received size_allocate event will cause a reconfigure. */ - battstat->height = -1; - battstat->width = -1; - - load_preferences (battstat); - create_layout (battstat); - setup_text_orientation( battstat ); - - action_group = gtk_action_group_new ("Battstat Applet Actions"); - gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE); - gtk_action_group_add_actions (action_group, - battstat_menu_actions, - G_N_ELEMENTS (battstat_menu_actions), - battstat); - - mate_panel_applet_setup_menu_from_resource (MATE_PANEL_APPLET (battstat->applet), - BATTSTAT_RESOURCE_PATH "battstat-applet-menu.xml", - action_group); - - if (mate_panel_applet_get_locked_down (MATE_PANEL_APPLET (battstat->applet))) { - GtkAction *action; - - action = gtk_action_group_get_action (action_group, "BattstatProperties"); - gtk_action_set_visible (action, FALSE); - } - g_object_unref (action_group); - - atk_widget = gtk_widget_get_accessible (battstat->applet); - if (GTK_IS_ACCESSIBLE (atk_widget)) { - atk_object_set_name (atk_widget, _("Battery Charge Monitor")); - atk_object_set_description(atk_widget, _("Monitor a laptop's remaining power")); - } - - if ((err = static_global_initialisation (battstat))) - battstat_error_dialog (GTK_WIDGET (applet), err); - - return TRUE; + ProgressData *battstat; + AtkObject *atk_widget; + GtkActionGroup *action_group; + const char *err; + + if (DEBUG) g_print ("main ()\n"); + + g_set_application_name (_("Battery Charge Monitor")); + + gtk_window_set_default_icon_name ("battery"); + + mate_panel_applet_set_flags (applet, + MATE_PANEL_APPLET_EXPAND_MINOR); + + battstat = g_new0 (ProgressData, 1); + battstat->settings = mate_panel_applet_settings_new (applet, + BATTSTAT_SCHEMA); + + /* Some starting values... */ + battstat->applet = GTK_WIDGET (applet); + battstat->refresh_label = TRUE; + battstat->last_batt_life = 1000; + battstat->last_acline_status = 1000; + battstat->last_charging = 1000; + battstat->orienttype = mate_panel_applet_get_orient (applet); + battstat->battery_low_dialog = NULL; + battstat->battery_low_label = NULL; + battstat->timeout = -1; + battstat->timeout_id = 0; + + /* The first received size_allocate event will cause a reconfigure. */ + battstat->height = -1; + battstat->width = -1; + + load_preferences (battstat); + create_layout (battstat); + setup_text_orientation (battstat); + + action_group = gtk_action_group_new ("Battstat Applet Actions"); + gtk_action_group_set_translation_domain (action_group, + GETTEXT_PACKAGE); + gtk_action_group_add_actions (action_group, + battstat_menu_actions, + G_N_ELEMENTS (battstat_menu_actions), + battstat); + + mate_panel_applet_setup_menu_from_resource (MATE_PANEL_APPLET (battstat->applet), + BATTSTAT_RESOURCE_PATH "battstat-applet-menu.xml", + action_group); + + if (mate_panel_applet_get_locked_down (MATE_PANEL_APPLET (battstat->applet))) { + GtkAction *action; + + action = gtk_action_group_get_action (action_group, + "BattstatProperties"); + gtk_action_set_visible (action, FALSE); + } + g_object_unref (action_group); + + atk_widget = gtk_widget_get_accessible (battstat->applet); + if (GTK_IS_ACCESSIBLE (atk_widget)) { + atk_object_set_name (atk_widget, + _("Battery Charge Monitor")); + atk_object_set_description (atk_widget, + _("Monitor a laptop's remaining power")); + } + + if ((err = static_global_initialisation (battstat))) + battstat_error_dialog (GTK_WIDGET (applet), err); + + return TRUE; } /* Boilerplate... */ static gboolean battstat_applet_factory (MatePanelApplet *applet, - const gchar *iid, - gpointer data) + const gchar *iid, + gpointer data) { - gboolean retval = FALSE; + gboolean retval = FALSE; - if (!strcmp (iid, "BattstatApplet")) - retval = battstat_applet_fill (applet); + if (!strcmp (iid, "BattstatApplet")) + retval = battstat_applet_fill (applet); - return retval; + return retval; } MATE_PANEL_APPLET_OUT_PROCESS_FACTORY ("BattstatAppletFactory", - PANEL_TYPE_APPLET, - "battstat", - battstat_applet_factory, - NULL) - + PANEL_TYPE_APPLET, + "battstat", + battstat_applet_factory, + NULL) diff --git a/battstat/power-management.c b/battstat/power-management.c index ca208891..8a19aed2 100644 --- a/battstat/power-management.c +++ b/battstat/power-management.c @@ -94,19 +94,19 @@ static int using_upower; BatteryStatus test_status; static void -test_update_boolean( GtkToggleButton *button, gboolean *value ) +test_update_boolean (GtkToggleButton *button, gboolean *value) { - *value = gtk_toggle_button_get_active( button ); + *value = gtk_toggle_button_get_active (button); } static void -test_update_integer( GtkSpinButton *spin, gint *value ) +test_update_integer (GtkSpinButton *spin, gint *value) { - *value = gtk_spin_button_get_value_as_int( spin ); + *value = gtk_spin_button_get_value_as_int (spin); } static void -initialise_test( void ) +initialise_test (void) { GtkWidget *w; GtkBox *box; @@ -117,43 +117,43 @@ initialise_test( void ) test_status.on_ac_power = FALSE; test_status.charging = FALSE; - box = GTK_BOX( gtk_box_new (GTK_ORIENTATION_VERTICAL, 5 ) ); - - gtk_box_pack_start( box, gtk_label_new( "percent" ), TRUE, TRUE, 0); - w = gtk_spin_button_new_with_range( -1.0, 100.0, 1 ); - gtk_spin_button_set_value( GTK_SPIN_BUTTON( w ), 50.0 ); - g_signal_connect( G_OBJECT( w ), "value-changed", - G_CALLBACK( test_update_integer ), &test_status.percent ); - gtk_box_pack_start( box, w, TRUE, TRUE, 0 ); - - gtk_box_pack_start( box, gtk_label_new( "minutes" ), TRUE, TRUE, 0); - w = gtk_spin_button_new_with_range( -1.0, 1000.0, 1 ); - gtk_spin_button_set_value( GTK_SPIN_BUTTON( w ), 180.0 ); - g_signal_connect( G_OBJECT( w ), "value-changed", - G_CALLBACK( test_update_integer ), &test_status.minutes ); - gtk_box_pack_start( box, w, TRUE, TRUE, 0); - - - w = gtk_toggle_button_new_with_label( "on_ac_power" ); - g_signal_connect( G_OBJECT( w ), "toggled", - G_CALLBACK( test_update_boolean ), - &test_status.on_ac_power ); - gtk_box_pack_start( box, w, TRUE, TRUE, 0); - - w = gtk_toggle_button_new_with_label( "charging" ); - g_signal_connect( G_OBJECT( w ), "toggled", - G_CALLBACK( test_update_boolean ), &test_status.charging ); - gtk_box_pack_start( box, w, TRUE, TRUE, 0); - - w = gtk_toggle_button_new_with_label( "present" ); - gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), TRUE ); - g_signal_connect( G_OBJECT( w ), "toggled", - G_CALLBACK( test_update_boolean ), &test_status.present ); - gtk_box_pack_start( box, w, TRUE, TRUE, 0); - - w = gtk_window_new( GTK_WINDOW_TOPLEVEL ); - gtk_container_add( GTK_CONTAINER( w ), GTK_WIDGET( box ) ); - gtk_widget_show_all( w ); + box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 5)); + + gtk_box_pack_start (box, gtk_label_new ("percent"), TRUE, TRUE, 0); + w = gtk_spin_button_new_with_range (-1.0, 100.0, 1); + gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), 50.0); + g_signal_connect (G_OBJECT (w), "value-changed", + G_CALLBACK (test_update_integer), &test_status.percent); + gtk_box_pack_start (box, w, TRUE, TRUE, 0); + + gtk_box_pack_start (box, gtk_label_new ("minutes"), TRUE, TRUE, 0); + w = gtk_spin_button_new_with_range (-1.0, 1000.0, 1); + gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), 180.0); + g_signal_connect (G_OBJECT (w), "value-changed", + G_CALLBACK (test_update_integer), &test_status.minutes); + gtk_box_pack_start (box, w, TRUE, TRUE, 0); + + + w = gtk_toggle_button_new_with_label ("on_ac_power"); + g_signal_connect (G_OBJECT (w), "toggled", + G_CALLBACK (test_update_boolean), + &test_status.on_ac_power); + gtk_box_pack_start (box, w, TRUE, TRUE, 0); + + w = gtk_toggle_button_new_with_label ("charging"); + g_signal_connect (G_OBJECT (w), "toggled", + G_CALLBACK (test_update_boolean), &test_status.charging); + gtk_box_pack_start (box, w, TRUE, TRUE, 0); + + w = gtk_toggle_button_new_with_label ("present"); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), TRUE); + g_signal_connect (G_OBJECT (w), "toggled", + G_CALLBACK (test_update_boolean), &test_status.present); + gtk_box_pack_start (box, w, TRUE, TRUE, 0); + + w = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_container_add (GTK_CONTAINER (w), GTK_WIDGET (box)); + gtk_widget_show_all (w); } static const char * @@ -161,8 +161,8 @@ apm_readinfo (BatteryStatus *status) { static int test_initialised; - if( !test_initialised ) - initialise_test(); + if (!test_initialised) + initialise_test (); test_initialised = 1; *status = test_status; @@ -189,13 +189,13 @@ apm_readinfo (BatteryStatus *status) { int fd; - if (DEBUG) g_print("apm_readinfo() (FreeBSD)\n"); + if (DEBUG) g_print ("apm_readinfo () (FreeBSD)\n"); if (using_acpi) { if (acpi_count <= 0) { acpi_count = 30; - acpi_process_event(&acpiinfo); - if (acpi_freebsd_read(&apminfo, &acpiinfo) == FALSE) + acpi_process_event (&acpiinfo); + if (acpi_freebsd_read (&apminfo, &acpiinfo) == FALSE) return ERR_FREEBSD_ACPI; } acpi_count--; @@ -206,17 +206,17 @@ apm_readinfo (BatteryStatus *status) FreeBSD. Each time this functions is called (once every second) the APM device is opened, read from and then closed. */ - fd = open(APMDEVICE, O_RDONLY); + fd = open (APMDEVICE, O_RDONLY); if (fd == -1) { return ERR_OPEN_APMDEV; } - if (ioctl(fd, APMIO_GETINFO, &apminfo) == -1) - err(1, "ioctl(APMIO_GETINFO)"); + if (ioctl (fd, APMIO_GETINFO, &apminfo) == -1) + err (1, "ioctl (APMIO_GETINFO)"); - close(fd); + close (fd); - if(apminfo.ai_status == 0) + if (apminfo.ai_status == 0) return ERR_APM_E; } @@ -257,9 +257,9 @@ apm_readinfo (BatteryStatus *status) int fd; #if defined(__NetBSD__) - if (DEBUG) g_print("apm_readinfo() (NetBSD)\n"); + if (DEBUG) g_print ("apm_readinfo () (NetBSD)\n"); #else /* __OpenBSD__ */ - if (DEBUG) g_print("apm_readinfo() (OpenBSD)\n"); + if (DEBUG) g_print ("apm_readinfo () (OpenBSD)\n"); #endif fd = open(APMDEVICE, O_RDONLY); @@ -268,9 +268,9 @@ apm_readinfo (BatteryStatus *status) pm_initialised = 0; return ERR_OPEN_APMDEV; } - if (ioctl(fd, APM_IOC_GETPOWER, &apminfo) == -1) - err(1, "ioctl(APM_IOC_GETPOWER)"); - close(fd); + if (ioctl (fd, APM_IOC_GETPOWER, &apminfo) == -1) + err(1, "ioctl (APM_IOC_GETPOWER)"); + close (fd); status->present = TRUE; status->on_ac_power = apminfo.ac_state ? 1 : 0; @@ -295,13 +295,13 @@ static struct apm_info apminfo; static gboolean acpi_callback (GIOChannel * chan, GIOCondition cond, gpointer data) { if (cond & (G_IO_ERR | G_IO_HUP)) { - acpi_linux_cleanup(&acpiinfo); + acpi_linux_cleanup (&acpiinfo); apminfo.battery_percentage = -1; return FALSE; } - if (acpi_process_event(&acpiinfo)) { - acpi_linux_read(&apminfo, &acpiinfo); + if (acpi_process_event (&acpiinfo)) { + acpi_linux_read (&apminfo, &acpiinfo); } return TRUE; } @@ -309,35 +309,35 @@ static gboolean acpi_callback (GIOChannel * chan, GIOCondition cond, gpointer da static const char * apm_readinfo (BatteryStatus *status) { - /* Code for Linux by Thomas Hood <[email protected]>. apm_read() will + /* Code for Linux by Thomas Hood <[email protected]>. apm_read () will read from /proc/... instead and we do not need to open the device ourselves. */ - if (DEBUG) g_print("apm_readinfo() (Linux)\n"); + if (DEBUG) g_print ("apm_readinfo () (Linux)\n"); /* ACPI support added by Lennart Poettering <[email protected]> 10/27/2001 * Updated by David Moore <[email protected]> 5/29/2003 to poll less and * use ACPI events. */ if (using_acpi && acpiinfo.event_fd >= 0) { if (acpi_count <= 0) { - /* Only call this one out of 30 calls to apm_readinfo() (every 30 seconds) + /* Only call this one out of 30 calls to apm_readinfo () (every 30 seconds) * since reading the ACPI system takes CPU cycles. */ acpi_count=30; - acpi_linux_read(&apminfo, &acpiinfo); + acpi_linux_read (&apminfo, &acpiinfo); } acpi_count--; } /* If we lost the file descriptor with ACPI events, try to get it back. */ else if (using_acpi) { - if (acpi_linux_init(&acpiinfo)) { + if (acpi_linux_init (&acpiinfo)) { acpiwatch = g_io_add_watch (acpiinfo.channel, G_IO_IN | G_IO_ERR | G_IO_HUP, acpi_callback, NULL); - acpi_linux_read(&apminfo, &acpiinfo); + acpi_linux_read (&apminfo, &acpiinfo); } } else - apm_read(&apminfo); + apm_read (&apminfo); status->present = TRUE; status->on_ac_power = apminfo.ac_line_status ? 1 : 0; @@ -379,11 +379,11 @@ apm_readinfo (BatteryStatus *status) * the problem might be. This error message is not to be freed. */ const char * -power_management_getinfo( BatteryStatus *status ) +power_management_getinfo (BatteryStatus *status) { const char *retval; - if( !pm_initialised ) + if (!pm_initialised) { status->on_ac_power = TRUE; status->minutes = -1; @@ -395,27 +395,27 @@ power_management_getinfo( BatteryStatus *status ) } #ifdef HAVE_UPOWER - if( using_upower) + if (using_upower) { - battstat_upower_get_battery_info( status ); + battstat_upower_get_battery_info (status); return NULL; } #endif - retval = apm_readinfo( status ); + retval = apm_readinfo (status); - if(status->percent == -1) { + if (status->percent == -1) { status->percent = 0; status->present = FALSE; } - if(status->percent > 100) + if (status->percent > 100) status->percent = 100; - if(status->percent == 100) + if (status->percent == 100) status->charging = FALSE; - if(!status->on_ac_power) + if (!status->on_ac_power) status->charging = FALSE; return retval; @@ -442,7 +442,7 @@ power_management_initialise (void (*callback) (void)) err = battstat_upower_initialise (callback); - if( err == NULL ) /* UPOWER is up */ + if (err == NULL) /* UPOWER is up */ { pm_initialised = 1; using_upower = TRUE; @@ -450,12 +450,12 @@ power_management_initialise (void (*callback) (void)) } else /* fallback to legacy methods */ - g_free( err ); + g_free (err); #endif /* HAVE_UPOWER */ #ifdef __linux__ - if (acpi_linux_init(&acpiinfo)) { + if (acpi_linux_init (&acpiinfo)) { using_acpi = TRUE; acpi_count = 0; } @@ -464,21 +464,21 @@ power_management_initialise (void (*callback) (void)) /* If neither ACPI nor APM could be read, but ACPI does seem to be * installed, warn the user how to get ACPI working. */ - if (!using_acpi && (apm_exists() == 1) && - (stat("/proc/acpi", &statbuf) == 0)) { + if (!using_acpi && (apm_exists () == 1) && + (stat ("/proc/acpi", &statbuf) == 0)) { using_acpi = TRUE; acpi_count = 0; return ERR_ACPID; } - /* Watch for ACPI events and handle them immediately with acpi_callback(). */ + /* Watch for ACPI events and handle them immediately with acpi_callback (). */ if (using_acpi && acpiinfo.event_fd >= 0) { acpiwatch = g_io_add_watch (acpiinfo.channel, G_IO_IN | G_IO_ERR | G_IO_HUP, acpi_callback, NULL); } #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - if (acpi_freebsd_init(&acpiinfo)) { + if (acpi_freebsd_init (&acpiinfo)) { using_acpi = TRUE; acpi_count = 0; } @@ -496,12 +496,12 @@ power_management_initialise (void (*callback) (void)) * Perform any cleanup that might be required. */ void -power_management_cleanup( void ) +power_management_cleanup (void) { #ifdef HAVE_UPOWER - if( using_upower) + if (using_upower) { - battstat_upower_cleanup(); + battstat_upower_cleanup (); pm_initialised = 1; return; } @@ -511,13 +511,13 @@ power_management_cleanup( void ) if (using_acpi) { if (acpiwatch != 0) - g_source_remove(acpiwatch); + g_source_remove (acpiwatch); acpiwatch = 0; - acpi_linux_cleanup(&acpiinfo); + acpi_linux_cleanup (&acpiinfo); } #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) if (using_acpi) { - acpi_freebsd_cleanup(&acpiinfo); + acpi_freebsd_cleanup (&acpiinfo); } #endif @@ -525,7 +525,7 @@ power_management_cleanup( void ) } int -power_management_using_upower( void) +power_management_using_upower (void) { #ifdef HAVE_UPOWER return using_upower; |