aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 9fae9e19bca079be50d40e06df319c212b31c09b (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
#include <jo_lowbat.hpp>
#include <cstring>
#include <thread>
#include <chrono>
#include <algorithm>

using namespace this_thread;
using namespace chrono;

int main(int argc, const char *argv[]) {
	string*	msg;
	string	acstat;
	string	batlvl;
	string	arg_one;
	string	arg_two;
	int		batlvlint;

	if (argc > 1) {
		arg_one = argv[1];
	}
	while (true) {
		acstat = jo_exec("acpi | awk '{print $3}' | rev | cut -c 2- | rev");
		batlvl = jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
		acstat.erase(remove(acstat.begin(), acstat.end(), '\n'), acstat.end());
		batlvl.erase(remove(batlvl.begin(), batlvl.end(), '\n'), batlvl.end());
		batlvlint = stoi(batlvl);
		if (batlvlint < 15) {
			while (!strcmp(acstat.c_str(), "Discharging")) {
				jo_notify(batlvl);
				if (strcmp(arg_one.c_str(), "--silent")) {
					if (argc > 2 && !strcmp(arg_one.c_str(), "--say")) {
						arg_two = argv[2];
						msg = new string(arg_two);
					}
					else {
						msg = new string("beep beep - low battery");
					}
					jo_speak(*msg);
					delete msg;
				}
				sleep_for(seconds(20));
				acstat.clear();
				batlvl.clear();
				acstat = jo_exec("acpi | awk '{print $3}' | rev | cut -c 2- | rev");
				batlvl = jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
				acstat.erase(remove(acstat.begin(), acstat.end(), '\n'), acstat.end());
				batlvl.erase(remove(batlvl.begin(), batlvl.end(), '\n'), batlvl.end());
			}
		}
		sleep_for(seconds(240));
	}

	return 0;
}