2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
22 /* Windows includes */
26 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
27 #include <windows.ui.xaml.media.dxinterop.h>
32 #include "../../SDL_internal.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"
41 /* SDL-internal globals: */
42 SDL_bool WINRT_XAMLWasEnabled = SDL_FALSE;
44 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
46 ISwapChainBackgroundPanelNative * WINRT_GlobalSwapChainBackgroundPanelNative = NULL;
47 static Windows::Foundation::EventRegistrationToken WINRT_XAMLAppEventToken;
52 * Input event handlers (XAML)
54 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
57 WINRT_OnPointerPressedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
59 WINRT_ProcessPointerPressedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
63 WINRT_OnPointerMovedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
65 WINRT_ProcessPointerMovedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
69 WINRT_OnPointerReleasedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
71 WINRT_ProcessPointerReleasedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
75 WINRT_OnPointerWheelChangedViaXAML(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args)
77 WINRT_ProcessPointerWheelChangedEvent(WINRT_GlobalSDLWindow, args->GetCurrentPoint(nullptr));
80 #endif // WINAPI_FAMILY == WINAPI_FAMILY_APP
84 * XAML-to-SDL Rendering Callback
86 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
89 WINRT_OnRenderViaXAML(_In_ Platform::Object^ sender, _In_ Platform::Object^ args)
91 WINRT_CycleXAMLThread();
94 #endif // WINAPI_FAMILY == WINAPI_FAMILY_APP
98 * SDL + XAML Initialization
102 SDL_WinRTInitXAMLApp(int (*mainFunction)(int, char **), void * backgroundPanelAsIInspectable)
104 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
105 return SDL_SetError("XAML support is not yet available in Windows Phone.");
107 // Declare C++/CX namespaces:
108 using namespace Platform;
109 using namespace Windows::Foundation;
110 using namespace Windows::UI::Core;
111 using namespace Windows::UI::Xaml;
112 using namespace Windows::UI::Xaml::Controls;
113 using namespace Windows::UI::Xaml::Input;
114 using namespace Windows::UI::Xaml::Media;
116 // Make sure we have a valid XAML element (to draw onto):
117 if ( ! backgroundPanelAsIInspectable) {
118 return SDL_SetError("'backgroundPanelAsIInspectable' can't be NULL");
121 Platform::Object ^ backgroundPanel = reinterpret_cast<Object ^>((IInspectable *) backgroundPanelAsIInspectable);
122 SwapChainBackgroundPanel ^swapChainBackgroundPanel = dynamic_cast<SwapChainBackgroundPanel ^>(backgroundPanel);
123 if ( ! swapChainBackgroundPanel) {
124 return SDL_SetError("An unknown or unsupported type of XAML control was specified.");
127 // Setup event handlers:
128 swapChainBackgroundPanel->PointerPressed += ref new PointerEventHandler(WINRT_OnPointerPressedViaXAML);
129 swapChainBackgroundPanel->PointerReleased += ref new PointerEventHandler(WINRT_OnPointerReleasedViaXAML);
130 swapChainBackgroundPanel->PointerWheelChanged += ref new PointerEventHandler(WINRT_OnPointerWheelChangedViaXAML);
131 swapChainBackgroundPanel->PointerMoved += ref new PointerEventHandler(WINRT_OnPointerMovedViaXAML);
133 // Setup for rendering:
134 IInspectable *panelInspectable = (IInspectable*) reinterpret_cast<IInspectable*>(swapChainBackgroundPanel);
135 panelInspectable->QueryInterface(__uuidof(ISwapChainBackgroundPanelNative), (void **)&WINRT_GlobalSwapChainBackgroundPanelNative);
137 WINRT_XAMLAppEventToken = CompositionTarget::Rendering::add(ref new EventHandler<Object^>(WINRT_OnRenderViaXAML));
139 // Make sure the app is ready to call the SDL-centric main() function:
140 WINRT_SDLAppEntryPoint = mainFunction;
143 // Make sure video-init knows that we're initializing XAML:
144 SDL_bool oldXAMLWasEnabledValue = WINRT_XAMLWasEnabled;
145 WINRT_XAMLWasEnabled = SDL_TRUE;
147 // Make sure video modes are detected now, while we still have access to the WinRT
148 // CoreWindow. WinRT will not allow the app's CoreWindow to be accessed via the
150 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
151 // SDL_InitSubSystem will, on error, set the SDL error. Let that propogate to
152 // the caller to here:
153 WINRT_XAMLWasEnabled = oldXAMLWasEnabledValue;
157 // All done, for now.
159 #endif // WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP / else