1
0

SDL_mouse.c 34 KB

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