src/events/SDL_touch.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 6044 35448a5ea044
child 6299 3809b2eee787
permissions -rwxr-xr-x
Happy New Year!
jim@4640
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
jim@4640
     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@4640
     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@4640
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
jim@4640
    20
*/
jim@4640
    21
#include "SDL_config.h"
jim@4640
    22
jim@4640
    23
/* General touch handling code for SDL */
jim@4640
    24
jim@4640
    25
#include "SDL_events.h"
jim@4640
    26
#include "SDL_events_c.h"
jim@4640
    27
#include "../video/SDL_sysvideo.h"
jim@4640
    28
jimtla@4648
    29
#include <stdio.h>
jimtla@4648
    30
jim@4640
    31
jim@4640
    32
static int SDL_num_touch = 0;
jim@4641
    33
static SDL_Touch **SDL_touchPads = NULL;
jim@4640
    34
jim@4640
    35
jim@4640
    36
/* Public functions */
jim@4640
    37
int
jim@4640
    38
SDL_TouchInit(void)
jim@4640
    39
{
jim@4667
    40
  return (0);
jim@4640
    41
}
jim@4642
    42
jim@4640
    43
SDL_Touch *
jimtla@4678
    44
SDL_GetTouch(SDL_TouchID id)
jim@4640
    45
{
jim@4641
    46
    int index = SDL_GetTouchIndexId(id);
jim@4640
    47
    if (index < 0 || index >= SDL_num_touch) {
jim@4640
    48
        return NULL;
jim@4640
    49
    }
jim@4641
    50
    return SDL_touchPads[index];
jim@4640
    51
}
jim@4640
    52
jim@4642
    53
SDL_Touch *
jim@4642
    54
SDL_GetTouchIndex(int index)
jim@4641
    55
{
jim@4642
    56
    if (index < 0 || index >= SDL_num_touch) {
jim@4642
    57
        return NULL;
jim@4642
    58
    }
jim@4642
    59
    return SDL_touchPads[index];
jim@4641
    60
}
jim@4641
    61
slouken@6044
    62
static int
jimtla@4678
    63
SDL_GetFingerIndexId(SDL_Touch* touch,SDL_FingerID fingerid)
jim@4641
    64
{
jim@4641
    65
    int i;
jim@4641
    66
    for(i = 0;i < touch->num_fingers;i++)
slouken@4919
    67
        if(touch->fingers[i]->id == fingerid)
slouken@4919
    68
            return i;
jim@4641
    69
    return -1;
jim@4641
    70
}
jim@4641
    71
jim@4642
    72
jim@4642
    73
SDL_Finger *
jimtla@4678
    74
SDL_GetFinger(SDL_Touch* touch,SDL_FingerID id)
jim@4642
    75
{
jim@4642
    76
    int index = SDL_GetFingerIndexId(touch,id);
jim@4642
    77
    if(index < 0 || index >= touch->num_fingers)
slouken@4919
    78
        return NULL;
jim@4642
    79
    return touch->fingers[index];
jim@4642
    80
}
jim@4642
    81
jim@4642
    82
jim@4641
    83
int
jimtla@4678
    84
SDL_GetTouchIndexId(SDL_TouchID id)
jim@4640
    85
{
jim@4640
    86
    int index;
jim@4640
    87
    SDL_Touch *touch;
jim@4640
    88
jim@4640
    89
    for (index = 0; index < SDL_num_touch; ++index) {
jim@4641
    90
        touch = SDL_touchPads[index];
jim@4640
    91
        if (touch->id == id) {
jim@4640
    92
            return index;
jim@4640
    93
        }
jim@4640
    94
    }
jim@4640
    95
    return -1;
jim@4640
    96
}
jim@4640
    97
jim@4640
    98
int
jim@4642
    99
SDL_AddTouch(const SDL_Touch * touch, char *name)
jim@4640
   100
{
jim@4641
   101
    SDL_Touch **touchPads;
slouken@5076
   102
    int index;
slouken@5076
   103
    size_t 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
    }
