Skip to content

Commit 204a422

Browse files
authored
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
1 parent c18cb04 commit 204a422

File tree

10 files changed

+89
-23
lines changed

10 files changed

+89
-23
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# C++ generated headers
22
*.gch
3+
4+
# IDE-specific
5+
.idea
6+
.vscode

comfortable-swipe.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ int main(int argc, char** args)
5454
else if (arg == "debug")
5555
comfortable_swipe::service::debug();
5656

57+
else if (arg == "status")
58+
comfortable_swipe::service::status();
59+
5760
else /* if (arg == "help") */
5861
comfortable_swipe::service::help();
5962
}

lib/comfortable_swipe

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
3434
#include "service/help.cpp"
3535
#include "service/restart.cpp"
3636
#include "service/start.cpp"
37+
#include "service/status.cpp"
3738
#include "service/stop.cpp"
3839
#include "util/autostart_filename.cpp"
3940
#include "util/conf_filename.cpp"

lib/gesture/swipe_gesture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ namespace comfortable_swipe::gesture
116116
if (this->previous_gesture == swipe_gesture::FRESH
117117
|| this->previous_gesture == (mask ^ swipe_gesture::MSK_POSITIVE))
118118
{
119+
xdo_send_keysequence_window(this->xdo, CURRENTWINDOW, swipe_gesture::commands[mask], 0);
119120
this->x = this->y = 0;
120121
this->previous_gesture = mask;
121122
std::cout << "SWIPE " << swipe_gesture::command_map[mask] << std::endl;
122-
xdo_send_keysequence_window(xdo, CURRENTWINDOW, swipe_gesture::commands[mask], 0);
123123
}
124124
}
125125
}

lib/gesture/swipe_gesture.regex.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ You should have received a copy of the GNU General Public License
1919
along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*/
2121

22+
#include "swipe_gesture.h"
23+
2224
namespace comfortable_swipe::gesture
2325
{
2426
/**
2527
* Regex pattern for the libinput entry for start of swipe.
2628
* Extracts one match for the number of fingers used during the swipe.
27-
*
29+
*
2830
* eg. event15 GESTURE_SWIPE_BEGIN +34.33s 3
2931
* ^
3032
* fingers
@@ -41,7 +43,7 @@ namespace comfortable_swipe::gesture
4143
/**
4244
* Regex pattern for the libinput entry for the end of swipe.
4345
* Extracts one match for the number of fingers used during the swipe.
44-
*
46+
*
4547
* eg. event15 GESTURE_SWIPE_END +35.03s 3
4648
* ^
4749
* fingers
@@ -64,7 +66,7 @@ namespace comfortable_swipe::gesture
6466
/**
6567
* Regex pattern for the libinput entry for during a swipe.
6668
* Extracts number of fingers used and the speed (normal and accelerated) of the swipe.
67-
*
69+
*
6870
* eg. event15 GESTURE_SWIPE_UPDATE +34.70s 3 -0.12/ 4.99 (-0.33/13.50 unaccelerated)
6971
* ^ ^ ^ ^ ^
7072
* fingers dx dy udx udy

lib/index.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern "C"
5454
void help();
5555
void restart();
5656
void start();
57+
void status();
5758
void stop();
5859
}
5960
}

lib/service/buffer.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License
1919
along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*/
2121

22-
#include <cstdio> // std::fgets_unlocked, stdin
22+
#include <cstdio> // fgets_unlocked, stdin
2323
#include "../index.hpp"
2424

2525
/**
@@ -47,14 +47,13 @@ namespace comfortable_swipe::service
4747
);
4848

4949
// prepare data containers
50-
static const int MAX_LINE_LENGTH = 256;
51-
static char data[MAX_LINE_LENGTH];
50+
std::array<char, 256> line;
5251

5352
// start reading lines from input one by one
54-
while (fgets_unlocked(data, MAX_LINE_LENGTH, stdin) != NULL)
53+
while (fgets_unlocked(line.data(), line.size(), stdin) != NULL)
5554
{
5655
// attempt to parse swipe gestures
57-
swipe_gesture_handler.parse_line(data);
56+
swipe_gesture_handler.parse_line(line.data());
5857
}
5958
}
6059
}

lib/service/help.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace comfortable_swipe::service
3939
std::puts("buffer - parses output of libinput debug-events");
4040
std::puts("help - shows the help dialog");
4141
std::puts("debug - logs raw output from input events taken from libinput");
42+
std::puts("status - checks status of program and autostart");
4243
std::puts("");
4344
std::printf("Configuration file can be found in %s\n", conf_filename());
4445
}

lib/service/status.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef __COMFORTABLE_SWIPE__service_status__
2+
#define __COMFORTABLE_SWIPE__service_status__
3+
4+
/*
5+
Comfortable Swipe
6+
by Rico Tiongson
7+
8+
This program is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#include "../index.hpp"
23+
#include <stdexcept> // std::runtime_error
24+
#include <unistd.h> // popen, pclose, getpid, access, F_OK
25+
#include <memory> // std::unique_ptr
26+
#include <array> // std::array
27+
#include <cstdlib> // std::atoi
28+
#include <cstdio> // FILE, std::feof, std::fgets, std::printf
29+
30+
namespace comfortable_swipe::service
31+
{
32+
/**
33+
* Restarts the comfortable-swipe service.
34+
*/
35+
void status()
36+
{
37+
// check if comfortable-swipe is running
38+
bool running = false;
39+
std::array<char, 128> buffer;
40+
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("pgrep -f comfortable-swipe", "r"), pclose);
41+
if (pipe && !std::feof(pipe.get()) && std::fgets(buffer.data(), buffer.size(), pipe.get()) != NULL)
42+
{
43+
int pid = std::atoi(buffer.data());
44+
if (pid != getpid())
45+
running = true;
46+
}
47+
48+
// check if autostart is on
49+
auto autostart_f = comfortable_swipe::util::autostart_filename();
50+
bool autostart_on = access(autostart_f, F_OK) != -1;
51+
52+
// print status
53+
std::printf("program is %s\n", running ? "ON" : "OFF");
54+
std::printf("autostart is %s\n", autostart_on ? "ON" : "OFF");
55+
}
56+
}
57+
58+
#endif /* __COMFORTABLE_SWIPE__service_restart__ */

