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