SDL  2.0
SDL_gesture.c File Reference
#include "../SDL_internal.h"
#include "SDL_events.h"
#include "SDL_endian.h"
#include "SDL_events_c.h"
#include "SDL_gesture_c.h"
+ Include dependency graph for SDL_gesture.c:

Go to the source code of this file.

Data Structures

struct  SDL_FloatPoint
 
struct  SDL_DollarPath
 
struct  SDL_DollarTemplate
 
struct  SDL_GestureTouch
 

Macros

#define MAXPATHSIZE   1024
 
#define DOLLARNPOINTS   64
 
#define DOLLARSIZE   256
 
#define ENABLE_DOLLAR
 
#define PHI   0.618033989
 

Functions

int SDL_RecordGesture (SDL_TouchID touchId)
 Begin Recording a gesture on the specified touch, or all touches (-1) More...
 
void SDL_GestureQuit ()
 
static unsigned long SDL_HashDollar (SDL_FloatPoint *points)
 
static int SaveTemplate (SDL_DollarTemplate *templ, SDL_RWops *dst)
 
int SDL_SaveAllDollarTemplates (SDL_RWops *dst)
 Save all currently loaded Dollar Gesture templates. More...
 
int SDL_SaveDollarTemplate (SDL_GestureID gestureId, SDL_RWops *dst)
 Save a currently loaded Dollar Gesture template. More...
 
static int SDL_AddDollarGesture_one (SDL_GestureTouch *inTouch, SDL_FloatPoint *path)
 
static int SDL_AddDollarGesture (SDL_GestureTouch *inTouch, SDL_FloatPoint *path)
 
int SDL_LoadDollarTemplates (SDL_TouchID touchId, SDL_RWops *src)
 Load Dollar Gesture templates from a file. More...
 
static float dollarDifference (SDL_FloatPoint *points, SDL_FloatPoint *templ, float ang)
 
static float bestDollarDifference (SDL_FloatPoint *points, SDL_FloatPoint *templ)
 
static int dollarNormalize (const SDL_DollarPath *path, SDL_FloatPoint *points)
 
static float dollarRecognize (const SDL_DollarPath *path, int *bestTempl, SDL_GestureTouch *touch)
 
int SDL_GestureAddTouch (SDL_TouchID touchId)
 
int SDL_GestureDelTouch (SDL_TouchID touchId)
 
static SDL_GestureTouchSDL_GetGestureTouch (SDL_TouchID id)
 
static int SDL_SendGestureMulti (SDL_GestureTouch *touch, float dTheta, float dDist)
 
static int SDL_SendGestureDollar (SDL_GestureTouch *touch, SDL_GestureID gestureId, float error)
 
static int SDL_SendDollarRecord (SDL_GestureTouch *touch, SDL_GestureID gestureId)
 
void SDL_GestureProcessEvent (SDL_Event *event)
 

Variables

static SDL_GestureTouchSDL_gestureTouch
 
static int SDL_numGestureTouches = 0
 
static SDL_bool recordAll
 

Macro Definition Documentation

◆ DOLLARNPOINTS

◆ DOLLARSIZE

#define DOLLARSIZE   256

Definition at line 40 of file SDL_gesture.c.

Referenced by dollarNormalize().

◆ ENABLE_DOLLAR

#define ENABLE_DOLLAR

Definition at line 42 of file SDL_gesture.c.

◆ MAXPATHSIZE

#define MAXPATHSIZE   1024

Definition at line 37 of file SDL_gesture.c.

Referenced by SDL_GestureProcessEvent().

◆ PHI

#define PHI   0.618033989

Definition at line 44 of file SDL_gesture.c.

Referenced by bestDollarDifference().

Function Documentation

◆ bestDollarDifference()

static float bestDollarDifference ( SDL_FloatPoint points,
SDL_FloatPoint templ 
)
static

Definition at line 298 of file SDL_gesture.c.

References dollarDifference(), PHI, SDL_fabs, and SDL_min.

Referenced by dollarRecognize().

