Added include/touch.h Now reading in resolution of touch pad.
authorJim Grandpre <jim.tla@gmail.com>
Tue, 01 Jun 2010 02:54:33 -0400
changeset 4646 eea1bf53effa
parent 4645 0375d020e7e3
child 4647 be2250bb482b
child 4651 86c171888eee
Added include/touch.h Now reading in resolution of touch pad.
include/SDL_touch.h
src/events/SDL_touch.c
src/events/SDL_touch_c.h
src/video/x11/SDL_eventtouch.c
src/video/x11/SDL_x11events.c
touchTest/parseDevicesTest.c
touchTest/touchTest.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/SDL_touch.h	Tue Jun 01 02:54:33 2010 -0400
@@ -0,0 +1,118 @@
+/*
+    SDL - Simple DirectMedia Layer
+    Copyright (C) 1997-2010 Sam Lantinga
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+    Sam Lantinga
+    slouken@libsdl.org
+*/
+
+/**
+ *  \file SDL_touch.h
+ *  
+ *  Include file for SDL mouse event handling.
+ */
+
+#ifndef _SDL_touch_h
+#define _SDL_touch_h
+
+#include "SDL_stdinc.h"
+#include "SDL_error.h"
+#include "SDL_video.h"
+
+#include "begin_code.h"
+/* Set up for C function definitions, even when using C++ */
+#ifdef __cplusplus
+/* *INDENT-OFF* */
+extern "C" {
+/* *INDENT-ON* */
+#endif
+
+typedef struct SDL_Touch SDL_Touch;
+typedef struct SDL_Finger SDL_Finger;
+
+struct SDL_Finger {
+  int id;
+  int x;
+  int y;
+  int z;                      /* for future use */
+  int xdelta;
+  int ydelta;
+  int last_x, last_y,last_pressure;  /* the last reported coordinates */
+  int pressure;
+};
+
+
+struct SDL_Touch
+{
+  
+  /* Free the touch when it's time */
+  void (*FreeTouch) (SDL_Touch * touch);
+  
+  /* data common for tablets */
+  int pressure_max, pressure_min;
+  int x_max,x_min;
+  int y_max,y_min;
+  int xres,yres,pressureres;
+  int tilt;                   /* for future use */
+  int rotation;               /* for future use */
+  
+  /* Data common to all touch */
+  int id;
+  SDL_Window *focus;
+  
+  char *name;
+  Uint8 buttonstate;
+  SDL_bool relative_mode;
+  SDL_bool flush_motion;
+
+  int num_fingers;
+  int max_fingers;
+  SDL_Finger** fingers;
+    
+  void *driverdata;
+};
+
+
+/* Function prototypes */
+
+/**
+ *  \brief Get the touch object at the given id.
+ *
+ *
+ */
+  extern DECLSPEC SDL_Touch* SDLCALL SDL_GetTouch(int id);
+
+
+
+/**
+ *  \brief Get the finger object of the given touch, at the given id.
+ *
+ *
+ */
+  extern DECLSPEC SDL_Finger* SDLCALL SDL_GetFinger(SDL_Touch *touch, int id);
+
+/* Ends C function definitions when using C++ */
+#ifdef __cplusplus
+/* *INDENT-OFF* */
+}
+/* *INDENT-ON* */
+#endif
+#include "close_code.h"
+
+#endif /* _SDL_mouse_h */
+
+/* vi: set ts=4 sw=4 expandtab: */
--- a/src/events/SDL_touch.c	Mon May 31 00:24:37 2010 -0400
+++ b/src/events/SDL_touch.c	Tue Jun 01 02:54:33 2010 -0400
@@ -133,9 +133,9 @@
     SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);   
 
     SDL_touchPads[index]->num_fingers = 0;
-    SDL_touchPads[index]->max_fingers = 0;
-    SDL_touchPads[index]->fingers = NULL;
-    
+    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;
@@ -253,39 +253,37 @@
     int index;
     SDL_Finger **fingers;
     size_t length;
-    
+    //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){
-      if(fingers == NULL) {
-	touch->max_fingers = 1;
-	fingers = (SDL_Finger **) SDL_malloc(sizeof(finger));
-      }
-      else {
-	fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
-				     (touch->num_fingers + 1) * sizeof(finger));
+    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 *));
 	touch->max_fingers = touch->num_fingers+1;
-      }
-
+	if (!fingers) {
+	    SDL_OutOfMemory();
+	    return -1;
+	} 
+	else {
+	    touch->max_fingers = touch->num_fingers+1;
+	    touch->fingers = fingers;
+	}
     }
 
-    if (!fingers) {
-        SDL_OutOfMemory();
-        return -1;
-    }
+    index = touch->num_fingers;
+    //printf("Max_Fingers: %i Index: %i\n",touch->max_fingers,index);
 
-    touch->fingers = fingers;
-    index = touch->num_fingers;
-    touch->num_fingers++;
-    touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(*(touch->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->fingers[index]) = *finger;
+    touch->num_fingers++;
 
     return index;
 }
