Skip to content

Commit 7bae279

Browse files
authored
Merge pull request #1213 from moralrecordings/bindings_update
Update bindings to SDL 2.0.20
2 parents 808e489 + a5cc934 commit 7bae279

File tree

8 files changed

+10864
-3439
lines changed

8 files changed

+10864
-3439
lines changed

examples/game-controller.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,36 @@ fn main() -> Result<(), String> {
8787
}
8888
Event::ControllerButtonDown { button, .. } => println!("Button {:?} down", button),
8989
Event::ControllerButtonUp { button, .. } => println!("Button {:?} up", button),
90-
Event::ControllerTouchpadDown { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} down finger:{finger} x:{x} y:{y}"),
91-
Event::ControllerTouchpadMotion { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} move finger:{finger} x:{x} y:{y}"),
92-
Event::ControllerTouchpadUp { touchpad, finger, x, y, ..} => println!("Touchpad {touchpad} up finger:{finger} x:{x} y:{y}"),
90+
Event::ControllerTouchpadDown {
91+
touchpad,
92+
finger,
93+
x,
94+
y,
95+
..
96+
} => println!(
97+
"Touchpad {} down finger:{} x:{} y:{}",
98+
touchpad, finger, x, y
99+
),
100+
Event::ControllerTouchpadMotion {
101+
touchpad,
102+
finger,
103+
x,
104+
y,
105+
..
106+
} => println!(
107+
"Touchpad {} move finger:{} x:{} y:{}",
108+
touchpad, finger, x, y
109+
),
110+
Event::ControllerTouchpadUp {
111+
touchpad,
112+
finger,
113+
x,
114+
y,
115+
..
116+
} => println!(
117+
"Touchpad {} up finger:{} x:{} y:{}",
118+
touchpad, finger, x, y
119+
),
93120
Event::Quit { .. } => break,
94121
_ => (),
95122
}

sdl2-sys/sdl_bindings.rs

+10,429-3,434
Large diffs are not rendered by default.

src/sdl2/audio.rs

+43
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,49 @@ impl AudioSubsystem {
180180
}
181181
}
182182
}
183+
184+
#[doc(alias = "SDL_GetAudioDeviceSpec")]
185+
pub fn audio_playback_device_spec(&self, index: u32) -> Result<AudioSpec, String> {
186+
let mut spec = sys::SDL_AudioSpec {
187+
freq: 0,
188+
format: 0,
189+
channels: 0,
190+
silence: 0,
191+
samples: 0,
192+
padding: 0,
193+
size: 0,
194+
callback: None,
195+
userdata: ptr::null_mut(),
196+
};
197+
198+
let result = unsafe { sys::SDL_GetAudioDeviceSpec(index as c_int, 0, &mut spec) };
199+
if result != 0 {
200+
Err(get_error())
201+
} else {
202+
Ok(AudioSpec::convert_from_ll(spec))
203+
}
204+
}
205+
#[doc(alias = "SDL_GetAudioDeviceSpec")]
206+
pub fn audio_capture_device_spec(&self, index: u32) -> Result<AudioSpec, String> {
207+
let mut spec = sys::SDL_AudioSpec {
208+
freq: 0,
209+
format: 0,
210+
channels: 0,
211+
silence: 0,
212+
samples: 0,
213+
padding: 0,
214+
size: 0,
215+
callback: None,
216+
userdata: ptr::null_mut(),
217+
};
218+
219+
let result = unsafe { sys::SDL_GetAudioDeviceSpec(index as c_int, 1, &mut spec) };
220+
if result != 0 {
221+
Err(get_error())
222+
} else {
223+
Ok(AudioSpec::convert_from_ll(spec))
224+
}
225+
}
183226
}
184227

185228
#[repr(i32)]

src/sdl2/controller.rs

