diff --git a/src/sdl2/controller.rs b/src/sdl2/controller.rs index 33c60ca94f..022619fd05 100644 --- a/src/sdl2/controller.rs +++ b/src/sdl2/controller.rs @@ -112,6 +112,30 @@ impl GameControllerSubsystem { } } + /// Return the instance ID of the controller with player index `player_index`. + #[doc(alias = "SDL_GameControllerFromPlayerIndex")] + pub fn instance_id_for_player_index(&self, player_index: u32) -> Result, IntegerOrSdlError> { + let player_index = validate_int(player_index, "player_index")?; + + let controller = unsafe { sys::SDL_GameControllerFromPlayerIndex(player_index) }; + + if controller.is_null() { + Ok(None) + } else { + let result = unsafe { + let joystick = sys::SDL_GameControllerGetJoystick(controller); + sys::SDL_JoystickInstanceID(joystick) + }; + + if result < 0 { + // Should only fail if the joystick is NULL. + panic!("{}", get_error()) + } else { + Ok(Some(result as u32)) + } + } + } + /// If state is `true` controller events are processed, otherwise /// they're ignored. #[doc(alias = "SDL_GameControllerEventState")] @@ -545,6 +569,32 @@ impl GameController { } } + /// Set player index for game controller or `None` to clear the player index and turn off player LEDs. + #[doc(alias = "SDL_GameControllerSetPlayerIndex")] + pub fn set_player_index(&mut self, player_index: Option) -> Result<(), IntegerOrSdlError> { + let player_index = match player_index { + None => -1, + Some(player_index) => validate_int(player_index, "player_index")?, + }; + + unsafe { sys::SDL_GameControllerSetPlayerIndex(self.raw, player_index) }; + + Ok(()) + } + + /// Get player index for game controller or `None` if it's not available. + #[doc(alias = "SDL_GameControllerGetPlayerIndex")] + pub fn get_player_index(&self) -> Option { + let player_index = unsafe { sys::SDL_GameControllerGetPlayerIndex(self.raw) }; + + // if index is -1, controller has no player + if player_index == -1 { + None + } else { + Some(player_index as u32) + } + } + /// Query whether a game controller has an LED. #[doc(alias = "SDL_GameControllerHasLED")] pub fn has_led(&self) -> bool { diff --git a/src/sdl2/joystick.rs b/src/sdl2/joystick.rs index 82e20b449a..6c50189200 100644 --- a/src/sdl2/joystick.rs +++ b/src/sdl2/joystick.rs @@ -60,6 +60,42 @@ impl JoystickSubsystem { } } + /// Return the player index of the joystick with index `device_index`. + #[doc(alias = "SDL_JoystickGetDevicePlayerIndex")] + pub fn player_index_for_device_id(&self, device_index: u32) -> Result, IntegerOrSdlError> { + let device_index = validate_int(device_index, "device_index")?; + + let player_index = unsafe { sys::SDL_JoystickGetDevicePlayerIndex(device_index) }; + + // if index is -1, joystick has no player + if player_index == -1 { + Ok(None) + } else { + Ok(Some(player_index as u32)) + } + } + + /// Return the instance ID of the joystick with player index `player_index`. + #[doc(alias = "SDL_JoystickFromPlayerIndex")] + pub fn instance_id_for_player_index(&self, player_index: u32) -> Result, IntegerOrSdlError> { + let player_index = validate_int(player_index, "player_index")?; + + let joystick = unsafe { sys::SDL_JoystickFromPlayerIndex(player_index) }; + + if joystick.is_null() { + Ok(None) + } else { + let result = unsafe { sys::SDL_JoystickInstanceID(joystick) }; + + if result < 0 { + // Should only fail if the joystick is NULL. + panic!("{}", get_error()) + } else { + Ok(Some(result as u32)) + } + } + } + /// Get the GUID for the joystick at index `joystick_index` #[doc(alias = "SDL_JoystickGetDeviceGUID")] pub fn device_guid(&self, joystick_index: u32) -> Result { @@ -482,6 +518,32 @@ impl Joystick { Ok(()) } } + + /// Set player index for joystick or `None` to clear the player index and turn off player LEDs. + #[doc(alias = "SDL_JoystickSetPlayerIndex")] + pub fn set_player_index(&mut self, player_index: Option) -> Result<(), IntegerOrSdlError> { + let player_index = match player_index { + None => -1, + Some(player_index) => validate_int(player_index, "player_index")?, + }; + + unsafe { sys::SDL_JoystickSetPlayerIndex(self.raw, player_index) }; + + Ok(()) + } + + /// Get player index for joystick or `None` if it's not available. + #[doc(alias = "SDL_JoystickGetPlayerIndex")] + pub fn get_player_index(&self) -> Option { + let player_index = unsafe { sys::SDL_JoystickGetPlayerIndex(self.raw) }; + + // if index is -1, joystick has no player + if player_index == -1 { + None + } else { + Some(player_index as u32) + } + } } impl Drop for Joystick {