src/events/SDL_touch.c
author jimtla
Fri, 30 Jul 2010 23:18:35 +0400
changeset 4676 99b4560b7aa1
parent 4674 89d5e2201b00
child 4678 f8431f66613d
permissions -rw-r--r--
Upgraded touchId/fingerId to long. Changed position variables to floats.
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@4676
    45
SDL_GetTouch(long 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@4676
    64
SDL_GetFingerIndexId(SDL_Touch* touch,long fingerid)
jim@4641
    65
{
jim@4641
    66
    int i;
jim@4641
    67
    for(i = 0;i < touch->num_fingers;i++)
jim@4641
    68
	if(touch->fingers[i]->id == fingerid)
jim@4641
    69
	    return i;
jim@4641
    70
    return -1;
jim@4641
    71
}
jim@4641
    72
jim@4642
    73
jim@4642
    74
SDL_Finger *
jimtla@4676
    75
SDL_GetFinger(SDL_Touch* touch,long id)
jim@4642
    76
{
jim@4642
    77
    int index = SDL_GetFingerIndexId(touch,id);
jim@4642
    78
    if(index < 0 || index >= touch->num_fingers)
jim@4642
    79
	return NULL;
jim@4642
    80
    return touch->fingers[index];
jim@4642
    81
}
jim@4642
    82
jim@4642
    83
jim@4641
    84
int
jimtla@4676
    85
SDL_GetTouchIndexId(long 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
    
jim@4657
   142
    //Do I want this here? Probably
jim@4657
   143
    SDL_GestureAddTouch(SDL_touchPads[index]);
jim@4657
   144
jim@4640
   145
    return index;
jim@4640
   146
}
jim@4640
   147
jim@4640
   148
void
jimtla@4676
   149
SDL_DelTouch(long id)
jim@4640
   150
{
jim@4641
   151
    int index = SDL_GetTouchIndexId(id);
jim@4641
   152
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   153
jim@4640
   154
    if (!touch) {
jim@4640
   155
        return;
jim@4640
   156
    }
jim@4640
   157
jim@4641
   158
    
jim@4640
   159
    SDL_free(touch->name);
jim@4640
   160
 
jim@4640
   161
    if (touch->FreeTouch) {
jim@4640
   162
        touch->FreeTouch(touch);
jim@4640
   163
    }
jim@4640
   164
    SDL_free(touch);
jim@4640
   165
jim@4641
   166
    SDL_num_touch--;
jim@4641
   167
    SDL_touchPads[index] = SDL_touchPads[SDL_num_touch];
jim@4640
   168
}
jim@4640
   169
jim@4640
   170
void
jim@4640
   171
SDL_TouchQuit(void)
jim@4640
   172
{
jim@4640
   173
    int i;
jim@4640
   174
jim@4641
   175
    for (i = SDL_num_touch-1; i > 0 ; --i) {
jim@4640
   176
        SDL_DelTouch(i);
jim@4640
   177
    }
jim@4640
   178
    SDL_num_touch = 0;
jim@4640
   179
jim@4641
   180
    if (SDL_touchPads) {
jim@4641
   181
        SDL_free(SDL_touchPads);
jim@4641
   182
        SDL_touchPads = NULL;
jim@4640
   183
    }
jim@4640
   184
}
jim@4640
   185
jim@4640
   186
int
jim@4640
   187
SDL_GetNumTouch(void)
jim@4640
   188
{
jim@4640
   189
    return SDL_num_touch;
jim@4640
   190
}
jim@4641
   191
SDL_Window *
jimtla@4676
   192
SDL_GetTouchFocusWindow(long id)
jim@4640
   193
{
jim@4641
   194
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   195
jim@4640
   196
    if (!touch) {
jim@4640
   197
        return 0;
jim@4640
   198
    }
jim@4640
   199
    return touch->focus;
jim@4640
   200
}
jim@4640
   201
jim@4640
   202
void
jimtla@4676
   203
SDL_SetTouchFocus(long id, SDL_Window * window)
jim@4640
   204
{
jim@4640
   205
    int index = SDL_GetTouchIndexId(id);
jim@4641
   206
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   207
    int i;
jim@4640
   208
    SDL_bool focus;
jim@4640
   209
jim@4640
   210
    if (!touch || (touch->focus == window)) {
jim@4640
   211
        return;
jim@4640
   212
    }
jim@4640
   213
jim@4640
   214
    /* See if the current window has lost focus */
jim@4640
   215
    if (touch->focus) {
jim@4640
   216
        focus = SDL_FALSE;
jim@4640
   217
        for (i = 0; i < SDL_num_touch; ++i) {
jim@4640
   218
            SDL_Touch *check;
jim@4640
   219
            if (i != index) {
jim@4641
   220
                check = SDL_touchPads[i];
jim@4640
   221
                if (check && check->focus == touch->focus) {
jim@4640
   222
                    focus = SDL_TRUE;
jim@4640
   223
                    break;
jim@4640
   224
                }
jim@4640
   225
            }
jim@4640
   226
        }
jim@4640
   227
        if (!focus) {
jim@4640
   228
            SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
jim@4640
   229
        }
jim@4640
   230
    }
jim@4640
   231
jim@4640
   232
    touch->focus = window;
jim@4640
   233
jim@4640
   234
    if (touch->focus) {
jim@4640
   235
        focus = SDL_FALSE;
jim@4640
   236
        for (i = 0; i < SDL_num_touch; ++i) {
jim@4640
   237
            SDL_Touch *check;
jim@4640
   238
            if (i != index) {
jim@4641
   239
                check = SDL_touchPads[i];
jim@4640
   240
                if (check && check->focus == touch->focus) {
jim@4640
   241
                    focus = SDL_TRUE;
jim@4640
   242
                    break;
jim@4640
   243
                }
jim@4640
   244
            }
jim@4640
   245
        }
jim@4640
   246
        if (!focus) {
jim@4640
   247
            SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
jim@4640
   248
        }
jim@4640
   249
    }
jim@4640
   250
}
jim@4640
   251
jim@4641
   252
int 
jimtla@4676
   253
SDL_AddFinger(SDL_Touch* touch,SDL_Finger finger)
jim@4640
   254
{
jim@4641
   255
    int index;
jim@4641
   256
    SDL_Finger **fingers;
jim@4646
   257
    //printf("Adding Finger...\n");
jimtla@4676
   258
    if (SDL_GetFingerIndexId(touch,finger.id) != -1) {
jim@4641
   259
        SDL_SetError("Finger ID already in use");
jim@4642
   260
	}
jim@4640
   261
jim@4641
   262
    /* Add the touch to the list of touch */
jim@4646
   263
    if(touch->num_fingers  >= touch->max_fingers){
jimtla@4648
   264
		//printf("Making room for it!\n");
jimtla@4648
   265
		fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
jimtla@4648
   266
  						   (touch->num_fingers + 1) * sizeof(SDL_Finger *));
jimtla@4648
   267
		touch->max_fingers = touch->num_fingers+1;
jimtla@4648
   268
		if (!fingers) {
jimtla@4648
   269
			SDL_OutOfMemory();
jimtla@4648
   270
			return -1;
jimtla@4648
   271
		} else {
jimtla@4648
   272
			touch->max_fingers = touch->num_fingers+1;
jimtla@4648
   273
			touch->fingers = fingers;
jimtla@4648
   274
		}
jim@4646
   275
	}
jim@4645
   276
jim@4646
   277
    index = touch->num_fingers;
jim@4646
   278
    //printf("Max_Fingers: %i Index: %i\n",touch->max_fingers,index);
jim@4641
   279
jim@4646
   280
    touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(SDL_Finger));