+92-1
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,88 @@ impl GameController {
507507
Ok(())
508508
}
509509
}
510+
511+
/// Start a rumble effect in the game controller's triggers.
512+
#[doc(alias = "SDL_GameControllerRumbleTriggers")]
513+
pub fn set_rumble_triggers(
514+
&mut self,
515+
left_rumble: u16,
516+
right_rumble: u16,
517+
duration_ms: u32,
518+
) -> Result<(), IntegerOrSdlError> {
519+
let result = unsafe {
520+
sys::SDL_GameControllerRumbleTriggers(self.raw, left_rumble, right_rumble, duration_ms)
521+
};
522+
523+
if result != 0 {
524+
Err(IntegerOrSdlError::SdlError(get_error()))
525+
} else {
526+
Ok(())
527+
}
528+
}
529+
530+
/// Query whether a game controller has an LED.
531+
#[doc(alias = "SDL_GameControllerHasLED")]
532+
pub fn has_led(&self) -> bool {
533+
let result = unsafe { sys::SDL_GameControllerHasLED(self.raw) };
534+
535+
match result {
536+
sys::SDL_bool::SDL_FALSE => false,
537+
sys::SDL_bool::SDL_TRUE => true,
538+
}
539+
}
540+
541+
/// Query whether a game controller has rumble support.
542+
#[doc(alias = "SDL_GameControllerHasRumble")]
543+
pub fn has_rumble(&self) -> bool {
544+
let result = unsafe { sys::SDL_GameControllerHasRumble(self.raw) };
545+
546+
match result {
547+
sys::SDL_bool::SDL_FALSE => false,
548+
sys::SDL_bool::SDL_TRUE => true,
549+
}
550+
}
551+
552+
/// Query whether a game controller has rumble support on triggers.
553+
#[doc(alias = "SDL_GameControllerHasRumbleTriggers")]
554+
pub fn has_rumble_triggers(&self) -> bool {
555+
let result = unsafe { sys::SDL_GameControllerHasRumbleTriggers(self.raw) };
556+
557+
match result {
558+
sys::SDL_bool::SDL_FALSE => false,
559+
sys::SDL_bool::SDL_TRUE => true,
560+
}
561+
}
562+
563+
/// Update a game controller's LED color.
564+
#[doc(alias = "SDL_GameControllerSetLED")]
565+
pub fn set_led(&mut self, red: u8, green: u8, blue: u8) -> Result<(), IntegerOrSdlError> {
566+
let result = unsafe { sys::SDL_GameControllerSetLED(self.raw, red, green, blue) };
567+
568+
if result != 0 {
569+
Err(IntegerOrSdlError::SdlError(get_error()))
570+
} else {
571+
Ok(())
572+
}
573+
}
574+
575+
/// Send a controller specific effect packet.
576+
#[doc(alias = "SDL_GameControllerSendEffect")]
577+
pub fn send_effect(&mut self, data: &[u8]) -> Result<(), String> {
578+
let result = unsafe {
579+
sys::SDL_GameControllerSendEffect(
580+
self.raw,
581+
data.as_ptr() as *const libc::c_void,
582+
data.len() as i32,
583+
)
584+
};
585+
586+
if result != 0 {
587+
Err(get_error())
588+
} else {
589+
Ok(())
590+
}
591+
}
510592
}
511593

512594
#[cfg(feature = "hidapi")]
@@ -532,7 +614,7 @@ impl GameController {
532614
}
533615
}
534616

535-
#[doc(alias = "SDL_GameControllerHasSensor")]
617+
#[doc(alias = "SDL_GameControllerSetSensorEnabled")]
536618
pub fn sensor_set_enabled(
537619
&self,
538620
sensor_type: crate::sensor::SensorType,
@@ -557,6 +639,15 @@ impl GameController {
557639
}
558640
}
559641

642+
/// Get the data rate (number of events per second) of a game controller sensor.
643+
#[doc(alias = "SDL_GameControllerGetSensorDataRate")]
644+
pub fn sensor_get_data_rate(&self, sensor_type: SensorType) -> f32 {
645+
let result =
646+
unsafe { sys::SDL_GameControllerGetSensorDataRate(self.raw, sensor_type.into()) };
647+
648+
Ok(result)
649+
}
650+
560651
/// Get data from a sensor.
561652
///
562653
/// The number of data points depends on the sensor. Both Gyroscope and

src/sdl2/event.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ pub enum WindowEvent {
487487
Close,
488488
TakeFocus,
489489
HitTest,
490+
ICCProfChanged,
491+
DisplayChanged(i32),
490492
}
491493

492494
impl WindowEvent {
@@ -510,6 +512,8 @@ impl WindowEvent {
510512
14 => WindowEvent::Close,
511513
15 => WindowEvent::TakeFocus,
512514
16 => WindowEvent::HitTest,
515+
17 => WindowEvent::ICCProfChanged,
516+
18 => WindowEvent::DisplayChanged(data1),
513517
_ => WindowEvent::None,
514518
}
515519
}
@@ -533,6 +537,8 @@ impl WindowEvent {
533537
WindowEvent::Close => (14, 0, 0),
534538
WindowEvent::TakeFocus => (15, 0, 0),
535539
WindowEvent::HitTest => (16, 0, 0),
540+
WindowEvent::ICCProfChanged => (17, 0, 0),
541+
WindowEvent::DisplayChanged(d1) => (18, d1, 0),
536542
}
537543
}
538544

@@ -554,7 +560,9 @@ impl WindowEvent {
554560
| (Self::FocusLost, Self::FocusLost)
555561
| (Self::Close, Self::Close)
556562
| (Self::TakeFocus, Self::TakeFocus)
557-
| (Self::HitTest, Self::HitTest) => true,
563+
| (Self::HitTest, Self::HitTest)
564+
| (Self::ICCProfChanged, Self::ICCProfChanged)
565+
| (Self::DisplayChanged(_), Self::DisplayChanged(_)) => true,
558566
_ => false,
559567
}
560568
}
@@ -664,6 +672,8 @@ pub enum Event {
664672
x: i32,
665673
y: i32,
666674
direction: MouseWheelDirection,
675+
precise_x: f32,
676+
precise_y: f32,
667677
},
668678