299 {
300  /*------------BEGIN DOLLAR BLACKBOX------------------
301  -TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-
302  -"http://depts.washington.edu/aimgroup/proj/dollar/"
303  */
304  double ta = -M_PI/4;
305  double tb = M_PI/4;
306  double dt = M_PI/90;
307  float x1 = (float)(PHI*ta + (1-PHI)*tb);
308  float f1 = dollarDifference(points,templ,x1);
309  float x2 = (float)((1-PHI)*ta + PHI*tb);
310  float f2 = dollarDifference(points,templ,x2);
311  while (SDL_fabs(ta-tb) > dt) {
312  if (f1 < f2) {
313  tb = x2;
314  x2 = x1;
315  f2 = f1;
316  x1 = (float)(PHI*ta + (1-PHI)*tb);
317  f1 = dollarDifference(points,templ,x1);
318  }
319  else {
320  ta = x1;
321  x1 = x2;
322  f1 = f2;
323  x2 = (float)((1-PHI)*ta + PHI*tb);
324  f2 = dollarDifference(points,templ,x2);
325  }
326  }
327  /*
328  if (f1 <= f2)
329  printf("Min angle (x1): %f\n",x1);
330  else if (f1 > f2)
331  printf("Min angle (x2): %f\n",x2);
332  */
333  return SDL_min(f1,f2);
334 }
GLuint GLfloat GLfloat GLfloat x1
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
#define SDL_fabs
#define PHI
Definition: SDL_gesture.c:44
GLfixed GLfixed x2
static float dollarDifference(SDL_FloatPoint *points, SDL_FloatPoint *templ, float ang)
Definition: SDL_gesture.c:282

◆ dollarDifference()

static float dollarDifference ( SDL_FloatPoint points,
SDL_FloatPoint templ,
float  ang 
)
static

Definition at line 282 of file SDL_gesture.c.

References DOLLARNPOINTS, i, SDL_cos, SDL_sin, SDL_sqrt, SDL_FloatPoint::x, and SDL_FloatPoint::y.

Referenced by bestDollarDifference().

283 {
284  /* SDL_FloatPoint p[DOLLARNPOINTS]; */
285  float dist = 0;
287  int i;
288  for (i = 0; i < DOLLARNPOINTS; i++) {
289  p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang));
290  p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang));
291  dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
292  (p.y-templ[i].y)*(p.y-templ[i].y)));
293  }
294  return dist/DOLLARNPOINTS;
295 
296 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLfloat GLfloat p
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
#define SDL_cos
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define SDL_sqrt
#define SDL_sin

◆ dollarNormalize()

static int dollarNormalize ( const SDL_DollarPath path,
SDL_FloatPoint points 
)
static

Definition at line 337 of file SDL_gesture.c.

References d, DOLLARNPOINTS, DOLLARSIZE, i, SDL_DollarPath::length, SDL_DollarPath::numPoints, SDL_DollarPath::p, SDL_atan2, SDL_cos, SDL_SetError, SDL_sin, SDL_sqrt, SDL_FloatPoint::x, and SDL_FloatPoint::y.

Referenced by dollarRecognize(), and SDL_GestureProcessEvent().

338 {
339  int i;
340  float interval;
341  float dist;
342  int numPoints = 0;
343  SDL_FloatPoint centroid;
344  float xmin,xmax,ymin,ymax;
345  float ang;
346  float w,h;
347  float length = path->length;
348 
349  /* Calculate length if it hasn't already been done */
350  if (length <= 0) {
351  for (i=1;i < path->numPoints; i++) {
352  float dx = path->p[i ].x - path->p[i-1].x;
353  float dy = path->p[i ].y - path->p[i-1].y;
354  length += (float)(SDL_sqrt(dx*dx+dy*dy));
355  }
356  }
357 
358  /* Resample */
359  interval = length/(DOLLARNPOINTS - 1);
360  dist = interval;
361 
362  centroid.x = 0;centroid.y = 0;
363 
364  /* printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y); */
365  for (i = 1; i < path->numPoints; i++) {
366  float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+
367  (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y)));
368  /* printf("d = %f dist = %f/%f\n",d,dist,interval); */
369  while (dist + d > interval) {
370  points[numPoints].x = path->p[i-1].x +
371  ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x);
372  points[numPoints].y = path->p[i-1].y +
373  ((interval-dist)/d)*(path->p[i].y-path->p[i-1].y);
374  centroid.x += points[numPoints].x;
375  centroid.y += points[numPoints].y;
376  numPoints++;
377 
378  dist -= interval;
379  }
380  dist += d;
381  }
382  if (numPoints < DOLLARNPOINTS-1) {
383  SDL_SetError("ERROR: NumPoints = %i", numPoints);
384  return 0;
385  }
386  /* copy the last point */
387  points[DOLLARNPOINTS-1] = path->p[path->numPoints-1];
388  numPoints = DOLLARNPOINTS;
389 
390  centroid.x /= numPoints;
391  centroid.y /= numPoints;
392 
393  /* printf("Centroid (%f,%f)",centroid.x,centroid.y); */
394  /* Rotate Points so point 0 is left of centroid and solve for the bounding box */
395  xmin = centroid.x;
396  xmax = centroid.x;
397  ymin = centroid.y;
398  ymax = centroid.y;
399 
400  ang = (float)(SDL_atan2(centroid.y - points[0].y,
401  centroid.x - points[0].x));
402 
403  for (i = 0; i<numPoints; i++) {
404  float px = points[i].x;
405  float py = points[i].y;
406  points[i].x = (float)((px - centroid.x)*SDL_cos(ang) -
407  (py - centroid.y)*SDL_sin(ang) + centroid.x);
408  points[i].y = (float)((px - centroid.x)*SDL_sin(ang) +
409  (py - centroid.y)*SDL_cos(ang) + centroid.y);
410 
411 
412  if (points[i].x < xmin) xmin = points[i].x;
413  if (points[i].x > xmax) xmax = points[i].x;
414  if (points[i].y < ymin) ymin = points[i].y;
415  if (points[i].y > ymax) ymax = points[i].y;
416  }
417 
418  /* Scale points to DOLLARSIZE, and translate to the origin */
419  w = xmax-xmin;
420  h = ymax-ymin;
421 
422  for (i=0; i<numPoints; i++) {
423  points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
424  points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
425  }
426  return numPoints;
427 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_FloatPoint p[MAXPATHSIZE]
Definition: SDL_gesture.c:54
GLfloat GLfloat GLfloat GLfloat h
#define SDL_atan2
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
#define DOLLARSIZE
Definition: SDL_gesture.c:40
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 ** d
#define SDL_cos
GLubyte GLubyte GLubyte GLubyte w
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define SDL_SetError
#define SDL_sqrt
GLuint GLsizei GLsizei * length
#define SDL_sin

