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 warning message to controller docs about automatic SDL_GameControllerClose call on drop #1425

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions examples/game-controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ fn main() -> Result<(), String> {
println!("{} joysticks available", available);

// Iterate over all available joysticks and look for game controllers.
// Notice that the opened game controller instance must be kept alive because
// the controller gets automatically closed on drop.
let mut controller = (0..available)
.find_map(|id| {
if !game_controller_subsystem.is_game_controller(id) {
Expand Down
7 changes: 7 additions & 0 deletions src/sdl2/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl GameControllerSubsystem {
/// Attempt to open the controller at index `joystick_index` and return it.
/// Controller IDs are the same as joystick IDs and the maximum number can
/// be retrieved using the `SDL_NumJoysticks` function.
///
/// **Warning!** The returned [GameController] instance calls automatically `SDL_GameControllerClose`
/// on [GameController::drop] which means you must keep the [GameController] instance alive in order to keep the
/// controller functional and connected.
#[doc(alias = "SDL_GameControllerOpen")]
pub fn open(&self, joystick_index: u32) -> Result<GameController, IntegerOrSdlError> {
use crate::common::IntegerOrSdlError::*;
Expand Down Expand Up @@ -384,6 +388,9 @@ pub enum MappingStatus {
}

/// Wrapper around the `SDL_GameController` object
///
/// **Warning!** Dropping this struct calls automatically `SDL_GameControllerClose` on [GameController::drop] which means you must keep
/// the [GameController] instance alive in order to keep the controller functional and connected.
pub struct GameController {
subsystem: GameControllerSubsystem,
raw: *mut sys::SDL_GameController,
Expand Down
Loading