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
dludwig@8505
     1
/*
dludwig@8505
     2
  Simple DirectMedia Layer
dludwig@8505
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
dludwig@8505
     4
dludwig@8505
     5
  This software is provided 'as-is', without any express or implied
dludwig@8505
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8505
     7
  arising from the use of this software.
dludwig@8505
     8
dludwig@8505
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8505
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8505
    11
  freely, subject to the following restrictions:
dludwig@8505
    12
dludwig@8505
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8505
    14
     claim that you wrote the original software. If you use this software
dludwig@8505
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8505
    16
     appreciated but is not required.
dludwig@8505
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8505
    18
     misrepresented as being the original software.
dludwig@8505
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8505
    20
*/
dludwig@8505
    21
dludwig@8505
    22
/* Windows includes */
dludwig@8505
    23
#include <agile.h>
dludwig@8505
    24
#include <Windows.h>
dludwig@8505
    25
dludwig@8505
    26
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    27
#include <windows.ui.xaml.media.dxinterop.h>
dludwig@8505
    28
#endif
dludwig@8505
    29
dludwig@8505
    30
dludwig@8505
    31
/* SDL includes */
dludwig@8505
    32
#include "SDL.h"
dludwig@8505
    33
#include "../../video/winrt/SDL_winrtevents_c.h"
dludwig@8515
    34
#include "../../video/winrt/SDL_winrtvideo_cpp.h"
dludwig@8531
    35
#include "SDL_winrtapp_common.h"
dludwig@8522
    36
#include "SDL_winrtapp_xaml.h"
dludwig@8505
    37
dludwig@8505
    38
dludwig@8505
    39
dludwig@8515
    40
/* SDL-internal globals: */
dludwig@8505
    41
SDL_bool WINRT_XAMLWasEnabled = SDL_FALSE;
dludwig@8505
    42
dludwig@8505
    43
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    44
ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative = NULL;
dludwig@8505
    45
static Windows::Foundation::EventRegistrationToken	WINRT_XAMLAppEventToken;
dludwig@8505
    46
#endif
dludwig@8505
    47
dludwig@8505
    48
dludwig@8505
    49
/*
dludwig@8505
    50
 * Input event handlers (XAML)
dludwig@8505
    51
 */
dludwig@8505
    52
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    53
dludwig@8522
    54
static void
dludwig@8522
    55
WINRT_OnPointerPressedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
dludwig@8522
    56
{
dludwig@8522
    57
    WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
dludwig@8505
    58
}
dludwig@8505
    59
dludwig@8522
    60
static void
dludwig@8522
    61
WINRT_OnPointerMovedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
dludwig@8522
    62
{
dludwig@8522
    63
    WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
dludwig@8521
    64
}
dludwig@8521
    65
dludwig@8522
    66
static void
dludwig@8522
    67
WINRT_OnPointerReleasedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
dludwig@8522
    68
{
dludwig@8522
    69
    WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
dludwig@8505
    70
}
dludwig@8505
    71
dludwig@8522
    72
static void
dludwig@8522
    73
WINRT_OnPointerWheelChangedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
dludwig@8522
    74
{
dludwig@8522
    75
    WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
dludwig@8505
    76
}
dludwig@8505
    77
dludwig@8505
    78
#endif // WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    79
dludwig@8505
    80
dludwig@8505
    81
/*
dludwig@8505
    82
 * XAML-to-SDL Rendering Callback
dludwig@8505
    83
 */
dludwig@8505
    84
#if WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    85
dludwig@8505
    86
static void
dludwig@8505
    87
WINRT_OnRenderViaXAML(_In_ Platform::Object^ sender, _In_ Platform::Object^ args)
dludwig@8505
    88
{
dludwig@8505
    89
    WINRT_CycleXAMLThread();
dludwig@8505
    90
}
dludwig@8505
    91
dludwig@8505
    92
#endif // WINAPI_FAMILY == WINAPI_FAMILY_APP
dludwig@8505
    93
dludwig@8505
    94
dludwig@8505
    95
/*
dludwig@8505
    96
 * SDL + XAML Initialization
dludwig@8505
    97
 */
dludwig@8505
    98
dludwig@8531
    99
int
dludwig@8531
   100