slouken@5455
   125
    SDL_memcpy(SDL_touchPads[index], touch, sizeof(*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
}
slouken@6044
   193
jim@4641
   194
SDL_Window *
jimtla@4678
   195
SDL_GetTouchFocusWindow(SDL_TouchID id)
jim@4640
   196
{
jim@4641
   197
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   198
jim@4640
   199
    if (!touch) {
jim@4640
   200
        return 0;
jim@4640
   201
    }
jim@4640
   202
    return touch->focus;
jim@4640
   203
}
jim@4640
   204
jim@4640
   205
void
jimtla@4678
   206
SDL_SetTouchFocus(SDL_TouchID id, SDL_Window * window)
jim@4640
   207
{
jim@4640
   208
    int index = SDL_GetTouchIndexId(id);
jim@4641
   209
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   210
    int i;
jim@4640
   211
    SDL_bool focus;
jim@4640
   212
jim@4640
   213
    if (!touch || (touch->focus == window)) {
jim@4640
   214
        return;
jim@4640
   215
    }
jim@4640
   216
jim@4640
   217
    /* See if the current window has lost focus */
jim@4640
   218
    if (touch->focus) {
jim@4640
   219
        focus = SDL_FALSE;
jim@4640
   220
        for (i = 0; i < SDL_num_touch; ++i) {
jim@4640
   221
            SDL_Touch *check;
jim@4640
   222
            if (i != index) {
jim@4641
   223
                check = SDL_touchPads[i];
jim@4640
   224
                if (check && check->focus == touch->focus) {
jim@4640
   225
                    focus = SDL_TRUE;
jim@4640
   226
                    break;
jim@4640
   227
                }
jim@4640
   228
            }
jim@4640
   229
        }
jim@4640
   230
        if (!focus) {
jim@4640
   231
            SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
jim@4640
   232
        }
jim@4640
   233
    }
jim@4640
   234
jim@4640
   235
    touch->focus = window;
jim@4640
   236
jim@4640
   237
    if (touch->focus) {
jim@4640
   238
        focus = SDL_FALSE;
jim@4640
   239
        for (i = 0; i < SDL_num_touch; ++i) {
jim@4640
   240
            SDL_Touch *check;
jim@4640
   241
            if (i != index) {
jim@4641
   242
                check = SDL_touchPads[i];
jim@4640
   243
                if (check && check->focus == touch->focus) {
jim@4640
   244
                    focus = SDL_TRUE;
jim@4640
   245
                    break;
jim@4640
   246
                }
jim@4640
   247
            }
jim@4640
   248
        }
jim@4640
   249
        if (!focus) {
jim@4640
   250
            SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
jim@4640
   251
        }
jim@4640
   252
    }
jim@4640
   253
}
jim@4640
   254
jim@4641
   255
int 
jimtla@4678
   256
SDL_AddFinger(SDL_Touch* touch,SDL_Finger *finger)
jim@4640
   257
{
jim@4641
   258
    int index;
jim@4641
   259
    SDL_Finger **fingers;
jim@4646
   260
    //printf("Adding Finger...\n");
jimtla@4678
   261
    if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
jim@4641
   262
        SDL_SetError("Finger ID already in use");
slouken@4919
   263
        }
jim@4640
   264
jim@4641
   265
    /* Add the touch to the list of touch */
jim@4646
   266
    if(touch->num_fingers  >= touch->max_fingers){
slouken@4919
   267
                //printf("Making room for it!\n");
slouken@4919
   268
                fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
slouken@4919
   269
                                                     (touch->num_fingers + 1) * sizeof(SDL_Finger *));
slouken@4919
   270
                touch->max_fingers = touch->num_fingers+1;
slouken@4919
   271
                if (!fingers) {
slouken@4919
   272
                        SDL_OutOfMemory();
slouken@4919
   273
                        return -1;
slouken@4919
   274
                } else {
slouken@4919
   275
                        touch->max_fingers = touch->num_fingers+1;
slouken@4919
   276
                        touch->fingers = fingers;
slouken@4919
   277
                }
slouken@4919
   278
        }
jim@4645
   279
jim@4646
   280
    index = touch->num_fingers;
jim@4646
   281
    //printf("Max_Fingers: %i Index: %i\n",touch->max_fingers,index);
jim@4641
   282
jim@4646
   283
    touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(SDL_Finger));
jim@4641
   284
    if (!touch->fingers[index]) {
jim@4641
   285
        SDL_OutOfMemory();
jim@4641
   286
        return -1;
jim@4641
   287
    }
jimtla@4678
   288
    *(touch->fingers[index]) = *finger;
jim@4646
   289
    touch->num_fingers++;
jim@4641
   290
jim@4641
   291
    return index;
jim@4640
   292
}
jim@4640
   293
jim@4640
   294
int
jimtla@4678
   295
