src/events/SDL_gesture.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 18 Feb 2019 07:50:33 -0800
changeset 12612 07c39cbbeacf
parent 12503 806492103856
child 13028 1e4d6b83f946
permissions -rw-r--r--
Fixed bug 4500 - Heap-Buffer Overflow in Map1toN pertaining to SDL_pixels.c

Petr Pisar

The reproducer has these data in BITMAPINFOHEADER:

biSize = 40
biBitCount = 8
biClrUsed = 131075

SDL_LoadBMP_RW() function passes biBitCount as a color depth to SDL_CreateRGBSurface(), thus 256-color pallete is allocated. But then biClrUsed colors are read from a file and stored into the palette. SDL_LoadBMP_RW should report an error if biClrUsed is greater than 2^biBitCount.
jim@4657
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
jim@4657
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
jim@4657
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
jim@4657
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
philipp@7252
    14
     claim that you wrote the original software. If you use this software
philipp@7252
    15
     in a product, an acknowledgment in the product documentation would be
philipp@7252
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
philipp@7252
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
jim@4657
    20
*/
aschiffler@4865
    21
icculus@8093
    22
#include "../SDL_internal.h"
jim@4657
    23
philipp@10123
    24
/* General gesture handling code for SDL */
jim@4657
    25
jim@4657
    26
#include "SDL_events.h"
icculus@9457
    27
#include "SDL_endian.h"
jim@4657
    28
#include "SDL_events_c.h"
jim@4657
    29
#include "SDL_gesture_c.h"
jim@4657
    30
philipp@8775
    31
/*
aschiffler@4865
    32
#include <stdio.h>
philipp@8775
    33
*/
aschiffler@4865
    34
slouken@7191
    35
/* TODO: Replace with malloc */
jim@4688
    36
jim@4658
    37
#define MAXPATHSIZE 1024
jim@4658
    38
jim@4658
    39
#define DOLLARNPOINTS 64
jim@4658
    40
#define DOLLARSIZE 256
jim@4658
    41
jim@4684
    42
#define ENABLE_DOLLAR
jim@4684
    43
kees@6065
    44
#define PHI 0.618033989
jim@4657
    45
jim@4657
    46
typedef struct {
kees@6065
    47
    float x,y;
jim@4688
    48
} SDL_FloatPoint;
jim@4658
    49
jim@4658
    50
typedef struct {
kees@6065
    51
    float length;
kees@6065
    52
kees@6065
    53
    int numPoints;
kees@6065
    54
    SDL_FloatPoint p[MAXPATHSIZE];
jim@4688
    55
} SDL_DollarPath;
jim@4658
    56
jim@4657
    57
typedef struct {
kees@6065
    58
    SDL_FloatPoint path[DOLLARNPOINTS];
kees@6065
    59
    unsigned long hash;
jim@4688
    60
} SDL_DollarTemplate;
jim@4657
    61
jim@4657
    62
typedef struct {
kees@6069
    63
    SDL_TouchID id;
kees@6065
    64
    SDL_FloatPoint centroid;
kees@6065
    65
    SDL_DollarPath dollarPath;
kees@6065
    66
    Uint16 numDownFingers;
jim@4658
    67
kees@6065
    68
    int numDollarTemplates;
kees@6065
    69
    SDL_DollarTemplate *dollarTemplate;
jim@4659
    70
kees@6065
    71
    SDL_bool recording;
jim@4688
    72
} SDL_GestureTouch;
jim@4657
    73
slouken@10609
    74
static SDL_GestureTouch *SDL_gestureTouch;
slouken@10609
    75
static int SDL_numGestureTouches = 0;
slouken@10609
    76
static SDL_bool recordAll;
jim@4659
    77
slouken@4868
    78
#if 0
kees@6065
    79
static void PrintPath(SDL_FloatPoint *path)
kees@6065
    80
{
kees@6065
    81
    int i;
kees@6065
    82
    printf("Path:");
kees@6066
    83
    for (i=0; i<DOLLARNPOINTS; i++) {
kees@6065
    84
        printf(" (%f,%f)",path[i].x,path[i].y);
kees@6065
    85
    }
kees@6065
    86
    printf("\n");
jim@4685
    87
}
slouken@4868
    88
#endif
jim@4685
    89
kees@6065
    90
int SDL_RecordGesture(SDL_TouchID touchId)
kees@6065
    91
{
kees@6065
    92
    int i;
kees@6066
    93
    if (touchId < 0) recordAll = SDL_TRUE;
kees@6066
    94
    for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6066
    95
        if ((touchId < 0) || (SDL_gestureTouch[i].id == touchId)) {
kees@6065
    96
            SDL_gestureTouch[i].recording = SDL_TRUE;
kees@6066
    97
            if (touchId >= 0)
kees@6065
    98
                return 1;
kees@6065
    99
        }
kees@6065
   100
    }
kees@6065
   101
    return (touchId < 0);
jim@4659
   102
}
jim@4659
   103
