Updated touch API
* Normalized touch coordinates as floats in the 0...1 range * Removed unused touchpad concepts from the API * Added API functions to get active touch devices and current finger state
This commit is contained in:
parent
71ea3033fa
commit
bb0752e573
15 changed files with 356 additions and 917 deletions
|
@ -64,7 +64,6 @@ typedef struct {
|
|||
|
||||
typedef struct {
|
||||
SDL_TouchID id;
|
||||
SDL_FloatPoint res;
|
||||
SDL_FloatPoint centroid;
|
||||
SDL_DollarPath dollarPath;
|
||||
Uint16 numDownFingers;
|
||||
|
@ -410,7 +409,7 @@ static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_Gestu
|
|||
return bestDiff;
|
||||
}
|
||||
|
||||
int SDL_GestureAddTouch(SDL_Touch* touch)
|
||||
int SDL_GestureAddTouch(SDL_TouchID touchId)
|
||||
{
|
||||
SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
|
||||
(SDL_numGestureTouches + 1) *
|
||||
|
@ -423,12 +422,8 @@ int SDL_GestureAddTouch(SDL_Touch* touch)
|
|||
|
||||
SDL_gestureTouch = gestureTouch;
|
||||
|
||||
SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres;
|
||||
SDL_gestureTouch[SDL_numGestureTouches].res.y = touch->yres;
|
||||
SDL_gestureTouch[SDL_numGestureTouches].numDownFingers = 0;
|
||||
|
||||
SDL_gestureTouch[SDL_numGestureTouches].res.x = touch->xres;
|
||||
SDL_gestureTouch[SDL_numGestureTouches].id = touch->id;
|
||||
SDL_gestureTouch[SDL_numGestureTouches].id = touchId;
|
||||
|
||||
SDL_gestureTouch[SDL_numGestureTouches].numDollarTemplates = 0;
|
||||
|
||||
|
@ -468,11 +463,8 @@ static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
|
|||
SDL_Event event;
|
||||
event.dgesture.type = SDL_DOLLARGESTURE;
|
||||
event.dgesture.touchId = touch->id;
|
||||
/*
|
||||
//TODO: Add this to give location of gesture?
|
||||
event.mgesture.x = touch->centroid.x;
|
||||
event.mgesture.y = touch->centroid.y;
|
||||
*/
|
||||
event.dgesture.gestureId = gestureId;
|
||||
event.dgesture.error = error;
|
||||
//A finger came up to trigger this event.
|
||||
|
@ -513,14 +505,8 @@ void SDL_GestureProcessEvent(SDL_Event* event)
|
|||
//Shouldn't be possible
|
||||
if (inTouch == NULL) return;
|
||||
|
||||
//printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
|
||||
// (int)event->tfinger.y,
|
||||
// (int)inTouch->res.x,(int)inTouch->res.y);
|
||||
|
||||
|
||||
x = ((float)event->tfinger.x)/(float)inTouch->res.x;
|
||||
y = ((float)event->tfinger.y)/(float)inTouch->res.y;
|
||||
|
||||
x = event->tfinger.x;
|
||||
y = event->tfinger.y;
|
||||
|
||||
//Finger Up
|
||||
if (event->type == SDL_FINGERUP) {
|
||||
|
@ -569,9 +555,8 @@ void SDL_GestureProcessEvent(SDL_Event* event)
|
|||
}
|
||||
}
|
||||
else if (event->type == SDL_FINGERMOTION) {
|
||||
float dx = ((float)event->tfinger.dx)/(float)inTouch->res.x;
|
||||
float dy = ((float)event->tfinger.dy)/(float)inTouch->res.y;
|
||||
//printf("dx,dy: (%f,%f)\n",dx,dy);
|
||||
float dx = event->tfinger.dx;
|
||||
float dy = event->tfinger.dy;
|
||||
#ifdef ENABLE_DOLLAR
|
||||
SDL_DollarPath* path = &inTouch->dollarPath;
|
||||
if (path->numPoints < MAXPATHSIZE) {
|
||||
|
|
|
@ -23,12 +23,12 @@
|
|||
#ifndef _SDL_gesture_c_h
|
||||
#define _SDL_gesture_c_h
|
||||
|
||||
extern int SDL_GestureAddTouch(SDL_TouchID touchId);
|
||||
|
||||
extern void SDL_GestureProcessEvent(SDL_Event* event);
|
||||
|
||||
extern int SDL_RecordGesture(SDL_TouchID touchId);
|
||||
|
||||
extern int SDL_GestureAddTouch(SDL_Touch* touch);
|
||||
|
||||
#endif /* _SDL_gesture_c_h */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
|
@ -22,72 +22,46 @@
|
|||
|
||||
/* General touch handling code for SDL */
|
||||
|
||||
#include "SDL_assert.h"
|
||||
#include "SDL_events.h"
|
||||
#include "SDL_events_c.h"
|
||||
#include "../video/SDL_sysvideo.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
static int SDL_num_touch = 0;
|
||||
static SDL_Touch **SDL_touchPads = NULL;
|
||||
static SDL_Touch **SDL_touchDevices = NULL;
|
||||
|
||||
|
||||
/* Public functions */
|
||||
int
|
||||
SDL_TouchInit(void)
|
||||
{
|
||||
return (0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
SDL_Touch *
|
||||
SDL_GetTouch(SDL_TouchID id)
|
||||
int
|
||||
SDL_GetNumTouchDevices()
|
||||
{
|
||||
int index = SDL_GetTouchIndexId(id);
|
||||
if (index < 0 || index >= SDL_num_touch) {
|
||||
return NULL;
|
||||
}
|
||||
return SDL_touchPads[index];
|
||||
return SDL_num_touch;
|
||||
}
|
||||
|
||||
SDL_Touch *
|
||||
SDL_GetTouchIndex(int index)
|
||||
SDL_TouchID
|
||||
SDL_GetTouchDevice(int index)
|
||||
{
|
||||
if (index < 0 || index >= SDL_num_touch) {
|
||||
return NULL;
|
||||
SDL_SetError("Unknown touch device");
|
||||
return 0;
|
||||
}
|
||||
return SDL_touchPads[index];
|
||||
return SDL_touchDevices[index]->id;
|
||||
}
|
||||
|
||||
static int
|
||||
SDL_GetFingerIndexId(SDL_Touch* touch,SDL_FingerID fingerid)
|
||||
{
|
||||
int i;
|
||||
for(i = 0;i < touch->num_fingers;i++)
|
||||
if(touch->fingers[i]->id == fingerid)
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
SDL_Finger *
|
||||
SDL_GetFinger(SDL_Touch* touch,SDL_FingerID id)
|
||||
{
|
||||
int index = SDL_GetFingerIndexId(touch,id);
|
||||
if(index < 0 || index >= touch->num_fingers)
|
||||
return NULL;
|
||||
return touch->fingers[index];
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
SDL_GetTouchIndexId(SDL_TouchID id)
|
||||
SDL_GetTouchIndex(SDL_TouchID id)
|
||||
{
|
||||
int index;
|
||||
SDL_Touch *touch;
|
||||
|
||||
for (index = 0; index < SDL_num_touch; ++index) {
|
||||
touch = SDL_touchPads[index];
|
||||
touch = SDL_touchDevices[index];
|
||||
if (touch->id == id) {
|
||||
return index;
|
||||
}
|
||||
|
@ -95,78 +69,284 @@ SDL_GetTouchIndexId(SDL_TouchID id)
|
|||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_AddTouch(const SDL_Touch * touch, char *name)
|
||||
SDL_Touch *
|
||||
SDL_GetTouch(SDL_TouchID id)
|
||||
{
|
||||
SDL_Touch **touchPads;
|
||||
int index;
|
||||
size_t length;
|
||||
int index = SDL_GetTouchIndex(id);
|
||||
if (index < 0 || index >= SDL_num_touch) {
|
||||
SDL_SetError("Unknown touch device");
|
||||
return NULL;
|
||||
}
|
||||
return SDL_touchDevices[index];
|
||||
}
|
||||
|
||||
if (SDL_GetTouchIndexId(touch->id) != -1) {
|
||||
SDL_SetError("Touch ID already in use");
|
||||
static int
|
||||
SDL_GetFingerIndex(const SDL_Touch * touch, SDL_FingerID fingerid)
|
||||
{
|
||||
int index;
|
||||
for (index = 0; index < touch->num_fingers; ++index) {
|
||||
if (touch->fingers[index]->id == fingerid) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_Finger *
|
||||
SDL_GetFinger(const SDL_Touch * touch, SDL_FingerID id)
|
||||
{
|
||||
int index = SDL_GetFingerIndex(touch, id);
|
||||
if (index < 0 || index >= touch->num_fingers) {
|
||||
return NULL;
|
||||
}
|
||||
return touch->fingers[index];
|
||||
}
|
||||
|
||||
int
|
||||
SDL_GetNumTouchFingers(SDL_TouchID touchID)
|
||||
{
|
||||
SDL_Touch *touch = SDL_GetTouch(touchID);
|
||||
if (touch) {
|
||||
return touch->num_fingers;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_Finger *
|
||||
SDL_GetTouchFinger(SDL_TouchID touchID, int index)
|
||||
{
|
||||
SDL_Touch *touch = SDL_GetTouch(touchID);
|
||||
if (!touch) {
|
||||
return NULL;
|
||||
}
|
||||
if (index < 0 || index >= touch->num_fingers) {
|
||||
SDL_SetError("Unknown touch finger");
|
||||
return NULL;
|
||||
}
|
||||
return touch->fingers[index];
|
||||
}
|
||||
|
||||
int
|
||||
SDL_AddTouch(SDL_TouchID touchID, const char *name)
|
||||
{
|
||||
SDL_Touch **touchDevices;
|
||||
int index;
|
||||
|
||||
index = SDL_GetTouchIndex(touchID);
|
||||
if (index >= 0) {
|
||||
return index;
|
||||
}
|
||||
|
||||
/* Add the touch to the list of touch */
|
||||
touchPads = (SDL_Touch **) SDL_realloc(SDL_touchPads,
|
||||
(SDL_num_touch + 1) * sizeof(*touch));
|
||||
if (!touchPads) {
|
||||
touchDevices = (SDL_Touch **) SDL_realloc(SDL_touchDevices,
|
||||
(SDL_num_touch + 1) * sizeof(*touchDevices));
|
||||
if (!touchDevices) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_touchPads = touchPads;
|
||||
SDL_touchDevices = touchDevices;
|
||||
index = SDL_num_touch++;
|
||||
|
||||
SDL_touchPads[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchPads[index]));
|
||||
if (!SDL_touchPads[index]) {
|
||||
SDL_touchDevices[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchDevices[index]));
|
||||
if (!SDL_touchDevices[index]) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
}
|
||||
SDL_memcpy(SDL_touchPads[index], touch, sizeof(*touch));
|
||||
|
||||
/* we're setting the touch properties */
|
||||
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_touchDevices[index]->id = touchID;
|
||||
SDL_touchDevices[index]->num_fingers = 0;
|
||||
SDL_touchDevices[index]->max_fingers = 0;
|
||||
SDL_touchDevices[index]->fingers = NULL;
|
||||
|
||||
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]);
|
||||
/* Record this touch device for gestures */
|
||||
/* We could do this on the fly in the gesture code if we wanted */
|
||||
SDL_GestureAddTouch(touchID);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
static int
|
||||
SDL_AddFinger(SDL_Touch *touch, SDL_FingerID fingerid, float x, float y, float pressure)
|
||||
{
|
||||
SDL_Finger *finger;
|
||||
|
||||
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) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
}
|
||||
touch->fingers = new_fingers;
|
||||
touch->fingers[touch->max_fingers] = (SDL_Finger *)SDL_malloc(sizeof(*finger));
|
||||
if (!touch->fingers[touch->max_fingers]) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
}
|
||||
touch->max_fingers++;
|
||||
}
|
||||
|
||||
finger = touch->fingers[touch->num_fingers++];
|
||||
finger->id = fingerid;
|
||||
finger->x = x;
|
||||
finger->y = y;
|
||||
finger->pressure = pressure;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
SDL_DelFinger(SDL_Touch* touch, SDL_FingerID fingerid)
|
||||
{
|
||||
SDL_Finger *temp;
|
||||
|
||||
int index = SDL_GetFingerIndex(touch, fingerid);
|
||||
if (index < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
touch->num_fingers--;
|
||||
temp = touch->fingers[index];
|
||||
touch->fingers[index] = touch->fingers[touch->num_fingers];
|
||||
touch->fingers[touch->num_fingers] = temp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
|
||||
SDL_bool down, float x, float y, float pressure)
|
||||
{
|
||||
int posted;
|
||||
SDL_Finger *finger;
|
||||
|
||||
SDL_Touch* touch = SDL_GetTouch(id);
|
||||
if (!touch) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
posted = 0;
|
||||
if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
|
||||
SDL_Event event;
|
||||
event.tfinger.type = SDL_FINGERDOWN;
|
||||
event.tfinger.touchId = id;
|
||||
event.tfinger.fingerId = fingerid;
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
if (!finger) {
|
||||
/* This finger is already up */
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
/* I don't trust the coordinates passed on fingerUp */
|
||||
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);
|
||||
}
|
||||
|
||||
SDL_DelFinger(touch, fingerid);
|
||||
}
|
||||
return posted;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
|
||||
float x, float y, float pressure)
|
||||
{
|
||||
SDL_Touch *touch;
|
||||
SDL_Finger *finger;
|
||||
int posted;
|
||||
float xrel, yrel, prel;
|
||||
|
||||
touch = SDL_GetTouch(id);
|
||||
if (!touch) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
finger = SDL_GetFinger(touch,fingerid);
|
||||
if (!finger) {
|
||||
return SDL_SendTouch(id, fingerid, SDL_TRUE, x, y, pressure);
|
||||
}
|
||||
|
||||
xrel = x - finger->x;
|
||||
yrel = y - finger->y;
|
||||
prel = pressure - finger->pressure;
|
||||
|
||||
/* Drop events that don't change state */
|
||||
if (!xrel && !yrel && !prel) {
|
||||
#if 0
|
||||
printf("Touch event didn't change state - dropped!\n");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Update internal touch coordinates */
|
||||
finger->x = x;
|
||||
finger->y = y;
|
||||
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;
|
||||
posted = (SDL_PushEvent(&event) > 0);
|
||||
}
|
||||
return posted;
|
||||
}
|
||||
|
||||
void
|
||||
SDL_DelTouch(SDL_TouchID id)
|
||||
{
|
||||
int index = SDL_GetTouchIndexId(id);
|
||||
int i;
|
||||
int index = SDL_GetTouchIndex(id);
|
||||
SDL_Touch *touch = SDL_GetTouch(id);
|
||||
|
||||
if (!touch) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
SDL_free(touch->name);
|
||||
|
||||
if (touch->FreeTouch) {
|
||||
touch->FreeTouch(touch);
|
||||
for (i = 0; i < touch->max_fingers; ++i) {
|
||||
SDL_free(touch->fingers[i]);
|
||||
}
|
||||
SDL_free(touch->fingers);
|
||||
SDL_free(touch);
|
||||
|
||||
SDL_num_touch--;
|
||||
SDL_touchPads[index] = SDL_touchPads[SDL_num_touch];
|
||||
SDL_touchDevices[index] = SDL_touchDevices[SDL_num_touch];
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -174,400 +354,15 @@ SDL_TouchQuit(void)
|
|||
{
|
||||
int i;
|
||||
|
||||
for (i = SDL_num_touch-1; i > 0 ; --i) {
|
||||
SDL_DelTouch(i);
|
||||
for (i = SDL_num_touch; i--; ) {
|
||||
SDL_DelTouch(SDL_touchDevices[i]->id);
|
||||
}
|
||||
SDL_num_touch = 0;
|
||||
SDL_assert(SDL_num_touch == 0);
|
||||
|
||||
if (SDL_touchPads) {
|
||||
SDL_free(SDL_touchPads);
|
||||
SDL_touchPads = NULL;
|
||||
if (SDL_touchDevices) {
|
||||
SDL_free(SDL_touchDevices);
|
||||
SDL_touchDevices = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
SDL_GetNumTouch(void)
|
||||
{
|
||||
return SDL_num_touch;
|
||||
}
|
||||
|
||||
SDL_Window *
|
||||
SDL_GetTouchFocusWindow(SDL_TouchID id)
|
||||
{
|
||||
SDL_Touch *touch = SDL_GetTouch(id);
|
||||
|
||||
if (!touch) {
|
||||
return 0;
|
||||
}
|
||||
return touch->focus;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
SDL_AddFinger(SDL_Touch* touch,SDL_Finger *finger)
|
||||
{
|
||||
int index;
|
||||
SDL_Finger **fingers;
|
||||
//printf("Adding Finger...\n");
|
||||
if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
|
||||
SDL_SetError("Finger ID already in use");
|
||||
}
|
||||
|
||||
/* Add the touch to the list of touch */
|
||||
if(touch->num_fingers >= touch->max_fingers){
|
||||
//printf("Making room for it!\n");
|
||||
fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
|
||||
(touch->num_fingers + 1) * sizeof(SDL_Finger *));
|
||||
if (!fingers) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
} else {
|
||||
touch->max_fingers = touch->num_fingers+1;
|
||||
touch->fingers = fingers;
|
||||
}
|
||||
}
|
||||
|
||||
index = touch->num_fingers;
|
||||
//printf("Max_Fingers: %i Index: %i\n",touch->max_fingers,index);
|
||||
|
||||
touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(SDL_Finger));
|
||||
if (!touch->fingers[index]) {
|
||||
SDL_OutOfMemory();
|
||||
return -1;
|
||||
}
|
||||
*(touch->fingers[index]) = *finger;
|
||||
touch->num_fingers++;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_DelFinger(SDL_Touch* touch,SDL_FingerID fingerid)
|
||||
{
|
||||
int index = SDL_GetFingerIndexId(touch,fingerid);
|
||||
SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
|
||||
|
||||
if (!finger) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
SDL_free(finger);
|
||||
touch->num_fingers--;
|
||||
touch->fingers[index] = touch->fingers[touch->num_fingers];
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
SDL_SendFingerDown(SDL_TouchID id, SDL_FingerID fingerid, SDL_bool down,
|
||||
float xin, float yin, float pressurein)
|
||||
{
|
||||
int posted;
|
||||
Uint16 x;
|
||||
Uint16 y;
|
||||
Uint16 pressure;
|
||||
SDL_Finger *finger;
|
||||
|
||||
SDL_Touch* touch = SDL_GetTouch(id);
|
||||
|
||||
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)((pressurein+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
|
||||
|
||||
finger = SDL_GetFinger(touch,fingerid);
|
||||
if(down) {
|
||||
if(finger == NULL) {
|
||||
SDL_Finger nf;
|
||||
nf.id = fingerid;
|
||||
nf.x = x;
|
||||
nf.y = y;
|
||||
nf.pressure = pressure;
|
||||
nf.xdelta = 0;
|
||||
nf.ydelta = 0;
|
||||
nf.last_x = x;
|
||||
nf.last_y = y;
|
||||
nf.last_pressure = pressure;
|
||||
nf.down = SDL_FALSE;
|
||||
if(SDL_AddFinger(touch,&nf) < 0) return 0;
|
||||
finger = SDL_GetFinger(touch,fingerid);
|
||||
}
|
||||
else if(finger->down) return 0;
|
||||
if(xin < touch->x_min || yin < touch->y_min) return 0; //should defer if only a partial input
|
||||
posted = 0;
|
||||
if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
|
||||
SDL_Event event;
|
||||
event.tfinger.type = SDL_FINGERDOWN;
|
||||
event.tfinger.touchId = id;
|
||||
event.tfinger.x = x;
|
||||
event.tfinger.y = y;
|
||||
event.tfinger.dx = 0;
|
||||
event.tfinger.dy = 0;
|
||||
event.tfinger.pressure = pressure;
|
||||
event.tfinger.state = touch->buttonstate;
|
||||
event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
|
||||
event.tfinger.fingerId = fingerid;
|
||||
posted = (SDL_PushEvent(&event) > 0);
|
||||
}
|
||||
if(posted) finger->down = SDL_TRUE;
|
||||
return posted;
|
||||
}
|
||||
else {
|
||||
if(finger == NULL) {
|
||||
SDL_SetError("Finger not found.");
|
||||
return 0;
|
||||
}
|
||||
posted = 0;
|
||||
if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
|
||||
SDL_Event event;
|
||||
event.tfinger.type = SDL_FINGERUP;
|
||||
event.tfinger.touchId = id;
|
||||
event.tfinger.state = touch->buttonstate;
|
||||
event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
|
||||
event.tfinger.fingerId = fingerid;
|
||||
//I don't trust the coordinates passed on fingerUp
|
||||
event.tfinger.x = finger->x;
|
||||
event.tfinger.y = finger->y;
|
||||
event.tfinger.dx = 0;
|
||||
event.tfinger.dy = 0;
|
||||
event.tfinger.pressure = pressure;
|
||||
|
||||
if(SDL_DelFinger(touch,fingerid) < 0) return 0;
|
||||
posted = (SDL_PushEvent(&event) > 0);
|
||||
}
|
||||
return posted;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, int relative,
|
||||
float xin, float yin, float pressurein)
|
||||
{
|
||||
SDL_Touch *touch;
|
||||
SDL_Finger *finger;
|
||||
int posted;
|
||||
Sint16 xrel, yrel;
|
||||
Uint16 x;
|
||||
Uint16 y;
|
||||
Uint16 pressure;
|
||||
|
||||
touch = SDL_GetTouch(id);
|
||||
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)((pressurein+touch->pressure_min)*(touch->pressureres)/(touch->native_pressureres));
|
||||
if(touch->flush_motion) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
finger = SDL_GetFinger(touch,fingerid);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button)
|
||||
{
|
||||
SDL_Touch *touch;
|
||||
int posted;
|
||||
Uint32 type;
|
||||
|
||||
|
||||
touch = SDL_GetTouch(id);
|
||||
if (!touch) {
|
||||
return SDL_TouchNotFoundError(id);
|
||||
}
|
||||
|
||||
/* Figure out which event to perform */
|
||||
switch (state) {
|
||||
case SDL_PRESSED:
|
||||
if (touch->buttonstate & SDL_BUTTON(button)) {
|
||||
/* Ignore this event, no state change */
|
||||
return 0;
|
||||
}
|
||||
type = SDL_TOUCHBUTTONDOWN;
|
||||
touch->buttonstate |= SDL_BUTTON(button);
|
||||
break;
|
||||
case SDL_RELEASED:
|
||||
if (!(touch->buttonstate & SDL_BUTTON(button))) {
|
||||
/* Ignore this event, no state change */
|
||||
return 0;
|
||||
}
|
||||
type = SDL_TOUCHBUTTONUP;
|
||||
touch->buttonstate &= ~SDL_BUTTON(button);
|
||||
break;
|
||||
default:
|
||||
/* Invalid state -- bail */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Post the event, if desired */
|
||||
posted = 0;
|
||||
if (SDL_GetEventState(type) == SDL_ENABLE) {
|
||||
SDL_Event event;
|
||||
event.type = type;
|
||||
event.tbutton.touchId = touch->id;
|
||||
event.tbutton.state = state;
|
||||
event.tbutton.button = button;
|
||||
event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
|
||||
posted = (SDL_PushEvent(&event) > 0);
|
||||
}
|
||||
return posted;
|
||||
}
|
||||
|
||||
char *
|
||||
SDL_GetTouchName(SDL_TouchID id)
|
||||
{
|
||||
SDL_Touch *touch = SDL_GetTouch(id);
|
||||
if (!touch) {
|
||||
return NULL;
|
||||
}
|
||||
return touch->name;
|
||||
}
|
||||
|
||||
int SDL_TouchNotFoundError(SDL_TouchID id) {
|
||||
//int i;
|
||||
SDL_SetError("ERROR: Cannot send touch on non-existent device with id: %li make sure SDL_AddTouch has been called\n",id);
|
||||
#if 0
|
||||
printf("ERROR: There are %i touches installed with Id's:\n",SDL_num_touch);
|
||||
for(i=0;i < SDL_num_touch;i++) {
|
||||
printf("ERROR: %li\n",SDL_touchPads[i]->id);
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
|
@ -24,55 +24,38 @@
|
|||
#ifndef _SDL_touch_c_h
|
||||
#define _SDL_touch_c_h
|
||||
|
||||
typedef struct SDL_Touch
|
||||
{
|
||||
SDL_TouchID id;
|
||||
int num_fingers;
|
||||
int max_fingers;
|
||||
SDL_Finger** fingers;
|
||||
} SDL_Touch;
|
||||
|
||||
|
||||
/* Initialize the touch subsystem */
|
||||
extern int SDL_TouchInit(void);
|
||||
|
||||
/*Get the touch at an index */
|
||||
extern SDL_Touch *SDL_GetTouchIndex(int index);
|
||||
|
||||
/* Get the touch with id = id */
|
||||
/* Add a touch, returning the index of the touch, or -1 if there was an error. */
|
||||
extern int SDL_AddTouch(SDL_TouchID id, const char *name);
|
||||
|
||||
/* Get the touch with a given id */
|
||||
extern SDL_Touch *SDL_GetTouch(SDL_TouchID id);
|
||||
|
||||
/*Get the finger at an index */
|
||||
extern SDL_Finger *SDL_GetFingerIndex(SDL_Touch *touch, int index);
|
||||
|
||||
/* Get the finger with id = id */
|
||||
extern SDL_Finger *SDL_GetFinger(SDL_Touch *touch,SDL_FingerID id);
|
||||
|
||||
|
||||
/* Add a touch, possibly reattaching at a particular index (or -1),
|
||||
returning the index of the touch, or -1 if there was an error. */
|
||||
extern int SDL_AddTouch(const SDL_Touch * touch, char *name);
|
||||
|
||||
|
||||
/* Remove a touch at an index, clearing the slot for later */
|
||||
extern void SDL_DelTouch(SDL_TouchID id);
|
||||
|
||||
/* Set the touch focus window */
|
||||
extern void SDL_SetTouchFocus(SDL_TouchID id, SDL_Window * window);
|
||||
/* Send a touch down/up event for a touch */
|
||||
extern int SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
|
||||
SDL_bool down, float x, float y, float pressure);
|
||||
|
||||
/* Send a touch motion event for a touch */
|
||||
extern int SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
|
||||
int relative, float x, float y, float z);
|
||||
float x, float y, float pressure);
|
||||
|
||||
/* Send a touch down/up event for a touch */
|
||||
extern int SDL_SendFingerDown(SDL_TouchID id, SDL_FingerID fingerid,
|
||||
SDL_bool down, float x, float y, float pressure);
|
||||
|
||||
/* Send a touch button event for a touch */
|
||||
extern int SDL_SendTouchButton(SDL_TouchID id, Uint8 state, Uint8 button);
|
||||
/* Remove a touch */
|
||||
extern void SDL_DelTouch(SDL_TouchID id);
|
||||
|
||||
/* Shutdown the touch subsystem */
|
||||
extern void SDL_TouchQuit(void);
|
||||
|
||||
/* Get the index of a touch device */
|
||||
extern int SDL_GetTouchIndexId(SDL_TouchID id);
|
||||
|
||||
/* Print a debug message for a nonexistent touch */
|
||||
extern int SDL_TouchNotFoundError(SDL_TouchID id);
|
||||
|
||||
#endif /* _SDL_touch_c_h */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue