From c95b83d86f9edd540f7dfafa95ba86766796f878 Mon Sep 17 00:00:00 2001 From: RockasMockas <6405564+RockasMockas@users.noreply.github.com> Date: Sun, 6 Oct 2024 18:24:07 +0200 Subject: [PATCH] Start of gilrs.rs --- Cargo.lock | 76 ++++++++++-- framework_crates/bones_framework/src/input.rs | 1 + .../bones_framework/src/input/gilrs.rs | 117 ++++++++++++++++++ 3 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 framework_crates/bones_framework/src/input/gilrs.rs diff --git a/Cargo.lock b/Cargo.lock index dfe58520c2..45bc9ff5ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -709,7 +709,7 @@ dependencies = [ "bevy_log", "bevy_time", "bevy_utils 0.11.3", - "gilrs", + "gilrs 0.10.9", "thiserror", ] @@ -1480,6 +1480,7 @@ dependencies = [ "fluent-langneg", "futures-lite 2.3.0", "ggrs", + "gilrs 0.11.0", "glam 0.24.2", "hex", "image 0.24.9", @@ -1933,6 +1934,16 @@ dependencies = [ "libc", ] +[[package]] +name = "core-foundation" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b55271e5c8c478ad3f38ad24ef34923091e0548492a266d19b3c0b4d82574c63" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -1946,7 +1957,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2581bbab3b8ffc6fcbd550bf46c355135d16e9ff2a6ea032ad6b9bf1d7efe4fb" dependencies = [ "bitflags 1.3.2", - "core-foundation", + "core-foundation 0.9.4", "core-graphics-types", "foreign-types 0.3.2", "libc", @@ -1959,7 +1970,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" dependencies = [ "bitflags 1.3.2", - "core-foundation", + "core-foundation 0.9.4", "core-graphics-types", "foreign-types 0.5.0", "libc", @@ -1972,7 +1983,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" dependencies = [ "bitflags 1.3.2", - "core-foundation", + "core-foundation 0.9.4", "libc", ] @@ -3201,7 +3212,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfb8c78963a8856a5b10015c9349176ff5edbc8095384d52aada467a848bc03a" dependencies = [ "fnv", - "gilrs-core", + "gilrs-core 0.5.15", + "log", + "uuid", + "vec_map", +] + +[[package]] +name = "gilrs" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb2c998745a3c1ac90f64f4f7b3a54219fd3612d7705e7798212935641ed18f" +dependencies = [ + "fnv", + "gilrs-core 0.6.0", "log", "uuid", "vec_map", @@ -3213,7 +3237,7 @@ version = "0.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "732dadc05170599ddec9a89653f10d7a2af54da9181b3fa6e2bd49907ec8f7e4" dependencies = [ - "core-foundation", + "core-foundation 0.9.4", "inotify 0.10.2", "io-kit-sys", "js-sys", @@ -3228,6 +3252,27 @@ dependencies = [ "windows 0.58.0", ] +[[package]] +name = "gilrs-core" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "495af945e45efd6386227613cd9fb7bd7c43d3c095040e30c5304c489e6abed5" +dependencies = [ + "core-foundation 0.10.0", + "inotify 0.11.0", + "io-kit-sys", + "js-sys", + "libc", + "libudev-sys", + "log", + "nix 0.29.0", + "uuid", + "vec_map", + "wasm-bindgen", + "web-sys", + "windows 0.58.0", +] + [[package]] name = "gimli" version = "0.31.0" @@ -3867,6 +3912,17 @@ dependencies = [ "libc", ] +[[package]] +name = "inotify" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3" +dependencies = [ + "bitflags 2.6.0", + "inotify-sys", + "libc", +] + [[package]] name = "inotify-sys" version = "0.1.5" @@ -6462,7 +6518,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ "bitflags 2.6.0", - "core-foundation", + "core-foundation 0.9.4", "core-foundation-sys", "libc", "security-framework-sys", @@ -7148,7 +7204,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" dependencies = [ "bitflags 2.6.0", - "core-foundation", + "core-foundation 0.9.4", "system-configuration-sys", ] @@ -8000,7 +8056,7 @@ version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db67ae75a9405634f5882791678772c94ff5f16a66535aae186e26aa0841fc8b" dependencies = [ - "core-foundation", + "core-foundation 0.9.4", "home", "jni", "log", @@ -8581,7 +8637,7 @@ dependencies = [ "android-activity", "bitflags 1.3.2", "cfg_aliases 0.1.1", - "core-foundation", + "core-foundation 0.9.4", "core-graphics 0.22.3", "dispatch", "instant", diff --git a/framework_crates/bones_framework/src/input.rs b/framework_crates/bones_framework/src/input.rs index bf1189d72b..8cbaa8bb90 100644 --- a/framework_crates/bones_framework/src/input.rs +++ b/framework_crates/bones_framework/src/input.rs @@ -5,6 +5,7 @@ use bones_schema::HasSchema; use self::prelude::{GamepadInputs, KeyboardInputs}; pub mod gamepad; +pub mod gilrs; pub mod keyboard; pub mod mouse; pub mod window; diff --git a/framework_crates/bones_framework/src/input/gilrs.rs b/framework_crates/bones_framework/src/input/gilrs.rs new file mode 100644 index 0000000000..973fb07a2b --- /dev/null +++ b/framework_crates/bones_framework/src/input/gilrs.rs @@ -0,0 +1,117 @@ +//! Gamepad input session, systems, and resources. + +use crate::prelude::*; +use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter, Gilrs as GilrsContext}; +use once_cell::sync::Lazy; +use std::sync::{Arc, Mutex}; + +/// Name of the default bones input session +pub const DEFAULT_BONES_INPUT_SESSION: &str = "BONES_INPUT"; + +/// Lazy-initialized GilrsContext +static GILRS_CONTEXT: Lazy>> = Lazy::new(|| { + Arc::new(Mutex::new( + GilrsContext::new().expect("Failed to initialize GilrsContext"), + )) +}); + +/// Sets up gamepad-related resources and the default bones input session +pub fn game_plugin(game: &mut Game) { + let session = game.sessions.create(DEFAULT_BONES_INPUT_SESSION); + // Input doesn't do any rendering + session.visible = false; + session + .stages + .add_system_to_stage(CoreStage::First, process_gamepad_events); +} + +fn process_gamepad_events(mut gamepad_inputs: ResMut) { + let mut gilrs = GILRS_CONTEXT.lock().unwrap(); + while let Some(gilrs_event) = gilrs + .next_event() + .filter_ev(&axis_dpad_to_button, &mut gilrs) + { + gilrs.update(&gilrs_event); + + let gamepad = usize::from(gilrs_event.id) as u32; + match gilrs_event.event { + EventType::Connected => { + let _pad = gilrs.gamepad(gilrs_event.id); + gamepad_inputs.gamepad_events.push(GamepadEvent::Connection( + GamepadConnectionEvent { + gamepad, + event: GamepadConnectionEventKind::Connected, + }, + )); + } + EventType::Disconnected => { + gamepad_inputs.gamepad_events.push(GamepadEvent::Connection( + GamepadConnectionEvent { + gamepad, + event: GamepadConnectionEventKind::Disconnected, + }, + )); + } + EventType::ButtonChanged(gilrs_button, value, _) => { + if let Some(button) = convert_button(gilrs_button) { + gamepad_inputs + .gamepad_events + .push(GamepadEvent::Button(GamepadButtonEvent { + gamepad, + button, + value, + })); + } + } + EventType::AxisChanged(gilrs_axis, value, _) => { + if let Some(axis) = convert_axis(gilrs_axis) { + gamepad_inputs + .gamepad_events + .push(GamepadEvent::Axis(GamepadAxisEvent { + gamepad, + axis, + value, + })); + } + } + _ => (), + }; + } +} + +fn convert_button(button: gilrs::Button) -> Option { + match button { + gilrs::Button::South => Some(GamepadButton::South), + gilrs::Button::East => Some(GamepadButton::East), + gilrs::Button::North => Some(GamepadButton::North), + gilrs::Button::West => Some(GamepadButton::West), + gilrs::Button::C => Some(GamepadButton::C), + gilrs::Button::Z => Some(GamepadButton::Z), + gilrs::Button::LeftTrigger => Some(GamepadButton::LeftTrigger), + gilrs::Button::LeftTrigger2 => Some(GamepadButton::LeftTrigger2), + gilrs::Button::RightTrigger => Some(GamepadButton::RightTrigger), + gilrs::Button::RightTrigger2 => Some(GamepadButton::RightTrigger2), + gilrs::Button::Select => Some(GamepadButton::Select), + gilrs::Button::Start => Some(GamepadButton::Start), + gilrs::Button::Mode => Some(GamepadButton::Mode), + gilrs::Button::LeftThumb => Some(GamepadButton::LeftThumb), + gilrs::Button::RightThumb => Some(GamepadButton::RightThumb), + gilrs::Button::DPadUp => Some(GamepadButton::DPadUp), + gilrs::Button::DPadDown => Some(GamepadButton::DPadDown), + gilrs::Button::DPadLeft => Some(GamepadButton::DPadLeft), + gilrs::Button::DPadRight => Some(GamepadButton::DPadRight), + gilrs::Button::Unknown => None, + } +} + +fn convert_axis(axis: gilrs::Axis) -> Option { + match axis { + gilrs::Axis::LeftStickX => Some(GamepadAxis::LeftStickX), + gilrs::Axis::LeftStickY => Some(GamepadAxis::LeftStickY), + gilrs::Axis::LeftZ => Some(GamepadAxis::LeftZ), + gilrs::Axis::RightStickX => Some(GamepadAxis::RightStickX), + gilrs::Axis::RightStickY => Some(GamepadAxis::RightStickY), + gilrs::Axis::RightZ => Some(GamepadAxis::RightZ), + gilrs::Axis::Unknown | gilrs::Axis::DPadX | gilrs::Axis::DPadY => None, + } +}