src/core/winrt/SDL_winrtapp_xaml.cpp
author Sam Lantinga <slouken@libsdl.org>
Mon, 22 Jun 2015 23:36:06 -0700
changeset 9776 952ff8a5076f
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Fixed bug 3030 - SDL_RecreateWindow fails to restore title, icon, etc.

Adam M.

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