Skip to content

Commit

Permalink
fix(client): correctly invert joystick direction
Browse files Browse the repository at this point in the history
gilrs (the rust input library we're using) inverts up and down. This is
also underspecified at the protocol level.

Finally, we were also sending the right trigger incorrectly.
  • Loading branch information
colinmarc committed Oct 14, 2024
1 parent 203b8d1 commit a60eb39
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 10 additions & 3 deletions mm-client/src/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use mm_protocol::{
gamepad_motion::GamepadAxis,
Gamepad,
};
use tracing::{debug, error};
use tracing::{debug, error, trace};

#[derive(Debug, Clone)]
pub enum GamepadEvent {
Expand Down Expand Up @@ -144,6 +144,8 @@ where
continue;
};

trace!(?id, ?ev, "gamepad event");

if let EventType::Disconnected = ev {
if let Some(pad) = remote_gamepads.remove(&id) {
if proxy
Expand Down Expand Up @@ -214,7 +216,7 @@ where
EventType::ButtonReleased(button, _) => {
input_event(pad.id, button, GamepadButtonState::Released)
}
EventType::AxisChanged(axis, value, _) => {
EventType::AxisChanged(axis, mut value, _) => {
// Some gamepads treat the dpad as an axis. The protocol
// treats it as a bunch of buttons.
if matches!(axis, gilrs::Axis::DPadX | gilrs::Axis::DPadY) {
Expand All @@ -227,6 +229,11 @@ where
return Ok(());
};

// Gilrs treats 1.0 as up.
if matches!(axis, GamepadAxis::LeftY | GamepadAxis::RightY) {
value *= -1.0;
}

Some(GamepadEvent::Motion(pad.id, axis, value as _))
}
EventType::ButtonChanged(button, value, _) => {
Expand All @@ -239,7 +246,7 @@ where
)),
gilrs::Button::RightTrigger2 => Some(GamepadEvent::Motion(
pad.id,
GamepadAxis::LeftTrigger,
GamepadAxis::RightTrigger,
value.max(0.0) as _,
)),
_ => None,
Expand Down
6 changes: 4 additions & 2 deletions mm-protocol/src/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1099,8 +1099,10 @@ message GamepadMotion {
uint64 gamepad_id = 1; // Required.
GamepadAxis axis = 2; // Required.

// Required, with a value from -1.0 to 1.0. Zero always represents the resting
// position, and triggers will therefore usually range from 0.0 to 1.0.
// Required, with a value from -1.0 (towards the top of the gamepad) to 1.0
// (towards the bottom of the gamepad). Zero always represents the resting
// position, and triggers will therefore usually range from 0.0 to 1.0
// (fully pressed).
double value = 3;
}

Expand Down

0 comments on commit a60eb39

Please sign in to comment.