Skip to content

Latest commit

 

History

History
1061 lines (900 loc) · 27.4 KB

SDL_mouse.c

File metadata and controls

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