MainNoGUI.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DolphinNoGUI/Platform.h"
  4. #include <OptionParser.h>
  5. #include <csignal>
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. #ifndef _WIN32
  10. #include <unistd.h>
  11. #else
  12. #include <Windows.h>
  13. #endif
  14. #include "Common/ScopeGuard.h"
  15. #include "Common/StringUtil.h"
  16. #include "Core/Boot/Boot.h"
  17. #include "Core/BootManager.h"
  18. #include "Core/Core.h"
  19. #include "Core/DolphinAnalytics.h"
  20. #include "Core/Host.h"
  21. #include "Core/System.h"
  22. #include "UICommon/CommandLineParse.h"
  23. #ifdef USE_DISCORD_PRESENCE
  24. #include "UICommon/DiscordPresence.h"
  25. #endif
  26. #include "UICommon/UICommon.h"
  27. #include "VideoCommon/VideoBackendBase.h"
  28. static std::unique_ptr<Platform> s_platform;
  29. static void signal_handler(int)
  30. {
  31. constexpr char message[] = "A signal was received. A second signal will force Dolphin to stop.\n";
  32. #ifdef _WIN32
  33. puts(message);
  34. #else
  35. if (write(STDERR_FILENO, message, sizeof(message)) < 0)
  36. {
  37. }
  38. #endif
  39. s_platform->RequestShutdown();
  40. }
  41. std::vector<std::string> Host_GetPreferredLocales()
  42. {
  43. return {};
  44. }
  45. void Host_PPCSymbolsChanged()
  46. {
  47. }
  48. void Host_PPCBreakpointsChanged()
  49. {
  50. }
  51. void Host_RefreshDSPDebuggerWindow()
  52. {
  53. }
  54. bool Host_UIBlocksControllerState()
  55. {
  56. return false;
  57. }
  58. static Common::Event s_update_main_frame_event;
  59. void Host_Message(const HostMessageID id)
  60. {
  61. if (id == HostMessageID::WMUserStop)
  62. s_platform->Stop();
  63. }
  64. void Host_UpdateTitle(const std::string& title)
  65. {
  66. s_platform->SetTitle(title);
  67. }
  68. void Host_UpdateDisasmDialog()
  69. {
  70. }
  71. void Host_JitCacheInvalidation()
  72. {
  73. }
  74. void Host_JitProfileDataWiped()
  75. {
  76. }
  77. void Host_UpdateMainFrame()
  78. {
  79. s_update_main_frame_event.Set();
  80. }
  81. void Host_RequestRenderWindowSize(int width, int height)
  82. {
  83. }
  84. bool Host_RendererHasFocus()
  85. {
  86. return s_platform->IsWindowFocused();
  87. }
  88. bool Host_RendererHasFullFocus()
  89. {
  90. // Mouse capturing isn't implemented
  91. return Host_RendererHasFocus();
  92. }
  93. bool Host_RendererIsFullscreen()
  94. {
  95. return s_platform->IsWindowFullscreen();
  96. }
  97. bool Host_TASInputHasFocus()
  98. {
  99. return false;
  100. }
  101. void Host_YieldToUI()
  102. {
  103. }
  104. void Host_TitleChanged()
  105. {
  106. #ifdef USE_DISCORD_PRESENCE
  107. Discord::UpdateDiscordPresence();
  108. #endif
  109. }
  110. void Host_UpdateDiscordClientID(const std::string& client_id)
  111. {
  112. #ifdef USE_DISCORD_PRESENCE
  113. Discord::UpdateClientID(client_id);
  114. #endif
  115. }
  116. bool Host_UpdateDiscordPresenceRaw(const std::string& details, const std::string& state,
  117. const std::string& large_image_key,
  118. const std::string& large_image_text,
  119. const std::string& small_image_key,
  120. const std::string& small_image_text,
  121. const int64_t start_timestamp, const int64_t end_timestamp,
  122. const int party_size, const int party_max)
  123. {
  124. #ifdef USE_DISCORD_PRESENCE
  125. return Discord::UpdateDiscordPresenceRaw(details, state, large_image_key, large_image_text,
  126. small_image_key, small_image_text, start_timestamp,
  127. end_timestamp, party_size, party_max);
  128. #else
  129. return false;
  130. #endif
  131. }
  132. std::unique_ptr<GBAHostInterface> Host_CreateGBAHost(std::weak_ptr<HW::GBA::Core> core)
  133. {
  134. return nullptr;
  135. }
  136. static std::unique_ptr<Platform> GetPlatform(const optparse::Values& options)
  137. {
  138. std::string platform_name = static_cast<const char*>(options.get("platform"));
  139. #if HAVE_X11
  140. if (platform_name == "x11" || platform_name.empty())
  141. return Platform::CreateX11Platform();
  142. #endif
  143. #ifdef __linux__
  144. if (platform_name == "fbdev" || platform_name.empty())
  145. return Platform::CreateFBDevPlatform();
  146. #endif
  147. #ifdef _WIN32
  148. if (platform_name == "win32" || platform_name.empty())
  149. return Platform::CreateWin32Platform();
  150. #endif
  151. #ifdef __APPLE__
  152. if (platform_name == "macos" || platform_name.empty())
  153. return Platform::CreateMacOSPlatform();
  154. #endif
  155. if (platform_name == "headless" || platform_name.empty())
  156. return Platform::CreateHeadlessPlatform();
  157. return nullptr;
  158. }
  159. #ifdef _WIN32
  160. #define main app_main
  161. #endif
  162. int main(const int argc, char* argv[])
  163. {
  164. Core::DeclareAsHostThread();
  165. const auto parser =
  166. CommandLineParse::CreateParser(CommandLineParse::ParserOptions::OmitGUIOptions);
  167. parser->add_option("-p", "--platform")
  168. .action("store")
  169. .help("Window platform to use [%choices]")
  170. .choices({"headless"
  171. #ifdef __linux__
  172. ,
  173. "fbdev"
  174. #endif
  175. #if HAVE_X11
  176. ,
  177. "x11"
  178. #endif
  179. #ifdef _WIN32
  180. ,
  181. "win32"
  182. #endif
  183. #ifdef __APPLE__
  184. ,
  185. "macos"
  186. #endif
  187. });
  188. optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv);
  189. std::vector<std::string> args = parser->args();
  190. std::optional<std::string> save_state_path;
  191. if (options.is_set("save_state"))
  192. {
  193. save_state_path = static_cast<const char*>(options.get("save_state"));
  194. }
  195. std::unique_ptr<BootParameters> boot;
  196. bool game_specified = false;
  197. if (options.is_set("exec"))
  198. {
  199. const std::list<std::string> paths_list = options.all("exec");
  200. const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)),
  201. std::make_move_iterator(std::end(paths_list))};
  202. boot = BootParameters::GenerateFromFile(
  203. paths, BootSessionData(save_state_path, DeleteSavestateAfterBoot::No));
  204. game_specified = true;
  205. }
  206. else if (options.is_set("nand_title"))
  207. {
  208. const std::string hex_string = static_cast<const char*>(options.get("nand_title"));
  209. if (hex_string.length() != 16)
  210. {
  211. fprintf(stderr, "Invalid title ID\n");
  212. parser->print_help();
  213. return 1;
  214. }
  215. const u64 title_id = std::stoull(hex_string, nullptr, 16);
  216. boot = std::make_unique<BootParameters>(BootParameters::NANDTitle{title_id});
  217. }
  218. else if (args.size())
  219. {
  220. boot = BootParameters::GenerateFromFile(
  221. args.front(), BootSessionData(save_state_path, DeleteSavestateAfterBoot::No));
  222. args.erase(args.begin());
  223. game_specified = true;
  224. }
  225. else
  226. {
  227. parser->print_help();
  228. return 0;
  229. }
  230. std::string user_directory;
  231. if (options.is_set("user"))
  232. user_directory = static_cast<const char*>(options.get("user"));
  233. s_platform = GetPlatform(options);
  234. if (!s_platform || !s_platform->Init())
  235. {
  236. fprintf(stderr, "No platform found, or failed to initialize.\n");
  237. return 1;
  238. }
  239. const WindowSystemInfo wsi = s_platform->GetWindowSystemInfo();
  240. UICommon::SetUserDirectory(user_directory);
  241. UICommon::Init();
  242. UICommon::InitControllers(wsi);
  243. Common::ScopeGuard ui_common_guard([] {
  244. UICommon::ShutdownControllers();
  245. UICommon::Shutdown();
  246. });
  247. if (save_state_path && !game_specified)
  248. {
  249. fprintf(stderr, "A save state cannot be loaded without specifying a game to launch.\n");
  250. return 1;
  251. }
  252. Core::AddOnStateChangedCallback([](const Core::State state) {
  253. if (state == Core::State::Uninitialized)
  254. s_platform->Stop();
  255. });
  256. #ifdef _WIN32
  257. std::signal(SIGINT, signal_handler);
  258. std::signal(SIGTERM, signal_handler);
  259. #else
  260. // Shut down cleanly on SIGINT and SIGTERM
  261. struct sigaction sa;
  262. sa.sa_handler = signal_handler;
  263. sigemptyset(&sa.sa_mask);
  264. sa.sa_flags = SA_RESETHAND;
  265. sigaction(SIGINT, &sa, nullptr);
  266. sigaction(SIGTERM, &sa, nullptr);
  267. #endif
  268. DolphinAnalytics::Instance().ReportDolphinStart("nogui");
  269. if (!BootManager::BootCore(Core::System::GetInstance(), std::move(boot), wsi))
  270. {
  271. fprintf(stderr, "Could not boot the specified file\n");
  272. return 1;
  273. }
  274. #ifdef USE_DISCORD_PRESENCE
  275. Discord::UpdateDiscordPresence();
  276. #endif
  277. s_platform->MainLoop();
  278. Core::Stop(Core::System::GetInstance());
  279. Core::Shutdown(Core::System::GetInstance());
  280. s_platform.reset();
  281. return 0;
  282. }
  283. #ifdef _WIN32
  284. int wmain(int, wchar_t*[], wchar_t*[])
  285. {
  286. std::vector<std::string> args = Common::CommandLineToUtf8Argv(GetCommandLineW());
  287. const int argc = static_cast<int>(args.size());
  288. std::vector<char*> argv(args.size());
  289. for (size_t i = 0; i < args.size(); ++i)
  290. argv[i] = args[i].data();
  291. return main(argc, argv.data());
  292. }
  293. #undef main
  294. #endif