diff options
Diffstat (limited to '')
| -rw-r--r-- | src/jo_lowbat.cpp | 53 | ||||
| -rw-r--r-- | src/main.cpp | 45 | 
2 files changed, 70 insertions, 28 deletions
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;  }  | 