@@ -323,6 +321,7 @@
 	nf.ydelta = 0;
 	nf.last_x = x;
 	nf.last_y = y;
+	nf.last_pressure = pressure;
 	SDL_AddFinger(touch,&nf);
 
 	posted = 0;
@@ -383,6 +382,7 @@
 	} 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*/
+	    if(pressure < 0) pressure = finger->last_pressure;
 	    xrel = x - finger->last_x;
 	    yrel = y - finger->last_y;
 	}
@@ -418,8 +418,8 @@
 	  touch->y = 0;
 	  }
 	*/
-	finger->xdelta += xrel;
-	finger->ydelta += yrel;
+	finger->xdelta = xrel;
+	finger->ydelta = yrel;
 	finger->pressure = pressure;
 	
 	
@@ -440,6 +440,7 @@
 	}
 	finger->last_x = finger->x;
 	finger->last_y = finger->y;
+	finger->last_pressure = finger->pressure;
 	return posted;
     }
 }
--- a/src/events/SDL_touch_c.h	Mon May 31 00:24:37 2010 -0400
+++ b/src/events/SDL_touch_c.h	Tue Jun 01 02:54:33 2010 -0400
@@ -20,52 +20,11 @@
     slouken@libsdl.org
 */
 #include "SDL_config.h"
+#include "../../include/SDL_touch.h"
 
 #ifndef _SDL_touch_c_h
 #define _SDL_touch_c_h
 
-typedef struct SDL_Touch SDL_Touch;
-typedef struct SDL_Finger SDL_Finger;
-
-struct SDL_Finger {
-  int id;
-  int x;
-  int y;
-  int z;                      /* for future use */
-  int xdelta;
-  int ydelta;
-  int last_x, last_y;         /* the last reported x and y coordinates */
-  int pressure;
-};
-
-
-struct SDL_Touch
-{
-  
-  /* Free the touch when it's time */
-  void (*FreeTouch) (SDL_Touch * touch);
-  
-  /* data common for tablets */
-  int pressure_max;
-  int pressure_min;
-  int tilt;                   /* for future use */
-  int rotation;               /* for future use */
-  
-  /* Data common to all touch */
-  int id;
-  SDL_Window *focus;
-  
-  char *name;
-  Uint8 buttonstate;
-  SDL_bool relative_mode;
-  SDL_bool flush_motion;
-
-  int num_fingers;
-  int max_fingers;
-  SDL_Finger** fingers;
-    
-  void *driverdata;
-};
 
 
 /* Initialize the touch subsystem */
--- a/src/video/x11/SDL_eventtouch.c	Mon May 31 00:24:37 2010 -0400
+++ b/src/video/x11/SDL_eventtouch.c	Tue Jun 01 02:54:33 2010 -0400
@@ -53,7 +53,9 @@
 	touch.pressure_max = 0;
 	touch.pressure_min = 0;
 	touch.id = event; 
-    
+
+	
+
 	
 
 	touch.driverdata = SDL_malloc(sizeof(EventTouchData));
@@ -64,6 +66,24 @@
 				 O_RDONLY | O_NONBLOCK);
 	ioctl (data->eventStream, EVIOCGNAME (sizeof (tstr)), tstr);
 	printf ("Reading From : %s\n", tstr);
+
+
+
+	int abs[5];
+	ioctl(data->eventStream,EVIOCGABS(0),abs);	
+	touch.x_min = abs[1];
+	touch.x_max = abs[2];
+	touch.xres = touch.x_max - touch.x_min;
+	ioctl(data->eventStream,EVIOCGABS(ABS_Y),abs);	
+	touch.y_min = abs[1];
+	touch.y_max = abs[2];
+	touch.yres = touch.y_max - touch.y_min;
+	ioctl(data->eventStream,EVIOCGABS(ABS_PRESSURE),abs);	
+	touch.pressure_min = abs[1];
+	touch.pressure_max = abs[2];
+	touch.pressureres = touch.pressure_max - touch.pressure_min;
+
+
 	SDL_AddTouch(&touch, tstr);
 
       }
--- a/src/video/x11/SDL_x11events.c	Mon May 31 00:24:37 2010 -0400
+++ b/src/video/x11/SDL_x11events.c	Tue Jun 01 02:54:33 2010 -0400
@@ -449,6 +449,7 @@
 			    break;
 			case ABS_PRESSURE:
 			    data->pressure = ev[i].value;
+			    if(data->pressure < 0) data->pressure = 0;
 			    break;
 			case ABS_MISC:
 			    data->up = SDL_TRUE;
@@ -461,7 +462,7 @@
 			data->finger = ev[i].value;
 		    break;
 		case EV_SYN:
