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