slouken@11290
   104
void SDL_GestureQuit()
slouken@11290
   105
{
slouken@11290
   106
    SDL_free(SDL_gestureTouch);
slouken@11290
   107
    SDL_gestureTouch = NULL;
slouken@11290
   108
}
slouken@11290
   109
icculus@6070
   110
static unsigned long SDL_HashDollar(SDL_FloatPoint* points)
kees@6065
   111
{
kees@6065
   112
    unsigned long hash = 5381;
kees@6065
   113
    int i;
kees@6066
   114
    for (i = 0; i < DOLLARNPOINTS; i++) {
kees@6065
   115
        hash = ((hash<<5) + hash) + (unsigned long)points[i].x;
kees@6065
   116
        hash = ((hash<<5) + hash) + (unsigned long)points[i].y;
kees@6065
   117
    }
kees@6065
   118
    return hash;
jim@4659
   119
}
jim@4659
   120
jim@4665
   121
philipp@7900
   122
static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops *dst)
kees@6065
   123
{
icculus@9457
   124
    if (dst == NULL) {
icculus@9457
   125
        return 0;
icculus@9457
   126
    }
jim@4682
   127
gabomdq@7678
   128
    /* No Longer storing the Hash, rehash on load */
philipp@7900
   129
    /* if (SDL_RWops.write(dst, &(templ->hash), sizeof(templ->hash), 1) != 1) return 0; */
kees@6065
   130
icculus@9457
   131
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
philipp@7900
   132
    if (SDL_RWwrite(dst, templ->path,
icculus@9457
   133
                    sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
kees@6065
   134
        return 0;
icculus@9457
   135
    }
icculus@9457
   136
#else
icculus@9457
   137
    {
icculus@9457
   138
        SDL_DollarTemplate copy = *templ;
icculus@9457
   139
        SDL_FloatPoint *p = copy.path;
icculus@9457
   140
        int i;
icculus@9457
   141
        for (i = 0; i < DOLLARNPOINTS; i++, p++) {
icculus@9457
   142
            p->x = SDL_SwapFloatLE(p->x);
icculus@9457
   143
            p->y = SDL_SwapFloatLE(p->y);
icculus@9457
   144
        }
icculus@9457
   145
icculus@9457
   146
        if (SDL_RWwrite(dst, copy.path,
icculus@9457
   147
                        sizeof(copy.path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
icculus@9457
   148
            return 0;
icculus@9457
   149
        }
icculus@9457
   150
    }
icculus@9457
   151
#endif
kees@6065
   152
kees@6065
   153
    return 1;
jim@4659
   154
}
jim@4659
   155
jim@4659
   156
philipp@7900
   157
int SDL_SaveAllDollarTemplates(SDL_RWops *dst)
kees@6065
   158
{
kees@6065
   159
    int i,j,rtrn = 0;
kees@6066
   160
    for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   161
        SDL_GestureTouch* touch = &SDL_gestureTouch[i];
kees@6066
   162
        for (j = 0; j < touch->numDollarTemplates; j++) {
slouken@8705
   163
            rtrn += SaveTemplate(&touch->dollarTemplate[j], dst);
kees@6065
   164
        }
jim@4659
   165
    }
kees@6065
   166
    return rtrn;
jim@4659
   167
}
jim@4659
   168
philipp@7900
   169
int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *dst)
kees@6065
   170
{
kees@6065
   171
    int i,j;
kees@6066
   172
    for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   173
        SDL_GestureTouch* touch = &SDL_gestureTouch[i];
kees@6066
   174
        for (j = 0; j < touch->numDollarTemplates; j++) {
slouken@8726
   175
            if (touch->dollarTemplate[j].hash == gestureId) {
slouken@8705
   176
                return SaveTemplate(&touch->dollarTemplate[j], dst);
kees@6065
   177
            }
kees@6065
   178
        }
jim@4659
   179
    }
icculus@7037
   180
    return SDL_SetError("Unknown gestureId");
slouken@4663
   181
}
slouken@4663
   182
gabomdq@7678
   183
/* path is an already sampled set of points
gabomdq@7677
   184
Returns the index of the gesture on success, or -1 */
kees@6067
   185
