21 #include "../../SDL_internal.h"
32 using namespace concurrency;
33 using namespace Windows::ApplicationModel;
34 using namespace Windows::ApplicationModel::Core;
35 using namespace Windows::ApplicationModel::Activation;
36 using namespace Windows::Devices::Input;
37 using namespace Windows::Graphics::Display;
38 using namespace Windows::Foundation;
39 using namespace Windows::System;
40 using namespace Windows::UI::Core;
41 using namespace Windows::UI::Input;
43 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
44 using namespace Windows::Phone::UI::Input;
55 #include "../../video/SDL_sysvideo.h"
57 #include "../../events/SDL_events_c.h"
58 #include "../../events/SDL_keyboard_c.h"
59 #include "../../events/SDL_mouse_c.h"
60 #include "../../events/SDL_windowevents_c.h"
61 #include "../../render/SDL_sysrender.h"
62 #include "../windows/SDL_windows.h"
65 #include "../../video/winrt/SDL_winrtevents_c.h"
66 #include "../../video/winrt/SDL_winrtvideo_cpp.h"
70 #if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
94 ref class SDLApplicationSource
sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
97 virtual Windows::ApplicationModel::Core::IFrameworkView^
CreateView();
100 IFrameworkView^ SDLApplicationSource::CreateView()
106 SDL_WinRTApp ^ app =
ref new SDL_WinRTApp();
117 auto direct3DApplicationSource =
ref new SDLApplicationSource();
133 if ((oldValue ==
NULL) && (newValue ==
NULL)) {
139 unsigned int orientationFlags = 0;
141 std::istringstream tokenizer(newValue);
142 while (!tokenizer.eof()) {
144 std::getline(tokenizer, orientationName,
' ');
145 if (orientationName ==
"LandscapeLeft") {
146 orientationFlags |= (
unsigned int) DisplayOrientations::LandscapeFlipped;
147 }
else if (orientationName ==
"LandscapeRight") {
148 orientationFlags |= (
unsigned int) DisplayOrientations::Landscape;
149 }
else if (orientationName ==
"Portrait") {
150 orientationFlags |= (
unsigned int) DisplayOrientations::Portrait;
151 }
else if (orientationName ==
"PortraitUpsideDown") {
152 orientationFlags |= (
unsigned int) DisplayOrientations::PortraitFlipped;
158 if (!orientationFlags) {
160 orientationFlags = (
unsigned int) ( \
161 DisplayOrientations::Landscape |
162 DisplayOrientations::LandscapeFlipped |
163 DisplayOrientations::Portrait |
164 DisplayOrientations::PortraitFlipped);
181 WINRT_DISPLAY_PROPERTY(AutoRotationPreferences) = (DisplayOrientations) orientationFlags;
187 CoreWindow ^ coreWindow = CoreWindow::GetForCurrentThread();
193 int x = WINRT_DIPS_TO_PHYSICAL_PIXELS(
data->coreWindow->Bounds.Left);
194 int y = WINRT_DIPS_TO_PHYSICAL_PIXELS(
data->coreWindow->Bounds.Top);
195 int w = WINRT_DIPS_TO_PHYSICAL_PIXELS(
data->coreWindow->Bounds.Width);
196 int h = WINRT_DIPS_TO_PHYSICAL_PIXELS(
data->coreWindow->Bounds.Height);
198 #if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) && (NTDDI_VERSION == NTDDI_WIN8)
208 const DisplayOrientations currentOrientation = WINRT_DISPLAY_PROPERTY(CurrentOrientation);
209 switch (currentOrientation) {
210 case DisplayOrientations::Landscape:
211 case DisplayOrientations::LandscapeFlipped: {
236 SDL_WinRTApp::SDL_WinRTApp() :
237 m_windowClosed(false),
238 m_windowVisible(true)
242 void SDL_WinRTApp::Initialize(CoreApplicationView^ applicationView)
244 applicationView->Activated +=
245 ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(
this, &SDL_WinRTApp::OnAppActivated);
247 CoreApplication::Suspending +=
248 ref new EventHandler<SuspendingEventArgs^>(
this, &SDL_WinRTApp::OnSuspending);
250 CoreApplication::Resuming +=
251 ref new EventHandler<Platform::Object^>(
this, &SDL_WinRTApp::OnResuming);
253 CoreApplication::Exiting +=
254 ref new EventHandler<Platform::Object^>(
this, &SDL_WinRTApp::OnExiting);
256 #if NTDDI_VERSION >= NTDDI_WIN10
262 Windows::Gaming::Input::Gamepad::GamepadAdded +=
263 ref new Windows::Foundation::EventHandler<Windows::Gaming::Input::Gamepad^>(
264 this, &SDL_WinRTApp::OnGamepadAdded
269 #if NTDDI_VERSION > NTDDI_WIN8
270 void SDL_WinRTApp::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
272 void SDL_WinRTApp::OnOrientationChanged(Object^ sender)
275 #if LOG_ORIENTATION_EVENTS==1
277 CoreWindow^
window = CoreWindow::GetForCurrentThread();
279 SDL_Log(
"%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, CoreWindow Bounds={%f,%f,%f,%f}\n",
281 WINRT_DISPLAY_PROPERTY(CurrentOrientation),
282 WINRT_DISPLAY_PROPERTY(NativeOrientation),
283 WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
289 SDL_Log(
"%s, current orientation=%d, native orientation=%d, auto rot. pref=%d\n",
291 WINRT_DISPLAY_PROPERTY(CurrentOrientation),
292 WINRT_DISPLAY_PROPERTY(NativeOrientation),
293 WINRT_DISPLAY_PROPERTY(AutoRotationPreferences));
300 #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
313 int w = WINRT_DIPS_TO_PHYSICAL_PIXELS(
data->coreWindow->Bounds.Width);
314 int h = WINRT_DIPS_TO_PHYSICAL_PIXELS(
data->coreWindow->Bounds.Height);
321 void SDL_WinRTApp::SetWindow(CoreWindow^
window)
323 #if LOG_WINDOW_EVENTS==1
324 SDL_Log(
"%s, current orientation=%d, native orientation=%d, auto rot. pref=%d, window bounds={%f, %f, %f,%f}\n",
326 WINRT_DISPLAY_PROPERTY(CurrentOrientation),
327 WINRT_DISPLAY_PROPERTY(NativeOrientation),
328 WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
336 ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(
this, &SDL_WinRTApp::OnWindowSizeChanged);
338 window->VisibilityChanged +=
339 ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(
this, &SDL_WinRTApp::OnVisibilityChanged);
342 ref new TypedEventHandler<CoreWindow^, WindowActivatedEventArgs^>(
this, &SDL_WinRTApp::OnWindowActivated);
345 ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(
this, &SDL_WinRTApp::OnWindowClosed);
347 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
348 window->PointerCursor =
ref new CoreCursor(CoreCursorType::Arrow, 0);
352 ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(
this, &SDL_WinRTApp::OnPointerPressed);
355 ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(
this, &SDL_WinRTApp::OnPointerMoved);
357 window->PointerReleased +=
358 ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(
this, &SDL_WinRTApp::OnPointerReleased);
361 ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(
this, &SDL_WinRTApp::OnPointerEntered);
364 ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(
this, &SDL_WinRTApp::OnPointerExited);
366 window->PointerWheelChanged +=
367 ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(
this, &SDL_WinRTApp::OnPointerWheelChanged);
369 #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
371 Windows::Devices::Input::MouseDevice::GetForCurrentView()->MouseMoved +=
372 ref new TypedEventHandler<MouseDevice^, MouseEventArgs^>(
this, &SDL_WinRTApp::OnMouseMoved);
376 ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(
this, &SDL_WinRTApp::OnKeyDown);
379 ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(
this, &SDL_WinRTApp::OnKeyUp);
381 window->CharacterReceived +=
382 ref new TypedEventHandler<CoreWindow^, CharacterReceivedEventArgs^>(
this, &SDL_WinRTApp::OnCharacterReceived);
384 #if NTDDI_VERSION >= NTDDI_WIN10
385 Windows::UI::Core::SystemNavigationManager::GetForCurrentView()->BackRequested +=
386 ref new EventHandler<BackRequestedEventArgs^>(
this, &SDL_WinRTApp::OnBackButtonPressed);
387 #elif WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
388 HardwareButtons::BackPressed +=
389 ref new EventHandler<BackPressedEventArgs^>(
this, &SDL_WinRTApp::OnBackButtonPressed);
392 #if NTDDI_VERSION > NTDDI_WIN8
393 DisplayInformation::GetForCurrentView()->OrientationChanged +=
394 ref new TypedEventHandler<Windows::Graphics::Display::DisplayInformation^, Object^>(
this, &SDL_WinRTApp::OnOrientationChanged);
396 DisplayProperties::OrientationChanged +=
397 ref new DisplayPropertiesEventHandler(
this, &SDL_WinRTApp::OnOrientationChanged);
404 #if (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (NTDDI_VERSION < NTDDI_WIN10)
408 using namespace Windows::UI::ApplicationSettings;
409 SettingsPane::GetForCurrentView()->CommandsRequested +=
410 ref new TypedEventHandler<SettingsPane^, SettingsPaneCommandsRequestedEventArgs^>
411 (
this, &SDL_WinRTApp::OnSettingsPaneCommandsRequested);
415 void SDL_WinRTApp::Load(Platform::String^ entryPoint)
444 bool SDL_WinRTApp::ShouldWaitForAppResumeEvents()
447 if (m_windowVisible) {
471 void SDL_WinRTApp::PumpEvents()
473 if (!m_windowClosed) {
474 if (!ShouldWaitForAppResumeEvents()) {
479 CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
488 CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
493 void SDL_WinRTApp::Uninitialize()
497 #if (WINAPI_FAMILY == WINAPI_FAMILY_APP) && (NTDDI_VERSION < NTDDI_WIN10)
498 void SDL_WinRTApp::OnSettingsPaneCommandsRequested(
499 Windows::UI::ApplicationSettings::SettingsPane ^
p,
500 Windows::UI::ApplicationSettings::SettingsPaneCommandsRequestedEventArgs ^args)
502 using namespace Platform;
503 using namespace Windows::UI::ApplicationSettings;
504 using namespace Windows::UI::Popups;
506 String ^privacyPolicyURL =
nullptr;
507 String ^privacyPolicyLabel =
nullptr;
508 const char *tmpHintValue =
NULL;
509 wchar_t *tmpStr =
NULL;
513 if (tmpHintValue && tmpHintValue[0] !=
'\0') {
516 privacyPolicyURL =
ref new String(tmpStr);
522 if (tmpHintValue && tmpHintValue[0] !=
'\0') {
524 privacyPolicyLabel =
ref new String(tmpStr);
527 privacyPolicyLabel =
ref new String(L
"Privacy Policy");
532 auto cmd =
ref new SettingsCommand(L
"privacyPolicy", privacyPolicyLabel,
533 ref new UICommandInvokedHandler([=](IUICommand ^) {
534 Windows::System::Launcher::LaunchUriAsync(
ref new Uri(privacyPolicyURL));
536 args->Request->ApplicationCommands->Append(cmd);
541 void SDL_WinRTApp::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
543 #if LOG_WINDOW_EVENTS==1
544 SDL_Log(
"%s, size={%f,%f}, bounds={%f,%f,%f,%f}, current orientation=%d, native orientation=%d, auto rot. pref=%d, WINRT_GlobalSDLWindow?=%s\n",
546 args->Size.Width, args->Size.Height,
547 sender->Bounds.X, sender->Bounds.Y, sender->Bounds.Width, sender->Bounds.Height,
548 WINRT_DISPLAY_PROPERTY(CurrentOrientation),
549 WINRT_DISPLAY_PROPERTY(NativeOrientation),
550 WINRT_DISPLAY_PROPERTY(AutoRotationPreferences),
557 void SDL_WinRTApp::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
559 #if LOG_WINDOW_EVENTS==1
560 SDL_Log(
"%s, visible?=%s, bounds={%f,%f,%f,%f}, WINRT_GlobalSDLWindow?=%s\n",
562 (args->Visible ?
"yes" :
"no"),
563 sender->Bounds.X, sender->Bounds.Y,
564 sender->Bounds.Width, sender->Bounds.Height,
568 m_windowVisible = args->Visible;
596 void SDL_WinRTApp::OnWindowActivated(CoreWindow^ sender, WindowActivatedEventArgs^ args)
598 #if LOG_WINDOW_EVENTS==1
599 SDL_Log(
"%s, WINRT_GlobalSDLWindow?=%s\n\n",
609 sender->CustomProperties->Insert(
"SDLHelperWindowActivationState", args->WindowActivationState);
613 if (args->WindowActivationState != CoreWindowActivationState::Deactivated) {
628 #if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION >= NTDDI_WINBLUE)
629 Point cursorPos = WINRT_TransformCursorPosition(
window, sender->PointerPosition, TransformToSDLWindowSize);
658 void SDL_WinRTApp::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
660 #if LOG_WINDOW_EVENTS==1
663 m_windowClosed =
true;
666 void SDL_WinRTApp::OnAppActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
668 CoreWindow::GetForCurrentThread()->Activate();
671 void SDL_WinRTApp::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
687 SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
688 create_task([
this, deferral]()
702 #if SDL_VIDEO_RENDER_D3D11 && !SDL_RENDER_DISABLED
711 deferral->Complete();
715 void SDL_WinRTApp::OnResuming(Platform::Object^ sender, Platform::Object^ args)
724 void SDL_WinRTApp::OnExiting(Platform::Object^ sender, Platform::Object^ args)
730 WINRT_LogPointerEvent(
const char * header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
732 Windows::UI::Input::PointerPoint ^ pt = args->CurrentPoint;
733 SDL_Log(
"%s: Position={%f,%f}, Transformed Pos={%f, %f}, MouseWheelDelta=%d, FrameId=%d, PointerId=%d, SDL button=%d\n",
735 pt->Position.X, pt->Position.Y,
736 transformedPoint.X, transformedPoint.Y,
737 pt->Properties->MouseWheelDelta,
740 WINRT_GetSDLButtonForPointerPoint(pt));
743 void SDL_WinRTApp::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
745 #if LOG_POINTER_EVENTS
752 void SDL_WinRTApp::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
754 #if LOG_POINTER_EVENTS
761 void SDL_WinRTApp::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
763 #if LOG_POINTER_EVENTS
770 void SDL_WinRTApp::OnPointerEntered(CoreWindow^ sender, PointerEventArgs^ args)
772 #if LOG_POINTER_EVENTS
779 void SDL_WinRTApp::OnPointerExited(CoreWindow^ sender, PointerEventArgs^ args)
781 #if LOG_POINTER_EVENTS
788 void SDL_WinRTApp::OnPointerWheelChanged(CoreWindow^ sender, PointerEventArgs^ args)
790 #if LOG_POINTER_EVENTS
797 void SDL_WinRTApp::OnMouseMoved(MouseDevice^ mouseDevice, MouseEventArgs^ args)
802 void SDL_WinRTApp::OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
804 WINRT_ProcessKeyDownEvent(args);
807 void SDL_WinRTApp::OnKeyUp(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args)
809 WINRT_ProcessKeyUpEvent(args);
812 void SDL_WinRTApp::OnCharacterReceived(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::CharacterReceivedEventArgs^ args)
814 WINRT_ProcessCharacterReceivedEvent(args);
817 template <
typename BackButtonEventArgs>
824 args->Handled =
true;
828 #if NTDDI_VERSION >= NTDDI_WIN10
829 void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::UI::Core::BackRequestedEventArgs^ args)
834 #elif WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
835 void SDL_WinRTApp::OnBackButtonPressed(Platform::Object^ sender, Windows::Phone::UI::Input::BackPressedEventArgs^ args)
842 #if NTDDI_VERSION >= NTDDI_WIN10
843 void SDL_WinRTApp::OnGamepadAdded(Platform::Object ^sender, Windows::Gaming::Input::Gamepad ^gamepad)
#define SDL_assert(condition)
#define SDL_GetKeyboardFocus
#define SDL_GetHintBoolean
#define SDL_AddHintCallback
int SDL_SendAppEvent(SDL_EventType eventType)
@ SDL_APP_WILLENTERFOREGROUND
@ SDL_APP_DIDENTERFOREGROUND
@ SDL_APP_WILLENTERBACKGROUND
@ SDL_APP_DIDENTERBACKGROUND
#define SDL_HINT_ORIENTATIONS
A variable controlling which orientations are allowed on iOS/Android.
#define SDL_HINT_WINRT_PRIVACY_POLICY_LABEL
Label text for a WinRT app's privacy policy link.
#define SDL_HINT_WINRT_HANDLE_BACK_BUTTON
Allows back-button-press events on Windows Phone to be marked as handled.
#define SDL_HINT_WINRT_PRIVACY_POLICY_URL
A URL to a WinRT app's privacy policy.
void SDL_SetKeyboardFocus(SDL_Window *window)
int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
int SDL_SendMouseMotion(SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
GLint GLint GLint GLint GLint GLint y
GLuint GLuint GLsizei count
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
GLint GLint GLint GLint GLint x
GLuint const GLchar * name
GLsizei const GLchar *const * string
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
@ SDL_WINDOW_FULLSCREEN_DESKTOP
SDL_WindowEventID
Event subtype for window events.
@ SDL_WINDOWEVENT_FOCUS_LOST
@ SDL_WINDOWEVENT_RESIZED
@ SDL_WINDOWEVENT_FOCUS_GAINED
@ SDL_WINDOWEVENT_MINIMIZED
@ SDL_WINDOWEVENT_MAXIMIZED
@ SDL_WINDOWEVENT_SIZE_CHANGED
@ SDL_WINDOWEVENT_RESTORED
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
#define WIN_UTF8ToString(S)
int(* WINRT_SDLAppEntryPoint)(int, char **)
SDL_WinRTApp SDL_WinRTGlobalApp
static bool IsSDLWindowEventPending(SDL_WindowEventID windowEventID)
static void WINRT_SetDisplayOrientationsPreference(void *userdata, const char *name, const char *oldValue, const char *newValue)
int SDL_WinRTInitNonXAMLApp(int(*mainFunction)(int, char **))
static void WINRT_LogPointerEvent(const char *header, Windows::UI::Core::PointerEventArgs ^ args, Windows::Foundation::Point transformedPoint)
static void WINRT_ProcessWindowSizeChange()
static void WINRT_OnBackButtonPressed(BackButtonEventArgs ^ args)
Uint32 WINRT_DetectWindowFlags(SDL_Window *window)
SDL_Window * WINRT_GlobalSDLWindow
void WINRT_UpdateWindowFlags(SDL_Window *window, Uint32 mask)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
virtual Windows::ApplicationModel::Core::IFrameworkView CreateView()
EGLSurface EGLNativeWindowType * window
The type used to identify a window.
static SDL_Renderer * renderer
static SDL_Event events[EVENT_BUF_SIZE]
typedef int(__stdcall *FARPROC)()