SDL_Touch * SDL_GetTouch(SDL_TouchID id) { int index = SDL_GetTouchIndexId(id); if (index < 0 || index >= SDL_num_touch) { return NULL; } return SDL_touchPads[index]; }
int SDL_AddTouch(const SDL_Touch * touch, char *name) { SDL_Touch **touchPads; int index; size_t length; if (SDL_GetTouchIndexId(touch->id) != -1) { SDL_SetError("Touch ID already in use"); } /* Add the touch to the list of touch */ touchPads = (SDL_Touch **) SDL_realloc(SDL_touchPads, (SDL_num_touch + 1) * sizeof(*touch)); if (!touchPads) { SDL_OutOfMemory(); return -1; } SDL_touchPads = touchPads; index = SDL_num_touch++; SDL_touchPads[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchPads[index])); if (!SDL_touchPads[index]) { SDL_OutOfMemory(); return -1; } SDL_memcpy(SDL_touchPads[index], touch, sizeof(*touch)); /* we're setting the touch properties */ length = 0; length = SDL_strlen(name); SDL_touchPads[index]->focus = 0; SDL_touchPads[index]->name = SDL_malloc((length + 2) * sizeof(char)); SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1); SDL_touchPads[index]->num_fingers = 0; SDL_touchPads[index]->max_fingers = 1; SDL_touchPads[index]->fingers = (SDL_Finger **) SDL_malloc(sizeof(SDL_Finger*)); SDL_touchPads[index]->fingers[0] = NULL; SDL_touchPads[index]->buttonstate = 0; SDL_touchPads[index]->relative_mode = SDL_FALSE; SDL_touchPads[index]->flush_motion = SDL_FALSE; SDL_touchPads[index]->xres = (1<<(16-1)); SDL_touchPads[index]->yres = (1<<(16-1)); SDL_touchPads[index]->pressureres = (1<<(16-1)); //Do I want this here? Probably SDL_GestureAddTouch(SDL_touchPads[index]); return index; }
void SDL_SetTouchFocus(SDL_TouchID id, SDL_Window * window) { int index = SDL_GetTouchIndexId(id); SDL_Touch *touch = SDL_GetTouch(id); int i; SDL_bool focus; if (!touch || (touch->focus == window)) { return; } /* See if the current window has lost focus */ if (touch->focus) { focus = SDL_FALSE; for (i = 0; i < SDL_num_touch; ++i) { SDL_Touch *check; if (i != index) { check = SDL_touchPads[i]; if (check && check->focus == touch->focus) { focus = SDL_TRUE; break; } } } if (!focus) { SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_LEAVE, 0, 0); } } touch->focus = window; if (touch->focus) { focus = SDL_FALSE; for (i = 0; i < SDL_num_touch; ++i) { SDL_Touch *check; if (i != index) { check = SDL_touchPads[i]; if (check && check->focus == touch->focus) { focus = SDL_TRUE; break; } } } if (!focus) { SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_ENTER, 0, 0); } } }
void SDL_DelTouch(SDL_TouchID id) { int index = SDL_GetTouchIndexId(id); SDL_Touch *touch = SDL_GetTouch(id); if (!touch) { return; } SDL_free(touch->name); if (touch->FreeTouch) { touch->FreeTouch(touch); } SDL_free(touch); SDL_num_touch--; SDL_touchPads[index] = SDL_touchPads[SDL_num_touch]; }
int SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative, float xin, float yin, float pressurein) { int index = SDL_GetTouchIndexId(id); SDL_Touch *touch = SDL_GetTouch(id); SDL_Finger *finger = SDL_GetFinger(touch,fingerid); int posted; Sint16 xrel, yrel; float x_max = 0, y_max = 0; Uint16 x; Uint16 y; Uint16 pressure; if (!touch) { return SDL_TouchNotFoundError(id); } //scale to Integer coordinates x = (Uint16)((xin+touch->x_min)*(touch->xres)/(touch->native_xres)); y = (Uint16)((yin+touch->y_min)*(touch->yres)/(touch->native_yres)); pressure = (Uint16)((yin+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres)); if(touch->flush_motion) { return 0; } if(finger == NULL || !finger->down) { return SDL_SendFingerDown(id,fingerid,SDL_TRUE,xin,yin,pressurein); } else { /* the relative motion is calculated regarding the last position */ if (relative) { xrel = x; yrel = y; x = (finger->last_x + x); y = (finger->last_y + y); } else { if(xin < touch->x_min) x = finger->last_x; /*If movement is only in one axis,*/ if(yin < touch->y_min) y = finger->last_y; /*The other is marked as -1*/ if(pressurein < touch->pressure_min) pressure = finger->last_pressure; xrel = x - finger->last_x; yrel = y - finger->last_y; //printf("xrel,yrel (%i,%i)\n",(int)xrel,(int)yrel); } /* Drop events that don't change state */ if (!xrel && !yrel) { #if 0 printf("Touch event didn't change state - dropped!\n"); #endif return 0; } /* Update internal touch coordinates */ finger->x = x; finger->y = y; /*Should scale to window? Normalize? Maintain Aspect?*/ //SDL_GetWindowSize(touch->focus, &x_max, &y_max); /* make sure that the pointers find themselves inside the windows */ /* only check if touch->xmax is set ! */ /* if (x_max && touch->x > x_max) { touch->x = x_max; } else if (touch->x < 0) { touch->x = 0; } if (y_max && touch->y > y_max) { touch->y = y_max; } else if (touch->y < 0) { touch->y = 0; } */ finger->xdelta = xrel; finger->ydelta = yrel; finger->pressure = pressure; /* Post the event, if desired */ posted = 0; if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) { SDL_Event event; 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; event.tfinger.state = touch->buttonstate; event.tfinger.windowID = touch->focus ? touch->focus->id : 0; posted = (SDL_PushEvent(&event) > 0); } finger->last_x = finger->x; finger->last_y = finger->y; finger->last_pressure = finger->pressure; return posted; } }