Delegate line parsing and regex matching to gesture class implementation
This commit is contained in:
parent
e7573938b4
commit
48a79607dc
@ -44,9 +44,10 @@ namespace comfortable_swipe::gesture
|
||||
~pinch_gesture();
|
||||
float radius, omega;
|
||||
|
||||
void begin() override;
|
||||
void update() override;
|
||||
void end() override;
|
||||
inline void begin() override;
|
||||
inline void update() override;
|
||||
inline void end() override;
|
||||
inline bool parse_line(const char *) override;
|
||||
|
||||
protected:
|
||||
// current location
|
||||
|
||||
@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <iostream> // std::cout, std::endl
|
||||
#include <regex> // std::regex, std::regex_match
|
||||
#include <string> // std::stof, std::stoi
|
||||
#include "xdo_gesture.h"
|
||||
|
||||
extern "C"
|
||||
@ -62,7 +64,7 @@ namespace comfortable_swipe::gesture
|
||||
/**
|
||||
* Hook on begin of swipe gesture.
|
||||
*/
|
||||
void swipe_gesture::begin()
|
||||
inline void swipe_gesture::begin()
|
||||
{
|
||||
xdo_get_mouse_location(this->xdo, &this->ix, &this->iy, &this->screen_num);
|
||||
this->previous_gesture = swipe_gesture::FRESH;
|
||||
@ -73,7 +75,7 @@ namespace comfortable_swipe::gesture
|
||||
/**
|
||||
* Hook on update of swipe gesture.
|
||||
*/
|
||||
void swipe_gesture::update()
|
||||
inline void swipe_gesture::update()
|
||||
{
|
||||
this->x += this->dx;
|
||||
this->y += this->dy;
|
||||
@ -121,9 +123,64 @@ namespace comfortable_swipe::gesture
|
||||
/**
|
||||
* Hook on end of swipe gesture.
|
||||
*/
|
||||
void swipe_gesture::end()
|
||||
inline void swipe_gesture::end()
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Parses an output line and dispatches swipe begin/update/end upon match.
|
||||
* Uses GESTURE_BEGIN_REGEX_PATTERN for begin,
|
||||
* GESTURE_UPDATE_REGEX_PATTERN for update,
|
||||
* GESTURE_END_REGEX_PATTERN for end.
|
||||
*
|
||||
* @param line the output line from libinput debug-events
|
||||
* @return true if line matches this gesture
|
||||
*/
|
||||
inline bool swipe_gesture::parse_line(const char * line)
|
||||
{
|
||||
// pre-compile regex patterns
|
||||
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);
|
||||
std::cmatch matches;
|
||||
if (this->flag_swiping)
|
||||
{
|
||||
// currently swiping
|
||||
if (std::regex_match(line, matches, gesture_swipe_update) != 0)
|
||||
{
|
||||
// update swipe
|
||||
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]);
|
||||
this->update();
|
||||
return true;
|
||||
}
|
||||
else if (std::regex_match(line, matches, gesture_swipe_end) != 0)
|
||||
{
|
||||
// end swipe
|
||||
this->flag_swiping = false;
|
||||
this->fingers = std::stoi(matches[1]);
|
||||
this->end();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else /* !flag_swiping */
|
||||
{
|
||||
// not swiping, check if swipe will begin
|
||||
if (std::regex_match(line, matches, gesture_swipe_begin) != 0)
|
||||
{
|
||||
// begin swipe
|
||||
this->flag_swiping = true;
|
||||
this->fingers = std::stoi(matches[1]);
|
||||
this->begin();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/* STATICS DEFINITIONS */
|
||||
const int swipe_gesture::MSK_THREE_FINGERS = 0;
|
||||
const int swipe_gesture::MSK_FOUR_FINGERS = 1;
|
||||
|
||||
@ -49,9 +49,10 @@ namespace comfortable_swipe::gesture
|
||||
int fingers;
|
||||
float dx, dy, udx, udy;
|
||||
|
||||
void begin() override;
|
||||
void update() override;
|
||||
void end() override;
|
||||
inline void begin() override;
|
||||
inline void update() override;
|
||||
inline void end() override;
|
||||
inline bool parse_line(const char *) override;
|
||||
|
||||
protected:
|
||||
// location of mouse
|
||||
@ -60,6 +61,7 @@ namespace comfortable_swipe::gesture
|
||||
// current location
|
||||
float x, y, threshold_squared;
|
||||
int previous_gesture;
|
||||
bool flag_swiping;
|
||||
const char ** commands;
|
||||
|
||||
public:
|
||||
|
||||
@ -42,9 +42,10 @@ namespace comfortable_swipe
|
||||
~xdo_gesture();
|
||||
|
||||
// hooks
|
||||
virtual void begin() = 0;
|
||||
virtual void update() = 0;
|
||||
virtual void end() = 0;
|
||||
virtual inline void begin() = 0;
|
||||
virtual inline void update() = 0;
|
||||
virtual inline void end() = 0;
|
||||
virtual inline 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,11 +32,6 @@ 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
|
||||
(
|
||||
@ -55,48 +48,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;
|
||||
static char line[MAX_LINE_LENGTH];
|
||||
|
||||
// start reading lines from input one by one
|
||||
while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL)
|
||||
while (fgets_unlocked(line, 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();
|
||||
}
|
||||
}
|
||||
|
||||
swipe.parse_line(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user