Skip to content

Commit f3e4c0b

Browse files
authored
Replace GamepadSubsystem::num_gamepads in favor of GamepadSubsystem::gamepads (#141)
* Replace GamepadSubsystem::num_gamepads in favor of GamepadSubsystem::gamepads * Fix haptic example * Rename JoystickInstance to JoystickId
1 parent d4c4bc7 commit f3e4c0b

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

examples/game-controller.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
88
let sdl_context = sdl3::init()?;
99
let gamepad_subsystem = sdl_context.gamepad()?;
1010

11-
let available = gamepad_subsystem
12-
.num_gamepads()
11+
let gamepad_joysticks_ids = gamepad_subsystem
12+
.gamepads()
1313
.map_err(|e| format!("can't enumerate gamepads: {}", e))?;
1414

15-
println!("{} gamepads available", available);
15+
println!("{} gamepads available", gamepad_joysticks_ids.len());
1616

1717
// Iterate over all available joysticks and look for game controllers.
18-
let mut controller = (0..available)
18+
let mut controller = gamepad_joysticks_ids
19+
.into_iter()
1920
.find_map(|id| {
2021
println!("Attempting to open gamepad {}", id);
2122

examples/haptic.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1212
println!("{} joysticks available", joysticks.len());
1313

1414
// Iterate over all available joysticks and stop once we manage to open one.
15-
let (_joystick, joystick_id) = joysticks
15+
let joystick_id = joysticks
1616
.into_iter()
17-
.find_map(|joystick| {
18-
let id = joystick.id;
19-
match joystick_subsystem.open(joystick) {
20-
Ok(c) => {
21-
println!("Success: opened \"{}\"", c.name());
22-
Some((c, id))
23-
}
24-
Err(e) => {
25-
println!("failed: {:?}", e);
26-
None
27-
}
17+
.find_map(|id| match joystick_subsystem.open(id) {
18+
Ok(c) => {
19+
println!("Success: opened \"{}\"", c.name());
20+
Some(id)
21+
}
22+
Err(e) => {
23+
println!("failed: {:?}", e);
24+
None
2825
}
2926
})
3027
.expect("Couldn't open any joystick");

src/sdl3/gamepad.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::convert::TryInto;
1515
use crate::common::IntegerOrSdlError;
1616
use crate::get_error;
1717
use crate::guid::Guid;
18+
use crate::joystick::JoystickId;
1819
use crate::sys;
1920
use crate::Error;
2021
use crate::GamepadSubsystem;
@@ -57,35 +58,40 @@ impl error::Error for AddMappingError {
5758

5859
impl GamepadSubsystem {
5960
/// Retrieve the total number of attached gamepads identified by SDL.
60-
#[doc(alias = "SDL_GetJoysticks")]
61-
pub fn num_gamepads(&self) -> Result<u32, Error> {
61+
#[doc(alias = "SDL_GetGamepads")]
62+
pub fn gamepads(&self) -> Result<Vec<JoystickId>, Error> {
6263
let mut num_gamepads: i32 = 0;
6364
unsafe {
6465
// see: https://github.com/libsdl-org/SDL/blob/main/docs/README-migration.md#sdl_joystickh
6566
let gamepad_ids = sys::gamepad::SDL_GetGamepads(&mut num_gamepads);
66-
if (gamepad_ids as *mut sys::gamepad::SDL_Gamepad).is_null() {
67+
if gamepad_ids.is_null() {
6768
Err(get_error())
6869
} else {
70+
let mut instances = Vec::new();
71+
for i in 0..num_gamepads {
72+
let id = *gamepad_ids.offset(i as isize);
73+
instances.push(id);
74+
}
6975
sys::stdinc::SDL_free(gamepad_ids as *mut c_void);
70-
Ok(num_gamepads as u32)
76+
Ok(instances)
7177
}
7278
}
7379
}
7480

75-
/// Return true if the joystick at index `joystick_index` is a game controller.
81+
/// Return true if the joystick at index `joystick_id` is a game controller.
7682
#[inline]
7783
#[doc(alias = "SDL_IsGamepad")]
78-
pub fn is_game_controller(&self, joystick_index: u32) -> bool {
79-
unsafe { sys::gamepad::SDL_IsGamepad(joystick_index) }
84+
pub fn is_game_controller(&self, joystick_id: JoystickId) -> bool {
85+
unsafe { sys::gamepad::SDL_IsGamepad(joystick_id) }
8086
}
8187

82-
/// Attempt to open the controller at index `joystick_index` and return it.
88+
/// Attempt to open the controller at index `joystick_id` and return it.
8389
/// Controller IDs are the same as joystick IDs and the maximum number can
8490
/// be retrieved using the `SDL_GetJoysticks` function.
8591
#[doc(alias = "SDL_OpenGamepad")]
86-
pub fn open(&self, joystick_index: u32) -> Result<Gamepad, IntegerOrSdlError> {
92+
pub fn open(&self, joystick_id: JoystickId) -> Result<Gamepad, IntegerOrSdlError> {
8793
use crate::common::IntegerOrSdlError::*;
88-
let controller = unsafe { sys::gamepad::SDL_OpenGamepad(joystick_index) };
94+
let controller = unsafe { sys::gamepad::SDL_OpenGamepad(joystick_id) };
8995

9096
if controller.is_null() {
9197
Err(SdlError(get_error()))
@@ -97,11 +103,11 @@ impl GamepadSubsystem {
97103
}
98104
}
99105

100-
/// Return the name of the controller at index `joystick_index`.
106+
/// Return the name of the controller at index `joystick_id`.
101107
#[doc(alias = "SDL_GetGamepadNameForID")]
102-
pub fn name_for_index(&self, joystick_index: u32) -> Result<String, IntegerOrSdlError> {
108+
pub fn name_for_index(&self, joystick_id: JoystickId) -> Result<String, IntegerOrSdlError> {
103109
use crate::common::IntegerOrSdlError::*;
104-
let c_str = unsafe { sys::gamepad::SDL_GetGamepadNameForID(joystick_index) };
110+
let c_str = unsafe { sys::gamepad::SDL_GetGamepadNameForID(joystick_id) };
105111

106112
if c_str.is_null() {
107113
Err(SdlError(get_error()))

src/sdl3/joystick.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ use sys::joystick::SDL_JoystickID;
1414
use sys::power::{SDL_PowerState, SDL_POWERSTATE_UNKNOWN};
1515
use sys::stdinc::SDL_free;
1616

17-
pub struct JoystickInstance {
18-
pub id: SDL_JoystickID,
19-
name: String,
20-
path: String,
21-
}
17+
pub type JoystickId = SDL_JoystickID;
2218

2319
impl JoystickSubsystem {
2420
/// Get joystick instance IDs and names.
2521
#[doc(alias = "SDL_GetJoysticks")]
26-
pub fn joysticks(&self) -> Result<Vec<JoystickInstance>, Error> {
22+
pub fn joysticks(&self) -> Result<Vec<JoystickId>, Error> {
2723
let mut num_joysticks: i32 = 0;
2824
unsafe {
2925
let joystick_ids = sys::joystick::SDL_GetJoysticks(&mut num_joysticks);
@@ -33,10 +29,7 @@ impl JoystickSubsystem {
3329
let mut instances = Vec::new();
3430
for i in 0..num_joysticks {
3531
let id = *joystick_ids.offset(i as isize);
36-
37-
let name = c_str_to_string(sys::joystick::SDL_GetJoystickNameForID(id));
38-
let path = c_str_to_string(sys::joystick::SDL_GetJoystickPathForID(id));
39-
instances.push(JoystickInstance { id, name, path });
32+
instances.push(id);
4033
}
4134
SDL_free(joystick_ids as *mut c_void);
4235
Ok(instances)
@@ -46,9 +39,9 @@ impl JoystickSubsystem {
4639

4740
/// Attempt to open the joystick at index `joystick_index` and return it.
4841
#[doc(alias = "SDL_OpenJoystick")]
49-
pub fn open(&self, joystick_instance: JoystickInstance) -> Result<Joystick, IntegerOrSdlError> {
42+
pub fn open(&self, joystick_id: JoystickId) -> Result<Joystick, IntegerOrSdlError> {
5043
use crate::common::IntegerOrSdlError::*;
51-
let joystick = unsafe { sys::joystick::SDL_OpenJoystick(joystick_instance.id) };
44+
let joystick = unsafe { sys::joystick::SDL_OpenJoystick(joystick_id) };
5245

5346
if joystick.is_null() {
5447
Err(SdlError(get_error()))

0 commit comments

Comments
 (0)