Add service module

This commit is contained in:
Rico Tiongson 2019-02-07 16:10:13 +08:00
parent 99e1a42206
commit 6d38db4ee3
6 changed files with 264 additions and 0 deletions

View File

@ -0,0 +1,57 @@
#ifndef __COMFORTABLE_SWIPE__service_autostart__
#define __COMFORTABLE_SWIPE__service_autostart__
#include <iostream> // std::cerr, std::cout, std::endl
#include <fstream> // std::ifstream, std::ofstream
#include <string> // std::string
#include <cstdio> // std::remove
#include <cstdlib> // std::system
#include "../index.hpp"
namespace comfortable_swipe::service
{
/**
* Toggles automatic startup of comfortable swipe.
*/
void autostart()
{
using comfortable_swipe::util::autostart_filename;
const std::string& path = autostart_filename();
if (std::ifstream(path.data()).good())
{
// file found, delete it
if (std::remove(path.data()) != 0)
std::cerr << "Error: failed to switch off autostart. "
<< "Maybe the autostart file is in use?"
<< std::endl;
else
std::cout << "Autostart switched off" << std::endl;
}
else {
// file not found, create it
int result = std::system(("mkdir -p $(dirname " + path + ")").data());
std::ofstream fout(path.data());
if (result != 0 || !fout.good())
std::cerr << "Error: failed to switch on autostart. "
<< "Are you sure you have the permissions?"
<< std::endl;
else {
fout <<
"[Desktop Entry]\n"
"Type=Application\n"
"Exec=bash -c \""
__COMFORTABLE_SWIPE__PROGRAM__
" start\"\n"
"Hidden=false\n"
"NoDisplay=false\n"
"X-GNOME-Autostart-enabled=true\n"
"Name=Comfortable Swipe\n"
"Comment=3 or 4 touchpad gestures\n";
std::cout << "Autostart switched on" << std::endl;
}
}
}
}
#endif /* __COMFORTABLE_SWIPE__service_autostart__ */

View File

@ -0,0 +1,88 @@
#ifndef __COMFORTABLE_SWIPE__service_buffer__
#define __COMFORTABLE_SWIPE__service_buffer__
#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.
*/
void comfortable_swipe::service::buffer()
{
// import utility methods
using comfortable_swipe::util::read_config_file;
using comfortable_swipe::util::conf_filename;
using comfortable_swipe::gesture::swipe_gesture;
// import regex patterns
using comfortable_swipe::util::GESTURE_SWIPE_BEGIN_REGEX_PATTERN;
using comfortable_swipe::util::GESTURE_SWIPE_UPDATE_REGEX_PATTERN;
using comfortable_swipe::util::GESTURE_SWIPE_END_REGEX_PATTERN;
// pre-compile regex patterns
static const std::regex gesture_begin(GESTURE_SWIPE_BEGIN_REGEX_PATTERN);
static const std::regex gesture_update(GESTURE_SWIPE_UPDATE_REGEX_PATTERN);
static const std::regex gesture_end(GESTURE_SWIPE_END_REGEX_PATTERN);
// read config file
auto config = read_config_file(conf_filename());
// initialize swipegesture handler
swipe_gesture swipe
(
config.count("threshold") ? std::atof(config["threshold"].data()) : 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_begin = false;
// start reading lines from input one by one
while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL)
{
if (!flag_begin)
{
if (std::regex_match(data, matches, gesture_begin) != 0)
{
swipe.fingers = std::stoi(matches[1]);
swipe.begin();
flag_begin = true;
}
}
else /* flag_begin == true */
{
if (std::regex_match(data, matches, gesture_update) != 0)
{
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_end) != 0)
{
swipe.fingers = std::stoi(matches[1]);
swipe.end();
flag_begin = false;
}
}
}
}
#endif /* __COMFORTABLE_SWIPE__service_buffer__ */

View File

@ -0,0 +1,28 @@
#ifndef __COMFORTABLE_SWIPE__service_help__
#define __COMFORTABLE_SWIPE__service_help__
#include <cstdio> // std::puts, std::printf
#include "../index.hpp"
namespace comfortable_swipe::service
{
/**
* Shows the help window.
*/
void help()
{
using comfortable_swipe::util::conf_filename;
std::puts("comfortable-swipe [start|stop|restart|autostart|buffer|help]");
std::puts("");
std::puts("start - starts 3/4-finger gesture service");
std::puts("stop - stops 3/4-finger gesture service");
std::puts("restart - stops then starts 3/4-finger gesture 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("");
std::printf("Configuration file can be found in %s\n", conf_filename());
}
}
#endif /* __COMFORTABLE_SWIPE__service_help__ */

View File

@ -0,0 +1,18 @@
#ifndef __COMFORTABLE_SWIPE__service_restart__
#define __COMFORTABLE_SWIPE__service_restart__
#include "../index.hpp"
namespace comfortable_swipe::service
{
/**
* Restarts the comfortable-swipe service.
*/
void restart()
{
comfortable_swipe::service::start();
comfortable_swipe::service::stop();
}
}
#endif /* __COMFORTABLE_SWIPE__service_restart__ */

View File

@ -0,0 +1,18 @@
#ifndef __COMFORTABLE_SWIPE__service_start__
#define __COMFORTABLE_SWIPE__service_start__
#include "../index.hpp"
#include <cstdlib> // std::system
namespace comfortable_swipe::service
{
/**
* Starts the comfortable-swipe service by buffering libinput debug-events.
*/
void start()
{
std::system("stdbuf -oL -e0 libinput debug-events | " __COMFORTABLE_SWIPE__PROGRAM__ " buffer");
}
}
#endif /* __COMFORTABLE_SWIPE__service_start__ */

View File

@ -0,0 +1,55 @@
#ifndef __COMFORTABLE_SWIPE__service_stop__
#define __COMFORTABLE_SWIPE__service_stop__
#include <cstdio> // std::FILE, std::feof, std::fgets
#include <cstdlib> // std::atoi, std::system
#include <string> // std::string, std::to_string
#include <stdexcept> // std::runtime_error
#include <unistd.h> // popen, pclose, getpid
namespace comfortable_swipe::service
{
/**
* Stops all comfortable-swipe instances.
*/
void stop()
{
// read all service names from process (pgrep)
char* buffer = new char[20];
FILE* pipe = popen("pgrep -f comfortable-swipe", "r");
// make sure pipe exists
if (pipe == NULL)
{
throw std::runtime_error("stop command failed");
}
// buffer what to kill
std::string kill = "kill";
// read until end of line
while (!std::feof(pipe))
{
if (std::fgets(buffer, 20, pipe) != NULL)
{
int pid = std::atoi(buffer);
if (pid != getpid())
{
kill += " ";
kill += std::to_string(pid);
}
}
}
// run "kill {pid1} {pid2}..."
std::system(kill.data());
delete[] buffer;
// close the pipe
pclose(pipe);
}
}
#endif /* __COMFORTABLE_SWIPE__service_stop__ */