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
This commit is contained in:
Rico Tiongson 2019-02-13 09:27:41 +08:00 committed by GitHub
parent c18cb04230
commit 204a422f2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 89 additions and 23 deletions

4
.gitignore vendored
View File

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

View File

@ -54,6 +54,9 @@ int main(int argc, char** args)
else if (arg == "debug")
comfortable_swipe::service::debug();
else if (arg == "status")
comfortable_swipe::service::status();
else /* if (arg == "help") */
comfortable_swipe::service::help();
}

View File

@ -34,6 +34,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#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"

View File

@ -116,10 +116,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);
}
}
}

View File

@ -19,12 +19,14 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "swipe_gesture.h"
namespace comfortable_swipe::gesture
{
/**
* Regex pattern for the libinput entry for start of swipe.
* Extracts one match for the number of fingers used during the swipe.
*
*
* eg. event15 GESTURE_SWIPE_BEGIN +34.33s 3
* ^
* fingers
@ -41,7 +43,7 @@ namespace comfortable_swipe::gesture
/**
* Regex pattern for the libinput entry for the end of swipe.
* Extracts one match for the number of fingers used during the swipe.
*
*
* eg. event15 GESTURE_SWIPE_END +35.03s 3
* ^
* fingers
@ -64,7 +66,7 @@ namespace comfortable_swipe::gesture
/**
* Regex pattern for the libinput entry for during a swipe.
* Extracts number of fingers used and the speed (normal and accelerated) of the swipe.
*
*
* eg. event15 GESTURE_SWIPE_UPDATE +34.70s 3 -0.12/ 4.99 (-0.33/13.50 unaccelerated)
* ^ ^ ^ ^ ^
* fingers dx dy udx udy

View File

@ -54,6 +54,7 @@ extern "C"
void help();
void restart();
void start();
void status();
void stop();
}
}

View File

@ -19,7 +19,7 @@ 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> // std::fgets_unlocked, stdin
#include <cstdio> // fgets_unlocked, stdin
#include "../index.hpp"
/**
@ -47,14 +47,13 @@ namespace comfortable_swipe::service
);
// prepare data containers
static const int MAX_LINE_LENGTH = 256;
static char data[MAX_LINE_LENGTH];
std::array<char, 256> line;
// start reading lines from input one by one
while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL)
while (fgets_unlocked(line.data(), line.size(), stdin) != NULL)
{
// attempt to parse swipe gestures
swipe_gesture_handler.parse_line(data);
swipe_gesture_handler.parse_line(line.data());
}
}
}

View File

@ -39,6 +39,7 @@ namespace comfortable_swipe::service
std::puts("buffer - parses output of libinput debug-events");
std::puts("help - shows the help dialog");
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());
}

58
lib/service/status.cpp Normal file
View File

@ -0,0 +1,58 @@
#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
namespace comfortable_swipe::service
{
/**
* Restarts the comfortable-swipe service.
*/
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");
}
}
#endif /* __COMFORTABLE_SWIPE__service_restart__ */

View File

@ -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);
}
}