Skip to content

Commit e841b06

Browse files
committed
cocoa: Another attempt at mouse vs touch support.
This time, we make anything we think is a MacBook trackpad report its touches as SDL_MOUSE_TOUCHID, even though they're not _actually_ synthesized events, and let all mouse input--even if the OS synthesized it from a multitouch trackpad on our behalf--look like physical input. This is backwards from reality, but produces the results most apps will expect. Note that if you have a real touch device that doesn't appear to be the trackpad, it'll produce real touch events with unique device ids, so it's not a total loss here, but also note that the way we decide if it was the trackpad is an imperfect heuristic; it happens to work out right now, but it's not impossible that a real touchscreen could come to the Mac at some point and (incorrectly?) call it a "mouse" input, etc. But for now, good enough. Fixes Bugzilla #4690.
1 parent 680e793 commit e841b06

File tree

4 files changed

+13
-63
lines changed

4 files changed

+13
-63
lines changed

src/events/SDL_mouse.c

-2
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,11 @@ SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relativ
383383
mouse->has_position = SDL_TRUE;
384384
}
385385

386-
#ifndef __MACOSX__ /* all your trackpad input would lack relative motion when not dragging in this case. */
387386
/* Ignore relative motion positioning the first touch */
388387
if (mouseID == SDL_TOUCH_MOUSEID && !mouse->buttonstate) {
389388
xrel = 0;
390389
yrel = 0;
391390
}
392-
#endif
393391

394392
/* Update internal mouse coordinates */
395393
if (!mouse->relative_mode) {

src/events/SDL_touch.c

-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ static SDL_Touch **SDL_touchDevices = NULL;
3333

3434
/* for mapping touch events to mice */
3535

36-
#ifndef __MACOSX__ /* don't generate mouse events from touch on macOS, the OS handles that. */
3736
#define SYNTHESIZE_TOUCH_TO_MOUSE 1
38-
#else
39-
#define SYNTHESIZE_TOUCH_TO_MOUSE 0
40-
#endif
4137

4238
#if SYNTHESIZE_TOUCH_TO_MOUSE
4339
static SDL_bool finger_touching = SDL_FALSE;

src/video/cocoa/SDL_cocoamouse.m

-22
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
#define DLog(...) do { } while (0)
3939
#endif
4040

41-
#define TRACKPAD_REPORTS_TOUCH_MOUSEID 0
42-
4341
@implementation NSCursor (InvisibleCursor)
4442
+ (NSCursor *)invisibleCursor
4543
{
@@ -381,16 +379,6 @@ + (NSCursor *)invisibleCursor
381379
}
382380

383381
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
384-
#if TRACKPAD_REPORTS_TOUCH_MOUSEID
385-
if ([event subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
386-
if (mouse->touch_mouse_events) {
387-
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
388-
} else {
389-
return; /* no hint set, drop this one. */
390-
}
391-
}
392-
#endif
393-
394382
const SDL_bool seenWarp = driverdata->seenWarp;
395383
driverdata->seenWarp = NO;
396384

@@ -436,16 +424,6 @@ + (NSCursor *)invisibleCursor
436424
}
437425

438426
SDL_MouseID mouseID = mouse->mouseID;
439-
#if TRACKPAD_REPORTS_TOUCH_MOUSEID
440-
if ([event subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
441-
if (mouse->touch_mouse_events) {
442-
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
443-
} else {
444-
return; /* no hint set, drop this one. */
445-
}
446-
}
447-
#endif
448-
449427
CGFloat x = -[event deltaX];
450428
CGFloat y = [event deltaY];
451429
SDL_MouseWheelDirection direction = SDL_MOUSEWHEEL_NORMAL;

src/video/cocoa/SDL_cocoawindow.m

+13-35
Original file line numberDiff line numberDiff line change
@@ -918,20 +918,10 @@ - (void)mouseDown:(NSEvent *)theEvent
918918
return;
919919
}
920920

921-
SDL_MouseID mouseID = mouse->mouseID;
921+
const SDL_MouseID mouseID = mouse->mouseID;
922922
int button;
923923
int clicks;
924924

925-
#if TRACKPAD_REPORTS_TOUCH_MOUSEID
926-
if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
927-
if (mouse->touch_mouse_events) {
928-
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
929-
} else {
930-
return; /* no hint set, drop this one. */
931-
}
932-
}
933-
#endif
934-
935925
/* Ignore events that aren't inside the client area (i.e. title bar.) */
936926
if ([theEvent window]) {
937927
NSRect windowRect = [[[theEvent window] contentView] frame];
@@ -989,20 +979,10 @@ - (void)mouseUp:(NSEvent *)theEvent
989979
return;
990980
}
991981

