src/video/x11/SDL_eventtouch.c
author jimtla
Thu, 22 Jul 2010 08:12:28 +0400
changeset 4669 62e6a6e9720b
parent 4646 eea1bf53effa
child 4678 f8431f66613d
permissions -rw-r--r--
Fixed x11 compile bugs.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 #include "SDL_x11video.h"
    24 #include "SDL_eventtouch.h"
    25 #include "../../events/SDL_touch_c.h"
    26 
    27 #ifdef HAVE_LINUX_INPUT_H
    28 #include <linux/input.h>
    29 #include <fcntl.h>
    30 #endif
    31 
    32 void
    33 X11_InitTouch(_THIS)
    34 {
    35 #ifdef HAVE_LINUX_INPUT_H
    36   printf("Initializing touch...\n");
    37 
    38   FILE *fd;
    39   fd = fopen("/proc/bus/input/devices","r");
    40   
    41   char c;
    42   int i = 0;
    43   char line[256];
    44   char tstr[256];
    45   int vendor = -1,product = -1,event = -1;
    46   while(!feof(fd)) {
    47     if(fgets(line,256,fd) <=0) continue;
    48     //printf("%s",line);
    49     if(line[0] == '\n') {
    50       if(vendor == 1386){
    51 	printf("Wacom... Assuming it is a touch device\n");
    52 	sprintf(tstr,"/dev/input/event%i",event);
    53 	printf("At location: %s\n",tstr);
    54 
    55 	SDL_Touch touch;
    56 	touch.pressure_max = 0;
    57 	touch.pressure_min = 0;
    58 	touch.id = event; 
    59 
    60 	
    61 
    62 	
    63 
    64 	touch.driverdata = SDL_malloc(sizeof(EventTouchData));
    65 	EventTouchData* data = (EventTouchData*)(touch.driverdata);
    66 	printf("Opening device...\n");
    67 	//printf("New Touch - DataPtr: %i\n",data);
    68 	data->eventStream = open(tstr, 
    69 				 O_RDONLY | O_NONBLOCK);
    70 	ioctl (data->eventStream, EVIOCGNAME (sizeof (tstr)), tstr);
    71 	printf ("Reading From : %s\n", tstr);
    72 
    73 
    74 
    75 	int abs[5];
    76 	ioctl(data->eventStream,EVIOCGABS(0),abs);	
    77 	touch.x_min = abs[1];
    78 	touch.x_max = abs[2];
    79 	touch.xres = touch.x_max - touch.x_min;
    80 	ioctl(data->eventStream,EVIOCGABS(ABS_Y),abs);	
    81 	touch.y_min = abs[1];
    82 	touch.y_max = abs[2];
    83 	touch.yres = touch.y_max - touch.y_min;
    84 	ioctl(data->eventStream,EVIOCGABS(ABS_PRESSURE),abs);	
    85 	touch.pressure_min = abs[1];
    86 	touch.pressure_max = abs[2];
    87 	touch.pressureres = touch.pressure_max - touch.pressure_min;
    88 
    89 
    90 	SDL_AddTouch(&touch, tstr);
    91 
    92       }
    93       vendor = -1;
    94       product = -1;
    95       event = -1;      
    96     }
    97     else if(line[0] == 'I') {
    98       i = 1;
    99       while(line[i]) {
   100 	sscanf(&line[i],"Vendor=%x",&vendor);
   101 	sscanf(&line[i],"Product=%x",&product);
   102 	i++;
   103       }
   104     }
   105     else if(line[0] == 'H') {
   106       i = 1;
   107       while(line[i]) {
   108 	sscanf(&line[i],"event%d",&event);
   109 	i++;
   110       }
   111     }
   112   }
   113   
   114   close(fd);
   115 #endif
   116 }
   117 
   118 void
   119 X11_QuitTouch(_THIS)
   120 {
   121     SDL_TouchQuit();
   122 }
   123 
   124 /* vi: set ts=4 sw=4 expandtab: */