WinRTUtil.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "pch.h"
  2. #include <wrl.h>
  3. #include <string>
  4. #include <DXGI.h>
  5. #include <ppltasks.h>
  6. #include "../SDLPal.Common/AsyncHelper.h"
  7. #include "../SDLPal.Common/StringHelper.h"
  8. #include "../../global.h"
  9. #include "../../palcfg.h"
  10. #include "SDL.h"
  11. #include "SDL_endian.h"
  12. static std::string g_basepath, g_configpath;
  13. extern HANDLE g_eventHandle;
  14. extern "C"
  15. LPCSTR UTIL_BasePath(VOID)
  16. {
  17. if (g_basepath.empty())
  18. {
  19. auto mru_list = Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList;
  20. for each (auto entry in mru_list->Entries)
  21. {
  22. if (dynamic_cast<Windows::Storage::StorageFolder^>(AWait(mru_list->GetItemAsync(entry.Token), g_eventHandle)) != nullptr)
  23. {
  24. auto localfolder = entry.Metadata;
  25. if (localfolder->End()[-1] != L'\\') localfolder += "\\";
  26. ConvertString(localfolder, g_basepath);
  27. break;
  28. }
  29. }
  30. }
  31. return g_basepath.c_str();
  32. }
  33. extern "C"
  34. LPCSTR UTIL_SavePath(VOID)
  35. {
  36. return UTIL_BasePath();
  37. }
  38. extern "C"
  39. LPCSTR UTIL_ConfigPath(VOID)
  40. {
  41. if (g_configpath.empty())
  42. {
  43. auto localfolder = Windows::Storage::ApplicationData::Current->LocalFolder->Path;
  44. if (localfolder->End()[-1] != L'\\') localfolder += "\\";
  45. ConvertString(localfolder, g_configpath);
  46. }
  47. return g_configpath.c_str();
  48. }
  49. extern "C"
  50. LPCSTR UTIL_ScreenShotPath(VOID)
  51. {
  52. return gConfig.pszGamePath;
  53. }
  54. extern "C"
  55. BOOL UTIL_GetScreenSize(DWORD *pdwScreenWidth, DWORD *pdwScreenHeight)
  56. {
  57. DXGI_OUTPUT_DESC desc;
  58. IDXGIFactory1* pFactory = nullptr;
  59. IDXGIAdapter1* pAdapter = nullptr;
  60. IDXGIOutput* pOutput = nullptr;
  61. DWORD retval = FALSE;
  62. #if NTDDI_VERSION >= NTDDI_WIN10
  63. if (Windows::System::Profile::AnalyticsInfo::VersionInfo->DeviceFamily != L"Windows.Mobile") return FALSE;
  64. #endif
  65. if (!pdwScreenWidth || !pdwScreenHeight) return FALSE;
  66. if (FAILED(CreateDXGIFactory1(IID_IDXGIFactory1, (void**)&pFactory))) goto UTIL_WP_GetScreenSize_exit;
  67. if (FAILED(pFactory->EnumAdapters1(0, &pAdapter))) goto UTIL_WP_GetScreenSize_exit;
  68. if (FAILED(pAdapter->EnumOutputs(0, &pOutput))) goto UTIL_WP_GetScreenSize_exit;
  69. if (SUCCEEDED(pOutput->GetDesc(&desc)))
  70. {
  71. #if (NTDDI_VERSION < NTDDI_WIN10) && (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP)
  72. *pdwScreenWidth = (desc.DesktopCoordinates.right - desc.DesktopCoordinates.left);
  73. *pdwScreenHeight = (desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top);
  74. #else
  75. *pdwScreenWidth = (desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top);
  76. *pdwScreenHeight = (desc.DesktopCoordinates.right - desc.DesktopCoordinates.left);
  77. #endif
  78. retval = TRUE;
  79. }
  80. UTIL_WP_GetScreenSize_exit:
  81. if (pOutput) pOutput->Release();
  82. if (pAdapter) pAdapter->Release();
  83. if (pFactory) pFactory->Release();
  84. return retval;
  85. }
  86. extern "C"
  87. BOOL UTIL_IsAbsolutePath(LPCSTR lpszFileName)
  88. {
  89. char szDrive[_MAX_DRIVE], szDir[_MAX_DIR], szFname[_MAX_FNAME], szExt[_MAX_EXT];
  90. if (_splitpath_s(lpszFileName, szDrive, szDir, szFname, szExt) == 0)
  91. return (strlen(szDrive) > 0 && (szDir[0] == '\\' || szDir[0] == '/'));
  92. else
  93. return FALSE;
  94. }
  95. extern "C"
  96. BOOL UTIL_TouchEnabled(VOID)
  97. {
  98. return (ref new Windows::Devices::Input::TouchCapabilities())->TouchPresent;
  99. }
  100. static Windows::Storage::StorageFile^ g_running_file = nullptr;
  101. static void CreateRunningFile()
  102. {
  103. // Create the 'running' file for crash detection.
  104. try { g_running_file = AWait(Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync("running", Windows::Storage::CreationCollisionOption::OpenIfExists), g_eventHandle); }
  105. catch (Platform::Exception^) {}
  106. }
  107. static void DeleteRunningFile()
  108. {
  109. // Delete the 'running' file on normal exit.
  110. try { if (g_running_file) AWait(g_running_file->DeleteAsync()); g_running_file = nullptr; }
  111. catch (Platform::Exception^) {}
  112. }
  113. static int SDLCALL WinRT_EventFilter(void *userdata, SDL_Event * event)
  114. {
  115. switch (event->type)
  116. {
  117. case SDL_APP_DIDENTERFOREGROUND:
  118. CreateRunningFile();
  119. break;
  120. case SDL_APP_DIDENTERBACKGROUND:
  121. case SDL_APP_TERMINATING:
  122. // Enter background or exiting, treat as normal exit
  123. DeleteRunningFile();
  124. break;
  125. }
  126. return 0;
  127. }
  128. extern "C"
  129. INT UTIL_Platform_Init(int argc, char* argv[])
  130. {
  131. CreateRunningFile();
  132. SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight");
  133. SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
  134. SDL_AddEventWatch(WinRT_EventFilter, nullptr);
  135. return 0;
  136. }
  137. extern "C"
  138. VOID UTIL_Platform_Quit(VOID)
  139. {
  140. DeleteRunningFile();
  141. }