SDL_winrtkeyboard.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #include "SDL_internal.h"
  19. #ifdef SDL_VIDEO_DRIVER_WINRT
  20. /* Windows-specific includes */
  21. #include <Windows.h>
  22. #include <agile.h>
  23. /* SDL-specific includes */
  24. #include "SDL_winrtevents_c.h"
  25. extern "C" {
  26. #include "../../events/scancodes_windows.h"
  27. #include "../../events/SDL_keyboard_c.h"
  28. }
  29. #include "SDL_winrtvideo_cpp.h"
  30. static SDL_Scancode WINRT_TranslateKeycode(Windows::System::VirtualKey virtualKey, const Windows::UI::Core::CorePhysicalKeyStatus& keyStatus)
  31. {
  32. SDL_Scancode code;
  33. Uint8 index;
  34. Uint16 scanCode = keyStatus.ScanCode | (keyStatus.IsExtendedKey ? 0xe000 : 0);
  35. /* Pause/Break key have a special scan code with 0xe1 prefix
  36. * that is not properly reported under UWP.
  37. * Use Pause scan code that is used in Win32. */
  38. if (virtualKey == Windows::System::VirtualKey::Pause) {
  39. scanCode = 0xe046;
  40. }
  41. /* Pack scan code into one byte to make the index. */
  42. index = LOBYTE(scanCode) | (HIBYTE(scanCode) ? 0x80 : 0x00);
  43. code = windows_scancode_table[index];
  44. return code;
  45. }
  46. void WINRT_ProcessAcceleratorKeyActivated(Windows::UI::Core::AcceleratorKeyEventArgs ^ args)
  47. {
  48. using namespace Windows::UI::Core;
  49. Uint8 state;
  50. SDL_Scancode code;
  51. switch (args->EventType) {
  52. case CoreAcceleratorKeyEventType::SystemKeyDown:
  53. case CoreAcceleratorKeyEventType::KeyDown:
  54. state = SDL_PRESSED;
  55. break;
  56. case CoreAcceleratorKeyEventType::SystemKeyUp:
  57. case CoreAcceleratorKeyEventType::KeyUp:
  58. state = SDL_RELEASED;
  59. break;
  60. default:
  61. return;
  62. }
  63. code = WINRT_TranslateKeycode(args->VirtualKey, args->KeyStatus);
  64. SDL_SendKeyboardKey(0, state, code);
  65. }
  66. void WINRT_ProcessCharacterReceivedEvent(SDL_Window *window, Windows::UI::Core::CharacterReceivedEventArgs ^ args)
  67. {
  68. if (!window) {
  69. return;
  70. }
  71. SDL_WindowData *data = window->driverdata;
  72. /* Characters outside Unicode Basic Multilingual Plane (BMP)
  73. * are coded as so called "surrogate pair" in two separate UTF-16 character events.
  74. * Cache high surrogate until next character event. */
  75. if (IS_HIGH_SURROGATE(args->KeyCode)) {
  76. data->high_surrogate = (WCHAR)args->KeyCode;
  77. } else {
  78. WCHAR utf16[] = {
  79. data->high_surrogate ? data->high_surrogate : (WCHAR)args->KeyCode,
  80. data->high_surrogate ? (WCHAR)args->KeyCode : L'\0',
  81. L'\0'
  82. };
  83. char utf8[5];
  84. // doesn't need to be WIN_WideCharToMultiByte, since we don't care about WinXP support in WinRT.
  85. int result = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, utf16, -1, utf8, sizeof(utf8), NULL, NULL);
  86. if (result > 0) {
  87. SDL_SendKeyboardText(utf8);
  88. }
  89. data->high_surrogate = L'\0';
  90. }
  91. }
  92. #if NTDDI_VERSION >= NTDDI_WIN10
  93. static bool WINRT_InputPaneVisible = false;
  94. void WINTRT_OnInputPaneShowing(Windows::UI::ViewManagement::InputPane ^ sender, Windows::UI::ViewManagement::InputPaneVisibilityEventArgs ^ args)
  95. {
  96. WINRT_InputPaneVisible = true;
  97. }
  98. void WINTRT_OnInputPaneHiding(Windows::UI::ViewManagement::InputPane ^ sender, Windows::UI::ViewManagement::InputPaneVisibilityEventArgs ^ args)
  99. {
  100. WINRT_InputPaneVisible = false;
  101. }
  102. void WINTRT_InitialiseInputPaneEvents(SDL_VideoDevice *_this)
  103. {
  104. using namespace Windows::UI::ViewManagement;
  105. InputPane ^ inputPane = InputPane::GetForCurrentView();
  106. if (inputPane) {
  107. inputPane->Showing += ref new Windows::Foundation::TypedEventHandler<Windows::UI::ViewManagement::InputPane ^,
  108. Windows::UI::ViewManagement::InputPaneVisibilityEventArgs ^>(&WINTRT_OnInputPaneShowing);
  109. inputPane->Hiding += ref new Windows::Foundation::TypedEventHandler<Windows::UI::ViewManagement::InputPane ^,
  110. Windows::UI::ViewManagement::InputPaneVisibilityEventArgs ^>(&WINTRT_OnInputPaneHiding);
  111. }
  112. }
  113. SDL_bool WINRT_HasScreenKeyboardSupport(SDL_VideoDevice *_this)
  114. {
  115. return SDL_TRUE;
  116. }
  117. void WINRT_ShowScreenKeyboard(SDL_VideoDevice *_this, SDL_Window *window)
  118. {
  119. using namespace Windows::UI::ViewManagement;
  120. InputPane ^ inputPane = InputPane::GetForCurrentView();
  121. if (inputPane) {
  122. inputPane->TryShow();
  123. }
  124. }
  125. void WINRT_HideScreenKeyboard(SDL_VideoDevice *_this, SDL_Window *window)
  126. {
  127. using namespace Windows::UI::ViewManagement;
  128. InputPane ^ inputPane = InputPane::GetForCurrentView();
  129. if (inputPane) {
  130. inputPane->TryHide();
  131. }
  132. }
  133. SDL_bool WINRT_IsScreenKeyboardShown(SDL_VideoDevice *_this, SDL_Window *window)
  134. {
  135. using namespace Windows::UI::ViewManagement;
  136. InputPane ^ inputPane = InputPane::GetForCurrentView();
  137. if (inputPane) {
  138. switch (SDL_WinRTGetDeviceFamily()) {
  139. case SDL_WINRT_DEVICEFAMILY_XBOX:
  140. // Documentation recommends using inputPane->Visible
  141. // https://learn.microsoft.com/en-us/uwp/api/windows.ui.viewmanagement.inputpane.visible?view=winrt-22621
  142. // This does not seem to work on latest UWP/Xbox.
  143. // Workaround: Listen to Showing/Hiding events
  144. if (WINRT_InputPaneVisible) {
  145. return SDL_TRUE;
  146. }
  147. break;
  148. default:
  149. // OccludedRect is recommend on universal apps per docs
  150. // https://learn.microsoft.com/en-us/uwp/api/windows.ui.viewmanagement.inputpane.visible?view=winrt-22621
  151. Windows::Foundation::Rect rect = inputPane->OccludedRect;
  152. if (rect.Width > 0 && rect.Height > 0) {
  153. return SDL_TRUE;
  154. }
  155. break;
  156. }
  157. }
  158. return SDL_FALSE;
  159. }
  160. #endif // NTDDI_VERSION >= ...
  161. #endif // SDL_VIDEO_DRIVER_WINRT