Some more error checking. gsoc2008_force_feedback
authorEdgar Simo <bobbens@gmail.com>
Wed, 02 Jul 2008 09:52:44 +0000
branchgsoc2008_force_feedback
changeset 2490be9b206d44af
parent 2489 96adc8025331
child 2491 10bc7aaf5114
Some more error checking.
Implemented SDL_HapticOpenFromJoystick().
src/haptic/SDL_haptic.c
src/haptic/linux/SDL_syshaptic.c
src/joystick/linux/SDL_sysjoystick.c
src/joystick/linux/SDL_sysjoystick_c.h
     1.1 --- a/src/haptic/SDL_haptic.c	Wed Jul 02 08:24:35 2008 +0000
     1.2 +++ b/src/haptic/SDL_haptic.c	Wed Jul 02 09:52:44 2008 +0000
     1.3 @@ -123,7 +123,7 @@
     1.4  
     1.5     /* Add haptic to list */
     1.6     ++haptic->ref_count;
     1.7 -   for (i = 0; SDL_haptics[i]; ++i)
     1.8 +   for (i=0; SDL_haptics[i]; i++)
     1.9        /* Skip to next haptic */ ;
    1.10     SDL_haptics[i] = haptic;
    1.11  
    1.12 @@ -139,6 +139,7 @@
    1.13  {
    1.14     int ret;
    1.15  
    1.16 +   /* Must be a valid joystick */
    1.17     if (!SDL_PrivateJoystickValid(&joystick)) {
    1.18        return -1;
    1.19     }
    1.20 @@ -157,10 +158,49 @@
    1.21  SDL_Haptic *
    1.22  SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
    1.23  {
    1.24 +   int i;
    1.25 +   SDL_Haptic *haptic;
    1.26 +
    1.27 +   /* Must be a valid joystick */
    1.28     if (!SDL_PrivateJoystickValid(&joystick)) {
    1.29 -      return -1;
    1.30 +      return NULL;
    1.31     }
    1.32 -   return -1;
    1.33 +
    1.34 +   /* Joystick must be haptic */
    1.35 +   if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
    1.36 +      return NULL;
    1.37 +   }
    1.38 +
    1.39 +   /* Check to see if joystick's haptic is already open */
    1.40 +   for (i=0; SDL_haptics[i]; i++) {
    1.41 +      if (SDL_SYS_JoystickSameHaptic(&SDL_haptics[i],joystick)) {
    1.42 +         haptic = SDL_haptics[i];
    1.43 +         ++haptic->ref_count;
    1.44 +         return haptic;
    1.45 +      }
    1.46 +   }
    1.47 +
    1.48 +   /* Create the haptic device */
    1.49 +   haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
    1.50 +   if (haptic == NULL) {
    1.51 +      SDL_OutOfMemory();
    1.52 +      return NULL;
    1.53 +   }
    1.54 +
    1.55 +   /* Initialize the haptic device */
    1.56 +   SDL_memset(haptic, 0, (sizeof *haptic));
    1.57 +   if (SDL_SYS_HapticOpenFromJoystick(haptic,joystick) < 0) {
    1.58 +      SDL_free(haptic);
    1.59 +      return NULL;
    1.60 +   }
    1.61 +
    1.62 +   /* Add haptic to list */
    1.63 +   ++haptic->ref_count;
    1.64 +   for (i=0; SDL_haptics[i]; i++)
    1.65 +      /* Skip to next haptic */ ;
    1.66 +   SDL_haptics[i] = haptic;
    1.67 +
    1.68 +   return haptic;
    1.69  }
    1.70  
    1.71  
     2.1 --- a/src/haptic/linux/SDL_syshaptic.c	Wed Jul 02 08:24:35 2008 +0000
     2.2 +++ b/src/haptic/linux/SDL_syshaptic.c	Wed Jul 02 09:52:44 2008 +0000
     2.3 @@ -88,7 +88,10 @@
     2.4  
     2.5     ret = 0;
     2.6  
     2.7 -   ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features);
     2.8 +   if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) < 0) {
     2.9 +      SDL_SetError("Unable to get device's haptic abilities.");
    2.10 +      return -1;
    2.11 +   }
    2.12  
    2.13     EV_TEST(FF_CONSTANT,   SDL_HAPTIC_CONSTANT);
    2.14     EV_TEST(FF_PERIODIC,   SDL_HAPTIC_SINE |
    2.15 @@ -151,7 +154,7 @@
    2.16  #endif          
    2.17  
    2.18        /* see if it works */
    2.19 -      if (EV_IsHaptic(fd)!=0) {
    2.20 +      if (EV_IsHaptic(fd) > 0) {
    2.21           SDL_hapticlist[numhaptics].fname = SDL_strdup(path);
    2.22           SDL_hapticlist[numhaptics].haptic = NULL;
    2.23           dev_nums[numhaptics] = sb.st_rdev;
    2.24 @@ -191,21 +194,11 @@
    2.25  
    2.26  
    2.27  /*
    2.28 - * Opens a haptic device for usage.
    2.29 + * Opens the haptic device from the file descriptor.
    2.30   */
    2.31 -int
    2.32 -SDL_SYS_HapticOpen(SDL_Haptic * haptic)
    2.33 +static int
    2.34 +SDL_SYS_HapticOpenFromFD(SDL_Haptic * haptic, int fd)
    2.35  {
    2.36 -   int i;
    2.37 -   int fd;
    2.38 -
    2.39 -   /* Open the character device */
    2.40 -   fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
    2.41 -   if (fd < 0) {
    2.42 -      SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
    2.43 -      return -1;
    2.44 -   }
    2.45 -
    2.46     /* Allocate the hwdata */
    2.47     haptic->hwdata = (struct haptic_hwdata *)
    2.48           SDL_malloc(sizeof(*haptic->hwdata));
    2.49 @@ -247,13 +240,43 @@
    2.50  
    2.51  
    2.52  /*
    2.53 + * Opens a haptic device for usage.
    2.54 + */
    2.55 +int
    2.56 +SDL_SYS_HapticOpen(SDL_Haptic * haptic)
    2.57 +{
    2.58 +   int fd;
    2.59 +
    2.60 +   /* Open the character device */
    2.61 +   fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
    2.62 +   if (fd < 0) {
    2.63 +      SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
    2.64 +      return -1;
    2.65 +   }
    2.66 +   
    2.67 +   return SDL_SYS_HapticOpenFromFD(haptic,fd);
    2.68 +} 
    2.69 +
    2.70 +
    2.71 +/*
    2.72   * Checks to see if a joystick has haptic features.
    2.73   */
    2.74  int
    2.75  SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
    2.76  {
    2.77 -   if (EV_IsHaptic(joystick->hwdata->fd) > 0)
    2.78 +   return EV_IsHaptic(joystick->hwdata->fd);
    2.79 +}
    2.80 +
    2.81 +
    2.82 +/*
    2.83 + * Checks to see if the haptic device and joystick and in reality the same.
    2.84 + */
    2.85 +int
    2.86 +SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
    2.87 +{
    2.88 +   if (SDL_strcmp(joystick->name,haptic->name)==0) {
    2.89        return 1;
    2.90 +   }
    2.91     return 0;
    2.92  }
    2.93  
    2.94 @@ -264,6 +287,9 @@
    2.95  int
    2.96  SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
    2.97  {
    2.98 +   int fd;
    2.99 +   fd = open(joystick->hwdata->fname, O_RDWR, 0);
   2.100 +   return SDL_SYS_HapticOpenFromFD(haptic,fd);
   2.101  }
   2.102  
   2.103  
     3.1 --- a/src/joystick/linux/SDL_sysjoystick.c	Wed Jul 02 08:24:35 2008 +0000
     3.2 +++ b/src/joystick/linux/SDL_sysjoystick.c	Wed Jul 02 09:52:44 2008 +0000
     3.3 @@ -774,6 +774,7 @@
     3.4  SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
     3.5  {
     3.6      int fd;
     3.7 +    char *fname;
     3.8      SDL_logical_joydecl(int realindex);
     3.9      SDL_logical_joydecl(SDL_Joystick * realjoy = NULL);
    3.10  
    3.11 @@ -787,13 +788,16 @@
    3.12              return (-1);
    3.13  
    3.14          fd = realjoy->hwdata->fd;
    3.15 +        fname = realjoy->hwdata->fname;
    3.16  
    3.17      } else {
    3.18          fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);
    3.19 +        fname = SDL_joylist[joystick->index].fname;
    3.20      }
    3.21      SDL_joylist[joystick->index].joy = joystick;
    3.22  #else
    3.23      fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);
    3.24 +    fname = SDL_joylist[joystick->index].fname;
    3.25  #endif
    3.26  
    3.27      if (fd < 0) {
    3.28 @@ -809,6 +813,7 @@
    3.29      }
    3.30      SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
    3.31      joystick->hwdata->fd = fd;
    3.32 +    joystick->hwdata->fname = fname;
    3.33  
    3.34      /* Set the joystick to non-blocking read mode */
    3.35      fcntl(fd, F_SETFL, O_NONBLOCK);
     4.1 --- a/src/joystick/linux/SDL_sysjoystick_c.h	Wed Jul 02 08:24:35 2008 +0000
     4.2 +++ b/src/joystick/linux/SDL_sysjoystick_c.h	Wed Jul 02 09:52:44 2008 +0000
     4.3 @@ -28,6 +28,8 @@
     4.4  struct joystick_hwdata
     4.5  {
     4.6     int fd;
     4.7 +   char *fname; /* Used in haptic subsystem */
     4.8 +
     4.9     /* The current linux joystick driver maps hats to two axes */
    4.10     struct hwdata_hat
    4.11     {