cocoa: Another attempt at synthesized mouse/touch events.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 13 Jun 2019 21:31:03 -0400
changeset 12855ac0c65535817
parent 12854 d7d64c1a3969
child 12856 cfcd6e8aee7f
cocoa: Another attempt at synthesized mouse/touch events.
src/events/SDL_mouse.c
src/events/SDL_touch.c
src/video/cocoa/SDL_cocoamouse.m
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/events/SDL_mouse.c	Thu Jun 13 01:57:13 2019 -0400
     1.2 +++ b/src/events/SDL_mouse.c	Thu Jun 13 21:31:03 2019 -0400
     1.3 @@ -103,11 +103,7 @@
     1.4      if (hint && (*hint == '0' || SDL_strcasecmp(hint, "false") == 0)) {
     1.5          mouse->touch_mouse_events = SDL_FALSE;
     1.6      } else {
     1.7 -#if defined(__MACOSX__)  /* macOS synthesizes its own events for this. */
     1.8 -        mouse->touch_mouse_events = SDL_FALSE;
     1.9 -#else
    1.10          mouse->touch_mouse_events = SDL_TRUE;
    1.11 -#endif
    1.12      }
    1.13  }
    1.14  
    1.15 @@ -387,11 +383,13 @@
    1.16          mouse->has_position = SDL_TRUE;
    1.17      }
    1.18  
    1.19 +#ifndef __MACOSX__  /* all your trackpad input would lack relative motion when not dragging in this case. */
    1.20      /* Ignore relative motion positioning the first touch */
    1.21      if (mouseID == SDL_TOUCH_MOUSEID && !mouse->buttonstate) {
    1.22          xrel = 0;
    1.23          yrel = 0;
    1.24      }
    1.25 +#endif
    1.26  
    1.27      /* Update internal mouse coordinates */
    1.28      if (!mouse->relative_mode) {
     2.1 --- a/src/events/SDL_touch.c	Thu Jun 13 01:57:13 2019 -0400
     2.2 +++ b/src/events/SDL_touch.c	Thu Jun 13 21:31:03 2019 -0400
     2.3 @@ -32,9 +32,18 @@
     2.4  static SDL_Touch **SDL_touchDevices = NULL;
     2.5  
     2.6  /* for mapping touch events to mice */
     2.7 +
     2.8 +#ifndef __MACOSX__  /* don't generate mouse events from touch on macOS, the OS handles that. */
     2.9 +#define SYNTHESIZE_TOUCH_TO_MOUSE 1
    2.10 +#else
    2.11 +#define SYNTHESIZE_TOUCH_TO_MOUSE 0
    2.12 +#endif
    2.13 +
    2.14 +#if SYNTHESIZE_TOUCH_TO_MOUSE
    2.15  static SDL_bool finger_touching = SDL_FALSE;
    2.16  static SDL_FingerID track_fingerid;
    2.17  static SDL_TouchID  track_touchid;
    2.18 +#endif
    2.19  
    2.20  /* Public functions */
    2.21  int
    2.22 @@ -245,6 +254,7 @@
    2.23          return -1;
    2.24      }
    2.25  
    2.26 +#if SYNTHESIZE_TOUCH_TO_MOUSE
    2.27      /* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */
    2.28      {
    2.29          SDL_Mouse *mouse = SDL_GetMouse();
    2.30 @@ -284,6 +294,7 @@
    2.31              }
    2.32          }
    2.33      }
    2.34 +#endif
    2.35  
    2.36      finger = SDL_GetFinger(touch, fingerid);
    2.37      if (down) {
    2.38 @@ -349,6 +360,7 @@
    2.39          return -1;
    2.40      }
    2.41  
    2.42 +#if SYNTHESIZE_TOUCH_TO_MOUSE
    2.43      /* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */
    2.44      {
    2.45          SDL_Mouse *mouse = SDL_GetMouse();
    2.46 @@ -369,6 +381,7 @@
    2.47              }
    2.48          }
    2.49      }
    2.50 +#endif
    2.51  
    2.52      finger = SDL_GetFinger(touch,fingerid);
    2.53      if (!finger) {
     3.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Thu Jun 13 01:57:13 2019 -0400
     3.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Thu Jun 13 21:31:03 2019 -0400
     3.3 @@ -375,6 +375,15 @@
     3.4          return;  /* can happen when returning from fullscreen Space on shutdown */
     3.5      }
     3.6  
     3.7 +    SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
     3.8 +    if ([event subtype] == NSEventSubtypeTouch) {  /* this is a synthetic from the OS */
     3.9 +        if (mouse->touch_mouse_events) {
    3.10 +            mouseID = SDL_TOUCH_MOUSEID;   /* Hint is set */
    3.11 +        } else {
    3.12 +            return;  /* no hint set, drop this one. */
    3.13 +        }
    3.14 +    }
    3.15 +
    3.16      const SDL_bool seenWarp = driverdata->seenWarp;
    3.17      driverdata->seenWarp = NO;
    3.18  
    3.19 @@ -408,13 +417,21 @@
    3.20          DLog("Motion was (%g, %g), offset to (%g, %g)", [event deltaX], [event deltaY], deltaX, deltaY);
    3.21      }
    3.22  
    3.23 -    SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)deltaX, (int)deltaY);
    3.24 +    SDL_SendMouseMotion(mouse->focus, mouseID, 1, (int)deltaX, (int)deltaY);
    3.25  }
    3.26  
    3.27  void
    3.28  Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
    3.29  {
    3.30      SDL_Mouse *mouse = SDL_GetMouse();
    3.31 +    SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
    3.32 +    if ([event subtype] == NSEventSubtypeTouch) {  /* this is a synthetic from the OS */
    3.33 +        if (mouse->touch_mouse_events) {
    3.34 +            mouseID = SDL_TOUCH_MOUSEID;   /* Hint is set */
    3.35 +        } else {
    3.36 +            return;  /* no hint set, drop this one. */
    3.37 +        }
    3.38 +    }
    3.39  
    3.40      CGFloat x = -[event deltaX];
    3.41      CGFloat y = [event deltaY];
    3.42 @@ -437,7 +454,7 @@
    3.43          y = SDL_floor(y);
    3.44      }
    3.45  
    3.46 -    SDL_SendMouseWheel(window, mouse->mouseID, x, y, direction);
    3.47 +    SDL_SendMouseWheel(window, mouseID, x, y, direction);
    3.48  }
    3.49  
    3.50  void
     4.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Thu Jun 13 01:57:13 2019 -0400
     4.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Thu Jun 13 21:31:03 2019 -0400
     4.3 @@ -893,9 +893,19 @@
     4.4  
     4.5  - (void)mouseDown:(NSEvent *)theEvent
     4.6  {
     4.7 +    const SDL_Mouse *mouse = SDL_GetMouse();
     4.8 +    SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
     4.9      int button;
    4.10      int clicks;
    4.11  
    4.12 +    if ([theEvent subtype] == NSEventSubtypeTouch) {  /* this is a synthetic from the OS */
    4.13 +        if (mouse->touch_mouse_events) {
    4.14 +            mouseID = SDL_TOUCH_MOUSEID;   /* Hint is set */
    4.15 +        } else {
    4.16 +            return;  /* no hint set, drop this one. */
    4.17 +        }
    4.18 +    }
    4.19 +
    4.20      /* Ignore events that aren't inside the client area (i.e. title bar.) */
    4.21      if ([theEvent window]) {
    4.22          NSRect windowRect = [[[theEvent window] contentView] frame];
    4.23 @@ -933,8 +943,6 @@
    4.24  
    4.25      clicks = (int) [theEvent clickCount];
    4.26  
    4.27 -    const SDL_Mouse *mouse = SDL_GetMouse();
    4.28 -    const SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
    4.29      SDL_SendMouseButtonClicks(_data->window, mouseID, SDL_PRESSED, button, clicks);
    4.30  }
    4.31  
    4.32 @@ -950,9 +958,19 @@
    4.33  
    4.34  - (void)mouseUp:(NSEvent *)theEvent
    4.35  {
    4.36 +    const SDL_Mouse *mouse = SDL_GetMouse();
    4.37 +    SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
    4.38      int button;
    4.39      int clicks;
    4.40  
    4.41 +    if ([theEvent subtype] == NSEventSubtypeTouch) {  /* this is a synthetic from the OS */
    4.42 +        if (mouse->touch_mouse_events) {
    4.43 +            mouseID = SDL_TOUCH_MOUSEID;   /* Hint is set */
    4.44 +        } else {
    4.45 +            return;  /* no hint set, drop this one. */
    4.46 +        }
    4.47 +    }
    4.48 +
    4.49      if ([self processHitTest:theEvent]) {
    4.50          SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
    4.51          return;  /* stopped dragging, drop event. */
    4.52 @@ -980,8 +998,6 @@
    4.53  
    4.54      clicks = (int) [theEvent clickCount];
    4.55  
    4.56 -    const SDL_Mouse *mouse = SDL_GetMouse();
    4.57 -    const SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
    4.58      SDL_SendMouseButtonClicks(_data->window, mouseID, SDL_RELEASED, button, clicks);
    4.59  }
    4.60  
    4.61 @@ -998,10 +1014,19 @@
    4.62  - (void)mouseMoved:(NSEvent *)theEvent
    4.63  {
    4.64      SDL_Mouse *mouse = SDL_GetMouse();
    4.65 +    SDL_MouseID mouseID = mouse ? mouse->mouseID : 0;
    4.66      SDL_Window *window = _data->window;
    4.67      NSPoint point;
    4.68      int x, y;
    4.69  
    4.70 +    if ([theEvent subtype] == NSEventSubtypeTouch) {  /* this is a synthetic from the OS */
    4.71 +        if (mouse->touch_mouse_events) {
    4.72 +            mouseID = SDL_TOUCH_MOUSEID;   /* Hint is set */
    4.73 +        } else {
    4.74 +            return;  /* no hint set, drop this one. */
    4.75 +        }
    4.76 +    }
    4.77 +
    4.78      if ([self processHitTest:theEvent]) {
    4.79          SDL_SendWindowEvent(window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
    4.80          return;  /* dragging, drop event. */
    4.81 @@ -1046,7 +1071,7 @@
    4.82          }
    4.83      }
    4.84  
    4.85 -    SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
    4.86 +    SDL_SendMouseMotion(window, mouseID, 0, x, y);
    4.87  }
    4.88  
    4.89  - (void)mouseDragged:(NSEvent *)theEvent