gilrs/
lib.rs

1// Copyright 2016-2018 Mateusz Sieczko and other GilRs Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! GilRs - Game Input Library for Rust
9//! ===================================
10//!
11//! GilRs abstract platform specific APIs to provide unified interfaces for working with gamepads.
12//!
13//! Main features:
14//!
15//! - Unified gamepad layout—buttons and axes are represented by familiar names
16//! - Support for SDL2 mappings including `SDL_GAMECONTROLLERCONFIG` environment
17//!   variable which Steam uses
18//! - Hotplugging—GilRs will try to assign new IDs for new gamepads and reuse the same
19//!   ID for gamepads which reconnected
20//! - Force feedback (rumble)
21//! - Power information (is gamepad wired, current battery status)
22//!
23//! Example
24//! -------
25//!
26//! ```rust
27//! use gilrs::{Gilrs, Button, Event};
28//!
29//! let mut gilrs = Gilrs::new().unwrap();
30//!
31//! // Iterate over all connected gamepads
32//! for (_id, gamepad) in gilrs.gamepads() {
33//!     println!("{} is {:?}", gamepad.name(), gamepad.power_info());
34//! }
35//!
36//! let mut active_gamepad = None;
37//!
38//! loop {
39//!     // Examine new events
40//!     while let Some(Event { id, event, time, .. }) = gilrs.next_event() {
41//!         println!("{:?} New event from {}: {:?}", time, id, event);
42//!         active_gamepad = Some(id);
43//!     }
44//!
45//!     // You can also use cached gamepad state
46//!     if let Some(gamepad) = active_gamepad.map(|id| gilrs.gamepad(id)) {
47//!         if gamepad.is_pressed(Button::South) {
48//!             println!("Button South is pressed (XBox - A, PS - X)");
49//!         }
50//!     }
51//!     # break;
52//! }
53//! ```
54//!
55//! Supported features
56//! ------------------
57//!
58//! |                  | Input | Hotplugging | Force feedback |
59//! |------------------|:-----:|:-----------:|:--------------:|
60//! | Linux/BSD (evdev)|   ✓   |      ✓      |        ✓       |
61//! | Windows          |   ✓   |      ✓      |        ✓       |
62//! | OS X             |   ✓   |      ✓      |        ✕       |
63//! | Wasm             |   ✓   |      ✓      |       n/a      |
64//! | Android          |   ✕   |      ✕      |        ✕       |
65//!
66//! Controller layout
67//! -----------------
68//!
69//! ![Controller layout](https://gilrs-project.gitlab.io/gilrs/img/controller.svg)
70//! [original image by nicefrog](http://opengameart.org/content/generic-gamepad-template)
71//!
72//! Mappings
73//! --------
74//!
75//! GilRs use SDL-compatible controller mappings to fix on Linux legacy drivers that doesn't follow
76//! [Linux Gamepad API](https://www.kernel.org/doc/Documentation/input/gamepad.txt) and to provide
77//! unified button layout for platforms that doesn't make any guarantees about it. The main source
78//! is [SDL_GameControllerDB](https://github.com/gabomdq/SDL_GameControllerDB), but library also
79//! support loading mappings from environment variable `SDL_GAMECONTROLLERCONFIG` (which Steam
80//! use).
81//!
82//! Cargo features
83//! --------------
84//!
85//! - `serde-serialize` - enable deriving of serde's `Serialize` and `Deserialize` for
86//!   various types.
87//! - `wgi` - use Windows Gaming Input on Windows (enabled by default).
88//! - `xinput` - use XInput on Windows.
89//!
90//! Platform specific notes
91//! ======================
92//!
93//! Linux/BSD (evdev)
94//! -----
95//!
96//! With evdev, GilRs read (and write, in case of force feedback) directly from appropriate
97//! `/dev/input/event*` file. This mean that user have to have read and write access to this file.
98//! On most distros it shouldn't be a problem, but if it is, you will have to create udev rule.
99//! On FreeBSD generic HID gamepads use hgame(4) and special use Linux driver via `webcamd`.
100//!
101//! To build GilRs, you will need pkg-config and libudev .pc file. On some distributions this file
102//! is packaged in separate archive (e.g., `libudev-dev` in Debian, `libudev-devd` in FreeBSD).
103//!
104//! Windows
105//! -----
106//!
107//! Windows defaults to using Windows Gaming Input instead of XInput. If you need to use XInput you
108//! can disable the `wgi` feature (it's enabled by default) and enable the `xinput` feature.
109//!
110//! Windows Gaming Input requires an in focus window to be associated with the process to receive
111//! events. You can still switch back to using xInput by turning off default features and enabling
112//! the xinput feature.
113//!
114//! Note: Some (Older?) devices may still report inputs without a window but this is not the case
115//! for all devices so if you are writing a terminal based game, use the xinput feature instead.
116//!
117//! Wasm
118//! -----
119//!
120//! Wasm implementation uses stdweb, or wasm-bindgen with the wasm-bindgen feature.
121//! For stdweb, you will need [cargo-web](https://github.com/koute/cargo-web) to build gilrs for
122//! wasm32-unknown-unknown. For wasm-bindgen, you will need the wasm-bindgen cli or a tool like
123//! [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).
124//! Unlike other platforms, events are only generated when you call `Gilrs::next_event()`.
125
126#[macro_use]
127extern crate log;
128
129mod constants;
130mod gamepad;
131mod mapping;
132mod utils;
133
134pub mod ev;
135pub mod ff;
136
137pub use crate::ev::filter::Filter;
138pub use crate::ev::{Axis, Button, Event, EventType};
139pub use crate::gamepad::{
140    ConnectedGamepadsIterator, Error, Gamepad, GamepadId, Gilrs, GilrsBuilder, MappingSource,
141    PowerInfo,
142};
143
144#[cfg(target_os = "linux")]
145pub use crate::gamepad::LinuxGamepadExt;
146
147pub use crate::mapping::{MappingData as Mapping, MappingError};