Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
system_tray.cpp
Go to the documentation of this file.
1
5// macros
6#if defined HERMES_TRAY && HERMES_TRAY >= 1
7
8 // Use absolute paths for tray icons to ensure they work with all icon themes
9 #define TRAY_ICON "/usr/share/icons/hicolor/scalable/status/" HERMES_TRAY_PREFIX "-tray.svg"
10 #define TRAY_ICON_PLAYING "/usr/share/icons/hicolor/scalable/status/" HERMES_TRAY_PREFIX "-playing.svg"
11 #define TRAY_ICON_PAUSING "/usr/share/icons/hicolor/scalable/status/" HERMES_TRAY_PREFIX "-pausing.svg"
12 #define TRAY_ICON_LOCKED "/usr/share/icons/hicolor/scalable/status/" HERMES_TRAY_PREFIX "-locked.svg"
13
14 #define TRAY_MSG_NO_APP_RUNNING "Reload Apps"
15
16 #ifndef BOOST_PROCESS_VERSION
17 #define BOOST_PROCESS_VERSION 1
18 #endif
19
20 // standard includes
21 #include <atomic>
22 #include <chrono>
23 #include <csignal>
24 #include <format>
25 #include <string>
26 #include <thread>
27
28 // lib includes
29 #include <boost/filesystem.hpp>
30 #include <boost/process/v1/environment.hpp>
31 #include <tray/src/tray.h>
32
33 // local includes
34 #include "config.h"
35 #include "confighttp.h"
36 #include "logging.h"
37 #include "platform/common.h"
38 #include "process.h"
39 #include "network.h"
40 #include "src/entry_handler.h"
41 #include "platform/linux/misc.h"
42
43using namespace std::literals;
44
45// system_tray namespace
46namespace system_tray {
47 static std::atomic tray_initialized = false;
48
49 // Tray thread state
50 static std::thread tray_thread;
51 static std::atomic tray_thread_running = false;
52 static std::atomic tray_thread_should_exit = false;
53
54 void tray_open_ui_cb([[maybe_unused]] struct tray_menu *item) {
55 BOOST_LOG(info) << "Opening UI from system tray"sv;
56 launch_ui();
57 }
58
59 void
60 tray_force_stop_cb(struct tray_menu *item) {
61 BOOST_LOG(info) << "Force stop from system tray"sv;
62 proc::proc.terminate();
63 }
64
65 void tray_restart_cb([[maybe_unused]] struct tray_menu *item) {
66 BOOST_LOG(info) << "Restarting from system tray"sv;
67
68 proc::proc.terminate();
69 platf::restart();
70 }
71
72 void tray_quit_cb([[maybe_unused]] struct tray_menu *item) {
73 BOOST_LOG(info) << "Quitting from system tray"sv;
74
75 proc::proc.terminate();
77
79 }
80
81 // Tray menu
82 static struct tray tray = {
83 .icon = TRAY_ICON,
84 .tooltip = PROJECT_NAME,
85 .menu =
86 (struct tray_menu[]) {
87 // todo - use boost/locale to translate menu strings
88 { .text = "Open " PROJECT_NAME, .cb = tray_open_ui_cb },
89 { .text = "-" },
90 // { .text = "-" },
91 // { .text = "Donate",
92 // .submenu =
93 // (struct tray_menu[]) {
94 // { .text = "GitHub Sponsors", .cb = tray_donate_github_cb },
95 // { .text = "MEE6", .cb = tray_donate_mee6_cb },
96 // { .text = "Patreon", .cb = tray_donate_patreon_cb },
97 // { .text = "PayPal", .cb = tray_donate_paypal_cb },
98 // { .text = nullptr } } },
99 // { .text = "-" },
100 { .text = TRAY_MSG_NO_APP_RUNNING, .cb = tray_force_stop_cb },
101 {.text = "Restart", .cb = tray_restart_cb},
102 {.text = "Quit", .cb = tray_quit_cb},
103 {.text = nullptr}
104 },
105 .iconPathCount = 4,
106 .allIconPaths = {TRAY_ICON, TRAY_ICON_LOCKED, TRAY_ICON_PLAYING, TRAY_ICON_PAUSING},
107 };
108
109 int init_tray() {
110
111 if (tray_init(&tray) < 0) {
112 BOOST_LOG(warning) << "Failed to create system tray"sv;
113 return 1;
114 }
115
116 BOOST_LOG(info) << "System tray created"sv;
117 tray_initialized = true;
118 return 0;
119 }
120
122 if (!tray_initialized) {
123 return 1;
124 }
125
126 // Process one iteration of the tray loop with non-blocking mode (0)
127 if (const int result = tray_loop(0); result != 0) {
128 BOOST_LOG(warning) << "System tray loop failed"sv;
129 return result;
130 }
131
132 return 0;
133 }
134
135 int end_tray() {
136 if (tray_initialized) {
137 tray_initialized = false;
138 tray_exit();
139 }
140 return 0;
141 }
142
143 void update_tray_playing(std::string app_name) {
144 if (!tray_initialized) {
145 return;
146 }
147
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;
153
154 tray_update(&tray);
155 tray.icon = TRAY_ICON_PLAYING;
156 tray.notification_title = "App launched";
157 char msg[256];
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;
165 tray_update(&tray);
166 }
167
168 void update_tray_pausing(std::string app_name) {
169 if (!tray_initialized) {
170 return;
171 }
172
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;
178 tray_update(&tray);
179 char msg[256];
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;
186 tray_update(&tray);
187 }
188
189 void update_tray_stopped(std::string app_name) {
190 if (!tray_initialized) {
191 return;
192 }
193
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;
199 tray_update(&tray);
200 char msg[256];
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;
208 tray_update(&tray);
209 }
210
211 void
212 update_tray_launch_error(std::string app_name, int exit_code) {
213 if (!tray_initialized) {
214 return;
215 }
216
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;
222 tray_update(&tray);
223 char msg[256];
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();
232 };
233 tray.tooltip = PROJECT_NAME;
234 tray_update(&tray);
235 }
236
237 void update_tray_session_error(std::string reason) {
238 if (!tray_initialized) {
239 return;
240 }
241
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;
247 tray_update(&tray);
248
249 char msg[512];
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 = []() {
256 launch_ui();
257 };
258 tray.tooltip = PROJECT_NAME;
259 tray_update(&tray);
260 }
261
263 if (!tray_initialized) {
264 return;
265 }
266
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;
272 tray_update(&tray);
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 = []() {
279 launch_ui("/pin#PIN");
280 };
281 tray_update(&tray);
282 }
283
284 void
285 update_tray_paired(std::string device_name) {
286 if (!tray_initialized) {
287 return;
288 }
289
290 tray.notification_title = NULL;
291 tray.notification_text = NULL;
292 tray.notification_cb = NULL;
293 tray.notification_icon = NULL;
294 tray_update(&tray);
295 char msg[256];
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;
301 tray_update(&tray);
302 }
303
304 void
305 update_tray_client_connected(std::string client_name) {
306 if (!tray_initialized) {
307 return;
308 }
309
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;
315 tray_update(&tray);
316 char msg[256];
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;
322 tray_update(&tray);
323 }
324
325 // Threading functions available on all platforms
326 static void tray_thread_worker() {
327 BOOST_LOG(info) << "System tray thread started"sv;
328
329 // Initialize the tray in this thread
330 if (init_tray() != 0) {
331 BOOST_LOG(error) << "Failed to initialize tray in thread"sv;
332 tray_thread_running = false;
333 return;
334 }
335
336 tray_thread_running = true;
337
338 // Main tray event loop
339 while (!tray_thread_should_exit) {
340 if (process_tray_events() != 0) {
341 BOOST_LOG(warning) << "Tray event processing failed in thread"sv;
342 break;
343 }
344
345 // Sleep to avoid busy waiting
346 std::this_thread::sleep_for(std::chrono::milliseconds(50));
347 }
348
349 // Clean up the tray
350 end_tray();
351 tray_thread_running = false;
352 BOOST_LOG(info) << "System tray thread ended"sv;
353 }
354
356 if (tray_thread_running) {
357 BOOST_LOG(warning) << "Tray thread is already running"sv;
358 return 1;
359 }
360
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();
363
364 if (config::sunshine.hide_tray_controls) {
365 tray.menu[1].text = nullptr;
366 }
367
368 tray_thread_should_exit = false;
369
370 try {
371 tray_thread = std::thread(tray_thread_worker);
372
373 // Wait for the thread to start and initialize
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));
377
378 // Timeout after 10 seconds
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()) {
383 tray_thread.join();
384 }
385 return 1;
386 }
387 }
388
389 if (!tray_thread_running) {
390 BOOST_LOG(error) << "Tray thread failed to start"sv;
391 if (tray_thread.joinable()) {
392 tray_thread.join();
393 }
394 return 1;
395 }
396
397 BOOST_LOG(info) << "System tray thread initialized successfully"sv;
398 return 0;
399 } catch (const std::exception &e) {
400 BOOST_LOG(error) << "Failed to create tray thread: " << e.what();
401 return 1;
402 }
403 }
404
406 if (!tray_thread_running) {
407 return 0;
408 }
409
410 BOOST_LOG(info) << "Stopping system tray thread"sv;
411 tray_thread_should_exit = true;
412
413 if (tray_thread.joinable()) {
414 tray_thread.join();
415 }
416
417 BOOST_LOG(info) << "System tray thread stopped"sv;
418 return 0;
419 }
420
421} // namespace system_tray
422
423 #ifdef BOOST_PROCESS_VERSION
424 #undef BOOST_PROCESS_VERSION 1
425 #endif
426
427#endif
Declarations for common platform specific utilities.
Declarations for the configuration of Sunshine.
Declarations for the Web UI Config HTTP server.
void launch_ui(const std::optional< std::string > &path)
Launch the Web UI.
Declarations for entry handling functions.
Declarations for logging related functions.
bool request_user_service_stop()
Definition misc.cpp:1068
Miscellaneous declarations for Linux.
void exit_sunshine(int exit_code, bool async)
Terminates Sunshine gracefully with the provided exit code.
Handles the system tray icon and notification system.
int init_tray()
Initializes the system tray without starting a loop.
int init_tray_threaded()
Initializes and runs the system tray in a separate thread.
int process_tray_events()
Processes a single tray event iteration.
int end_tray_threaded()
Stops the threaded system tray and waits for the thread to finish.
void update_tray_require_pin()
Spawns a notification for PIN Pairing. Clicking it opens the PIN Web UI Page.
void update_tray_playing(std::string app_name)
Sets the tray icon in playing mode and spawns the appropriate notification.
void update_tray_pausing(std::string app_name)
Sets the tray icon in pausing mode (stream stopped but app running) and spawns the appropriate notifi...
void tray_restart_cb(struct tray_menu *item)
Callback for resetting display device configuration.
void update_tray_session_error(std::string reason)
Spawns a notification for a session that could not start at all.
void tray_open_ui_cb(struct tray_menu *item)
Callback for opening the UI from the system tray.
void tray_quit_cb(struct tray_menu *item)
Callback for exiting Sunshine from the system tray.
int end_tray()
Exit the system tray.
void update_tray_stopped(std::string app_name)
Sets the tray icon in stopped mode (app and stream stopped) and spawns the appropriate notification.
std::uint16_t map_port(int port)
Map a specified port based on the base port.
Definition network.cpp:350
Declarations for networking related functions.
Declarations for the startup and shutdown of the apps started by a streaming Session.