From 6d38db4ee3e5810016d5fb1403b8670595eb9b1a Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Thu, 7 Feb 2019 16:10:13 +0800 Subject: [PATCH] Add service module --- src/comfortable_swipe/service/autostart.cpp | 57 +++++++++++++ src/comfortable_swipe/service/buffer.cpp | 88 +++++++++++++++++++++ src/comfortable_swipe/service/help.cpp | 28 +++++++ src/comfortable_swipe/service/restart.cpp | 18 +++++ src/comfortable_swipe/service/start.cpp | 18 +++++ src/comfortable_swipe/service/stop.cpp | 55 +++++++++++++ 6 files changed, 264 insertions(+) create mode 100644 src/comfortable_swipe/service/autostart.cpp create mode 100644 src/comfortable_swipe/service/buffer.cpp create mode 100644 src/comfortable_swipe/service/help.cpp create mode 100644 src/comfortable_swipe/service/restart.cpp create mode 100644 src/comfortable_swipe/service/start.cpp create mode 100644 src/comfortable_swipe/service/stop.cpp diff --git a/src/comfortable_swipe/service/autostart.cpp b/src/comfortable_swipe/service/autostart.cpp new file mode 100644 index 0000000..e730a06 --- /dev/null +++ b/src/comfortable_swipe/service/autostart.cpp @@ -0,0 +1,57 @@ +#ifndef __COMFORTABLE_SWIPE__service_autostart__ +#define __COMFORTABLE_SWIPE__service_autostart__ + +#include // std::cerr, std::cout, std::endl +#include // std::ifstream, std::ofstream +#include // std::string +#include // std::remove +#include // std::system +#include "../index.hpp" + +namespace comfortable_swipe::service +{ + /** + * Toggles automatic startup of comfortable swipe. + */ + void autostart() + { + using comfortable_swipe::util::autostart_filename; + + const std::string& path = autostart_filename(); + if (std::ifstream(path.data()).good()) + { + // file found, delete it + if (std::remove(path.data()) != 0) + std::cerr << "Error: failed to switch off autostart. " + << "Maybe the autostart file is in use?" + << std::endl; + else + std::cout << "Autostart switched off" << std::endl; + } + else { + // file not found, create it + int result = std::system(("mkdir -p $(dirname " + path + ")").data()); + std::ofstream fout(path.data()); + if (result != 0 || !fout.good()) + std::cerr << "Error: failed to switch on autostart. " + << "Are you sure you have the permissions?" + << std::endl; + else { + fout << + "[Desktop Entry]\n" + "Type=Application\n" + "Exec=bash -c \"" + __COMFORTABLE_SWIPE__PROGRAM__ + " start\"\n" + "Hidden=false\n" + "NoDisplay=false\n" + "X-GNOME-Autostart-enabled=true\n" + "Name=Comfortable Swipe\n" + "Comment=3 or 4 touchpad gestures\n"; + std::cout << "Autostart switched on" << std::endl; + } + } + } +} + +#endif /* __COMFORTABLE_SWIPE__service_autostart__ */ diff --git a/src/comfortable_swipe/service/buffer.cpp b/src/comfortable_swipe/service/buffer.cpp new file mode 100644 index 0000000..9293df9 --- /dev/null +++ b/src/comfortable_swipe/service/buffer.cpp @@ -0,0 +1,88 @@ +#ifndef __COMFORTABLE_SWIPE__service_buffer__ +#define __COMFORTABLE_SWIPE__service_buffer__ + +#include // std::stoi, std::stof +#include // std::fgets_unlocked, stdin +#include // std::regex, std::regex_match, std::cmatch +#include "../index.hpp" + +/** + * Starts the comfortable-swipe service by buffering libinput debug-events. + */ +void comfortable_swipe::service::buffer() +{ + + // import utility methods + using comfortable_swipe::util::read_config_file; + using comfortable_swipe::util::conf_filename; + using comfortable_swipe::gesture::swipe_gesture; + + // import regex patterns + using comfortable_swipe::util::GESTURE_SWIPE_BEGIN_REGEX_PATTERN; + using comfortable_swipe::util::GESTURE_SWIPE_UPDATE_REGEX_PATTERN; + using comfortable_swipe::util::GESTURE_SWIPE_END_REGEX_PATTERN; + + // pre-compile regex patterns + static const std::regex gesture_begin(GESTURE_SWIPE_BEGIN_REGEX_PATTERN); + static const std::regex gesture_update(GESTURE_SWIPE_UPDATE_REGEX_PATTERN); + static const std::regex gesture_end(GESTURE_SWIPE_END_REGEX_PATTERN); + + // read config file + auto config = read_config_file(conf_filename()); + + // initialize swipegesture handler + swipe_gesture swipe + ( + config.count("threshold") ? std::atof(config["threshold"].data()) : 0.0, + config["left3"].c_str(), + config["left4"].c_str(), + config["right3"].c_str(), + config["right4"].c_str(), + config["up3"].c_str(), + config["up4"].c_str(), + config["down3"].c_str(), + config["down4"].c_str() + ); + + // prepare data containers + static const int MAX_LINE_LENGTH = 256; + static char data[MAX_LINE_LENGTH]; + static std::cmatch matches; + + // optimization flag for checking if GESTURE_SWIPE_BEGIN was dispatched + bool flag_begin = false; + + // start reading lines from input one by one + while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL) + { + if (!flag_begin) + { + if (std::regex_match(data, matches, gesture_begin) != 0) + { + swipe.fingers = std::stoi(matches[1]); + swipe.begin(); + flag_begin = true; + } + } + else /* flag_begin == true */ + { + if (std::regex_match(data, matches, gesture_update) != 0) + { + swipe.fingers = std::stoi(matches[1]); + swipe.dx = std::stof(matches[2]); + swipe.dy = std::stof(matches[3]); + swipe.udx = std::stof(matches[4]); + swipe.udy = std::stof(matches[5]); + swipe.update(); + } + else if (std::regex_match(data, matches, gesture_end) != 0) + { + swipe.fingers = std::stoi(matches[1]); + swipe.end(); + flag_begin = false; + } + } + } +} + +#endif /* __COMFORTABLE_SWIPE__service_buffer__ */ diff --git a/src/comfortable_swipe/service/help.cpp b/src/comfortable_swipe/service/help.cpp new file mode 100644 index 0000000..1ce9e89 --- /dev/null +++ b/src/comfortable_swipe/service/help.cpp @@ -0,0 +1,28 @@ +#ifndef __COMFORTABLE_SWIPE__service_help__ +#define __COMFORTABLE_SWIPE__service_help__ + +#include // std::puts, std::printf +#include "../index.hpp" + +namespace comfortable_swipe::service +{ + /** + * Shows the help window. + */ + void help() + { + using comfortable_swipe::util::conf_filename; + std::puts("comfortable-swipe [start|stop|restart|autostart|buffer|help]"); + std::puts(""); + std::puts("start - starts 3/4-finger gesture service"); + std::puts("stop - stops 3/4-finger gesture service"); + std::puts("restart - stops then starts 3/4-finger gesture service"); + std::puts("autostart - automatically run on startup (toggleable)"); + std::puts("buffer - parses output of libinput debug-events"); + std::puts("help - shows the help dialog"); + std::puts(""); + std::printf("Configuration file can be found in %s\n", conf_filename()); + } +} + +#endif /* __COMFORTABLE_SWIPE__service_help__ */ diff --git a/src/comfortable_swipe/service/restart.cpp b/src/comfortable_swipe/service/restart.cpp new file mode 100644 index 0000000..3cbad55 --- /dev/null +++ b/src/comfortable_swipe/service/restart.cpp @@ -0,0 +1,18 @@ +#ifndef __COMFORTABLE_SWIPE__service_restart__ +#define __COMFORTABLE_SWIPE__service_restart__ + +#include "../index.hpp" + +namespace comfortable_swipe::service +{ + /** + * Restarts the comfortable-swipe service. + */ + void restart() + { + comfortable_swipe::service::start(); + comfortable_swipe::service::stop(); + } +} + +#endif /* __COMFORTABLE_SWIPE__service_restart__ */ diff --git a/src/comfortable_swipe/service/start.cpp b/src/comfortable_swipe/service/start.cpp new file mode 100644 index 0000000..6022fbf --- /dev/null +++ b/src/comfortable_swipe/service/start.cpp @@ -0,0 +1,18 @@ +#ifndef __COMFORTABLE_SWIPE__service_start__ +#define __COMFORTABLE_SWIPE__service_start__ + +#include "../index.hpp" +#include // std::system + +namespace comfortable_swipe::service +{ + /** + * Starts the comfortable-swipe service by buffering libinput debug-events. + */ + void start() + { + std::system("stdbuf -oL -e0 libinput debug-events | " __COMFORTABLE_SWIPE__PROGRAM__ " buffer"); + } +} + +#endif /* __COMFORTABLE_SWIPE__service_start__ */ diff --git a/src/comfortable_swipe/service/stop.cpp b/src/comfortable_swipe/service/stop.cpp new file mode 100644 index 0000000..bd726c5 --- /dev/null +++ b/src/comfortable_swipe/service/stop.cpp @@ -0,0 +1,55 @@ +#ifndef __COMFORTABLE_SWIPE__service_stop__ +#define __COMFORTABLE_SWIPE__service_stop__ + +#include // std::FILE, std::feof, std::fgets +#include // std::atoi, std::system +#include // std::string, std::to_string +#include // std::runtime_error +#include // popen, pclose, getpid + +namespace comfortable_swipe::service +{ + /** + * Stops all comfortable-swipe instances. + */ + void stop() + { + + // read all service names from process (pgrep) + char* buffer = new char[20]; + FILE* pipe = popen("pgrep -f comfortable-swipe", "r"); + + // make sure pipe exists + if (pipe == NULL) + { + throw std::runtime_error("stop command failed"); + } + + // buffer what to kill + std::string kill = "kill"; + + // read until end of line + while (!std::feof(pipe)) + { + if (std::fgets(buffer, 20, pipe) != NULL) + { + int pid = std::atoi(buffer); + if (pid != getpid()) + { + kill += " "; + kill += std::to_string(pid); + } + } + } + + // run "kill {pid1} {pid2}..." + std::system(kill.data()); + delete[] buffer; + + // close the pipe + pclose(pipe); + + } +} + +#endif /* __COMFORTABLE_SWIPE__service_stop__ */