From 8121ee29f46d58f90a56348eb5bf7a64f52f923b Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Wed, 19 Aug 2026 23:48:18 -0500 Subject: [PATCH] Clean up the CLI code --- cli.cpp | 1154 ++++++++++++++++++++++++++++--------------------------- cli.h | 34 +- 2 files changed, 614 insertions(+), 574 deletions(-) diff --git a/cli.cpp b/cli.cpp index 37bc469e8..2e506e054 100644 --- a/cli.cpp +++ b/cli.cpp @@ -13,20 +13,19 @@ #include #include #include "AutoStart.h" +#include "cli.h" +#include "Colors.h" #include "filesystem.h" +#include "LogManager.h" +#include "NetworkClient.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 | -\*-------------------------------------------------------------*/ +/*---------------------------------------------------------*\ +| Quirk for MSVC; which doesn't support this | +| case-insensitive function | +\*---------------------------------------------------------*/ #ifdef _WIN32 #include #define strcasecmp _strcmpi @@ -34,25 +33,16 @@ 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, -}; +/*---------------------------------------------------------*\ +| Static variables for CLI | +\*---------------------------------------------------------*/ +static std::string profile_save_name = ""; +static int preserve_argc = 0; +static char** preserve_argv = nullptr; +/*---------------------------------------------------------*\ +| Options for configuring devices from CLI | +\*---------------------------------------------------------*/ struct DeviceOptions { int device; @@ -69,239 +59,260 @@ struct DeviceOptions struct ServerOptions { - bool start = false; - unsigned short port = OPENRGB_SDK_PORT; + bool start = false; + unsigned short port = OPENRGB_SDK_PORT; }; struct Options { std::vector 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; + /*-----------------------------------------------------*\ + | 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 | -\*---------------------------------------------------------------------------------------------------------*/ +/*---------------------------------------------------------*\ +| 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 } + { 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_AQUA, "aqua" }, + { COLOR_SPRINGGREEN, "springgreen" }, + { COLOR_MIDNIGHTBLUE, "midnightblue" }, + { COLOR_CYAN, "cyan" }, + { COLOR_LIGHTSEAGREEN, "lightseagreen" }, + { COLOR_DODGERBLUE, "dodgerblue" }, + { COLOR_SEAGREEN, "seagreen" }, + { COLOR_FORESTGREEN, "forestgreen" }, + { COLOR_DARKSLATEGREY, "darkslategrey" }, + { COLOR_DARKSLATEGRAY, "darkslategray" }, + { COLOR_MEDIUMSEAGREEN, "mediumseagreen" }, + { COLOR_LIMEGREEN, "limegreen" }, + { COLOR_ROYALBLUE, "royalblue" }, + { COLOR_TURQUOISE, "turquoise" }, + { COLOR_DARKSLATEBLUE, "darkslateblue" }, + { COLOR_STEELBLUE, "steelblue" }, + { COLOR_INDIGO, "indigo" }, + { COLOR_MEDIUMTURQUOISE, "mediumturquoise" }, + { COLOR_CADETBLUE, "cadetblue" }, + { COLOR_DARKOLIVEGREEN, "darkolivegreen" }, + { COLOR_MEDIUMAQUAMARINE, "mediumaquamarine" }, + { COLOR_CORNFLOWERBLUE, "cornflowerblue" }, + { COLOR_DIMGREY, "dimgrey" }, + { COLOR_DIMGRAY, "dimgray" }, + { COLOR_OLIVEDRAB, "olivedrab" }, + { COLOR_SLATEBLUE, "slateblue" }, + { COLOR_SLATEGREY, "slategrey" }, + { COLOR_SLATEGRAY, "slategray" }, + { COLOR_LIGHTSLATEGREY, "lightslategrey" }, + { COLOR_LIGHTSLATEGRAY, "lightslategray" }, + { COLOR_LAWNGREEN, "lawngreen" }, + { COLOR_MEDIUMSLATEBLUE, "mediumslateblue" }, + { COLOR_AQUAMARINE, "aquamarine" }, + { COLOR_CHARTREUSE, "chartreuse" }, + { COLOR_PURPLE, "purple" }, + { COLOR_MAROON, "maroon" }, + { 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) + 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; + 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; - 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") + if(color.length() <= 0) { - options->random_colors = true; + break; + } + + /*-------------------------------------------------*\ + | This will set correct color mode for modes with | + | a MODE_COLORS_RANDOM else generate a random color | + | 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; + 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++) + /*---------------------------------------------*\ + | 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) + if(strcasecmp(hc->keyword, color.c_str()) != 0) + { continue; + } - rgb = hc->rgb; parsed = true; + rgb = hc->rgb; parsed = true; break; } } - /* swy: (B) no luck, try interpreting it as an hexadecimal number instead */ - if (!parsed) + /*-------------------------------------------------*\ + | No luck, try interpreting it as an hexadecimal | + | number instead | + \*-------------------------------------------------*/ + if(!parsed) { - if (color.length() == 6) + if(color.length() == 6) { - const char *colorptr = color.c_str(); char *endptr = NULL; + const char* colorptr = color.c_str(); + char* endptr = NULL; - rgb = strtoul(colorptr, &endptr, 16); + 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; + /*-----------------------------------------*\ + | 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) + /*-------------------------------------------------*\ + | 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.... */ @@ -314,12 +325,19 @@ bool ParseColors(std::string colors_string, DeviceOptions *options) std::cout << "Error: Unknown color: '" + color + "', skipping." << std::endl; } - // If there are no more colors - if (rgb_end == std::string::npos) + /*-------------------------------------------------*\ + | 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); + /*-------------------------------------------------*\ + | Remove the current color and the next color's | + | leading comma | + \*-------------------------------------------------*/ + colors_string = colors_string.substr(color.length() + 1); } return options->colors.size() > 0; @@ -327,16 +345,19 @@ bool ParseColors(std::string colors_string, DeviceOptions *options) unsigned int ParseMode(DeviceOptions& options, std::vector &rgb_controllers) { - // no need to check if --mode wasn't passed + /*-----------------------------------------------------*\ + | No need to check if --mode wasn't passed | + \*-----------------------------------------------------*/ if(options.mode.size() == 0) { return rgb_controllers[options.device]->GetActiveMode(); } - /*---------------------------------------------------------*\ - | Search through all of the device modes and see if there is| - | a match. If no match is found, print an error message. | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | 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]->GetModeCount(); mode_idx++) { if(strcasecmp(rgb_controllers[options.device]->GetModeName(mode_idx).c_str(), options.mode.c_str()) == 0) @@ -351,12 +372,12 @@ unsigned int ParseMode(DeviceOptions& options, std::vector &rgb DeviceOptions* GetDeviceOptionsForDevID(Options *opts, int device) { - if (device == -1) + if(device == -1) { return &opts->allDeviceOptions; } - for (unsigned int i = 0; i < opts->devices.size(); i++) + for(unsigned int i = 0; i < opts->devices.size(); i++) { if (opts->devices[i].device == device) { @@ -364,14 +385,13 @@ DeviceOptions* GetDeviceOptionsForDevID(Options *opts, int device) } } - // 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) + if(str.find(' ') == std::string::npos) { return str; } @@ -381,10 +401,9 @@ std::string QuoteIfNecessary(std::string str) } } -/*---------------------------------------------------------------------------------------------------------*\ -| Option processing functions | -\*---------------------------------------------------------------------------------------------------------*/ - +/*---------------------------------------------------------*\ +| Option processing functions | +\*---------------------------------------------------------*/ void OptionHelp() { std::string help_text; @@ -466,9 +485,9 @@ void OptionListDevices(std::vector& rgb_controllers, bool detai { RGBController *controller = rgb_controllers[controller_idx]; - /*---------------------------------------------------------*\ - | Print device name | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device name | + \*-------------------------------------------------*/ std::cout << controller_idx << ": " << controller->GetDisplayName() << std::endl; if(!detailed) @@ -476,46 +495,46 @@ void OptionListDevices(std::vector& rgb_controllers, bool detai continue; } - /*---------------------------------------------------------*\ - | Print device type | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device type | + \*-------------------------------------------------*/ std::cout << " Type: " << RGBController::DeviceTypeToString(controller->GetDeviceType()) << std::endl; - /*---------------------------------------------------------*\ - | Print device description | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device description | + \*-------------------------------------------------*/ if(!controller->GetDescription().empty()) { std::cout << " Description: " << controller->GetDescription() << std::endl; } - /*---------------------------------------------------------*\ - | Print device version | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device version | + \*-------------------------------------------------*/ if(!controller->GetVersion().empty()) { std::cout << " Version: " << controller->GetVersion() << std::endl; } - /*---------------------------------------------------------*\ - | Print device location | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device location | + \*-------------------------------------------------*/ if(!controller->GetLocation().empty()) { std::cout << " Location: " << controller->GetLocation() << std::endl; } - /*---------------------------------------------------------*\ - | Print device serial | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device serial | + \*-------------------------------------------------*/ if(!controller->GetSerial().empty()) { std::cout << " Serial: " << controller->GetSerial() << std::endl; } - /*---------------------------------------------------------*\ - | Print device modes | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device modes | + \*-------------------------------------------------*/ if(controller->GetModeCount() > 0) { std::cout << " Modes:"; @@ -534,9 +553,9 @@ void OptionListDevices(std::vector& rgb_controllers, bool detai std::cout << std::endl; } - /*---------------------------------------------------------*\ - | Print device zones | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device zones | + \*-------------------------------------------------*/ if(controller->GetZoneCount() > 0) { std::cout << " Zones:"; @@ -548,9 +567,9 @@ void OptionListDevices(std::vector& rgb_controllers, bool detai std::cout << std::endl; } - /*---------------------------------------------------------*\ - | Print device LEDs | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print device LEDs | + \*-------------------------------------------------*/ if(controller->GetLEDCount() > 0) { std::cout << " LEDs:"; @@ -580,9 +599,9 @@ void OptionListProfiles() for(std::size_t profile_idx = 0; profile_idx < profile_list.size(); profile_idx++) { - /*---------------------------------------------------------*\ - | Print profile name | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Print profile name | + \*-------------------------------------------------*/ if(profile_list[profile_idx] == active_profile) { std::cout << "*" << profile_idx << ": " << profile_list[profile_idx] << std::endl; @@ -633,10 +652,11 @@ bool OptionDevice(std::vector* current_devices, std::string argum 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 | - \*---------------------------------------------------------*/ + /*-----------------------------------------*\ + | If the argument is not a number then | + | check all the controllers names for a | + | match | + \*-----------------------------------------*/ std::string name = rgb_controllers[i]->GetDisplayName(); std::transform(name.begin(), name.end(), name.begin(), ::tolower); LOG_TRACE("[CLI] Comparing to %s", name.c_str()); @@ -719,10 +739,10 @@ bool CheckColor(std::string argument, DeviceOptions* currentDevOpts) bool OptionColor(std::vector* current_devices, std::string argument, Options* options) { - /*---------------------------------------------------------*\ - | If a device is not selected i.e. size() == 0 | - | then add color to allDeviceOptions | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | If a device is not selected i.e. size() == 0 then | + | add color to allDeviceOptions | + \*-----------------------------------------------------*/ bool found = false; DeviceOptions* currentDevOpts = &options->allDeviceOptions; @@ -751,10 +771,10 @@ bool OptionMode(std::vector* current_devices, std::string argumen return false; } - /*---------------------------------------------------------*\ - | If a device is not selected i.e. size() == 0 | - | then add mode to allDeviceOptions | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | If a device is not selected i.e. size() == 0 then | + | add mode to allDeviceOptions | + \*-----------------------------------------------------*/ bool found = false; DeviceOptions* currentDevOpts = &options->allDeviceOptions; @@ -791,17 +811,17 @@ bool OptionSpeed(std::vector* current_devices, std::string argume 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 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; + currentDevOpts->speed = std::min(std::max(std::stoi(argument), 0), 100); + currentDevOpts->hasOption = true; found = true; } else @@ -810,9 +830,9 @@ bool OptionSpeed(std::vector* current_devices, std::string argume { DeviceOptions* currentDevOpts = ¤t_devices->at(i); - currentDevOpts->speed = std::min(std::max(std::stoi(argument), 0),(int)speed_percentage); + currentDevOpts->speed = std::min(std::max(std::stoi(argument), 0), 100); currentDevOpts->hasOption = true; - found = true; + found = true; } } @@ -820,6 +840,7 @@ bool OptionSpeed(std::vector* current_devices, std::string argume { std::cout << "Error: No devices for speed \"" << argument << "\"" << std::endl; } + return found; } @@ -831,16 +852,16 @@ bool OptionBrightness(std::vector* current_devices, std::string a return false; } - /*---------------------------------------------------------*\ - | If a device is not selected i.e. size() == 0 | - | then add brightness to allDeviceOptions | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | 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->brightness = std::min(std::max(std::stoi(argument), 0), 100); currentDevOpts->hasOption = true; found = true; } @@ -850,7 +871,7 @@ bool OptionBrightness(std::vector* current_devices, std::string a { DeviceOptions* currentDevOpts = ¤t_devices->at(i); - currentDevOpts->brightness = std::min(std::max(std::stoi(argument), 0),(int)brightness_percentage); + currentDevOpts->brightness = std::min(std::max(std::stoi(argument), 0), 100); currentDevOpts->hasOption = true; found = true; } @@ -874,9 +895,10 @@ bool OptionSize(std::vector* current_devices, std::string argumen 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 | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Fail out if device, zone, or size are out of | + | range | + \*-------------------------------------------------*/ if((current_device >= static_cast(rgb_controllers.size())) || (current_device < 0)) { std::cout << "Error: Device is out of range" << std::endl; @@ -892,14 +914,14 @@ bool OptionSize(std::vector* current_devices, std::string argumen std::cout << "Error: New size is out of range" << std::endl; } - /*---------------------------------------------------------*\ - | Resize the zone | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Resize the zone | + \*-------------------------------------------------*/ rgb_controllers[current_device]->ResizeZone(current_zone, new_size); - /*---------------------------------------------------------*\ - | Save configuration | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Save configuration | + \*-------------------------------------------------*/ ResourceManager::get()->GetProfileManager()->SaveConfiguration(); } @@ -910,9 +932,9 @@ bool OptionProfile(std::string argument) { ResourceManager::get()->WaitForDetection(); - /*---------------------------------------------------------*\ - | Attempt to load profile | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Attempt to load profile | + \*-----------------------------------------------------*/ if(ResourceManager::get()->GetProfileManager()->LoadProfile(argument)) { std::cout << "Profile loaded successfully" << std::endl; @@ -927,76 +949,77 @@ bool OptionProfile(std::string argument) bool OptionSaveProfile(std::string argument) { - /*---------------------------------------------------------*\ - | Set save profile filename | - \*---------------------------------------------------------*/ - profile_save_filename = argument; + /*-----------------------------------------------------*\ + | Set save profile name | + \*-----------------------------------------------------*/ + profile_save_name = argument; + return(true); } int ProcessOptions(Options* options, std::vector& rgb_controllers) { - unsigned int ret_flags = 0; - int arg_index = 1; + unsigned int ret_flags = 0; + int arg_index = 1; std::vector current_devices; - options->hasDevice = false; - options->profile_loaded = false; + options->hasDevice = false; + options->profile_loaded = false; #ifdef _WIN32 - int fake_argc; - wchar_t** argvw = CommandLineToArgvW(GetCommandLineW(), &fake_argc); + 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; + std::string option = preserve_argv[arg_index]; + std::string argument = ""; + filesystem::path arg_path; - /*---------------------------------------------------------*\ - | Handle options that take an argument | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Handle options that take an argument | + \*-------------------------------------------------*/ if(arg_index + 1 < preserve_argc) { - argument = preserve_argv[arg_index + 1]; + argument = preserve_argv[arg_index + 1]; #ifdef _WIN32 - arg_path = argvw[arg_index + 1]; + arg_path = argvw[arg_index + 1]; #else - arg_path = argument; + arg_path = argument; #endif } - /*---------------------------------------------------------*\ - | -l / --list-devices (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -l / --list-devices (no arguments) | + \*-------------------------------------------------*/ if(option == "--list-devices" || option == "-l") { OptionListDevices(rgb_controllers, false); exit(0); } - /*---------------------------------------------------------*\ - | -ld / --list-detailed (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -ld / --list-detailed (no arguments) | + \*-------------------------------------------------*/ else if(option == "--list-detailed" || option == "-ld") { OptionListDevices(rgb_controllers, true); exit(0); } - /*---------------------------------------------------------*\ - | -lp / --list-profiles (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -lp / --list-profiles (no arguments) | + \*-------------------------------------------------*/ else if(option == "--list-profiles" || option == "-lp") { OptionListProfiles(); exit(0); } - /*---------------------------------------------------------*\ - | -d / --device | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -d / --device | + \*-------------------------------------------------*/ else if(option == "--device" || option == "-d") { while(!current_devices.empty()) @@ -1013,9 +1036,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | -z / --zone | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -z / --zone | + \*-------------------------------------------------*/ else if(option == "--zone" || option == "-z") { if(!OptionZone(¤t_devices, argument, options, rgb_controllers)) @@ -1026,9 +1049,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | -c / --color | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -c / --color | + \*-------------------------------------------------*/ else if(option == "--color" || option == "-c") { if(!OptionColor(¤t_devices, argument, options)) @@ -1039,9 +1062,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | -m / --mode | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -m / --mode | + \*-------------------------------------------------*/ else if(option == "--mode" || option == "-m") { if(!OptionMode(¤t_devices, argument, options)) @@ -1052,9 +1075,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | -b / --brightness | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -b / --brightness | + \*-------------------------------------------------*/ else if(option == "--brightness" || option == "-b") { if(!OptionBrightness(¤t_devices, argument, options)) @@ -1065,9 +1088,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | -s / --speed | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -s / --speed | + \*-------------------------------------------------*/ else if(option == "--speed" || option == "-s") { if(!OptionSpeed(¤t_devices, argument, options)) @@ -1078,9 +1101,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | -sz / --size | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -sz / --size | + \*-------------------------------------------------*/ else if(option == "--size" || option == "-sz") { if(!OptionSize(¤t_devices, argument, options, rgb_controllers)) @@ -1091,9 +1114,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | -p / --profile | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -p / --profile | + \*-------------------------------------------------*/ else if(option == "--profile" || option == "-p") { options->profile_loaded = OptionProfile(arg_path.generic_u8string()); @@ -1101,9 +1124,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | -sp / --save-profile | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -sp / --save-profile | + \*-------------------------------------------------*/ else if(option == "--save-profile" || option == "-sp") { OptionSaveProfile(arg_path.generic_u8string()); @@ -1111,9 +1134,9 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | Invalid option | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Invalid option | + \*-------------------------------------------------*/ else { if((option == "--localconfig") @@ -1131,10 +1154,11 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle ||(option == "--autostart-check") ||(option == "--autostart-disable")) { - /*-------------------------------------------------*\ - | Do nothing, these are pre-detection arguments | - | and this parser should ignore them | - \*-------------------------------------------------*/ + /*-----------------------------------------*\ + | Do nothing, these are pre-detection | + | arguments and this parser should ignore | + | them | + \*-----------------------------------------*/ } else if((option == "--server-port") ||(option == "--server-host") @@ -1143,18 +1167,18 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle ||(option == "--client") ||(option == "--autostart-enable")) { - /*-------------------------------------------------*\ - | Increment index for pre-detection arguments with | - | parameter | - \*-------------------------------------------------*/ + /*-----------------------------------------*\ + | Increment index for pre-detection | + | arguments with parameter | + \*-----------------------------------------*/ arg_index++; } else { - /*-------------------------------------------------*\ - | If the argument is not a pre-detection argument, | - | throw an error and print help | - \*-------------------------------------------------*/ + /*-----------------------------------------*\ + | If the argument is not a pre-detection | + | argument, throw an error and print help | + \*-----------------------------------------*/ std::cout << "Error: Invalid option: " + option << std::endl; return RET_FLAG_PRINT_HELP; } @@ -1163,10 +1187,10 @@ int ProcessOptions(Options* options, std::vector& rgb_controlle arg_index++; } - /*---------------------------------------------------------*\ - | If a device was specified, check to verify that a | - | corresponding option was also specified | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | If a device was specified, check to verify that a | + | corresponding option was also specified | + \*-----------------------------------------------------*/ while(!current_devices.empty()) { options->devices.push_back(current_devices.back()); @@ -1195,54 +1219,54 @@ void ApplyOptions(DeviceOptions& options, std::vector& rgb_cont { RGBController* device = rgb_controllers[options.device]; - /*---------------------------------------------------------*\ - | Clear the active profile when applying device changes | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Clear the active profile when applying device changes | + \*-----------------------------------------------------*/ ResourceManager::get()->GetProfileManager()->ClearActiveProfile(); - /*---------------------------------------------------------*\ - | Set mode first, in case it's 'direct' (which affects | - | SetColor below) | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Set mode first, in case it's 'direct' (which affects | + | SetColor below) | + \*-----------------------------------------------------*/ unsigned int mode = ParseMode(options, rgb_controllers); - /*---------------------------------------------------------*\ - | If the user has specified random colours and the device | - | supports that colour mode then swich to it before | - | evaluating if a colour needs to be set | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | If the user has specified random colours and the | + | device supports that colour mode then swich to it | + | before evaluating if a colour needs to be set | + \*-----------------------------------------------------*/ if(options.random_colors && (device->GetModeFlags(mode) & MODE_FLAG_HAS_RANDOM_COLOR)) { device->SetModeColorMode(mode, MODE_COLORS_RANDOM); } - /*---------------------------------------------------------*\ - | If the user has specified random colours and the device | - | supports that colour mode then swich to it before | - | evaluating if a colour needs to be set | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | If the user has specified random colours and the | + | device supports that colour mode then swich to it | + | before evaluating if a colour needs to be set | + \*-----------------------------------------------------*/ if((device->GetModeFlags(mode) & MODE_FLAG_HAS_BRIGHTNESS)) { unsigned int new_brightness = device->GetModeBrightnessMax(mode) - device->GetModeBrightnessMin(mode); new_brightness *= options.brightness; - new_brightness /= brightness_percentage; + new_brightness /= 100; device->SetModeBrightness(mode, device->GetModeBrightnessMin(mode) + new_brightness); } if((device->GetModeFlags(mode) & MODE_FLAG_HAS_SPEED)) { - unsigned int new_speed = device->GetModeSpeedMax(mode) - device->GetModeSpeedMin(mode); - new_speed *= options.speed; - new_speed /= speed_percentage; + unsigned int new_speed = device->GetModeSpeedMax(mode) - device->GetModeSpeedMin(mode); + new_speed *= options.speed; + new_speed /= 100; device->SetModeSpeed(mode, device->GetModeSpeedMin(mode) + new_speed); } - /*---------------------------------------------------------*\ - | Determine which color mode this mode uses and update | - | colors accordingly | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Determine which color mode this mode uses and update | + | colors accordingly | + \*-----------------------------------------------------*/ switch(device->GetModeColorMode(mode)) { case MODE_COLORS_NONE: @@ -1259,13 +1283,13 @@ void ApplyOptions(DeviceOptions& options, std::vector& rgb_cont if(options.zone < 0) { - start_from = 0; - led_count = device->GetLEDCount(); + start_from = 0; + led_count = device->GetLEDCount(); } else { - start_from = device->GetZoneStartIndex(options.zone); - led_count = device->GetLEDsInZone(options.zone); + start_from = device->GetZoneStartIndex(options.zone); + led_count = device->GetLEDsInZone(options.zone); } for(unsigned int led_idx = 0; led_idx < led_count; led_idx++) @@ -1274,7 +1298,7 @@ void ApplyOptions(DeviceOptions& options, std::vector& rgb_cont if(color_idx >= options.colors.size()) { - color_idx = (unsigned int)options.colors.size() - 1; + color_idx = (unsigned int)options.colors.size() - 1; } device->SetColor((unsigned int)(start_from + led_idx), ToRGBColor(std::get<0>(options.colors[color_idx]), @@ -1303,14 +1327,14 @@ void ApplyOptions(DeviceOptions& options, std::vector& rgb_cont break; } - /*---------------------------------------------------------*\ - | Set device mode | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Set device mode | + \*-----------------------------------------------------*/ device->SetActiveMode(mode); - /*---------------------------------------------------------*\ - | Set device per-LED colors if necessary | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Set device per-LED colors if necessary | + \*-----------------------------------------------------*/ if(device->GetModeColorMode(mode) == MODE_COLORS_PER_LED) { device->DeviceUpdateLEDs(); @@ -1320,54 +1344,54 @@ void ApplyOptions(DeviceOptions& options, std::vector& rgb_cont unsigned int cli_pre_detection(int argc, char* argv[]) { - /*---------------------------------------------------------*\ - | Process only the arguments that should be performed prior | - | to detecting devices and/or starting clients | - \*---------------------------------------------------------*/ - int arg_index = 1; - unsigned int cfg_args = 0; - unsigned int ret_flags = 0; - bool server_start = false; - bool print_help = false; + /*-----------------------------------------------------*\ + | Process only the arguments that should be performed | + | prior to detecting devices and/or starting clients | + \*-----------------------------------------------------*/ + int arg_index = 1; + unsigned int cfg_args = 0; + unsigned int ret_flags = 0; + bool server_start = false; + bool print_help = false; - preserve_argc = argc; - preserve_argv = argv; + preserve_argc = argc; + preserve_argv = argv; #ifdef _WIN32 - int fake_argc; - wchar_t** argvw = CommandLineToArgvW(GetCommandLineW(), &fake_argc); + int fake_argc; + wchar_t** argvw = CommandLineToArgvW(GetCommandLineW(), &fake_argc); #endif while(arg_index < argc) { - std::string option = argv[arg_index]; - std::string argument = ""; + std::string option = argv[arg_index]; + std::string argument = ""; LOG_DEBUG("[CLI] Parsing CLI option: %s", option.c_str()); - /*---------------------------------------------------------*\ - | Handle options that take an argument | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Handle options that take an argument | + \*-------------------------------------------------*/ if(arg_index + 1 < argc) { - argument = argv[arg_index + 1]; + argument = argv[arg_index + 1]; } - /*---------------------------------------------------------*\ - | --localconfig | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --localconfig | + \*-------------------------------------------------*/ if(option == "--localconfig") { ResourceManager::get()->SetConfigurationDirectory("./"); cfg_args++; } - /*---------------------------------------------------------*\ - | --config | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --config | + \*-------------------------------------------------*/ else if(option == "--config") { - cfg_args+= 2; + cfg_args += 2; arg_index++; #ifdef _WIN32 filesystem::path config_path(argvw[arg_index]); @@ -1388,43 +1412,43 @@ unsigned int cli_pre_detection(int argc, char* argv[]) } } - /*---------------------------------------------------------*\ - | --nodetect | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --nodetect | + \*-------------------------------------------------*/ else if(option == "--nodetect") { - ret_flags |= RET_FLAG_NO_DETECT; + ret_flags |= RET_FLAG_NO_DETECT; cfg_args++; } - /*---------------------------------------------------------*\ - | --noautoconnect | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --noautoconnect | + \*-------------------------------------------------*/ else if(option == "--noautoconnect") { - ret_flags |= RET_FLAG_NO_AUTO_CONNECT; + ret_flags |= RET_FLAG_NO_AUTO_CONNECT; cfg_args++; } - /*---------------------------------------------------------*\ - | --client | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --client | + \*-------------------------------------------------*/ else if(option == "--client") { - NetworkClient * client = new NetworkClient(); + NetworkClient* client = new NetworkClient(); - std::size_t pos = argument.find(":"); - std::string ip = argument.substr(0, pos); - unsigned short port_val; + std::size_t pos = argument.find(":"); + std::string ip = argument.substr(0, pos); + unsigned short port_val; if(pos == argument.npos) { - port_val = OPENRGB_SDK_PORT; + port_val = OPENRGB_SDK_PORT; } else { - std::string port = argument.substr(argument.find(":") + 1); - port_val = std::stoi(port); + std::string port = argument.substr(argument.find(":") + 1); + port_val = std::stoi(port); } std::string titleString = "OpenRGB "; @@ -1451,24 +1475,25 @@ unsigned int cli_pre_detection(int argc, char* argv[]) arg_index++; } - /*---------------------------------------------------------*\ - | --server (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --server (no arguments) | + \*-------------------------------------------------*/ else if(option == "--server") { - server_start = true; + server_start = true; } - /*---------------------------------------------------------*\ - | --server-port | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --server-port | + \*-------------------------------------------------*/ else if(option == "--server-port") { if(argument != "") { try { - int port = std::stoi(argument); + int port = std::stoi(argument); + if(port >= 1024 && port <= 65535) { ResourceManager::get()->SetDefaultServerPort(port); @@ -1477,64 +1502,68 @@ unsigned int cli_pre_detection(int argc, char* argv[]) else { std::cout << "Error: Port out of range: " << port << " (1024-65535)" << std::endl; - print_help = true; + print_help = true; break; } } catch(std::invalid_argument& /*e*/) { std::cout << "Error: Invalid data in --server-port argument (expected a number in range 1024-65535)" << std::endl; - print_help = true; + print_help = true; break; } } else { std::cout << "Error: Missing argument for --server-port" << std::endl; - print_help = true; + print_help = true; break; } + cfg_args++; arg_index++; } - /*---------------------------------------------------------*\ - | --server-host | - \*---------------------------------------------------------*/ + + /*-------------------------------------------------*\ + | --server-host | + \*-------------------------------------------------*/ else if(option == "--server-host") { if(argument != "") { ResourceManager::get()->SetDefaultServerHost(argument); - server_start = true; + server_start = true; } else { std::cout << "Error: Missing argument for --server-host" << std::endl; - print_help = true; + print_help = true; break; } + cfg_args++; arg_index++; } - /*---------------------------------------------------------*\ - | --loglevel | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --loglevel | + \*-------------------------------------------------*/ else if(option == "--loglevel") { - if (argument != "") + if(argument != "") { try { - int level = std::stoi(argument); - if (level >= 0 && level <= LL_TRACE) + int level = std::stoi(argument); + + if(level >= 0 && level <= LL_TRACE) { LogManager::get()->SetLogLevel(level); } else { LOG_ERROR("[CLI] LogLevel out of range: %d (0-6)", level); - print_help = true; + print_help = true; break; } } @@ -1571,7 +1600,7 @@ unsigned int cli_pre_detection(int argc, char* argv[]) else { LOG_ERROR("[CLI] Invalid loglevel"); - print_help = true; + print_help = true; break; } } @@ -1579,16 +1608,16 @@ unsigned int cli_pre_detection(int argc, char* argv[]) else { LOG_ERROR("[CLI] Missing argument for --loglevel"); - print_help = true; + print_help = true; break; } cfg_args+= 2; arg_index++; } - /*---------------------------------------------------------*\ - | --autostart-check (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --autostart-check (no arguments) | + \*-------------------------------------------------*/ else if(option == "--autostart-check") { AutoStart auto_start("OpenRGB"); @@ -1603,9 +1632,9 @@ unsigned int cli_pre_detection(int argc, char* argv[]) } } - /*---------------------------------------------------------*\ - | --autostart-disable (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --autostart-disable (no arguments) | + \*-------------------------------------------------*/ else if(option == "--autostart-disable") { AutoStart auto_start("OpenRGB"); @@ -1620,25 +1649,25 @@ unsigned int cli_pre_detection(int argc, char* argv[]) } } - /*---------------------------------------------------------*\ - | --autostart-enable | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --autostart-enable | + \*-------------------------------------------------*/ else if(option == "--autostart-enable") { - if (argument != "") + if(argument != "") { - std::string desc = "OpenRGB "; - desc += VERSION_STRING; - desc += ", for controlling RGB lighting."; + std::string desc = "OpenRGB "; + desc += VERSION_STRING; + desc += ", for controlling RGB lighting."; AutoStart auto_start("OpenRGB"); AutoStartInfo auto_start_interface; - auto_start_interface.args = argument; - auto_start_interface.category = "Utility;"; - auto_start_interface.desc = desc; - auto_start_interface.icon = "OpenRGB"; - auto_start_interface.path = auto_start.GetExePath(); + auto_start_interface.args = argument; + auto_start_interface.category = "Utility;"; + auto_start_interface.desc = desc; + auto_start_interface.icon = "OpenRGB"; + auto_start_interface.path = auto_start.GetExePath(); if(auto_start.EnableAutoStart(auto_start_interface)) { @@ -1660,81 +1689,82 @@ unsigned int cli_pre_detection(int argc, char* argv[]) arg_index++; } - /*---------------------------------------------------------*\ - | --gui (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --gui (no arguments) | + \*-------------------------------------------------*/ else if(option == "--gui") { - ret_flags |= RET_FLAG_START_GUI; + ret_flags |= RET_FLAG_START_GUI; } - /*---------------------------------------------------------*\ - | --i2c-tools / --yolo (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --i2c-tools / --yolo (no arguments) | + \*-------------------------------------------------*/ else if(option == "--i2c-tools" || option == "--yolo") { - ret_flags |= RET_FLAG_START_GUI | RET_FLAG_I2C_TOOLS; + ret_flags |= RET_FLAG_START_GUI | RET_FLAG_I2C_TOOLS; } - /*---------------------------------------------------------*\ - | --startminimized (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --startminimized (no arguments) | + \*-------------------------------------------------*/ else if(option == "--startminimized") { ret_flags |= RET_FLAG_START_GUI | RET_FLAG_START_MINIMIZED; } - /*---------------------------------------------------------*\ - | -h / --help (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -h / --help (no arguments) | + \*-------------------------------------------------*/ else if(option == "--help" || option == "-h") { print_help = true; break; } - /*---------------------------------------------------------*\ - | -V / --version (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -V / --version (no arguments) | + \*-------------------------------------------------*/ else if(option == "--version" || option == "-V") { OptionVersion(); exit(0); } - /*---------------------------------------------------------*\ - | -v / --verbose (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -v / --verbose (no arguments) | + \*-------------------------------------------------*/ else if(option == "--verbose" || option == "-v") { LogManager::get()->SetVerbosity(LL_VERBOSE); cfg_args++; } - /*---------------------------------------------------------*\ - | -vv / --very-verbose (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | -vv / --very-verbose (no arguments) | + \*-------------------------------------------------*/ else if(option == "--very-verbose" || option == "-vv") { LogManager::get()->SetVerbosity(LL_TRACE); cfg_args++; } - /*---------------------------------------------------------*\ - | --print-source (no arguments) | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | --print-source (no arguments) | + \*-------------------------------------------------*/ else if(option == "--print-source") { LogManager::get()->SetPrintSource(true); cfg_args++; } - /*---------------------------------------------------------*\ - | Any unrecognized arguments trigger the post-detection CLI | - \*---------------------------------------------------------*/ + /*-------------------------------------------------*\ + | Any unrecognized arguments trigger the | + | post-detection CLI | + \*-------------------------------------------------*/ else { - ret_flags |= RET_FLAG_CLI_POST_DETECTION; + ret_flags |= RET_FLAG_CLI_POST_DETECTION; } arg_index++; @@ -1761,26 +1791,26 @@ unsigned int cli_pre_detection(int argc, char* argv[]) unsigned int cli_post_detection() { - /*---------------------------------------------------------*\ - | Wait for device detection | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Wait for device detection | + \*-----------------------------------------------------*/ ResourceManager::get()->WaitForDetection(); - /*---------------------------------------------------------*\ - | Get controller list from resource manager | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Get controller list from resource manager | + \*-----------------------------------------------------*/ std::vector rgb_controllers = ResourceManager::get()->GetRGBControllers(); - /*---------------------------------------------------------*\ - | Process the argument options | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | Process the argument options | + \*-----------------------------------------------------*/ Options options; unsigned int ret_flags = ProcessOptions(&options, rgb_controllers); - /*---------------------------------------------------------*\ - | If the return flags are set, exit CLI mode without | - | processing device updates from CLI input. | - \*---------------------------------------------------------*/ + /*-----------------------------------------------------*\ + | If the return flags are set, exit CLI mode without | + | processing device updates from CLI input. | + \*-----------------------------------------------------*/ switch(ret_flags) { case 0: @@ -1796,33 +1826,33 @@ unsigned int cli_post_detection() break; } - /*---------------------------------------------------------*\ - | If the options has one or more specific devices, loop | - | through all of the specific devices and apply settings. | - | Otherwise, apply settings to all devices. | - \*---------------------------------------------------------*/ - if (options.hasDevice) + /*-----------------------------------------------------*\ + | If the options has one or more specific devices, loop | + | through all of the specific devices and apply | + | settings. Otherwise, apply settings to all devices. | + \*-----------------------------------------------------*/ + if(options.hasDevice) { for(unsigned int device_idx = 0; device_idx < options.devices.size(); device_idx++) { ApplyOptions(options.devices[device_idx], rgb_controllers); } } - else if (!options.profile_loaded) + else if(!options.profile_loaded) { - for (unsigned int device_idx = 0; device_idx < rgb_controllers.size(); device_idx++) + for(unsigned int device_idx = 0; device_idx < rgb_controllers.size(); device_idx++) { options.allDeviceOptions.device = device_idx; ApplyOptions(options.allDeviceOptions, rgb_controllers); } } - /*---------------------------------------------------------*\ - | If there is a save filename set, save the profile | - \*---------------------------------------------------------*/ - if (profile_save_filename != "") + /*-----------------------------------------------------*\ + | If there is a save name set, save the profile | + \*-----------------------------------------------------*/ + if(profile_save_name != "") { - if(ResourceManager::get()->GetProfileManager()->SaveProfile(profile_save_filename)) + if(ResourceManager::get()->GetProfileManager()->SaveProfile(profile_save_name)) { LOG_INFO("[CLI] Profile saved successfully"); } diff --git a/cli.h b/cli.h index 45e15b37c..4bf68bbd3 100644 --- a/cli.h +++ b/cli.h @@ -1,19 +1,29 @@ -#ifndef CLI_H -#define CLI_H +/*---------------------------------------------------------*\ +| cli.h | +| | +| OpenRGB command line interface | +| | +| This file is part of the OpenRGB project | +| SPDX-License-Identifier: GPL-2.0-or-later | +\*---------------------------------------------------------*/ + +#pragma once unsigned int cli_pre_detection(int argc, char* argv[]); unsigned int cli_post_detection(); +/*---------------------------------------------------------*\ +| Return flags, a bitfield containing the relevant command | +| line arguments needed for application startup | +\*---------------------------------------------------------*/ 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, + 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, }; - -#endif