stick/
raw.rs

1// Stick
2// Copyright © 2017-2021 Jeron Aldaron Lau.
3//
4// Licensed under any of:
5// - Apache License, Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
6// - MIT License (https://mit-license.org/)
7// - Boost Software License, Version 1.0 (https://www.boost.org/LICENSE_1_0.txt)
8// At your choosing (See accompanying files LICENSE_APACHE_2_0.txt,
9// LICENSE_MIT.txt and LICENSE_BOOST_1_0.txt).
10
11#![allow(unsafe_code)]
12
13use crate::{Event, Remap};
14use std::task::{Context, Poll};
15
16#[cfg_attr(
17    any(target_arch = "wasm32", target_arch = "asmjs"),
18    cfg_attr(target_os = "wasi", path = "raw/wasi.rs"),
19    cfg_attr(target_os = "ardaku", path = "raw/ardaku.rs"),
20    cfg_attr(
21        any(target_os = "unknown", target_os = "emscripten"),
22        path = "raw/dom.rs"
23    )
24)]
25#[cfg_attr(
26    not(any(target_arch = "wasm32", target_arch = "asmjs")),
27    cfg_attr(target_os = "linux", path = "raw/linux.rs"),
28    cfg_attr(target_os = "android", path = "raw/android.rs"),
29    cfg_attr(target_os = "macos", path = "raw/macos.rs"),
30    cfg_attr(target_os = "ios", path = "raw/ios.rs"),
31    cfg_attr(target_os = "windows", path = "raw/windows.rs"),
32    cfg_attr(target_os = "fuchsia", path = "raw/fuchsia.rs"),
33    cfg_attr(target_os = "redox", path = "raw/redox.rs"),
34    cfg_attr(
35        any(
36            target_os = "freebsd",
37            target_os = "dragonfly",
38            target_os = "bitrig",
39            target_os = "openbsd",
40            target_os = "netbsd"
41        ),
42        path = "raw/bsd.rs",
43    )
44)]
45mod ffi;
46
47/// Global state for when the system implementation can fail.
48struct FakeGlobal;
49
50impl Global for FakeGlobal {}
51
52/// A Listener that never returns any controllers for unsupported platforms.
53struct FakeListener;
54
55impl Listener for FakeListener {
56    fn poll(&mut self, _cx: &mut Context<'_>) -> Poll<crate::Controller> {
57        Poll::Pending
58    }
59}
60
61/// Controller Listener Implementation
62pub(crate) trait Listener: Send {
63    /// Poll for controllers.
64    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<crate::Controller>;
65}
66
67/// Controller Implementation
68pub(crate) trait Controller: Send {
69    /// The hardware identifier for this controller.
70    fn id(&self) -> u64 {
71        0
72    }
73    /// Poll for events.
74    fn poll(&mut self, _cx: &mut Context<'_>) -> Poll<Event> {
75        Poll::Pending
76    }
77    /// Stereo rumble effect (left is low frequency, right is high frequency).
78    fn rumble(&mut self, _left: f32, _right: f32) {}
79    /// Get the name of this controller.
80    fn name(&self) -> &str {
81        "Unknown"
82    }
83    /// Floating Point Translation for pressure axis/buttons.
84    fn pressure(&self, input: f64) -> f64 {
85        input
86    }
87    /// Floating Point Translation for full axis values.
88    fn axis(&self, input: f64) -> f64 {
89        input
90    }
91}
92
93/// Thread local global state implementation.
94pub(crate) trait Global: std::any::Any {
95    /// Enable all events (when window comes in focus).
96    fn enable(&self) {}
97    /// Disable all events (when window leaves focus).
98    fn disable(&self) {}
99    /// Create a new listener.
100    fn listener(&self, _remap: Remap) -> Box<dyn Listener> {
101        Box::new(FakeListener)
102    }
103}
104
105thread_local! {
106    pub(crate) static GLOBAL: Box<dyn Global> = ffi::global();
107}