-  		    printf("Id: %i\n",touch->id); 
+		  //printf("Id: %i\n",touch->id); 
 		    if(data->x >= 0 || data->y >= 0)
 			SDL_SendTouchMotion(touch->id,data->finger, 
 					    SDL_FALSE,data->x,data->y,
--- a/touchTest/parseDevicesTest.c	Mon May 31 00:24:37 2010 -0400
+++ b/touchTest/parseDevicesTest.c	Tue Jun 01 02:54:33 2010 -0400
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <linux/input.h>
+#include <fcntl.h>
 
 
 int main(int agrc,char **argv)
@@ -22,7 +23,23 @@
 	sprintf(tstr,"/dev/input/event%i",event);
 	printf("At location: %s\n",tstr);
 		
+	int inFile = open(tstr,O_RDONLY);
 	
+	unsigned long bits[4];
+	int abs[5];
+	ioctl(inFile,EVIOCGABS(ABS_X),abs);	
+	int minx,maxx,miny,maxy,minp,maxp;
+	minx = abs[1];
+	maxx = abs[2];
+	ioctl(inFile,EVIOCGABS(ABS_Y),abs);	
+	miny = abs[1];
+	maxy = abs[2];
+	ioctl(inFile,EVIOCGABS(ABS_PRESSURE),abs);	
+	minp = abs[1];
+	maxp = abs[2];
+	
+
+	close(inFile);
       }
       vendor = -1;
       product = -1;
--- a/touchTest/touchTest.c	Mon May 31 00:24:37 2010 -0400
+++ b/touchTest/touchTest.c	Tue Jun 01 02:54:33 2010 -0400
@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include <SDL.h>
 #include <math.h>
-#include "../src/events/SDL_touch_c.h" //BAD!!!
+#include <SDL_touch.h>
 
 #define PI 3.1415926535897
 #define WIDTH 640
@@ -19,12 +19,12 @@
 
 
 typedef struct {
-  int x,y;
+  float x,y;
 } Point;
 
 typedef struct {
   Point p;
-  int pressure;
+  float pressure;
 } Finger;
 
 
@@ -32,7 +32,7 @@
 
 void handler (int sig)
 {
-  printf ("\nexiting...(%d)\n", sig);
+  printf ("\exiting...(%d)\n", sig);
   exit (0);
 }
 
@@ -61,8 +61,17 @@
 {
 
   float a;
-  for(a=0;a<PI/2;a+=1.f/(float)r)
+  int tx;
+  for(a=0;a<PI/2;a+=1.f/(float)abs(r))
   {
+    if(r > 0) { //r<0 ==> unfilled circle
+      for(tx=x-r*cos(a);tx<x+r*cos(a);tx++) {
+	setpix(screen,tx,(int)(y+r*sin(a)),c);
+	setpix(screen,tx,(int)(y-r*sin(a)),c);
+      }
+    }
+    
+    //Always Draw Outline
     setpix(screen,(int)(x+r*cos(a)),(int)(y+r*sin(a)),c);
     setpix(screen,(int)(x-r*cos(a)),(int)(y+r*sin(a)),c);
     setpix(screen,(int)(x+r*cos(a)),(int)(y-r*sin(a)),c);
@@ -89,12 +98,17 @@
 	  setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
         }
     }
-  drawCircle(screen,mousx,mousy,30,0xFFFFFF);
+  drawCircle(screen,mousx,mousy,-30,0xFFFFFF);
   
   int i;
   for(i=0;i<MAXFINGERS;i++)
     if(finger[i].p.x >= 0 && finger[i].p.y >= 0)
-      drawCircle(screen,finger[i].p.x,finger[i].p.y,20,0xFF6600-finger[i].pressure);
+      if(finger[i].pressure > 0)
+	drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h
+		   ,20,0xFF*finger[i].pressure);
+      else
+	drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h
+		   ,20,0xFF);
   
   if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
   
@@ -162,14 +176,28 @@
 	    bstatus &= ~(1<<(event.button.button-1));
 	    break;
 	  case SDL_FINGERMOTION:    
-	    
+	    ;
 	    //printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
 	    //	   event.tfinger.x,event.tfinger.y);
-	    finger[event.tfinger.fingerId].p.x = event.tfinger.x;
-	    finger[event.tfinger.fingerId].p.y = event.tfinger.y;
-	    finger[event.tfinger.fingerId].pressure = event.tfinger.pressure;
-	    printf("Finger: %i, pressure: %i\n",event.tfinger.fingerId,
-		   event.tfinger.pressure);
+	    SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId);
+	    SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId);
+	    
+	    finger[event.tfinger.fingerId].p.x = ((float)event.tfinger.x)/
+	      inTouch->xres;
+	    finger[event.tfinger.fingerId].p.y = ((float)event.tfinger.y)/
+	      inTouch->yres;
+
+	    finger[event.tfinger.fingerId].pressure = 
+	      ((float)event.tfinger.pressure)/inTouch->pressureres;
+
+	    printf("Finger: %i, Pressure: %f Pressureres: %i\n",
+		   event.tfinger.fingerId,
+		   finger[event.tfinger.fingerId].pressure,
+		   inTouch->pressureres);
+	    //printf("Finger: %i, pressure: %f\n",event.tfinger.fingerId,
+	    //   finger[event.tfinger.fingerId].pressure);
+
+	    
 	    break;	    
 	  case SDL_FINGERDOWN:
 	    printf("Figner: %i down - x: %i, y: %i\n",event.tfinger.fingerId,