WinRTUtil.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "pch.h"
  2. #include <wrl.h>
  3. #include <string>
  4. #include <map>
  5. #include <list>
  6. #include <DXGI.h>
  7. #include <ppltasks.h>
  8. #include <mfapi.h>
  9. #include "../SDLPal.Common/AsyncHelper.h"
  10. #include "../SDLPal.Common/StringHelper.h"
  11. #include "util.h"
  12. #include "palcfg.h"
  13. #include "global.h"
  14. #include "main.h"
  15. #include "SDL.h"
  16. #include "SDL_endian.h"
  17. static std::string g_configpath;
  18. extern HANDLE g_eventHandle;
  19. extern "C" std::map<std::string, Windows::Storage::StorageFile^>* get_special_files_map();
  20. extern "C" std::map<std::string, Windows::Storage::StorageFolder^>* get_special_folders_map();
  21. extern "C"
  22. LPCSTR UTIL_ConfigPath(VOID)
  23. {
  24. if (g_configpath.empty())
  25. {
  26. auto localfolder = Windows::Storage::ApplicationData::Current->LocalFolder->Path;
  27. if (localfolder->End()[-1] != L'\\') localfolder += "\\";
  28. ConvertString(localfolder, g_configpath);
  29. }
  30. return g_configpath.c_str();
  31. }
  32. extern "C"
  33. LPCSTR UTIL_ScreenShotPath(VOID)
  34. {
  35. return gConfig.pszGamePath;
  36. }
  37. extern "C"
  38. BOOL UTIL_GetScreenSize(DWORD *pdwScreenWidth, DWORD *pdwScreenHeight)
  39. {
  40. DXGI_OUTPUT_DESC desc;
  41. IDXGIFactory1* pFactory = nullptr;
  42. IDXGIAdapter1* pAdapter = nullptr;
  43. IDXGIOutput* pOutput = nullptr;
  44. DWORD retval = FALSE;
  45. #if NTDDI_VERSION >= NTDDI_WIN10
  46. if (Windows::System::Profile::AnalyticsInfo::VersionInfo->DeviceFamily != L"Windows.Mobile") return FALSE;
  47. #endif
  48. if (!pdwScreenWidth || !pdwScreenHeight) return FALSE;
  49. if (FAILED(CreateDXGIFactory1(IID_IDXGIFactory1, (void**)&pFactory))) goto UTIL_WP_GetScreenSize_exit;
  50. if (FAILED(pFactory->EnumAdapters1(0, &pAdapter))) goto UTIL_WP_GetScreenSize_exit;
  51. if (FAILED(pAdapter->EnumOutputs(0, &pOutput))) goto UTIL_WP_GetScreenSize_exit;
  52. if (SUCCEEDED(pOutput->GetDesc(&desc)))
  53. {
  54. #if (NTDDI_VERSION < NTDDI_WIN10) && (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP)
  55. *pdwScreenWidth = (desc.DesktopCoordinates.right - desc.DesktopCoordinates.left);
  56. *pdwScreenHeight = (desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top);
  57. #else
  58. *pdwScreenWidth = (desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top);
  59. *pdwScreenHeight = (desc.DesktopCoordinates.right - desc.DesktopCoordinates.left);
  60. #endif
  61. retval = TRUE;
  62. }
  63. UTIL_WP_GetScreenSize_exit:
  64. if (pOutput) pOutput->Release();
  65. if (pAdapter) pAdapter->Release();
  66. if (pFactory) pFactory->Release();
  67. return retval;
  68. }
  69. extern "C"
  70. BOOL UTIL_IsAbsolutePath(LPCSTR lpszFileName)
  71. {
  72. char szDrive[_MAX_DRIVE], szDir[_MAX_DIR], szFname[_MAX_FNAME], szExt[_MAX_EXT];
  73. if (_splitpath_s(lpszFileName, szDrive, szDir, szFname, szExt) == 0)
  74. return (strlen(szDrive) > 0 && (szDir[0] == '\\' || szDir[0] == '/'));
  75. else
  76. return FALSE;
  77. }
  78. extern "C"
  79. BOOL UTIL_TouchEnabled(VOID)
  80. {
  81. return (ref new Windows::Devices::Input::TouchCapabilities())->TouchPresent;
  82. }
  83. static Windows::Storage::StorageFile^ g_running_file = nullptr;
  84. static void CreateRunningFile()
  85. {
  86. // Create the 'running' file for crash detection.
  87. try { g_running_file = AWait(Windows::Storage::ApplicationData::Current->LocalFolder->CreateFileAsync("running", Windows::Storage::CreationCollisionOption::OpenIfExists), g_eventHandle); }
  88. catch (Platform::Exception^) {}
  89. }
  90. static void DeleteRunningFile()
  91. {
  92. // Delete the 'running' file on normal exit.
  93. try { if (g_running_file) AWait(g_running_file->DeleteAsync()); g_running_file = nullptr; }
  94. catch (Platform::Exception^) {}
  95. }
  96. static int SDLCALL WinRT_EventFilter(void *userdata, SDL_Event * event)
  97. {
  98. switch (event->type)
  99. {
  100. case SDL_APP_DIDENTERFOREGROUND:
  101. CreateRunningFile();
  102. break;
  103. case SDL_APP_DIDENTERBACKGROUND:
  104. case SDL_APP_TERMINATING:
  105. // Enter background or exiting, treat as normal exit
  106. DeleteRunningFile();
  107. break;
  108. }
  109. return 0;
  110. }
  111. static int input_event_filter(const SDL_Event *lpEvent, PALINPUTSTATE *state)
  112. {
  113. if (lpEvent->type == SDL_KEYDOWN &&
  114. lpEvent->key.keysym.sym == SDLK_AC_BACK &&
  115. !gpGlobals->fInMainGame)
  116. {
  117. PAL_Shutdown(0);
  118. }
  119. return 0;
  120. }
  121. extern "C"
  122. INT UTIL_Platform_Init(int argc, char* argv[])
  123. {
  124. // Defaults log to debug output
  125. UTIL_LogAddOutputCallback([](LOGLEVEL, const char* str, const char*)->void {
  126. OutputDebugStringA(str);
  127. }, PAL_DEFAULT_LOGLEVEL);
  128. CreateRunningFile();
  129. SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeRight");
  130. SDL_SetHint(SDL_HINT_WINRT_HANDLE_BACK_BUTTON, "1");
  131. SDL_AddEventWatch(WinRT_EventFilter, nullptr);
  132. std::list<Platform::String^> invalid_tokens;
  133. auto& files = *get_special_files_map();
  134. auto& folders = *get_special_folders_map();
  135. auto fal = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList;
  136. for each (auto entry in fal->Entries)
  137. {
  138. Windows::Storage::IStorageItem^ item = nullptr;
  139. try { item = AWait(fal->GetItemAsync(entry.Token), g_eventHandle); } catch (Exception^) {}
  140. if (!item)
  141. {
  142. invalid_tokens.push_back(entry.Token);
  143. continue;
  144. }
  145. if (dynamic_cast<Windows::Storage::StorageFolder^>(item) != nullptr)
  146. {
  147. auto localfolder = entry.Metadata;
  148. if (localfolder->End()[-1] != L'\\') localfolder += "\\";
  149. auto folder = ConvertString(localfolder);
  150. folders[folder] = dynamic_cast<Windows::Storage::StorageFolder^>(item);
  151. PAL_SetConfigItem(PAL_ConfigIndex(ConvertString(entry.Token).c_str()), ConfigValue{ folder.c_str() });
  152. continue;
  153. }
  154. if (dynamic_cast<Windows::Storage::StorageFile^>(item) != nullptr)
  155. {
  156. auto file = ConvertString(entry.Metadata);
  157. files[file] = dynamic_cast<Windows::Storage::StorageFile^>(item);
  158. PAL_SetConfigItem(PAL_ConfigIndex(ConvertString(entry.Token).c_str()), ConfigValue{ file.c_str() });
  159. continue;
  160. }
  161. }
  162. for (auto i = invalid_tokens.begin(); i != invalid_tokens.end(); fal->Remove(*i++));
  163. MFStartup(MF_VERSION, MFSTARTUP_LITE);
  164. return 0;
  165. }
  166. extern "C"
  167. VOID UTIL_Platform_Quit(VOID)
  168. {
  169. MFShutdown();
  170. DeleteRunningFile();
  171. }