diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | inc/jo_lowbat.hpp | 7 | ||||
-rw-r--r-- | src/jo_lowbat.cpp | 53 | ||||
-rw-r--r-- | src/main.cpp | 45 |
4 files changed, 77 insertions, 32 deletions
@@ -10,8 +10,6 @@ OBJS_DIR = obj/ TRGT_DIR = bin/ SRCS_NAME = main.cpp -#SRCS_NAME += jo_exec.cpp -SRCS_NAME += jo_notify.cpp SRCS_NAME += jo_lowbat.cpp SRCS = $(addprefix ${SRCS_DIR}, ${SRCS_NAME}) @@ -34,7 +32,7 @@ CXXFLAGS += -Wextra CXXFLAGS += -Werror #CXXFLAGS += ${OPTIMIZE} CXXFLAGS += ${DEBUG} -CXXFLAGS += ${FSANITIZE} +#CXXFLAGS += ${FSANITIZE} NAME = lowbat diff --git a/inc/jo_lowbat.hpp b/inc/jo_lowbat.hpp index a03c336..a2bd06e 100644 --- a/inc/jo_lowbat.hpp +++ b/inc/jo_lowbat.hpp @@ -9,13 +9,18 @@ using namespace std; class Lowbat { public: uint8_t jo_testAcpi(void); + uint8_t jo_testNotifySend(void); + void jo_testEspeak(void); int jo_fetchBatlvl(void); + int jo_fetchAcstat(void); int jo_notify(void); - int jo_speak(const string); + int jo_speak(void); + void jo_setMsg(const char *msg); static string jo_exec(const char*); private: string m_batlvl; + string m_msg; }; #endif diff --git a/src/jo_lowbat.cpp b/src/jo_lowbat.cpp index f4c3b2e..b6cc0d6 100644 --- a/src/jo_lowbat.cpp +++ b/src/jo_lowbat.cpp @@ -8,10 +8,31 @@ 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; + if (system("which acpi > /dev/null 2>&1")) { + cout << "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")) { + cout << "notify-send is not installed. Please install it in order to run lowbat" << endl; + return 1; + } + cout << "notify-send is installed" + return 0; +} + +void +Lowbat::jo_testEspeak(void) { + if (system("which espeak > /dev/null 2>&1")) { + cout << "espeak is not installed. Please install it in order to run --say option" << endl; + return 1; + } + cout << "espeak is installed" << endl; return 0; } @@ -21,7 +42,17 @@ Lowbat::jo_fetchBatlvl(void) { 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)); + return stoi(m_batlvl); +} + +int +Lowbat::jo_fetchAcstat(void) { + const int ret = system("acpi | grep -q Discharging"); + if (ret != 0) + cout << ret << "acstat: Charging" << endl; + else + cout << "acstat: Discharging" << endl; + return ret; } string @@ -48,3 +79,19 @@ Lowbat::jo_notify(void) { cout << "Notifying" << endl; return system(str.c_str()); } + +int +Lowbat::jo_speak(void) { + string str; + + str = "echo \""; + str += m_msg; + str += "\" | espeak"; + cout << "Speaking: " << m_msg << endl; + return system(str.c_str()); +} + +void +Lowbat::jo_setMsg(const char *msg) { + m_msg = msg; +} diff --git a/src/main.cpp b/src/main.cpp index a6c291d..56ee872 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ #include <jo_lowbat.hpp> #include <iostream> -#include <cstring> +#include <cstdint> #include <thread> #include <chrono> @@ -8,32 +8,28 @@ using namespace this_thread; using namespace chrono; int main(int argc, const char *argv[]) { - string* msg; - string acstat; - string batlvl; - int batlvlint; - (void)argc; - (void)argv; - (void)msg; - (void)batlvl; - (void)batlvlint; - (void)acstat; + Lowbat lowbat; + uint8_t speaks; - Lowbat lowbat; + speaks = 0; if (lowbat.jo_testAcpi()) - return (1); + return 1; + if (lowbat.jo_testNotifySend()) + return 2; + if (argc > 2 && !strcmp(argv[1], "--say")) { + if (lowbat.jo_testEspeak()) { + lowbat.jo_setMsg(argv[2]); + speaks = 1; + } + } while (true) { - while (lowbat.jo_fetchBatlvl() < 115 && !system("acpi | grep -q Discharging")) { - lowbat.jo_notify(); - if (argc > 1 && strcmp(argv[1], "--silent")) { - if (argc > 2 && !strcmp(argv[1], "--say")) { - msg = new string(argv[2]); - } - else { - msg = new string("beep beep - low battery"); - } - jo_speak(*msg); - delete msg; + while (lowbat.jo_fetchBatlvl() < 115 && !lowbat.jo_fetchAcstat()) { + if (lowbat.jo_notify()) { + cout << "Error: could not use notify-send" << endl; + return 3; + } + if (speaks && lowbat.jo_speak()) { + cout << "Error: could not use espeak" << endl; } cout << "Sleep for 20s" << endl; sleep_for(seconds(20)); @@ -41,6 +37,5 @@ int main(int argc, const char *argv[]) { cout << "Sleep for 4m" << endl; sleep_for(seconds(240)); } - return 0; } |