Skip to content

Commit

Permalink
fix(LegionGo): Fix Legion Go mouse.
Browse files Browse the repository at this point in the history
  • Loading branch information
pastaq authored and ShadowApex committed Apr 4, 2024
1 parent f3a5a0e commit 411ff61
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 141 deletions.
41 changes: 23 additions & 18 deletions rootfs/usr/share/inputplumber/devices/50-legion_go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ matches:
# One or more source devices to combine into a single virtual device. The events
# from these devices will be watched and translated according to the key map.
source_devices:
- group: keyboard # Block data
blocked: true
hidraw:
vendor_id: 0x17ef
product_id: 0x6182
interface_num: 0
- group: mouse # Touch Device
hidraw:
vendor_id: 0x17ef
Expand All @@ -36,16 +30,6 @@ source_devices:
vendor_id: 0x17ef
product_id: 0x6182
interface_num: 2
- group: mouse # Only for optical X/Y
hidraw:
vendor_id: 0x17ef
product_id: 0x6182
interface_num: 3
- group: mouse # Only for optical X/Y
hidraw:
vendor_id: 0x17ef
product_id: 0x6185
interface_num: 1
- group: gamepad
hidraw:
vendor_id: 0x17ef
Expand All @@ -61,8 +45,29 @@ source_devices:
unique: false
evdev:
vendor_id: "17ef"
product_id: "618*"
name: "*Legion*Controller*"
product_id: "6182"
name: "*Legion Controller*"
- group: keyboard
blocked: false
unique: false
evdev:
vendor_id: "17ef"
product_id: "6185"
name: "Legion-Controller 1-D6"
- group: keyboard
blocked: true
unique: true
evdev:
vendor_id: "17ef"
product_id: "6185"
name: "Legion-Controller 1-D6 Keyboard"
- group: keyboard
blocked: true
unique: true
evdev:
vendor_id: "17ef"
product_id: "6185"
name: "Legion-Controller 1-D6 Mouse"

# The target input device(s) that the virtual device profile can use
target_devices:
Expand Down
52 changes: 18 additions & 34 deletions src/drivers/lego/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const MOUSE_PACKET_SIZE: usize = 7;
const TOUCHPAD_PACKET_SIZE: usize = 20;
const HID_TIMEOUT: i32 = 5000;

pub const DINPUTLEFT_DATA: u8 = 0x07;
pub const DINPUTRIGHT_DATA: u8 = 0x08;
pub const DINPUT_LEFT_DATA: u8 = 0x07;
pub const DINPUT_RIGHT_DATA: u8 = 0x08;
pub const KEYBOARD_TOUCH_DATA: u8 = 0x01;
pub const MOUSEFPS_DATA: u8 = 0x02;
pub const MOUSE_FPS_DATA: u8 = 0x02;
//pub const MOUSE_DATA: u8 = 0x09;
pub const XINPUT_DATA: u8 = 0x04;

Expand All @@ -42,10 +42,6 @@ pub const XINPUT_DATA: u8 = 0x04;
//pub const GYRO_Y_MIN: f64 = 0.0;
pub const MOUSE_WHEEL_MAX: f64 = 120.0;
//pub const MOUSE_WHEEL_MIN: f64 = -120.0;
pub const MOUSE_X_MAX: f64 = 2048.0;
pub const MOUSE_X_MIN: f64 = -2048.0;
pub const MOUSE_Y_MAX: f64 = 2048.0;
pub const MOUSE_Y_MIN: f64 = -2048.0;
pub const PAD_X_MAX: f64 = 1024.0;
pub const PAD_X_MIN: f64 = 0.0;
pub const PAD_Y_MAX: f64 = 1024.0;
Expand All @@ -57,18 +53,6 @@ pub const STICK_Y_MIN: f64 = 0.0;
pub const TRIGG_MAX: f64 = 255.0;
//pub const TRIGG_MIN: f64 = 0.0;

// NORMALIZED AXIS
//pub const GYRO_X_NORM: f64 = 1.0 / GYRO_X_MAX;
//pub const ACCEL_Y_NORM: f64 = 1.0 / GYRO_Y_MAX;
//pub const MOUSE_WHEEL_NORM: f64 = 1.0 / MOUSE_WHEEL_MAX;
//pub const MOUSE_X_NORM: f64 = 1.0 / MOUSE_X_MAX;
//pub const MOUSE_Y_NORM: f64 = 1.0 / MOUSE_Y_MAX;
//pub const PAD_X_AXIS_NORM: f64 = 1.0 / PAD_X_MAX;
//pub const PAD_Y_AXIS_NORM: f64 = 1.0 / PAD_Y_MAX;
//pub const STICK_X_AXIS_NORM: f64 = 1.0 / STICK_X_MAX;
//pub const STICK_Y_AXIS_NORM: f64 = 1.0 / STICK_Y_MAX;
//pub const TRIGG_AXIS_NORM: f64 = 1.0 / TRIGG_MAX;

