1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
mod backends;
mod types;

pub use types::*;

#[cfg(not(any(target_family = "wasm", target_os = "android")))]
use backends::gilrs::GilrsBackend as Backend;

#[cfg(any(target_family = "wasm", target_os = "android"))]
use backends::dummy::DummyBackend as Backend;

pub struct GamepadEngine {
    backend: Box<dyn crate::backends::GamepadEngineBackend>,
}
impl GamepadEngine {
    /// Instantiates gamepad engine, begins polling for input
    pub fn new() -> Self {
        GamepadEngine {
            backend: Box::new(Backend::new()),
        }
    }

    /// Polls for input and updates all gamepad states
    pub fn update(&mut self) -> Result<(), GamepadError> {
        self.backend.update()?;

        Ok(())
    }

    pub fn gamepads(&self) -> &Vec<GamepadState> {
        self.backend.gamepads()
    }

    pub fn gamepads_mut(&mut self) -> &mut Vec<GamepadState> {
        self.backend.gamepads_mut()
    }
}

unsafe impl Send for GamepadEngine {}
unsafe impl Sync for GamepadEngine {}