SDL_mouse.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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. /* General mouse handling code for SDL */
  20. #include "SDL_assert.h"
  21. #include "SDL_hints.h"
  22. #include "SDL_timer.h"
  23. #include "SDL_events.h"
  24. #include "SDL_events_c.h"
  25. #include "../SDL_hints_c.h"
  26. #include "../video/SDL_sysvideo.h"
  27. #ifdef __WIN32__
  28. #include "../core/windows/SDL_windows.h" // For GetDoubleClickTime()
  29. #endif
  30. /* #define DEBUG_MOUSE */
  31. /* The mouse state */
  32. static SDL_Mouse SDL_mouse;
  33. /* for mapping mouse events to touch */
  34. static SDL_bool track_mouse_down = SDL_FALSE;
  35. static int
  36. SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);
  37. static void SDLCALL
  38. SDL_MouseDoubleClickTimeChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  39. {
  40. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  41. if (hint && *hint) {
  42. mouse->double_click_time = SDL_atoi(hint);
  43. } else {
  44. #ifdef __WIN32__
  45. mouse->double_click_time = GetDoubleClickTime();
  46. #else
  47. mouse->double_click_time = 500;
  48. #endif
  49. }
  50. }
  51. static void SDLCALL
  52. SDL_MouseDoubleClickRadiusChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  53. {
  54. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  55. if (hint && *hint) {
  56. mouse->double_click_radius = SDL_atoi(hint);
  57. } else {
  58. mouse->double_click_radius = 32; /* 32 pixels seems about right for touch interfaces */
  59. }
  60. }
  61. static void SDLCALL
  62. SDL_MouseNormalSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  63. {
  64. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  65. if (hint && *hint) {
  66. mouse->normal_speed_scale = (float)SDL_atof(hint);
  67. } else {
  68. mouse->normal_speed_scale = 1.0f;
  69. }
  70. }
  71. static void SDLCALL
  72. SDL_MouseRelativeSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  73. {
  74. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  75. if (hint && *hint) {
  76. mouse->relative_speed_scale = (float)SDL_atof(hint);
  77. } else {
  78. mouse->relative_speed_scale = 1.0f;
  79. }
  80. }
  81. static void SDLCALL
  82. SDL_TouchMouseEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  83. {
  84. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  85. mouse->touch_mouse_events = SDL_GetStringBoolean(hint, SDL_TRUE);
  86. }
  87. static void SDLCALL
  88. SDL_MouseTouchEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  89. {
  90. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  91. SDL_bool default_value;
  92. #if defined(__ANDROID__) || (defined(__IPHONEOS__) && !defined(__TVOS__))
  93. default_value = SDL_TRUE;
  94. #else
  95. default_value = SDL_FALSE;
  96. #endif
  97. mouse->mouse_touch_events = SDL_GetStringBoolean(hint, default_value);
  98. if (mouse->mouse_touch_events) {
  99. SDL_AddTouch(SDL_MOUSE_TOUCHID, SDL_TOUCH_DEVICE_DIRECT, "mouse_input");
  100. }
  101. }
  102. /* Public functions */
  103. int
  104. SDL_MouseInit(void)
  105. {
  106. SDL_Mouse *mouse = SDL_GetMouse();
  107. SDL_zerop(mouse);
  108. SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_TIME,
  109. SDL_MouseDoubleClickTimeChanged, mouse);
  110. SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS,
  111. SDL_MouseDoubleClickRadiusChanged, mouse);
  112. SDL_AddHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
  113. SDL_MouseNormalSpeedScaleChanged, mouse);
  114. SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
  115. SDL_MouseRelativeSpeedScaleChanged, mouse);
  116. SDL_AddHintCallback(SDL_HINT_TOUCH_MOUSE_EVENTS,
  117. SDL_TouchMouseEventsChanged, mouse);
  118. SDL_AddHintCallback(SDL_HINT_MOUSE_TOUCH_EVENTS,
  119. SDL_MouseTouchEventsChanged, mouse);
  120. mouse->was_touch_mouse_events = SDL_FALSE; /* no touch to mouse movement event pending */
  121. mouse->cursor_shown = SDL_TRUE;
  122. return (0);
  123. }
  124. void
  125. SDL_SetDefaultCursor(SDL_Cursor * cursor)
  126. {
  127. SDL_Mouse *mouse = SDL_GetMouse();
  128. mouse->def_cursor = cursor;
  129. if (!mouse->cur_cursor) {
  130. SDL_SetCursor(cursor);
  131. }
  132. }
  133. SDL_Mouse *
  134. SDL_GetMouse(void)
  135. {
  136. return &SDL_mouse;
  137. }
  138. SDL_Window *
  139. SDL_GetMouseFocus(void)
  140. {
  141. SDL_Mouse *mouse = SDL_GetMouse();
  142. return mouse->focus;
  143. }
  144. #if 0
  145. void
  146. SDL_ResetMouse(void)
  147. {
  148. SDL_Mouse *mouse = SDL_GetMouse();
  149. Uint8 i;
  150. #ifdef DEBUG_MOUSE
  151. printf("Resetting mouse\n");
  152. #endif
  153. for (i = 1; i <= sizeof(mouse->buttonstate)*8; ++i) {
  154. if (mouse->buttonstate & SDL_BUTTON(i)) {
  155. SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, i);
  156. }
  157. }
  158. SDL_assert(mouse->buttonstate == 0);
  159. }
  160. #endif
  161. void
  162. SDL_SetMouseFocus(SDL_Window * window)
  163. {
  164. SDL_Mouse *mouse = SDL_GetMouse();
  165. if (mouse->focus == window) {
  166. return;
  167. }
  168. /* Actually, this ends up being a bad idea, because most operating
  169. systems have an implicit grab when you press the mouse button down
  170. so you can drag things out of the window and then get the mouse up
  171. when it happens. So, #if 0...
  172. */
  173. #if 0
  174. if (mouse->focus && !window) {
  175. /* We won't get anymore mouse messages, so reset mouse state */
  176. SDL_ResetMouse();
  177. }
  178. #endif
  179. /* See if the current window has lost focus */
  180. if (mouse->focus) {
  181. SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
  182. }
  183. mouse->focus = window;
  184. mouse->has_position = SDL_FALSE;
  185. if (mouse->focus) {
  186. SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
  187. }
  188. /* Update cursor visibility */
  189. SDL_SetCursor(NULL);
  190. }
  191. /* Check to see if we need to synthesize focus events */
  192. static SDL_bool
  193. SDL_UpdateMouseFocus(SDL_Window * window, int x, int y, Uint32 buttonstate, SDL_bool send_mouse_motion)
  194. {
  195. SDL_Mouse *mouse = SDL_GetMouse();
  196. SDL_bool inWindow = SDL_TRUE;
  197. if (window && ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0)) {
  198. int w, h;
  199. SDL_GetWindowSize(window, &w, &h);
  200. if (x < 0 || y < 0 || x >= w || y >= h) {
  201. inWindow = SDL_FALSE;
  202. }
  203. }
  204. /* Linux doesn't give you mouse events outside your window unless you grab
  205. the pointer.
  206. Windows doesn't give you mouse events outside your window unless you call
  207. SetCapture().
  208. Both of these are slightly scary changes, so for now we'll punt and if the
  209. mouse leaves the window you'll lose mouse focus and reset button state.
  210. */
  211. #ifdef SUPPORT_DRAG_OUTSIDE_WINDOW
  212. if (!inWindow && !buttonstate) {
  213. #else
  214. if (!inWindow) {
  215. #endif
  216. if (window == mouse->focus) {
  217. #ifdef DEBUG_MOUSE
  218. printf("Mouse left window, synthesizing move & focus lost event\n");
  219. #endif
  220. if (send_mouse_motion) {
  221. SDL_PrivateSendMouseMotion(window, mouse->mouseID, 0, x, y);
  222. }
  223. SDL_SetMouseFocus(NULL);
  224. }
  225. return SDL_FALSE;
  226. }
  227. if (window != mouse->focus) {
  228. #ifdef DEBUG_MOUSE
  229. printf("Mouse entered window, synthesizing focus gain & move event\n");
  230. #endif
  231. SDL_SetMouseFocus(window);
  232. if (send_mouse_motion) {
  233. SDL_PrivateSendMouseMotion(window, mouse->mouseID, 0, x, y);
  234. }
  235. }
  236. return SDL_TRUE;
  237. }
  238. int
  239. SDL_SendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
  240. {
  241. if (window && !relative) {
  242. SDL_Mouse *mouse = SDL_GetMouse();
  243. if (!SDL_UpdateMouseFocus(window, x, y, mouse->buttonstate, (mouseID == SDL_TOUCH_MOUSEID) ? SDL_FALSE : SDL_TRUE)) {
  244. return 0;
  245. }
  246. }
  247. return SDL_PrivateSendMouseMotion(window, mouseID, relative, x, y);
  248. }
  249. static int
  250. GetScaledMouseDelta(float scale, int value, float *accum)
  251. {
  252. if (scale != 1.0f) {
  253. *accum += scale * value;
  254. if (*accum >= 0.0f) {
  255. value = (int)SDL_floor(*accum);
  256. } else {
  257. value = (int)SDL_ceil(*accum);
  258. }
  259. *accum -= value;
  260. }
  261. return value;
  262. }
  263. static int
  264. SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
  265. {
  266. SDL_Mouse *mouse = SDL_GetMouse();
  267. int posted;
  268. int xrel;
  269. int yrel;
  270. /* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
  271. if (mouse->mouse_touch_events) {
  272. if (mouseID != SDL_TOUCH_MOUSEID && !relative && track_mouse_down) {
  273. if (window) {
  274. float fx = (float)x / (float)window->w;
  275. float fy = (float)y / (float)window->h;
  276. SDL_SendTouchMotion(SDL_MOUSE_TOUCHID, 0, window, fx, fy, 1.0f);
  277. }
  278. }
  279. }
  280. /* SDL_HINT_TOUCH_MOUSE_EVENTS: if not set, discard synthetic mouse events coming from platform layer */
  281. if (mouse->touch_mouse_events == 0) {
  282. if (mouseID == SDL_TOUCH_MOUSEID) {
  283. return 0;
  284. }
  285. }
  286. if (mouseID != SDL_TOUCH_MOUSEID && mouse->relative_mode_warp) {
  287. int center_x = 0, center_y = 0;
  288. SDL_GetWindowSize(window, &center_x, &center_y);
  289. center_x /= 2;
  290. center_y /= 2;
  291. if (x == center_x && y == center_y) {
  292. mouse->last_x = center_x;
  293. mouse->last_y = center_y;
  294. return 0;
  295. }
  296. SDL_WarpMouseInWindow(window, center_x, center_y);
  297. }
  298. if (relative) {
  299. if (mouse->relative_mode) {
  300. x = GetScaledMouseDelta(mouse->relative_speed_scale, x, &mouse->scale_accum_x);
  301. y = GetScaledMouseDelta(mouse->relative_speed_scale, y, &mouse->scale_accum_y);
  302. } else {
  303. x = GetScaledMouseDelta(mouse->normal_speed_scale, x, &mouse->scale_accum_x);
  304. y = GetScaledMouseDelta(mouse->normal_speed_scale, y, &mouse->scale_accum_y);
  305. }
  306. xrel = x;
  307. yrel = y;
  308. x = (mouse->last_x + xrel);
  309. y = (mouse->last_y + yrel);
  310. } else {
  311. xrel = x - mouse->last_x;
  312. yrel = y - mouse->last_y;
  313. }
  314. /* Ignore relative motion when first positioning the mouse */
  315. if (!mouse->has_position) {
  316. xrel = 0;
  317. yrel = 0;
  318. mouse->has_position = SDL_TRUE;
  319. } else if (!xrel && !yrel) { /* Drop events that don't change state */
  320. #ifdef DEBUG_MOUSE
  321. printf("Mouse event didn't change state - dropped!\n");
  322. #endif
  323. return 0;
  324. }
  325. /* Ignore relative motion positioning the first touch */
  326. if (mouseID == SDL_TOUCH_MOUSEID && !mouse->buttonstate) {
  327. xrel = 0;
  328. yrel = 0;
  329. }
  330. /* Update internal mouse coordinates */
  331. if (!mouse->relative_mode) {
  332. mouse->x = x;
  333. mouse->y = y;
  334. } else {
  335. mouse->x += xrel;
  336. mouse->y += yrel;
  337. }
  338. /* make sure that the pointers find themselves inside the windows,
  339. unless we have the mouse captured. */
  340. if (window && ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0)) {
  341. int x_max = 0, y_max = 0;
  342. /* !!! FIXME: shouldn't this be (window) instead of (mouse->focus)? */
  343. SDL_GetWindowSize(mouse->focus, &x_max, &y_max);
  344. --x_max;
  345. --y_max;
  346. if (mouse->x > x_max) {
  347. mouse->x = x_max;
  348. }
  349. if (mouse->x < 0) {
  350. mouse->x = 0;
  351. }
  352. if (mouse->y > y_max) {
  353. mouse->y = y_max;
  354. }
  355. if (mouse->y < 0) {
  356. mouse->y = 0;
  357. }
  358. }
  359. mouse->xdelta += xrel;
  360. mouse->ydelta += yrel;
  361. /* Move the mouse cursor, if needed */
  362. if (mouse->cursor_shown && !mouse->relative_mode &&
  363. mouse->MoveCursor && mouse->cur_cursor) {
  364. mouse->MoveCursor(mouse->cur_cursor);
  365. }
  366. /* Post the event, if desired */
  367. posted = 0;
  368. if (SDL_GetEventState(SDL_MOUSEMOTION) == SDL_ENABLE) {
  369. SDL_Event event;
  370. event.motion.type = SDL_MOUSEMOTION;
  371. event.motion.windowID = mouse->focus ? mouse->focus->id : 0;
  372. event.motion.which = mouseID;
  373. /* Set us pending (or clear during a normal mouse movement event) as having triggered */
  374. mouse->was_touch_mouse_events = (mouseID == SDL_TOUCH_MOUSEID)? SDL_TRUE : SDL_FALSE;
  375. event.motion.state = mouse->buttonstate;
  376. event.motion.x = mouse->x;
  377. event.motion.y = mouse->y;
  378. event.motion.xrel = xrel;
  379. event.motion.yrel = yrel;
  380. posted = (SDL_PushEvent(&event) > 0);
  381. }
  382. if (relative) {
  383. mouse->last_x = mouse->x;
  384. mouse->last_y = mouse->y;
  385. } else {
  386. /* Use unclamped values if we're getting events outside the window */
  387. mouse->last_x = x;
  388. mouse->last_y = y;
  389. }
  390. return posted;
  391. }
  392. static SDL_MouseClickState *GetMouseClickState(SDL_Mouse *mouse, Uint8 button)
  393. {
  394. if (button >= mouse->num_clickstates) {
  395. int i, count = button + 1;
  396. SDL_MouseClickState *clickstate = (SDL_MouseClickState *)SDL_realloc(mouse->clickstate, count * sizeof(*mouse->clickstate));
  397. if (!clickstate) {
  398. return NULL;
  399. }
  400. mouse->clickstate = clickstate;
  401. for (i = mouse->num_clickstates; i < count; ++i) {
  402. SDL_zero(mouse->clickstate[i]);
  403. }
  404. mouse->num_clickstates = count;
  405. }
  406. return &mouse->clickstate[button];
  407. }
  408. static int
  409. SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks)
  410. {
  411. SDL_Mouse *mouse = SDL_GetMouse();
  412. int posted;
  413. Uint32 type;
  414. Uint32 buttonstate = mouse->buttonstate;
  415. /* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
  416. if (mouse->mouse_touch_events) {
  417. if (mouseID != SDL_TOUCH_MOUSEID && button == SDL_BUTTON_LEFT) {
  418. if (state == SDL_PRESSED) {
  419. track_mouse_down = SDL_TRUE;
  420. } else {
  421. track_mouse_down = SDL_FALSE;
  422. }
  423. if (window) {
  424. float fx = (float)mouse->x / (float)window->w;
  425. float fy = (float)mouse->y / (float)window->h;
  426. SDL_SendTouch(SDL_MOUSE_TOUCHID, 0, window, track_mouse_down, fx, fy, 1.0f);
  427. }
  428. }
  429. }
  430. /* SDL_HINT_TOUCH_MOUSE_EVENTS: if not set, discard synthetic mouse events coming from platform layer */
  431. if (mouse->touch_mouse_events == 0) {
  432. if (mouseID == SDL_TOUCH_MOUSEID) {
  433. return 0;
  434. }
  435. }
  436. /* Figure out which event to perform */
  437. switch (state) {
  438. case SDL_PRESSED:
  439. type = SDL_MOUSEBUTTONDOWN;
  440. buttonstate |= SDL_BUTTON(button);
  441. break;
  442. case SDL_RELEASED:
  443. type = SDL_MOUSEBUTTONUP;
  444. buttonstate &= ~SDL_BUTTON(button);
  445. break;
  446. default:
  447. /* Invalid state -- bail */
  448. return 0;
  449. }
  450. /* We do this after calculating buttonstate so button presses gain focus */
  451. if (window && state == SDL_PRESSED) {
  452. SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate, SDL_TRUE);
  453. }
  454. if (buttonstate == mouse->buttonstate) {
  455. /* Ignore this event, no state change */
  456. return 0;
  457. }
  458. mouse->buttonstate = buttonstate;
  459. if (clicks < 0) {
  460. SDL_MouseClickState *clickstate = GetMouseClickState(mouse, button);
  461. if (clickstate) {
  462. if (state == SDL_PRESSED) {
  463. Uint32 now = SDL_GetTicks();
  464. if (SDL_TICKS_PASSED(now, clickstate->last_timestamp + mouse->double_click_time) ||
  465. SDL_abs(mouse->x - clickstate->last_x) > mouse->double_click_radius ||
  466. SDL_abs(mouse->y - clickstate->last_y) > mouse->double_click_radius) {
  467. clickstate->click_count = 0;
  468. }
  469. clickstate->last_timestamp = now;
  470. clickstate->last_x = mouse->x;
  471. clickstate->last_y = mouse->y;
  472. if (clickstate->click_count < 255) {
  473. ++clickstate->click_count;
  474. }
  475. }
  476. clicks = clickstate->click_count;
  477. } else {
  478. clicks = 1;
  479. }
  480. }
  481. /* Post the event, if desired */
  482. posted = 0;
  483. if (SDL_GetEventState(type) == SDL_ENABLE) {
  484. SDL_Event event;
  485. event.type = type;
  486. event.button.windowID = mouse->focus ? mouse->focus->id : 0;
  487. event.button.which = mouseID;
  488. event.button.state = state;
  489. event.button.button = button;
  490. event.button.clicks = (Uint8) SDL_min(clicks, 255);
  491. event.button.x = mouse->x;
  492. event.button.y = mouse->y;
  493. posted = (SDL_PushEvent(&event) > 0);
  494. }
  495. /* We do this after dispatching event so button releases can lose focus */
  496. if (window && state == SDL_RELEASED) {
  497. SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate, SDL_TRUE);
  498. }
  499. return posted;
  500. }
  501. int
  502. SDL_SendMouseButtonClicks(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks)
  503. {
  504. clicks = SDL_max(clicks, 0);
  505. return SDL_PrivateSendMouseButton(window, mouseID, state, button, clicks);
  506. }
  507. int
  508. SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
  509. {
  510. return SDL_PrivateSendMouseButton(window, mouseID, state, button, -1);
  511. }
  512. int
  513. SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
  514. {
  515. SDL_Mouse *mouse = SDL_GetMouse();
  516. int posted;
  517. int integral_x, integral_y;
  518. if (window) {
  519. SDL_SetMouseFocus(window);
  520. }
  521. if (x == 0.0f && y == 0.0f) {
  522. return 0;
  523. }
  524. mouse->accumulated_wheel_x += x;
  525. if (mouse->accumulated_wheel_x > 0) {
  526. integral_x = (int)SDL_floor(mouse->accumulated_wheel_x);
  527. } else if (mouse->accumulated_wheel_x < 0) {
  528. integral_x = (int)SDL_ceil(mouse->accumulated_wheel_x);
  529. } else {
  530. integral_x = 0;
  531. }
  532. mouse->accumulated_wheel_x -= integral_x;
  533. mouse->accumulated_wheel_y += y;
  534. if (mouse->accumulated_wheel_y > 0) {
  535. integral_y = (int)SDL_floor(mouse->accumulated_wheel_y);
  536. } else if (mouse->accumulated_wheel_y < 0) {
  537. integral_y = (int)SDL_ceil(mouse->accumulated_wheel_y);
  538. } else {
  539. integral_y = 0;
  540. }
  541. mouse->accumulated_wheel_y -= integral_y;
  542. /* Post the event, if desired */
  543. posted = 0;
  544. if (SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE) {
  545. SDL_Event event;
  546. event.type = SDL_MOUSEWHEEL;
  547. event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
  548. event.wheel.which = mouseID;
  549. #if 0 /* Uncomment this when it goes in for SDL 2.1 */
  550. event.wheel.preciseX = x;
  551. event.wheel.preciseY = y;
  552. #endif
  553. event.wheel.x = integral_x;
  554. event.wheel.y = integral_y;
  555. event.wheel.direction = (Uint32)direction;
  556. posted = (SDL_PushEvent(&event) > 0);
  557. }
  558. return posted;
  559. }
  560. void
  561. SDL_MouseQuit(void)
  562. {
  563. SDL_Cursor *cursor, *next;
  564. SDL_Mouse *mouse = SDL_GetMouse();
  565. if (mouse->CaptureMouse) {
  566. SDL_CaptureMouse(SDL_FALSE);
  567. }
  568. SDL_SetRelativeMouseMode(SDL_FALSE);
  569. SDL_ShowCursor(1);
  570. cursor = mouse->cursors;
  571. while (cursor) {
  572. next = cursor->next;
  573. SDL_FreeCursor(cursor);
  574. cursor = next;
  575. }
  576. mouse->cursors = NULL;
  577. mouse->cur_cursor = NULL;
  578. if (mouse->def_cursor && mouse->FreeCursor) {
  579. mouse->FreeCursor(mouse->def_cursor);
  580. mouse->def_cursor = NULL;
  581. }
  582. if (mouse->clickstate) {
  583. SDL_free(mouse->clickstate);
  584. mouse->clickstate = NULL;
  585. }
  586. SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
  587. SDL_MouseNormalSpeedScaleChanged, mouse);
  588. SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
  589. SDL_MouseRelativeSpeedScaleChanged, mouse);
  590. }
  591. Uint32
  592. SDL_GetMouseState(int *x, int *y)
  593. {
  594. SDL_Mouse *mouse = SDL_GetMouse();
  595. if (x) {
  596. *x = mouse->x;
  597. }
  598. if (y) {
  599. *y = mouse->y;
  600. }
  601. return mouse->buttonstate;
  602. }
  603. Uint32
  604. SDL_GetRelativeMouseState(int *x, int *y)
  605. {
  606. SDL_Mouse *mouse = SDL_GetMouse();
  607. if (x) {
  608. *x = mouse->xdelta;
  609. }
  610. if (y) {
  611. *y = mouse->ydelta;
  612. }
  613. mouse->xdelta = 0;
  614. mouse->ydelta = 0;
  615. return mouse->buttonstate;
  616. }
  617. Uint32
  618. SDL_GetGlobalMouseState(int *x, int *y)
  619. {
  620. SDL_Mouse *mouse = SDL_GetMouse();
  621. if (mouse->GetGlobalMouseState) {
  622. int tmpx, tmpy;
  623. /* make sure these are never NULL for the backend implementations... */
  624. if (!x) {
  625. x = &tmpx;
  626. }
  627. if (!y) {
  628. y = &tmpy;
  629. }
  630. *x = *y = 0;
  631. return mouse->GetGlobalMouseState(x, y);
  632. } else {
  633. return SDL_GetMouseState(x, y);
  634. }
  635. }
  636. void
  637. SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
  638. {
  639. SDL_Mouse *mouse = SDL_GetMouse();
  640. if (window == NULL) {
  641. window = mouse->focus;
  642. }
  643. if (window == NULL) {
  644. return;
  645. }
  646. if (mouse->WarpMouse) {
  647. mouse->WarpMouse(window, x, y);
  648. } else {
  649. SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
  650. }
  651. }
  652. int
  653. SDL_WarpMouseGlobal(int x, int y)
  654. {
  655. SDL_Mouse *mouse = SDL_GetMouse();
  656. if (mouse->WarpMouseGlobal) {
  657. return mouse->WarpMouseGlobal(x, y);
  658. }
  659. return SDL_Unsupported();
  660. }
  661. static SDL_bool
  662. ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
  663. {
  664. if (!mouse->WarpMouse) {
  665. /* Need this functionality for relative mode warp implementation */
  666. return SDL_FALSE;
  667. }
  668. return SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, SDL_FALSE);
  669. }
  670. int
  671. SDL_SetRelativeMouseMode(SDL_bool enabled)
  672. {
  673. SDL_Mouse *mouse = SDL_GetMouse();
  674. SDL_Window *focusWindow = SDL_GetKeyboardFocus();
  675. if (enabled == mouse->relative_mode) {
  676. return 0;
  677. }
  678. /* Set the relative mode */
  679. if (!enabled && mouse->relative_mode_warp) {
  680. mouse->relative_mode_warp = SDL_FALSE;
  681. } else if (enabled && ShouldUseRelativeModeWarp(mouse)) {
  682. mouse->relative_mode_warp = SDL_TRUE;
  683. } else if (!mouse->SetRelativeMouseMode || mouse->SetRelativeMouseMode(enabled) < 0) {
  684. if (enabled) {
  685. /* Fall back to warp mode if native relative mode failed */
  686. if (!mouse->WarpMouse) {
  687. return SDL_SetError("No relative mode implementation available");
  688. }
  689. mouse->relative_mode_warp = SDL_TRUE;
  690. }
  691. }
  692. mouse->relative_mode = enabled;
  693. mouse->scale_accum_x = 0.0f;
  694. mouse->scale_accum_y = 0.0f;
  695. if (enabled && focusWindow) {
  696. /* Center it in the focused window to prevent clicks from going through
  697. * to background windows.
  698. */
  699. SDL_SetMouseFocus(focusWindow);
  700. SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
  701. }
  702. if (mouse->focus) {
  703. SDL_UpdateWindowGrab(mouse->focus);
  704. /* Put the cursor back to where the application expects it */
  705. if (!enabled) {
  706. SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
  707. }
  708. }
  709. /* Flush pending mouse motion - ideally we would pump events, but that's not always safe */
  710. SDL_FlushEvent(SDL_MOUSEMOTION);
  711. /* Update cursor visibility */
  712. SDL_SetCursor(NULL);
  713. return 0;
  714. }
  715. SDL_bool
  716. SDL_GetRelativeMouseMode()
  717. {
  718. SDL_Mouse *mouse = SDL_GetMouse();
  719. return mouse->relative_mode;
  720. }
  721. int
  722. SDL_CaptureMouse(SDL_bool enabled)
  723. {
  724. SDL_Mouse *mouse = SDL_GetMouse();
  725. SDL_Window *focusWindow;
  726. SDL_bool isCaptured;
  727. if (!mouse->CaptureMouse) {
  728. return SDL_Unsupported();
  729. }
  730. focusWindow = SDL_GetKeyboardFocus();
  731. isCaptured = focusWindow && (focusWindow->flags & SDL_WINDOW_MOUSE_CAPTURE);
  732. if (isCaptured == enabled) {
  733. return 0; /* already done! */
  734. }
  735. if (enabled) {
  736. if (!focusWindow) {
  737. return SDL_SetError("No window has focus");
  738. } else if (mouse->CaptureMouse(focusWindow) == -1) {
  739. return -1; /* CaptureMouse() should call SetError */
  740. }
  741. focusWindow->flags |= SDL_WINDOW_MOUSE_CAPTURE;
  742. } else {
  743. if (mouse->CaptureMouse(NULL) == -1) {
  744. return -1; /* CaptureMouse() should call SetError */
  745. }
  746. focusWindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
  747. }
  748. return 0;
  749. }
  750. SDL_Cursor *
  751. SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
  752. int w, int h, int hot_x, int hot_y)
  753. {
  754. SDL_Surface *surface;
  755. SDL_Cursor *cursor;
  756. int x, y;
  757. Uint32 *pixel;
  758. Uint8 datab = 0, maskb = 0;
  759. const Uint32 black = 0xFF000000;
  760. const Uint32 white = 0xFFFFFFFF;
  761. const Uint32 transparent = 0x00000000;
  762. /* Make sure the width is a multiple of 8 */
  763. w = ((w + 7) & ~7);
  764. /* Create the surface from a bitmap */
  765. surface = SDL_CreateRGBSurface(0, w, h, 32,
  766. 0x00FF0000,
  767. 0x0000FF00,
  768. 0x000000FF,
  769. 0xFF000000);
  770. if (!surface) {
  771. return NULL;
  772. }
  773. for (y = 0; y < h; ++y) {
  774. pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
  775. for (x = 0; x < w; ++x) {
  776. if ((x % 8) == 0) {
  777. datab = *data++;
  778. maskb = *mask++;
  779. }
  780. if (maskb & 0x80) {
  781. *pixel++ = (datab & 0x80) ? black : white;
  782. } else {
  783. *pixel++ = (datab & 0x80) ? black : transparent;
  784. }
  785. datab <<= 1;
  786. maskb <<= 1;
  787. }
  788. }
  789. cursor = SDL_CreateColorCursor(surface, hot_x, hot_y);
  790. SDL_FreeSurface(surface);
  791. return cursor;
  792. }
  793. SDL_Cursor *
  794. SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
  795. {
  796. SDL_Mouse *mouse = SDL_GetMouse();
  797. SDL_Surface *temp = NULL;
  798. SDL_Cursor *cursor;
  799. if (!surface) {
  800. SDL_SetError("Passed NULL cursor surface");
  801. return NULL;
  802. }
  803. if (!mouse->CreateCursor) {
  804. SDL_SetError("Cursors are not currently supported");
  805. return NULL;
  806. }
  807. /* Sanity check the hot spot */
  808. if ((hot_x < 0) || (hot_y < 0) ||
  809. (hot_x >= surface->w) || (hot_y >= surface->h)) {
  810. SDL_SetError("Cursor hot spot doesn't lie within cursor");
  811. return NULL;
  812. }
  813. if (surface->format->format != SDL_PIXELFORMAT_ARGB8888) {
  814. temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
  815. if (!temp) {
  816. return NULL;
  817. }
  818. surface = temp;
  819. }
  820. cursor = mouse->CreateCursor(surface, hot_x, hot_y);
  821. if (cursor) {
  822. cursor->next = mouse->cursors;
  823. mouse->cursors = cursor;
  824. }
  825. SDL_FreeSurface(temp);
  826. return cursor;
  827. }
  828. SDL_Cursor *
  829. SDL_CreateSystemCursor(SDL_SystemCursor id)
  830. {
  831. SDL_Mouse *mouse = SDL_GetMouse();
  832. SDL_Cursor *cursor;
  833. if (!mouse->CreateSystemCursor) {
  834. SDL_SetError("CreateSystemCursor is not currently supported");
  835. return NULL;
  836. }
  837. cursor = mouse->CreateSystemCursor(id);
  838. if (cursor) {
  839. cursor->next = mouse->cursors;
  840. mouse->cursors = cursor;
  841. }
  842. return cursor;
  843. }
  844. /* SDL_SetCursor(NULL) can be used to force the cursor redraw,
  845. if this is desired for any reason. This is used when setting
  846. the video mode and when the SDL window gains the mouse focus.
  847. */
  848. void
  849. SDL_SetCursor(SDL_Cursor * cursor)
  850. {
  851. SDL_Mouse *mouse = SDL_GetMouse();
  852. /* Set the new cursor */
  853. if (cursor) {
  854. /* Make sure the cursor is still valid for this mouse */
  855. if (cursor != mouse->def_cursor) {
  856. SDL_Cursor *found;
  857. for (found = mouse->cursors; found; found = found->next) {
  858. if (found == cursor) {
  859. break;
  860. }
  861. }
  862. if (!found) {
  863. SDL_SetError("Cursor not associated with the current mouse");
  864. return;
  865. }
  866. }
  867. mouse->cur_cursor = cursor;
  868. } else {
  869. if (mouse->focus) {
  870. cursor = mouse->cur_cursor;
  871. } else {
  872. cursor = mouse->def_cursor;
  873. }
  874. }
  875. if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
  876. if (mouse->ShowCursor) {
  877. mouse->ShowCursor(cursor);
  878. }
  879. } else {
  880. if (mouse->ShowCursor) {
  881. mouse->ShowCursor(NULL);
  882. }
  883. }
  884. }
  885. SDL_Cursor *
  886. SDL_GetCursor(void)
  887. {
  888. SDL_Mouse *mouse = SDL_GetMouse();
  889. if (!mouse) {
  890. return NULL;
  891. }
  892. return mouse->cur_cursor;
  893. }
  894. SDL_Cursor *
  895. SDL_GetDefaultCursor(void)
  896. {
  897. SDL_Mouse *mouse = SDL_GetMouse();
  898. if (!mouse) {
  899. return NULL;
  900. }
  901. return mouse->def_cursor;
  902. }
  903. void
  904. SDL_FreeCursor(SDL_Cursor * cursor)
  905. {
  906. SDL_Mouse *mouse = SDL_GetMouse();
  907. SDL_Cursor *curr, *prev;
  908. if (!cursor) {
  909. return;
  910. }
  911. if (cursor == mouse->def_cursor) {
  912. return;
  913. }
  914. if (cursor == mouse->cur_cursor) {
  915. SDL_SetCursor(mouse->def_cursor);
  916. }
  917. for (prev = NULL, curr = mouse->cursors; curr;
  918. prev = curr, curr = curr->next) {
  919. if (curr == cursor) {
  920. if (prev) {
  921. prev->next = curr->next;
  922. } else {
  923. mouse->cursors = curr->next;
  924. }
  925. if (mouse->FreeCursor) {
  926. mouse->FreeCursor(curr);
  927. }
  928. return;
  929. }
  930. }
  931. }
  932. int
  933. SDL_ShowCursor(int toggle)
  934. {
  935. SDL_Mouse *mouse = SDL_GetMouse();
  936. SDL_bool shown;
  937. if (!mouse) {
  938. return 0;
  939. }
  940. shown = mouse->cursor_shown;
  941. if (toggle >= 0) {
  942. if (toggle) {
  943. mouse->cursor_shown = SDL_TRUE;
  944. } else {
  945. mouse->cursor_shown = SDL_FALSE;
  946. }
  947. if (mouse->cursor_shown != shown) {
  948. SDL_SetCursor(NULL);
  949. }
  950. }
  951. return shown;
  952. }
  953. /* vi: set ts=4 sw=4 expandtab: */