summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <t.j.smeding@uu.nl>2022-07-31 13:57:24 +0200
committerTom Smeding <t.j.smeding@uu.nl>2022-07-31 13:57:24 +0200
commitba3d42030decabe0a8f3c5c32f845114fba677e5 (patch)
treec82edd9b1ddddd64fc580d3a517b34f27c50f0e0
parentce255302720ab5c53f0fcb04abbe2c4738ae251d (diff)
Warning nagbar when half-fullHEADmaster
-rw-r--r--batwarn_daemon.c51
1 files changed, 35 insertions, 16 deletions
diff --git a/batwarn_daemon.c b/batwarn_daemon.c
index 1cd1d17..e7d6129 100644
--- a/batwarn_daemon.c
+++ b/batwarn_daemon.c
@@ -14,7 +14,9 @@
// Warning when battery percentage is <= this
-static const int WARN_PERC = 10;
+static const int WARN_LO = 10;
+// Warning when battery percentage is >= this (only warns once per charge cycle)
+static const int WARN_HI = 65;
// Time to sleep between checks
static const struct timespec SLEEP_SPEC = {60, 0};
@@ -38,6 +40,28 @@ static inline void parse(const char *buffer, bool *discharging, int *charge, int
}
}
+static void show_nagbar(int percentage, const char *nagtype) {
+ pid_t pid = fork();
+ if (pid < 0) perror("fork");
+ else if (pid == 0) {
+ char msg[strlen(MESSAGE_FORMAT) + 16];
+ snprintf(msg, sizeof msg, MESSAGE_FORMAT, percentage);
+
+ char nagtypebuf[16];
+ assert(strlen(nagtype) < sizeof nagtypebuf);
+ strcpy(nagtypebuf, nagtype);
+
+ char *argv[6] = {"i3-nagbar", "-m", msg, "-t", nagtypebuf, NULL};
+ execv("/usr/bin/i3-nagbar", argv);
+ perror("execv");
+ exit(255);
+ } else {
+ int status;
+ do if (waitpid(pid, &status, 0) < 0 && errno == ECHILD) break;
+ while (!WIFEXITED(status) && !WIFSIGNALED(status));
+ }
+}
+
int main(void) {
int fd = open("/sys/class/power_supply/BAT1/uevent", O_RDONLY);
if (fd < 0) {
@@ -47,6 +71,8 @@ int main(void) {
char buffer[1024];
+ bool prev_less_than_hi = false;
+
while (true) {
ssize_t nr = pread(fd, buffer, sizeof buffer, 0);
if (nr < 0) {
@@ -57,22 +83,15 @@ int main(void) {
bool discharging = false;
int charge = 1, full = 1;
parse(buffer, &discharging, &charge, &full);
+ int percentage = charge * 100 / full;
- if (full > 0 && discharging && charge * 100 / full <= WARN_PERC) {
- pid_t pid = fork();
- if (pid < 0) perror("fork");
- else if (pid == 0) {
- char msg[strlen(MESSAGE_FORMAT) + 16];
- snprintf(msg, sizeof msg, MESSAGE_FORMAT, charge * 100 / full);
- char *argv[4] = {"i3-nagbar", "-m", msg, NULL};
- execv("/usr/bin/i3-nagbar", argv);
- perror("execv");
- exit(255);
- } else {
- int status;
- do if (waitpid(pid, &status, 0) < 0 && errno == ECHILD) break;
- while (!WIFEXITED(status) && !WIFSIGNALED(status));
- }
+ if (full > 0 && discharging && percentage <= WARN_LO) {
+ show_nagbar(percentage, "error");
+ } else if (percentage < WARN_HI) {
+ prev_less_than_hi = true;
+ } else if (prev_less_than_hi) {
+ prev_less_than_hi = false;
+ show_nagbar(percentage, "warning");
}
(void)nanosleep(&SLEEP_SPEC, NULL);