From 02f0a3b7c364ba7a011b7137de124d2c261012d7 Mon Sep 17 00:00:00 2001 From: Jim Grandpre Date: Tue, 1 Jun 2010 02:54:33 -0400 Subject: [PATCH] Added include/touch.h Now reading in resolution of touch pad. --- include/SDL_touch.h | 118 +++++++++++++++++++++++++++++++++ src/events/SDL_touch.c | 51 +++++++------- src/events/SDL_touch_c.h | 43 +----------- src/video/x11/SDL_eventtouch.c | 22 +++++- src/video/x11/SDL_x11events.c | 3 +- touchTest/parseDevicesTest.c | 17 +++++ touchTest/touchTest.c | 54 +++++++++++---- 7 files changed, 226 insertions(+), 82 deletions(-) create mode 100644 include/SDL_touch.h diff --git a/include/SDL_touch.h b/include/SDL_touch.h new file mode 100644 index 000000000..359748ccb --- /dev/null +++ b/include/SDL_touch.h @@ -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: */ diff --git a/src/events/SDL_touch.c b/src/events/SDL_touch.c index 0b4957701..571d3d4af 100644 --- a/src/events/SDL_touch.c +++ b/src/events/SDL_touch.c @@ -133,9 +133,9 @@ SDL_AddTouch(const SDL_Touch * touch, char *name) 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 @@ SDL_AddFinger(SDL_Touch* touch,SDL_Finger* finger) 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; + if (!fingers) { + SDL_OutOfMemory(); + return -1; + } + else { + touch->max_fingers = touch->num_fingers+1; + touch->fingers = fingers; + } } - touch->fingers = fingers; index = touch->num_fingers; - touch->num_fingers++; - touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(*(touch->fingers[index]))); + //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->fingers[index]) = *finger; + touch->num_fingers++; return index; } @@ -323,6 +321,7 @@ SDL_SendFingerDown(int id, int fingerid, SDL_bool down, int x, int y, int pressu 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 @@ SDL_SendTouchMotion(int id, int fingerid, int relative, } 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 @@ SDL_SendTouchMotion(int id, int fingerid, int relative, touch->y = 0; } */ - finger->xdelta += xrel; - finger->ydelta += yrel; + finger->xdelta = xrel; + finger->ydelta = yrel; finger->pressure = pressure; @@ -440,6 +440,7 @@ SDL_SendTouchMotion(int id, int fingerid, int relative, } finger->last_x = finger->x; finger->last_y = finger->y; + finger->last_pressure = finger->pressure; return posted; } } diff --git a/src/events/SDL_touch_c.h b/src/events/SDL_touch_c.h index eb2a0cbbb..1c61bc948 100644 --- a/src/events/SDL_touch_c.h +++ b/src/events/SDL_touch_c.h @@ -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 */ diff --git a/src/video/x11/SDL_eventtouch.c b/src/video/x11/SDL_eventtouch.c index 691169e56..1c74779b3 100644 --- a/src/video/x11/SDL_eventtouch.c +++ b/src/video/x11/SDL_eventtouch.c @@ -53,7 +53,9 @@ X11_InitTouch(_THIS) touch.pressure_max = 0; touch.pressure_min = 0; touch.id = event; - + + + touch.driverdata = SDL_malloc(sizeof(EventTouchData)); @@ -64,6 +66,24 @@ X11_InitTouch(_THIS) 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); } diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c index 8c5ec04c8..85db7725f 100644 --- a/src/video/x11/SDL_x11events.c +++ b/src/video/x11/SDL_x11events.c @@ -449,6 +449,7 @@ X11_PumpEvents(_THIS) 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 @@ X11_PumpEvents(_THIS) 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, diff --git a/touchTest/parseDevicesTest.c b/touchTest/parseDevicesTest.c index 85d4ffef3..28ef82e06 100644 --- a/touchTest/parseDevicesTest.c +++ b/touchTest/parseDevicesTest.c @@ -1,6 +1,7 @@ #include #include #include +#include int main(int agrc,char **argv) @@ -22,7 +23,23 @@ int main(int agrc,char **argv) 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; diff --git a/touchTest/touchTest.c b/touchTest/touchTest.c index f169c3574..b77a00e5c 100644 --- a/touchTest/touchTest.c +++ b/touchTest/touchTest.c @@ -1,7 +1,7 @@ #include #include #include -#include "../src/events/SDL_touch_c.h" //BAD!!! +#include #define PI 3.1415926535897 #define WIDTH 640 @@ -19,12 +19,12 @@ int bstatus; typedef struct { - int x,y; + float x,y; } Point; typedef struct { Point p; - int pressure; + float pressure; } Finger; @@ -32,7 +32,7 @@ Finger finger[MAXFINGERS]; void handler (int sig) { - printf ("\nexiting...(%d)\n", sig); + printf ("\exiting...(%d)\n", sig); exit (0); } @@ -61,8 +61,17 @@ void drawCircle(SDL_Surface* screen,int x,int y,int r,int c) { float a; - for(a=0;a 0) { //r<0 ==> unfilled circle + for(tx=x-r*cos(a);tx= 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 @@ int main(int argc, char* argv[]) 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,