From f40145b6fa89de4222d47f334f2800ab2121671d Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Thu, 7 Feb 2019 18:46:41 +0800 Subject: [PATCH] Improve semantics (#42) * Keep consistency in buffer method * Revert default threshold to 0.0 --- README.md | 2 +- src/defaults.conf | 4 +- src/lib/index.hpp | 35 +++++----- src/lib/service/buffer.cpp | 128 ++++++++++++++++++------------------- src/lib/service/start.cpp | 2 + src/main.cpp | 29 ++++++--- 6 files changed, 106 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index 92024d5..d487f8c 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Comfortable swipe makes use of keyboard shortcuts for configurations. The config Property | Description | Default Value | Default Behavior --------- | ----------- | -------------- | ----- -threshold | mouse pixels to activate swipe; higher = less sensitive; floating-point (Note: Sky is the limit! Can be as large as 1000.0) | 20.0 +threshold | mouse pixels to activate swipe; higher = less sensitive; floating-point (Note: Sky is the limit! Can be as large as 1000.0) | 0.0 left3 | 3-finger swipe left | ctrl+shift+Right | switch to right workspace left4 | 4-finger swipe left | ctrl+alt+shift+Right | move window to right workspace right3 | 3-finger swipe right | ctrl+shift+Left | switch to left workspace diff --git a/src/defaults.conf b/src/defaults.conf index 9b92261..7d201e3 100644 --- a/src/defaults.conf +++ b/src/defaults.conf @@ -16,8 +16,8 @@ # # (Note: Sky is the limit! Can be as large as 1000.0) # -# Default: threshold = 20.0 -threshold = 20.0 +# Default: threshold = 0.0 +threshold = 0.0 ############################# # THREE / FOUR FINGER SWIPE # diff --git a/src/lib/index.hpp b/src/lib/index.hpp index ba2b232..c1a0228 100644 --- a/src/lib/index.hpp +++ b/src/lib/index.hpp @@ -37,23 +37,26 @@ along with this program. If not, see . #include "gesture/swipe_gesture.h" extern "C" { - namespace comfortable_swipe::util + namespace comfortable_swipe { - extern const char* GESTURE_SWIPE_BEGIN_REGEX_PATTERN; - extern const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN; - extern const char* GESTURE_SWIPE_END_REGEX_PATTERN; - const char* autostart_filename(); - constexpr const char* conf_filename(); - std::map read_config_file(const char*); - } - namespace comfortable_swipe::service - { - void autostart(); - void buffer(); - void help(); - void restart(); - void start(); - void stop(); + namespace util + { + extern const char* GESTURE_SWIPE_BEGIN_REGEX_PATTERN; + extern const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN; + extern const char* GESTURE_SWIPE_END_REGEX_PATTERN; + const char* autostart_filename(); + constexpr const char* conf_filename(); + std::map read_config_file(const char*); + } + namespace service + { + void autostart(); + void buffer(); + void help(); + void restart(); + void start(); + void stop(); + } } } diff --git a/src/lib/service/buffer.cpp b/src/lib/service/buffer.cpp index c2d7303..4d71f16 100644 --- a/src/lib/service/buffer.cpp +++ b/src/lib/service/buffer.cpp @@ -27,78 +27,76 @@ along with this program. If not, see . /** * Starts the comfortable-swipe service by buffering libinput debug-events. */ -void comfortable_swipe::service::buffer() +namespace comfortable_swipe::service { - - // 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) + void buffer() { - if (!flag_begin) + // 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::util::GESTURE_SWIPE_BEGIN_REGEX_PATTERN); + static const std::regex gesture_swipe_update(comfortable_swipe::util::GESTURE_SWIPE_UPDATE_REGEX_PATTERN); + static const std::regex gesture_swipe_end(comfortable_swipe::util::GESTURE_SWIPE_END_REGEX_PATTERN); + + // initialize swipe gesture handler + comfortable_swipe::gesture::swipe_gesture swipe + ( + config.count("threshold") ? std::stof(config["threshold"]) : 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_swiping = false; + + // start reading lines from input one by one + while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL) { - if (std::regex_match(data, matches, gesture_begin) != 0) + if (flag_swiping) { - swipe.fingers = std::stoi(matches[1]); - swipe.begin(); - flag_begin = true; + // 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_begin == true */ - { - if (std::regex_match(data, matches, gesture_update) != 0) + else /* !flag_swiping */ { - 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; + // 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(); + } } + } } } diff --git a/src/lib/service/start.cpp b/src/lib/service/start.cpp index 61996fc..f20c9a6 100644 --- a/src/lib/service/start.cpp +++ b/src/lib/service/start.cpp @@ -26,6 +26,8 @@ namespace comfortable_swipe::service { /** * Starts the comfortable-swipe service by buffering libinput debug-events. + * This method is deferred. Please refer to comfortable_swipe::service::buffer() + * for the technical implementation. */ void start() { diff --git a/src/main.cpp b/src/main.cpp index 6f2b7b7..8698f1b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,22 +25,31 @@ along with this program. If not, see . int main(int argc, char** args) { - using namespace comfortable_swipe::service; - if (argc > 1) { std::string arg = args[1]; + // select based on argument - if (arg == "start") start(); - else if (arg == "stop") stop(); - else if (arg == "restart") restart(); - else if (arg == "buffer") buffer(); - else if (arg == "autostart") autostart(); - else help(); - } + if (arg == "start") + comfortable_swipe::service::start(); + else if (arg == "stop") + comfortable_swipe::service::stop(); + + else if (arg == "restart") + comfortable_swipe::service::restart(); + + else if (arg == "buffer") + comfortable_swipe::service::buffer(); + + else if (arg == "autostart") + comfortable_swipe::service::autostart(); + + else /* if (arg == "help") */ + comfortable_swipe::service::help(); + } else - help(); + comfortable_swipe::service::help(); return 0; } \ No newline at end of file