src/video/winrt/SDL_winrtevents.cpp
author David Ludwig
Tue, 27 Aug 2013 21:21:09 -0400
changeset 8505 a76a3842c9eb
parent 8494 5d165785918d
child 8515 bc6cf9201dab
permissions -rw-r--r--
WinRT: experimental and preliminary support for XAML-based overlays on Windows 8/RT

The XAML support here is still rudimentary. Bugs do exist. You've been warned.

XAML support in Windows Phone 8 is not yet available (in SDL/WinRT).
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_WINRT
    24 
    25 /* SDL includes */
    26 #include "SDL_winrtevents_c.h"
    27 #include "../../core/winrt/SDL_winrtapp.h"
    28 #include "SDL_assert.h"
    29 #include "SDL_system.h"
    30 
    31 extern "C" {
    32 #include "../SDL_sysvideo.h"
    33 #include "../../events/SDL_events_c.h"
    34 }
    35 
    36 
    37 /* Forward declarations and globals */
    38 extern SDL_WinRTApp ^ SDL_WinRTGlobalApp;
    39 extern int (*WINRT_XAMLAppMainFunction)(int, char **);
    40 extern void WINRT_YieldXAMLThread();
    41 
    42 
    43 /* Global event management */
    44 
    45 void
    46 WINRT_PumpEvents(_THIS)
    47 {
    48     if (SDL_WinRTGlobalApp) {
    49         SDL_WinRTGlobalApp->PumpEvents();
    50     } else if (WINRT_XAMLAppMainFunction) {
    51         WINRT_YieldXAMLThread();
    52     }
    53 }
    54 
    55 
    56 /* XAML Thread management */
    57 
    58 enum SDL_XAMLAppThreadState
    59 {
    60     ThreadState_NotLaunched = 0,
    61     ThreadState_Running,
    62     ThreadState_Yielding
    63 };
    64 
    65 static SDL_XAMLAppThreadState _threadState = ThreadState_NotLaunched;
    66 static SDL_Thread * _XAMLThread = nullptr;
    67 static SDL_mutex * _mutex = nullptr;
    68 static SDL_cond * _cond = nullptr;
    69 
    70 static void
    71 WINRT_YieldXAMLThread()
    72 {
    73     SDL_LockMutex(_mutex);
    74     SDL_assert(_threadState == ThreadState_Running);
    75     _threadState = ThreadState_Yielding;
    76     SDL_UnlockMutex(_mutex);
    77 
    78     SDL_CondSignal(_cond);
    79 
    80     SDL_LockMutex(_mutex);
    81     while (_threadState != ThreadState_Running) {
    82         SDL_CondWait(_cond, _mutex);
    83     }
    84     SDL_UnlockMutex(_mutex);
    85 }
    86 
    87 static int
    88 WINRT_XAMLThreadMain(void * userdata)
    89 {
    90     return WINRT_XAMLAppMainFunction(0, NULL);
    91 }
    92 
    93 void
    94 WINRT_CycleXAMLThread()
    95 {
    96     switch (_threadState) {
    97         case ThreadState_NotLaunched:
    98         {
    99             _cond = SDL_CreateCond();
   100 
   101             _mutex = SDL_CreateMutex();
   102             _threadState = ThreadState_Running;
   103             _XAMLThread = SDL_CreateThread(WINRT_XAMLThreadMain, "SDL/XAML App Thread", nullptr);
   104 
   105             SDL_LockMutex(_mutex);
   106             while (_threadState != ThreadState_Yielding) {
   107                 SDL_CondWait(_cond, _mutex);
   108             }
   109             SDL_UnlockMutex(_mutex);
   110 
   111             break;
   112         }
   113 
   114         case ThreadState_Running:
   115         {
   116             SDL_assert(false);
   117             break;
   118         }
   119 
   120         case ThreadState_Yielding:
   121         {
   122             SDL_LockMutex(_mutex);
   123             SDL_assert(_threadState == ThreadState_Yielding);
   124             _threadState = ThreadState_Running;
   125             SDL_UnlockMutex(_mutex);
   126 
   127             SDL_CondSignal(_cond);
   128 
   129             SDL_LockMutex(_mutex);
   130             while (_threadState != ThreadState_Yielding) {
   131                 SDL_CondWait(_cond, _mutex);
   132             }
   133             SDL_UnlockMutex(_mutex);
   134         }
   135     }
   136 }
   137 
   138 
   139 #endif /* SDL_VIDEO_DRIVER_WINRT */
   140 
   141 /* vi: set ts=4 sw=4 expandtab: */