comparison src/events/SDL_touch.c @ 4678:f8431f66613d

Added SDL_TouchID, SDL_FingerID, SDL_GestureID types. Converted to integer cioordinates (<- not working).
author jimtla
date Sat, 31 Jul 2010 20:02:54 +0400
parents 99b4560b7aa1
children 5ee96ba0c01e
comparison
equal deleted inserted replaced
4677:31607094315c 4678:f8431f66613d
40 { 40 {
41 return (0); 41 return (0);
42 } 42 }
43 43
44 SDL_Touch * 44 SDL_Touch *
45 SDL_GetTouch(long id) 45 SDL_GetTouch(SDL_TouchID id)
46 { 46 {
47 int index = SDL_GetTouchIndexId(id); 47 int index = SDL_GetTouchIndexId(id);
48 if (index < 0 || index >= SDL_num_touch) { 48 if (index < 0 || index >= SDL_num_touch) {
49 return NULL; 49 return NULL;
50 } 50 }
59 } 59 }
60 return SDL_touchPads[index]; 60 return SDL_touchPads[index];
61 } 61 }
62 62
63 int 63 int
64 SDL_GetFingerIndexId(SDL_Touch* touch,long fingerid) 64 SDL_GetFingerIndexId(SDL_Touch* touch,SDL_FingerID fingerid)
65 { 65 {
66 int i; 66 int i;
67 for(i = 0;i < touch->num_fingers;i++) 67 for(i = 0;i < touch->num_fingers;i++)
68 if(touch->fingers[i]->id == fingerid) 68 if(touch->fingers[i]->id == fingerid)
69 return i; 69 return i;
70 return -1; 70 return -1;
71 } 71 }
72 72
73 73
74 SDL_Finger * 74 SDL_Finger *
75 SDL_GetFinger(SDL_Touch* touch,long id) 75 SDL_GetFinger(SDL_Touch* touch,SDL_FingerID id)
76 { 76 {
77 int index = SDL_GetFingerIndexId(touch,id); 77 int index = SDL_GetFingerIndexId(touch,id);
78 if(index < 0 || index >= touch->num_fingers) 78 if(index < 0 || index >= touch->num_fingers)
79 return NULL; 79 return NULL;
80 return touch->fingers[index]; 80 return touch->fingers[index];
81 } 81 }
82 82
83 83
84 int 84 int
85 SDL_GetTouchIndexId(long id) 85 SDL_GetTouchIndexId(SDL_TouchID id)
86 { 86 {
87 int index; 87 int index;
88 SDL_Touch *touch; 88 SDL_Touch *touch;
89 89
90 for (index = 0; index < SDL_num_touch; ++index) { 90 for (index = 0; index < SDL_num_touch; ++index) {
137 SDL_touchPads[index]->fingers[0] = NULL; 137 SDL_touchPads[index]->fingers[0] = NULL;
138 SDL_touchPads[index]->buttonstate = 0; 138 SDL_touchPads[index]->buttonstate = 0;
139 SDL_touchPads[index]->relative_mode = SDL_FALSE; 139 SDL_touchPads[index]->relative_mode = SDL_FALSE;
140 SDL_touchPads[index]->flush_motion = SDL_FALSE; 140 SDL_touchPads[index]->flush_motion = SDL_FALSE;
141 141
142 SDL_touchPads[index]->xres = (1<<(16-1));
143 SDL_touchPads[index]->yres = (1<<(16-1));
142 //Do I want this here? Probably 144 //Do I want this here? Probably
143 SDL_GestureAddTouch(SDL_touchPads[index]); 145 SDL_GestureAddTouch(SDL_touchPads[index]);
144 146
145 return index; 147 return index;
146 } 148 }
147 149
148 void 150 void
149 SDL_DelTouch(long id) 151 SDL_DelTouch(SDL_TouchID id)
150 { 152 {
151 int index = SDL_GetTouchIndexId(id); 153 int index = SDL_GetTouchIndexId(id);
152 SDL_Touch *touch = SDL_GetTouch(id); 154 SDL_Touch *touch = SDL_GetTouch(id);
153 155
154 if (!touch) { 156 if (!touch) {
187 SDL_GetNumTouch(void) 189 SDL_GetNumTouch(void)
188 { 190 {
189 return SDL_num_touch; 191 return SDL_num_touch;
190 } 192 }
191 SDL_Window * 193 SDL_Window *
192 SDL_GetTouchFocusWindow(long id) 194 SDL_GetTouchFocusWindow(SDL_TouchID id)
193 { 195 {
194 SDL_Touch *touch = SDL_GetTouch(id); 196 SDL_Touch *touch = SDL_GetTouch(id);
195 197
196 if (!touch) { 198 if (!touch) {
197 return 0; 199 return 0;
198 } 200 }
199 return touch->focus; 201 return touch->focus;
200 } 202 }
201 203
202 void 204 void
203 SDL_SetTouchFocus(long id, SDL_Window * window) 205 SDL_SetTouchFocus(SDL_TouchID id, SDL_Window * window)
204 { 206 {
205 int index = SDL_GetTouchIndexId(id); 207 int index = SDL_GetTouchIndexId(id);
206 SDL_Touch *touch = SDL_GetTouch(id); 208 SDL_Touch *touch = SDL_GetTouch(id);
207 int i; 209 int i;
208 SDL_bool focus; 210 SDL_bool focus;
248 } 250 }
249 } 251 }
250 } 252 }
251 253
252 int 254 int
253 SDL_AddFinger(SDL_Touch* touch,SDL_Finger finger) 255 SDL_AddFinger(SDL_Touch* touch,SDL_Finger *finger)
254 { 256 {
255 int index; 257 int index;
256 SDL_Finger **fingers; 258 SDL_Finger **fingers;
257 //printf("Adding Finger...\n"); 259 //printf("Adding Finger...\n");
258 if (SDL_GetFingerIndexId(touch,finger.id) != -1) { 260 if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
259 SDL_SetError("Finger ID already in use"); 261 SDL_SetError("Finger ID already in use");
260 } 262 }
261 263
262 /* Add the touch to the list of touch */ 264 /* Add the touch to the list of touch */
263 if(touch->num_fingers >= touch->max_fingers){ 265 if(touch->num_fingers >= touch->max_fingers){
280 touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(SDL_Finger)); 282 touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(SDL_Finger));
281 if (!touch->fingers[index]) { 283 if (!touch->fingers[index]) {
282 SDL_OutOfMemory(); 284 SDL_OutOfMemory();
283 return -1; 285 return -1;
284 } 286 }
285 *(touch->fingers[index]) = finger; 287 *(touch->fingers[index]) = *finger;
286 touch->num_fingers++; 288 touch->num_fingers++;
287 289
288 return index; 290 return index;
289 } 291 }
290 292
291 int 293 int
292 SDL_DelFinger(SDL_Touch* touch,long fingerid) 294 SDL_DelFinger(SDL_Touch* touch,SDL_FingerID fingerid)
293 { 295 {
294 int index = SDL_GetFingerIndexId(touch,fingerid); 296 int index = SDL_GetFingerIndexId(touch,fingerid);
295 SDL_Finger* finger = SDL_GetFinger(touch,fingerid); 297 SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
296 298
297 if (!finger) { 299 if (!finger) {
305 return 0; 307 return 0;
306 } 308 }
307 309
308 310
309 int 311 int
310 SDL_SendFingerDown(long id, long fingerid, SDL_bool down, float x, float y, float pressure) 312 SDL_SendFingerDown(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down,
313 float xin, float yin, float pressurein)
311 { 314 {
312 int posted; 315 int posted;
313 SDL_Touch* touch = SDL_GetTouch(id); 316 SDL_Touch* touch = SDL_GetTouch(id);
314 317
315 if(!touch) { 318 if(!touch) {
316 return SDL_TouchNotFoundError(id); 319 return SDL_TouchNotFoundError(id);
317 } 320 }
318 321 //scale to Integer coordinates
322 Uint16 x = (xin+touch->x_min)*(touch->xres)/(touch->native_xres);
323 Uint16 y = (yin+touch->y_min)*(touch->yres)/(touch->native_yres);
324 Uint16 pressure = (yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres);
319 if(down) { 325 if(down) {
320 SDL_Finger *finger = SDL_GetFinger(touch,fingerid); 326 SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
327 SDL_Finger nf;
321 if(finger == NULL) { 328 if(finger == NULL) {
322 SDL_Finger nf; 329
323 nf.id = fingerid; 330 nf.id = fingerid;
324 nf.x = x; 331 nf.x = x;
325 nf.y = y; 332 nf.y = y;
326 nf.pressure = pressure; 333 nf.pressure = pressure;
327 nf.xdelta = 0; 334 nf.xdelta = 0;
328 nf.ydelta = 0; 335 nf.ydelta = 0;
329 nf.last_x = x; 336 nf.last_x = x;
330 nf.last_y = y; 337 nf.last_y = y;
331 nf.last_pressure = pressure; 338 nf.last_pressure = pressure;
332 nf.down = SDL_FALSE; 339 nf.down = SDL_FALSE;
333 SDL_AddFinger(touch,nf); 340 SDL_AddFinger(touch,&nf);
334 finger = &nf; 341 finger = &nf;
335 } 342 }
336 else if(finger->down) return 0; 343 else if(finger->down) return 0;
337 if(x < 0 || y < 0) return 0; //should defer if only a partial input 344 if(xin < touch->x_min || yin < touch->y_min) return 0; //should defer if only a partial input
338 posted = 0; 345 posted = 0;
339 if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) { 346 if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
340 SDL_Event event; 347 SDL_Event event;
341 event.tfinger.type = SDL_FINGERDOWN; 348 event.tfinger.type = SDL_FINGERDOWN;
342 event.tfinger.touchId = id; 349 event.tfinger.touchId = id;
365 return posted; 372 return posted;
366 } 373 }
367 } 374 }
368 375
369 int 376 int
370 SDL_SendTouchMotion(long id, long fingerid, int relative, 377 SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative,
371 float x, float y, float pressure) 378 float xin, float yin, float pressurein)
372 { 379 {
373 int index = SDL_GetTouchIndexId(id); 380 int index = SDL_GetTouchIndexId(id);
374 SDL_Touch *touch = SDL_GetTouch(id); 381 SDL_Touch *touch = SDL_GetTouch(id);
375 SDL_Finger *finger = SDL_GetFinger(touch,fingerid); 382 SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
376 int posted; 383 int posted;
379 float x_max = 0, y_max = 0; 386 float x_max = 0, y_max = 0;
380 387
381 if (!touch) { 388 if (!touch) {
382 return SDL_TouchNotFoundError(id); 389 return SDL_TouchNotFoundError(id);
383 } 390 }
391
392 //scale to Integer coordinates
393 Uint16 x = (xin+touch->x_min)*(touch->xres)/(touch->native_xres);
394 Uint16 y = (yin+touch->y_min)*(touch->yres)/(touch->native_yres);
395 Uint16 pressure = (yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres);
396 printf("(%f,%f) --> (%i,%i)",xin,yin,x,y);
384 if(touch->flush_motion) { 397 if(touch->flush_motion) {
385 return 0; 398 return 0;
386 } 399 }
387 400
388 if(finger == NULL || !finger->down) { 401 if(finger == NULL || !finger->down) {
393 xrel = x; 406 xrel = x;
394 yrel = y; 407 yrel = y;
395 x = (finger->last_x + x); 408 x = (finger->last_x + x);
396 y = (finger->last_y + y); 409 y = (finger->last_y + y);
397 } else { 410 } else {
398 if(x < 0) x = finger->last_x; /*If movement is only in one axis,*/ 411 if(xin < touch->x_min) x = finger->last_x; /*If movement is only in one axis,*/
399 if(y < 0) y = finger->last_y; /*The other is marked as -1*/ 412 if(yin < touch->y_min) y = finger->last_y; /*The other is marked as -1*/
400 if(pressure < 0) pressure = finger->last_pressure; 413 if(pressurein < touch->pressure_min) pressure = finger->last_pressure;
401 xrel = x - finger->last_x; 414 xrel = x - finger->last_x;
402 yrel = y - finger->last_y; 415 yrel = y - finger->last_y;
403 } 416 }
404 417
405 /* Drop events that don't change state */ 418 /* Drop events that don't change state */
446 event.tfinger.type = SDL_FINGERMOTION; 459 event.tfinger.type = SDL_FINGERMOTION;
447 event.tfinger.touchId = id; 460 event.tfinger.touchId = id;
448 event.tfinger.fingerId = fingerid; 461 event.tfinger.fingerId = fingerid;
449 event.tfinger.x = x; 462 event.tfinger.x = x;
450 event.tfinger.y = y; 463 event.tfinger.y = y;
464
451 event.tfinger.pressure = pressure; 465 event.tfinger.pressure = pressure;
452 event.tfinger.state = touch->buttonstate; 466 event.tfinger.state = touch->buttonstate;
453 event.tfinger.windowID = touch->focus ? touch->focus->id : 0; 467 event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
454 posted = (SDL_PushEvent(&event) > 0); 468 posted = (SDL_PushEvent(&event) > 0);
455 } 469 }
458 finger->last_pressure = finger->pressure; 472 finger->last_pressure = finger->pressure;
459 return posted; 473 return posted;
460 } 474 }
461 } 475 }
462 int 476 int
463 SDL_SendTouchButton(long id, Uint8 state, Uint8 button) 477 SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button)
464 { 478 {
465 SDL_Touch *touch = SDL_GetTouch(id); 479 SDL_Touch *touch = SDL_GetTouch(id);
466 int posted; 480 int posted;
467 Uint32 type; 481 Uint32 type;
468 482
507 } 521 }
508 return posted; 522 return posted;
509 } 523 }
510 524
511 char * 525 char *
512 SDL_GetTouchName(long id) 526 SDL_GetTouchName(SDL_TouchID id)
513 { 527 {
514 SDL_Touch *touch = SDL_GetTouch(id); 528 SDL_Touch *touch = SDL_GetTouch(id);
515 if (!touch) { 529 if (!touch) {
516 return NULL; 530 return NULL;
517 } 531 }
518 return touch->name; 532 return touch->name;
519 } 533 }
520 534
521 int SDL_TouchNotFoundError(long id) { 535 int SDL_TouchNotFoundError(SDL_TouchID id) {
522 printf("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id); 536 printf("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id);
523 printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch); 537 printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch);
524 int i; 538 int i;
525 for(i=0;i < SDL_num_touch;i++) { 539 for(i=0;i < SDL_num_touch;i++) {
526 printf("ERROR: %li\n",SDL_touchPads[i]->id); 540 printf("ERROR: %li\n",SDL_touchPads[i]->id);