◆ dollarRecognize()

static float dollarRecognize ( const SDL_DollarPath path,
int *  bestTempl,
SDL_GestureTouch touch 
)
static

Definition at line 429 of file SDL_gesture.c.

References bestDollarDifference(), dollarNormalize(), DOLLARNPOINTS, SDL_GestureTouch::dollarTemplate, i, SDL_GestureTouch::numDollarTemplates, SDL_DollarTemplate::path, and SDL_memset.

Referenced by SDL_GestureProcessEvent().

430 {
432  int i;
433  float bestDiff = 10000;
434 
435  SDL_memset(points, 0, sizeof(points));
436 
437  dollarNormalize(path,points);
438 
439  /* PrintPath(points); */
440  *bestTempl = -1;
441  for (i = 0; i < touch->numDollarTemplates; i++) {
442  float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
443  if (diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
444  }
445  return bestDiff;
446 }
SDL_FloatPoint path[DOLLARNPOINTS]
Definition: SDL_gesture.c:58
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
static float bestDollarDifference(SDL_FloatPoint *points, SDL_FloatPoint *templ)
Definition: SDL_gesture.c:298
GLfixed GLfixed GLint GLint GLfixed points
static int dollarNormalize(const SDL_DollarPath *path, SDL_FloatPoint *points)
Definition: SDL_gesture.c:337
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_DollarTemplate * dollarTemplate
Definition: SDL_gesture.c:69
#define SDL_memset

◆ SaveTemplate()

static int SaveTemplate ( SDL_DollarTemplate templ,
SDL_RWops dst 
)
static

Definition at line 122 of file SDL_gesture.c.

References DOLLARNPOINTS, i, NULL, SDL_DollarTemplate::path, SDL_RWwrite, SDL_SwapFloatLE, SDL_FloatPoint::x, and SDL_FloatPoint::y.

Referenced by SDL_SaveAllDollarTemplates(), and SDL_SaveDollarTemplate().

123 {
124  if (dst == NULL) {
125  return 0;
126  }
127 
128  /* No Longer storing the Hash, rehash on load */
129  /* if (SDL_RWops.write(dst, &(templ->hash), sizeof(templ->hash), 1) != 1) return 0; */
130 
131 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
132  if (SDL_RWwrite(dst, templ->path,
133  sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
134  return 0;
135  }
136 #else
137  {
138  SDL_DollarTemplate copy = *templ;
139  SDL_FloatPoint *p = copy.path;
140  int i;
141  for (i = 0; i < DOLLARNPOINTS; i++, p++) {
142  p->x = SDL_SwapFloatLE(p->x);
143  p->y = SDL_SwapFloatLE(p->y);
144  }
145 
146  if (SDL_RWwrite(dst, copy.path,
147  sizeof(copy.path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
148  return 0;
149  }
150  }
151 #endif
152 
153  return 1;
154 }
SDL_FloatPoint path[DOLLARNPOINTS]
Definition: SDL_gesture.c:58
#define SDL_RWwrite(ctx, ptr, size, n)
Definition: SDL_rwops.h:188
GLfloat GLfloat p
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
#define SDL_SwapFloatLE(X)
Definition: SDL_endian.h:235
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164

◆ SDL_AddDollarGesture()

static int SDL_AddDollarGesture ( SDL_GestureTouch inTouch,
SDL_FloatPoint path 
)
static

Definition at line 209 of file SDL_gesture.c.

References i, NULL, SDL_AddDollarGesture_one(), SDL_numGestureTouches, and SDL_SetError.

Referenced by SDL_GestureProcessEvent(), and SDL_LoadDollarTemplates().

210 {
211  int index = -1;
212  int i = 0;
213  if (inTouch == NULL) {
214  if (SDL_numGestureTouches == 0) return SDL_SetError("no gesture touch devices registered");
215  for (i = 0; i < SDL_numGestureTouches; i++) {
216  inTouch = &SDL_gestureTouch[i];
217  index = SDL_AddDollarGesture_one(inTouch, path);
218  if (index < 0)
219  return -1;
220  }
221  /* Use the index of the last one added. */
222  return index;
223  }
224  return SDL_AddDollarGesture_one(inTouch, path);
225 }
static int SDL_AddDollarGesture_one(SDL_GestureTouch *inTouch, SDL_FloatPoint *path)
Definition: SDL_gesture.c:185
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
GLuint index
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
#define SDL_SetError

◆ SDL_AddDollarGesture_one()

static int SDL_AddDollarGesture_one ( SDL_GestureTouch inTouch,
SDL_FloatPoint path 
)
static

Definition at line 185 of file SDL_gesture.c.

References DOLLARNPOINTS, SDL_GestureTouch::dollarTemplate, SDL_DollarTemplate::hash, SDL_GestureTouch::numDollarTemplates, SDL_DollarTemplate::path, SDL_HashDollar(), SDL_memcpy, SDL_OutOfMemory, and SDL_realloc.

Referenced by SDL_AddDollarGesture().

186 {
187  SDL_DollarTemplate* dollarTemplate;
188  SDL_DollarTemplate *templ;
189  int index;
190 
191  index = inTouch->numDollarTemplates;
192  dollarTemplate =
194  (index + 1) *
195  sizeof(SDL_DollarTemplate));
196  if (!dollarTemplate) {
197  return SDL_OutOfMemory();
198  }
199  inTouch->dollarTemplate = dollarTemplate;
200 
201  templ = &inTouch->dollarTemplate[index];
202  SDL_memcpy(templ->path, path, DOLLARNPOINTS*sizeof(SDL_FloatPoint));
203  templ->hash = SDL_HashDollar(templ->path);
204  inTouch->numDollarTemplates++;
205 
206  return index;
207 }
static unsigned long SDL_HashDollar(SDL_FloatPoint *points)
Definition: SDL_gesture.c:110
SDL_FloatPoint path[DOLLARNPOINTS]
Definition: SDL_gesture.c:58
unsigned long hash
Definition: SDL_gesture.c:59
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
#define SDL_realloc
#define SDL_memcpy
GLuint index
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_DollarTemplate * dollarTemplate
Definition: SDL_gesture.c:69

◆ SDL_GestureAddTouch()

int SDL_GestureAddTouch ( SDL_TouchID  touchId)

Definition at line 448 of file SDL_gesture.c.

References SDL_GestureTouch::id, SDL_numGestureTouches, SDL_OutOfMemory, SDL_realloc, and SDL_zero.

Referenced by SDL_AddTouch().

449 {
451  (SDL_numGestureTouches + 1) *
452  sizeof(SDL_GestureTouch));
453 
454  if (!gestureTouch) {
455  return SDL_OutOfMemory();
456  }
457 
458  SDL_gestureTouch = gestureTouch;
459 
463  return 0;
464 }
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
#define SDL_realloc
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_TouchID id
Definition: SDL_gesture.c:63

◆ SDL_GestureDelTouch()

int SDL_GestureDelTouch ( SDL_TouchID  touchId)

Definition at line 466 of file SDL_gesture.c.

References i, SDL_free, SDL_memcpy, SDL_numGestureTouches, and SDL_zero.

Referenced by SDL_DelTouch().

467 {
468  int i;
469  for (i = 0; i < SDL_numGestureTouches; i++) {
470  if (SDL_gestureTouch[i].id == touchId) {
471  break;
472  }
473  }
474 
475  if (i == SDL_numGestureTouches) {
476  /* not found */
477  return -1;
478  }
479 
480  SDL_free(SDL_gestureTouch[i].dollarTemplate);
482 
483  SDL_numGestureTouches--;
484  SDL_memcpy(&SDL_gestureTouch[i], &SDL_gestureTouch[SDL_numGestureTouches], sizeof(SDL_gestureTouch[i]));
485  return 0;
486 }
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
#define SDL_memcpy
#define SDL_free
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50

◆ SDL_GestureProcessEvent()

void SDL_GestureProcessEvent ( SDL_Event event)

Definition at line 538 of file SDL_gesture.c.

References SDL_GestureTouch::centroid, dollarNormalize(), DOLLARNPOINTS, SDL_GestureTouch::dollarPath, dollarRecognize(), SDL_GestureTouch::dollarTemplate, SDL_DollarTemplate::hash, i, SDL_DollarPath::length, MAXPATHSIZE, NULL, SDL_GestureTouch::numDownFingers, SDL_DollarPath::numPoints, SDL_DollarPath::p, recordAll, SDL_GestureTouch::recording, SDL_AddDollarGesture(), SDL_atan2, SDL_FALSE, SDL_FINGERDOWN, SDL_FINGERMOTION, SDL_FINGERUP, SDL_GetGestureTouch(), SDL_numGestureTouches, SDL_SendDollarRecord(), SDL_SendGestureDollar(), SDL_SendGestureMulti(), SDL_sqrt, SDL_Event::tfinger, SDL_TouchFingerEvent::touchId, SDL_Event::type, SDL_FloatPoint::x, and SDL_FloatPoint::y.

Referenced by SDL_PushEvent().

539 {
540  float x,y;
541  int index;
542  int i;
543  float pathDx, pathDy;
544  SDL_FloatPoint lastP;
545  SDL_FloatPoint lastCentroid;
546  float lDist;
547  float Dist;
548  float dtheta;
549  float dDist;
550 
551  if (event->type == SDL_FINGERMOTION ||
552  event->type == SDL_FINGERDOWN ||
553  event->type == SDL_FINGERUP) {
555 
556  /* Shouldn't be possible */
557  if (inTouch == NULL) return;
558 
559  x = event->tfinger.x;
560  y = event->tfinger.y;
561 
562  /* Finger Up */
563  if (event->type == SDL_FINGERUP) {
565 
566  inTouch->numDownFingers--;
567 
568 #ifdef ENABLE_DOLLAR
569  if (inTouch->recording) {
570  inTouch->recording = SDL_FALSE;
571  dollarNormalize(&inTouch->dollarPath,path);
572  /* PrintPath(path); */
573  if (recordAll) {
574  index = SDL_AddDollarGesture(NULL,path);
575  for (i = 0; i < SDL_numGestureTouches; i++)
576  SDL_gestureTouch[i].recording = SDL_FALSE;
577  }
578  else {
579  index = SDL_AddDollarGesture(inTouch,path);
580  }
581 
582  if (index >= 0) {
583  SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
584  }
585  else {
586  SDL_SendDollarRecord(inTouch,-1);
587  }
588  }
589  else {
590  int bestTempl;
591  float error;
592  error = dollarRecognize(&inTouch->dollarPath,
593  &bestTempl,inTouch);
594  if (bestTempl >= 0){
595  /* Send Event */
596  unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
597  SDL_SendGestureDollar(inTouch,gestureId,error);
598  /* printf ("%s\n",);("Dollar error: %f\n",error); */
599  }
600  }
601 #endif
602  /* inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; */
603  if (inTouch->numDownFingers > 0) {
604  inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
605  x)/inTouch->numDownFingers;
606  inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
607  y)/inTouch->numDownFingers;
608  }
609  }
610  else if (event->type == SDL_FINGERMOTION) {
611  float dx = event->tfinger.dx;
612  float dy = event->tfinger.dy;
613 #ifdef ENABLE_DOLLAR
614  SDL_DollarPath* path = &inTouch->dollarPath;
615  if (path->numPoints < MAXPATHSIZE) {
616  path->p[path->numPoints].x = inTouch->centroid.x;
617  path->p[path->numPoints].y = inTouch->centroid.y;
618  pathDx =
619  (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
620  pathDy =
621  (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
622  path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy);
623  path->numPoints++;
624  }
625 #endif
626  lastP.x = x - dx;
627  lastP.y = y - dy;
628  lastCentroid = inTouch->centroid;
629 
630  inTouch->centroid.x += dx/inTouch->numDownFingers;
631  inTouch->centroid.y += dy/inTouch->numDownFingers;
632  /* printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); */
633  if (inTouch->numDownFingers > 1) {
634  SDL_FloatPoint lv; /* Vector from centroid to last x,y position */
635  SDL_FloatPoint v; /* Vector from centroid to current x,y position */
636  /* lv = inTouch->gestureLast[j].cv; */
637  lv.x = lastP.x - lastCentroid.x;
638  lv.y = lastP.y - lastCentroid.y;
639  lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y);
640  /* printf("lDist = %f\n",lDist); */
641  v.x = x - inTouch->centroid.x;
642  v.y = y - inTouch->centroid.y;
643  /* inTouch->gestureLast[j].cv = v; */
644  Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y);
645  /* SDL_cos(dTheta) = (v . lv)/(|v| * |lv|) */
646 
647  /* Normalize Vectors to simplify angle calculation */
648  lv.x/=lDist;
649  lv.y/=lDist;
650  v.x/=Dist;
651  v.y/=Dist;
652  dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
653 
654  dDist = (Dist - lDist);
655  if (lDist == 0) {dDist = 0;dtheta = 0;} /* To avoid impossible values */
656 
657  /* inTouch->gestureLast[j].dDist = dDist;
658  inTouch->gestureLast[j].dtheta = dtheta;
659 
660  printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
661  gdtheta = gdtheta*.9 + dtheta*.1;
662  gdDist = gdDist*.9 + dDist*.1
663  knob.r += dDist/numDownFingers;
664  knob.ang += dtheta;
665  printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
666  printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist); */
667  SDL_SendGestureMulti(inTouch,dtheta,dDist);
668  }
669  else {
670  /* inTouch->gestureLast[j].dDist = 0;
671  inTouch->gestureLast[j].dtheta = 0;
672  inTouch->gestureLast[j].cv.x = 0;
673  inTouch->gestureLast[j].cv.y = 0; */
674  }
675  /* inTouch->gestureLast[j].f.p.x = x;
676  inTouch->gestureLast[j].f.p.y = y;
677  break;
678  pressure? */
679  }
680  else if (event->type == SDL_FINGERDOWN) {
681 
682  inTouch->numDownFingers++;
683  inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+
684  x)/inTouch->numDownFingers;
685  inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
686  y)/inTouch->numDownFingers;
687  /* printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
688  inTouch->centroid.x,inTouch->centroid.y); */
689 
690 #ifdef ENABLE_DOLLAR
691  inTouch->dollarPath.length = 0;
692  inTouch->dollarPath.p[0].x = x;
693  inTouch->dollarPath.p[0].y = y;
694  inTouch->dollarPath.numPoints = 1;
695 #endif
696  }
697  }
698 }
const GLdouble * v
Definition: SDL_opengl.h:2064
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
static int SDL_SendGestureMulti(SDL_GestureTouch *touch, float dTheta, float dDist)
Definition: SDL_gesture.c:499
unsigned long hash
Definition: SDL_gesture.c:59
SDL_FloatPoint p[MAXPATHSIZE]
Definition: SDL_gesture.c:54
static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id)
Definition: SDL_gesture.c:488
#define SDL_atan2
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
SDL_TouchID touchId
Definition: SDL_events.h:417
static int SDL_SendDollarRecord(SDL_GestureTouch *touch, SDL_GestureID gestureId)
Definition: SDL_gesture.c:528
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
SDL_bool recording
Definition: SDL_gesture.c:71
SDL_FloatPoint centroid
Definition: SDL_gesture.c:64
Uint16 numDownFingers
Definition: SDL_gesture.c:66
static SDL_bool recordAll
Definition: SDL_gesture.c:76
static float dollarRecognize(const SDL_DollarPath *path, int *bestTempl, SDL_GestureTouch *touch)
Definition: SDL_gesture.c:429
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
static int dollarNormalize(const SDL_DollarPath *path, SDL_FloatPoint *points)
Definition: SDL_gesture.c:337
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLuint index
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
static int SDL_SendGestureDollar(SDL_GestureTouch *touch, SDL_GestureID gestureId, float error)
Definition: SDL_gesture.c:512
#define NULL
Definition: begin_code.h:164
SDL_DollarTemplate * dollarTemplate
Definition: SDL_gesture.c:69
static int SDL_AddDollarGesture(SDL_GestureTouch *inTouch, SDL_FloatPoint *path)
Definition: SDL_gesture.c:209
#define SDL_sqrt
#define MAXPATHSIZE
Definition: SDL_gesture.c:37
GLsizei const GLchar *const * path
SDL_DollarPath dollarPath
Definition: SDL_gesture.c:65
Uint32 type
Definition: SDL_events.h:527
SDL_TouchFingerEvent tfinger
Definition: SDL_events.h:548

