SDL_winrtapp_xaml.cpp 5.6 KB

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