This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_touchscreen.c
482 lines (425 loc) · 12.7 KB
/
SDL_touchscreen.c
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
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 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"
/* This is the touchscreen API for Simple DirectMedia Layer */
#include "SDL_events.h"
#include "SDL_systouchscreen.h"
#include "SDL_touchscreen_c.h"
#if !SDL_EVENTS_DISABLED
#include "../events/SDL_events_c.h"
#endif
/* This is used for Quake III Arena */
#if SDL_EVENTS_DISABLED
#define SDL_Lock_EventThread()
#define SDL_Unlock_EventThread()
#endif
Uint8 SDL_numtouchscreens = 0;
SDL_Touchscreen **SDL_touchscreens = NULL;
static SDL_Touchscreen *default_touchscreen = NULL;
int
SDL_TouchscreenInit(void)
{
int arraylen;
int status;
SDL_numtouchscreens = 0;
status = SDL_SYS_TouchscreenInit();
if (status >= 0) {
arraylen = (status + 1) * sizeof(*SDL_touchscreens);
SDL_touchscreens = (SDL_Touchscreen **) SDL_malloc(arraylen);
if (SDL_touchscreens == NULL) {
SDL_numtouchscreens = 0;
} else {
SDL_memset(SDL_touchscreens, 0, arraylen);
SDL_numtouchscreens = status;
}
status = 0;
}
default_touchscreen = NULL;
return (status);
}
/*
* Count the number of touchscreens attached to the system
*/
int
SDL_NumTouchscreens(void)
{
return SDL_numtouchscreens;
}
/*
* Get the implementation dependent name of a touchscreen
*/
const char *
SDL_TouchscreenName(int device_index)
{
if ((device_index < 0) || (device_index >= SDL_numtouchscreens)) {
SDL_SetError("There are %d touchscreens available", SDL_numtouchscreens);
return (NULL);
}
return (SDL_SYS_TouchscreenName(device_index));
}
/*
* Open a touchscreen for use - the index passed as an argument refers to
* the N'th touchscreen on the system. This index is the value which will
* identify this touchscreen in future touchscreen events.
*
* This function returns a touchscreen identifier, or NULL if an error occurred.
*/
SDL_Touchscreen *
SDL_TouchscreenOpen(int device_index)
{
int i;
SDL_Touchscreen *touchscreen;
if ((device_index < 0) || (device_index >= SDL_numtouchscreens)) {
SDL_SetError("There are %d touchscreens available", SDL_numtouchscreens);
return (NULL);
}
/* If the touchscreen is already open, return it */
for (i = 0; SDL_touchscreens[i]; ++i) {
if (device_index == SDL_touchscreens[i]->index) {
touchscreen = SDL_touchscreens[i];
++touchscreen->ref_count;
return (touchscreen);
}
}
/* Create and initialize the touchscreen */
touchscreen = (SDL_Touchscreen *) SDL_malloc((sizeof *touchscreen));
if (touchscreen != NULL) {
SDL_memset(touchscreen, 0, (sizeof *touchscreen));
touchscreen->index = device_index;
if (SDL_SYS_TouchscreenOpen(touchscreen) < 0) {
SDL_free(touchscreen);
touchscreen = NULL;
} else {
if (touchscreen->maxpoints > 0) {
touchscreen->points = (struct touchpoint *) SDL_malloc
(touchscreen->maxpoints * sizeof(*touchscreen->points));
}
if ((touchscreen->maxpoints > 0) && !touchscreen->points) {
SDL_OutOfMemory();
SDL_TouchscreenClose(touchscreen);
touchscreen = NULL;
}
if (touchscreen->points) {
SDL_memset(touchscreen->points, 0,
touchscreen->maxpoints * sizeof(*touchscreen->points));
}
}
}
if (touchscreen) {
/* Add touchscreen to list */
++touchscreen->ref_count;
SDL_Lock_EventThread();
for (i = 0; SDL_touchscreens[i]; ++i)
/* Skip to next touchscreen */ ;
SDL_touchscreens[i] = touchscreen;
SDL_Unlock_EventThread();
}
return (touchscreen);
}
/*
* Returns 1 if the touchscreen has been opened, or 0 if it has not.
*/
int
SDL_TouchscreenOpened(int device_index)
{
int i, opened;
opened = 0;
for (i = 0; SDL_touchscreens[i]; ++i) {
if (SDL_touchscreens[i]->index == (Uint8) device_index) {
opened = 1;
break;
}
}
return (opened);
}
static int
ValidTouchscreen(SDL_Touchscreen ** touchscreen)
{
int valid;
if (*touchscreen == NULL) {
*touchscreen = default_touchscreen;
}
if (*touchscreen == NULL) {
SDL_SetError("Touchscreen hasn't been opened yet");
valid = 0;
} else {
valid = 1;
}
return valid;
}
/*
* Get the device index of an opened touchscreen.
*/
int
SDL_TouchscreenIndex(SDL_Touchscreen * touchscreen)
{
if (!ValidTouchscreen(&touchscreen)) {
return (-1);
}
return (touchscreen->index);
}
/*
* Get the max number of points on a multi-touch screen (or 1 on a single-touch)
*/
int
SDL_TouchscreenMaxPoints(SDL_Touchscreen * touchscreen)
{
if (!ValidTouchscreen(&touchscreen)) {
return (-1);
}
return (touchscreen->maxpoints);
}
/*
* Get the current X,Y position of the indicated point on the touchscreen
*/
int
SDL_TouchscreenGetXY(SDL_Touchscreen *touchscreen, int point, int *x, int *y)
{
int retval;
if (!ValidTouchscreen(&touchscreen)) {
return (-1);
}
retval = 0;
if (point < 0) {
int i; long avg;
if(x) {
avg = 0;
for(i = 0; i < touchscreen->npoints; ++i) {
avg += touchscreen->points[i].x;
}
*x = avg;
}
if(y) {
avg = 0;
for(i = 0; i < touchscreen->npoints; ++i) {
avg += touchscreen->points[i].y;
}
*y = avg;
}
avg = 0;
for(i = 0; i < touchscreen->npoints; ++i) {
avg += touchscreen->points[i].pressure;
}
return (int)avg;
} else if (point < touchscreen->maxpoints) {
if (x) {
*x = touchscreen->points[point].x;
}
if (y) {
*y = touchscreen->points[point].y;
}
retval = touchscreen->points[point].pressure;
} else {
SDL_SetError("Touchscreen only can have %d points", touchscreen->maxpoints);
retval = -1;
}
return (retval);
}
int
SDL_TouchscreenGetPoints(SDL_Touchscreen *touchscreen) {
if (!ValidTouchscreen(&touchscreen)) {
return (-1);
}
return touchscreen->npoints;
}
/*
* Close a touchscreen previously opened with SDL_TouchscreenOpen()
*/
void
SDL_TouchscreenClose(SDL_Touchscreen * touchscreen)
{
int i;
if (!ValidTouchscreen(&touchscreen)) {
return;
}
/* First decrement ref count */
if (--touchscreen->ref_count > 0) {
return;
}
/* Lock the event queue - prevent touchscreen polling */
SDL_Lock_EventThread();
if (touchscreen == default_touchscreen) {
default_touchscreen = NULL;
}
SDL_SYS_TouchscreenClose(touchscreen);
/* Remove touchscreen from list */
for (i = 0; SDL_touchscreens[i]; ++i) {
if (touchscreen == SDL_touchscreens[i]) {
SDL_memcpy(&SDL_touchscreens[i], &SDL_touchscreens[i + 1],
(SDL_numtouchscreens - i) * sizeof(touchscreen));
break;
}
}
/* Let the event thread keep running */
SDL_Unlock_EventThread();
/* Free the data associated with this touchscreen */
if (touchscreen->points) {
SDL_free(touchscreen->points);
}
SDL_free(touchscreen);
}
void
SDL_TouchscreenQuit(void)
{
/* Stop the event polling */
SDL_Lock_EventThread();
SDL_numtouchscreens = 0;
SDL_Unlock_EventThread();
/* Quit the touchscreen setup */
SDL_SYS_TouchscreenQuit();
if (SDL_touchscreens) {
SDL_free(SDL_touchscreens);
SDL_touchscreens = NULL;
}
}
/* These are global for SDL_systouchscreen.c and SDL_events.c */
int
SDL_PrivateTouchPress(SDL_Touchscreen * touchscreen, int point,
int x, int y, int pressure) {
int posted;
int ev = SDL_TOUCHMOTION;
if (!ValidTouchscreen(&touchscreen)) {
return -1;
}
if(point >= touchscreen->maxpoints) {
SDL_SetError("Touchscreen only can have %d points", touchscreen->maxpoints);
return -1;
}
/* on neg. point, set the given args as the *only* point.
so set the struct to have no points pressed, then continue as normal. */
if(point < 0) {
point = 0;
touchscreen->npoints = 0;
SDL_memset(touchscreen->points, 0,
touchscreen->maxpoints * sizeof(touchscreen->points[0]));
}
/* new touch point! that means a TOUCHPRESSED event is due. */
if(point >= touchscreen->npoints) {
ev = SDL_TOUCHPRESSED;
point = touchscreen->npoints;
++touchscreen->npoints;
}
/* no motion, no change, don't report an event. */
if(touchscreen->points[point].x == x &&
touchscreen->points[point].y == y &&
touchscreen->points[point].pressure == pressure) {
return 0;
}
/* Update internal touchscreen point state */
touchscreen->points[point].x = x;
touchscreen->points[point].y = y;
touchscreen->points[point].pressure = pressure;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
if (SDL_ProcessEvents[ev] == SDL_ENABLE) {
SDL_Event event;
event.touch.type = ev;
event.touch.which = touchscreen->index;
event.touch.point = point;
event.touch.xpos = x;
event.touch.ypos = y;
event.touch.pressure = pressure;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
posted = 1;
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
return (posted);
}
int
SDL_PrivateTouchRelease(SDL_Touchscreen * touchscreen, int point) {
int posted;
if (!ValidTouchscreen(&touchscreen)) {
return -1;
}
if(point >= touchscreen->maxpoints) {
SDL_SetError("Touchscreen only can have %d points", touchscreen->maxpoints);
return -1;
} else if(point >= touchscreen->npoints) {
SDL_SetError("Point %d up when only %d were down", point, touchscreen->npoints);
return -1;
}
/* on neg. point, clear all points. so set the struct to have one point
pressed, then continue as normal. */
if(point < 0) {
point = 0;
touchscreen->npoints = 1;
SDL_memset(&(touchscreen->points[1]), 0,
(touchscreen->maxpoints-1) * sizeof(touchscreen->points[0]));
}
/* Update internal touchscreen point state */
touchscreen->points[point].pressure = 0;
if(touchscreen->npoints >= 0) --touchscreen->npoints;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
if (SDL_ProcessEvents[SDL_TOUCHRELEASED] == SDL_ENABLE) {
SDL_Event event;
event.touch.type = SDL_TOUCHRELEASED;
event.touch.which = touchscreen->index;
event.touch.point = point;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
posted = 1;
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
return (posted);
}
void
SDL_TouchscreenUpdate(void)
{
int i;
for (i = 0; SDL_touchscreens[i]; ++i) {
SDL_SYS_TouchscreenUpdate(SDL_touchscreens[i]);
}
}
int
SDL_TouchscreenEventState(int state)
{
#if SDL_EVENTS_DISABLED
return SDL_IGNORE;
#else
const Uint8 event_list[] = {
SDL_TOUCHPRESSED, SDL_TOUCHRELEASED, SDL_TOUCHMOTION
};
unsigned int i;
switch (state) {
case SDL_QUERY:
state = SDL_IGNORE;
for (i = 0; i < SDL_arraysize(event_list); ++i) {
state = SDL_EventState(event_list[i], SDL_QUERY);
if (state == SDL_ENABLE) {
break;
}
}
break;
default:
for (i = 0; i < SDL_arraysize(event_list); ++i) {
SDL_EventState(event_list[i], state);
}
break;
}
return (state);
#endif /* SDL_EVENTS_DISABLED */
}
/* vi: set ts=4 sw=4 expandtab: */