◆ SDL_GestureQuit()

void SDL_GestureQuit ( void  )

Definition at line 104 of file SDL_gesture.c.

References NULL, and SDL_free.

Referenced by SDL_TouchQuit().

105 {
108 }
#define SDL_free
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
#define NULL
Definition: begin_code.h:164

◆ SDL_GetGestureTouch()

static SDL_GestureTouch* SDL_GetGestureTouch ( SDL_TouchID  id)
static

Definition at line 488 of file SDL_gesture.c.

References i, NULL, and SDL_numGestureTouches.

Referenced by SDL_GestureProcessEvent().

489 {
490  int i;
491  for (i = 0; i < SDL_numGestureTouches; i++) {
492  /* printf("%i ?= %i\n",SDL_gestureTouch[i].id,id); */
493  if (SDL_gestureTouch[i].id == id)
494  return &SDL_gestureTouch[i];
495  }
496  return NULL;
497 }
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164

◆ SDL_HashDollar()

static unsigned long SDL_HashDollar ( SDL_FloatPoint points)
static

Definition at line 110 of file SDL_gesture.c.

References DOLLARNPOINTS, and i.

Referenced by SDL_AddDollarGesture_one().

111 {
112  unsigned long hash = 5381;
113  int i;
114  for (i = 0; i < DOLLARNPOINTS; i++) {
115  hash = ((hash<<5) + hash) + (unsigned long)points[i].x;
116  hash = ((hash<<5) + hash) + (unsigned long)points[i].y;
117  }
118  return hash;
119 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50

◆ SDL_LoadDollarTemplates()

int SDL_LoadDollarTemplates ( SDL_TouchID  touchId,
SDL_RWops src 
)

Load Dollar Gesture templates from a file.

Definition at line 227 of file SDL_gesture.c.

References DOLLARNPOINTS, i, NULL, SDL_DollarTemplate::path, SDL_AddDollarGesture(), SDL_numGestureTouches, SDL_RWread, SDL_SetError, SDL_SwapFloatLE, SDL_FloatPoint::x, and SDL_FloatPoint::y.

228 {
229  int i,loaded = 0;
230  SDL_GestureTouch *touch = NULL;
231  if (src == NULL) return 0;
232  if (touchId >= 0) {
233  for (i = 0; i < SDL_numGestureTouches; i++) {
234  if (SDL_gestureTouch[i].id == touchId) {
235  touch = &SDL_gestureTouch[i];
236  }
237  }
238  if (touch == NULL) {
239  return SDL_SetError("given touch id not found");
240  }
241  }
242 
243  while (1) {
244  SDL_DollarTemplate templ;
245 
246  if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) {
247  if (loaded == 0) {
248  return SDL_SetError("could not read any dollar gesture from rwops");
249  }
250  break;
251  }
252 
253 #if SDL_BYTEORDER != SDL_LIL_ENDIAN
254  for (i = 0; i < DOLLARNPOINTS; i++) {
255  SDL_FloatPoint *p = &templ.path[i];
256  p->x = SDL_SwapFloatLE(p->x);
257  p->y = SDL_SwapFloatLE(p->y);
258  }
259 #endif
260 
261  if (touchId >= 0) {
262  /* printf("Adding loaded gesture to 1 touch\n"); */
263  if (SDL_AddDollarGesture(touch, templ.path) >= 0)
264  loaded++;
265  }
266  else {
267  /* printf("Adding to: %i touches\n",SDL_numGestureTouches); */
268  for (i = 0; i < SDL_numGestureTouches; i++) {
269  touch = &SDL_gestureTouch[i];
270  /* printf("Adding loaded gesture to + touches\n"); */
271  /* TODO: What if this fails? */
272  SDL_AddDollarGesture(touch,templ.path);
273  }
274  loaded++;
275  }
276  }
277 
278  return loaded;
279 }
SDL_FloatPoint path[DOLLARNPOINTS]
Definition: SDL_gesture.c:58
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
GLfloat GLfloat p
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
#define SDL_SwapFloatLE(X)
Definition: SDL_endian.h:235
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
#define SDL_SetError
static int SDL_AddDollarGesture(SDL_GestureTouch *inTouch, SDL_FloatPoint *path)
Definition: SDL_gesture.c:209

