-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
82 lines (73 loc) · 2.33 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <memory>
#include <signal.h>
#include <vector>
#include "./lib/ConfigLoader.h"
#include "./lib/handler/HandlerComposer.h"
#include "./lib/handler/OutputKeyCodeHandler.h"
#include "./lib/hooks/LinuxKeyHook.h"
LinuxKeyHook keyHook;
void sigIntHandler(int)
{
std::cout << "\nStopped by user..." << std::endl;
keyHook.abort();
exit(0);
}
void showUsage()
{
std::cout
<< "Usage: dualkey-tools [OPTION]...\n"
<< "Options:\n"
<< "\t-h,--help\t\tShow this help message\n"
<< "\t-r,--rules FILE (yaml)\tSpecify the rules file. Defaults to rules.yaml\n"
<< "\t-t,--test\t\tTest mode. Useful for figuring out keycodes.\n"
<< "\nYou should run this tool with the highest process priority possible.\n"
<< ">> Unix: sudo nice -n -20 ./dualkey-tools"
<< std::endl;
}
int main(int argc, char* argv[])
{
// TODO mutex / only allow one app instance
// dev note: mutex is not needed in linux
// because app will close because input device
// cannot be grabbed a second time
signal(SIGINT, sigIntHandler);
setbuf(stdin, NULL), setbuf(stdout, NULL);
auto rulesFile = "rules.yaml";
bool testMode = false;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if ((arg == "-h") || (arg == "--help")) {
showUsage();
return 0;
} else if ((arg == "-r") || (arg == "--rules")) {
if (i + 1 < argc) {
rulesFile = argv[++i];
} else {
std::cerr << "-r,--rules option requires one argument." << std::endl;
showUsage();
return 1;
}
} else if ((arg == "-t") || (arg == "--test")) {
testMode = true;
} else {
std::cerr << "unknown option: " << arg << std::endl;
showUsage();
return 1;
}
}
try {
if (testMode) {
std::cout << "TEST MODE - use CTRL + C to quit" << std::endl;
OutputKeyCodeHandler testHandler;
keyHook.hookSync(testHandler);
} else {
auto composed = ConfigLoader::loadRulesFile(rulesFile);
keyHook.hookSync(composed);
}
} catch (const std::exception& e) {
printf("%s\n", e.what());
return 1;
}
return 0;
}