31 #include <boost/log/core.hpp>
33#define PROBE_DISPLAY_UUID "38F72B96-B00C-4F21-8B6C-E1BFF1602B0E"
39using namespace std::literals;
41std::map<int, std::function<void()>> signal_handlers;
43void on_signal_forwarder(
int sig) {
44 signal_handlers.at(sig)();
48void on_signal(
int sig, FN &&fn) {
49 signal_handlers.emplace(sig, std::forward<FN>(fn));
51 std::signal(sig, on_signal_forwarder);
54std::map<std::string_view, std::function<int(
const char *name,
int argc,
char **argv)>> cmd_to_func {
55 {
"creds"sv, [](
const char *name,
int argc,
char **argv) {
58 {
"help"sv, [](
const char *name,
int argc,
char **argv) {
61 {
"version"sv, [](
const char *name,
int argc,
char **argv) {
67#if defined HERMES_TRAY && HERMES_TRAY >= 1
68constexpr bool tray_is_enabled =
true;
70constexpr bool tray_is_enabled =
false;
74 bool run_loop =
false;
77 run_loop = tray_is_enabled;
80 BOOST_LOG(info) <<
"No main thread features enabled, skipping event loop"sv;
85 BOOST_LOG(info) <<
"Starting main loop"sv;
87 if (shutdown_event->peek()) {
88 BOOST_LOG(info) <<
"Shutdown event detected, breaking main loop"sv;
89 if (tray_is_enabled && config::sunshine.
system_tray) {
95 if (tray_is_enabled) {
100 std::this_thread::sleep_for(std::chrono::milliseconds(50));
104int main(
int argc,
char *argv[]) {
105 lifetime::argv = argv;
107 task_pool_util::TaskPool::task_id_t force_shutdown =
nullptr;
110#pragma GCC diagnostic push
111#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
113 std::locale utf8_locale(std::locale(),
new std::codecvt_utf8<wchar_t>);
114 std::locale::global(utf8_locale);
115 boost::filesystem::path::imbue(utf8_locale);
116#pragma GCC diagnostic pop
118 mail::man = std::make_shared<safe::mail_raw_t>();
123 const bool prelogin_command = std::any_of(argv + 1, argv + argc, [](
const char *argument) {
124 return argument ==
"--prelogin"sv;
126 if (prelogin_command) {
127 boost::log::core::get()->set_logging_enabled(
false);
131 if (config::parse(argc, argv)) {
135 if (prelogin_command && config::sunshine.cmd.name ==
"prelogin"sv) {
139 auto log_deinit_guard =
logging::init(config::sunshine.min_log_level, config::sunshine.log_file);
140 if (!log_deinit_guard) {
141 BOOST_LOG(error) <<
"Logging failed to initialize"sv;
147 BOOST_LOG(info) << PROJECT_NAME <<
" version: " << PROJECT_VERSION <<
" commit: " << PROJECT_VERSION_COMMIT;
153 for (
auto &[name, val] : config::modified_config_settings) {
154 BOOST_LOG(info) <<
"config: '"sv << name <<
"' = "sv << val;
156 config::modified_config_settings.clear();
158 if (!config::sunshine.cmd.name.empty()) {
159 auto fn = cmd_to_func.find(config::sunshine.cmd.name);
160 if (fn == std::end(cmd_to_func)) {
161 BOOST_LOG(fatal) <<
"Unknown command: "sv << config::sunshine.cmd.name;
163 BOOST_LOG(info) <<
"Possible commands:"sv;
164 for (
auto &[key, _] : cmd_to_func) {
165 BOOST_LOG(info) <<
'\t' << key;
171 return fn->second(argv[0], config::sunshine.cmd.argc, config::sunshine.cmd.argv);
178 if (!display_device_deinit_guard) {
179 BOOST_LOG(error) <<
"Display device session failed to initialize"sv;
186 auto shutdown_event =
mail::man->event<
bool>(mail::shutdown);
187 on_signal(SIGINT, [&force_shutdown, &display_device_deinit_guard, shutdown_event]() {
188 BOOST_LOG(info) <<
"Interrupt handler called"sv;
191 BOOST_LOG(fatal) <<
"10 seconds passed, yet Hermes is still running: Forcing shutdown"sv;
196 proc::proc.terminate();
198 force_shutdown =
task_pool.pushDelayed(task, 10s).task_id;
200 shutdown_event->raise(
true);
201 display_device_deinit_guard =
nullptr;
204 on_signal(SIGTERM, [&force_shutdown, &display_device_deinit_guard, shutdown_event]() {
205 BOOST_LOG(info) <<
"Terminate handler called"sv;
208 BOOST_LOG(fatal) <<
"10 seconds passed, yet Hermes is still running: Forcing shutdown"sv;
212 force_shutdown =
task_pool.pushDelayed(task, 10s).task_id;
214 shutdown_event->raise(
true);
215 display_device_deinit_guard =
nullptr;
219 proc::refresh(config::stream.file_apps);
226 auto platf_deinit_guard = platf::init();
227 if (!platf_deinit_guard) {
228 BOOST_LOG(error) <<
"Platform failed to initialize"sv;
232 if (!proc_deinit_guard) {
233 BOOST_LOG(error) <<
"Proc failed to initialize"sv;
247 auto input_deinit_guard = input::init();
249 if (input::probe_gamepads()) {
250 BOOST_LOG(warning) <<
"No gamepad input is available"sv;
265 BOOST_LOG(info) <<
"No monitor is lit and no graphical session is attached; "
266 "probing encoders when the first session brings up its virtual display."sv;
274 BOOST_LOG(info) <<
"No screen to probe encoders on; probing them without one."sv;
276 BOOST_LOG(error) <<
"Video failed to find working encoder: allow probing but failed"sv;
281 BOOST_LOG(fatal) <<
"HTTP interface failed to initialize"sv;
287 std::unique_ptr<platf::deinit_t> mDNS;
288 auto sync_mDNS = std::async(std::launch::async, [&mDNS]() {
289 if (config::sunshine.enable_discovery) {
290 mDNS = platf::publish::start();
294 std::unique_ptr<platf::deinit_t> upnp_unmap;
295 auto sync_upnp = std::async(std::launch::async, [&upnp_unmap]() {
300 if (shutdown_event->peek()) {
301 return lifetime::desired_exit_code;
313 if (tray_is_enabled && config::sunshine.
system_tray) {
314 BOOST_LOG(info) <<
"Starting system tray"sv;
318 mainThreadLoop(shutdown_event);
321 shutdown_event->view();
331 return lifetime::desired_exit_code;
std::filesystem::path appdata()
Performs migration if necessary, then returns the appdata directory.
void start()
Start the HTTPS server.
Declarations for the Web UI Config HTTP server.
std::unique_ptr< platf::deinit_t > init(const std::filesystem::path &persistence_filepath, const config::video_t &video_config)
Initialize the implementation and perform the initial state recovery (if needed).
Declarations for display device handling.
void log_publisher_data()
Log the publisher metadata provided from CMake.
Declarations for entry handling functions.
thread_pool_util::ThreadPool task_pool
A thread pool for processing tasks.
Declarations for globally accessible variables and functions.
Declarations for common HTTP.
Declarations for logging related functions.
int main(int argc, char *argv[])
Main application entry point.
Declarations for the main entry point for Sunshine.
int creds(const char *name, int argc, char *argv[])
Reset the user credentials.
int version()
Print the version to stdout, then exit.
int help(const char *name)
Print help to stdout, then exit.
void debug_trap()
Breaks into the debugger or terminates Sunshine if no debugger is attached.
std::unique_ptr< deinit_t > init(int min_log_level, const std::string &log_file)
Initialize the logging system.
void log_flush()
Flush the log.
safe::mail_t man
A process-wide communication mechanism.
void start()
Start the nvhttp server.
Handles the system tray icon and notification system.
int init_tray()
Initializes the system tray without starting a loop.
int process_tray_events()
Processes a single tray event iteration.
int end_tray()
Exit the system tray.
std::unique_ptr< platf::deinit_t > start()
Start UPnP port mapping and return its shutdown guard.
Declarations for the nvhttp (GameStream) server.
int command(const char *name, int argc, char *argv[])
hermes --prelogin status|enable|disable.
void log_report()
Say at startup what stands between this host and being reachable before login. Silent unless Hermes i...
Set up and check what it takes for Hermes to be reachable before login.
std::unique_ptr< platf::deinit_t > init()
Initialize proc functions.
void initVDisplayDriver()
Initialize the virtual display driver. This should be called early in startup to enable virtual displ...
Declarations for the startup and shutdown of the apps started by a streaming Session.
#define reed_solomon_init
This initializes the RS function pointers to the best vectorized version available.
Wrappers for nanors vectorization.
void start()
Runs the RTSP server loop.
void drop_stale_display_environment()
Forget display variables that point at a display server that is gone.
std::unique_ptr< platf::deinit_t > start()
Start watching for a session to attach to.
Let a Hermes that started before the graphical session catch up with it.
Declarations for the system tray icon and notification system.
Declarations for UPnP port mapping.
Declarations for UUID generation.
bool display_present()
Whether the host has a display to capture without creating one.
int probe_encoders()
Probe encoders and select the preferred encoder. This is called once at startup and each time a strea...
int probe_encoders_without_display()
Probe encoders without opening any screen.
bool windowSystemAttached()
Whether Hermes runs inside a graphical session (Wayland or X11).
void logSessionAssessment()
Probe, assess and log what this session can and cannot do.
Virtual display declarations for Linux.