@@ -15,6 +15,7 @@ use std::convert::TryInto;
15
15
use crate :: common:: IntegerOrSdlError ;
16
16
use crate :: get_error;
17
17
use crate :: guid:: Guid ;
18
+ use crate :: joystick:: JoystickId ;
18
19
use crate :: sys;
19
20
use crate :: Error ;
20
21
use crate :: GamepadSubsystem ;
@@ -57,35 +58,40 @@ impl error::Error for AddMappingError {
57
58
58
59
impl GamepadSubsystem {
59
60
/// 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 > {
62
63
let mut num_gamepads: i32 = 0 ;
63
64
unsafe {
64
65
// see: https://github.com/libsdl-org/SDL/blob/main/docs/README-migration.md#sdl_joystickh
65
66
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 ( ) {
67
68
Err ( get_error ( ) )
68
69
} 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
+ }
69
75
sys:: stdinc:: SDL_free ( gamepad_ids as * mut c_void ) ;
70
- Ok ( num_gamepads as u32 )
76
+ Ok ( instances )
71
77
}
72
78
}
73
79
}
74
80
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.
76
82
#[ inline]
77
83
#[ 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 ) }
80
86
}
81
87
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.
83
89
/// Controller IDs are the same as joystick IDs and the maximum number can
84
90
/// be retrieved using the `SDL_GetJoysticks` function.
85
91
#[ 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 > {
87
93
use crate :: common:: IntegerOrSdlError :: * ;
88
- let controller = unsafe { sys:: gamepad:: SDL_OpenGamepad ( joystick_index ) } ;
94
+ let controller = unsafe { sys:: gamepad:: SDL_OpenGamepad ( joystick_id ) } ;
89
95
90
96
if controller. is_null ( ) {
91
97
Err ( SdlError ( get_error ( ) ) )
@@ -97,11 +103,11 @@ impl GamepadSubsystem {
97
103
}
98
104
}
99
105
100
- /// Return the name of the controller at index `joystick_index `.
106
+ /// Return the name of the controller at index `joystick_id `.
101
107
#[ 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 > {
103
109
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 ) } ;
105
111
106
112
if c_str. is_null ( ) {
107
113
Err ( SdlError ( get_error ( ) ) )
0 commit comments