static int SDL_AddDollarGesture_one(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
kees@6065
   186
{
kees@6065
   187
    SDL_DollarTemplate* dollarTemplate;
kees@6065
   188
    SDL_DollarTemplate *templ;
kees@6067
   189
    int index;
kees@6067
   190
kees@6067
   191
    index = inTouch->numDollarTemplates;
kees@6067
   192
    dollarTemplate =
kees@6067
   193
        (SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
kees@6067
   194
                                          (index + 1) *
kees@6067
   195
                                          sizeof(SDL_DollarTemplate));
kees@6067
   196
    if (!dollarTemplate) {
icculus@7037
   197
        return SDL_OutOfMemory();
kees@6067
   198
    }
kees@6067
   199
    inTouch->dollarTemplate = dollarTemplate;
kees@6067
   200
kees@6067
   201
    templ = &inTouch->dollarTemplate[index];
kees@6067
   202
    SDL_memcpy(templ->path, path, DOLLARNPOINTS*sizeof(SDL_FloatPoint));
kees@6067
   203
    templ->hash = SDL_HashDollar(templ->path);
kees@6067
   204
    inTouch->numDollarTemplates++;
kees@6067
   205
kees@6067
   206
    return index;
kees@6067
   207
}
kees@6067
   208
kees@6067
   209
static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
kees@6067
   210
{
icculus@6071
   211
    int index = -1;
kees@6065
   212
    int i = 0;
kees@6066
   213
    if (inTouch == NULL) {
icculus@9456
   214
        if (SDL_numGestureTouches == 0) return SDL_SetError("no gesture touch devices registered");
kees@6066
   215
        for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   216
            inTouch = &SDL_gestureTouch[i];
kees@6067
   217
            index = SDL_AddDollarGesture_one(inTouch, path);
kees@6067
   218
            if (index < 0)
kees@6065
   219
                return -1;
kees@6065
   220
        }
gabomdq@7677
   221
        /* Use the index of the last one added. */
kees@6067
   222
        return index;
slouken@4663
   223
    }
philipp@7899
   224
    return SDL_AddDollarGesture_one(inTouch, path);
jim@4659
   225
}
jim@4659
   226
kees@6065
   227
int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
kees@6065
   228
{
kees@6065
   229
    int i,loaded = 0;
kees@6065
   230
    SDL_GestureTouch *touch = NULL;
kees@6066
   231
    if (src == NULL) return 0;
kees@6066
   232
    if (touchId >= 0) {
icculus@9457
   233
        for (i = 0; i < SDL_numGestureTouches; i++) {
icculus@9457
   234
            if (SDL_gestureTouch[i].id == touchId) {
kees@6065
   235
                touch = &SDL_gestureTouch[i];
icculus@9457
   236
            }
icculus@9457
   237
        }
icculus@9457
   238
        if (touch == NULL) {
icculus@9457
   239
            return SDL_SetError("given touch id not found");
icculus@9457
   240
        }
kees@6065
   241
    }
jim@4659
   242
kees@6066
   243
    while (1) {
kees@6065
   244
        SDL_DollarTemplate templ;
jim@4688
   245
icculus@9457
   246
        if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) {
icculus@9457
   247
            if (loaded == 0) {
icculus@9457
   248
                return SDL_SetError("could not read any dollar gesture from rwops");
icculus@9457
   249
            }
icculus@9457
   250
            break;
icculus@9457
   251
        }
icculus@9457
   252
icculus@9457
   253
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
icculus@9457
   254
        for (i = 0; i < DOLLARNPOINTS; i++) {
icculus@9457
   255
            SDL_FloatPoint *p = &templ.path[i];
icculus@9457
   256
            p->x = SDL_SwapFloatLE(p->x);
icculus@9457
   257
            p->y = SDL_SwapFloatLE(p->y);
icculus@9457
   258
        }
icculus@9457
   259
#endif
jim@4659
   260
kees@6066
   261
        if (touchId >= 0) {
gabomdq@7678
   262
            /* printf("Adding loaded gesture to 1 touch\n"); */
kees@6068
   263
            if (SDL_AddDollarGesture(touch, templ.path) >= 0)
kees@6068
   264
                loaded++;
kees@6065
   265
        }
kees@6065
   266
        else {
gabomdq@7678
   267
            /* printf("Adding to: %i touches\n",SDL_numGestureTouches); */
kees@6066
   268
            for (i = 0; i < SDL_numGestureTouches; i++) {
kees@6065
   269
                touch = &SDL_gestureTouch[i];
gabomdq@7678
   270
                /* printf("Adding loaded gesture to + touches\n"); */
gabomdq@7678
   271
                /* TODO: What if this fails? */
kees@6065
   272
                SDL_AddDollarGesture(touch,templ.path);
kees@6065
   273
            }
kees@6065
   274
            loaded++;
kees@6065
   275
        }
jim@4659
   276
    }
jim@4659
   277
kees@6065
   278
    return loaded;
jim@4659
   279
}
jim@4659
   280
jim@4659
   281
sylvain@12479
   282
#if defined(ENABLE_DOLLAR)
icculus@6070
   283
static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang)
kees@6065
   284
{
gabomdq@7677
   285
    /*  SDL_FloatPoint p[DOLLARNPOINTS]; */
kees@6065
   286
    float dist = 0;
kees@6065
   287
    SDL_FloatPoint p;
kees@6065
   288
    int i;
kees@6066
   289
    for (i = 0; i < DOLLARNPOINTS; i++) {
kees@6065
   290
        p.x = (float)(points[i].x * SDL_cos(ang) - points[i].y * SDL_sin(ang));
kees@6065
   291
        p.y = (float)(points[i].x * SDL_sin(ang) + points[i].y * SDL_cos(ang));
kees@6065
   292
        dist += (float)(SDL_sqrt((p.x-templ[i].x)*(p.x-templ[i].x)+
kees@6065
   293
                                 (p.y-templ[i].y)*(p.y-templ[i].y)));
kees@6065
   294
    }
kees@6065
   295
    return dist/DOLLARNPOINTS;
kees@6065
   296
jim@4658
   297
}
jim@4658
   298
