SDL  2.0
SDL_touch.c File Reference
#include "../SDL_internal.h"
#include "SDL_assert.h"
#include "SDL_events.h"
#include "SDL_events_c.h"
#include "../video/SDL_sysvideo.h"
+ Include dependency graph for SDL_touch.c:

Go to the source code of this file.

Functions

int SDL_TouchInit (void)
 
int SDL_GetNumTouchDevices (void)
 Get the number of registered touch devices. More...
 
SDL_TouchID SDL_GetTouchDevice (int index)
 Get the touch ID with the given index, or 0 if the index is invalid. More...
 
static int SDL_GetTouchIndex (SDL_TouchID id)
 
SDL_TouchSDL_GetTouch (SDL_TouchID id)
 
static int SDL_GetFingerIndex (const SDL_Touch *touch, SDL_FingerID fingerid)
 
static SDL_FingerSDL_GetFinger (const SDL_Touch *touch, SDL_FingerID id)
 
int SDL_GetNumTouchFingers (SDL_TouchID touchID)
 Get the number of active fingers for a given touch device. More...
 
SDL_FingerSDL_GetTouchFinger (SDL_TouchID touchID, int index)
 Get the finger object of the given touch, with the given index. More...
 
int SDL_AddTouch (SDL_TouchID touchID, const char *name)
 
static int SDL_AddFinger (SDL_Touch *touch, SDL_FingerID fingerid, float x, float y, float pressure)
 
static int SDL_DelFinger (SDL_Touch *touch, SDL_FingerID fingerid)
 
int SDL_SendTouch (SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down, float x, float y, float pressure)
 
int SDL_SendTouchMotion (SDL_TouchID id, SDL_FingerID fingerid, float x, float y, float pressure)
 
void SDL_DelTouch (SDL_TouchID id)
 
void SDL_TouchQuit (void)
 

Variables

static int SDL_num_touch = 0
 
static SDL_Touch ** SDL_touchDevices = NULL
 

Function Documentation

◆ SDL_AddFinger()

static int SDL_AddFinger ( SDL_Touch touch,
SDL_FingerID  fingerid,
float  x,
float  y,
float  pressure 
)
static

Definition at line 178 of file SDL_touch.c.

References SDL_Touch::fingers, SDL_Finger::id, SDL_Touch::max_fingers, SDL_Touch::num_fingers, SDL_Finger::pressure, SDL_malloc, SDL_OutOfMemory, SDL_realloc, SDL_Finger::x, and SDL_Finger::y.

Referenced by SDL_SendTouch().

179 {
180  SDL_Finger *finger;
181 
182  if (touch->num_fingers == touch->max_fingers) {
183  SDL_Finger **new_fingers;
184  new_fingers = (SDL_Finger **)SDL_realloc(touch->fingers, (touch->max_fingers+1)*sizeof(*touch->fingers));
185  if (!new_fingers) {
186  return SDL_OutOfMemory();
187  }
188  touch->fingers = new_fingers;
189  touch->fingers[touch->max_fingers] = (SDL_Finger *)SDL_malloc(sizeof(*finger));
190  if (!touch->fingers[touch->max_fingers]) {
191  return SDL_OutOfMemory();
192  }
193  touch->max_fingers++;
194  }
195 
196  finger = touch->fingers[touch->num_fingers++];
197  finger->id = fingerid;
198  finger->x = x;
199  finger->y = y;
200  finger->pressure = pressure;
201  return 0;
202 }
int max_fingers
Definition: SDL_touch_c.h:31
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_Finger ** fingers
Definition: SDL_touch_c.h:32
#define SDL_realloc
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
float y
Definition: SDL_touch.h:48
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
int num_fingers
Definition: SDL_touch_c.h:30
SDL_FingerID id
Definition: SDL_touch.h:46
#define SDL_malloc
float pressure
Definition: SDL_touch.h:49
float x
Definition: SDL_touch.h:47

◆ SDL_AddTouch()

int SDL_AddTouch ( SDL_TouchID  touchID,
const char *  name 
)

Definition at line 136 of file SDL_touch.c.

References SDL_Touch::fingers, SDL_Touch::id, SDL_Touch::max_fingers, NULL, SDL_Touch::num_fingers, SDL_GestureAddTouch(), SDL_GetTouchIndex(), SDL_malloc, SDL_num_touch, SDL_OutOfMemory, and SDL_realloc.

