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

Latest commit

 

History

History
463 lines (394 loc) · 10.7 KB

SDL_touch.c

File metadata and controls

463 lines (394 loc) · 10.7 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
30
31
/*
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"
static int SDL_num_touch = 0;
May 27, 2010
May 27, 2010
32
static SDL_Touch **SDL_touchPads = NULL;
33
34
35
36
37
38
39
40
41
/* Public functions */
int
SDL_TouchInit(void)
{
return (0);
}
SDL_Touch *
May 27, 2010
May 27, 2010
42
SDL_GetTouch(int id)
May 27, 2010
May 27, 2010
44
int index = SDL_GetTouchIndexId(id);
45
46
47
if (index < 0 || index >= SDL_num_touch) {
return NULL;
}
May 27, 2010
May 27, 2010
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
return SDL_touchPads[index];
}
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];
}
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 27, 2010
May 27, 2010
70
int
71
72
73
74
75
76
SDL_GetTouchIndexId(int id)
{
int index;
SDL_Touch *touch;
for (index = 0; index < SDL_num_touch; ++index) {
May 27, 2010
May 27, 2010
77
touch = SDL_touchPads[index];
78
79
80
81
82
83
84
85
86
87
88
if (touch->id == id) {
return index;
}
}
return -1;
}
int
SDL_AddTouch(const SDL_Touch * touch, char *name, int pressure_max,
int pressure_min, int ends)
{
May 27, 2010
May 27, 2010
89
SDL_Touch **touchPads;
90
91
92
93
94
95
96
97
98
int selected_touch;
int index;
size_t length;
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
99
touchPads = (SDL_Touch **) SDL_realloc(SDL_touchPads,
100
(SDL_num_touch + 1) * sizeof(*touch));
May 27, 2010
May 27, 2010
101
if (!touchPads) {
102
103
104
105
SDL_OutOfMemory();
return -1;
}
May 27, 2010
May 27, 2010
106
SDL_touchPads = touchPads;
107
108
index = SDL_num_touch++;
May 27, 2010
May 27, 2010
109
110
SDL_touchPads[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchPads[index]));
if (!SDL_touchPads[index]) {
111
112
113
SDL_OutOfMemory();
return -1;
}
May 27, 2010
May 27, 2010
114
*SDL_touchPads[index] = *touch;
115
116
117
118
/* we're setting the touch properties */
length = 0;
length = SDL_strlen(name);
May 27, 2010
May 27, 2010
119
120
121
122
123
124
SDL_touchPads[index]->focus = 0;
SDL_touchPads[index]->name = SDL_malloc((length + 2) * sizeof(char));
SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);
SDL_touchPads[index]->pressure_max = pressure_max;
SDL_touchPads[index]->pressure_min = pressure_min;
125
126
127
128
129
return index;
}
void
May 27, 2010
May 27, 2010
130
SDL_DelTouch(int id)
May 27, 2010
May 27, 2010
132
133
int index = SDL_GetTouchIndexId(id);
SDL_Touch *touch = SDL_GetTouch(id);
134
135
136
137
138
if (!touch) {
return;
}
May 27, 2010
May 27, 2010
139
140
141
142
143
144
145
146
SDL_free(touch->name);
if (touch->FreeTouch) {
touch->FreeTouch(touch);
}
SDL_free(touch);
May 27, 2010
May 27, 2010
147
148
SDL_num_touch--;
SDL_touchPads[index] = SDL_touchPads[SDL_num_touch];
149
150
151
152
153
154
155
}
void
SDL_TouchQuit(void)
{
int i;
May 27, 2010
May 27, 2010
156
for (i = SDL_num_touch-1; i > 0 ; --i) {
157
158
159
160
SDL_DelTouch(i);
}
SDL_num_touch = 0;
May 27, 2010
May 27, 2010
161
162
163
if (SDL_touchPads) {
SDL_free(SDL_touchPads);
SDL_touchPads = NULL;
164
165
166
167
168
169
170
171
172
}
}
int
SDL_GetNumTouch(void)
{
return SDL_num_touch;
}
SDL_Window *
May 27, 2010
May 27, 2010
173
SDL_GetTouchFocusWindow(int id)
May 27, 2010
May 27, 2010
175
SDL_Touch *touch = SDL_GetTouch(id);
176
177
178
179
180
181
182
183
184
185
186
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
187
SDL_Touch *touch = SDL_GetTouch(id);
188
189
190
191
192
193
194
195
196
197
198
199
200
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
201
check = SDL_touchPads[i];
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
220
check = SDL_touchPads[i];
221
222
223
224
225
226
227
228
229
230
231
232
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
int
SDL_AddFinger(SDL_Touch* touch,SDL_Finger* finger)
{
int index;
SDL_Finger **fingers;
size_t length;
if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
SDL_SetError("Finger ID already in use");
}
/* Add the touch to the list of touch */
fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
(touch->num_fingers + 1) * sizeof(*touch));
if (!fingers) {
SDL_OutOfMemory();
return -1;
}
touch->fingers = fingers;
index = SDL_num_touch++;
touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(*(touch->fingers[index])));
if (!touch->fingers[index]) {
SDL_OutOfMemory();
return -1;
}
*touch->fingers[index] = *finger;
return index;
}
May 27, 2010
May 27, 2010
266
SDL_DelFinger(SDL_Touch* touch,int fingerid)
May 27, 2010
May 27, 2010
268
269
int index = SLD_GetFingerIndexId(touch,fingerid);
SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
May 27, 2010
May 27, 2010
271
272
if (!finger) {
return;
May 27, 2010
May 27, 2010
274
May 27, 2010
May 27, 2010
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
SDL_free(finger);
touch->num_fingers--;
touch->fingers[index] = touch->fingers[touch->num_fingers];
}
int
SDL_SendFingerDown(int id, int fingerid, SDL_bool down, int x, int y, int pressure)
{
SDL_Touch* touch = SDL_GetTouch(id);
if(down) {
SDL_Finger nf;
nf.id = id;
nf.x = x;
nf.y = y;
nf.pressure = pressure;
nf.xdelta = 0;
nf.ydelta = 0;
nf.last_x = x;
nf.last_y = y;
SDL_AddFinger(touch,&nf);
posted = 0;
if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
SDL_Event event;
event.tfinger.type = SDL_FINGERDOWN;
event.tfinger.touchId = (Uint8) id;
event.tfinger.state = touch->buttonstate;
event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
event.fingerId = id;
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
}
else {
SDL_DelFinger(touch,id);
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;
event.fingerId = id;
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
May 27, 2010
May 27, 2010
327
328
SDL_SendTouchMotion(int id, int fingerid, int relative,
int x, int y, int pressure)
329
330
{
int index = SDL_GetTouchIndexId(id);
May 27, 2010
May 27, 2010
331
332
SDL_Touch *touch = SDL_GetTouch(id);
SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
333
334
335
336
337
338
339
340
341
342
343
344
345
int posted;
int xrel;
int yrel;
int x_max = 0, y_max = 0;
if (!touch || touch->flush_motion) {
return 0;
}
/* the relative motion is calculated regarding the system cursor last position */
if (relative) {
xrel = x;
yrel = y;
May 27, 2010
May 27, 2010
346
347
x = (finger->last_x + x);
y = (finger->last_y + y);
May 27, 2010
May 27, 2010
349
350
xrel = x - finger->last_x;
yrel = y - finger->last_y;
351
352
353
354
355
356
357
358
359
360
361
362
}
/* Drop events that don't change state */
if (!xrel && !yrel) {
#if 0
printf("Touch event didn't change state - dropped!\n");
#endif
return 0;
}
/* Update internal touch coordinates */
May 27, 2010
May 27, 2010
363
364
365
366
367
finger->x = x;
finger->y = y;
/*Should scale to window? Normalize? Maintain Aspect?*/
//SDL_GetWindowSize(touch->focus, &x_max, &y_max);
368
369
370
/* make sure that the pointers find themselves inside the windows */
/* only check if touch->xmax is set ! */
May 27, 2010
May 27, 2010
371
/*
372
373
374
375
376
377
378
379
380
381
382
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;
}
May 27, 2010
May 27, 2010
383
384
385
386
*/
finger->xdelta += xrel;
finger->ydelta += yrel;
finger->pressure = pressure;
387
388
389
390
391
/* Post the event, if desired */
posted = 0;
May 27, 2010
May 27, 2010
392
if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
May 27, 2010
May 27, 2010
394
395
396
397
event.tfinger.type = SDL_FINGERMOTION;
event.tfinger.which = (Uint8) index;
event.tfinger.state = touch->buttonstate;
event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
398
399
posted = (SDL_PushEvent(&event) > 0);
}
May 27, 2010
May 27, 2010
400
401
finger->last_x = finger->x;
finger->last_y = finger->y;
402
403
404
405
406
407
return posted;
}
int
SDL_SendTouchButton(int id, Uint8 state, Uint8 button)
{
May 27, 2010
May 27, 2010
408
SDL_Touch *touch = SDL_GetTouch(id);
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
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;
May 27, 2010
May 27, 2010
444
445
446
447
event.tbutton.which = (Uint8) index;
event.tbutton.state = state;
event.tbutton.button = button;
event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
448
449
450
451
452
453
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
}
char *
May 27, 2010
May 27, 2010
454
SDL_GetTouchName(int id)
May 27, 2010
May 27, 2010
456
SDL_Touch *touch = SDL_GetTouch(id);
457
458
459
460
461
462
463
if (!touch) {
return NULL;
}
return touch->name;
}
/* vi: set ts=4 sw=4 expandtab: */