src/haptic/SDL_haptic.c
branchgsoc2008_force_feedback
changeset 2490 be9b206d44af
parent 2489 96adc8025331
child 2491 10bc7aaf5114
     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