Skip to content

Update bindings to SDL 2.0.20 #1213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 25, 2022
Merged
33 changes: 30 additions & 3 deletions examples/game-controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,36 @@ fn main() -> Result<(), String> {
}
Event::ControllerButtonDown { button, .. } => println!("Button {:?} down", button),
Event::ControllerButtonUp { button, .. } => println!("Button {:?} up", button),
Event::ControllerTouchpadDown { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} down finger:{finger} x:{x} y:{y}"),
Event::ControllerTouchpadMotion { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} move finger:{finger} x:{x} y:{y}"),
Event::ControllerTouchpadUp { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} up finger:{finger} x:{x} y:{y}"),
Event::ControllerTouchpadDown {
touchpad,
finger,
x,
y,
..
} => println!(
"Touchpad {} down finger:{} x:{} y:{}",
touchpad, finger, x, y
),
Event::ControllerTouchpadMotion {
touchpad,
finger,
x,
y,
..
} => println!(
"Touchpad {} move finger:{} x:{} y:{}",
touchpad, finger, x, y
),
Event::ControllerTouchpadUp {
touchpad,
finger,
x,
y,
..
} => println!(
"Touchpad {} up finger:{} x:{} y:{}",
touchpad, finger, x, y
),
Event::Quit { .. } => break,
_ => (),
}
Expand Down
13,863 changes: 10,429 additions & 3,434 deletions sdl2-sys/sdl_bindings.rs

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions src/sdl2/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,49 @@ impl AudioSubsystem {
}
}
}

#[doc(alias = "SDL_GetAudioDeviceSpec")]
pub fn audio_playback_device_spec(&self, index: u32) -> Result<AudioSpec, String> {
let mut spec = sys::SDL_AudioSpec {
freq: 0,
format: 0,
channels: 0,
silence: 0,
samples: 0,
padding: 0,
size: 0,
callback: None,
userdata: ptr::null_mut(),
};

let result = unsafe { sys::SDL_GetAudioDeviceSpec(index as c_int, 0, &mut spec) };
if result != 0 {
Err(get_error())
} else {
Ok(AudioSpec::convert_from_ll(spec))
}
}
#[doc(alias = "SDL_GetAudioDeviceSpec")]
pub fn audio_capture_device_spec(&self, index: u32) -> Result<AudioSpec, String> {
let mut spec = sys::SDL_AudioSpec {
freq: 0,
format: 0,
channels: 0,
silence: 0,
samples: 0,
padding: 0,
size: 0,
callback: None,
userdata: ptr::null_mut(),
};

let result = unsafe { sys::SDL_GetAudioDeviceSpec(index as c_int, 1, &mut spec) };
if result != 0 {
Err(get_error())
} else {
Ok(AudioSpec::convert_from_ll(spec))
}
}
}

#[repr(i32)]
Expand Down
93 changes: 92 additions & 1 deletion src/sdl2/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,88 @@ impl GameController {
Ok(())
}
}

/// Start a rumble effect in the game controller's triggers.
#[doc(alias = "SDL_GameControllerRumbleTriggers")]
pub fn set_rumble_triggers(
&mut self,
left_rumble: u16,
right_rumble: u16,
duration_ms: u32,
) -> Result<(), IntegerOrSdlError> {
let result = unsafe {
sys::SDL_GameControllerRumbleTriggers(self.raw, left_rumble, right_rumble, duration_ms)
};

if result != 0 {
Err(IntegerOrSdlError::SdlError(get_error()))
} else {
Ok(())
}
}

/// Query whether a game controller has an LED.
#[doc(alias = "SDL_GameControllerHasLED")]
pub fn has_led(&self) -> bool {
let result = unsafe { sys::SDL_GameControllerHasLED(self.raw) };

match result {
sys::SDL_bool::SDL_FALSE => false,
sys::SDL_bool::SDL_TRUE => true,
}
}

/// Query whether a game controller has rumble support.
#[doc(alias = "SDL_GameControllerHasRumble")]
pub fn has_rumble(&self) -> bool {
let result = unsafe { sys::SDL_GameControllerHasRumble(self.raw) };

match result {
sys::SDL_bool::SDL_FALSE => false,
sys::SDL_bool::SDL_TRUE => true,
}
}