icculus@6070
   299
static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
kees@6065
   300
{
gabomdq@7662
   301
    /*------------BEGIN DOLLAR BLACKBOX------------------
gabomdq@7662
   302
      -TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-
gabomdq@7662
   303
      -"http://depts.washington.edu/aimgroup/proj/dollar/"
gabomdq@7662
   304
    */
kees@6065
   305
    double ta = -M_PI/4;
kees@6065
   306
    double tb = M_PI/4;
kees@6065
   307
    double dt = M_PI/90;
kees@6065
   308
    float x1 = (float)(PHI*ta + (1-PHI)*tb);
kees@6065
   309
    float f1 = dollarDifference(points,templ,x1);
kees@6065
   310
    float x2 = (float)((1-PHI)*ta + PHI*tb);
kees@6065
   311
    float f2 = dollarDifference(points,templ,x2);
kees@6066
   312
    while (SDL_fabs(ta-tb) > dt) {
kees@6066
   313
        if (f1 < f2) {
kees@6065
   314
            tb = x2;
kees@6065
   315
            x2 = x1;
kees@6065
   316
            f2 = f1;
kees@6065
   317
            x1 = (float)(PHI*ta + (1-PHI)*tb);
kees@6065
   318
            f1 = dollarDifference(points,templ,x1);
kees@6065
   319
        }
kees@6065
   320
        else {
kees@6065
   321
            ta = x1;
kees@6065
   322
            x1 = x2;
kees@6065
   323
            f1 = f2;
kees@6065
   324
            x2 = (float)((1-PHI)*ta + PHI*tb);
kees@6065
   325
            f2 = dollarDifference(points,templ,x2);
kees@6065
   326
        }
jim@4658
   327
    }
kees@6065
   328
    /*
kees@6066
   329
      if (f1 <= f2)
kees@6066
   330
          printf("Min angle (x1): %f\n",x1);
kees@6066
   331
      else if (f1 >  f2)
kees@6066
   332
          printf("Min angle (x2): %f\n",x2);
kees@6065
   333
    */
kees@6065
   334
    return SDL_min(f1,f2);
jim@4658
   335
}
jim@4658
   336
gabomdq@7678
   337
/* DollarPath contains raw points, plus (possibly) the calculated length */
icculus@6070
   338