jim@4641
   281
    if (!touch->fingers[index]) {
jim@4641
   282
        SDL_OutOfMemory();
jim@4641
   283
        return -1;
jim@4641
   284
    }
jimtla@4676
   285
    *(touch->fingers[index]) = finger;
jim@4646
   286
    touch->num_fingers++;
jim@4641
   287
jim@4641
   288
    return index;
jim@4640
   289
}
jim@4640
   290
jim@4640
   291
int
jimtla@4676
   292
SDL_DelFinger(SDL_Touch* touch,long fingerid)
jim@4641
   293
{
jim@4642
   294
    int index = SDL_GetFingerIndexId(touch,fingerid);
jim@4641
   295
    SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
jim@4641
   296
jim@4641
   297
    if (!finger) {
jimtla@4648
   298
        return -1;
jim@4641
   299
    }
jim@4641
   300
 
jim@4641
   301
jim@4641
   302
    SDL_free(finger);
jim@4641
   303
    touch->num_fingers--;
jim@4641
   304
    touch->fingers[index] = touch->fingers[touch->num_fingers];
jim@4655
   305
    return 0;
jim@4641
   306
}
jim@4641
   307
jim@4641
   308
jim@4641
   309
int
jimtla@4676
   310
SDL_SendFingerDown(long id, long fingerid, SDL_bool down, float x, float y, float pressure)
jim@4641
   311
{
jim@4643
   312
    int posted;
jim@4641
   313
    SDL_Touch* touch = SDL_GetTouch(id);
jim@4643
   314
jim@4674
   315
    if(!touch) {
jim@4674
   316
      return SDL_TouchNotFoundError(id);
jim@4674
   317
    }
jim@4674
   318
jim@4641
   319
    if(down) {
jim@4655
   320
	SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
jim@4655
   321
	if(finger == NULL) {
jim@4655
   322
	    SDL_Finger nf;
jim@4655
   323
	    nf.id = fingerid;
jim@4655
   324
	    nf.x = x;
jim@4655
   325
	    nf.y = y;
jim@4655
   326
	    nf.pressure = pressure;
jim@4655
   327
	    nf.xdelta = 0;
jim@4655
   328
	    nf.ydelta = 0;
jim@4655
   329
	    nf.last_x = x;
jim@4655
   330
	    nf.last_y = y;
jim@4655
   331
	    nf.last_pressure = pressure;
jim@4655
   332
	    nf.down = SDL_FALSE;
jimtla@4676
   333
	    SDL_AddFinger(touch,nf);
jim@4655
   334
	    finger = &nf;
jim@4655
   335
	}
jim@4655
   336
	else if(finger->down) return 0;
jim@4655
   337
	if(x < 0 || y < 0) return 0; //should defer if only a partial input
jim@4641
   338
	posted = 0;
jim@4641
   339
	if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
jim@4641
   340
	    SDL_Event event;
jim@4641
   341
	    event.tfinger.type = SDL_FINGERDOWN;
jimtla@4676
   342
	    event.tfinger.touchId = id;
jim@4642
   343
	    event.tfinger.x = x;
jim@4642
   344
	    event.tfinger.y = y;
jim@4641
   345
	    event.tfinger.state = touch->buttonstate;
jim@4641
   346
	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
jim@4643
   347
	    event.tfinger.fingerId = fingerid;
jim@4641
   348
	    posted = (SDL_PushEvent(&event) > 0);
jim@4641
   349
	}
jim@4655
   350
	if(posted) finger->down = SDL_TRUE;
jim@4641
   351
	return posted;
jim@4641
   352
    }