137 {
138  SDL_Touch **touchDevices;
139  int index;
140 
141  index = SDL_GetTouchIndex(touchID);
142  if (index >= 0) {
143  return index;
144  }
145 
146  /* Add the touch to the list of touch */
147  touchDevices = (SDL_Touch **) SDL_realloc(SDL_touchDevices,
148  (SDL_num_touch + 1) * sizeof(*touchDevices));
149  if (!touchDevices) {
150  return SDL_OutOfMemory();
151  }
152 
153  SDL_touchDevices = touchDevices;
154  index = SDL_num_touch;
155 
157  if (!SDL_touchDevices[index]) {
158  return SDL_OutOfMemory();
159  }
160 
161  /* Added touch to list */
162  ++SDL_num_touch;
163 
164  /* we're setting the touch properties */
165  SDL_touchDevices[index]->id = touchID;
169 
170  /* Record this touch device for gestures */
171  /* We could do this on the fly in the gesture code if we wanted */
172  SDL_GestureAddTouch(touchID);
173 
174  return index;
175 }
static int SDL_num_touch
Definition: SDL_touch.c:31
int max_fingers
Definition: SDL_touch_c.h:31
SDL_Finger ** fingers
Definition: SDL_touch_c.h:32
#define SDL_realloc
SDL_TouchID id
Definition: SDL_touch_c.h:29
static int SDL_GetTouchIndex(SDL_TouchID id)
Definition: SDL_touch.c:59
int SDL_GestureAddTouch(SDL_TouchID touchId)
Definition: SDL_gesture.c:448
GLuint index
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
int num_fingers
Definition: SDL_touch_c.h:30
#define SDL_malloc
static SDL_Touch ** SDL_touchDevices
Definition: SDL_touch.c:32

◆ SDL_DelFinger()

static int SDL_DelFinger ( SDL_Touch touch,
SDL_FingerID  fingerid 
)
static

Definition at line 205 of file SDL_touch.c.

References SDL_Touch::fingers, SDL_Touch::num_fingers, and SDL_GetFingerIndex().

Referenced by SDL_SendTouch().

206 {
207  SDL_Finger *temp;
208 
209  int index = SDL_GetFingerIndex(touch, fingerid);
210  if (index < 0) {
211  return -1;
212  }
213 
214  touch->num_fingers--;
215  temp = touch->fingers[index];
216  touch->fingers[index] = touch->fingers[touch->num_fingers];
217  touch->fingers[touch->num_fingers] = temp;
218  return 0;
219 }
SDL_Finger ** fingers
Definition: SDL_touch_c.h:32
GLuint index
int num_fingers
Definition: SDL_touch_c.h:30
static int SDL_GetFingerIndex(const SDL_Touch *touch, SDL_FingerID fingerid)
Definition: SDL_touch.c:90

◆ SDL_DelTouch()

void SDL_DelTouch ( SDL_TouchID  id)

Definition at line 337 of file SDL_touch.c.

References SDL_Touch::fingers, i, SDL_Touch::max_fingers, SDL_free, SDL_GestureDelTouch(), SDL_GetTouch(), SDL_GetTouchIndex(), and SDL_num_touch.

Referenced by SDL_TouchQuit().

338 {
339  int i;
340  int index = SDL_GetTouchIndex(id);
341  SDL_Touch *touch = SDL_GetTouch(id);
342 
343  if (!touch) {
344  return;
345  }
346 
347  for (i = 0; i < touch->max_fingers; ++i) {
348  SDL_free(touch->fingers[i]);
349  }
350  SDL_free(touch->fingers);
351  SDL_free(touch);
352 
353  SDL_num_touch--;
355 
356  /* Delete this touch device for gestures */
358 }
static int SDL_num_touch
Definition: SDL_touch.c:31
int max_fingers
Definition: SDL_touch_c.h:31
int SDL_GestureDelTouch(SDL_TouchID touchId)
Definition: SDL_gesture.c:466
SDL_Finger ** fingers
Definition: SDL_touch_c.h:32
static int SDL_GetTouchIndex(SDL_TouchID id)
Definition: SDL_touch.c:59
#define SDL_free
GLuint index
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
static SDL_Touch ** SDL_touchDevices
Definition: SDL_touch.c:32
SDL_Touch * SDL_GetTouch(SDL_TouchID id)
Definition: SDL_touch.c:74

◆ SDL_GetFinger()

static SDL_Finger* SDL_GetFinger ( const SDL_Touch touch,
SDL_FingerID  id 
)
static

