Skip to content

Commit d9a2eff

Browse files
committed
cocoa: Another attempt at synthesized mouse/touch events.
1 parent 2945746 commit d9a2eff

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

src/events/SDL_mouse.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ SDL_TouchMouseEventsChanged(void *userdata, const char *name, const char *oldVal
103103
if (hint && (*hint == '0' || SDL_strcasecmp(hint, "false") == 0)) {
104104
mouse->touch_mouse_events = SDL_FALSE;
105105
} else {
106-
#if defined(__MACOSX__) /* macOS synthesizes its own events for this. */
107-
mouse->touch_mouse_events = SDL_FALSE;
108-
#else
109106
mouse->touch_mouse_events = SDL_TRUE;
110-
#endif
111107
}
112108
}
113109

@@ -387,11 +383,13 @@ SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relativ
387383
mouse->has_position = SDL_TRUE;
388384
}
389385

386+
#ifndef __MACOSX__ /* all your trackpad input would lack relative motion when not dragging in this case. */
390387
/* Ignore relative motion positioning the first touch */
391388
if (mouseID == SDL_TOUCH_MOUSEID && !mouse->buttonstate) {
392389
xrel = 0;
393390
yrel = 0;
394391
}
392+
#endif
395393

396394
/* Update internal mouse coordinates */
397395
if (!mouse->relative_mode) {

src/events/SDL_touch.c

+13
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,18 @@ static int SDL_num_touch = 0;
3232
static SDL_Touch **SDL_touchDevices = NULL;
3333

3434
/* for mapping touch events to mice */
35+
36+
#ifndef __MACOSX__ /* don't generate mouse events from touch on macOS, the OS handles that. */
37+
#define SYNTHESIZE_TOUCH_TO_MOUSE 1
38+
#else
39+
#define SYNTHESIZE_TOUCH_TO_MOUSE 0
40+
#endif
41+
42+
#if SYNTHESIZE_TOUCH_TO_MOUSE
3543
static SDL_bool finger_touching = SDL_FALSE;
3644
static SDL_FingerID track_fingerid;
3745
static SDL_TouchID track_touchid;
46+
#endif
3847

3948
/* Public functions */
4049
int
@@ -245,6 +254,7 @@ SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
245254
return -1;
246255
}
247256

257+
#if SYNTHESIZE_TOUCH_TO_MOUSE
248258
/* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */
249259
{
250260
SDL_Mouse *mouse = SDL_GetMouse();
@@ -284,6 +294,7 @@ SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
284294
}
285295
}
286296
}
297+
#endif
287298

288299
finger = SDL_GetFinger(touch, fingerid);
289300
if (down) {
@@ -349,6 +360,7 @@ SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
349360
return -1;
350361
}
351362

363+
#if SYNTHESIZE_TOUCH_TO_MOUSE
352364
/* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */
353365
{
354366
SDL_Mouse *mouse = SDL_GetMouse();
@@ -369,6 +381,7 @@ SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
369381
}
370382
}
371383
}
384+
#endif
372385

373386
finger = SDL_GetFinger(touch,fingerid);
374387
if (!finger) {

src/video/cocoa/SDL_cocoamouse.m

+19-2
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@ + (NSCursor *)invisibleCursor
375375
return; /* can happen when returning from fullscreen Space on shutdown */
376376
}
377377

378+
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
379+
if ([event subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
380+
if (mouse->touch_mouse_events) {
381+
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
382+
} else {
383+
return; /* no hint set, drop this one. */
384+
}
385+
}
386+
378387
const SDL_bool seenWarp = driverdata->seenWarp;
379388
driverdata->seenWarp = NO;
380389

@@ -408,13 +417,21 @@ + (NSCursor *)invisibleCursor
408417
DLog("Motion was (%g, %g), offset to (%g, %g)", [event deltaX], [event deltaY], deltaX, deltaY);
409418
}
410419

411-
SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)deltaX, (int)deltaY);
420+
SDL_SendMouseMotion(mouse->focus, mouseID, 1, (int)deltaX, (int)deltaY);
412421
}
413422

