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 | |
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
-rw-r--r-- | Makefile | 20 | ||||
-rw-r--r-- | inc/jo_lowbat.hpp | 16 | ||||
-rw-r--r-- | src/jo_lowbat.cpp | 93 | ||||
-rw-r--r-- | src/main.cpp | 15 |
4 files changed, 77 insertions, 67 deletions
@@ -23,15 +23,15 @@ FSANITIZE = -fsanitize=address CXX = g++ -OPTIMIZE = -O2 +OPTIMIZE = -O2 OPTIMIZE += -pipe -CXXFLAGS = -Wall +CXXFLAGS = -Wall CXXFLAGS += -Wextra CXXFLAGS += -Werror CXXFLAGS += ${OPTIMIZE} -#CXXFLAGS += ${DEBUG} -#CXXFLAGS += ${FSANITIZE} +# CXXFLAGS += ${DEBUG} +# CXXFLAGS += ${FSANITIZE} NAME = lowbat @@ -41,13 +41,15 @@ RM = rm -rf OS = $(shell uname -s) ifeq ($(OS), Darwin) PREFIX = /usr/local +endif +ifeq ($(OS), FreeBSD) + PREFIX = /usr/local else PREFIX = /usr endif MANPREFIX = $(PREFIX)/share/man - ${OBJS_DIR}%.o: ${SRCS_DIR}%.cpp ${INCS_DIR}${INCS} @${MKDIR} ${OBJS_DIR} ${CXX} -c ${CXXFLAGS} -I${INCS_DIR} -o $@ $< @@ -56,17 +58,17 @@ ${TRGT_DIR}${NAME}: ${OBJS} @${MKDIR} ${TRGT_DIR} ${CXX} ${CXXFLAGS} -o ${TRGT_DIR}${NAME} ${OBJS} -all: ${TRGT_DIR}${NAME} +all: ${TRGT_DIR}${NAME} clean: ${RM} ${OBJS_DIR} -fclean: clean +fclean: clean ${RM} ${TRGT_DIR} -re: fclean all +re: fclean all -install: all +install: all mkdir -p $(DESTDIR)$(PREFIX)/bin cp -f bin/lowbat $(DESTDIR)$(PREFIX)/bin/ chmod 755 $(DESTDIR)$(PREFIX)/bin/lowbat diff --git a/inc/jo_lowbat.hpp b/inc/jo_lowbat.hpp index 7428c86..db7658d 100644 --- a/inc/jo_lowbat.hpp +++ b/inc/jo_lowbat.hpp @@ -6,21 +6,27 @@ using namespace std; +enum retvals { + JO_RET_FINE, + JO_RET_MISS_DEP, + JO_RET_OPEN_FAILED, + JO_RET_NOTIFY_FAILED +}; + class Lowbat { public: - uint8_t jo_testAcpi(void); uint8_t jo_testNotifySend(void); uint8_t jo_testEspeak(void); int jo_fetchBatlvl(void); - int jo_fetchAcstat(void); + uint8_t jo_fetchAcstat(void); int jo_notify(void); int jo_speak(void); - void jo_setMsg(const char *msg); - static string jo_exec(const char*); + void jo_setMsg(const char *); + void jo_delMsg(void); private: string m_batlvl; - string m_msg; + string *m_msg; }; #endif 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); } |