From d4f750464326c24ad67950706dffc48a25486de3 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Thu, 7 Feb 2019 01:10:06 +0800 Subject: [PATCH 1/4] Remove unused device and stamp from swipe_impl --- src/comfortable-swipe.cpp | 58 ++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/src/comfortable-swipe.cpp b/src/comfortable-swipe.cpp index 4c08ce1..18553fc 100644 --- a/src/comfortable-swipe.cpp +++ b/src/comfortable-swipe.cpp @@ -90,7 +90,7 @@ int main(int argc, char** args) { } struct swipe_gesture { - string device, stamp, fingers; + string fingers; string dx, dy, udx, udy; xdo_t* xdo; virtual void on_update() = 0; @@ -234,32 +234,26 @@ namespace service { auto data = sentence.data(); cmatch matches; if (regex_match(data, matches, gesture_begin)) { - swipe.device = matches[1]; - swipe.stamp = matches[2]; - swipe.fingers = matches[3]; + swipe.fingers = matches[1]; swipe.on_begin(); } else if (regex_match(data, matches, gesture_end)) { - swipe.device = matches[1]; - swipe.stamp = matches[2]; - swipe.fingers = matches[3]; + swipe.fingers = matches[1]; swipe.on_end(); } else if (regex_match(data, matches, gesture_update)) { - swipe.device = matches[1]; - swipe.stamp = matches[2]; - swipe.fingers = matches[3]; - swipe.dx = matches[4]; - swipe.dy = matches[5]; - swipe.udx = matches[6]; - swipe.udy = matches[7]; + swipe.fingers = matches[1]; + swipe.dx = matches[2]; + swipe.dy = matches[3]; + swipe.udx = matches[4]; + swipe.udy = matches[5]; swipe.on_update(); } } } // starts service void start() { - int x = system("stdbuf -oL -eL libinput debug-events | " PROGRAM " buffer"); + int x = system("stdbuf -oL -e0 libinput debug-events | " PROGRAM " buffer"); } // stops service void stop() { @@ -342,41 +336,43 @@ namespace util { } string join(cstr delim, string arr[], int n) { - string ans = arr[0]; + string ans = "^\\s*" + arr[0]; for (int i = 1; i < n; ++i) { ans += delim; ans += arr[i]; } + ans += "\\s*$"; return ans; } string build_gesture_begin() { - string device = "\\s*(\\S+)\\s*"; - string gesture = "\\s*GESTURE_SWIPE_BEGIN\\s*"; - string seconds = "\\s*(\\S+)\\s*"; - string fingers = "\\s*(\\d+)\\s*"; + string device = "\\S+"; + string gesture = "GESTURE_SWIPE_BEGIN"; + string seconds = "\\S+"; + string fingers = "(\\d+)"; string arr[] = {device, gesture, seconds, fingers}; return join("\\s+", arr, 4); } string build_gesture_update() { - string device = "\\s*(\\S+)\\s*"; - string gesture = "\\s*GESTURE_SWIPE_UPDATE\\s*"; - string seconds = "\\s*(\\S+)\\s*"; - string fingers = "\\s*(\\d+)\\s*"; - string num_1 = "\\s*(" + number_regex() + ")\\s*"; + string device = "\\S+"; + string gesture = "GESTURE_SWIPE_UPDATE"; + string seconds = "\\S+"; + string fingers = "(\\d+)"; + string num_1 = "\\s*(" + number_regex() + ")"; string num_2 = num_1; string num_div = num_1 + "/" + num_2; - string num_accel = "\\s*\\(\\s*" + num_div + "\\s*unaccelerated\\s*\\)\\s*"; + string num_accel = "\\(" + num_div + "\\s+unaccelerated\\)"; string arr[] = {device, gesture, seconds, fingers, num_div, num_accel}; - return join("\\s+", arr, 6); + string result = join("\\s+", arr, 6); + return result; } string build_gesture_end() { - string device = "\\s*(\\S+)\\s*"; - string gesture = "\\s*GESTURE_SWIPE_END\\s*"; - string seconds = "\\s*(\\S+)\\s*"; - string fingers = "\\s*(\\d+)\\s*"; + string device = "\\S+"; + string gesture = "GESTURE_SWIPE_END"; + string seconds = "\\S+"; + string fingers = "(\\d+)"; string arr[] = {device, gesture, seconds, fingers}; return join("\\s+", arr, 4); } From 1079d0afe2985a77e738bd12e3743d7d4ee51bb3 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Thu, 7 Feb 2019 01:45:44 +0800 Subject: [PATCH 2/4] Use constants for regex patterns --- src/comfortable-swipe.cpp | 119 +++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 48 deletions(-) diff --git a/src/comfortable-swipe.cpp b/src/comfortable-swipe.cpp index 18553fc..841cf3a 100644 --- a/src/comfortable-swipe.cpp +++ b/src/comfortable-swipe.cpp @@ -56,10 +56,9 @@ extern "C" { /* FORWARD DECLARATIONS */ namespace util { - string join(cstr, string[], int); - string build_gesture_begin(); - string build_gesture_update(); - string build_gesture_end(); + extern const char* GESTURE_SWIPE_BEGIN_REGEX_PATTERN; + extern const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN; + extern const char* GESTURE_SWIPE_END_REGEX_PATTERN; map read_config_file(const char*); } @@ -212,9 +211,9 @@ namespace service { // check first if $user ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); - const regex gesture_begin(util::build_gesture_begin()); - const regex gesture_update(util::build_gesture_update()); - const regex gesture_end(util::build_gesture_end()); + const regex gesture_begin(util::GESTURE_SWIPE_BEGIN_REGEX_PATTERN); + const regex gesture_update(util::GESTURE_SWIPE_UPDATE_REGEX_PATTERN); + const regex gesture_end(util::GESTURE_SWIPE_END_REGEX_PATTERN); string sentence; // read config file auto config = util::read_config_file(conf_filename().data()); @@ -331,52 +330,76 @@ namespace service { namespace util { - string number_regex() { - return "-?\\d+(?:\\.\\d+)"; - } - string join(cstr delim, string arr[], int n) { - string ans = "^\\s*" + arr[0]; - for (int i = 1; i < n; ++i) { - ans += delim; - ans += arr[i]; - } - ans += "\\s*$"; - return ans; - } + /** + * 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 + */ + const char* GESTURE_SWIPE_BEGIN_REGEX_PATTERN = + "^\\s*" // trim start of string + "\\s+event\\d+" // event + "\\s+GESTURE_SWIPE_BEGIN" // gesture + "\\s+\\S+" // timestamp + "\\s+(\\d+)" // fingers + "\\s*$" // trim end of string + ; - string build_gesture_begin() { - string device = "\\S+"; - string gesture = "GESTURE_SWIPE_BEGIN"; - string seconds = "\\S+"; - string fingers = "(\\d+)"; - string arr[] = {device, gesture, seconds, fingers}; - return join("\\s+", arr, 4); - } + /** + * 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 + */ + const char* GESTURE_SWIPE_END_REGEX_PATTERN = + "^\\s*" // trim start of string + "\\s+event\\d+" // event + "\\s+GESTURE_SWIPE_END" // gesture + "\\s+\\S+" // timestamp + "\\s+(\\d+)" // fingers + "\\s*$" // trim end of string + ; - string build_gesture_update() { - string device = "\\S+"; - string gesture = "GESTURE_SWIPE_UPDATE"; - string seconds = "\\S+"; - string fingers = "(\\d+)"; - string num_1 = "\\s*(" + number_regex() + ")"; - string num_2 = num_1; - string num_div = num_1 + "/" + num_2; - string num_accel = "\\(" + num_div + "\\s+unaccelerated\\)"; - string arr[] = {device, gesture, seconds, fingers, num_div, num_accel}; - string result = join("\\s+", arr, 6); - return result; - } + // matches signed decimal numbers (eg. "6.02" "-1.1") + #define CF_NUMBER_REGEX "-?\\d+(?:\\.\\d+)" - string build_gesture_end() { - string device = "\\S+"; - string gesture = "GESTURE_SWIPE_END"; - string seconds = "\\S+"; - string fingers = "(\\d+)"; - string arr[] = {device, gesture, seconds, fingers}; - return join("\\s+", arr, 4); - } + // matches and extracts a space-prefixed signed fraction (eg. "-3.00/ 5.12") + #define CF_NUMBER_DIVISION "\\s*(" CF_NUMBER_REGEX ")/\\s*(" CF_NUMBER_REGEX ")" + + /** + * 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 + */ + const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN = + "^\\s*" // trim start of string + "\\s+event\\d+" // event + "\\s+GESTURE_SWIPE_UPDATE" // gesture + "\\s+\\S+" // timestamp + "\\s+(\\d+)" // fingers + "\\s+" CF_NUMBER_DIVISION // speed (dx/dy) + "\\s+\\(" CF_NUMBER_DIVISION "\\s+unaccelerated\\)" // unaccelerated speed (udx/udy) + "\\s*$" // trim end of string + ; + + // delete macros + #undef CF_NUMBER_DIVISION + #undef CF_NUMBER_EXTRACT + #undef CF_NUMBER_REGEX + /** + * A utility method for reading the config file. + * + * @param filename (const char*) the path of the config file. + */ map read_config_file(const char* filename) { map conf; ifstream fin(filename); From cdaa98abe18bb8b19dff917a307641a5ac78a039 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Thu, 7 Feb 2019 02:12:19 +0800 Subject: [PATCH 3/4] Compile regex outside of buffer function to avoid runtime hiccup --- src/comfortable-swipe.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/comfortable-swipe.cpp b/src/comfortable-swipe.cpp index 841cf3a..9793a4f 100644 --- a/src/comfortable-swipe.cpp +++ b/src/comfortable-swipe.cpp @@ -206,14 +206,17 @@ namespace service { } namespace service { + + // process regex at compile time + const regex gesture_begin(util::GESTURE_SWIPE_BEGIN_REGEX_PATTERN); + const regex gesture_update(util::GESTURE_SWIPE_UPDATE_REGEX_PATTERN); + const regex gesture_end(util::GESTURE_SWIPE_END_REGEX_PATTERN); + // parses output from libinput debug-events void buffer() { // check first if $user ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); - const regex gesture_begin(util::GESTURE_SWIPE_BEGIN_REGEX_PATTERN); - const regex gesture_update(util::GESTURE_SWIPE_UPDATE_REGEX_PATTERN); - const regex gesture_end(util::GESTURE_SWIPE_END_REGEX_PATTERN); string sentence; // read config file auto config = util::read_config_file(conf_filename().data()); @@ -252,7 +255,7 @@ namespace service { } // starts service void start() { - int x = system("stdbuf -oL -e0 libinput debug-events | " PROGRAM " buffer"); + int x = system("stdbuf -oL -eL libinput debug-events | " PROGRAM " buffer"); } // stops service void stop() { @@ -380,14 +383,14 @@ namespace util { * fingers dx dy udx udy */ const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN = - "^\\s*" // trim start of string - "\\s+event\\d+" // event - "\\s+GESTURE_SWIPE_UPDATE" // gesture - "\\s+\\S+" // timestamp - "\\s+(\\d+)" // fingers + "^\\s*" // trim start of string + "\\s+event\\d+" // event + "\\s+GESTURE_SWIPE_UPDATE" // gesture + "\\s+\\S+" // timestamp + "\\s+(\\d+)" // fingers "\\s+" CF_NUMBER_DIVISION // speed (dx/dy) - "\\s+\\(" CF_NUMBER_DIVISION "\\s+unaccelerated\\)" // unaccelerated speed (udx/udy) - "\\s*$" // trim end of string + "\\s+\\(" CF_NUMBER_DIVISION "\\s+unaccelerated\\)" // unaccelerated speed (udx/udy) + "\\s*$" // trim end of string ; // delete macros From 2b3dfd00b1d6a48376c317c88a26229533d22ad9 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Thu, 7 Feb 2019 04:18:43 +0800 Subject: [PATCH 4/4] Catch dash symbol before event, remove trimming --- src/comfortable-swipe.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/comfortable-swipe.cpp b/src/comfortable-swipe.cpp index 9793a4f..e7643d4 100644 --- a/src/comfortable-swipe.cpp +++ b/src/comfortable-swipe.cpp @@ -343,12 +343,12 @@ namespace util { * fingers */ const char* GESTURE_SWIPE_BEGIN_REGEX_PATTERN = - "^\\s*" // trim start of string - "\\s+event\\d+" // event + "^" // start of string + "[ -]event\\d+" // event "\\s+GESTURE_SWIPE_BEGIN" // gesture "\\s+\\S+" // timestamp "\\s+(\\d+)" // fingers - "\\s*$" // trim end of string + "$" // end of string ; /** @@ -360,12 +360,12 @@ namespace util { * fingers */ const char* GESTURE_SWIPE_END_REGEX_PATTERN = - "^\\s*" // trim start of string - "\\s+event\\d+" // event + "^" // start of string + "[ -]event\\d+" // event "\\s+GESTURE_SWIPE_END" // gesture "\\s+\\S+" // timestamp "\\s+(\\d+)" // fingers - "\\s*$" // trim end of string + "$" // end of string ; // matches signed decimal numbers (eg. "6.02" "-1.1") @@ -383,14 +383,14 @@ namespace util { * fingers dx dy udx udy */ const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN = - "^\\s*" // trim start of string - "\\s+event\\d+" // event + "^" // start of string + "[ -]event\\d+" // event "\\s+GESTURE_SWIPE_UPDATE" // gesture "\\s+\\S+" // timestamp "\\s+(\\d+)" // fingers "\\s+" CF_NUMBER_DIVISION // speed (dx/dy) "\\s+\\(" CF_NUMBER_DIVISION "\\s+unaccelerated\\)" // unaccelerated speed (udx/udy) - "\\s*$" // trim end of string + "$" // end of string ; // delete macros