WinRTUtil.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. extern "C" void TerminateOnError(const char *fmt, ...);
  9. #define PAL_PATH_NAME "SDLPAL"
  10. static std::string g_savepath, g_basepath, g_configpath, g_screenshotpath;
  11. static Windows::Storage::StorageFolder ^g_basefolder, ^g_savefolder, ^g_configfolder, ^g_screenshotfolder;
  12. extern HANDLE g_eventHandle;
  13. extern "C"
  14. LPCSTR UTIL_BasePath(VOID)
  15. {
  16. if (g_basepath.empty())
  17. {
  18. g_basefolder = Windows::Storage::ApplicationData::Current->LocalFolder;
  19. auto localfolder = g_basefolder->Path;
  20. if (localfolder->End()[-1] != L'\\') localfolder += "\\";
  21. ConvertString(localfolder, g_basepath);
  22. }
  23. return g_basepath.c_str();
  24. }
  25. extern "C"
  26. LPCSTR UTIL_SavePath(VOID)
  27. {
  28. if (g_savepath.empty())
  29. {
  30. g_savefolder = Windows::Storage::ApplicationData::Current->LocalFolder;
  31. auto localfolder = g_savefolder->Path;
  32. if (localfolder->End()[-1] != L'\\') localfolder += "\\";
  33. ConvertString(localfolder, g_savepath);
  34. }
  35. return g_savepath.c_str();
  36. }
  37. extern "C"
  38. LPCSTR UTIL_ConfigPath(VOID)
  39. {
  40. if (g_configpath.empty())
  41. {
  42. g_configfolder = Windows::Storage::ApplicationData::Current->LocalFolder;
  43. auto localfolder = g_configfolder->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. if (g_screenshotpath.empty())
  53. {
  54. Windows::Storage::StorageFolder^ folder = nullptr;
  55. try { folder = AWait(Windows::Storage::KnownFolders::PicturesLibrary->GetFolderAsync("SDLPAL"), g_eventHandle); }
  56. catch (Platform::Exception^) {}
  57. if (folder == nullptr)
  58. {
  59. try { folder = AWait(Windows::Storage::KnownFolders::PicturesLibrary->CreateFolderAsync("SDLPAL"), g_eventHandle); }
  60. catch (Platform::Exception^) {}
  61. }
  62. if (folder)
  63. {
  64. g_screenshotfolder = folder;
  65. auto localfolder = g_screenshotfolder->Path;
  66. if (localfolder->End()[-1] != L'\\') localfolder += "\\";
  67. ConvertString(localfolder, g_screenshotpath);
  68. }
  69. else
  70. {
  71. g_screenshotpath = UTIL_SavePath();
  72. g_screenshotfolder = g_savefolder;
  73. }
  74. }
  75. return g_screenshotpath.c_str();
  76. }
  77. static BOOL UTIL_IsMobile(VOID)
  78. {
  79. auto rc = Windows::ApplicationModel::Resources::Core::ResourceContext::GetForCurrentView();
  80. auto qv = rc->QualifierValues;
  81. return qv->HasKey("DeviceFamily") && qv->Lookup("DeviceFamily") == "Mobile";
  82. }
  83. extern "C"
  84. BOOL UTIL_GetScreenSize(DWORD *pdwScreenWidth, DWORD *pdwScreenHeight)
  85. {
  86. DXGI_OUTPUT_DESC desc;
  87. IDXGIFactory1* pFactory = nullptr;
  88. IDXGIAdapter1* pAdapter = nullptr;
  89. IDXGIOutput* pOutput = nullptr;
  90. DWORD retval = FALSE;
  91. #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  92. if (!UTIL_IsMobile()) return FALSE;
  93. #endif
  94. if (!pdwScreenWidth || !pdwScreenHeight) return FALSE;
  95. if (FAILED(CreateDXGIFactory1(IID_IDXGIFactory1, (void**)&pFactory))) goto UTIL_WP_GetScreenSize_exit;
  96. if (FAILED(pFactory->EnumAdapters1(0, &pAdapter))) goto UTIL_WP_GetScreenSize_exit;
  97. if (FAILED(pAdapter->EnumOutputs(0, &pOutput))) goto UTIL_WP_GetScreenSize_exit;
  98. if (SUCCEEDED(pOutput->GetDesc(&desc)))
  99. {
  100. *pdwScreenWidth = (desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top);
  101. *pdwScreenHeight = (desc.DesktopCoordinates.right - desc.DesktopCoordinates.left);
  102. retval = TRUE;
  103. }
  104. UTIL_WP_GetScreenSize_exit:
  105. if (pOutput) pOutput->Release();
  106. if (pAdapter) pAdapter->Release();
  107. if (pFactory) pFactory->Release();
  108. return retval;
  109. }
  110. extern "C"
  111. BOOL UTIL_TouchEnabled(VOID)
  112. {
  113. return (ref new Windows::Devices::Input::TouchCapabilities())->TouchPresent;
  114. }
  115. #include "SDL.h"
  116. #include "SDL_endian.h"
  117. # include <setjmp.h>
  118. static jmp_buf g_exit_jmp_env;
  119. # define LONGJMP_EXIT_CODE 0xff
  120. extern "C"
  121. INT UTIL_Platform_Init(int argc, char* argv[])
  122. {
  123. //
  124. // In windows phone, calling exit(0) directly will cause an abnormal exit.
  125. // By using setjmp/longjmp to avoid this.
  126. //
  127. if (setjmp(g_exit_jmp_env) == LONGJMP_EXIT_CODE) return -1;
  128. // We should first check the SD card before running actual codes
  129. UTIL_BasePath();
  130. SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight");
  131. SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
  132. return 0;
  133. }
  134. extern "C"
  135. VOID UTIL_Platform_Quit(VOID)
  136. {
  137. longjmp(g_exit_jmp_env, LONGJMP_EXIT_CODE);
  138. }