src/events/SDL_touch.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 23 Aug 2010 23:44:28 -0700
changeset 4865 fff50e86c891
parent 4689 f9ab8df6d45a
child 4868 d6adaafcfb10
permissions -rw-r--r--
Update VS2010 project to add new files; update new files so code builds on Win32/Win64
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++)
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@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)
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@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");
jim@4642
   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){
jimtla@4648
   266
		//printf("Making room for it!\n");
jimtla@4648
   267
		fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
jimtla@4648
   268
  						   (touch->num_fingers + 1) * sizeof(SDL_Finger *));
jimtla@4648
   269
		touch->max_fingers = touch->num_fingers+1;
jimtla@4648
   270
		if (!fingers) {
jimtla@4648
   271
			SDL_OutOfMemory();
jimtla@4648
   272
			return -1;
jimtla@4648
   273
		} else {
jimtla@4648
   274
			touch->max_fingers = touch->num_fingers+1;
jimtla@4648
   275
			touch->fingers = fingers;
jimtla@4648
   276
		}
jim@4646
   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, 
jimtla@4678
   313
		   float xin, float yin, float pressurein)
jim@4641
   314
{
jim@4643
   315
    int posted;
aschiffler@4865
   316
	Uint16 x;
aschiffler@4865
   317
	Uint16 y;
aschiffler@4865
   318
	Uint16 pressure;
aschiffler@4865
   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) {
jim@4655
   335
	if(finger == NULL) {
jim@4683
   336
	    SDL_Finger nf;
jim@4655
   337
	    nf.id = fingerid;
jim@4655
   338
	    nf.x = x;
jim@4655
   339
	    nf.y = y;
jim@4655
   340
	    nf.pressure = pressure;
jim@4655
   341
	    nf.xdelta = 0;
jim@4655
   342
	    nf.ydelta = 0;
jim@4655
   343
	    nf.last_x = x;
jim@4655
   344
	    nf.last_y = y;
jim@4655
   345
	    nf.last_pressure = pressure;
jim@4655
   346
	    nf.down = SDL_FALSE;
jim@4683
   347
	    if(SDL_AddFinger(touch,&nf) < 0) return 0;
jim@4683
   348
	    finger = SDL_GetFinger(touch,fingerid);
jim@4655
   349
	}
jim@4655
   350
	else if(finger->down) return 0;
jimtla@4678
   351
	if(xin < touch->x_min || yin < touch->y_min) return 0; //should defer if only a partial input
jim@4641
   352
	posted = 0;
jim@4641
   353
	if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
jim@4641
   354
	    SDL_Event event;
jim@4641
   355
	    event.tfinger.type = SDL_FINGERDOWN;
jimtla@4676
   356
	    event.tfinger.touchId = id;
jim@4642
   357
	    event.tfinger.x = x;
jim@4642
   358
	    event.tfinger.y = y;
jim@4689
   359
	    event.tfinger.pressure = pressure;
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@4655
   365
	if(posted) finger->down = SDL_TRUE;
jim@4641
   366
	return posted;
jim@4641
   367
    }
jim@4641
   368
    else {
jim@4683
   369
        if(finger == NULL) {printf("Finger not found...\n");return 0;}      
jim@4641
   370
	posted = 0;
jim@4641
   371
	if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
jim@4641
   372
	    SDL_Event event;
jim@4641
   373
	    event.tfinger.type = SDL_FINGERUP;
jimtla@4676
   374
	    event.tfinger.touchId =  id;
jim@4641
   375
	    event.tfinger.state = touch->buttonstate;
jim@4641
   376
	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
jim@4643
   377
	    event.tfinger.fingerId = fingerid;
jim@4683
   378
	    //I don't trust the coordinates passed on fingerUp
jim@4683
   379
	    event.tfinger.x = finger->x; 
jim@4683
   380
	    event.tfinger.y = finger->y;
jim@4683
   381
	    event.tfinger.dx = 0;
jim@4683
   382
	    event.tfinger.dy = 0;
jim@4683
   383
jim@4683
   384
	    if(SDL_DelFinger(touch,fingerid) < 0) return 0;
jim@4641
   385
	    posted = (SDL_PushEvent(&event) > 0);
jim@4683
   386
	}	
jim@4641
   387
	return posted;
jim@4641
   388
    }
jim@4641
   389
}
jim@4641
   390
