/* * Opens a haptic device from a joystick. */ SDL_Haptic * SDL_HapticOpenFromJoystick(SDL_Joystick * joystick) { SDL_Haptic *haptic; SDL_Haptic *hapticlist; /* Make sure there is room. */ if (SDL_NumHaptics() <= 0) { SDL_SetError("Haptic: There are %d haptic devices available", SDL_NumHaptics()); return NULL; } /* Must be a valid joystick */ if (!SDL_PrivateJoystickValid(joystick)) { SDL_SetError("Haptic: Joystick isn't valid."); return NULL; } /* Joystick must be haptic */ if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) { SDL_SetError("Haptic: Joystick isn't a haptic device."); return NULL; } hapticlist = SDL_haptics; /* Check to see if joystick's haptic is already open */ while ( hapticlist ) { if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) { haptic = hapticlist; ++haptic->ref_count; return haptic; } hapticlist = hapticlist->next; } /* 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(SDL_Haptic)); haptic->rumble_id = -1; if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) { SDL_free(haptic); return NULL; } /* Add haptic to list */ ++haptic->ref_count; /* Link the haptic in the list */ haptic->next = SDL_haptics; SDL_haptics = haptic; return haptic; }
/* * Opens a haptic device from a joystick. */ SDL_Haptic * SDL_HapticOpenFromJoystick(SDL_Joystick * joystick) { int i; SDL_Haptic *haptic; /* Must be a valid joystick */ #ifdef SDL_JOYSTICK_DISABLED /* if joystick support is disabled it can't be valid */ return -1; #else if (!SDL_PrivateJoystickValid(joystick)) { SDL_SetError("Haptic: Joystick isn't valid."); return NULL; } #endif /* Joystick must be haptic */ if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) { SDL_SetError("Haptic: Joystick isn't a haptic device."); 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; } } /* 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(SDL_Haptic)); haptic->rumble_id = -1; 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; }
/* * Opens a haptic device from a joystick. */ SDL_Haptic * SDL_HapticOpenFromJoystick(SDL_Joystick * joystick) { int i; SDL_Haptic *haptic; /* Make sure there is room. */ if (SDL_numhaptics <= 0) { SDL_SetError("Haptic: There are %d haptic devices available", SDL_numhaptics); return NULL; } /* Must be a valid joystick */ if (!SDL_PrivateJoystickValid(joystick)) { SDL_SetError("Haptic: Joystick isn't valid."); return NULL; } /* Joystick must be haptic */ if (SDL_SYS_JoystickIsHaptic(joystick) <= 0) { SDL_SetError("Haptic: Joystick isn't a haptic device."); 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; } } /* 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(SDL_Haptic)); haptic->rumble_id = -1; if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) { SDL_free(haptic); return NULL; } /* Add haptic to list */ for (i = 0; SDL_haptics[i]; i++) /* Skip to next haptic */ ; if (i >= SDL_numhaptics) { SDL_free(haptic); SDL_SetError("Haptic: Trying to add device past the number originally detected"); return NULL; } SDL_haptics[i] = haptic; ++haptic->ref_count; return haptic; }