JNIEXPORT jint JNICALL Java_at_wisch_joystick_FFJoystick_setGainNative (JNIEnv *env, jclass, jint hapticDeviceIndex, jint gainValue){ int num = SDL_HapticSetGain(ffjoysticks[hapticDeviceIndex], gainValue); if (num < 0) { throwException(env, SDL_GetError()); return -18; } return num; }
/* * Opens a Haptic device. */ SDL_Haptic * SDL_HapticOpen(int device_index) { int i; SDL_Haptic *haptic; if ((device_index < 0) || (device_index >= SDL_numhaptics)) { SDL_SetError("Haptic: There are %d haptic devices available", SDL_numhaptics); return NULL; } /* If the haptic is already open, return it */ for (i = 0; SDL_haptics[i]; i++) { if (device_index == SDL_haptics[i]->index) { 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 *haptic)); haptic->rumble_id = -1; haptic->index = device_index; if (SDL_SYS_HapticOpen(haptic) < 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; /* Disable autocenter and set gain to max. */ if (haptic->supported & SDL_HAPTIC_GAIN) SDL_HapticSetGain(haptic, 100); if (haptic->supported & SDL_HAPTIC_AUTOCENTER) SDL_HapticSetAutocenter(haptic, 0); return haptic; }
/* * Opens a Haptic device. */ SDL_Haptic * SDL_HapticOpen(int device_index) { SDL_Haptic *haptic; SDL_Haptic *hapticlist; if ((device_index < 0) || (device_index >= SDL_NumHaptics())) { SDL_SetError("Haptic: There are %d haptic devices available", SDL_NumHaptics()); return NULL; } hapticlist = SDL_haptics; /* If the haptic is already open, return it * TODO: Should we create haptic instance IDs like the Joystick API? */ while ( hapticlist ) { if (device_index == hapticlist->index) { 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 *haptic)); haptic->rumble_id = -1; haptic->index = device_index; if (SDL_SYS_HapticOpen(haptic) < 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; /* Disable autocenter and set gain to max. */ if (haptic->supported & SDL_HAPTIC_GAIN) SDL_HapticSetGain(haptic, 100); if (haptic->supported & SDL_HAPTIC_AUTOCENTER) SDL_HapticSetAutocenter(haptic, 0); return haptic; }
bool SDL2FFBDevice::setGain(const int gain) { if (!m_adjustableGain) { QMessageBox::warning(nullptr, SDL2DEV_ERR_CAPTION, "Device does not have adjustable gain"); return false; } if (gain < 0 || gain > 100) { QMessageBox::warning(nullptr, SDL2DEV_ERR_CAPTION, "Gain must be within <0; 100>"); return false; } if (SDL_HapticSetGain(c_haptic, gain) < 0) { QMessageBox::warning(nullptr, SDL2DEV_ERR_CAPTION, QString("Unable to set gain:\n%1").arg(SDL_GetError())); return false; } return true; }