Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
514 lines (440 loc) · 12.2 KB

SDL_touch.c

File metadata and controls

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