Apply clang format

This commit is contained in:
Rico Tiongson 2020-04-20 19:36:26 +08:00
parent 45ab9f9c16
commit ffe664c7d8
5 changed files with 84 additions and 78 deletions

View File

@ -22,7 +22,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
* COMFORTABLE_SWIPE_CONFIG * COMFORTABLE_SWIPE_CONFIG
* *
* This definition points to the location of the configuration file. * This definition points to the location of the configuration file.
* A warning will be issued to the compiler if you do not define this at compile time. * A warning will be issued to the compiler if you do not define this at compile
* time.
*/ */
#ifndef COMFORTABLE_SWIPE_CONFIG #ifndef COMFORTABLE_SWIPE_CONFIG
#pragma message "Please define COMFORTABLE_SWIPE_CONFIG during compilation!" #pragma message "Please define COMFORTABLE_SWIPE_CONFIG during compilation!"
@ -33,7 +34,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
* COMFORTABLE_SWIPE_AUTOSTART * COMFORTABLE_SWIPE_AUTOSTART
* *
* This definition points to the location of the autostart (.desktop) file. * This definition points to the location of the autostart (.desktop) file.
* A warning will be issued to the compiler if you do not define this at compile time. * A warning will be issued to the compiler if you do not define this at compile
* time.
*/ */
#ifndef COMFORTABLE_SWIPE_AUTOSTART #ifndef COMFORTABLE_SWIPE_AUTOSTART
#pragma message "Please define COMFORTABLE_SWIPE_AUTOSTART during compilation!" #pragma message "Please define COMFORTABLE_SWIPE_AUTOSTART during compilation!"

View File

