src/events/SDL_touch.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6863 73be5b6b2a86
child 6951 7833f01322b3
permissions -rw-r--r--
Happy New Year!
jim@4640
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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 = SDL_strlen(name);
jim@4641
   129
    SDL_touchPads[index]->focus = 0;
jim@4641
   130
    SDL_touchPads[index]->name = SDL_malloc((length + 2) * sizeof(char));
jim@4642
   131
    SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);   
jim@4642
   132
jim@4642
   133
    SDL_touchPads[index]->num_fingers = 0;
jim@4646
   134
    SDL_touchPads[index]->max_fingers = 1;
jim@4646
   135
    SDL_touchPads[index]->fingers = (SDL_Finger **) SDL_malloc(sizeof(SDL_Finger*));
jim@4646
   136
    SDL_touchPads[index]->fingers[0] = NULL;
jim@4642
   137
    SDL_touchPads[index]->buttonstate = 0;
jim@4642
   138
    SDL_touchPads[index]->relative_mode = SDL_FALSE;
jim@4642
   139
    SDL_touchPads[index]->flush_motion = SDL_FALSE;
jim@4641
   140
    
jimtla@4678
   141
    SDL_touchPads[index]->xres = (1<<(16-1));
jimtla@4678
   142
    SDL_touchPads[index]->yres = (1<<(16-1));
slouken@6299
   143
    SDL_touchPads[index]->pressureres = (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
                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));
slouken@6299
   331
    pressure = (Uint16)((pressurein+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@6299
   359
            event.tfinger.dx = 0;
slouken@6299
   360
            event.tfinger.dy = 0;
slouken@4919
   361
            event.tfinger.pressure = pressure;
slouken@4919
   362
            event.tfinger.state = touch->buttonstate;
slouken@4919
   363
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   364
            event.tfinger.fingerId = fingerid;
slouken@4919
   365
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   366
        }
slouken@4919
   367
        if(posted) finger->down = SDL_TRUE;
slouken@4919
   368
        return posted;
jim@4641
   369
    }
jim@4641
   370
    else {
slouken@4868
   371
        if(finger == NULL) {
slouken@4868
   372
            SDL_SetError("Finger not found.");
slouken@4868
   373
            return 0;
slouken@4868
   374
        }      
slouken@4919
   375
        posted = 0;
slouken@4919
   376
        if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
slouken@4919
   377
            SDL_Event event;
slouken@4919
   378
            event.tfinger.type = SDL_FINGERUP;
slouken@4919
   379
            event.tfinger.touchId =  id;
slouken@4919
   380
            event.tfinger.state = touch->buttonstate;
slouken@4919
   381
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   382
            event.tfinger.fingerId = fingerid;
slouken@4919
   383
            //I don't trust the coordinates passed on fingerUp
slouken@4919
   384
            event.tfinger.x = finger->x; 
slouken@4919
   385
            event.tfinger.y = finger->y;
slouken@4919
   386
            event.tfinger.dx = 0;
slouken@4919
   387
            event.tfinger.dy = 0;
slouken@6299
   388
            event.tfinger.pressure = pressure;
jim@4683
   389
slouken@4919
   390
            if(SDL_DelFinger(touch,fingerid) < 0) return 0;
slouken@4919
   391
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   392
        }        
slouken@4919
   393
        return posted;
jim@4641
   394
    }
jim@4641
   395
}
jim@4641
   396
jim@4641
   397
int
jimtla@4678
   398
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative, 
slouken@4919
   399
                    float xin, float yin, float pressurein)
