cocoa: report proper input IDs for mouse/touch events.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 09 Jun 2019 19:27:25 -0400
changeset 128124f015bc7cbb1
parent 12811 9b59bc963a74
child 12813 dbc1fc3b74b1
cocoa: report proper input IDs for mouse/touch events.

Otherwise, we generate incorrect mouse events for MacBook trackpads (which
are also multitouch devices), etc.

Partially fixes Bugzilla #4576.
src/video/cocoa/SDL_cocoamouse.m
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Sun Jun 09 14:08:18 2019 -0700
     1.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Sun Jun 09 19:27:25 2019 -0400
     1.3 @@ -408,7 +408,8 @@
     1.4          DLog("Motion was (%g, %g), offset to (%g, %g)", [event deltaX], [event deltaY], deltaX, deltaY);
     1.5      }
     1.6  
     1.7 -    SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)deltaX, (int)deltaY);
     1.8 +    const SDL_MouseID mouseID = ([event subtype] == NSEventSubtypeTouch) ? SDL_TOUCH_MOUSEID : mouse->mouseID;
     1.9 +    SDL_SendMouseMotion(mouse->focus, mouseID, 1, (int)deltaX, (int)deltaY);
    1.10  }
    1.11  
    1.12  void
    1.13 @@ -436,7 +437,8 @@
    1.14      } else if (y < 0) {
    1.15          y = SDL_floor(y);
    1.16      }
    1.17 -    SDL_SendMouseWheel(window, mouse->mouseID, x, y, direction);
    1.18 +    const SDL_MouseID mouseID = ([event subtype] == NSEventSubtypeTouch) ? SDL_TOUCH_MOUSEID : mouse->mouseID;
    1.19 +    SDL_SendMouseWheel(window, mouseID, x, y, direction);
    1.20  }
    1.21  
    1.22  void
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Sun Jun 09 14:08:18 2019 -0700
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Sun Jun 09 19:27:25 2019 -0400
     2.3 @@ -622,7 +622,7 @@
     2.4          y = (int)(window->h - point.y);
     2.5  
     2.6          if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
     2.7 -            SDL_SendMouseMotion(window, 0, 0, x, y);
     2.8 +            SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
     2.9          }
    2.10      }
    2.11  
    2.12 @@ -932,7 +932,16 @@
    2.13      }
    2.14  
    2.15      clicks = (int) [theEvent clickCount];
    2.16 -    SDL_SendMouseButtonClicks(_data->window, 0, SDL_PRESSED, button, clicks);
    2.17 +
    2.18 +    SDL_MouseID mouseID;
    2.19 +    if ([theEvent subtype] == NSEventSubtypeTouch) {
    2.20 +        mouseID = SDL_TOUCH_MOUSEID;
    2.21 +    } else {
    2.22 +        SDL_Mouse *mouse = SDL_GetMouse();
    2.23 +        mouseID = mouse ? mouse->mouseID : 0;
    2.24 +    }
    2.25 +
    2.26 +    SDL_SendMouseButtonClicks(_data->window, mouseID, SDL_PRESSED, button, clicks);
    2.27  }
    2.28  
    2.29  - (void)rightMouseDown:(NSEvent *)theEvent
    2.30 @@ -976,7 +985,16 @@
    2.31      }
    2.32  
    2.33      clicks = (int) [theEvent clickCount];
    2.34 -    SDL_SendMouseButtonClicks(_data->window, 0, SDL_RELEASED, button, clicks);
    2.35 +
    2.36 +    SDL_MouseID mouseID;
    2.37 +    if ([theEvent subtype] == NSEventSubtypeTouch) {
    2.38 +        mouseID = SDL_TOUCH_MOUSEID;
    2.39 +    } else {
    2.40 +        SDL_Mouse *mouse = SDL_GetMouse();
    2.41 +        mouseID = mouse ? mouse->mouseID : 0;
    2.42 +    }
    2.43 +
    2.44 +    SDL_SendMouseButtonClicks(_data->window, mouseID, SDL_RELEASED, button, clicks);
    2.45  }
    2.46  
    2.47  - (void)rightMouseUp:(NSEvent *)theEvent
    2.48 @@ -1039,7 +1057,9 @@
    2.49  #endif
    2.50          }
    2.51      }
    2.52 -    SDL_SendMouseMotion(window, 0, 0, x, y);
    2.53 +
    2.54 +    const SDL_MouseID mouseID = ([theEvent subtype] == NSEventSubtypeTouch) ? SDL_TOUCH_MOUSEID : mouse->mouseID;
    2.55 +    SDL_SendMouseMotion(window, mouseID, 0, x, y);
    2.56  }
    2.57  
    2.58  - (void)mouseDragged:(NSEvent *)theEvent
    2.59 @@ -1073,7 +1093,8 @@
    2.60          }
    2.61      }
    2.62      if (existingTouchCount == 0) {
    2.63 -        SDL_TouchID touchID = (SDL_TouchID)(intptr_t)[[touches anyObject] device];
    2.64 +        const BOOL ismouse = ([theEvent subtype] == NSEventSubtypeMouseEvent);
    2.65 +        const SDL_TouchID touchID = ismouse ? SDL_MOUSE_TOUCHID : (SDL_TouchID)(intptr_t)[[touches anyObject] device];
    2.66          int numFingers = SDL_GetNumTouchFingers(touchID);
    2.67          DLog("Reset Lost Fingers: %d", numFingers);
    2.68          for (--numFingers; numFingers >= 0; --numFingers) {
    2.69 @@ -1103,10 +1124,11 @@
    2.70  
    2.71  - (void)handleTouches:(NSTouchPhase) phase withEvent:(NSEvent *) theEvent
    2.72  {
    2.73 +    const BOOL ismouse = ([theEvent subtype] == NSEventSubtypeMouseEvent);
    2.74      NSSet *touches = [theEvent touchesMatchingPhase:phase inView:nil];
    2.75  
    2.76      for (NSTouch *touch in touches) {
    2.77 -        const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
    2.78 +        const SDL_TouchID touchId = ismouse ? SDL_MOUSE_TOUCHID : (SDL_TouchID)(intptr_t)[touch device];
    2.79          SDL_TouchDeviceType devtype = SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE;
    2.80  
    2.81  #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101202 /* Added in the 10.12.2 SDK. */