Compare commits

...

6 Commits

Author SHA1 Message Date
Rico Tiongson
a47c7f8115 Bootstrap pinch gesture 2019-02-12 21:39:53 +08:00
Rico Tiongson
0fdf94b723 Merge current branch with upstream 'master' 2019-02-12 21:27:43 +08:00
Rico Tiongson
bf09ee79f5 Fix bugs in compilation in implementation file 2019-02-07 23:20:39 +08:00
Rico Tiongson
7b0502c0b7 Update pinch gesture logic 2019-02-07 23:14:58 +08:00
Rico Tiongson
48a79607dc Delegate line parsing and regex matching to gesture class implementation 2019-02-07 22:51:23 +08:00
Rico Tiongson
e7573938b4 Bootstrap pinch_gestures class 2019-02-07 22:30:03 +08:00
12 changed files with 397 additions and 33 deletions

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
# C++ generated headers
*.gch
# IDE-specific
.vscode

View File

@ -28,6 +28,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "gesture/xdo_gesture.cpp"
#include "gesture/swipe_gesture.cpp"
#include "gesture/swipe_gesture.regex.cpp"
#include "gesture/pinch_gesture.cpp"
#include "gesture/pinch_gesture.regex.cpp"
#include "service/autostart.cpp"
#include "service/buffer.cpp"
#include "service/debug.cpp"

View File

@ -0,0 +1,172 @@
/* WARNING: pinch gestures are experimental */
#ifndef __COMFORTABLE_SWIPE__gesture_pinch_gesture__
#define __COMFORTABLE_SWIPE__gesture_pinch_gesture__
/*
Comfortable Swipe
by Rico Tiongson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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 <iostream> // std::cout, std::endl
#include <regex> // std::regex, std::cmatch, std::regex_match
#include "xdo_gesture.h"
#include "pinch_gesture.h"
extern "C"
{
#include <xdo.h> // xdo, xdo_new, xdo_free,
// xdo_get_mouse_location
// CURRENT_WINDOW
}
namespace comfortable_swipe::gesture
{
/**
* Constructs a new pinch gesture, given configurations for certain pinch events.
*/
pinch_gesture::pinch_gesture
(
const float threshold,
const char* pinch_in3,
const char* pinch_in4,
const char* pinch_out3,
const char* pinch_out4
):
comfortable_swipe::gesture::xdo_gesture(),
threshold(threshold),
commands(new const char*[4]{pinch_in3, pinch_in4, pinch_out3, pinch_out4})
{ }
/**
* Destructs this pinch gesture.
*/
pinch_gesture::~pinch_gesture()
{
delete[] commands;
}
/**
* Hook on begin of pinch gesture.
*/
inline void pinch_gesture::begin()
{
this->previous_gesture = pinch_gesture::FRESH;
this->previous_radius = this->radius;
}
/**
* Hook on update of swipe gesture.
*/
inline void pinch_gesture::update()
{
float delta_radius = this->radius - this->previous_radius;
this->previous_radius = this->radius;
// TODO: use a different epsilon threshold
const float EPSILON = this->threshold;
if (delta_radius > EPSILON)
{
if (this->previous_gesture != 0)
{
// TODO
std::cout << "PINCH OUT" << std::endl;
this->previous_gesture = 0;
}
}
else if (delta_radius < -EPSILON)
{
if (this->previous_gesture != 1)
{
// TODO
std::cout << "PINCH IN" << std::endl;
this->previous_gesture = 1;
}
}
}
/**
* Hook on end of swipe gesture.
*/
inline void pinch_gesture::end()
{ }
/**
* Parses an output line and dispatches pinch 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 pinch_gesture::parse_line(const char * line)
{
// pre-compile regex patterns
static const std::regex gesture_swipe_begin(pinch_gesture::GESTURE_BEGIN_REGEX_PATTERN);
static const std::regex gesture_swipe_update(pinch_gesture::GESTURE_UPDATE_REGEX_PATTERN);
static const std::regex gesture_swipe_end(pinch_gesture::GESTURE_END_REGEX_PATTERN);
std::cmatch matches;
if (this->flag_pinching)
{
// currently pinching
if (std::regex_match(line, matches, gesture_swipe_update) != 0)
{
// update pinch
this->fingers = std::stoi(matches[1]);
this->radius = std::stof(matches[2]);
this->update();
return true;
}
else if (std::regex_match(line, matches, gesture_swipe_end) != 0)
{
// end pinch
this->flag_pinching = false;
this->fingers = std::stoi(matches[1]);
this->end();
return true;
}
}
else /* if (!this->flag_pinching) */
{
// not swiping, check if swipe will begin
if (std::regex_match(line, matches, gesture_swipe_begin) != 0)
{
// begin swipe
this->flag_pinching = true;
this->fingers = std::stoi(matches[1]);
this->begin();
return true;
}
}
return false;
}
/* STATICS DEFINITIONS */
const int pinch_gesture::MSK_THREE_FINGERS = 0;
const int pinch_gesture::MSK_FOUR_FINGERS = 1;
const int pinch_gesture::MSK_NEGATIVE = 0;
const int pinch_gesture::MSK_POSITIVE = 2;
const int pinch_gesture::FRESH = -1;
const char * const pinch_gesture::command_map[4] = {
"pinch_in 3",
"pinch_in 4",
"pinch_out 3",
"pinch_out 4"
};
}
#endif /* __COMFORTABLE_SWIPE__gesture_pinch_gesture__ */

