src/haptic/linux/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Sun, 01 Jun 2008 18:46:51 +0000
branchgsoc2008_force_feedback
changeset 2474 3f80bf1528b4
parent 2472 3f73c88c9abb
child 2475 4b874e3a3a2c
permissions -rw-r--r--
Properly quit the SDL_haptic subsystem.
Added SDL_Haptic dummy driver.
bobbens@2472
     1
/*
bobbens@2472
     2
    SDL - Simple DirectMedia Layer
bobbens@2472
     3
    Copyright (C) 2008 Edgar Simo
bobbens@2472
     4
bobbens@2472
     5
    This library is free software; you can redistribute it and/or
bobbens@2472
     6
    modify it under the terms of the GNU Lesser General Public
bobbens@2472
     7
    License as published by the Free Software Foundation; either
bobbens@2472
     8
    version 2.1 of the License, or (at your option) any later version.
bobbens@2472
     9
bobbens@2472
    10
    This library is distributed in the hope that it will be useful,
bobbens@2472
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bobbens@2472
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bobbens@2472
    13
    Lesser General Public License for more details.
bobbens@2472
    14
bobbens@2472
    15
    You should have received a copy of the GNU Lesser General Public
bobbens@2472
    16
    License along with this library; if not, write to the Free Software
bobbens@2472
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bobbens@2472
    18
bobbens@2472
    19
    Sam Lantinga
bobbens@2472
    20
    slouken@libsdl.org
bobbens@2472
    21
*/
bobbens@2472
    22
#include "SDL_config.h"
bobbens@2472
    23
bobbens@2472
    24
#ifdef SDL_JOYSTICK_LINUX
bobbens@2472
    25
bobbens@2472
    26
#include "SDL_haptic.h"
bobbens@2472
    27
#include "../SDL_haptic_c.h"
bobbens@2472
    28
#include "../SDL_syshaptic.h"
bobbens@2472
    29
bobbens@2472
    30
#include <unistd.h> /* close */
bobbens@2472
    31
#include <linux/input.h>
bobbens@2472
    32
#include <sys/ioctl.h>
bobbens@2472
    33
#include <sys/types.h>
bobbens@2472
    34
#include <sys/stat.h>
bobbens@2472
    35
#include <fcntl.h>
bobbens@2472
    36
#include <linux/limits.h>
bobbens@2472
    37
#include <string.h>
bobbens@2472
    38
bobbens@2472
    39
bobbens@2472
    40
#include <stdio.h>
bobbens@2472
    41
bobbens@2472
    42
bobbens@2472
    43
#define MAX_HAPTICS  32
bobbens@2472
    44
bobbens@2472
    45
bobbens@2472
    46
static struct
bobbens@2472
    47
{
bobbens@2472
    48
   char *fname;
bobbens@2472
    49
   SDL_Haptic *haptic;
bobbens@2472
    50
} SDL_hapticlist[MAX_HAPTICS];
bobbens@2472
    51
bobbens@2472
    52
struct haptic_hwdata
bobbens@2472
    53
{
bobbens@2472
    54
   int fd;
bobbens@2472
    55
};
bobbens@2472
    56
bobbens@2472
    57
bobbens@2472
    58
#define test_bit(nr, addr) \
bobbens@2472
    59
   (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
bobbens@2472
    60
#define EV_TEST(ev,f) \
bobbens@2472
    61
   if (test_bit((ev), features)) ret |= (f);
bobbens@2472
    62
static int
bobbens@2472
    63
EV_IsHaptic(int fd)
bobbens@2472
    64
{
bobbens@2472
    65
   unsigned int ret;
bobbens@2472
    66
   unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
bobbens@2472
    67
bobbens@2472
    68
   ret = 0;
bobbens@2472
    69
bobbens@2472
    70
   ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features);
bobbens@2472
    71
bobbens@2472
    72
   EV_TEST(FF_CONSTANT,   SDL_HAPTIC_CONSTANT);
bobbens@2472
    73
   EV_TEST(FF_PERIODIC,   SDL_HAPTIC_PERIODIC);
bobbens@2472
    74
   EV_TEST(FF_RAMP,       SDL_HAPTIC_RAMP);
bobbens@2472
    75
   EV_TEST(FF_SPRING,     SDL_HAPTIC_SPRING);
bobbens@2472
    76
   EV_TEST(FF_FRICTION,   SDL_HAPTIC_FRICTION);
bobbens@2472
    77
   EV_TEST(FF_DAMPER,     SDL_HAPTIC_DAMPER);
bobbens@2472
    78
   EV_TEST(FF_RUMBLE,     SDL_HAPTIC_RUMBLE);
bobbens@2472
    79
   EV_TEST(FF_INERTIA,    SDL_HAPTIC_INERTIA);
bobbens@2472
    80
   EV_TEST(FF_GAIN,       SDL_HAPTIC_GAIN);
bobbens@2472
    81
   EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
bobbens@2472
    82
bobbens@2472
    83
   return ret;
bobbens@2472
    84
}
bobbens@2472
    85
bobbens@2472
    86
int
bobbens@2472
    87
