src/events/SDL_touch.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 30 Nov 2010 17:58:51 -0800
changeset 4919 716b2cbf4c9e
parent 4868 d6adaafcfb10
child 5076 8bf5781fc582
permissions -rw-r--r--
First pass at Windows multi-touch gesture support
jim@4640
     1
/*
jim@4640
     2
    SDL - Simple DirectMedia Layer
jim@4640
     3
    Copyright (C) 1997-2010 Sam Lantinga
jim@4640
     4
jim@4640
     5
    This library is free software; you can redistribute it and/or
jim@4640
     6
    modify it under the terms of the GNU Lesser General Public
jim@4640
     7
    License as published by the Free Software Foundation; either
jim@4640
     8
    version 2.1 of the License, or (at your option) any later version.
jim@4640
     9
jim@4640
    10
    This library is distributed in the hope that it will be useful,
jim@4640
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
jim@4640
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
jim@4640
    13
    Lesser General Public License for more details.
jim@4640
    14
jim@4640
    15
    You should have received a copy of the GNU Lesser General Public
jim@4640
    16
    License along with this library; if not, write to the Free Software
jim@4640
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
jim@4640
    18
jim@4640
    19
    Sam Lantinga
jim@4640
    20
    slouken@libsdl.org
jim@4640
    21
*/
jim@4640
    22
#include "SDL_config.h"
jim@4640
    23
jim@4640
    24
/* General touch handling code for SDL */
jim@4640
    25
jim@4640
    26
#include "SDL_events.h"
jim@4640
    27
#include "SDL_events_c.h"
jim@4640
    28
#include "../video/SDL_sysvideo.h"
jim@4640
    29
jimtla@4648
    30
#include <stdio.h>
jimtla@4648
    31
jim@4640
    32
jim@4640
    33
static int SDL_num_touch = 0;
jim@4641
    34
static SDL_Touch **SDL_touchPads = NULL;
jim@4640
    35
jim@4640
    36
jim@4640
    37
/* Public functions */
jim@4640
    38
int
jim@4640
    39
SDL_TouchInit(void)
jim@4640
    40
{
jim@4667
    41
  return (0);
jim@4640
    42
}
jim@4642
    43
jim@4640
    44
SDL_Touch *
jimtla@4678
    45
SDL_GetTouch(SDL_TouchID id)
jim@4640
    46
{
jim@4641
    47
    int index = SDL_GetTouchIndexId(id);
jim@4640
    48
    if (index < 0 || index >= SDL_num_touch) {
jim@4640
    49
        return NULL;
jim@4640
    50
    }
jim@4641
    51
    return SDL_touchPads[index];
jim@4640
    52
}
jim@4640
    53
jim@4642
    54
SDL_Touch *
jim@4642
    55
SDL_GetTouchIndex(int index)
jim@4641
    56
{
jim@4642
    57
    if (index < 0 || index >= SDL_num_touch) {
jim@4642
    58
        return NULL;
jim@4642
    59
    }
jim@4642
    60
    return SDL_touchPads[index];
jim@4641
    61
}
jim@4641
    62
jim@4641
    63
int
jimtla@4678
    64
SDL_GetFingerIndexId(SDL_Touch* touch,SDL_FingerID fingerid)
jim@4641
    65
{
jim@4641
    66
    int i;
jim@4641
    67
    for(i = 0;i < touch->num_fingers;i++)
slouken@4919
    68
        if(touch->fingers[i]->id == fingerid)
slouken@4919
    69
            return i;
jim@4641
    70
    return -1;
jim@4641
    71
}
jim@4641
    72
jim@4642
    73
jim@4642
    74
SDL_Finger *
jimtla@4678
    75
SDL_GetFinger(SDL_Touch* touch,SDL_FingerID id)
jim@4642
    76
{
jim@4642
    77
    int index = SDL_GetFingerIndexId(touch,id);
jim@4642
    78
    if(index < 0 || index >= touch->num_fingers)
slouken@4919
    79
        return NULL;
jim@4642
    80
    return touch->fingers[index];
jim@4642
    81
}
jim@4642
    82
