SDL_winrtevents.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_VIDEO_DRIVER_WINRT
  20. /*
  21. * Windows includes:
  22. */
  23. #include <Windows.h>
  24. using namespace Windows::UI::Core;
  25. using Windows::UI::Core::CoreCursor;
  26. /*
  27. * SDL includes:
  28. */
  29. #include "SDL_winrtevents_c.h"
  30. #include "../../core/winrt/SDL_winrtapp_common.h"
  31. #include "../../core/winrt/SDL_winrtapp_direct3d.h"
  32. #include "../../core/winrt/SDL_winrtapp_xaml.h"
  33. extern "C" {
  34. #include "../../thread/SDL_systhread.h"
  35. #include "../SDL_sysvideo.h"
  36. #include "../../events/SDL_events_c.h"
  37. }
  38. /* Forward declarations */
  39. static void WINRT_YieldXAMLThread();
  40. /* Global event management */
  41. void WINRT_PumpEvents(_THIS)
  42. {
  43. if (SDL_WinRTGlobalApp) {
  44. SDL_WinRTGlobalApp->PumpEvents();
  45. } else if (WINRT_XAMLWasEnabled) {
  46. WINRT_YieldXAMLThread();
  47. }
  48. }
  49. /* XAML Thread management */
  50. enum SDL_XAMLAppThreadState
  51. {
  52. ThreadState_NotLaunched = 0,
  53. ThreadState_Running,
  54. ThreadState_Yielding
  55. };
  56. static SDL_XAMLAppThreadState _threadState = ThreadState_NotLaunched;
  57. static SDL_Thread *_XAMLThread = nullptr;
  58. static SDL_Mutex *_mutex = nullptr;
  59. static SDL_Condition *_cond = nullptr;
  60. static void WINRT_YieldXAMLThread()
  61. {
  62. SDL_LockMutex(_mutex);
  63. SDL_assert(_threadState == ThreadState_Running);
  64. _threadState = ThreadState_Yielding;
  65. SDL_UnlockMutex(_mutex);
  66. SDL_SignalCondition(_cond);
  67. SDL_LockMutex(_mutex);
  68. while (_threadState != ThreadState_Running) {
  69. SDL_WaitCondition(_cond, _mutex);
  70. }
  71. SDL_UnlockMutex(_mutex);
  72. }
  73. static int WINRT_XAMLThreadMain(void *userdata)
  74. {
  75. // TODO, WinRT: pass the C-style main() a reasonably realistic
  76. // representation of command line arguments.
  77. int argc = 0;
  78. char **argv = NULL;
  79. return WINRT_SDLAppEntryPoint(argc, argv);
  80. }
  81. void WINRT_CycleXAMLThread(void)
  82. {
  83. switch (_threadState) {
  84. case ThreadState_NotLaunched:
  85. {
  86. _cond = SDL_CreateCondition();
  87. _mutex = SDL_CreateMutex();
  88. _threadState = ThreadState_Running;
  89. _XAMLThread = SDL_CreateThreadInternal(WINRT_XAMLThreadMain, "SDL/XAML App Thread", 0, nullptr);
  90. SDL_LockMutex(_mutex);
  91. while (_threadState != ThreadState_Yielding) {
  92. SDL_WaitCondition(_cond, _mutex);
  93. }
  94. SDL_UnlockMutex(_mutex);
  95. break;
  96. }
  97. case ThreadState_Running:
  98. {
  99. SDL_assert(false);
  100. break;
  101. }
  102. case ThreadState_Yielding:
  103. {
  104. SDL_LockMutex(_mutex);
  105. SDL_assert(_threadState == ThreadState_Yielding);
  106. _threadState = ThreadState_Running;
  107. SDL_UnlockMutex(_mutex);
  108. SDL_SignalCondition(_cond);
  109. SDL_LockMutex(_mutex);
  110. while (_threadState != ThreadState_Yielding) {
  111. SDL_WaitCondition(_cond, _mutex);
  112. }
  113. SDL_UnlockMutex(_mutex);
  114. }
  115. }
  116. }
  117. #endif /* SDL_VIDEO_DRIVER_WINRT */