Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stick/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
name = "stick"
version = "0.13.0"
license = "Apache-2.0 OR BSL-1.0 OR MIT"

description = """
Platform-agnostic asynchronous gamepad, joystick, and flightstick library
"""
Expand All @@ -16,7 +15,8 @@ readme = "README.md"
edition = "2021"

[target.'cfg(all(not(target_arch="wasm32"),target_os="linux"))'.dependencies]
smelling_salts = "0.4"
smelling_salts = "0.6"
lookit = "0.1"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["libloaderapi", "xinput", "winerror"] }
Expand Down
19 changes: 19 additions & 0 deletions stick/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::env;

fn main() {
let target = &env::var("TARGET").unwrap();
let target_family = &env::var("CARGO_CFG_TARGET_FAMILY").unwrap();
let target_os = &env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_arch = &env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let target_vendor = &env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
let target_env = &env::var("CARGO_CFG_TARGET_ENV").unwrap();
let unsupported =
format!(
"Target environment {} ({}, {}, {}, {}, {}) not suppported, please \
consider opening an issue at https://github.com/libcala/stick/issues",
target, target_family, target_os, target_arch, target_vendor, target_env
);
let mut out_file = env::var("OUT_DIR").unwrap();
out_file.push_str("/unsupported.rs");
std::fs::write(out_file, unsupported).unwrap();
}
9 changes: 4 additions & 5 deletions stick/examples/haptic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

use std::task::Poll::{self, Pending, Ready};

use pasts::Loop;
use stick::{Controller, Event, Listener};
use stick::{Connector, Controller, Event};

type Exit = usize;

