src/haptic/SDL_haptic.c
branchgsoc2008_force_feedback
changeset 2480 b883974445fc
parent 2479 b9eb2cfe16cd
child 2483 9d52368ebcf5
     1.1 --- a/src/haptic/SDL_haptic.c	Mon Jun 30 21:38:29 2008 +0000
     1.2 +++ b/src/haptic/SDL_haptic.c	Tue Jul 01 09:22:22 2008 +0000
     1.3 @@ -166,7 +166,12 @@
     1.4        return;
     1.5     }
     1.6  
     1.7 -   /* Close it */
     1.8 +   /* Close it, properly removing effects if needed */
     1.9 +   for (i=0; i<haptic->neffects; i++) {
    1.10 +      if (haptic->effects[i].hweffect != NULL) {
    1.11 +         SDL_HapticDestroyEffect(haptic,i);
    1.12 +      }
    1.13 +   }
    1.14     SDL_SYS_HapticClose(haptic);
    1.15  
    1.16     /* Remove from the list */
    1.17 @@ -248,12 +253,18 @@
    1.18        return -1;
    1.19     }
    1.20  
    1.21 +   /* Check to see if effect is supported */
    1.22 +   if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
    1.23 +      SDL_SetError("Haptic effect not supported by haptic device.");
    1.24 +      return -1;
    1.25 +   }
    1.26 +
    1.27     /* See if there's a free slot */
    1.28     for (i=0; i<haptic->neffects; i++) {
    1.29        if (haptic->effects[i].hweffect == NULL) {
    1.30  
    1.31           /* Now let the backend create the real effect */
    1.32 -         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i]) != 0) {
    1.33 +         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
    1.34              return -1; /* Backend failed to create effect */
    1.35           }
    1.36           return i;
    1.37 @@ -265,12 +276,25 @@
    1.38  }
    1.39  
    1.40  /*
    1.41 + * Checks to see if an effect is valid.
    1.42 + */
    1.43 +static int
    1.44 +ValidEffect(SDL_Haptic * haptic, int effect)
    1.45 +{
    1.46 +   if ((effect < 0) || (effect >= haptic->neffects)) {
    1.47 +      SDL_SetError("Invalid haptic effect identifier.");
    1.48 +      return 0;
    1.49 +   }
    1.50 +   return 1;
    1.51 +}
    1.52 +
    1.53 +/*
    1.54   * Runs the haptic effect on the device.
    1.55   */
    1.56  int
    1.57  SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
    1.58  {
    1.59 -   if (!ValidHaptic(&haptic)) {
    1.60 +   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
    1.61        return -1;
    1.62     }
    1.63  
    1.64 @@ -288,7 +312,7 @@
    1.65  void
    1.66  SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
    1.67  {
    1.68 -   if (!ValidHaptic(&haptic)) {
    1.69 +   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
    1.70        return;
    1.71     }
    1.72