414423
void
415424
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
416425
{
417426
SDL_Mouse *mouse = SDL_GetMouse();
427+
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
428+
if ([event subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
429+
if (mouse->touch_mouse_events) {
430+
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
431+
} else {
432+
return; /* no hint set, drop this one. */
433+
}
434+
}
418435

419436
CGFloat x = -[event deltaX];
420437
CGFloat y = [event deltaY];
@@ -437,7 +454,7 @@ + (NSCursor *)invisibleCursor
437454
y = SDL_floor(y);
438455
}
439456

440-
SDL_SendMouseWheel(window, mouse->mouseID, x, y, direction);
457+
SDL_SendMouseWheel(window, mouseID, x, y, direction);
441458
}
442459

443460
void

src/video/cocoa/SDL_cocoawindow.m

+30-5
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,19 @@ - (BOOL)processHitTest:(NSEvent *)theEvent
893893

894894
- (void)mouseDown:(NSEvent *)theEvent
895895
{
896+
const SDL_Mouse *mouse = SDL_GetMouse();
897+
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
896898
int button;
897899
int clicks;
898900

901+
if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
902+
if (mouse->touch_mouse_events) {
903+
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
904+
} else {
905+
return; /* no hint set, drop this one. */
906+
}
907+
}
908+
899909
/* Ignore events that aren't inside the client area (i.e. title bar.) */
900910
if ([theEvent window]) {
901911
NSRect windowRect = [[[theEvent window] contentView] frame];
@@ -933,8 +943,6 @@ - (void)mouseDown:(NSEvent *)theEvent
933943

934944
clicks = (int) [theEvent clickCount];
935945

936-
const SDL_Mouse *mouse = SDL_GetMouse();
937-
const SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
938946
SDL_SendMouseButtonClicks(_data->window, mouseID, SDL_PRESSED, button, clicks);
939947
}
940948

@@ -950,9 +958,19 @@ - (void)otherMouseDown:(NSEvent *)theEvent
950958

951959
- (void)mouseUp:(NSEvent *)theEvent
952960
{
961+
const SDL_Mouse *mouse = SDL_GetMouse();
962+
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
953963
int button;
954964
int clicks;
955965

966+
if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
967+
if (mouse->touch_mouse_events) {
968+
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
969+
} else {
970+
return; /* no hint set, drop this one. */
971+
}
972+
}
973+
956974
if ([self processHitTest:theEvent]) {
957975
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
958976
return; /* stopped dragging, drop event. */
@@ -980,8 +998,6 @@ - (void)mouseUp:(NSEvent *)theEvent
980998

981999
clicks = (int) [theEvent clickCount];
9821000

983-
const SDL_Mouse *mouse = SDL_GetMouse();
984-
const SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
9851001
SDL_SendMouseButtonClicks(_data->window, mouseID, SDL_RELEASED, button, clicks);
9861002
}
9871003

@@ -998,10 +1014,19 @@ - (void)otherMouseUp:(NSEvent *)theEvent
9981014
- (void)mouseMoved:(NSEvent *)theEvent
9991015
{
10001016
SDL_Mouse *mouse = SDL_GetMouse();
1017+
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
10011018
SDL_Window *window = _data->window;
10021019
NSPoint point;
10031020
int x, y;
10041021

1022+
if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
1023+
if (mouse->touch_mouse_events) {
1024+
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
1025+
} else {
1026+
return; /* no hint set, drop this one. */
1027+
}
1028+
}
1029+
10051030
if ([self processHitTest:theEvent]) {
10061031
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
10071032
return; /* dragging, drop event. */
@@ -1046,7 +1071,7 @@ - (void)mouseMoved:(NSEvent *)theEvent
10461071
}
10471072
}
10481073

1049-
SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
1074+
SDL_SendMouseMotion(window, mouseID, 0, x, y);
10501075
}
10511076

10521077
- (void)mouseDragged:(NSEvent *)theEvent

0 commit comments

Comments
 (0)