Definition at line 102 of file SDL_touch.c.

References SDL_Touch::fingers, NULL, SDL_Touch::num_fingers, and SDL_GetFingerIndex().

Referenced by SDL_SendTouch(), and SDL_SendTouchMotion().

103 {
104  int index = SDL_GetFingerIndex(touch, id);
105  if (index < 0 || index >= touch->num_fingers) {
106  return NULL;
107  }
108  return touch->fingers[index];
109 }
SDL_Finger ** fingers
Definition: SDL_touch_c.h:32
GLuint index
#define NULL
Definition: begin_code.h:164
int num_fingers
Definition: SDL_touch_c.h:30
static int SDL_GetFingerIndex(const SDL_Touch *touch, SDL_FingerID fingerid)
Definition: SDL_touch.c:90

◆ SDL_GetFingerIndex()

static int SDL_GetFingerIndex ( const SDL_Touch touch,
SDL_FingerID  fingerid 
)
static

Definition at line 90 of file SDL_touch.c.

References SDL_Touch::fingers, SDL_Finger::id, and SDL_Touch::num_fingers.

Referenced by SDL_DelFinger(), and SDL_GetFinger().

91 {
92  int index;
93  for (index = 0; index < touch->num_fingers; ++index) {
94  if (touch->fingers[index]->id == fingerid) {
95  return index;
96  }
97  }
98  return -1;
99 }
SDL_Finger ** fingers
Definition: SDL_touch_c.h:32
GLuint index
int num_fingers
Definition: SDL_touch_c.h:30
SDL_FingerID id
Definition: SDL_touch.h:46

◆ SDL_GetNumTouchDevices()

int SDL_GetNumTouchDevices ( void  )

Get the number of registered touch devices.

Definition at line 43 of file SDL_touch.c.

References SDL_num_touch.

44 {
45  return SDL_num_touch;
46 }
static int SDL_num_touch
Definition: SDL_touch.c:31

◆ SDL_GetNumTouchFingers()

int SDL_GetNumTouchFingers ( SDL_TouchID  touchID)

Get the number of active fingers for a given touch device.

Definition at line 112 of file SDL_touch.c.

References SDL_Touch::num_fingers, and SDL_GetTouch().

113 {
114  SDL_Touch *touch = SDL_GetTouch(touchID);
115  if (touch) {
116  return touch->num_fingers;
117  }
118  return 0;
119 }
int num_fingers
Definition: SDL_touch_c.h:30
SDL_Touch * SDL_GetTouch(SDL_TouchID id)
Definition: SDL_touch.c:74

◆ SDL_GetTouch()

SDL_Touch* SDL_GetTouch ( SDL_TouchID  id)

Definition at line 74 of file SDL_touch.c.

References NULL, SDL_VideoDevice::ResetTouch, SDL_GetTouchIndex(), SDL_GetVideoDevice(), SDL_num_touch, and SDL_SetError.

Referenced by SDL_DelTouch(), SDL_GetNumTouchFingers(), SDL_GetTouchFinger(), SDL_SendTouch(), and SDL_SendTouchMotion().

75 {
76  int index = SDL_GetTouchIndex(id);
78  if (SDL_GetVideoDevice()->ResetTouch != NULL) {
79  SDL_SetError("Unknown touch id %d, resetting", (int) id);
81  } else {
82  SDL_SetError("Unknown touch device id %d, cannot reset", (int) id);
83  }
84  return NULL;
85  }
86  return SDL_touchDevices[index];
87 }
static int SDL_num_touch
Definition: SDL_touch.c:31
static int SDL_GetTouchIndex(SDL_TouchID id)
Definition: SDL_touch.c:59
GLuint index
void(* ResetTouch)(_THIS)
Definition: SDL_sysvideo.h:171
#define NULL
Definition: begin_code.h:164
#define SDL_SetError
SDL_VideoDevice * SDL_GetVideoDevice(void)
Definition: SDL_video.c:586
static SDL_Touch ** SDL_touchDevices
Definition: SDL_touch.c:32

◆ SDL_GetTouchDevice()

SDL_TouchID SDL_GetTouchDevice ( int  index)

Get the touch ID with the given index, or 0 if the index is invalid.

Definition at line 49 of file SDL_touch.c.

References SDL_Touch::id, SDL_num_touch, and SDL_SetError.

