Fixed bugs related to keyboard handling in gestureSDLTest. Fixed gesture code (dynamic memory allocation). Cleaned up gesture and test code.
authorJim Grandpre <jim.tla@gmail.com>
Fri, 13 Aug 2010 23:08:32 -0400
changeset 4688494f71f57a80
parent 4687 257bdf117af8
child 4689 f9ab8df6d45a
Fixed bugs related to keyboard handling in gestureSDLTest. Fixed gesture code (dynamic memory allocation). Cleaned up gesture and test code.
src/events/SDL_gesture.c
touchTest/gestureSDLTest.c
     1.1 --- a/src/events/SDL_gesture.c	Fri Aug 13 17:05:06 2010 -0700
     1.2 +++ b/src/events/SDL_gesture.c	Fri Aug 13 23:08:32 2010 -0400
     1.3 @@ -27,11 +27,12 @@
     1.4  #include "SDL_gesture_c.h"
     1.5  
     1.6  //TODO: Replace with malloc
     1.7 -#define MAXFINGERS 5
     1.8 -#define MAXTOUCHES 2
     1.9 -#define MAXTEMPLATES 4
    1.10 +
    1.11  #define MAXPATHSIZE 1024
    1.12  
    1.13 + 
    1.14 +
    1.15 +
    1.16  #define DOLLARNPOINTS 64
    1.17  #define DOLLARSIZE 256
    1.18  
    1.19 @@ -42,55 +43,38 @@
    1.20  
    1.21  typedef struct {
    1.22    float x,y;
    1.23 -} Point;
    1.24 -
    1.25 -
    1.26 -typedef struct {
    1.27 -  Point p;
    1.28 -  float pressure;
    1.29 -  SDL_FingerID id;
    1.30 -} Finger;
    1.31 -
    1.32 +} SDL_FloatPoint;
    1.33  
    1.34  typedef struct {
    1.35    float length;
    1.36    
    1.37    int numPoints;
    1.38 -  Point p[MAXPATHSIZE];
    1.39 -} DollarPath;
    1.40 +  SDL_FloatPoint p[MAXPATHSIZE];
    1.41 +} SDL_DollarPath;
    1.42  
    1.43 -/*
    1.44  typedef struct {
    1.45 -  Finger f;
    1.46 -  Point cv;
    1.47 -  float dtheta,dDist;
    1.48 -  DollarPath dollarPath;
    1.49 -} TouchPoint;
    1.50 -*/
    1.51 -typedef struct {
    1.52 -  Point path[DOLLARNPOINTS];
    1.53 +  SDL_FloatPoint path[DOLLARNPOINTS];
    1.54    unsigned long hash;
    1.55 -} DollarTemplate;
    1.56 +} SDL_DollarTemplate;
    1.57  
    1.58  typedef struct {
    1.59    SDL_GestureID id;
    1.60 -  Point res;
    1.61 -  Point centroid;
    1.62 -  //TouchPoint gestureLast[MAXFINGERS];
    1.63 -  DollarPath dollarPath;
    1.64 +  SDL_FloatPoint res;
    1.65 +  SDL_FloatPoint centroid;
    1.66 +  SDL_DollarPath dollarPath;
    1.67    Uint16 numDownFingers;
    1.68  
    1.69    int numDollarTemplates;
    1.70 -  DollarTemplate dollarTemplate[MAXTEMPLATES];
    1.71 +  SDL_DollarTemplate *dollarTemplate;
    1.72  
    1.73    SDL_bool recording;
    1.74 -} GestureTouch;
    1.75 +} SDL_GestureTouch;
    1.76  
    1.77 -GestureTouch gestureTouch[MAXTOUCHES];
    1.78 -int numGestureTouches = 0;
    1.79 +SDL_GestureTouch *SDL_gestureTouch;
    1.80 +int SDL_numGestureTouches = 0;
    1.81  SDL_bool recordAll;
    1.82  
    1.83 -void SDL_PrintPath(Point *path) {
    1.84 +void SDL_PrintPath(SDL_FloatPoint *path) {
    1.85    int i;
    1.86    printf("Path:");
    1.87    for(i=0;i<DOLLARNPOINTS;i++) {
    1.88 @@ -102,9 +86,9 @@
    1.89  int SDL_RecordGesture(SDL_TouchID touchId) {
    1.90    int i;
    1.91    if(touchId < 0) recordAll = SDL_TRUE;
    1.92 -  for(i = 0;i < numGestureTouches; i++) {
    1.93 -    if((touchId < 0) || (gestureTouch[i].id == touchId)) {
    1.94 -      gestureTouch[i].recording = SDL_TRUE;
    1.95 +  for(i = 0;i < SDL_numGestureTouches; i++) {
    1.96 +    if((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
    1.97 +      SDL_gestureTouch[i].recording = SDL_TRUE;
    1.98        if(touchId >= 0)
    1.99  	return 1;
   1.100      }      
   1.101 @@ -112,7 +96,7 @@
   1.102    return (touchId < 0);
   1.103  }
   1.104  
   1.105 -unsigned long SDL_HashDollar(Point* points) {
   1.106 +unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
   1.107    unsigned long hash = 5381;
   1.108    int i;
   1.109    for(i = 0;i < DOLLARNPOINTS; i++) { 
   1.110 @@ -123,14 +107,14 @@
   1.111  }
   1.112  
   1.113  
   1.114 -static int SaveTemplate(DollarTemplate *templ, SDL_RWops * src) {
   1.115 +static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
   1.116    if(src == NULL) return 0;
   1.117  
   1.118    int i;
   1.119    
   1.120    //No Longer storing the Hash, rehash on load
   1.121    //if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
   1.122 -
   1.123 +  
   1.124    if(SDL_RWwrite(src,templ->path,
   1.125  		 sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) 
   1.126      return 0;
   1.127 @@ -141,8 +125,8 @@
   1.128  
   1.129  int SDL_SaveAllDollarTemplates(SDL_RWops *src) {  
   1.130    int i,j,rtrn = 0;
   1.131 -  for(i = 0; i < numGestureTouches; i++) {
   1.132 -    GestureTouch* touch = &gestureTouch[i];
   1.133 +  for(i = 0; i < SDL_numGestureTouches; i++) {
   1.134 +    SDL_GestureTouch* touch = &SDL_gestureTouch[i];
   1.135      for(j = 0;j < touch->numDollarTemplates; j++) {
   1.136  	rtrn += SaveTemplate(&touch->dollarTemplate[i],src);
   1.137      }
   1.138 @@ -152,8 +136,8 @@
   1.139  
   1.140  int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
   1.141    int i,j;
   1.142 -  for(i = 0; i < numGestureTouches; i++) {
   1.143 -    GestureTouch* touch = &gestureTouch[i];
   1.144 +  for(i = 0; i < SDL_numGestureTouches; i++) {
   1.145 +    SDL_GestureTouch* touch = &SDL_gestureTouch[i];
   1.146      for(j = 0;j < touch->numDollarTemplates; j++) {
   1.147        if(touch->dollarTemplate[i].hash == gestureId) {
   1.148  	return SaveTemplate(&touch->dollarTemplate[i],src);
   1.149 @@ -166,25 +150,46 @@
   1.150  
   1.151  //path is an already sampled set of points
   1.152  //Returns the index of the gesture on success, or -1
   1.153 -static int SDL_AddDollarGesture(GestureTouch* inTouch,Point* path) {
   1.154 +static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) {
   1.155    if(inTouch == NULL) {
   1.156 -    if(numGestureTouches == 0) return -1;
   1.157 +    if(SDL_numGestureTouches == 0) return -1;
   1.158      int i = 0;
   1.159 -    for(i = 0;i < numGestureTouches; i++) {
   1.160 -      inTouch = &gestureTouch[i];
   1.161 -      if(inTouch->numDollarTemplates < MAXTEMPLATES) {
   1.162 -	DollarTemplate *templ = 
   1.163 -	  &inTouch->dollarTemplate[inTouch->numDollarTemplates];
   1.164 -	memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
   1.165 -	templ->hash = SDL_HashDollar(templ->path);
   1.166 -	inTouch->numDollarTemplates++;
   1.167 +    for(i = 0;i < SDL_numGestureTouches; i++) {
   1.168 +      inTouch = &SDL_gestureTouch[i];
   1.169 +
   1.170 +      SDL_DollarTemplate* dollarTemplate = 
   1.171 +	SDL_realloc(inTouch->dollarTemplate,
   1.172 +		    (inTouch->numDollarTemplates + 1) * 
   1.173 +		    sizeof(SDL_DollarTemplate));
   1.174 +      if(!dollarTemplate) {
   1.175 +	SDL_OutOfMemory();
   1.176 +	return -1;
   1.177        }
   1.178 +	
   1.179 +      inTouch->dollarTemplate = dollarTemplate;
   1.180 +
   1.181 +      SDL_DollarTemplate *templ = 
   1.182 +	&inTouch->dollarTemplate[inTouch->numDollarTemplates];
   1.183 +      memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
   1.184 +      templ->hash = SDL_HashDollar(templ->path);
   1.185 +      inTouch->numDollarTemplates++;    
   1.186      }
   1.187      return inTouch->numDollarTemplates - 1;
   1.188 -  }else if(inTouch->numDollarTemplates < MAXTEMPLATES) {
   1.189 -    DollarTemplate *templ = 
   1.190 +  } else {
   1.191 +    SDL_DollarTemplate* dollarTemplate = 
   1.192 +      SDL_realloc(inTouch->dollarTemplate,
   1.193 +		  (inTouch->numDollarTemplates + 1) * 
   1.194 +		  sizeof(SDL_DollarTemplate));
   1.195 +    if(!dollarTemplate) {
   1.196 +      SDL_OutOfMemory();
   1.197 +      return -1;
   1.198 +    }
   1.199 +    
   1.200 +    inTouch->dollarTemplate = dollarTemplate;
   1.201 +
   1.202 +    SDL_DollarTemplate *templ = 
   1.203        &inTouch->dollarTemplate[inTouch->numDollarTemplates];
   1.204 -    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(Point));
   1.205 +    memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
   1.206      templ->hash = SDL_HashDollar(templ->path);
   1.207      inTouch->numDollarTemplates++;
   1.208      return inTouch->numDollarTemplates - 1;
   1.209 @@ -195,36 +200,28 @@
   1.210  int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
   1.211    if(src == NULL) return 0;
   1.212    int i,loaded = 0;
   1.213 -  GestureTouch *touch = NULL;
   1.214 +  SDL_GestureTouch *touch = NULL;
   1.215    if(touchId >= 0) {
   1.216 -    for(i = 0;i < numGestureTouches; i++)
   1.217 -      if(gestureTouch[i].id == touchId)
   1.218 -	touch = &gestureTouch[i];
   1.219 +    for(i = 0;i < SDL_numGestureTouches; i++)
   1.220 +      if(SDL_gestureTouch[i].id == touchId)
   1.221 +	touch = &SDL_gestureTouch[i];
   1.222      if(touch == NULL) return -1;
   1.223    }
   1.224  
   1.225    while(1) {
   1.226 -    DollarTemplate templ;
   1.227 -    //fscanf(fp,"%lu ",&templ.hash);
   1.228 -    /*
   1.229 -    for(i = 0;i < DOLLARNPOINTS; i++) {		
   1.230 -      int x,y;
   1.231 -      if(fscanf(fp,"%i %i ",&x,&y) != 2) break;
   1.232 -      templ.path[i].x = x;
   1.233 -      templ.path[i].y = y;
   1.234 -    }
   1.235 -    fscanf(fp,"\n");
   1.236 -    */
   1.237 -    if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) break;
   1.238 +    SDL_DollarTemplate templ;
   1.239 +
   1.240 +    if(SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < 
   1.241 +       DOLLARNPOINTS) break;
   1.242  
   1.243      if(touchId >= 0) {
   1.244        printf("Adding loaded gesture to 1 touch\n");
   1.245        if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
   1.246      }
   1.247      else {
   1.248 -      printf("Adding to: %i touches\n",numGestureTouches);
   1.249 -      for(i = 0;i < numGestureTouches; i++) {
   1.250 -	touch = &gestureTouch[i];
   1.251 +      printf("Adding to: %i touches\n",SDL_numGestureTouches);
   1.252 +      for(i = 0;i < SDL_numGestureTouches; i++) {
   1.253 +	touch = &SDL_gestureTouch[i];
   1.254  	printf("Adding loaded gesture to + touches\n");
   1.255  	//TODO: What if this fails?
   1.256  	SDL_AddDollarGesture(touch,templ.path);	
   1.257 @@ -237,10 +234,10 @@
   1.258  }
   1.259  
   1.260  
   1.261 -float dollarDifference(Point* points,Point* templ,float ang) {
   1.262 -  //  Point p[DOLLARNPOINTS];
   1.263 +float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang) {
   1.264 +  //  SDL_FloatPoint p[DOLLARNPOINTS];
   1.265    float dist = 0;
   1.266 -  Point p;
   1.267 +  SDL_FloatPoint p;
   1.268    int i;
   1.269    for(i = 0; i < DOLLARNPOINTS; i++) {
   1.270      p.x = points[i].x * cos(ang) - points[i].y * sin(ang);
   1.271 @@ -252,7 +249,7 @@
   1.272    
   1.273  }
   1.274  
   1.275 -float bestDollarDifference(Point* points,Point* templ) {
   1.276 +float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) {
   1.277    //------------BEGIN DOLLAR BLACKBOX----------------//
   1.278    //-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
   1.279    //-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
   1.280 @@ -289,10 +286,9 @@
   1.281  }
   1.282  
   1.283  //DollarPath contains raw points, plus (possibly) the calculated length
   1.284 -int dollarNormalize(DollarPath path,Point *points) {
   1.285 +int dollarNormalize(SDL_DollarPath path,SDL_FloatPoint *points) {
   1.286    int i;
   1.287    //Calculate length if it hasn't already been done
   1.288 -  printf("length: %f\n",path.length);
   1.289    if(path.length <= 0) {
   1.290      for(i=1;i<path.numPoints;i++) {
   1.291        float dx = path.p[i  ].x - 
   1.292 @@ -302,14 +298,13 @@
   1.293        path.length += sqrt(dx*dx+dy*dy);
   1.294      }
   1.295    }
   1.296 -  printf("New length: %f\n",path.length);
   1.297  
   1.298    //Resample
   1.299    float interval = path.length/(DOLLARNPOINTS - 1);
   1.300    float dist = interval;
   1.301  
   1.302    int numPoints = 0;
   1.303 -  Point centroid; 
   1.304 +  SDL_FloatPoint centroid; 
   1.305    centroid.x = 0;centroid.y = 0;
   1.306    
   1.307    //printf("(%f,%f)\n",path.p[path.numPoints-1].x,path.p[path.numPoints-1].y);
   1.308 @@ -378,9 +373,9 @@
   1.309    return numPoints;
   1.310  }
   1.311  
   1.312 -float dollarRecognize(DollarPath path,int *bestTempl,GestureTouch* touch) {
   1.313 +float dollarRecognize(SDL_DollarPath path,int *bestTempl,SDL_GestureTouch* touch) {
   1.314  	
   1.315 -	Point points[DOLLARNPOINTS];
   1.316 +	SDL_FloatPoint points[DOLLARNPOINTS];
   1.317  	int numPoints = dollarNormalize(path,points);
   1.318  	//SDL_PrintPath(points);
   1.319  	int i;
   1.320 @@ -394,34 +389,56 @@
   1.321  	return bestDiff;
   1.322  }
   1.323  
   1.324 -int SDL_GestureAddTouch(SDL_Touch* touch) { 
   1.325 -  if(numGestureTouches >= MAXTOUCHES) return -1;
   1.326 -  
   1.327 -  gestureTouch[numGestureTouches].res.x = touch->xres;
   1.328 -  gestureTouch[numGestureTouches].res.y = touch->yres;
   1.329 -  gestureTouch[numGestureTouches].numDownFingers = 0;
   1.330 +int SDL_GestureAddTouch(SDL_Touch* touch) {  
   1.331 +  SDL_GestureTouch *gestureTouch = SDL_realloc(SDL_gestureTouch,
   1.332 +					       (SDL_numGestureTouches + 1) *
   1.333 +					       sizeof(SDL_GestureTouch));
   1.334  
   1.335 -  gestureTouch[numGestureTouches].res.x = touch->xres;
   1.336 -  gestureTouch[numGestureTouches].id = touch->id;
   1.337 +  if(!gestureTouch) {
   1.338 +    SDL_OutOfMemory();
   1.339 +    return -1;
   1.340 +  }
   1.341  
   1.342 -  gestureTouch[numGestureTouches].numDollarTemplates = 0;
   1.343 +  SDL_gestureTouch = gestureTouch;
   1.344  
   1.345 -  gestureTouch[numGestureTouches].recording = SDL_FALSE;
   1.346 +  SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres;
   1.347 +  SDL_gestureTouch[SDL_numGestureTouches].res.y = touch->yres;
   1.348 +  SDL_gestureTouch[SDL_numGestureTouches].numDownFingers = 0;
   1.349  
   1.350 -  numGestureTouches++;
   1.351 +  SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres;
   1.352 +  SDL_gestureTouch[SDL_numGestureTouches].id = touch->id;
   1.353 +
   1.354 +  SDL_gestureTouch[SDL_numGestureTouches].numDollarTemplates = 0;
   1.355 +
   1.356 +  SDL_gestureTouch[SDL_numGestureTouches].recording = SDL_FALSE;
   1.357 +
   1.358 +  SDL_numGestureTouches++;
   1.359    return 0;
   1.360  }
   1.361  
   1.362 -GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) {
   1.363 +int SDL_GestureRemoveTouch(SDL_TouchID id) {
   1.364    int i;
   1.365 -  for(i = 0;i < numGestureTouches; i++) {
   1.366 -    //printf("%i ?= %i\n",gestureTouch[i].id,id);
   1.367 -    if(gestureTouch[i].id == id) return &gestureTouch[i];
   1.368 +  for(i = 0;i < SDL_numGestureTouches; i++) {
   1.369 +    if(SDL_gestureTouch[i].id == id) {
   1.370 +      SDL_numGestureTouches--;
   1.371 +      SDL_gestureTouch[i] = SDL_gestureTouch[SDL_numGestureTouches];
   1.372 +      return 1;
   1.373 +    }
   1.374 +  }
   1.375 +  return -1;
   1.376 +}
   1.377 +
   1.378 +
   1.379 +SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) {
   1.380 +  int i;
   1.381 +  for(i = 0;i < SDL_numGestureTouches; i++) {
   1.382 +    //printf("%i ?= %i\n",SDL_gestureTouch[i].id,id);
   1.383 +    if(SDL_gestureTouch[i].id == id) return &SDL_gestureTouch[i];
   1.384    }
   1.385    return NULL;
   1.386  }
   1.387  
   1.388 -int SDL_SendGestureMulti(GestureTouch* touch,float dTheta,float dDist) {
   1.389 +int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist) {
   1.390    SDL_Event event;
   1.391    event.mgesture.type = SDL_MULTIGESTURE;
   1.392    event.mgesture.touchId = touch->id;
   1.393 @@ -433,7 +450,7 @@
   1.394    return SDL_PushEvent(&event) > 0;
   1.395  }
   1.396  
   1.397 -int SDL_SendGestureDollar(GestureTouch* touch,
   1.398 +int SDL_SendGestureDollar(SDL_GestureTouch* touch,
   1.399  			  SDL_GestureID gestureId,float error) {
   1.400    SDL_Event event;
   1.401    event.dgesture.type = SDL_DOLLARGESTURE;
   1.402 @@ -449,7 +466,7 @@
   1.403  }
   1.404  
   1.405  
   1.406 -int SDL_SendDollarRecord(GestureTouch* touch,SDL_GestureID gestureId) {
   1.407 +int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId) {
   1.408    SDL_Event event;
   1.409    event.dgesture.type = SDL_DOLLARRECORD;
   1.410    event.dgesture.touchId = touch->id;
   1.411 @@ -464,7 +481,7 @@
   1.412    if(event->type == SDL_FINGERMOTION || 
   1.413       event->type == SDL_FINGERDOWN ||
   1.414       event->type == SDL_FINGERUP) {
   1.415 -    GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
   1.416 +    SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
   1.417      
   1.418      //Shouldn't be possible
   1.419      if(inTouch == NULL) return;
   1.420 @@ -485,15 +502,15 @@
   1.421  #ifdef ENABLE_DOLLAR
   1.422        if(inTouch->recording) {
   1.423  	inTouch->recording = SDL_FALSE;
   1.424 -	Point path[DOLLARNPOINTS];
   1.425 +	SDL_FloatPoint path[DOLLARNPOINTS];
   1.426  	dollarNormalize(inTouch->dollarPath,path);
   1.427  	//SDL_PrintPath(path);
   1.428  	int index;
   1.429  	if(recordAll) {
   1.430  	  index = SDL_AddDollarGesture(NULL,path);
   1.431  	  int i;
   1.432 -	  for(i = 0;i < numGestureTouches; i++)
   1.433 -	    gestureTouch[i].recording = SDL_FALSE;
   1.434 +	  for(i = 0;i < SDL_numGestureTouches; i++)
   1.435 +	    SDL_gestureTouch[i].recording = SDL_FALSE;
   1.436  	}
   1.437  	else {
   1.438  	  index = SDL_AddDollarGesture(inTouch,path);
   1.439 @@ -532,7 +549,7 @@
   1.440        float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y;
   1.441        //printf("dx,dy: (%f,%f)\n",dx,dy); 
   1.442  #ifdef ENABLE_DOLLAR
   1.443 -      DollarPath* path = &inTouch->dollarPath;
   1.444 +      SDL_DollarPath* path = &inTouch->dollarPath;
   1.445        if(path->numPoints < MAXPATHSIZE) {
   1.446  	path->p[path->numPoints].x = inTouch->centroid.x;
   1.447  	path->p[path->numPoints].y = inTouch->centroid.y;
   1.448 @@ -544,18 +561,18 @@
   1.449  	path->numPoints++;
   1.450        }
   1.451  #endif
   1.452 -      Point lastP;
   1.453 +      SDL_FloatPoint lastP;
   1.454        lastP.x = x - dx;
   1.455        lastP.y = y - dy;
   1.456 -      Point lastCentroid;
   1.457 +      SDL_FloatPoint lastCentroid;
   1.458        lastCentroid = inTouch->centroid;
   1.459        
   1.460        inTouch->centroid.x += dx/inTouch->numDownFingers;
   1.461        inTouch->centroid.y += dy/inTouch->numDownFingers;
   1.462        //printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
   1.463        if(inTouch->numDownFingers > 1) {
   1.464 -	Point lv; //Vector from centroid to last x,y position
   1.465 -	Point v; //Vector from centroid to current x,y position
   1.466 +	SDL_FloatPoint lv; //Vector from centroid to last x,y position
   1.467 +	SDL_FloatPoint v; //Vector from centroid to current x,y position
   1.468  	//lv = inTouch->gestureLast[j].cv;
   1.469  	lv.x = lastP.x - lastCentroid.x;
   1.470  	lv.y = lastP.y - lastCentroid.y;
   1.471 @@ -608,15 +625,9 @@
   1.472  			     x)/inTouch->numDownFingers;
   1.473        inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
   1.474  			     y)/inTouch->numDownFingers;
   1.475 -      printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
   1.476 -	     inTouch->centroid.x,inTouch->centroid.y);
   1.477 -      /*
   1.478 -	inTouch->gestureLast[j].f.id = event->tfinger.fingerId;
   1.479 -	inTouch->gestureLast[j].f.p.x  = x;
   1.480 -	inTouch->gestureLast[j].f.p.y  = y;	
   1.481 -	inTouch->gestureLast[j].cv.x = 0;
   1.482 -	inTouch->gestureLast[j].cv.y = 0;
   1.483 -      */
   1.484 +      //printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
   1.485 +      //     inTouch->centroid.x,inTouch->centroid.y);
   1.486 +
   1.487  #ifdef ENABLE_DOLLAR
   1.488        inTouch->dollarPath.length = 0;
   1.489        inTouch->dollarPath.p[0].x = x;
     2.1 --- a/touchTest/gestureSDLTest.c	Fri Aug 13 17:05:06 2010 -0700
     2.2 +++ b/touchTest/gestureSDLTest.c	Fri Aug 13 23:08:32 2010 -0400
     2.3 @@ -26,83 +26,33 @@
     2.4  #endif
     2.5  #endif
     2.6  
     2.7 -#define PI 3.1415926535897
     2.8 -#define PHI ((sqrt(5)-1)/2)
     2.9  #define WIDTH 640
    2.10  #define HEIGHT 480
    2.11  #define BPP 4
    2.12  #define DEPTH 32
    2.13  
    2.14 -#define MAXFINGERS 5
    2.15 -
    2.16 -#define DOLLARNPOINTS 64
    2.17 -#define DOLLARSIZE 256
    2.18 -
    2.19  //MUST BE A POWER OF 2!
    2.20  #define EVENT_BUF_SIZE 256
    2.21  
    2.22 +
    2.23 +#define VERBOSE SDL_FALSE
    2.24 +
    2.25  SDL_Event events[EVENT_BUF_SIZE];
    2.26  int eventWrite;
    2.27  
    2.28 -int mousx,mousy;
    2.29 -int keystat[512];
    2.30 -int bstatus;
    2.31 -
    2.32  int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF};
    2.33  
    2.34 -SDL_FingerID index2fingerid[MAXFINGERS];
    2.35 -int fingersDown;
    2.36 -
    2.37  typedef struct {
    2.38    float x,y;
    2.39  } Point;
    2.40  
    2.41  typedef struct {
    2.42 -  Point p;
    2.43 -  float pressure;
    2.44 -  SDL_FingerID id;
    2.45 -} Finger;
    2.46 -
    2.47 -typedef struct {
    2.48 -  Finger f;
    2.49 -  Point cv;
    2.50 -  float dtheta,dDist;
    2.51 -} TouchPoint;
    2.52 -
    2.53 - 
    2.54 -typedef struct { //dt + s
    2.55 -  Point d,s; //direction, start
    2.56 -  int points;
    2.57 -} Line;
    2.58 -
    2.59 -
    2.60 -typedef struct {
    2.61 -  float length;
    2.62 -  
    2.63 -  int numPoints;
    2.64 -  Point p[EVENT_BUF_SIZE]; //To be safe
    2.65 -} DollarPath;
    2.66 -
    2.67 -typedef struct {
    2.68    float ang,r;
    2.69    Point p;
    2.70  } Knob;
    2.71  
    2.72  Knob knob;
    2.73  
    2.74 -Finger finger[MAXFINGERS];
    2.75 -
    2.76 -
    2.77 -DollarPath dollarPath[MAXFINGERS];
    2.78 -
    2.79 -#define MAXTEMPLATES 4
    2.80 -
    2.81 -Point dollarTemplate[MAXTEMPLATES][DOLLARNPOINTS];
    2.82 -int numDollarTemplates = 0;
    2.83 -#ifdef DRAW_VECTOR_EST
    2.84 -Line gestureLine[MAXFINGERS];
    2.85 -#endif
    2.86 -
    2.87  void handler (int sig)
    2.88  {
    2.89    printf ("\exiting...(%d)\n", sig);
    2.90 @@ -115,7 +65,6 @@
    2.91    handler (9);
    2.92  }
    2.93  
    2.94 -
    2.95  void setpix(SDL_Surface *screen, int x, int y, unsigned int col)
    2.96  {
    2.97    Uint32 *pixmem32;
    2.98 @@ -150,13 +99,10 @@
    2.99    for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1)))
   2.100      setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col);
   2.101  }
   2.102 +
   2.103  void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c)
   2.104  {
   2.105 -
   2.106 -  float a;
   2.107 -  int tx;
   2.108 -  
   2.109 -  int ty;
   2.110 +  int tx,ty;
   2.111    float xr;
   2.112    for(ty = -abs(r);ty <= abs(r);ty++) {
   2.113      xr = sqrt(r*r - ty*ty);
   2.114 @@ -173,50 +119,24 @@
   2.115  }
   2.116  
   2.117  void drawKnob(SDL_Surface* screen,Knob k) {
   2.118 -  //printf("Knob: x = %f, y = %f, r = %f, a = %f\n",k.p.x,k.p.y,k.r,k.ang);
   2.119 - 
   2.120 -  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);
   2.121 -  
   2.122 +  drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF);  
   2.123    drawCircle(screen,(k.p.x+k.r/2*cos(k.ang))*screen->w,
   2.124    	            (k.p.y+k.r/2*sin(k.ang))*screen->h,k.r/4*screen->w,0);
   2.125 -  
   2.126  }
   2.127  
   2.128 -void DrawScreen(SDL_Surface* screen, int h)
   2.129 +void DrawScreen(SDL_Surface* screen)
   2.130  {
   2.131 -  int x, y, xm,ym,c;
   2.132 +  int x, y;
   2.133    if(SDL_MUSTLOCK(screen))
   2.134      {                                              
   2.135        if(SDL_LockSurface(screen) < 0) return;
   2.136      }
   2.137 -  for(y = 0; y < screen->h; y++ )
   2.138 -    {
   2.139 -      for( x = 0; x < screen->w; x++ )
   2.140 -        {
   2.141 -	  //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
   2.142 -	  //xm = (x+h)%screen->w;
   2.143 -	  //ym = (y+h)%screen->w;
   2.144 -	  //c = sin(h/256*2*PI)*x*y/screen->w/screen->h;
   2.145 -	  //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c);
   2.146 -	  setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   2.147 -	  //setpix(screen,x,y,0); //Inefficient, but that's okay...
   2.148 -        }
   2.149 -    }
   2.150 -  drawCircle(screen,mousx,mousy,-30,0xFFFFFF);
   2.151 -  drawLine(screen,0,0,screen->w,screen->h,0xFFFFFF);
   2.152 +  for(y = 0;y < screen->h;y++)
   2.153 +    for(x = 0;x < screen->w;x++)
   2.154 +	setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
   2.155  
   2.156    int i;
   2.157 -//draw Touch History
   2.158 -  TouchPoint gestureLast[MAXFINGERS];
   2.159 -  //printf("------------------Start History------------------\n");
   2.160 -  for(i = 0;i < MAXFINGERS;i++) {
   2.161 -    gestureLast[i].f.id = -1;
   2.162 -  }
   2.163 -  int numDownFingers = 0;
   2.164 -  Point centroid;
   2.165 -  float gdtheta,gdDist;
   2.166 -
   2.167 -
   2.168 +  //draw Touch History
   2.169    for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) {
   2.170      SDL_Event event = events[i&(EVENT_BUF_SIZE-1)];
   2.171      int age = eventWrite - i - 1;
   2.172 @@ -224,13 +144,12 @@
   2.173         event.type == SDL_FINGERDOWN ||
   2.174         event.type == SDL_FINGERUP) {
   2.175        SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   2.176 -      //SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   2.177 -	    
   2.178 +      if(inTouch == NULL) continue;
   2.179 +
   2.180        float x = ((float)event.tfinger.x)/inTouch->xres;
   2.181        float y = ((float)event.tfinger.y)/inTouch->yres;      
   2.182        
   2.183 -      //draw the touch:
   2.184 -      
   2.185 +      //draw the touch:      
   2.186        unsigned int c = colors[event.tfinger.touchId%7]; 
   2.187        unsigned int col = 
   2.188  	((unsigned int)(c*(.1+.85))) |
   2.189 @@ -243,25 +162,10 @@
   2.190      }
   2.191    }
   2.192    
   2.193 -  /*
   2.194 -  for(i=0;i<MAXFINGERS;i++)
   2.195 -    if(finger[i].p.x >= 0 && finger[i].p.y >= 0)
   2.196 -      if(finger[i].pressure > 0)
   2.197 -	drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h
   2.198 -		   ,20,0xFF*finger[i].pressure);
   2.199 -      else
   2.200 -	drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h
   2.201 -		   ,20,0xFF);
   2.202 -  */
   2.203 -
   2.204 -  
   2.205 -  keystat[32] = 0;
   2.206 -  
   2.207    if(knob.p.x > 0)
   2.208      drawKnob(screen,knob);
   2.209    
   2.210    if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
   2.211 -  
   2.212    SDL_Flip(screen);
   2.213  }
   2.214  
   2.215 @@ -275,21 +179,15 @@
   2.216  {  
   2.217    SDL_Surface *screen;
   2.218    SDL_Event event;
   2.219 -  
   2.220 -  int keypress = 0;
   2.221 -  int h=0,s=1,i,j;
   2.222  
   2.223    //gesture variables
   2.224 -  int numDownFingers = 0;
   2.225 -  float gdtheta = 0,gdDist = 0;
   2.226 -  Point centroid;
   2.227    knob.r = .1;
   2.228    knob.ang = 0;
   2.229 -  TouchPoint gestureLast[MAXFINGERS];
   2.230 +
   2.231    
   2.232 +  SDL_bool quitting = SDL_FALSE;
   2.233 +  SDL_RWops *src;
   2.234  
   2.235 -
   2.236 -  memset(keystat,0,512*sizeof(keystat[0]));
   2.237    if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
   2.238    
   2.239    if (!(screen = initScreen(WIDTH,HEIGHT)))
   2.240 @@ -298,8 +196,7 @@
   2.241        return 1;
   2.242      }
   2.243  
   2.244 -  while(!keystat[27]) {
   2.245 -    //Poll SDL
   2.246 +  while(!quitting) {
   2.247      while(SDL_PollEvent(&event)) 
   2.248        {
   2.249  	//Record _all_ events
   2.250 @@ -309,38 +206,28 @@
   2.251  	switch (event.type) 
   2.252  	  {
   2.253  	  case SDL_QUIT:
   2.254 -	    keystat[27] = 1;
   2.255 +	    quitting = SDL_TRUE;
   2.256  	    break;
   2.257  	  case SDL_KEYDOWN:
   2.258 -	    //printf("%i\n",event.key.keysym.sym);
   2.259 -	    keystat[event.key.keysym.sym] = 1;
   2.260 -	    if(event.key.keysym.sym == 32) {
   2.261 -	      SDL_RecordGesture(-1);
   2.262 +	    switch (event.key.keysym.sym)
   2.263 +	      {
   2.264 +	      case SDLK_SPACE:
   2.265 +		SDL_RecordGesture(-1);
   2.266 +		break;
   2.267 +	      case SDLK_s:
   2.268 +		src = SDL_RWFromFile("gestureSave","w");
   2.269 +		printf("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src));
   2.270 +		SDL_RWclose(src);
   2.271 +		break;
   2.272 +	      case SDLK_l:
   2.273 +		src = SDL_RWFromFile("gestureSave","r");
   2.274 +		printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src));
   2.275 +		SDL_RWclose(src);
   2.276 +		break;
   2.277 +	      case SDLK_ESCAPE:
   2.278 +		quitting = SDL_TRUE;
   2.279 +		break;
   2.280  	    }
   2.281 -	    else if(event.key.keysym.sym == 115) {
   2.282 -	      SDL_RWops *src;
   2.283 -	      //fp = fopen("gestureSave","w");
   2.284 -	      src = SDL_RWFromFile("gestureSave","w");
   2.285 -	      
   2.286 -	      printf("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src));
   2.287 -	      //fclose(fp);
   2.288 -	      SDL_RWclose(src);
   2.289 -	    }
   2.290 -	    else if(event.key.keysym.sym == 108) {
   2.291 -	      SDL_RWops *src;
   2.292 -	      //fp = fopen("gestureSave","r");
   2.293 -	      src = SDL_RWFromFile("gestureSave","r");
   2.294 -	      printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src));
   2.295 -	      //fclose(fp);
   2.296 -	      SDL_RWclose(src);
   2.297 -	    }
   2.298 -	    
   2.299 -	    //keypress = 1;
   2.300 -	    break;
   2.301 -	  case SDL_KEYUP:
   2.302 -	      //printf("%i\n",event.key.keysym.sym);
   2.303 -	    keystat[event.key.keysym.sym] = 0;
   2.304 -	    //keypress = 1;
   2.305  	    break;
   2.306  	  case SDL_VIDEORESIZE:
   2.307  	    if (!(screen = initScreen(event.resize.w,
   2.308 @@ -350,78 +237,36 @@
   2.309  		return 1;
   2.310  	      }
   2.311  	    break;
   2.312 -	  case SDL_MOUSEMOTION:
   2.313 -	    mousx = event.motion.x;
   2.314 -	    mousy = event.motion.y; 
   2.315 -	    break;
   2.316 -	  case SDL_MOUSEBUTTONDOWN:
   2.317 -	    bstatus |=  (1<<(event.button.button-1));
   2.318 -	    break;
   2.319 -	  case SDL_MOUSEBUTTONUP:
   2.320 -	    bstatus &= ~(1<<(event.button.button-1));
   2.321 -	    break;
   2.322  	  case SDL_FINGERMOTION:    
   2.323  	    ;
   2.324 -	    //printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
   2.325 -	    //	   event.tfinger.x,event.tfinger.y);
   2.326 +#if VERBOSE
   2.327 +	    printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
   2.328 +	    	   event.tfinger.x,event.tfinger.y);
   2.329 +#endif
   2.330  	    SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
   2.331  	    SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
   2.332 -	    /*
   2.333 -	    for(i = 0;i<MAXFINGERS;i++) 
   2.334 -	      if(index2fingerid[i] == event.tfinger.fingerId) 	      
   2.335 -		break;
   2.336 -	    if(i == MAXFINGERS) break;  
   2.337 -	    if(inTouch > 0) {
   2.338 -	      finger[i].p.x = ((float)event.tfinger.x)/
   2.339 -		inTouch->xres;
   2.340 -	      finger[i].p.y = ((float)event.tfinger.y)/
   2.341 -		inTouch->yres;
   2.342 -	      
   2.343 -	      finger[i].pressure = 
   2.344 -		((float)event.tfinger.pressure)/inTouch->pressureres;
   2.345 -	    */
   2.346 -	      /*
   2.347 -	      printf("Finger: %i, Pressure: %f Pressureres: %i\n",
   2.348 -		     event.tfinger.fingerId,
   2.349 -		     finger[i].pressure,
   2.350 -		     inTouch->pressureres);
   2.351 -	      */
   2.352 -	      //printf("Finger: %i, pressure: %f\n",event.tfinger.fingerId,
   2.353 -	      //   finger[event.tfinger.fingerId].pressure);
   2.354 -	    //}
   2.355 -	    
   2.356  	    break;	    
   2.357  	  case SDL_FINGERDOWN:
   2.358 -	    //printf("Finger: %"PRIs64" down - x: %i, y: %i\n",event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   2.359 -	    /*
   2.360 -	    for(i = 0;i<MAXFINGERS;i++) 
   2.361 -	      if(index2fingerid[i] == -1) {
   2.362 -		index2fingerid[i] = event.tfinger.fingerId;
   2.363 -		break;
   2.364 -	      }
   2.365 -	    finger[i].p.x = event.tfinger.x;
   2.366 -	    finger[i].p.y = event.tfinger.y;
   2.367 +#if VERBOSE
   2.368 +	    printf("Finger: %"PRIs64" down - x: %i, y: %i\n",
   2.369 +		   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   2.370 +#endif
   2.371  	    break;
   2.372  	  case SDL_FINGERUP:
   2.373 -	    //printf("Finger: %"PRIs64" up - x: %i, y: %i\n",event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   2.374 -	    for(i = 0;i<MAXFINGERS;i++) 
   2.375 -	      if(index2fingerid[i] == event.tfinger.fingerId) {
   2.376 -		index2fingerid[i] = -1;
   2.377 -		break;
   2.378 -	      }
   2.379 -	    finger[i].p.x = -1;
   2.380 -	    finger[i].p.y = -1;
   2.381 -	    */
   2.382 +#if VERBOSE
   2.383 +	    printf("Finger: %"PRIs64" up - x: %i, y: %i\n",
   2.384 +	    	   event.tfinger.fingerId,event.tfinger.x,event.tfinger.y);
   2.385 +#endif
   2.386  	    break;
   2.387  	  case SDL_MULTIGESTURE:
   2.388 -	    /*
   2.389 +#if VERBOSE	    
   2.390  	    printf("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f\n",
   2.391  		   event.mgesture.x,
   2.392  		   event.mgesture.y,
   2.393  		   event.mgesture.dTheta,
   2.394  		   event.mgesture.dDist);
   2.395  	    printf("MG: numDownTouch = %i\n",event.mgesture.numFingers);
   2.396 -	    */
   2.397 +#endif
   2.398  	    knob.p.x = event.mgesture.x;
   2.399  	    knob.p.y = event.mgesture.y;
   2.400  	    knob.ang += event.mgesture.dTheta;
   2.401 @@ -437,13 +282,9 @@
   2.402  	    break;
   2.403  	  }
   2.404        }
   2.405 -    DrawScreen(screen,h);    
   2.406 -    //for(i = 0; i < 256; i++) 
   2.407 -    //  if(keystat[i]) 
   2.408 -    //  printf("Key %i down\n",i);
   2.409 +    DrawScreen(screen);
   2.410    }  
   2.411 -  SDL_Quit();
   2.412 -  
   2.413 +  SDL_Quit();  
   2.414    return 0;
   2.415  }
   2.416