669679
JoyAxisMotion {
@@ -1193,6 +1203,8 @@ impl Event {
11931203
x,
11941204
y,
11951205
direction,
1206+
precise_x,
1207+
precise_y,
11961208
} => {
11971209
let event = sys::SDL_MouseWheelEvent {
11981210
type_: SDL_EventType::SDL_MOUSEWHEEL as u32,
@@ -1202,6 +1214,8 @@ impl Event {
12021214
x,
12031215
y,
12041216
direction: direction.to_ll(),
1217+
preciseX: precise_x,
1218+
preciseY: precise_y,
12051219
};
12061220
unsafe {
12071221
ptr::copy(&event, ret.as_mut_ptr() as *mut sys::SDL_MouseWheelEvent, 1);
@@ -1664,6 +1678,8 @@ impl Event {
16641678
x: event.x,
16651679
y: event.y,
16661680
direction: mouse::MouseWheelDirection::from_ll(event.direction),
1681+
precise_x: event.preciseX,
1682+
precise_y: event.preciseY,
16671683
}
16681684
}
16691685

@@ -2336,6 +2352,8 @@ impl Event {
23362352
/// timestamp: 0,
23372353
/// window_id: 0,
23382354
/// which: 0,
2355+
/// precise_x: 0.0,
2356+
/// precise_y: 0.0,
23392357
/// x: 0,
23402358
/// y: 0,
23412359
/// direction: MouseWheelDirection::Normal,
@@ -2905,6 +2923,8 @@ mod test {
29052923
x: 23,
29062924
y: 91,
29072925
direction: MouseWheelDirection::Flipped,
2926+
precise_x: 1.6,
2927+
precise_y: 2.7,
29082928
};
29092929
let e2 = Event::from_ll(e.clone().to_ll().unwrap());
29102930
assert_eq!(e, e2);

src/sdl2/joystick.rs

+82
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,88 @@ impl Joystick {
400400
Ok(())
401401
}
402402
}
403+
404+
/// Start a rumble effect in the joystick's triggers.
405+
#[doc(alias = "SDL_JoystickRumbleTriggers")]
406+
pub fn set_rumble_triggers(
407+
&mut self,
408+
left_rumble: u16,
409+
right_rumble: u16,
410+
duration_ms: u32,
411+
) -> Result<(), IntegerOrSdlError> {
412+
let result = unsafe {
413+
sys::SDL_JoystickRumbleTriggers(self.raw, left_rumble, right_rumble, duration_ms)
414+
};
415+
416+
if result != 0 {
417+
Err(IntegerOrSdlError::SdlError(get_error()))
418+
} else {
419+
Ok(())
420+
}
421+
}
422+
423+
/// Query whether a joystick has an LED.
424+
#[doc(alias = "SDL_JoystickHasLED")]
425+
pub fn has_led(&self) -> bool {
426+
let result = unsafe { sys::SDL_JoystickHasLED(self.raw) };
427+
428+
match result {
429+
sys::SDL_bool::SDL_FALSE => false,
430+
sys::SDL_bool::SDL_TRUE => true,
431+
}
432+
}
433+
434+
/// Query whether a joystick has rumble support.
435+
#[doc(alias = "SDL_JoystickHasRumble")]
436+
pub fn has_rumble(&self) -> bool {
437+
let result = unsafe { sys::SDL_JoystickHasRumble(self.raw) };
438+
439+
match result {
440+
sys::SDL_bool::SDL_FALSE => false,
441+
sys::SDL_bool::SDL_TRUE => true,
442+
}
443+
}
444+
445+
/// Query whether a joystick has rumble support on triggers.
446+
#[doc(alias = "SDL_JoystickHasRumbleTriggers")]
447+
pub fn has_rumble_triggers(&self) -> bool {
448+
let result = unsafe { sys::SDL_JoystickHasRumbleTriggers(self.raw) };
449+
450+
match result {
451+
sys::SDL_bool::SDL_FALSE => false,
452+
sys::SDL_bool::SDL_TRUE => true,
453+
}
454+
}
455+
456+
/// Update a joystick's LED color.
457+
#[doc(alias = "SDL_JoystickSetLED")]
458+
pub fn set_led(&mut self, red: u8, green: u8, blue: u8) -> Result<(), IntegerOrSdlError> {
459+
let result = unsafe { sys::SDL_JoystickSetLED(self.raw, red, green, blue) };
460+
461+
if result != 0 {
462+
Err(IntegerOrSdlError::SdlError(get_error()))
463+
} else {
464+
Ok(())
465+
}
466+
}
467+
468+
/// Send a joystick specific effect packet.
469+
#[doc(alias = "SDL_JoystickSendEffect")]
470+
pub fn send_effect(&mut self, data: &[u8]) -> Result<(), IntegerOrSdlError> {
471+
let result = unsafe {
472+
sys::SDL_JoystickSendEffect(
473+
self.raw,
474+
data.as_ptr() as *const libc::c_void,
475+
data.len() as i32,
476+
)
477+
};
478+
479+
if result != 0 {
480+
Err(IntegerOrSdlError::SdlError(get_error()))
481+
} else {
482+
Ok(())
483+
}
484+
}
403485
}
404486

405487
impl Drop for Joystick {

0 commit comments

Comments
 (0)