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