comparison src/events/SDL_touch.c @ 4642:057e8762d2a1

Added reading of event* for touch events.
author Jim Grandpre <jim.tla@gmail.com>
date Fri, 28 May 2010 01:26:52 -0400
parents 49a97daea6ec
children 8806b78988f7
comparison
equal deleted inserted replaced
4641:49a97daea6ec 4642:057e8762d2a1
34 34
35 /* Public functions */ 35 /* Public functions */
36 int 36 int
37 SDL_TouchInit(void) 37 SDL_TouchInit(void)
38 { 38 {
39 SDL_Touch touch;
40 touch.pressure_max = 0;
41 touch.pressure_min = 0;
42 touch.id = 0; //Should be function?
43
44 SDL_AddTouch(&touch, "Touch1");
39 return (0); 45 return (0);
40 } 46 }
47
41 SDL_Touch * 48 SDL_Touch *
42 SDL_GetTouch(int id) 49 SDL_GetTouch(int id)
43 { 50 {
44 int index = SDL_GetTouchIndexId(id); 51 int index = SDL_GetTouchIndexId(id);
45 if (index < 0 || index >= SDL_num_touch) { 52 if (index < 0 || index >= SDL_num_touch) {
46 return NULL; 53 return NULL;
47 } 54 }
48 return SDL_touchPads[index]; 55 return SDL_touchPads[index];
49 } 56 }
50 57
51 SDL_Finger * 58 SDL_Touch *
52 SDL_GetFinger(SDL_Touch* touch,int id) 59 SDL_GetTouchIndex(int index)
53 { 60 {
54 int index = SDL_GetFingerIndexId(touch,id); 61 if (index < 0 || index >= SDL_num_touch) {
55 if(index < 0 || index >= touch->num_fingers) 62 return NULL;
56 return NULL; 63 }
57 return touch->fingers[index]; 64 return SDL_touchPads[index];
58 } 65 }
59 66
60 int 67 int
61 SDL_GetFingerIndexId(SDL_Touch* touch,int fingerid) 68 SDL_GetFingerIndexId(SDL_Touch* touch,int fingerid)
62 { 69 {
65 if(touch->fingers[i]->id == fingerid) 72 if(touch->fingers[i]->id == fingerid)
66 return i; 73 return i;
67 return -1; 74 return -1;
68 } 75 }
69 76
77
78 SDL_Finger *
79 SDL_GetFinger(SDL_Touch* touch,int id)
80 {
81 int index = SDL_GetFingerIndexId(touch,id);
82 if(index < 0 || index >= touch->num_fingers)
83 return NULL;
84 return touch->fingers[index];
85 }
86
87
70 int 88 int
71 SDL_GetTouchIndexId(int id) 89 SDL_GetTouchIndexId(int id)
72 { 90 {
73 int index; 91 int index;
74 SDL_Touch *touch; 92 SDL_Touch *touch;
81 } 99 }
82 return -1; 100 return -1;
83 } 101 }
84 102
85 int 103 int
86 SDL_AddTouch(const SDL_Touch * touch, char *name, int pressure_max, 104 SDL_AddTouch(const SDL_Touch * touch, char *name)
87 int pressure_min, int ends)
88 { 105 {
89 SDL_Touch **touchPads; 106 SDL_Touch **touchPads;
90 int selected_touch; 107 int selected_touch;
91 int index; 108 int index;
92 size_t length; 109 size_t length;
116 /* we're setting the touch properties */ 133 /* we're setting the touch properties */
117 length = 0; 134 length = 0;
118 length = SDL_strlen(name); 135 length = SDL_strlen(name);
119 SDL_touchPads[index]->focus = 0; 136 SDL_touchPads[index]->focus = 0;
120 SDL_touchPads[index]->name = SDL_malloc((length + 2) * sizeof(char)); 137 SDL_touchPads[index]->name = SDL_malloc((length + 2) * sizeof(char));
121 SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1); 138 SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);
122 SDL_touchPads[index]->pressure_max = pressure_max; 139
123 SDL_touchPads[index]->pressure_min = pressure_min; 140 SDL_touchPads[index]->num_fingers = 0;
141 SDL_touchPads[index]->buttonstate = 0;
142 SDL_touchPads[index]->relative_mode = SDL_FALSE;
143 SDL_touchPads[index]->flush_motion = SDL_FALSE;
124 144
125
126 return index; 145 return index;
127 } 146 }
128 147
129 void 148 void
130 SDL_DelTouch(int id) 149 SDL_DelTouch(int id)
237 SDL_Finger **fingers; 256 SDL_Finger **fingers;
238 size_t length; 257 size_t length;
239 258
240 if (SDL_GetFingerIndexId(touch,finger->id) != -1) { 259 if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
241 SDL_SetError("Finger ID already in use"); 260 SDL_SetError("Finger ID already in use");
242 } 261 }
243 262
244 /* Add the touch to the list of touch */ 263 /* Add the touch to the list of touch */
245 fingers = (SDL_Finger **) SDL_realloc(touch->fingers, 264 fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
246 (touch->num_fingers + 1) * sizeof(*touch)); 265 (touch->num_fingers + 1) * sizeof(*touch));
247 if (!fingers) { 266 if (!fingers) {
248 SDL_OutOfMemory(); 267 SDL_OutOfMemory();
249 return -1; 268 return -1;
250 } 269 }
251 270
252 touch->fingers = fingers; 271 touch->fingers = fingers;
253 index = SDL_num_touch++; 272 index = touch->num_fingers++;
254 273
255 touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(*(touch->fingers[index]))); 274 touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(*(touch->fingers[index])));
256 if (!touch->fingers[index]) { 275 if (!touch->fingers[index]) {
257 SDL_OutOfMemory(); 276 SDL_OutOfMemory();
258 return -1; 277 return -1;
263 } 282 }
264 283
265 int 284 int
266 SDL_DelFinger(SDL_Touch* touch,int fingerid) 285 SDL_DelFinger(SDL_Touch* touch,int fingerid)
267 { 286 {
268 int index = SLD_GetFingerIndexId(touch,fingerid); 287 int index = SDL_GetFingerIndexId(touch,fingerid);
269 SDL_Finger* finger = SDL_GetFinger(touch,fingerid); 288 SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
270 289
271 if (!finger) { 290 if (!finger) {
272 return; 291 return;
273 } 292 }
280 299
281 300
282 int 301 int
283 SDL_SendFingerDown(int id, int fingerid, SDL_bool down, int x, int y, int pressure) 302 SDL_SendFingerDown(int id, int fingerid, SDL_bool down, int x, int y, int pressure)
284 { 303 {
304 int posted;
285 SDL_Touch* touch = SDL_GetTouch(id); 305 SDL_Touch* touch = SDL_GetTouch(id);
286 if(down) { 306 if(down) {
287 SDL_Finger nf; 307 SDL_Finger nf;
288 nf.id = id; 308 nf.id = id;
289 nf.x = x; 309 nf.x = x;
298 posted = 0; 318 posted = 0;
299 if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) { 319 if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
300 SDL_Event event; 320 SDL_Event event;
301 event.tfinger.type = SDL_FINGERDOWN; 321 event.tfinger.type = SDL_FINGERDOWN;
302 event.tfinger.touchId = (Uint8) id; 322 event.tfinger.touchId = (Uint8) id;
323 event.tfinger.x = x;
324 event.tfinger.y = y;
303 event.tfinger.state = touch->buttonstate; 325 event.tfinger.state = touch->buttonstate;
304 event.tfinger.windowID = touch->focus ? touch->focus->id : 0; 326 event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
305 event.fingerId = id; 327 event.tfinger.fingerId = id;
306 posted = (SDL_PushEvent(&event) > 0); 328 posted = (SDL_PushEvent(&event) > 0);
307 } 329 }
308 return posted; 330 return posted;
309 } 331 }
310 else { 332 else {
314 SDL_Event event; 336 SDL_Event event;
315 event.tfinger.type = SDL_FINGERUP; 337 event.tfinger.type = SDL_FINGERUP;
316 event.tfinger.touchId = (Uint8) id; 338 event.tfinger.touchId = (Uint8) id;
317 event.tfinger.state = touch->buttonstate; 339 event.tfinger.state = touch->buttonstate;
318 event.tfinger.windowID = touch->focus ? touch->focus->id : 0; 340 event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
319 event.fingerId = id; 341 event.tfinger.fingerId = id;
320 posted = (SDL_PushEvent(&event) > 0); 342 posted = (SDL_PushEvent(&event) > 0);
321 } 343 }
322 return posted; 344 return posted;
323 } 345 }
324 } 346 }
337 359
338 if (!touch || touch->flush_motion) { 360 if (!touch || touch->flush_motion) {
339 return 0; 361 return 0;
340 } 362 }
341 363
342 /* the relative motion is calculated regarding the system cursor last position */ 364 if(finger == NULL)
343 if (relative) { 365 SDL_SendFingerDown(id,fingerid,SDL_TRUE,x,y,pressure);
344 xrel = x; 366 else {
345 yrel = y; 367 /* the relative motion is calculated regarding the last position */
346 x = (finger->last_x + x); 368 if (relative) {
347 y = (finger->last_y + y); 369 xrel = x;
348 } else { 370 yrel = y;
349 xrel = x - finger->last_x; 371 x = (finger->last_x + x);
350 yrel = y - finger->last_y; 372 y = (finger->last_y + y);
351 } 373 } else {
352 374 if(x < 0) x = finger->last_x; /*If movement is only in one axis,*/
353 /* Drop events that don't change state */ 375 if(y < 0) y = finger->last_y; /*The other is marked as -1*/
354 if (!xrel && !yrel) { 376 xrel = x - finger->last_x;
377 yrel = y - finger->last_y;
378 }
379
380 /* Drop events that don't change state */
381 if (!xrel && !yrel) {
355 #if 0 382 #if 0
356 printf("Touch event didn't change state - dropped!\n"); 383 printf("Touch event didn't change state - dropped!\n");
357 #endif 384 #endif
358 return 0; 385 return 0;
359 } 386 }
360 387
361 /* Update internal touch coordinates */ 388 /* Update internal touch coordinates */
362 389
363 finger->x = x; 390 finger->x = x;
364 finger->y = y; 391 finger->y = y;
365 392
366 /*Should scale to window? Normalize? Maintain Aspect?*/ 393 /*Should scale to window? Normalize? Maintain Aspect?*/
367 //SDL_GetWindowSize(touch->focus, &x_max, &y_max); 394 //SDL_GetWindowSize(touch->focus, &x_max, &y_max);
368 395
369 /* make sure that the pointers find themselves inside the windows */ 396 /* make sure that the pointers find themselves inside the windows */
370 /* only check if touch->xmax is set ! */ 397 /* only check if touch->xmax is set ! */
371 /* 398 /*
372 if (x_max && touch->x > x_max) { 399 if (x_max && touch->x > x_max) {
373 touch->x = x_max; 400 touch->x = x_max;
374 } else if (touch->x < 0) { 401 } else if (touch->x < 0) {
375 touch->x = 0; 402 touch->x = 0;
376 } 403 }
377 404
378 if (y_max && touch->y > y_max) { 405 if (y_max && touch->y > y_max) {
379 touch->y = y_max; 406 touch->y = y_max;
380 } else if (touch->y < 0) { 407 } else if (touch->y < 0) {
381 touch->y = 0; 408 touch->y = 0;
382 } 409 }
383 */ 410 */
384 finger->xdelta += xrel; 411 finger->xdelta += xrel;
385 finger->ydelta += yrel; 412 finger->ydelta += yrel;
386 finger->pressure = pressure; 413 finger->pressure = pressure;
387 414
388 415
389 416
390 /* Post the event, if desired */ 417 /* Post the event, if desired */
391 posted = 0; 418 posted = 0;
392 if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) { 419 if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
393 SDL_Event event; 420 SDL_Event event;
394 event.tfinger.type = SDL_FINGERMOTION; 421 event.tfinger.type = SDL_FINGERMOTION;
395 event.tfinger.which = (Uint8) index; 422 event.tfinger.touchId = (Uint8) index;
396 event.tfinger.state = touch->buttonstate; 423 event.tfinger.x = x;
397 event.tfinger.windowID = touch->focus ? touch->focus->id : 0; 424 event.tfinger.y = y;
398 posted = (SDL_PushEvent(&event) > 0); 425 event.tfinger.state = touch->buttonstate;
399 } 426 event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
400 finger->last_x = finger->x; 427 posted = (SDL_PushEvent(&event) > 0);
401 finger->last_y = finger->y; 428 }
402 return posted; 429 finger->last_x = finger->x;
403 } 430 finger->last_y = finger->y;
404 431 return posted;
432 }
433 }
405 int 434 int
406 SDL_SendTouchButton(int id, Uint8 state, Uint8 button) 435 SDL_SendTouchButton(int id, Uint8 state, Uint8 button)
407 { 436 {
408 SDL_Touch *touch = SDL_GetTouch(id); 437 SDL_Touch *touch = SDL_GetTouch(id);
409 int posted; 438 int posted;
439 /* Post the event, if desired */ 468 /* Post the event, if desired */
440 posted = 0; 469 posted = 0;
441 if (SDL_GetEventState(type) == SDL_ENABLE) { 470 if (SDL_GetEventState(type) == SDL_ENABLE) {
442 SDL_Event event; 471 SDL_Event event;
443 event.type = type; 472 event.type = type;
444 event.tbutton.which = (Uint8) index; 473 event.tbutton.touchId = (Uint8) index;
445 event.tbutton.state = state; 474 event.tbutton.state = state;
446 event.tbutton.button = button; 475 event.tbutton.button = button;
447 event.tbutton.windowID = touch->focus ? touch->focus->id : 0; 476 event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
448 posted = (SDL_PushEvent(&event) > 0); 477 posted = (SDL_PushEvent(&event) > 0);
449 } 478 }