SDL_DelFinger(SDL_Touch* touch,SDL_FingerID fingerid)
jim@4641
   296
{
jim@4642
   297
    int index = SDL_GetFingerIndexId(touch,fingerid);
jim@4641
   298
    SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
jim@4641
   299
jim@4641
   300
    if (!finger) {
jimtla@4648
   301
        return -1;
jim@4641
   302
    }
jim@4641
   303
 
jim@4641
   304
jim@4641
   305
    SDL_free(finger);
jim@4641
   306
    touch->num_fingers--;
jim@4641
   307
    touch->fingers[index] = touch->fingers[touch->num_fingers];
jim@4655
   308
    return 0;
jim@4641
   309
}
jim@4641
   310
jim@4641
   311
jim@4641
   312
int
jimtla@4678
   313
SDL_SendFingerDown(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down, 
slouken@4919
   314
                   float xin, float yin, float pressurein)
jim@4641
   315
{
jim@4643
   316
    int posted;
slouken@4919
   317
        Uint16 x;
slouken@4919
   318
        Uint16 y;
slouken@4919
   319
        Uint16 pressure;
slouken@4919
   320
        SDL_Finger *finger;
aschiffler@4865
   321
jim@4641
   322
    SDL_Touch* touch = SDL_GetTouch(id);
jim@4643
   323
jim@4674
   324
    if(!touch) {
jim@4674
   325
      return SDL_TouchNotFoundError(id);
jim@4674
   326
    }
jim@4683
   327
jim@4683
   328
    
jimtla@4678
   329
    //scale to Integer coordinates
aschiffler@4865
   330
    x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
aschiffler@4865
   331
    y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
aschiffler@4865
   332
    pressure = (Uint16)((yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
jim@4683
   333
    
aschiffler@4865
   334
    finger = SDL_GetFinger(touch,fingerid);
jim@4641
   335
    if(down) {
slouken@4919
   336
        if(finger == NULL) {
slouken@4919
   337
            SDL_Finger nf;
slouken@4919
   338
            nf.id = fingerid;
slouken@4919
   339
            nf.x = x;
slouken@4919
   340
            nf.y = y;
slouken@4919
   341
            nf.pressure = pressure;
slouken@4919
   342
            nf.xdelta = 0;
slouken@4919
   343
            nf.ydelta = 0;
slouken@4919
   344
            nf.last_x = x;
slouken@4919
   345
            nf.last_y = y;
slouken@4919
   346
            nf.last_pressure = pressure;
slouken@4919
   347
            nf.down = SDL_FALSE;
slouken@4919
   348
            if(SDL_AddFinger(touch,&nf) < 0) return 0;
slouken@4919
   349
            finger = SDL_GetFinger(touch,fingerid);
slouken@4919
   350
        }
slouken@4919
   351
        else if(finger->down) return 0;
slouken@4919
   352
        if(xin < touch->x_min || yin < touch->y_min) return 0; //should defer if only a partial input
slouken@4919
   353
        posted = 0;
slouken@4919
   354
        if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
slouken@4919
   355
            SDL_Event event;
slouken@4919
   356
            event.tfinger.type = SDL_FINGERDOWN;
slouken@4919
   357
            event.tfinger.touchId = id;
slouken@4919
   358
            event.tfinger.x = x;
slouken@4919
   359
            event.tfinger.y = y;
slouken@4919
   360
            event.tfinger.pressure = pressure;
slouken@4919
   361
            event.tfinger.state = touch->buttonstate;
slouken@4919
   362
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   363
            event.tfinger.fingerId = fingerid;
slouken@4919
   364
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   365
        }
slouken@4919
   366
        if(posted) finger->down = SDL_TRUE;
slouken@4919
   367
        return posted;
jim@4641
   368
    }
jim@4641
   369
    else {
slouken@4868
   370
        if(finger == NULL) {
slouken@4868
   371
            SDL_SetError("Finger not found.");
slouken@4868
   372
            return 0;
slouken@4868
   373
        }      
slouken@4919
   374
        posted = 0;
slouken@4919
   375
        if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
slouken@4919
   376
            SDL_Event event;
slouken@4919
   377
            event.tfinger.type = SDL_FINGERUP;
slouken@4919
   378
            event.tfinger.touchId =  id;
slouken@4919
   379
            event.tfinger.state = touch->buttonstate;
slouken@4919
   380
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   381
            event.tfinger.fingerId = fingerid;
slouken@4919
   382
            //I don't trust the coordinates passed on fingerUp
slouken@4919
   383
            event.tfinger.x = finger->x; 
slouken@4919
   384
            event.tfinger.y = finger->y;
slouken@4919
   385
            event.tfinger.dx = 0;
slouken@4919
   386
            event.tfinger.dy = 0;
jim@4683
   387
slouken@4919
   388
            if(SDL_DelFinger(touch,fingerid) < 0) return 0;
slouken@4919
   389
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   390
        }        
slouken@4919
   391
        return posted;
jim@4641
   392
    }
jim@4641
   393
}
jim@4641
   394
