-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
cli.cpp
1782 lines (1575 loc) · 66.2 KB
/
cli.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------*\
| cli.cpp |
| |
| OpenRGB command line interface |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#include <vector>
#include <cstring>
#include <string>
#include <tuple>
#include <iostream>
#include "AutoStart.h"
#include "filesystem.h"
#include "ProfileManager.h"
#include "ResourceManager.h"
#include "RGBController.h"
#include "i2c_smbus.h"
#include "NetworkClient.h"
#include "NetworkServer.h"
#include "LogManager.h"
#include "Colors.h"
/*-------------------------------------------------------------*\
| Quirk for MSVC; which doesn't support this case-insensitive |
| function |
\*-------------------------------------------------------------*/
#ifdef _WIN32
#include <shellapi.h>
#define strcasecmp strcmpi
#endif
using namespace std::chrono_literals;
static std::string profile_save_filename = "";
const unsigned int brightness_percentage = 100;
const unsigned int speed_percentage = 100;
static int preserve_argc = 0;
static char** preserve_argv = nullptr;
enum
{
RET_FLAG_PRINT_HELP = 1,
RET_FLAG_START_GUI = 2,
RET_FLAG_I2C_TOOLS = 4,
RET_FLAG_START_MINIMIZED = 8,
RET_FLAG_NO_DETECT = 16,
RET_FLAG_CLI_POST_DETECTION = 32,
RET_FLAG_START_SERVER = 64,
RET_FLAG_NO_AUTO_CONNECT = 128,
};
struct DeviceOptions
{
int device;
int zone = -1;
std::vector<std::tuple<unsigned char, unsigned char, unsigned char>> colors;
std::string mode;
unsigned int speed = 100;
unsigned int brightness = 100;
unsigned int size;
bool random_colors = false;
bool hasSize = false;
bool hasOption = false;
};
struct ServerOptions
{
bool start = false;
unsigned short port = OPENRGB_SDK_PORT;
};
struct Options
{
std::vector<DeviceOptions> devices;
/*---------------------------------------------------------*\
| If hasDevice is false, devices above is empty and |
| allDeviceOptions shall be applied to all available devices|
| except in the case that a profile was loaded. |
\*---------------------------------------------------------*/
bool hasDevice = false;
bool profile_loaded = false;
DeviceOptions allDeviceOptions;
ServerOptions servOpts;
};
/*---------------------------------------------------------------------------------------------------------*\
| Support a common subset of human colors; for easier typing: https://www.w3.org/TR/css-color-3/#svg-color |
\*---------------------------------------------------------------------------------------------------------*/
struct HumanColors { uint32_t rgb; const char* keyword; } static const human_colors[] =
{
{ COLOR_BLACK, "black" },
{ COLOR_NAVY, "navy" },
{ COLOR_DARKBLUE, "darkblue" },
{ COLOR_MEDIUMBLUE, "mediumblue" },
{ COLOR_BLUE, "blue" },
{ COLOR_DARKGREEN, "darkgreen" },
{ COLOR_GREEN, "green" },
{ COLOR_TEAL, "teal" },
{ COLOR_DARKCYAN, "darkcyan" },
{ COLOR_DEEPSKYBLUE, "deepskyblue" },
{ COLOR_DARKTURQUOISE, "darkturquoise" },
{ COLOR_MEDIUMSPRINGGREEN, "mediumspringgreen" },
{ COLOR_LIME, "lime" },
{ COLOR_SPRINGGREEN, "springgreen" },
{ COLOR_AQUA, "aqua" },
{ COLOR_CYAN, "cyan" },
{ COLOR_MIDNIGHTBLUE, "midnightblue" },
{ COLOR_DODGERBLUE, "dodgerblue" },
{ COLOR_LIGHTSEAGREEN, "lightseagreen" },
{ COLOR_FORESTGREEN, "forestgreen" },
{ COLOR_SEAGREEN, "seagreen" },
{ COLOR_DARKSLATEGRAY, "darkslategray" },
{ COLOR_DARKSLATEGREY, "darkslategrey" },
{ COLOR_LIMEGREEN, "limegreen" },
{ COLOR_MEDIUMSEAGREEN, "mediumseagreen" },
{ COLOR_TURQUOISE, "turquoise" },
{ COLOR_ROYALBLUE, "royalblue" },
{ COLOR_STEELBLUE, "steelblue" },
{ COLOR_DARKSLATEBLUE, "darkslateblue" },
{ COLOR_MEDIUMTURQUOISE, "mediumturquoise" },
{ COLOR_INDIGO, "indigo" },
{ COLOR_DARKOLIVEGREEN, "darkolivegreen" },
{ COLOR_CADETBLUE, "cadetblue" },
{ COLOR_CORNFLOWERBLUE, "cornflowerblue" },
{ COLOR_MEDIUMAQUAMARINE, "mediumaquamarine" },
{ COLOR_DIMGRAY, "dimgray" },
{ COLOR_DIMGREY, "dimgrey" },
{ COLOR_SLATEBLUE, "slateblue" },
{ COLOR_OLIVEDRAB, "olivedrab" },
{ COLOR_SLATEGRAY, "slategray" },
{ COLOR_SLATEGREY, "slategrey" },
{ COLOR_LIGHTSLATEGRAY, "lightslategray" },
{ COLOR_LIGHTSLATEGREY, "lightslategrey" },
{ COLOR_MEDIUMSLATEBLUE, "mediumslateblue" },
{ COLOR_LAWNGREEN, "lawngreen" },
{ COLOR_CHARTREUSE, "chartreuse" },
{ COLOR_AQUAMARINE, "aquamarine" },
{ COLOR_MAROON, "maroon" },
{ COLOR_PURPLE, "purple" },
{ COLOR_ELECTRIC_ULTRAMARINE, "electricultramarine" },
{ COLOR_OLIVE, "olive" },
{ COLOR_GRAY, "gray" },
{ COLOR_GREY, "grey" },
{ COLOR_SKYBLUE, "skyblue" },
{ COLOR_LIGHTSKYBLUE, "lightskyblue" },
{ COLOR_BLUEVIOLET, "blueviolet" },
{ COLOR_DARKRED, "darkred" },
{ COLOR_DARKMAGENTA, "darkmagenta" },
{ COLOR_SADDLEBROWN, "saddlebrown" },
{ COLOR_DARKSEAGREEN, "darkseagreen" },
{ COLOR_LIGHTGREEN, "lightgreen" },
{ COLOR_MEDIUMPURPLE, "mediumpurple" },
{ COLOR_DARKVIOLET, "darkviolet" },
{ COLOR_PALEGREEN, "palegreen" },
{ COLOR_DARKORCHID, "darkorchid" },
{ COLOR_YELLOWGREEN, "yellowgreen" },
{ COLOR_SIENNA, "sienna" },
{ COLOR_BROWN, "brown" },
{ COLOR_DARKGRAY, "darkgray" },
{ COLOR_DARKGREY, "darkgrey" },
{ COLOR_LIGHTBLUE, "lightblue" },
{ COLOR_GREENYELLOW, "greenyellow" },
{ COLOR_PALETURQUOISE, "paleturquoise" },
{ COLOR_LIGHTSTEELBLUE, "lightsteelblue" },
{ COLOR_POWDERBLUE, "powderblue" },
{ COLOR_FIREBRICK, "firebrick" },
{ COLOR_DARKGOLDENROD, "darkgoldenrod" },
{ COLOR_MEDIUMORCHID, "mediumorchid" },
{ COLOR_ROSYBROWN, "rosybrown" },
{ COLOR_DARKKHAKI, "darkkhaki" },
{ COLOR_SILVER, "silver" },
{ COLOR_MEDIUMVIOLETRED, "mediumvioletred" },
{ COLOR_INDIANRED, "indianred" },
{ COLOR_PERU, "peru" },
{ COLOR_CHOCOLATE, "chocolate" },
{ COLOR_TAN, "tan" },
{ COLOR_LIGHTGRAY, "lightgray" },
{ COLOR_LIGHTGREY, "lightgrey" },
{ COLOR_THISTLE, "thistle" },
{ COLOR_ORCHID, "orchid" },
{ COLOR_GOLDENROD, "goldenrod" },
{ COLOR_PALEVIOLETRED, "palevioletred" },
{ COLOR_CRIMSON, "crimson" },
{ COLOR_GAINSBORO, "gainsboro" },
{ COLOR_PLUM, "plum" },
{ COLOR_BURLYWOOD, "burlywood" },
{ COLOR_LIGHTCYAN, "lightcyan" },
{ COLOR_LAVENDER, "lavender" },
{ COLOR_DARKSALMON, "darksalmon" },
{ COLOR_VIOLET, "violet" },
{ COLOR_PALEGOLDENROD, "palegoldenrod" },
{ COLOR_LIGHTCORAL, "lightcoral" },
{ COLOR_KHAKI, "khaki" },
{ COLOR_ALICEBLUE, "aliceblue" },
{ COLOR_HONEYDEW, "honeydew" },
{ COLOR_AZURE, "azure" },
{ COLOR_SANDYBROWN, "sandybrown" },
{ COLOR_WHEAT, "wheat" },
{ COLOR_BEIGE, "beige" },
{ COLOR_WHITESMOKE, "whitesmoke" },
{ COLOR_MINTCREAM, "mintcream" },
{ COLOR_GHOSTWHITE, "ghostwhite" },
{ COLOR_SALMON, "salmon" },
{ COLOR_ANTIQUEWHITE, "antiquewhite" },
{ COLOR_LINEN, "linen" },
{ COLOR_LIGHTGOLDENRODYELLOW, "lightgoldenrodyellow" },
{ COLOR_OLDLACE, "oldlace" },
{ COLOR_RED, "red" },
{ COLOR_FUCHSIA, "fuchsia" },
{ COLOR_MAGENTA, "magenta" },
{ COLOR_DEEPPINK, "deeppink" },
{ COLOR_ORANGERED, "orangered" },
{ COLOR_TOMATO, "tomato" },
{ COLOR_HOTPINK, "hotpink" },
{ COLOR_CORAL, "coral" },
{ COLOR_DARKORANGE, "darkorange" },
{ COLOR_LIGHTSALMON, "lightsalmon" },
{ COLOR_ORANGE, "orange" },
{ COLOR_LIGHTPINK, "lightpink" },
{ COLOR_PINK, "pink" },
{ COLOR_GOLD, "gold" },
{ COLOR_PEACHPUFF, "peachpuff" },
{ COLOR_NAVAJOWHITE, "navajowhite" },
{ COLOR_MOCCASIN, "moccasin" },
{ COLOR_BISQUE, "bisque" },
{ COLOR_MISTYROSE, "mistyrose" },
{ COLOR_BLANCHEDALMOND, "blanchedalmond" },
{ COLOR_PAPAYAWHIP, "papayawhip" },
{ COLOR_LAVENDERBLUSH, "lavenderblush" },
{ COLOR_SEASHELL, "seashell" },
{ COLOR_CORNSILK, "cornsilk" },
{ COLOR_LEMONCHIFFON, "lemonchiffon" },
{ COLOR_FLORALWHITE, "floralwhite" },
{ COLOR_SNOW, "snow" },
{ COLOR_YELLOW, "yellow" },
{ COLOR_LIGHTYELLOW, "lightyellow" },
{ COLOR_IVORY, "ivory" },
{ COLOR_WHITE, "white" },
{ 0, NULL }
};
bool ParseColors(std::string colors_string, DeviceOptions *options)
{
while (colors_string.length() > 0)
{
size_t rgb_end = colors_string.find_first_of(',');
std::string color = colors_string.substr(0, rgb_end);
int32_t rgb = 0;
bool parsed = false;
if (color.length() <= 0)
break;
/*-----------------------------------------------------------------*\
| This will set correct colour mode for modes with a |
| MODE_COLORS_RANDOM else generate a random colour from the |
| human_colors list above |
\*-----------------------------------------------------------------*/
if (color == "random")
{
options->random_colors = true;
srand((unsigned int)time(NULL));
int index = rand() % (sizeof(human_colors) / sizeof(human_colors[0])) + 1; //Anything other than black
rgb = human_colors[index].rgb;
parsed = true;
}
else
{
/* swy: (A) try interpreting it as text; as human keywords, otherwise strtoul() will pick up 'darkgreen' as 0xDA */
for (const struct HumanColors *hc = human_colors; hc->keyword != NULL; hc++)
{
if (strcasecmp(hc->keyword, color.c_str()) != 0)
continue;
rgb = hc->rgb; parsed = true;
break;
}
}
/* swy: (B) no luck, try interpreting it as an hexadecimal number instead */
if (!parsed)
{
if (color.length() == 6)
{
const char *colorptr = color.c_str(); char *endptr = NULL;
rgb = strtoul(colorptr, &endptr, 16);
/* swy: check that strtoul() has advanced the read pointer until the end (NULL terminator);
that means it has read the whole thing */
if (colorptr != endptr && endptr && *endptr == '\0')
parsed = true;
}
}
/* swy: we got it, save the 32-bit integer as a tuple of three RGB bytes */
if (parsed)
{
options->colors.push_back(std::make_tuple(
(rgb >> (8 * 2)) & 0xFF, /* RR.... */
(rgb >> (8 * 1)) & 0xFF, /* ..GG.. */
(rgb >> (8 * 0)) & 0xFF /* ....BB */
));
}
else
{
std::cout << "Error: Unknown color: '" + color + "', skipping." << std::endl;
}
// If there are no more colors
if (rgb_end == std::string::npos)
break;
// Remove the current color and the next color's leading comma
colors_string = colors_string.substr(color.length() + 1);
}
return options->colors.size() > 0;
}
unsigned int ParseMode(DeviceOptions& options, std::vector<RGBController *> &rgb_controllers)
{
// no need to check if --mode wasn't passed
if (options.mode.size() == 0)
{
return rgb_controllers[options.device]->active_mode;
}
/*---------------------------------------------------------*\
| Search through all of the device modes and see if there is|
| a match. If no match is found, print an error message. |
\*---------------------------------------------------------*/
for(unsigned int mode_idx = 0; mode_idx < rgb_controllers[options.device]->modes.size(); mode_idx++)
{
if (strcasecmp(rgb_controllers[options.device]->modes[mode_idx].name.c_str(), options.mode.c_str()) == 0)
{
return mode_idx;
}
}
std::cout << "Error: Mode '" + options.mode + "' not available for device '" + rgb_controllers[options.device]->name + "'" << std::endl;
return false;
}
DeviceOptions* GetDeviceOptionsForDevID(Options *opts, int device)
{
if (device == -1)
{
return &opts->allDeviceOptions;
}
for (unsigned int i = 0; i < opts->devices.size(); i++)
{
if (opts->devices[i].device == device)
{
return &opts->devices[i];
}
}
// should never happen
std::cout << "Internal error: Tried setting an option on a device that wasn't specified" << std::endl;
abort();
}
std::string QuoteIfNecessary(std::string str)
{
if (str.find(' ') == std::string::npos)
{
return str;
}
else
{
return "'" + str + "'";
}
}
/*---------------------------------------------------------------------------------------------------------*\
| Option processing functions |
\*---------------------------------------------------------------------------------------------------------*/
void OptionHelp()
{
std::string help_text;
help_text += "OpenRGB ";
help_text += VERSION_STRING;
help_text += ", for controlling RGB lighting.\n";
help_text += "Usage: OpenRGB (--device [--mode] [--color])...\n";
help_text += "\n";
help_text += "Options:\n";
help_text += "--gui Shows the GUI. GUI also appears when not passing any parameters\n";
help_text += "--startminimized Starts the GUI minimized to tray. Implies --gui, even if not specified\n";
help_text += "--client [IP]:[Port] Starts an SDK client on the given IP:Port (assumes port 6742 if not specified)\n";
help_text += "--server Starts the SDK's server\n";
help_text += "--server-host Sets the SDK's server host. Default: 0.0.0.0 (all network interfaces)\n";
help_text += "--server-port Sets the SDK's server port. Default: 6742 (1024-65535)\n";
help_text += "-l, --list-devices Lists every compatible device with their number\n";
help_text += "-d, --device [0-9 | \"name\"] Selects device to apply colors and/or effect to, or applies to all devices if omitted\n";
help_text += " Basic string search is implemented 3 characters or more\n";
help_text += " Can be specified multiple times with different modes and colors\n";
help_text += "-z, --zone [0-9] Selects zone to apply colors and/or sizes to, or applies to all zones in device if omitted\n";
help_text += " Must be specified after specifying a device\n";
help_text += "-c, --color [random | FFFFF,00AAFF ...] Sets colors on each device directly if no effect is specified, and sets the effect color if an effect is specified\n";
help_text += " If there are more LEDs than colors given, the last color will be applied to the remaining LEDs\n";
help_text += "-m, --mode [breathing | static | ...] Sets the mode to be applied, check --list-devices to see which modes are supported on your device\n";
help_text += "-b, --brightness [0-100] Sets the brightness as a percentage if the mode supports brightness\n";
help_text += "-s, --speed [0-100] Sets the speed as a percentage if the mode supports speed\n";
help_text += "-sz, --size [0-N] Sets the new size of the specified device zone.\n";
help_text += " Must be specified after specifying a zone.\n";
help_text += " If the specified size is out of range, or the zone does not offer resizing capability, the size will not be changed\n";
help_text += "-V, --version Display version and software build information\n";
help_text += "-p, --profile filename[.orp] Load the profile from filename/filename.orp\n";
help_text += "-sp, --save-profile filename.orp Save the given settings to profile filename.orp\n";
help_text += "--i2c-tools Shows the I2C/SMBus Tools page in the GUI. Implies --gui, even if not specified.\n";
help_text += " USE I2C TOOLS AT YOUR OWN RISK! Don't use this option if you don't know what you're doing!\n";
help_text += " There is a risk of bricking your motherboard, RGB controller, and RAM if you send invalid SMBus/I2C transactions.\n";
help_text += "--localconfig Use the current working directory instead of the global configuration directory.\n";
help_text += "--config path Use a custom path instead of the global configuration directory.\n";
help_text += "--nodetect Do not try to detect hardware at startup.\n";
help_text += "--noautoconnect Do not try to autoconnect to a local server at startup.\n";
help_text += "--loglevel [0-6 | error | warning ...] Set the log level (0: fatal to 6: trace).\n";
help_text += "--print-source Print the source code file and line number for each log entry.\n";
help_text += "-v, --verbose Print log messages to stdout.\n";
help_text += "-vv, --very-verbose Print debug messages and log messages to stdout.\n";
help_text += "--autostart-check Check if OpenRGB starting at login is enabled.\n";
help_text += "--autostart-disable Disable OpenRGB starting at login.\n";
help_text += "--autostart-enable arguments Enable OpenRGB to start at login. Requires arguments to give to OpenRGB at login.\n";
std::cout << help_text << std::endl;
}
void OptionVersion()
{
std::string version_text;
version_text += "OpenRGB ";
version_text += VERSION_STRING;
version_text += ", for controlling RGB lighting.\n";
version_text += " Version:\t\t ";
version_text += VERSION_STRING;
version_text += "\n Build Date\t\t ";
version_text += BUILDDATE_STRING;
version_text += "\n Git Commit ID\t\t ";
version_text += GIT_COMMIT_ID;
version_text += "\n Git Commit Date\t ";
version_text += GIT_COMMIT_DATE;
version_text += "\n Git Branch\t\t ";
version_text += GIT_BRANCH;
version_text += "\n";
std::cout << version_text << std::endl;
}
void OptionListDevices(std::vector<RGBController *>& rgb_controllers)
{
ResourceManager::get()->WaitForDeviceDetection();
for(std::size_t controller_idx = 0; controller_idx < rgb_controllers.size(); controller_idx++)
{
RGBController *controller = rgb_controllers[controller_idx];
/*---------------------------------------------------------*\
| Print device name |
\*---------------------------------------------------------*/
std::cout << controller_idx << ": " << controller->name << std::endl;
/*---------------------------------------------------------*\
| Print device type |
\*---------------------------------------------------------*/
std::cout << " Type: " << device_type_to_str(controller->type) << std::endl;
/*---------------------------------------------------------*\
| Print device description |
\*---------------------------------------------------------*/
if(!controller->description.empty())
{
std::cout << " Description: " << controller->description << std::endl;
}
/*---------------------------------------------------------*\
| Print device version |
\*---------------------------------------------------------*/
if(!controller->version.empty())
{
std::cout << " Version: " << controller->version << std::endl;
}
/*---------------------------------------------------------*\
| Print device location |
\*---------------------------------------------------------*/
if(!controller->location.empty())
{
std::cout << " Location: " << controller->location << std::endl;
}
/*---------------------------------------------------------*\
| Print device serial |
\*---------------------------------------------------------*/
if(!controller->serial.empty())
{
std::cout << " Serial: " << controller->serial << std::endl;
}
/*---------------------------------------------------------*\
| Print device modes |
\*---------------------------------------------------------*/
if(!controller->modes.empty())
{
std::cout << " Modes:";
int current_mode = controller->GetMode();
for(std::size_t mode_idx = 0; mode_idx < controller->modes.size(); mode_idx++)
{
std::string modeStr = QuoteIfNecessary(controller->modes[mode_idx].name);
if(current_mode == (int)mode_idx)
{
modeStr = "[" + modeStr + "]";
}
std::cout << " " << modeStr;
}
std::cout << std::endl;
}
/*---------------------------------------------------------*\
| Print device zones |
\*---------------------------------------------------------*/
if(!controller->zones.empty())
{
std::cout << " Zones:";
for(std::size_t zone_idx = 0; zone_idx < controller->zones.size(); zone_idx++)
{
std::cout << " " << QuoteIfNecessary(controller->zones[zone_idx].name);
}
std::cout << std::endl;
}
/*---------------------------------------------------------*\
| Print device LEDs |
\*---------------------------------------------------------*/
if(!controller->leds.empty())
{
std::cout << " LEDs:";
for(std::size_t led_idx = 0; led_idx < controller->leds.size(); led_idx++)
{
std::cout << " " << QuoteIfNecessary(controller->leds[led_idx].name);
}
std::cout << std::endl;
}
std::cout << std::endl;
}
}
bool OptionDevice(std::vector<DeviceOptions>* current_devices, std::string argument, Options* options, std::vector<RGBController *>& rgb_controllers)
{
bool found = false;
ResourceManager::get()->WaitForDeviceDetection();
try
{
int current_device = std::stoi(argument);
if((current_device >= static_cast<int>(rgb_controllers.size())) || (current_device < 0))
{
throw nullptr;
}
DeviceOptions newDev;
newDev.device = current_device;
if(!options->hasDevice)
{
options->hasDevice = true;
}
current_devices->push_back(newDev);
found = true;
}
catch(...)
{
if(argument.length() > 1)
{
for(unsigned int i = 0; i < rgb_controllers.size(); i++)
{
/*---------------------------------------------------------*\
| If the argument is not a number then check all the |
| controllers names for a match |
\*---------------------------------------------------------*/
std::string name = rgb_controllers[i]->name;
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
std::transform(argument.begin(), argument.end(), argument.begin(), ::tolower);
if(name.find(argument) != std::string::npos)
{
found = true;
DeviceOptions newDev;
newDev.device = i;
if(!options->hasDevice)
{
options->hasDevice = true;
}
current_devices->push_back(newDev);
}
}
}
else
{
std::cout << "Error: Invalid device ID: " + argument << std::endl;
return false;
}
}
return found;
}
bool OptionZone(std::vector<DeviceOptions>* current_devices, std::string argument, Options* /*options*/, std::vector<RGBController *>& rgb_controllers)
{
bool found = false;
ResourceManager::get()->WaitForDeviceDetection();
try
{
int current_zone = std::stoi(argument);
for(size_t i = 0; i < current_devices->size(); i++)
{
int current_device = current_devices->at(i).device;
if(current_zone >= static_cast<int>(rgb_controllers[current_device]->zones.size()) || (current_zone < 0))
{
throw nullptr;
}
current_devices->at(i).zone = current_zone;
found = true;
}
}
catch(...)
{
std::cout << "Error: Invalid zone ID: " + argument << std::endl;
return false;
}
return found;
}
bool CheckColor(std::string argument, DeviceOptions* currentDevOpts)
{
if(ParseColors(argument, currentDevOpts))
{
currentDevOpts->hasOption = true;
return true;
}
else
{
std::cout << "Error: Invalid color value: " + argument << std::endl;
return false;
}
}
bool OptionColor(std::vector<DeviceOptions>* current_devices, std::string argument, Options* options)
{
/*---------------------------------------------------------*\
| If a device is not selected i.e. size() == 0 |
| then add color to allDeviceOptions |
\*---------------------------------------------------------*/
bool found = false;
DeviceOptions* currentDevOpts = &options->allDeviceOptions;
if(current_devices->size() == 0)
{
found = CheckColor(argument, currentDevOpts);
}
else
{
for(size_t i = 0; i < current_devices->size(); i++)
{
currentDevOpts = ¤t_devices->at(i);
found = CheckColor(argument, currentDevOpts);
}
}
return found;
}
bool OptionMode(std::vector<DeviceOptions>* current_devices, std::string argument, Options* options)
{
if(argument.size() == 0)
{
std::cout << "Error: --mode passed with no argument" << std::endl;
return false;
}
/*---------------------------------------------------------*\
| If a device is not selected i.e. size() == 0 |
| then add mode to allDeviceOptions |
\*---------------------------------------------------------*/
bool found = false;
DeviceOptions* currentDevOpts = &options->allDeviceOptions;
if(current_devices->size() == 0)
{
currentDevOpts->mode = argument;
currentDevOpts->hasOption = true;
found = true;
}
else
{
for(size_t i = 0; i < current_devices->size(); i++)
{
currentDevOpts = ¤t_devices->at(i);
currentDevOpts->mode = argument;
currentDevOpts->hasOption = true;
found = true;
}
}
return found;
}
bool OptionSpeed(std::vector<DeviceOptions>* current_devices, std::string argument, Options* options)
{
if(argument.size() == 0)
{
std::cout << "Error: --speed passed with no argument" << std::endl;
return false;
}
/*---------------------------------------------------------*\
| If a device is not selected i.e. size() == 0 |
| then add speed to allDeviceOptions |
\*---------------------------------------------------------*/
bool found = false;
DeviceOptions* currentDevOpts = &options->allDeviceOptions;
if(current_devices->size() == 0)
{
currentDevOpts->speed = std::min(std::max(std::stoi(argument), 0),(int)speed_percentage);
currentDevOpts->hasOption = true;
found = true;
}
else
{
for(size_t i = 0; i < current_devices->size(); i++)
{
DeviceOptions* currentDevOpts = ¤t_devices->at(i);
currentDevOpts->speed = std::min(std::max(std::stoi(argument), 0),(int)speed_percentage);
currentDevOpts->hasOption = true;
found = true;
}
}
return found;
}
bool OptionBrightness(std::vector<DeviceOptions>* current_devices, std::string argument, Options* options)
{
if(argument.size() == 0)
{
std::cout << "Error: --brightness passed with no argument" << std::endl;
return false;
}
/*---------------------------------------------------------*\
| If a device is not selected i.e. size() == 0 |
| then add brightness to allDeviceOptions |
\*---------------------------------------------------------*/
bool found = false;
DeviceOptions* currentDevOpts = &options->allDeviceOptions;
if(current_devices->size() == 0)
{
currentDevOpts->brightness = std::min(std::max(std::stoi(argument), 0),(int)brightness_percentage);
currentDevOpts->hasOption = true;
found = true;
}
else
{
for(size_t i = 0; i < current_devices->size(); i++)
{
DeviceOptions* currentDevOpts = ¤t_devices->at(i);
currentDevOpts->brightness = std::min(std::max(std::stoi(argument), 0),(int)brightness_percentage);
currentDevOpts->hasOption = true;
found = true;
}
}
return found;
}
bool OptionSize(std::vector<DeviceOptions>* current_devices, std::string argument, Options* /*options*/, std::vector<RGBController *>& rgb_controllers)
{
const unsigned int new_size = std::stoi(argument);
ResourceManager::get()->WaitForDeviceDetection();
for(size_t i = 0; i < current_devices->size(); i++)
{
int current_device = current_devices->at(i).device;
int current_zone = current_devices->at(i).zone;
/*---------------------------------------------------------*\
| Fail out if device, zone, or size are out of range |
\*---------------------------------------------------------*/
if((current_device >= static_cast<int>(rgb_controllers.size())) || (current_device < 0))
{
std::cout << "Error: Device is out of range" << std::endl;
return false;
}
else if((current_zone >= static_cast<int>(rgb_controllers[current_device]->zones.size())) || (current_zone < 0))
{
std::cout << "Error: Zone is out of range" << std::endl;
return false;
}
else if((new_size < rgb_controllers[current_device]->zones[current_zone].leds_min) || (new_size > rgb_controllers[current_device]->zones[current_zone].leds_max))
{
std::cout << "Error: New size is out of range" << std::endl;
}
/*---------------------------------------------------------*\
| Resize the zone |
\*---------------------------------------------------------*/
rgb_controllers[current_device]->ResizeZone(current_zone, new_size);
/*---------------------------------------------------------*\
| Save the profile |
\*---------------------------------------------------------*/
ResourceManager::get()->GetProfileManager()->SaveProfile("sizes", true);
}
return true;
}
bool OptionProfile(std::string argument, std::vector<RGBController *>& rgb_controllers)
{
ResourceManager::get()->WaitForDeviceDetection();
/*---------------------------------------------------------*\
| Attempt to load profile |
\*---------------------------------------------------------*/
if(ResourceManager::get()->GetProfileManager()->LoadProfile(argument))
{
/*-----------------------------------------------------*\
| Change device mode if profile loading was successful |
\*-----------------------------------------------------*/
for(std::size_t controller_idx = 0; controller_idx < rgb_controllers.size(); controller_idx++)
{
RGBController* device = rgb_controllers[controller_idx];
device->DeviceUpdateMode();
LOG_DEBUG("Updating mode for %s to %i", device->name.c_str(), device->active_mode);
if(device->modes[device->active_mode].color_mode == MODE_COLORS_PER_LED)
{
device->DeviceUpdateLEDs();
LOG_DEBUG("Mode uses per-LED color, also updating LEDs");
}
}
std::cout << "Profile loaded successfully" << std::endl;
return true;
}
else
{
std::cout << "Profile failed to load" << std::endl;
return false;
}
}
bool OptionSaveProfile(std::string argument)
{
/*---------------------------------------------------------*\
| Set save profile filename |
\*---------------------------------------------------------*/
profile_save_filename = argument;
return(true);
}
int ProcessOptions(Options* options, std::vector<RGBController *>& rgb_controllers)
{
unsigned int ret_flags = 0;
int arg_index = 1;
std::vector<DeviceOptions> current_devices;
options->hasDevice = false;
options->profile_loaded = false;
#ifdef _WIN32
int fake_argc;
wchar_t** argvw = CommandLineToArgvW(GetCommandLineW(), &fake_argc);
#endif
while(arg_index < preserve_argc)
{
std::string option = preserve_argv[arg_index];
std::string argument = "";
filesystem::path arg_path;
/*---------------------------------------------------------*\
| Handle options that take an argument |
\*---------------------------------------------------------*/
if(arg_index + 1 < preserve_argc)
{
argument = preserve_argv[arg_index + 1];
#ifdef _WIN32
arg_path = argvw[arg_index + 1];
#else
arg_path = argument;
#endif
}
/*---------------------------------------------------------*\
| -l / --list-devices (no arguments) |
\*---------------------------------------------------------*/
if(option == "--list-devices" || option == "-l")
{
OptionListDevices(rgb_controllers);
exit(0);
}
/*---------------------------------------------------------*\
| -d / --device |
\*---------------------------------------------------------*/
else if(option == "--device" || option == "-d")
{
while(!current_devices.empty())
{
options->devices.push_back(current_devices.back());
current_devices.pop_back();
}
if(!OptionDevice(¤t_devices, argument, options, rgb_controllers))
{
return RET_FLAG_PRINT_HELP;
}
arg_index++;
}
/*---------------------------------------------------------*\
| -z / --zone |
\*---------------------------------------------------------*/
else if(option == "--zone" || option == "-z")
{
if(!OptionZone(¤t_devices, argument, options, rgb_controllers))
{
return RET_FLAG_PRINT_HELP;
}
arg_index++;
}
/*---------------------------------------------------------*\
| -c / --color |
\*---------------------------------------------------------*/
else if(option == "--color" || option == "-c")
{
if(!OptionColor(¤t_devices, argument, options))
{
return RET_FLAG_PRINT_HELP;
}
arg_index++;
}
/*---------------------------------------------------------*\
| -m / --mode |
\*---------------------------------------------------------*/
else if(option == "--mode" || option == "-m")
{
if(!OptionMode(¤t_devices, argument, options))
{
return RET_FLAG_PRINT_HELP;
}
arg_index++;
}
/*---------------------------------------------------------*\
| -b / --brightness |
\*---------------------------------------------------------*/
else if(option == "--brightness" || option == "-b")
{
if(!OptionBrightness(¤t_devices, argument, options))