◆ SDL_RecordGesture()

int SDL_RecordGesture ( SDL_TouchID  touchId)

Begin Recording a gesture on the specified touch, or all touches (-1)

Definition at line 90 of file SDL_gesture.c.

References i, recordAll, SDL_GestureTouch::recording, SDL_numGestureTouches, and SDL_TRUE.

91 {
92  int i;
93  if (touchId < 0) recordAll = SDL_TRUE;
94  for (i = 0; i < SDL_numGestureTouches; i++) {
95  if ((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
97  if (touchId >= 0)
98  return 1;
99  }
100  }
101  return (touchId < 0);
102 }
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
SDL_bool recording
Definition: SDL_gesture.c:71
static SDL_bool recordAll
Definition: SDL_gesture.c:76
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50

◆ SDL_SaveAllDollarTemplates()

int SDL_SaveAllDollarTemplates ( SDL_RWops dst)

Save all currently loaded Dollar Gesture templates.

Definition at line 157 of file SDL_gesture.c.

References SDL_GestureTouch::dollarTemplate, i, j, SDL_GestureTouch::numDollarTemplates, SaveTemplate(), and SDL_numGestureTouches.

158 {
159  int i,j,rtrn = 0;
160  for (i = 0; i < SDL_numGestureTouches; i++) {
162  for (j = 0; j < touch->numDollarTemplates; j++) {
163  rtrn += SaveTemplate(&touch->dollarTemplate[j], dst);
164  }
165  }
166  return rtrn;
167 }
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_DollarTemplate * dollarTemplate
Definition: SDL_gesture.c:69
static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops *dst)
Definition: SDL_gesture.c:122

