diff options
-rw-r--r-- | lowbat.1 | 32 | ||||
-rw-r--r-- | lowbat.c | 40 |
2 files changed, 51 insertions, 21 deletions
@@ -1,17 +1,35 @@ -.TH LOWBAT 1 lowbat\-1.0 +.TH LOWBAT 1 lowbat\-1.2 .SH NAME lowbat - A minimal battery level monitor daemon, written in C .SH DESCRIPTION lowbat is configured by default to check the system battery level every 3 -seconds. It is able to detect each battery installed and the system, and -calculate the remaining capacity of all batteries combined. Batteries added to -the system during runtime will also be detected. +seconds. It is able to detect each of the system's batteries, and calculate the +remaining sum capacity of all installed batteries. Additional batteries +installed during runtime will also be detected and summed. .P -The daemon will send a warning notification the user when their battery level -drops below 20% while the batteries are discharging, and another critical +The daemon will send a warning notification the user when the sum battery level +drops below 20% while any battery is discharging, and another critical warning at 5%. +.SH BATTERY STATUS LOG +The current charging status and sum capacity will be logged in stdout whenever +a change is detected. .P -The current battery level and charging state will also be displayed in stdout. +Each possible battery status value is assigned a single character symbol which +will prefix the current battery level. E.g. `-50%` if any attached battery is +currently discharging and the current sum capacity equals 50%. The symbols are +defined as follows in order of highest to lowest priority: +.TP +.B - +Discharging +.TP +.B + +Charging +.TP +.B = +Full / Not charging +.TP +.B ? +Unknown .SH USAGE Call the lowbat executable in your .B @@ -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); } |