From be9d711d6f09a35b044eab8312272e901c688a32 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Thu, 7 Feb 2019 16:32:04 +0800 Subject: [PATCH] Add util module --- .../util/autostart_filename.cpp | 28 +++++++ src/comfortable_swipe/util/conf_filename.cpp | 17 ++++ .../util/read_config_file.cpp | 78 +++++++++++++++++++ src/comfortable_swipe/util/regex.cpp | 71 +++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 src/comfortable_swipe/util/autostart_filename.cpp create mode 100644 src/comfortable_swipe/util/conf_filename.cpp create mode 100644 src/comfortable_swipe/util/read_config_file.cpp create mode 100644 src/comfortable_swipe/util/regex.cpp diff --git a/src/comfortable_swipe/util/autostart_filename.cpp b/src/comfortable_swipe/util/autostart_filename.cpp new file mode 100644 index 0000000..4f8c347 --- /dev/null +++ b/src/comfortable_swipe/util/autostart_filename.cpp @@ -0,0 +1,28 @@ +#ifndef __COMFORTABLE_SWIPE__util_conf_filename__ +#define __COMFORTABLE_SWIPE__util_conf_filename__ + +#include // std::string +#include // getenv + +namespace comfortable_swipe::util +{ + /** + * The path where the autostart configuration is located. + */ + const char* autostart_filename() + { + static std::string filename; + if (filename.empty()) { + const char* xdg_config = getenv("XDG_CONFIG_HOME"); + std::string config( + xdg_config == NULL + ? std::string(getenv("HOME")) + "/.config" + : xdg_config + ); + filename = config + "/autostart/comfortable-swipe.desktop"; + } + return filename.data(); + } +} + +#endif /* __COMFORTABLE_SWIPE__util_conf_filename__ */ diff --git a/src/comfortable_swipe/util/conf_filename.cpp b/src/comfortable_swipe/util/conf_filename.cpp new file mode 100644 index 0000000..d6da08f --- /dev/null +++ b/src/comfortable_swipe/util/conf_filename.cpp @@ -0,0 +1,17 @@ +#ifndef __COMFORTABLE_SWIPE__util_conf_filename__ +#define __COMFORTABLE_SWIPE__util_conf_filename__ + +#include "../index.hpp" + +namespace comfortable_swipe::util +{ + /** + * The path where the configuration file is located. + */ + constexpr const char* conf_filename() + { + return __COMFORTABLE_SWIPE__CONFIG__; + } +} + +#endif /* __COMFORTABLE_SWIPE__util_conf_filename__ */ diff --git a/src/comfortable_swipe/util/read_config_file.cpp b/src/comfortable_swipe/util/read_config_file.cpp new file mode 100644 index 0000000..2ab2cdb --- /dev/null +++ b/src/comfortable_swipe/util/read_config_file.cpp @@ -0,0 +1,78 @@ +#include // std::map +#include // std::string +#include // std::ifstream +#include // std::cerr, std::endl, std::getline +#include // exit +#include // std::isspace + +namespace comfortable_swipe::util +{ + /** + * A utility method for reading the config file. + * + * @param filename (const char*) the path of the config file. + */ + std::map read_config_file(const char* filename) + { + + std::map conf; + std::ifstream fin(filename); + + if (!fin.is_open()) + { + std::cerr << "file \"" << filename << "\" does not exist!" << std::endl; + exit(1); + } + + static std::string line, token[2]; + int line_number = 0; + + while (std::getline(fin, line)) + { + ++line_number; + token[0].clear(); + token[1].clear(); + int length = line.length(); + int equal_flag = 0; + + // tokenize comfig config + for (int i = 0; i < length; ++i) + { + if (line[i] == '#') // skip comments + break; + if (line[i] == '=') // flag equal sign + { + if (++equal_flag > 1) + { + std::cerr << "error in conf file " << filename << std::endl; + std::cerr << "multiple equal signs in line " << line_number << std::endl; + exit(1); + } + } + else if (!std::isspace(line[i])) + { + // add to buffer + token[equal_flag].push_back(line[i]); + } + } + + // ignore empty lines + if (equal_flag == 0 && token[0].length() == 0) + continue; + + // no equal sign found in non-empty line + if (equal_flag == 0) + { + std::cerr << "error in conf file: " << filename << std::endl; + std::cerr << "equal sign expected in line " << line_number << std::endl; + exit(1); + } + + // equal sign found, add to tokens + if (token[1].length() > 0) + conf[token[0]] = token[1]; + } + + return conf; + } +} diff --git a/src/comfortable_swipe/util/regex.cpp b/src/comfortable_swipe/util/regex.cpp new file mode 100644 index 0000000..9dc12e3 --- /dev/null +++ b/src/comfortable_swipe/util/regex.cpp @@ -0,0 +1,71 @@ +#ifndef __COMFORTABLE_SWIPE__util_regex__ +#define __COMFORTABLE_SWIPE__util_regex__ + +namespace comfortable_swipe::util +{ + /** + * 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 = + "^" // start of string + "[ -]event\\d+" // event + "\\s+GESTURE_SWIPE_BEGIN" // gesture + "\\s+\\S+" // timestamp + "\\s+(\\d+)" // fingers + "\\s*$" // end of string + ; + + /** + * 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 = + "^" // start of string + "[ -]event\\d+" // event + "\\s+GESTURE_SWIPE_END" // gesture + "\\s+\\S+" // timestamp + "\\s+(\\d+)" // fingers + "\\s*$" // end of string + ; + + // matches signed decimal numbers (eg. "6.02" "-1.1") + #define CF_NUMBER_REGEX "-?\\d+(?:\\.\\d+)" + + // 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 = + "^" // 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*$" // end of string + ; + + // delete macros + #undef CF_NUMBER_DIVISION + #undef CF_NUMBER_EXTRACT + #undef CF_NUMBER_REGEX +} + +#endif /* __COMFORTABLE_SWIPE__util_regex__ */