src/video/winrt/SDL_winrtevents.cpp
author David Ludwig
Mon, 02 Sep 2013 15:23:33 -0400
changeset 8515 bc6cf9201dab
parent 8505 a76a3842c9eb
child 8522 3ae9dc9c439f
permissions -rw-r--r--
WinRT: misc code cleanups regarding touch and mouse events, and also SDL-internal globals
     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 /*
    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.h"
    37 #include "../../core/winrt/SDL_winrtxaml_cpp.h"
    38 #include "SDL_assert.h"
    39 #include "SDL_system.h"
    40 
    41 extern "C" {
    42 #include "../SDL_sysvideo.h"
    43 #include "../../events/SDL_events_c.h"
    44 }
    45 
    46 
    47 /* Forward declarations */
    48 static void WINRT_YieldXAMLThread();
    49 
    50 
    51 /* Global event management */
    52 
    53 void
    54 WINRT_PumpEvents(_THIS)
    55 {
    56     if (SDL_WinRTGlobalApp) {
    57         SDL_WinRTGlobalApp->PumpEvents();
    58     } else if (WINRT_XAMLAppMainFunction) {
    59         WINRT_YieldXAMLThread();
    60     }
    61 }
    62 
    63 
    64 /* XAML Thread management */
    65 
    66 enum SDL_XAMLAppThreadState
    67 {
    68     ThreadState_NotLaunched = 0,
    69     ThreadState_Running,
    70     ThreadState_Yielding
    71 };
    72 
    73 static SDL_XAMLAppThreadState _threadState = ThreadState_NotLaunched;
    74 static SDL_Thread * _XAMLThread = nullptr;
    75 static SDL_mutex * _mutex = nullptr;
    76 static SDL_cond * _cond = nullptr;
    77 
    78 static void
    79 WINRT_YieldXAMLThread()
    80 {
    81     SDL_LockMutex(_mutex);
    82     SDL_assert(_threadState == ThreadState_Running);
    83     _threadState = ThreadState_Yielding;
    84     SDL_UnlockMutex(_mutex);
    85 
    86     SDL_CondSignal(_cond);
    87 
    88     SDL_LockMutex(_mutex);
    89     while (_threadState != ThreadState_Running) {
    90         SDL_CondWait(_cond, _mutex);
    91     }
    92     SDL_UnlockMutex(_mutex);
    93 }
    94 
    95 static int
    96 WINRT_XAMLThreadMain(void * userdata)
    97 {
    98     return WINRT_XAMLAppMainFunction(0, NULL);
    99 }
   100 
   101 void
   102 WINRT_CycleXAMLThread()
   103 {
   104     switch (_threadState) {
   105         case ThreadState_NotLaunched:
   106         {
   107             _cond = SDL_CreateCond();
   108 
   109             _mutex = SDL_CreateMutex();
   110             _threadState = ThreadState_Running;
   111             _XAMLThread = SDL_CreateThread(WINRT_XAMLThreadMain, "SDL/XAML App Thread", nullptr);
   112 
   113             SDL_LockMutex(_mutex);
   114             while (_threadState != ThreadState_Yielding) {
   115                 SDL_CondWait(_cond, _mutex);
   116             }
   117             SDL_UnlockMutex(_mutex);
   118 
   119             break;
   120         }
   121 
   122         case ThreadState_Running:
   123         {
   124             SDL_assert(false);
   125             break;
   126         }
   127 
   128         case ThreadState_Yielding:
   129         {
   130             SDL_LockMutex(_mutex);
   131             SDL_assert(_threadState == ThreadState_Yielding);
   132             _threadState = ThreadState_Running;
   133             SDL_UnlockMutex(_mutex);
   134 
   135             SDL_CondSignal(_cond);
   136 
   137             SDL_LockMutex(_mutex);
   138             while (_threadState != ThreadState_Yielding) {
   139                 SDL_CondWait(_cond, _mutex);
   140             }
   141             SDL_UnlockMutex(_mutex);
   142         }
   143     }
   144 }
   145 
   146 #endif /* SDL_VIDEO_DRIVER_WINRT */
   147 
   148 /* vi: set ts=4 sw=4 expandtab: */