Mercurial > sdl-ios-xcode
changeset 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 |
files | include/SDL_events.h src/events/SDL_events.c src/events/SDL_touch.c src/events/SDL_touch_c.h src/video/x11/SDL_eventtouch.h src/video/x11/SDL_x11events.c src/video/x11/SDL_x11video.h touchTest/makefile touchTest/touchPong touchTest/touchSimp touchTest/touchSimp.c touchTest/touchTest.c |
diffstat | 12 files changed, 407 insertions(+), 88 deletions(-) [+] |
line wrap: on
line diff
--- a/include/SDL_events.h Thu May 27 01:21:37 2010 -0400 +++ b/include/SDL_events.h Fri May 28 01:26:52 2010 -0400 @@ -310,6 +310,8 @@ Uint8 state; /**< The current button state */ Uint8 fingerId; Uint8 padding1; + int x; + int y; } SDL_TouchFingerEvent;
--- a/src/events/SDL_events.c Thu May 27 01:21:37 2010 -0400 +++ b/src/events/SDL_events.c Fri May 28 01:26:52 2010 -0400 @@ -254,6 +254,7 @@ retcode = 0; retcode += SDL_KeyboardInit(); retcode += SDL_MouseInit(); + retcode += SDL_TouchInit(); retcode += SDL_QuitInit(); if (retcode < 0) { /* We don't expect them to fail, but... */
--- 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;
--- a/src/events/SDL_touch_c.h Thu May 27 01:21:37 2010 -0400 +++ b/src/events/SDL_touch_c.h Fri May 28 01:26:52 2010 -0400 @@ -51,9 +51,6 @@ int tilt; /* for future use */ int rotation; /* for future use */ - int total_ends; - int current_end; - /* Data common to all touch */ int id; SDL_Window *focus; @@ -73,14 +70,23 @@ /* Initialize the touch subsystem */ extern int SDL_TouchInit(void); -/* Get the touch at an index */ -extern SDL_Touch *SDL_GetTouch(int index); +/*Get the touch at an index */ +extern SDL_Touch *SDL_GetTouchIndex(int index); + +/* Get the touch with id = id */ +extern SDL_Touch *SDL_GetTouch(int 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,int 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, - int pressure_max, int pressure_min, int ends); + 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(int index);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/video/x11/SDL_eventtouch.h Fri May 28 01:26:52 2010 -0400 @@ -0,0 +1,42 @@ +/* + 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 +*/ +#include "SDL_config.h" + +#ifndef _SDL_eventtouch_h +#define _SDL_eventtouch_h + + +//What should this be? +#if SDL_VIDEO_DRIVER_X11_XINPUT +typedef struct EventTouchData +{ + int x,y,pressure,finger; //Temporary Variables until sync + int eventStream; +} EventTouchData; +#endif + +//extern void X11_InitMouse(_THIS); +//extern void X11_QuitMouse(_THIS); + +#endif /* _SDL_eventtouch_h */ + +/* vi: set ts=4 sw=4 expandtab: */
--- a/src/video/x11/SDL_x11events.c Thu May 27 01:21:37 2010 -0400 +++ b/src/video/x11/SDL_x11events.c Fri May 28 01:26:52 2010 -0400 @@ -28,9 +28,16 @@ #include "SDL_x11video.h" #include "../../events/SDL_events_c.h" #include "../../events/SDL_mouse_c.h" +#include "../../events/SDL_touch_c.h" #include "SDL_syswm.h" +#include <stdio.h> + +//Touch Input/event* includes +#include <linux/input.h> +#include <fcntl.h> + static void X11_DispatchEvent(_THIS) { @@ -410,8 +417,65 @@ } - /* Process Touch events - TODO When X gets touch support, REMOVE THIS*/ - + /* Process Touch events - TODO When X gets touch support, use that instead*/ + int i = 0,rd; + char * name[256]; + struct input_event ev[64]; + int size = sizeof (struct input_event); + static int initd = 0; //TODO - HACK! + for(i = 0;i < SDL_GetNumTouch();++i) { + SDL_Touch* touch = SDL_GetTouchIndex(i); + if(!touch) printf("Touch %i/%i DNE\n",i,SDL_GetNumTouch()); + EventTouchData* data; + if(!initd){//data->eventStream <= 0) { + touch->driverdata = SDL_malloc(sizeof(EventTouchData)); + data = (EventTouchData*)(touch->driverdata); + printf("Openning device...\n"); + data->eventStream = open("/dev/input/wacom-touch", + O_RDONLY | O_NONBLOCK); + ioctl (data->eventStream, EVIOCGNAME (sizeof (name)), name); + printf ("Reading From : %s\n", name); + initd = 1; + } + else + data = (EventTouchData*)(touch->driverdata); + if(data->eventStream <= 0) + printf("Error: Couldn't open stream\n"); + rd = read(data->eventStream, ev, size * 64); + //printf("Got %i/%i bytes\n",rd,size); + if(rd >= size) { + for (i = 0; i < rd / sizeof(struct input_event); i++) { + switch (ev[i].type) { + case EV_ABS: + //printf("Got position x: %i!\n",data->x); + if(ev[i].code == ABS_X) + data->x = ev[i].value; + else if (ev[i].code == ABS_Y) + data->y = ev[i].value; + break; + case EV_MSC: + if(ev[i].code == MSC_SERIAL) + data->finger = ev[i].value; + break; + case EV_SYN: + data->finger -= 1; /*Wacom indexes fingers from 1, + I index from 0*/ + if(data->x >= 0 || data->y >= 0) + SDL_SendTouchMotion(touch->id,data->finger, + SDL_FALSE,data->x,data->y, + data->pressure); + + //printf("Synched: %i tx: %i, ty: %i\n", + // data->finger,data->x,data->y); + data->x = -1; + data->y = -1; + data->pressure = -1; + + break; + } + } + } + } } /* This is so wrong it hurts */
--- a/src/video/x11/SDL_x11video.h Thu May 27 01:21:37 2010 -0400 +++ b/src/video/x11/SDL_x11video.h Fri May 28 01:26:52 2010 -0400 @@ -53,6 +53,7 @@ #include "SDL_x11keyboard.h" #include "SDL_x11modes.h" #include "SDL_x11mouse.h" +#include "SDL_eventtouch.h" #include "SDL_x11opengl.h" #include "SDL_x11window.h"
--- a/touchTest/makefile Thu May 27 01:21:37 2010 -0400 +++ b/touchTest/makefile Fri May 28 01:26:52 2010 -0400 @@ -1,3 +1,5 @@ SDLTest : touchSimp.c touchPong.c + gcc touchTest.c -o touchTest `sdl-config --cflags --libs` -g gcc touchSimp.c -o touchSimp `sdl-config --cflags --libs` -g gcc touchPong.c -o touchPong `sdl-config --cflags --libs` -g +
--- a/touchTest/touchSimp.c Thu May 27 01:21:37 2010 -0400 +++ b/touchTest/touchSimp.c Fri May 28 01:26:52 2010 -0400 @@ -186,6 +186,10 @@ case SDL_MOUSEBUTTONUP: bstatus &= ~(1<<(event.button.button-1)); break; + case SDL_FINGERMOTION: + printf("Holy SH!T\n"); + break; + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/touchTest/touchTest.c Fri May 28 01:26:52 2010 -0400 @@ -0,0 +1,168 @@ +#include <stdio.h> +#include <SDL.h> +#include <math.h> +#include "../src/events/SDL_touch_c.h" //BAD!!! + +#define PI 3.1415926535897 +#define WIDTH 640 +#define HEIGHT 480 +#define BPP 4 +#define DEPTH 32 + +int mousx,mousy; +int keystat[512]; +int bstatus; + + + + +typedef struct { + int x,y; +} Point; + +void handler (int sig) +{ + printf ("\nexiting...(%d)\n", sig); + exit (0); +} + +void perror_exit (char *error) +{ + perror (error); + handler (9); +} + + +void setpix(SDL_Surface *screen, int x, int y, int col) +{ + Uint32 *pixmem32; + Uint32 colour; + + if((unsigned)x > screen->w) return; + if((unsigned)y > screen->h) return; + + colour = SDL_MapRGB( screen->format, (col>>16)&0xFF, (col>>8)&0xFF, col&0xFF); + + pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x; + *pixmem32 = colour; +} + +void drawCircle(SDL_Surface* screen,int x,int y,int r,int c) +{ + + float a; + for(a=0;a<2*PI;a+=1.f/(float)r) + { + setpix(screen,(int)(x+r*cos(a)),(int)(y+r*sin(a)),c); + } +} + +void DrawScreen(SDL_Surface* screen, int h) +{ + int x, y, xm,ym,c; + if(SDL_MUSTLOCK(screen)) + { + if(SDL_LockSurface(screen) < 0) return; + } + for(y = 0; y < screen->h; y++ ) + { + for( x = 0; x < screen->w; x++ ) + { + //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h); + //xm = (x+h)%screen->w; + //ym = (y+h)%screen->w; + //c = sin(h/256*2*PI)*x*y/screen->w/screen->h; + //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c); + setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255); + } + } + drawCircle(screen,mousx,mousy,30,0xFFFFFF); + + + if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); + + SDL_Flip(screen); +} + +SDL_Surface* initScreen(int width,int height) +{ + return SDL_SetVideoMode(width, height, DEPTH, + SDL_HWSURFACE | SDL_RESIZABLE); +} + +int main(int argc, char* argv[]) +{ + SDL_Surface *screen; + SDL_Event event; + + int keypress = 0; + int h=0,s=1,i,j; + + memset(keystat,0,512*sizeof(keystat[0])); + if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; + + if (!(screen = initScreen(WIDTH,HEIGHT))) + { + SDL_Quit(); + return 1; + } + + while(!keystat[27]) { + //Poll SDL + while(SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + keystat[27] = 1; + break; + case SDL_KEYDOWN: + //printf("%i\n",event.key.keysym.sym); + keystat[event.key.keysym.sym] = 1; + //keypress = 1; + break; + case SDL_KEYUP: + //printf("%i\n",event.key.keysym.sym); + keystat[event.key.keysym.sym] = 0; + //keypress = 1; + break; + case SDL_VIDEORESIZE: + if (!(screen = initScreen(event.resize.w, + event.resize.h))) + { + SDL_Quit(); + return 1; + } + break; + case SDL_MOUSEMOTION: + mousx = event.motion.x; + mousy = event.motion.y; + break; + case SDL_MOUSEBUTTONDOWN: + bstatus |= (1<<(event.button.button-1)); + break; + case SDL_MOUSEBUTTONUP: + bstatus &= ~(1<<(event.button.button-1)); + break; + case SDL_FINGERMOTION: + i = 1; + + + printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId, + event.tfinger.x,event.tfinger.y); + + break; + } + } + //And draw + DrawScreen(screen,h); + /* + for(i=0;i<512;i++) + if(keystat[i]) printf("%i\n",i); + printf("Buttons:%i\n",bstatus); + */ + } + SDL_Quit(); + + return 0; +}