diff --git a/cli.cpp b/cli.cpp index 01609ac1..8164b9f9 100644 --- a/cli.cpp +++ b/cli.cpp @@ -24,6 +24,13 @@ static ProfileManager* profile_manager; static NetworkServer* network_server; static std::string profile_save_filename = ""; +enum +{ + RET_FLAG_PRINT_HELP = 1, + RET_FLAG_START_GUI = 2, + RET_FLAG_I2C_TOOLS = 4, +}; + struct DeviceOptions { int device; @@ -663,15 +670,14 @@ int ProcessOptions(int argc, char *argv[], Options *options) if(arg_index + 1 < argc) { argument = argv[arg_index + 1]; - arg_index++; } /*---------------------------------------------------------*\ - | --server | + | --server (no arguments) | \*---------------------------------------------------------*/ if(option == "--server") { - options->servOpts.start = true; + options->servOpts.start = true; } /*---------------------------------------------------------*\ @@ -689,36 +695,36 @@ int ProcessOptions(int argc, char *argv[], Options *options) else { std::cout << "Error: port out of range: " << port << " (1024-65535)" << std::endl; - return 1; + return RET_FLAG_PRINT_HELP; } } else { std::cout << "Error: Missing argument for --server-port" << std::endl; - return 1; + return RET_FLAG_PRINT_HELP; } + arg_index++; } - /*---------------------------------------------------------*\ - | --gui | + | --gui (no arguments) | \*---------------------------------------------------------*/ else if(option == "--gui") { - ret_flags |= 2; + ret_flags |= RET_FLAG_START_GUI; } /*---------------------------------------------------------*\ - | --i2c-tools / --yolo | + | --i2c-tools / --yolo (no arguments) | \*---------------------------------------------------------*/ else if(option == "--i2c-tools" || option == "--yolo") { - ret_flags |= 4; + ret_flags |= RET_FLAG_I2C_TOOLS; } /*---------------------------------------------------------*\ - | -h / --help | + | -h / --help (no arguments) | \*---------------------------------------------------------*/ else if(option == "--help" || option == "-h") { @@ -727,7 +733,7 @@ int ProcessOptions(int argc, char *argv[], Options *options) } /*---------------------------------------------------------*\ - | -v / --version | + | -v / --version (no arguments) | \*---------------------------------------------------------*/ else if(option == "--version" || option == "-v") { @@ -736,7 +742,7 @@ int ProcessOptions(int argc, char *argv[], Options *options) } /*---------------------------------------------------------*\ - | -l / --list-devices | + | -l / --list-devices (no arguments) | \*---------------------------------------------------------*/ else if(option == "--list-devices" || option == "-l") { @@ -751,8 +757,10 @@ int ProcessOptions(int argc, char *argv[], Options *options) { if(!OptionDevice(¤t_device, argument, options)) { - return 1; + return RET_FLAG_PRINT_HELP; } + + arg_index++; } /*---------------------------------------------------------*\ @@ -762,8 +770,10 @@ int ProcessOptions(int argc, char *argv[], Options *options) { if(!OptionZone(¤t_device, ¤t_zone, argument, options)) { - return 1; + return RET_FLAG_PRINT_HELP; } + + arg_index++; } /*---------------------------------------------------------*\ @@ -773,8 +783,10 @@ int ProcessOptions(int argc, char *argv[], Options *options) { if(!OptionColor(¤t_device, ¤t_zone, argument, options)) { - return 1; + return RET_FLAG_PRINT_HELP; } + + arg_index++; } /*---------------------------------------------------------*\ @@ -784,8 +796,10 @@ int ProcessOptions(int argc, char *argv[], Options *options) { if(!OptionMode(¤t_device, argument, options)) { - return 1; + return RET_FLAG_PRINT_HELP; } + + arg_index++; } /*---------------------------------------------------------*\ @@ -795,8 +809,10 @@ int ProcessOptions(int argc, char *argv[], Options *options) { if(!OptionSize(¤t_device, ¤t_zone, argument, options)) { - return 1; + return RET_FLAG_PRINT_HELP; } + + arg_index++; } /*---------------------------------------------------------*\ @@ -806,6 +822,8 @@ int ProcessOptions(int argc, char *argv[], Options *options) { OptionProfile(argument); exit(0); + + arg_index++; } /*---------------------------------------------------------*\ @@ -814,6 +832,8 @@ int ProcessOptions(int argc, char *argv[], Options *options) else if(option == "--save-profile" || option == "-sp") { OptionSaveProfile(argument); + + arg_index++; } /*---------------------------------------------------------*\ @@ -822,7 +842,7 @@ int ProcessOptions(int argc, char *argv[], Options *options) else { std::cout << "Error: Invalid option: " + option << std::endl; - return 1; + return RET_FLAG_PRINT_HELP; } arg_index++; @@ -839,7 +859,7 @@ int ProcessOptions(int argc, char *argv[], Options *options) if(!options->devices[option_idx].hasOption) { std::cout << "Error: Device " + std::to_string(option_idx) + " specified, but neither mode nor color given" << std::endl; - return 1; + return RET_FLAG_PRINT_HELP; } } return 0; @@ -939,18 +959,59 @@ unsigned int cli_main(int argc, char *argv[], std::vector rgb_c profile_manager = profile_manager_in; network_server = network_server_in; + /*---------------------------------------------------------*\ + | Windows only - Attach console output | + \*---------------------------------------------------------*/ +#ifdef _WIN32 + AttachConsole(-1); + freopen("CONIN$", "r", stdin); + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); +#endif /*---------------------------------------------------------*\ | Process the argument options | \*---------------------------------------------------------*/ Options options; unsigned int ret_flags = ProcessOptions(argc, argv, &options); + + /*---------------------------------------------------------*\ + | If the server was told to start, start it before returning| + \*---------------------------------------------------------*/ + if(options.servOpts.start) + { + network_server->SetPort(options.servOpts.port); + network_server->StartServer(); + + if(network_server->GetOnline()) + { + /*---------------------------------------------------------*\ + | If the GUI has been started, return from cli_main. | + | Otherwise, we are in daemon mode and cli_main should wait | + | to keep the program alive as long as the server is online | + \*---------------------------------------------------------*/ + if((ret_flags & RET_FLAG_START_GUI) == 0) + { + WaitWhileServerOnline(network_server); + } + } + else + { + std::cout << "Server failed to start" << std::endl; + exit(1); + } + } + + /*---------------------------------------------------------*\ + | If the return flags are set, exit CLI mode without | + | processing device updates from CLI input. | + \*---------------------------------------------------------*/ switch(ret_flags) { case 0: break; - case 1: + case RET_FLAG_PRINT_HELP: OptionHelp(); exit(-1); break; @@ -996,25 +1057,6 @@ unsigned int cli_main(int argc, char *argv[], std::vector rgb_c } } - if (options.servOpts.start) - { - network_server->SetPort(options.servOpts.port); - network_server->StartServer(); - - if(network_server->GetOnline()) - { - WaitWhileServerOnline(network_server); - return 0; - } - else - { - std::cout << "Server failed to start" << std::endl; - exit(1); - } - - - } - exit(0); return 0; diff --git a/main.cpp b/main.cpp index 253be98c..d5004c5c 100644 --- a/main.cpp +++ b/main.cpp @@ -21,9 +21,18 @@ extern std::vector busses; extern std::vector rgb_controllers; -// See cli.cpp +/*-------------------------------------------------------------*\ +| Command line functionality and return flags | +\*-------------------------------------------------------------*/ extern unsigned int cli_main(int argc, char *argv[], std::vector rgb_controllers_in, ProfileManager* profile_manager_in, NetworkServer* network_server_in); +enum +{ + RET_FLAG_PRINT_HELP = 1, + RET_FLAG_START_GUI = 2, + RET_FLAG_I2C_TOOLS = 4, +}; + /******************************************************************************************\ * * * main * @@ -41,30 +50,39 @@ int main(int argc, char* argv[]) profile_manager.LoadSizeFromProfile("sizes.ors"); - unsigned int ret_flags = 0; + /*---------------------------------------------------------*\ + | Process command line arguments | + \*---------------------------------------------------------*/ + unsigned int ret_flags = RET_FLAG_START_GUI; if(argc > 1) { ret_flags = cli_main(argc, argv, rgb_controllers, &profile_manager, &server); } - if(ret_flags && 2) + /*---------------------------------------------------------*\ + | If the command line parser indicates that the GUI should | + | run, or if there were no command line arguments, start the| + | GUI. | + \*---------------------------------------------------------*/ + if(ret_flags & RET_FLAG_START_GUI) { - //GUI is enabled - } + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication a(argc, argv); - bool show_i2c_tools = false; - if(ret_flags && 4) + bool show_i2c_tools = false; + if(ret_flags & RET_FLAG_I2C_TOOLS) + { + //I2C Tools is enabled + show_i2c_tools = true; + } + + Ui::OpenRGBDialog2 dlg(busses, rgb_controllers, &profile_manager, &server, show_i2c_tools); + dlg.show(); + + return a.exec(); + } + else { - //I2C Tools is enabled - show_i2c_tools = true; + return 0; } - - - QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - QApplication a(argc, argv); - - Ui::OpenRGBDialog2 dlg(busses, rgb_controllers, &profile_manager, &server, show_i2c_tools); - dlg.show(); - - return a.exec(); }