pub struct Driver {
dinputl_state: Option<DInputDataLeftReport>,
dinputr_state: Option<DInputDataRightReport>,
Expand Down Expand Up @@ -113,7 +97,7 @@ impl Driver {
//log::trace!("Got Report Size: {bytes_read}");

match report_id {
DINPUTLEFT_DATA => {
DINPUT_LEFT_DATA => {
if bytes_read != DINPUT_PACKET_SIZE {
return Err("Invalid packet size for Direct Input Data.".into());
}
Expand All @@ -123,7 +107,7 @@ impl Driver {
Ok(events)
}

DINPUTRIGHT_DATA => {
DINPUT_RIGHT_DATA => {
if bytes_read != DINPUT_PACKET_SIZE {
return Err("Invalid packet size for Direct Input Data.".into());
}
Expand Down Expand Up @@ -151,7 +135,7 @@ impl Driver {
}
}

MOUSEFPS_DATA => {
MOUSE_FPS_DATA => {
if bytes_read != MOUSE_PACKET_SIZE {
return Err("Invalid packet size for Mouse Data.".into());
}
Expand All @@ -171,7 +155,7 @@ impl Driver {
Ok(events)
}
_ => {
log::trace!("Invalid Report ID.");
//log::trace!("Invalid Report ID.");
let events = vec![];
Ok(events)
}
Expand Down Expand Up @@ -210,14 +194,14 @@ impl Driver {
}

/// Translate the state into individual events
fn translate_dinputl(&self, old_state: Option<DInputDataLeftReport>) -> Vec<Event> {
fn translate_dinputl(&self, _old_state: Option<DInputDataLeftReport>) -> Vec<Event> {
let events = Vec::new();
let Some(_) = self.dinputl_state else {
return events;
};

// Translate state changes into events if they have changed
if let Some(_) = old_state {}
//if let Some(_) = old_state {}
events
}

Expand Down Expand Up @@ -254,14 +238,14 @@ impl Driver {
}

/// Translate the state into individual events
fn translate_dinputr(&self, old_state: Option<DInputDataRightReport>) -> Vec<Event> {
fn translate_dinputr(&self, _old_state: Option<DInputDataRightReport>) -> Vec<Event> {
let events = Vec::new();
let Some(_) = self.dinputr_state else {
return events;
};

// Translate state changes into events if they have changed
if let Some(_) = old_state {}
//if let Some(_) = old_state {}
events
}

Expand Down Expand Up @@ -298,14 +282,14 @@ impl Driver {
}

/// Translate the state into individual events
fn translate_keyboard(&self, old_state: Option<KeyboardDataReport>) -> Vec<Event> {
fn translate_keyboard(&self, _old_state: Option<KeyboardDataReport>) -> Vec<Event> {
let events = Vec::new();
let Some(_) = self.keyboard_state else {
return events;
};

// Translate state changes into events if they have changed
if let Some(_) = old_state {}
//if let Some(_) = old_state {}
events
}

Expand All @@ -323,10 +307,10 @@ impl Driver {
//log::trace!("---- End Report ----");

// Update the state
let old_dinput_state = self.update_mouseinput_state(input_report);
let old_mouse_state = self.update_mouseinput_state(input_report);

// Translate the state into a stream of input events
let events = self.translate_mouse(old_dinput_state);
let events = self.translate_mouse(old_mouse_state);

Ok(events)
}
Expand Down Expand Up @@ -428,14 +412,14 @@ impl Driver {
}

/// Translate the state into individual events
fn translate_touch(&self, old_state: Option<TouchpadDataReport>) -> Vec<Event> {
fn translate_touch(&self, _old_state: Option<TouchpadDataReport>) -> Vec<Event> {
let events = Vec::new();
let Some(_) = self.touchpad_state else {
return events;
};

// Translate state changes into events if they have changed
if let Some(_) = old_state {}
//if let Some(_) = old_state {}
events
}

Expand Down Expand Up @@ -485,7 +469,7 @@ impl Driver {
);
}
if state.gamepad_mode == 2 {
log::debug!("In FPS Mode, rejecting gamepad input.");
//log::debug!("In FPS Mode, rejecting gamepad input.");
return events;
}
// Binary events
Expand Down
12 changes: 6 additions & 6 deletions src/input/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ pub enum MouseButton {
/// Mouse wheel right
WheelRight,
/// Extra mouse button, usually on the side of the mouse
Extra1,
Extra,
/// Extra mouse button, usually on the side of the mouse
Extra2,
Side,
}

impl fmt::Display for MouseButton {
Expand All @@ -216,8 +216,8 @@ impl fmt::Display for MouseButton {
MouseButton::WheelDown => write!(f, "WheelDown"),
MouseButton::WheelLeft => write!(f, "WheelLeft"),
MouseButton::WheelRight => write!(f, "WheelRight"),
MouseButton::Extra1 => write!(f, "Extra1"),
MouseButton::Extra2 => write!(f, "Extra2"),
MouseButton::Extra => write!(f, "Extra1"),
MouseButton::Side => write!(f, "Extra2"),
}
}
}
Expand All @@ -234,8 +234,8 @@ impl FromStr for MouseButton {
"WheelDown" => Ok(MouseButton::WheelDown),
"WheelLeft" => Ok(MouseButton::WheelLeft),
"WheelRight" => Ok(MouseButton::WheelRight),
"Extra1" => Ok(MouseButton::Extra1),
"Extra2" => Ok(MouseButton::Extra2),
"Extra1" => Ok(MouseButton::Extra),
"Extra2" => Ok(MouseButton::Side),
_ => Err(()),
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/input/composite_device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,11 @@ impl CompositeDevice {
device_id: String,
raw_event: Event,
) -> Result<(), Box<dyn Error>> {
log::trace!("Received event: {:?} from {device_id}", raw_event);

if self.source_devices_blocked.contains(&device_id) {
log::trace!("Blocking event! {:?}", raw_event);
return Ok(());
}
log::trace!("Received event: {:?} from {device_id}", raw_event);

// Convert the event into a NativeEvent
let event: NativeEvent = match raw_event {
Expand Down
29 changes: 26 additions & 3 deletions src/input/event/evdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ impl EvdevEvent {
},
_ => InputValue::Float(normal_value),
},
EventType::RELATIVE => match RelativeAxisCode(code) {
RelativeAxisCode::REL_X => InputValue::Vector2 {
x: Some(normal_value),
y: None,
},
RelativeAxisCode::REL_Y => InputValue::Vector2 {
x: None,
y: Some(normal_value),
},
_ => InputValue::Float(normal_value),
},

_ => InputValue::Float(normal_value),
}
}
Expand Down Expand Up @@ -125,6 +137,12 @@ impl EvdevEvent {
match event_type {
EventType::SYNCHRONIZATION => Capability::Sync,
EventType::KEY => match KeyCode::new(code) {
// Mouse Buttons
KeyCode::BTN_LEFT => Capability::Mouse(Mouse::Button(MouseButton::Left)),
KeyCode::BTN_RIGHT => Capability::Mouse(Mouse::Button(MouseButton::Right)),
KeyCode::BTN_MIDDLE => Capability::Mouse(Mouse::Button(MouseButton::Middle)),
KeyCode::BTN_SIDE => Capability::Mouse(Mouse::Button(MouseButton::Side)),
KeyCode::BTN_EXTRA => Capability::Mouse(Mouse::Button(MouseButton::Extra)),
// Gamepad Buttons
KeyCode::BTN_SOUTH => Capability::Gamepad(Gamepad::Button(GamepadButton::South)),
KeyCode::BTN_NORTH => Capability::Gamepad(Gamepad::Button(GamepadButton::North)),
Expand Down Expand Up @@ -414,7 +432,12 @@ impl EvdevEvent {
)),
_ => Capability::NotImplemented,
},
EventType::RELATIVE => Capability::NotImplemented,
EventType::RELATIVE => match RelativeAxisCode(code) {
RelativeAxisCode::REL_X => Capability::Mouse(Mouse::Motion),
RelativeAxisCode::REL_Y => Capability::Mouse(Mouse::Motion),
RelativeAxisCode::REL_WHEEL => Capability::NotImplemented,
_ => Capability::NotImplemented,
},
EventType::MISC => Capability::NotImplemented,
EventType::SWITCH => Capability::NotImplemented,
EventType::LED => Capability::NotImplemented,
Expand Down Expand Up @@ -639,8 +662,8 @@ fn event_codes_from_capability(capability: Capability) -> Vec<u16> {
MouseButton::WheelDown => vec![],
MouseButton::WheelLeft => vec![],
MouseButton::WheelRight => vec![],
MouseButton::Extra1 => vec![KeyCode::BTN_EXTRA.0],
MouseButton::Extra2 => vec![],
MouseButton::Extra => vec![KeyCode::BTN_EXTRA.0],
MouseButton::Side => vec![KeyCode::BTN_SIDE.0],
},
},
Capability::Keyboard(key) => match key {
Expand Down
Loading

0 comments on commit 411ff61

Please sign in to comment.