/// Query whether a game controller has rumble support on triggers.
#[doc(alias = "SDL_GameControllerHasRumbleTriggers")]
pub fn has_rumble_triggers(&self) -> bool {
let result = unsafe { sys::SDL_GameControllerHasRumbleTriggers(self.raw) };

match result {
sys::SDL_bool::SDL_FALSE => false,
sys::SDL_bool::SDL_TRUE => true,
}
}

/// Update a game controller's LED color.
#[doc(alias = "SDL_GameControllerSetLED")]
pub fn set_led(&mut self, red: u8, green: u8, blue: u8) -> Result<(), IntegerOrSdlError> {
let result = unsafe { sys::SDL_GameControllerSetLED(self.raw, red, green, blue) };

if result != 0 {
Err(IntegerOrSdlError::SdlError(get_error()))
} else {
Ok(())
}
}

/// Send a controller specific effect packet.
#[doc(alias = "SDL_GameControllerSendEffect")]
pub fn send_effect(&mut self, data: &[u8]) -> Result<(), String> {
let result = unsafe {
sys::SDL_GameControllerSendEffect(
self.raw,
data.as_ptr() as *const libc::c_void,
data.len() as i32,
)
};

if result != 0 {
Err(get_error())
} else {
Ok(())
}
}
}

#[cfg(feature = "hidapi")]
Expand All @@ -532,7 +614,7 @@ impl GameController {
}
}

#[doc(alias = "SDL_GameControllerHasSensor")]
#[doc(alias = "SDL_GameControllerSetSensorEnabled")]
pub fn sensor_set_enabled(
&self,
sensor_type: crate::sensor::SensorType,
Expand All @@ -557,6 +639,15 @@ impl GameController {
}
}

/// Get the data rate (number of events per second) of a game controller sensor.
#[doc(alias = "SDL_GameControllerGetSensorDataRate")]
pub fn sensor_get_data_rate(&self, sensor_type: SensorType) -> f32 {
let result =
unsafe { sys::SDL_GameControllerGetSensorDataRate(self.raw, sensor_type.into()) };

Ok(result)
}

/// Get data from a sensor.
///
/// The number of data points depends on the sensor. Both Gyroscope and
Expand Down
22 changes: 21 additions & 1 deletion src/sdl2/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ pub enum WindowEvent {
Close,
TakeFocus,
HitTest,
ICCProfChanged,
DisplayChanged(i32),
}

impl WindowEvent {
Expand All @@ -510,6 +512,8 @@ impl WindowEvent {
14 => WindowEvent::Close,
15 => WindowEvent::TakeFocus,
16 => WindowEvent::HitTest,
17 => WindowEvent::ICCProfChanged,
18 => WindowEvent::DisplayChanged(data1),
_ => WindowEvent::None,
}
}
Expand All @@ -533,6 +537,8 @@ impl WindowEvent {
WindowEvent::Close => (14, 0, 0),
WindowEvent::TakeFocus => (15, 0, 0),
WindowEvent::HitTest => (16, 0, 0),
WindowEvent::ICCProfChanged => (17, 0, 0),
WindowEvent::DisplayChanged(d1) => (18, d1, 0),
}
}

Expand All @@ -554,7 +560,9 @@ impl WindowEvent {
| (Self::FocusLost, Self::FocusLost)
| (Self::Close, Self::Close)
| (Self::TakeFocus, Self::TakeFocus)
| (Self::HitTest, Self::HitTest) => true,
| (Self::HitTest, Self::HitTest)
| (Self::ICCProfChanged, Self::ICCProfChanged)
| (Self::DisplayChanged(_), Self::DisplayChanged(_)) => true,
_ => false,
}
}
Expand Down Expand Up @@ -664,6 +672,8 @@ pub enum Event {
x: i32,
y: i32,
direction: MouseWheelDirection,
precise_x: f32,
precise_y: f32,
},

