src/haptic/SDL_haptic.c
branchgsoc2008_force_feedback
changeset 2490 be9b206d44af
parent 2489 96adc8025331
child 2491 10bc7aaf5114
--- a/src/haptic/SDL_haptic.c	Wed Jul 02 08:24:35 2008 +0000
+++ b/src/haptic/SDL_haptic.c	Wed Jul 02 09:52:44 2008 +0000
@@ -123,7 +123,7 @@
 
    /* Add haptic to list */
    ++haptic->ref_count;
-   for (i = 0; SDL_haptics[i]; ++i)
+   for (i=0; SDL_haptics[i]; i++)
       /* Skip to next haptic */ ;
    SDL_haptics[i] = haptic;
 
@@ -139,6 +139,7 @@
 {
    int ret;
 
+   /* Must be a valid joystick */
    if (!SDL_PrivateJoystickValid(&joystick)) {
       return -1;
    }
@@ -157,10 +158,49 @@
 SDL_Haptic *
 SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)
 {
+   int i;
+   SDL_Haptic *haptic;
+
+   /* Must be a valid joystick */
    if (!SDL_PrivateJoystickValid(&joystick)) {
-      return -1;
+      return NULL;
+   }
+
+   /* Joystick must be haptic */
+   if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) {
+      return NULL;
+   }
+
+   /* Check to see if joystick's haptic is already open */
+   for (i=0; SDL_haptics[i]; i++) {
+      if (SDL_SYS_JoystickSameHaptic(&SDL_haptics[i],joystick)) {
+         haptic = SDL_haptics[i];
+         ++haptic->ref_count;
+         return haptic;
+      }
    }
-   return -1;
+
+   /* Create the haptic device */
+   haptic = (SDL_Haptic *) SDL_malloc((sizeof *haptic));
+   if (haptic == NULL) {
+      SDL_OutOfMemory();
+      return NULL;
+   }
+
+   /* Initialize the haptic device */
+   SDL_memset(haptic, 0, (sizeof *haptic));
+   if (SDL_SYS_HapticOpenFromJoystick(haptic,joystick) < 0) {
+      SDL_free(haptic);
+      return NULL;
+   }
+
+   /* Add haptic to list */
+   ++haptic->ref_count;
+   for (i=0; SDL_haptics[i]; i++)
+      /* Skip to next haptic */ ;
+   SDL_haptics[i] = haptic;
+
+   return haptic;
 }