aboutsummaryrefslogtreecommitdiffstats
path: root/src/jo_lowbat.cpp
blob: f4c3b2e7b4c0d927445742fb228dab451adb2e7f (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
#include <jo_lowbat.hpp>
#include <iostream>
#include <array>
#include <memory>
#include <algorithm>

using namespace std;

uint8_t
Lowbat::jo_testAcpi(void) {
	if (system("acpi > /dev/null 2>&1")) {
		cout << "acpi is not installed. Please install it in order to run lowbat." << endl;
		return 1;
	}
	return 0;
}

int
Lowbat::jo_fetchBatlvl(void) {
	cout << "Fetching batlvl: ";
	m_batlvl = Lowbat::jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
	m_batlvl.erase(remove(m_batlvl.begin(), m_batlvl.end(), '\n'), m_batlvl.end());
	cout << m_batlvl << "%" << endl;
	return(stoi(m_batlvl));
}

string
Lowbat::jo_exec(const char* cmd) {
    array<char, 128> buffer;
    string result;
    unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
    if (!pipe) {
        throw runtime_error("popen() failed!");
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
    return result;
}

int
Lowbat::jo_notify(void) {
	string		str;

	str = "notify-send \"Low battery: ";
	str += m_batlvl;
	str += "%\" \"Please plug in computer\" -u critical -t 15000";
	cout << "Notifying" << endl;
	return system(str.c_str());
}