diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/jo_lowbat.cpp | 93 | ||||
-rw-r--r-- | src/main.cpp | 15 |
2 files changed, 55 insertions, 53 deletions
diff --git a/src/jo_lowbat.cpp b/src/jo_lowbat.cpp index 26e3713..84905ea 100644 --- a/src/jo_lowbat.cpp +++ b/src/jo_lowbat.cpp @@ -1,5 +1,7 @@ #include <jo_lowbat.hpp> #include <iostream> +#include <fstream> +#include <cstring> #include <array> #include <memory> #include <algorithm> @@ -7,69 +9,65 @@ using namespace std; uint8_t -Lowbat::jo_testAcpi(void) { - if (system("which acpi > /dev/null 2>&1")) { - cerr << "acpi is not installed. Please install it in order to run lowbat" << endl; - return 1; - } - cout << "acpi is installed" << endl; - return 0; -} - -uint8_t Lowbat::jo_testNotifySend(void) { - if (system("which notify-send > /dev/null 2>&1")) { + if (system("type notify-send > /dev/null 2>&1")) { cerr << "notify-send is not installed. Please install it in order to run lowbat" << endl; - return 1; + return (1); } cout << "notify-send is installed" << endl; - return 0; + return (0); } uint8_t Lowbat::jo_testEspeak(void) { - if (system("which espeak > /dev/null 2>&1")) { + if (system("type espeak > /dev/null 2>&1")) { cerr << "espeak is not installed. Please install it in order to run --say option" << endl; - return 1; + return (1); } cout << "espeak is installed" << endl; - return 0; + return (0); } int Lowbat::jo_fetchBatlvl(void) { + ifstream bat; + cout << "Fetching batlvl: "; - m_batlvl = Lowbat::jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev"); + bat.open("/sys/class/power_supply/BAT0/capacity"); + if (!bat.is_open()) { + cerr << "Failed to open battery info file" << endl; + exit(JO_RET_OPEN_FAILED); + } + getline(bat, m_batlvl); + bat.close(); + cout << m_batlvl << endl; m_batlvl.erase(remove(m_batlvl.begin(), m_batlvl.end(), '\n'), m_batlvl.end()); cout << m_batlvl << "%" << endl; - return stoi(m_batlvl); + return (stoi(m_batlvl)); } -int +uint8_t Lowbat::jo_fetchAcstat(void) { - int ret; + ifstream stat; + string l; + uint8_t ret; cout << "Fetching acstat: "; - ret = system("acpi | grep -q Discharging"); - if (ret != 0) - cout << "Charging" << endl; - else - cout << "Discharging" << endl; - return ret; -} - -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; + stat.open("/sys/class/power_supply/BAT0/status"); + if (!stat.is_open()) { + cerr << "Failed to open battery info file" << endl; + exit(JO_RET_OPEN_FAILED); + } + getline(stat, l); + cout << l << endl; + if (!strcmp("Discharging", l.c_str())) { + ret = 0; + } + else { + ret = 1; + } + stat.close(); + return (ret); } int @@ -80,7 +78,7 @@ Lowbat::jo_notify(void) { str += m_batlvl; str += "%\" \"Please plug in computer\" -u critical -t 15000"; cout << "Notifying" << endl; - return system(str.c_str()); + return (system(str.c_str())); } int @@ -88,13 +86,18 @@ Lowbat::jo_speak(void) { string str; str = "echo \""; - str += m_msg; + str += m_msg->c_str(); str += "\" | espeak"; - cout << "Speaking: " << m_msg << endl; - return system(str.c_str()); + cout << "Speaking: " << m_msg->c_str() << endl; + return (system(str.c_str())); } void Lowbat::jo_setMsg(const char *msg) { - m_msg = msg; + m_msg = new string(msg); +} + +void +Lowbat::jo_delMsg(void) { + delete m_msg; } diff --git a/src/main.cpp b/src/main.cpp index 54f8a0f..2452840 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,15 +13,11 @@ int main(int argc, const char *argv[]) { uint8_t speaks; speaks = 0; - if (lowbat.jo_testAcpi()) { - return 1; - } if (lowbat.jo_testNotifySend()) { - return 2; + return (JO_RET_MISS_DEP); } if (argc > 2 && !strcmp(argv[1], "--say")) { if (!lowbat.jo_testEspeak()) { - lowbat.jo_setMsg(argv[2]); speaks = 1; } } @@ -29,9 +25,12 @@ int main(int argc, const char *argv[]) { while (lowbat.jo_fetchBatlvl() < 15 && !lowbat.jo_fetchAcstat()) { if (lowbat.jo_notify()) { cerr << "Error: could not use notify-send" << endl; - return 3; + return (JO_RET_NOTIFY_FAILED); } - if (speaks && lowbat.jo_speak()) { + if (speaks) { + lowbat.jo_setMsg(argv[2]); + lowbat.jo_speak(); + lowbat.jo_delMsg(); cerr << "Error: could not use espeak" << endl; } cout << "Sleep for 20s" << endl; @@ -40,5 +39,5 @@ int main(int argc, const char *argv[]) { cout << "Sleep for 4m" << endl; sleep_for(seconds(240)); } - return 0; + return (JO_RET_FINE); } |