jim@4641
   353
    else {
jim@4655
   354
	if(SDL_DelFinger(touch,fingerid) < 0) return 0;
jim@4641
   355
	posted = 0;
jim@4641
   356
	if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
jim@4641
   357
	    SDL_Event event;
jim@4641
   358
	    event.tfinger.type = SDL_FINGERUP;
jimtla@4676
   359
	    event.tfinger.touchId =  id;
jim@4641
   360
	    event.tfinger.state = touch->buttonstate;
jim@4641
   361
	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
jim@4643
   362
	    event.tfinger.fingerId = fingerid;
jim@4641
   363
	    posted = (SDL_PushEvent(&event) > 0);
jim@4641
   364
	}
jim@4641
   365
	return posted;
jim@4641
   366
    }
jim@4641
   367
}
jim@4641
   368
jim@4641
   369
int
jimtla@4676
   370
SDL_SendTouchMotion(long id, long fingerid, int relative, 
jimtla@4676
   371
		    float x, float y, float pressure)
jim@4640
   372
{
jim@4640
   373
    int index = SDL_GetTouchIndexId(id);
jim@4641
   374
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4641
   375
    SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
jim@4640
   376
    int posted;
jimtla@4676
   377
    float xrel;
jimtla@4676
   378
    float yrel;
jimtla@4676
   379
    float x_max = 0, y_max = 0;
jim@4655
   380
    
jim@4674
   381
    if (!touch) {
jim@4674
   382
      return SDL_TouchNotFoundError(id);
jim@4674
   383
    }
jim@4674
   384
    if(touch->flush_motion) {
jim@4655
   385
	return 0;
jim@4640
   386
    }
jim@4654
   387
    
jim@4655
   388
    if(finger == NULL || !finger->down) {
jim@4655
   389
	return SDL_SendFingerDown(id,fingerid,SDL_TRUE,x,y,pressure);	
jim@4654
   390
    } else {
jim@4655
   391
	/* the relative motion is calculated regarding the last position */
jim@4655
   392
	if (relative) {
jim@4655
   393
	    xrel = x;
jim@4655
   394
	    yrel = y;
jim@4655
   395
	    x = (finger->last_x + x);
jim@4655
   396
	    y = (finger->last_y + y);
jim@4655
   397
	} else {
jim@4655
   398
	    if(x < 0) x = finger->last_x; /*If movement is only in one axis,*/
jim@4655
   399
	    if(y < 0) y = finger->last_y; /*The other is marked as -1*/
jim@4655
   400
	    if(pressure < 0) pressure = finger->last_pressure;
jim@4655
   401
	    xrel = x - finger->last_x;
jim@4655
   402
	    yrel = y - finger->last_y;
jim@4655
   403
	}
jim@4655
   404
	
jim@4655
   405
	/* Drop events that don't change state */
jim@4655
   406
	if (!xrel && !yrel) {
jim@4654
   407
#if 0
jim@4655
   408
	    printf("Touch event didn't change state - dropped!\n");
jim@4654
   409
#endif
jim@4655
   410
	    return 0;
jim@4642
   411
	}
jim@4655
   412
	
jim@4655
   413
	/* Update internal touch coordinates */
jim@4655
   414
	
jim@4655
   415
	finger->x = x;
jim@4655
   416
	finger->y = y;
jim@4655
   417
	
jim@4655
   418
	/*Should scale to window? Normalize? Maintain Aspect?*/
jim@4655
   419
	//SDL_GetWindowSize(touch->focus, &x_max, &y_max);
jim@4655
   420
	
jim@4655
   421
	/* make sure that the pointers find themselves inside the windows */
jim@4655
   422
	/* only check if touch->xmax is set ! */
jim@4655
   423
	/*
jim@4655
   424
	  if (x_max && touch->x > x_max) {
jim@4655
   425
	  touch->x = x_max;
jim@4655
   426
	  } else if (touch->x < 0) {
jim@4655
   427
	  touch->x = 0;
jim@4655
   428
	  }
jim@4655
   429
	  
jim@4655
   430
	  if (y_max && touch->y > y_max) {
jim@4655
   431
	  touch->y = y_max;
jim@4655
   432
	  } else if (touch->y < 0) {
jim@4655
   433
	  touch->y = 0;
jim@4655
   434
	  }
jim@4655
   435
	*/
jim@4655
   436
	finger->xdelta = xrel;
jim@4655
   437
	finger->ydelta = yrel;
jim@4655
   438
	finger->pressure = pressure;
jim@4655
   439
	
jim@4655
   440
	
jim@4655
   441
	
jim@4655
   442
	/* Post the event, if desired */
jim@4655
   443
	posted = 0;
jim@4655
   444
	if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
jim@4655
   445
	    SDL_Event event;
jim@4655
   446
	    event.tfinger.type = SDL_FINGERMOTION;
jimtla@4676
   447
	    event.tfinger.touchId = id;
jimtla@4676
   448
	    event.tfinger.fingerId = fingerid;
jim@4655
   449
	    event.tfinger.x = x;
jim@4655
   450
	    event.tfinger.y = y;
jim@4655
   451
	    event.tfinger.pressure = pressure;
jim@4655
   452
	    event.tfinger.state = touch->buttonstate;
jim@4655
   453
	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
jim@4655
   454
	    posted = (SDL_PushEvent(&event) > 0);
jim@4655
   455
	}
jim@4655
   456
	finger->last_x = finger->x;
jim@4655
   457
	finger->last_y = finger->y;
jim@4655
   458
	finger->last_pressure = finger->pressure;
jim@4655
   459
	return posted;
jim@4655
   460
    }
