18 releases

0.7.1 Mar 4, 2019
0.6.3 Jan 14, 2019
0.6.1 Jul 18, 2018
0.6.0 Feb 11, 2018
0.3.1 Sep 23, 2016

#36 in Game development

Download history 540/week @ 2019-01-21 415/week @ 2019-01-28 239/week @ 2019-02-04 238/week @ 2019-02-11 638/week @ 2019-02-18 576/week @ 2019-02-25 460/week @ 2019-03-04 575/week @ 2019-03-11 527/week @ 2019-03-18 630/week @ 2019-03-25 454/week @ 2019-04-01 443/week @ 2019-04-08 387/week @ 2019-04-15 372/week @ 2019-04-22 486/week @ 2019-04-29

1,987 downloads per month
Used in 3 crates

Apache-2.0/MIT

306KB
7.5K SLoC


lib.rs:

GilRs - Game Input Library for Rust

GilRs abstract platform specific APIs to provide unified interfaces for working with gamepads.

Main features:

  • Unified gamepad layout—buttons and axes are represented by familiar names
  • Support for SDL2 mappings including SDL_GAMECONTROLLERCONFIG environment variable which Steam uses
  • Hotplugging—GilRs will try to assign new IDs for new gamepads and reuse same ID for gamepads which reconnected
  • Force feedback (rumble)
  • Power information (is gamepad wired, current battery status)

Example

use gilrs::{Gilrs, Button, Event};

let mut gilrs = Gilrs::new().unwrap();

// Iterate over all connected gamepads
for (_id, gamepad) in gilrs.gamepads() {
    println!("{} is {:?}", gamepad.name(), gamepad.power_info());
}

let mut active_gamepad = None;

loop {
    // Examine new events
    while let Some(Event { id, event, time }) = gilrs.next_event() {
        println!("{:?} New event from {}: {:?}", time, id, event);
        active_gamepad = Some(id);
    }

    // You can also use cached gamepad state
    if let Some(gamepad) = active_gamepad.map(|id| gilrs.gamepad(id)) {
        if gamepad.is_pressed(Button::South) {
            println!("Button South is pressed (XBox - A, PS - X)");
        }
    }
    # break;
}

Supported features

Input Hotplugging Force feedback
Linux
Windows (XInput)
OS X
Wasm n/a
Android

Controller layout

Controller layout original image by nicefrog

Mappings

GilRs use SDL-compatible controller mappings to fix on Linux legacy drivers that doesn't follow Linux Gamepad API and to provide unified button layout for platforms that doesn't make any guarantees about it. The main source is SDL_GameControllerDB, but library also support loading mappings from environment variable SDL_GAMECONTROLLERCONFIG (which Steam use).

Platform specific notes

Linux

On Linux, GilRs read (and write, in case of force feedback) directly from appropriate /dev/input/event* file. This mean that user have to have read and write access to this file. On most distros it shouldn't be a problem, but if it is, you will have to create udev rule.

To build GilRs, you will need pkg-config and libudev .pc file. On some distributions this file is packaged in separate archive (for example libudev-dev in Debian).

Wasm

Wasm implementation uses stdweb, so you will need cargo-web to build gilrs for wasm32-unknown-unknown. Unlike other platforms, events are only generated when you call Gilrs::next_event().

Dependencies

~1.5MB
~21K SLoC