src/core/winrt/SDL_winrtapp_xaml.cpp
author David Ludwig <dludwig@pobox.com>
Wed, 12 Mar 2014 11:57:15 -0400
changeset 8611 a762dfc67e1a
parent 8600 092802455aed
child 8612 24d36bb9c7af
permissions -rw-r--r--
Fixed various build and runtime errors when using WinRT with VS2012.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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 
    22 /* Windows includes */
    23 #include <agile.h>
    24 #include <Windows.h>
    25 
    26 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
    27 #include <windows.ui.xaml.media.dxinterop.h>
    28 #endif
    29 
    30 
    31 /* SDL includes */
    32 #include "../../SDL_internal.h"
    33 #include "SDL.h"
    34 #include "../../video/winrt/SDL_winrtevents_c.h"
    35 #include "../../video/winrt/SDL_winrtvideo_cpp.h"
    36 #include "SDL_winrtapp_common.h"
    37 #include "SDL_winrtapp_xaml.h"
    38 
    39 
    40 
    41 /* SDL-internal globals: */
    42 SDL_bool WINRT_XAMLWasEnabled = SDL_FALSE;
    43 
    44 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
    45 extern "C" ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative = NULL;
    46 static Windows::Foundation::EventRegistrationToken	WINRT_XAMLAppEventToken;
    47 #endif
    48 
    49 
    50 /*
    51  * Input event handlers (XAML)
    52  */
    53 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
    54 
    55 static void
    56 WINRT_OnPointerPressedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
    57 {
    58     WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
    59 }
    60 
    61 static void
    62 WINRT_OnPointerMovedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
    63 {
    64     WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
    65 }
    66 
    67 static void
    68 WINRT_OnPointerReleasedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
    69 {
    70     WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
    71 }
    72 
    73 static void
    74 WINRT_OnPointerWheelChangedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
    75 {
    76     WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
    77 }
    78 
    79 #endif // WINAPI_FAMILY == WINAPI_FAMILY_APP
    80 
    81 
    82 /*
    83  * XAML-to-SDL Rendering Callback
    84  */
    85 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
    86 
    87 static void
    88 WINRT_OnRenderViaXAML(_In_ Platform::Object^ sender, _In_ Platform::Object^ args)
    89 {
    90     WINRT_CycleXAMLThread();
    91 }
    92 
    93 #endif // WINAPI_FAMILY == WINAPI_FAMILY_APP
    94 
    95 
    96 /*
    97  * SDL + XAML Initialization
    98  */
    99 
   100 int
   101 SDL_WinRTInitXAMLApp(int (*mainFunction)(int, char **), void * backgroundPanelAsIInspectable)
   102 {
   103 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   104     return SDL_SetError("XAML support is not yet available in Windows Phone.");
   105 #else
   106     // Declare C++/CX namespaces:
   107     using namespace Platform;
   108     using namespace Windows::Foundation;
   109     using namespace Windows::UI::Core;
   110     using namespace Windows::UI::Xaml;
   111     using namespace Windows::UI::Xaml::Controls;
   112     using namespace Windows::UI::Xaml::Input;
   113     using namespace Windows::UI::Xaml::Media;
   114 
   115     // Make sure we have a valid XAML element (to draw onto):
   116     if ( ! backgroundPanelAsIInspectable) {
   117         return SDL_SetError("'backgroundPanelAsIInspectable' can't be NULL");
   118     }
   119 
   120     Platform::Object ^ backgroundPanel = reinterpret_cast<Object ^>((IInspectable *) backgroundPanelAsIInspectable);
   121     SwapChainBackgroundPanel ^swapChainBackgroundPanel = dynamic_cast<SwapChainBackgroundPanel ^>(backgroundPanel);
   122     if ( ! swapChainBackgroundPanel) {
   123         return SDL_SetError("An unknown or unsupported type of XAML control was specified.");
   124     }
   125 
   126     // Setup event handlers:
   127     swapChainBackgroundPanel->PointerPressed += ref new PointerEventHandler(WINRT_OnPointerPressedViaXAML);
   128     swapChainBackgroundPanel->PointerReleased += ref new PointerEventHandler(WINRT_OnPointerReleasedViaXAML);
   129     swapChainBackgroundPanel->PointerWheelChanged += ref new PointerEventHandler(WINRT_OnPointerWheelChangedViaXAML);
   130     swapChainBackgroundPanel->PointerMoved += ref new PointerEventHandler(WINRT_OnPointerMovedViaXAML);
   131 
   132     // Setup for rendering:
   133     IInspectable *panelInspectable = (IInspectable*) reinterpret_cast<IInspectable*>(swapChainBackgroundPanel);
   134     panelInspectable->QueryInterface(__uuidof(ISwapChainBackgroundPanelNative), (void **)&WINRT_GlobalSwapChainBackgroundPanelNative);
   135 
   136     WINRT_XAMLAppEventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(WINRT_OnRenderViaXAML));
   137 
   138     // Make sure the app is ready to call the SDL-centric main() function:
   139     WINRT_SDLAppEntryPoint = mainFunction;
   140     SDL_SetMainReady();
   141 
   142     // Make sure video-init knows that we're initializing XAML:
   143     SDL_bool oldXAMLWasEnabledValue = WINRT_XAMLWasEnabled;
   144     WINRT_XAMLWasEnabled = SDL_TRUE;
   145 
   146     // Make sure video modes are detected now, while we still have access to the WinRT
   147     // CoreWindow.  WinRT will not allow the app's CoreWindow to be accessed via the
   148     // SDL/WinRT thread.
   149     if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
   150         // SDL_InitSubSystem will, on error, set the SDL error.  Let that propogate to
   151         // the caller to here:
   152         WINRT_XAMLWasEnabled = oldXAMLWasEnabledValue;
   153         return -1;
   154     }
   155 
   156     // All done, for now.
   157     return 0;
   158 #endif // WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP  /  else
   159 }