| 1 | // Copyright 2014 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/UICommon.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <clocale> |
| 8 | #include <cmath> |
| 9 | #include <locale> |
| 10 | #include <memory> |
| 11 | #ifdef _WIN32 |
| 12 | #include <shlobj.h> // for SHGetFolderPath |
| 13 | |
| 14 | #include <wil/resource.h> |
| 15 | #endif |
| 16 | |
| 17 | #include <fmt/format.h> |
| 18 | |
| 19 | #include "Common/Common.h" |
| 20 | #include "Common/CommonPaths.h" |
| 21 | #include "Common/Config/Config.h" |
| 22 | #include "Common/FileUtil.h" |
| 23 | #include "Common/Logging/LogManager.h" |
| 24 | #include "Common/MathUtil.h" |
| 25 | #include "Common/MsgHandler.h" |
| 26 | |
| 27 | #include "Core/Config/MainSettings.h" |
| 28 | #include "Core/ConfigLoaders/BaseConfigLoader.h" |
| 29 | #include "Core/ConfigManager.h" |
| 30 | #include "Core/Core.h" |
| 31 | #include "Core/FreeLookManager.h" |
| 32 | #include "Core/HW/GBAPad.h" |
| 33 | #include "Core/HW/GCKeyboard.h" |
| 34 | #include "Core/HW/GCPad.h" |
| 35 | #include "Core/HW/ProcessorInterface.h" |
| 36 | #include "Core/HW/Wiimote.h" |
| 37 | #include "Core/HotkeyManager.h" |
| 38 | #include "Core/IOS/IOS.h" |
| 39 | #include "Core/IOS/STM/STM.h" |
| 40 | #include "Core/System.h" |
| 41 | #include "Core/WiiRoot.h" |
| 42 | |
| 43 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 44 | #include "InputCommon/GCAdapter.h" |
| 45 | |
| 46 | #include "UICommon/DiscordPresence.h" |
| 47 | |
| 48 | #include "VideoCommon/Statistics.h" |
| 49 | #include "VideoCommon/VideoBackendBase.h" |
| 50 | #include "VideoCommon/VideoConfig.h" |
| 51 | |
| 52 | #ifdef HAVE_QTDBUS |
| 53 | #include "UICommon/DBusUtils.h" |
| 54 | #endif |
| 55 | |
| 56 | #ifdef __APPLE__ |
| 57 | #include <IOKit/pwr_mgt/IOPMLib.h> |
| 58 | #endif |
| 59 | |
| 60 | namespace UICommon |
| 61 | { |
| 62 | static Config::ConfigChangedCallbackID s_config_changed_callback_id; |
| 63 | static Common::HookableEvent<> s_flush_unsaved_data_event_hook; |
| 64 | |
| 65 | static void CreateDumpPath(std::string path) |
| 66 | { |
| 67 | if (!path.empty()) |
| 68 | File::SetUserPath(D_DUMP_IDX, std::move(path)); |
| 69 | File::CreateFullPath(File::GetUserPath(D_DUMPAUDIO_IDX)); |
| 70 | File::CreateFullPath(File::GetUserPath(D_DUMPDSP_IDX)); |
| 71 | File::CreateFullPath(File::GetUserPath(D_DUMPSSL_IDX)); |
| 72 | File::CreateFullPath(File::GetUserPath(D_DUMPFRAMES_IDX)); |
| 73 | File::CreateFullPath(File::GetUserPath(D_DUMPOBJECTS_IDX)); |
| 74 | File::CreateFullPath(File::GetUserPath(D_DUMPTEXTURES_IDX)); |
| 75 | File::CreateFullPath(File::GetUserPath(D_DUMPDEBUG_IDX)); |
| 76 | File::CreateFullPath(File::GetUserPath(D_DUMPDEBUG_BRANCHWATCH_IDX)); |
| 77 | File::CreateFullPath(File::GetUserPath(D_DUMPDEBUG_JITBLOCKS_IDX)); |
| 78 | } |
| 79 | |
| 80 | static void CreateLoadPath(std::string path) |
| 81 | { |
| 82 | if (!path.empty()) |
| 83 | File::SetUserPath(D_LOAD_IDX, std::move(path)); |
| 84 | File::CreateFullPath(File::GetUserPath(D_HIRESTEXTURES_IDX)); |
| 85 | File::CreateFullPath(File::GetUserPath(D_RIIVOLUTION_IDX)); |
| 86 | File::CreateFullPath(File::GetUserPath(D_GRAPHICSMOD_IDX)); |
| 87 | File::CreateFullPath(File::GetUserPath(D_DYNAMICINPUT_IDX)); |
| 88 | } |
| 89 | |
| 90 | static void CreateResourcePackPath(std::string path) |
| 91 | { |
| 92 | if (!path.empty()) |
| 93 | File::SetUserPath(D_RESOURCEPACK_IDX, std::move(path)); |
| 94 | } |
| 95 | |
| 96 | static void CreateWFSPath(const std::string& path) |
| 97 | { |
| 98 | if (!path.empty()) |
| 99 | File::SetUserPath(D_WFSROOT_IDX, path + '/'); |
| 100 | } |
| 101 | |
| 102 | static void InitCustomPaths() |
| 103 | { |
| 104 | File::SetUserPath(D_WIIROOT_IDX, Config::Get(Config::MAIN_FS_PATH)); |
| 105 | CreateLoadPath(Config::Get(Config::MAIN_LOAD_PATH)); |
| 106 | CreateDumpPath(Config::Get(Config::MAIN_DUMP_PATH)); |
| 107 | CreateResourcePackPath(Config::Get(Config::MAIN_RESOURCEPACK_PATH)); |
| 108 | CreateWFSPath(Config::Get(Config::MAIN_WFS_PATH)); |
| 109 | File::SetUserPath(F_WIISDCARDIMAGE_IDX, Config::Get(Config::MAIN_WII_SD_CARD_IMAGE_PATH)); |
| 110 | File::SetUserPath(D_WIISDCARDSYNCFOLDER_IDX, |
| 111 | Config::Get(Config::MAIN_WII_SD_CARD_SYNC_FOLDER_PATH)); |
| 112 | File::CreateFullPath(File::GetUserPath(D_WIISDCARDSYNCFOLDER_IDX)); |
| 113 | #ifdef HAS_LIBMGBA |
| 114 | File::SetUserPath(F_GBABIOS_IDX, Config::Get(Config::MAIN_GBA_BIOS_PATH)); |
| 115 | File::SetUserPath(D_GBASAVES_IDX, Config::Get(Config::MAIN_GBA_SAVES_PATH)); |
| 116 | File::CreateFullPath(File::GetUserPath(D_GBASAVES_IDX)); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | static void RefreshConfig() |
| 121 | { |
| 122 | Common::SetEnableAlert(Config::Get(Config::MAIN_USE_PANIC_HANDLERS)); |
| 123 | Common::SetAbortOnPanicAlert(Config::Get(Config::MAIN_ABORT_ON_PANIC_ALERT)); |
| 124 | } |
| 125 | |
| 126 | void Init() |
| 127 | { |
| 128 | Core::RestoreWiiSettings(Core::RestoreReason::CrashRecovery); |
| 129 | |
| 130 | Config::Init(); |
| 131 | const auto config_changed_callback = [] { |
| 132 | InitCustomPaths(); |
| 133 | RefreshConfig(); |
| 134 | }; |
| 135 | s_config_changed_callback_id = Config::AddConfigChangedCallback(config_changed_callback); |
| 136 | Config::AddLayer(ConfigLoaders::GenerateBaseConfigLoader()); |
| 137 | SConfig::Init(); |
| 138 | g_Config.Init(); |
| 139 | Discord::Init(); |
| 140 | Common::Log::LogManager::Init(); |
| 141 | VideoBackendBase::ActivateBackend(Config::Get(Config::MAIN_GFX_BACKEND)); |
| 142 | Statistics::Init(); |
| 143 | |
| 144 | RefreshConfig(); |
| 145 | } |
| 146 | |
| 147 | void Shutdown() |
| 148 | { |
| 149 | Config::RemoveConfigChangedCallback(s_config_changed_callback_id); |
| 150 | |
| 151 | Statistics::Shutdown(); |
| 152 | GCAdapter::Shutdown(); |
| 153 | WiimoteReal::Shutdown(); |
| 154 | Common::Log::LogManager::Shutdown(); |
| 155 | Discord::Shutdown(); |
| 156 | g_Config.Shutdown(); |
| 157 | SConfig::Shutdown(); |
| 158 | Config::Shutdown(); |
| 159 | } |
| 160 | |
| 161 | [[nodiscard]] Common::EventHook AddFlushUnsavedDataCallback(std::function<void()> callback) |
| 162 | { |
| 163 | return s_flush_unsaved_data_event_hook.Register(std::move(callback)); |
| 164 | } |
| 165 | |
| 166 | void FlushUnsavedData() |
| 167 | { |
| 168 | INFO_LOG_FMT(CORE, "Flushing unsaved data..."); |
| 169 | s_flush_unsaved_data_event_hook.Trigger(); |
| 170 | } |
| 171 | |
| 172 | void InitControllers(const WindowSystemInfo& wsi) |
| 173 | { |
| 174 | if (g_controller_interface.IsInit()) |
| 175 | return; |
| 176 | |
| 177 | g_controller_interface.Initialize(wsi); |
| 178 | |
| 179 | if (!g_controller_interface.HasDefaultDevice()) |
| 180 | { |
| 181 | // Note that the CI default device could be still temporarily removed at any time |
| 182 | WARN_LOG_FMT(CONTROLLERINTERFACE, "No default device has been added in time. Premade control " |
| 183 | "mappings intended for the default device may not work."); |
| 184 | } |
| 185 | |
| 186 | GCAdapter::Init(); |
| 187 | Pad::Initialize(); |
| 188 | Pad::InitializeGBA(); |
| 189 | Keyboard::Initialize(); |
| 190 | Wiimote::Initialize(Wiimote::InitializeMode::DO_NOT_WAIT_FOR_WIIMOTES); |
| 191 | HotkeyManagerEmu::Initialize(); |
| 192 | FreeLook::Initialize(); |
| 193 | } |
| 194 | |
| 195 | void ShutdownControllers() |
| 196 | { |
| 197 | Pad::Shutdown(); |
| 198 | Pad::ShutdownGBA(); |
| 199 | Keyboard::Shutdown(); |
| 200 | Wiimote::Shutdown(); |
| 201 | HotkeyManagerEmu::Shutdown(); |
| 202 | FreeLook::Shutdown(); |
| 203 | |
| 204 | g_controller_interface.Shutdown(); |
| 205 | } |
| 206 | |
| 207 | void SetLocale(std::string locale_name) |
| 208 | { |
| 209 | auto set_locale = [](const std::string& locale) { |
| 210 | #ifdef __linux__ |
| 211 | std::string adjusted_locale = locale; |
| 212 | if (!locale.empty()) |
| 213 | adjusted_locale += ".UTF-8"; |
| 214 | #else |
| 215 | const std::string& adjusted_locale = locale; |
| 216 | #endif |
| 217 | |
| 218 | // setlocale sets the C locale, and global sets the C and C++ locales, so the call to setlocale |
| 219 | // would be redundant if it wasn't for not having any other good way to check whether |
| 220 | // the locale name is valid. (Constructing a std::locale object for an unsupported |
| 221 | // locale name throws std::runtime_error, and exception handling is disabled in Dolphin.) |
| 222 | if (!std::setlocale(LC_ALL, adjusted_locale.c_str())) |
| 223 | return false; |
| 224 | std::locale::global(std::locale(adjusted_locale)); |
| 225 | return true; |
| 226 | }; |
| 227 | |
| 228 | #ifdef _WIN32 |
| 229 | constexpr char PREFERRED_SEPARATOR = '-'; |
| 230 | constexpr char OTHER_SEPARATOR = '_'; |
| 231 | #else |
| 232 | constexpr char PREFERRED_SEPARATOR = '_'; |
| 233 | constexpr char OTHER_SEPARATOR = '-'; |
| 234 | #endif |
| 235 | |
| 236 | // Users who use a system language other than English are unlikely to prefer American date and |
| 237 | // time formats, so let's explicitly request "en_GB" if Dolphin's language is set to "en". |
| 238 | // (The settings window only allows setting "en", not anything like "en_US" or "en_GB".) |
| 239 | // Users who prefer the American formats are likely to have their system language set to en_US, |
| 240 | // and are thus likely to leave Dolphin's language as the default value "" (<System Language>). |
| 241 | if (locale_name == "en") |
| 242 | locale_name = "en_GB"; |
| 243 | |
| 244 | std::ranges::replace(locale_name, OTHER_SEPARATOR, PREFERRED_SEPARATOR); |
| 245 | |
| 246 | // Use the specified locale if supported. |
| 247 | if (set_locale(locale_name)) |
| 248 | return; |
| 249 | |
| 250 | // Remove subcodes until we get a supported locale. If that doesn't give us a supported locale, |
| 251 | // "" is passed to set_locale in order to get the system default locale. |
| 252 | while (!locale_name.empty()) |
| 253 | { |
| 254 | const size_t separator_index = locale_name.rfind(PREFERRED_SEPARATOR); |
| 255 | locale_name.erase(separator_index == std::string::npos ? 0 : separator_index); |
| 256 | if (set_locale(locale_name)) |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // If none of the locales tried above are supported, we just keep using whatever locale is set |
| 261 | // (which is the classic locale by default). |
| 262 | } |
| 263 | |
| 264 | void CreateDirectories() |
| 265 | { |
| 266 | File::CreateFullPath(File::GetUserPath(D_RESOURCEPACK_IDX)); |
| 267 | File::CreateFullPath(File::GetUserPath(D_USER_IDX)); |
| 268 | File::CreateFullPath(File::GetUserPath(D_CACHE_IDX)); |
| 269 | File::CreateFullPath(File::GetUserPath(D_COVERCACHE_IDX)); |
| 270 | File::CreateFullPath(File::GetUserPath(D_CONFIG_IDX)); |
| 271 | File::CreateFullPath(File::GetUserPath(D_CONFIG_IDX) + GRAPHICSMOD_CONFIG_DIR DIR_SEP); |
| 272 | File::CreateFullPath(File::GetUserPath(D_DUMPDSP_IDX)); |
| 273 | File::CreateFullPath(File::GetUserPath(D_DUMPSSL_IDX)); |
| 274 | File::CreateFullPath(File::GetUserPath(D_DUMPTEXTURES_IDX)); |
| 275 | File::CreateFullPath(File::GetUserPath(D_DUMPDEBUG_IDX)); |
| 276 | File::CreateFullPath(File::GetUserPath(D_DUMPDEBUG_BRANCHWATCH_IDX)); |
| 277 | File::CreateFullPath(File::GetUserPath(D_DUMPDEBUG_JITBLOCKS_IDX)); |
| 278 | File::CreateFullPath(File::GetUserPath(D_GAMESETTINGS_IDX)); |
| 279 | File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX)); |
| 280 | File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + USA_DIR DIR_SEP); |
| 281 | File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + EUR_DIR DIR_SEP); |
| 282 | File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + JAP_DIR DIR_SEP); |
| 283 | File::CreateFullPath(File::GetUserPath(D_TRIUSER_IDX)); |
| 284 | File::CreateFullPath(File::GetUserPath(D_HIRESTEXTURES_IDX)); |
| 285 | File::CreateFullPath(File::GetUserPath(D_GRAPHICSMOD_IDX)); |
| 286 | File::CreateFullPath(File::GetUserPath(D_LOGS_IDX)); |
| 287 | File::CreateFullPath(File::GetUserPath(D_MAPS_IDX)); |
| 288 | File::CreateFullPath(File::GetUserPath(D_SCREENSHOTS_IDX)); |
| 289 | File::CreateFullPath(File::GetUserPath(D_SHADERS_IDX)); |
| 290 | File::CreateFullPath(File::GetUserPath(D_SHADERS_IDX) + ANAGLYPH_DIR DIR_SEP); |
| 291 | File::CreateFullPath(File::GetUserPath(D_RETROACHIEVEMENTSCACHE_IDX)); |
| 292 | File::CreateFullPath(File::GetUserPath(D_STATESAVES_IDX)); |
| 293 | File::CreateFullPath(File::GetUserPath(D_ASM_ROOT_IDX)); |
| 294 | File::CreateFullPath(File::GetUserPath(D_WFSROOT_IDX)); |
| 295 | #ifndef ANDROID |
| 296 | File::CreateFullPath(File::GetUserPath(D_THEMES_IDX)); |
| 297 | File::CreateFullPath(File::GetUserPath(D_STYLES_IDX)); |
| 298 | #else |
| 299 | // Disable media scanning in directories with a .nomedia file |
| 300 | File::CreateEmptyFile(File::GetUserPath(D_COVERCACHE_IDX) + DIR_SEP NOMEDIA_FILE); |
| 301 | #endif |
| 302 | } |
| 303 | |
| 304 | void SetUserDirectory(std::string custom_path) |
| 305 | { |
| 306 | if (!custom_path.empty()) |
| 307 | { |
| 308 | File::CreateFullPath(custom_path + DIR_SEP); |
| 309 | File::SetUserPath(D_USER_IDX, std::move(custom_path)); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | std::string user_path; |
| 314 | #ifdef _WIN32 |
| 315 | // Detect where the User directory is. There are five different cases |
| 316 | // (on top of the command line flag, which overrides all this): |
| 317 | // 1. GetExeDirectory()\portable.txt exists |
| 318 | // -> Use GetExeDirectory()\User |
| 319 | // 2. HKCU\Software\Dolphin Emulator\LocalUserConfig exists and is true |
| 320 | // -> Use GetExeDirectory()\User |
| 321 | // 3. HKCU\Software\Dolphin Emulator\UserConfigPath exists |
| 322 | // -> Use this as the user directory path |
| 323 | // 4. My Documents\Dolphin Emulator exists (default user folder before PR 10708) |
| 324 | // -> Use this as the user directory path |
| 325 | // 5. AppData\Roaming exists |
| 326 | // -> Use AppData\Roaming\Dolphin Emulator as the User directory path |
| 327 | // 6. Default |
| 328 | // -> Use GetExeDirectory()\User |
| 329 | |
| 330 | // Get AppData path in case we need it. |
| 331 | wil::unique_cotaskmem_string appdata; |
| 332 | bool appdata_found = SUCCEEDED( |
| 333 | SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, appdata.put())); |
| 334 | |
| 335 | // Check our registry keys |
| 336 | wil::unique_hkey hkey; |
| 337 | DWORD local = 0; |
| 338 | std::unique_ptr<TCHAR[]> configPath; |
| 339 | if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE, |
| 340 | hkey.put()) == ERROR_SUCCESS) |
| 341 | { |
| 342 | DWORD size = sizeof(local); |
| 343 | if (RegQueryValueEx(hkey.get(), TEXT("LocalUserConfig"), nullptr, nullptr, |
| 344 | reinterpret_cast<LPBYTE>(&local), &size) != ERROR_SUCCESS) |
| 345 | { |
| 346 | local = 0; |
| 347 | } |
| 348 | |
| 349 | size = 0; |
| 350 | RegQueryValueEx(hkey.get(), TEXT("UserConfigPath"), nullptr, nullptr, nullptr, &size); |
| 351 | configPath = std::make_unique<TCHAR[]>(size / sizeof(TCHAR)); |
| 352 | if (RegQueryValueEx(hkey.get(), TEXT("UserConfigPath"), nullptr, nullptr, |
| 353 | reinterpret_cast<LPBYTE>(configPath.get()), &size) != ERROR_SUCCESS) |
| 354 | { |
| 355 | configPath.reset(); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | local = local != 0 || File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt"); |
| 360 | |
| 361 | // Attempt to check if the old User directory exists in Documents. |
| 362 | wil::unique_cotaskmem_string documents; |
| 363 | bool documents_found = SUCCEEDED( |
| 364 | SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, documents.put())); |
| 365 | |
| 366 | std::optional<std::string> old_user_folder; |
| 367 | if (documents_found) |
| 368 | { |
| 369 | old_user_folder = TStrToUTF8(documents.get()) + DIR_SEP NORMAL_USER_DIR DIR_SEP; |
| 370 | } |
| 371 | |
| 372 | if (local) // Case 1-2 |
| 373 | { |
| 374 | user_path = File::GetExeDirectory() + DIR_SEP PORTABLE_USER_DIR DIR_SEP; |
| 375 | } |
| 376 | else if (configPath) // Case 3 |
| 377 | { |
| 378 | user_path = TStrToUTF8(configPath.get()); |
| 379 | } |
| 380 | else if (old_user_folder && File::Exists(old_user_folder.value())) // Case 4 |
| 381 | { |
| 382 | user_path = old_user_folder.value(); |
| 383 | } |
| 384 | else if (appdata_found) // Case 5 |
| 385 | { |
| 386 | user_path = TStrToUTF8(appdata.get()) + DIR_SEP NORMAL_USER_DIR DIR_SEP; |
| 387 | |
| 388 | // Set the UserConfigPath value in the registry for backwards compatibility with older Dolphin |
| 389 | // builds, which will look for the default User directory in Documents. If we set this key, |
| 390 | // they will use this as the User directory instead. |
| 391 | // (If we're in this case, then this key doesn't exist, so it's OK to set it.) |
| 392 | std::wstring wstr_path = UTF8ToWString(user_path); |
| 393 | RegSetKeyValueW(HKEY_CURRENT_USER, TEXT("Software\\Dolphin Emulator"), TEXT("UserConfigPath"), |
| 394 | REG_SZ, wstr_path.c_str(), |
| 395 | static_cast<DWORD>((wstr_path.size() + 1) * sizeof(wchar_t))); |
| 396 | } |
| 397 | else // Case 6 |
| 398 | { |
| 399 | user_path = File::GetExeDirectory() + DIR_SEP PORTABLE_USER_DIR DIR_SEP; |
| 400 | } |
| 401 | #else |
| 402 | if (File::IsDirectory(ROOT_DIR DIR_SEP EMBEDDED_USER_DIR)) |
| 403 | { |
| 404 | user_path = ROOT_DIR DIR_SEP EMBEDDED_USER_DIR DIR_SEP; |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | const char* env_path = getenv("DOLPHIN_EMU_USERPATH"); |
| 409 | const char* home = getenv("HOME"); |
| 410 | if (!home) |
| 411 | home = getenv("PWD"); |
| 412 | if (!home) |
| 413 | home = ""; |
| 414 | std::string home_path = std::string(home) + DIR_SEP; |
| 415 | |
| 416 | // On a non-Apple and non-Android POSIX system, there are 4 cases: |
| 417 | // 1. GetExeDirectory()/portable.txt exists |
| 418 | // -> Use GetExeDirectory()/User |
| 419 | // 2. $DOLPHIN_EMU_USERPATH is set |
| 420 | // -> Use $DOLPHIN_EMU_USERPATH |
| 421 | // 3. ~/.dolphin-emu directory exists, and we're not in flatpak |
| 422 | // -> Use ~/.dolphin-emu |
| 423 | // 4. Default |
| 424 | // -> Use XDG basedir, see |
| 425 | // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
| 426 | // |
| 427 | // On macOS: |
| 428 | // 1. GetExeDirectory()/portable.txt exists |
| 429 | // -> Use GetExeDirectory()/User |
| 430 | // 2. $DOLPHIN_EMU_USERPATH is set |
| 431 | // -> Use $DOLPHIN_EMU_USERPATH |
| 432 | // 3. Default |
| 433 | // |
| 434 | // On Android, custom_path is set, so this code path is never reached. |
| 435 | std::string exe_path = File::GetExeDirectory(); |
| 436 | if (File::Exists(exe_path + DIR_SEP "portable.txt")) |
| 437 | { |
| 438 | user_path = exe_path + DIR_SEP PORTABLE_USER_DIR DIR_SEP; |
| 439 | } |
| 440 | else if (env_path) |
| 441 | { |
| 442 | user_path = env_path; |
| 443 | } |
| 444 | #if defined(__APPLE__) || defined(ANDROID) |
| 445 | else |
| 446 | { |
| 447 | user_path = home_path + NORMAL_USER_DIR DIR_SEP; |
| 448 | } |
| 449 | #else |
| 450 | else |
| 451 | { |
| 452 | user_path = home_path + "." NORMAL_USER_DIR DIR_SEP; |
| 453 | |
| 454 | if (File::Exists("/.flatpak-info") || !File::Exists(user_path)) |
| 455 | { |
| 456 | const char* data_home = getenv("XDG_DATA_HOME"); |
| 457 | std::string data_path = |
| 458 | std::string(data_home && data_home[0] == '/' ? data_home : |
| 459 | (home_path + ".local" DIR_SEP "share")) + |
| 460 | DIR_SEP NORMAL_USER_DIR DIR_SEP; |
| 461 | |
| 462 | const char* config_home = getenv("XDG_CONFIG_HOME"); |
| 463 | std::string config_path = |
| 464 | std::string(config_home && config_home[0] == '/' ? config_home : |
| 465 | (home_path + ".config")) + |
| 466 | DIR_SEP NORMAL_USER_DIR DIR_SEP; |
| 467 | |
| 468 | const char* cache_home = getenv("XDG_CACHE_HOME"); |
| 469 | std::string cache_path = |
| 470 | std::string(cache_home && cache_home[0] == '/' ? cache_home : (home_path + ".cache")) + |
| 471 | DIR_SEP NORMAL_USER_DIR DIR_SEP; |
| 472 | |
| 473 | File::SetUserPath(D_USER_IDX, data_path); |
| 474 | File::SetUserPath(D_CONFIG_IDX, config_path); |
| 475 | File::SetUserPath(D_CACHE_IDX, cache_path); |
| 476 | return; |
| 477 | } |
| 478 | } |
| 479 | #endif |
| 480 | } |
| 481 | #endif |
| 482 | File::SetUserPath(D_USER_IDX, std::move(user_path)); |
| 483 | } |
| 484 | |
| 485 | bool TriggerSTMPowerEvent() |
| 486 | { |
| 487 | const auto ios = Core::System::GetInstance().GetIOS(); |
| 488 | if (!ios) |
| 489 | return false; |
| 490 | |
| 491 | const auto stm = ios->GetDeviceByName("/dev/stm/eventhook"); |
| 492 | if (!stm || !std::static_pointer_cast<IOS::HLE::STMEventHookDevice>(stm)->HasHookInstalled()) |
| 493 | return false; |
| 494 | |
| 495 | Core::DisplayMessage("Shutting down", 30000); |
| 496 | ios->GetSystem().GetProcessorInterface().PowerButton_Tap(); |
| 497 | |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | void InhibitScreenSaver(bool inhibit) |
| 502 | { |
| 503 | // Inhibit the screensaver. Depending on the operating system this may also |
| 504 | // disable low-power states and/or screen dimming. |
| 505 | |
| 506 | #ifdef HAVE_QTDBUS |
| 507 | DBusUtils::InhibitScreenSaver(inhibit); |
| 508 | #endif |
| 509 | |
| 510 | #ifdef _WIN32 |
| 511 | // Prevents Windows from sleeping, turning off the display, or idling |
| 512 | SetThreadExecutionState(ES_CONTINUOUS | |
| 513 | (inhibit ? (ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED) : 0)); |
| 514 | #endif |
| 515 | |
| 516 | #ifdef __APPLE__ |
| 517 | static IOPMAssertionID s_power_assertion = kIOPMNullAssertionID; |
| 518 | if (inhibit) |
| 519 | { |
| 520 | CFStringRef reason_for_activity = CFSTR("Emulation Running"); |
| 521 | if (IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep, |
| 522 | kIOPMAssertionLevelOn, reason_for_activity, |
| 523 | &s_power_assertion) != kIOReturnSuccess) |
| 524 | { |
| 525 | s_power_assertion = kIOPMNullAssertionID; |
| 526 | } |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | if (s_power_assertion != kIOPMNullAssertionID) |
| 531 | { |
| 532 | IOPMAssertionRelease(s_power_assertion); |
| 533 | s_power_assertion = kIOPMNullAssertionID; |
| 534 | } |
| 535 | } |
| 536 | #endif |
| 537 | } |
| 538 | |
| 539 | std::string FormatSize(u64 bytes, int decimals) |
| 540 | { |
| 541 | // i18n: The symbol for the unit "bytes" |
| 542 | const char* const unit_symbols[] = {_trans("B"), _trans("KiB"), _trans("MiB"), _trans("GiB"), |
| 543 | _trans("TiB"), _trans("PiB"), _trans("EiB")}; |
| 544 | |
| 545 | // Find largest power of 2 less than size. |
| 546 | // div 10 to get largest named unit less than size |
| 547 | // 10 == log2(1024) (number of B in a KiB, KiB in a MiB, etc) |
| 548 | // Max value is 63 / 10 = 6 |
| 549 | const int unit = MathUtil::IntLog2(std::max<u64>(bytes, 1)) / 10; |
| 550 | |
| 551 | // Don't need exact values, only 5 most significant digits |
| 552 | const double unit_size = std::pow(2, unit * 10); |
| 553 | return fmt::format("{:.{}Lf} {}", bytes / unit_size, decimals, |
| 554 | Common::GetStringT(unit_symbols[unit])); |
| 555 | } |
| 556 | |
| 557 | } // namespace UICommon |