rich_sdl2_rust/haptic/
mouse.rs

1//! A haptic device integrated with the mouse.
2
3use std::{marker::PhantomData, ops::Deref, ptr::NonNull};
4
5use crate::bind;
6
7use super::Haptic;
8
9/// A haptic device got from the mouse.
10#[derive(Debug)]
11pub struct MouseHaptic {
12    haptic: Haptic,
13}
14
15impl MouseHaptic {
16    /// Constructs if the mouse had the haptic device.
17    #[must_use]
18    pub fn new() -> Option<Self> {
19        let is_supported = unsafe { bind::SDL_MouseIsHaptic() as bind::SDL_bool == bind::SDL_TRUE };
20        if !is_supported {
21            return None;
22        }
23        let ptr = unsafe { bind::SDL_HapticOpenFromMouse() };
24        Some(Self {
25            haptic: Haptic {
26                ptr: NonNull::new(ptr).unwrap(),
27            },
28        })
29    }
30}
31
32impl Deref for MouseHaptic {
33    type Target = Haptic;
34
35    fn deref(&self) -> &Self::Target {
36        &self.haptic
37    }
38}
39
40impl Drop for MouseHaptic {
41    fn drop(&mut self) {
42        unsafe { bind::SDL_HapticClose(self.ptr.as_ptr()) }
43    }
44}