jim@4642
    83
jim@4641
    84
int
jimtla@4678
    85
SDL_GetTouchIndexId(SDL_TouchID id)
jim@4640
    86
{
jim@4640
    87
    int index;
jim@4640
    88
    SDL_Touch *touch;
jim@4640
    89
jim@4640
    90
    for (index = 0; index < SDL_num_touch; ++index) {
jim@4641
    91
        touch = SDL_touchPads[index];
jim@4640
    92
        if (touch->id == id) {
jim@4640
    93
            return index;
jim@4640
    94
        }
jim@4640
    95
    }
jim@4640
    96
    return -1;
jim@4640
    97
}
jim@4640
    98
jim@4640
    99
int
jim@4642
   100
SDL_AddTouch(const SDL_Touch * touch, char *name)
jim@4640
   101
{
jim@4641
   102
    SDL_Touch **touchPads;
jimtla@4648
   103
    int index,length;
jim@4640
   104
jim@4640
   105
    if (SDL_GetTouchIndexId(touch->id) != -1) {
jim@4640
   106
        SDL_SetError("Touch ID already in use");
jim@4640
   107
    }
jim@4640
   108
jim@4640
   109
    /* Add the touch to the list of touch */
jim@4641
   110
    touchPads = (SDL_Touch **) SDL_realloc(SDL_touchPads,
jim@4640
   111
                                      (SDL_num_touch + 1) * sizeof(*touch));
jim@4641
   112
    if (!touchPads) {
jim@4640
   113
        SDL_OutOfMemory();
jim@4640
   114
        return -1;
jim@4640
   115
    }
jim@4640
   116
jim@4641
   117
    SDL_touchPads = touchPads;
jim@4640
   118
    index = SDL_num_touch++;
jim@4640
   119
jim@4641
   120
    SDL_touchPads[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchPads[index]));
jim@4641
   121
    if (!SDL_touchPads[index]) {
jim@4640
   122
        SDL_OutOfMemory();
jim@4640
   123
        return -1;
jim@4640
   124
    }
jim@4641
   125
    *SDL_touchPads[index] = *touch;
jim@4640
   126
jim@4640
   127
    /* we're setting the touch properties */
jim@4640
   128
    length = 0;
jim@4640
   129
    length = SDL_strlen(name);
jim@4641
   130
    SDL_touchPads[index]->focus = 0;
jim@4641
   131
    SDL_touchPads[index]->name = SDL_malloc((length + 2) * sizeof(char));
jim@4642
   132
    SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);   
jim@4642
   133
jim@4642
   134
    SDL_touchPads[index]->num_fingers = 0;
jim@4646
   135
    SDL_touchPads[index]->max_fingers = 1;
jim@4646
   136
    SDL_touchPads[index]->fingers = (SDL_Finger **) SDL_malloc(sizeof(SDL_Finger*));
jim@4646
   137
    SDL_touchPads[index]->fingers[0] = NULL;
jim@4642
   138
    SDL_touchPads[index]->buttonstate = 0;
jim@4642
   139
    SDL_touchPads[index]->relative_mode = SDL_FALSE;
jim@4642
   140
    SDL_touchPads[index]->flush_motion = SDL_FALSE;
jim@4641
   141
    
jimtla@4678
   142
    SDL_touchPads[index]->xres = (1<<(16-1));
jimtla@4678
   143
    SDL_touchPads[index]->yres = (1<<(16-1));
jim@4657
   144
    //Do I want this here? Probably
jim@4657
   145
    SDL_GestureAddTouch(SDL_touchPads[index]);
jim@4657
   146
jim@4640
   147
    return index;
jim@4640
   148
}
jim@4640
   149
jim@4640
   150
void
jimtla@4678
   151
