Improved some ioctl handling. gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Thu, 10 Jul 2008 08:38:08 +0000
branchgsoc2008_force_feedback
changeset 2512ef147ee4896c
parent 2511 f12ae0bae468
child 2513 55fd9103a330
Improved some ioctl handling.
Implemented SDL_MouseIsHaptic and SDL_HapticOpenFromMouse.
More code comments.
include/SDL_haptic.h
src/haptic/SDL_haptic.c
src/haptic/SDL_syshaptic.h
src/haptic/dummy/SDL_syshaptic.c
src/haptic/linux/SDL_syshaptic.c
     1.1 --- a/include/SDL_haptic.h	Wed Jul 09 18:29:11 2008 +0000
     1.2 +++ b/include/SDL_haptic.h	Thu Jul 10 08:38:08 2008 +0000
     1.3 @@ -638,6 +638,7 @@
     1.4   *    \return Device identifier or NULL on error.
     1.5   *
     1.6   * \sa SDL_HapticIndex
     1.7 + * \sa SDL_HapticOpenFromMouse
     1.8   * \sa SDL_HapticOpenFromJoystick
     1.9   * \sa SDL_HapticClose
    1.10   * \sa SDL_HapticSetGain
    1.11 @@ -672,6 +673,29 @@
    1.12  extern DECLSPEC int SDL_HapticIndex(SDL_Haptic * haptic);
    1.13  
    1.14  /**
    1.15 + * \fn int SDL_MouseIsHaptic(void)
    1.16 + *
    1.17 + * \brief Gets whether or not the current mouse has haptic capabilities.
    1.18 + *
    1.19 + *    \return SDL_TRUE if the mouse is haptic, SDL_FALSE if it isn't.
    1.20 + *
    1.21 + * \sa SDL_HapticOpenFromMouse
    1.22 + */
    1.23 +extern DECLSPEC SDL_MouseIsHaptic(void);
    1.24 +
    1.25 +/**
    1.26 + * \fn SDL_Haptic * SDL_HapticOpenFromMouse(void)
    1.27 + *
    1.28 + * \brief Tries to open a haptic device from the current mouse.
    1.29 + *
    1.30 + *    \return The haptic device identifier or NULL on error.
    1.31 + *
    1.32 + * \sa SDL_MouseIsHaptic
    1.33 + * \sa SDL_HapticOpen
    1.34 + */
    1.35 +extern DECLSPEC SDL_Haptic * SDL_HapticOpenFromMouse(void);
    1.36 +
    1.37 +/**
    1.38   * \fn int SDL_JoystickIsHaptic(SDL_Joystick * joystick)
    1.39   *
    1.40   * \brief Checks to see if a joystick has haptic features.
     2.1 --- a/src/haptic/SDL_haptic.c	Wed Jul 09 18:29:11 2008 +0000
     2.2 +++ b/src/haptic/SDL_haptic.c	Thu Jul 10 08:38:08 2008 +0000
     2.3 @@ -25,7 +25,7 @@
     2.4  #include "../joystick/SDL_joystick_c.h" /* For SDL_PrivateJoystickValid */
     2.5  
     2.6  
     2.7 -static Uint8 SDL_numhaptics = 0;
     2.8 +Uint8 SDL_numhaptics = 0;
     2.9  SDL_Haptic **SDL_haptics = NULL;
    2.10  static SDL_Haptic *default_haptic = NULL;
    2.11  
    2.12 @@ -188,6 +188,37 @@
    2.13  
    2.14  
    2.15  /*
    2.16 + * Returns SDL_TRUE if mouse is haptic, SDL_FALSE if it isn't.
    2.17 + */
    2.18 +int
    2.19 +SDL_MouseIsHaptic(void)
    2.20 +{
    2.21 +   if (SDL_SYS_HapticMouse() < 0)
    2.22 +      return SDL_FALSE;
    2.23 +   return SDL_TRUE;
    2.24 +}
    2.25 +
    2.26 +
    2.27 +/*
    2.28 + * Returns the haptic device if mouse is haptic or NULL elsewise.
    2.29 + */
    2.30 +SDL_Haptic *
    2.31 +SDL_HapticOpenFromMouse(void)
    2.32 +{
    2.33 +   int device_index;
    2.34 +
    2.35 +   device_index = SDL_SYS_HapticMouse();
    2.36 +
    2.37 +   if (device_index < 0) {
    2.38 +      SDL_SetError("Mouse isn't a haptic device.");
    2.39 +      return NULL;
    2.40 +   }
    2.41 +
    2.42 +   return SDL_HapticOpen(device_index);
    2.43 +}
    2.44 +
    2.45 +
    2.46 +/*
    2.47   * Returns SDL_TRUE if joystick has haptic features.
    2.48   */
    2.49  int
     3.1 --- a/src/haptic/SDL_syshaptic.h	Wed Jul 09 18:29:11 2008 +0000
     3.2 +++ b/src/haptic/SDL_syshaptic.h	Thu Jul 10 08:38:08 2008 +0000
     3.3 @@ -25,6 +25,12 @@
     3.4  #include "SDL_haptic.h"
     3.5  
     3.6  
     3.7 +/*
     3.8 + * Number of haptic devices on the system.
     3.9 + */
    3.10 +extern Uint8 SDL_numhaptics;
    3.11 +
    3.12 +
    3.13  struct haptic_effect
    3.14  {
    3.15     SDL_HapticEffect effect; /* The current event */
    3.16 @@ -68,6 +74,11 @@
    3.17  extern int SDL_SYS_HapticOpen(SDL_Haptic * haptic);
    3.18  
    3.19  /*
    3.20 + * Returns the index of the haptic core pointer or -1 if none is found.
    3.21 + */
    3.22 +int SDL_SYS_HapticMouse(void);
    3.23 +
    3.24 +/*
    3.25   * Checks to see if the joystick has haptic capabilities.
    3.26   *
    3.27   * Returns >0 if haptic capabilities are detected, 0 if haptic
    3.28 @@ -83,7 +94,6 @@
    3.29   */
    3.30  extern int SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic,
    3.31                                            SDL_Joystick * joystick);
    3.32 -
    3.33  /*
    3.34   * Checks to see if haptic device and joystick device are the same.
    3.35   *
     4.1 --- a/src/haptic/dummy/SDL_syshaptic.c	Wed Jul 09 18:29:11 2008 +0000
     4.2 +++ b/src/haptic/dummy/SDL_syshaptic.c	Thu Jul 10 08:38:08 2008 +0000
     4.3 @@ -51,6 +51,13 @@
     4.4  
     4.5  
     4.6  int
     4.7 +SDL_SYS_HapticMouse(void)
     4.8 +{
     4.9 +   return -1;
    4.10 +}
    4.11 +
    4.12 +
    4.13 +int
    4.14  SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
    4.15  {
    4.16     return 0;
     5.1 --- a/src/haptic/linux/SDL_syshaptic.c	Wed Jul 09 18:29:11 2008 +0000
     5.2 +++ b/src/haptic/linux/SDL_syshaptic.c	Thu Jul 10 08:38:08 2008 +0000
     5.3 @@ -91,13 +91,14 @@
     5.4     unsigned int ret;
     5.5     unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
     5.6  
     5.7 +   /* Ask device for what it has. */
     5.8     ret = 0;
     5.9 -
    5.10 -   if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) < 0) {
    5.11 +   if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features) < 0) {
    5.12        SDL_SetError("Unable to get device's haptic abilities: %s", strerror(errno));
    5.13        return -1;
    5.14     }
    5.15  
    5.16 +   /* Convert supported features to SDL_HAPTIC platform-neutral features. */
    5.17     EV_TEST(FF_CONSTANT,   SDL_HAPTIC_CONSTANT);
    5.18     EV_TEST(FF_PERIODIC,   SDL_HAPTIC_SINE |
    5.19                            SDL_HAPTIC_SQUARE |
    5.20 @@ -113,9 +114,32 @@
    5.21     EV_TEST(FF_GAIN,       SDL_HAPTIC_GAIN);
    5.22     EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER);
    5.23  
    5.24 +   /* Return what it supports. */
    5.25     return ret;
    5.26  }
    5.27  
    5.28 +
    5.29 +/*
    5.30 + * Tests whether a device is a mouse or not.
    5.31 + */
    5.32 +static int
    5.33 +EV_IsMouse(int fd)
    5.34 +{
    5.35 +   unsigned long argp[40];
    5.36 +
    5.37 +   /* Ask for supported features. */
    5.38 +   if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(argp)), argp) < 0) {
    5.39 +      return -1;
    5.40 +   }
    5.41 +
    5.42 +   /* Currently we only test for BTN_MOUSE which can give fake positives. */
    5.43 +   if (test_bit(BTN_MOUSE,argp) != 0) {
    5.44 +      return 1;
    5.45 +   }
    5.46 +
    5.47 +   return 0;
    5.48 +}
    5.49 +
    5.50  /*
    5.51   * Initializes the haptic subsystem by finding available devices.
    5.52   */
    5.53 @@ -133,6 +157,10 @@
    5.54  
    5.55     numhaptics = 0;
    5.56  
    5.57 +   /* 
    5.58 +    * Limit amount of checks to MAX_HAPTICS since we may or may not have
    5.59 +    * permission to some or all devices.
    5.60 +    */
    5.61     i = 0;
    5.62     for (j = 0; j < MAX_HAPTICS; ++j) {
    5.63  
    5.64 @@ -185,12 +213,19 @@
    5.65     static char namebuf[128];
    5.66     char *name;
    5.67  
    5.68 +   /* Open the haptic device. */
    5.69     name = NULL;
    5.70     fd = open(SDL_hapticlist[index].fname, O_RDONLY, 0);
    5.71 +
    5.72     if (fd >= 0) {
    5.73 +
    5.74 +      /* Check for name ioctl. */
    5.75        if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
    5.76 +
    5.77 +         /* No name found, return device character device */
    5.78           name = SDL_hapticlist[index].fname;
    5.79        }
    5.80 +      /* Name found, return name. */
    5.81        else {
    5.82           name = namebuf;
    5.83        }
    5.84 @@ -215,6 +250,7 @@
    5.85        goto open_err;
    5.86     }
    5.87     SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
    5.88 +
    5.89     /* Set the data */
    5.90     haptic->hwdata->fd = fd;
    5.91     haptic->supported = EV_IsHaptic(fd);
    5.92 @@ -264,7 +300,38 @@
    5.93     }
    5.94     
    5.95     return SDL_SYS_HapticOpenFromFD(haptic,fd);
    5.96 -} 
    5.97 +}
    5.98 +
    5.99 +
   5.100 +/*
   5.101 + * Opens a haptic device from first mouse it finds for usage.
   5.102 + */
   5.103 +int
   5.104 +SDL_SYS_HapticMouse(void)
   5.105 +{
   5.106 +   int fd;
   5.107 +   int i;
   5.108 +
   5.109 +   for (i=0; i<SDL_numhaptics; i++) {
   5.110 +
   5.111 +      /* Open the device. */
   5.112 +      fd = open(SDL_hapticlist[i].fname, O_RDWR, 0);
   5.113 +      if (fd < 0) {
   5.114 +         SDL_SetError("Unable to open %s: %s",
   5.115 +               SDL_hapticlist[i], strerror(errno));
   5.116 +         return -1;
   5.117 +      }
   5.118 +
   5.119 +      if (EV_IsMouse(fd)) {
   5.120 +         close(fd);
   5.121 +         return i;
   5.122 +      }
   5.123 +
   5.124 +      close(fd);
   5.125 +   }
   5.126 +   
   5.127 +   return -1;
   5.128 +}
   5.129  
   5.130  
   5.131  /*