◆ SDL_SaveDollarTemplate()

int SDL_SaveDollarTemplate ( SDL_GestureID  gestureId,
SDL_RWops dst 
)

Save a currently loaded Dollar Gesture template.

Definition at line 169 of file SDL_gesture.c.

References SDL_GestureTouch::dollarTemplate, SDL_DollarTemplate::hash, i, j, SDL_GestureTouch::numDollarTemplates, SaveTemplate(), SDL_numGestureTouches, and SDL_SetError.

170 {
171  int i,j;
172  for (i = 0; i < SDL_numGestureTouches; i++) {
174  for (j = 0; j < touch->numDollarTemplates; j++) {
175  if (touch->dollarTemplate[j].hash == gestureId) {
176  return SaveTemplate(&touch->dollarTemplate[j], dst);
177  }
178  }
179  }
180  return SDL_SetError("Unknown gestureId");
181 }
unsigned long hash
Definition: SDL_gesture.c:59
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
static SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_DollarTemplate * dollarTemplate
Definition: SDL_gesture.c:69
#define SDL_SetError
static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops *dst)
Definition: SDL_gesture.c:122

◆ SDL_SendDollarRecord()

static int SDL_SendDollarRecord ( SDL_GestureTouch touch,
SDL_GestureID  gestureId 
)
static