jim@4641
   395
int
jimtla@4678
   396
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative, 
slouken@4919
   397
                    float xin, float yin, float pressurein)
jim@4640
   398
{
slouken@5124
   399
    SDL_Touch *touch;
slouken@5124
   400
    SDL_Finger *finger;
jim@4640
   401
    int posted;
jim@4683
   402
    Sint16 xrel, yrel;
slouken@5124
   403
    Uint16 x;
slouken@5124
   404
    Uint16 y;
slouken@5124
   405
    Uint16 pressure;
jim@4655
   406
    
slouken@5124
   407
    touch = SDL_GetTouch(id);
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
    
slouken@5124
   420
    finger = SDL_GetFinger(touch,fingerid);
jim@4655
   421
    if(finger == NULL || !finger->down) {
slouken@4919
   422
        return SDL_SendFingerDown(id,fingerid,SDL_TRUE,xin,yin,pressurein);        
jim@4654
   423
    } else {
slouken@4919
   424
        /* the relative motion is calculated regarding the last position */
slouken@4919
   425
        if (relative) {
slouken@4919
   426
            xrel = x;
slouken@4919
   427
            yrel = y;
slouken@4919
   428
            x = (finger->last_x + x);
slouken@4919
   429
            y = (finger->last_y + y);
slouken@4919
   430
        } else {
slouken@4919
   431
            if(xin < touch->x_min) x = finger->last_x; /*If movement is only in one axis,*/
slouken@4919
   432
            if(yin < touch->y_min) y = finger->last_y; /*The other is marked as -1*/
slouken@4919
   433
            if(pressurein < touch->pressure_min) pressure = finger->last_pressure;
slouken@4919
   434
            xrel = x - finger->last_x;
slouken@4919
   435
            yrel = y - finger->last_y;
slouken@4919
   436
            //printf("xrel,yrel (%i,%i)\n",(int)xrel,(int)yrel);
slouken@4919
   437
        }
slouken@4919
   438
        
slouken@4919
   439
        /* Drop events that don't change state */
slouken@4919
   440
        if (!xrel && !yrel) {
jim@4654
   441
#if 0
slouken@4919
   442
            printf("Touch event didn't change state - dropped!\n");
jim@4654
   443
#endif
slouken@4919
   444
            return 0;
slouken@4919
   445
        }
slouken@4919
   446
        
slouken@4919
   447
        /* Update internal touch coordinates */
slouken@4919
   448
        
slouken@4919
   449
        finger->x = x;
slouken@4919
   450
        finger->y = y;
slouken@4919
   451
        
slouken@4919
   452
        /*Should scale to window? Normalize? Maintain Aspect?*/
slouken@4919
   453
        //SDL_GetWindowSize(touch->focus, &x_max, &y_max);
slouken@4919
   454
        
slouken@4919
   455
        /* make sure that the pointers find themselves inside the windows */
slouken@4919
   456
        /* only check if touch->xmax is set ! */
slouken@4919
   457
        /*
slouken@4919
   458
          if (x_max && touch->x > x_max) {
slouken@4919
   459
          touch->x = x_max;
slouken@4919
   460
          } else if (touch->x < 0) {
slouken@4919
   461
          touch->x = 0;
slouken@4919
   462
          }
slouken@4919
   463
          
slouken@4919
   464
          if (y_max && touch->y > y_max) {
slouken@4919
   465
          touch->y = y_max;
slouken@4919
   466
          } else if (touch->y < 0) {
slouken@4919
   467
          touch->y = 0;
slouken@4919
   468
          }
slouken@4919
   469
        */
slouken@4919
   470
        finger->xdelta = xrel;
slouken@4919
   471
        finger->ydelta = yrel;
slouken@4919
   472
        finger->pressure = pressure;
slouken@4919
   473
        
slouken@4919
   474
        
slouken@4919
   475
        
slouken@4919
   476
        /* Post the event, if desired */
slouken@4919
   477
        posted = 0;
slouken@4919
   478
        if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
slouken@4919
   479
            SDL_Event event;
slouken@4919
   480
            event.tfinger.type = SDL_FINGERMOTION;
slouken@4919
   481
            event.tfinger.touchId = id;
slouken@4919
   482
            event.tfinger.fingerId = fingerid;
slouken@4919
   483
            event.tfinger.x = x;
slouken@4919
   484
            event.tfinger.y = y;
slouken@4919
   485
            event.tfinger.dx = xrel;
slouken@4919
   486
            event.tfinger.dy = yrel;            
slouken@4919
   487
                
slouken@4919
   488
            event.tfinger.pressure = pressure;
slouken@4919
   489
            event.tfinger.state = touch->buttonstate;
slouken@4919
   490
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   491
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   492
        }
slouken@4919
   493
        finger->last_x = finger->x;
slouken@4919
   494
        finger->last_y = finger->y;
slouken@4919
   495
        finger->last_pressure = finger->pressure;
slouken@4919
   496
        return posted;
jim@4655
   497
    }