50 {
52  SDL_SetError("Unknown touch device index %d", index);
53  return 0;
54  }
55  return SDL_touchDevices[index]->id;
56 }
static int SDL_num_touch
Definition: SDL_touch.c:31
SDL_TouchID id
Definition: SDL_touch_c.h:29
GLuint index
#define SDL_SetError
static SDL_Touch ** SDL_touchDevices
Definition: SDL_touch.c:32

◆ SDL_GetTouchFinger()

SDL_Finger* SDL_GetTouchFinger ( SDL_TouchID  touchID,
int  index 
)

Get the finger object of the given touch, with the given index.

Definition at line 122 of file SDL_touch.c.

References SDL_Touch::fingers, NULL, SDL_Touch::num_fingers, SDL_GetTouch(), and SDL_SetError.

123 {
124  SDL_Touch *touch = SDL_GetTouch(touchID);
125  if (!touch) {
126  return NULL;
127  }
128  if (index < 0 || index >= touch->num_fingers) {
129  SDL_SetError("Unknown touch finger");
130  return NULL;
131  }
132  return touch->fingers[index];
133 }
SDL_Finger ** fingers
Definition: SDL_touch_c.h:32
GLuint index
#define NULL
Definition: begin_code.h:164
#define SDL_SetError
int num_fingers
Definition: SDL_touch_c.h:30
SDL_Touch * SDL_GetTouch(SDL_TouchID id)
Definition: SDL_touch.c:74

◆ SDL_GetTouchIndex()

static int SDL_GetTouchIndex ( SDL_TouchID  id)
static

Definition at line 59 of file SDL_touch.c.

References SDL_Touch::id, and SDL_num_touch.

Referenced by SDL_AddTouch(), SDL_DelTouch(), and SDL_GetTouch().

60 {
61  int index;
62  SDL_Touch *touch;
63 
64  for (index = 0; index < SDL_num_touch; ++index) {
65  touch = SDL_touchDevices[index];
66  if (touch->id == id) {
67  return index;
68  }
69  }
70  return -1;
71 }
static int SDL_num_touch
Definition: SDL_touch.c:31
SDL_TouchID id
Definition: SDL_touch_c.h:29
GLuint index
static SDL_Touch ** SDL_touchDevices
Definition: SDL_touch.c:32

◆ SDL_SendTouch()

int SDL_SendTouch ( SDL_TouchID  id,
SDL_FingerID  fingerid,
SDL_bool  down,
float  x,
float  y,
float  pressure 
)

Definition at line 222 of file SDL_touch.c.

References SDL_AddFinger(), SDL_DelFinger(), SDL_ENABLE, SDL_FINGERDOWN, SDL_FINGERUP, SDL_GetEventState, SDL_GetFinger(), SDL_GetTouch(), SDL_PushEvent, SDL_Finger::x, and SDL_Finger::y.

Referenced by SDL_SendTouchMotion().

224 {
225  int posted;
226  SDL_Finger *finger;
227 
228  SDL_Touch* touch = SDL_GetTouch(id);
229  if (!touch) {
230  return -1;
231  }
232 
233  finger = SDL_GetFinger(touch, fingerid);
234  if (down) {
235  if (finger) {
236  /* This finger is already down */
237  return 0;
238  }
239 
240  if (SDL_AddFinger(touch, fingerid, x, y, pressure) < 0) {
241  return 0;
242  }
243 
244  posted = 0;
247  event.tfinger.type = SDL_FINGERDOWN;
248  event.tfinger.touchId = id;
249  event.tfinger.fingerId = fingerid;
250  event.tfinger.x = x;
251  event.tfinger.y = y;
252  event.tfinger.dx = 0;
253  event.tfinger.dy = 0;
254  event.tfinger.pressure = pressure;
255  posted = (SDL_PushEvent(&event) > 0);
256  }
257  } else {
258  if (!finger) {
259  /* This finger is already up */
260  return 0;
261  }
262 
263  posted = 0;
266  event.tfinger.type = SDL_FINGERUP;
267  event.tfinger.touchId = id;
268  event.tfinger.fingerId = fingerid;
269  /* I don't trust the coordinates passed on fingerUp */
270  event.tfinger.x = finger->x;
271  event.tfinger.y = finger->y;
272  event.tfinger.dx = 0;
273  event.tfinger.dy = 0;
274  event.tfinger.pressure = pressure;
275  posted = (SDL_PushEvent(&event) > 0);
276  }
277 
278  SDL_DelFinger(touch, fingerid);
279  }
280  return posted;
281 }
GLuint id
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
static int SDL_DelFinger(SDL_Touch *touch, SDL_FingerID fingerid)
Definition: SDL_touch.c:205
static SDL_Finger * SDL_GetFinger(const SDL_Touch *touch, SDL_FingerID id)
Definition: SDL_touch.c:102
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
static int SDL_AddFinger(SDL_Touch *touch, SDL_FingerID fingerid, float x, float y, float pressure)
Definition: SDL_touch.c:178
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
float y
Definition: SDL_touch.h:48
General event structure.
Definition: SDL_events.h:525
SDL_Touch * SDL_GetTouch(SDL_TouchID id)
Definition: SDL_touch.c:74
float x
Definition: SDL_touch.h:47

