MainNoGUI.cpp 8.1 KB

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