SDL_DelTouch(SDL_TouchID id)
jim@4640
   152
{
jim@4641
   153
    int index = SDL_GetTouchIndexId(id);
jim@4641
   154
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   155
jim@4640
   156
    if (!touch) {
jim@4640
   157
        return;
jim@4640
   158
    }
jim@4640
   159
jim@4641
   160
    
jim@4640
   161
    SDL_free(touch->name);
jim@4640
   162
 
jim@4640
   163
    if (touch->FreeTouch) {
jim@4640
   164
        touch->FreeTouch(touch);
jim@4640
   165
    }
jim@4640
   166
    SDL_free(touch);
jim@4640
   167
jim@4641
   168
    SDL_num_touch--;
jim@4641
   169
    SDL_touchPads[index] = SDL_touchPads[SDL_num_touch];
jim@4640
   170
}
jim@4640
   171
jim@4640
   172
void
jim@4640
   173
SDL_TouchQuit(void)
jim@4640
   174
{
jim@4640
   175
    int i;
jim@4640
   176
jim@4641
   177
    for (i = SDL_num_touch-1; i > 0 ; --i) {
jim@4640
   178
        SDL_DelTouch(i);
jim@4640
   179
    }
jim@4640
   180
    SDL_num_touch = 0;
jim@4640
   181
jim@4641
   182
    if (SDL_touchPads) {
jim@4641
   183
        SDL_free(SDL_touchPads);
jim@4641
   184
        SDL_touchPads = NULL;
jim@4640
   185
    }
jim@4640
   186
}
jim@4640
   187
jim@4640
   188
int
jim@4640
   189
SDL_GetNumTouch(void)
jim@4640
   190
{
jim@4640
   191
    return SDL_num_touch;
jim@4640
   192
}
jim@4641
   193
SDL_Window *
jimtla@4678
   194
SDL_GetTouchFocusWindow(SDL_TouchID id)
jim@4640
   195
{
jim@4641
   196
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   197
jim@4640
   198
    if (!touch) {
jim@4640
   199
        return 0;
jim@4640
   200
    }
jim@4640
   201
    return touch->focus;
jim@4640
   202
}
jim@4640
   203
jim@4640
   204
void
jimtla@4678
   205
SDL_SetTouchFocus(SDL_TouchID id, SDL_Window * window)
jim@4640
   206
{
jim@4640
   207
    int index = SDL_GetTouchIndexId(id);
jim@4641
   208
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   209
    int i;
jim@4640
   210
    SDL_bool focus;
jim@4640
   211
jim@4640
   212
    if (!touch || (touch->focus == window)) {
jim@4640
   213
        return;
jim@4640
   214
    }
jim@4640
   215
jim@4640
   216
    /* See if the current window has lost focus */
jim@4640
   217
    if (touch->focus) {
jim@4640
   218
        focus = SDL_FALSE;
jim@4640
   219
        for (i = 0; i < SDL_num_touch; ++i) {
jim@4640
   220
            SDL_Touch *check;
jim@4640
   221
            if (i != index) {
jim@4641
   222
                check = SDL_touchPads[i];
jim@4640
   223
                if (check && check->focus == touch->focus) {
jim@4640
   224
                    focus = SDL_TRUE;
jim@4640
   225
                    break;
jim@4640
   226
                }
jim@4640
   227
            }
jim@4640
   228
        }
jim@4640
   229
        if (!focus) {
jim@4640
   230
            SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
jim@4640
   231
        }
jim@4640
   232
    }
jim@4640
   233
jim@4640
   234
    touch->focus = window;
jim@4640
   235
jim@4640
   236
    if (touch->focus) {
jim@4640
   237
        focus = SDL_FALSE;
jim@4640
   238
        for (i = 0; i < SDL_num_touch; ++i) {
jim@4640
   239
            SDL_Touch *check;
jim@4640
   240
            if (i != index) {
jim@4641
   241
                check = SDL_touchPads[i];
jim@4640
   242
                if (check && check->focus == touch->focus) {
jim@4640
   243
                    focus = SDL_TRUE;
jim@4640
   244
                    break;
jim@4640
   245
                }
jim@4640
   246
            }
jim@4640
   247
        }
jim@4640
   248
        if (!focus) {
jim@4640
   249
            SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
jim@4640
   250
        }
jim@4640
   251
    }
jim@4640
   252
}
jim@4640
   253
