1 //! Haptic Functions 2 use crate::sys; 3 4 use crate::HapticSubsystem; 5 use crate::common::{validate_int, IntegerOrSdlError}; 6 use crate::get_error; 7 8 impl HapticSubsystem { 9 /// Attempt to open the joystick at index `joystick_index` and return its haptic device. open_from_joystick_id(&self, joystick_index: u32) -> Result<Haptic, IntegerOrSdlError>10 pub fn open_from_joystick_id(&self, joystick_index: u32) -> Result<Haptic, IntegerOrSdlError> { 11 use crate::common::IntegerOrSdlError::*; 12 let joystick_index = r#try!(validate_int(joystick_index, "joystick_index")); 13 14 let haptic = unsafe { 15 let joystick = sys::SDL_JoystickOpen(joystick_index); 16 sys::SDL_HapticOpenFromJoystick(joystick) 17 }; 18 19 if haptic.is_null() { 20 Err(SdlError(get_error())) 21 } else { 22 unsafe { sys::SDL_HapticRumbleInit(haptic) }; 23 Ok(Haptic { 24 subsystem: self.clone(), 25 raw: haptic, 26 }) 27 } 28 } 29 } 30 31 /// Wrapper around the `SDL_Haptic` object 32 pub struct Haptic { 33 subsystem: HapticSubsystem, 34 raw: *mut sys::SDL_Haptic, 35 } 36 37 38 impl Haptic { 39 #[inline] subsystem(&self) -> &HapticSubsystem40 pub fn subsystem(&self) -> &HapticSubsystem { &self.subsystem } 41 42 /// Run a simple rumble effect on the haptic device. rumble_play(&mut self, strength: f32, duration: u32)43 pub fn rumble_play(&mut self, strength: f32, duration: u32) { 44 unsafe { sys::SDL_HapticRumblePlay(self.raw, strength, duration) }; 45 } 46 47 /// Stop the simple rumble on the haptic device. rumble_stop(&mut self)48 pub fn rumble_stop(&mut self) { 49 unsafe { sys::SDL_HapticRumbleStop(self.raw) }; 50 } 51 } 52 53 54 impl Drop for Haptic { drop(&mut self)55 fn drop(&mut self) { 56 unsafe { sys::SDL_HapticClose(self.raw) } 57 } 58 } 59