-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
87 lines (73 loc) · 2.74 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
83
84
85
86
87
#include "Keyboard.h"
#include <regex>
#include <iostream>
using namespace std;
typedef pair<Color, Color> ColorPair;
regex rgbColor("[0-9a-fA-F]{6}");
regex hueColor("h(0|[1-2]\\d{0,2}|3[0-5]\\d?|3[6-9]|[4-9]\\d?)");
regex colorPair("([^-]+)-([^-]+)");
static bool isColor(const string &str) {
return regex_match(str, rgbColor) || regex_match(str, hueColor);
}
static bool isColorPair(const string &str) {
smatch sm;
if (regex_match(str, sm, colorPair)) {
return isColor(sm[1]) && isColor(sm[2]);
} else {
return isColor(str);
}
}
static Color parseColor(const string &str) {
if (regex_match(str, rgbColor)) {
int rgb = stoi(str, nullptr, 16);
return Color::rgb(rgb);
}
smatch sm;
if (regex_match(str, sm, hueColor)) {
int hue = stoi(sm[1]);
return Color::hue(hue);
}
return Color(0, 0, 0);
}
static ColorPair parseColorPair(const string &str) {
if (isColor(str)) {
return make_pair(parseColor(str), Color(0, 0, 0));
}
smatch sm;
if (regex_match(str, sm, colorPair)) {
return make_pair(parseColor(sm[1]), parseColor(sm[2]));
}
return make_pair(Color(0, 0, 0), Color(0, 0, 0));
}
static void printUsage(const string &self) {
cerr << self << " normal <left> <middle> <right>" << endl;
cerr << self << " gaming <left>" << endl;
cerr << self << " breathing <left> <middle> <right>" << endl;
cerr << self << " wave <left> <middle> <right>" << endl;
#ifdef WIN32
cerr << self << " auto" << endl;
#endif
cerr << endl;
cerr << "All colors can be specified either in HEX form (rrggbb) or as hue (hXXX)" << endl;
cerr << "Breathing and wave mode also accepts color pairs (first-second) as argument" << endl;
}
int main(int argc, char **argv) {
Keyboard kbd;
if (argc == 5 && strcmp(argv[1], "normal") == 0 && isColor(argv[2]) && isColor(argv[3]) && isColor(argv[4])) {
kbd.normal(parseColor(argv[2]), parseColor(argv[3]), parseColor(argv[4]));
} else if (argc == 3 && strcmp(argv[1], "gaming") == 0 && isColor(argv[2])) {
kbd.gaming(parseColor(argv[2]));
} else if (argc == 5 && strcmp(argv[1], "breathing") == 0 && isColorPair(argv[2]) && isColorPair(argv[3]) && isColorPair(argv[4])) {
kbd.breathing(parseColorPair(argv[2]), parseColorPair(argv[3]), parseColorPair(argv[4]));
} else if (argc == 5 && strcmp(argv[1], "wave") == 0 && isColorPair(argv[2]) && isColorPair(argv[3]) && isColorPair(argv[4])) {
kbd.wave(parseColorPair(argv[2]), parseColorPair(argv[3]), parseColorPair(argv[4]));
#ifdef WIN32
} else if (argc == 2 && strcmp(argv[1], "auto") == 0) {
kbd.automatic();
#endif
} else {
printUsage(argv[0]);
return 1;
}
return 0;
}