jim@4641
   254
int 
jimtla@4678
   255
SDL_AddFinger(SDL_Touch* touch,SDL_Finger *finger)
jim@4640
   256
{
jim@4641
   257
    int index;
jim@4641
   258
    SDL_Finger **fingers;
jim@4646
   259
    //printf("Adding Finger...\n");
jimtla@4678
   260
    if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
jim@4641
   261
        SDL_SetError("Finger ID already in use");
slouken@4919
   262
        }
jim@4640
   263
jim@4641
   264
    /* Add the touch to the list of touch */
jim@4646
   265
    if(touch->num_fingers  >= touch->max_fingers){
slouken@4919
   266
                //printf("Making room for it!\n");
slouken@4919
   267
                fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
slouken@4919
   268
                                                     (touch->num_fingers + 1) * sizeof(SDL_Finger *));
slouken@4919
   269
                touch->max_fingers = touch->num_fingers+1;
slouken@4919
   270
                if (!fingers) {
slouken@4919
   271
                        SDL_OutOfMemory();
slouken@4919
   272
                        return -1;
slouken@4919
   273
                } else {
slouken@4919
   274
                        touch->max_fingers = touch->num_fingers+1;
slouken@4919
   275
                        touch->fingers = fingers;
slouken@4919
   276
                }
slouken@4919
   277
        }
jim@4645
   278
jim@4646
   279
    index = touch->num_fingers;
jim@4646
   280
    //printf("Max_Fingers: %i Index: %i\n",touch->max_fingers,index);
jim@4641
   281
jim@4646
   282
    touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(SDL_Finger));
jim@4641
   283
    if (!touch->fingers[index]) {
jim@4641
   284
        SDL_OutOfMemory();
jim@4641
   285
        return -1;
jim@4641
   286
    }
jimtla@4678
   287
    *(touch->fingers[index]) = *finger;
jim@4646
   288
    touch->num_fingers++;
jim@4641
   289
jim@4641
   290
    return index;
jim@4640
   291
}
jim@4640
   292
jim@4640
   293
int
jimtla@4678
   294
SDL_DelFinger(SDL_Touch* touch,SDL_FingerID fingerid)
jim@4641
   295
{
jim@4642
   296
    int index = SDL_GetFingerIndexId(touch,fingerid);
jim@4641
   297
    SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
jim@4641
   298
jim@4641
   299
    if (!finger) {
jimtla@4648
   300
        return -1;
jim@4641
   301
    }
jim@4641
   302
 
jim@4641
   303
jim@4641
   304
    SDL_free(finger);
jim@4641
   305
    touch->num_fingers--;
jim@4641
   306
    touch->fingers[index] = touch->fingers[touch->num_fingers];
jim@4655
   307
    return 0;
jim@4641
   308
}
jim@4641
   309
jim@4641
   310
jim@4641
   311
int
jimtla@4678
   312
SDL_SendFingerDown(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down, 
slouken@4919
   313
                   float xin, float yin, float pressurein)
