Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28f5ca2a91 | ||
|
|
89b79bc9bc | ||
|
|
7d76db6693 | ||
|
|
204a422f2d | ||
|
|
c18cb04230 | ||
|
|
b1f8a7cb0a |
No files matched your search
@@ -2,4 +2,5 @@
|
|||||||
*.gch
|
*.gch
|
||||||
|
|
||||||
# IDE-specific
|
# IDE-specific
|
||||||
|
.idea
|
||||||
.vscode
|
.vscode
|
||||||
@@ -41,13 +41,21 @@ Comfortable, seamless, and fast 3-finger (and 4-finger) touchpad swipe gestures
|
|||||||
```
|
```
|
||||||
comfortable-swipe autostart
|
comfortable-swipe autostart
|
||||||
```
|
```
|
||||||
5. _Optional_: Change [configurations](#configurations) (see below). After making changes, run
|
5. Check the status of your application by running
|
||||||
```
|
```
|
||||||
comfortable-swipe restart
|
comfortable-swipe status
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configurations
|
## Configurations
|
||||||
Comfortable swipe makes use of keyboard shortcuts for configurations. The configuration file is located at `/usr/local/share/comfortable-swipe/comfortable-swipe.conf`. Make sure to run `comfortable-swipe restart` after making changes.
|
Comfortable swipe makes use of keyboard shortcuts for configurations. Edit by running
|
||||||
|
```
|
||||||
|
nano $(comfortable-swipe config)
|
||||||
|
```
|
||||||
|
|
||||||
|
Make sure to run after making changes:
|
||||||
|
```
|
||||||
|
comfortable-swipe restart
|
||||||
|
```
|
||||||
|
|
||||||
Property | Description | Default Value | Default Behavior
|
Property | Description | Default Value | Default Behavior
|
||||||
--------- | ----------- | -------------- | -----
|
--------- | ----------- | -------------- | -----
|
||||||
@@ -80,7 +88,13 @@ Refer to https://www.linux.org/threads/xdotool-keyboard.10528/ for a complete li
|
|||||||
|
|
||||||
## Debugging
|
## Debugging
|
||||||
|
|
||||||
You can check your touchpad driver by running `comfortable-swipe debug`. This is an alias of `libinput debug-events`. This logs all gestures you make on your touchpad, along with other input-based events that can be captured by libinput.
|
You can check your touchpad driver by running
|
||||||
|
|
||||||
|
```bash
|
||||||
|
comfortable-swipe debug
|
||||||
|
```
|
||||||
|
|
||||||
|
This is an alias of `libinput debug-events`. This logs all gestures you make on your touchpad, along with other input-based events that can be captured by libinput.
|
||||||
|
|
||||||
A working swipe gesture will show the following:
|
A working swipe gesture will show the following:
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,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 <ios> // std::ios
|
||||||
|
#include <iostream> // std::cin, std::cout, std::cerr
|
||||||
#include <string> // std::string
|
#include <string> // std::string
|
||||||
#include "lib/comfortable_swipe"
|
#include "lib/comfortable_swipe"
|
||||||
|
|
||||||
@@ -23,6 +25,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
int main(int argc, char** args)
|
int main(int argc, char** args)
|
||||||
{
|
{
|
||||||
|
// improve buffering by decoupling loggers from stdio
|
||||||
|
std::ios::sync_with_stdio(false);
|
||||||
|
std::cin.tie(0);
|
||||||
|
std::cout.tie(0);
|
||||||
|
std::cerr.tie(0);
|
||||||
|
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
{
|
{
|
||||||
std::string arg = args[1];
|
std::string arg = args[1];
|
||||||
@@ -43,9 +51,15 @@ int main(int argc, char** args)
|
|||||||
else if (arg == "autostart")
|
else if (arg == "autostart")
|
||||||
comfortable_swipe::service::autostart();
|
comfortable_swipe::service::autostart();
|
||||||
|
|
||||||
|
else if (arg == "config")
|
||||||
|
comfortable_swipe::service::config();
|
||||||
|
|
||||||
else if (arg == "debug")
|
else if (arg == "debug")
|
||||||
comfortable_swipe::service::debug();
|
comfortable_swipe::service::debug();
|
||||||
|
|
||||||
|
else if (arg == "status")
|
||||||
|
comfortable_swipe::service::status();
|
||||||
|
|
||||||
else /* if (arg == "help") */
|
else /* if (arg == "help") */
|
||||||
comfortable_swipe::service::help();
|
comfortable_swipe::service::help();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,14 +28,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
#include "gesture/xdo_gesture.cpp"
|
#include "gesture/xdo_gesture.cpp"
|
||||||
#include "gesture/swipe_gesture.cpp"
|
#include "gesture/swipe_gesture.cpp"
|
||||||
#include "gesture/swipe_gesture.regex.cpp"
|
#include "gesture/swipe_gesture.regex.cpp"
|
||||||
#include "gesture/pinch_gesture.cpp"
|
|
||||||
#include "gesture/pinch_gesture.regex.cpp"
|
|
||||||
#include "service/autostart.cpp"
|
#include "service/autostart.cpp"
|
||||||
#include "service/buffer.cpp"
|
#include "service/buffer.cpp"
|
||||||
|
#include "service/config.cpp"
|
||||||
#include "service/debug.cpp"
|
#include "service/debug.cpp"
|
||||||
#include "service/help.cpp"
|
#include "service/help.cpp"
|
||||||
#include "service/restart.cpp"
|
#include "service/restart.cpp"
|
||||||
#include "service/start.cpp"
|
#include "service/start.cpp"
|
||||||
|
#include "service/status.cpp"
|
||||||
#include "service/stop.cpp"
|
#include "service/stop.cpp"
|
||||||
#include "util/autostart_filename.cpp"
|
#include "util/autostart_filename.cpp"
|
||||||
#include "util/conf_filename.cpp"
|
#include "util/conf_filename.cpp"
|
||||||
|
|||||||
@@ -1,172 +0,0 @@
|
|||||||
/* 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__ */
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
/* 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__ */
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
/* 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__ */
|
|
||||||
@@ -20,9 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream> // std::cout, std::endl
|
#include <iostream> // std::cout, std::endl
|
||||||
#include <regex> // std::regex, std::regex_match
|
#include <string> // std::stoi, std::stof
|
||||||
#include <string> // std::stof, std::stoi
|
#include <regex> // std::regex, std::regex_match, std::cmatch
|
||||||
#include "xdo_gesture.h"
|
|
||||||
#include "swipe_gesture.h"
|
#include "swipe_gesture.h"
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
@@ -51,7 +50,8 @@ namespace comfortable_swipe::gesture
|
|||||||
):
|
):
|
||||||
comfortable_swipe::gesture::xdo_gesture(),
|
comfortable_swipe::gesture::xdo_gesture(),
|
||||||
threshold_squared(threshold*threshold),
|
threshold_squared(threshold*threshold),
|
||||||
commands(new const char*[8]{left3, left4, right3, right4, up3, up4, down3, down4})
|
commands(new const char*[8]{left3, left4, right3, right4, up3, up4, down3, down4}),
|
||||||
|
flag_swiping(false)
|
||||||
{
|
{
|
||||||
// improve responsiveness of first gesture by pre-empting xdotool runtime
|
// improve responsiveness of first gesture by pre-empting xdotool runtime
|
||||||
xdo_get_mouse_location(this->xdo, &this->ix, &this->iy, &this->screen_num);
|
xdo_get_mouse_location(this->xdo, &this->ix, &this->iy, &this->screen_num);
|
||||||
@@ -68,7 +68,7 @@ namespace comfortable_swipe::gesture
|
|||||||
/**
|
/**
|
||||||
* Hook on begin of swipe gesture.
|
* Hook on begin of swipe gesture.
|
||||||
*/
|
*/
|
||||||
inline void swipe_gesture::begin()
|
void swipe_gesture::begin()
|
||||||
{
|
{
|
||||||
xdo_get_mouse_location(this->xdo, &this->ix, &this->iy, &this->screen_num);
|
xdo_get_mouse_location(this->xdo, &this->ix, &this->iy, &this->screen_num);
|
||||||
this->previous_gesture = swipe_gesture::FRESH;
|
this->previous_gesture = swipe_gesture::FRESH;
|
||||||
@@ -79,7 +79,7 @@ namespace comfortable_swipe::gesture
|
|||||||
/**
|
/**
|
||||||
* Hook on update of swipe gesture.
|
* Hook on update of swipe gesture.
|
||||||
*/
|
*/
|
||||||
inline void swipe_gesture::update()
|
void swipe_gesture::update()
|
||||||
{
|
{
|
||||||
this->x += this->dx;
|
this->x += this->dx;
|
||||||
this->y += this->dy;
|
this->y += this->dy;
|
||||||
@@ -87,7 +87,9 @@ namespace comfortable_swipe::gesture
|
|||||||
float scale = this->previous_gesture == swipe_gesture::FRESH
|
float scale = this->previous_gesture == swipe_gesture::FRESH
|
||||||
? 1.00f
|
? 1.00f
|
||||||
: 0.01f; // square root of 1/10
|
: 0.01f; // square root of 1/10
|
||||||
if (this->x * this->x + this->y * this->y > this->threshold_squared * scale)
|
static const float EPSILON = 1e-6f;
|
||||||
|
if (this->x * this->x + this->y * this->y
|
||||||
|
> this->threshold_squared * scale + EPSILON)
|
||||||
{
|
{
|
||||||
int mask = 0;
|
int mask = 0;
|
||||||
if (this->fingers == 3) mask |= swipe_gesture::MSK_THREE_FINGERS;
|
if (this->fingers == 3) mask |= swipe_gesture::MSK_THREE_FINGERS;
|
||||||
@@ -116,10 +118,10 @@ namespace comfortable_swipe::gesture
|
|||||||
if (this->previous_gesture == swipe_gesture::FRESH
|
if (this->previous_gesture == swipe_gesture::FRESH
|
||||||
|| this->previous_gesture == (mask ^ swipe_gesture::MSK_POSITIVE))
|
|| this->previous_gesture == (mask ^ swipe_gesture::MSK_POSITIVE))
|
||||||
{
|
{
|
||||||
|
xdo_send_keysequence_window(this->xdo, CURRENTWINDOW, swipe_gesture::commands[mask], 0);
|
||||||
this->x = this->y = 0;
|
this->x = this->y = 0;
|
||||||
this->previous_gesture = mask;
|
this->previous_gesture = mask;
|
||||||
std::cout << "SWIPE " << swipe_gesture::command_map[mask] << std::endl;
|
std::cout << "SWIPE " << swipe_gesture::command_map[mask] << std::endl;
|
||||||
xdo_send_keysequence_window(xdo, CURRENTWINDOW, swipe_gesture::commands[mask], 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,25 +129,26 @@ namespace comfortable_swipe::gesture
|
|||||||
/**
|
/**
|
||||||
* Hook on end of swipe gesture.
|
* Hook on end of swipe gesture.
|
||||||
*/
|
*/
|
||||||
inline void swipe_gesture::end()
|
void swipe_gesture::end()
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses an output line and dispatches swipe begin/update/end upon match.
|
* Dispatches begin/update/end depending on the regex pattern provided by this class.
|
||||||
* 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
|
* @param line the line from libinput debug-events to parse
|
||||||
* @return true if line matches this gesture
|
* @return true if begin/update/end was dispatched
|
||||||
*/
|
*/
|
||||||
inline bool swipe_gesture::parse_line(const char * line)
|
bool swipe_gesture::parse_line(const char * line)
|
||||||
{
|
{
|
||||||
// pre-compile regex patterns
|
|
||||||
|
// 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_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_update(swipe_gesture::GESTURE_UPDATE_REGEX_PATTERN);
|
||||||
static const std::regex gesture_swipe_end(swipe_gesture::GESTURE_END_REGEX_PATTERN);
|
static const std::regex gesture_swipe_end(swipe_gesture::GESTURE_END_REGEX_PATTERN);
|
||||||
std::cmatch matches;
|
|
||||||
|
// prepare holder for regex matches
|
||||||
|
static std::cmatch matches;
|
||||||
|
|
||||||
if (this->flag_swiping)
|
if (this->flag_swiping)
|
||||||
{
|
{
|
||||||
// currently swiping
|
// currently swiping
|
||||||
@@ -171,7 +174,7 @@ namespace comfortable_swipe::gesture
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else /* if (!this->flag_swiping) */
|
else
|
||||||
{
|
{
|
||||||
// not swiping, check if swipe will begin
|
// not swiping, check if swipe will begin
|
||||||
if (std::regex_match(line, matches, gesture_swipe_begin) != 0)
|
if (std::regex_match(line, matches, gesture_swipe_begin) != 0)
|
||||||
@@ -197,14 +200,14 @@ namespace comfortable_swipe::gesture
|
|||||||
const int swipe_gesture::MSK_VERTICAL = 4;
|
const int swipe_gesture::MSK_VERTICAL = 4;
|
||||||
const int swipe_gesture::FRESH = -1;
|
const int swipe_gesture::FRESH = -1;
|
||||||
const char * const swipe_gesture::command_map[8] = {
|
const char * const swipe_gesture::command_map[8] = {
|
||||||
"left 3",
|
"left3",
|
||||||
"left 4",
|
"left4",
|
||||||
"right 3",
|
"right3",
|
||||||
"right 4",
|
"right4",
|
||||||
"up 3",
|
"up3",
|
||||||
"up 4",
|
"up4",
|
||||||
"down 3",
|
"down3",
|
||||||
"down 4"
|
"down4"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ namespace comfortable_swipe::gesture
|
|||||||
int fingers;
|
int fingers;
|
||||||
float dx, dy, udx, udy;
|
float dx, dy, udx, udy;
|
||||||
|
|
||||||
inline void begin() override;
|
void begin() override;
|
||||||
inline void update() override;
|
void update() override;
|
||||||
inline void end() override;
|
void end() override;
|
||||||
inline bool parse_line(const char *) override;
|
bool parse_line(const char *) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// location of mouse
|
// location of mouse
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ extern "C"
|
|||||||
#include <xdo.h> // xdo, xdo_new
|
#include <xdo.h> // xdo, xdo_new
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "xdo_gesture.h"
|
||||||
|
|
||||||
namespace comfortable_swipe::gesture
|
namespace comfortable_swipe::gesture
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -42,10 +42,10 @@ namespace comfortable_swipe
|
|||||||
~xdo_gesture();
|
~xdo_gesture();
|
||||||
|
|
||||||
// hooks
|
// hooks
|
||||||
virtual inline void begin() = 0;
|
virtual void begin() = 0;
|
||||||
virtual inline void update() = 0;
|
virtual void update() = 0;
|
||||||
virtual inline void end() = 0;
|
virtual void end() = 0;
|
||||||
virtual inline bool parse_line(const char *) = 0;
|
virtual bool parse_line(const char *) = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -36,7 +36,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
*/
|
*/
|
||||||
#include "gesture/xdo_gesture.h"
|
#include "gesture/xdo_gesture.h"
|
||||||
#include "gesture/swipe_gesture.h"
|
#include "gesture/swipe_gesture.h"
|
||||||
#include "gesture/pinch_gesture.h"
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
namespace comfortable_swipe
|
namespace comfortable_swipe
|
||||||
@@ -51,10 +50,12 @@ extern "C"
|
|||||||
{
|
{
|
||||||
void autostart();
|
void autostart();
|
||||||
void buffer();
|
void buffer();
|
||||||
|
void config();
|
||||||
void debug();
|
void debug();
|
||||||
void help();
|
void help();
|
||||||
void restart();
|
void restart();
|
||||||
void start();
|
void start();
|
||||||
|
void status();
|
||||||
void stop();
|
void stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-12
@@ -19,7 +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 <cstdio> // std::fgets_unlocked, stdin
|
#include <cstdio> // fgets_unlocked, stdin
|
||||||
|
#include <iostream> // std::ios, std::cout, std::cin
|
||||||
#include "../index.hpp"
|
#include "../index.hpp"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,6 +30,11 @@ namespace comfortable_swipe::service
|
|||||||
{
|
{
|
||||||
void buffer()
|
void buffer()
|
||||||
{
|
{
|
||||||
|
std::ios::sync_with_stdio(false);
|
||||||
|
std::cin.tie(0);
|
||||||
|
std::cout.tie(0);
|
||||||
|
std::cout.flush();
|
||||||
|
|
||||||
// read config file
|
// read config file
|
||||||
auto config = comfortable_swipe::util::read_config_file(comfortable_swipe::util::conf_filename());
|
auto config = comfortable_swipe::util::read_config_file(comfortable_swipe::util::conf_filename());
|
||||||
|
|
||||||
@@ -46,23 +52,14 @@ namespace comfortable_swipe::service
|
|||||||
config["down4"].c_str()
|
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
|
// prepare data containers
|
||||||
std::array<char, 256> line;
|
std::array<char, 256> line;
|
||||||
|
|
||||||
// start reading lines from input one by one
|
// start reading lines from input one by one
|
||||||
while (fgets_unlocked(line.data(), line.size(), stdin) != NULL)
|
while (fgets_unlocked(line.data(), line.size(), stdin) != NULL)
|
||||||
{
|
{
|
||||||
swipe_gesture_handler.parse_line(line.data()) ||
|
// attempt to parse swipe gestures
|
||||||
pinch_gesture_handler.parse_line(line.data());
|
swipe_gesture_handler.parse_line(line.data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#ifndef __COMFORTABLE_SWIPE__service_config__
|
||||||
|
#define __COMFORTABLE_SWIPE__service_config__
|
||||||
|
|
||||||
|
/*
|
||||||
|
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 "../index.hpp"
|
||||||
|
#include <cstdio> // std::puts
|
||||||
|
|
||||||
|
namespace comfortable_swipe::service
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Prints where the config file of comfortable swipe is located.
|
||||||
|
*
|
||||||
|
* Usage: nano $(comfortable-swipe config)
|
||||||
|
*/
|
||||||
|
void config()
|
||||||
|
{
|
||||||
|
std::puts(comfortable_swipe::util::conf_filename());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* __COMFORTABLE_SWIPE__service_config__ */
|
||||||
@@ -28,7 +28,7 @@ namespace comfortable_swipe::service
|
|||||||
*/
|
*/
|
||||||
void debug()
|
void debug()
|
||||||
{
|
{
|
||||||
(void) std::system("stdbuf -oL -e0 libinput debug-events");
|
(void) std::system("bash -c \"stdbuf -oL -e0 libinput debug-events 2> >(grep -v 'double tracking')\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ namespace comfortable_swipe::service
|
|||||||
void help()
|
void help()
|
||||||
{
|
{
|
||||||
using comfortable_swipe::util::conf_filename;
|
using comfortable_swipe::util::conf_filename;
|
||||||
std::puts("comfortable-swipe [start|stop|restart|autostart|buffer|help|debug]");
|
std::puts("comfortable-swipe [start|stop|restart|autostart|buffer|help|config|debug|status]");
|
||||||
std::puts("");
|
std::puts("");
|
||||||
std::puts("start - starts 3/4-finger gesture service");
|
std::puts("start - starts 3/4-finger gesture service");
|
||||||
std::puts("stop - stops 3/4-finger gesture service");
|
std::puts("stop - stops 3/4-finger gesture service");
|
||||||
@@ -38,7 +38,9 @@ namespace comfortable_swipe::service
|
|||||||
std::puts("autostart - automatically run on startup (toggleable)");
|
std::puts("autostart - automatically run on startup (toggleable)");
|
||||||
std::puts("buffer - parses output of libinput debug-events");
|
std::puts("buffer - parses output of libinput debug-events");
|
||||||
std::puts("help - shows the help dialog");
|
std::puts("help - shows the help dialog");
|
||||||
|
std::puts("config - locates the config file [/usr/share/comfortable-swipe/comfortable-swipe.conf]");
|
||||||
std::puts("debug - logs raw output from input events taken from libinput");
|
std::puts("debug - logs raw output from input events taken from libinput");
|
||||||
|
std::puts("status - checks status of program and autostart");
|
||||||
std::puts("");
|
std::puts("");
|
||||||
std::printf("Configuration file can be found in %s\n", conf_filename());
|
std::printf("Configuration file can be found in %s\n", conf_filename());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ namespace comfortable_swipe::service
|
|||||||
*/
|
*/
|
||||||
void start()
|
void start()
|
||||||
{
|
{
|
||||||
(void) std::system("stdbuf -oL -e0 libinput debug-events | " __COMFORTABLE_SWIPE__PROGRAM__ " buffer");
|
(void) std::system(__COMFORTABLE_SWIPE__PROGRAM__ " debug | " __COMFORTABLE_SWIPE__PROGRAM__ " buffer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
#ifndef __COMFORTABLE_SWIPE__service_status__
|
||||||
|
#define __COMFORTABLE_SWIPE__service_status__
|
||||||
|
|
||||||
|
/*
|
||||||
|
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 "../index.hpp"
|
||||||
|
#include <stdexcept> // std::runtime_error
|
||||||
|
#include <unistd.h> // popen, pclose, getpid, access, F_OK
|
||||||
|
#include <memory> // std::unique_ptr
|
||||||
|
#include <array> // std::array
|
||||||
|
#include <cstdlib> // std::atoi
|
||||||
|
#include <cstdio> // FILE, std::feof, std::fgets, std::printf
|
||||||
|
#include <regex> // std::cmatch, std::regex, std::regex_match
|
||||||
|
|
||||||
|
namespace comfortable_swipe::service
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Prints the status of comfortable-swipe.
|
||||||
|
*/
|
||||||
|
void status()
|
||||||
|
{
|
||||||
|
// check if comfortable-swipe is running
|
||||||
|
bool running = false;
|
||||||
|
std::array<char, 128> buffer;
|
||||||
|
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("pgrep -f comfortable-swipe", "r"), pclose);
|
||||||
|
if (pipe && !std::feof(pipe.get()) && std::fgets(buffer.data(), buffer.size(), pipe.get()) != NULL)
|
||||||
|
{
|
||||||
|
int pid = std::atoi(buffer.data());
|
||||||
|
if (pid != getpid())
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if autostart is on
|
||||||
|
auto autostart_f = comfortable_swipe::util::autostart_filename();
|
||||||
|
bool autostart_on = access(autostart_f, F_OK) != -1;
|
||||||
|
|
||||||
|
// print status
|
||||||
|
std::printf("program is %s\n", running ? "ON" : "OFF");
|
||||||
|
std::printf("autostart is %s\n", autostart_on ? "ON" : "OFF");
|
||||||
|
std::printf("config file at %s\n", comfortable_swipe::util::conf_filename());
|
||||||
|
|
||||||
|
// check status of configuration file
|
||||||
|
try
|
||||||
|
{
|
||||||
|
auto config = comfortable_swipe::util::read_config_file(comfortable_swipe::util::conf_filename());
|
||||||
|
|
||||||
|
// print keys and values of config file
|
||||||
|
std::printf("\nConfigurations:\n");
|
||||||
|
|
||||||
|
// print threshold
|
||||||
|
if (config.count("threshold") > 0)
|
||||||
|
{
|
||||||
|
auto & threshold = config["threshold"];
|
||||||
|
|
||||||
|
// check if regex pattern matches threshold
|
||||||
|
std::cmatch matches;
|
||||||
|
bool ok = (std::regex_match(threshold.data(), matches, std::regex("^\\d+(?:\\.\\d+)??$")) != 0);
|
||||||
|
|
||||||
|
// print status of threshold
|
||||||
|
std::printf(" %9s is %s (%s)\n", "threshold", ok ? "OK" : "INVALID", threshold.data());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::printf(" %9s is OFF\n", "threshold");
|
||||||
|
|
||||||
|
// print swipe commands
|
||||||
|
for (auto &command : comfortable_swipe::gesture::swipe_gesture::command_map)
|
||||||
|
{
|
||||||
|
if (config.count(command) > 0)
|
||||||
|
std::printf(" %9s is OK (%s)\n", command, config[command].data());
|
||||||
|
else
|
||||||
|
std::printf(" %9s is OFF\n", command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error& e)
|
||||||
|
{
|
||||||
|
std::printf("config error: %s\n", e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* __COMFORTABLE_SWIPE__service_restart__ */
|
||||||
+11
-14
@@ -24,6 +24,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
#include <stdexcept> // std::runtime_error
|
#include <stdexcept> // std::runtime_error
|
||||||
#include <unistd.h> // popen, pclose, getpid
|
#include <unistd.h> // popen, pclose, getpid
|
||||||
|
#include <memory> // std::unique_ptr
|
||||||
|
#include <array> // std::array
|
||||||
|
#include <cstdlib> // std::atoi
|
||||||
|
#include <cstdio> // FILE, std::feof, std::fgets
|
||||||
|
|
||||||
namespace comfortable_swipe::service
|
namespace comfortable_swipe::service
|
||||||
{
|
{
|
||||||
@@ -34,24 +38,22 @@ namespace comfortable_swipe::service
|
|||||||
{
|
{
|
||||||
|
|
||||||
// read all service names from process (pgrep)
|
// read all service names from process (pgrep)
|
||||||
char* buffer = new char[20];
|
std::array<char, 128> buffer;
|
||||||
FILE* pipe = popen("pgrep -f comfortable-swipe", "r");
|
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("pgrep -f comfortable-swipe", "r"), pclose);
|
||||||
|
|
||||||
// make sure pipe exists
|
// make sure pipe exists
|
||||||
if (pipe == NULL)
|
if (!pipe)
|
||||||
{
|
|
||||||
throw std::runtime_error("stop command failed");
|
throw std::runtime_error("stop command failed");
|
||||||
}
|
|
||||||
|
|
||||||
// buffer what to kill
|
// buffer what to kill
|
||||||
std::string kill = "kill";
|
std::string kill = "kill";
|
||||||
|
|
||||||
// read until end of line
|
// read until end of line
|
||||||
while (!std::feof(pipe))
|
while (!std::feof(pipe.get()))
|
||||||
{
|
{
|
||||||
if (std::fgets(buffer, 20, pipe) != NULL)
|
if (std::fgets(buffer.data(), buffer.size(), pipe.get()) != NULL)
|
||||||
{
|
{
|
||||||
int pid = std::atoi(buffer);
|
int pid = std::atoi(buffer.data());
|
||||||
if (pid != getpid())
|
if (pid != getpid())
|
||||||
{
|
{
|
||||||
kill += " ";
|
kill += " ";
|
||||||
@@ -62,11 +64,6 @@ namespace comfortable_swipe::service
|
|||||||
|
|
||||||
// run "kill {pid1} {pid2}..."
|
// run "kill {pid1} {pid2}..."
|
||||||
(void) std::system(kill.data());
|
(void) std::system(kill.data());
|
||||||
delete[] buffer;
|
|
||||||
|
|
||||||
// close the pipe
|
|
||||||
pclose(pipe);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,32 +22,33 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
#include <map> // std::map
|
#include <map> // std::map
|
||||||
#include <string> // std::string
|
#include <string> // std::string
|
||||||
#include <fstream> // std::ifstream
|
#include <fstream> // std::ifstream
|
||||||
#include <iostream> // std::cerr, std::endl, std::getline
|
#include <sstream> // std::ostringstream
|
||||||
|
#include <iostream> // std::endl, std::getline
|
||||||
#include <cstdlib> // exit
|
#include <cstdlib> // exit
|
||||||
#include <cctype> // std::isspace
|
#include <cctype> // std::isspace
|
||||||
|
#include <stdexcept> // std::runtime_error
|
||||||
|
|
||||||
namespace comfortable_swipe::util
|
namespace comfortable_swipe::util
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* A utility method for reading the config file.
|
* A utility method for reading the config file.
|
||||||
*
|
*
|
||||||
* @param filename (const char*) the path of the config file.
|
* @param filename (const char*) the path of the config file.
|
||||||
*/
|
*/
|
||||||
std::map<std::string, std::string> read_config_file(const char* filename)
|
std::map<std::string, std::string> read_config_file(const char* filename)
|
||||||
{
|
{
|
||||||
|
|
||||||
std::map<std::string, std::string> conf;
|
std::map<std::string, std::string> conf;
|
||||||
std::ifstream fin(filename);
|
std::ifstream fin(filename);
|
||||||
|
|
||||||
if (!fin.is_open())
|
if (!fin.is_open())
|
||||||
{
|
{
|
||||||
std::cerr << "file \"" << filename << "\" does not exist!" << std::endl;
|
throw std::runtime_error("config file does not exist");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string line, token[2];
|
static std::string line, token[2];
|
||||||
int line_number = 0;
|
int line_number = 0;
|
||||||
|
|
||||||
while (std::getline(fin, line))
|
while (std::getline(fin, line))
|
||||||
{
|
{
|
||||||
++line_number;
|
++line_number;
|
||||||
@@ -55,7 +56,7 @@ namespace comfortable_swipe::util
|
|||||||
token[1].clear();
|
token[1].clear();
|
||||||
int length = line.length();
|
int length = line.length();
|
||||||
int equal_flag = 0;
|
int equal_flag = 0;
|
||||||
|
|
||||||
// tokenize comfig config
|
// tokenize comfig config
|
||||||
for (int i = 0; i < length; ++i)
|
for (int i = 0; i < length; ++i)
|
||||||
{
|
{
|
||||||
@@ -65,9 +66,10 @@ namespace comfortable_swipe::util
|
|||||||
{
|
{
|
||||||
if (++equal_flag > 1)
|
if (++equal_flag > 1)
|
||||||
{
|
{
|
||||||
std::cerr << "error in conf file " << filename << std::endl;
|
std::ostringstream stream;
|
||||||
std::cerr << "multiple equal signs in line " << line_number << std::endl;
|
stream << "error in conf file " << filename << std::endl;
|
||||||
exit(1);
|
stream << "multiple equal signs in line " << line_number << std::endl;
|
||||||
|
throw std::runtime_error(stream.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!std::isspace(line[i]))
|
else if (!std::isspace(line[i]))
|
||||||
@@ -84,16 +86,17 @@ namespace comfortable_swipe::util
|
|||||||
// no equal sign found in non-empty line
|
// no equal sign found in non-empty line
|
||||||
if (equal_flag == 0)
|
if (equal_flag == 0)
|
||||||
{
|
{
|
||||||
std::cerr << "error in conf file: " << filename << std::endl;
|
std::ostringstream stream;
|
||||||
std::cerr << "equal sign expected in line " << line_number << std::endl;
|
stream << "error in conf file: " << filename << std::endl;
|
||||||
exit(1);
|
stream << "equal sign expected in line " << line_number << std::endl;
|
||||||
|
throw std::runtime_error(stream.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// equal sign found, add to tokens
|
// equal sign found, add to tokens
|
||||||
if (token[1].length() > 0)
|
if (token[1].length() > 0)
|
||||||
conf[token[0]] = token[1];
|
conf[token[0]] = token[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return conf;
|
return conf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -3,4 +3,4 @@
|
|||||||
DIR=$(dirname $0)
|
DIR=$(dirname $0)
|
||||||
g++ -std=c++11 -O2 $DIR/test_regex.cpp -lxdo -o test.out || exec "Test aborted"
|
g++ -std=c++11 -O2 $DIR/test_regex.cpp -lxdo -o test.out || exec "Test aborted"
|
||||||
./test.out || rm test.out
|
./test.out || rm test.out
|
||||||
rm test.out
|
rm test.out
|
||||||
Reference in new issue
Block a user