src/haptic/linux/SDL_syshaptic.c
author Edgar Simo <bobbens@gmail.com>
Sun, 01 Jun 2008 19:11:49 +0000
branchgsoc2008_force_feedback
changeset 2475 4b874e3a3a2c
parent 2474 3f80bf1528b4
child 2476 242d8a668ebb
permissions -rw-r--r--
Some code cleanup.
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
#define MAX_HAPTICS  32
bobbens@2472
    41
bobbens@2472
    42
bobbens@2475
    43
/*
bobbens@2475
    44
 * List of available haptic devices.
bobbens@2475
    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@2475
    52
bobbens@2475
    53
/*
bobbens@2475
    54
 * Haptic system hardware data.
bobbens@2475
    55
 */
bobbens@2472
    56
struct haptic_hwdata
bobbens@2472
    57
{
bobbens@2472
    58
   int fd;
bobbens@2472
    59
};
bobbens@2472
    60
bobbens@2472
    61
bobbens@2475
    62
bobbens@2472
    63
#define test_bit(nr, addr) \
bobbens@2472
    64
   (((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)
bobbens@2472
    65
#define EV_TEST(ev,f) \
bobbens@2472
    66
   if (test_bit((ev), features)) ret |= (f);
bobbens@2475
    67
/*
bobbens@2475
    68
 * Test whether a device has haptic properties.
bobbens@2475
    69
 * Returns available properties or 0 if there are none.
bobbens@2475
    70
 */
bobbens@2472
    71
static int
bobbens@2472
    72
EV_IsHaptic(int fd)
bobbens@2472
    73
{
bobbens@2472
    74
   unsigned int ret;
bobbens@2472
    75
   unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
bobbens@2472
    76
bobbens@2472
    77
   ret = 0;
bobbens@2472
    78
bobbens@2472
    79
   ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features);
bobbens@2472
    80
bobbens@2472
    81
   EV_TEST(FF_CONSTANT,   SDL_HAPTIC_CONSTANT);
bobbens@2472
    82
   EV_TEST(FF_PERIODIC,   SDL_HAPTIC_PERIODIC);
bobbens@2472
    83
   EV_TEST(FF_RAMP,       SDL_HAPTIC_RAMP);
bobbens@2472
    84
   EV_TEST(FF_SPRING,     SDL_HAPTIC_SPRING);
bobbens@2472
    85
   EV_TEST(FF_FRICTION,   SDL_HAPTIC_FRICTION);
bobbens@2472
    86
   EV_TEST(FF_DAMPER,     SDL_HAPTIC_DAMPER);
bobbens@2472
    87
   EV_TEST(FF_RUMBLE,     SDL_HAPTIC_RUMBLE);
bobbens@2472
    88
   EV_TEST(FF_INERTIA,    SDL_HAPTIC_INERTIA);
bobbens@2472
    89
   EV_TEST(FF_GAIN,       SDL_HAPTIC_GAIN);
bobbens@2472
    90
   EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
bobbens@2472
    91
bobbens@2472
    92
   return ret;
bobbens@2472
    93
}
bobbens@2472
    94
bobbens@2472
    95
int
bobbens@2472
    96
SDL_SYS_HapticInit(void)
bobbens@2472
    97
{
bobbens@2472
    98
   const char joydev_pattern[] = "/dev/input/event%d";
bobbens@2472
    99
   dev_t dev_nums[MAX_HAPTICS];
bobbens@2472
   100
   char path[PATH_MAX];
bobbens@2472
   101
   struct stat sb;
bobbens@2472
   102
   int fd;
bobbens@2472
   103
   int i, j, k;
bobbens@2472
   104
   int duplicate;
bobbens@2472
   105
   int numhaptics;
bobbens@2472
   106
bobbens@2472
   107
   numhaptics = 0;
bobbens@2472
   108
bobbens@2472
   109
   i = 0;
bobbens@2472
   110
   for (j = 0; j < MAX_HAPTICS; ++j) {
bobbens@2472
   111
bobbens@2472
   112
      snprintf(path, PATH_MAX, joydev_pattern, i++);
bobbens@2472
   113
bobbens@2472
   114
      /* check to see if file exists */
bobbens@2472
   115
      if (stat(path,&sb) != 0)
bobbens@2472
   116
         break;
bobbens@2472
   117
bobbens@2472
   118
      /* check for duplicates */
bobbens@2472
   119
      duplicate = 0;
bobbens@2472
   120
      for (k = 0; (k < numhaptics) && !duplicate; ++k) {
bobbens@2472
   121
         if (sb.st_rdev == dev_nums[k]) {
bobbens@2472
   122
            duplicate = 1;
bobbens@2472
   123
         }                                                     
bobbens@2472
   124
      }                                                         
bobbens@2472
   125
      if (duplicate) {
bobbens@2472
   126
         continue;
bobbens@2472
   127
      }
bobbens@2472
   128
bobbens@2472
   129
      /* try to open */
bobbens@2472
   130
      fd = open(path, O_RDWR, 0);
bobbens@2472
   131
      if (fd < 0) continue;
bobbens@2472
   132
bobbens@2472
   133
#ifdef DEBUG_INPUT_EVENTS
bobbens@2472
   134
      printf("Checking %s\n", path);
bobbens@2472
   135
#endif          
bobbens@2472
   136
bobbens@2472
   137
      /* see if it works */
bobbens@2472
   138
      if (EV_IsHaptic(fd)!=0) {
bobbens@2472
   139
         SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
bobbens@2472
   140
         SDL_hapticlist[numhaptics].haptic = NULL;
bobbens@2472
   141
         dev_nums[numhaptics] = sb.st_rdev;
bobbens@2472
   142
         ++numhaptics;
bobbens@2472
   143
      }
bobbens@2472
   144
      close(fd);
bobbens@2472
   145
   }
bobbens@2472
   146
bobbens@2472
   147
   return numhaptics;
bobbens@2472
   148
}
bobbens@2472
   149
bobbens@2472
   150
bobbens@2475
   151
/*
bobbens@2475
   152
 * Return the name of a haptic device, does not need to be opened.
bobbens@2475
   153
 */
bobbens@2472
   154
const char *
bobbens@2472
   155
SDL_SYS_HapticName(int index)
bobbens@2472
   156
{
bobbens@2472
   157
   int fd;
bobbens@2472
   158
   static char namebuf[128];
bobbens@2472
   159
   char *name;
bobbens@2472
   160
bobbens@2472
   161
   name = NULL;
bobbens@2472
   162
   fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
bobbens@2472
   163
   if (fd >= 0) {
bobbens@2472
   164
      if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
bobbens@2472
   165
         name = SDL_hapticlist[index].fname;
bobbens@2472
   166
      }
bobbens@2472
   167
      else {
bobbens@2472
   168
         name = namebuf;
bobbens@2472
   169
      }
bobbens@2472
   170
   }
bobbens@2472
   171
   close(fd);
bobbens@2472
   172
bobbens@2472
   173
   return name;
bobbens@2472
   174
}
bobbens@2472
   175