jim@4640
   400
{
slouken@5124
   401
    SDL_Touch *touch;
slouken@5124
   402
    SDL_Finger *finger;
jim@4640
   403
    int posted;
jim@4683
   404
    Sint16 xrel, yrel;
slouken@5124
   405
    Uint16 x;
slouken@5124
   406
    Uint16 y;
slouken@5124
   407
    Uint16 pressure;
jim@4655
   408
    
slouken@5124
   409
    touch = SDL_GetTouch(id);
jim@4674
   410
    if (!touch) {
jim@4674
   411
      return SDL_TouchNotFoundError(id);
jim@4674
   412
    }
jimtla@4678
   413
jimtla@4678
   414
    //scale to Integer coordinates
aschiffler@4865
   415
    x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
aschiffler@4865
   416
    y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
slouken@6299
   417
    pressure = (Uint16)((pressurein+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
jim@4674
   418
    if(touch->flush_motion) {
slouken@4919
   419
        return 0;
jim@4640
   420
    }
jim@4654
   421
    
slouken@5124
   422
    finger = SDL_GetFinger(touch,fingerid);
jim@4655
   423
    if(finger == NULL || !finger->down) {
slouken@4919
   424
        return SDL_SendFingerDown(id,fingerid,SDL_TRUE,xin,yin,pressurein);        
jim@4654
   425
    } else {
slouken@4919
   426
        /* the relative motion is calculated regarding the last position */
slouken@4919
   427
        if (relative) {
slouken@4919
   428
            xrel = x;
slouken@4919
   429
            yrel = y;
slouken@4919
   430
            x = (finger->last_x + x);
slouken@4919
   431
            y = (finger->last_y + y);
slouken@4919
   432
        } else {
slouken@4919
   433
            if(xin < touch->x_min) x = finger->last_x; /*If movement is only in one axis,*/
slouken@4919
   434
            if(yin < touch->y_min) y = finger->last_y; /*The other is marked as -1*/
slouken@4919
   435
            if(pressurein < touch->pressure_min) pressure = finger->last_pressure;
slouken@4919
   436
            xrel = x - finger->last_x;
slouken@4919
   437
            yrel = y - finger->last_y;
slouken@4919
   438
            //printf("xrel,yrel (%i,%i)\n",(int)xrel,(int)yrel);
slouken@4919
   439
        }
slouken@4919
   440
        
slouken@4919
   441
        /* Drop events that don't change state */
slouken@4919
   442
        if (!xrel && !yrel) {
jim@4654
   443
#if 0
slouken@4919
   444
            printf("Touch event didn't change state - dropped!\n");
jim@4654
   445
#endif
slouken@4919
   446
            return 0;
slouken@4919
   447
        }
slouken@4919
   448
        
slouken@4919
   449
        /* Update internal touch coordinates */
slouken@4919
   450
        
slouken@4919
   451
        finger->x = x;
slouken@4919
   452
        finger->y = y;
slouken@4919
   453
        
slouken@4919
   454
        /*Should scale to window? Normalize? Maintain Aspect?*/
slouken@4919
   455
        //SDL_GetWindowSize(touch->focus, &x_max, &y_max);
slouken@4919
   456
        
slouken@4919
   457
        /* make sure that the pointers find themselves inside the windows */
slouken@4919
   458
        /* only check if touch->xmax is set ! */
slouken@4919
   459
        /*
slouken@4919
   460
          if (x_max && touch->x > x_max) {
slouken@4919
   461
          touch->x = x_max;
slouken@4919
   462
          } else if (touch->x < 0) {
slouken@4919
   463
          touch->x = 0;
slouken@4919
   464
          }
slouken@4919
   465
          
slouken@4919
   466
          if (y_max && touch->y > y_max) {
slouken@4919
   467
          touch->y = y_max;
slouken@4919
   468
          } else if (touch->y < 0) {
slouken@4919
   469
          touch->y = 0;
slouken@4919
   470
          }
slouken@4919
   471
        */
slouken@4919
   472
        finger->xdelta = xrel;
slouken@4919
   473
        finger->ydelta = yrel;
slouken@4919
   474
        finger->pressure = pressure;
slouken@4919
   475
        
slouken@4919
   476
        
slouken@4919
   477
        
slouken@4919
   478
        /* Post the event, if desired */
slouken@4919
   479
        posted = 0;
slouken@4919
   480
        if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
slouken@4919
   481
            SDL_Event event;
slouken@4919
   482
            event.tfinger.type = SDL_FINGERMOTION;
slouken@4919
   483
            event.tfinger.touchId = id;
slouken@4919
   484
            event.tfinger.fingerId = fingerid;
slouken@4919
   485
            event.tfinger.x = x;
slouken@4919
   486
            event.tfinger.y = y;
slouken@4919
   487
            event.tfinger.dx = xrel;
slouken@4919
   488
            event.tfinger.dy = yrel;            
slouken@4919
   489
                
slouken@4919
   490
            event.tfinger.pressure = pressure;
slouken@4919
   491
            event.tfinger.state = touch->buttonstate;
slouken@4919
   492
            event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
slouken@4919
   493
            posted = (SDL_PushEvent(&event) > 0);
slouken@4919
   494
        }
slouken@4919
   495
        finger->last_x = finger->x;
slouken@4919
   496
        finger->last_y = finger->y;
slouken@4919
   497
        finger->last_pressure = finger->pressure;
slouken@4919
   498
        return posted;
jim@4655
   499
    }
jim@4640
   500
}
slouken@5124
   501
jim@4640
   502
int
jimtla@4678
   503
SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button)
jim@4640
   504
{
slouken@5124
   505
    SDL_Touch *touch;
jim@4640
   506
    int posted;
jim@4640
   507
    Uint32 type;
jim@4640
   508
jim@4674
   509
    
slouken@5124
   510
    touch = SDL_GetTouch(id);
jim@4640
   511
    if (!touch) {
jim@4674
   512
      return SDL_TouchNotFoundError(id);
jim@4640
   513
    }
jim@4640
   514
jim@4640
   515
    /* Figure out which event to perform */
jim@4640
   516
    switch (state) {
jim@4640
   517
    case SDL_PRESSED:
jim@4640
   518
        if (touch->buttonstate & SDL_BUTTON(button)) {
jim@4640
   519
            /* Ignore this event, no state change */
jim@4640
   520
            return 0;
jim@4640
   521
        }
jim@4640
   522
        type = SDL_TOUCHBUTTONDOWN;
jim@4640
   523
        touch->buttonstate |= SDL_BUTTON(button);
jim@4640
   524
        break;
jim@4640
   525
    case SDL_RELEASED:
jim@4640
   526
        if (!(touch->buttonstate & SDL_BUTTON(button))) {
jim@4640
   527
            /* Ignore this event, no state change */
jim@4640
   528
            return 0;
jim@4640
   529
        }
jim@4640
   530
        type = SDL_TOUCHBUTTONUP;
jim@4640
   531
        touch->buttonstate &= ~SDL_BUTTON(button);
jim@4640
   532
        break;
jim@4640
   533
    default:
jim@4640
   534
        /* Invalid state -- bail */
jim@4640
   535
        return 0;
jim@4640
   536
    }
jim@4640
   537
jim@4640
   538
    /* Post the event, if desired */
jim@4640
   539
    posted = 0;
jim@4640
   540
    if (SDL_GetEventState(type) == SDL_ENABLE) {
jim@4640
   541
        SDL_Event event;
jim@4640
   542
        event.type = type;
jimtla@4676
   543
        event.tbutton.touchId = touch->id;
jim@4641
   544
        event.tbutton.state = state;
jim@4641
   545
        event.tbutton.button = button;
jim@4641
   546
        event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
jim@4640
   547
        posted = (SDL_PushEvent(&event) > 0);
jim@4640
   548
    }
jim@4640
   549
    return posted;
jim@4640
   550
}
jim@4640
   551
jim@4641
   552
char *
jimtla@4678
   553
SDL_GetTouchName(SDL_TouchID id)
jim@4640
   554
{
jim@4641
   555
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   556
    if (!touch) {
jim@4640
   557
        return NULL;
jim@4640
   558
    }
jim@4640
   559
    return touch->name;
jim@4640
   560
}
jim@4640
   561
jimtla@4678
   562
int SDL_TouchNotFoundError(SDL_TouchID id) {
slouken@4868
   563
  //int i;
slouken@4868
   564
  SDL_SetError("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id);
slouken@4868
   565
#if 0
jim@4674
   566
  printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch);
jim@4674
   567
  for(i=0;i < SDL_num_touch;i++) {
jimtla@4676
   568
    printf("ERROR: %li\n",SDL_touchPads[i]->id);
jim@4674
   569
  }
slouken@4868
   570
#endif
jim@4674
   571
  return 0;
jim@4674
   572
}
jim@4640
   573
/* vi: set ts=4 sw=4 expandtab: */