@ -19,8 +19,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <iostream> // std::cout, std::endl
#include <array> // std::array #include <array> // std::array
#include <iostream> // std::cout, std::endl
#include <string> // std::string #include <string> // std::string
extern "C" { extern "C" {
@ -40,37 +40,38 @@ namespace comfortable_swipe {
* When constructing, please mind the order in the * When constructing, please mind the order in the
* `gesture_swipe_xdokey::command_name[]` static array. * `gesture_swipe_xdokey::command_name[]` static array.
*/ */
class gesture_swipe_xdokey : public gesture_swipe class gesture_swipe_xdokey : public gesture_swipe {
{
public: public:
// stores the 8 command strings for xdo to execute // stores the 8 command strings for xdo to execute
std::array<std::string, 8> commands; std::array<std::string, 8> commands;
// constructor // constructor
gesture_swipe_xdokey(const decltype(commands)& commands, float threshold=0); gesture_swipe_xdokey(const decltype(commands) &commands, float threshold = 0);
// destructor // destructor
virtual ~gesture_swipe_xdokey(); virtual ~gesture_swipe_xdokey();
// hooks that we override // hooks that we override
virtual void begin() override; virtual void begin() override;
virtual void update() override; virtual void update() override;
virtual void end() override; virtual void end() override;
// override this when keyboard gesture is to be performed // override this when keyboard gesture is to be performed
virtual void do_keyboard_gesture(int mask); virtual void do_keyboard_gesture(int mask);
protected: protected:
// stores square of threshold so we can compute faster // stores square of threshold so we can compute faster
float threshold_squared; float threshold_squared;
// stores previous gesture so we don't repeat action // stores previous gesture so we don't repeat action
int previous_gesture; int previous_gesture;
public: public:
// static enums we will use for gesture matching // static enums we will use for gesture matching
static const int FRESH = -1; static const int FRESH = -1;
static const int MSK_THREE_FINGERS = 0; static const int MSK_THREE_FINGERS = 0;
static const int MSK_FOUR_FINGERS = 1; static const int MSK_FOUR_FINGERS = 1;
static const int MSK_NEGATIVE = 0; static const int MSK_NEGATIVE = 0;
static const int MSK_POSITIVE = 2; static const int MSK_POSITIVE = 2;
static const int MSK_HORIZONTAL = 0; static const int MSK_HORIZONTAL = 0;
static const int MSK_VERTICAL = 4; static const int MSK_VERTICAL = 4;
// mappings to our command string names // mappings to our command string names
static const std::string command_name[]; static const std::string command_name[];
}; };
/** /**
* Our mapped directional command names. These will be * Our mapped directional command names. These will be
@ -81,8 +82,8 @@ static const std::string command_name[];
* Refer to the order of this array for the "commands" * Refer to the order of this array for the "commands"
* paramter in the constructor. * paramter in the constructor.
*/ */
decltype(gesture_swipe_xdokey::command_name) decltype(
gesture_swipe_xdokey::command_name = { gesture_swipe_xdokey::command_name) gesture_swipe_xdokey::command_name = {
// the order of variables is based on bitmasking // the order of variables is based on bitmasking
// <VERTICAL?> | <POSITIVE?> | <FOUR FINGERS?> // <VERTICAL?> | <POSITIVE?> | <FOUR FINGERS?>
"left3", // 000 "left3", // 000
@ -102,13 +103,13 @@ gesture_swipe_xdokey::command_name = {
* scrolling direction (ie. left3 is natural movement of 3 fingers left). * scrolling direction (ie. left3 is natural movement of 3 fingers left).
*/ */
gesture_swipe_xdokey::gesture_swipe_xdokey( gesture_swipe_xdokey::gesture_swipe_xdokey(
const decltype(gesture_swipe_xdokey::commands)& commands, const decltype(gesture_swipe_xdokey::commands) &commands, float threshold)
float threshold : gesture_swipe(), commands(commands),
): gesture_swipe(), commands(commands), threshold_squared(threshold * threshold) {} threshold_squared(threshold * threshold) {}
/** /**
* Destructs this keyboard swipe gesture. * Destructs this keyboard swipe gesture.
*/ */
gesture_swipe_xdokey::~gesture_swipe_xdokey() { } gesture_swipe_xdokey::~gesture_swipe_xdokey() {}
/** /**
* Hook on begin of swipe gesture. * Hook on begin of swipe gesture.
*/ */
@ -150,8 +151,7 @@ void gesture_swipe_xdokey::update() {
mask |= gesture_swipe_xdokey::MSK_NEGATIVE; mask |= gesture_swipe_xdokey::MSK_NEGATIVE;
else else
mask |= gesture_swipe_xdokey::MSK_POSITIVE; mask |= gesture_swipe_xdokey::MSK_POSITIVE;
} } else /* std::abs(x) <= std::abs(y) */
else /* std::abs(x) <= std::abs(y) */
{ {
// gesture is vertical // gesture is vertical
mask |= gesture_swipe_xdokey::MSK_VERTICAL; mask |= gesture_swipe_xdokey::MSK_VERTICAL;
@ -162,8 +162,7 @@ void gesture_swipe_xdokey::update() {
} }
// send command on fresh OR opposite gesture // send command on fresh OR opposite gesture
if (this->previous_gesture == gesture_swipe_xdokey::FRESH || if (this->previous_gesture == gesture_swipe_xdokey::FRESH ||
this->previous_gesture == this->previous_gesture == (mask ^ gesture_swipe_xdokey::MSK_POSITIVE)) {
(mask ^ gesture_swipe_xdokey::MSK_POSITIVE)) {
// finally, perform keyboard gesture // finally, perform keyboard gesture
this->do_keyboard_gesture(mask); this->do_keyboard_gesture(mask);
} }
@ -174,7 +173,8 @@ void gesture_swipe_xdokey::update() {
*/ */
void gesture_swipe_xdokey::do_keyboard_gesture(int mask) { void gesture_swipe_xdokey::do_keyboard_gesture(int mask) {
// perform our keyboard command with xdo_send_keysequence // perform our keyboard command with xdo_send_keysequence
xdo_send_keysequence_window(this->xdo, CURRENTWINDOW, commands[mask].data(), 0); xdo_send_keysequence_window(this->xdo, CURRENTWINDOW, commands[mask].data(),
0);
std::cout << "SWIPE " << command_name[mask] << std::endl; std::cout << "SWIPE " << command_name[mask] << std::endl;
// reset our location variables // reset our location variables
this->x = this->y = 0; this->x = this->y = 0;
@ -187,5 +187,5 @@ void gesture_swipe_xdokey::end() {
// just call superclass method // just call superclass method
gesture_swipe::end(); gesture_swipe::end();
} }
} } // namespace comfortable_swipe
#endif /* __comfortable_swipe_gesture_swipe_xdokey__ */ #endif /* __comfortable_swipe_gesture_swipe_xdokey__ */

View File

@ -83,10 +83,12 @@ public:
virtual bool is_holding() const; virtual bool is_holding() const;
// utility method to parse mouse input given config characters // utility method to parse mouse input given config characters
static int parse_mouse_button(const char *); static int parse_mouse_button(const char *);
protected: protected:
// command holders // command holders
const char *hold3; const char *hold3;
const char *hold4; const char *hold4;
private: private:
// flag we can use to check if mouse is down // flag we can use to check if mouse is down
bool flag_is_holding; bool flag_is_holding;
@ -94,7 +96,8 @@ private:
/** /**
* Constructs a new mouse gesture, given "hold3" and "hold4" configurations. * Constructs a new mouse gesture, given "hold3" and "hold4" configurations.
*/ */
gesture_swipe_xdomouse::gesture_swipe_xdomouse(const char *hold3, const char *hold4) gesture_swipe_xdomouse::gesture_swipe_xdomouse(const char *hold3,
const char *hold4)
: gesture_swipe(), button(MOUSE_NONE), hold3(hold3), hold4(hold4), : gesture_swipe(), button(MOUSE_NONE), hold3(hold3), hold4(hold4),
flag_is_holding(false) {} flag_is_holding(false) {}
/** /**
@ -214,6 +217,6 @@ void gesture_swipe_xdomouse::end() {
// call superclass method // call superclass method
gesture_swipe::end(); gesture_swipe::end();
} }
} } // namespace comfortable_swipe
#endif /* __comfortable_swipe_gesture_swipe_xdomouse__ */ #endif /* __comfortable_swipe_gesture_swipe_xdomouse__ */

View File

@ -70,6 +70,7 @@ public:
virtual void update(); virtual void update();
virtual void end(); virtual void end();
virtual bool run(const char *); virtual bool run(const char *);
protected: protected:
// xdo container // xdo container
xdo_t *xdo; xdo_t *xdo;
@ -77,6 +78,7 @@ protected:
int screen_num, ix, iy; int screen_num, ix, iy;
// optimization flag for checking if GESTURE_SWIPE_BEGIN was dispatched // optimization flag for checking if GESTURE_SWIPE_BEGIN was dispatched
bool flag_swiping; bool flag_swiping;
public: public:
// defined in: comfortable-swipe-gesture-swipe.regex.cpp // defined in: comfortable-swipe-gesture-swipe.regex.cpp
static const std::regex GESTURE_SWIPE_BEGIN; static const std::regex GESTURE_SWIPE_BEGIN;
@ -140,8 +142,7 @@ bool gesture_swipe::run(const char *line) {
this->begin(); this->begin();
return true; return true;
} }
} } else {
else {
// currently swiping // currently swiping
if (std::regex_match(line, matches, GESTURE_SWIPE_UPDATE) != 0) { if (std::regex_match(line, matches, GESTURE_SWIPE_UPDATE) != 0) {
// assign necessary variables for swipe update // assign necessary variables for swipe update
@ -172,14 +173,14 @@ bool gesture_swipe::run(const char *line) {
* ^ * ^
* fingers * fingers
*/ */
const std::regex gesture_swipe::GESTURE_SWIPE_BEGIN( const std::regex
"^" // start of string gesture_swipe::GESTURE_SWIPE_BEGIN("^" // start of string
"[ -]event\\d+" // event "[ -]event\\d+" // event
"\\s+GESTURE_SWIPE_BEGIN" // gesture "\\s+GESTURE_SWIPE_BEGIN" // gesture
"\\s+\\S+" // timestamp "\\s+\\S+" // timestamp
"\\s+(\\d+)" // fingers "\\s+(\\d+)" // fingers
"\\s*$" // end of string "\\s*$" // end of string
); );
/** /**
* Regex pattern for the libinput entry for the end of swipe. * Regex pattern for the libinput entry for the end of swipe.
* Extracts one match for the number of fingers used during the swipe. * Extracts one match for the number of fingers used during the swipe.
@ -188,14 +189,14 @@ const std::regex gesture_swipe::GESTURE_SWIPE_BEGIN(
* ^ * ^
* fingers * fingers
*/ */
const std::regex gesture_swipe::GESTURE_SWIPE_END( const std::regex
"^" // start of string gesture_swipe::GESTURE_SWIPE_END("^" // start of string
"[ -]event\\d+" // event "[ -]event\\d+" // event
"\\s+GESTURE_SWIPE_END" // gesture "\\s+GESTURE_SWIPE_END" // gesture
"\\s+\\S+" // timestamp "\\s+\\S+" // timestamp
"\\s+(\\d+)" // fingers "\\s+(\\d+)" // fingers
"\\s*$" // end of string "\\s*$" // end of string
); );
// matches signed decimal numbers (eg. "6.02" "-1.1") // matches signed decimal numbers (eg. "6.02" "-1.1")
#define _NUMBER_REGEX "-?\\d+(?:\\.\\d+)" #define _NUMBER_REGEX "-?\\d+(?:\\.\\d+)"
// matches and extracts a space-prefixed signed fraction (eg. "-3.00/ 5.12") // matches and extracts a space-prefixed signed fraction (eg. "-3.00/ 5.12")

View File

@ -28,24 +28,24 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <iostream> // std::ios, std::cin, std::getline
#include <string> // std::string
#include <map> // std::map
#include "comfortable-swipe-defines.cpp" #include "comfortable-swipe-defines.cpp"
#include "comfortable-swipe-gesture-swipe.cpp"
#include "comfortable-swipe-gesture-swipe-xdokey.cpp" #include "comfortable-swipe-gesture-swipe-xdokey.cpp"
#include "comfortable-swipe-gesture-swipe-xdomouse.cpp" #include "comfortable-swipe-gesture-swipe-xdomouse.cpp"
#include "comfortable-swipe-gesture-swipe.cpp"
#include <iostream> // std::ios, std::cin, std::getline
#include <map> // std::map
#include <string> // std::string
extern "C" { extern "C" {
#include <ini.h> // ini_parse #include <ini.h> // ini_parse
} }
/** /**
* Registers keys and values from the config file to a map. * Registers keys and values from the config file to a map.
*/ */
int parse_config(void* config, const char section[], const char name[], const char value[]) int parse_config(void *config, const char section[], const char name[],
{ const char value[]) {
auto& configmap = *(std::map<std::string, std::string>*) config; auto &configmap = *(std::map<std::string, std::string> *)config;
configmap[name] = value; configmap[name] = value;
return 0; return 0;
} }
@ -76,8 +76,7 @@ int main() {
// up3=super+Up maximize // up3=super+Up maximize
// down3=super+Down minimize // down3=super+Down minimize
decltype(gesture_swipe_xdokey::commands) commands; decltype(gesture_swipe_xdokey::commands) commands;
for (size_t i = 0; i < commands.size(); ++i) for (size_t i = 0; i < commands.size(); ++i) {
{
commands[i] = config[gesture_swipe_xdokey::command_name[i]]; commands[i] = config[gesture_swipe_xdokey::command_name[i]];
} }
gesture_swipe_xdokey keyswipe(commands, stof(config["threshold"])); gesture_swipe_xdokey keyswipe(commands, stof(config["threshold"]));
@ -88,7 +87,8 @@ int main() {
// hold3=button1 hold button 1 on 3 fingers // hold3=button1 hold button 1 on 3 fingers
// hold4=button3 hold button 3 (right click) on 3 fingers // hold4=button3 hold button 3 (right click) on 3 fingers
// hold3=ignore <do nothing> // hold3=ignore <do nothing>
gesture_swipe_xdomouse mousehold(config["hold3"].data(), config["hold4"].data()); gesture_swipe_xdomouse mousehold(config["hold3"].data(),
config["hold4"].data());
// start reading lines from input one by one // start reading lines from input one by one
for (string line; getline(cin, line);) { for (string line; getline(cin, line);) {
// prioritize mouse hold gesture first before keyboard gesture // prioritize mouse hold gesture first before keyboard gesture