aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoe <bousset.rudy@gmail.com>2019-12-03 20:21:09 +0100
committerJoe <bousset.rudy@gmail.com>2019-12-03 20:21:09 +0100
commit89737ad005f4c066234a5282a1a46eb68b2c1ace (patch)
tree7b521091227050c84dca220715e20bf1347c39d3
parentREADME update (diff)
downloadlowbat-gnu-89737ad005f4c066234a5282a1a46eb68b2c1ace.tar.gz
lowbat-gnu-89737ad005f4c066234a5282a1a46eb68b2c1ace.tar.bz2
lowbat-gnu-89737ad005f4c066234a5282a1a46eb68b2c1ace.tar.xz
lowbat-gnu-89737ad005f4c066234a5282a1a46eb68b2c1ace.tar.zst
lowbat-gnu-89737ad005f4c066234a5282a1a46eb68b2c1ace.zip
cpp mode
-rw-r--r--Makefile27
-rw-r--r--inc/jo_lowbat.hpp22
-rw-r--r--src/jo_exec.cpp15
-rw-r--r--src/jo_notify.cpp26
-rw-r--r--src/main.cpp35
5 files changed, 125 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index d7db2c4..eec2142 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,32 @@
.POSIX:
+default: all
+
+SHELL
+SRCS_DIR = src/
+INCS_DIR = inc/
+OBJS_DIR = obj/
+
+SRCS = ${SRCS_DIR}main.cpp
+SRCS += ${SRCS_DIR}jo_exec.cpp
+SRCS += ${SRCS_DIR}jo_notify.cpp
+
+INCS = ${INCS_DIR}jo_lowbat.hpp
+
+OBJS = $(patsubst ${SRCS_DIR}%.cpp,${OBJS_DIR}%.o,${SRCS})
+
+CC = g++
+CFLAGS = -Wall
+CFLAGS += -Wextra
+CFLAGS += -Werror
+
+DEBUG = -g3
+FSANITIZE = -fsanitize=address
+
+OPTIMIZE = -O2
+
+TARGET = lowbat
+
OS = $(shell uname -s)
ifeq ($(OS), Darwin)
PREFIX = /usr/local
diff --git a/inc/jo_lowbat.hpp b/inc/jo_lowbat.hpp
new file mode 100644
index 0000000..2269902
--- /dev/null
+++ b/inc/jo_lowbat.hpp
@@ -0,0 +1,22 @@
+#ifndef JO_LOWBAT_HPP
+#define JO_LOWBAT_HPP
+
+#include <iostream>
+#include <cstring>
+#include <string>
+#include <stream>
+#include <thread>
+#include <chrono>
+#include <memory>
+#include <stdexcept>
+#include <array>
+
+using namespace std;
+using namespace this_thread;
+using namespace chrono;
+
+void jo_notify(const string);
+void jo_speak (const string);
+string jo_exec (const char*);
+
+#endif
diff --git a/src/jo_exec.cpp b/src/jo_exec.cpp
new file mode 100644
index 0000000..a97012c
--- /dev/null
+++ b/src/jo_exec.cpp
@@ -0,0 +1,15 @@
+#include <jo_lowbat.hpp>
+
+string
+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;
+}
diff --git a/src/jo_notify.cpp b/src/jo_notify.cpp
new file mode 100644
index 0000000..1683871
--- /dev/null
+++ b/src/jo_notify.cpp
@@ -0,0 +1,26 @@
+#include <jo_lowbat.hpp>
+
+void
+jo_notify(const string batlvl) {
+ string str = nullptr;
+ const char *cmd = nullptr;
+
+ str = "notify-send \"";
+ str += batlvl;
+ str += "%\" \"Please plug in computer\" -u critical -t 15000";
+
+ cmd = str.c_str();
+ system(cmd);
+}
+
+void
+jo_speak(const string msg) {
+ string str = nullptr;
+ const char *cmd = nullptr;
+
+ str = "echo \"";
+ str += msg;
+ str += "\" | espeak";
+ cmd = str.c_str();
+ system(cmd);
+}
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..2fe6063
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,35 @@
+#include <jo_lowbat.hpp>
+
+int main(int argc, const char *argv[]) {
+ string msg = nullptr;
+ string acstat = nullptr;
+ string batlvl = nullptr;
+ stringstream battoint = nullptr;
+ int batlvlint = 0;
+
+ if (memcmp(argv[1], "--say", strlen(argv[1])))
+ msg = "beep beep - low battery";
+ else
+ msg = argv[2];
+
+ while (true) {
+ acstat = jo_exec("acpi | awk '{print $3}' | rev | cut -c 2- | rev");
+ batlvl = jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
+ battoint = batlvl;
+ battoint >> batlvlint;
+ if (batlvlint < 15) {
+ while (!memcmp(acstat, "Discharging", strlen(acstat))) {
+ jo_notify(batlvl);
+ if (memcmp(argv[1], "--silent", strlen(argv[1])))
+ jo_speak(msg);
+ sleep_for(seconds(20));
+ acstat = jo_exec("acpi | awk '{print $3}' | rev | cut -c 2- | rev");
+ batlvl = jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev");
+ if (!memcmp(acstat, "Charging", strlen(acstat)))
+ break ;
+ }
+ }
+ sleep_for(seconds(240));
+ }
+ return 0;
+}