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

Latest commit

 

History

History
570 lines (489 loc) · 15.4 KB

SDL_touch.c

File metadata and controls

570 lines (489 loc) · 15.4 KB
 
Apr 8, 2011
Apr 8, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
22
23
24
25
26
27
28
*/
#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
29
30
#include <stdio.h>
31
32
static int SDL_num_touch = 0;
May 27, 2010
May 27, 2010
33
static SDL_Touch **SDL_touchPads = NULL;
34
35
36
37
38
39
/* Public functions */
int
SDL_TouchInit(void)
{
Jul 18, 2010
Jul 18, 2010
40
return (0);
May 28, 2010
May 28, 2010
42
Jul 31, 2010
Jul 31, 2010
44
SDL_GetTouch(SDL_TouchID id)
May 27, 2010
May 27, 2010
46
int index = SDL_GetTouchIndexId(id);
47
48
49
if (index < 0 || index >= SDL_num_touch) {
return NULL;
}
May 27, 2010
May 27, 2010
50
51
52
return SDL_touchPads[index];
}
May 28, 2010
May 28, 2010
53
54
SDL_Touch *
SDL_GetTouchIndex(int index)
May 27, 2010
May 27, 2010
55
{
May 28, 2010
May 28, 2010
56
57
58
59
if (index < 0 || index >= SDL_num_touch) {
return NULL;
}
return SDL_touchPads[index];
May 27, 2010
May 27, 2010
60
61
62
}
int
Jul 31, 2010
Jul 31, 2010
63
SDL_GetFingerIndexId(SDL_Touch* touch,SDL_FingerID fingerid)
May 27, 2010
May 27, 2010
64
65
66
{
int i;
for(i = 0;i < touch->num_fingers;i++)
Dec 1, 2010
Dec 1, 2010
67
68
if(touch->fingers[i]->id == fingerid)
return i;
May 27, 2010
May 27, 2010
69
return -1;
May 28, 2010
May 28, 2010
72
73
SDL_Finger *
Jul 31, 2010
Jul 31, 2010
74
SDL_GetFinger(SDL_Touch* touch,SDL_FingerID id)
May 28, 2010
May 28, 2010
75
76
77
{
int index = SDL_GetFingerIndexId(touch,id);
if(index < 0 || index >= touch->num_fingers)
Dec 1, 2010
Dec 1, 2010
78
return NULL;
May 28, 2010
May 28, 2010
79
80
81
82
return touch->fingers[index];
}
May 27, 2010
May 27, 2010
83
int
Jul 31, 2010
Jul 31, 2010
84
SDL_GetTouchIndexId(SDL_TouchID id)
85
86
87
88
89
{
int index;
SDL_Touch *touch;
for (index = 0; index < SDL_num_touch; ++index) {
May 27, 2010
May 27, 2010
90
touch = SDL_touchPads[index];
91
92
93
94
95
96
97
98
if (touch->id == id) {
return index;
}
}
return -1;
}
int
May 28, 2010
May 28, 2010
99
SDL_AddTouch(const SDL_Touch * touch, char *name)
May 27, 2010
May 27, 2010
101
SDL_Touch **touchPads;
Jan 22, 2011
Jan 22, 2011
102
103
int index;
size_t length;
104
105
106
107
108
109
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
110
touchPads = (SDL_Touch **) SDL_realloc(SDL_touchPads,
111
(SDL_num_touch + 1) * sizeof(*touch));
May 27, 2010
May 27, 2010
112
if (!touchPads) {
113
114
115
116
SDL_OutOfMemory();
return -1;
}
May 27, 2010
May 27, 2010
117
SDL_touchPads = touchPads;
118
119
index = SDL_num_touch++;
May 27, 2010
May 27, 2010
120
121
SDL_touchPads[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchPads[index]));
if (!SDL_touchPads[index]) {
122
123
124
SDL_OutOfMemory();
return -1;
}
Mar 9, 2011
Mar 9, 2011
125
SDL_memcpy(SDL_touchPads[index], touch, sizeof(*touch));
126
127
128
129
/* we're setting the touch properties */
length = 0;
length = SDL_strlen(name);
May 27, 2010
May 27, 2010
130
131
SDL_touchPads[index]->focus = 0;
SDL_touchPads[index]->name = SDL_malloc((length + 2) * sizeof(char));
May 28, 2010
May 28, 2010
132
SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);
May 28, 2010
May 28, 2010
134
SDL_touchPads[index]->num_fingers = 0;
Jun 1, 2010
Jun 1, 2010
135
136
137
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
138
139
140
141
SDL_touchPads[index]->buttonstate = 0;
SDL_touchPads[index]->relative_mode = SDL_FALSE;
SDL_touchPads[index]->flush_motion = SDL_FALSE;
Jul 31, 2010
Jul 31, 2010
142
143
SDL_touchPads[index]->xres = (1<<(16-1));
SDL_touchPads[index]->yres = (1<<(16-1));
Jul 7, 2010
Jul 7, 2010
144
145
146
//Do I want this here? Probably
SDL_GestureAddTouch(SDL_touchPads[index]);
147
148
149
150
return index;
}
void
Jul 31, 2010
Jul 31, 2010
151
SDL_DelTouch(SDL_TouchID id)
May 27, 2010
May 27, 2010
153
154
int index = SDL_GetTouchIndexId(id);
SDL_Touch *touch = SDL_GetTouch(id);
155
156
157
158
159
if (!touch) {
return;
}
May 27, 2010
May 27, 2010
160
161
162
163
164
165
166
167
SDL_free(touch->name);
if (touch->FreeTouch) {
touch->FreeTouch(touch);
}
SDL_free(touch);
May 27, 2010
May 27, 2010
168
169
SDL_num_touch--;
SDL_touchPads[index] = SDL_touchPads[SDL_num_touch];
170
171
172
173
174
175
176
}
void
SDL_TouchQuit(void)
{
int i;
May 27, 2010
May 27, 2010
177
for (i = SDL_num_touch-1; i > 0 ; --i) {
178
179
180
181
SDL_DelTouch(i);
}
SDL_num_touch = 0;
May 27, 2010
May 27, 2010
182
183
184
if (SDL_touchPads) {
SDL_free(SDL_touchPads);
SDL_touchPads = NULL;
185
186
187
188
189
190
191
192
193
}
}
int
SDL_GetNumTouch(void)
{
return SDL_num_touch;
}
SDL_Window *
Jul 31, 2010
Jul 31, 2010
194
SDL_GetTouchFocusWindow(SDL_TouchID id)
May 27, 2010
May 27, 2010
196
SDL_Touch *touch = SDL_GetTouch(id);
197
198
199
200
201
202
203
204
if (!touch) {
return 0;
}
return touch->focus;
}
void
Jul 31, 2010
Jul 31, 2010
205
SDL_SetTouchFocus(SDL_TouchID id, SDL_Window * window)
206
207
{
int index = SDL_GetTouchIndexId(id);
May 27, 2010
May 27, 2010
208
SDL_Touch *touch = SDL_GetTouch(id);
209
210
211
212
213
214
215
216
217
218
219
220
221
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
222
check = SDL_touchPads[i];
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
241
check = SDL_touchPads[i];
242
243
244
245
246
247
248
249
250
251
252
253
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
254
int
Jul 31, 2010
Jul 31, 2010
255
SDL_AddFinger(SDL_Touch* touch,SDL_Finger *finger)
May 27, 2010
May 27, 2010
256
257
258
{
int index;
SDL_Finger **fingers;
Jun 1, 2010
Jun 1, 2010
259
//printf("Adding Finger...\n");
Jul 31, 2010
Jul 31, 2010
260
if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
May 27, 2010
May 27, 2010
261
SDL_SetError("Finger ID already in use");
Dec 1, 2010
Dec 1, 2010
262
}
May 27, 2010
May 27, 2010
263
264
/* Add the touch to the list of touch */
Jun 1, 2010
Jun 1, 2010
265
if(touch->num_fingers >= touch->max_fingers){
Dec 1, 2010
Dec 1, 2010
266
267
268
269
270
271
272
273
274
275
276
277
//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;
}
}
May 27, 2010
May 27, 2010
278
May 31, 2010
May 31, 2010
279
index = touch->num_fingers;
Jun 1, 2010
Jun 1, 2010
280
281
282
//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
283
284
285
286
if (!touch->fingers[index]) {
SDL_OutOfMemory();
return -1;
}
Jul 31, 2010
Jul 31, 2010
287
*(touch->fingers[index]) = *finger;
Jun 1, 2010
Jun 1, 2010
288
touch->num_fingers++;
May 27, 2010
May 27, 2010
289
290
291
292
return index;
}
Jul 31, 2010
Jul 31, 2010
294
SDL_DelFinger(SDL_Touch* touch,SDL_FingerID fingerid)
May 28, 2010
May 28, 2010
296
int index = SDL_GetFingerIndexId(touch,fingerid);
May 27, 2010
May 27, 2010
297
SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
May 27, 2010
May 27, 2010
299
if (!finger) {
Jun 3, 2010
Jun 3, 2010
300
return -1;
May 27, 2010
May 27, 2010
302
May 27, 2010
May 27, 2010
304
305
306
SDL_free(finger);
touch->num_fingers--;
touch->fingers[index] = touch->fingers[touch->num_fingers];
Jun 18, 2010
Jun 18, 2010
307
return 0;
May 27, 2010
May 27, 2010
308
309
310
311
}
int
Jul 31, 2010
Jul 31, 2010
312
SDL_SendFingerDown(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down,
Dec 1, 2010
Dec 1, 2010
313
float xin, float yin, float pressurein)
May 27, 2010
May 27, 2010
314
{
May 29, 2010
May 29, 2010
315
int posted;
Dec 1, 2010
Dec 1, 2010
316
317
318
319
Uint16 x;
Uint16 y;
Uint16 pressure;
SDL_Finger *finger;
Aug 24, 2010
Aug 24, 2010
320
May 27, 2010
May 27, 2010
321
SDL_Touch* touch = SDL_GetTouch(id);
May 29, 2010
May 29, 2010
322
Jul 29, 2010
Jul 29, 2010
323
324
325
if(!touch) {
return SDL_TouchNotFoundError(id);
}
Aug 5, 2010
Aug 5, 2010
326
327
Jul 31, 2010
Jul 31, 2010
328
//scale to Integer coordinates
Aug 24, 2010
Aug 24, 2010
329
330
331
x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
pressure = (Uint16)((yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
Aug 5, 2010
Aug 5, 2010
332
Aug 24, 2010
Aug 24, 2010
333
finger = SDL_GetFinger(touch,fingerid);
May 27, 2010
May 27, 2010
334
if(down) {
Dec 1, 2010
Dec 1, 2010
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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;
if(SDL_AddFinger(touch,&nf) < 0) return 0;
finger = SDL_GetFinger(touch,fingerid);
}
else if(finger->down) return 0;
if(xin < touch->x_min || yin < touch->y_min) return 0; //should defer if only a partial input
posted = 0;
if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
SDL_Event event;
event.tfinger.type = SDL_FINGERDOWN;
event.tfinger.touchId = id;
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;
event.tfinger.fingerId = fingerid;
posted = (SDL_PushEvent(&event) > 0);
}
if(posted) finger->down = SDL_TRUE;
return posted;
May 27, 2010
May 27, 2010
367
368
}
else {
Aug 29, 2010
Aug 29, 2010
369
370
371
372
if(finger == NULL) {
SDL_SetError("Finger not found.");
return 0;
}
Dec 1, 2010
Dec 1, 2010
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
posted = 0;
if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
SDL_Event event;
event.tfinger.type = SDL_FINGERUP;
event.tfinger.touchId = id;
event.tfinger.state = touch->buttonstate;
event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
event.tfinger.fingerId = fingerid;
//I don't trust the coordinates passed on fingerUp
event.tfinger.x = finger->x;
event.tfinger.y = finger->y;
event.tfinger.dx = 0;
event.tfinger.dy = 0;
if(SDL_DelFinger(touch,fingerid) < 0) return 0;
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
Jul 31, 2010
Jul 31, 2010
395
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative,
Dec 1, 2010
Dec 1, 2010
396
float xin, float yin, float pressurein)
Jan 28, 2011
Jan 28, 2011
398
399
SDL_Touch *touch;
SDL_Finger *finger;
Aug 5, 2010
Aug 5, 2010
401
Sint16 xrel, yrel;
Jan 28, 2011
Jan 28, 2011
402
403
404
Uint16 x;
Uint16 y;
Uint16 pressure;
Jun 18, 2010
Jun 18, 2010
405
Jan 28, 2011
Jan 28, 2011
406
touch = SDL_GetTouch(id);
Jul 29, 2010
Jul 29, 2010
407
408
409
if (!touch) {
return SDL_TouchNotFoundError(id);
}
Jul 31, 2010
Jul 31, 2010
410
411
//scale to Integer coordinates
Aug 24, 2010
Aug 24, 2010
412
413
414
x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres));
y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres));
pressure = (Uint16)((yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
Jul 29, 2010
Jul 29, 2010
415
if(touch->flush_motion) {
Dec 1, 2010
Dec 1, 2010
416
return 0;
Jun 17, 2010
Jun 17, 2010
417
418
}
Jan 28, 2011
Jan 28, 2011
419
finger = SDL_GetFinger(touch,fingerid);
Jun 18, 2010
Jun 18, 2010
420
if(finger == NULL || !finger->down) {
Dec 1, 2010
Dec 1, 2010
421
return SDL_SendFingerDown(id,fingerid,SDL_TRUE,xin,yin,pressurein);
Jun 17, 2010
Jun 17, 2010
422
} else {
Dec 1, 2010
Dec 1, 2010
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* 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(xin < touch->x_min) x = finger->last_x; /*If movement is only in one axis,*/
if(yin < touch->y_min) y = finger->last_y; /*The other is marked as -1*/
if(pressurein < touch->pressure_min) pressure = finger->last_pressure;
xrel = x - finger->last_x;
yrel = y - finger->last_y;
//printf("xrel,yrel (%i,%i)\n",(int)xrel,(int)yrel);
}
/* Drop events that don't change state */
if (!xrel && !yrel) {
Jun 17, 2010
Jun 17, 2010
440
#if 0
Dec 1, 2010
Dec 1, 2010
441
printf("Touch event didn't change state - dropped!\n");
Jun 17, 2010
Jun 17, 2010
442
#endif
Dec 1, 2010
Dec 1, 2010
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
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
495
return 0;
}
/* 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 = id;
event.tfinger.fingerId = fingerid;
event.tfinger.x = x;
event.tfinger.y = y;
event.tfinger.dx = xrel;
event.tfinger.dy = yrel;
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;
Jun 18, 2010
Jun 18, 2010
496
}
Jan 28, 2011
Jan 28, 2011
498
Jul 31, 2010
Jul 31, 2010
500
SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button)
Jan 28, 2011
Jan 28, 2011
502
SDL_Touch *touch;
503
504
505
int posted;
Uint32 type;
Jul 29, 2010
Jul 29, 2010
506
Jan 28, 2011
Jan 28, 2011
507
touch = SDL_GetTouch(id);
Jul 29, 2010
Jul 29, 2010
509
return SDL_TouchNotFoundError(id);
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
}
/* 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;
Jul 30, 2010
Jul 30, 2010
540
event.tbutton.touchId = touch->id;
May 27, 2010
May 27, 2010
541
542
543
event.tbutton.state = state;
event.tbutton.button = button;
event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
544
545
546
547
548
549
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
}
char *
Jul 31, 2010
Jul 31, 2010
550
SDL_GetTouchName(SDL_TouchID id)
May 27, 2010
May 27, 2010
552
SDL_Touch *touch = SDL_GetTouch(id);
553
554
555
556
557
558
if (!touch) {
return NULL;
}
return touch->name;
}
Jul 31, 2010
Jul 31, 2010
559
int SDL_TouchNotFoundError(SDL_TouchID id) {
Aug 29, 2010
Aug 29, 2010
560
561
562
//int i;
SDL_SetError("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id);
#if 0
Jul 29, 2010
Jul 29, 2010
563
564
printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch);
for(i=0;i < SDL_num_touch;i++) {
Jul 30, 2010
Jul 30, 2010
565
printf("ERROR: %li\n",SDL_touchPads[i]->id);
Jul 29, 2010
Jul 29, 2010
566
}
Aug 29, 2010
Aug 29, 2010
567
#endif
Jul 29, 2010
Jul 29, 2010
568
569
return 0;
}
570
/* vi: set ts=4 sw=4 expandtab: */