static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points)
kees@6065
   339
{
kees@6065
   340
    int i;
kees@6065
   341
    float interval;
kees@6065
   342
    float dist;
kees@6065
   343
    int numPoints = 0;
kees@6065
   344
    SDL_FloatPoint centroid;
kees@6065
   345
    float xmin,xmax,ymin,ymax;
kees@6065
   346
    float ang;
kees@6065
   347
    float w,h;
kees@6065
   348
    float length = path->length;
aschiffler@4865
   349
gabomdq@7678
   350
    /* Calculate length if it hasn't already been done */
kees@6066
   351
    if (length <= 0) {
kees@6066
   352
        for (i=1;i < path->numPoints; i++) {
kees@6066
   353
            float dx = path->p[i  ].x - path->p[i-1].x;
kees@6066
   354
            float dy = path->p[i  ].y - path->p[i-1].y;
kees@6065
   355
            length += (float)(SDL_sqrt(dx*dx+dy*dy));
kees@6065
   356
        }
jim@4658
   357
    }
jim@4658
   358
gabomdq@7678
   359
    /* Resample */
kees@6065
   360
    interval = length/(DOLLARNPOINTS - 1);
kees@6065
   361
    dist = interval;
jim@4658
   362
kees@6065
   363
    centroid.x = 0;centroid.y = 0;
jim@4658
   364
gabomdq@7678
   365
    /* printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y); */
kees@6066
   366
    for (i = 1; i < path->numPoints; i++) {
kees@6065
   367
        float d = (float)(SDL_sqrt((path->p[i-1].x-path->p[i].x)*(path->p[i-1].x-path->p[i].x)+
kees@6065
   368
                                   (path->p[i-1].y-path->p[i].y)*(path->p[i-1].y-path->p[i].y)));
gabomdq@7678
   369
        /* printf("d = %f dist = %f/%f\n",d,dist,interval); */
kees@6066
   370
        while (dist + d > interval) {
kees@6065
   371
            points[numPoints].x = path->p[i-1].x +
kees@6065
   372
                ((interval-dist)/d)*(path->p[i].x-path->p[i-1].x);
kees@6065
   373
            points[numPoints].y = path->p[i-1].y +
kees@6065
   374
                ((interval-dist)/d)*(path->p[i].y-path->p[i-1].y);
kees@6065
   375
            centroid.x += points[numPoints].x;
kees@6065
   376
            centroid.y += points[numPoints].y;
kees@6065
   377
            numPoints++;
kees@6065
   378
kees@6065
   379
            dist -= interval;
kees@6065
   380
        }
kees@6065
   381
        dist += d;
jim@4658
   382
    }
kees@6066
   383
    if (numPoints < DOLLARNPOINTS-1) {
philipp@10945
   384
        SDL_SetError("ERROR: NumPoints = %i", numPoints);
kees@6065
   385
        return 0;
kees@6065
   386
    }
gabomdq@7678
   387
    /* copy the last point */
kees@6065
   388
    points[DOLLARNPOINTS-1] = path->p[path->numPoints-1];
kees@6065
   389
    numPoints = DOLLARNPOINTS;
jim@4685
   390
kees@6065
   391
    centroid.x /= numPoints;
kees@6065
   392
    centroid.y /= numPoints;
jim@4658
   393
gabomdq@7678
   394
    /* printf("Centroid (%f,%f)",centroid.x,centroid.y); */
gabomdq@7678
   395
    /* Rotate Points so point 0 is left of centroid and solve for the bounding box */
kees@6065
   396
    xmin = centroid.x;
kees@6065
   397
    xmax = centroid.x;
kees@6065
   398
    ymin = centroid.y;
kees@6065
   399
    ymax = centroid.y;
jim@4658
   400
kees@6065
   401
    ang = (float)(SDL_atan2(centroid.y - points[0].y,
kees@6065
   402
                            centroid.x - points[0].x));
jim@4658
   403
kees@6066
   404
    for (i = 0; i<numPoints; i++) {
kees@6065
   405
        float px = points[i].x;
kees@6065
   406
        float py = points[i].y;
kees@6065
   407
        points[i].x = (float)((px - centroid.x)*SDL_cos(ang) -
kees@6065
   408
                              (py - centroid.y)*SDL_sin(ang) + centroid.x);
kees@6065
   409
        points[i].y = (float)((px - centroid.x)*SDL_sin(ang) +
kees@6065
   410
                              (py - centroid.y)*SDL_cos(ang) + centroid.y);
jim@4658
   411
jim@4658
   412
kees@6066
   413
        if (points[i].x < xmin) xmin = points[i].x;
kees@6066
   414
        if (points[i].x > xmax) xmax = points[i].x;
kees@6066
   415
        if (points[i].y < ymin) ymin = points[i].y;
kees@6066
   416
        if (points[i].y > ymax) ymax = points[i].y;
kees@6065
   417
    }
kees@6065
   418
gabomdq@7678
   419
    /* Scale points to DOLLARSIZE, and translate to the origin */
kees@6065
   420
    w = xmax-xmin;
kees@6065
   421
    h = ymax-ymin;
kees@6065
   422
kees@6066
   423
    for (i=0; i<numPoints; i++) {
kees@6065
   424
        points[i].x = (points[i].x - centroid.x)*DOLLARSIZE/w;
kees@6065
   425
        points[i].y = (points[i].y - centroid.y)*DOLLARSIZE/h;
kees@6065
   426
    }
kees@6065
   427
    return numPoints;
jim@4658
   428
}
jim@4658
   429
icculus@6070
   430
static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch)
kees@6065
   431
{
jorgen@7510
   432
    SDL_FloatPoint points[DOLLARNPOINTS];
kees@6065
   433
    int i;
kees@6065
   434
    float bestDiff = 10000;
icculus@5981
   435
jorgen@7510
   436
    SDL_memset(points, 0, sizeof(points));
jorgen@7510
   437
kees@6065
   438
    dollarNormalize(path,points);
kees@6065
   439
gabomdq@7678
   440
    /* PrintPath(points); */
kees@6065
   441
    *bestTempl = -1;
kees@6066
   442
    for (i = 0; i < touch->numDollarTemplates; i++) {
kees@6065
   443
        float diff = bestDollarDifference(points,touch->dollarTemplate[i].path);
kees@6066
   444
        if (diff < bestDiff) {bestDiff = diff; *bestTempl = i;}
kees@6065
   445
    }
kees@6065
   446
    return bestDiff;
slouken@4663
   447
}
sylvain@12479
   448
#endif
slouken@4663
   449
slouken@6951
   450
int SDL_GestureAddTouch(SDL_TouchID touchId)
kees@6065
   451
{
kees@6065
   452
    SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
kees@6065
   453
                                                                     (SDL_numGestureTouches + 1) *
kees@6065
   454
                                                                     sizeof(SDL_GestureTouch));
jim@4657
   455
kees@6066
   456
    if (!gestureTouch) {
icculus@7037
   457
        return SDL_OutOfMemory();
kees@6065
   458
    }
jim@4657
   459
kees@6065
   460
    SDL_gestureTouch = gestureTouch;
jim@4658
   461
slouken@8296
   462
    SDL_zero(SDL_gestureTouch[SDL_numGestureTouches]);
