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