jim@4640
   498
}
slouken@5124
   499
jim@4640
   500
int
jimtla@4678
   501
SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button)
jim@4640
   502
{
slouken@5124
   503
    SDL_Touch *touch;
jim@4640
   504
    int posted;
jim@4640
   505
    Uint32 type;
jim@4640
   506
jim@4674
   507
    
slouken@5124
   508
    touch = SDL_GetTouch(id);
jim@4640
   509
    if (!touch) {
jim@4674
   510
      return SDL_TouchNotFoundError(id);
jim@4640
   511
    }
jim@4640
   512
jim@4640
   513
    /* Figure out which event to perform */
jim@4640
   514
    switch (state) {
jim@4640
   515
    case SDL_PRESSED:
jim@4640
   516
        if (touch->buttonstate & SDL_BUTTON(button)) {
jim@4640
   517
            /* Ignore this event, no state change */
jim@4640
   518
            return 0;
jim@4640
   519
        }
jim@4640
   520
        type = SDL_TOUCHBUTTONDOWN;
jim@4640
   521
        touch->buttonstate |= SDL_BUTTON(button);
jim@4640
   522
        break;
jim@4640
   523
    case SDL_RELEASED:
jim@4640
   524
        if (!(touch->buttonstate & SDL_BUTTON(button))) {
jim@4640
   525
            /* Ignore this event, no state change */
jim@4640
   526
            return 0;
jim@4640
   527
        }
jim@4640
   528
        type = SDL_TOUCHBUTTONUP;
jim@4640
   529
        touch->buttonstate &= ~SDL_BUTTON(button);
jim@4640
   530
        break;
jim@4640
   531
    default:
jim@4640
   532
        /* Invalid state -- bail */
jim@4640
   533
        return 0;
jim@4640
   534
    }
jim@4640
   535
jim@4640
   536
    /* Post the event, if desired */
jim@4640
   537
    posted = 0;
jim@4640
   538
    if (SDL_GetEventState(type) == SDL_ENABLE) {
jim@4640
   539
        SDL_Event event;
jim@4640
   540
        event.type = type;
jimtla@4676
   541
        event.tbutton.touchId = touch->id;
jim@4641
   542
        event.tbutton.state = state;
jim@4641
   543
        event.tbutton.button = button;
jim@4641
   544
        event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
jim@4640
   545
        posted = (SDL_PushEvent(&event) > 0);
jim@4640
   546
    }
jim@4640
   547
    return posted;
jim@4640
   548
}
jim@4640
   549
jim@4641
   550
char *
jimtla@4678
   551
SDL_GetTouchName(SDL_TouchID id)
jim@4640
   552
{
jim@4641
   553
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   554
    if (!touch) {
jim@4640
   555
        return NULL;
jim@4640
   556
    }
jim@4640
   557
    return touch->name;
jim@4640
   558
}
jim@4640
   559
jimtla@4678
   560
int SDL_TouchNotFoundError(SDL_TouchID id) {
slouken@4868
   561
  //int i;
slouken@4868
   562
  SDL_SetError("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id);
slouken@4868
   563
#if 0
jim@4674
   564
  printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch);
jim@4674
   565
  for(i=0;i < SDL_num_touch;i++) {
jimtla@4676
   566
    printf("ERROR: %li\n",SDL_touchPads[i]->id);
jim@4674
   567
  }
slouken@4868
   568
#endif
jim@4674
   569
  return 0;
jim@4674
   570
}
jim@4640
   571
/* vi: set ts=4 sw=4 expandtab: */