slouken@6951
   463
    SDL_gestureTouch[SDL_numGestureTouches].id = touchId;
kees@6065
   464
    SDL_numGestureTouches++;
kees@6065
   465
    return 0;
jim@4657
   466
}
jim@4657
   467
slouken@11290
   468
int SDL_GestureDelTouch(SDL_TouchID touchId)
slouken@11290
   469
{
slouken@11290
   470
    int i;
slouken@11290
   471
    for (i = 0; i < SDL_numGestureTouches; i++) {
slouken@11290
   472
        if (SDL_gestureTouch[i].id == touchId) {
slouken@11290
   473
            break;
slouken@11290
   474
        }
slouken@11290
   475
    }
slouken@11290
   476
slouken@11290
   477
    if (i == SDL_numGestureTouches) {
slouken@11290
   478
        /* not found */
slouken@11290
   479
        return -1;
slouken@11290
   480
    }
slouken@11290
   481
slouken@11290
   482
    SDL_free(SDL_gestureTouch[i].dollarTemplate);
slouken@11290
   483
    SDL_zero(SDL_gestureTouch[i]);
slouken@11290
   484
slouken@11290
   485
    SDL_numGestureTouches--;
slouken@11293
   486
    SDL_memcpy(&SDL_gestureTouch[i], &SDL_gestureTouch[SDL_numGestureTouches], sizeof(SDL_gestureTouch[i]));
slouken@11290
   487
    return 0;
slouken@11290
   488
}
slouken@11290
   489
icculus@6070
   490
static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id)
kees@6065
   491
{
kees@6065
   492
    int i;
kees@6066
   493
    for (i = 0; i < SDL_numGestureTouches; i++) {
gabomdq@7678
   494
        /* printf("%i ?= %i\n",SDL_gestureTouch[i].id,id); */
kees@6066
   495
        if (SDL_gestureTouch[i].id == id)
kees@6066
   496
            return &SDL_gestureTouch[i];
kees@6065
   497
    }
kees@6065
   498
    return NULL;
jim@4657
   499
}
jim@4657
   500
slouken@10609
   501
static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist)
kees@6065
   502
{
kees@6065
   503
    SDL_Event event;
kees@6065
   504
    event.mgesture.type = SDL_MULTIGESTURE;
kees@6065
   505
    event.mgesture.touchId = touch->id;
kees@6065
   506
    event.mgesture.x = touch->centroid.x;
kees@6065
   507
    event.mgesture.y = touch->centroid.y;
kees@6065
   508
    event.mgesture.dTheta = dTheta;
kees@6065
   509
    event.mgesture.dDist = dDist;
kees@6065
   510
    event.mgesture.numFingers = touch->numDownFingers;
kees@6065
   511
    return SDL_PushEvent(&event) > 0;
jim@4657
   512
}
jim@4657
   513
sylvain@12479
   514
#if defined(ENABLE_DOLLAR)
slouken@6044
   515
static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
kees@6065
   516
                          SDL_GestureID gestureId,float error)
kees@6065
   517
{
kees@6065
   518
    SDL_Event event;
kees@6065
   519
    event.dgesture.type = SDL_DOLLARGESTURE;
kees@6065
   520
    event.dgesture.touchId = touch->id;
slouken@8705
   521
    event.dgesture.x = touch->centroid.x;
slouken@8705
   522
    event.dgesture.y = touch->centroid.y;
kees@6065
   523
    event.dgesture.gestureId = gestureId;
kees@6065
   524
    event.dgesture.error = error;
gabomdq@7678
   525
    /* A finger came up to trigger this event. */
kees@6065
   526
    event.dgesture.numFingers = touch->numDownFingers + 1;
kees@6065
   527
    return SDL_PushEvent(&event) > 0;
jim@4658
   528
}
jim@4658
   529
jim@4659
   530
icculus@6070
   531
static int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId)
kees@6065
   532
{
kees@6065
   533
    SDL_Event event;
kees@6065
   534
    event.dgesture.type = SDL_DOLLARRECORD;
kees@6065
   535
    event.dgesture.touchId = touch->id;
kees@6065
   536
    event.dgesture.gestureId = gestureId;
kees@6065
   537
    return SDL_PushEvent(&event) > 0;
jim@4659
   538
}
sylvain@12479
   539
#endif
jim@4659
   540
jim@4659
   541
jim@4657
   542
