engine/presentation/src/input/joystick.rs

41 lines
873 B
Rust
Raw Normal View History

2025-03-03 08:47:37 +00:00
use anyhow::Result;
use sdl2::JoystickSubsystem;
pub struct Joystick {
_sdl2_joystick: sdl2::joystick::Joystick,
id: u32,
name: String,
}
impl Joystick {
pub fn new(joystick_subsystem: &JoystickSubsystem, id: u32) -> Result<Self> {
let sdl2_joystick = joystick_subsystem.open(id)?;
Ok(Self {
name: sdl2_joystick.name(),
id,
_sdl2_joystick: sdl2_joystick,
})
}
pub fn id(&self) -> u32 {
self.id
}
pub fn name(&self) -> &str {
&self.name
}
}
impl std::fmt::Debug for Joystick {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Joystick")
.field("id", &self.id)
.field("name", &self.name)
.finish()
}
}
unsafe impl Send for Joystick {}
unsafe impl Sync for Joystick {}