Skip to content
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

Add player index functions for game controller and joystick #1455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/sdl2/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<u32>, 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")]
Expand Down Expand Up @@ -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<u32>) -> 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<u32> {
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 {
Expand Down
62 changes: 62 additions & 0 deletions src/sdl2/joystick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<u32>, 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<Option<u32>, 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<Guid, IntegerOrSdlError> {
Expand Down Expand Up @@ -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<u32>) -> 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<u32> {
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 {
Expand Down