Skip to content

Commit 8ced128

Browse files
authored
Merge pull request #1455 from PARTYMANX/master
Add player index functions for game controller and joystick
2 parents 5f65b60 + bbd1f6a commit 8ced128

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

src/sdl2/controller.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ impl GameControllerSubsystem {
112112
}
113113
}
114114

115+
/// Return the instance ID of the controller with player index `player_index`.
116+
#[doc(alias = "SDL_GameControllerFromPlayerIndex")]
117+
pub fn instance_id_for_player_index(&self, player_index: u32) -> Result<Option<u32>, IntegerOrSdlError> {
118+
let player_index = validate_int(player_index, "player_index")?;
119+
120+
let controller = unsafe { sys::SDL_GameControllerFromPlayerIndex(player_index) };
121+
122+
if controller.is_null() {
123+
Ok(None)
124+
} else {
125+
let result = unsafe {
126+
let joystick = sys::SDL_GameControllerGetJoystick(controller);
127+
sys::SDL_JoystickInstanceID(joystick)
128+
};
129+
130+
if result < 0 {
131+
// Should only fail if the joystick is NULL.
132+
panic!("{}", get_error())
133+
} else {
134+
Ok(Some(result as u32))
135+
}
136+
}
137+
}
138+
115139
/// If state is `true` controller events are processed, otherwise
116140
/// they're ignored.
117141
#[doc(alias = "SDL_GameControllerEventState")]
@@ -545,6 +569,32 @@ impl GameController {
545569
}
546570
}
547571

572+
/// Set player index for game controller or `None` to clear the player index and turn off player LEDs.
573+
#[doc(alias = "SDL_GameControllerSetPlayerIndex")]
574+
pub fn set_player_index(&mut self, player_index: Option<u32>) -> Result<(), IntegerOrSdlError> {
575+
let player_index = match player_index {
576+
None => -1,
577+
Some(player_index) => validate_int(player_index, "player_index")?,
578+
};
579+
580+
unsafe { sys::SDL_GameControllerSetPlayerIndex(self.raw, player_index) };
581+
582+
Ok(())
583+
}
584+
585+
/// Get player index for game controller or `None` if it's not available.
586+
#[doc(alias = "SDL_GameControllerGetPlayerIndex")]
587+
pub fn get_player_index(&self) -> Option<u32> {
588+
let player_index = unsafe { sys::SDL_GameControllerGetPlayerIndex(self.raw) };
589+
590+
// if index is -1 (or less than 0), controller has no player
591+
if player_index < 0 {
592+
None
593+
} else {
594+
Some(player_index as u32)
595+
}
596+
}
597+
548598
/// Query whether a game controller has an LED.
549599
#[doc(alias = "SDL_GameControllerHasLED")]
550600
pub fn has_led(&self) -> bool {

src/sdl2/joystick.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,42 @@ impl JoystickSubsystem {
6060
}
6161
}
6262

63+
/// Return the player index of the joystick with index `device_index`.
64+
#[doc(alias = "SDL_JoystickGetDevicePlayerIndex")]
65+
pub fn player_index_for_device_id(&self, device_index: u32) -> Result<Option<u32>, IntegerOrSdlError> {
66+
let device_index = validate_int(device_index, "device_index")?;
67+
68+
let player_index = unsafe { sys::SDL_JoystickGetDevicePlayerIndex(device_index) };
69+
70+
// if index is -1 (or less than 0), joystick has no player
71+
if player_index < 0 {
72+
Ok(None)
73+
} else {
74+
Ok(Some(player_index as u32))
75+
}
76+
}
77+
78+
/// Return the instance ID of the joystick with player index `player_index`.
79+
#[doc(alias = "SDL_JoystickFromPlayerIndex")]
80+
pub fn instance_id_for_player_index(&self, player_index: u32) -> Result<Option<u32>, IntegerOrSdlError> {
81+
let player_index = validate_int(player_index, "player_index")?;
82+
83+
let joystick = unsafe { sys::SDL_JoystickFromPlayerIndex(player_index) };
84+
85+
if joystick.is_null() {
86+
Ok(None)
87+
} else {
88+
let result = unsafe { sys::SDL_JoystickInstanceID(joystick) };
89+
90+
if result < 0 {
91+
// Should only fail if the joystick is NULL.
92+
panic!("{}", get_error())
93+
} else {
94+
Ok(Some(result as u32))
95+
}
96+
}
97+
}
98+
6399
/// Get the GUID for the joystick at index `joystick_index`
64100
#[doc(alias = "SDL_JoystickGetDeviceGUID")]
65101
pub fn device_guid(&self, joystick_index: u32) -> Result<Guid, IntegerOrSdlError> {
@@ -482,6 +518,32 @@ impl Joystick {
482518
Ok(())
483519
}
484520
}
521+
522+
/// Set player index for joystick or `None` to clear the player index and turn off player LEDs.
523+
#[doc(alias = "SDL_JoystickSetPlayerIndex")]
524+
pub fn set_player_index(&mut self, player_index: Option<u32>) -> Result<(), IntegerOrSdlError> {
525+
let player_index = match player_index {
526+
None => -1,
527+
Some(player_index) => validate_int(player_index, "player_index")?,
528+
};
529+
530+
unsafe { sys::SDL_JoystickSetPlayerIndex(self.raw, player_index) };
531+
532+
Ok(())
533+
}
534+
535+
/// Get player index for joystick or `None` if it's not available.
536+
#[doc(alias = "SDL_JoystickGetPlayerIndex")]
537+
pub fn get_player_index(&self) -> Option<u32> {
538+
let player_index = unsafe { sys::SDL_JoystickGetPlayerIndex(self.raw) };
539+
540+
// if index is -1 (or less than 0), joystick has no player
541+
if player_index < 0 {
542+
None
543+
} else {
544+
Some(player_index as u32)
545+
}
546+
}
485547
}
486548

487549
impl Drop for Joystick {

0 commit comments

Comments
 (0)