WinRTUtil.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include <wrl.h>
  2. #include <string>
  3. #include <DXGI.h>
  4. #include <ppltasks.h>
  5. #include "../SDLPal.Common/AsyncHelper.h"
  6. extern "C" void TerminateOnError(const char *fmt, ...);
  7. #define PAL_PATH_NAME "SDLPAL"
  8. static std::string g_savepath, g_basepath;
  9. static Windows::Storage::StorageFolder ^g_basefolder, ^g_savefolder;
  10. static void ConvertString(Platform::String^ src, std::string& dst)
  11. {
  12. int len = WideCharToMultiByte(CP_ACP, 0, src->Begin(), -1, nullptr, 0, nullptr, nullptr);
  13. dst.resize(len - 1);
  14. WideCharToMultiByte(CP_ACP, 0, src->Begin(), -1, (char*)dst.data(), len, nullptr, nullptr);
  15. }
  16. static std::string ConvertString(Platform::String^ src)
  17. {
  18. int len = WideCharToMultiByte(CP_ACP, 0, src->Begin(), -1, nullptr, 0, nullptr, nullptr);
  19. std::string dst(len - 1, ' ');
  20. WideCharToMultiByte(CP_ACP, 0, src->Begin(), -1, (char*)dst.data(), len, nullptr, nullptr);
  21. return dst;
  22. }
  23. static void ConvertString(const std::string& src, std::wstring& dst)
  24. {
  25. int len = MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, nullptr, 0);
  26. dst.resize(len - 1);
  27. MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, (wchar_t*)dst.data(), len);
  28. }
  29. static Platform::String^ ConvertString(const std::string& src)
  30. {
  31. int len = MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, nullptr, 0);
  32. auto wc = new wchar_t[len];
  33. MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, wc, len);
  34. auto dst = ref new Platform::String(wc);
  35. delete[] wc;
  36. return dst;
  37. }
  38. static Windows::Storage::StorageFolder^ CheckGamePath(Windows::Storage::StorageFolder^ root, HANDLE eventHandle)
  39. {
  40. Platform::String^ required_files[] = {
  41. L"ABC.MKF", L"BALL.MKF", L"DATA.MKF", L"F.MKF", L"FBP.MKF",
  42. L"FIRE.MKF", L"GOP.MKF", L"MAP.MKF", L"MGO.MKF", L"PAT.MKF",
  43. L"RGM.MKF", L"RNG.MKF", L"SSS.MKF"
  44. };
  45. Platform::String^ optional_required_files[] = {
  46. L"VOC.MKF", L"SOUNDS.MKF"
  47. };
  48. /* The words.dat & m.msg may be configurable in the future, so not check here */
  49. try
  50. {
  51. /* Try to get the path */
  52. auto folder = AWait(root->GetFolderAsync(PAL_PATH_NAME), eventHandle);
  53. /* Check the access right of necessary files */
  54. for (int i = 0; i < 13; i++)
  55. {
  56. if (!AWait(AWait(folder->GetFileAsync(required_files[i]), eventHandle)->OpenReadAsync(), eventHandle)->CanRead)
  57. return nullptr;
  58. }
  59. for (int i = 0; i < 2; i++)
  60. {
  61. try {
  62. if (AWait(AWait(folder->GetFileAsync(optional_required_files[i]), eventHandle)->OpenReadAsync(), eventHandle)->CanRead)
  63. return folder;
  64. }
  65. catch (Platform::Exception^) {}
  66. }
  67. }
  68. catch (Platform::Exception^)
  69. { /* Accessing SD card failed, or required file is missing, or access is denied */
  70. }
  71. return nullptr;
  72. }
  73. extern "C"
  74. LPCSTR UTIL_BasePath(VOID)
  75. {
  76. if (g_basepath.empty())
  77. {
  78. Windows::Storage::StorageFolder^ folder = nullptr;
  79. HANDLE eventHandle = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
  80. auto folders = AWait(Windows::Storage::KnownFolders::RemovableDevices->GetFoldersAsync())->First();
  81. while (folders->HasCurrent)
  82. {
  83. if (folder = CheckGamePath(folders->Current, eventHandle))
  84. break;
  85. else
  86. folders->MoveNext();
  87. }
  88. if (!folder)
  89. {
  90. Windows::Storage::StorageFolder^ search_folders[] = {
  91. Windows::Storage::KnownFolders::PicturesLibrary,
  92. Windows::Storage::KnownFolders::SavedPictures,
  93. //Windows::Storage::KnownFolders::MusicLibrary,
  94. //Windows::Storage::KnownFolders::VideosLibrary,
  95. };
  96. for (int i = 0; i < sizeof(search_folders) / sizeof(search_folders[0]); i++)
  97. {
  98. if (folder = CheckGamePath(search_folders[i], eventHandle))
  99. break;
  100. }
  101. }
  102. CloseHandle(eventHandle);
  103. if (folder)
  104. {
  105. /* Folder examination succeeded */
  106. auto path = folder->Path;
  107. if (path->End()[-1] != L'\\') path += "\\";
  108. ConvertString(path, g_basepath);
  109. g_basefolder = folder;
  110. /* Check whether the folder is writable */
  111. FILE* fp = fopen(ConvertString(path + "sdlpal.rpg").c_str(), "wb");
  112. if (fp)
  113. {
  114. g_savepath = g_basepath;
  115. g_savefolder = g_basefolder;
  116. fclose(fp);
  117. }
  118. }
  119. else
  120. {
  121. TerminateOnError("Could not find PAL folder.\n");
  122. //ConvertString(Windows::ApplicationModel::Package::Current->InstalledLocation->Path + "\\Assets\\Data\\", g_basepath);
  123. }
  124. }
  125. return g_basepath.c_str();
  126. }
  127. extern "C"
  128. LPCSTR UTIL_SavePath(VOID)
  129. {
  130. if (g_savepath.empty())
  131. {
  132. auto localfolder = Windows::Storage::ApplicationData::Current->LocalFolder->Path;
  133. if (localfolder->End()[-1] != L'\\') localfolder += "\\";
  134. ConvertString(localfolder, g_savepath);
  135. g_savefolder = Windows::Storage::ApplicationData::Current->LocalFolder;
  136. }
  137. return g_savepath.c_str();
  138. }
  139. static BOOL UTIL_IsMobile(VOID)
  140. {
  141. auto rc = Windows::ApplicationModel::Resources::Core::ResourceContext::GetForCurrentView();
  142. auto qv = rc->QualifierValues;
  143. return qv->HasKey("DeviceFamily") && qv->Lookup("DeviceFamily") == "Mobile";
  144. }
  145. extern "C"
  146. BOOL UTIL_GetScreenSize(DWORD *pdwScreenWidth, DWORD *pdwScreenHeight)
  147. {
  148. DXGI_OUTPUT_DESC desc;
  149. IDXGIFactory1* pFactory = nullptr;
  150. IDXGIAdapter1* pAdapter = nullptr;
  151. IDXGIOutput* pOutput = nullptr;
  152. DWORD retval = FALSE;
  153. if (!UTIL_IsMobile()) return FALSE;
  154. if (!pdwScreenWidth || !pdwScreenHeight) return FALSE;
  155. if (FAILED(CreateDXGIFactory1(IID_IDXGIFactory1, (void**)&pFactory))) goto UTIL_WP_GetScreenSize_exit;
  156. if (FAILED(pFactory->EnumAdapters1(0, &pAdapter))) goto UTIL_WP_GetScreenSize_exit;
  157. if (FAILED(pAdapter->EnumOutputs(0, &pOutput))) goto UTIL_WP_GetScreenSize_exit;
  158. if (SUCCEEDED(pOutput->GetDesc(&desc)))
  159. {
  160. *pdwScreenWidth = (desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top);
  161. *pdwScreenHeight = (desc.DesktopCoordinates.right - desc.DesktopCoordinates.left);
  162. retval = TRUE;
  163. }
  164. UTIL_WP_GetScreenSize_exit:
  165. if (pOutput) pOutput->Release();
  166. if (pAdapter) pAdapter->Release();
  167. if (pFactory) pFactory->Release();
  168. return retval;
  169. }
  170. extern "C"
  171. BOOL UTIL_TouchEnabled(VOID)
  172. {
  173. return (ref new Windows::Devices::Input::TouchCapabilities())->TouchPresent;
  174. }
  175. #include "SDL.h"
  176. #include "SDL_endian.h"
  177. # include <setjmp.h>
  178. static jmp_buf g_exit_jmp_env;
  179. # define LONGJMP_EXIT_CODE 0xff
  180. extern "C"
  181. INT UTIL_Platform_Init(int argc, char* argv[])
  182. {
  183. //
  184. // In windows phone, calling exit(0) directly will cause an abnormal exit.
  185. // By using setjmp/longjmp to avoid this.
  186. //
  187. if (setjmp(g_exit_jmp_env) == LONGJMP_EXIT_CODE) return -1;
  188. // We should first check the SD card before running actual codes
  189. UTIL_BasePath();
  190. SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight");
  191. SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
  192. return 0;
  193. }
  194. extern "C"
  195. VOID UTIL_Platform_Quit(VOID)
  196. {
  197. longjmp(g_exit_jmp_env, LONGJMP_EXIT_CODE);
  198. }
  199. int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  200. {
  201. if (FAILED(Windows::Foundation::Initialize(RO_INIT_MULTITHREADED))) {
  202. return 1;
  203. }
  204. SDL_WinRTRunApp(SDL_main, NULL);
  205. return 0;
  206. }