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

Latest commit

 

History

History
364 lines (310 loc) · 8.89 KB

SDL_touch.c

File metadata and controls

364 lines (310 loc) · 8.89 KB
 
1
2
/*
Simple DirectMedia Layer
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.
*/
#include "SDL_config.h"
/* General touch handling code for SDL */
Mar 3, 2013
Mar 3, 2013
25
#include "SDL_assert.h"
26
27
28
29
30
#include "SDL_events.h"
#include "SDL_events_c.h"
static int SDL_num_touch = 0;
Mar 3, 2013
Mar 3, 2013
31
static SDL_Touch **SDL_touchDevices = NULL;
32
33
34
35
36
37
/* Public functions */
int
SDL_TouchInit(void)
{
Mar 3, 2013
Mar 3, 2013
38
return (0);
Mar 3, 2013
Mar 3, 2013
41
int
May 12, 2013
May 12, 2013
42
SDL_GetNumTouchDevices(void)
Mar 3, 2013
Mar 3, 2013
43
44
45
46
47
48
{
return SDL_num_touch;
}
SDL_TouchID
SDL_GetTouchDevice(int index)
49
50
{
if (index < 0 || index >= SDL_num_touch) {
Mar 3, 2013
Mar 3, 2013
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
SDL_SetError("Unknown touch device");
return 0;
}
return SDL_touchDevices[index]->id;
}
static int
SDL_GetTouchIndex(SDL_TouchID id)
{
int index;
SDL_Touch *touch;
for (index = 0; index < SDL_num_touch; ++index) {
touch = SDL_touchDevices[index];
if (touch->id == id) {
return index;
}
Mar 3, 2013
Mar 3, 2013
69
return -1;
Mar 3, 2013
Mar 3, 2013
73
SDL_GetTouch(SDL_TouchID id)
Mar 3, 2013
Mar 3, 2013
75
int index = SDL_GetTouchIndex(id);
76
if (index < 0 || index >= SDL_num_touch) {
Mar 3, 2013
Mar 3, 2013
77
SDL_SetError("Unknown touch device");
Mar 3, 2013
Mar 3, 2013
80
return SDL_touchDevices[index];
Mar 3, 2013
Mar 3, 2013
84
SDL_GetFingerIndex(const SDL_Touch * touch, SDL_FingerID fingerid)
Mar 3, 2013
Mar 3, 2013
86
87
88
89
90
91
int index;
for (index = 0; index < touch->num_fingers; ++index) {
if (touch->fingers[index]->id == fingerid) {
return index;
}
}
92
93
94
95
return -1;
}
SDL_Finger *
Mar 3, 2013
Mar 3, 2013
96
SDL_GetFinger(const SDL_Touch * touch, SDL_FingerID id)
Mar 3, 2013
Mar 3, 2013
98
99
int index = SDL_GetFingerIndex(touch, id);
if (index < 0 || index >= touch->num_fingers) {
Mar 3, 2013
Mar 3, 2013
101
}
102
103
104
105
return touch->fingers[index];
}
int
Mar 3, 2013
Mar 3, 2013
106
SDL_GetNumTouchFingers(SDL_TouchID touchID)
Mar 3, 2013
Mar 3, 2013
108
109
110
111
112
113
SDL_Touch *touch = SDL_GetTouch(touchID);
if (touch) {
return touch->num_fingers;
}
return 0;
}
Mar 3, 2013
Mar 3, 2013
115
116
117
118
119
120
SDL_Finger *
SDL_GetTouchFinger(SDL_TouchID touchID, int index)
{
SDL_Touch *touch = SDL_GetTouch(touchID);
if (!touch) {
return NULL;
Mar 3, 2013
Mar 3, 2013
122
123
124
125
126
if (index < 0 || index >= touch->num_fingers) {
SDL_SetError("Unknown touch finger");
return NULL;
}
return touch->fingers[index];
Mar 3, 2013
Mar 3, 2013
130
SDL_AddTouch(SDL_TouchID touchID, const char *name)
Mar 3, 2013
Mar 3, 2013
132
SDL_Touch **touchDevices;
Mar 3, 2013
Mar 3, 2013
135
136
137
index = SDL_GetTouchIndex(touchID);
if (index >= 0) {
return index;
138
139
140
}
/* Add the touch to the list of touch */
Mar 3, 2013
Mar 3, 2013
141
142
143
touchDevices = (SDL_Touch **) SDL_realloc(SDL_touchDevices,
(SDL_num_touch + 1) * sizeof(*touchDevices));
if (!touchDevices) {
Mar 31, 2013
Mar 31, 2013
144
return SDL_OutOfMemory();
Mar 3, 2013
Mar 3, 2013
147
SDL_touchDevices = touchDevices;
148
149
index = SDL_num_touch++;
Mar 3, 2013
Mar 3, 2013
150
151
SDL_touchDevices[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchDevices[index]));
if (!SDL_touchDevices[index]) {
Mar 31, 2013
Mar 31, 2013
152
return SDL_OutOfMemory();
153
154
155
}
/* we're setting the touch properties */
Mar 3, 2013
Mar 3, 2013
156
157
158
159
SDL_touchDevices[index]->id = touchID;
SDL_touchDevices[index]->num_fingers = 0;
SDL_touchDevices[index]->max_fingers = 0;
SDL_touchDevices[index]->fingers = NULL;
Mar 3, 2013
Mar 3, 2013
161
162
163
/* Record this touch device for gestures */
/* We could do this on the fly in the gesture code if we wanted */
SDL_GestureAddTouch(touchID);
Mar 3, 2013
Mar 3, 2013
165
return index;
Mar 3, 2013
Mar 3, 2013
168
169
static int
SDL_AddFinger(SDL_Touch *touch, SDL_FingerID fingerid, float x, float y, float pressure)
Mar 3, 2013
Mar 3, 2013
171
SDL_Finger *finger;
Mar 3, 2013
Mar 3, 2013
173
174
175
176
if (touch->num_fingers == touch->max_fingers) {
SDL_Finger **new_fingers;
new_fingers = (SDL_Finger **)SDL_realloc(touch->fingers, (touch->max_fingers+1)*sizeof(*touch->fingers));
if (!new_fingers) {
Mar 31, 2013
Mar 31, 2013
177
return SDL_OutOfMemory();
Mar 3, 2013
Mar 3, 2013
179
180
181
touch->fingers = new_fingers;
touch->fingers[touch->max_fingers] = (SDL_Finger *)SDL_malloc(sizeof(*finger));
if (!touch->fingers[touch->max_fingers]) {
Mar 31, 2013
Mar 31, 2013
182
return SDL_OutOfMemory();
Mar 3, 2013
Mar 3, 2013
184
touch->max_fingers++;
Mar 3, 2013
Mar 3, 2013
187
188
189
190
191
192
finger = touch->fingers[touch->num_fingers++];
finger->id = fingerid;
finger->x = x;
finger->y = y;
finger->pressure = pressure;
return 0;
Mar 3, 2013
Mar 3, 2013
195
196
static int
SDL_DelFinger(SDL_Touch* touch, SDL_FingerID fingerid)
Mar 3, 2013
Mar 3, 2013
198
SDL_Finger *temp;
Mar 3, 2013
Mar 3, 2013
200
201
int index = SDL_GetFingerIndex(touch, fingerid);
if (index < 0) {
202
203
204
205
return -1;
}
touch->num_fingers--;
Mar 3, 2013
Mar 3, 2013
206
temp = touch->fingers[index];
207
touch->fingers[index] = touch->fingers[touch->num_fingers];
Mar 3, 2013
Mar 3, 2013
208
touch->fingers[touch->num_fingers] = temp;
209
210
211
212
return 0;
}
int
Mar 3, 2013
Mar 3, 2013
213
214
SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
SDL_bool down, float x, float y, float pressure)
Mar 3, 2013
Mar 3, 2013
217
SDL_Finger *finger;
218
219
SDL_Touch* touch = SDL_GetTouch(id);
Mar 3, 2013
Mar 3, 2013
220
221
if (!touch) {
return -1;
Mar 3, 2013
Mar 3, 2013
224
225
226
227
228
229
230
231
232
finger = SDL_GetFinger(touch, fingerid);
if (down) {
if (finger) {
/* This finger is already down */
return 0;
}
if (SDL_AddFinger(touch, fingerid, x, y, pressure) < 0) {
return 0;
Mar 3, 2013
Mar 3, 2013
234
235
236
237
238
239
posted = 0;
if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
SDL_Event event;
event.tfinger.type = SDL_FINGERDOWN;
event.tfinger.touchId = id;
Mar 3, 2013
Mar 3, 2013
240
event.tfinger.fingerId = fingerid;
241
242
243
244
245
246
247
event.tfinger.x = x;
event.tfinger.y = y;
event.tfinger.dx = 0;
event.tfinger.dy = 0;
event.tfinger.pressure = pressure;
posted = (SDL_PushEvent(&event) > 0);
}
Mar 3, 2013
Mar 3, 2013
248
249
250
} else {
if (!finger) {
/* This finger is already up */
Mar 3, 2013
Mar 3, 2013
252
253
}
254
255
256
257
258
259
posted = 0;
if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
SDL_Event event;
event.tfinger.type = SDL_FINGERUP;
event.tfinger.touchId = id;
event.tfinger.fingerId = fingerid;
Mar 3, 2013
Mar 3, 2013
260
/* I don't trust the coordinates passed on fingerUp */
261
262
263
264
265
266
event.tfinger.x = finger->x;
event.tfinger.y = finger->y;
event.tfinger.dx = 0;
event.tfinger.dy = 0;
event.tfinger.pressure = pressure;
posted = (SDL_PushEvent(&event) > 0);
Mar 3, 2013
Mar 3, 2013
267
268
269
}
SDL_DelFinger(touch, fingerid);
Mar 3, 2013
Mar 3, 2013
271
return posted;
Mar 3, 2013
Mar 3, 2013
275
276
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
float x, float y, float pressure)
277
278
279
280
{
SDL_Touch *touch;
SDL_Finger *finger;
int posted;
Mar 3, 2013
Mar 3, 2013
281
282
float xrel, yrel, prel;
283
284
touch = SDL_GetTouch(id);
if (!touch) {
Mar 3, 2013
Mar 3, 2013
285
return -1;
286
287
288
}
finger = SDL_GetFinger(touch,fingerid);
Mar 3, 2013
Mar 3, 2013
289
290
if (!finger) {
return SDL_SendTouch(id, fingerid, SDL_TRUE, x, y, pressure);
Mar 3, 2013
Mar 3, 2013
293
294
295
xrel = x - finger->x;
yrel = y - finger->y;
prel = pressure - finger->pressure;
Mar 3, 2013
Mar 3, 2013
297
298
299
300
301
/* Drop events that don't change state */
if (!xrel && !yrel && !prel) {
#if 0
printf("Touch event didn't change state - dropped!\n");
#endif
Mar 3, 2013
Mar 3, 2013
305
306
307
308
309
/* Update internal touch coordinates */
finger->x = x;
finger->y = y;
finger->pressure = pressure;
310
311
/* Post the event, if desired */
posted = 0;
Mar 3, 2013
Mar 3, 2013
312
if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
Mar 3, 2013
Mar 3, 2013
314
315
316
317
318
319
320
321
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;
322
323
324
325
326
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
}
Mar 3, 2013
Mar 3, 2013
327
328
void
SDL_DelTouch(SDL_TouchID id)
Mar 3, 2013
Mar 3, 2013
330
331
int i;
int index = SDL_GetTouchIndex(id);
332
SDL_Touch *touch = SDL_GetTouch(id);
Mar 3, 2013
Mar 3, 2013
333
Mar 3, 2013
Mar 3, 2013
335
336
337
338
339
return;
}
for (i = 0; i < touch->max_fingers; ++i) {
SDL_free(touch->fingers[i]);
Mar 3, 2013
Mar 3, 2013
341
342
343
344
345
SDL_free(touch->fingers);
SDL_free(touch);
SDL_num_touch--;
SDL_touchDevices[index] = SDL_touchDevices[SDL_num_touch];
Mar 3, 2013
Mar 3, 2013
348
349
350
351
352
353
354
355
356
357
358
359
360
361
void
SDL_TouchQuit(void)
{
int i;
for (i = SDL_num_touch; i--; ) {
SDL_DelTouch(SDL_touchDevices[i]->id);
}
SDL_assert(SDL_num_touch == 0);
if (SDL_touchDevices) {
SDL_free(SDL_touchDevices);
SDL_touchDevices = NULL;
}
Mar 3, 2013
Mar 3, 2013
363
364
/* vi: set ts=4 sw=4 expandtab: */