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 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//! ```
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 (XInput) | ✓ | ✓ | ✓ |
62//! | OS X | ✓ | ✓ | ✕ |
63//! | Wasm | ✓ | ✓ | n/a |
64//! | Android | ✕ | ✕ | ✕ |
65//!
66//! Controller layout
67//! -----------------
68//!
69//! 
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//!
88//! Platform specific notes
89//! ======================
90//!
91//! Linux/BSD (evdev)
92//! -----
93//!
94//! With evdev, GilRs read (and write, in case of force feedback) directly from appropriate
95//! `/dev/input/event*` file. This mean that user have to have read and write access to this file.
96//! On most distros it shouldn't be a problem, but if it is, you will have to create udev rule.
97//! On FreeBSD generic HID gamepads use hgame(4) and special use Linux driver via `webcamd`.
98//!
99//! To build GilRs, you will need pkg-config and libudev .pc file. On some distributions this file
100//! is packaged in separate archive (e.g., `libudev-dev` in Debian, `libudev-devd` in FreeBSD).
101//!
102//! Wasm
103//! ----
104//!
105//! Wasm implementation uses stdweb, or wasm-bindgen with the wasm-bindgen feature.
106//! For stdweb, you will need [cargo-web](https://github.com/koute/cargo-web) to build gilrs for
107//! wasm32-unknown-unknown. For wasm-bindgen, you will need the wasm-bindgen cli or a tool like
108//! [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).
109//! Unlike other platforms, events are only generated when you call `Gilrs::next_event()`.
110
111#[macro_use]
112extern crate log;
113
114mod constants;
115mod gamepad;
116mod mapping;
117mod utils;
118
119pub mod ev;
120pub mod ff;
121
122pub use crate::ev::filter::Filter;
123pub use crate::ev::{Axis, Button, Event, EventType};
124pub use crate::gamepad::{
125 ConnectedGamepadsIterator, Error, Gamepad, GamepadId, Gilrs, GilrsBuilder, MappingSource,
126 PowerInfo,
127};
128pub use crate::mapping::{MappingData as Mapping, MappingError};