|
| 1 | +use crate::sys; |
| 2 | + |
| 3 | +use crate::common::{validate_int, IntegerOrSdlError}; |
| 4 | +use crate::get_error; |
| 5 | +use crate::SensorSubsystem; |
| 6 | +use libc::c_char; |
| 7 | +use std::ffi::CStr; |
| 8 | +use sys::SDL_SensorGetData; |
| 9 | +use sys::SDL_SensorType; |
| 10 | + |
| 11 | +impl SensorSubsystem { |
| 12 | + /// Retrieve the total number of attached joysticks *and* controllers identified by SDL. |
| 13 | + #[doc(alias = "SDL_NumSensors")] |
| 14 | + pub fn num_sensors(&self) -> Result<u32, String> { |
| 15 | + let result = unsafe { sys::SDL_NumSensors() }; |
| 16 | + |
| 17 | + if result >= 0 { |
| 18 | + Ok(result as u32) |
| 19 | + } else { |
| 20 | + Err(get_error()) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /// Attempt to open the joystick at index `joystick_index` and return it. |
| 25 | + #[doc(alias = "SDL_SensorOpen")] |
| 26 | + pub fn open(&self, sensor_index: u32) -> Result<Sensor, IntegerOrSdlError> { |
| 27 | + use crate::common::IntegerOrSdlError::*; |
| 28 | + let sensor_index = validate_int(sensor_index, "sensor_index")?; |
| 29 | + |
| 30 | + let sensor = unsafe { sys::SDL_SensorOpen(sensor_index) }; |
| 31 | + |
| 32 | + if sensor.is_null() { |
| 33 | + Err(SdlError(get_error())) |
| 34 | + } else { |
| 35 | + Ok(Sensor { |
| 36 | + subsystem: self.clone(), |
| 37 | + raw: sensor, |
| 38 | + }) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Force joystick update when not using the event loop |
| 43 | + #[inline] |
| 44 | + #[doc(alias = "SDL_SensorUpdate")] |
| 45 | + pub fn update(&self) { |
| 46 | + unsafe { sys::SDL_SensorUpdate() }; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Debug, Clone, Copy)] |
| 51 | +pub enum SensorType { |
| 52 | + Unknown, |
| 53 | + Gyroscope, |
| 54 | + Accelerometer, |
| 55 | +} |
| 56 | + |
| 57 | +impl Into<SDL_SensorType> for SensorType { |
| 58 | + fn into(self) -> SDL_SensorType { |
| 59 | + match self { |
| 60 | + SensorType::Unknown => SDL_SensorType::SDL_SENSOR_UNKNOWN, |
| 61 | + SensorType::Gyroscope => SDL_SensorType::SDL_SENSOR_GYRO, |
| 62 | + SensorType::Accelerometer => SDL_SensorType::SDL_SENSOR_ACCEL, |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Wrapper around the `SDL_Joystick` object |
| 68 | +pub struct Sensor { |
| 69 | + subsystem: SensorSubsystem, |
| 70 | + raw: *mut sys::SDL_Sensor, |
| 71 | +} |
| 72 | + |
| 73 | +impl Sensor { |
| 74 | + #[inline] |
| 75 | + pub const fn subsystem(&self) -> &SensorSubsystem { |
| 76 | + &self.subsystem |
| 77 | + } |
| 78 | + |
| 79 | + /// Return the name of the joystick or an empty string if no name |
| 80 | + /// is found. |
| 81 | + #[doc(alias = "SDL_SensorGetName")] |
| 82 | + pub fn name(&self) -> String { |
| 83 | + let name = unsafe { sys::SDL_SensorGetName(self.raw) }; |
| 84 | + |
| 85 | + c_str_to_string(name) |
| 86 | + } |
| 87 | + |
| 88 | + #[doc(alias = "SDL_SensorGetInstanceID")] |
| 89 | + pub fn instance_id(&self) -> u32 { |
| 90 | + let result = unsafe { sys::SDL_SensorGetInstanceID(self.raw) }; |
| 91 | + |
| 92 | + if result < 0 { |
| 93 | + // Should only fail if the joystick is NULL. |
| 94 | + panic!("{}", get_error()) |
| 95 | + } else { |
| 96 | + result as u32 |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[doc(alias = "SDL_SensorGetType")] |
| 101 | + pub fn sensor_type(&self) -> SensorType { |
| 102 | + let result = unsafe { sys::SDL_SensorGetType(self.raw) }; |
| 103 | + |
| 104 | + match result { |
| 105 | + sys::SDL_SensorType::SDL_SENSOR_INVALID => { |
| 106 | + panic!("{}", get_error()) |
| 107 | + } |
| 108 | + sys::SDL_SensorType::SDL_SENSOR_UNKNOWN => SensorType::Unknown, |
| 109 | + sys::SDL_SensorType::SDL_SENSOR_ACCEL => SensorType::Accelerometer, |
| 110 | + sys::SDL_SensorType::SDL_SENSOR_GYRO => SensorType::Gyroscope, |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + #[doc(alias = "SDL_SensorGetType")] |
| 115 | + pub fn get_data(&self) -> Result<SensorData, IntegerOrSdlError> { |
| 116 | + let mut data = [0f32; 16]; |
| 117 | + let result = unsafe { SDL_SensorGetData(self.raw, data.as_mut_ptr(), data.len() as i32) }; |
| 118 | + |
| 119 | + if result != 0 { |
| 120 | + Err(IntegerOrSdlError::SdlError(get_error())) |
| 121 | + } else { |
| 122 | + Ok(match self.sensor_type() { |
| 123 | + SensorType::Gyroscope => SensorData::Accel([data[0], data[1], data[2]]), |
| 124 | + SensorType::Accelerometer => SensorData::Accel([data[0], data[1], data[2]]), |
| 125 | + SensorType::Unknown => SensorData::Unknown(data), |
| 126 | + }) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +#[derive(Debug, Clone, Copy)] |
| 132 | +pub enum SensorData { |
| 133 | + Gyro([f32; 3]), |
| 134 | + Accel([f32; 3]), |
| 135 | + Unknown([f32; 16]), |
| 136 | +} |
| 137 | + |
| 138 | +impl Drop for Sensor { |
| 139 | + #[doc(alias = "SDL_SensorClose")] |
| 140 | + fn drop(&mut self) { |
| 141 | + unsafe { sys::SDL_SensorClose(self.raw) } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/// Convert C string `c_str` to a String. Return an empty string if |
| 146 | +/// `c_str` is NULL. |
| 147 | +fn c_str_to_string(c_str: *const c_char) -> String { |
| 148 | + if c_str.is_null() { |
| 149 | + String::new() |
| 150 | + } else { |
| 151 | + unsafe { |
| 152 | + CStr::from_ptr(c_str as *const _) |
| 153 | + .to_str() |
| 154 | + .unwrap() |
| 155 | + .to_owned() |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments