Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Updated touch API
Browse files Browse the repository at this point in the history
* Normalized touch coordinates as floats in the 0...1 range
* Removed unused touchpad concepts from the API
* Added API functions to get active touch devices and current finger state
  • Loading branch information
slouken committed Mar 3, 2013
1 parent 9707138 commit 000345d
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 848 deletions.
56 changes: 14 additions & 42 deletions include/SDL_events.h
Expand Up @@ -105,8 +105,6 @@ typedef enum
SDL_FINGERDOWN = 0x700,
SDL_FINGERUP,
SDL_FINGERMOTION,
SDL_TOUCHBUTTONDOWN,
SDL_TOUCHBUTTONUP,

/* Gesture events */
SDL_DOLLARGESTURE = 0x800,
Expand Down Expand Up @@ -367,75 +365,50 @@ typedef struct SDL_ControllerDeviceEvent


/**
* \brief Touch finger motion/finger event structure (event.tfinger.*)
* \brief Touch finger event structure (event.tfinger.*)
*/
typedef struct SDL_TouchFingerEvent
{
Uint32 type; /**< ::SDL_FINGERMOTION or ::SDL_FINGERDOWN or ::SDL_FINGERUP */
Uint32 timestamp;
Uint32 windowID; /**< The window with mouse focus, if any */
SDL_TouchID touchId; /**< The touch device id */
SDL_FingerID fingerId;
Uint8 state; /**< The current button state */
Uint8 padding1;
Uint8 padding2;
Uint8 padding3;
Uint16 x;
Uint16 y;
Sint16 dx;
Sint16 dy;
Uint16 pressure;
float x; /**< Normalized in the range 0...1 */
float y; /**< Normalized in the range 0...1 */
float dx; /**< Normalized in the range 0...1 */
float dy; /**< Normalized in the range 0...1 */
float pressure; /**< Normalized in the range 0...1 */
} SDL_TouchFingerEvent;


/**
* \brief Touch finger motion/finger event structure (event.tbutton.*)
*/
typedef struct SDL_TouchButtonEvent
{
Uint32 type; /**< ::SDL_TOUCHBUTTONUP OR SDL_TOUCHBUTTONDOWN */
Uint32 timestamp;
Uint32 windowID; /**< The window with mouse focus, if any */
SDL_TouchID touchId; /**< The touch device index */
Uint8 state; /**< The current button state */
Uint8 button; /**< The button changing state */
Uint8 padding1;
Uint8 padding2;
} SDL_TouchButtonEvent;


/**
* \brief Multiple Finger Gesture Event (event.mgesture.*)
*/
typedef struct SDL_MultiGestureEvent
{
Uint32 type; /**< ::SDL_MULTIGESTURE */
Uint32 timestamp;
Uint32 windowID; /**< The window with mouse focus, if any */
SDL_TouchID touchId; /**< The touch device index */
SDL_TouchID touchId; /**< The touch device index */
float dTheta;
float dDist;
float x; /* currently 0...1. Change to screen coords? */
float x;
float y;
Uint16 numFingers;
Uint16 padding;
} SDL_MultiGestureEvent;


/* (event.dgesture.*) */
typedef struct SDL_DollarGestureEvent
{
Uint32 type; /**< ::SDL_DOLLARGESTURE */
Uint32 timestamp;
Uint32 windowID; /**< The window with mouse focus, if any */
SDL_TouchID touchId; /**< The touch device index */
SDL_TouchID touchId; /**< The touch device id */
SDL_GestureID gestureId;
Uint32 numFingers;
float error;
/*
//TODO: Enable to give location?
float x; //currently 0...1. Change to screen coords?
float y;
*/
float x; /**< Normalized center of gesture */
float y; /**< Normalized center of gesture */
} SDL_DollarGestureEvent;


Expand Down Expand Up @@ -518,9 +491,8 @@ typedef union SDL_Event
SDL_UserEvent user; /**< Custom event data */
SDL_SysWMEvent syswm; /**< System dependent window event data */
SDL_TouchFingerEvent tfinger; /**< Touch finger event data */
SDL_TouchButtonEvent tbutton; /**< Touch button event data */
SDL_MultiGestureEvent mgesture; /**< Multi Finger Gesture data */
SDL_DollarGestureEvent dgesture; /**< Multi Finger Gesture data */
SDL_MultiGestureEvent mgesture; /**< Gesture event data */
SDL_DollarGestureEvent dgesture; /**< Gesture event data */
SDL_DropEvent drop; /**< Drag and drop event data */

/* This is necessary for ABI compatibility between Visual C++ and GCC
Expand Down
59 changes: 17 additions & 42 deletions include/SDL_touch.h
Expand Up @@ -46,61 +46,36 @@ typedef Sint64 SDL_FingerID;
typedef struct SDL_Finger
{
SDL_FingerID id;
Uint16 x;
Uint16 y;
Uint16 pressure;
Uint16 xdelta;
Uint16 ydelta;
Uint16 last_x, last_y,last_pressure; /* the last reported coordinates */
SDL_bool down;
float x;
float y;
float pressure;
} SDL_Finger;