JoyAxisMotion {
Expand Down Expand Up @@ -1193,6 +1203,8 @@ impl Event {
x,
y,
direction,
precise_x,
precise_y,
} => {
let event = sys::SDL_MouseWheelEvent {
type_: SDL_EventType::SDL_MOUSEWHEEL as u32,
Expand All @@ -1202,6 +1214,8 @@ impl Event {
x,
y,
direction: direction.to_ll(),
preciseX: precise_x,
preciseY: precise_y,
};
unsafe {
ptr::copy(&event, ret.as_mut_ptr() as *mut sys::SDL_MouseWheelEvent, 1);
Expand Down Expand Up @@ -1664,6 +1678,8 @@ impl Event {
x: event.x,
y: event.y,
direction: mouse::MouseWheelDirection::from_ll(event.direction),
precise_x: event.preciseX,
precise_y: event.preciseY,
}
}

Expand Down Expand Up @@ -2336,6 +2352,8 @@ impl Event {
/// timestamp: 0,
/// window_id: 0,
/// which: 0,
/// precise_x: 0.0,
/// precise_y: 0.0,
/// x: 0,
/// y: 0,
/// direction: MouseWheelDirection::Normal,
Expand Down Expand Up @@ -2905,6 +2923,8 @@ mod test {
x: 23,
y: 91,
direction: MouseWheelDirection::Flipped,
precise_x: 1.6,
precise_y: 2.7,
};
let e2 = Event::from_ll(e.clone().to_ll().unwrap());
assert_eq!(e, e2);
Expand Down
82 changes: 82 additions & 0 deletions src/sdl2/joystick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,88 @@ impl Joystick {
Ok(())
}
}

/// Start a rumble effect in the joystick's triggers.
#[doc(alias = "SDL_JoystickRumbleTriggers")]
pub fn set_rumble_triggers(
&mut self,
left_rumble: u16,
right_rumble: u16,
duration_ms: u32,
) -> Result<(), IntegerOrSdlError> {
let result = unsafe {
sys::SDL_JoystickRumbleTriggers(self.raw, left_rumble, right_rumble, duration_ms)
};

if result != 0 {
Err(IntegerOrSdlError::SdlError(get_error()))
} else {
Ok(())
}
}

/// Query whether a joystick has an LED.
#[doc(alias = "SDL_JoystickHasLED")]
pub fn has_led(&self) -> bool {
let result = unsafe { sys::SDL_JoystickHasLED(self.raw) };

match result {
sys::SDL_bool::SDL_FALSE => false,
sys::SDL_bool::SDL_TRUE => true,
}
}

/// Query whether a joystick has rumble support.
#[doc(alias = "SDL_JoystickHasRumble")]
pub fn has_rumble(&self) -> bool {
let result = unsafe { sys::SDL_JoystickHasRumble(self.raw) };

match result {
sys::SDL_bool::SDL_FALSE => false,
sys::SDL_bool::SDL_TRUE => true,
}
}

/// Query whether a joystick has rumble support on triggers.
#[doc(alias = "SDL_JoystickHasRumbleTriggers")]
pub fn has_rumble_triggers(&self) -> bool {
let result = unsafe { sys::SDL_JoystickHasRumbleTriggers(self.raw) };

match result {
sys::SDL_bool::SDL_FALSE => false,
sys::SDL_bool::SDL_TRUE => true,
}
}

/// Update a joystick's LED color.
#[doc(alias = "SDL_JoystickSetLED")]
pub fn set_led(&mut self, red: u8, green: u8, blue: u8) -> Result<(), IntegerOrSdlError> {
let result = unsafe { sys::SDL_JoystickSetLED(self.raw, red, green, blue) };

if result != 0 {
Err(IntegerOrSdlError::SdlError(get_error()))
} else {
Ok(())
}
}

/// Send a joystick specific effect packet.
#[doc(alias = "SDL_JoystickSendEffect")]
pub fn send_effect(&mut self, data: &[u8]) -> Result<(), IntegerOrSdlError> {
let result = unsafe {
sys::SDL_JoystickSendEffect(
self.raw,
data.as_ptr() as *const libc::c_void,
data.len() as i32,
)
};

if result != 0 {
Err(IntegerOrSdlError::SdlError(get_error()))
} else {
Ok(())
}
}
}

impl Drop for Joystick {
Expand Down
Loading