Improve semantics (#42)

* Keep consistency in buffer method

* Revert default threshold to 0.0
This commit is contained in:
Rico Tiongson 2019-02-07 18:46:41 +08:00 committed by GitHub
parent b171fb2cdf
commit f40145b6fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 106 additions and 94 deletions

View File

@ -51,7 +51,7 @@ Comfortable swipe makes use of keyboard shortcuts for configurations. The config
Property | Description | Default Value | Default Behavior 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 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 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 right3 | 3-finger swipe right | ctrl+shift+Left | switch to left workspace

View File

@ -16,8 +16,8 @@
# #
# (Note: Sky is the limit! Can be as large as 1000.0) # (Note: Sky is the limit! Can be as large as 1000.0)
# #
# Default: threshold = 20.0 # Default: threshold = 0.0
threshold = 20.0 threshold = 0.0
############################# #############################
# THREE / FOUR FINGER SWIPE # # THREE / FOUR FINGER SWIPE #

View File

@ -37,7 +37,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "gesture/swipe_gesture.h" #include "gesture/swipe_gesture.h"
extern "C" extern "C"
{ {
namespace comfortable_swipe::util namespace comfortable_swipe
{
namespace util
{ {
extern const char* GESTURE_SWIPE_BEGIN_REGEX_PATTERN; extern const char* GESTURE_SWIPE_BEGIN_REGEX_PATTERN;
extern const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN; extern const char* GESTURE_SWIPE_UPDATE_REGEX_PATTERN;
@ -46,7 +48,7 @@ extern "C"
constexpr const char* conf_filename(); constexpr const char* conf_filename();
std::map<std::string, std::string> read_config_file(const char*); std::map<std::string, std::string> read_config_file(const char*);
} }
namespace comfortable_swipe::service namespace service
{ {
void autostart(); void autostart();
void buffer(); void buffer();
@ -56,5 +58,6 @@ extern "C"
void stop(); void stop();
} }
} }
}
#endif /* __COMFORTABLE_SWIPE__index_hpp__ */ #endif /* __COMFORTABLE_SWIPE__index_hpp__ */

View File

@ -27,31 +27,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
/** /**
* Starts the comfortable-swipe service by buffering libinput debug-events. * Starts the comfortable-swipe service by buffering libinput debug-events.
*/ */
void comfortable_swipe::service::buffer() namespace comfortable_swipe::service
{ {
void buffer()
// import utility methods {
using comfortable_swipe::util::read_config_file; // read config file
using comfortable_swipe::util::conf_filename; auto config = comfortable_swipe::util::read_config_file(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 // pre-compile regex patterns
static const std::regex gesture_begin(GESTURE_SWIPE_BEGIN_REGEX_PATTERN); static const std::regex gesture_swipe_begin(comfortable_swipe::util::GESTURE_SWIPE_BEGIN_REGEX_PATTERN);
static const std::regex gesture_update(GESTURE_SWIPE_UPDATE_REGEX_PATTERN); static const std::regex gesture_swipe_update(comfortable_swipe::util::GESTURE_SWIPE_UPDATE_REGEX_PATTERN);
static const std::regex gesture_end(GESTURE_SWIPE_END_REGEX_PATTERN); static const std::regex gesture_swipe_end(comfortable_swipe::util::GESTURE_SWIPE_END_REGEX_PATTERN);
// read config file
auto config = read_config_file(conf_filename());
// initialize swipe gesture handler // initialize swipe gesture handler
swipe_gesture swipe comfortable_swipe::gesture::swipe_gesture swipe
( (
config.count("threshold") ? std::atof(config["threshold"].data()) : 0.0, config.count("threshold") ? std::stof(config["threshold"]) : 0.0,
config["left3"].c_str(), config["left3"].c_str(),
config["left4"].c_str(), config["left4"].c_str(),
config["right3"].c_str(), config["right3"].c_str(),
@ -68,24 +59,17 @@ void comfortable_swipe::service::buffer()
static std::cmatch matches; static std::cmatch matches;
// optimization flag for checking if GESTURE_SWIPE_BEGIN was dispatched // optimization flag for checking if GESTURE_SWIPE_BEGIN was dispatched
bool flag_begin = false; bool flag_swiping = false;
// start reading lines from input one by one // start reading lines from input one by one
while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL) while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL)
{ {
if (!flag_begin) if (flag_swiping)
{ {
if (std::regex_match(data, matches, gesture_begin) != 0) // currently swiping
{ if (std::regex_match(data, matches, gesture_swipe_update) != 0)
swipe.fingers = std::stoi(matches[1]);
swipe.begin();
flag_begin = true;
}
}
else /* flag_begin == true */
{
if (std::regex_match(data, matches, gesture_update) != 0)
{ {
// update swipe
swipe.fingers = std::stoi(matches[1]); swipe.fingers = std::stoi(matches[1]);
swipe.dx = std::stof(matches[2]); swipe.dx = std::stof(matches[2]);
swipe.dy = std::stof(matches[3]); swipe.dy = std::stof(matches[3]);
@ -93,13 +77,27 @@ void comfortable_swipe::service::buffer()
swipe.udy = std::stof(matches[5]); swipe.udy = std::stof(matches[5]);
swipe.update(); swipe.update();
} }
else if (std::regex_match(data, matches, gesture_end) != 0) else if (std::regex_match(data, matches, gesture_swipe_end) != 0)
{ {
// end swipe
flag_swiping = false;
swipe.fingers = std::stoi(matches[1]); swipe.fingers = std::stoi(matches[1]);
swipe.end(); swipe.end();
flag_begin = false;
} }
} }
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();
}
}
}
} }
} }

View File

@ -26,6 +26,8 @@ namespace comfortable_swipe::service
{ {
/** /**
* Starts the comfortable-swipe service by buffering libinput debug-events. * 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() void start()
{ {

View File

@ -25,22 +25,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
int main(int argc, char** args) int main(int argc, char** args)
{ {
using namespace comfortable_swipe::service;
if (argc > 1) if (argc > 1)
{ {
std::string arg = args[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();
}
// select based on argument
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 else
help(); comfortable_swipe::service::help();
return 0; return 0;
} }