SDL_winrtmouse.cpp 4.7 KB

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