From 48a79607dcd1a7ade0a62bc7a4def82b83e30e75 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Thu, 7 Feb 2019 22:51:23 +0800 Subject: [PATCH] Delegate line parsing and regex matching to gesture class implementation --- src/lib/gesture/pinch_gesture.h | 7 ++-- src/lib/gesture/swipe_gesture.cpp | 63 +++++++++++++++++++++++++++++-- src/lib/gesture/swipe_gesture.h | 8 ++-- src/lib/gesture/xdo_gesture.h | 7 ++-- src/lib/service/buffer.cpp | 49 ++---------------------- 5 files changed, 76 insertions(+), 58 deletions(-) diff --git a/src/lib/gesture/pinch_gesture.h b/src/lib/gesture/pinch_gesture.h index 7968a9f..4b4e79d 100644 --- a/src/lib/gesture/pinch_gesture.h +++ b/src/lib/gesture/pinch_gesture.h @@ -44,9 +44,10 @@ namespace comfortable_swipe::gesture ~pinch_gesture(); float radius, omega; - void begin() override; - void update() override; - void end() override; + inline void begin() override; + inline void update() override; + inline void end() override; + inline bool parse_line(const char *) override; protected: // current location diff --git a/src/lib/gesture/swipe_gesture.cpp b/src/lib/gesture/swipe_gesture.cpp index 1340a54..5fb4a00 100644 --- a/src/lib/gesture/swipe_gesture.cpp +++ b/src/lib/gesture/swipe_gesture.cpp @@ -20,6 +20,8 @@ along with this program. If not, see . */ #include // std::cout, std::endl +#include // std::regex, std::regex_match +#include // std::stof, std::stoi #include "xdo_gesture.h" extern "C" @@ -62,7 +64,7 @@ namespace comfortable_swipe::gesture /** * Hook on begin of swipe gesture. */ - void swipe_gesture::begin() + inline void swipe_gesture::begin() { xdo_get_mouse_location(this->xdo, &this->ix, &this->iy, &this->screen_num); this->previous_gesture = swipe_gesture::FRESH; @@ -73,7 +75,7 @@ namespace comfortable_swipe::gesture /** * Hook on update of swipe gesture. */ - void swipe_gesture::update() + inline void swipe_gesture::update() { this->x += this->dx; this->y += this->dy; @@ -121,9 +123,64 @@ namespace comfortable_swipe::gesture /** * Hook on end of swipe gesture. */ - void swipe_gesture::end() + inline void swipe_gesture::end() { } + /** + * Parses an output line and dispatches swipe begin/update/end upon match. + * Uses GESTURE_BEGIN_REGEX_PATTERN for begin, + * GESTURE_UPDATE_REGEX_PATTERN for update, + * GESTURE_END_REGEX_PATTERN for end. + * + * @param line the output line from libinput debug-events + * @return true if line matches this gesture + */ + inline bool swipe_gesture::parse_line(const char * line) + { + // pre-compile regex patterns + static const std::regex gesture_swipe_begin(swipe_gesture::GESTURE_BEGIN_REGEX_PATTERN); + static const std::regex gesture_swipe_update(swipe_gesture::GESTURE_UPDATE_REGEX_PATTERN); + static const std::regex gesture_swipe_end(swipe_gesture::GESTURE_END_REGEX_PATTERN); + std::cmatch matches; + if (this->flag_swiping) + { + // currently swiping + if (std::regex_match(line, matches, gesture_swipe_update) != 0) + { + // update swipe + this->fingers = std::stoi(matches[1]); + this->dx = std::stof(matches[2]); + this->dy = std::stof(matches[3]); + this->udx = std::stof(matches[4]); + this->udy = std::stof(matches[5]); + this->update(); + return true; + } + else if (std::regex_match(line, matches, gesture_swipe_end) != 0) + { + // end swipe + this->flag_swiping = false; + this->fingers = std::stoi(matches[1]); + this->end(); + return true; + } + } + else /* !flag_swiping */ + { + // not swiping, check if swipe will begin + if (std::regex_match(line, matches, gesture_swipe_begin) != 0) + { + // begin swipe + this->flag_swiping = true; + this->fingers = std::stoi(matches[1]); + this->begin(); + return true; + } + } + return false; + } + + /* STATICS DEFINITIONS */ const int swipe_gesture::MSK_THREE_FINGERS = 0; const int swipe_gesture::MSK_FOUR_FINGERS = 1; diff --git a/src/lib/gesture/swipe_gesture.h b/src/lib/gesture/swipe_gesture.h index 4061aec..9f341e4 100644 --- a/src/lib/gesture/swipe_gesture.h +++ b/src/lib/gesture/swipe_gesture.h @@ -49,9 +49,10 @@ namespace comfortable_swipe::gesture int fingers; float dx, dy, udx, udy; - void begin() override; - void update() override; - void end() override; + inline void begin() override; + inline void update() override; + inline void end() override; + inline bool parse_line(const char *) override; protected: // location of mouse @@ -60,6 +61,7 @@ namespace comfortable_swipe::gesture // current location float x, y, threshold_squared; int previous_gesture; + bool flag_swiping; const char ** commands; public: diff --git a/src/lib/gesture/xdo_gesture.h b/src/lib/gesture/xdo_gesture.h index ef9d181..39cf47a 100644 --- a/src/lib/gesture/xdo_gesture.h +++ b/src/lib/gesture/xdo_gesture.h @@ -42,9 +42,10 @@ namespace comfortable_swipe ~xdo_gesture(); // hooks - virtual void begin() = 0; - virtual void update() = 0; - virtual void end() = 0; + virtual inline void begin() = 0; + virtual inline void update() = 0; + virtual inline void end() = 0; + virtual inline bool parse_line(const char *) = 0; }; } } diff --git a/src/lib/service/buffer.cpp b/src/lib/service/buffer.cpp index f3c9517..c2299bb 100644 --- a/src/lib/service/buffer.cpp +++ b/src/lib/service/buffer.cpp @@ -19,9 +19,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include // std::stoi, std::stof #include // std::fgets_unlocked, stdin -#include // std::regex, std::regex_match, std::cmatch #include "../index.hpp" /** @@ -34,11 +32,6 @@ namespace comfortable_swipe::service // read config file auto config = comfortable_swipe::util::read_config_file(comfortable_swipe::util::conf_filename()); - // pre-compile regex patterns - static const std::regex gesture_swipe_begin(comfortable_swipe::gesture::swipe_gesture::GESTURE_BEGIN_REGEX_PATTERN); - static const std::regex gesture_swipe_update(comfortable_swipe::gesture::swipe_gesture::GESTURE_UPDATE_REGEX_PATTERN); - static const std::regex gesture_swipe_end(comfortable_swipe::gesture::swipe_gesture::GESTURE_END_REGEX_PATTERN); - // initialize swipe gesture handler comfortable_swipe::gesture::swipe_gesture swipe ( @@ -55,48 +48,12 @@ namespace comfortable_swipe::service // 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_swiping = false; + static char line[MAX_LINE_LENGTH]; // start reading lines from input one by one - while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL) + while (fgets_unlocked(line, MAX_LINE_LENGTH, stdin) != NULL) { - if (flag_swiping) - { - // currently swiping - if (std::regex_match(data, matches, gesture_swipe_update) != 0) - { - // update swipe - 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_swipe_end) != 0) - { - // end swipe - flag_swiping = false; - swipe.fingers = std::stoi(matches[1]); - swipe.end(); - } - } - else /* !flag_swiping */ - { - // not swiping, check if swipe will begin - if (std::regex_match(data, matches, gesture_swipe_begin) != 0) - { - // begin swipe - flag_swiping = true; - swipe.fingers = std::stoi(matches[1]); - swipe.begin(); - } - } - + swipe.parse_line(line); } } }