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

#define DOLLARNPOINTS   64

Definition at line 39 of file SDL_gesture.c.

◆ DOLLARSIZE

#define DOLLARSIZE   256

Definition at line 40 of file SDL_gesture.c.

◆ 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.

◆ PHI

#define PHI   0.618033989

Definition at line 44 of file SDL_gesture.c.

Function Documentation

◆ bestDollarDifference()

static float bestDollarDifference ( SDL_FloatPoint points,
SDL_FloatPoint templ 
)
static

Definition at line 299 of file SDL_gesture.c.

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

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

Referenced by dollarRecognize().

◆ dollarDifference()

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

Definition at line 283 of file SDL_gesture.c.

284{
285 /* SDL_FloatPoint p[DOLLARNPOINTS]; */
286 float dist = 0;
288 int i;
289 for (i = 0; i < DOLLARNPOINTS; i++) {
290 p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang));
291 p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang));
292 dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
293 (p.y-templ[i].y)*(p.y-templ[i].y)));
294 }
295 return dist/DOLLARNPOINTS;
296
297}
#define SDL_sqrt
#define SDL_sin
#define SDL_cos
#define DOLLARNPOINTS
Definition: SDL_gesture.c:39
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLfloat GLfloat p
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

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

Referenced by bestDollarDifference().

◆ dollarNormalize()

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

Definition at line 338 of file SDL_gesture.c.

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

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

Referenced by dollarRecognize(), and SDL_GestureProcessEvent().

◆ dollarRecognize()

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

Definition at line 430 of file SDL_gesture.c.

431{
433 int i;
434 float bestDiff = 10000;
435
436 SDL_memset(points, 0, sizeof(points));
437
439
440 /* PrintPath(points); */
441 *bestTempl = -1;
442 for (i = 0; i < touch->numDollarTemplates; i++) {
443 float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
444 if (diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
445 }
446 return bestDiff;
447}
#define SDL_memset
static int dollarNormalize(const SDL_DollarPath *path, SDL_FloatPoint *points)
Definition: SDL_gesture.c:338
static float bestDollarDifference(SDL_FloatPoint *points, SDL_FloatPoint *templ)
Definition: SDL_gesture.c:299
SDL_FloatPoint path[DOLLARNPOINTS]
Definition: SDL_gesture.c:58
SDL_DollarTemplate * dollarTemplate
Definition: SDL_gesture.c:69

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

Referenced by SDL_GestureProcessEvent().

◆ SaveTemplate()

static int SaveTemplate ( SDL_DollarTemplate templ,
SDL_RWops dst 
)
static

Definition at line 122 of file SDL_gesture.c.

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}
#define SDL_RWwrite
#define SDL_SwapFloatLE(X)
Definition: SDL_endian.h:235
GLenum GLenum dst
#define NULL
Definition: begin_code.h:167

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

Referenced by SDL_SaveAllDollarTemplates(), and SDL_SaveDollarTemplate().

◆ SDL_AddDollarGesture()

static int SDL_AddDollarGesture ( SDL_GestureTouch inTouch,
SDL_FloatPoint path 
)
static

Definition at line 209 of file SDL_gesture.c.

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];
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 SDL_GestureTouch * SDL_gestureTouch
Definition: SDL_gesture.c:74
static int SDL_numGestureTouches
Definition: SDL_gesture.c:75
GLuint index

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

Referenced by SDL_GestureProcessEvent(), and SDL_LoadDollarTemplates().

◆ SDL_AddDollarGesture_one()

static int SDL_AddDollarGesture_one ( SDL_GestureTouch inTouch,
SDL_FloatPoint path 
)
static

Definition at line 185 of file SDL_gesture.c.

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];
203 templ->hash = SDL_HashDollar(templ->path);
204 inTouch->numDollarTemplates++;
205
206 return index;
207}
#define SDL_realloc
#define SDL_memcpy
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
static unsigned long SDL_HashDollar(SDL_FloatPoint *points)
Definition: SDL_gesture.c:110
unsigned long hash
Definition: SDL_gesture.c:59

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().

◆ SDL_GestureAddTouch()

int SDL_GestureAddTouch ( SDL_TouchID  touchId)

Definition at line 450 of file SDL_gesture.c.

451{
454 sizeof(SDL_GestureTouch));
455
456 if (!gestureTouch) {
457 return SDL_OutOfMemory();
458 }
459
460 SDL_gestureTouch = gestureTouch;
461
465 return 0;
466}
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
SDL_TouchID id
Definition: SDL_gesture.c:63

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

Referenced by SDL_AddTouch().

◆ SDL_GestureDelTouch()

int SDL_GestureDelTouch ( SDL_TouchID  touchId)

Definition at line 468 of file SDL_gesture.c.

