@@ -60,6 +60,42 @@ impl JoystickSubsystem {
60
60
}
61
61
}
62
62
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
+
63
99
/// Get the GUID for the joystick at index `joystick_index`
64
100
#[ doc( alias = "SDL_JoystickGetDeviceGUID" ) ]
65
101
pub fn device_guid ( & self , joystick_index : u32 ) -> Result < Guid , IntegerOrSdlError > {
@@ -482,6 +518,32 @@ impl Joystick {
482
518
Ok ( ( ) )
483
519
}
484
520
}
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
+ }
485
547
}
486
548
487
549
impl Drop for Joystick {
0 commit comments