mirror of
https://github.com/rust-windowing/winit.git
synced 2026-06-26 22:53:15 -04:00
* Initial implementation * Corrected RAWINPUT buffer sizing * Mostly complete XInput implementation * XInput triggers * Add preliminary CHANGELOG entry. * match unix common API to evl 2.0 * wayland: eventloop2.0 * make EventLoopProxy require T: 'static * Revamp device event API, as well as several misc. fixes on Windows: * When you have multiple windows, you no longer receive duplicate device events * Mouse Device Events now send X-button input * Mouse Device Events now send horizontal scroll wheel input * Add MouseEvent documentation and Device ID debug passthrough * Improve type safety on get_raw_input_data * Remove button_id field from MouseEvent::Button in favor of utton * Remove regex dependency on Windows * Remove axis filtering in XInput * Make gamepads not use lazy_static * Publicly expose gamepad rumble * Unstack DeviceEvent and fix examples/tests * Add HANDLE retrieval method to DeviceExtWindows * Add distinction between non-joystick axes and joystick axes. This helps with properly calculating the deadzone for controller joysticks. One potential issue is that the `Stick` variant isn't used for *all* joysticks, which could be potentially confusing - for example, raw input joysticks will never use the `Stick` variant because we don't understand the semantic meaning of raw input joystick axes. * Add ability to get gamepad port * Fix xinput controller hot swapping * Add functions for enumerating attached devices * Clamp input to [0.0, 1.0] on gamepad rumble * Expose gamepad rumble errors * Add method to check if device is still connected * Add docs * Rename AxisHint and ButtonHint to GamepadAxis and GamepadButton * Add CHANGELOG entry * Update CHANGELOG.md * Add HidId and MovedAbsolute * Fix xinput deprecation warnings * Add ability to retrieve gamepad battery level * Fix weird imports in gamepad example * Update CHANGELOG.md * Resolve francesca64 comments
90 lines
2.6 KiB
Rust
90 lines
2.6 KiB
Rust
use std::sync::Weak;
|
|
|
|
use winapi::um::winnt::HANDLE;
|
|
|
|
use crate::{
|
|
event::device::{BatteryLevel, GamepadEvent, RumbleError},
|
|
platform_impl::platform::{
|
|
raw_input::{get_raw_input_device_name, RawGamepad},
|
|
xinput::{self, XInputGamepad, XInputGamepadShared},
|
|
},
|
|
};
|
|
|
|
#[derive(Debug)]
|
|
pub enum GamepadType {
|
|
Raw(RawGamepad),
|
|
XInput(XInputGamepad),
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub enum GamepadShared {
|
|
Raw(()),
|
|
XInput(Weak<XInputGamepadShared>),
|
|
Dummy,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Gamepad {
|
|
handle: HANDLE,
|
|
backend: GamepadType,
|
|
}
|
|
|
|
impl Gamepad {
|
|
pub fn new(handle: HANDLE) -> Option<Self> {
|
|
// TODO: Verify that this is an HID device
|
|
let name = get_raw_input_device_name(handle)?;
|
|
xinput::id_from_name(&name)
|
|
.and_then(XInputGamepad::new)
|
|
.map(GamepadType::XInput)
|
|
.or_else(|| RawGamepad::new(handle).map(GamepadType::Raw))
|
|
.map(|backend| Gamepad { handle, backend })
|
|
}
|
|
|
|
pub unsafe fn update_state(&mut self, raw_input_report: &mut [u8]) -> Option<()> {
|
|
match self.backend {
|
|
GamepadType::Raw(ref mut gamepad) => gamepad.update_state(raw_input_report),
|
|
GamepadType::XInput(ref mut gamepad) => gamepad.update_state(),
|
|
}
|
|
}
|
|
|
|
pub fn get_gamepad_events(&self) -> Vec<GamepadEvent> {
|
|
match self.backend {
|
|
GamepadType::Raw(ref gamepad) => gamepad.get_gamepad_events(),
|
|
GamepadType::XInput(ref gamepad) => gamepad.get_gamepad_events(),
|
|
}
|
|
}
|
|
|
|
pub fn shared_data(&self) -> GamepadShared {
|
|
match self.backend {
|
|
GamepadType::Raw(_) => GamepadShared::Raw(()),
|
|
GamepadType::XInput(ref gamepad) => GamepadShared::XInput(gamepad.shared_data()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl GamepadShared {
|
|
pub fn rumble(&self, left_speed: f64, right_speed: f64) -> Result<(), RumbleError> {
|
|
match self {
|
|
GamepadShared::Raw(_) | GamepadShared::Dummy => Ok(()),
|
|
GamepadShared::XInput(ref data) => data
|
|
.upgrade()
|
|
.map(|r| r.rumble(left_speed, right_speed))
|
|
.unwrap_or(Err(RumbleError::DeviceNotConnected)),
|
|
}
|
|
}
|
|
|
|
pub fn port(&self) -> Option<u8> {
|
|
match self {
|
|
GamepadShared::Raw(_) | GamepadShared::Dummy => None,
|
|
GamepadShared::XInput(ref data) => data.upgrade().map(|r| r.port()),
|
|
}
|
|
}
|
|
|
|
pub fn battery_level(&self) -> Option<BatteryLevel> {
|
|
match self {
|
|
GamepadShared::Raw(_) | GamepadShared::Dummy => None,
|
|
GamepadShared::XInput(ref data) => data.upgrade().and_then(|r| r.battery_level()),
|
|
}
|
|
}
|
|
}
|