SDL_SYS_HapticInit(void)
bobbens@2472
    88
{
bobbens@2472
    89
   const char joydev_pattern[] = "/dev/input/event%d";
bobbens@2472
    90
   dev_t dev_nums[MAX_HAPTICS];
bobbens@2472
    91
   char path[PATH_MAX];
bobbens@2472
    92
   struct stat sb;
bobbens@2472
    93
   int fd;
bobbens@2472
    94
   int i, j, k;
bobbens@2472
    95
   int duplicate;
bobbens@2472
    96
   int numhaptics;
bobbens@2472
    97
bobbens@2472
    98
   numhaptics = 0;
bobbens@2472
    99
bobbens@2472
   100
   i = 0;
bobbens@2472
   101
   for (j = 0; j < MAX_HAPTICS; ++j) {
bobbens@2472
   102
bobbens@2472
   103
      snprintf(path, PATH_MAX, joydev_pattern, i++);
bobbens@2472
   104
bobbens@2472
   105
      /* check to see if file exists */
bobbens@2472
   106
      if (stat(path,&sb) != 0)
bobbens@2472
   107
         break;
bobbens@2472
   108
bobbens@2472
   109
      /* check for duplicates */
bobbens@2472
   110
      duplicate = 0;
bobbens@2472
   111
      for (k = 0; (k < numhaptics) && !duplicate; ++k) {
bobbens@2472
   112
         if (sb.st_rdev == dev_nums[k]) {
bobbens@2472
   113
            duplicate = 1;
bobbens@2472
   114
         }                                                     
bobbens@2472
   115
      }                                                         
bobbens@2472
   116
      if (duplicate) {
bobbens@2472
   117
         continue;
bobbens@2472
   118
      }
bobbens@2472
   119
bobbens@2472
   120
      /* try to open */
bobbens@2472
   121
      fd = open(path, O_RDWR, 0);
bobbens@2472
   122
      if (fd < 0) continue;
bobbens@2472
   123
bobbens@2472
   124
#ifdef DEBUG_INPUT_EVENTS
bobbens@2472
   125
      printf("Checking %s\n", path);
bobbens@2472
   126
#endif          
bobbens@2472
   127
bobbens@2472
   128
      /* see if it works */
bobbens@2472
   129
      if (EV_IsHaptic(fd)!=0) {
bobbens@2472
   130
         SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
bobbens@2472
   131
         SDL_hapticlist[numhaptics].haptic = NULL;
bobbens@2472
   132
         dev_nums[numhaptics] = sb.st_rdev;
bobbens@2472
   133
         ++numhaptics;
bobbens@2472
   134
      }
bobbens@2472
   135
      close(fd);
bobbens@2472
   136
   }
bobbens@2472
   137
bobbens@2472
   138
   return numhaptics;
bobbens@2472
   139
}
bobbens@2472
   140
bobbens@2472
   141
bobbens@2472
   142
const char *
bobbens@2472
   143
SDL_SYS_HapticName(int index)
bobbens@2472
   144
{
bobbens@2472
   145
   int fd;
bobbens@2472
   146
   static char namebuf[128];
bobbens@2472
   147
   char *name;
bobbens@2472
   148
bobbens@2472
   149
   name = NULL;
bobbens@2472
   150
   fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
bobbens@2472
   151
   if (fd >= 0) {
bobbens@2472
   152
      if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
bobbens@2472
   153
         name = SDL_hapticlist[index].fname;
bobbens@2472
   154
      }
bobbens@2472
   155
      else {
bobbens@2472
   156
         name = namebuf;
bobbens@2472
   157
      }
bobbens@2472
   158
   }
bobbens@2472
   159
   close(fd);
bobbens@2472
   160
bobbens@2472
   161
   return name;
bobbens@2472
   162
}
bobbens@2472
   163
bobbens@2472
   164
bobbens@2472
   165
int
bobbens@2472
   166
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
bobbens@2472
   167
{
bobbens@2472
   168
   /* TODO finish
bobbens@2472
   169
   int fd;
bobbens@2472
   170
bobbens@2472
   171
   fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
bobbens@2472
   172
   
bobbens@2472
   173
   if (fd < 0) {
bobbens@2472
   174
      SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
bobbens@2472
   175
      return (-1);
bobbens@2472
   176
   }
bobbens@2472
   177
bobbens@2472
   178
   
bobbens@2472
   179
bobbens@2472
   180
   return 0;
bobbens@2472
   181
   */
bobbens@2472
   182
}
bobbens@2472
   183
bobbens@2472
   184
bobbens@2474
   185
/* Clean up after system specific haptic stuff */
bobbens@2474
   186
void
bobbens@2474
   187
SDL_SYS_HapticQuit(void)
bobbens@2474
   188
{
bobbens@2474
   189
   int i;
bobbens@2474
   190
bobbens@2474
   191
   for (i=0; SDL_hapticlist[i].fname != NULL; i++) {
bobbens@2474
   192
      SDL_free(SDL_hapticlist[i].fname);
bobbens@2474
   193
   }
bobbens@2474
   194
   SDL_hapticlist[0].fname = NULL;
bobbens@2474
   195
}
bobbens@2474
   196
bobbens@2474
   197
bobbens@2472
   198
#endif /* SDL_HAPTIC_LINUX */