Files
Sunshine/src/system_tray.h
T
ReenigneArcher 15b06f1707 refactor(config): inject owned settings into runtime services
Replace namespace configuration state with an application-owned store. Pass immutable startup settings into services, platform capture and encoder contexts, network helpers, CLI commands, and tray callbacks. Keep runtime credentials in synchronized snapshots and gamepad policy in its input owner.

Validation: UCRT64 sunshine, test_sunshine, audio-info, and sunshinesvc builds with tray support; 566 regression tests passed and 5 device/workflow tests skipped; strict production Doxygen, required clang-format, and CLI smoke checks passed. Full Linux/macOS builds, hardware coverage, and hosted Sonar reanalysis remain CI validation.
2026-10-11 14:37:32 -04:00

239 lines
7.6 KiB
C++

/**
* @file src/system_tray.h
* @brief Declarations for the system tray icon and notification system.
*/
#pragma once
#include "config.h"
// standard includes
#include <memory>
#include <string>
#include <string_view>
namespace lifetime {
class controller_t;
}
namespace display_device {
class manager_t;
}
namespace safe {
template<class T>
class event_t;
}
#if defined(_WIN32) || defined(__APPLE__)
namespace lvh {
struct LicenseStatus;
}
#endif
/**
* @brief Handles the system tray icon and notification system.
*/
namespace system_tray {
/**
* @brief Bind native tray callbacks to the application's scoped owners.
*/
class application_binding_t {
public:
/**
* @brief Attach callback userdata before creating the native tray.
* @param settings Application settings used by native callbacks.
* @param lifetime Application termination controller.
* @param displays Display restoration owner.
*/
application_binding_t(const config::settings_t &settings, lifetime::controller_t &lifetime, display_device::manager_t &displays);
/** @brief Stop native callbacks before their dependencies are destroyed. */
~application_binding_t();
application_binding_t(const application_binding_t &) = delete;
application_binding_t &operator=(const application_binding_t &) = delete;
const config::settings_t &settings; ///< Settings used by native UI callbacks.
lifetime::controller_t &lifetime; ///< Application termination controller.
display_device::manager_t &displays; ///< Display restoration owner.
};
/**
* @brief Callback for opening the UI from the system tray.
* @param item The tray menu item.
*/
void tray_open_ui_cb([[maybe_unused]] struct tray_menu *item);
/**
* @brief Callback for opening GitHub Sponsors from the system tray.
* @param item The tray menu item.
*/
void tray_donate_github_cb([[maybe_unused]] struct tray_menu *item);
/**
* @brief Callback for opening Patreon from the system tray.
* @param item The tray menu item.
*/
void tray_donate_patreon_cb([[maybe_unused]] struct tray_menu *item);
/**
* @brief Callback for opening PayPal donation from the system tray.
* @param item The tray menu item.
*/
void tray_donate_paypal_cb([[maybe_unused]] struct tray_menu *item);
#if defined(_WIN32) || defined(__APPLE__)
/**
* @brief Callback for opening Virtual HID Broker license settings in the Web UI.
* @param item The tray menu item.
*/
void tray_virtualhid_license_cb([[maybe_unused]] struct tray_menu *item);
/**
* @brief Callback for opening the latest Virtual HID Broker release.
* @param item The tray menu item.
*/
void tray_virtualhid_download_cb([[maybe_unused]] struct tray_menu *item);
#endif
/**
* @brief Callback for resetting display device configuration.
* @param item The tray menu item.
*/
void tray_reset_display_device_config_cb([[maybe_unused]] struct tray_menu *item);
/**
* @brief Callback for restarting Sunshine from the system tray.
* @param item The tray menu item.
*/
void tray_restart_cb([[maybe_unused]] struct tray_menu *item);
/**
* @brief Callback for exiting Sunshine from the system tray.
* @param item The tray menu item.
*/
void tray_quit_cb([[maybe_unused]] struct tray_menu *item);
/**
* @brief Initializes the system tray without starting a loop.
* @return 0 if initialization was successful, non-zero otherwise.
*/
int init_tray();
/**
* @brief Processes a single tray event iteration.
* @return 0 if processing was successful, non-zero otherwise.
*/
int process_tray_events();
/**
* @brief Process tray events until exit and notify the application's workers.
*
* @param shutdown_event Event used to stop Sunshine's server threads.
*/
void run_tray_until_exit(const std::shared_ptr<safe::event_t<bool>> &shutdown_event);
/**
* @brief Exit the system tray.
* @return 0 after exiting the system tray.
*/
int end_tray();
/**
* @brief Sets the tray icon in playing mode and spawns the appropriate notification
* @param app_name The started application name
*/
void update_tray_playing(std::string app_name);
/**
* @brief Sets the tray icon in pausing mode (stream stopped but app running) and spawns the appropriate notification
* @param app_name The paused application name
*/
void update_tray_pausing(std::string app_name);
/**
* @brief Sets the tray icon in stopped mode (app and stream stopped) and spawns the appropriate notification
* @param app_name The started application name
*/
void update_tray_stopped(std::string app_name);
/**
* @brief Spawns a notification for PIN Pairing. Clicking it opens the PIN Web UI Page
*/
void update_tray_require_pin();
#if defined(_WIN32) || defined(__APPLE__)
/**
* @brief Update the Virtual HID Broker license submenu and optional notification.
*
* @param settings Application input configuration.
* @param license Latest machine license details.
* @param notify_if_unlicensed Whether to notify the user when the broker needs a license.
*/
void update_tray_virtualhid_license(const config::settings_t &settings, const lvh::LicenseStatus &license, bool notify_if_unlicensed);
/**
* @brief Query the Virtual HID Broker license and prepare the startup tray state.
* @param settings Application input configuration.
*/
void prepare_tray_virtualhid_license(const config::settings_t &settings);
/**
* @brief Show an update notification for an unsupported Virtual HID Broker.
*
* Existing notifications are preserved when gamepad input is disabled, the broker is absent or supported,
* or the Windows backend choice does not use the broker.
*
* @param installed Whether the broker is installed.
* @param settings Application input configuration.
* @param version Installed broker version.
* @param version_compatible Whether Sunshine supports the installed version.
* @param supported_versions User-visible supported version range.
*/
void update_tray_virtualhid_driver(const config::settings_t &settings, bool installed, std::string_view version, bool version_compatible, std::string_view supported_versions);
/**
* @brief Query the Virtual HID Broker version and prepare its startup notification.
* @param settings Application input configuration.
*/
void prepare_tray_virtualhid_driver(const config::settings_t &settings);
#endif
/**
* @brief Initializes and runs the system tray in a separate thread.
* @return 0 if initialization was successful, non-zero otherwise.
*/
int init_tray_threaded();
#ifdef SUNSHINE_TESTS
/**
* @brief Get the tray data used by the system tray implementation.
*
* @return Read-only tray data for unit-test assertions.
*/
const struct tray &tray_data_for_testing();
/**
* @brief Check whether the system tray is initialized.
*
* @return true if the system tray is initialized; otherwise, false.
*/
bool tray_initialized_for_testing();
/**
* @brief Restore the persistent tray data to its initial state between tests.
*/
void reset_tray_data_for_testing();
/**
* @brief Resolve a tray resource path using the production platform logic.
*
* @param relative_path Resource path relative to the executable or application bundle.
* @return Stable resource path used by the tray backend.
*/
const char *resource_path_for_testing(const char *relative_path);
/**
* @brief Resolve all tray icon paths using the production platform logic.
*/
void resolve_tray_icon_paths_for_testing();
#endif
} // namespace system_tray