void SDL_GestureProcessEvent(SDL_Event* event)
jim@4657
   543
{
kees@6065
   544
    float x,y;
sylvain@12479
   545
#if defined(ENABLE_DOLLAR)
kees@6065
   546
    int index;
kees@6065
   547
    int i;
kees@6065
   548
    float pathDx, pathDy;
sylvain@12479
   549
#endif
kees@6065
   550
    SDL_FloatPoint lastP;
kees@6065
   551
    SDL_FloatPoint lastCentroid;
kees@6065
   552
    float lDist;
kees@6065
   553
    float Dist;
kees@6065
   554
    float dtheta;
kees@6065
   555
    float dDist;
aschiffler@4865
   556
kees@6066
   557
    if (event->type == SDL_FINGERMOTION ||
kees@6066
   558
        event->type == SDL_FINGERDOWN ||
kees@6066
   559
        event->type == SDL_FINGERUP) {
kees@6065
   560
        SDL_GestureTouch* inTouch = SDL_GetGestureTouch(event->tfinger.touchId);
jim@4683
   561
gabomdq@7678
   562
        /* Shouldn't be possible */
kees@6066
   563
        if (inTouch == NULL) return;
jim@4658
   564
slouken@6951
   565
        x = event->tfinger.x;
slouken@6951
   566
        y = event->tfinger.y;
kees@6065
   567
gabomdq@7678
   568
        /* Finger Up */
kees@6066
   569
        if (event->type == SDL_FINGERUP) {
sylvain@12479
   570
#if defined(ENABLE_DOLLAR)
slouken@8910
   571
            SDL_FloatPoint path[DOLLARNPOINTS];
sylvain@12479
   572
#endif
slouken@8910
   573
kees@6065
   574
            inTouch->numDownFingers--;
jim@4688
   575
sylvain@12479
   576
#if defined(ENABLE_DOLLAR)
kees@6066
   577
            if (inTouch->recording) {
kees@6065
   578
                inTouch->recording = SDL_FALSE;
kees@6065
   579
                dollarNormalize(&inTouch->dollarPath,path);
gabomdq@7678
   580
                /* PrintPath(path); */
kees@6066
   581
                if (recordAll) {
kees@6065
   582
                    index = SDL_AddDollarGesture(NULL,path);
kees@6066
   583
                    for (i = 0; i < SDL_numGestureTouches; i++)
kees@6065
   584
                        SDL_gestureTouch[i].recording = SDL_FALSE;
kees@6065
   585
                }
kees@6065
   586
                else {
kees@6065
   587
                    index = SDL_AddDollarGesture(inTouch,path);
kees@6065
   588
                }
kees@6065
   589
kees@6066
   590
                if (index >= 0) {
kees@6065
   591
                    SDL_SendDollarRecord(inTouch,inTouch->dollarTemplate[index].hash);
kees@6065
   592
                }
kees@6065
   593
                else {
kees@6065
   594
                    SDL_SendDollarRecord(inTouch,-1);
kees@6065
   595
                }
kees@6065
   596
            }
kees@6065
   597
            else {
kees@6065
   598
                int bestTempl;
kees@6065
   599
                float error;
kees@6065
   600
                error = dollarRecognize(&inTouch->dollarPath,
kees@6065
   601
                                        &bestTempl,inTouch);
kees@6066
   602
                if (bestTempl >= 0){
gabomdq@7678
   603
                    /* Send Event */
kees@6065
   604
                    unsigned long gestureId = inTouch->dollarTemplate[bestTempl].hash;
kees@6065
   605
                    SDL_SendGestureDollar(inTouch,gestureId,error);
gabomdq@7678
   606
                    /* printf ("%s\n",);("Dollar error: %f\n",error); */
kees@6065
   607
                }
kees@6065
   608
            }
jim@4683
   609
#endif
gabomdq@7678
   610
            /* inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers]; */
kees@6066
   611
            if (inTouch->numDownFingers > 0) {
kees@6065
   612
                inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers+1)-
kees@6065
   613
                                       x)/inTouch->numDownFingers;
kees@6065
   614
                inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers+1)-
kees@6065
   615
                                       y)/inTouch->numDownFingers;
kees@6065
   616
            }
kees@6065
   617
        }
