SDL_windowsmouse.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_WINDOWS
  20. #include "SDL_assert.h"
  21. #include "SDL_windowsvideo.h"
  22. #include "../../events/SDL_mouse_c.h"
  23. HCURSOR SDL_cursor = NULL;
  24. static int rawInputEnableCount = 0;
  25. static int
  26. ToggleRawInput(SDL_bool enabled)
  27. {
  28. RAWINPUTDEVICE rawMouse = { 0x01, 0x02, 0, NULL }; /* Mouse: UsagePage = 1, Usage = 2 */
  29. if (enabled) {
  30. rawInputEnableCount++;
  31. if (rawInputEnableCount > 1) {
  32. return 0; /* already done. */
  33. }
  34. } else {
  35. if (rawInputEnableCount == 0) {
  36. return 0; /* already done. */
  37. }
  38. rawInputEnableCount--;
  39. if (rawInputEnableCount > 0) {
  40. return 0; /* not time to disable yet */
  41. }
  42. }
  43. if (!enabled) {
  44. rawMouse.dwFlags |= RIDEV_REMOVE;
  45. }
  46. /* (Un)register raw input for mice */
  47. if (RegisterRawInputDevices(&rawMouse, 1, sizeof(RAWINPUTDEVICE)) == FALSE) {
  48. /* Only return an error when registering. If we unregister and fail,
  49. then it's probably that we unregistered twice. That's OK. */
  50. if (enabled) {
  51. return SDL_Unsupported();
  52. }
  53. }
  54. return 0;
  55. }
  56. static SDL_Cursor *
  57. WIN_CreateDefaultCursor()
  58. {
  59. SDL_Cursor *cursor;
  60. cursor = SDL_calloc(1, sizeof(*cursor));
  61. if (cursor) {
  62. cursor->driverdata = LoadCursor(NULL, IDC_ARROW);
  63. } else {
  64. SDL_OutOfMemory();
  65. }
  66. return cursor;
  67. }
  68. static SDL_Cursor *
  69. WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
  70. {
  71. /* msdn says cursor mask has to be padded out to word alignment. Not sure
  72. if that means machine word or WORD, but this handles either case. */
  73. const size_t pad = (sizeof (size_t) * 8); /* 32 or 64, or whatever. */
  74. SDL_Cursor *cursor;
  75. HICON hicon;
  76. HDC hdc;
  77. BITMAPV4HEADER bmh;
  78. LPVOID pixels;
  79. LPVOID maskbits;
  80. size_t maskbitslen;
  81. ICONINFO ii;
  82. SDL_zero(bmh);
  83. bmh.bV4Size = sizeof(bmh);
  84. bmh.bV4Width = surface->w;
  85. bmh.bV4Height = -surface->h; /* Invert the image */
  86. bmh.bV4Planes = 1;
  87. bmh.bV4BitCount = 32;
  88. bmh.bV4V4Compression = BI_BITFIELDS;
  89. bmh.bV4AlphaMask = 0xFF000000;
  90. bmh.bV4RedMask = 0x00FF0000;
  91. bmh.bV4GreenMask = 0x0000FF00;
  92. bmh.bV4BlueMask = 0x000000FF;
  93. maskbitslen = ((surface->w + (pad - (surface->w % pad))) / 8) * surface->h;
  94. maskbits = SDL_stack_alloc(Uint8,maskbitslen);
  95. if (maskbits == NULL) {
  96. SDL_OutOfMemory();
  97. return NULL;
  98. }
  99. /* AND the cursor against full bits: no change. We already have alpha. */
  100. SDL_memset(maskbits, 0xFF, maskbitslen);
  101. hdc = GetDC(NULL);
  102. SDL_zero(ii);
  103. ii.fIcon = FALSE;
  104. ii.xHotspot = (DWORD)hot_x;
  105. ii.yHotspot = (DWORD)hot_y;
  106. ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
  107. ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, maskbits);
  108. ReleaseDC(NULL, hdc);
  109. SDL_stack_free(maskbits);
  110. SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
  111. SDL_assert(surface->pitch == surface->w * 4);
  112. SDL_memcpy(pixels, surface->pixels, surface->h * surface->pitch);
  113. hicon = CreateIconIndirect(&ii);
  114. DeleteObject(ii.hbmColor);
  115. DeleteObject(ii.hbmMask);
  116. if (!hicon) {
  117. WIN_SetError("CreateIconIndirect()");
  118. return NULL;
  119. }
  120. cursor = SDL_calloc(1, sizeof(*cursor));
  121. if (cursor) {
  122. cursor->driverdata = hicon;
  123. } else {
  124. DestroyIcon(hicon);
  125. SDL_OutOfMemory();
  126. }
  127. return cursor;
  128. }
  129. static SDL_Cursor *
  130. WIN_CreateSystemCursor(SDL_SystemCursor id)
  131. {
  132. SDL_Cursor *cursor;
  133. LPCTSTR name;
  134. switch(id)
  135. {
  136. default:
  137. SDL_assert(0);
  138. return NULL;
  139. case SDL_SYSTEM_CURSOR_ARROW: name = IDC_ARROW; break;
  140. case SDL_SYSTEM_CURSOR_IBEAM: name = IDC_IBEAM; break;
  141. case SDL_SYSTEM_CURSOR_WAIT: name = IDC_WAIT; break;
  142. case SDL_SYSTEM_CURSOR_CROSSHAIR: name = IDC_CROSS; break;
  143. case SDL_SYSTEM_CURSOR_WAITARROW: name = IDC_WAIT; break;
  144. case SDL_SYSTEM_CURSOR_SIZENWSE: name = IDC_SIZENWSE; break;
  145. case SDL_SYSTEM_CURSOR_SIZENESW: name = IDC_SIZENESW; break;
  146. case SDL_SYSTEM_CURSOR_SIZEWE: name = IDC_SIZEWE; break;
  147. case SDL_SYSTEM_CURSOR_SIZENS: name = IDC_SIZENS; break;
  148. case SDL_SYSTEM_CURSOR_SIZEALL: name = IDC_SIZEALL; break;
  149. case SDL_SYSTEM_CURSOR_NO: name = IDC_NO; break;
  150. case SDL_SYSTEM_CURSOR_HAND: name = IDC_HAND; break;
  151. }
  152. cursor = SDL_calloc(1, sizeof(*cursor));
  153. if (cursor) {
  154. HICON hicon;
  155. hicon = LoadCursor(NULL, name);
  156. cursor->driverdata = hicon;
  157. } else {
  158. SDL_OutOfMemory();
  159. }
  160. return cursor;
  161. }
  162. static void
  163. WIN_FreeCursor(SDL_Cursor * cursor)
  164. {
  165. HICON hicon = (HICON)cursor->driverdata;
  166. DestroyIcon(hicon);
  167. SDL_free(cursor);
  168. }
  169. static int
  170. WIN_ShowCursor(SDL_Cursor * cursor)
  171. {
  172. if (cursor) {
  173. SDL_cursor = (HCURSOR)cursor->driverdata;
  174. } else {
  175. SDL_cursor = NULL;
  176. }
  177. if (SDL_GetMouseFocus() != NULL) {
  178. SetCursor(SDL_cursor);
  179. }
  180. return 0;
  181. }
  182. static void
  183. WIN_WarpMouse(SDL_Window * window, int x, int y)
  184. {
  185. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  186. HWND hwnd = data->hwnd;
  187. POINT pt;
  188. /* Don't warp the mouse while we're doing a modal interaction */
  189. if (data->in_title_click || data->in_modal_loop) {
  190. return;
  191. }
  192. pt.x = x;
  193. pt.y = y;
  194. ClientToScreen(hwnd, &pt);
  195. SetCursorPos(pt.x, pt.y);
  196. }
  197. static int
  198. WIN_SetRelativeMouseMode(SDL_bool enabled)
  199. {
  200. return ToggleRawInput(enabled);
  201. }
  202. static int
  203. WIN_CaptureMouse(SDL_Window *window)
  204. {
  205. if (!window) {
  206. SDL_Window *focusWin = SDL_GetKeyboardFocus();
  207. if (focusWin) {
  208. SDL_WindowData *data = (SDL_WindowData *)focusWin->driverdata;
  209. WIN_OnWindowEnter(SDL_GetVideoDevice(), focusWin); /* make sure WM_MOUSELEAVE messages are (re)enabled. */
  210. }
  211. }
  212. /* While we were thinking of SetCapture() when designing this API in SDL,
  213. we didn't count on the fact that SetCapture() only tracks while the
  214. left mouse button is held down! Instead, we listen for raw mouse input
  215. and manually query the mouse when it leaves the window. :/ */
  216. return ToggleRawInput(window != NULL);
  217. }
  218. void
  219. WIN_InitMouse(_THIS)
  220. {
  221. SDL_Mouse *mouse = SDL_GetMouse();
  222. mouse->CreateCursor = WIN_CreateCursor;
  223. mouse->CreateSystemCursor = WIN_CreateSystemCursor;
  224. mouse->ShowCursor = WIN_ShowCursor;
  225. mouse->FreeCursor = WIN_FreeCursor;
  226. mouse->WarpMouse = WIN_WarpMouse;
  227. mouse->SetRelativeMouseMode = WIN_SetRelativeMouseMode;
  228. mouse->CaptureMouse = WIN_CaptureMouse;
  229. SDL_SetDefaultCursor(WIN_CreateDefaultCursor());
  230. SDL_SetDoubleClickTime(GetDoubleClickTime());
  231. }
  232. void
  233. WIN_QuitMouse(_THIS)
  234. {
  235. SDL_Mouse *mouse = SDL_GetMouse();
  236. if ( mouse->def_cursor ) {
  237. SDL_free(mouse->def_cursor);
  238. mouse->def_cursor = NULL;
  239. mouse->cur_cursor = NULL;
  240. }
  241. if (rawInputEnableCount) { /* force RAWINPUT off here. */
  242. rawInputEnableCount = 1;
  243. ToggleRawInput(SDL_FALSE);
  244. }
  245. }
  246. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  247. /* vi: set ts=4 sw=4 expandtab: */