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..d2b51e6 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::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_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