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

Latest commit

 

History

History
575 lines (491 loc) · 13.1 KB

SDL_touch.c

File metadata and controls

575 lines (491 loc) · 13.1 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
265
266
267
268
269
270
271
272
273
274
275
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
323
324
325
326
327
328
329
330
331
332
333
334
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
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;
static int SDL_current_touch = -1;
static SDL_Touch **SDL_touch = NULL;
/* Public functions */
int
SDL_TouchInit(void)
{
return (0);
}
SDL_Touch *
SDL_GetTouch(int index)
{
if (index < 0 || index >= SDL_num_touch) {
return NULL;
}
return SDL_touch[index];
}
static int
SDL_GetTouchIndexId(int id)
{
int index;
SDL_Touch *touch;
for (index = 0; index < SDL_num_touch; ++index) {
touch = SDL_GetTouch(index);
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)
{
SDL_Touch **touch;
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 */
touch = (SDL_Touch **) SDL_realloc(SDL_touch,
(SDL_num_touch + 1) * sizeof(*touch));
if (!touch) {
SDL_OutOfMemory();
return -1;
}
SDL_touch = touch;
index = SDL_num_touch++;
SDL_touch[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touch[index]));
if (!SDL_touch[index]) {
SDL_OutOfMemory();
return -1;
}
*SDL_touch[index] = *touch;
/* we're setting the touch properties */
length = 0;
length = SDL_strlen(name);
SDL_touch[index]->focus = 0;
SDL_touch[index]->name = SDL_malloc((length + 2) * sizeof(char));
SDL_strlcpy(SDL_touch[index]->name, name, length + 1);
SDL_touch[index]->pressure_max = pressure_max;
SDL_touch[index]->pressure_min = pressure_min;
SDL_touch[index]->cursor_shown = SDL_TRUE;
selected_touch = SDL_SelectTouch(index);
SDL_touch[index]->cur_cursor = NULL;
SDL_touch[index]->def_cursor =
/* we're assuming that all touch are in the computer sensing zone */
SDL_touch[index]->proximity = SDL_TRUE;
/* we're assuming that all touch are working in the absolute position mode
thanx to that, the users that don't want to use many touch don't have to
worry about anything */
SDL_touch[index]->relative_mode = SDL_FALSE;
SDL_touch[index]->current_end = 0;
SDL_touch[index]->total_ends = ends;
SDL_SelectTouch(selected_touch);
return index;
}
void
SDL_DelTouch(int index)
{
SDL_Touch *touch = SDL_GetTouch(index);
if (!touch) {
return;
}
touch->def_cursor = NULL;
SDL_free(touch->name);
if (touch->FreeTouch) {
touch->FreeTouch(touch);
}
SDL_free(touch);
SDL_touch[index] = NULL;
}
void
SDL_ResetTouch(int index)
{
SDL_Touch *touch = SDL_GetTouch(index);
if (!touch) {
return;
}
/* FIXME */
}
void
SDL_TouchQuit(void)
{
int i;
for (i = 0; i < SDL_num_touch; ++i) {
SDL_DelTouch(i);
}
SDL_num_touch = 0;
SDL_current_touch = -1;
if (SDL_touch) {
SDL_free(SDL_touch);
SDL_touch = NULL;
}
}
int
SDL_GetNumTouch(void)
{
return SDL_num_touch;
}
int
SDL_SelectTouch(int index)
{
if (index >= 0 && index < SDL_num_touch) {
SDL_current_touch = index;
}
return SDL_current_touch;
}
SDL_Window *
SDL_GetTouchFocusWindow(int index)
{
SDL_Touch *touch = SDL_GetTouch(index);
if (!touch) {
return 0;
}
return touch->focus;
}
static int SDLCALL
FlushTouchMotion(void *param, SDL_Event * event)
{
if (event->type == SDL_TOUCHMOTION
&& event->motion.which == (Uint8) SDL_current_touch) {
return 0;
} else {
return 1;
}
}
int
SDL_SetRelativeTouchMode(int index, SDL_bool enabled)
{
SDL_Touch *touch = SDL_GetTouch(index);
if (!touch) {
return -1;
}
/* Flush pending touch motion */
touch->flush_motion = SDL_TRUE;
SDL_PumpEvents();
touch->flush_motion = SDL_FALSE;
SDL_FilterEvents(FlushTouchMotion, touch);
/* Set the relative mode */
touch->relative_mode = enabled;
if (!enabled) {
/* Restore the expected touch position */
SDL_WarpTouchInWindow(touch->focus, touch->x, touch->y);
}
return 0;
}
SDL_bool
SDL_GetRelativeTouchMode(int index)
{
SDL_Touch *touch = SDL_GetTouch(index);
if (!touch) {
return SDL_FALSE;
}
return touch->relative_mode;
}
Uint8
SDL_GetTouchState(int *x, int *y)
{
SDL_Touch *touch = SDL_GetTouch(SDL_current_touch);
if (!touch) {
if (x) {
*x = 0;
}
if (y) {
*y = 0;
}
return 0;
}
if (x) {
*x = touch->x;
}
if (y) {
*y = touch->y;
}
return touch->buttonstate;
}
Uint8
SDL_GetRelativeTouchState(int index, int *x, int *y)
{
SDL_Touch *touch = SDL_GetTouch(index);
if (!touch) {
if (x) {
*x = 0;
}
if (y) {
*y = 0;
}
return 0;
}
if (x) {
*x = touch->xdelta;
}
if (y) {
*y = touch->ydelta;
}
touch->xdelta = 0;
touch->ydelta = 0;
return touch->buttonstate;
}
void
SDL_SetTouchFocus(int id, SDL_Window * window)
{
int index = SDL_GetTouchIndexId(id);
SDL_Touch *touch = SDL_GetTouch(index);
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) {
check = SDL_GetTouch(i);
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) {
check = SDL_GetTouch(i);
if (check && check->focus == touch->focus) {
focus = SDL_TRUE;
break;
}
}
}
if (!focus) {
SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
}
}
}
int
SDL_SendProximity(int id, int x, int y, int type)
{
int index = SDL_GetTouchIndexId(id);
SDL_Touch *touch = SDL_GetTouch(index);
int posted = 0;
if (!touch) {
return 0;
}
touch->last_x = x;
touch->last_y = y;
if (SDL_GetEventState(type) == SDL_ENABLE) {
SDL_Event event;
event.proximity.which = (Uint8) index;
event.proximity.x = x;
event.proximity.y = y;
event.proximity.cursor = touch->current_end;
event.proximity.type = type;
/* FIXME: is this right? */
event.proximity.windowID = touch->focus ? touch->focus->id : 0;
posted = (SDL_PushEvent(&event) > 0);
if (type == SDL_PROXIMITYIN) {
touch->proximity = SDL_TRUE;
} else {
touch->proximity = SDL_FALSE;
}
}
return posted;
}
int
SDL_SendTouchMotion(int id, int relative, int x, int y, int pressure)
{
int index = SDL_GetTouchIndexId(id);
SDL_Touch *touch = SDL_GetTouch(index);
int posted;
int xrel;
int yrel;
int x_max = 0, y_max = 0;
if (!touch || touch->flush_motion) {
return 0;
}
/* if the touch is out of proximity we don't to want to have any motion from it */
if (touch->proximity == SDL_FALSE) {
touch->last_x = x;
touch->last_y = y;
return 0;
}
/* the relative motion is calculated regarding the system cursor last position */
if (relative) {
xrel = x;
yrel = y;
x = (touch->last_x + x);
y = (touch->last_y + y);
} else {
xrel = x - touch->last_x;
yrel = y - touch->last_y;
}
/* 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 */
if (touch->relative_mode == SDL_FALSE) {
touch->x = x;
touch->y = y;
} else {
touch->x += xrel;
touch->y += yrel;
}
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;
}
touch->xdelta += xrel;
touch->ydelta += yrel;
touch->pressure = pressure;
/* Post the event, if desired */
posted = 0;
if (SDL_GetEventState(SDL_TOUCHMOTION) == SDL_ENABLE &&
touch->proximity == SDL_TRUE) {
SDL_Event event;
event.motion.type = SDL_TOUCHMOTION;
event.motion.which = (Uint8) index;
event.motion.state = touch->buttonstate;
event.motion.x = touch->x;
event.motion.y = touch->y;
event.motion.z = touch->z;
event.motion.pressure = touch->pressure;
event.motion.pressure_max = touch->pressure_max;
event.motion.pressure_min = touch->pressure_min;
event.motion.rotation = 0;
event.motion.tilt_x = 0;
event.motion.tilt_y = 0;
event.motion.cursor = touch->current_end;
event.motion.xrel = xrel;
event.motion.yrel = yrel;
event.motion.windowID = touch->focus ? touch->focus->id : 0;
posted = (SDL_PushEvent(&event) > 0);
}
touch->last_x = touch->x;
touch->last_y = touch->y;
return posted;
}
int
SDL_SendTouchButton(int id, Uint8 state, Uint8 button)
{
int index = SDL_GetTouchIndexId(id);
SDL_Touch *touch = SDL_GetTouch(index);
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;
event.button.which = (Uint8) index;
event.button.state = state;
event.button.button = button;
event.button.x = touch->x;
event.button.y = touch->y;
event.button.windowID = touch->focus ? touch->focus->id : 0;
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
}
int
SDL_SendTouchWheel(int index, int x, int y)
{
SDL_Touch *touch = SDL_GetTouch(index);
int posted;
if (!touch || (!x && !y)) {
return 0;
}
/* Post the event, if desired */
posted = 0;
if (SDL_GetEventState(SDL_TOUCHWHEEL) == SDL_ENABLE) {
SDL_Event event;
event.type = SDL_TOUCHWHEEL;
event.wheel.which = (Uint8) index;
event.wheel.x = x;
event.wheel.y = y;
event.wheel.windowID = touch->focus ? touch->focus->id : 0;
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
}
char *
SDL_GetTouchName(int index)
{
SDL_Touch *touch = SDL_GetTouch(index);
if (!touch) {
return NULL;
}
return touch->name;
}
void
SDL_ChangeEnd(int id, int end)
{
int index = SDL_GetTouchIndexId(id);
SDL_Touch *touch = SDL_GetTouch(index);
if (touch) {
touch->current_end = end;
}
}
/* vi: set ts=4 sw=4 expandtab: */