forked from giuliopaci/msi-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
277 lines (215 loc) · 8 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
main.cpp
(C) Copyright 2015, Brad Parker
All rights reserved.
License: 3-clause BSD. See COPYING
*/
#include <QCoreApplication>
#include <QCommandLineParser>
#include <iostream>
#include "keyboard.h"
enum CommandLineParseResult
{
CommandLineOk,
CommandLineError,
CommandLineVersionRequested,
CommandLineHelpRequested
};
struct ColorPresetOption {
ColorPresetOption() :
region(REGION_LEFT)
,color(COLOR_RED)
,intensity(INTENSITY_HIGH)
{}
Region region;
Color color;
Intensity intensity;
};
struct ColorOption {
ColorOption() :
region(TOUCHPAD)
,rgb{0,0,0}
{}
Region region;
char rgb[3];
};
struct KeyboardOptions {
KeyboardOptions() :
modeOption(MODE_NORMAL)
,colorOptions()
,modeSet(false)
,colorSet(false)
,presetSet(false)
{}
Mode modeOption;
QList<ColorPresetOption*> presetOptions;
QList<ColorOption*> colorOptions;
bool modeSet;
bool colorSet;
bool presetSet;
void setMode(QString mode) {
if(mode == "normal") { modeOption = MODE_NORMAL; }
if(mode == "gaming") { modeOption = MODE_GAMING; }
if(mode == "breathe") { modeOption = MODE_BREATHE; }
if(mode == "demo") { modeOption = MODE_DEMO; }
if(mode == "wave") { modeOption = MODE_WAVE; }
modeSet = true;
}
void setColorPreset(QString colorString) {
QStringList fields = colorString.split(',');
if(fields.count() != 3) {
std::cerr << "invalid color selection" << std::endl;
qApp->quit();
return;
}
QString region = fields.at(0);
QString color = fields.at(1);
QString intensity = fields.at(2);
ColorPresetOption *presetOption = new ColorPresetOption;
if(region == "left") presetOption->region = REGION_LEFT;
else if(region == "middle") presetOption->region = REGION_MIDDLE;
else if(region == "right") presetOption->region = REGION_RIGHT;
else{
std::cerr << "invalid region selection" << std::endl;
qApp->quit();
return;
}
if(color == "off") presetOption->color = COLOR_OFF;
if(color == "red") presetOption->color = COLOR_RED;
if(color == "orange") presetOption->color = COLOR_ORANGE;
if(color == "yellow") presetOption->color = COLOR_YELLOW;
if(color == "green") presetOption->color = COLOR_GREEN;
if(color == "sky") presetOption->color = COLOR_SKY;
if(color == "blue") presetOption->color = COLOR_BLUE;
if(color == "purple") presetOption->color = COLOR_PURPLE;
if(color == "white") presetOption->color = COLOR_WHITE;
if(intensity == "high") presetOption->intensity = INTENSITY_HIGH;
if(intensity == "medium") presetOption->intensity = INTENSITY_MEDIUM;
if(intensity == "low") presetOption->intensity = INTENSITY_LOW;
if(intensity == "light") presetOption->intensity = INTENSITY_LIGHT;
presetSet = true;
presetOptions.append(presetOption);
}
void setColor(QString colorString) {
QStringList fields = colorString.split(',');
if(fields.count() != 4) {
std::cerr << "invalid color selection" << std::endl;
qApp->quit();
return;
}
QString region = fields.at(0);
ColorOption *colorOption = new ColorOption;
if(region == "left") colorOption->region = REGION_LEFT;
if(region == "middle") colorOption->region = REGION_MIDDLE;
if(region == "right") colorOption->region = REGION_RIGHT;
if(region == "logo") colorOption->region = LOGO;
if(region == "front-left") colorOption->region = FRL_LEFT;
if(region == "front-right") colorOption->region = FRL_RIGHT;
if(region == "touchpad") colorOption->region = TOUCHPAD;
bool ok=false;
for (uint k=0;k<3;k++){
colorOption->rgb[k]=fields.at(k+1).toInt(&ok);
if (!ok){
std::cerr << "invalid color selection" << std::endl;
qApp->quit();
return;
}
}
colorSet = true;
colorOptions.append(colorOption);
}
};
CommandLineParseResult parseCommandLine(QCommandLineParser &parser, KeyboardOptions *keyboardOptions, QString *errorMessage) {
QCommandLineOption helpOption = parser.addHelpOption();
QCommandLineOption versionOption = parser.addVersionOption();
QCommandLineOption mode(QStringList() << "m" << "mode", "set color <mode>: normal, gaming, breathe, demo, wave", "mode");
QCommandLineOption color(QStringList() << "r" << "rgb", "set an <rgb> color using the format: region,red,green,blue with rgb values between 0 and 255", "rgb");
QCommandLineOption colorPreset(QStringList() << "c" << "color", "set a <color> preset using the format: region,color,intensity (only valid for left/middle/right regions). When using this option, a mode must be specified with -m", "color");
parser.addOption(mode);
parser.addOption(color);
parser.addOption(colorPreset);
if(!parser.parse(QCoreApplication::arguments())) {
*errorMessage = parser.errorText();
return CommandLineError;
}
if(parser.isSet(versionOption))
return CommandLineVersionRequested;
if(parser.isSet(helpOption))
return CommandLineHelpRequested;
if(parser.isSet(mode)) {
keyboardOptions->setMode(parser.value(mode));
}
if(parser.isSet(colorPreset)) {
foreach(const QString &presetValue, parser.values(colorPreset)) {
keyboardOptions->setColorPreset(presetValue);
}
}
if(parser.isSet(color)) {
foreach(const QString &colorValue, parser.values(color)) {
keyboardOptions->setColor(colorValue);
}
}
return CommandLineOk;
}
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
app.setApplicationName("msi-keyboard");
app.setApplicationVersion("1.1");
QCommandLineParser parser;
parser.setApplicationDescription("Keyboard color changer for MSI steelseries keyboards");
QString errorMessage;
KeyboardOptions keyboardOptions;
switch(parseCommandLine(parser, &keyboardOptions, &errorMessage)) {
case CommandLineOk:
break;
case CommandLineError:
fputs(qPrintable(errorMessage), stderr);
fputs("\n\n", stderr);
fputs(qPrintable(parser.helpText()), stderr);
return 1;
case CommandLineVersionRequested:
printf("%s %s\n", qPrintable(QCoreApplication::applicationName()),
qPrintable(QCoreApplication::applicationVersion()));
return 0;
case CommandLineHelpRequested:
{
std::cout << qPrintable(parser.helpText()) << std::endl;
QStringList regions = QStringList() << "left" << "middle" << "right"<<"logo"<<"front-left"<<"front-right"<<"touchpad";
QStringList colors = QStringList() << "off" << "red" << "orange" << "yellow" << "green" << "sky" << "blue" << "purple" << "white";
QStringList intensities = QStringList() << "high" << "medium" << "low" << "light";
QString colorHelp = QString(R"(Available regions:
%1
Available color presets (only for left/middle/right regions):
%2
Available preset intensities (only for left/middle/right regions):
%3
Example:
%4
)").arg(regions.join('\n')).arg(colors.join('\n')).arg(intensities.join('\n')).arg(QString(argv[0]) + " -m normal -c left,red,high -c middle,purple,high -c right,sky,high -r touchpad,255,0,0 -r logo,255,255,255");
std::cout << qPrintable(colorHelp) << std::endl;
return 0;
}
}
if((!keyboardOptions.modeSet || !keyboardOptions.presetSet) && !keyboardOptions.colorSet ) {
std::cerr << "Please set either a mode together with at least one region to change a color preset, or a color region to change rgb color." << std::endl;
return 1;
}else{
Keyboard k;
if(keyboardOptions.presetSet) {
for(int i = 0; i < keyboardOptions.presetOptions.count(); ++i) {
ColorPresetOption *presetOption = keyboardOptions.presetOptions.at(i);
k.setColorPreset(presetOption->region, presetOption->color, presetOption->intensity);
}
}
if(keyboardOptions.colorSet) {
for(int i = 0; i < keyboardOptions.colorOptions.count(); ++i) {
ColorOption *colorOption = keyboardOptions.colorOptions.at(i);
k.setColor(colorOption->region, colorOption->rgb);
}
}
if(keyboardOptions.modeSet) {
k.setMode(keyboardOptions.modeOption);
}
}
return 0;
}