SDL_winrtevents.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 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. #if 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. #include "SDL_assert.h"
  34. #include "SDL_system.h"
  35. extern "C" {
  36. #include "../../thread/SDL_systhread.h"
  37. #include "../SDL_sysvideo.h"
  38. #include "../../events/SDL_events_c.h"
  39. }
  40. /* Forward declarations */
  41. static void WINRT_YieldXAMLThread();
  42. /* Global event management */
  43. void
  44. WINRT_PumpEvents(_THIS)
  45. {
  46. if (SDL_WinRTGlobalApp) {
  47. SDL_WinRTGlobalApp->PumpEvents();
  48. } else if (WINRT_XAMLWasEnabled) {
  49. WINRT_YieldXAMLThread();
  50. }
  51. }
  52. /* XAML Thread management */
  53. enum SDL_XAMLAppThreadState
  54. {
  55. ThreadState_NotLaunched = 0,
  56. ThreadState_Running,
  57. ThreadState_Yielding
  58. };
  59. static SDL_XAMLAppThreadState _threadState = ThreadState_NotLaunched;
  60. static SDL_Thread * _XAMLThread = nullptr;
  61. static SDL_mutex * _mutex = nullptr;
  62. static SDL_cond * _cond = nullptr;
  63. static void
  64. WINRT_YieldXAMLThread()
  65. {
  66. SDL_LockMutex(_mutex);
  67. SDL_assert(_threadState == ThreadState_Running);
  68. _threadState = ThreadState_Yielding;
  69. SDL_UnlockMutex(_mutex);
  70. SDL_CondSignal(_cond);
  71. SDL_LockMutex(_mutex);
  72. while (_threadState != ThreadState_Running) {
  73. SDL_CondWait(_cond, _mutex);
  74. }
  75. SDL_UnlockMutex(_mutex);
  76. }
  77. static int
  78. WINRT_XAMLThreadMain(void * userdata)
  79. {
  80. // TODO, WinRT: pass the C-style main() a reasonably realistic
  81. // representation of command line arguments.
  82. int argc = 0;
  83. char **argv = NULL;
  84. return WINRT_SDLAppEntryPoint(argc, argv);
  85. }
  86. void
  87. WINRT_CycleXAMLThread(void)
  88. {
  89. switch (_threadState) {
  90. case ThreadState_NotLaunched:
  91. {
  92. _cond = SDL_CreateCond();
  93. _mutex = SDL_CreateMutex();
  94. _threadState = ThreadState_Running;
  95. _XAMLThread = SDL_CreateThreadInternal(WINRT_XAMLThreadMain, "SDL/XAML App Thread", 0, nullptr);
  96. SDL_LockMutex(_mutex);
  97. while (_threadState != ThreadState_Yielding) {
  98. SDL_CondWait(_cond, _mutex);
  99. }
  100. SDL_UnlockMutex(_mutex);
  101. break;
  102. }
  103. case ThreadState_Running:
  104. {
  105. SDL_assert(false);
  106. break;
  107. }
  108. case ThreadState_Yielding:
  109. {
  110. SDL_LockMutex(_mutex);
  111. SDL_assert(_threadState == ThreadState_Yielding);
  112. _threadState = ThreadState_Running;
  113. SDL_UnlockMutex(_mutex);
  114. SDL_CondSignal(_cond);
  115. SDL_LockMutex(_mutex);
  116. while (_threadState != ThreadState_Yielding) {
  117. SDL_CondWait(_cond, _mutex);
  118. }
  119. SDL_UnlockMutex(_mutex);
  120. }
  121. }
  122. }
  123. #endif /* SDL_VIDEO_DRIVER_WINRT */
  124. /* vi: set ts=4 sw=4 expandtab: */