View File

@ -0,0 +1,80 @@
/* WARNING: experimental */
#ifndef __COMFORTABLE_SWIPE__gesture_pinch_gesture_h__
#define __COMFORTABLE_SWIPE__gesture_pinch_gesture_h__
/*
Comfortable Swipe
by Rico Tiongson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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 "xdo_gesture.h"
#ifdef __cplusplus
extern "C" {
#endif
namespace comfortable_swipe::gesture
{
class pinch_gesture : protected xdo_gesture
{
public:
// constructor
pinch_gesture(
const float threshold,
const char* pinch_in3,
const char* pinch_in4,
const char* pinch_out3,
const char* pinch_out4
);
~pinch_gesture();
int fingers;
float radius;
inline void begin() override;
inline void update() override;
inline void end() override;
inline bool parse_line(const char *) override;
protected:
// current location
float previous_radius;
float threshold;
int previous_gesture;
bool flag_pinching;
const char ** commands;
public:
// static constants
static const int MSK_THREE_FINGERS;
static const int MSK_FOUR_FINGERS;
static const int MSK_NEGATIVE;
static const int MSK_POSITIVE;
static const int FRESH;
static const char * const command_map[4];
// regex patterns
static const char* GESTURE_BEGIN_REGEX_PATTERN;
static const char* GESTURE_UPDATE_REGEX_PATTERN;
static const char* GESTURE_END_REGEX_PATTERN;
};
}
#ifdef __cplusplus
}
#endif
#endif /* __COMFORTABLE_SWIPE__gesture_pinch_gesture_h__ */

View File

@ -0,0 +1,96 @@
/* WARNING: pinch gestures are experimental */
#ifndef __COMFORTABLE_SWIPE__gesture_pinch_gesture_regex__
#define __COMFORTABLE_SWIPE__gesture_pinch_gesture_regex__
/*
Comfortable Swipe
by Rico Tiongson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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 "pinch_gesture.h"
namespace comfortable_swipe::gesture
{
/**
* Regex pattern for the libinput entry for start of pinch.
* Extracts one match for the number of fingers used during the pionch.
*
* eg. event15 GESTURE_PINCH_BEGIN +34.33s 3
* ^
* fingers
*/
const char* pinch_gesture::GESTURE_BEGIN_REGEX_PATTERN =
"^" // start of string
"[ -]event\\d+" // event
"\\s+GESTURE_PINCH_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_PINCH_END +35.03s 3
* ^
* fingers
*/
const char* pinch_gesture::GESTURE_END_REGEX_PATTERN =
"^" // start of string
"[ -]event\\d+" // event
"\\s+GESTURE_PINCH_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 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 pinch.
* Extracts number of fingers used and the speed (normal and accelerated) of the pinch.
* Extracts radius and rotational velocity of the pinch motion as well.
*
* eg. event8 GESTURE_PINCH_UPDATE +128.15s 3 -1.64/ 2.08 (-4.43/ 5.62 unaccelerated) 1.40 @ -0.14
* ^ ^ ^ ^ ^ ^ ^
* fingers dx dy udx udy radius omega
*/
const char* pinch_gesture::GESTURE_UPDATE_REGEX_PATTERN =
"^" // start of string
"[ -]event\\d+" // event
"\\s+GESTURE_PINCH_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+(" CF_NUMBER_REGEX ")" // radius
" @\\s+" CF_NUMBER_REGEX "" // angular velocity (omega)
"\\s*$" // end of string
;
// delete macros
#undef CF_NUMBER_DIVISION
#undef CF_NUMBER_EXTRACT
#undef CF_NUMBER_REGEX
}
#endif /* __COMFORTABLE_SWIPE__gesture_pinch_gesture_regex__ */