jim@4640
   461
}
jim@4640
   462
int
jimtla@4676
   463
SDL_SendTouchButton(long id, Uint8 state, Uint8 button)
jim@4640
   464
{
jim@4641
   465
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   466
    int posted;
jim@4640
   467
    Uint32 type;
jim@4640
   468
jim@4674
   469
    
jim@4640
   470
    if (!touch) {
jim@4674
   471
      return SDL_TouchNotFoundError(id);
jim@4640
   472
    }
jim@4640
   473
jim@4640
   474
    /* Figure out which event to perform */
jim@4640
   475
    switch (state) {
jim@4640
   476
    case SDL_PRESSED:
jim@4640
   477
        if (touch->buttonstate & SDL_BUTTON(button)) {
jim@4640
   478
            /* Ignore this event, no state change */
jim@4640
   479
            return 0;
jim@4640
   480
        }
jim@4640
   481
        type = SDL_TOUCHBUTTONDOWN;
jim@4640
   482
        touch->buttonstate |= SDL_BUTTON(button);
jim@4640
   483
        break;
jim@4640
   484
    case SDL_RELEASED:
jim@4640
   485
        if (!(touch->buttonstate & SDL_BUTTON(button))) {
jim@4640
   486
            /* Ignore this event, no state change */
jim@4640
   487
            return 0;
jim@4640
   488
        }
jim@4640
   489
        type = SDL_TOUCHBUTTONUP;
jim@4640
   490
        touch->buttonstate &= ~SDL_BUTTON(button);
jim@4640
   491
        break;
jim@4640
   492
    default:
jim@4640
   493
        /* Invalid state -- bail */
jim@4640
   494
        return 0;
jim@4640
   495
    }
jim@4640
   496
jim@4640
   497
    /* Post the event, if desired */
jim@4640
   498
    posted = 0;
jim@4640
   499
    if (SDL_GetEventState(type) == SDL_ENABLE) {
jim@4640
   500
        SDL_Event event;
jim@4640
   501
        event.type = type;
jimtla@4676
   502
        event.tbutton.touchId = touch->id;
jim@4641
   503
        event.tbutton.state = state;
jim@4641
   504
        event.tbutton.button = button;
jim@4641
   505
        event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
jim@4640
   506
        posted = (SDL_PushEvent(&event) > 0);
jim@4640
   507
    }
jim@4640
   508
    return posted;
jim@4640
   509
}
jim@4640
   510
jim@4641
   511
char *
jimtla@4676
   512
SDL_GetTouchName(long id)
jim@4640
   513
{
jim@4641
   514
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   515
    if (!touch) {
jim@4640
   516
        return NULL;
jim@4640
   517
    }
jim@4640
   518
    return touch->name;
jim@4640
   519
}
jim@4640
   520
jimtla@4676
   521
int SDL_TouchNotFoundError(long id) {
jimtla@4676
   522
  printf("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id);
jim@4674
   523
  printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch);
jim@4674
   524
  int i;
jim@4674
   525
  for(i=0;i < SDL_num_touch;i++) {
jimtla@4676
   526
    printf("ERROR: %li\n",SDL_touchPads[i]->id);
jim@4674
   527
  }
jim@4674
   528
  return 0;
jim@4674
   529
}
jim@4640
   530
/* vi: set ts=4 sw=4 expandtab: */