Debian Codegraph @

Source/Core/DolphinNoGUI/MainNoGUI.cpp

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