Skip to content

Commit 7d76db6

Browse files
authored
Update status command and add config command (#55)
* Add configurations to status * Update comment of status * Add config command * Modify README
1 parent 204a422 commit 7d76db6

File tree

9 files changed

+128
-28
lines changed

9 files changed

+128
-28
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@ Comfortable, seamless, and fast 3-finger (and 4-finger) touchpad swipe gestures
4141
```
4242
comfortable-swipe autostart
4343
```
44-
5. _Optional_: Change [configurations](#configurations) (see below). After making changes, run
44+
5. Check the status of your application by running
4545
```
46-
comfortable-swipe restart
46+
comfortable-swipe status
4747
```
4848
4949
## Configurations
50-
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.
50+
Comfortable swipe makes use of keyboard shortcuts for configurations. Edit by running
51+
```
52+
nano $(comfortable-swipe config)
53+
```
54+
55+
Make sure to run after making changes:
56+
```
57+
comfortable-swipe restart
58+
```
5159
5260
Property | Description | Default Value | Default Behavior
5361
--------- | ----------- | -------------- | -----
@@ -80,7 +88,13 @@ Refer to https://www.linux.org/threads/xdotool-keyboard.10528/ for a complete li
8088

8189
## Debugging
8290

83-
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.
91+
You can check your touchpad driver by running
92+
93+
```bash
94+
comfortable-swipe debug
95+
```
96+
97+
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.
8498

8599
A working swipe gesture will show the following:
86100

comfortable-swipe.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ int main(int argc, char** args)
5151
else if (arg == "autostart")
5252
comfortable_swipe::service::autostart();
5353

54+
else if (arg == "config")
55+
comfortable_swipe::service::config();
56+
5457
else if (arg == "debug")
5558
comfortable_swipe::service::debug();
5659

lib/comfortable_swipe

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
3030
#include "gesture/swipe_gesture.regex.cpp"
3131
#include "service/autostart.cpp"
3232
#include "service/buffer.cpp"
33+
#include "service/config.cpp"
3334
#include "service/debug.cpp"
3435
#include "service/help.cpp"
3536
#include "service/restart.cpp"

lib/gesture/swipe_gesture.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ namespace comfortable_swipe::gesture
198198
const int swipe_gesture::MSK_VERTICAL = 4;
199199
const int swipe_gesture::FRESH = -1;
200200
const char * const swipe_gesture::command_map[8] = {
201-
"left 3",
202-
"left 4",
203-
"right 3",
204-
"right 4",
205-
"up 3",
206-
"up 4",
207-
"down 3",
208-
"down 4"
201+
"left3",
202+
"left4",
203+
"right3",
204+
"right4",
205+
"up3",
206+
"up4",
207+
"down3",
208+
"down4"
209209
};
210210
}
211211

lib/index.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ extern "C"
5050
{
5151
void autostart();
5252
void buffer();
53+
void config();
5354
void debug();
5455
void help();
5556
void restart();

lib/service/config.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef __COMFORTABLE_SWIPE__service_config__
2+
#define __COMFORTABLE_SWIPE__service_config__
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 <cstdio> // std::puts
24+
25+
namespace comfortable_swipe::service
26+
{
27+
/**
28+
* Prints where the config file of comfortable swipe is located.
29+
*
30+
* Usage: nano $(comfortable-swipe config)
31+
*/
32+
void config()
33+
{
34+
std::puts(comfortable_swipe::util::conf_filename());
35+
}
36+
}
37+
38+
#endif /* __COMFORTABLE_SWIPE__service_config__ */

lib/service/help.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ namespace comfortable_swipe::service
3030
void help()
3131
{
3232
using comfortable_swipe::util::conf_filename;
33-
std::puts("comfortable-swipe [start|stop|restart|autostart|buffer|help|debug]");
33+
std::puts("comfortable-swipe [start|stop|restart|autostart|buffer|help|config|debug|status]");
3434
std::puts("");
3535
std::puts("start - starts 3/4-finger gesture service");
3636
std::puts("stop - stops 3/4-finger gesture service");
3737
std::puts("restart - stops then starts 3/4-finger gesture service");
3838
std::puts("autostart - automatically run on startup (toggleable)");
3939
std::puts("buffer - parses output of libinput debug-events");
4040
std::puts("help - shows the help dialog");
41+
std::puts("config - locates the config file [/usr/share/comfortable-swipe/comfortable-swipe.conf]");
4142
std::puts("debug - logs raw output from input events taken from libinput");
4243
std::puts("status - checks status of program and autostart");
4344
std::puts("");

lib/service/status.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2626
#include <array> // std::array
2727
#include <cstdlib> // std::atoi
2828
#include <cstdio> // FILE, std::feof, std::fgets, std::printf
29+
#include <regex> // std::cmatch, std::regex, std::regex_match
2930

3031
namespace comfortable_swipe::service
3132
{
3233
/**
33-
* Restarts the comfortable-swipe service.
34+
* Prints the status of comfortable-swipe.
3435
*/
3536
void status()
3637
{
@@ -52,6 +53,44 @@ namespace comfortable_swipe::service
5253
// print status
5354
std::printf("program is %s\n", running ? "ON" : "OFF");
5455
std::printf("autostart is %s\n", autostart_on ? "ON" : "OFF");
56+
std::printf("config file at %s\n", comfortable_swipe::util::conf_filename());
57+
58+
// check status of configuration file
59+
try
60+
{
61+
auto config = comfortable_swipe::util::read_config_file(comfortable_swipe::util::conf_filename());
62+
63+
// print keys and values of config file
64+
std::printf("\nConfigurations:\n");
65+
66+
// print threshold
67+
if (config.count("threshold") > 0)
68+
{
69+
auto & threshold = config["threshold"];
70+
71+
// check if regex pattern matches threshold
72+
std::cmatch matches;
73+
bool ok = (std::regex_match(threshold.data(), matches, std::regex("^\\d+(?:\\.\\d+)??$")) != 0);
74+
75+
// print status of threshold
76+
std::printf(" %9s is %s (%s)\n", "threshold", ok ? "OK" : "INVALID", threshold.data());
77+
}
78+
else
79+
std::printf(" %9s is OFF\n", "threshold");
80+
81+
// print swipe commands
82+
for (auto &command : comfortable_swipe::gesture::swipe_gesture::command_map)
83+
{
84+
if (config.count(command) > 0)
85+
std::printf(" %9s is OK (%s)\n", command, config[command].data());
86+
else
87+
std::printf(" %9s is OFF\n", command);
88+
}
89+
}
90+
catch (const std::runtime_error& e)
91+
{
92+
std::printf("config error: %s\n", e.what());
93+
}
5594
}
5695
}
5796

lib/util/read_config_file.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,41 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
#include <map> // std::map
2323
#include <string> // std::string
2424
#include <fstream> // std::ifstream
25-
#include <iostream> // std::cerr, std::endl, std::getline
25+
#include <sstream> // std::ostringstream
26+
#include <iostream> // std::endl, std::getline
2627
#include <cstdlib> // exit
2728
#include <cctype> // std::isspace
29+
#include <stdexcept> // std::runtime_error
2830

2931
namespace comfortable_swipe::util
3032
{
3133
/**
3234
* A utility method for reading the config file.
33-
*
35+
*
3436
* @param filename (const char*) the path of the config file.
3537
*/
3638
std::map<std::string, std::string> read_config_file(const char* filename)
3739
{
38-
40+
3941
std::map<std::string, std::string> conf;
4042
std::ifstream fin(filename);
4143

4244
if (!fin.is_open())
4345
{
44-
std::cerr << "file \"" << filename << "\" does not exist!" << std::endl;
45-
exit(1);
46+
throw std::runtime_error("config file does not exist");
4647
}
4748

4849
static std::string line, token[2];
4950
int line_number = 0;
50-
51+
5152
while (std::getline(fin, line))
5253
{
5354
++line_number;
5455
token[0].clear();
5556
token[1].clear();
5657
int length = line.length();
5758
int equal_flag = 0;
58-
59+
5960
// tokenize comfig config
6061
for (int i = 0; i < length; ++i)
6162
{
@@ -65,9 +66,10 @@ namespace comfortable_swipe::util
6566
{
6667
if (++equal_flag > 1)
6768
{
68-
std::cerr << "error in conf file " << filename << std::endl;
69-
std::cerr << "multiple equal signs in line " << line_number << std::endl;
70-
exit(1);
69+
std::ostringstream stream;
70+
stream << "error in conf file " << filename << std::endl;
71+
stream << "multiple equal signs in line " << line_number << std::endl;
72+
throw std::runtime_error(stream.str());
7173
}
7274
}
7375
else if (!std::isspace(line[i]))
@@ -84,16 +86,17 @@ namespace comfortable_swipe::util
8486
// no equal sign found in non-empty line
8587
if (equal_flag == 0)
8688
{
87-
std::cerr << "error in conf file: " << filename << std::endl;
88-
std::cerr << "equal sign expected in line " << line_number << std::endl;
89-
exit(1);
89+
std::ostringstream stream;
90+
stream << "error in conf file: " << filename << std::endl;
91+
stream << "equal sign expected in line " << line_number << std::endl;
92+
throw std::runtime_error(stream.str());
9093
}
9194

9295
// equal sign found, add to tokens
9396
if (token[1].length() > 0)
9497
conf[token[0]] = token[1];
9598
}
96-
99+
97100
return conf;
98101
}
99102
}

0 commit comments

Comments
 (0)