typedef struct SDL_Touch
{
/* Free the touch when it's time */
void (*FreeTouch) (struct SDL_Touch * touch);

/* data common for tablets */
float pressure_max, pressure_min;
float x_max,x_min;
float y_max,y_min;
Uint16 xres,yres,pressureres;
float native_xres,native_yres,native_pressureres;
float tilt_x; /* for future use */
float tilt_y; /* for future use */
float rotation; /* for future use */

/* Data common to all touch */
SDL_TouchID id;
SDL_Window *focus;

char *name;
Uint8 buttonstate;
SDL_bool relative_mode;
SDL_bool flush_motion;

int num_fingers;
int max_fingers;
SDL_Finger** fingers;

void *driverdata;
} SDL_Touch;

/* Used as the device ID for mouse events simulated with touch input */
#define SDL_TOUCH_MOUSEID ((Uint32)-1)


/* Function prototypes */

/**
* \brief Get the touch object with the given id.
* \brief Get the number of registered touch devices.
*/
extern DECLSPEC int SDLCALL SDL_GetNumTouchDevices();

/**
* \brief Get the touch ID with the given index, or 0 if the index is invalid.
*/
extern DECLSPEC SDL_TouchID SDLCALL SDL_GetTouchDevice(int index);

/**
* \brief Get the number of active fingers for a given touch device.
*/
extern DECLSPEC SDL_Touch* SDLCALL SDL_GetTouch(SDL_TouchID id);
extern DECLSPEC int SDLCALL SDL_GetNumTouchFingers(SDL_TouchID touchID);

/**
* \brief Get the finger object of the given touch, with the given id.
* \brief Get the finger object of the given touch, with the given index.
*/
extern DECLSPEC SDL_Finger* SDLCALL SDL_GetFinger(SDL_Touch *touch, SDL_FingerID id);
extern DECLSPEC SDL_Finger * SDLCALL SDL_GetTouchFinger(SDL_TouchID touchID, int index);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Expand Down
27 changes: 6 additions & 21 deletions src/events/SDL_gesture.c
Expand Up @@ -64,7 +64,6 @@ typedef struct {

typedef struct {
SDL_TouchID id;
SDL_FloatPoint res;
SDL_FloatPoint centroid;
SDL_DollarPath dollarPath;
Uint16 numDownFingers;
Expand Down Expand Up @@ -410,7 +409,7 @@ static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_Gestu
return bestDiff;
}

int SDL_GestureAddTouch(SDL_Touch* touch)
int SDL_GestureAddTouch(SDL_TouchID touchId)
{
SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
(SDL_numGestureTouches + 1) *
Expand All @@ -423,12 +422,8 @@ int SDL_GestureAddTouch(SDL_Touch* touch)

SDL_gestureTouch = gestureTouch;

SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres;
SDL_gestureTouch[SDL_numGestureTouches].res.y = touch->yres;
SDL_gestureTouch[SDL_numGestureTouches].numDownFingers = 0;

SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres;
SDL_gestureTouch[SDL_numGestureTouches].id = touch->id;
SDL_gestureTouch[SDL_numGestureTouches].id = touchId;

SDL_gestureTouch[SDL_numGestureTouches].numDollarTemplates = 0;

Expand Down Expand Up @@ -468,11 +463,8 @@ static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
SDL_Event event;
event.dgesture.type = SDL_DOLLARGESTURE;
event.dgesture.touchId = touch->id;
/*
//TODO: Add this to give location of gesture?
event.mgesture.x = touch->centroid.x;
event.mgesture.y = touch->centroid.y;
*/
event.dgesture.gestureId = gestureId;
event.dgesture.error = error;
//A finger came up to trigger this event.
Expand Down Expand Up @@ -513,14 +505,8 @@ void SDL_GestureProcessEvent(SDL_Event* event)
//Shouldn't be possible
if (inTouch == NULL) return;

//printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
// (int)event->tfinger.y,
// (int)inTouch->res.x,(int)inTouch->res.y);


x = ((float)event->tfinger.x)/(float)inTouch->res.x;
y = ((float)event->tfinger.y)/(float)inTouch->res.y;

x = event->tfinger.x;
y = event->tfinger.y;

//Finger Up
if (event->type == SDL_FINGERUP) {
Expand Down Expand Up @@ -569,9 +555,8 @@ void SDL_GestureProcessEvent(SDL_Event* event)
}
}
else if (event->type == SDL_FINGERMOTION) {
float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x;
float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y;
//printf("dx,dy: (%f,%f)\n",dx,dy);
float dx = event->tfinger.dx;
float dy = event->tfinger.dy;
#ifdef ENABLE_DOLLAR
SDL_DollarPath* path = &inTouch->dollarPath;
if (path->numPoints < MAXPATHSIZE) {
Expand Down
4 changes: 2 additions & 2 deletions src/events/SDL_gesture_c.h
Expand Up @@ -23,12 +23,12 @@
#ifndef _SDL_gesture_c_h
#define _SDL_gesture_c_h

extern int SDL_GestureAddTouch(SDL_TouchID touchId);

extern void SDL_GestureProcessEvent(SDL_Event* event);

extern int SDL_RecordGesture(SDL_TouchID touchId);

extern int SDL_GestureAddTouch(SDL_Touch* touch);

#endif /* _SDL_gesture_c_h */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 000345d

Please sign in to comment.