Add SDL_TouchDeviceType enum and SDL_GetTouchDeviceType(SDL_TouchID id).
authorAlex Szpakowski <slime73@gmail.com>
Sat, 10 Nov 2018 16:15:48 -0400
changeset 12404eb60e952b13f
parent 12403 a4d730631698
child 12405 5a95fbfd3617
Add SDL_TouchDeviceType enum and SDL_GetTouchDeviceType(SDL_TouchID id).

Touch device types include SDL_TOUCH_DEVICE_DIRECT (a touch screen with window-relative coordinates for touches), SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE (a trackpad-style device with absolute device coordinates), and SDL_TOUCH_DEVICE_INDIRECT_RELATIVE (a trackpad-style device with screen cursor-relative coordinates).

Phone screens are an example of a direct device type. Mac trackpads are the indirect-absolute touch device type. The Apple TV remote is an indirect-relative touch device type.
include/SDL_touch.h
src/core/linux/SDL_evdev.c
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/events/SDL_touch.c
src/events/SDL_touch_c.h
src/video/android/SDL_androidtouch.c
src/video/cocoa/SDL_cocoawindow.m
src/video/emscripten/SDL_emscriptenevents.c
src/video/uikit/SDL_uikitview.m
src/video/wayland/SDL_waylandevents.c
src/video/wayland/SDL_waylandtouch.c
src/video/windows/SDL_windowsevents.c
src/video/winrt/SDL_winrtpointerinput.cpp
src/video/x11/SDL_x11xinput2.c
     1.1 --- a/include/SDL_touch.h	Wed Nov 07 07:52:39 2018 -0800
     1.2 +++ b/include/SDL_touch.h	Sat Nov 10 16:15:48 2018 -0400
     1.3 @@ -41,6 +41,14 @@
     1.4  typedef Sint64 SDL_TouchID;
     1.5  typedef Sint64 SDL_FingerID;
     1.6  
     1.7 +typedef enum
     1.8 +{
     1.9 +    SDL_TOUCH_DEVICE_INVALID = -1,
    1.10 +    SDL_TOUCH_DEVICE_DIRECT,            /* touch screen with window-relative coordinates */
    1.11 +    SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE, /* trackpad with absolute device coordinates */
    1.12 +    SDL_TOUCH_DEVICE_INDIRECT_RELATIVE, /* trackpad with screen cursor-relative coordinates */
    1.13 +} SDL_TouchDeviceType;
    1.14 +
    1.15  typedef struct SDL_Finger
    1.16  {
    1.17      SDL_FingerID id;
    1.18 @@ -66,6 +74,11 @@
    1.19  extern DECLSPEC SDL_TouchID SDLCALL SDL_GetTouchDevice(int index);
    1.20  
    1.21  /**
    1.22 + * \brief Get the type of the given touch device.
    1.23 + */
    1.24 +extern DECLSPEC SDL_TouchDeviceType SDLCALL SDL_GetTouchDeviceType(SDL_TouchID touchID);
    1.25 +
    1.26 +/**
    1.27   *  \brief Get the number of active fingers for a given touch device.
    1.28   */
    1.29  extern DECLSPEC int SDLCALL SDL_GetNumTouchFingers(SDL_TouchID touchID);
     2.1 --- a/src/core/linux/SDL_evdev.c	Wed Nov 07 07:52:39 2018 -0800
     2.2 +++ b/src/core/linux/SDL_evdev.c	Sat Nov 10 16:15:48 2018 -0400
     2.3 @@ -475,6 +475,7 @@
     2.4      }
     2.5  
     2.6      ret = SDL_AddTouch(item->fd, /* I guess our fd is unique enough */
     2.7 +        SDL_TOUCH_DEVICE_DIRECT,
     2.8          item->touchscreen_data->name);
     2.9      if (ret < 0) {
    2.10          SDL_free(item->touchscreen_data->slots);
     3.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Wed Nov 07 07:52:39 2018 -0800
     3.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Sat Nov 10 16:15:48 2018 -0400
     3.3 @@ -712,3 +712,4 @@
     3.4  #define SDL_RenderFillRectsF SDL_RenderFillRectsF_REAL
     3.5  #define SDL_RenderCopyF SDL_RenderCopyF_REAL
     3.6  #define SDL_RenderCopyExF SDL_RenderCopyExF_REAL
     3.7 +#define SDL_GetTouchDeviceType SDL_GetTouchDeviceType_REAL
     4.1 --- a/src/dynapi/SDL_dynapi_procs.h	Wed Nov 07 07:52:39 2018 -0800
     4.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Sat Nov 10 16:15:48 2018 -0400
     4.3 @@ -766,3 +766,4 @@
     4.4  SDL_DYNAPI_PROC(int,SDL_RenderFillRectsF,(SDL_Renderer *a, const SDL_FRect *b, int c),(a,b,c),return)
     4.5  SDL_DYNAPI_PROC(int,SDL_RenderCopyF,(SDL_Renderer *a, SDL_Texture *b, const SDL_Rect *c, const SDL_FRect *d),(a,b,c,d),return)
     4.6  SDL_DYNAPI_PROC(int,SDL_RenderCopyExF,(SDL_Renderer *a, SDL_Texture *b, const SDL_Rect *c, const SDL_FRect *d, const double e, const SDL_FPoint *f, const SDL_RendererFlip g),(a,b,c,d,e,f,g),return)
     4.7 +SDL_DYNAPI_PROC(SDL_TouchDeviceType,SDL_GetTouchDeviceType,(SDL_TouchID a),(a),return)
     5.1 --- a/src/events/SDL_touch.c	Wed Nov 07 07:52:39 2018 -0800
     5.2 +++ b/src/events/SDL_touch.c	Sat Nov 10 16:15:48 2018 -0400
     5.3 @@ -86,6 +86,16 @@
     5.4      return SDL_touchDevices[index];
     5.5  }
     5.6  
     5.7 +SDL_TouchDeviceType
     5.8 +SDL_GetTouchDeviceType(SDL_TouchID id)
     5.9 +{
    5.10 +    SDL_Touch *touch = SDL_GetTouch(id);
    5.11 +    if (touch) {
    5.12 +        return touch->type;
    5.13 +    }
    5.14 +    return SDL_TOUCH_DEVICE_INVALID;
    5.15 +}
    5.16 +
    5.17  static int
    5.18  SDL_GetFingerIndex(const SDL_Touch * touch, SDL_FingerID fingerid)
    5.19  {
    5.20 @@ -133,7 +143,7 @@
    5.21  }
    5.22  
    5.23  int
    5.24 -SDL_AddTouch(SDL_TouchID touchID, const char *name)
    5.25 +SDL_AddTouch(SDL_TouchID touchID, SDL_TouchDeviceType type, const char *name)
    5.26  {
    5.27      SDL_Touch **touchDevices;
    5.28      int index;
    5.29 @@ -163,6 +173,7 @@
    5.30  
    5.31      /* we're setting the touch properties */
    5.32      SDL_touchDevices[index]->id = touchID;
    5.33 +    SDL_touchDevices[index]->type = type;
    5.34      SDL_touchDevices[index]->num_fingers = 0;
    5.35      SDL_touchDevices[index]->max_fingers = 0;
    5.36      SDL_touchDevices[index]->fingers = NULL;
     6.1 --- a/src/events/SDL_touch_c.h	Wed Nov 07 07:52:39 2018 -0800
     6.2 +++ b/src/events/SDL_touch_c.h	Sat Nov 10 16:15:48 2018 -0400
     6.3 @@ -27,6 +27,7 @@
     6.4  typedef struct SDL_Touch
     6.5  {
     6.6      SDL_TouchID id;
     6.7 +    SDL_TouchDeviceType type;
     6.8      int num_fingers;
     6.9      int max_fingers;
    6.10      SDL_Finger** fingers;
    6.11 @@ -37,7 +38,7 @@
    6.12  extern int SDL_TouchInit(void);
    6.13  
    6.14  /* Add a touch, returning the index of the touch, or -1 if there was an error. */
    6.15 -extern int SDL_AddTouch(SDL_TouchID id, const char *name);
    6.16 +extern int SDL_AddTouch(SDL_TouchID id, SDL_TouchDeviceType type, const char *name);
    6.17  
    6.18  /* Get the touch with a given id */
    6.19  extern SDL_Touch *SDL_GetTouch(SDL_TouchID id);
     7.1 --- a/src/video/android/SDL_androidtouch.c	Wed Nov 07 07:52:39 2018 -0800
     7.2 +++ b/src/video/android/SDL_androidtouch.c	Sat Nov 10 16:15:48 2018 -0400
     7.3 @@ -72,7 +72,7 @@
     7.4  
     7.5      if (0 < number) {
     7.6          for (i = 0; i < number; ++i) {
     7.7 -            SDL_AddTouch((SDL_TouchID) ids[i], ""); /* no error handling */
     7.8 +            SDL_AddTouch((SDL_TouchID) ids[i], SDL_TOUCH_DEVICE_DIRECT, ""); /* no error handling */
     7.9          }
    7.10          SDL_free(ids);
    7.11      }
    7.12 @@ -97,7 +97,7 @@
    7.13      }
    7.14  
    7.15      touchDeviceId = (SDL_TouchID)touch_device_id_in;
    7.16 -    if (SDL_AddTouch(touchDeviceId, "") < 0) {
    7.17 +    if (SDL_AddTouch(touchDeviceId, SDL_TOUCH_DEVICE_DIRECT, "") < 0) {
    7.18          SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
    7.19      }
    7.20  
     8.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Wed Nov 07 07:52:39 2018 -0800
     8.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Sat Nov 10 16:15:48 2018 -0400
     8.3 @@ -1103,7 +1103,17 @@
     8.4  
     8.5      for (NSTouch *touch in touches) {
     8.6          const SDL_TouchID touchId = (SDL_TouchID)(intptr_t)[touch device];
     8.7 -        if (SDL_AddTouch(touchId, "") < 0) {
     8.8 +        SDL_TouchDeviceType devtype = SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE;
     8.9 +
    8.10 +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101202 /* Added in the 10.12.2 SDK. */
    8.11 +        if ([touch respondsToSelector:@selector(type)]) {
    8.12 +            if ([touch type] == NSTouchTypeDirect) {
    8.13 +                devtype = SDL_TOUCH_DEVICE_DIRECT;
    8.14 +            }
    8.15 +        }
    8.16 +#endif
    8.17 +
    8.18 +        if (SDL_AddTouch(touchId, devtype, "") < 0) {
    8.19              return;
    8.20          }
    8.21  
     9.1 --- a/src/video/emscripten/SDL_emscriptenevents.c	Wed Nov 07 07:52:39 2018 -0800
     9.2 +++ b/src/video/emscripten/SDL_emscriptenevents.c	Sat Nov 10 16:15:48 2018 -0400
     9.3 @@ -428,7 +428,7 @@
     9.4      int preventDefault = 0;
     9.5  
     9.6      SDL_TouchID deviceId = 1;
     9.7 -    if (SDL_AddTouch(deviceId, "") < 0) {
     9.8 +    if (SDL_AddTouch(deviceId, SDL_TOUCH_DEVICE_DIRECT, "") < 0) {
     9.9           return 0;
    9.10      }
    9.11  
    10.1 --- a/src/video/uikit/SDL_uikitview.m	Wed Nov 07 07:52:39 2018 -0800
    10.2 +++ b/src/video/uikit/SDL_uikitview.m	Sat Nov 10 16:15:48 2018 -0400
    10.3 @@ -39,7 +39,9 @@
    10.4  @implementation SDL_uikitview {
    10.5      SDL_Window *sdlwindow;
    10.6  
    10.7 -    SDL_TouchID touchId;
    10.8 +    SDL_TouchID directTouchId;
    10.9 +    SDL_TouchID indirectTouchId;
   10.10 +
   10.11      UITouch * __weak firstFingerDown;
   10.12  }
   10.13  
   10.14 @@ -68,12 +70,13 @@
   10.15          self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
   10.16          self.autoresizesSubviews = YES;
   10.17  
   10.18 +        directTouchId = 1;
   10.19 +        indirectTouchId = 2;
   10.20 +
   10.21  #if !TARGET_OS_TV
   10.22          self.multipleTouchEnabled = YES;
   10.23 +        SDL_AddTouch(directTouchId, SDL_TOUCH_DEVICE_DIRECT, "");
   10.24  #endif
   10.25 -
   10.26 -        touchId = 1;
   10.27 -        SDL_AddTouch(touchId, "");
   10.28      }
   10.29  
   10.30      return self;
   10.31 @@ -135,6 +138,30 @@
   10.32      sdlwindow = window;
   10.33  }
   10.34  
   10.35 +- (SDL_TouchDeviceType)touchTypeForTouch:(UITouch *)touch
   10.36 +{
   10.37 +#ifdef __IPHONE_9_0
   10.38 +    if ([touch respondsToSelector:@selector((type))]) {
   10.39 +        if (touch.type == UITouchTypeIndirect) {
   10.40 +            return SDL_TOUCH_DEVICE_INDIRECT_RELATIVE;
   10.41 +        }
   10.42 +    }
   10.43 +#endif
   10.44 +
   10.45 +    return SDL_TOUCH_DEVICE_DIRECT;
   10.46 +}
   10.47 +
   10.48 +- (SDL_TouchID)touchIdForType:(SDL_TouchDeviceType)type
   10.49 +{
   10.50 +    switch (type) {
   10.51 +        case SDL_TOUCH_DEVICE_DIRECT:
   10.52 +        default:
   10.53 +            return directTouchId;
   10.54 +        case SDL_TOUCH_DEVICE_INDIRECT_RELATIVE:
   10.55 +            return indirectTouchId;
   10.56 +    }
   10.57 +}
   10.58 +
   10.59  - (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
   10.60  {
   10.61      CGPoint point = [touch locationInView:self];
   10.62 @@ -162,8 +189,14 @@
   10.63  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
   10.64  {
   10.65      for (UITouch *touch in touches) {
   10.66 +        SDL_TouchDeviceType touchType = [self touchTypeForTouch:touch];
   10.67 +        SDL_TouchID touchId = [self touchIdForType:touchType];
   10.68          float pressure = [self pressureForTouch:touch];
   10.69  
   10.70 +        if (SDL_AddTouch(touchId, touchType, "") < 0) {
   10.71 +            continue;
   10.72 +        }
   10.73 +
   10.74          if (!firstFingerDown) {
   10.75              CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
   10.76              int clicks = (int) touch.tapCount;
   10.77 @@ -186,8 +219,14 @@
   10.78  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
   10.79  {
   10.80      for (UITouch *touch in touches) {
   10.81 +        SDL_TouchDeviceType touchType = [self touchTypeForTouch:touch];
   10.82 +        SDL_TouchID touchId = [self touchIdForType:touchType];
   10.83          float pressure = [self pressureForTouch:touch];
   10.84  
   10.85 +        if (SDL_AddTouch(touchId, touchType, "") < 0) {
   10.86 +            continue;
   10.87 +        }
   10.88 +
   10.89          if (touch == firstFingerDown) {
   10.90              /* send mouse up */
   10.91              int clicks = (int) touch.tapCount;
   10.92 @@ -209,8 +248,14 @@
   10.93  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
   10.94  {
   10.95      for (UITouch *touch in touches) {
   10.96 +        SDL_TouchDeviceType touchType = [self touchTypeForTouch:touch];
   10.97 +        SDL_TouchID touchId = [self touchIdForType:touchType];
   10.98          float pressure = [self pressureForTouch:touch];
   10.99  
  10.100 +        if (SDL_AddTouch(touchId, touchType, "") < 0) {
  10.101 +            continue;
  10.102 +        }
  10.103 +
  10.104          if (touch == firstFingerDown) {
  10.105              CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
  10.106  
    11.1 --- a/src/video/wayland/SDL_waylandevents.c	Wed Nov 07 07:52:39 2018 -0800
    11.2 +++ b/src/video/wayland/SDL_waylandevents.c	Sat Nov 10 16:15:48 2018 -0400
    11.3 @@ -614,7 +614,7 @@
    11.4      }
    11.5  
    11.6      if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
    11.7 -        SDL_AddTouch(1, "wayland_touch");
    11.8 +        SDL_AddTouch(1, SDL_TOUCH_DEVICE_DIRECT, "wayland_touch");
    11.9          input->touch = wl_seat_get_touch(seat);
   11.10          wl_touch_set_user_data(input->touch, input);
   11.11          wl_touch_add_listener(input->touch, &touch_listener,
    12.1 --- a/src/video/wayland/SDL_waylandtouch.c	Wed Nov 07 07:52:39 2018 -0800
    12.2 +++ b/src/video/wayland/SDL_waylandtouch.c	Sat Nov 10 16:15:48 2018 -0400
    12.3 @@ -89,7 +89,7 @@
    12.4      */
    12.5  
    12.6      SDL_TouchID deviceId = 1;
    12.7 -    if (SDL_AddTouch(deviceId, "qt_touch_extension") < 0) {
    12.8 +    if (SDL_AddTouch(deviceId, SDL_TOUCH_DEVICE_DIRECT, "qt_touch_extension") < 0) {
    12.9           SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
   12.10      }
   12.11  
    13.1 --- a/src/video/windows/SDL_windowsevents.c	Wed Nov 07 07:52:39 2018 -0800
    13.2 +++ b/src/video/windows/SDL_windowsevents.c	Sat Nov 10 16:15:48 2018 -0400
    13.3 @@ -932,7 +932,11 @@
    13.4                      PTOUCHINPUT input = &inputs[i];
    13.5  
    13.6                      const SDL_TouchID touchId = (SDL_TouchID)((size_t)input->hSource);
    13.7 -                    if (SDL_AddTouch(touchId, "") < 0) {
    13.8 +
    13.9 +                    /* TODO: Can we use GetRawInputDeviceInfo and HID info to
   13.10 +                       determine if this is a direct or indirect touch device?
   13.11 +                     */
   13.12 +                    if (SDL_AddTouch(touchId, SDL_TOUCH_DEVICE_DIRECT, "") < 0) {
   13.13                          continue;
   13.14                      }
   13.15  
    14.1 --- a/src/video/winrt/SDL_winrtpointerinput.cpp	Wed Nov 07 07:52:39 2018 -0800
    14.2 +++ b/src/video/winrt/SDL_winrtpointerinput.cpp	Sat Nov 10 16:15:48 2018 -0400
    14.3 @@ -44,7 +44,7 @@
    14.4  void
    14.5  WINRT_InitTouch(_THIS)
    14.6  {
    14.7 -    SDL_AddTouch(WINRT_TouchID, "");
    14.8 +    SDL_AddTouch(WINRT_TouchID, SDL_TOUCH_DEVICE_DIRECT, "");
    14.9  }
   14.10  
   14.11  
    15.1 --- a/src/video/x11/SDL_x11xinput2.c	Wed Nov 07 07:52:39 2018 -0800
    15.2 +++ b/src/video/x11/SDL_x11xinput2.c	Sat Nov 10 16:15:48 2018 -0400
    15.3 @@ -244,6 +244,7 @@
    15.4          XIDeviceInfo *dev = &info[i];
    15.5          for (j = 0; j < dev->num_classes; j++) {
    15.6              SDL_TouchID touchId;
    15.7 +            SDL_TouchDeviceType touchType;
    15.8              XIAnyClassInfo *class = dev->classes[j];
    15.9              XITouchClassInfo *t = (XITouchClassInfo*)class;
   15.10  
   15.11 @@ -251,8 +252,14 @@
   15.12              if (class->type != XITouchClass)
   15.13                  continue;
   15.14  
   15.15 +            if (t->mode == XIDependentTouch) {
   15.16 +                touchType = SDL_TOUCH_DEVICE_INDIRECT_RELATIVE;
   15.17 +            } else { /* XIDirectTouch */
   15.18 +                touchType = SDL_TOUCH_DEVICE_DIRECT;
   15.19 +            }
   15.20 +
   15.21              touchId = t->sourceid;
   15.22 -            SDL_AddTouch(touchId, dev->name);
   15.23 +            SDL_AddTouch(touchId, touchType, dev->name);
   15.24          }
   15.25      }
   15.26      X11_XIFreeDeviceInfo(info);