Compare commits

..
20 Commits
Author SHA1 Message Date
Rico Tiongson c6cdc98257 Attempt fix travis build 2019-03-10 18:49:13 +08:00
Rico Tiongson 6f8212c4ed Attempt fix travis errors 2019-03-10 18:45:16 +08:00
Rico Tiongson 37b8538452 Attempt fix 2019-03-10 18:41:07 +08:00
Rico Tiongson 528d8513ea Merge branch 'travis-cpp' of https://github.com/Hikari9/comfortable-swipe into travis-cpp 2019-03-10 18:38:44 +08:00
Rico Tiongson c04e1499c7 Merge branch 'travis-cpp' of https://github.com/Hikari9/comfortable-swipe into travis-cpp 2019-03-10 18:38:21 +08:00
Rico Tiongson 535e1faf70 Merge branch 'travis-cpp' of https://github.com/Hikari9/comfortable-swipe into travis-cpp 2019-03-10 18:35:27 +08:00
Rico Tiongson f432d36fa9 Attempt fix c++ version 2019-03-10 18:34:53 +08:00
Rico Tiongson f2329e8b7a Attempt fix c++ version 2019-03-10 18:24:54 +08:00
Rico Tiongson 115ec68ca9 Add travis config for c++ branch 2019-03-10 16:50:22 +08:00
Rico Tiongson 28f5ca2a91 Optimize swipe function 2019-03-03 22:00:48 +08:00
Rico Tiongson 89b79bc9bc Status command (#56)
* Add configurations to status

* Update comment of status

* Add config command

* Modify README

* Add EPSILON instead of subtract for a more reliable >= threshold
2019-02-15 14:17:47 +08:00
Rico Tiongson 7d76db6693 Update status command and add config command (#55)
* Add configurations to status

* Update comment of status

* Add config command

* Modify README
2019-02-13 10:15:42 +08:00
Rico Tiongson 204a422f2d Add status service command (#54)
* Use std::unique_ptr for stop service

* Use std::unique_ptr for stop service

* Add status command

* Prefer to use std::array to avoid memory leaking in buffer

* Perform xdo key sequence before output message

* Fix missing swipe_gesture header in regex implementation
2019-02-13 09:27:41 +08:00
Rico Tiongson c18cb04230 Improve buffering performance 2019-02-12 22:29:13 +08:00
Rico Tiongson b1f8a7cb0a Remove irrelevant double tracking bug in error log (#53)
* Do not show double tracking

* Do not show double tracking in debug and start
2019-02-12 22:23:20 +08:00
Rico Tiongson 1c9b497b56 Add default -y flag to install 2019-02-12 21:13:40 +08:00
Rico Tiongson 16742495eb Add debug command (#51)
* Add debug command

* Update README to include debugging
2019-02-12 20:18:15 +08:00
Rico Tiongson 459fd68ba8 Add parse_line method to decouple buffer from gesture handler (#50) 2019-02-12 19:28:53 +08:00
Rico Tiongson 7923effa02 Simplify compile command structure 2019-02-12 19:07:30 +08:00
Rico Tiongson 602b148fb4 Move library to root 2019-02-12 19:04:31 +08:00
29 changed files with 501 additions and 176 deletions

No files matched your search

+4
View File
@@ -1,2 +1,6 @@
# C++ generated headers
*.gch
# IDE-specific
.idea
.vscode
+31
View File
@@ -0,0 +1,31 @@
dist: xenial
sudo: required
language: c++
matrix:
include:
- env: {MATRIX_EVAL: '"CC=gcc-7 && CXX=g++-7 && alias g++=g++-7"'}
addons:
apt:
update: true
sources:
- ubuntu-toolchain-r-test
packages:
- libxdo-dev
- libinput-tools
- g++-7
- env: {MATRIX_EVAL: '"CC=gcc-8 && CXX=g++-8 && alias g++=g+++-8"'}
addons:
apt:
update: true
sources:
- ubuntu-toolchain-r-test
packages:
- libxdo-dev
- libinput-tools
- g++-8
before_install:
- eval "${MATRIX_EVAL}"
- g++ --version
install: bash install
script: bash tests/run_tests
after_script: bash uninstall
+50 -7
View File
@@ -1,18 +1,18 @@
# Comfortable Swipe (Ubuntu)
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
Comfortable, seamless, and fast 3-finger (and 4-finger) touchpad swipe gestures for Ubuntu 14.04 LTS and beyond. May work for other Linux distros that support `libinput`.
## Installation
1. Install git, libinput, and g++
```bash
sudo apt-get install git libinput-tools libxdo-dev g++
```
2. Clone this repository
```bash
git clone https://github.com/Hikari9/comfortable-swipe-ubuntu.git
cd comfortable-swipe-ubuntu
@@ -41,13 +41,21 @@ Comfortable, seamless, and fast 3-finger (and 4-finger) touchpad swipe gestures
```
comfortable-swipe autostart
```
5. _Optional_: Change keyboard [configurations](#configurations). After making changes, run
5. Check the status of your application by running
```
comfortable-swipe restart
comfortable-swipe status
```
## 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
--------- | ----------- | -------------- | -----
@@ -78,9 +86,44 @@ Taken from `man xdotool`:
Refer to https://www.linux.org/threads/xdotool-keyboard.10528/ for a complete list of keycodes you can use.
## Debugging
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:
```bash
$ comfortable-swipe debug
...
-event9 DEVICE_ADDED TouchPad seat0 default group7 cap:pg size 70x50mm tap(dl off) left scroll-nat scroll-2fg-edge click-buttonareas-clickfinger dwt-on
...
event9 GESTURE_SWIPE_BEGIN +2.03s 3
event9 GESTURE_SWIPE_UPDATE +2.03s 3 -9.95/ 2.64 (-26.90/ 7.12 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.03s 3 -10.44/ 3.19 (-28.22/ 8.62 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.04s 3 -9.71/ 2.64 (-26.25/ 7.12 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.05s 3 -8.98/ 2.64 (-24.28/ 7.12 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.06s 3 -7.40/ 2.36 (-20.01/ 6.37 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.06s 3 -6.31/ 2.50 (-17.06/ 6.75 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.07s 3 -5.34/ 1.80 (-14.44/ 4.87 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.08s 3 -4.61/ 2.08 (-12.47/ 5.62 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.09s 3 -4.49/ 1.53 (-12.14/ 4.12 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.09s 3 -4.01/ 1.25 (-10.83/ 3.37 unaccelerated)
event9 GESTURE_SWIPE_UPDATE +2.10s 3 -4.13/ 0.42 (-11.15/ 1.12 unaccelerated)
event9 GESTURE_SWIPE_END +2.11s 3
...
```
If you can see `GESTURE_SWIPE_XXX` in your output, that means your touchpad supports multi-touch swipe gestures.
## Uninstall
Download the `uninstall` script then run `bash uninstall`
Download the `uninstall` script then run `bash uninstall`
## Bug Reports
Create an issue [here](https://github.com/Hikari9/comfortable-swipe-ubuntu/issues/new) to report a bug.
+2
View File
@@ -0,0 +1,2 @@
#!/bin/sh
g++ $(dirname $0)/comfortable-swipe.cpp -std=c++11 -O2 -lxdo -Wno-unused-result -o $1
+19 -4
View File
@@ -16,8 +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/>.
*/
// Compile: g++ main.cpp -std=c++11 -lxdo
#include <ios> // std::ios
#include <iostream> // std::cin, std::cout, std::cerr
#include <string> // std::string
#include "lib/comfortable_swipe"
@@ -25,10 +25,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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)
{
std::string arg = args[1];
// select based on argument
if (arg == "start")
comfortable_swipe::service::start();
@@ -45,6 +51,15 @@ int main(int argc, char** args)
else if (arg == "autostart")
comfortable_swipe::service::autostart();
else if (arg == "config")
comfortable_swipe::service::config();
else if (arg == "debug")
comfortable_swipe::service::debug();
else if (arg == "status")
comfortable_swipe::service::status();
else /* if (arg == "help") */
comfortable_swipe::service::help();
}
@@ -52,4 +67,4 @@ int main(int argc, char** args)
comfortable_swipe::service::help();
return 0;
}
}
View File
File renamed without changes.
+28 -11
View File
@@ -2,8 +2,9 @@
# prefer running as root
DIR=$(dirname $0)
PROGRAM=/usr/local/bin/comfortable-swipe
COMPILE=$DIR/comfortable-swipe.compile.sh
CONF_PATH=/usr/local/share/comfortable-swipe/comfortable-swipe.conf
DCONF_PATH=$DIR/src/defaults.conf
DCONF_PATH=$DIR/defaults.conf
OLD_CONF_PATH=${XDG_CONFIG_HOME:-$HOME/.config}/comfortable-swipe.conf
if [ -x "$(command -v $PROGRAM)" ]; then
@@ -16,6 +17,20 @@ abort () {
exec echo "Installation aborted"
}
sudo mkdir -p $(dirname $CONF_PATH) || abort
# check if "-y" or "--yes" is passed as an argument
YES=false
while test $# -gt 0
do
case "$1" in
-y) YES=true
;;
--yes) YES=true
;;
esac
shift
done
sudo chown $USER $(dirname $CONF_PATH)
if [ ! -f $CONF_PATH ]; then
if [ ! -f $OLD_CONF_PATH ]; then
@@ -29,15 +44,17 @@ if [ ! -f $CONF_PATH ]; then
else
# config file found, ask user if overwrite
echo "Old conf file found in $CONF_PATH"
read -r -p "Keep the old conf file? (default: yes) [Y/n] " response
response=${response,,} # tolower
if [[ "$response" =~ ^(no|n)$ ]]; then
read -r -p "Conf file will be overwritten. Are you sure? [Y/n] " response
response=${response,,}
if [[ "$response" =~ ^(yes|y)$ ]]; then
cat $DCONF_PATH > $CONF_PATH || abort
else
abort
if [ $YES == false ]; then
read -r -p "Keep the old conf file? (default: yes) [Y/n] " response
response=${response,,} # tolower
if [[ "$response" =~ ^(no|n)$ ]]; then
read -r -p "Conf file will be overwritten. Are you sure? [Y/n] " response
response=${response,,}
if [[ "$response" =~ ^(yes|y)$ ]]; then
cat $DCONF_PATH > $CONF_PATH || abort
else
abort
fi
fi
fi
fi
@@ -50,7 +67,7 @@ if [ -x "$(command -v $PROGRAM)" ]; then
fi
# compile library
sudo $DIR/src/compile $PROGRAM || abort
sudo $COMPILE $PROGRAM || abort
# add permissions to input group (defer)
# GROUP=$(ls -l /dev/input/event* | awk '{print $4}' | head --line=1) || abort
@@ -30,9 +30,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "gesture/swipe_gesture.regex.cpp"
#include "service/autostart.cpp"
#include "service/buffer.cpp"
#include "service/config.cpp"
#include "service/debug.cpp"
#include "service/help.cpp"
#include "service/restart.cpp"
#include "service/start.cpp"
#include "service/status.cpp"
#include "service/stop.cpp"
#include "util/autostart_filename.cpp"
#include "util/conf_filename.cpp"
@@ -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"
{
@@ -48,7 +50,8 @@ namespace comfortable_swipe::gesture
):
comfortable_swipe::gesture::xdo_gesture(),
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
xdo_get_mouse_location(this->xdo, &this->ix, &this->iy, &this->screen_num);
@@ -84,7 +87,9 @@ namespace comfortable_swipe::gesture
float scale = this->previous_gesture == swipe_gesture::FRESH
? 1.00f
: 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;
if (this->fingers == 3) mask |= swipe_gesture::MSK_THREE_FINGERS;
@@ -113,10 +118,10 @@ namespace comfortable_swipe::gesture
if (this->previous_gesture == swipe_gesture::FRESH
|| 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->previous_gesture = mask;
std::cout << "SWIPE " << swipe_gesture::command_map[mask] << std::endl;
xdo_send_keysequence_window(xdo, CURRENTWINDOW, swipe_gesture::commands[mask], 0);
}
}
}
@@ -127,6 +132,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;
@@ -136,14 +200,14 @@ namespace comfortable_swipe::gesture
const int swipe_gesture::MSK_VERTICAL = 4;
const int swipe_gesture::FRESH = -1;
const char * const swipe_gesture::command_map[8] = {
"left 3",
"left 4",
"right 3",
"right 4",
"up 3",
"up 4",
"down 3",
"down 4"
"left3",
"left4",
"right3",
"right4",
"up3",
"up4",
"down3",
"down4"
};
}
@@ -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;
@@ -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
@@ -24,6 +24,8 @@ extern "C"
#include <xdo.h> // xdo, xdo_new
}
#include "xdo_gesture.h"
namespace comfortable_swipe::gesture
{
/**
@@ -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;
};
}
}
+3
View File
@@ -50,9 +50,12 @@ extern "C"
{
void autostart();
void buffer();
void config();
void debug();
void help();
void restart();
void start();
void status();
void stop();
}
}
File renamed without changes.
+67
View File
@@ -0,0 +1,67 @@
#ifndef __COMFORTABLE_SWIPE__service_buffer__
#define __COMFORTABLE_SWIPE__service_buffer__
/*
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 <cstdio> // fgets_unlocked, stdin
#include <iostream> // std::ios, std::cout, std::cin
#include "../index.hpp"
/**
* Starts the comfortable-swipe service by buffering libinput debug-events.
*/
namespace comfortable_swipe::service
{
void buffer()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cout.flush();
// read config file
auto config = comfortable_swipe::util::read_config_file(comfortable_swipe::util::conf_filename());
// initialize swipe gesture handler
comfortable_swipe::gesture::swipe_gesture swipe_gesture_handler
(
config.count("threshold") ? std::stof(config["threshold"]) : 0.0,
config["left3"].c_str(),
config["left4"].c_str(),
config["right3"].c_str(),
config["right4"].c_str(),
config["up3"].c_str(),
config["up4"].c_str(),
config["down3"].c_str(),
config["down4"].c_str()
);
// prepare data containers
std::array<char, 256> line;
// start reading lines from input one by one
while (fgets_unlocked(line.data(), line.size(), stdin) != NULL)
{
// attempt to parse swipe gestures
swipe_gesture_handler.parse_line(line.data());
}
}
}
#endif /* __COMFORTABLE_SWIPE__service_buffer__ */
+38
View File
@@ -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__ */
+35
View File
@@ -0,0 +1,35 @@
#ifndef __COMFORTABLE_SWIPE__service_debug__
#define __COMFORTABLE_SWIPE__service_debug__
/*
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 <cstdlib> // std::system
namespace comfortable_swipe::service
{
/**
* Debugs output from `libinput debug-events`.
*/
void debug()
{
(void) std::system("bash -c \"stdbuf -oL -e0 libinput debug-events 2> >(grep -v 'double tracking')\"");
}
}
#endif /* __COMFORTABLE_SWIPE__service_debug__ */
@@ -30,7 +30,7 @@ namespace comfortable_swipe::service
void help()
{
using comfortable_swipe::util::conf_filename;
std::puts("comfortable-swipe [start|stop|restart|autostart|buffer|help]");
std::puts("comfortable-swipe [start|stop|restart|autostart|buffer|help|config|debug|status]");
std::puts("");
std::puts("start - starts 3/4-finger gesture service");
std::puts("stop - stops 3/4-finger gesture service");
@@ -38,6 +38,9 @@ namespace comfortable_swipe::service
std::puts("autostart - automatically run on startup (toggleable)");
std::puts("buffer - parses output of libinput debug-events");
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("status - checks status of program and autostart");
std::puts("");
std::printf("Configuration file can be found in %s\n", conf_filename());
}
File renamed without changes.
@@ -31,7 +31,7 @@ namespace comfortable_swipe::service
*/
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");
}
}
+97
View File
@@ -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__ */
@@ -24,6 +24,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdexcept> // std::runtime_error
#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
{
@@ -34,24 +38,22 @@ namespace comfortable_swipe::service
{
// read all service names from process (pgrep)
char* buffer = new char[20];
FILE* pipe = popen("pgrep -f comfortable-swipe", "r");
std::array<char, 128> buffer;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("pgrep -f comfortable-swipe", "r"), pclose);
// make sure pipe exists
if (pipe == NULL)
{
if (!pipe)
throw std::runtime_error("stop command failed");
}
// buffer what to kill
std::string kill = "kill";
// 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())
{
kill += " ";
@@ -62,11 +64,6 @@ namespace comfortable_swipe::service
// run "kill {pid1} {pid2}..."
(void) std::system(kill.data());
delete[] buffer;
// close the pipe
pclose(pipe);
}
}
File renamed without changes.
File renamed without changes.
@@ -22,32 +22,33 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <map> // std::map
#include <string> // std::string
#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 <cctype> // std::isspace
#include <stdexcept> // std::runtime_error
namespace comfortable_swipe::util
{
/**
* A utility method for reading 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> conf;
std::ifstream fin(filename);
if (!fin.is_open())
{
std::cerr << "file \"" << filename << "\" does not exist!" << std::endl;
exit(1);
throw std::runtime_error("config file does not exist");
}
static std::string line, token[2];
int line_number = 0;
while (std::getline(fin, line))
{
++line_number;
@@ -55,7 +56,7 @@ namespace comfortable_swipe::util
token[1].clear();
int length = line.length();
int equal_flag = 0;
// tokenize comfig config
for (int i = 0; i < length; ++i)
{
@@ -65,9 +66,10 @@ namespace comfortable_swipe::util
{
if (++equal_flag > 1)
{
std::cerr << "error in conf file " << filename << std::endl;
std::cerr << "multiple equal signs in line " << line_number << std::endl;
exit(1);
std::ostringstream stream;
stream << "error in conf file " << filename << std::endl;
stream << "multiple equal signs in line " << line_number << std::endl;
throw std::runtime_error(stream.str());
}
}
else if (!std::isspace(line[i]))
@@ -84,16 +86,17 @@ namespace comfortable_swipe::util
// no equal sign found in non-empty line
if (equal_flag == 0)
{
std::cerr << "error in conf file: " << filename << std::endl;
std::cerr << "equal sign expected in line " << line_number << std::endl;
exit(1);
std::ostringstream stream;
stream << "error in conf file: " << filename << std::endl;
stream << "equal sign expected in line " << line_number << std::endl;
throw std::runtime_error(stream.str());
}
// equal sign found, add to tokens
if (token[1].length() > 0)
conf[token[0]] = token[1];
}
return conf;
}
}
-2
View File
@@ -1,2 +0,0 @@
#!/bin/sh
g++ $(dirname $0)/main.cpp -std=c++11 -O2 -lxdo -Wno-unused-result -o $1
-104
View File
@@ -1,104 +0,0 @@
#ifndef __COMFORTABLE_SWIPE__service_buffer__
#define __COMFORTABLE_SWIPE__service_buffer__
/*
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 <string> // std::stoi, std::stof
#include <cstdio> // std::fgets_unlocked, stdin
#include <regex> // std::regex, std::regex_match, std::cmatch
#include "../index.hpp"
/**
* Starts the comfortable-swipe service by buffering libinput debug-events.
*/
namespace comfortable_swipe::service
{
void buffer()
{
// 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
(
config.count("threshold") ? std::stof(config["threshold"]) : 0.0,
config["left3"].c_str(),
config["left4"].c_str(),
config["right3"].c_str(),
config["right4"].c_str(),
config["up3"].c_str(),
config["up4"].c_str(),
config["down3"].c_str(),
config["down4"].c_str()
);
// 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();
}
}
}
}
}
#endif /* __COMFORTABLE_SWIPE__service_buffer__ */
+2 -2
View File
@@ -2,8 +2,8 @@
#include <cassert>
#include <regex>
#include <string>
#include "../src/lib/gesture/swipe_gesture.h"
#include "../src/lib/gesture/swipe_gesture.regex.cpp"
#include "../lib/gesture/swipe_gesture.h"
#include "../lib/gesture/swipe_gesture.regex.cpp"
/*
Comfortable Swipe