jim@4641
   314
{
jim@4643
   315
    int posted;
slouken@4919
   316
        Uint16 x;
slouken@4919
   317
        Uint16 y;
slouken@4919
   318
        Uint16 pressure;
slouken@4919
   319
        SDL_Finger *finger;
aschiffler@4865
   320
jim@4641
   321
    SDL_Touch* touch = SDL_GetTouch(id);
jim@4643
   322
jim@4674
   323
    if(!touch) {
jim@4674
   324
      return SDL_TouchNotFoundError(id);
jim@4674
   325
    }
jim@4683
   326
jim@4683
   327
    
jimtla@4678
   328
    //scale to Integer coordinates
aschiffler@4865
   329
    x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
aschiffler@4865
   330
    y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
aschiffler@4865
   331
    pressure = (Uint16)((yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
jim@4683
   332
    
aschiffler@4865
   333
    finger = SDL_GetFinger(touch,fingerid);
jim@4641
   334
    if(down) {
slouken@4919
   335
        if(finger == NULL) {
slouken@4919
   336
            SDL_Finger nf;
slouken@4919
   337
            nf.id = fingerid;
slouken@4919
   338
            nf.x = x;
slouken@4919
   339
            nf.y = y;
slouken@4919
   340
            nf.pressure = pressure;
slouken@4919
   341
            nf.xdelta = 0;
slouken@4919
   342
            nf.ydelta = 0;
slouken@4919
   343
            nf.last_x = x;
slouken@4919
   344
            nf.last_y = y;
slouken@4919
   345
            nf.last_pressure = pressure;
slouken@4919
   346
            nf.down = SDL_FALSE;
slouken@4919
   347
            if(SDL_AddFinger(touch,&nf) < 0) return 0;
slouken@4919
   348
            finger = SDL_GetFinger(touch,fingerid);
slouken@4919
   349
        }
slouken@4919
   350
        else if(finger->down) return 0;
slouken@4919
   351
        if(xin < touch->x_min || yin < touch->y_min) return 0; //should defer if only a partial input
slouken@4919
   352
        posted = 0;
slouken@4919
   353
        if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
slouken@4919
   354
            SDL_Event event;
slouken@4919
   355
            event.tfinger.type = SDL_FINGERDOWN;
slouken@4919
   356
            event.tfinger.touchId = id;
slouken@4919
   357
            event.tfinger.x = x;
slouken@4919
   358
            event.tfinger.y = y;
slouken@4919
   359
            event.tfinger.pressure = pressure;
slouken@4919
   360
            event.tfinger.state = touch->buttonstate;
slouken@4919
   361
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   362
            event.tfinger.fingerId = fingerid;
slouken@4919
   363
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   364
        }
slouken@4919
   365
        if(posted) finger->down = SDL_TRUE;
slouken@4919
   366
        return posted;
jim@4641
   367
    }
jim@4641
   368
    else {
slouken@4868
   369
        if(finger == NULL) {
slouken@4868
   370
            SDL_SetError("Finger not found.");
slouken@4868
   371
            return 0;
slouken@4868
   372
        }      
slouken@4919
   373
        posted = 0;
slouken@4919
   374
        if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
slouken@4919
   375
            SDL_Event event;
slouken@4919
   376
            event.tfinger.type = SDL_FINGERUP;
slouken@4919
   377
            event.tfinger.touchId =  id;
slouken@4919
   378
            event.tfinger.state = touch->buttonstate;
slouken@4919
   379
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   380
            event.tfinger.fingerId = fingerid;
slouken@4919
   381
            //I don't trust the coordinates passed on fingerUp
slouken@4919
   382
            event.tfinger.x = finger->x; 
slouken@4919
   383
            event.tfinger.y = finger->y;
slouken@4919
   384
            event.tfinger.dx = 0;
slouken@4919
   385
            event.tfinger.dy = 0;
jim@4683
   386
slouken@4919
   387
            if(SDL_DelFinger(touch,fingerid) < 0) return 0;
slouken@4919
   388
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   389
        }        
slouken@4919
   390
        return posted;
jim@4641
   391
    }
jim@4641
   392
}
jim@4641
   393
jim@4641
   394
int
jimtla@4678
   395
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative, 
slouken@4919
   396
                    float xin, float yin, float pressurein)