Definition at line 528 of file SDL_gesture.c.

References SDL_GestureTouch::id, SDL_DOLLARRECORD, and SDL_PushEvent.

Referenced by SDL_GestureProcessEvent().

529 {
531  event.dgesture.type = SDL_DOLLARRECORD;
532  event.dgesture.touchId = touch->id;
533  event.dgesture.gestureId = gestureId;
534  return SDL_PushEvent(&event) > 0;
535 }
struct _cl_event * event
#define SDL_PushEvent
General event structure.
Definition: SDL_events.h:525
SDL_TouchID id
Definition: SDL_gesture.c:63

◆ SDL_SendGestureDollar()

static int SDL_SendGestureDollar ( SDL_GestureTouch touch,
SDL_GestureID  gestureId,
float  error 
)
static

Definition at line 512 of file SDL_gesture.c.

References SDL_GestureTouch::centroid, SDL_GestureTouch::id, SDL_GestureTouch::numDownFingers, SDL_DOLLARGESTURE, SDL_PushEvent, SDL_FloatPoint::x, and SDL_FloatPoint::y.

Referenced by SDL_GestureProcessEvent().

514 {
516  event.dgesture.type = SDL_DOLLARGESTURE;
517  event.dgesture.touchId = touch->id;
518  event.dgesture.x = touch->centroid.x;
519  event.dgesture.y = touch->centroid.y;
520  event.dgesture.gestureId = gestureId;
521  event.dgesture.error = error;
522  /* A finger came up to trigger this event. */
523  event.dgesture.numFingers = touch->numDownFingers + 1;
524  return SDL_PushEvent(&event) > 0;
525 }
SDL_FloatPoint centroid
Definition: SDL_gesture.c:64
Uint16 numDownFingers
Definition: SDL_gesture.c:66
struct _cl_event * event
#define SDL_PushEvent
General event structure.
Definition: SDL_events.h:525
SDL_TouchID id
Definition: SDL_gesture.c:63