struct State {
listener: Listener,
connector: Connector,
controllers: Vec<Controller>,
rumble: (f32, f32),
}
Expand Down Expand Up @@ -55,13 +54,13 @@ impl State {

async fn event_loop() {
let mut state = State {
listener: Listener::default(),
connector: Connector::default(),
controllers: Vec::new(),
rumble: (0.0, 0.0),
};

let player_id = Loop::new(&mut state)
.when(|s| &mut s.listener, State::connect)
.when(|s| &mut s.connector, State::connect)
.poll(|s| &mut s.controllers, State::event)
.await;

Expand Down
51 changes: 51 additions & 0 deletions stick/src/connector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};

use lookit::Lookit;

use crate::{Controller, Remap};

#[cfg(windows)]
mod lookit {
#[derive(Debug)]
pub(crate) struct Lookit {}
}

/// Future that you can `.await` to connect to
/// [`Controller`](crate::Controller)s
#[derive(Debug)]
pub struct Connector(Lookit, Remap);

impl Default for Connector {
fn default() -> Self {
Self::new(Remap::default())
}
}

impl Connector {
/// Create a new controller connector
pub fn new(remap: Remap) -> Self {
Self(Lookit::with_input(), remap)
}
}

impl Future for Connector {
type Output = Controller;

fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Self::Output> {
let a = Pin::new(&mut self.as_mut().0)
.poll(cx)
.map(|device| Controller::new(device, &self.1));
match a {
Poll::Ready(Some(x)) => Poll::Ready(x),
Poll::Ready(None) => self.poll(cx),
Poll::Pending => Poll::Pending,
}
}
}
80 changes: 56 additions & 24 deletions stick/src/ctlr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@ use std::{
task::{Context, Poll},
};

use crate::Event;
use lookit::It;

use crate::{
platform::{platform, CtlrId, Support},
Event,
};

#[cfg(windows)]
pub(crate) mod lookit {
#[derive(Debug)]
pub(crate) struct It {}

impl It {
pub fn id(&self) -> u8 {
todo!()
}
}
}

#[repr(i8)]
enum Btn {
Expand Down Expand Up @@ -266,13 +283,19 @@ pub struct Controller {
// Shared remapping.
remap: Arc<Info>,
//
raw: Box<dyn crate::raw::Controller>,
raw: CtlrId,
// Button states
btns: u128,
// Number button states
nums: u128,
// Axis states:
axis: [f64; Axs::Count as usize],
// Unique platform-specific controller ID.
id: u64,
// Name of the controller.
name: String,
// Is the platform controller ready? If so, keep polling.
ready: bool,
}

impl Debug for Controller {
Expand All @@ -283,31 +306,33 @@ impl Debug for Controller {

impl Controller {
#[allow(unused)]
pub(crate) fn new(
raw: Box<dyn crate::raw::Controller>,
remap: &Remap,
) -> Self {
pub(crate) fn new(which: It, remap: &Remap) -> Option<Self> {
let btns = 0;
let nums = 0;
let axis = [0.0; Axs::Count as usize];
let remap = remap.0.get(&raw.id()).cloned().unwrap_or_default();
Self {
let (id, name, raw) = platform().connect(which)?;
let remap = remap.0.get(&id).cloned().unwrap_or_default();
let ready = true;
Some(Self {
remap,
raw,
btns,
nums,
axis,
}
id,
name,
ready,
})
}

/// Get a unique identifier for the specific model of gamepad.
pub fn id(&self) -> u64 {
self.raw.id()
self.id
}

/// Get the name of this Pad.
pub fn name(&self) -> &str {
self.raw.name()
&self.name
}

/// Turn on/off haptic force feedback.
Expand All @@ -319,7 +344,7 @@ impl Controller {
/// located on the left, and the second is typically high frequency and is
/// located on the right (controllers may vary).
pub fn rumble<R: Rumble>(&mut self, power: R) {
self.raw.rumble(power.left(), power.right());
platform().rumble(&mut self.raw, power.left(), power.right());
}

fn button(&mut self, b: Btn, f: fn(bool) -> Event, p: bool) -> Poll<Event> {
Expand Down Expand Up @@ -363,15 +388,15 @@ impl Controller {
- 1.0)
.clamp(-1.0, 1.0)
} else {
self.raw.axis(v).clamp(-1.0, 1.0)
v.clamp(-1.0, 1.0)
};
if !map.deadzone.is_nan() && v.abs() <= map.deadzone {
0.0
} else {
v
}
} else {
self.raw.axis(v).clamp(-1.0, 1.0)
v.clamp(-1.0, 1.0)
};
let axis = a as usize;
if self.axis[axis] == v {
Expand All @@ -396,15 +421,15 @@ impl Controller {
((v - f64::from(map.min)) / f64::from(map.max - map.min))
.clamp(0.0, 1.0)
} else {
self.raw.pressure(v).clamp(0.0, 1.0)
v.clamp(0.0, 1.0)
};
if !map.deadzone.is_nan() && v <= map.deadzone {
0.0
} else {
v
}
} else {
self.raw.pressure(v).clamp(0.0, 1.0)
v.clamp(0.0, 1.0)
};
let axis = a as usize;
if self.axis[axis] == v {
Expand Down Expand Up @@ -567,16 +592,23 @@ impl Future for Controller {
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Event> {
let mut this = self.as_mut();

if let Poll::Ready(event) = this.raw.poll(cx) {
let out = Self::process(&mut this, event);
if out.is_pending() {
Self::poll(self, cx)
} else {
out
if this.ready {
if let Poll::Ready(event) = platform().event(&mut this.raw) {
let out = Self::process(&mut *this, event);
return if out.is_pending() {
Self::poll(self, cx)
} else {
out
};
}
} else {
Poll::Pending
}

this.ready = Pin::new(&mut this.raw.device).poll(cx).is_ready();
if !this.ready {
return Poll::Pending;
}

Self::poll(self, cx)
}
}

Expand Down
6 changes: 4 additions & 2 deletions stick/src/focus.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::platform::Support;

/// Window grab focus, re-enable events if they were disabled.
pub fn focus() {
crate::raw::GLOBAL.with(|g| g.enable());
crate::platform::platform().enable();
}

/// Window ungrab focus, disable events.
pub fn unfocus() {
crate::raw::GLOBAL.with(|g| g.disable());
crate::platform::platform().disable();
}
26 changes: 19 additions & 7 deletions stick/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
//! ```rust,no_run
//! use pasts::Loop;
//! use std::task::Poll::{self, Pending, Ready};
//! use stick::{Controller, Event, Listener};
//! use stick::{Controller, Event, Connector};
//!
//! type Exit = usize;
//!
//! struct State {
//! listener: Listener,
//! connector: Connector,
//! controllers: Vec<Controller>,
//! rumble: (f32, f32),
//! }
Expand Down Expand Up @@ -66,13 +66,13 @@
//!
//! async fn event_loop() {
//! let mut state = State {
//! listener: Listener::default(),
//! connector: Connector::default(),
//! controllers: Vec::new(),
//! rumble: (0.0, 0.0),
//! };
//!
//! let player_id = Loop::new(&mut state)
//! .when(|s| &mut s.listener, State::connect)
//! .when(|s| &mut s.connector, State::connect)
//! .poll(|s| &mut s.controllers, State::event)
//! .await;
//!
Expand Down Expand Up @@ -110,13 +110,25 @@
#[macro_use]
extern crate log;

// Platform-specific implementation
mod platform {
#![allow(clippy::module_inception)]

mod platform;

pub(crate) use platform::{platform, CtlrId, Support};
}

mod connector;
mod ctlr;
mod event;
mod focus;
mod listener;
mod raw;
// mod listener;
// mod raw;

pub use connector::Connector;
pub use ctlr::{Controller, Remap};
pub use event::Event;
pub use focus::{focus, unfocus};
pub use listener::Listener;

// pub use listener::Listener;
Loading