src/video/x11/SDL_x11touch.c
author Sam Lantinga
Tue, 27 Sep 2011 23:15:01 -0400
changeset 5967 ae97a61f002f
parent 5966 2edecf22a2e7
child 5968 b229328c7ad4
permissions -rw-r--r--
============================================================
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_X11
    24 
    25 #include "SDL_x11video.h"
    26 #include "SDL_x11touch.h"
    27 #include "../../events/SDL_touch_c.h"
    28 
    29 
    30 #ifdef SDL_INPUT_LINUXEV
    31 #include <linux/input.h>
    32 #include <fcntl.h>
    33 #endif
    34 
    35 void
    36 X11_InitTouch(_THIS)
    37 {
    38 #ifdef SDL_INPUT_LINUXEV
    39   FILE *fd;
    40   fd = fopen("/proc/bus/input/devices","r");
    41 
    42   char c;
    43   int i = 0;
    44   char line[256];
    45   char tstr[256];
    46   int vendor = -1,product = -1,event = -1;
    47   while(!feof(fd)) {
    48     if(fgets(line,256,fd) <=0) continue;
    49     if(line[0] == '\n') {
    50       if(vendor == 1386 || vendor==1) {
    51 
    52         sprintf(tstr,"/dev/input/event%i",event);
    53 
    54         int tsfd = open( tstr, O_RDONLY | O_NONBLOCK );
    55         if ( tsfd == -1 )
    56             continue;   /* Maybe not enough permissions ? */
    57 
    58         SDL_Touch touch;
    59         touch.pressure_max = 0;
    60         touch.pressure_min = 0;
    61         touch.id = event; 
    62 
    63         touch.driverdata = SDL_malloc(sizeof(EventTouchData));
    64         EventTouchData* data = (EventTouchData*)(touch.driverdata);
    65 
    66         data->x = -1;
    67         data->y = -1;
    68         data->pressure = -1;
    69         data->finger = 0;
    70         data->up = SDL_FALSE;
    71         data->down = SDL_FALSE;
    72 
    73         data->eventStream = tsfd;
    74         ioctl (data->eventStream, EVIOCGNAME (sizeof (tstr)), tstr);
    75 
    76         int abs[5];
    77         ioctl(data->eventStream,EVIOCGABS(0),abs);	
    78         touch.x_min = abs[1];
    79         touch.x_max = abs[2];
    80         touch.native_xres = touch.x_max - touch.x_min;
    81         ioctl(data->eventStream,EVIOCGABS(ABS_Y),abs);	
    82         touch.y_min = abs[1];
    83         touch.y_max = abs[2];
    84         touch.native_yres = touch.y_max - touch.y_min;
    85         ioctl(data->eventStream,EVIOCGABS(ABS_PRESSURE),abs);	
    86         touch.pressure_min = abs[1];
    87         touch.pressure_max = abs[2];
    88         touch.native_pressureres = touch.pressure_max - touch.pressure_min;
    89 
    90         SDL_AddTouch(&touch, tstr);
    91       }
    92       vendor = -1;
    93       product = -1;
    94       event = -1;      
    95     }
    96     else if(line[0] == 'I') {
    97       i = 1;
    98       while(line[i]) {
    99         sscanf(&line[i],"Vendor=%x",&vendor);
   100         sscanf(&line[i],"Product=%x",&product);
   101         i++;
   102       }
   103     }
   104     else if(line[0] == 'H') {
   105       i = 1;
   106       while(line[i]) {
   107         sscanf(&line[i],"event%d",&event);
   108         i++;
   109       }
   110     }
   111   }
   112 
   113   close(fd);
   114 #endif
   115 }
   116 
   117 void
   118 X11_QuitTouch(_THIS)
   119 {
   120     SDL_TouchQuit();
   121 }
   122 
   123 #endif /* SDL_VIDEO_DRIVER_X11 */
   124 
   125 /* vi: set ts=4 sw=4 expandtab: */