47 static std::atomic tray_initialized =
false;
50 static std::thread tray_thread;
51 static std::atomic tray_thread_running =
false;
52 static std::atomic tray_thread_should_exit =
false;
55 BOOST_LOG(info) <<
"Opening UI from system tray"sv;
60 tray_force_stop_cb(
struct tray_menu *item) {
61 BOOST_LOG(info) <<
"Force stop from system tray"sv;
62 proc::proc.terminate();
66 BOOST_LOG(info) <<
"Restarting from system tray"sv;
68 proc::proc.terminate();
73 BOOST_LOG(info) <<
"Quitting from system tray"sv;
75 proc::proc.terminate();
82 static struct tray tray = {
84 .tooltip = PROJECT_NAME,
86 (
struct tray_menu[]) {
88 { .text =
"Open " PROJECT_NAME, .cb = tray_open_ui_cb },
100 { .text = TRAY_MSG_NO_APP_RUNNING, .cb = tray_force_stop_cb },
106 .allIconPaths = {TRAY_ICON, TRAY_ICON_LOCKED, TRAY_ICON_PLAYING, TRAY_ICON_PAUSING},
111 if (tray_init(&tray) < 0) {
112 BOOST_LOG(warning) <<
"Failed to create system tray"sv;
116 BOOST_LOG(info) <<
"System tray created"sv;
117 tray_initialized =
true;
122 if (!tray_initialized) {
127 if (
const int result = tray_loop(0); result != 0) {
128 BOOST_LOG(warning) <<
"System tray loop failed"sv;
136 if (tray_initialized) {
137 tray_initialized =
false;
144 if (!tray_initialized) {
148 tray.notification_title =
nullptr;
149 tray.notification_text =
nullptr;
150 tray.notification_cb =
nullptr;
151 tray.notification_icon =
nullptr;
152 tray.icon = TRAY_ICON_PLAYING;
155 tray.icon = TRAY_ICON_PLAYING;
156 tray.notification_title =
"App launched";
158 static char force_close_msg[256];
159 snprintf(msg, std::size(msg),
"%s launched.", app_name.c_str());
160 snprintf(force_close_msg, std::size(force_close_msg),
"Force close [%s]", app_name.c_str());
161 tray.notification_text = msg;
162 tray.notification_icon = TRAY_ICON_PLAYING;
163 tray.tooltip = PROJECT_NAME;
164 tray.menu[2].text = force_close_msg;
169 if (!tray_initialized) {
173 tray.notification_title =
nullptr;
174 tray.notification_text =
nullptr;
175 tray.notification_cb =
nullptr;
176 tray.notification_icon =
nullptr;
177 tray.icon = TRAY_ICON_PAUSING;
180 snprintf(msg, std::size(msg),
"Streaming paused for %s", app_name.c_str());
181 tray.icon = TRAY_ICON_PAUSING;
182 tray.notification_title =
"Stream Paused";
183 tray.notification_text = msg;
184 tray.notification_icon = TRAY_ICON_PAUSING;
185 tray.tooltip = PROJECT_NAME;
190 if (!tray_initialized) {
194 tray.notification_title =
nullptr;
195 tray.notification_text =
nullptr;
196 tray.notification_cb =
nullptr;
197 tray.notification_icon =
nullptr;
198 tray.icon = TRAY_ICON;
201 snprintf(msg, std::size(msg),
"Streaming stopped for %s", app_name.c_str());
202 tray.icon = TRAY_ICON;
203 tray.notification_icon = TRAY_ICON;
204 tray.notification_title =
"Application Stopped";
205 tray.notification_text = msg;
206 tray.tooltip = PROJECT_NAME;
207 tray.menu[2].text = TRAY_MSG_NO_APP_RUNNING;
212 update_tray_launch_error(std::string app_name,
int exit_code) {
213 if (!tray_initialized) {
217 tray.notification_title = NULL;
218 tray.notification_text = NULL;
219 tray.notification_cb = NULL;
220 tray.notification_icon = NULL;
221 tray.icon = TRAY_ICON;
224 snprintf(msg, std::size(msg),
"Application %s exited too fast with code %d. Click here to terminate the stream.", app_name.c_str(), exit_code);
225 tray.icon = TRAY_ICON;
226 tray.notification_icon = TRAY_ICON;
227 tray.notification_title =
"Launch Error";
228 tray.notification_text = msg;
229 tray.notification_cb = []() {
230 BOOST_LOG(info) <<
"Force stop from notification"sv;
231 proc::proc.terminate();
233 tray.tooltip = PROJECT_NAME;
238 if (!tray_initialized) {
242 tray.notification_title =
nullptr;
243 tray.notification_text =
nullptr;
244 tray.notification_cb =
nullptr;
245 tray.notification_icon =
nullptr;
246 tray.icon = TRAY_ICON;
250 snprintf(msg, std::size(msg),
"%s", reason.c_str());
251 tray.icon = TRAY_ICON;
252 tray.notification_icon = TRAY_ICON;
253 tray.notification_title =
"Stream could not start";
254 tray.notification_text = msg;
255 tray.notification_cb = []() {
258 tray.tooltip = PROJECT_NAME;
263 if (!tray_initialized) {
267 tray.notification_title =
nullptr;
268 tray.notification_text =
nullptr;
269 tray.notification_cb =
nullptr;
270 tray.notification_icon =
nullptr;
271 tray.icon = TRAY_ICON;
273 tray.icon = TRAY_ICON;
274 tray.notification_title =
"Incoming Pairing Request";
275 tray.notification_text =
"Click here to complete the pairing process";
276 tray.notification_icon = TRAY_ICON_LOCKED;
277 tray.tooltip = PROJECT_NAME;
278 tray.notification_cb = []() {
285 update_tray_paired(std::string device_name) {
286 if (!tray_initialized) {
290 tray.notification_title = NULL;
291 tray.notification_text = NULL;
292 tray.notification_cb = NULL;
293 tray.notification_icon = NULL;
296 snprintf(msg, std::size(msg),
"Device %s paired Succesfully. Please make sure you have access to the device.", device_name.c_str());
297 tray.notification_title =
"Device Paired Succesfully";
298 tray.notification_text = msg;
299 tray.notification_icon = TRAY_ICON;
300 tray.tooltip = PROJECT_NAME;
305 update_tray_client_connected(std::string client_name) {
306 if (!tray_initialized) {
310 tray.notification_title = NULL;
311 tray.notification_text = NULL;
312 tray.notification_cb = NULL;
313 tray.notification_icon = NULL;
314 tray.icon = TRAY_ICON;
317 snprintf(msg, std::size(msg),
"%s has connected to the session.", client_name.c_str());
318 tray.notification_title =
"Client Connected";
319 tray.notification_text = msg;
320 tray.notification_icon = TRAY_ICON;
321 tray.tooltip = PROJECT_NAME;
326 static void tray_thread_worker() {
327 BOOST_LOG(info) <<
"System tray thread started"sv;
331 BOOST_LOG(error) <<
"Failed to initialize tray in thread"sv;
332 tray_thread_running =
false;
336 tray_thread_running =
true;
339 while (!tray_thread_should_exit) {
341 BOOST_LOG(warning) <<
"Tray event processing failed in thread"sv;
346 std::this_thread::sleep_for(std::chrono::milliseconds(50));
351 tray_thread_running =
false;
352 BOOST_LOG(info) <<
"System tray thread ended"sv;
356 if (tray_thread_running) {
357 BOOST_LOG(warning) <<
"Tray thread is already running"sv;
361 static const std::string title_str =
"Open " PROJECT_NAME
" (" + config::nvhttp.sunshine_name +
":" + std::to_string(
net::map_port(confighttp::PORT_HTTPS)) +
")";
362 tray.menu[0].text = title_str.c_str();
364 if (config::sunshine.hide_tray_controls) {
365 tray.menu[1].text =
nullptr;
368 tray_thread_should_exit =
false;
371 tray_thread = std::thread(tray_thread_worker);
374 const auto start_time = std::chrono::steady_clock::now();
375 while (!tray_thread_running && !tray_thread_should_exit) {
376 std::this_thread::sleep_for(std::chrono::milliseconds(10));
379 if (std::chrono::steady_clock::now() - start_time > std::chrono::seconds(10)) {
380 BOOST_LOG(error) <<
"Tray thread initialization timeout"sv;
381 tray_thread_should_exit =
true;
382 if (tray_thread.joinable()) {
389 if (!tray_thread_running) {
390 BOOST_LOG(error) <<
"Tray thread failed to start"sv;
391 if (tray_thread.joinable()) {
397 BOOST_LOG(info) <<
"System tray thread initialized successfully"sv;
399 }
catch (
const std::exception &e) {
400 BOOST_LOG(error) <<
"Failed to create tray thread: " << e.what();
406 if (!tray_thread_running) {
410 BOOST_LOG(info) <<
"Stopping system tray thread"sv;
411 tray_thread_should_exit =
true;
413 if (tray_thread.joinable()) {
417 BOOST_LOG(info) <<
"System tray thread stopped"sv;
void launch_ui(const std::optional< std::string > &path)
Launch the Web UI.
Declarations for entry handling functions.