469{
470 int i;
471 for (i = 0; i < SDL_numGestureTouches; i++) {
472 if (SDL_gestureTouch[i].id == touchId) {
473 break;
474 }
475 }
476
477 if (i == SDL_numGestureTouches) {
478 /* not found */
479 return -1;
480 }
481
482 SDL_free(SDL_gestureTouch[i].dollarTemplate);
484
487 return 0;
488}
#define SDL_free

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

Referenced by SDL_DelTouch().

◆ SDL_GestureProcessEvent()

void SDL_GestureProcessEvent ( SDL_Event event)

Definition at line 542 of file SDL_gesture.c.

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

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_gestureTouch, SDL_GetGestureTouch(), SDL_numGestureTouches, SDL_SendDollarRecord(), SDL_SendGestureDollar(), SDL_SendGestureMulti(), SDL_sqrt, SDL_FloatPoint::x, and SDL_FloatPoint::y.

Referenced by SDL_PushEvent().

◆ SDL_GestureQuit()

void SDL_GestureQuit ( void  )

Definition at line 104 of file SDL_gesture.c.

References NULL, SDL_free, and SDL_gestureTouch.

Referenced by SDL_TouchQuit().

◆ SDL_GetGestureTouch()

static SDL_GestureTouch * SDL_GetGestureTouch ( SDL_TouchID  id)
static

Definition at line 490 of file SDL_gesture.c.

491{
492 int i;
493 for (i = 0; i < SDL_numGestureTouches; i++) {
494 /* printf("%i ?= %i\n",SDL_gestureTouch[i].id,id); */
495 if (SDL_gestureTouch[i].id == id)
496 return &SDL_gestureTouch[i];
497 }
498 return NULL;
499}

References i, NULL, SDL_gestureTouch, and SDL_numGestureTouches.

Referenced by SDL_GestureProcessEvent().

◆ SDL_HashDollar()

static unsigned long SDL_HashDollar ( SDL_FloatPoint points)
static

Definition at line 110 of file SDL_gesture.c.

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}

References DOLLARNPOINTS, and i.

Referenced by SDL_AddDollarGesture_one().

◆ 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.

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}
#define SDL_RWread
GLenum src

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

◆ 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.

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}
@ SDL_TRUE
Definition: SDL_stdinc.h:164

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

◆ SDL_SaveAllDollarTemplates()

int SDL_SaveAllDollarTemplates ( SDL_RWops dst)

Save all currently loaded Dollar Gesture templates.

Definition at line 157 of file SDL_gesture.c.

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 SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops *dst)
Definition: SDL_gesture.c:122
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

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

◆ 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.

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}

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

◆ SDL_SendDollarRecord()

static int SDL_SendDollarRecord ( SDL_GestureTouch touch,
SDL_GestureID  gestureId 
)
static

Definition at line 531 of file SDL_gesture.c.

532{
534 event.dgesture.type = SDL_DOLLARRECORD;
535 event.dgesture.touchId = touch->id;
536 event.dgesture.gestureId = gestureId;
537 return SDL_PushEvent(&event) > 0;
538}
#define SDL_PushEvent
@ SDL_DOLLARRECORD
Definition: SDL_events.h:134
General event structure.
Definition: SDL_events.h:558

References SDL_GestureTouch::id, SDL_DOLLARRECORD, and SDL_PushEvent.

Referenced by SDL_GestureProcessEvent().

◆ SDL_SendGestureDollar()

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

Definition at line 515 of file SDL_gesture.c.

517{
519 event.dgesture.type = SDL_DOLLARGESTURE;
520 event.dgesture.touchId = touch->id;
521 event.dgesture.x = touch->centroid.x;
522 event.dgesture.y = touch->centroid.y;
523 event.dgesture.gestureId = gestureId;
524 event.dgesture.error = error;
525 /* A finger came up to trigger this event. */
526 event.dgesture.numFingers = touch->numDownFingers + 1;
527 return SDL_PushEvent(&event) > 0;
528}
@ SDL_DOLLARGESTURE
Definition: SDL_events.h:133

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().

◆ SDL_SendGestureMulti()

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

Definition at line 501 of file SDL_gesture.c.

502{
504 event.mgesture.type = SDL_MULTIGESTURE;
505 event.mgesture.touchId = touch->id;
506 event.mgesture.x = touch->centroid.x;
507 event.mgesture.y = touch->centroid.y;
508 event.mgesture.dTheta = dTheta;
509 event.mgesture.dDist = dDist;
510 event.mgesture.numFingers = touch->numDownFingers;
511 return SDL_PushEvent(&event) > 0;
512}
@ SDL_MULTIGESTURE
Definition: SDL_events.h:135

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().

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_numGestureTouches