Skip to content

Commit

Permalink
cocoa: Another attempt at synthesized mouse/touch events.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jun 14, 2019
1 parent 2945746 commit d9a2eff
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/events/SDL_mouse.c
Expand Up @@ -103,11 +103,7 @@ SDL_TouchMouseEventsChanged(void *userdata, const char *name, const char *oldVal
if (hint && (*hint == '0' || SDL_strcasecmp(hint, "false") == 0)) {
mouse->touch_mouse_events = SDL_FALSE;
} else {
#if defined(__MACOSX__) /* macOS synthesizes its own events for this. */
mouse->touch_mouse_events = SDL_FALSE;
#else
mouse->touch_mouse_events = SDL_TRUE;
#endif
}
}

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

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

/* Update internal mouse coordinates */
if (!mouse->relative_mode) {
Expand Down
13 changes: 13 additions & 0 deletions src/events/SDL_touch.c
Expand Up @@ -32,9 +32,18 @@ static int SDL_num_touch = 0;
static SDL_Touch **SDL_touchDevices = NULL;

/* for mapping touch events to mice */

#ifndef __MACOSX__ /* don't generate mouse events from touch on macOS, the OS handles that. */
#define SYNTHESIZE_TOUCH_TO_MOUSE 1
#else
#define SYNTHESIZE_TOUCH_TO_MOUSE 0
#endif

#if SYNTHESIZE_TOUCH_TO_MOUSE
static SDL_bool finger_touching = SDL_FALSE;
static SDL_FingerID track_fingerid;
static SDL_TouchID track_touchid;
#endif

/* Public functions */
int
Expand Down Expand Up @@ -245,6 +254,7 @@ SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
return -1;
}

#if SYNTHESIZE_TOUCH_TO_MOUSE
/* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */
{
SDL_Mouse *mouse = SDL_GetMouse();
Expand Down Expand Up @@ -284,6 +294,7 @@ SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
}
}
}
#endif

finger = SDL_GetFinger(touch, fingerid);
if (down) {
Expand Down Expand Up @@ -349,6 +360,7 @@ SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
return -1;
}

#if SYNTHESIZE_TOUCH_TO_MOUSE
/* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */
{
SDL_Mouse *mouse = SDL_GetMouse();
Expand All @@ -369,6 +381,7 @@ SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
}
}
}
#endif

finger = SDL_GetFinger(touch,fingerid);
if (!finger) {
Expand Down
21 changes: 19 additions & 2 deletions src/video/cocoa/SDL_cocoamouse.m
Expand Up @@ -375,6 +375,15 @@ + (NSCursor *)invisibleCursor
return; /* can happen when returning from fullscreen Space on shutdown */
}

SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
if ([event subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
if (mouse->touch_mouse_events) {
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
} else {
return; /* no hint set, drop this one. */
}
}

const SDL_bool seenWarp = driverdata->seenWarp;
driverdata->seenWarp = NO;

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

SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)deltaX, (int)deltaY);
SDL_SendMouseMotion(mouse->focus, mouseID, 1, (int)deltaX, (int)deltaY);
}

void
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
{
SDL_Mouse *mouse = SDL_GetMouse();
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
if ([event subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
if (mouse->touch_mouse_events) {
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
} else {
return; /* no hint set, drop this one. */
}
}

CGFloat x = -[event deltaX];
CGFloat y = [event deltaY];
Expand All @@ -437,7 +454,7 @@ + (NSCursor *)invisibleCursor
y = SDL_floor(y);
}

SDL_SendMouseWheel(window, mouse->mouseID, x, y, direction);
SDL_SendMouseWheel(window, mouseID, x, y, direction);
}

void
Expand Down
35 changes: 30 additions & 5 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -893,9 +893,19 @@ - (BOOL)processHitTest:(NSEvent *)theEvent

- (void)mouseDown:(NSEvent *)theEvent
{
const SDL_Mouse *mouse = SDL_GetMouse();
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
int button;
int clicks;

if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
if (mouse->touch_mouse_events) {
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
} else {
return; /* no hint set, drop this one. */
}
}

/* Ignore events that aren't inside the client area (i.e. title bar.) */
if ([theEvent window]) {
NSRect windowRect = [[[theEvent window] contentView] frame];
Expand Down Expand Up @@ -933,8 +943,6 @@ - (void)mouseDown:(NSEvent *)theEvent

clicks = (int) [theEvent clickCount];

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

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

- (void)mouseUp:(NSEvent *)theEvent
{
const SDL_Mouse *mouse = SDL_GetMouse();
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
int button;
int clicks;

if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
if (mouse->touch_mouse_events) {
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
} else {
return; /* no hint set, drop this one. */
}
}

if ([self processHitTest:theEvent]) {
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
return; /* stopped dragging, drop event. */
Expand Down Expand Up @@ -980,8 +998,6 @@ - (void)mouseUp:(NSEvent *)theEvent

clicks = (int) [theEvent clickCount];

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

Expand All @@ -998,10 +1014,19 @@ - (void)otherMouseUp:(NSEvent *)theEvent
- (void)mouseMoved:(NSEvent *)theEvent
{
SDL_Mouse *mouse = SDL_GetMouse();
SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
SDL_Window *window = _data->window;
NSPoint point;
int x, y;

if ([theEvent subtype] == NSEventSubtypeTouch) { /* this is a synthetic from the OS */
if (mouse->touch_mouse_events) {
mouseID = SDL_TOUCH_MOUSEID; /* Hint is set */
} else {
return; /* no hint set, drop this one. */
}
}

if ([self processHitTest:theEvent]) {
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
return; /* dragging, drop event. */
Expand Down Expand Up @@ -1046,7 +1071,7 @@ - (void)mouseMoved:(NSEvent *)theEvent
}
}

SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
SDL_SendMouseMotion(window, mouseID, 0, x, y);
}

- (void)mouseDragged:(NSEvent *)theEvent
Expand Down

0 comments on commit d9a2eff

Please sign in to comment.