From 204a422f2d8cc59d1dcf44b6e4df4b99412a02db Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Wed, 13 Feb 2019 09:27:41 +0800 Subject: [PATCH] Add status service command (#54) * Use std::unique_ptr for stop service * Use std::unique_ptr for stop service * Add status command * Prefer to use std::array to avoid memory leaking in buffer * Perform xdo key sequence before output message * Fix missing swipe_gesture header in regex implementation --- .gitignore | 4 ++ comfortable-swipe.cpp | 3 ++ lib/comfortable_swipe | 1 + lib/gesture/swipe_gesture.cpp | 2 +- lib/gesture/swipe_gesture.regex.cpp | 8 ++-- lib/index.hpp | 1 + lib/service/buffer.cpp | 9 ++--- lib/service/help.cpp | 1 + lib/service/status.cpp | 58 +++++++++++++++++++++++++++++ lib/service/stop.cpp | 25 ++++++------- 10 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 lib/service/status.cpp diff --git a/.gitignore b/.gitignore index 4753a05..2fca53d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ # C++ generated headers *.gch + +# IDE-specific +.idea +.vscode diff --git a/comfortable-swipe.cpp b/comfortable-swipe.cpp index c49baf0..a11b232 100644 --- a/comfortable-swipe.cpp +++ b/comfortable-swipe.cpp @@ -54,6 +54,9 @@ int main(int argc, char** args) else if (arg == "debug") comfortable_swipe::service::debug(); + else if (arg == "status") + comfortable_swipe::service::status(); + else /* if (arg == "help") */ comfortable_swipe::service::help(); } diff --git a/lib/comfortable_swipe b/lib/comfortable_swipe index 3316ca4..ce75197 100644 --- a/lib/comfortable_swipe +++ b/lib/comfortable_swipe @@ -34,6 +34,7 @@ along with this program. If not, see . #include "service/help.cpp" #include "service/restart.cpp" #include "service/start.cpp" +#include "service/status.cpp" #include "service/stop.cpp" #include "util/autostart_filename.cpp" #include "util/conf_filename.cpp" diff --git a/lib/gesture/swipe_gesture.cpp b/lib/gesture/swipe_gesture.cpp index 16d5580..7e4e20b 100644 --- a/lib/gesture/swipe_gesture.cpp +++ b/lib/gesture/swipe_gesture.cpp @@ -116,10 +116,10 @@ namespace comfortable_swipe::gesture if (this->previous_gesture == swipe_gesture::FRESH || this->previous_gesture == (mask ^ swipe_gesture::MSK_POSITIVE)) { + xdo_send_keysequence_window(this->xdo, CURRENTWINDOW, swipe_gesture::commands[mask], 0); this->x = this->y = 0; this->previous_gesture = mask; std::cout << "SWIPE " << swipe_gesture::command_map[mask] << std::endl; - xdo_send_keysequence_window(xdo, CURRENTWINDOW, swipe_gesture::commands[mask], 0); } } } diff --git a/lib/gesture/swipe_gesture.regex.cpp b/lib/gesture/swipe_gesture.regex.cpp index a39fbeb..7dd1d04 100644 --- a/lib/gesture/swipe_gesture.regex.cpp +++ b/lib/gesture/swipe_gesture.regex.cpp @@ -19,12 +19,14 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "swipe_gesture.h" + namespace comfortable_swipe::gesture { /** * Regex pattern for the libinput entry for start of swipe. * Extracts one match for the number of fingers used during the swipe. - * + * * eg. event15 GESTURE_SWIPE_BEGIN +34.33s 3 * ^ * fingers @@ -41,7 +43,7 @@ namespace comfortable_swipe::gesture /** * Regex pattern for the libinput entry for the end of swipe. * Extracts one match for the number of fingers used during the swipe. - * + * * eg. event15 GESTURE_SWIPE_END +35.03s 3 * ^ * fingers @@ -64,7 +66,7 @@ namespace comfortable_swipe::gesture /** * Regex pattern for the libinput entry for during a swipe. * Extracts number of fingers used and the speed (normal and accelerated) of the swipe. - * + * * eg. event15 GESTURE_SWIPE_UPDATE +34.70s 3 -0.12/ 4.99 (-0.33/13.50 unaccelerated) * ^ ^ ^ ^ ^ * fingers dx dy udx udy diff --git a/lib/index.hpp b/lib/index.hpp index 21a36a7..35e1482 100644 --- a/lib/index.hpp +++ b/lib/index.hpp @@ -54,6 +54,7 @@ extern "C" void help(); void restart(); void start(); + void status(); void stop(); } } diff --git a/lib/service/buffer.cpp b/lib/service/buffer.cpp index 14b421a..3c7064d 100644 --- a/lib/service/buffer.cpp +++ b/lib/service/buffer.cpp @@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include // std::fgets_unlocked, stdin +#include // fgets_unlocked, stdin #include "../index.hpp" /** @@ -47,14 +47,13 @@ namespace comfortable_swipe::service ); // prepare data containers - static const int MAX_LINE_LENGTH = 256; - static char data[MAX_LINE_LENGTH]; + std::array line; // start reading lines from input one by one - while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL) + while (fgets_unlocked(line.data(), line.size(), stdin) != NULL) { // attempt to parse swipe gestures - swipe_gesture_handler.parse_line(data); + swipe_gesture_handler.parse_line(line.data()); } } } diff --git a/lib/service/help.cpp b/lib/service/help.cpp index e1355e6..6514f24 100644 --- a/lib/service/help.cpp +++ b/lib/service/help.cpp @@ -39,6 +39,7 @@ namespace comfortable_swipe::service std::puts("buffer - parses output of libinput debug-events"); std::puts("help - shows the help dialog"); std::puts("debug - logs raw output from input events taken from libinput"); + std::puts("status - checks status of program and autostart"); std::puts(""); std::printf("Configuration file can be found in %s\n", conf_filename()); } diff --git a/lib/service/status.cpp b/lib/service/status.cpp new file mode 100644 index 0000000..7894e76 --- /dev/null +++ b/lib/service/status.cpp @@ -0,0 +1,58 @@ +#ifndef __COMFORTABLE_SWIPE__service_status__ +#define __COMFORTABLE_SWIPE__service_status__ + +/* +Comfortable Swipe +by Rico Tiongson + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include "../index.hpp" +#include // std::runtime_error +#include // popen, pclose, getpid, access, F_OK +#include // std::unique_ptr +#include // std::array +#include // std::atoi +#include // FILE, std::feof, std::fgets, std::printf + +namespace comfortable_swipe::service +{ + /** + * Restarts the comfortable-swipe service. + */ + void status() + { + // check if comfortable-swipe is running + bool running = false; + std::array buffer; + std::unique_ptr pipe(popen("pgrep -f comfortable-swipe", "r"), pclose); + if (pipe && !std::feof(pipe.get()) && std::fgets(buffer.data(), buffer.size(), pipe.get()) != NULL) + { + int pid = std::atoi(buffer.data()); + if (pid != getpid()) + running = true; + } + + // check if autostart is on + auto autostart_f = comfortable_swipe::util::autostart_filename(); + bool autostart_on = access(autostart_f, F_OK) != -1; + + // print status + std::printf("program is %s\n", running ? "ON" : "OFF"); + std::printf("autostart is %s\n", autostart_on ? "ON" : "OFF"); + } +} + +#endif /* __COMFORTABLE_SWIPE__service_restart__ */ diff --git a/lib/service/stop.cpp b/lib/service/stop.cpp index 655a6d2..7469c69 100644 --- a/lib/service/stop.cpp +++ b/lib/service/stop.cpp @@ -24,6 +24,10 @@ along with this program. If not, see . #include // std::runtime_error #include // popen, pclose, getpid +#include // std::unique_ptr +#include // std::array +#include // std::atoi +#include // FILE, std::feof, std::fgets namespace comfortable_swipe::service { @@ -34,24 +38,22 @@ namespace comfortable_swipe::service { // read all service names from process (pgrep) - char* buffer = new char[20]; - FILE* pipe = popen("pgrep -f comfortable-swipe", "r"); - + std::array buffer; + std::unique_ptr pipe(popen("pgrep -f comfortable-swipe", "r"), pclose); + // make sure pipe exists - if (pipe == NULL) - { + if (!pipe) throw std::runtime_error("stop command failed"); - } // buffer what to kill std::string kill = "kill"; // read until end of line - while (!std::feof(pipe)) + while (!std::feof(pipe.get())) { - if (std::fgets(buffer, 20, pipe) != NULL) + if (std::fgets(buffer.data(), buffer.size(), pipe.get()) != NULL) { - int pid = std::atoi(buffer); + int pid = std::atoi(buffer.data()); if (pid != getpid()) { kill += " "; @@ -62,11 +64,6 @@ namespace comfortable_swipe::service // run "kill {pid1} {pid2}..." (void) std::system(kill.data()); - delete[] buffer; - - // close the pipe - pclose(pipe); - } }