diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-06 22:41:33 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-06 22:41:33 +0200 |
commit | 5f1fdd425ad147efd33b54b39deb1d50459185c2 (patch) | |
tree | 408ca975011c6e7d62826891189d3a69da870d7f /src/jo_lowbat.cpp | |
parent | Canceled system() remplacement for libnotify as it leaks a lot (diff) | |
download | lowbat-gnu-5f1fdd425ad147efd33b54b39deb1d50459185c2.tar.gz lowbat-gnu-5f1fdd425ad147efd33b54b39deb1d50459185c2.tar.bz2 lowbat-gnu-5f1fdd425ad147efd33b54b39deb1d50459185c2.tar.xz lowbat-gnu-5f1fdd425ad147efd33b54b39deb1d50459185c2.tar.zst lowbat-gnu-5f1fdd425ad147efd33b54b39deb1d50459185c2.zip |
ACPI is not required anymore
Diffstat (limited to 'src/jo_lowbat.cpp')
-rw-r--r-- | src/jo_lowbat.cpp | 93 |
1 files changed, 48 insertions, 45 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; } |