lib/service/stop.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2424

2525
#include <stdexcept> // std::runtime_error
2626
#include <unistd.h> // popen, pclose, getpid
27+
#include <memory> // std::unique_ptr
28+
#include <array> // std::array
29+
#include <cstdlib> // std::atoi
30+
#include <cstdio> // FILE, std::feof, std::fgets
2731

2832
namespace comfortable_swipe::service
2933
{
@@ -34,24 +38,22 @@ namespace comfortable_swipe::service
3438
{
3539

3640
// read all service names from process (pgrep)
37-
char* buffer = new char[20];
38-
FILE* pipe = popen("pgrep -f comfortable-swipe", "r");
39-
41+
std::array<char, 128> buffer;
42+
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("pgrep -f comfortable-swipe", "r"), pclose);
43+
4044
// make sure pipe exists
41-
if (pipe == NULL)
42-
{
45+
if (!pipe)
4346
throw std::runtime_error("stop command failed");
44-
}
4547

4648
// buffer what to kill
4749
std::string kill = "kill";
4850

4951
// read until end of line
50-
while (!std::feof(pipe))
52+
while (!std::feof(pipe.get()))
5153
{
52-
if (std::fgets(buffer, 20, pipe) != NULL)
54+
if (std::fgets(buffer.data(), buffer.size(), pipe.get()) != NULL)
5355
{
54-
int pid = std::atoi(buffer);
56+
int pid = std::atoi(buffer.data());
5557
if (pid != getpid())
5658
{
5759
kill += " ";
@@ -62,11 +64,6 @@ namespace comfortable_swipe::service
6264

6365
// run "kill {pid1} {pid2}..."
6466
(void) std::system(kill.data());
65-
delete[] buffer;
66-
67-
// close the pipe
68-
pclose(pipe);
69-
7067
}
7168
}
7269

0 commit comments

Comments
 (0)