diff options
Diffstat (limited to 'lowbat.c')
-rw-r--r-- | lowbat.c | 40 |
1 files changed, 26 insertions, 14 deletions
@@ -21,6 +21,7 @@ static char batteries[MAX_BATTERIES][MAX_PSU_NAME_LEN]; static unsigned int num_batteries; static FILE *fp; static char prop_path[MAX_PROP_PATH_LEN]; +static enum StatusSymbols { UNKNOWN = '?', CHARGING = '+', DISCHARGING = '-', NOT_CHARGING = '=', FULL = '=' }; // functions void load_installed_batteries() { @@ -111,33 +112,44 @@ int get_total_capacity() { return energy_now * 100 / energy_full; } -int is_discharging() { - char status[12]; // valid values: "Unknown", "Charging", "Discharging", "Not charging", "Full" +char get_status() { + char status[MAX_BATTERIES]; int i; if (!num_batteries) - return 0; + return UNKNOWN; + /* store first letter of each battery's status + * valid values: "Unknown", "Charging", "Discharging", "Not charging", "Full" */ + // return discharging if any batteries are discharging for (i = 0; i < num_batteries; i++) { - read_prop(batteries[i], "status", "%s", &status); - if (strcmp(status, "Discharging") != 0) - return 0; + read_prop(batteries[i], "status", "%c", &status[i]); + if (status[i] == 'D') + return DISCHARGING; } - - return 1; + // return charging if any batteries are charging + if (strchr(status, 'C') != NULL) + return CHARGING; + // return not charging if any batteries are not charging + if (strchr(status, 'F') != NULL || strchr(status, 'N') != NULL) + return NOT_CHARGING; + + // return unknown otherwise + return UNKNOWN; } int main() { - int discharging, capacity, old_discharging = -1, old_capacity = -1; + char status, old_status = -1; + int capacity, old_capacity = -1; int warning_level = 0; // 1 if warned already, 2 if warned with critical warning char msg[MAX_MSG_LEN]; while (1) { load_installed_batteries(); - discharging = is_discharging(); + status = get_status(); capacity = get_total_capacity(); - if (discharging) { + if (status == DISCHARGING) { if (capacity <= 5 && warning_level < 2) { snprintf(msg, MAX_MSG_LEN, "%d%% remains", capacity); notifysend("Critical Low Battery Warning", msg, NOTIFY_URGENCY_CRITICAL); @@ -153,11 +165,11 @@ int main() { warning_level = 0; // clear conditions } - if (discharging != old_discharging || capacity != old_capacity) { - printf("%s%d%%\n", discharging ? "-" : "+", capacity); + if (status != old_status || capacity != old_capacity) { + printf("%c%d%%\n", status, capacity); fflush(stdout); } - old_discharging = discharging; + old_status = status; old_capacity = capacity; sleep(PERIOD); } |