2 Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "SDL_config.h"
23 #if SDL_VIDEO_DRIVER_WINRT
29 using namespace Windows::UI::Core;
30 using Windows::UI::Core::CoreCursor;
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_assert.h"
40 #include "SDL_system.h"
43 #include "../SDL_sysvideo.h"
44 #include "../../events/SDL_events_c.h"
48 /* Forward declarations */
49 static void WINRT_YieldXAMLThread();
52 /* Global event management */
55 WINRT_PumpEvents(_THIS)
57 if (SDL_WinRTGlobalApp) {
58 SDL_WinRTGlobalApp->PumpEvents();
59 } else if (WINRT_XAMLWasEnabled) {
60 WINRT_YieldXAMLThread();
65 /* XAML Thread management */
67 enum SDL_XAMLAppThreadState
69 ThreadState_NotLaunched = 0,
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;
80 WINRT_YieldXAMLThread()
82 SDL_LockMutex(_mutex);
83 SDL_assert(_threadState == ThreadState_Running);
84 _threadState = ThreadState_Yielding;
85 SDL_UnlockMutex(_mutex);
87 SDL_CondSignal(_cond);
89 SDL_LockMutex(_mutex);
90 while (_threadState != ThreadState_Running) {
91 SDL_CondWait(_cond, _mutex);
93 SDL_UnlockMutex(_mutex);
97 WINRT_XAMLThreadMain(void * userdata)
99 // TODO, WinRT: pass the C-style main() a reasonably realistic
100 // representation of command line arguments.
103 return WINRT_SDLAppEntryPoint(argc, argv);
107 WINRT_CycleXAMLThread()
109 switch (_threadState) {
110 case ThreadState_NotLaunched:
112 _cond = SDL_CreateCond();
114 _mutex = SDL_CreateMutex();
115 _threadState = ThreadState_Running;
116 _XAMLThread = SDL_CreateThread(WINRT_XAMLThreadMain, "SDL/XAML App Thread", nullptr);
118 SDL_LockMutex(_mutex);
119 while (_threadState != ThreadState_Yielding) {
120 SDL_CondWait(_cond, _mutex);
122 SDL_UnlockMutex(_mutex);
127 case ThreadState_Running:
133 case ThreadState_Yielding:
135 SDL_LockMutex(_mutex);
136 SDL_assert(_threadState == ThreadState_Yielding);
137 _threadState = ThreadState_Running;
138 SDL_UnlockMutex(_mutex);
140 SDL_CondSignal(_cond);
142 SDL_LockMutex(_mutex);
143 while (_threadState != ThreadState_Yielding) {
144 SDL_CondWait(_cond, _mutex);
146 SDL_UnlockMutex(_mutex);
151 #endif /* SDL_VIDEO_DRIVER_WINRT */
153 /* vi: set ts=4 sw=4 expandtab: */