View File

@ -20,8 +20,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream> // std::cout, std::endl
#include <string> // std::stoi, std::stof
#include <regex> // std::regex, std::regex_match, std::cmatch
#include <regex> // std::regex, std::regex_match
#include <string> // std::stof, std::stoi
#include "xdo_gesture.h"
#include "swipe_gesture.h"
extern "C"
@ -67,7 +68,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;
@ -78,7 +79,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;
@ -126,26 +127,25 @@ namespace comfortable_swipe::gesture
/**
* Hook on end of swipe gesture.
*/
void swipe_gesture::end()
inline void swipe_gesture::end()
{ }
/**
* Dispatches begin/update/end depending on the regex pattern provided by this class.
* 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 line from libinput debug-events to parse
* @return true if begin/update/end was dispatched
* @param line the output line from libinput debug-events
* @return true if line matches this gesture
*/
bool swipe_gesture::parse_line(const char * line)
inline bool swipe_gesture::parse_line(const char * line)
{
// prepare regex matchers (will only load at most once)
// 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);
// prepare holder for regex matches
static std::cmatch matches;
std::cmatch matches;
if (this->flag_swiping)
{
// currently swiping
@ -171,7 +171,7 @@ namespace comfortable_swipe::gesture
return true;
}
}
else
else /* if (!this->flag_swiping) */
{
// not swiping, check if swipe will begin
if (std::regex_match(line, matches, gesture_swipe_begin) != 0)

View File

@ -49,10 +49,10 @@ namespace comfortable_swipe::gesture
int fingers;
float dx, dy, udx, udy;
void begin() override;
void update() override;
void end() override;
bool parse_line(const char *) override;
inline void begin() override;
inline void update() override;
inline void end() override;
inline bool parse_line(const char *) override;
protected:
// location of mouse

View File

@ -19,12 +19,14 @@ 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 "swipe_gesture.h"
namespace comfortable_swipe::gesture
{
/**
* 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
@ -41,7 +43,7 @@ namespace comfortable_swipe::gesture
/**
* 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
@ -64,7 +66,7 @@ namespace comfortable_swipe::gesture
/**
* 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

View File

@ -42,10 +42,10 @@ namespace comfortable_swipe
~xdo_gesture();
// hooks
virtual void begin() = 0;
virtual void update() = 0;
virtual void end() = 0;
virtual bool parse_line(const char *) = 0;
virtual inline void begin() = 0;
virtual inline void update() = 0;
virtual inline void end() = 0;
virtual inline bool parse_line(const char *) = 0;
};
}
}

View File

@ -36,6 +36,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gesture/xdo_gesture.h"
#include "gesture/swipe_gesture.h"
#include "gesture/pinch_gesture.h"
extern "C"
{
namespace comfortable_swipe

View File

@ -46,15 +46,23 @@ namespace comfortable_swipe::service
config["down4"].c_str()
);
comfortable_swipe::gesture::pinch_gesture pinch_gesture_handler
(
config.count("threshold") ? std::stof(config["threshold"]) : 0.0,
config["pinch_in3"].c_str(),
config["pinch_in4"].c_str(),
config["pinch_out3"].c_str(),
config["pinch_out4"].c_str()
);
// prepare data containers
static const int MAX_LINE_LENGTH = 256;
static char data[MAX_LINE_LENGTH];
std::array<char, 256> line;
// start reading lines from input one by one
while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL)
while (fgets_unlocked(line.data(), line.size(), stdin) != NULL)
{
// attempt to parse swipe gestures
swipe_gesture_handler.parse_line(data);
swipe_gesture_handler.parse_line(line.data()) ||
pinch_gesture_handler.parse_line(line.data());
}
}
}

View File

@ -3,4 +3,4 @@
DIR=$(dirname $0)
g++ -std=c++11 -O2 $DIR/test_regex.cpp -lxdo -o test.out || exec "Test aborted"
./test.out || rm test.out
rm test.out
rm test.out