1 // Copyright (c) 2012- PPSSPP Project.
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
11
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18 // NativeApp implementation for platforms that will use that framework, like:
19 // Android, Linux, MacOSX.
20 //
21 // Native is a cross platform framework. It's not very mature and mostly
22 // just built according to the needs of my own apps.
23 //
24 // Windows has its own code that bypasses the framework entirely.
25
26 #include "ppsspp_config.h"
27
28 // Background worker threads should be spawned in NativeInit and joined
29 // in NativeShutdown.
30
31 #include <locale.h>
32 #include <algorithm>
33 #include <cstdlib>
34 #include <memory>
35 #include <mutex>
36 #include <thread>
37
38 #if defined(_WIN32)
39 #include "Windows/WindowsAudio.h"
40 #include "Windows/MainWindow.h"
41 #endif
42
43 #if defined(_WIN32) && !PPSSPP_PLATFORM(UWP)
44 #include "Windows/CaptureDevice.h"
45 #endif
46
47 #include "Common/Net/HTTPClient.h"
48 #include "Common/Net/Resolve.h"
49 #include "Common/Net/URL.h"
50 #include "Common/Render/TextureAtlas.h"
51 #include "Common/Render/Text/draw_text.h"
52 #include "Common/GPU/OpenGL/GLFeatures.h"
53 #include "Common/GPU/thin3d.h"
54 #include "Common/UI/UI.h"
55 #include "Common/UI/Screen.h"
56 #include "Common/UI/Context.h"
57 #include "Common/UI/View.h"
58 #include "android/jni/app-android.h"
59
60 #include "Common/System/Display.h"
61 #include "Common/System/System.h"
62 #include "Common/System/NativeApp.h"
63 #include "Common/Data/Text/I18n.h"
64 #include "Common/Input/InputState.h"
65 #include "Common/Math/fast/fast_math.h"
66 #include "Common/Math/math_util.h"
67 #include "Common/Math/lin/matrix4x4.h"
68 #include "Common/Profiler/Profiler.h"
69 #include "Common/Data/Encoding/Utf8.h"
70 #include "Common/File/VFS/VFS.h"
71 #include "Common/File/VFS/AssetReader.h"
72 #include "Common/CPUDetect.h"
73 #include "Common/File/FileUtil.h"
74 #include "Common/TimeUtil.h"
75 #include "Common/StringUtils.h"
76 #include "Common/LogManager.h"
77 #include "Common/MemArena.h"
78 #include "Common/GraphicsContext.h"
79 #include "Common/OSVersion.h"
80 #include "Common/GPU/ShaderTranslation.h"
81
82 #include "Core/ControlMapper.h"
83 #include "Core/Config.h"
84 #include "Core/ConfigValues.h"
85 #include "Core/Core.h"
86 #include "Core/FileLoaders/DiskCachingFileLoader.h"
87 #include "Core/Host.h"
88 #include "Core/KeyMap.h"
89 #include "Core/Reporting.h"
90 #include "Core/SaveState.h"
91 #include "Core/Screenshot.h"
92 #include "Core/System.h"
93 #include "Core/HLE/__sceAudio.h"
94 #include "Core/HLE/sceCtrl.h"
95 #include "Core/HLE/sceUsbCam.h"
96 #include "Core/HLE/sceUsbGps.h"
97 #include "Core/HLE/proAdhoc.h"
98 #include "Core/HW/MemoryStick.h"
99 #include "Core/Util/GameManager.h"
100 #include "Core/Util/AudioFormat.h"
101 #include "Core/WebServer.h"
102 #include "Core/ThreadPools.h"
103
104 #include "GPU/GPUInterface.h"
105 #include "UI/BackgroundAudio.h"
106 #include "UI/ControlMappingScreen.h"
107 #include "UI/DiscordIntegration.h"
108 #include "UI/EmuScreen.h"
109 #include "UI/GameInfoCache.h"
110 #include "UI/GPUDriverTestScreen.h"
111 #include "UI/HostTypes.h"
112 #include "UI/MiscScreens.h"
113 #include "UI/MemStickScreen.h"
114 #include "UI/OnScreenDisplay.h"
115 #include "UI/RemoteISOScreen.h"
116 #include "UI/TiltEventProcessor.h"
117 #include "UI/TextureUtil.h"
118
119 #if !defined(MOBILE_DEVICE) && defined(USING_QT_UI)
120 #include "Qt/QtHost.h"
121 #endif
122 #if defined(USING_QT_UI)
123 #include <QFontDatabase>
124 #endif
125 #if PPSSPP_PLATFORM(UWP)
126 #include <dwrite_3.h>
127 #endif
128 #if PPSSPP_PLATFORM(ANDROID)
129 #include "android/jni/app-android.h"
130 #endif
131
132 // The new UI framework, for initialization
133
134 static UI::Theme ui_theme;
135
136 Atlas g_ui_atlas;
137
138 #if PPSSPP_ARCH(ARM) && defined(__ANDROID__)
139 #include "../../android/jni/ArmEmitterTest.h"
140 #elif PPSSPP_ARCH(ARM64) && defined(__ANDROID__)
141 #include "../../android/jni/Arm64EmitterTest.h"
142 #endif
143
144 #if PPSSPP_PLATFORM(IOS)
145 #include "ios/iOSCoreAudio.h"
146 #elif defined(__APPLE__)
147 #include <mach-o/dyld.h>
148 #endif
149
150 ScreenManager *screenManager;
151 std::string config_filename;
152
153 // Really need to clean this mess of globals up... but instead I add more :P
154 bool g_TakeScreenshot;
155 static bool isOuya;
156 static bool resized = false;
157 static bool restarting = false;
158
159 static int renderCounter = 0;
160
161 struct PendingMessage {
162 std::string msg;
163 std::string value;
164 };
165
166 struct PendingInputBox {
167 std::function<void(bool, const std::string &)> cb;
168 bool result;
169 std::string value;
170 };
171
172 static std::mutex pendingMutex;
173 static std::vector<PendingMessage> pendingMessages;
174 static std::vector<PendingInputBox> pendingInputBoxes;
175 static Draw::DrawContext *g_draw;
176 static Draw::Pipeline *colorPipeline;
177 static Draw::Pipeline *texColorPipeline;
178 static UIContext *uiContext;
179
180 #ifdef _WIN32
181 WindowsAudioBackend *winAudioBackend;
182 #endif
183
184 std::thread *graphicsLoadThread;
185
186 class PrintfLogger : public LogListener {
187 public:
Log(const LogMessage & message)188 void Log(const LogMessage &message) override {
189 // Log with simplified headers as Android already provides timestamp etc.
190 switch (message.level) {
191 case LogTypes::LVERBOSE:
192 case LogTypes::LDEBUG:
193 case LogTypes::LINFO:
194 printf("INFO [%s] %s", message.log, message.msg.c_str());
195 break;
196 case LogTypes::LERROR:
197 printf("ERR [%s] %s", message.log, message.msg.c_str());
198 break;
199 case LogTypes::LWARNING:
200 printf("WARN [%s] %s", message.log, message.msg.c_str());
201 break;
202 case LogTypes::LNOTICE:
203 default:
204 printf("NOTE [%s] !!! %s", message.log, message.msg.c_str());
205 break;
206 }
207 }
208 };
209
210 #ifdef _WIN32
Win32Mix(short * buffer,int numSamples,int bits,int rate)211 int Win32Mix(short *buffer, int numSamples, int bits, int rate) {
212 return NativeMix(buffer, numSamples);
213 }
214 #endif
215
216 // globals
217 static LogListener *logger = nullptr;
218 Path boot_filename;
219
InitSound()220 void NativeHost::InitSound() {
221 #if PPSSPP_PLATFORM(IOS)
222 iOSCoreAudioInit();
223 #endif
224 }
225
ShutdownSound()226 void NativeHost::ShutdownSound() {
227 #if PPSSPP_PLATFORM(IOS)
228 iOSCoreAudioShutdown();
229 #endif
230 }
231
232 #if !defined(MOBILE_DEVICE) && defined(USING_QT_UI)
InitSound()233 void QtHost::InitSound() { }
ShutdownSound()234 void QtHost::ShutdownSound() { }
235 #endif
236
NativeQueryConfig(std::string query)237 std::string NativeQueryConfig(std::string query) {
238 char temp[128];
239 if (query == "screenRotation") {
240 INFO_LOG(G3D, "g_Config.screenRotation = %d", g_Config.iScreenRotation);
241 snprintf(temp, sizeof(temp), "%d", g_Config.iScreenRotation);
242 return std::string(temp);
243 } else if (query == "immersiveMode") {
244 return std::string(g_Config.bImmersiveMode ? "1" : "0");
245 } else if (query == "hwScale") {
246 int scale = g_Config.iAndroidHwScale;
247 // Override hw scale for TV type devices.
248 if (System_GetPropertyInt(SYSPROP_DEVICE_TYPE) == DEVICE_TYPE_TV)
249 scale = 0;
250
251 if (scale == 1) {
252 // If g_Config.iInternalResolution is also set to Auto (1), we fall back to "Device resolution" (0). It works out.
253 scale = g_Config.iInternalResolution;
254 } else if (scale >= 2) {
255 scale -= 1;
256 }
257
258 int max_res = std::max(System_GetPropertyInt(SYSPROP_DISPLAY_XRES), System_GetPropertyInt(SYSPROP_DISPLAY_YRES)) / 480 + 1;
259 snprintf(temp, sizeof(temp), "%d", std::min(scale, max_res));
260 return std::string(temp);
261 } else if (query == "androidJavaGL") {
262 // If we're using Vulkan, we say no... need C++ to use Vulkan.
263 if (GetGPUBackend() == GPUBackend::VULKAN) {
264 return "false";
265 }
266 // Otherwise, some devices prefer the Java init so play it safe.
267 return "true";
268 } else if (query == "sustainedPerformanceMode") {
269 return std::string(g_Config.bSustainedPerformanceMode ? "1" : "0");
270 } else {
271 return "";
272 }
273 }
274
NativeMix(short * audio,int num_samples)275 int NativeMix(short *audio, int num_samples) {
276 if (GetUIState() != UISTATE_INGAME) {
277 g_BackgroundAudio.Play();
278 }
279
280 int sample_rate = System_GetPropertyInt(SYSPROP_AUDIO_SAMPLE_RATE);
281 num_samples = __AudioMix(audio, num_samples, sample_rate > 0 ? sample_rate : 44100);
282
283 #ifdef _WIN32
284 winAudioBackend->Update();
285 #endif
286
287 return num_samples;
288 }
289
290 // This is called before NativeInit so we do a little bit of initialization here.
NativeGetAppInfo(std::string * app_dir_name,std::string * app_nice_name,bool * landscape,std::string * version)291 void NativeGetAppInfo(std::string *app_dir_name, std::string *app_nice_name, bool *landscape, std::string *version) {
292 *app_nice_name = "PPSSPP";
293 *app_dir_name = "ppsspp";
294 *landscape = true;
295 *version = PPSSPP_GIT_VERSION;
296
297 #if PPSSPP_ARCH(ARM) && defined(__ANDROID__)
298 ArmEmitterTest();
299 #elif PPSSPP_ARCH(ARM64) && defined(__ANDROID__)
300 Arm64EmitterTest();
301 #endif
302 }
303
304 #if defined(USING_WIN_UI) && !PPSSPP_PLATFORM(UWP)
CheckFontIsUsable(const wchar_t * fontFace)305 static bool CheckFontIsUsable(const wchar_t *fontFace) {
306 wchar_t actualFontFace[1024] = { 0 };
307
308 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);
309 if (f != nullptr) {
310 HDC hdc = CreateCompatibleDC(nullptr);
311 if (hdc != nullptr) {
312 SelectObject(hdc, f);
313 GetTextFace(hdc, 1024, actualFontFace);
314 DeleteDC(hdc);
315 }
316 DeleteObject(f);
317 }
318
319 // If we were able to get the font name, did it load?
320 if (actualFontFace[0] != 0) {
321 return wcsncmp(actualFontFace, fontFace, ARRAY_SIZE(actualFontFace)) == 0;
322 }
323 return false;
324 }
325 #endif
326
327 bool CreateDirectoriesAndroid();
328
PostLoadConfig()329 void PostLoadConfig() {
330 // On Windows, we deal with currentDirectory in InitSysDirectories().
331 #if !PPSSPP_PLATFORM(WINDOWS)
332 if (g_Config.currentDirectory.empty()) {
333 g_Config.currentDirectory = g_Config.defaultCurrentDirectory;
334 }
335 #endif
336
337 // Allow the lang directory to be overridden for testing purposes (e.g. Android, where it's hard to
338 // test new languages without recompiling the entire app, which is a hassle).
339 const Path langOverridePath = GetSysDirectory(DIRECTORY_SYSTEM) / "lang";
340
341 // If we run into the unlikely case that "lang" is actually a file, just use the built-in translations.
342 if (!File::Exists(langOverridePath) || !File::IsDirectory(langOverridePath))
343 i18nrepo.LoadIni(g_Config.sLanguageIni);
344 else
345 i18nrepo.LoadIni(g_Config.sLanguageIni, langOverridePath);
346
347 #if PPSSPP_PLATFORM(ANDROID)
348 CreateDirectoriesAndroid();
349 #endif
350 }
351
CreateDirectoriesAndroid()352 bool CreateDirectoriesAndroid() {
353 // TODO: We should probably simply use this as the shared function to create memstick directories.
354
355 Path pspDir = g_Config.memStickDirectory;
356 if (pspDir.GetFilename() != "PSP") {
357 pspDir /= "PSP";
358 }
359
360 INFO_LOG(IO, "Creating '%s' and subdirs:", pspDir.c_str());
361 File::CreateFullPath(pspDir);
362 if (!File::Exists(pspDir)) {
363 INFO_LOG(IO, "Not a workable memstick directory. Giving up");
364 return false;
365 }
366
367 File::CreateFullPath(GetSysDirectory(DIRECTORY_CHEATS));
368 File::CreateFullPath(GetSysDirectory(DIRECTORY_SAVEDATA));
369 File::CreateFullPath(GetSysDirectory(DIRECTORY_SAVESTATE));
370 File::CreateFullPath(GetSysDirectory(DIRECTORY_GAME));
371 File::CreateFullPath(GetSysDirectory(DIRECTORY_SYSTEM));
372 File::CreateFullPath(GetSysDirectory(DIRECTORY_TEXTURES));
373 File::CreateFullPath(GetSysDirectory(DIRECTORY_PLUGINS));
374
375 // Avoid media scanners in PPSSPP_STATE and SAVEDATA directories,
376 // and in the root PSP directory as well.
377 File::CreateEmptyFile(GetSysDirectory(DIRECTORY_SAVESTATE) / ".nomedia");
378 File::CreateEmptyFile(GetSysDirectory(DIRECTORY_SAVEDATA) / ".nomedia");
379 File::CreateEmptyFile(GetSysDirectory(DIRECTORY_SYSTEM) / ".nomedia");
380 File::CreateEmptyFile(GetSysDirectory(DIRECTORY_TEXTURES) / ".nomedia");
381 File::CreateEmptyFile(GetSysDirectory(DIRECTORY_PLUGINS) / ".nomedia");
382 return true;
383 }
384
CheckFailedGPUBackends()385 static void CheckFailedGPUBackends() {
386 #ifdef _DEBUG
387 // If you're in debug mode, you probably don't want a fallback. If you're in release mode, use IGNORE below.
388 NOTICE_LOG(LOADER, "Not checking for failed graphics backends in debug mode");
389 return;
390 #endif
391
392 // We only want to do this once per process run and backend, to detect process crashes.
393 // If NativeShutdown is called before we finish, we might call this multiple times.
394 static int lastBackend = -1;
395 if (lastBackend == g_Config.iGPUBackend) {
396 return;
397 }
398 lastBackend = g_Config.iGPUBackend;
399
400 Path cache = GetSysDirectory(DIRECTORY_APP_CACHE) / "FailedGraphicsBackends.txt";
401
402 if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS)) {
403 std::string data;
404 if (File::ReadFileToString(true, cache, data))
405 g_Config.sFailedGPUBackends = data;
406 }
407
408 // Use this if you want to debug a graphics crash...
409 if (g_Config.sFailedGPUBackends == "IGNORE")
410 return;
411 else if (!g_Config.sFailedGPUBackends.empty())
412 ERROR_LOG(LOADER, "Failed graphics backends: %s", g_Config.sFailedGPUBackends.c_str());
413
414 // Okay, let's not try a backend in the failed list.
415 g_Config.iGPUBackend = g_Config.NextValidBackend();
416 if (lastBackend != g_Config.iGPUBackend) {
417 std::string param = GPUBackendToString((GPUBackend)lastBackend) + " -> " + GPUBackendToString((GPUBackend)g_Config.iGPUBackend);
418 System_SendMessage("graphics_failedBackend", param.c_str());
419 WARN_LOG(LOADER, "Failed graphics backend switched from %s (%d to %d)", param.c_str(), lastBackend, g_Config.iGPUBackend);
420 }
421 // And then let's - for now - add the current to the failed list.
422 if (g_Config.sFailedGPUBackends.empty()) {
423 g_Config.sFailedGPUBackends = GPUBackendToString((GPUBackend)g_Config.iGPUBackend);
424 } else if (g_Config.sFailedGPUBackends.find("ALL") == std::string::npos) {
425 g_Config.sFailedGPUBackends += "," + GPUBackendToString((GPUBackend)g_Config.iGPUBackend);
426 }
427
428 if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS)) {
429 // Let's try to create, in case it doesn't exist.
430 if (!File::Exists(GetSysDirectory(DIRECTORY_APP_CACHE)))
431 File::CreateDir(GetSysDirectory(DIRECTORY_APP_CACHE));
432 File::WriteStringToFile(true, g_Config.sFailedGPUBackends, cache);
433 } else {
434 // Just save immediately, since we have storage.
435 g_Config.Save("got storage permission");
436 }
437 }
438
ClearFailedGPUBackends()439 static void ClearFailedGPUBackends() {
440 if (g_Config.sFailedGPUBackends == "IGNORE")
441 return;
442
443 // We've successfully started graphics without crashing, hurray.
444 // In case they update drivers and have totally different problems much later, clear the failed list.
445 g_Config.sFailedGPUBackends.clear();
446 if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS) || System_GetPropertyBool(SYSPROP_ANDROID_SCOPED_STORAGE)) {
447 File::Delete(GetSysDirectory(DIRECTORY_APP_CACHE) / "FailedGraphicsBackends.txt");
448 } else {
449 g_Config.Save("clearFailedGPUBackends");
450 }
451 }
452
NativeInit(int argc,const char * argv[],const char * savegame_dir,const char * external_dir,const char * cache_dir)453 void NativeInit(int argc, const char *argv[], const char *savegame_dir, const char *external_dir, const char *cache_dir) {
454 net::Init(); // This needs to happen before we load the config. So on Windows we also run it in Main. It's fine to call multiple times.
455
456 ShaderTranslationInit();
457
458 InitFastMath(cpu_info.bNEON);
459 g_threadManager.Init(cpu_info.num_cores, cpu_info.logical_cpu_count);
460
461 SetupAudioFormats();
462
463 g_Discord.SetPresenceMenu();
464
465 // Make sure UI state is MENU.
466 ResetUIState();
467
468 bool skipLogo = false;
469 setlocale( LC_ALL, "C" );
470 std::string user_data_path = savegame_dir;
471 pendingMessages.clear();
472 pendingInputBoxes.clear();
473
474 // external_dir has all kinds of meanings depending on platform.
475 // on iOS it's even the path to bundled app assets. It's a mess.
476
477 // We want this to be FIRST.
478 #if PPSSPP_PLATFORM(IOS)
479 // Packed assets are included in app
480 VFSRegister("", new DirectoryAssetReader(Path(external_dir)));
481 #endif
482 #if defined(ASSETS_DIR)
483 VFSRegister("", new DirectoryAssetReader(Path(ASSETS_DIR)));
484 #endif
485 #if !defined(MOBILE_DEVICE) && !defined(_WIN32) && !PPSSPP_PLATFORM(SWITCH)
486 VFSRegister("", new DirectoryAssetReader(File::GetExeDirectory() / "assets"));
487 VFSRegister("", new DirectoryAssetReader(File::GetExeDirectory()));
488 VFSRegister("", new DirectoryAssetReader(Path("/usr/local/share/ppsspp/assets")));
489 VFSRegister("", new DirectoryAssetReader(Path("/usr/local/share/games/ppsspp/assets")));
490 VFSRegister("", new DirectoryAssetReader(Path("/usr/local/share/ppsspp/assets")));
491 VFSRegister("", new DirectoryAssetReader(Path("/usr/local/share/games/ppsspp/assets")));
492 #endif
493
494 #if PPSSPP_PLATFORM(SWITCH)
495 Path assetPath = Path(user_data_path) / "assets";
496 VFSRegister("", new DirectoryAssetReader(assetPath));
497 #else
498 VFSRegister("", new DirectoryAssetReader(Path("assets")));
499 #endif
500 VFSRegister("", new DirectoryAssetReader(Path(savegame_dir)));
501
502 #if (defined(MOBILE_DEVICE) || !defined(USING_QT_UI)) && !PPSSPP_PLATFORM(UWP)
503 if (host == nullptr) {
504 host = new NativeHost();
505 }
506 #endif
507
508 g_Config.defaultCurrentDirectory = Path("/");
509 g_Config.internalDataDirectory = Path(savegame_dir);
510
511 #if PPSSPP_PLATFORM(ANDROID)
512 // In Android 12 with scoped storage, due to the above, the external directory
513 // is no longer the plain root of external storage, but it's an app specific directory
514 // on external storage (g_extFilesDir).
515 if (System_GetPropertyBool(SYSPROP_ANDROID_SCOPED_STORAGE)) {
516 // There's no sensible default directory. Let the user browse for files.
517 g_Config.defaultCurrentDirectory.clear();
518 } else {
519 g_Config.memStickDirectory = Path(external_dir);
520 g_Config.defaultCurrentDirectory = Path(external_dir);
521 }
522
523 // Might also add an option to move it to internal / non-visible storage, but there's
524 // little point, really.
525
526 g_Config.flash0Directory = Path(external_dir) / "flash0";
527
528 Path memstickDirFile = g_Config.internalDataDirectory / "memstick_dir.txt";
529 if (File::Exists(memstickDirFile)) {
530 INFO_LOG(SYSTEM, "Reading '%s' to find memstick dir.", memstickDirFile.c_str());
531 std::string memstickDir;
532 if (File::ReadFileToString(true, memstickDirFile, memstickDir)) {
533 Path memstickPath(memstickDir);
534 if (!memstickPath.empty() && File::Exists(memstickPath)) {
535 g_Config.memStickDirectory = memstickPath;
536 INFO_LOG(SYSTEM, "Memstick Directory from memstick_dir.txt: '%s'", g_Config.memStickDirectory.c_str());
537 } else {
538 ERROR_LOG(SYSTEM, "Couldn't read directory '%s' specified by memstick_dir.txt.", memstickDir.c_str());
539 if (System_GetPropertyBool(SYSPROP_ANDROID_SCOPED_STORAGE)) {
540 // Ask the user to configure a memstick directory.
541 INFO_LOG(SYSTEM, "Asking the user.");
542 g_Config.memStickDirectory.clear();
543 }
544 }
545 }
546 } else {
547 INFO_LOG(SYSTEM, "No memstick directory file found (tried to open '%s')", memstickDirFile.c_str());
548 }
549
550 // Attempt to create directories after reading the path.
551 if (!System_GetPropertyBool(SYSPROP_ANDROID_SCOPED_STORAGE)) {
552 CreateDirectoriesAndroid();
553 }
554
555 #elif PPSSPP_PLATFORM(IOS)
556 g_Config.defaultCurrentDirectory = g_Config.internalDataDirectory;
557 g_Config.memStickDirectory = Path(user_data_path);
558 g_Config.flash0Directory = Path(std::string(external_dir)) / "flash0";
559 #elif PPSSPP_PLATFORM(SWITCH)
560 g_Config.memStickDirectory = g_Config.internalDataDirectory / "config/ppsspp";
561 g_Config.flash0Directory = g_Config.internalDataDirectory / "assets/flash0";
562 #elif !PPSSPP_PLATFORM(WINDOWS)
563 std::string config;
564 if (getenv("XDG_CONFIG_HOME") != NULL)
565 config = getenv("XDG_CONFIG_HOME");
566 else if (getenv("HOME") != NULL)
567 config = getenv("HOME") + std::string("/.config");
568 else // Just in case
569 config = "./config";
570
571 g_Config.memStickDirectory = Path(config) / "ppsspp";
572 g_Config.flash0Directory = File::GetExeDirectory() / "assets/flash0";
573 if (getenv("HOME") != nullptr) {
574 g_Config.defaultCurrentDirectory = Path(getenv("HOME"));
575 } else {
576 // Hm, should probably actually explicitly set the current directory..
577 // Though it's not many platforms that'll land us here.
578 g_Config.currentDirectory = Path(".");
579 }
580 #endif
581
582 if (cache_dir && strlen(cache_dir)) {
583 g_Config.appCacheDirectory = Path(cache_dir);
584 DiskCachingFileLoaderCache::SetCacheDir(g_Config.appCacheDirectory);
585 }
586
587 if (!LogManager::GetInstance()) {
588 LogManager::Init(&g_Config.bEnableLogging);
589 }
590
591 #if !PPSSPP_PLATFORM(WINDOWS)
592 g_Config.SetSearchPath(GetSysDirectory(DIRECTORY_SYSTEM));
593
594 // Note that if we don't have storage permission here, loading the config will
595 // fail and it will be set to the default. Later, we load again when we get permission.
596 g_Config.Load();
597 #endif
598
599 LogManager *logman = LogManager::GetInstance();
600
601 const char *fileToLog = 0;
602 Path stateToLoad;
603
604 bool gotBootFilename = false;
605 bool gotoGameSettings = false;
606 bool gotoTouchScreenTest = false;
607 boot_filename.clear();
608
609 // Parse command line
610 LogTypes::LOG_LEVELS logLevel = LogTypes::LINFO;
611 bool forceLogLevel = false;
612 const auto setLogLevel = [&logLevel, &forceLogLevel](LogTypes::LOG_LEVELS level) {
613 logLevel = level;
614 forceLogLevel = true;
615 };
616
617 for (int i = 1; i < argc; i++) {
618 if (argv[i][0] == '-') {
619 #if defined(__APPLE__)
620 // On Apple system debugged executable may get -NSDocumentRevisionsDebugMode YES in argv.
621 if (!strcmp(argv[i], "-NSDocumentRevisionsDebugMode") && argc - 1 > i) {
622 i++;
623 continue;
624 }
625 #endif
626 switch (argv[i][1]) {
627 case 'd':
628 // Enable debug logging
629 // Note that you must also change the max log level in Log.h.
630 setLogLevel(LogTypes::LDEBUG);
631 break;
632 case 'v':
633 // Enable verbose logging
634 // Note that you must also change the max log level in Log.h.
635 setLogLevel(LogTypes::LVERBOSE);
636 break;
637 case 'j':
638 g_Config.iCpuCore = (int)CPUCore::JIT;
639 g_Config.bSaveSettings = false;
640 break;
641 case 'i':
642 g_Config.iCpuCore = (int)CPUCore::INTERPRETER;
643 g_Config.bSaveSettings = false;
644 break;
645 case 'r':
646 g_Config.iCpuCore = (int)CPUCore::IR_JIT;
647 g_Config.bSaveSettings = false;
648 break;
649 case '-':
650 if (!strncmp(argv[i], "--loglevel=", strlen("--loglevel=")) && strlen(argv[i]) > strlen("--loglevel="))
651 setLogLevel(static_cast<LogTypes::LOG_LEVELS>(std::atoi(argv[i] + strlen("--loglevel="))));
652 if (!strncmp(argv[i], "--log=", strlen("--log=")) && strlen(argv[i]) > strlen("--log="))
653 fileToLog = argv[i] + strlen("--log=");
654 if (!strncmp(argv[i], "--state=", strlen("--state=")) && strlen(argv[i]) > strlen("--state="))
655 stateToLoad = Path(std::string(argv[i] + strlen("--state=")));
656 #if !defined(MOBILE_DEVICE)
657 if (!strncmp(argv[i], "--escape-exit", strlen("--escape-exit")))
658 g_Config.bPauseExitsEmulator = true;
659 #endif
660 if (!strncmp(argv[i], "--pause-menu-exit", strlen("--pause-menu-exit")))
661 g_Config.bPauseMenuExitsEmulator = true;
662 if (!strcmp(argv[i], "--fullscreen")) {
663 g_Config.bFullScreen = true;
664 System_SendMessage("toggle_fullscreen", "1");
665 }
666 if (!strcmp(argv[i], "--windowed")) {
667 g_Config.bFullScreen = false;
668 System_SendMessage("toggle_fullscreen", "0");
669 }
670 if (!strcmp(argv[i], "--touchscreentest"))
671 gotoTouchScreenTest = true;
672 if (!strcmp(argv[i], "--gamesettings"))
673 gotoGameSettings = true;
674 break;
675 }
676 } else {
677 // This parameter should be a boot filename. Only accept it if we
678 // don't already have one.
679 if (!gotBootFilename) {
680 gotBootFilename = true;
681 INFO_LOG(SYSTEM, "Boot filename found in args: '%s'", argv[i]);
682
683 bool okToLoad = true;
684 bool okToCheck = true;
685 if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS)) {
686 PermissionStatus status = System_GetPermissionStatus(SYSTEM_PERMISSION_STORAGE);
687 if (status == PERMISSION_STATUS_DENIED) {
688 ERROR_LOG(IO, "Storage permission denied. Launching without argument.");
689 okToLoad = false;
690 okToCheck = false;
691 } else if (status != PERMISSION_STATUS_GRANTED) {
692 ERROR_LOG(IO, "Storage permission not granted. Launching without argument check.");
693 okToCheck = false;
694 } else {
695 INFO_LOG(IO, "Storage permission granted.");
696 }
697 }
698 if (okToLoad) {
699 std::string str = std::string(argv[i]);
700 // Handle file:/// URIs, since you get those when creating shortcuts on some Android systems.
701 if (startsWith(str, "file:///")) {
702 str = UriDecode(str.substr(7));
703 INFO_LOG(IO, "Decoding '%s' to '%s'", argv[i], str.c_str());
704 }
705
706 boot_filename = Path(str);
707 skipLogo = true;
708 }
709 if (okToLoad && okToCheck) {
710 std::unique_ptr<FileLoader> fileLoader(ConstructFileLoader(boot_filename));
711 if (!fileLoader->Exists()) {
712 fprintf(stderr, "File not found: %s\n", boot_filename.c_str());
713 #if defined(_WIN32) || defined(__ANDROID__)
714 // Ignore and proceed.
715 #else
716 // Bail.
717 exit(1);
718 #endif
719 }
720 }
721 } else {
722 fprintf(stderr, "Can only boot one file");
723 #if defined(_WIN32) || defined(__ANDROID__)
724 // Ignore and proceed.
725 #else
726 // Bail.
727 exit(1);
728 #endif
729 }
730 }
731 }
732
733 if (fileToLog)
734 LogManager::GetInstance()->ChangeFileLog(fileToLog);
735
736 if (forceLogLevel)
737 LogManager::GetInstance()->SetAllLogLevels(logLevel);
738
739 PostLoadConfig();
740
741 #if PPSSPP_PLATFORM(ANDROID)
742 logger = new AndroidLogger();
743 logman->AddListener(logger);
744 #elif (defined(MOBILE_DEVICE) && !defined(_DEBUG))
745 // Enable basic logging for any kind of mobile device, since LogManager doesn't.
746 // The MOBILE_DEVICE/_DEBUG condition matches LogManager.cpp.
747 logger = new PrintfLogger();
748 logman->AddListener(logger);
749 #endif
750
751 if (System_GetPropertyBool(SYSPROP_SUPPORTS_PERMISSIONS)) {
752 if (System_GetPermissionStatus(SYSTEM_PERMISSION_STORAGE) != PERMISSION_STATUS_GRANTED) {
753 System_AskForPermission(SYSTEM_PERMISSION_STORAGE);
754 }
755 }
756
757 if (System_GetPropertyBool(SYSPROP_CAN_JIT) == false && g_Config.iCpuCore == (int)CPUCore::JIT) {
758 // Just gonna force it to the IR interpreter on startup.
759 // We don't hide the option, but we make sure it's off on bootup. In case someone wants
760 // to experiment in future iOS versions or something...
761 g_Config.iCpuCore = (int)CPUCore::IR_JIT;
762 }
763
764 auto des = GetI18NCategory("DesktopUI");
765 // Note to translators: do not translate this/add this to PPSSPP-lang's files.
766 // It's intended to be custom for every user.
767 // Only add it to your own personal copies of PPSSPP.
768 #if PPSSPP_PLATFORM(UWP)
769 // Roboto font is loaded in TextDrawerUWP.
770 g_Config.sFont = des->T("Font", "Roboto");
771 #elif defined(USING_WIN_UI) && !PPSSPP_PLATFORM(UWP)
772 // TODO: Could allow a setting to specify a font file to load?
773 // TODO: Make this a constant if we can sanely load the font on other systems?
774 AddFontResourceEx(L"assets/Roboto-Condensed.ttf", FR_PRIVATE, NULL);
775 // The font goes by two names, let's allow either one.
776 if (CheckFontIsUsable(L"Roboto Condensed")) {
777 g_Config.sFont = des->T("Font", "Roboto Condensed");
778 } else {
779 g_Config.sFont = des->T("Font", "Roboto");
780 }
781 #elif defined(USING_QT_UI)
782 size_t fontSize = 0;
783 uint8_t *fontData = VFSReadFile("Roboto-Condensed.ttf", &fontSize);
784 if (fontData) {
785 int fontID = QFontDatabase::addApplicationFontFromData(QByteArray((const char *)fontData, fontSize));
786 delete [] fontData;
787
788 QStringList fontsFound = QFontDatabase::applicationFontFamilies(fontID);
789 if (fontsFound.size() >= 1) {
790 // Might be "Roboto" or "Roboto Condensed".
791 g_Config.sFont = des->T("Font", fontsFound.at(0).toUtf8().constData());
792 }
793 } else {
794 // Let's try for it being a system font.
795 g_Config.sFont = des->T("Font", "Roboto Condensed");
796 }
797 #endif
798
799 // TODO: Load these in the background instead of synchronously.
800 g_BackgroundAudio.LoadSamples();
801
802 if (!boot_filename.empty() && stateToLoad.Valid()) {
803 SaveState::Load(stateToLoad, -1, [](SaveState::Status status, const std::string &message, void *) {
804 if (!message.empty() && (!g_Config.bDumpFrames || !g_Config.bDumpVideoOutput)) {
805 osm.Show(message, status == SaveState::Status::SUCCESS ? 2.0 : 5.0);
806 }
807 });
808 }
809
810 DEBUG_LOG(SYSTEM, "ScreenManager!");
811 screenManager = new ScreenManager();
812 if (g_Config.memStickDirectory.empty()) {
813 INFO_LOG(SYSTEM, "No memstick directory! Asking for one to be configured.");
814 screenManager->switchScreen(new LogoScreen(AfterLogoScreen::MEMSTICK_SCREEN_INITIAL_SETUP));
815 } else if (gotoGameSettings) {
816 screenManager->switchScreen(new LogoScreen(AfterLogoScreen::TO_GAME_SETTINGS));
817 } else if (gotoTouchScreenTest) {
818 screenManager->switchScreen(new MainScreen());
819 screenManager->push(new TouchTestScreen());
820 } else if (skipLogo) {
821 screenManager->switchScreen(new EmuScreen(boot_filename));
822 } else {
823 screenManager->switchScreen(new LogoScreen(AfterLogoScreen::DEFAULT));
824 }
825
826 // Easy testing
827 // screenManager->push(new GPUDriverTestScreen());
828
829 if (g_Config.bRemoteShareOnStartup && g_Config.bRemoteDebuggerOnStartup)
830 StartWebServer(WebServerFlags::ALL);
831 else if (g_Config.bRemoteShareOnStartup)
832 StartWebServer(WebServerFlags::DISCS);
833 else if (g_Config.bRemoteDebuggerOnStartup)
834 StartWebServer(WebServerFlags::DEBUGGER);
835
836 std::string sysName = System_GetProperty(SYSPROP_NAME);
837 isOuya = KeyMap::IsOuya(sysName);
838
839 #if !defined(MOBILE_DEVICE) && defined(USING_QT_UI)
840 MainWindow *mainWindow = new MainWindow(nullptr, g_Config.bFullScreen);
841 mainWindow->show();
842 if (host == nullptr) {
843 host = new QtHost(mainWindow);
844 }
845 #endif
846
847 // We do this here, instead of in NativeInitGraphics, because the display may be reset.
848 // When it's reset we don't want to forget all our managed things.
849 CheckFailedGPUBackends();
850 SetGPUBackend((GPUBackend) g_Config.iGPUBackend);
851 renderCounter = 0;
852
853 // Must be done restarting by now.
854 restarting = false;
855 }
856
MakeStyle(uint32_t fg,uint32_t bg)857 static UI::Style MakeStyle(uint32_t fg, uint32_t bg) {
858 UI::Style s;
859 s.background = UI::Drawable(bg);
860 s.fgColor = fg;
861 return s;
862 }
863
UIThemeInit()864 static void UIThemeInit() {
865 #if defined(USING_WIN_UI) || PPSSPP_PLATFORM(UWP) || defined(USING_QT_UI)
866 ui_theme.uiFont = UI::FontStyle(FontID("UBUNTU24"), g_Config.sFont.c_str(), 22);
867 ui_theme.uiFontSmall = UI::FontStyle(FontID("UBUNTU24"), g_Config.sFont.c_str(), 15);
868 ui_theme.uiFontSmaller = UI::FontStyle(FontID("UBUNTU24"), g_Config.sFont.c_str(), 12);
869 #else
870 ui_theme.uiFont = UI::FontStyle(FontID("UBUNTU24"), "", 20);
871 ui_theme.uiFontSmall = UI::FontStyle(FontID("UBUNTU24"), "", 14);
872 ui_theme.uiFontSmaller = UI::FontStyle(FontID("UBUNTU24"), "", 11);
873 #endif
874
875 ui_theme.checkOn = ImageID("I_CHECKEDBOX");
876 ui_theme.checkOff = ImageID("I_SQUARE");
877 ui_theme.whiteImage = ImageID("I_SOLIDWHITE");
878 ui_theme.sliderKnob = ImageID("I_CIRCLE");
879 ui_theme.dropShadow4Grid = ImageID("I_DROP_SHADOW");
880
881 ui_theme.itemStyle = MakeStyle(g_Config.uItemStyleFg, g_Config.uItemStyleBg);
882 ui_theme.itemFocusedStyle = MakeStyle(g_Config.uItemFocusedStyleFg, g_Config.uItemFocusedStyleBg);
883 ui_theme.itemDownStyle = MakeStyle(g_Config.uItemDownStyleFg, g_Config.uItemDownStyleBg);
884 ui_theme.itemDisabledStyle = MakeStyle(g_Config.uItemDisabledStyleFg, g_Config.uItemDisabledStyleBg);
885 ui_theme.itemHighlightedStyle = MakeStyle(g_Config.uItemHighlightedStyleFg, g_Config.uItemHighlightedStyleBg);
886
887 ui_theme.buttonStyle = MakeStyle(g_Config.uButtonStyleFg, g_Config.uButtonStyleBg);
888 ui_theme.buttonFocusedStyle = MakeStyle(g_Config.uButtonFocusedStyleFg, g_Config.uButtonFocusedStyleBg);
889 ui_theme.buttonDownStyle = MakeStyle(g_Config.uButtonDownStyleFg, g_Config.uButtonDownStyleBg);
890 ui_theme.buttonDisabledStyle = MakeStyle(g_Config.uButtonDisabledStyleFg, g_Config.uButtonDisabledStyleBg);
891 ui_theme.buttonHighlightedStyle = MakeStyle(g_Config.uButtonHighlightedStyleFg, g_Config.uButtonHighlightedStyleBg);
892
893 ui_theme.headerStyle.fgColor = g_Config.uHeaderStyleFg;
894 ui_theme.infoStyle = MakeStyle(g_Config.uInfoStyleFg, g_Config.uInfoStyleBg);
895
896 ui_theme.popupTitle.fgColor = g_Config.uPopupTitleStyleFg;
897 ui_theme.popupStyle = MakeStyle(g_Config.uPopupStyleFg, g_Config.uPopupStyleBg);
898 }
899
900 void RenderOverlays(UIContext *dc, void *userdata);
901 bool CreateGlobalPipelines();
902
NativeInitGraphics(GraphicsContext * graphicsContext)903 bool NativeInitGraphics(GraphicsContext *graphicsContext) {
904 INFO_LOG(SYSTEM, "NativeInitGraphics");
905
906 // We set this now so any resize during init is processed later.
907 resized = false;
908
909 Core_SetGraphicsContext(graphicsContext);
910 g_draw = graphicsContext->GetDrawContext();
911
912 if (!CreateGlobalPipelines()) {
913 ERROR_LOG(G3D, "Failed to create global pipelines");
914 return false;
915 }
916
917 // Load the atlas.
918 size_t atlas_data_size = 0;
919 if (!g_ui_atlas.IsMetadataLoaded()) {
920 const uint8_t *atlas_data = VFSReadFile("ui_atlas.meta", &atlas_data_size);
921 bool load_success = atlas_data != nullptr && g_ui_atlas.Load(atlas_data, atlas_data_size);
922 if (!load_success) {
923 ERROR_LOG(G3D, "Failed to load ui_atlas.meta - graphics will be broken.");
924 // Stumble along with broken visuals instead of dying.
925 }
926 delete[] atlas_data;
927 }
928
929 ui_draw2d.SetAtlas(&g_ui_atlas);
930 ui_draw2d_front.SetAtlas(&g_ui_atlas);
931
932 UIThemeInit();
933
934 uiContext = new UIContext();
935 uiContext->theme = &ui_theme;
936
937 ui_draw2d.Init(g_draw, texColorPipeline);
938 ui_draw2d_front.Init(g_draw, texColorPipeline);
939
940 uiContext->Init(g_draw, texColorPipeline, colorPipeline, &ui_draw2d, &ui_draw2d_front);
941 if (uiContext->Text())
942 uiContext->Text()->SetFont("Tahoma", 20, 0);
943
944 screenManager->setUIContext(uiContext);
945 screenManager->setDrawContext(g_draw);
946 screenManager->setPostRenderCallback(&RenderOverlays, nullptr);
947 screenManager->deviceRestored();
948
949 #ifdef _WIN32
950 winAudioBackend = CreateAudioBackend((AudioBackendType)g_Config.iAudioBackend);
951 #if PPSSPP_PLATFORM(UWP)
952 winAudioBackend->Init(0, &Win32Mix, 44100);
953 #else
954 winAudioBackend->Init(MainWindow::GetHWND(), &Win32Mix, 44100);
955 #endif
956 #endif
957
958 #if defined(_WIN32) && !PPSSPP_PLATFORM(UWP)
959 if (IsWin7OrHigher()) {
960 winCamera = new WindowsCaptureDevice(CAPTUREDEVIDE_TYPE::VIDEO);
961 winCamera->sendMessage({ CAPTUREDEVIDE_COMMAND::INITIALIZE, nullptr });
962 winMic = new WindowsCaptureDevice(CAPTUREDEVIDE_TYPE::AUDIO);
963 winMic->sendMessage({ CAPTUREDEVIDE_COMMAND::INITIALIZE, nullptr });
964 }
965 #endif
966
967 g_gameInfoCache = new GameInfoCache();
968
969 if (gpu) {
970 gpu->DeviceRestore();
971 }
972
973 INFO_LOG(SYSTEM, "NativeInitGraphics completed");
974 return true;
975 }
976
CreateGlobalPipelines()977 bool CreateGlobalPipelines() {
978 using namespace Draw;
979
980 InputLayout *inputLayout = ui_draw2d.CreateInputLayout(g_draw);
981 BlendState *blendNormal = g_draw->CreateBlendState({ true, 0xF, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA });
982 DepthStencilState *depth = g_draw->CreateDepthStencilState({ false, false, Comparison::LESS });
983 RasterState *rasterNoCull = g_draw->CreateRasterState({});
984
985 PipelineDesc colorDesc{
986 Primitive::TRIANGLE_LIST,
987 { g_draw->GetVshaderPreset(VS_COLOR_2D), g_draw->GetFshaderPreset(FS_COLOR_2D) },
988 inputLayout, depth, blendNormal, rasterNoCull, &vsColBufDesc,
989 };
990 PipelineDesc texColorDesc{
991 Primitive::TRIANGLE_LIST,
992 { g_draw->GetVshaderPreset(VS_TEXTURE_COLOR_2D), g_draw->GetFshaderPreset(FS_TEXTURE_COLOR_2D) },
993 inputLayout, depth, blendNormal, rasterNoCull, &vsTexColBufDesc,
994 };
995
996 colorPipeline = g_draw->CreateGraphicsPipeline(colorDesc);
997 if (!colorPipeline) {
998 // Something really critical is wrong, don't care much about correct releasing of the states.
999 return false;
1000 }
1001
1002 texColorPipeline = g_draw->CreateGraphicsPipeline(texColorDesc);
1003 if (!texColorPipeline) {
1004 // Something really critical is wrong, don't care much about correct releasing of the states.
1005 return false;
1006 }
1007
1008 // Release these now, reference counting should ensure that they get completely released
1009 // once we delete both pipelines.
1010 inputLayout->Release();
1011 rasterNoCull->Release();
1012 blendNormal->Release();
1013 depth->Release();
1014 return true;
1015 }
1016
NativeShutdownGraphics()1017 void NativeShutdownGraphics() {
1018 screenManager->deviceLost();
1019
1020 if (gpu)
1021 gpu->DeviceLost();
1022
1023 INFO_LOG(SYSTEM, "NativeShutdownGraphics");
1024
1025 #if PPSSPP_PLATFORM(WINDOWS)
1026 delete winAudioBackend;
1027 winAudioBackend = nullptr;
1028 #endif
1029
1030 #if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
1031 if (winCamera) {
1032 winCamera->waitShutDown();
1033 delete winCamera;
1034 winCamera = nullptr;
1035 }
1036 if (winMic) {
1037 winMic->waitShutDown();
1038 delete winMic;
1039 winMic = nullptr;
1040 }
1041 #endif
1042
1043 UIBackgroundShutdown();
1044
1045 delete g_gameInfoCache;
1046 g_gameInfoCache = nullptr;
1047
1048 delete uiContext;
1049 uiContext = nullptr;
1050
1051 ui_draw2d.Shutdown();
1052 ui_draw2d_front.Shutdown();
1053
1054 if (colorPipeline) {
1055 colorPipeline->Release();
1056 colorPipeline = nullptr;
1057 }
1058 if (texColorPipeline) {
1059 texColorPipeline->Release();
1060 texColorPipeline = nullptr;
1061 }
1062
1063 INFO_LOG(SYSTEM, "NativeShutdownGraphics done");
1064 }
1065
TakeScreenshot()1066 void TakeScreenshot() {
1067 g_TakeScreenshot = false;
1068
1069 Path path = GetSysDirectory(DIRECTORY_SCREENSHOT);
1070 if (!File::Exists(path)) {
1071 File::CreateDir(path);
1072 }
1073
1074 // First, find a free filename.
1075 int i = 0;
1076
1077 std::string gameId = g_paramSFO.GetDiscID();
1078
1079 Path filename;
1080 while (i < 10000){
1081 if (g_Config.bScreenshotsAsPNG)
1082 filename = path / StringFromFormat("%s_%05d.png", gameId.c_str(), i);
1083 else
1084 filename = path / StringFromFormat("%s_%05d.jpg", gameId.c_str(), i);
1085 File::FileInfo info;
1086 if (!File::Exists(filename))
1087 break;
1088 i++;
1089 }
1090
1091 bool success = TakeGameScreenshot(filename, g_Config.bScreenshotsAsPNG ? ScreenshotFormat::PNG : ScreenshotFormat::JPG, SCREENSHOT_OUTPUT);
1092 if (success) {
1093 osm.Show(filename.ToVisualString());
1094 } else {
1095 auto err = GetI18NCategory("Error");
1096 osm.Show(err->T("Could not save screenshot file"));
1097 }
1098 }
1099
RenderOverlays(UIContext * dc,void * userdata)1100 void RenderOverlays(UIContext *dc, void *userdata) {
1101 // Thin bar at the top of the screen like Chrome.
1102 std::vector<float> progress = g_DownloadManager.GetCurrentProgress();
1103 if (!progress.empty()) {
1104 static const uint32_t colors[4] = {
1105 0xFFFFFFFF,
1106 0xFFCCCCCC,
1107 0xFFAAAAAA,
1108 0xFF777777,
1109 };
1110
1111 dc->Begin();
1112 int h = 5;
1113 for (size_t i = 0; i < progress.size(); i++) {
1114 float barWidth = 10 + (dc->GetBounds().w - 10) * progress[i];
1115 Bounds bounds(0, h * i, barWidth, h);
1116 UI::Drawable solid(colors[i & 3]);
1117 dc->FillRect(solid, bounds);
1118 }
1119 dc->Flush();
1120 }
1121
1122 if (g_TakeScreenshot) {
1123 TakeScreenshot();
1124 }
1125 }
1126
NativeRender(GraphicsContext * graphicsContext)1127 void NativeRender(GraphicsContext *graphicsContext) {
1128 _assert_(graphicsContext != nullptr);
1129 _assert_(screenManager != nullptr);
1130
1131 g_GameManager.Update();
1132
1133 if (GetUIState() != UISTATE_INGAME) {
1134 // Note: We do this from NativeRender so that the graphics context is
1135 // guaranteed valid, to be safe - g_gameInfoCache messes around with textures.
1136 g_BackgroundAudio.Update();
1137 }
1138
1139 float xres = dp_xres;
1140 float yres = dp_yres;
1141
1142 // Apply the UIContext bounds as a 2D transformation matrix.
1143 // TODO: This should be moved into the draw context...
1144 Matrix4x4 ortho;
1145 switch (GetGPUBackend()) {
1146 case GPUBackend::VULKAN:
1147 ortho.setOrthoD3D(0.0f, xres, 0, yres, -1.0f, 1.0f);
1148 break;
1149 case GPUBackend::DIRECT3D9:
1150 ortho.setOrthoD3D(0.0f, xres, yres, 0.0f, -1.0f, 1.0f);
1151 Matrix4x4 translation;
1152 // Account for the small window adjustment.
1153 translation.setTranslation(Vec3(-0.5f * g_dpi_scale_x / g_dpi_scale_real_x, -0.5f * g_dpi_scale_y / g_dpi_scale_real_y, 0.0f));
1154 ortho = translation * ortho;
1155 break;
1156 case GPUBackend::DIRECT3D11:
1157 ortho.setOrthoD3D(0.0f, xres, yres, 0.0f, -1.0f, 1.0f);
1158 break;
1159 case GPUBackend::OPENGL:
1160 default:
1161 ortho.setOrtho(0.0f, xres, yres, 0.0f, -1.0f, 1.0f);
1162 break;
1163 }
1164
1165 // Compensate for rotated display if needed.
1166 if (g_display_rotation != DisplayRotation::ROTATE_0) {
1167 ortho = ortho * g_display_rot_matrix;
1168 }
1169
1170 ui_draw2d.PushDrawMatrix(ortho);
1171 ui_draw2d_front.PushDrawMatrix(ortho);
1172
1173 // All actual rendering happen in here.
1174 screenManager->render();
1175 if (screenManager->getUIContext()->Text()) {
1176 screenManager->getUIContext()->Text()->OncePerFrame();
1177 }
1178
1179 if (resized) {
1180 INFO_LOG(G3D, "Resized flag set - recalculating bounds");
1181 resized = false;
1182
1183 if (uiContext) {
1184 // Modifying the bounds here can be used to "inset" the whole image to gain borders for TV overscan etc.
1185 // The UI now supports any offset but not the EmuScreen yet.
1186 uiContext->SetBounds(Bounds(0, 0, dp_xres, dp_yres));
1187 // uiContext->SetBounds(Bounds(dp_xres/2, 0, dp_xres / 2, dp_yres / 2));
1188
1189 // OSX 10.6 and SDL 1.2 bug.
1190 #if defined(__APPLE__) && !defined(USING_QT_UI)
1191 static int dp_xres_old = dp_xres;
1192 if (dp_xres != dp_xres_old) {
1193 // uiTexture->Load("ui_atlas.zim");
1194 dp_xres_old = dp_xres;
1195 }
1196 #endif
1197 }
1198
1199 graphicsContext->Resize();
1200 screenManager->resized();
1201
1202 // TODO: Move this to the GraphicsContext objects for each backend.
1203 #if !PPSSPP_PLATFORM(WINDOWS) && !defined(ANDROID)
1204 PSP_CoreParameter().pixelWidth = pixel_xres;
1205 PSP_CoreParameter().pixelHeight = pixel_yres;
1206 NativeMessageReceived("gpu_resized", "");
1207 #endif
1208 } else {
1209 // INFO_LOG(G3D, "Polling graphics context");
1210 graphicsContext->Poll();
1211 }
1212
1213 ui_draw2d.PopDrawMatrix();
1214 ui_draw2d_front.PopDrawMatrix();
1215
1216 if (renderCounter < 10 && ++renderCounter == 10) {
1217 // We're rendering fine, clear out failure info.
1218 ClearFailedGPUBackends();
1219 }
1220 }
1221
HandleGlobalMessage(const std::string & msg,const std::string & value)1222 void HandleGlobalMessage(const std::string &msg, const std::string &value) {
1223 if (msg == "inputDeviceConnected") {
1224 KeyMap::NotifyPadConnected(value);
1225 }
1226 if (msg == "bgImage_updated") {
1227 if (!value.empty()) {
1228 Path dest = GetSysDirectory(DIRECTORY_SYSTEM) / (endsWithNoCase(value, ".jpg") ? "background.jpg" : "background.png");
1229 File::Copy(Path(value), dest);
1230 }
1231 UIBackgroundShutdown();
1232 // It will init again automatically. We can't init outside a frame on Vulkan.
1233 }
1234 if (msg == "savestate_displayslot") {
1235 auto sy = GetI18NCategory("System");
1236 std::string msg = StringFromFormat("%s: %d", sy->T("Savestate Slot"), SaveState::GetCurrentSlot() + 1);
1237 // Show for the same duration as the preview.
1238 osm.Show(msg, 2.0f, 0xFFFFFF, -1, true, "savestate_slot");
1239 }
1240 if (msg == "gpu_resized" || msg == "gpu_clearCache") {
1241 if (gpu) {
1242 gpu->ClearCacheNextFrame();
1243 gpu->Resized();
1244 }
1245 Reporting::UpdateConfig();
1246 }
1247 if (msg == "core_powerSaving") {
1248 if (value != "false") {
1249 auto sy = GetI18NCategory("System");
1250 #if PPSSPP_PLATFORM(ANDROID)
1251 osm.Show(sy->T("WARNING: Android battery save mode is on"), 2.0f, 0xFFFFFF, -1, true, "core_powerSaving");
1252 #else
1253 osm.Show(sy->T("WARNING: Battery save mode is on"), 2.0f, 0xFFFFFF, -1, true, "core_powerSaving");
1254 #endif
1255 }
1256 Core_SetPowerSaving(value != "false");
1257 }
1258 if (msg == "permission_granted" && value == "storage") {
1259 #if PPSSPP_PLATFORM(ANDROID)
1260 CreateDirectoriesAndroid();
1261 #endif
1262 // We must have failed to load the config before, so load it now to avoid overwriting the old config
1263 // with a freshly generated one.
1264 // NOTE: If graphics backend isn't what's in the config (due to error fallback, or not matching the default
1265 // and then getting permission), it will get out of sync. So we save and restore g_Config.iGPUBackend.
1266 // Ideally we should simply reinitialize graphics to the mode from the config, but there are potential issues
1267 // and I can't risk it before 1.9.0.
1268 int gpuBackend = g_Config.iGPUBackend;
1269 INFO_LOG(IO, "Reloading config after storage permission grant.");
1270 g_Config.Reload();
1271 PostLoadConfig();
1272 g_Config.iGPUBackend = gpuBackend;
1273 }
1274 if (msg == "app_resumed" || msg == "got_focus") {
1275 // Assume that the user may have modified things.
1276 MemoryStick_NotifyWrite();
1277 }
1278 }
1279
NativeUpdate()1280 void NativeUpdate() {
1281 PROFILE_END_FRAME();
1282
1283 std::vector<PendingMessage> toProcess;
1284 std::vector<PendingInputBox> inputToProcess;
1285 {
1286 std::lock_guard<std::mutex> lock(pendingMutex);
1287 toProcess = std::move(pendingMessages);
1288 inputToProcess = std::move(pendingInputBoxes);
1289 pendingMessages.clear();
1290 pendingInputBoxes.clear();
1291 }
1292
1293 for (const auto &item : toProcess) {
1294 HandleGlobalMessage(item.msg, item.value);
1295 screenManager->sendMessage(item.msg.c_str(), item.value.c_str());
1296 }
1297 for (const auto &item : inputToProcess) {
1298 item.cb(item.result, item.value);
1299 }
1300
1301 g_DownloadManager.Update();
1302 screenManager->update();
1303
1304 g_Discord.Update();
1305
1306 UI::SetSoundEnabled(g_Config.bUISound);
1307 }
1308
NativeIsAtTopLevel()1309 bool NativeIsAtTopLevel() {
1310 // This might need some synchronization?
1311 if (!screenManager) {
1312 ERROR_LOG(SYSTEM, "No screen manager active");
1313 return false;
1314 }
1315 Screen *currentScreen = screenManager->topScreen();
1316 if (currentScreen) {
1317 bool top = currentScreen->isTopLevel();
1318 INFO_LOG(SYSTEM, "Screen toplevel: %i", (int)top);
1319 return currentScreen->isTopLevel();
1320 } else {
1321 ERROR_LOG(SYSTEM, "No current screen");
1322 return false;
1323 }
1324 }
1325
NativeTouch(const TouchInput & touch)1326 bool NativeTouch(const TouchInput &touch) {
1327 if (screenManager) {
1328 // Brute force prevent NaNs from getting into the UI system
1329 if (my_isnan(touch.x) || my_isnan(touch.y)) {
1330 return false;
1331 }
1332 screenManager->touch(touch);
1333 return true;
1334 } else {
1335 return false;
1336 }
1337 }
1338
NativeKey(const KeyInput & key)1339 bool NativeKey(const KeyInput &key) {
1340 // INFO_LOG(SYSTEM, "Key code: %i flags: %i", key.keyCode, key.flags);
1341 #if !defined(MOBILE_DEVICE)
1342 if (g_Config.bPauseExitsEmulator) {
1343 static std::vector<int> pspKeys;
1344 pspKeys.clear();
1345 if (KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &pspKeys)) {
1346 if (std::find(pspKeys.begin(), pspKeys.end(), VIRTKEY_PAUSE) != pspKeys.end()) {
1347 System_SendMessage("finish", "");
1348 return true;
1349 }
1350 }
1351 }
1352 #endif
1353 bool retval = false;
1354 if (screenManager)
1355 retval = screenManager->key(key);
1356 return retval;
1357 }
1358
NativeAxis(const AxisInput & axis)1359 bool NativeAxis(const AxisInput &axis) {
1360 if (!screenManager) {
1361 // Too early.
1362 return false;
1363 }
1364
1365 using namespace TiltEventProcessor;
1366
1367 // only handle tilt events if tilt is enabled.
1368 if (g_Config.iTiltInputType == TILT_NULL) {
1369 // if tilt events are disabled, then run it through the usual way.
1370 if (screenManager) {
1371 return screenManager->axis(axis);
1372 } else {
1373 return false;
1374 }
1375 }
1376
1377 // create the base coordinate tilt system from the calibration data.
1378 Tilt baseTilt;
1379 baseTilt.x_ = g_Config.fTiltBaseX;
1380 baseTilt.y_ = g_Config.fTiltBaseY;
1381
1382 // figure out what the current tilt orientation is by checking the axis event
1383 // This is static, since we need to remember where we last were (in terms of orientation)
1384 static Tilt currentTilt;
1385
1386 // tilt on x or y?
1387 static bool verticalTilt;
1388 if (g_Config.iTiltOrientation == 0)
1389 verticalTilt = false;
1390 else if (g_Config.iTiltOrientation == 1)
1391 verticalTilt = true;
1392
1393 // x and y are flipped if we are in landscape orientation. The events are
1394 // sent with respect to the portrait coordinate system, while we
1395 // take all events in landscape.
1396 // see [http://developer.android.com/guide/topics/sensors/sensors_overview.html] for details
1397 bool portrait = dp_yres > dp_xres;
1398 switch (axis.axisId) {
1399 //TODO: make this generic.
1400 case JOYSTICK_AXIS_ACCELEROMETER_X:
1401 if (verticalTilt) {
1402 if (fabs(axis.value) < 0.8f && g_Config.iTiltOrientation == 2) // Auto tilt switch
1403 verticalTilt = false;
1404 else
1405 return false; // Tilt on Z instead
1406 }
1407 if (portrait) {
1408 currentTilt.x_ = axis.value;
1409 } else {
1410 currentTilt.y_ = axis.value;
1411 }
1412 break;
1413
1414 case JOYSTICK_AXIS_ACCELEROMETER_Y:
1415 if (portrait) {
1416 currentTilt.y_ = axis.value;
1417 } else {
1418 currentTilt.x_ = axis.value;
1419 }
1420 break;
1421
1422 case JOYSTICK_AXIS_ACCELEROMETER_Z:
1423 if (!verticalTilt) {
1424 if (fabs(axis.value) < 0.8f && g_Config.iTiltOrientation == 2) // Auto tilt switch
1425 verticalTilt = true;
1426 else
1427 return false; // Tilt on X instead
1428 }
1429 if (portrait) {
1430 currentTilt.x_ = -axis.value;
1431 } else {
1432 currentTilt.y_ = -axis.value;
1433 }
1434 break;
1435
1436 case JOYSTICK_AXIS_OUYA_UNKNOWN1:
1437 case JOYSTICK_AXIS_OUYA_UNKNOWN2:
1438 case JOYSTICK_AXIS_OUYA_UNKNOWN3:
1439 case JOYSTICK_AXIS_OUYA_UNKNOWN4:
1440 //Don't know how to handle these. Someone should figure it out.
1441 //Does the Ouya even have an accelerometer / gyro? I can't find any reference to these
1442 //in the Ouya docs...
1443 return false;
1444
1445 default:
1446 // Don't take over completely!
1447 return screenManager->axis(axis);
1448 }
1449
1450 //figure out the sensitivity of the tilt. (sensitivity is originally 0 - 100)
1451 //We divide by 50, so that the rest of the 50 units can be used to overshoot the
1452 //target. If you want control, you'd keep the sensitivity ~50.
1453 //For games that don't need much control but need fast reactions,
1454 //then a value of 70-80 is the way to go.
1455 float xSensitivity = g_Config.iTiltSensitivityX / 50.0;
1456 float ySensitivity = g_Config.iTiltSensitivityY / 50.0;
1457
1458 //now transform out current tilt to the calibrated coordinate system
1459 Tilt trueTilt = GenTilt(baseTilt, currentTilt, g_Config.bInvertTiltX, g_Config.bInvertTiltY, g_Config.fDeadzoneRadius, xSensitivity, ySensitivity);
1460
1461 TranslateTiltToInput(trueTilt);
1462 return true;
1463 }
1464
NativeMessageReceived(const char * message,const char * value)1465 void NativeMessageReceived(const char *message, const char *value) {
1466 // We can only have one message queued.
1467 std::lock_guard<std::mutex> lock(pendingMutex);
1468 PendingMessage pendingMessage;
1469 pendingMessage.msg = message;
1470 pendingMessage.value = value;
1471 pendingMessages.push_back(pendingMessage);
1472 }
1473
NativeInputBoxReceived(std::function<void (bool,const std::string &)> cb,bool result,const std::string & value)1474 void NativeInputBoxReceived(std::function<void(bool, const std::string &)> cb, bool result, const std::string &value) {
1475 std::lock_guard<std::mutex> lock(pendingMutex);
1476 PendingInputBox pendingMessage;
1477 pendingMessage.cb = cb;
1478 pendingMessage.result = result;
1479 pendingMessage.value = value;
1480 pendingInputBoxes.push_back(pendingMessage);
1481 }
1482
NativeResized()1483 void NativeResized() {
1484 // NativeResized can come from any thread so we just set a flag, then process it later.
1485 INFO_LOG(G3D, "NativeResized - setting flag");
1486 resized = true;
1487 }
1488
NativeSetRestarting()1489 void NativeSetRestarting() {
1490 restarting = true;
1491 }
1492
NativeIsRestarting()1493 bool NativeIsRestarting() {
1494 return restarting;
1495 }
1496
NativeShutdown()1497 void NativeShutdown() {
1498 if (screenManager)
1499 screenManager->shutdown();
1500 delete screenManager;
1501 screenManager = nullptr;
1502
1503 host->ShutdownGraphics();
1504
1505 #if !PPSSPP_PLATFORM(UWP)
1506 delete host;
1507 host = nullptr;
1508 #endif
1509 g_Config.Save("NativeShutdown");
1510
1511 INFO_LOG(SYSTEM, "NativeShutdown called");
1512
1513 ShutdownWebServer();
1514
1515 System_SendMessage("finish", "");
1516
1517 net::Shutdown();
1518
1519 g_Discord.Shutdown();
1520
1521 ShaderTranslationShutdown();
1522
1523 // Avoid shutting this down when restarting core.
1524 if (!restarting)
1525 LogManager::Shutdown();
1526
1527 if (logger) {
1528 delete logger;
1529 logger = nullptr;
1530 }
1531
1532 g_threadManager.Teardown();
1533
1534 // Previously we did exit() here on Android but that makes it hard to do things like restart on backend change.
1535 // I think we handle most globals correctly or correct-enough now.
1536 }
1537