jim@4640
   397
{
jim@4640
   398
    int index = SDL_GetTouchIndexId(id);
jim@4641
   399
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4641
   400
    SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
jim@4640
   401
    int posted;
jim@4683
   402
    Sint16 xrel, yrel;
jimtla@4676
   403
    float x_max = 0, y_max = 0;
slouken@4919
   404
        Uint16 x;
slouken@4919
   405
        Uint16 y;
slouken@4919
   406
        Uint16 pressure;
jim@4655
   407
    
jim@4674
   408
    if (!touch) {
jim@4674
   409
      return SDL_TouchNotFoundError(id);
jim@4674
   410
    }
jimtla@4678
   411
jimtla@4678
   412
    //scale to Integer coordinates
aschiffler@4865
   413
    x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
aschiffler@4865
   414
    y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
aschiffler@4865
   415
    pressure = (Uint16)((yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
jim@4674
   416
    if(touch->flush_motion) {
slouken@4919
   417
        return 0;
jim@4640
   418
    }
jim@4654
   419
    
jim@4655
   420
    if(finger == NULL || !finger->down) {
slouken@4919
   421
        return SDL_SendFingerDown(id,fingerid,SDL_TRUE,xin,yin,pressurein);        
jim@4654
   422
    } else {
slouken@4919
   423
        /* the relative motion is calculated regarding the last position */
slouken@4919
   424
        if (relative) {
slouken@4919
   425
            xrel = x;
slouken@4919
   426
            yrel = y;
slouken@4919
   427
            x = (finger->last_x + x);
slouken@4919
   428
            y = (finger->last_y + y);
slouken@4919
   429
        } else {
slouken@4919
   430
            if(xin < touch->x_min) x = finger->last_x; /*If movement is only in one axis,*/
slouken@4919
   431
            if(yin < touch->y_min) y = finger->last_y; /*The other is marked as -1*/
slouken@4919
   432
            if(pressurein < touch->pressure_min) pressure = finger->last_pressure;
slouken@4919
   433
            xrel = x - finger->last_x;
slouken@4919
   434
            yrel = y - finger->last_y;
slouken@4919
   435
            //printf("xrel,yrel (%i,%i)\n",(int)xrel,(int)yrel);
slouken@4919
   436
        }
slouken@4919
   437
        
slouken@4919
   438
        /* Drop events that don't change state */
slouken@4919
   439
        if (!xrel && !yrel) {
jim@4654
   440
#if 0
slouken@4919
   441
            printf("Touch event didn't change state - dropped!\n");
jim@4654
   442
#endif
slouken@4919
   443
            return 0;
slouken@4919
   444
        }
slouken@4919
   445
        
slouken@4919
   446
        /* Update internal touch coordinates */
slouken@4919
   447
        
slouken@4919
   448
        finger->x = x;
slouken@4919
   449
        finger->y = y;
slouken@4919
   450
        
slouken@4919
   451
        /*Should scale to window? Normalize? Maintain Aspect?*/
slouken@4919
   452
        //SDL_GetWindowSize(touch->focus, &x_max, &y_max);
slouken@4919
   453
        
slouken@4919
   454
        /* make sure that the pointers find themselves inside the windows */
slouken@4919
   455
        /* only check if touch->xmax is set ! */
slouken@4919
   456
        /*
slouken@4919
   457
          if (x_max && touch->x > x_max) {
slouken@4919
   458
          touch->x = x_max;
slouken@4919
   459
          } else if (touch->x < 0) {
slouken@4919
   460
          touch->x = 0;
slouken@4919
   461
          }
slouken@4919
   462
          
slouken@4919
   463
          if (y_max && touch->y > y_max) {
slouken@4919
   464
          touch->y = y_max;
slouken@4919
   465
          } else if (touch->y < 0) {
slouken@4919
   466
          touch->y = 0;
slouken@4919
   467
          }
slouken@4919
   468
        */
slouken@4919
   469
        finger->xdelta = xrel;
slouken@4919
   470
        finger->ydelta = yrel;
slouken@4919
   471
        finger->pressure = pressure;
slouken@4919
   472
        
slouken@4919
   473
        
slouken@4919
   474
        
slouken@4919
   475
        /* Post the event, if desired */
slouken@4919
   476
        posted = 0;
slouken@4919
   477
        if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
slouken@4919
   478
            SDL_Event event;
slouken@4919
   479
            event.tfinger.type = SDL_FINGERMOTION;
slouken@4919
   480
            event.tfinger.touchId = id;
slouken@4919
   481
            event.tfinger.fingerId = fingerid;
slouken@4919
   482
            event.tfinger.x = x;
slouken@4919
   483
            event.tfinger.y = y;
slouken@4919
   484
            event.tfinger.dx = xrel;
slouken@4919
   485
            event.tfinger.dy = yrel;            
slouken@4919
   486
                
slouken@4919
   487
            event.tfinger.pressure = pressure;
slouken@4919
   488
            event.tfinger.state = touch->buttonstate;
slouken@4919
   489
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   490
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   491
        }
slouken@4919
   492
        finger->last_x = finger->x;
slouken@4919
   493
        finger->last_y = finger->y;
slouken@4919
   494
        finger->last_pressure = finger->pressure;
slouken@4919
   495
        return posted;
jim@4655
   496
    }
jim@4640
   497
}
jim@4640
   498
int
jimtla@4678
   499
SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button)
jim@4640
   500
{
jim@4641
   501
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   502
    int posted;
jim@4640
   503
    Uint32 type;
jim@4640
   504
jim@4674
   505
    
jim@4640
   506
    if (!touch) {
jim@4674
   507
      return SDL_TouchNotFoundError(id);
jim@4640
   508
    }
jim@4640
   509
jim@4640
   510
    /* Figure out which event to perform */
jim@4640
   511
    switch (state) {
jim@4640
   512
    case SDL_PRESSED:
jim@4640
   513
        if (touch->buttonstate & SDL_BUTTON(button)) {
jim@4640
   514
            /* Ignore this event, no state change */
jim@4640
   515
            return 0;
jim@4640
   516
        }
jim@4640
   517
        type = SDL_TOUCHBUTTONDOWN;
jim@4640
   518
        touch->buttonstate |= SDL_BUTTON(button);
jim@4640
   519
        break;
jim@4640
   520
    case SDL_RELEASED:
jim@4640
   521
        if (!(touch->buttonstate & SDL_BUTTON(button))) {
jim@4640
   522
            /* Ignore this event, no state change */
jim@4640
   523
            return 0;
jim@4640
   524
        }
jim@4640
   525
        type = SDL_TOUCHBUTTONUP;
jim@4640
   526
        touch->buttonstate &= ~SDL_BUTTON(button);
jim@4640
   527
        break;
jim@4640
   528
    default:
jim@4640
   529
        /* Invalid state -- bail */
jim@4640
   530
        return 0;
jim@4640
   531
    }
jim@4640
   532
jim@4640
   533
    /* Post the event, if desired */
jim@4640
   534
    posted = 0;
jim@4640
   535
    if (SDL_GetEventState(type) == SDL_ENABLE) {
jim@4640
   536
        SDL_Event event;
jim@4640
   537
        event.type = type;
jimtla@4676
   538
        event.tbutton.touchId = touch->id;
jim@4641
   539
        event.tbutton.state = state;
jim@4641
   540
        event.tbutton.button = button;
jim@4641
   541
        event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
jim@4640
   542
        posted = (SDL_PushEvent(&event) > 0);
jim@4640
   543
    }
jim@4640
   544
    return posted;
jim@4640
   545
}
jim@4640
   546
jim@4641
   547
char *
jimtla@4678
   548
SDL_GetTouchName(SDL_TouchID id)
jim@4640
   549
{
jim@4641
   550
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   551
    if (!touch) {
jim@4640
   552
        return NULL;
jim@4640
   553
    }
jim@4640
   554
    return touch->name;
jim@4640
   555
}
jim@4640
   556
jimtla@4678
   557
int SDL_TouchNotFoundError(SDL_TouchID id) {
slouken@4868
   558
  //int i;
slouken@4868
   559
  SDL_SetError("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id);
slouken@4868
   560
#if 0
jim@4674
   561
  printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch);
jim@4674
   562
  for(i=0;i < SDL_num_touch;i++) {
jimtla@4676
   563
    printf("ERROR: %li\n",SDL_touchPads[i]->id);
jim@4674
   564
  }
slouken@4868
   565
#endif
jim@4674
   566
  return 0;
jim@4674
   567
}
jim@4640
   568
/* vi: set ts=4 sw=4 expandtab: */