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