15 releases (9 breaking)

0.10.2 Sep 20, 2020
0.10.1 Jun 26, 2020
0.9.0 May 18, 2020
0.7.1 Jul 18, 2019
0.3.0 Feb 4, 2018

#8 in Hardware support

Download history 163/week @ 2020-06-28 17/week @ 2020-07-05 134/week @ 2020-07-12 29/week @ 2020-07-19 13/week @ 2020-07-26 15/week @ 2020-08-02 38/week @ 2020-08-09 77/week @ 2020-08-16 38/week @ 2020-08-23 50/week @ 2020-08-30 72/week @ 2020-09-06 31/week @ 2020-09-13 41/week @ 2020-09-20 56/week @ 2020-09-27 42/week @ 2020-10-04 30/week @ 2020-10-11

261 downloads per month
Used in 10 crates (2 directly)

Apache-2.0 OR Zlib

84KB
1.5K SLoC

Stick

Platform-agnostic asynchronous gamepad/joystick library for Rust

Build Status Docs crates.io

Stick supports getting controller input from a large variety of gamepads, joysticks, and other controllers, as well as rumble haptic effects.

Why Does Stick Exist?

The main reason is that I hadn't heard of gilrs when I started stick back in 2017 when gilrs was only a year old and had less than 500 all-time downloads. Now, I think there are many other reasons for stick to exist despite gilrs:

  • Executor-agnostic async/.await for gamepads, joysticks, etc (I recommend using the pasts crate for a simple single-threaded executor).
  • Low-level hotplugging support (you assign the gamepad ID's)
  • Meaningful Event Names (ActionA and ActionB instead of South and East)
  • Minimal dependencies
  • Dual licensed with the Zlib license (permission to use without attribution in the binary's UI) - making it great for game development.
  • Not game-specific, doesn't depend on a "standard gamepad" model (which doesn't work due to the variety of controllers in existence) - therefore can also be used in robotics, control centers, advanced flight simulations, etc.
  • Support more gamepads/joysticks than gilrs, and (WIP) unified support across platforms.

Platform Support

  • Linux

Planned Platform Support

  • Windows
  • MacOS
  • BSD
  • Redox
  • Fuchsia
  • Android
  • iOS
  • Web Assembly
  • Nintendo Switch (And other game consoles)
  • Others

Table of Contents

Getting Started

Add the following to your Cargo.toml.

[dependencies]
pasts = "0.4"
stick = "0.10"

Example

This example can be used to test joystick input and haptic feedback.

use pasts::{prelude::*, CvarExec};
use stick::{Event, Hub, Pad};

async fn event_loop() {
    let mut hub = Hub::new();
    let mut pads = Vec::<Pad>::new();
    'e: loop {
        match [hub.fut(), pads.select().fut()].select().await.1 {
            (_, Event::Connect(pad)) => {
                println!(
                    "Connected p{}, id: {:04X}_{:04X}_{:04X}_{:04X}, name: {}",
                    pads.len() + 1,
                    pad.id()[0],
                    pad.id()[1],
                    pad.id()[2],
                    pad.id()[3],
                    pad.name(),
                );
                pads.push(*pad);
            }
            (id, Event::Disconnect) => {
                println!("Disconnected p{}", id + 1);
                pads.swap_remove(id);
            }
            (id, Event::Home(true)) => {
                println!("p{} ended the session", id + 1);
                break 'e;
            }
            (id, event) => {
                println!("p{}: {}", id + 1, event);
                match event {
                    Event::ActionA(pressed) => {
                        pads[id].rumble(if pressed { 1.0 } else { 0.0 });
                    }
                    Event::ActionB(pressed) => {
                        pads[id].rumble(if pressed { 0.25 } else { 0.0 });
                    }
                    _ => {}
                }
            }
        }
    }
}

fn main() {
    static EXECUTOR: CvarExec = CvarExec::new();

    EXECUTOR.block_on(event_loop())
}

API

API documentation can be found on docs.rs.

Features

There are no optional features.

Upgrade

You can use the changelog to facilitate upgrading this crate as a dependency.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Contributors are always welcome (thank you for being interested!), whether it be a bug report, bug fix, feature request, feature implementation or whatever. Don't be shy about getting involved. I always make time to fix bugs, so usually a patched version of the library will be out a few days after a report. Features requests will not complete as fast. If you have any questions, design critques, or want me to find you something to work on based on your skill level, you can email me at jeronlau@plopgrizzly.com. Otherwise, here's a link to the issues on GitHub. Before contributing, check out the contribution guidelines, and, as always, make sure to follow the code of conduct.

Dependencies

~140KB