WinRTUtil.cpp 5.5 KB

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