Add parse_line method to decouple buffer from gesture handler (#50)
This commit is contained in:
parent
7923effa02
commit
459fd68ba8
@ -20,7 +20,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <iostream> // std::cout, std::endl
|
||||
#include "xdo_gesture.h"
|
||||
#include <string> // std::stoi, std::stof
|
||||
#include <regex> // std::regex, std::regex_match, std::cmatch
|
||||
#include "swipe_gesture.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
@ -127,6 +129,65 @@ namespace comfortable_swipe::gesture
|
||||
void swipe_gesture::end()
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Dispatches begin/update/end depending on the regex pattern provided by this class.
|
||||
*
|
||||
* @param line the line from libinput debug-events to parse
|
||||
* @return true if begin/update/end was dispatched
|
||||
*/
|
||||
bool swipe_gesture::parse_line(const char * line)
|
||||
{
|
||||
|
||||
// prepare regex matchers (will only load at most once)
|
||||
static const std::regex gesture_swipe_begin(swipe_gesture::GESTURE_BEGIN_REGEX_PATTERN);
|
||||
static const std::regex gesture_swipe_update(swipe_gesture::GESTURE_UPDATE_REGEX_PATTERN);
|
||||
static const std::regex gesture_swipe_end(swipe_gesture::GESTURE_END_REGEX_PATTERN);
|
||||
|
||||
// prepare holder for regex matches
|
||||
static std::cmatch matches;
|
||||
|
||||
if (this->flag_swiping)
|
||||
{
|
||||
// currently swiping
|
||||
if (std::regex_match(line, matches, gesture_swipe_update) != 0)
|
||||
{
|
||||
// assign necessary variables for swipe update
|
||||
this->fingers = std::stoi(matches[1]);
|
||||
this->dx = std::stof(matches[2]);
|
||||
this->dy = std::stof(matches[3]);
|
||||
this->udx = std::stof(matches[4]);
|
||||
this->udy = std::stof(matches[5]);
|
||||
// dispatch update
|
||||
this->update();
|
||||
return true;
|
||||
}
|
||||
else if (std::regex_match(line, matches, gesture_swipe_end) != 0)
|
||||
{
|
||||
// assign necessary variables for swipe end
|
||||
this->flag_swiping = false;
|
||||
this->fingers = std::stoi(matches[1]);
|
||||
// dispatch end
|
||||
this->end();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// not swiping, check if swipe will begin
|
||||
if (std::regex_match(line, matches, gesture_swipe_begin) != 0)
|
||||
{
|
||||
// assign necessary variables for swipe begin
|
||||
this->flag_swiping = true;
|
||||
this->fingers = std::stoi(matches[1]);
|
||||
// dispatch begin
|
||||
this->begin();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* STATICS DEFINITIONS */
|
||||
const int swipe_gesture::MSK_THREE_FINGERS = 0;
|
||||
const int swipe_gesture::MSK_FOUR_FINGERS = 1;
|
||||
|
||||
@ -52,7 +52,8 @@ namespace comfortable_swipe::gesture
|
||||
void begin() override;
|
||||
void update() override;
|
||||
void end() override;
|
||||
|
||||
bool parse_line(const char *) override;
|
||||
|
||||
protected:
|
||||
// location of mouse
|
||||
int screen_num, ix, iy;
|
||||
@ -62,6 +63,9 @@ namespace comfortable_swipe::gesture
|
||||
int previous_gesture;
|
||||
const char ** commands;
|
||||
|
||||
// optimization flag for checking if GESTURE_SWIPE_BEGIN was dispatched
|
||||
bool flag_swiping;
|
||||
|
||||
public:
|
||||
// static constants
|
||||
static const int MSK_THREE_FINGERS;
|
||||
|
||||
@ -45,6 +45,7 @@ namespace comfortable_swipe
|
||||
virtual void begin() = 0;
|
||||
virtual void update() = 0;
|
||||
virtual void end() = 0;
|
||||
virtual bool parse_line(const char *) = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,9 +19,7 @@ You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <string> // std::stoi, std::stof
|
||||
#include <cstdio> // std::fgets_unlocked, stdin
|
||||
#include <regex> // std::regex, std::regex_match, std::cmatch
|
||||
#include "../index.hpp"
|
||||
|
||||
/**
|
||||
@ -34,13 +32,8 @@ namespace comfortable_swipe::service
|
||||
// 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::gesture::swipe_gesture::GESTURE_BEGIN_REGEX_PATTERN);
|
||||
static const std::regex gesture_swipe_update(comfortable_swipe::gesture::swipe_gesture::GESTURE_UPDATE_REGEX_PATTERN);
|
||||
static const std::regex gesture_swipe_end(comfortable_swipe::gesture::swipe_gesture::GESTURE_END_REGEX_PATTERN);
|
||||
|
||||
// initialize swipe gesture handler
|
||||
comfortable_swipe::gesture::swipe_gesture swipe
|
||||
comfortable_swipe::gesture::swipe_gesture swipe_gesture_handler
|
||||
(
|
||||
config.count("threshold") ? std::stof(config["threshold"]) : 0.0,
|
||||
config["left3"].c_str(),
|
||||
@ -56,47 +49,12 @@ namespace comfortable_swipe::service
|
||||
// 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 (flag_swiping)
|
||||
{
|
||||
// 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_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();
|
||||
}
|
||||
}
|
||||
|
||||
// attempt to parse swipe gestures
|
||||
swipe_gesture_handler.parse_line(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user