bobbens@2472
   176
bobbens@2475
   177
/*
bobbens@2475
   178
 * Opens a haptic device for usage.
bobbens@2475
   179
 */
bobbens@2472
   180
int
bobbens@2472
   181
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
bobbens@2472
   182
{
bobbens@2472
   183
   /* TODO finish
bobbens@2472
   184
   int fd;
bobbens@2472
   185
bobbens@2472
   186
   fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
bobbens@2472
   187
   
bobbens@2472
   188
   if (fd < 0) {
bobbens@2472
   189
      SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
bobbens@2472
   190
      return (-1);
bobbens@2472
   191
   }
bobbens@2472
   192
bobbens@2472
   193
   
bobbens@2472
   194
bobbens@2472
   195
   return 0;
bobbens@2472
   196
   */
bobbens@2472
   197
}
bobbens@2472
   198
bobbens@2472
   199
bobbens@2475
   200
/*
bobbens@2475
   201
 * Closes the haptic device.
bobbens@2475
   202
 */
bobbens@2475
   203
void
bobbens@2475
   204
SDL_SYS_HapticClose(SDL_Haptic * haptic)
bobbens@2475
   205
{
bobbens@2475
   206
}
bobbens@2475
   207
bobbens@2475
   208
bobbens@2474
   209
/* Clean up after system specific haptic stuff */
bobbens@2474
   210
void
bobbens@2474
   211
SDL_SYS_HapticQuit(void)
bobbens@2474
   212
{
bobbens@2474
   213
   int i;
bobbens@2474
   214
bobbens@2474
   215
   for (i=0; SDL_hapticlist[i].fname != NULL; i++) {
bobbens@2474
   216
      SDL_free(SDL_hapticlist[i].fname);
bobbens@2474
   217
   }
bobbens@2474
   218
   SDL_hapticlist[0].fname = NULL;
bobbens@2474
   219
}
bobbens@2474
   220
bobbens@2474
   221
bobbens@2472
   222
#endif /* SDL_HAPTIC_LINUX */