#include "ppsspp_config.h"
#include <errno.h>
#include <clocale>
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <mutex>
#include <thread>
#if defined(_WIN32)
#include "Windows/WindowsAudio.h"
#include "Windows/MainWindow.h"
#endif
#if defined(_WIN32) && !PPSSPP_PLATFORM(UWP)
#include "Windows/CaptureDevice.h"
#endif
#include "Common/Net/HTTPClient.h"
#include "Common/Net/Resolve.h"
#include "Common/Net/URL.h"
#include "Common/Render/TextureAtlas.h"
#include "Common/Render/Text/draw_text.h"
#include "Common/GPU/OpenGL/GLFeatures.h"
#include "Common/GPU/thin3d.h"
#include "Common/UI/UI.h"
#include "Common/UI/Screen.h"
#include "Common/UI/Context.h"
#include "Common/UI/View.h"
#include "Common/UI/IconCache.h"
#include "android/jni/app-android.h"
#include "Common/System/Display.h"
#include "Common/System/Request.h"
#include "Common/System/System.h"
#include "Common/System/OSD.h"
#include "Common/System/NativeApp.h"
#include "Common/Data/Text/I18n.h"
#include "Common/Input/InputState.h"
#include "Common/Math/math_util.h"
#include "Common/Math/lin/matrix4x4.h"
#include "Common/Profiler/Profiler.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/File/VFS/VFS.h"
#include "Common/File/VFS/ZipFileReader.h"
#include "Common/File/VFS/DirectoryReader.h"
#include "Common/CPUDetect.h"
#include "Common/File/FileUtil.h"
#include "Common/TimeUtil.h"
#include "Common/StringUtils.h"
#include "Common/Log/LogManager.h"
#include "Common/MemArena.h"
#include "Common/GraphicsContext.h"
#include "Common/OSVersion.h"
#include "Common/GPU/ShaderTranslation.h"
#include "Common/VR/PPSSPPVR.h"
#include "Common/Thread/ThreadManager.h"
#include "Common/Audio/AudioBackend.h"
#include "Common/UI/PopupScreens.h"
#include "Core/ControlMapper.h"
#include "Core/Config.h"
#include "Core/ConfigValues.h"
#include "Core/Core.h"
#include "Core/Debugger/Breakpoints.h"
#include "Core/FileLoaders/DiskCachingFileLoader.h"
#include "Core/FrameTiming.h"
#include "Core/KeyMap.h"
#include "Core/Reporting.h"
#include "Core/RetroAchievements.h"
#include "Core/SaveState.h"
#include "Core/Screenshot.h"
#include "Core/System.h"
#include "Core/HLE/__sceAudio.h"
#include "Core/HLE/sceCtrl.h"
#include "Core/HLE/sceUsbCam.h"
#include "Core/HLE/sceUsbGps.h"
#include "Core/HLE/proAdhoc.h"
#include "Core/HW/MemoryStick.h"
#include "Core/Util/GameManager.h"
#include "Core/Util/PortManager.h"
#include "Core/Util/AudioFormat.h"
#include "Core/Util/RecentFiles.h"
#include "Core/Util/PathUtil.h"
#include "Core/WebServer.h"
#include "Core/TiltEventProcessor.h"
#include "GPU/GPUCommon.h"
#include "GPU/Common/PresentationCommon.h"
#include "UI/AudioCommon.h"
#include "UI/Background.h"
#include "UI/BackgroundAudio.h"
#include "UI/ControlMappingScreen.h"
#include "UI/DevScreens.h"
#include "UI/DiscordIntegration.h"
#include "UI/EmuScreen.h"
#include "UI/GameInfoCache.h"
#include "UI/GameSettingsScreen.h"
#include "UI/DeveloperToolsScreen.h"
#include "UI/GPUDriverTestScreen.h"
#include "UI/MiscScreens.h"
#include "UI/MemStickScreen.h"
#include "UI/OnScreenDisplay.h"
#include "UI/RemoteISOScreen.h"
#include "UI/Theme.h"
#include "UI/UIAtlas.h"
#if PPSSPP_PLATFORM(UWP)
#include <dwrite_3.h>
#include "UWP/UWPHelpers/InputHelpers.h"
#endif
#if PPSSPP_PLATFORM(ANDROID)
#include "android/jni/app-android.h"
#endif
#if PPSSPP_ARCH(ARM) && defined(__ANDROID__)
#include "../../android/jni/ArmEmitterTest.h"
#elif PPSSPP_ARCH(ARM64) && defined(__ANDROID__)
#include "../../android/jni/Arm64EmitterTest.h"
#endif
#if PPSSPP_PLATFORM(IOS)
#include "ios/iOSCoreAudio.h"
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
#if PPSSPP_PLATFORM(IOS) || PPSSPP_PLATFORM(MAC)
#include "UI/DarwinFileSystemServices.h"
#endif
#if !defined(__LIBRETRO__)
#include "Core/Util/GameDB.h"
#endif
#include <Core/HLE/Plugins.h>
bool HandleGlobalMessage(UIMessage message, const std::string &value);
static void ProcessWheelRelease(InputKeyCode keyCode, double now, bool keyPress);
void SaveFrameDump();
ScreenManager *g_screenManager;
std::string config_filename;
bool g_TakeScreenshot;
static bool resized = false;
static bool restarting = false;
static int renderCounter = 0;
struct PendingMessage {
UIMessage message;
std::string value;
};
static std::mutex g_pendingMutex;
static std::vector<PendingMessage> pendingMessages;
static Draw::DrawContext *g_draw;
static Draw::Pipeline *colorPipeline;
static Draw::Pipeline *texColorPipeline;
static UIContext *uiContext;
static int g_restartGraphics;
static bool g_windowHidden = false;
std::vector<std::function<void()>> g_pendingClosures;
AudioBackend *g_audioBackend = nullptr;
std::thread *graphicsLoadThread;
Path boot_filename;
void NativeGetAppInfo(std::string *app_dir_name, std::string *app_nice_name, bool *landscape, std::string *version) {
*app_nice_name = "PPSSPP";
*app_dir_name = "ppsspp";
*landscape = true;
*version = PPSSPP_GIT_VERSION;
#if PPSSPP_ARCH(ARM) && defined(__ANDROID__)
ArmEmitterTest();
#elif PPSSPP_ARCH(ARM64) && defined(__ANDROID__)
Arm64EmitterTest();
#endif
}
#if defined(USING_WIN_UI) && !PPSSPP_PLATFORM(UWP)
static bool CheckFontIsUsable(const wchar_t *fontFace) {
wchar_t actualFontFace[1024] = { 0 };
HFONT f = CreateFont(0, 0, 0, 0, FW_LIGHT, 0, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH, fontFace);
if (f != nullptr) {
HDC hdc = CreateCompatibleDC(nullptr);
if (hdc != nullptr) {
SelectObject(hdc, f);
GetTextFace(hdc, 1024, actualFontFace);
DeleteDC(hdc);
}
DeleteObject(f);
}
if (actualFontFace[0] != 0) {
return wcsncmp(actualFontFace, fontFace, ARRAY_SIZE(actualFontFace)) == 0;
}
return false;
}
#endif
void PostLoadConfig() {
if (g_Config.currentDirectory.empty()) {
g_Config.currentDirectory = g_Config.defaultCurrentDirectory;
}
const Path langOverridePath = GetSysDirectory(DIRECTORY_SYSTEM) / "lang";
if (!File::Exists(langOverridePath) || !File::IsDirectory(langOverridePath))
g_i18nrepo.LoadIni(g_Config.sLanguageIni);
else
g_i18nrepo.LoadIni(g_Config.sLanguageIni, langOverridePath);
#if !PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(UWP)
CreateSysDirectories();
#endif
}
static void CheckFailedGPUBackends() {
#ifdef _DEBUG
NOTICE_LOG(Log::Loader, "Not checking for failed graphics backends in debug mode");
return;
#endif
#if PPSSPP_PLATFORM(ANDROID)
if (System_GetPropertyInt(SYSPROP_SYSTEMVERSION) >= 30) {
return;
}
#endif
static int lastBackend = -1;
if (lastBackend == g_Config.iGPUBackend) {
return;
}
lastBackend = g_Config.iGPUBackend;
const Path failedBackendsDir = GetFailedBackendsDir();
const Path failedBackendsFile = failedBackendsDir / "FailedGraphicsBackends.txt";
std::string data;
if (File::ReadTextFileToString(failedBackendsFile, &data)) {
g_Config.sFailedGPUBackends = data;
}
if (g_Config.sFailedGPUBackends == "IGNORE")
return;
else if (!g_Config.sFailedGPUBackends.empty()) {
ERROR_LOG(Log::Loader, "Failed graphics backends: %s", g_Config.sFailedGPUBackends.c_str());
}
g_Config.iGPUBackend = g_Config.NextValidBackend();
if (lastBackend != g_Config.iGPUBackend) {
std::string param = GPUBackendToString((GPUBackend)lastBackend) + " -> " + GPUBackendToString((GPUBackend)g_Config.iGPUBackend);
System_GraphicsBackendFailedAlert(param);
INFO_LOG(Log::Loader, "Failed graphics backend switched from %s (%d to %d)", param.c_str(), lastBackend, g_Config.iGPUBackend);
} else {
WARN_LOG(Log::Loader, "Did not switch failed backend! %d", g_Config.iGPUBackend);
}
const std::string curBackend = GPUBackendToString((GPUBackend)g_Config.iGPUBackend);
if (g_Config.sFailedGPUBackends.empty()) {
g_Config.sFailedGPUBackends = curBackend;
} else if (g_Config.sFailedGPUBackends.find(curBackend) != std::string::npos) {
ERROR_LOG(Log::Loader, "Unexpected: Backend already in failed backends. Should not have been attempted");
} else if (g_Config.sFailedGPUBackends.find("ALL") == std::string::npos) {
g_Config.sFailedGPUBackends += "," + GPUBackendToString((GPUBackend)g_Config.iGPUBackend);
}
File::CreateFullPath(failedBackendsDir);
File::WriteStringToFile(true, g_Config.sFailedGPUBackends, failedBackendsFile);
}
static void ClearFailedGPUBackends() {
if (g_Config.sFailedGPUBackends == "IGNORE")
return;
const Path failedBackendsDir = GetFailedBackendsDir();
const Path failedBackendsFile = failedBackendsDir / "FailedGraphicsBackends.txt";
g_Config.sFailedGPUBackends.clear();
File::Delete(failedBackendsFile);
}
void NativeInit(int argc, const char *argv[], const char *savegame_dir, const char *external_dir, const char *cache_dir) {
net::Init();
g_Config.Init();
IncrementDebugCounter(DebugCounter::APP_BOOT);
__UPnPInit(2000);
ShaderTranslationInit();
g_threadManager.Init(cpu_info.num_cores, cpu_info.logical_cpu_count);
g_recentFiles.EnsureThread();
ResetUIState();
bool skipLogo = false;
setlocale( LC_ALL, "C" );
std::string user_data_path = savegame_dir;
pendingMessages.clear();
g_pendingClosures.clear();
g_requestManager.Clear();
#if PPSSPP_PLATFORM(IOS) || PPSSPP_PLATFORM(MAC)
g_VFS.Register("", new DirectoryReader(Path(external_dir)));
#endif
#if defined(ASSETS_DIR)
g_VFS.Register("", new DirectoryReader(Path(ASSETS_DIR)));
#endif
#if !defined(MOBILE_DEVICE) && !defined(_WIN32) && !PPSSPP_PLATFORM(SWITCH)
g_VFS.Register("", new DirectoryReader(File::GetExeDirectory() / "assets"));
g_VFS.Register("", new DirectoryReader(File::GetExeDirectory()));
g_VFS.Register("", new DirectoryReader(Path("/usr/local/share/ppsspp/assets")));
g_VFS.Register("", new DirectoryReader(Path("/usr/local/share/games/ppsspp/assets")));
g_VFS.Register("", new DirectoryReader(Path("/usr/share/ppsspp/assets")));
g_VFS.Register("", new DirectoryReader(Path("/usr/share/games/ppsspp/assets")));
#endif
#if PPSSPP_PLATFORM(SWITCH)
Path assetPath = Path(user_data_path) / "assets";
g_VFS.Register("", new DirectoryReader(assetPath));
#else
g_VFS.Register("", new DirectoryReader(Path("assets")));
#endif
g_VFS.Register("", new DirectoryReader(Path(savegame_dir)));
#if PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(MAC)
g_Config.defaultCurrentDirectory = Path(System_GetProperty(SYSPROP_USER_DOCUMENTS_DIR));
#else
g_Config.defaultCurrentDirectory = Path("/");
#endif
#if !PPSSPP_PLATFORM(UWP)
g_Config.internalDataDirectory = Path(savegame_dir);
#endif
#if PPSSPP_PLATFORM(ANDROID)
if (System_GetPropertyBool(SYSPROP_ANDROID_SCOPED_STORAGE)) {
g_Config.defaultCurrentDirectory.clear();
} else {
g_Config.memStickDirectory = Path(external_dir);
g_Config.defaultCurrentDirectory = Path(external_dir);
}
g_Config.flash0Directory = Path(external_dir) / "flash0";
Path memstickDirFile = g_Config.internalDataDirectory / "memstick_dir.txt";
if (File::Exists(memstickDirFile)) {
INFO_LOG(Log::System, "Reading '%s' to find memstick dir.", memstickDirFile.c_str());
std::string memstickDir;
if (File::ReadTextFileToString(memstickDirFile, &memstickDir)) {
Path memstickPath(memstickDir);
if (!memstickPath.empty() && File::Exists(memstickPath)) {
g_Config.memStickDirectory = memstickPath;
INFO_LOG(Log::System, "Memstick Directory from memstick_dir.txt: '%s'", g_Config.memStickDirectory.c_str());
} else {
ERROR_LOG(Log::System, "Couldn't read directory '%s' specified by memstick_dir.txt.", memstickDir.c_str());
if (System_GetPropertyBool(SYSPROP_ANDROID_SCOPED_STORAGE)) {
INFO_LOG(Log::System, "Asking the user.");
g_Config.memStickDirectory.clear();
}
}
}
} else {
INFO_LOG(Log::System, "No memstick directory file found (tried to open '%s')", memstickDirFile.c_str());
}
if (!System_GetPropertyBool(SYSPROP_ANDROID_SCOPED_STORAGE)) {
CreateSysDirectories();
}
#elif PPSSPP_PLATFORM(UWP) && !defined(__LIBRETRO__)
Path memstickDirFile = g_Config.internalDataDirectory / "memstick_dir.txt";
if (File::Exists(memstickDirFile)) {
INFO_LOG(Log::System, "Reading '%s' to find memstick dir.", memstickDirFile.c_str());
std::string memstickDir;
if (File::ReadTextFileToString(memstickDirFile, &memstickDir)) {
Path memstickPath(memstickDir);
if (!memstickPath.empty() && File::Exists(memstickPath)) {
g_Config.memStickDirectory = memstickPath;
g_Config.SetSearchPath(GetSysDirectory(DIRECTORY_SYSTEM));
g_Config.Reload();
INFO_LOG(Log::System, "Memstick Directory from memstick_dir.txt: '%s'", g_Config.memStickDirectory.c_str());
} else {
ERROR_LOG(Log::System, "Couldn't read directory '%s' specified by memstick_dir.txt.", memstickDir.c_str());
g_Config.memStickDirectory.clear();
}
}
}
else {
INFO_LOG(Log::System, "No memstick directory file found (tried to open '%s')", memstickDirFile.c_str());
}
#elif PPSSPP_PLATFORM(IOS)
g_Config.defaultCurrentDirectory = g_Config.internalDataDirectory;
g_Config.memStickDirectory = DarwinFileSystemServices::appropriateMemoryStickDirectoryToUse();
g_Config.flash0Directory = Path(external_dir) / "flash0";
#elif PPSSPP_PLATFORM(MAC)
g_Config.memStickDirectory = DarwinFileSystemServices::appropriateMemoryStickDirectoryToUse();
g_Config.flash0Directory = Path(external_dir) / "flash0";
#elif PPSSPP_PLATFORM(SWITCH)
g_Config.memStickDirectory = g_Config.internalDataDirectory / "config/ppsspp";
g_Config.flash0Directory = g_Config.internalDataDirectory / "assets/flash0";
#elif !PPSSPP_PLATFORM(WINDOWS)
std::string config;
if (getenv("XDG_CONFIG_HOME") != NULL)
config = getenv("XDG_CONFIG_HOME");
else if (getenv("HOME") != NULL)
config = getenv("HOME") + std::string("/.config");
else
config = "./config";
g_Config.memStickDirectory = Path(config) / "ppsspp";
g_Config.flash0Directory = File::GetExeDirectory() / "assets/flash0";
if (getenv("HOME") != nullptr) {
g_Config.defaultCurrentDirectory = Path(getenv("HOME"));
} else {
g_Config.currentDirectory = Path(".");
}
#endif
if (g_Config.currentDirectory.empty()) {
g_Config.currentDirectory = g_Config.defaultCurrentDirectory;
}
if (cache_dir && strlen(cache_dir)) {
g_Config.appCacheDirectory = Path(cache_dir);
DiskCachingFileLoaderCache::SetCacheDir(g_Config.appCacheDirectory);
}
g_logManager.Init(&g_Config.bEnableLogging);
#if !PPSSPP_PLATFORM(WINDOWS)
g_Config.SetSearchPath(GetSysDirectory(DIRECTORY_SYSTEM));
g_Config.Load();
#endif
const char *fileToLog = nullptr;
Path stateToLoad;
bool gotBootFilename = false;
bool gotoGameSettings = false;
bool gotoTouchScreenTest = false;
bool gotoDeveloperTools = false;
boot_filename.clear();
LogLevel logLevel = LogLevel::LINFO;
bool forceLogLevel = false;
const auto setLogLevel = [&logLevel, &forceLogLevel](LogLevel level) {
logLevel = level;
forceLogLevel = true;
};
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
#if defined(__APPLE__)
if (!strcmp(argv[i], "-NSDocumentRevisionsDebugMode") && argc - 1 > i) {
i++;
continue;
}
#endif
switch (argv[i][1]) {
case 'd':
setLogLevel(LogLevel::LDEBUG);
break;
case 'v':
setLogLevel(LogLevel::LVERBOSE);
break;
case 'j':
g_Config.iCpuCore = (int)CPUCore::JIT;
g_Config.bSaveSettings = false;
break;
case 'i':
g_Config.iCpuCore = (int)CPUCore::INTERPRETER;
g_Config.bSaveSettings = false;
break;
case 'r':
g_Config.iCpuCore = (int)CPUCore::IR_INTERPRETER;
g_Config.bSaveSettings = false;
break;
case 'J':
g_Config.iCpuCore = (int)CPUCore::JIT_IR;
g_Config.bSaveSettings = false;
break;
case '-':
if (!strncmp(argv[i], "--loglevel=", strlen("--loglevel=")) && strlen(argv[i]) > strlen("--loglevel="))
setLogLevel(static_cast<LogLevel>(std::atoi(argv[i] + strlen("--loglevel="))));
if (!strncmp(argv[i], "--log=", strlen("--log=")) && strlen(argv[i]) > strlen("--log="))
fileToLog = argv[i] + strlen("--log=");
if (!strncmp(argv[i], "--state=", strlen("--state=")) && strlen(argv[i]) > strlen("--state="))
stateToLoad = Path(argv[i] + strlen("--state="));
if (!strncmp(argv[i], "--escape-exit", strlen("--escape-exit")))
g_Config.bPauseExitsEmulator = true;
if (!strncmp(argv[i], "--pause-menu-exit", strlen("--pause-menu-exit")))
g_Config.bPauseMenuExitsEmulator = true;
if (!strcmp(argv[i], "--fullscreen")) {
g_Config.DoNotSaveSetting(&g_Config.bFullScreen);
g_Config.bFullScreen = true;
}
if (!strncmp(argv[i], "--root=", strlen("--root=")) && strlen(argv[i]) > strlen("--root=")) {
g_Config.mountRoot = Path(argv[i] + strlen("--root="));
}
if (!strcmp(argv[i], "--windowed")) {
g_Config.DoNotSaveSetting(&g_Config.bFullScreen);
g_Config.bFullScreen = false;
}
if (!strcmp(argv[i], "--touchscreentest"))
gotoTouchScreenTest = true;
if (!strcmp(argv[i], "--gamesettings"))
gotoGameSettings = true;
if (!strcmp(argv[i], "--developertools"))
gotoDeveloperTools = true;
if (!strncmp(argv[i], "--appendconfig=", strlen("--appendconfig=")) && strlen(argv[i]) > strlen("--appendconfig=")) {
g_Config.SetAppendedConfigIni(Path(argv[i] + strlen("--appendconfig=")));
g_Config.LoadAppendedConfig();
}
break;
}
} else {
if (!gotBootFilename) {
gotBootFilename = true;
INFO_LOG(Log::System, "Boot filename found in args: '%s'", argv[i]);
bool okToLoad = true;
bool okToCheck = true;
if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS)) {
PermissionStatus status = System_GetPermissionStatus(SYSTEM_PERMISSION_STORAGE);
if (status == PERMISSION_STATUS_DENIED) {
ERROR_LOG(Log::IO, "Storage permission denied. Launching without argument.");
okToLoad = false;
okToCheck = false;
} else if (status != PERMISSION_STATUS_GRANTED) {
ERROR_LOG(Log::IO, "Storage permission not granted. Launching without argument check.");
okToCheck = false;
} else {
INFO_LOG(Log::IO, "Storage permission granted.");
}
}
if (okToLoad) {
std::string str = std::string(argv[i]);
if (startsWith(str, "file:///")) {
str = UriDecode(str.substr(7));
INFO_LOG(Log::IO, "Decoding '%s' to '%s'", argv[i], str.c_str());
}
boot_filename = Path(str);
skipLogo = true;
}
if (okToLoad && okToCheck) {
std::unique_ptr<FileLoader> fileLoader(ConstructFileLoader(boot_filename));
if (!fileLoader->Exists()) {
fprintf(stderr, "File not found: %s\n", boot_filename.c_str());
#if defined(_WIN32) || defined(__ANDROID__)
boot_filename.clear();
#else
exit(1);
#endif
}
}
} else {
fprintf(stderr, "Syntax error: Can only boot one file.\nNote: Many command line args need a =, like --appendconfig=FILENAME.ini.\n");
#if defined(_WIN32) || defined(__ANDROID__)
#else
exit(1);
#endif
}
}
}
if (fileToLog) {
g_logManager.EnableOutput(LogOutput::File);
g_logManager.SetFileLogPath(Path(fileToLog));
} else {
g_logManager.SetFileLogPath(GetSysDirectory(DIRECTORY_DUMP) / "log.txt");
}
if (forceLogLevel) {
NOTICE_LOG(Log::System, "Setting log level to %d due to command line override", (int)logLevel);
g_logManager.SetAllLogLevels(logLevel);
}
PostLoadConfig();
#if PPSSPP_PLATFORM(ANDROID)
g_logManager.EnableOutput(LogOutput::Stdio);
#elif (defined(MOBILE_DEVICE) && !defined(_DEBUG))
g_logManager.EnableOutput(LogOutput::Printf);
#endif
if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS)) {
if (System_GetPermissionStatus(SYSTEM_PERMISSION_STORAGE) != PERMISSION_STATUS_GRANTED) {
System_AskForPermission(SYSTEM_PERMISSION_STORAGE);
}
}
g_BackgroundAudio.SFX().Init();
if (!boot_filename.empty() && stateToLoad.Valid()) {
SaveState::Load(stateToLoad, -1, [](SaveState::Status status, std::string_view message) {
if (!message.empty() && (!g_Config.bDumpFrames || !g_Config.bDumpVideoOutput)) {
g_OSD.Show(status == SaveState::Status::SUCCESS ? OSDType::MESSAGE_SUCCESS : OSDType::MESSAGE_ERROR,
message, status == SaveState::Status::SUCCESS ? 2.0 : 5.0);
}
});
}
if (g_Config.bAchievementsEnable) {
FILE *iconCacheFile = File::OpenCFile(GetSysDirectory(DIRECTORY_CACHE) / "icon.cache", "rb");
if (iconCacheFile) {
g_iconCache.LoadFromFile(iconCacheFile);
fclose(iconCacheFile);
}
}
g_DownloadManager.SetCacheDir(GetSysDirectory(DIRECTORY_APP_CACHE));
DEBUG_LOG(Log::System, "ScreenManager!");
g_screenManager = new ScreenManager();
if (g_Config.memStickDirectory.empty()) {
INFO_LOG(Log::System, "No memstick directory! Asking for one to be configured.");
g_screenManager->switchScreen(new LogoScreen(AfterLogoScreen::MEMSTICK_SCREEN_INITIAL_SETUP));
} else if (gotoGameSettings) {
g_screenManager->switchScreen(new LogoScreen(AfterLogoScreen::TO_GAME_SETTINGS));
} else if (gotoTouchScreenTest) {
g_screenManager->switchScreen(new MainScreen());
g_screenManager->push(new TouchTestScreen(Path()));
} else if (gotoDeveloperTools) {
g_screenManager->switchScreen(new MainScreen());
g_screenManager->push(new DeveloperToolsScreen(Path()));
} else if (skipLogo && !boot_filename.empty()) {
INFO_LOG(Log::System, "Launching EmuScreen with boot filename '%s'", boot_filename.c_str());
g_screenManager->switchScreen(new EmuScreen(boot_filename));
} else {
g_screenManager->switchScreen(new LogoScreen(AfterLogoScreen::DEFAULT));
}
g_screenManager->SetBackgroundOverlayScreens(new BackgroundScreen(), new OSDOverlayScreen());
WebServerFlags flags = (WebServerFlags)0;
if (g_Config.bRemoteShareOnStartup) {
flags |= WebServerFlags::DISCS;
}
if (g_Config.bRemoteDebuggerOnStartup) {
flags |= WebServerFlags::DEBUGGER;
}
if (flags != WebServerFlags::NONE) {
StartWebServer(WebServerFlags::ALL);
}
std::string sysName = System_GetProperty(SYSPROP_NAME);
CheckFailedGPUBackends();
SetGPUBackend((GPUBackend)g_Config.iGPUBackend);
renderCounter = 0;
Achievements::Initialize();
restarting = false;
}
void CallbackPostRender(UIContext *dc, void *userdata);
bool CreateGlobalPipelines();
static void NativeMixWrapper(float *dest, int framesToWrite, int sampleRateHz, void *userdata) {
static int16_t *buffer;
static int bufSize;
if (bufSize < framesToWrite * 2) {
buffer = new int16_t[framesToWrite * 2];
bufSize = framesToWrite * 2;
}
NativeMix(buffer, framesToWrite, sampleRateHz, userdata);
for (int i = 0; i < framesToWrite * 2; i++) {
dest[i] = (float)buffer[i] * (float)(1.0f / 32767.0f);
}
}
bool NativeInitGraphics(GraphicsContext *graphicsContext) {
INFO_LOG(Log::System, "NativeInitGraphics");
_assert_msg_(g_screenManager, "No screenmanager, bad init order. Backend = %d", g_Config.iGPUBackend);
resized = false;
Core_SetGraphicsContext(graphicsContext);
g_draw = graphicsContext->GetDrawContext();
_assert_(g_draw);
if (!CreateGlobalPipelines()) {
ERROR_LOG(Log::G3D, "Failed to create global pipelines");
return false;
}
ui_draw2d.SetAtlas(GetUIAtlas());
ui_draw2d.SetFontAtlas(GetFontAtlas());
uiContext = new UIContext();
uiContext->SetTheme(GetTheme());
uiContext->SetAtlasProvider(&AtlasProvider);
UpdateTheme();
ui_draw2d.Init(g_draw, texColorPipeline);
uiContext->Init(g_draw, texColorPipeline, colorPipeline, &ui_draw2d);
if (uiContext->Text()) {
}
g_screenManager->setUIContext(uiContext);
g_screenManager->setPostRenderCallback(&CallbackPostRender, nullptr);
g_screenManager->deviceRestored(g_draw);
g_audioBackend = System_CreateAudioBackend();
if (g_audioBackend) {
g_audioBackend->SetRenderCallback(&NativeMixWrapper, nullptr);
bool reverted = false;
g_audioBackend->InitOutputDevice(g_Config.sAudioDevice, LatencyMode::Aggressive, &reverted);
if (reverted) {
g_Config.sAudioDevice.clear();
}
}
#if defined(_WIN32) && !PPSSPP_PLATFORM(UWP)
if (IsWin7OrHigher()) {
winCamera = new WindowsCaptureDevice(CAPTUREDEVIDE_TYPE::VIDEO);
winCamera->sendMessage({ CAPTUREDEVIDE_COMMAND::INITIALIZE, nullptr });
winMic = new WindowsCaptureDevice(CAPTUREDEVIDE_TYPE::Audio);
winMic->sendMessage({ CAPTUREDEVIDE_COMMAND::INITIALIZE, nullptr });
}
#endif
#if PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(MAC)
const double displayHz = System_GetPropertyFloat(SYSPROP_DISPLAY_REFRESH_RATE);
if (displayHz < 55.0f) {
auto g = GetI18NCategory(I18NCat::GRAPHICS);
g_OSD.Show(OSDType::MESSAGE_WARNING, ApplySafeSubstitutions(g->T("Your display is set to a low refresh rate: %1 Hz. 60 Hz or higher is recommended."), (int)displayHz), 8.0f, "low_refresh");
g_OSD.SetClickCallback("low_refresh", [](bool clicked, void *) {
if (clicked) {
System_OpenDisplaySettings();
}
}, nullptr);
}
#endif
g_gameInfoCache = new GameInfoCache();
if (gpu) {
PSP_CoreParameter().pixelWidth = g_display.pixel_xres;
PSP_CoreParameter().pixelHeight = g_display.pixel_yres;
gpu->DeviceRestore(g_draw);
}
INFO_LOG(Log::System, "NativeInitGraphics completed");
return true;
}
bool CreateGlobalPipelines() {
using namespace Draw;
ShaderModule *vs_color_2d = g_draw->GetVshaderPreset(VS_COLOR_2D);
ShaderModule *fs_color_2d = g_draw->GetFshaderPreset(FS_COLOR_2D);
ShaderModule *vs_texture_color_2d = g_draw->GetVshaderPreset(VS_TEXTURE_COLOR_2D);
ShaderModule *fs_texture_color_2d = g_draw->GetFshaderPreset(FS_TEXTURE_COLOR_2D);
if (!vs_color_2d || !fs_color_2d || !vs_texture_color_2d || !fs_texture_color_2d) {
ERROR_LOG(Log::G3D, "Failed to get shader preset");
return false;
}
InputLayout *inputLayout = ui_draw2d.CreateInputLayout(g_draw);
BlendState *blendNormal = g_draw->CreateBlendState({ true, 0xF, BlendFactor::ONE, BlendFactor::ONE_MINUS_SRC_ALPHA });
DepthStencilState *depth = g_draw->CreateDepthStencilState({ false, false, Comparison::LESS });
RasterState *rasterNoCull = g_draw->CreateRasterState({});
PipelineDesc colorDesc{
Primitive::TRIANGLE_LIST,
{ vs_color_2d, fs_color_2d },
inputLayout, depth, blendNormal, rasterNoCull, &vsColBufDesc,
};
PipelineDesc texColorDesc{
Primitive::TRIANGLE_LIST,
{ vs_texture_color_2d, fs_texture_color_2d },
inputLayout, depth, blendNormal, rasterNoCull, &vsTexColBufDesc,
};
colorPipeline = g_draw->CreateGraphicsPipeline(colorDesc, "global_color");
if (!colorPipeline) {
return false;
}
texColorPipeline = g_draw->CreateGraphicsPipeline(texColorDesc, "global_texcolor");
if (!texColorPipeline) {
return false;
}
inputLayout->Release();
rasterNoCull->Release();
blendNormal->Release();
depth->Release();
return true;
}
void NativeShutdownGraphics() {
INFO_LOG(Log::System, "NativeShutdownGraphics begin");
if (g_screenManager) {
g_screenManager->deviceLost();
}
g_iconCache.ClearTextures();
if (gpu)
gpu->DeviceLost();
#if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
if (winCamera) {
winCamera->waitShutDown();
delete winCamera;
winCamera = nullptr;
}
if (winMic) {
winMic->waitShutDown();
delete winMic;
winMic = nullptr;
}
#endif
if (g_audioBackend) {
delete g_audioBackend;
g_audioBackend = nullptr;
}
UIBackgroundShutdown();
delete g_gameInfoCache;
g_gameInfoCache = nullptr;
delete uiContext;
uiContext = nullptr;
ui_draw2d.Shutdown();
if (colorPipeline) {
colorPipeline->Release();
colorPipeline = nullptr;
}
if (texColorPipeline) {
texColorPipeline->Release();
texColorPipeline = nullptr;
}
INFO_LOG(Log::System, "NativeShutdownGraphics end");
}
static void TakeScreenshot(Draw::DrawContext *draw) {
Path path = GetSysDirectory(DIRECTORY_SCREENSHOT);
if (!File::Exists(path)) {
File::CreateDir(path);
}
const std::string gameId = g_paramSFO.GetDiscID();
std::vector<File::FileInfo> files;
const std::string prefix = gameId + "_";
File::GetFilesInDir(path, &files, nullptr, 0, prefix);
std::set<std::string> existingNames;
for (auto &file : files) {
existingNames.insert(file.name);
}
Path filename;
int i = 0;
for (int i = 0; i < 20000; i++) {
const std::string pngName = prefix + StringFromFormat("%05d.png", i);
const std::string jpgName = prefix + StringFromFormat("%05d.jpg", i);
if (existingNames.find(pngName) == existingNames.end() && existingNames.find(jpgName) == existingNames.end()) {
filename = path / (g_Config.bScreenshotsAsPNG ? pngName : jpgName);
break;
}
}
if (filename.empty()) {
filename = path / (prefix + (g_Config.bScreenshotsAsPNG ? "20000.png" : "20000.jpg"));
}
const ScreenshotType type = g_Config.iScreenshotMode == (int)ScreenshotMode::GameImage ? SCREENSHOT_DISPLAY : SCREENSHOT_OUTPUT;
const ScreenshotResult result = TakeGameScreenshot(draw, filename, g_Config.bScreenshotsAsPNG ? ScreenshotFormat::PNG : ScreenshotFormat::JPG, type, -1, [filename](bool success) {
if (success) {
g_OSD.Show(OSDType::MESSAGE_FILE_LINK, filename.ToVisualString(), 0.0f, "screenshot_link");
if (System_GetPropertyBool(SYSPROP_CAN_SHOW_FILE)) {
g_OSD.SetClickCallback("screenshot_link", [](bool clicked, void *data) -> void {
Path *path = reinterpret_cast<Path *>(data);
if (clicked) {
System_ShowFileInFolder(*path);
} else {
delete path;
}
}, new Path(filename));
}
} else {
auto err = GetI18NCategory(I18NCat::ERRORS);
g_OSD.Show(OSDType::MESSAGE_ERROR, err->T("Could not save screenshot file"));
WARN_LOG(Log::System, "Failed to take screenshot.");
}
});
}
void CallbackPostRender(UIContext *dc, void *userdata) {
if (g_TakeScreenshot) {
TakeScreenshot(dc->GetDrawContext());
g_TakeScreenshot = false;
}
}
static void SendMouseDeltaAxis();
void NativeFrame(GraphicsContext *graphicsContext) {
PROFILE_END_FRAME();
if (System_GetPropertyInt(SYSPROP_DEVICE_TYPE) == DEVICE_TYPE_DESKTOP) {
if (g_windowHidden && g_Config.bPauseWhenMinimized) {
sleep_ms(16, "window-hidden");
return;
}
}
if (g_restartGraphics == 1) {
NativeShutdownGraphics();
g_restartGraphics++;
return;
}
else if (g_restartGraphics == 2) {
NativeInitGraphics(graphicsContext);
g_restartGraphics = 0;
}
double startTime = time_now_d();
ProcessWheelRelease(NKCODE_EXT_MOUSEWHEEL_UP, startTime, false);
ProcessWheelRelease(NKCODE_EXT_MOUSEWHEEL_DOWN, startTime, false);
SetOverrideScreenFrame(nullptr);
Achievements::Idle();
g_DownloadManager.Update();
g_Discord.Update();
g_OSD.Update();
_dbg_assert_(graphicsContext != nullptr);
_dbg_assert_(g_screenManager != nullptr);
g_GameManager.Update();
if (GetUIState() != UISTATE_INGAME) {
g_BackgroundAudio.Update();
}
g_iconCache.FrameUpdate();
g_screenManager->update();
if (g_audioBackend) {
g_audioBackend->FrameUpdate(g_Config.bAutoAudioDevice);
}
{
std::vector<PendingMessage> toProcess;
std::vector<std::function<void()>> toRun;
{
std::lock_guard<std::mutex> lock(g_pendingMutex);
toProcess = std::move(pendingMessages);
toRun = std::move(g_pendingClosures);
pendingMessages.clear();
g_pendingClosures.clear();
}
for (auto &item : toRun) {
item();
}
for (const auto &item : toProcess) {
if (item.message == UIMessage::WINDOW_RESTORED && graphicsContext) {
graphicsContext->NotifyWindowRestored();
}
if (HandleGlobalMessage(item.message, item.value)) {
VERBOSE_LOG(Log::System, "Handled global message: %d / %s", (int)item.message, item.value.c_str());
}
g_screenManager->sendMessage(item.message, item.value.c_str());
}
}
g_requestManager.ProcessRequests();
g_breakpoints.Frame();
Matrix4x4 ortho = ComputeOrthoMatrix(g_display.dp_xres, g_display.dp_yres, graphicsContext->GetDrawContext()->GetDeviceCaps().coordConvention);
Draw::DebugFlags debugFlags = Draw::DebugFlags::NONE;
if ((DebugOverlay)g_Config.iDebugOverlay == DebugOverlay::GPU_PROFILE)
debugFlags |= Draw::DebugFlags::PROFILE_TIMESTAMPS;
if (g_Config.bGpuLogProfiler)
debugFlags |= Draw::DebugFlags::PROFILE_SCOPES;
g_frameTiming.ComputePresentMode(g_draw, false);
g_draw->BeginFrame(debugFlags);
ui_draw2d.PushDrawMatrix(ortho);
g_screenManager->getUIContext()->SetTintSaturation(g_Config.fUITint, g_Config.fUISaturation);
ScreenRenderFlags renderFlags = g_screenManager->render();
if (g_screenManager->getUIContext()->Text()) {
g_screenManager->getUIContext()->Text()->OncePerFrame();
}
ui_draw2d.PopDrawMatrix();
g_draw->EndFrame();
g_frameTiming.PostSubmit();
if (renderCounter < 10 && ++renderCounter == 10) {
ClearFailedGPUBackends();
}
g_draw->Present(g_frameTiming.PresentMode());
if (resized) {
INFO_LOG(Log::G3D, "Resized flag set - recalculating bounds");
resized = false;
if (uiContext) {
uiContext->SetBounds(Bounds(0, 0, g_display.dp_xres, g_display.dp_yres));
#if defined(__APPLE__) && !defined(USING_QT_UI)
static int dp_xres_old = g_display.dp_xres;
if (g_display.dp_xres != dp_xres_old) {
dp_xres_old = g_display.dp_xres;
}
#endif
}
graphicsContext->Resize();
g_screenManager->resized();
#if !PPSSPP_PLATFORM(WINDOWS) && !defined(ANDROID)
PSP_CoreParameter().pixelWidth = g_display.pixel_xres;
PSP_CoreParameter().pixelHeight = g_display.pixel_yres;
System_PostUIMessage(UIMessage::GPU_DISPLAY_RESIZED);
#endif
} else {
graphicsContext->Poll();
}
SendMouseDeltaAxis();
if (!(renderFlags & ScreenRenderFlags::HANDLED_THROTTLING)) {
g_BackgroundAudio.Play();
float refreshRate = System_GetPropertyFloat(SYSPROP_DISPLAY_REFRESH_RATE);
static double lastTime = 0.0;
if (lastTime > 0.0) {
double now = time_now_d();
double diffTime = now - lastTime;
int sleepTimeUs = (int)(1000000 * ((1.0 / refreshRate) - diffTime));
if (sleepTimeUs > 0)
sleep_us(sleepTimeUs, "fallback-throttle");
}
lastTime = time_now_d();
}
}
bool HandleGlobalMessage(UIMessage message, const std::string &value) {
if (message == UIMessage::RESTART_GRAPHICS) {
g_restartGraphics = 1;
return true;
} else if (message == UIMessage::SAVESTATE_DISPLAY_SLOT) {
auto sy = GetI18NCategory(I18NCat::SYSTEM);
std::string msg = StringFromFormat("%s: %d", sy->T_cstr("Savestate Slot"), SaveState::GetCurrentSlot() + 1);
g_OSD.Show(OSDType::MESSAGE_INFO, msg, 2.0f, "savestate_slot");
return true;
}
else if (message == UIMessage::GPU_DISPLAY_RESIZED) {
if (gpu) {
gpu->NotifyDisplayResized();
}
return true;
}
else if (message == UIMessage::GPU_RENDER_RESIZED) {
if (gpu) {
DisplayLayoutConfig &config = g_Config.GetDisplayLayoutConfig(g_display.GetDeviceOrientation());
gpu->NotifyRenderResized(config);
}
return true;
}
else if (message == UIMessage::GPU_CONFIG_CHANGED) {
if (gpu) {
gpu->NotifyConfigChanged();
}
Reporting::UpdateConfig();
return true;
}
else if (message == UIMessage::POWER_SAVING) {
if (value != "false") {
auto sy = GetI18NCategory(I18NCat::SYSTEM);
#if PPSSPP_PLATFORM(ANDROID)
g_OSD.Show(OSDType::MESSAGE_WARNING, sy->T("WARNING: Android battery save mode is on"), 2.0f, "core_powerSaving");
#else
g_OSD.Show(OSDType::MESSAGE_WARNING, sy->T("WARNING: Battery save mode is on"), 2.0f, "core_powerSaving");
#endif
}
Core_SetPowerSaving(value != "false");
return true;
}
else if (message == UIMessage::PERMISSION_GRANTED && value == "storage") {
CreateSysDirectories();
int gpuBackend = g_Config.iGPUBackend;
INFO_LOG(Log::IO, "Reloading config after storage permission grant.");
g_Config.Reload();
PostLoadConfig();
g_Config.iGPUBackend = gpuBackend;
return true;
} else if (message == UIMessage::APP_RESUMED || message == UIMessage::GOT_FOCUS) {
MemoryStick_NotifyWrite();
return true;
} else if (message == UIMessage::SAVE_FRAME_DUMP) {
SaveFrameDump();
return true;
} else {
return false;
}
}
bool NativeIsAtTopLevel() {
if (!g_screenManager) {
ERROR_LOG(Log::System, "No screen manager active");
return false;
}
Screen *currentScreen = g_screenManager->topScreen();
if (currentScreen) {
bool top = currentScreen->isTopLevel();
return currentScreen->isTopLevel();
} else {
ERROR_LOG(Log::System, "No current screen");
return false;
}
}
void NativeTouch(const TouchInput &touch) {
if (!g_screenManager) {
return;
}
if (my_isnan(touch.x) || my_isnan(touch.y)) {
return;
}
g_screenManager->touch(touch);
}
static double g_wheelReleaseTime[2]{};
static void ProcessWheelRelease(InputKeyCode keyCode, double now, bool keyPress) {
int dir = keyCode - NKCODE_EXT_MOUSEWHEEL_UP;
if (g_wheelReleaseTime[dir] != 0.0 && (keyPress || now >= g_wheelReleaseTime[dir])) {
g_wheelReleaseTime[dir] = 0.0;
KeyInput key{};
key.deviceId = DEVICE_ID_MOUSE;
key.keyCode = keyCode;
key.flags = KeyInputFlags::UP;
NativeKey(key);
}
if (keyPress) {
float releaseTime = (float)g_Config.iMouseWheelUpDelayMs * (1.0f / 1000.0f);
g_wheelReleaseTime[dir] = now + releaseTime;
}
}
bool NativeKey(const KeyInput &key) {
double now = time_now_d();
if ((IsVREnabled() || g_Config.bForceVR) && !UpdateVRKeys(key)) {
return false;
}
#if PPSSPP_PLATFORM(UWP)
if (IgnoreInput(key.keyCode) && !(key.flags & KeyInputFlags::CHAR)) {
return false;
}
#endif
#if !defined(MOBILE_DEVICE)
if (g_Config.bPauseExitsEmulator) {
std::vector<int> pspKeys;
pspKeys.clear();
if (KeyMap::InputMappingToPspButton(InputMapping(key.deviceId, key.keyCode), &pspKeys)) {
if (std::find(pspKeys.begin(), pspKeys.end(), VIRTKEY_PAUSE) != pspKeys.end()) {
System_ExitApp();
return true;
}
}
}
#endif
#ifdef _DEBUG
if ((key.keyCode == NKCODE_F9 && (key.flags & KeyInputFlags::DOWN))) {
std::vector<File::FileInfo> tempLangs;
g_VFS.GetFileListing("lang", &tempLangs, "ini");
int x = rand() % tempLangs.size();
std::string_view code, part2;
if (SplitStringOnce(tempLangs[x].name, &code, &part2, '.')) {
g_Config.sLanguageIni = code;
INFO_LOG(Log::System, "Switching to random language: %s", g_Config.sLanguageIni.c_str());
if (g_i18nrepo.LoadIni(g_Config.sLanguageIni)) {
g_screenManager->RecreateAllViews();
System_Notify(SystemNotification::UI);
}
}
}
#endif
if (!g_screenManager) {
return false;
}
if ((key.flags & KeyInputFlags::DOWN) && key.deviceId == DEVICE_ID_MOUSE && (key.keyCode == NKCODE_EXT_MOUSEWHEEL_UP || key.keyCode == NKCODE_EXT_MOUSEWHEEL_DOWN)) {
ProcessWheelRelease(key.keyCode, now, true);
}
HLEPlugins::SetKey(key.keyCode, (key.flags & KeyInputFlags::DOWN) ? 1 : 0);
bool retval = g_screenManager->key(key);
if (key.keyCode == NKCODE_BUTTON_MODE) {
retval = true;
}
return retval;
}
void NativeAxis(const AxisInput *axes, size_t count) {
if ((IsVREnabled() || g_Config.bForceVR) && !UpdateVRAxis(axes, count)) {
return;
}
if (!g_screenManager) {
return;
}
g_screenManager->axis(axes, count);
for (size_t i = 0; i < count; i++) {
const AxisInput &axis = axes[i];
HLEPlugins::PluginDataAxis[axis.axisId] = axis.value;
}
}
static void SendMouseDeltaAxis() {
float mx, my;
MouseEventProcessor::MouseDeltaToAxes(time_now_d(), &mx, &my);
AxisInput axis[2];
axis[0].axisId = JOYSTICK_AXIS_MOUSE_REL_X;
axis[0].deviceId = DEVICE_ID_MOUSE;
axis[0].value = mx;
axis[1].axisId = JOYSTICK_AXIS_MOUSE_REL_Y;
axis[1].deviceId = DEVICE_ID_MOUSE;
axis[1].value = my;
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_MOUSE_REL_X] = mx;
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_MOUSE_REL_Y] = my;
if (GetUIState() == UISTATE_INGAME || g_IsMappingMouseInput) {
NativeAxis(axis, 2);
}
}
void NativeMouseDelta(float dx, float dy) {
if (!g_Config.bMouseControl)
return;
MouseEventProcessor::ProcessDelta(time_now_d(), dx, dy);
SendMouseDeltaAxis();
}
void NativeAccelerometer(float tiltX, float tiltY, float tiltZ) {
if (g_Config.iTiltInputType == TILT_NULL) {
return;
}
float tiltBaseAngleY = g_Config.fTiltBaseAngleY;
float xSensitivity = g_Config.iTiltSensitivityX / 50.0;
float ySensitivity = g_Config.iTiltSensitivityY / 50.0;
bool landscape = g_display.dp_yres < g_display.dp_xres;
TiltEventProcessor::ProcessTilt(landscape, tiltBaseAngleY, tiltX, tiltY, tiltZ,
g_Config.bInvertTiltX, g_Config.bInvertTiltY,
xSensitivity, ySensitivity);
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_X] = tiltX;
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_Y] = tiltY;
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_Z] = tiltZ;
}
void System_PostUIMessage(UIMessage message, std::string_view param) {
std::lock_guard<std::mutex> lock(g_pendingMutex);
PendingMessage pendingMessage;
pendingMessage.message = message;
pendingMessage.value = param;
pendingMessages.push_back(pendingMessage);
}
void System_RunOnMainThread(std::function<void()> func) {
std::lock_guard<std::mutex> lock(g_pendingMutex);
g_pendingClosures.push_back(std::move(func));
}
void NativeResized() {
VERBOSE_LOG(Log::G3D, "NativeResized - setting flag");
resized = true;
}
void NativeSetRestarting() {
restarting = true;
}
bool NativeIsRestarting() {
return restarting;
}
void NativeShutdown() {
INFO_LOG(Log::System, "NativeShutdown begin");
Achievements::Shutdown();
if (g_Config.bAchievementsEnable) {
FILE *iconCacheFile = File::OpenCFile(GetSysDirectory(DIRECTORY_CACHE) / "icon.cache", "wb");
if (iconCacheFile) {
g_iconCache.SaveToFile(iconCacheFile);
fclose(iconCacheFile);
}
}
if (g_screenManager) {
g_screenManager->shutdown();
delete g_screenManager;
g_screenManager = nullptr;
}
g_Config.Save("NativeShutdown");
g_i18nrepo.LogMissingKeys();
ShutdownWebServer();
__UPnPShutdown();
g_PortManager.Shutdown();
net::Shutdown();
g_Discord.Shutdown();
ShaderTranslationShutdown();
if (!restarting) {
g_logManager.Shutdown();
}
g_threadManager.Teardown();
#if !PPSSPP_PLATFORM(IOS)
System_ExitApp();
#endif
INFO_LOG(Log::System, "NativeShutdown end");
}
static Path GetSecretPath(std::string_view nameOfSecret) {
return GetSysDirectory(DIRECTORY_SYSTEM) / ("ppsspp_" + std::string(nameOfSecret) + ".dat");
}
bool NativeSaveSecret(std::string_view nameOfSecret, std::string_view data) {
Path path = GetSecretPath(nameOfSecret);
if (data.empty() && File::Exists(path)) {
return File::Delete(path);
} else if (!File::WriteDataToFile(false, data.data(), data.size(), path)) {
WARN_LOG(Log::System, "Failed to write secret '%.*s' to path '%s'", (int)nameOfSecret.size(), nameOfSecret.data(), path.c_str());
return false;
}
return true;
}
std::string NativeLoadSecret(std::string_view nameOfSecret) {
Path path = GetSecretPath(nameOfSecret);
std::string data;
if (!File::ReadBinaryFileToString(path, &data)) {
data.clear();
}
return data;
}
void Native_NotifyWindowHidden(bool hidden) {
g_windowHidden = hidden;
}
bool Native_IsWindowHidden() {
return g_windowHidden;
}
static bool IsWindowSmall(int pixelWidth, int pixelHeight) {
if (!g_Config.bShrinkIfWindowSmall) {
return false;
}
int w = (int)(pixelWidth * g_display.dpi_scale_real_x);
int h = (int)(pixelHeight * g_display.dpi_scale_real_y);
DisplayLayoutConfig &config = g_Config.GetDisplayLayoutConfig(g_display.GetDeviceOrientation());
return config.InternalRotationIsPortrait() ? (h < 480 + 80) : (w < 480 + 80);
}
bool Native_UpdateScreenScale(int pixel_width, int pixel_height, float customScale) {
_dbg_assert_(customScale > 0.1f);
float g_logical_dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_LOGICAL_DPI);
float dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_DPI);
if (dpi < 0.0f) {
dpi = 96.0f;
}
if (g_logical_dpi < 0.0f) {
g_logical_dpi = 96.0f;
}
bool smallWindow = IsWindowSmall(pixel_width, pixel_height);
if (smallWindow) {
customScale *= 0.5f;
} else {
customScale = UIScaleFactorToMultiplier(g_Config.iUIScaleFactor);
}
if (g_display.Recalculate(pixel_width, pixel_height, g_logical_dpi / dpi, g_logical_dpi / dpi, customScale)) {
NativeResized();
return true;
} else {
return false;
}
}