From c131e73ce1d1b80ff40776f1ac47f2b78c943e56 Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Sat, 23 May 2020 11:45:25 +0300 Subject: [PATCH] Support controll remove/add events for SDL2 frontend Former-commit-id: b0e97f9c9b3f0b32140f1425159088270be6a433 Former-commit-id: b158c5c4ac491561f33f71d2bcd5cf069d55b343 --- platform/rustboyadvance-sdl2/src/main.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/platform/rustboyadvance-sdl2/src/main.rs b/platform/rustboyadvance-sdl2/src/main.rs index 1f2d962..3e908de 100644 --- a/platform/rustboyadvance-sdl2/src/main.rs +++ b/platform/rustboyadvance-sdl2/src/main.rs @@ -162,7 +162,7 @@ fn main() -> Result<(), Box> { .filter(|&id| controller_subsystem.is_game_controller(id)) .collect::>(); - let _active_controller = match available_controllers.first() { + let mut active_controller = match available_controllers.first() { Some(&id) => { let controller = controller_subsystem.open(id)?; info!("Found game controller: {}", controller.name()); @@ -295,6 +295,27 @@ fn main() -> Result<(), Box> { Event::ControllerAxisMotion { axis, value, .. } => { input.borrow_mut().on_axis_motion(axis, value); } + Event::ControllerDeviceRemoved { which, .. } => { + let removed = if let Some(active_controller) = &active_controller { + active_controller.instance_id() == (which as i32) + } else { + false + }; + if removed { + let name = active_controller + .and_then(|controller| Some(controller.name())) + .unwrap(); + info!("Removing game controller: {}", name); + active_controller = None; + } + } + Event::ControllerDeviceAdded { which, .. } => { + if active_controller.is_none() { + let controller = controller_subsystem.open(which)?; + info!("Adding game controller: {}", controller.name()); + active_controller = Some(controller); + } + } Event::Quit { .. } => break 'running, Event::DropFile { filename, .. } => { // load the new rom