jim@4641
   391
int
jimtla@4678
   392
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative, 
jimtla@4678
   393
		    float xin, float yin, float pressurein)
jim@4640
   394
{
jim@4640
   395
    int index = SDL_GetTouchIndexId(id);
jim@4641
   396
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4641
   397
    SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
jim@4640
   398
    int posted;
jim@4683
   399
    Sint16 xrel, yrel;
jimtla@4676
   400
    float x_max = 0, y_max = 0;
aschiffler@4865
   401
	Uint16 x;
aschiffler@4865
   402
	Uint16 y;
aschiffler@4865
   403
	Uint16 pressure;
jim@4655
   404
    
jim@4674
   405
    if (!touch) {
jim@4674
   406
      return SDL_TouchNotFoundError(id);
jim@4674
   407
    }
jimtla@4678
   408
jimtla@4678
   409
    //scale to Integer coordinates
aschiffler@4865
   410
    x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
aschiffler@4865
   411
    y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
aschiffler@4865
   412
    pressure = (Uint16)((yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
jim@4674
   413
    if(touch->flush_motion) {
jim@4655
   414
	return 0;
jim@4640
   415
    }
jim@4654
   416
    
jim@4655
   417
    if(finger == NULL || !finger->down) {
jim@4683
   418
	return SDL_SendFingerDown(id,fingerid,SDL_TRUE,xin,yin,pressurein);	
jim@4654
   419
    } else {
jim@4655
   420
	/* the relative motion is calculated regarding the last position */
jim@4655
   421
	if (relative) {
jim@4655
   422
	    xrel = x;
jim@4655
   423
	    yrel = y;
jim@4655
   424
	    x = (finger->last_x + x);
jim@4655
   425
	    y = (finger->last_y + y);
jim@4655
   426
	} else {
jimtla@4678
   427
	    if(xin < touch->x_min) x = finger->last_x; /*If movement is only in one axis,*/
jimtla@4678
   428
	    if(yin < touch->y_min) y = finger->last_y; /*The other is marked as -1*/
jimtla@4678
   429
	    if(pressurein < touch->pressure_min) pressure = finger->last_pressure;
jim@4655
   430
	    xrel = x - finger->last_x;
jim@4655
   431
	    yrel = y - finger->last_y;
jim@4683
   432
	    //printf("xrel,yrel (%i,%i)\n",(int)xrel,(int)yrel);
jim@4655
   433
	}
jim@4655
   434
	
jim@4655
   435
	/* Drop events that don't change state */
jim@4655
   436
	if (!xrel && !yrel) {
jim@4654
   437
#if 0
jim@4655
   438
	    printf("Touch event didn't change state - dropped!\n");
jim@4654
   439
#endif
jim@4655
   440
	    return 0;
jim@4642
   441
	}
jim@4655
   442
	
jim@4655
   443
	/* Update internal touch coordinates */
jim@4655
   444
	
jim@4655
   445
	finger->x = x;
jim@4655
   446
	finger->y = y;
jim@4655
   447
	
jim@4655
   448
	/*Should scale to window? Normalize? Maintain Aspect?*/
jim@4655
   449
	//SDL_GetWindowSize(touch->focus, &x_max, &y_max);
jim@4655
   450
	
jim@4655
   451
	/* make sure that the pointers find themselves inside the windows */
jim@4655
   452
	/* only check if touch->xmax is set ! */
jim@4655
   453
	/*
jim@4655
   454
	  if (x_max && touch->x > x_max) {
jim@4655
   455
	  touch->x = x_max;
jim@4655
   456
	  } else if (touch->x < 0) {
jim@4655
   457
	  touch->x = 0;
jim@4655
   458
	  }
jim@4655
   459
	  
jim@4655
   460
	  if (y_max && touch->y > y_max) {
jim@4655
   461
	  touch->y = y_max;
jim@4655
   462
	  } else if (touch->y < 0) {
jim@4655
   463
	  touch->y = 0;
jim@4655
   464
	  }
jim@4655
   465
	*/
jim@4655
   466
	finger->xdelta = xrel;
jim@4655
   467
	finger->ydelta = yrel;
jim@4655
   468
	finger->pressure = pressure;
jim@4655
   469
	
jim@4655
   470
	
jim@4655
   471
	
jim@4655
   472
	/* Post the event, if desired */
jim@4655
   473
	posted = 0;
jim@4655
   474
	if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
jim@4655
   475
	    SDL_Event event;
jim@4655
   476
	    event.tfinger.type = SDL_FINGERMOTION;
jimtla@4676
   477
	    event.tfinger.touchId = id;
jimtla@4676
   478
	    event.tfinger.fingerId = fingerid;
jim@4655
   479
	    event.tfinger.x = x;
jim@4655
   480
	    event.tfinger.y = y;
jim@4683
   481
	    event.tfinger.dx = xrel;
jim@4689
   482
	    event.tfinger.dy = yrel;	    
jimtla@4678
   483
		
jim@4655
   484
	    event.tfinger.pressure = pressure;
jim@4655
   485
	    event.tfinger.state = touch->buttonstate;
jim@4655
   486
	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
jim@4655
   487
	    posted = (SDL_PushEvent(&event) > 0);
jim@4655
   488
	}
jim@4655
   489
	finger->last_x = finger->x;
jim@4655
   490
	finger->last_y = finger->y;
jim@4655
   491
	finger->last_pressure = finger->pressure;
jim@4655
   492
	return posted;
jim@4655
   493
    }
jim@4640
   494
}
jim@4640
   495
int
jimtla@4678
   496
SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button)
jim@4640
   497
{
jim@4641
   498
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   499
    int posted;
jim@4640
   500
    Uint32 type;
jim@4640
   501
jim@4674
   502
    
jim@4640
   503
    if (!touch) {
jim@4674
   504
      return SDL_TouchNotFoundError(id);
jim@4640
   505
    }
jim@4640
   506
jim@4640
   507
    /* Figure out which event to perform */
jim@4640
   508
    switch (state) {
jim@4640
   509
    case SDL_PRESSED:
jim@4640
   510
        if (touch->buttonstate & SDL_BUTTON(button)) {
jim@4640
   511
            /* Ignore this event, no state change */
jim@4640
   512
            return 0;
jim@4640
   513
        }
jim@4640
   514
        type = SDL_TOUCHBUTTONDOWN;
jim@4640
   515
        touch->buttonstate |= SDL_BUTTON(button);
jim@4640
   516
        break;
jim@4640
   517
    case SDL_RELEASED:
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_TOUCHBUTTONUP;
jim@4640
   523
        touch->buttonstate &= ~SDL_BUTTON(button);
jim@4640
   524
        break;
jim@4640
   525
    default:
jim@4640
   526
        /* Invalid state -- bail */
jim@4640
   527
        return 0;
jim@4640
   528
    }
jim@4640
   529
jim@4640
   530
    /* Post the event, if desired */
jim@4640
   531
    posted = 0;
jim@4640
   532
    if (SDL_GetEventState(type) == SDL_ENABLE) {
jim@4640
   533
        SDL_Event event;
jim@4640
   534
        event.type = type;
jimtla@4676
   535
        event.tbutton.touchId = touch->id;
jim@4641
   536
        event.tbutton.state = state;
jim@4641
   537
        event.tbutton.button = button;
jim@4641
   538
        event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
jim@4640
   539
        posted = (SDL_PushEvent(&event) > 0);
jim@4640
   540
    }
jim@4640
   541
    return posted;
jim@4640
   542
}
jim@4640
   543
jim@4641
   544
char *
jimtla@4678
   545
SDL_GetTouchName(SDL_TouchID id)
jim@4640
   546
{
jim@4641
   547
    SDL_Touch *touch = SDL_GetTouch(id);
jim@4640
   548
    if (!touch) {
jim@4640
   549
        return NULL;
jim@4640
   550
    }
jim@4640
   551
    return touch->name;
jim@4640
   552
}
jim@4640
   553
jimtla@4678
   554
int SDL_TouchNotFoundError(SDL_TouchID id) {
aschiffler@4865
   555
  int i;
jimtla@4676
   556
  printf("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id);
jim@4674
   557
  printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch);
jim@4674
   558
  for(i=0;i < SDL_num_touch;i++) {
jimtla@4676
   559
    printf("ERROR: %li\n",SDL_touchPads[i]->id);
jim@4674
   560
  }
jim@4674
   561
  return 0;
jim@4674
   562
}
jim@4640
   563
/* vi: set ts=4 sw=4 expandtab: */