Auto-detects Wacom touch devices.
authorJim Grandpre <jim.tla@gmail.com>
Mon, 31 May 2010 00:24:37 -0400
changeset 46450375d020e7e3
parent 4644 fb500b3e1717
child 4646 eea1bf53effa
Auto-detects Wacom touch devices.
src/events/SDL_touch.c
src/events/SDL_touch_c.h
src/video/x11/SDL_eventtouch.c
src/video/x11/SDL_eventtouch.h
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11video.c
touchTest/makefile
touchTest/parseDevicesTest.c
     1.1 --- a/src/events/SDL_touch.c	Sat May 29 02:09:16 2010 -0400
     1.2 +++ b/src/events/SDL_touch.c	Mon May 31 00:24:37 2010 -0400
     1.3 @@ -36,12 +36,7 @@
     1.4  int
     1.5  SDL_TouchInit(void)
     1.6  {
     1.7 -    SDL_Touch touch;
     1.8 -    touch.pressure_max = 0;
     1.9 -    touch.pressure_min = 0;
    1.10 -    touch.id = 0; //Should be function? 
    1.11      
    1.12 -    SDL_AddTouch(&touch, "Touch1");
    1.13      return (0);
    1.14  }
    1.15  
    1.16 @@ -138,6 +133,9 @@
    1.17      SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);   
    1.18  
    1.19      SDL_touchPads[index]->num_fingers = 0;
    1.20 +    SDL_touchPads[index]->max_fingers = 0;
    1.21 +    SDL_touchPads[index]->fingers = NULL;
    1.22 +    
    1.23      SDL_touchPads[index]->buttonstate = 0;
    1.24      SDL_touchPads[index]->relative_mode = SDL_FALSE;
    1.25      SDL_touchPads[index]->flush_motion = SDL_FALSE;
    1.26 @@ -261,16 +259,27 @@
    1.27  	}
    1.28  
    1.29      /* Add the touch to the list of touch */
    1.30 -    fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
    1.31 -					  (touch->num_fingers + 1) * sizeof(*touch));
    1.32 +    if(touch->num_fingers >= touch->max_fingers){
    1.33 +      if(fingers == NULL) {
    1.34 +	touch->max_fingers = 1;
    1.35 +	fingers = (SDL_Finger **) SDL_malloc(sizeof(finger));
    1.36 +      }
    1.37 +      else {
    1.38 +	fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
    1.39 +				     (touch->num_fingers + 1) * sizeof(finger));
    1.40 +	touch->max_fingers = touch->num_fingers+1;
    1.41 +      }
    1.42 +
    1.43 +    }
    1.44 +
    1.45      if (!fingers) {
    1.46          SDL_OutOfMemory();
    1.47          return -1;
    1.48      }
    1.49  
    1.50      touch->fingers = fingers;
    1.51 -    index = touch->num_fingers++;
    1.52 -
    1.53 +    index = touch->num_fingers;
    1.54 +    touch->num_fingers++;
    1.55      touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(*(touch->fingers[index])));
    1.56      if (!touch->fingers[index]) {
    1.57          SDL_OutOfMemory();
     2.1 --- a/src/events/SDL_touch_c.h	Sat May 29 02:09:16 2010 -0400
     2.2 +++ b/src/events/SDL_touch_c.h	Mon May 31 00:24:37 2010 -0400
     2.3 @@ -61,6 +61,7 @@
     2.4    SDL_bool flush_motion;
     2.5  
     2.6    int num_fingers;
     2.7 +  int max_fingers;
     2.8    SDL_Finger** fingers;
     2.9      
    2.10    void *driverdata;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/video/x11/SDL_eventtouch.c	Mon May 31 00:24:37 2010 -0400
     3.3 @@ -0,0 +1,100 @@
     3.4 +/*
     3.5 +    SDL - Simple DirectMedia Layer
     3.6 +    Copyright (C) 1997-2010 Sam Lantinga
     3.7 +
     3.8 +    This library is free software; you can redistribute it and/or
     3.9 +    modify it under the terms of the GNU Lesser General Public
    3.10 +    License as published by the Free Software Foundation; either
    3.11 +    version 2.1 of the License, or (at your option) any later version.
    3.12 +
    3.13 +    This library is distributed in the hope that it will be useful,
    3.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.16 +    Lesser General Public License for more details.
    3.17 +
    3.18 +    You should have received a copy of the GNU Lesser General Public
    3.19 +    License along with this library; if not, write to the Free Software
    3.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    3.21 +
    3.22 +    Sam Lantinga
    3.23 +    slouken@libsdl.org
    3.24 +*/
    3.25 +#include "SDL_config.h"
    3.26 +#include "SDL_x11video.h"
    3.27 +#include "SDL_eventtouch.h"
    3.28 +#include "../../events/SDL_touch_c.h"
    3.29 +
    3.30 +#include <linux/input.h>
    3.31 +#include <fcntl.h>
    3.32 +
    3.33 +void
    3.34 +X11_InitTouch(_THIS)
    3.35 +{
    3.36 +  printf("Initializing touch...\n");
    3.37 +
    3.38 +  FILE *fd;
    3.39 +  fd = fopen("/proc/bus/input/devices","r");
    3.40 +  
    3.41 +  char c;
    3.42 +  int i = 0;
    3.43 +  char line[256];
    3.44 +  char tstr[256];
    3.45 +  int vendor = -1,product = -1,event = -1;
    3.46 +  while(!feof(fd)) {
    3.47 +    if(fgets(line,256,fd) <=0) continue;
    3.48 +    //printf("%s",line);
    3.49 +    if(line[0] == '\n') {
    3.50 +      if(vendor == 1386){
    3.51 +	printf("Wacom... Assuming it is a touch device\n");
    3.52 +	sprintf(tstr,"/dev/input/event%i",event);
    3.53 +	printf("At location: %s\n",tstr);
    3.54 +
    3.55 +	SDL_Touch touch;
    3.56 +	touch.pressure_max = 0;
    3.57 +	touch.pressure_min = 0;
    3.58 +	touch.id = event; 
    3.59 +    
    3.60 +	
    3.61 +
    3.62 +	touch.driverdata = SDL_malloc(sizeof(EventTouchData));
    3.63 +	EventTouchData* data = (EventTouchData*)(touch.driverdata);
    3.64 +	printf("Opening device...\n");
    3.65 +	//printf("New Touch - DataPtr: %i\n",data);
    3.66 +	data->eventStream = open(tstr, 
    3.67 +				 O_RDONLY | O_NONBLOCK);
    3.68 +	ioctl (data->eventStream, EVIOCGNAME (sizeof (tstr)), tstr);
    3.69 +	printf ("Reading From : %s\n", tstr);
    3.70 +	SDL_AddTouch(&touch, tstr);
    3.71 +
    3.72 +      }
    3.73 +      vendor = -1;
    3.74 +      product = -1;
    3.75 +      event = -1;      
    3.76 +    }
    3.77 +    else if(line[0] == 'I') {
    3.78 +      i = 1;
    3.79 +      while(line[i]) {
    3.80 +	sscanf(&line[i],"Vendor=%x",&vendor);
    3.81 +	sscanf(&line[i],"Product=%x",&product);
    3.82 +	i++;
    3.83 +      }
    3.84 +    }
    3.85 +    else if(line[0] == 'H') {
    3.86 +      i = 1;
    3.87 +      while(line[i]) {
    3.88 +	sscanf(&line[i],"event%d",&event);
    3.89 +	i++;
    3.90 +      }
    3.91 +    }
    3.92 +  }
    3.93 +  
    3.94 +  close(fd);
    3.95 +}
    3.96 +
    3.97 +void
    3.98 +X11_QuitTouch(_THIS)
    3.99 +{
   3.100 +    SDL_TouchQuit();
   3.101 +}
   3.102 +
   3.103 +/* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/video/x11/SDL_eventtouch.h	Sat May 29 02:09:16 2010 -0400
     4.2 +++ b/src/video/x11/SDL_eventtouch.h	Mon May 31 00:24:37 2010 -0400
     4.3 @@ -35,8 +35,8 @@
     4.4  } EventTouchData;
     4.5  #endif
     4.6  
     4.7 -//extern void X11_InitMouse(_THIS);
     4.8 -//extern void X11_QuitMouse(_THIS);
     4.9 +extern void X11_InitTouch(_THIS);
    4.10 +extern void X11_QuitTouch(_THIS);
    4.11  
    4.12  #endif /* _SDL_eventtouch_h */
    4.13  
     5.1 --- a/src/video/x11/SDL_x11events.c	Sat May 29 02:09:16 2010 -0400
     5.2 +++ b/src/video/x11/SDL_x11events.c	Mon May 31 00:24:37 2010 -0400
     5.3 @@ -109,7 +109,6 @@
     5.4  #endif
     5.5          }
     5.6          break;
     5.7 -
     5.8          /* Losing mouse coverage? */
     5.9      case LeaveNotify:{
    5.10  #ifdef DEBUG_XEVENTS
    5.11 @@ -422,23 +421,16 @@
    5.12      char name[256];
    5.13      struct input_event ev[64];
    5.14      int size = sizeof (struct input_event);
    5.15 -    static int initd = 0; //TODO - HACK!
    5.16 +
    5.17      for(i = 0;i < SDL_GetNumTouch();++i) {
    5.18  	SDL_Touch* touch = SDL_GetTouchIndex(i);
    5.19  	if(!touch) printf("Touch %i/%i DNE\n",i,SDL_GetNumTouch());
    5.20  	EventTouchData* data;
    5.21 -	if(!initd){//data->eventStream <= 0) {
    5.22 -	    touch->driverdata = SDL_malloc(sizeof(EventTouchData));
    5.23 -	    data = (EventTouchData*)(touch->driverdata);
    5.24 -	    printf("Openning device...\n");
    5.25 -	    data->eventStream = open("/dev/input/wacom", 
    5.26 -				     O_RDONLY | O_NONBLOCK);
    5.27 -	    ioctl (data->eventStream, EVIOCGNAME (sizeof (name)), name);
    5.28 -	    printf ("Reading From : %s\n", name);
    5.29 -	    initd = 1;
    5.30 +	data = (EventTouchData*)(touch->driverdata);
    5.31 +	if(data == NULL) {
    5.32 +	  printf("No driver data\n");
    5.33 +	  continue;
    5.34  	}
    5.35 -	else
    5.36 -	 data = (EventTouchData*)(touch->driverdata);
    5.37  	if(data->eventStream <= 0) 
    5.38  	    printf("Error: Couldn't open stream\n");
    5.39  	rd = read(data->eventStream, ev, size * 64);
    5.40 @@ -469,6 +461,7 @@
    5.41  			data->finger = ev[i].value;
    5.42  		    break;
    5.43  		case EV_SYN:
    5.44 +  		    printf("Id: %i\n",touch->id); 
    5.45  		    if(data->x >= 0 || data->y >= 0)
    5.46  			SDL_SendTouchMotion(touch->id,data->finger, 
    5.47  					    SDL_FALSE,data->x,data->y,
     6.1 --- a/src/video/x11/SDL_x11video.c	Sat May 29 02:09:16 2010 -0400
     6.2 +++ b/src/video/x11/SDL_x11video.c	Mon May 31 00:24:37 2010 -0400
     6.3 @@ -23,6 +23,7 @@
     6.4  
     6.5  #include "SDL_video.h"
     6.6  #include "SDL_mouse.h"
     6.7 +#include "SDL_eventtouch.h" 
     6.8  #include "../SDL_sysvideo.h"
     6.9  #include "../SDL_pixels_c.h"
    6.10  
    6.11 @@ -269,6 +270,7 @@
    6.12      }
    6.13      X11_InitMouse(_this);
    6.14  
    6.15 +    X11_InitTouch(_this);
    6.16      return 0;
    6.17  }
    6.18  
    6.19 @@ -289,6 +291,7 @@
    6.20      X11_QuitModes(_this);
    6.21      X11_QuitKeyboard(_this);
    6.22      X11_QuitMouse(_this);
    6.23 +    X11_QuitTouch(_this);
    6.24  }
    6.25  
    6.26  SDL_bool
     7.1 --- a/touchTest/makefile	Sat May 29 02:09:16 2010 -0400
     7.2 +++ b/touchTest/makefile	Mon May 31 00:24:37 2010 -0400
     7.3 @@ -2,4 +2,6 @@
     7.4  	gcc touchTest.c -o touchTest `sdl-config --cflags --libs` -g		
     7.5  	gcc touchSimp.c -o touchSimp `sdl-config --cflags --libs` -g
     7.6  	gcc touchPong.c -o touchPong `sdl-config --cflags --libs` -g
     7.7 +	gcc parseDevicesTest.c -o parseDevicesTest -g
     7.8  
     7.9 +
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/touchTest/parseDevicesTest.c	Mon May 31 00:24:37 2010 -0400
     8.3 @@ -0,0 +1,50 @@
     8.4 +#include <stdio.h>
     8.5 +#include <stdlib.h>
     8.6 +#include <linux/input.h>
     8.7 +
     8.8 +
     8.9 +int main(int agrc,char **argv)
    8.10 +{
    8.11 +  FILE *fd;
    8.12 +  fd = fopen("/proc/bus/input/devices","r");
    8.13 +  
    8.14 +  char c;
    8.15 +  int i = 0;
    8.16 +  char line[256];
    8.17 +  char tstr[256];
    8.18 +  int vendor = -1,product = -1,event = -1;
    8.19 +  while(!feof(fd)) {
    8.20 +    fgets(line,256,fd);
    8.21 +    //printf("%s",line);
    8.22 +    if(line[0] == '\n') {
    8.23 +      if(vendor == 1386){
    8.24 +	printf("Wacom... Assuming it is a touch device\n");
    8.25 +	sprintf(tstr,"/dev/input/event%i",event);
    8.26 +	printf("At location: %s\n",tstr);
    8.27 +		
    8.28 +	
    8.29 +      }
    8.30 +      vendor = -1;
    8.31 +      product = -1;
    8.32 +      event = -1;      
    8.33 +    }
    8.34 +    else if(line[0] == 'I') {
    8.35 +      i = 1;
    8.36 +      while(line[i]) {
    8.37 +	sscanf(&line[i],"Vendor=%x",&vendor);
    8.38 +	sscanf(&line[i],"Product=%x",&product);
    8.39 +	i++;
    8.40 +      }
    8.41 +    }
    8.42 +    else if(line[0] == 'H') {
    8.43 +      i = 1;
    8.44 +      while(line[i]) {
    8.45 +	sscanf(&line[i],"event%d",&event);
    8.46 +	i++;
    8.47 +      }
    8.48 +    }
    8.49 +  }
    8.50 +  
    8.51 +  close(fd);
    8.52 +  return 0;
    8.53 +}