SDL_WinRTInitXAMLApp(int (*mainFunction)(int, char **), void * backgroundPanelAsIInspectable)
dludwig@8505
   101
{
dludwig@8505
   102
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8505
   103
    return SDL_SetError("XAML support is not yet available in Windows Phone.");
dludwig@8505
   104
#else
dludwig@8505
   105
    // Declare C++/CX namespaces:
dludwig@8505
   106
    using namespace Platform;
dludwig@8505
   107
    using namespace Windows::Foundation;
dludwig@8505
   108
    using namespace Windows::UI::Core;
dludwig@8505
   109
    using namespace Windows::UI::Xaml;
dludwig@8505
   110
    using namespace Windows::UI::Xaml::Controls;
dludwig@8505
   111
    using namespace Windows::UI::Xaml::Input;
dludwig@8505
   112
    using namespace Windows::UI::Xaml::Media;
dludwig@8505
   113
dludwig@8505
   114
    // Make sure we have a valid XAML element (to draw onto):
dludwig@8531
   115
    if ( ! backgroundPanelAsIInspectable) {
dludwig@8531
   116
        return SDL_SetError("'backgroundPanelAsIInspectable' can't be NULL");
dludwig@8505
   117
    }
dludwig@8505
   118
dludwig@8531
   119
    Platform::Object ^ backgroundPanel = reinterpret_cast<Object ^>((IInspectable *) backgroundPanelAsIInspectable);
dludwig@8505
   120
    SwapChainBackgroundPanel ^swapChainBackgroundPanel = dynamic_cast<SwapChainBackgroundPanel ^>(backgroundPanel);
dludwig@8505
   121
    if ( ! swapChainBackgroundPanel) {
dludwig@8505
   122
        return SDL_SetError("An unknown or unsupported type of XAML control was specified.");
dludwig@8505
   123
    }
dludwig@8505
   124
dludwig@8505
   125
    // Setup event handlers:
dludwig@8505
   126
    swapChainBackgroundPanel->PointerPressed += ref new PointerEventHandler(WINRT_OnPointerPressedViaXAML);
dludwig@8505
   127
    swapChainBackgroundPanel->PointerReleased += ref new PointerEventHandler(WINRT_OnPointerReleasedViaXAML);
dludwig@8505
   128
    swapChainBackgroundPanel->PointerWheelChanged += ref new PointerEventHandler(WINRT_OnPointerWheelChangedViaXAML);
dludwig@8505
   129
    swapChainBackgroundPanel->PointerMoved += ref new PointerEventHandler(WINRT_OnPointerMovedViaXAML);
dludwig@8505
   130
dludwig@8505
   131
    // Setup for rendering:
dludwig@8522
   132
    IInspectable *panelInspectable = (IInspectable*) reinterpret_cast<IInspectable*>(swapChainBackgroundPanel);
dludwig@8505
   133
    panelInspectable->QueryInterface(__uuidof(ISwapChainBackgroundPanelNative), (void **)&WINRT_GlobalSwapChainBackgroundPanelNative);
dludwig@8505
   134
dludwig@8505
   135
    WINRT_XAMLAppEventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(WINRT_OnRenderViaXAML));
dludwig@8505
   136
dludwig@8505
   137
    // Make sure the app is ready to call the SDL-centric main() function:
dludwig@8531
   138
    WINRT_SDLAppEntryPoint = mainFunction;
dludwig@8505
   139
    SDL_SetMainReady();
dludwig@8505
   140
dludwig@8505
   141
    // Make sure video-init knows that we're initializing XAML:
dludwig@8505
   142
    SDL_bool oldXAMLWasEnabledValue = WINRT_XAMLWasEnabled;
dludwig@8505
   143
    WINRT_XAMLWasEnabled = SDL_TRUE;
dludwig@8505
   144
dludwig@8505
   145
    // Make sure video modes are detected now, while we still have access to the WinRT
dludwig@8505
   146
    // CoreWindow.  WinRT will not allow the app's CoreWindow to be accessed via the
dludwig@8505
   147
    // SDL/WinRT thread.
dludwig@8505
   148
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
dludwig@8505
   149
        // SDL_InitSubSystem will, on error, set the SDL error.  Let that propogate to
dludwig@8505
   150
        // the caller to here:
dludwig@8505
   151
        WINRT_XAMLWasEnabled = oldXAMLWasEnabledValue;
dludwig@8505
   152
        return -1;
dludwig@8505
   153
    }
dludwig@8505
   154
dludwig@8505
   155
    // All done, for now.
dludwig@8505
   156
    return 0;
dludwig@8505
   157
#endif // WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP  /  else
dludwig@8505
   158
}