diff src/haptic/SDL_haptic.c @ 2480:b883974445fc gsoc2008_force_feedback

Some more error reporting. Added periodic effect. Confirmed it works.
author Edgar Simo <bobbens@gmail.com>
date Tue, 01 Jul 2008 09:22:22 +0000
parents b9eb2cfe16cd
children 9d52368ebcf5
line wrap: on
line diff
--- a/src/haptic/SDL_haptic.c	Mon Jun 30 21:38:29 2008 +0000
+++ b/src/haptic/SDL_haptic.c	Tue Jul 01 09:22:22 2008 +0000
@@ -166,7 +166,12 @@
       return;
    }
 
-   /* Close it */
+   /* Close it, properly removing effects if needed */
+   for (i=0; i<haptic->neffects; i++) {
+      if (haptic->effects[i].hweffect != NULL) {
+         SDL_HapticDestroyEffect(haptic,i);
+      }
+   }
    SDL_SYS_HapticClose(haptic);
 
    /* Remove from the list */
@@ -248,12 +253,18 @@
       return -1;
    }
 
+   /* Check to see if effect is supported */
+   if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
+      SDL_SetError("Haptic effect not supported by haptic device.");
+      return -1;
+   }
+
    /* See if there's a free slot */
    for (i=0; i<haptic->neffects; i++) {
       if (haptic->effects[i].hweffect == NULL) {
 
          /* Now let the backend create the real effect */
-         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i]) != 0) {
+         if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
             return -1; /* Backend failed to create effect */
          }
          return i;
@@ -265,12 +276,25 @@
 }
 
 /*
+ * Checks to see if an effect is valid.
+ */
+static int
+ValidEffect(SDL_Haptic * haptic, int effect)
+{
+   if ((effect < 0) || (effect >= haptic->neffects)) {
+      SDL_SetError("Invalid haptic effect identifier.");
+      return 0;
+   }
+   return 1;
+}
+
+/*
  * Runs the haptic effect on the device.
  */
 int
 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
 {
-   if (!ValidHaptic(&haptic)) {
+   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
       return -1;
    }
 
@@ -288,7 +312,7 @@
 void
 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
 {
-   if (!ValidHaptic(&haptic)) {
+   if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
       return;
    }