kees@6066
   618
        else if (event->type == SDL_FINGERMOTION) {
slouken@6951
   619
            float dx = event->tfinger.dx;
slouken@6951
   620
            float dy = event->tfinger.dy;
sylvain@12479
   621
#if defined(ENABLE_DOLLAR)
kees@6065
   622
            SDL_DollarPath* path = &inTouch->dollarPath;
kees@6066
   623
            if (path->numPoints < MAXPATHSIZE) {
kees@6065
   624
                path->p[path->numPoints].x = inTouch->centroid.x;
kees@6065
   625
                path->p[path->numPoints].y = inTouch->centroid.y;
kees@6065
   626
                pathDx =
kees@6065
   627
                    (path->p[path->numPoints].x-path->p[path->numPoints-1].x);
kees@6065
   628
                pathDy =
kees@6065
   629
                    (path->p[path->numPoints].y-path->p[path->numPoints-1].y);
kees@6065
   630
                path->length += (float)SDL_sqrt(pathDx*pathDx + pathDy*pathDy);
kees@6065
   631
                path->numPoints++;
kees@6065
   632
            }
kees@6065
   633
#endif
kees@6065
   634
            lastP.x = x - dx;
kees@6065
   635
            lastP.y = y - dy;
kees@6065
   636
            lastCentroid = inTouch->centroid;
kees@6065
   637
kees@6065
   638
            inTouch->centroid.x += dx/inTouch->numDownFingers;
kees@6065
   639
            inTouch->centroid.y += dy/inTouch->numDownFingers;
gabomdq@7678
   640
            /* printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y); */
kees@6066
   641
            if (inTouch->numDownFingers > 1) {
gabomdq@7678
   642
                SDL_FloatPoint lv; /* Vector from centroid to last x,y position */
gabomdq@7678
   643
                SDL_FloatPoint v; /* Vector from centroid to current x,y position */
gabomdq@7678
   644
                /* lv = inTouch->gestureLast[j].cv; */
kees@6065
   645
                lv.x = lastP.x - lastCentroid.x;
kees@6065
   646
                lv.y = lastP.y - lastCentroid.y;
kees@6065
   647
                lDist = (float)SDL_sqrt(lv.x*lv.x + lv.y*lv.y);
gabomdq@7678
   648
                /* printf("lDist = %f\n",lDist); */
kees@6065
   649
                v.x = x - inTouch->centroid.x;
kees@6065
   650
                v.y = y - inTouch->centroid.y;
gabomdq@7678
   651
                /* inTouch->gestureLast[j].cv = v; */
kees@6065
   652
                Dist = (float)SDL_sqrt(v.x*v.x+v.y*v.y);
gabomdq@7677
   653
                /* SDL_cos(dTheta) = (v . lv)/(|v| * |lv|) */
kees@6065
   654
gabomdq@7678
   655
                /* Normalize Vectors to simplify angle calculation */
kees@6065
   656
                lv.x/=lDist;
kees@6065
   657
                lv.y/=lDist;
kees@6065
   658
                v.x/=Dist;
kees@6065
   659
                v.y/=Dist;
kees@6065
   660
                dtheta = (float)SDL_atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y);
kees@6065
   661
kees@6065
   662
                dDist = (Dist - lDist);
gabomdq@7678
   663
                if (lDist == 0) {dDist = 0;dtheta = 0;} /* To avoid impossible values */
kees@6065
   664
gabomdq@7678
   665
                /* inTouch->gestureLast[j].dDist = dDist;
gabomdq@7662
   666
                inTouch->gestureLast[j].dtheta = dtheta;
kees@6065
   667
gabomdq@7662
   668
                printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
gabomdq@7662
   669
                gdtheta = gdtheta*.9 + dtheta*.1;
gabomdq@7662
   670
                gdDist  =  gdDist*.9 +  dDist*.1
gabomdq@7662
   671
                knob.r += dDist/numDownFingers;
gabomdq@7662
   672
                knob.ang += dtheta;
gabomdq@7662
   673
                printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
gabomdq@7677
   674
                printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist); */
kees@6065
   675
                SDL_SendGestureMulti(inTouch,dtheta,dDist);
kees@6065
   676
            }
kees@6065
   677
            else {
gabomdq@7678
   678
                /* inTouch->gestureLast[j].dDist = 0;
gabomdq@7662
   679
                inTouch->gestureLast[j].dtheta = 0;
gabomdq@7662
   680
                inTouch->gestureLast[j].cv.x = 0;
gabomdq@7677
   681
                inTouch->gestureLast[j].cv.y = 0; */
kees@6065
   682
            }
gabomdq@7678
   683
            /* inTouch->gestureLast[j].f.p.x = x;
gabomdq@7662
   684
            inTouch->gestureLast[j].f.p.y = y;
gabomdq@7662
   685
            break;
gabomdq@7677
   686
            pressure? */
kees@6065
   687
        }
philipp@9791
   688
        else if (event->type == SDL_FINGERDOWN) {
kees@6065
   689
kees@6065
   690
            inTouch->numDownFingers++;
kees@6065
   691
            inTouch->centroid.x = (inTouch->centroid.x*(inTouch->numDownFingers - 1)+
kees@6065
   692
                                   x)/inTouch->numDownFingers;
kees@6065
   693
            inTouch->centroid.y = (inTouch->centroid.y*(inTouch->numDownFingers - 1)+
kees@6065
   694
                                   y)/inTouch->numDownFingers;
gabomdq@7678
   695
            /* printf("Finger Down: (%f,%f). Centroid: (%f,%f\n",x,y,
gabomdq@7677
   696
                 inTouch->centroid.x,inTouch->centroid.y); */
kees@6065
   697
sylvain@12479
   698
#if defined(ENABLE_DOLLAR)
kees@6065
   699
            inTouch->dollarPath.length = 0;
kees@6065
   700
            inTouch->dollarPath.p[0].x = x;
kees@6065
   701
            inTouch->dollarPath.p[0].y = y;
kees@6065
   702
            inTouch->dollarPath.numPoints = 1;
kees@6065
   703
#endif
kees@6065
   704
        }
jim@4657
   705
    }
jim@4683
   706
}
jim@4683
   707
kees@6065
   708
/* vi: set ts=4 sw=4 expandtab: */