SDL_winrtmouse.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. #if SDL_VIDEO_DRIVER_WINRT
  20. /*
  21. * Windows includes:
  22. */
  23. #include <Windows.h>
  24. using namespace Windows::UI::Core;
  25. using Windows::UI::Core::CoreCursor;
  26. /*
  27. * SDL includes:
  28. */
  29. extern "C" {
  30. #include "SDL_assert.h"
  31. #include "../../events/SDL_mouse_c.h"
  32. #include "../../events/SDL_touch_c.h"
  33. #include "../SDL_sysvideo.h"
  34. #include "SDL_events.h"
  35. #include "SDL_log.h"
  36. }
  37. #include "../../core/winrt/SDL_winrtapp_direct3d.h"
  38. #include "SDL_winrtvideo_cpp.h"
  39. #include "SDL_winrtmouse_c.h"
  40. extern "C" SDL_bool WINRT_UsingRelativeMouseMode = SDL_FALSE;
  41. static SDL_Cursor *
  42. WINRT_CreateSystemCursor(SDL_SystemCursor id)
  43. {
  44. SDL_Cursor *cursor;
  45. CoreCursorType cursorType = CoreCursorType::Arrow;
  46. switch(id)
  47. {
  48. default:
  49. SDL_assert(0);
  50. return NULL;
  51. case SDL_SYSTEM_CURSOR_ARROW: cursorType = CoreCursorType::Arrow; break;
  52. case SDL_SYSTEM_CURSOR_IBEAM: cursorType = CoreCursorType::IBeam; break;
  53. case SDL_SYSTEM_CURSOR_WAIT: cursorType = CoreCursorType::Wait; break;
  54. case SDL_SYSTEM_CURSOR_CROSSHAIR: cursorType = CoreCursorType::Cross; break;
  55. case SDL_SYSTEM_CURSOR_WAITARROW: cursorType = CoreCursorType::Wait; break;
  56. case SDL_SYSTEM_CURSOR_SIZENWSE: cursorType = CoreCursorType::SizeNorthwestSoutheast; break;
  57. case SDL_SYSTEM_CURSOR_SIZENESW: cursorType = CoreCursorType::SizeNortheastSouthwest; break;
  58. case SDL_SYSTEM_CURSOR_SIZEWE: cursorType = CoreCursorType::SizeWestEast; break;
  59. case SDL_SYSTEM_CURSOR_SIZENS: cursorType = CoreCursorType::SizeNorthSouth; break;
  60. case SDL_SYSTEM_CURSOR_SIZEALL: cursorType = CoreCursorType::SizeAll; break;
  61. case SDL_SYSTEM_CURSOR_NO: cursorType = CoreCursorType::UniversalNo; break;
  62. case SDL_SYSTEM_CURSOR_HAND: cursorType = CoreCursorType::Hand; break;
  63. }
  64. cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
  65. if (cursor) {
  66. /* Create a pointer to a COM reference to a cursor. The extra
  67. pointer is used (on top of the COM reference) to allow the cursor
  68. to be referenced by the SDL_cursor's driverdata field, which is
  69. a void pointer.
  70. */
  71. CoreCursor ^* theCursor = new CoreCursor^(nullptr);
  72. *theCursor = ref new CoreCursor(cursorType, 0);
  73. cursor->driverdata = (void *) theCursor;
  74. } else {
  75. SDL_OutOfMemory();
  76. }
  77. return cursor;
  78. }
  79. static SDL_Cursor *
  80. WINRT_CreateDefaultCursor()
  81. {
  82. return WINRT_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  83. }
  84. static void
  85. WINRT_FreeCursor(SDL_Cursor * cursor)
  86. {
  87. if (cursor->driverdata) {
  88. CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
  89. *theCursor = nullptr; // Release the COM reference to the CoreCursor
  90. delete theCursor; // Delete the pointer to the COM reference
  91. }
  92. SDL_free(cursor);
  93. }
  94. static int
  95. WINRT_ShowCursor(SDL_Cursor * cursor)
  96. {
  97. // TODO, WinRT, XAML: make WINRT_ShowCursor work when XAML support is enabled.
  98. if ( ! CoreWindow::GetForCurrentThread()) {
  99. return 0;
  100. }
  101. if (cursor) {
  102. CoreCursor ^* theCursor = (CoreCursor ^*) cursor->driverdata;
  103. CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;
  104. } else {
  105. CoreWindow::GetForCurrentThread()->PointerCursor = nullptr;
  106. }
  107. return 0;
  108. }
  109. static int
  110. WINRT_SetRelativeMouseMode(SDL_bool enabled)
  111. {
  112. WINRT_UsingRelativeMouseMode = enabled;
  113. return 0;
  114. }
  115. void
  116. WINRT_InitMouse(_THIS)
  117. {
  118. SDL_Mouse *mouse = SDL_GetMouse();
  119. /* DLudwig, Dec 3, 2012: WinRT does not currently provide APIs for
  120. the following features, AFAIK:
  121. - custom cursors (multiple system cursors are, however, available)
  122. - programmatically moveable cursors
  123. */
  124. #if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  125. //mouse->CreateCursor = WINRT_CreateCursor;
  126. mouse->CreateSystemCursor = WINRT_CreateSystemCursor;
  127. mouse->ShowCursor = WINRT_ShowCursor;
  128. mouse->FreeCursor = WINRT_FreeCursor;
  129. //mouse->WarpMouse = WINRT_WarpMouse;
  130. mouse->SetRelativeMouseMode = WINRT_SetRelativeMouseMode;
  131. SDL_SetDefaultCursor(WINRT_CreateDefaultCursor());
  132. #endif
  133. }
  134. void
  135. WINRT_QuitMouse(_THIS)
  136. {
  137. }
  138. #endif /* SDL_VIDEO_DRIVER_WINRT */
  139. /* vi: set ts=4 sw=4 expandtab: */