src/video/winrt/SDL_winrtevents.cpp
changeset 8505 a76a3842c9eb
parent 8494 5d165785918d
child 8515 bc6cf9201dab
     1.1 --- a/src/video/winrt/SDL_winrtevents.cpp	Tue Aug 27 13:03:43 2013 -0400
     1.2 +++ b/src/video/winrt/SDL_winrtevents.cpp	Tue Aug 27 21:21:09 2013 -0400
     1.3 @@ -25,23 +25,117 @@
     1.4  /* SDL includes */
     1.5  #include "SDL_winrtevents_c.h"
     1.6  #include "../../core/winrt/SDL_winrtapp.h"
     1.7 +#include "SDL_assert.h"
     1.8 +#include "SDL_system.h"
     1.9  
    1.10  extern "C" {
    1.11  #include "../SDL_sysvideo.h"
    1.12  #include "../../events/SDL_events_c.h"
    1.13  }
    1.14  
    1.15 +
    1.16 +/* Forward declarations and globals */
    1.17  extern SDL_WinRTApp ^ SDL_WinRTGlobalApp;
    1.18 +extern int (*WINRT_XAMLAppMainFunction)(int, char **);
    1.19 +extern void WINRT_YieldXAMLThread();
    1.20  
    1.21  
    1.22 -/* General event-management function(s) */
    1.23 +/* Global event management */
    1.24  
    1.25  void
    1.26  WINRT_PumpEvents(_THIS)
    1.27  {
    1.28 -    SDL_WinRTGlobalApp->PumpEvents();
    1.29 +    if (SDL_WinRTGlobalApp) {
    1.30 +        SDL_WinRTGlobalApp->PumpEvents();
    1.31 +    } else if (WINRT_XAMLAppMainFunction) {
    1.32 +        WINRT_YieldXAMLThread();
    1.33 +    }
    1.34  }
    1.35  
    1.36 +
    1.37 +/* XAML Thread management */
    1.38 +
    1.39 +enum SDL_XAMLAppThreadState
    1.40 +{
    1.41 +    ThreadState_NotLaunched = 0,
    1.42 +    ThreadState_Running,
    1.43 +    ThreadState_Yielding
    1.44 +};
    1.45 +
    1.46 +static SDL_XAMLAppThreadState _threadState = ThreadState_NotLaunched;
    1.47 +static SDL_Thread * _XAMLThread = nullptr;
    1.48 +static SDL_mutex * _mutex = nullptr;
    1.49 +static SDL_cond * _cond = nullptr;
    1.50 +
    1.51 +static void
    1.52 +WINRT_YieldXAMLThread()
    1.53 +{
    1.54 +    SDL_LockMutex(_mutex);
    1.55 +    SDL_assert(_threadState == ThreadState_Running);
    1.56 +    _threadState = ThreadState_Yielding;
    1.57 +    SDL_UnlockMutex(_mutex);
    1.58 +
    1.59 +    SDL_CondSignal(_cond);
    1.60 +
    1.61 +    SDL_LockMutex(_mutex);
    1.62 +    while (_threadState != ThreadState_Running) {
    1.63 +        SDL_CondWait(_cond, _mutex);
    1.64 +    }
    1.65 +    SDL_UnlockMutex(_mutex);
    1.66 +}
    1.67 +
    1.68 +static int
    1.69 +WINRT_XAMLThreadMain(void * userdata)
    1.70 +{
    1.71 +    return WINRT_XAMLAppMainFunction(0, NULL);
    1.72 +}
    1.73 +
    1.74 +void
    1.75 +WINRT_CycleXAMLThread()
    1.76 +{
    1.77 +    switch (_threadState) {
    1.78 +        case ThreadState_NotLaunched:
    1.79 +        {
    1.80 +            _cond = SDL_CreateCond();
    1.81 +
    1.82 +            _mutex = SDL_CreateMutex();
    1.83 +            _threadState = ThreadState_Running;
    1.84 +            _XAMLThread = SDL_CreateThread(WINRT_XAMLThreadMain, "SDL/XAML App Thread", nullptr);
    1.85 +
    1.86 +            SDL_LockMutex(_mutex);
    1.87 +            while (_threadState != ThreadState_Yielding) {
    1.88 +                SDL_CondWait(_cond, _mutex);
    1.89 +            }
    1.90 +            SDL_UnlockMutex(_mutex);
    1.91 +
    1.92 +            break;
    1.93 +        }
    1.94 +
    1.95 +        case ThreadState_Running:
    1.96 +        {
    1.97 +            SDL_assert(false);
    1.98 +            break;
    1.99 +        }
   1.100 +
   1.101 +        case ThreadState_Yielding:
   1.102 +        {
   1.103 +            SDL_LockMutex(_mutex);
   1.104 +            SDL_assert(_threadState == ThreadState_Yielding);
   1.105 +            _threadState = ThreadState_Running;
   1.106 +            SDL_UnlockMutex(_mutex);
   1.107 +
   1.108 +            SDL_CondSignal(_cond);
   1.109 +
   1.110 +            SDL_LockMutex(_mutex);
   1.111 +            while (_threadState != ThreadState_Yielding) {
   1.112 +                SDL_CondWait(_cond, _mutex);
   1.113 +            }
   1.114 +            SDL_UnlockMutex(_mutex);
   1.115 +        }
   1.116 +    }
   1.117 +}
   1.118 +
   1.119 +
   1.120  #endif /* SDL_VIDEO_DRIVER_WINRT */
   1.121  
   1.122  /* vi: set ts=4 sw=4 expandtab: */