◆ SDL_SendTouchMotion()

int SDL_SendTouchMotion ( SDL_TouchID  id,
SDL_FingerID  fingerid,
float  x,
float  y,
float  pressure 
)

Definition at line 284 of file SDL_touch.c.

References SDL_Finger::pressure, SDL_ENABLE, SDL_FINGERMOTION, SDL_GetEventState, SDL_GetFinger(), SDL_GetTouch(), SDL_PushEvent, SDL_SendTouch(), SDL_TRUE, SDL_Finger::x, and SDL_Finger::y.

286 {
287  SDL_Touch *touch;
288  SDL_Finger *finger;
289  int posted;
290  float xrel, yrel, prel;
291 
292  touch = SDL_GetTouch(id);
293  if (!touch) {
294  return -1;
295  }
296 
297  finger = SDL_GetFinger(touch,fingerid);
298  if (!finger) {
299  return SDL_SendTouch(id, fingerid, SDL_TRUE, x, y, pressure);
300  }
301 
302  xrel = x - finger->x;
303  yrel = y - finger->y;
304  prel = pressure - finger->pressure;
305 
306  /* Drop events that don't change state */
307  if (!xrel && !yrel && !prel) {
308 #if 0
309  printf("Touch event didn't change state - dropped!\n");
310 #endif
311  return 0;
312  }
313 
314  /* Update internal touch coordinates */
315  finger->x = x;
316  finger->y = y;
317  finger->pressure = pressure;
318 
319  /* Post the event, if desired */
320  posted = 0;
323  event.tfinger.type = SDL_FINGERMOTION;
324  event.tfinger.touchId = id;
325  event.tfinger.fingerId = fingerid;
326  event.tfinger.x = x;
327  event.tfinger.y = y;
328  event.tfinger.dx = xrel;
329  event.tfinger.dy = yrel;
330  event.tfinger.pressure = pressure;
331  posted = (SDL_PushEvent(&event) > 0);
332  }
333  return posted;
334 }
GLuint id
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
int SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down, float x, float y, float pressure)
Definition: SDL_touch.c:222
static SDL_Finger * SDL_GetFinger(const SDL_Touch *touch, SDL_FingerID id)
Definition: SDL_touch.c:102
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
float y
Definition: SDL_touch.h:48
General event structure.
Definition: SDL_events.h:525
float pressure
Definition: SDL_touch.h:49
SDL_Touch * SDL_GetTouch(SDL_TouchID id)
Definition: SDL_touch.c:74
float x
Definition: SDL_touch.h:47

◆ SDL_TouchInit()

int SDL_TouchInit ( void  )

Definition at line 37 of file SDL_touch.c.

Referenced by SDL_VideoInit().

38 {
39  return (0);
40 }

◆ SDL_TouchQuit()

void SDL_TouchQuit ( void  )

Definition at line 361 of file SDL_touch.c.

References i, NULL, SDL_assert, SDL_DelTouch(), SDL_free, SDL_GestureQuit(), and SDL_num_touch.

Referenced by SDL_VideoQuit().

362 {
363  int i;
364 
365  for (i = SDL_num_touch; i--; ) {
367  }
369 
372  SDL_GestureQuit();
373 }
static int SDL_num_touch
Definition: SDL_touch.c:31
void SDL_GestureQuit()
Definition: SDL_gesture.c:104
#define SDL_free
void SDL_DelTouch(SDL_TouchID id)
Definition: SDL_touch.c:337
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define NULL
Definition: begin_code.h:164
static SDL_Touch ** SDL_touchDevices
Definition: SDL_touch.c:32

Variable Documentation

◆ SDL_num_touch

int SDL_num_touch = 0
static

◆ SDL_touchDevices

SDL_Touch** SDL_touchDevices = NULL
static

Definition at line 32 of file SDL_touch.c.