From 16a1db4badadc58516f4f75ede0a59db8d913630 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 10 Feb 2024 10:11:31 -0500 Subject: [PATCH 01/10] refactor(main): move remaining entry related code --- cmake/compile_definitions/common.cmake | 2 + docs/source/source_code/src/entry_handler.rst | 5 + src/audio.cpp | 2 +- src/config.cpp | 3 +- src/confighttp.cpp | 2 +- src/entry_handler.cpp | 343 ++++++++++++++++++ src/entry_handler.h | 92 +++++ src/input.cpp | 2 +- src/main.cpp | 337 +---------------- src/main.h | 67 ---- src/nvhttp.cpp | 2 +- src/platform/linux/audio.cpp | 2 +- src/platform/linux/cuda.cpp | 2 +- src/platform/linux/input.cpp | 2 +- src/platform/linux/kmsgrab.cpp | 2 +- src/platform/linux/misc.cpp | 2 +- src/platform/linux/wlgrab.cpp | 2 +- src/platform/linux/x11grab.cpp | 3 +- src/platform/macos/misc.mm | 2 +- src/platform/windows/display_base.cpp | 2 +- src/platform/windows/input.cpp | 4 +- src/platform/windows/misc.cpp | 2 +- src/platform/windows/publish.cpp | 1 - src/process.cpp | 2 +- src/rtsp.cpp | 2 +- src/stream.cpp | 3 +- src/system_tray.cpp | 2 +- src/upnp.cpp | 2 +- src/video.cpp | 3 +- 29 files changed, 475 insertions(+), 422 deletions(-) create mode 100644 docs/source/source_code/src/entry_handler.rst create mode 100644 src/entry_handler.cpp create mode 100644 src/entry_handler.h diff --git a/cmake/compile_definitions/common.cmake b/cmake/compile_definitions/common.cmake index e51dbf56526..47f32acf20b 100644 --- a/cmake/compile_definitions/common.cmake +++ b/cmake/compile_definitions/common.cmake @@ -45,6 +45,8 @@ set(SUNSHINE_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/uuid.h" "${CMAKE_SOURCE_DIR}/src/config.h" "${CMAKE_SOURCE_DIR}/src/config.cpp" + "${CMAKE_SOURCE_DIR}/src/entry_handler.cpp" + "${CMAKE_SOURCE_DIR}/src/entry_handler.h" "${CMAKE_SOURCE_DIR}/src/file_handler.cpp" "${CMAKE_SOURCE_DIR}/src/file_handler.h" "${CMAKE_SOURCE_DIR}/src/logging.cpp" diff --git a/docs/source/source_code/src/entry_handler.rst b/docs/source/source_code/src/entry_handler.rst new file mode 100644 index 00000000000..c522b065652 --- /dev/null +++ b/docs/source/source_code/src/entry_handler.rst @@ -0,0 +1,5 @@ +entry_handler +============= + +.. doxygenfile:: entry_handler.h + :allow-dot-graphs: diff --git a/src/audio.cpp b/src/audio.cpp index a3555eaa080..bf4680d1202 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -10,8 +10,8 @@ #include "audio.h" #include "config.h" +#include "entry_handler.h" #include "logging.h" -#include "main.h" #include "thread_safe.h" #include "utility.h" diff --git a/src/config.cpp b/src/config.cpp index 5cd08cee94e..b7cabca27c2 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -15,11 +15,12 @@ #include #include "config.h" +#include "entry_handler.h" #include "file_handler.h" #include "logging.h" -#include "main.h" #include "nvhttp.h" #include "rtsp.h" +#include "thread_pool.h" #include "utility.h" #include "platform/common.h" diff --git a/src/confighttp.cpp b/src/confighttp.cpp index e3a5f898e39..856d85b6f5c 100644 --- a/src/confighttp.cpp +++ b/src/confighttp.cpp @@ -29,10 +29,10 @@ #include "config.h" #include "confighttp.h" #include "crypto.h" +#include "entry_handler.h" #include "file_handler.h" #include "httpcommon.h" #include "logging.h" -#include "main.h" #include "network.h" #include "nvhttp.h" #include "platform/common.h" diff --git a/src/entry_handler.cpp b/src/entry_handler.cpp new file mode 100644 index 00000000000..7007ea4670e --- /dev/null +++ b/src/entry_handler.cpp @@ -0,0 +1,343 @@ +/** + * @file entry_handler.cpp + * @brief Entry point related functions. + */ + +// standard includes +#include +#include +#include + +// local includes +#include "config.h" +#include "confighttp.h" +#include "entry_handler.h" +#include "httpcommon.h" +#include "logging.h" +#include "network.h" +#include "platform/common.h" +#include "thread_pool.h" +#include "version.h" + +extern "C" { +#ifdef _WIN32 + #include +#endif +} + +using namespace std::literals; + +/** + * @brief Launch the Web UI. + * + * EXAMPLES: + * ```cpp + * launch_ui(); + * ``` + */ +void +launch_ui() { + std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)); + platf::open_url(url); +} + +/** + * @brief Launch the Web UI at a specific endpoint. + * + * EXAMPLES: + * ```cpp + * launch_ui_with_path("/pin"); + * ``` + */ +void +launch_ui_with_path(std::string path) { + std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)) + path; + platf::open_url(url); +} + +#ifdef _WIN32 +// Define global singleton used for NVIDIA control panel modifications +nvprefs::nvprefs_interface nvprefs_instance; +#endif + +namespace args { + int + creds(const char *name, int argc, char *argv[]) { + if (argc < 2 || argv[0] == "help"sv || argv[1] == "help"sv) { + print_help(name); + return 0; + } + + http::save_user_creds(config::sunshine.credentials_file, argv[0], argv[1]); + + return 0; + } + + int + help(const char *name, int argc, char *argv[]) { + print_help(name); + return 0; + } + + int + version(const char *name, int argc, char *argv[]) { + std::cout << PROJECT_NAME << " version: v" << PROJECT_VER << std::endl; + return 0; + } + +#ifdef _WIN32 + int + restore_nvprefs_undo(const char *name, int argc, char *argv[]) { + // Restore global NVIDIA control panel settings to the undo file + // left by improper termination of sunshine.exe, if it exists. + // This entry point is typically called by the uninstaller. + if (nvprefs_instance.load()) { + nvprefs_instance.restore_from_and_delete_undo_file_if_exists(); + nvprefs_instance.unload(); + } + return 0; + } +#endif +} // namespace args + +namespace lifetime { + char **argv; + std::atomic_int desired_exit_code; + + /** + * @brief Terminates Sunshine gracefully with the provided exit code. + * @param exit_code The exit code to return from main(). + * @param async Specifies whether our termination will be non-blocking. + */ + void + exit_sunshine(int exit_code, bool async) { + // Store the exit code of the first exit_sunshine() call + int zero = 0; + desired_exit_code.compare_exchange_strong(zero, exit_code); + + // Raise SIGINT to start termination + std::raise(SIGINT); + + // Termination will happen asynchronously, but the caller may + // have wanted synchronous behavior. + while (!async) { + std::this_thread::sleep_for(1s); + } + } + + /** + * @brief Gets the argv array passed to main(). + */ + char ** + get_argv() { + return argv; + } +} // namespace lifetime + +#ifdef _WIN32 +/** + * @brief Checks if NVIDIA's GameStream software is running. + * @return `true` if GameStream is enabled. + */ +bool +is_gamestream_enabled() { + DWORD enabled; + DWORD size = sizeof(enabled); + return RegGetValueW( + HKEY_LOCAL_MACHINE, + L"SOFTWARE\\NVIDIA Corporation\\NvStream", + L"EnableStreaming", + RRF_RT_REG_DWORD, + nullptr, + &enabled, + &size) == ERROR_SUCCESS && + enabled != 0; +} + +namespace service_ctrl { + class service_controller { + public: + /** + * @brief Constructor for service_controller class. + * @param service_desired_access SERVICE_* desired access flags. + */ + service_controller(DWORD service_desired_access) { + scm_handle = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CONNECT); + if (!scm_handle) { + auto winerr = GetLastError(); + BOOST_LOG(error) << "OpenSCManager() failed: "sv << winerr; + return; + } + + service_handle = OpenServiceA(scm_handle, "SunshineService", service_desired_access); + if (!service_handle) { + auto winerr = GetLastError(); + BOOST_LOG(error) << "OpenService() failed: "sv << winerr; + return; + } + } + + ~service_controller() { + if (service_handle) { + CloseServiceHandle(service_handle); + } + + if (scm_handle) { + CloseServiceHandle(scm_handle); + } + } + + /** + * @brief Asynchronously starts the Sunshine service. + */ + bool + start_service() { + if (!service_handle) { + return false; + } + + if (!StartServiceA(service_handle, 0, nullptr)) { + auto winerr = GetLastError(); + if (winerr != ERROR_SERVICE_ALREADY_RUNNING) { + BOOST_LOG(error) << "StartService() failed: "sv << winerr; + return false; + } + } + + return true; + } + + /** + * @brief Query the service status. + * @param status The SERVICE_STATUS struct to populate. + */ + bool + query_service_status(SERVICE_STATUS &status) { + if (!service_handle) { + return false; + } + + if (!QueryServiceStatus(service_handle, &status)) { + auto winerr = GetLastError(); + BOOST_LOG(error) << "QueryServiceStatus() failed: "sv << winerr; + return false; + } + + return true; + } + + private: + SC_HANDLE scm_handle = NULL; + SC_HANDLE service_handle = NULL; + }; + + /** + * @brief Check if the service is running. + * + * EXAMPLES: + * ```cpp + * is_service_running(); + * ``` + */ + bool + is_service_running() { + service_controller sc { SERVICE_QUERY_STATUS }; + + SERVICE_STATUS status; + if (!sc.query_service_status(status)) { + return false; + } + + return status.dwCurrentState == SERVICE_RUNNING; + } + + /** + * @brief Start the service and wait for startup to complete. + * + * EXAMPLES: + * ```cpp + * start_service(); + * ``` + */ + bool + start_service() { + service_controller sc { SERVICE_QUERY_STATUS | SERVICE_START }; + + std::cout << "Starting Sunshine..."sv; + + // This operation is asynchronous, so we must wait for it to complete + if (!sc.start_service()) { + return false; + } + + SERVICE_STATUS status; + do { + Sleep(1000); + std::cout << '.'; + } while (sc.query_service_status(status) && status.dwCurrentState == SERVICE_START_PENDING); + + if (status.dwCurrentState != SERVICE_RUNNING) { + BOOST_LOG(error) << SERVICE_NAME " failed to start: "sv << status.dwWin32ExitCode; + return false; + } + + std::cout << std::endl; + return true; + } + + /** + * @brief Wait for the UI to be ready after Sunshine startup. + * + * EXAMPLES: + * ```cpp + * wait_for_ui_ready(); + * ``` + */ + bool + wait_for_ui_ready() { + std::cout << "Waiting for Web UI to be ready..."; + + // Wait up to 30 seconds for the web UI to start + for (int i = 0; i < 30; i++) { + PMIB_TCPTABLE tcp_table = nullptr; + ULONG table_size = 0; + ULONG err; + + auto fg = util::fail_guard([&tcp_table]() { + free(tcp_table); + }); + + do { + // Query all open TCP sockets to look for our web UI port + err = GetTcpTable(tcp_table, &table_size, false); + if (err == ERROR_INSUFFICIENT_BUFFER) { + free(tcp_table); + tcp_table = (PMIB_TCPTABLE) malloc(table_size); + } + } while (err == ERROR_INSUFFICIENT_BUFFER); + + if (err != NO_ERROR) { + BOOST_LOG(error) << "Failed to query TCP table: "sv << err; + return false; + } + + uint16_t port_nbo = htons(net::map_port(confighttp::PORT_HTTPS)); + for (DWORD i = 0; i < tcp_table->dwNumEntries; i++) { + auto &entry = tcp_table->table[i]; + + // Look for our port in the listening state + if (entry.dwLocalPort == port_nbo && entry.dwState == MIB_TCP_STATE_LISTEN) { + std::cout << std::endl; + return true; + } + } + + Sleep(1000); + std::cout << '.'; + } + + std::cout << "timed out"sv << std::endl; + return false; + } +} // namespace service_ctrl +#endif diff --git a/src/entry_handler.h b/src/entry_handler.h new file mode 100644 index 00000000000..ba7dff9ba49 --- /dev/null +++ b/src/entry_handler.h @@ -0,0 +1,92 @@ +/** + * @file entry_handler.h + * @brief Header file for entry point functions. + */ +#pragma once + +// standard includes +#include +#include + +// local includes +#include "thread_pool.h" +#include "thread_safe.h" + +extern thread_pool_util::ThreadPool task_pool; +extern bool display_cursor; + +// functions +void +launch_ui(); +void +launch_ui_with_path(std::string path); + +#ifdef _WIN32 + // Declare global singleton used for NVIDIA control panel modifications + #include "platform/windows/nvprefs/nvprefs_interface.h" +extern nvprefs::nvprefs_interface nvprefs_instance; + +// windows only functions +bool +is_gamestream_enabled(); +#endif + +namespace args { + int + creds(const char *name, int argc, char *argv[]); + int + help(const char *name, int argc, char *argv[]); + int + version(const char *name, int argc, char *argv[]); +#ifdef _WIN32 + int + restore_nvprefs_undo(const char *name, int argc, char *argv[]); +#endif +} // namespace args + +namespace lifetime { + extern char **argv; + extern std::atomic_int desired_exit_code; + void + exit_sunshine(int exit_code, bool async); + char ** + get_argv(); +} // namespace lifetime + +namespace mail { +#define MAIL(x) \ + constexpr auto x = std::string_view { \ + #x \ + } + + extern safe::mail_t man; + + // Global mail + MAIL(shutdown); + MAIL(broadcast_shutdown); + MAIL(video_packets); + MAIL(audio_packets); + MAIL(switch_display); + + // Local mail + MAIL(touch_port); + MAIL(idr); + MAIL(invalidate_ref_frames); + MAIL(gamepad_feedback); + MAIL(hdr); +#undef MAIL + +} // namespace mail + +#ifdef _WIN32 +namespace service_ctrl { + bool + is_service_running(); + + bool + start_service(); + + bool + wait_for_ui_ready(); +} // namespace service_ctrl +#endif diff --git a/src/input.cpp b/src/input.cpp index b7416fffac2..c48b86842da 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -17,9 +17,9 @@ extern "C" { #include #include "config.h" +#include "entry_handler.h" #include "input.h" #include "logging.h" -#include "main.h" #include "platform/common.h" #include "thread_pool.h" #include "utility.h" diff --git a/src/main.cpp b/src/main.cpp index 1d1bb305398..62524998727 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,14 +18,13 @@ #include // local includes -#include "config.h" #include "confighttp.h" +#include "entry_handler.h" #include "httpcommon.h" #include "logging.h" #include "main.h" #include "network.h" #include "nvhttp.h" -#include "platform/common.h" #include "process.h" #include "rtsp.h" #include "system_tray.h" @@ -37,10 +36,6 @@ extern "C" { #include #include - -#ifdef _WIN32 - #include -#endif } safe::mail_t mail::man; @@ -48,11 +43,6 @@ safe::mail_t mail::man; using namespace std::literals; namespace bl = boost::log; -#ifdef _WIN32 -// Define global singleton used for NVIDIA control panel modifications -nvprefs::nvprefs_interface nvprefs_instance; -#endif - thread_pool_util::ThreadPool task_pool; bool display_cursor = true; @@ -64,309 +54,6 @@ struct NoDelete { BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", int) -namespace help { - int - entry(const char *name, int argc, char *argv[]) { - print_help(name); - return 0; - } -} // namespace help - -namespace version { - int - entry(const char *name, int argc, char *argv[]) { - std::cout << PROJECT_NAME << " version: v" << PROJECT_VER << std::endl; - return 0; - } -} // namespace version - -#ifdef _WIN32 -namespace restore_nvprefs_undo { - int - entry(const char *name, int argc, char *argv[]) { - // Restore global NVIDIA control panel settings to the undo file - // left by improper termination of sunshine.exe, if it exists. - // This entry point is typically called by the uninstaller. - if (nvprefs_instance.load()) { - nvprefs_instance.restore_from_and_delete_undo_file_if_exists(); - nvprefs_instance.unload(); - } - return 0; - } -} // namespace restore_nvprefs_undo -#endif - -namespace lifetime { - static char **argv; - static std::atomic_int desired_exit_code; - - /** - * @brief Terminates Sunshine gracefully with the provided exit code. - * @param exit_code The exit code to return from main(). - * @param async Specifies whether our termination will be non-blocking. - */ - void - exit_sunshine(int exit_code, bool async) { - // Store the exit code of the first exit_sunshine() call - int zero = 0; - desired_exit_code.compare_exchange_strong(zero, exit_code); - - // Raise SIGINT to start termination - std::raise(SIGINT); - - // Termination will happen asynchronously, but the caller may - // have wanted synchronous behavior. - while (!async) { - std::this_thread::sleep_for(1s); - } - } - - /** - * @brief Gets the argv array passed to main(). - */ - char ** - get_argv() { - return argv; - } -} // namespace lifetime - -#ifdef _WIN32 -namespace service_ctrl { - class service_controller { - public: - /** - * @brief Constructor for service_controller class. - * @param service_desired_access SERVICE_* desired access flags. - */ - service_controller(DWORD service_desired_access) { - scm_handle = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CONNECT); - if (!scm_handle) { - auto winerr = GetLastError(); - BOOST_LOG(error) << "OpenSCManager() failed: "sv << winerr; - return; - } - - service_handle = OpenServiceA(scm_handle, "SunshineService", service_desired_access); - if (!service_handle) { - auto winerr = GetLastError(); - BOOST_LOG(error) << "OpenService() failed: "sv << winerr; - return; - } - } - - ~service_controller() { - if (service_handle) { - CloseServiceHandle(service_handle); - } - - if (scm_handle) { - CloseServiceHandle(scm_handle); - } - } - - /** - * @brief Asynchronously starts the Sunshine service. - */ - bool - start_service() { - if (!service_handle) { - return false; - } - - if (!StartServiceA(service_handle, 0, nullptr)) { - auto winerr = GetLastError(); - if (winerr != ERROR_SERVICE_ALREADY_RUNNING) { - BOOST_LOG(error) << "StartService() failed: "sv << winerr; - return false; - } - } - - return true; - } - - /** - * @brief Query the service status. - * @param status The SERVICE_STATUS struct to populate. - */ - bool - query_service_status(SERVICE_STATUS &status) { - if (!service_handle) { - return false; - } - - if (!QueryServiceStatus(service_handle, &status)) { - auto winerr = GetLastError(); - BOOST_LOG(error) << "QueryServiceStatus() failed: "sv << winerr; - return false; - } - - return true; - } - - private: - SC_HANDLE scm_handle = NULL; - SC_HANDLE service_handle = NULL; - }; - - /** - * @brief Check if the service is running. - * - * EXAMPLES: - * ```cpp - * is_service_running(); - * ``` - */ - bool - is_service_running() { - service_controller sc { SERVICE_QUERY_STATUS }; - - SERVICE_STATUS status; - if (!sc.query_service_status(status)) { - return false; - } - - return status.dwCurrentState == SERVICE_RUNNING; - } - - /** - * @brief Start the service and wait for startup to complete. - * - * EXAMPLES: - * ```cpp - * start_service(); - * ``` - */ - bool - start_service() { - service_controller sc { SERVICE_QUERY_STATUS | SERVICE_START }; - - std::cout << "Starting Sunshine..."sv; - - // This operation is asynchronous, so we must wait for it to complete - if (!sc.start_service()) { - return false; - } - - SERVICE_STATUS status; - do { - Sleep(1000); - std::cout << '.'; - } while (sc.query_service_status(status) && status.dwCurrentState == SERVICE_START_PENDING); - - if (status.dwCurrentState != SERVICE_RUNNING) { - BOOST_LOG(error) << SERVICE_NAME " failed to start: "sv << status.dwWin32ExitCode; - return false; - } - - std::cout << std::endl; - return true; - } - - /** - * @brief Wait for the UI to be ready after Sunshine startup. - * - * EXAMPLES: - * ```cpp - * wait_for_ui_ready(); - * ``` - */ - bool - wait_for_ui_ready() { - std::cout << "Waiting for Web UI to be ready..."; - - // Wait up to 30 seconds for the web UI to start - for (int i = 0; i < 30; i++) { - PMIB_TCPTABLE tcp_table = nullptr; - ULONG table_size = 0; - ULONG err; - - auto fg = util::fail_guard([&tcp_table]() { - free(tcp_table); - }); - - do { - // Query all open TCP sockets to look for our web UI port - err = GetTcpTable(tcp_table, &table_size, false); - if (err == ERROR_INSUFFICIENT_BUFFER) { - free(tcp_table); - tcp_table = (PMIB_TCPTABLE) malloc(table_size); - } - } while (err == ERROR_INSUFFICIENT_BUFFER); - - if (err != NO_ERROR) { - BOOST_LOG(error) << "Failed to query TCP table: "sv << err; - return false; - } - - uint16_t port_nbo = htons(net::map_port(confighttp::PORT_HTTPS)); - for (DWORD i = 0; i < tcp_table->dwNumEntries; i++) { - auto &entry = tcp_table->table[i]; - - // Look for our port in the listening state - if (entry.dwLocalPort == port_nbo && entry.dwState == MIB_TCP_STATE_LISTEN) { - std::cout << std::endl; - return true; - } - } - - Sleep(1000); - std::cout << '.'; - } - - std::cout << "timed out"sv << std::endl; - return false; - } -} // namespace service_ctrl - -/** - * @brief Checks if NVIDIA's GameStream software is running. - * @return `true` if GameStream is enabled. - */ -bool -is_gamestream_enabled() { - DWORD enabled; - DWORD size = sizeof(enabled); - return RegGetValueW( - HKEY_LOCAL_MACHINE, - L"SOFTWARE\\NVIDIA Corporation\\NvStream", - L"EnableStreaming", - RRF_RT_REG_DWORD, - nullptr, - &enabled, - &size) == ERROR_SUCCESS && - enabled != 0; -} - -#endif - -/** - * @brief Launch the Web UI. - * - * EXAMPLES: - * ```cpp - * launch_ui(); - * ``` - */ -void -launch_ui() { - std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)); - platf::open_url(url); -} - -/** - * @brief Launch the Web UI at a specific endpoint. - * - * EXAMPLES: - * ```cpp - * launch_ui_with_path("/pin"); - * ``` - */ -void -launch_ui_with_path(std::string path) { - std::string url = "https://localhost:" + std::to_string(net::map_port(confighttp::PORT_HTTPS)) + path; - platf::open_url(url); -} - std::map> signal_handlers; void on_signal_forwarder(int sig) { @@ -381,26 +68,12 @@ on_signal(int sig, FN &&fn) { std::signal(sig, on_signal_forwarder); } -namespace gen_creds { - int - entry(const char *name, int argc, char *argv[]) { - if (argc < 2 || argv[0] == "help"sv || argv[1] == "help"sv) { - print_help(name); - return 0; - } - - http::save_user_creds(config::sunshine.credentials_file, argv[0], argv[1]); - - return 0; - } -} // namespace gen_creds - std::map> cmd_to_func { - { "creds"sv, gen_creds::entry }, - { "help"sv, help::entry }, - { "version"sv, version::entry }, + { "creds"sv, args::creds }, + { "help"sv, args::help }, + { "version"sv, args::version }, #ifdef _WIN32 - { "restore-nvprefs-undo"sv, restore_nvprefs_undo::entry }, + { "restore-nvprefs-undo"sv, args::restore_nvprefs_undo }, #endif }; diff --git a/src/main.h b/src/main.h index 02a21fd321e..f34ca6cda66 100644 --- a/src/main.h +++ b/src/main.h @@ -6,73 +6,6 @@ // macros #pragma once -// standard includes -#include -#include - -// local includes -#include "thread_pool.h" -#include "thread_safe.h" - -#ifdef _WIN32 - // Declare global singleton used for NVIDIA control panel modifications - #include "platform/windows/nvprefs/nvprefs_interface.h" -extern nvprefs::nvprefs_interface nvprefs_instance; -#endif - -extern thread_pool_util::ThreadPool task_pool; -extern bool display_cursor; - // functions int main(int argc, char *argv[]); -void -launch_ui(); -void -launch_ui_with_path(std::string path); - -// namespaces -namespace mail { -#define MAIL(x) \ - constexpr auto x = std::string_view { \ - #x \ - } - - extern safe::mail_t man; - - // Global mail - MAIL(shutdown); - MAIL(broadcast_shutdown); - MAIL(video_packets); - MAIL(audio_packets); - MAIL(switch_display); - - // Local mail - MAIL(touch_port); - MAIL(idr); - MAIL(invalidate_ref_frames); - MAIL(gamepad_feedback); - MAIL(hdr); -#undef MAIL - -} // namespace mail - -namespace lifetime { - void - exit_sunshine(int exit_code, bool async); - char ** - get_argv(); -} // namespace lifetime - -#ifdef _WIN32 -namespace service_ctrl { - bool - is_service_running(); - - bool - start_service(); - - bool - wait_for_ui_ready(); -} // namespace service_ctrl -#endif diff --git a/src/nvhttp.cpp b/src/nvhttp.cpp index af211c579b7..0d7c55e1141 100644 --- a/src/nvhttp.cpp +++ b/src/nvhttp.cpp @@ -22,10 +22,10 @@ // local includes #include "config.h" #include "crypto.h" +#include "entry_handler.h" #include "file_handler.h" #include "httpcommon.h" #include "logging.h" -#include "main.h" #include "network.h" #include "nvhttp.h" #include "platform/common.h" diff --git a/src/platform/linux/audio.cpp b/src/platform/linux/audio.cpp index 577287b77ef..0aa96b34389 100644 --- a/src/platform/linux/audio.cpp +++ b/src/platform/linux/audio.cpp @@ -14,8 +14,8 @@ #include "src/platform/common.h" #include "src/config.h" +#include "src/entry_handler.h" #include "src/logging.h" -#include "src/main.h" #include "src/thread_safe.h" namespace platf { diff --git a/src/platform/linux/cuda.cpp b/src/platform/linux/cuda.cpp index 856fecc657a..2c85f854573 100644 --- a/src/platform/linux/cuda.cpp +++ b/src/platform/linux/cuda.cpp @@ -20,7 +20,7 @@ extern "C" { #include "cuda.h" #include "graphics.h" #include "src/logging.h" -#include "src/main.h" +#include "src/thread_pool.h" #include "src/utility.h" #include "src/video.h" #include "wayland.h" diff --git a/src/platform/linux/input.cpp b/src/platform/linux/input.cpp index 43433e58a92..6e7d2b6d460 100644 --- a/src/platform/linux/input.cpp +++ b/src/platform/linux/input.cpp @@ -24,8 +24,8 @@ #include "src/config.h" #include "src/input.h" #include "src/logging.h" -#include "src/main.h" #include "src/platform/common.h" +#include "src/thread_pool.h" #include "src/utility.h" #include "src/platform/common.h" diff --git a/src/platform/linux/kmsgrab.cpp b/src/platform/linux/kmsgrab.cpp index a18fc31a823..39eb64bbd9d 100644 --- a/src/platform/linux/kmsgrab.cpp +++ b/src/platform/linux/kmsgrab.cpp @@ -15,9 +15,9 @@ #include #include "src/logging.h" -#include "src/main.h" #include "src/platform/common.h" #include "src/round_robin.h" +#include "src/thread_pool.h" #include "src/utility.h" #include "src/video.h" diff --git a/src/platform/linux/misc.cpp b/src/platform/linux/misc.cpp index 8ead76b0715..ecc5887e0f1 100644 --- a/src/platform/linux/misc.cpp +++ b/src/platform/linux/misc.cpp @@ -26,8 +26,8 @@ #include "graphics.h" #include "misc.h" #include "src/config.h" +#include "src/entry_handler.h" #include "src/logging.h" -#include "src/main.h" #include "src/platform/common.h" #include "vaapi.h" diff --git a/src/platform/linux/wlgrab.cpp b/src/platform/linux/wlgrab.cpp index 6acde691479..e9182cdd0b1 100644 --- a/src/platform/linux/wlgrab.cpp +++ b/src/platform/linux/wlgrab.cpp @@ -5,7 +5,7 @@ #include "src/platform/common.h" #include "src/logging.h" -#include "src/main.h" +#include "src/thread_pool.h" #include "src/video.h" #include "cuda.h" diff --git a/src/platform/linux/x11grab.cpp b/src/platform/linux/x11grab.cpp index 0c1583002b9..aff3df3183e 100644 --- a/src/platform/linux/x11grab.cpp +++ b/src/platform/linux/x11grab.cpp @@ -17,9 +17,10 @@ #include #include "src/config.h" +#include "src/entry_handler.h" #include "src/logging.h" -#include "src/main.h" #include "src/task_pool.h" +#include "src/thread_pool.h" #include "src/video.h" #include "cuda.h" diff --git a/src/platform/macos/misc.mm b/src/platform/macos/misc.mm index 0a8bf1b78ae..20c2247e049 100644 --- a/src/platform/macos/misc.mm +++ b/src/platform/macos/misc.mm @@ -18,8 +18,8 @@ #include #include "misc.h" +#include "src/entry_handler.h" #include "src/logging.h" -#include "src/main.h" #include "src/platform/common.h" #include diff --git a/src/platform/windows/display_base.cpp b/src/platform/windows/display_base.cpp index 3ae6e337d26..82693746537 100644 --- a/src/platform/windows/display_base.cpp +++ b/src/platform/windows/display_base.cpp @@ -16,9 +16,9 @@ typedef long NTSTATUS; #include "misc.h" #include "src/config.h" #include "src/logging.h" -#include "src/main.h" #include "src/platform/common.h" #include "src/stat_trackers.h" +#include "src/thread_pool.h" #include "src/video.h" namespace platf { diff --git a/src/platform/windows/input.cpp b/src/platform/windows/input.cpp index 144dac1999a..b9484a4fafc 100644 --- a/src/platform/windows/input.cpp +++ b/src/platform/windows/input.cpp @@ -12,9 +12,11 @@ #include "keylayout.h" #include "misc.h" #include "src/config.h" +#include "src/entry_handler.h" #include "src/logging.h" -#include "src/main.h" #include "src/platform/common.h" +#include "src/task_pool.h" +#include "src/thread_pool.h" #ifdef __MINGW32__ DECLARE_HANDLE(HSYNTHETICPOINTERDEVICE); diff --git a/src/platform/windows/misc.cpp b/src/platform/windows/misc.cpp index 1bbfe113a38..0e31f70d86e 100644 --- a/src/platform/windows/misc.cpp +++ b/src/platform/windows/misc.cpp @@ -35,8 +35,8 @@ #define NTDDI_VERSION NTDDI_WIN10 #include +#include "src/entry_handler.h" #include "src/logging.h" -#include "src/main.h" #include "src/platform/common.h" #include "src/utility.h" #include diff --git a/src/platform/windows/publish.cpp b/src/platform/windows/publish.cpp index 47c16721e20..6eb4d8948e1 100644 --- a/src/platform/windows/publish.cpp +++ b/src/platform/windows/publish.cpp @@ -14,7 +14,6 @@ #include "misc.h" #include "src/config.h" #include "src/logging.h" -#include "src/main.h" #include "src/network.h" #include "src/nvhttp.h" #include "src/platform/common.h" diff --git a/src/process.cpp b/src/process.cpp index fef585cdae6..530471dbcfc 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -23,9 +23,9 @@ #include "config.h" #include "crypto.h" #include "logging.h" -#include "main.h" #include "platform/common.h" #include "system_tray.h" +#include "thread_pool.h" #include "utility.h" #ifdef _WIN32 diff --git a/src/rtsp.cpp b/src/rtsp.cpp index 73f224b8ef1..b5b98cbc5c9 100644 --- a/src/rtsp.cpp +++ b/src/rtsp.cpp @@ -16,9 +16,9 @@ extern "C" { #include #include "config.h" +#include "entry_handler.h" #include "input.h" #include "logging.h" -#include "main.h" #include "network.h" #include "rtsp.h" #include "stream.h" diff --git a/src/stream.cpp b/src/stream.cpp index 12fb8663fc8..0cabc750e76 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -18,14 +18,15 @@ extern "C" { } #include "config.h" +#include "entry_handler.h" #include "input.h" #include "logging.h" -#include "main.h" #include "network.h" #include "stat_trackers.h" #include "stream.h" #include "sync.h" #include "system_tray.h" +#include "thread_pool.h" #include "thread_safe.h" #include "utility.h" diff --git a/src/system_tray.cpp b/src/system_tray.cpp index 621ace0cfb9..39131ba30fe 100644 --- a/src/system_tray.cpp +++ b/src/system_tray.cpp @@ -38,9 +38,9 @@ // local includes #include "confighttp.h" #include "logging.h" - #include "main.h" #include "platform/common.h" #include "process.h" + #include "src/entry_handler.h" #include "version.h" using namespace std::literals; diff --git a/src/upnp.cpp b/src/upnp.cpp index 55c49aaf67c..a809228bd91 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -7,8 +7,8 @@ #include "config.h" #include "confighttp.h" +#include "entry_handler.h" #include "logging.h" -#include "main.h" #include "network.h" #include "nvhttp.h" #include "rtsp.h" diff --git a/src/video.cpp b/src/video.cpp index e013249880b..07b2eaa39cb 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -19,12 +19,13 @@ extern "C" { #include "cbs.h" #include "config.h" +#include "entry_handler.h" #include "input.h" #include "logging.h" -#include "main.h" #include "nvenc/nvenc_base.h" #include "platform/common.h" #include "sync.h" +#include "thread_pool.h" #include "video.h" #ifdef _WIN32 From 2673bb96f6ba643117b332c509d7440749ed4b49 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 10 Feb 2024 14:07:53 -0500 Subject: [PATCH 02/10] refactor(main): move globals out of main --- cmake/compile_definitions/common.cmake | 2 ++ src/entry_handler.h | 3 --- src/globals.cpp | 9 +++++++++ src/globals.h | 12 ++++++++++++ src/input.cpp | 1 + src/main.cpp | 7 +------ src/platform/linux/x11grab.cpp | 1 + src/platform/windows/input.cpp | 1 + src/stream.cpp | 1 + src/video.cpp | 1 + 10 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 src/globals.cpp create mode 100644 src/globals.h diff --git a/cmake/compile_definitions/common.cmake b/cmake/compile_definitions/common.cmake index 47f32acf20b..94f1ac598cc 100644 --- a/cmake/compile_definitions/common.cmake +++ b/cmake/compile_definitions/common.cmake @@ -49,6 +49,8 @@ set(SUNSHINE_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/entry_handler.h" "${CMAKE_SOURCE_DIR}/src/file_handler.cpp" "${CMAKE_SOURCE_DIR}/src/file_handler.h" + "${CMAKE_SOURCE_DIR}/src/globals.cpp" + "${CMAKE_SOURCE_DIR}/src/globals.h" "${CMAKE_SOURCE_DIR}/src/logging.cpp" "${CMAKE_SOURCE_DIR}/src/logging.h" "${CMAKE_SOURCE_DIR}/src/main.cpp" diff --git a/src/entry_handler.h b/src/entry_handler.h index ba7dff9ba49..99d071e9185 100644 --- a/src/entry_handler.h +++ b/src/entry_handler.h @@ -12,9 +12,6 @@ #include "thread_pool.h" #include "thread_safe.h" -extern thread_pool_util::ThreadPool task_pool; -extern bool display_cursor; - // functions void launch_ui(); diff --git a/src/globals.cpp b/src/globals.cpp new file mode 100644 index 00000000000..e753b4c0cf2 --- /dev/null +++ b/src/globals.cpp @@ -0,0 +1,9 @@ +/** + * @file globals.cpp + * @brief Implementation for globally accessible variables and functions. + */ +#include "globals.h" + +safe::mail_t mail::man; +thread_pool_util::ThreadPool task_pool; +bool display_cursor = true; diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 00000000000..3078e19dad5 --- /dev/null +++ b/src/globals.h @@ -0,0 +1,12 @@ +/** + * @file globals.h + * @brief Header for globally accessible variables and functions. + */ +#pragma once + +#include "entry_handler.h" +#include "thread_pool.h" + +extern safe::mail_t mail::man; +extern thread_pool_util::ThreadPool task_pool; +extern bool display_cursor; diff --git a/src/input.cpp b/src/input.cpp index c48b86842da..f47a2f20a67 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -18,6 +18,7 @@ extern "C" { #include "config.h" #include "entry_handler.h" +#include "globals.h" #include "input.h" #include "logging.h" #include "platform/common.h" diff --git a/src/main.cpp b/src/main.cpp index 62524998727..66dda6cf105 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,7 @@ // local includes #include "confighttp.h" #include "entry_handler.h" +#include "globals.h" #include "httpcommon.h" #include "logging.h" #include "main.h" @@ -38,15 +39,9 @@ extern "C" { #include } -safe::mail_t mail::man; - using namespace std::literals; namespace bl = boost::log; -thread_pool_util::ThreadPool task_pool; - -bool display_cursor = true; - struct NoDelete { void operator()(void *) {} diff --git a/src/platform/linux/x11grab.cpp b/src/platform/linux/x11grab.cpp index aff3df3183e..86625a58d70 100644 --- a/src/platform/linux/x11grab.cpp +++ b/src/platform/linux/x11grab.cpp @@ -18,6 +18,7 @@ #include "src/config.h" #include "src/entry_handler.h" +#include "src/globals.h" #include "src/logging.h" #include "src/task_pool.h" #include "src/thread_pool.h" diff --git a/src/platform/windows/input.cpp b/src/platform/windows/input.cpp index b9484a4fafc..75199164f58 100644 --- a/src/platform/windows/input.cpp +++ b/src/platform/windows/input.cpp @@ -13,6 +13,7 @@ #include "misc.h" #include "src/config.h" #include "src/entry_handler.h" +#include "src/globals.h" #include "src/logging.h" #include "src/platform/common.h" #include "src/task_pool.h" diff --git a/src/stream.cpp b/src/stream.cpp index 0cabc750e76..513ca3f7923 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -19,6 +19,7 @@ extern "C" { #include "config.h" #include "entry_handler.h" +#include "globals.h" #include "input.h" #include "logging.h" #include "network.h" diff --git a/src/video.cpp b/src/video.cpp index 07b2eaa39cb..3f0d920ddb0 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -20,6 +20,7 @@ extern "C" { #include "cbs.h" #include "config.h" #include "entry_handler.h" +#include "globals.h" #include "input.h" #include "logging.h" #include "nvenc/nvenc_base.h" From 19769a5406346e1996851d99457e18a5d64ff1f7 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 10 Feb 2024 18:34:49 -0500 Subject: [PATCH 03/10] include thread and remove dangling entry_handler includes --- src/config.cpp | 2 +- src/entry_handler.cpp | 1 - src/input.cpp | 1 - src/platform/linux/audio.cpp | 2 +- src/platform/linux/cuda.cpp | 4 +--- src/platform/linux/input.cpp | 2 +- src/platform/linux/kmsgrab.cpp | 2 +- src/platform/linux/wlgrab.cpp | 3 ++- src/platform/linux/x11grab.cpp | 3 +-- src/platform/windows/display_base.cpp | 2 +- src/platform/windows/input.cpp | 4 +--- src/process.cpp | 2 +- src/stream.cpp | 2 -- src/video.cpp | 2 -- 14 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index b7cabca27c2..d9aaab3e31c 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -20,7 +21,6 @@ #include "logging.h" #include "nvhttp.h" #include "rtsp.h" -#include "thread_pool.h" #include "utility.h" #include "platform/common.h" diff --git a/src/entry_handler.cpp b/src/entry_handler.cpp index 7007ea4670e..f4965a5623c 100644 --- a/src/entry_handler.cpp +++ b/src/entry_handler.cpp @@ -16,7 +16,6 @@ #include "logging.h" #include "network.h" #include "platform/common.h" -#include "thread_pool.h" #include "version.h" extern "C" { diff --git a/src/input.cpp b/src/input.cpp index f47a2f20a67..89f7291f11a 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -17,7 +17,6 @@ extern "C" { #include #include "config.h" -#include "entry_handler.h" #include "globals.h" #include "input.h" #include "logging.h" diff --git a/src/platform/linux/audio.cpp b/src/platform/linux/audio.cpp index 0aa96b34389..e663c811ba2 100644 --- a/src/platform/linux/audio.cpp +++ b/src/platform/linux/audio.cpp @@ -4,6 +4,7 @@ */ #include #include +#include #include @@ -14,7 +15,6 @@ #include "src/platform/common.h" #include "src/config.h" -#include "src/entry_handler.h" #include "src/logging.h" #include "src/thread_safe.h" diff --git a/src/platform/linux/cuda.cpp b/src/platform/linux/cuda.cpp index 2c85f854573..5b121c70691 100644 --- a/src/platform/linux/cuda.cpp +++ b/src/platform/linux/cuda.cpp @@ -3,10 +3,9 @@ * @brief todo */ #include - #include - #include +#include #include #include @@ -20,7 +19,6 @@ extern "C" { #include "cuda.h" #include "graphics.h" #include "src/logging.h" -#include "src/thread_pool.h" #include "src/utility.h" #include "src/video.h" #include "wayland.h" diff --git a/src/platform/linux/input.cpp b/src/platform/linux/input.cpp index 6e7d2b6d460..85818110730 100644 --- a/src/platform/linux/input.cpp +++ b/src/platform/linux/input.cpp @@ -20,12 +20,12 @@ #include #include #include +#include #include "src/config.h" #include "src/input.h" #include "src/logging.h" #include "src/platform/common.h" -#include "src/thread_pool.h" #include "src/utility.h" #include "src/platform/common.h" diff --git a/src/platform/linux/kmsgrab.cpp b/src/platform/linux/kmsgrab.cpp index 39eb64bbd9d..192482deaa1 100644 --- a/src/platform/linux/kmsgrab.cpp +++ b/src/platform/linux/kmsgrab.cpp @@ -13,11 +13,11 @@ #include #include +#include #include "src/logging.h" #include "src/platform/common.h" #include "src/round_robin.h" -#include "src/thread_pool.h" #include "src/utility.h" #include "src/video.h" diff --git a/src/platform/linux/wlgrab.cpp b/src/platform/linux/wlgrab.cpp index e9182cdd0b1..84b69bd0be0 100644 --- a/src/platform/linux/wlgrab.cpp +++ b/src/platform/linux/wlgrab.cpp @@ -2,10 +2,11 @@ * @file src/platform/linux/wlgrab.cpp * @brief todo */ +#include + #include "src/platform/common.h" #include "src/logging.h" -#include "src/thread_pool.h" #include "src/video.h" #include "cuda.h" diff --git a/src/platform/linux/x11grab.cpp b/src/platform/linux/x11grab.cpp index 86625a58d70..1167d3f5809 100644 --- a/src/platform/linux/x11grab.cpp +++ b/src/platform/linux/x11grab.cpp @@ -5,6 +5,7 @@ #include "src/platform/common.h" #include +#include #include #include @@ -17,11 +18,9 @@ #include #include "src/config.h" -#include "src/entry_handler.h" #include "src/globals.h" #include "src/logging.h" #include "src/task_pool.h" -#include "src/thread_pool.h" #include "src/video.h" #include "cuda.h" diff --git a/src/platform/windows/display_base.cpp b/src/platform/windows/display_base.cpp index 82693746537..78c927e7782 100644 --- a/src/platform/windows/display_base.cpp +++ b/src/platform/windows/display_base.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -18,7 +19,6 @@ typedef long NTSTATUS; #include "src/logging.h" #include "src/platform/common.h" #include "src/stat_trackers.h" -#include "src/thread_pool.h" #include "src/video.h" namespace platf { diff --git a/src/platform/windows/input.cpp b/src/platform/windows/input.cpp index 75199164f58..5056c2c7246 100644 --- a/src/platform/windows/input.cpp +++ b/src/platform/windows/input.cpp @@ -6,18 +6,16 @@ #include #include +#include #include #include "keylayout.h" #include "misc.h" #include "src/config.h" -#include "src/entry_handler.h" #include "src/globals.h" #include "src/logging.h" #include "src/platform/common.h" -#include "src/task_pool.h" -#include "src/thread_pool.h" #ifdef __MINGW32__ DECLARE_HANDLE(HSYNTHETICPOINTERDEVICE); diff --git a/src/process.cpp b/src/process.cpp index 530471dbcfc..e660e8162a1 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -25,7 +26,6 @@ #include "logging.h" #include "platform/common.h" #include "system_tray.h" -#include "thread_pool.h" #include "utility.h" #ifdef _WIN32 diff --git a/src/stream.cpp b/src/stream.cpp index 513ca3f7923..b9238057b1c 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -18,7 +18,6 @@ extern "C" { } #include "config.h" -#include "entry_handler.h" #include "globals.h" #include "input.h" #include "logging.h" @@ -27,7 +26,6 @@ extern "C" { #include "stream.h" #include "sync.h" #include "system_tray.h" -#include "thread_pool.h" #include "thread_safe.h" #include "utility.h" diff --git a/src/video.cpp b/src/video.cpp index 3f0d920ddb0..636bcf60138 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -19,14 +19,12 @@ extern "C" { #include "cbs.h" #include "config.h" -#include "entry_handler.h" #include "globals.h" #include "input.h" #include "logging.h" #include "nvenc/nvenc_base.h" #include "platform/common.h" #include "sync.h" -#include "thread_pool.h" #include "video.h" #ifdef _WIN32 From 5be6cfba9ea0abdf480b7c5749461d3fef1237ba Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 10 Feb 2024 18:36:50 -0500 Subject: [PATCH 04/10] docs(src): add globals documentation --- docs/source/source_code/src/globals.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/source/source_code/src/globals.rst diff --git a/docs/source/source_code/src/globals.rst b/docs/source/source_code/src/globals.rst new file mode 100644 index 00000000000..ed70cecf692 --- /dev/null +++ b/docs/source/source_code/src/globals.rst @@ -0,0 +1,5 @@ +globals +======= + +.. doxygenfile:: globals.h + :allow-dot-graphs: From 6d595c562b7069696c4ded67907b1fc581027235 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 10 Feb 2024 18:52:29 -0500 Subject: [PATCH 05/10] remove unused imports from main.cpp --- src/main.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 66dda6cf105..19af144ddeb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,17 +5,11 @@ // standard includes #include -#include #include #include -#include // lib includes -#include -#include #include -#include -#include // local includes #include "confighttp.h" @@ -24,18 +18,14 @@ #include "httpcommon.h" #include "logging.h" #include "main.h" -#include "network.h" #include "nvhttp.h" #include "process.h" -#include "rtsp.h" #include "system_tray.h" -#include "thread_pool.h" #include "upnp.h" #include "version.h" #include "video.h" extern "C" { -#include #include } From 7b18b7aece057947336ec38e029c74953cbde0a8 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 10 Feb 2024 21:06:07 -0500 Subject: [PATCH 06/10] refactor(mail): move mail to globals --- src/audio.cpp | 2 +- src/confighttp.cpp | 2 +- src/entry_handler.h | 25 ------------------------- src/globals.h | 26 +++++++++++++++++++++++++- src/nvhttp.cpp | 2 +- src/rtsp.cpp | 2 +- src/upnp.cpp | 2 +- 7 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index bf4680d1202..1995e380ea7 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -10,7 +10,7 @@ #include "audio.h" #include "config.h" -#include "entry_handler.h" +#include "globals.h" #include "logging.h" #include "thread_safe.h" #include "utility.h" diff --git a/src/confighttp.cpp b/src/confighttp.cpp index 856d85b6f5c..0657902dd16 100644 --- a/src/confighttp.cpp +++ b/src/confighttp.cpp @@ -29,8 +29,8 @@ #include "config.h" #include "confighttp.h" #include "crypto.h" -#include "entry_handler.h" #include "file_handler.h" +#include "globals.h" #include "httpcommon.h" #include "logging.h" #include "network.h" diff --git a/src/entry_handler.h b/src/entry_handler.h index 99d071e9185..24dc0640cb9 100644 --- a/src/entry_handler.h +++ b/src/entry_handler.h @@ -50,31 +50,6 @@ namespace lifetime { get_argv(); } // namespace lifetime -namespace mail { -#define MAIL(x) \ - constexpr auto x = std::string_view { \ - #x \ - } - - extern safe::mail_t man; - - // Global mail - MAIL(shutdown); - MAIL(broadcast_shutdown); - MAIL(video_packets); - MAIL(audio_packets); - MAIL(switch_display); - - // Local mail - MAIL(touch_port); - MAIL(idr); - MAIL(invalidate_ref_frames); - MAIL(gamepad_feedback); - MAIL(hdr); -#undef MAIL - -} // namespace mail - #ifdef _WIN32 namespace service_ctrl { bool diff --git a/src/globals.h b/src/globals.h index 3078e19dad5..c62cbaaf3fb 100644 --- a/src/globals.h +++ b/src/globals.h @@ -7,6 +7,30 @@ #include "entry_handler.h" #include "thread_pool.h" -extern safe::mail_t mail::man; extern thread_pool_util::ThreadPool task_pool; extern bool display_cursor; + +namespace mail { +#define MAIL(x) \ + constexpr auto x = std::string_view { \ + #x \ + } + + extern safe::mail_t man; + + // Global mail + MAIL(shutdown); + MAIL(broadcast_shutdown); + MAIL(video_packets); + MAIL(audio_packets); + MAIL(switch_display); + + // Local mail + MAIL(touch_port); + MAIL(idr); + MAIL(invalidate_ref_frames); + MAIL(gamepad_feedback); + MAIL(hdr); +#undef MAIL + +} // namespace mail diff --git a/src/nvhttp.cpp b/src/nvhttp.cpp index 0d7c55e1141..b8bddb44bb7 100644 --- a/src/nvhttp.cpp +++ b/src/nvhttp.cpp @@ -22,8 +22,8 @@ // local includes #include "config.h" #include "crypto.h" -#include "entry_handler.h" #include "file_handler.h" +#include "globals.h" #include "httpcommon.h" #include "logging.h" #include "network.h" diff --git a/src/rtsp.cpp b/src/rtsp.cpp index b5b98cbc5c9..0180fbee37a 100644 --- a/src/rtsp.cpp +++ b/src/rtsp.cpp @@ -16,7 +16,7 @@ extern "C" { #include #include "config.h" -#include "entry_handler.h" +#include "globals.h" #include "input.h" #include "logging.h" #include "network.h" diff --git a/src/upnp.cpp b/src/upnp.cpp index a809228bd91..f65bcb87cc4 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -7,7 +7,7 @@ #include "config.h" #include "confighttp.h" -#include "entry_handler.h" +#include "globals.h" #include "logging.h" #include "network.h" #include "nvhttp.h" From dc2d4161f1b15d3905d06992953145839f5ec82b Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 10 Feb 2024 22:03:36 -0500 Subject: [PATCH 07/10] revert removal of `#include ` --- src/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.cpp b/src/main.cpp index 19af144ddeb..cbe481ee2e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,7 @@ #include "video.h" extern "C" { +#include #include } From 2e8d51ec70453c030703dfd92a59d3f02199f6c4 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sun, 11 Feb 2024 09:36:39 -0500 Subject: [PATCH 08/10] docs(src): add documentation blocks to globals.cpp --- src/globals.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/globals.cpp b/src/globals.cpp index e753b4c0cf2..0e66faf2e04 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -4,6 +4,17 @@ */ #include "globals.h" +/** + * @brief A process-wide communication mechanism. + */ safe::mail_t mail::man; + +/** + * @brief A thread pool for processing tasks. + */ thread_pool_util::ThreadPool task_pool; + +/** + * @brief A boolean flag to indicate whether the cursor should be displayed. + */ bool display_cursor = true; From b18b6ce3959add3f301672adebee107813388e02 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sun, 11 Feb 2024 09:37:12 -0500 Subject: [PATCH 09/10] refactor(globals): move nvprefs_instance to globals --- src/entry_handler.cpp | 6 +----- src/entry_handler.h | 4 ---- src/globals.cpp | 7 +++++++ src/globals.h | 6 ++++++ src/platform/windows/misc.cpp | 1 + 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/entry_handler.cpp b/src/entry_handler.cpp index f4965a5623c..42fad103cbf 100644 --- a/src/entry_handler.cpp +++ b/src/entry_handler.cpp @@ -12,6 +12,7 @@ #include "config.h" #include "confighttp.h" #include "entry_handler.h" +#include "globals.h" #include "httpcommon.h" #include "logging.h" #include "network.h" @@ -54,11 +55,6 @@ launch_ui_with_path(std::string path) { platf::open_url(url); } -#ifdef _WIN32 -// Define global singleton used for NVIDIA control panel modifications -nvprefs::nvprefs_interface nvprefs_instance; -#endif - namespace args { int creds(const char *name, int argc, char *argv[]) { diff --git a/src/entry_handler.h b/src/entry_handler.h index 24dc0640cb9..c58d0325d70 100644 --- a/src/entry_handler.h +++ b/src/entry_handler.h @@ -19,10 +19,6 @@ void launch_ui_with_path(std::string path); #ifdef _WIN32 - // Declare global singleton used for NVIDIA control panel modifications - #include "platform/windows/nvprefs/nvprefs_interface.h" -extern nvprefs::nvprefs_interface nvprefs_instance; - // windows only functions bool is_gamestream_enabled(); diff --git a/src/globals.cpp b/src/globals.cpp index 0e66faf2e04..ae6c7544360 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -18,3 +18,10 @@ thread_pool_util::ThreadPool task_pool; * @brief A boolean flag to indicate whether the cursor should be displayed. */ bool display_cursor = true; + +#ifdef _WIN32 +/** + * @brief A global singleton used for NVIDIA control panel modifications. + */ +nvprefs::nvprefs_interface nvprefs_instance; +#endif diff --git a/src/globals.h b/src/globals.h index c62cbaaf3fb..a137bc9c4d5 100644 --- a/src/globals.h +++ b/src/globals.h @@ -10,6 +10,12 @@ extern thread_pool_util::ThreadPool task_pool; extern bool display_cursor; +#ifdef _WIN32 + // Declare global singleton used for NVIDIA control panel modifications + #include "platform/windows/nvprefs/nvprefs_interface.h" +extern nvprefs::nvprefs_interface nvprefs_instance; +#endif + namespace mail { #define MAIL(x) \ constexpr auto x = std::string_view { \ diff --git a/src/platform/windows/misc.cpp b/src/platform/windows/misc.cpp index 0e31f70d86e..708fd267484 100644 --- a/src/platform/windows/misc.cpp +++ b/src/platform/windows/misc.cpp @@ -36,6 +36,7 @@ #include #include "src/entry_handler.h" +#include "src/globals.h" #include "src/logging.h" #include "src/platform/common.h" #include "src/utility.h" From e0a66d09f2baba7a6020ae25680623ce43c80c66 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sun, 11 Feb 2024 09:58:19 -0500 Subject: [PATCH 10/10] docs(src): add/update documentation blocks for entry_handler.cpp --- src/entry_handler.cpp | 60 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/entry_handler.cpp b/src/entry_handler.cpp index 42fad103cbf..c7719eb0ed1 100644 --- a/src/entry_handler.cpp +++ b/src/entry_handler.cpp @@ -56,11 +56,22 @@ launch_ui_with_path(std::string path) { } namespace args { + /** + * @brief Reset the user credentials. + * + * @param name The name of the program. + * @param argc The number of arguments. + * @param argv The arguments. + * + * EXAMPLES: + * ```cpp + * creds("sunshine", 2, {"new_username", "new_password"}); + * ``` + */ int creds(const char *name, int argc, char *argv[]) { if (argc < 2 || argv[0] == "help"sv || argv[1] == "help"sv) { - print_help(name); - return 0; + help(name, argc, argv); } http::save_user_creds(config::sunshine.credentials_file, argv[0], argv[1]); @@ -68,12 +79,34 @@ namespace args { return 0; } + /** + * @brief Print help to stdout, then exit. + * @param name The name of the program. + * @param argc The number of arguments. (Unused) + * @param argv The arguments. (Unused) + * + * EXAMPLES: + * ```cpp + * print_help("sunshine", 0, nullptr); + * ``` + */ int help(const char *name, int argc, char *argv[]) { print_help(name); return 0; } + /** + * @brief Print the version to stdout, then exit. + * @param name The name of the program. (Unused) + * @param argc The number of arguments. (Unused) + * @param argv The arguments. (Unused) + * + * EXAMPLES: + * ```cpp + * version("sunshine", 0, nullptr); + * ``` + */ int version(const char *name, int argc, char *argv[]) { std::cout << PROJECT_NAME << " version: v" << PROJECT_VER << std::endl; @@ -81,11 +114,24 @@ namespace args { } #ifdef _WIN32 + /** + * @brief Restore global NVIDIA control panel settings. + * + * If Sunshine was improperly terminated, this function restores + * the global NVIDIA control panel settings to the undo file left + * by Sunshine. This function is typically called by the uninstaller. + * + * @param name The name of the program. (Unused) + * @param argc The number of arguments. (Unused) + * @param argv The arguments. (Unused) + * + * EXAMPLES: + * ```cpp + * restore_nvprefs_undo("sunshine", 0, nullptr); + * ``` + */ int restore_nvprefs_undo(const char *name, int argc, char *argv[]) { - // Restore global NVIDIA control panel settings to the undo file - // left by improper termination of sunshine.exe, if it exists. - // This entry point is typically called by the uninstaller. if (nvprefs_instance.load()) { nvprefs_instance.restore_from_and_delete_undo_file_if_exists(); nvprefs_instance.unload(); @@ -131,8 +177,8 @@ namespace lifetime { #ifdef _WIN32 /** - * @brief Checks if NVIDIA's GameStream software is running. - * @return `true` if GameStream is enabled. + * @brief Check if NVIDIA's GameStream software is running. + * @return `true` if GameStream is enabled, `false` otherwise. */ bool is_gamestream_enabled() {