summaryrefslogtreecommitdiff
path: root/batwarn_daemon.c
blob: e7d61298dc469e3797a89dd12473bcea8f2add9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// For nanosleep(), pread()
#define _POSIX_C_SOURCE 200809L

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <assert.h>


// Warning when battery percentage is <= this
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};

// Format string with percentage %d
static const char *MESSAGE_FORMAT = "Battery at %d%%!";


static inline void parse(const char *buffer, bool *discharging, int *charge, int *full) {
	while (true) {
		if (memcmp(buffer, "POWER_SUPPLY_STATUS", 19) == 0) {
			*discharging = memcmp(buffer + 20, "Discharging", 11) == 0;
		} else if (memcmp(buffer, "POWER_SUPPLY_CHARGE_NOW", 23) == 0) {
			*charge = strtol(buffer + 24, NULL, 10);
		} else if (memcmp(buffer, "POWER_SUPPLY_CHARGE_FULL_DESIGN", 31) == 0) {
			*full = strtol(buffer + 32, NULL, 10);
		}
		buffer = strchr(buffer, '\n');
		if (buffer != NULL) buffer++;
		else break;
	}
}

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) {
		perror("open");
		return 1;
	}

	char buffer[1024];

	bool prev_less_than_hi = false;

	while (true) {
		ssize_t nr = pread(fd, buffer, sizeof buffer, 0);
		if (nr < 0) {
			perror("pread");
			break;
		}

		bool discharging = false;
		int charge = 1, full = 1;
		parse(buffer, &discharging, &charge, &full);
		int percentage = charge * 100 / full;

		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);
	}

	if (close(fd) < 0) {
		perror("close");
		return 1;
	}
}