From 0286a5dd58bf222074986e4fbf19df5497c6f3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A9rence=20Clastres?= Date: Sat, 20 Jun 2020 02:27:19 +0200 Subject: [PATCH] CLI: Add daemon/server mode --- cli.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- main.cpp | 6 +-- 2 files changed, 109 insertions(+), 7 deletions(-) diff --git a/cli.cpp b/cli.cpp index dff08d0c..50d77eaa 100644 --- a/cli.cpp +++ b/cli.cpp @@ -7,9 +7,34 @@ #include "ProfileManager.h" #include "RGBController.h" #include "i2c_smbus.h" +#include "NetworkServer.h" + +#ifdef _WIN32 +#include +#endif + +#ifdef __APPLE__ +#include + +static void Sleep(unsigned int milliseconds) +{ + usleep(1000 * milliseconds); +} +#endif + +#ifdef __linux__ +#include + +static void Sleep(unsigned int milliseconds) +{ + usleep(1000 * milliseconds); +} +#endif + static std::vector rgb_controllers; static ProfileManager* profile_manager; +static NetworkServer* network_server; static std::string profile_save_filename = ""; struct DeviceOptions @@ -22,6 +47,12 @@ struct DeviceOptions bool hasOption; }; +struct ServerOptions +{ + bool start = false; + unsigned short port = 6742; //default port +}; + struct Options { std::vector devices; @@ -32,6 +63,7 @@ struct Options \*---------------------------------------------------------*/ bool hasDevice; DeviceOptions allDeviceOptions; + ServerOptions servOpts; }; bool ParseColors(std::string colors_string, DeviceOptions *options) @@ -67,6 +99,10 @@ bool ParseColors(std::string colors_string, DeviceOptions *options) unsigned int ParseMode(DeviceOptions& options) { + // no need to check if --mode wasn't passed + if (options.mode.size() == 0) + return false; + /*---------------------------------------------------------*\ | Search through all of the device modes and see if there is| | a match. If no match is found, print an error message. | @@ -128,7 +164,9 @@ void OptionHelp() help_text += "Usage: OpenRGB (--device [--mode] [--color])...\n"; help_text += "\n"; help_text += "Options:\n"; - help_text += "--gui Show GUI, also appears when not passing any parameters\n"; + help_text += "--server Starts the SDK's server\n"; + help_text += "--server-port Sets the SDK's server port. Default: 6742 (1024-65535)\n"; + help_text += "--gui Shows the GUI, also appears when not passing any parameters\n"; help_text += "-l, --list-devices Lists every compatible device with their number\n"; help_text += "-d, --device [0-9] Selects device to apply colors and/or effect to, or applies to all devices if omitted\n"; help_text += " Can be specified multiple times with different modes and colors\n"; @@ -445,10 +483,45 @@ int ProcessOptions(int argc, char *argv[], Options *options) arg_index++; } + /*---------------------------------------------------------*\ + | --server | + \*---------------------------------------------------------*/ + if(option == "--server") + { + options->servOpts.start = true; + } + + /*---------------------------------------------------------*\ + | --server-port | + \*---------------------------------------------------------*/ + else if(option == "--server-port") + { + if (argument != "") + { + unsigned short port = std::stoi(argument); + if (port >= 1024 && port <= 65535) + { + options->servOpts.port = port; + } + else + { + std::cout << "Error: port out of range: " << port << " (1024-65535)" << std::endl; + return 1; + } + } + else + { + std::cout << "Error: Missing argument for --server-port" << std::endl; + return 1; + } + + } + + /*---------------------------------------------------------*\ | --gui | \*---------------------------------------------------------*/ - if(option == "--gui") + else if(option == "--gui") { ret_flags |= 2; } @@ -669,10 +742,20 @@ void ApplyOptions(DeviceOptions& options) } } -unsigned int cli_main(int argc, char *argv[], std::vector rgb_controllers_in, ProfileManager* profile_manager_in) +void WaitWhileServerOnline(NetworkServer* srv) +{ + while (network_server->GetOnline()) + { + Sleep(1000); + }; +} + +unsigned int cli_main(int argc, char *argv[], std::vector rgb_controllers_in, ProfileManager* profile_manager_in, NetworkServer* network_server_in) { rgb_controllers = rgb_controllers_in; profile_manager = profile_manager_in; + network_server = network_server_in; + /*---------------------------------------------------------*\ | Process the argument options | @@ -718,7 +801,7 @@ unsigned int cli_main(int argc, char *argv[], std::vector rgb_c /*---------------------------------------------------------*\ | If there is a save filename set, save the profile | \*---------------------------------------------------------*/ - if(profile_save_filename != "") + if (profile_save_filename != "") { if(profile_manager->SaveProfile(profile_save_filename)) { @@ -730,6 +813,25 @@ 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 303111e6..253be98c 100644 --- a/main.cpp +++ b/main.cpp @@ -22,7 +22,7 @@ extern std::vector busses; extern std::vector rgb_controllers; // See cli.cpp -extern unsigned int cli_main(int argc, char *argv[], std::vector rgb_controllers_in, ProfileManager* profile_manager_in); +extern unsigned int cli_main(int argc, char *argv[], std::vector rgb_controllers_in, ProfileManager* profile_manager_in, NetworkServer* network_server_in); /******************************************************************************************\ * * @@ -35,6 +35,7 @@ extern unsigned int cli_main(int argc, char *argv[], std::vector 1) { - ret_flags = cli_main(argc, argv, rgb_controllers, &profile_manager); + ret_flags = cli_main(argc, argv, rgb_controllers, &profile_manager, &server); } if(ret_flags && 2) @@ -58,7 +59,6 @@ int main(int argc, char* argv[]) show_i2c_tools = true; } - NetworkServer server(rgb_controllers); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication a(argc, argv);