diff 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
line wrap: on
line diff
--- a/src/events/SDL_touch.c	Thu May 27 01:21:37 2010 -0400
+++ b/src/events/SDL_touch.c	Fri May 28 01:26:52 2010 -0400
@@ -36,8 +36,15 @@
 int
 SDL_TouchInit(void)
 {
+    SDL_Touch touch;
+    touch.pressure_max = 0;
+    touch.pressure_min = 0;
+    touch.id = 0; //Should be function? 
+    
+    SDL_AddTouch(&touch, "Touch1");
     return (0);
 }
+
 SDL_Touch *
 SDL_GetTouch(int id)
 {
@@ -48,13 +55,13 @@
     return SDL_touchPads[index];
 }
 
-SDL_Finger *
-SDL_GetFinger(SDL_Touch* touch,int id)
+SDL_Touch *
+SDL_GetTouchIndex(int index)
 {
-    int index = SDL_GetFingerIndexId(touch,id);
-    if(index < 0 || index >= touch->num_fingers)
-	return NULL;
-    return touch->fingers[index];
+    if (index < 0 || index >= SDL_num_touch) {
+        return NULL;
+    }
+    return SDL_touchPads[index];
 }
 
 int
@@ -67,6 +74,17 @@
     return -1;
 }
 
+
+SDL_Finger *
+SDL_GetFinger(SDL_Touch* touch,int id)
+{
+    int index = SDL_GetFingerIndexId(touch,id);
+    if(index < 0 || index >= touch->num_fingers)
+	return NULL;
+    return touch->fingers[index];
+}
+
+
 int
 SDL_GetTouchIndexId(int id)
 {
@@ -83,8 +101,7 @@
 }
 
 int
-SDL_AddTouch(const SDL_Touch * touch, char *name, int pressure_max,
-             int pressure_min, int ends)
+SDL_AddTouch(const SDL_Touch * touch, char *name)
 {
     SDL_Touch **touchPads;
     int selected_touch;
@@ -118,11 +135,13 @@
     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]->pressure_max = pressure_max;
-    SDL_touchPads[index]->pressure_min = pressure_min;
+    SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);   
+
+    SDL_touchPads[index]->num_fingers = 0;
+    SDL_touchPads[index]->buttonstate = 0;
+    SDL_touchPads[index]->relative_mode = SDL_FALSE;
+    SDL_touchPads[index]->flush_motion = SDL_FALSE;
     
-
     return index;
 }
 
@@ -239,7 +258,7 @@
     
     if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
         SDL_SetError("Finger ID already in use");
-    }
+	}
 
     /* Add the touch to the list of touch */
     fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
@@ -250,7 +269,7 @@
     }
 
     touch->fingers = fingers;
-    index = SDL_num_touch++;
+    index = touch->num_fingers++;
 
     touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(*(touch->fingers[index])));
     if (!touch->fingers[index]) {
@@ -265,7 +284,7 @@
 int
 SDL_DelFinger(SDL_Touch* touch,int fingerid)
 {
-    int index = SLD_GetFingerIndexId(touch,fingerid);
+    int index = SDL_GetFingerIndexId(touch,fingerid);
     SDL_Finger* finger = SDL_GetFinger(touch,fingerid);
 
     if (!finger) {
@@ -282,6 +301,7 @@
 int
 SDL_SendFingerDown(int id, int fingerid, SDL_bool down, int x, int y, int pressure)
 {
+  int posted;
     SDL_Touch* touch = SDL_GetTouch(id);
     if(down) {
 	SDL_Finger nf;
@@ -300,9 +320,11 @@
 	    SDL_Event event;
 	    event.tfinger.type = SDL_FINGERDOWN;
 	    event.tfinger.touchId = (Uint8) id;
+	    event.tfinger.x = x;
+	    event.tfinger.y = y;
 	    event.tfinger.state = touch->buttonstate;
 	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
-	    event.fingerId = id;
+	    event.tfinger.fingerId = id;
 	    posted = (SDL_PushEvent(&event) > 0);
 	}
 	return posted;
@@ -316,7 +338,7 @@
 	    event.tfinger.touchId = (Uint8) id;
 	    event.tfinger.state = touch->buttonstate;
 	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
-	    event.fingerId = id;
+	    event.tfinger.fingerId = id;
 	    posted = (SDL_PushEvent(&event) > 0);
 	}
 	return posted;
@@ -339,69 +361,76 @@
         return 0;
     }
 
-    /* the relative motion is calculated regarding the system cursor last position */
-    if (relative) {
-        xrel = x;
-        yrel = y;
-        x = (finger->last_x + x);
-        y = (finger->last_y + y);
-    } else {
-        xrel = x - finger->last_x;
-        yrel = y - finger->last_y;
-    }
-
-    /* Drop events that don't change state */
-    if (!xrel && !yrel) {
+    if(finger == NULL)
+	SDL_SendFingerDown(id,fingerid,SDL_TRUE,x,y,pressure);
+    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(x < 0) x = finger->last_x; /*If movement is only in one axis,*/
+	    if(y < 0) y = finger->last_y; /*The other is marked as -1*/
+	    xrel = x - finger->last_x;
+	    yrel = y - finger->last_y;
+	}
+	
+	/* Drop events that don't change state */
+	if (!xrel && !yrel) {
 #if 0
-        printf("Touch event didn't change state - dropped!\n");
+	    printf("Touch event didn't change state - dropped!\n");
 #endif
-        return 0;
+	    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 = (Uint8) index;
+	    event.tfinger.x = x;
+	    event.tfinger.y = y;
+	    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;
+	return posted;
     }
-
-    /* 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.which = (Uint8) index;
-        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;
-    return posted;
 }
-
 int
 SDL_SendTouchButton(int id, Uint8 state, Uint8 button)
 {
@@ -441,7 +470,7 @@
     if (SDL_GetEventState(type) == SDL_ENABLE) {
         SDL_Event event;
         event.type = type;
-        event.tbutton.which = (Uint8) index;
+        event.tbutton.touchId = (Uint8) index;
         event.tbutton.state = state;
         event.tbutton.button = button;
         event.tbutton.windowID = touch->focus ? touch->focus->id : 0;