◆ SDL_SendGestureMulti()

static int SDL_SendGestureMulti ( SDL_GestureTouch touch,
float  dTheta,
float  dDist 
)
static

Definition at line 499 of file SDL_gesture.c.

References SDL_GestureTouch::centroid, SDL_GestureTouch::id, SDL_GestureTouch::numDownFingers, SDL_MULTIGESTURE, SDL_PushEvent, SDL_FloatPoint::x, and SDL_FloatPoint::y.

Referenced by SDL_GestureProcessEvent().

500 {
502  event.mgesture.type = SDL_MULTIGESTURE;
503  event.mgesture.touchId = touch->id;
504  event.mgesture.x = touch->centroid.x;
505  event.mgesture.y = touch->centroid.y;
506  event.mgesture.dTheta = dTheta;
507  event.mgesture.dDist = dDist;
508  event.mgesture.numFingers = touch->numDownFingers;
509  return SDL_PushEvent(&event) > 0;
510 }
SDL_FloatPoint centroid
Definition: SDL_gesture.c:64
Uint16 numDownFingers
Definition: SDL_gesture.c:66
struct _cl_event * event
#define SDL_PushEvent
General event structure.
Definition: SDL_events.h:525
SDL_TouchID id
Definition: SDL_gesture.c:63

Variable Documentation

◆ recordAll

SDL_bool recordAll
static

Definition at line 76 of file SDL_gesture.c.

Referenced by SDL_GestureProcessEvent(), and SDL_RecordGesture().

◆ SDL_gestureTouch

SDL_GestureTouch* SDL_gestureTouch
static

Definition at line 74 of file SDL_gesture.c.

◆ SDL_numGestureTouches