992-
SDL_MouseID mouseID = mouse->mouseID;
982+
const SDL_MouseID mouseID = mouse->mouseID;
993983
int button;
994984
int clicks;
995985

996-
#if TRACKPAD_REPORTS_TOUCH_MOUSEID
997-
if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
998-
if (mouse->touch_mouse_events) {
999-
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
1000-
} else {
1001-
return; /* no hint set, drop this one. */
1002-
}
1003-
}
1004-
#endif
1005-
1006986
if ([self processHitTest:theEvent]) {
1007987
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
1008988
return; /* stopped dragging, drop event. */
@@ -1050,21 +1030,11 @@ - (void)mouseMoved:(NSEvent *)theEvent
10501030
return;
10511031
}
10521032

1053-
SDL_MouseID mouseID = mouse->mouseID;
1033+
const SDL_MouseID mouseID = mouse->mouseID;
10541034
SDL_Window *window = _data->window;
10551035
NSPoint point;
10561036
int x, y;
10571037

1058-
#if TRACKPAD_REPORTS_TOUCH_MOUSEID
1059-
if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
1060-
if (mouse->touch_mouse_events) {
1061-
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
1062-
} else {
1063-
return; /* no hint set, drop this one. */
1064-
}
1065-
}
1066-
#endif
1067-
10681038
if ([self processHitTest:theEvent]) {
10691039
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
10701040
return; /* dragging, drop event. */
@@ -1134,7 +1104,12 @@ - (void)scrollWheel:(NSEvent *)theEvent
11341104

11351105
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
11361106
{
1107+
/* probably a MacBook trackpad; make this look like a synthesized event.
1108+
This is backwards from reality, but better matches user expectations. */
1109+
const BOOL istrackpad = ([theEvent subtype] == NSEventSubtypeMouseEvent);
1110+
11371111
NSSet *touches = [theEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil];
1112+
const SDL_TouchID touchID = istrackpad ? SDL_MOUSE_TOUCHID : (SDL_TouchID)(intptr_t)[[touches anyObject] device];
11381113
int existingTouchCount = 0;
11391114

11401115
for (NSTouch* touch in touches) {
@@ -1143,7 +1118,6 @@ - (void)touchesBeganWithEvent:(NSEvent *) theEvent
11431118
}
11441119
}
11451120
if (existingTouchCount == 0) {
1146-
const SDL_TouchID touchID = (SDL_TouchID)(intptr_t)[[touches anyObject] device];
11471121
int numFingers = SDL_GetNumTouchFingers(touchID);
11481122
DLog("Reset Lost Fingers: %d", numFingers);
11491123
for (--numFingers; numFingers >= 0; --numFingers) {
@@ -1175,8 +1149,12 @@ - (void)handleTouches:(NSTouchPhase) phase withEvent:(NSEvent *) theEvent
11751149
{
11761150
NSSet *touches = [theEvent touchesMatchingPhase:phase inView:nil];
11771151

1152+
/* probably a MacBook trackpad; make this look like a synthesized event.
1153+
This is backwards from reality, but better matches user expectations. */
1154+
const BOOL istrackpad = ([theEvent subtype] == NSEventSubtypeMouseEvent);
1155+
11781156
for (NSTouch *touch in touches) {
1179-
const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
1157+
const SDL_TouchID touchId = istrackpad ? SDL_MOUSE_TOUCHID : (SDL_TouchID)(intptr_t)[touch device];
11801158
SDL_TouchDeviceType devtype = SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE;
11811159

11821160
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101202 /* Added in the 10.12.2 SDK. */

0 commit comments

Comments
 (0)