Skip to content

Commit

Permalink
fix(CompositeDevice): add mouse motion properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowApex committed Mar 31, 2024
1 parent 767da35 commit a0feed7
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 117 deletions.
103 changes: 1 addition & 102 deletions rootfs/usr/share/inputplumber/profiles/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,105 +9,4 @@ kind: DeviceProfile
name: Default

# Profile mappings
mapping:
# Button to button
- name: Guide Button
source_event:
keyboard: KeyRightCtrl
target_events:
- gamepad:
button: Guide

# Button to axis direction
- name: A to axis
source_event:
keyboard: KeyA
target_events:
- gamepad:
axis:
name: Hat1
direction: left

# Axis direction to button
- name: A
source_event:
gamepad:
axis:
name: LeftStick
direction: left
deadzone: 0.3 # defaults to 0.3
target_events:
- keyboard: KeyA

# Axis to mouse
- name: Joystick Mouse
source_event:
gamepad:
axis:
name: RightStick
target_events:
- mouse:
motion: continuous

# Gyro to mouse
- name: Gyro Mouse
source_event:
gamepad:
gyro:
name: Gyro1
target_events:
- mouse:
motion: continuous

# Gyro to axis
- name: Gyro Stick
source_event:
gamepad:
gyro:
name: Gyro1
target_events:
- gamepad:
axis:
name: RightStick

# Gyro to key press
- name: Gyro Key
source_event:
gamepad:
gyro:
name: Gyro1
axis: yaw
direction: positive
deadzone: 0.3
target_events:
- keyboard: KeyW

# Key to input chord
- name: Steam QuickAccess Chord
source_event:
gamepad:
button: QuickAccess
target_events:
- gamepad:
button: Guide
- gamepad:
button: South

# Key to DBus event
- name: DBus QuickAccess
source_event:
gamepad:
button: QuickAccess
target_events:
- dbus: Quick

# Trigger to button
- name: Trigger to button
source_event:
gamepad:
trigger:
name: LeftTrigger
deadzone: 0.4
target_events:
- gamepad:
button: South
mapping: []
11 changes: 10 additions & 1 deletion rootfs/usr/share/inputplumber/profiles/mouse_keyboard_wasd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ mapping:
target_events:
- dbus: ui_guide

- name: Quick Access
source_event:
gamepad:
button: QuickAccess
target_events:
- dbus: ui_quick

# Left Stick
- name: Left
source_event:
Expand Down Expand Up @@ -70,7 +77,9 @@ mapping:
name: RightStick
target_events:
- mouse:
motion: continuous
motion:
speed_pps: 800 # default to 800pps
direction: left

# Buttons
- name: Menu
Expand Down
123 changes: 123 additions & 0 deletions rootfs/usr/share/inputplumber/profiles/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# yaml-language-server: $schema=../schema/device_profile_v1.json
# Schema version number
version: 1

# The type of configuration schema
kind: DeviceProfile

# Name of the device profile
name: Test Profile

# Profile mappings
mapping:
# Button to button
- name: Guide Button
source_event:
keyboard: KeyRightCtrl
target_events:
- gamepad:
button: Guide

# Button to axis direction
- name: A to axis
source_event:
keyboard: KeyA
target_events:
- gamepad:
axis:
name: Hat1
direction: left

# Axis direction to button
- name: A
source_event:
gamepad:
axis:
name: LeftStick
direction: left
deadzone: 0.3 # defaults to 0.3
target_events:
- keyboard: KeyA

- name: D
source_event:
gamepad:
axis:
name: LeftStick
direction: right
deadzone: 0.3 # defaults to 0.3
target_events:
- keyboard: KeyD

# Axis to mouse
- name: Joystick Mouse
source_event:
gamepad:
axis:
name: RightStick
target_events:
- mouse:
motion: {}

# Gyro to mouse
- name: Gyro Mouse
source_event:
gamepad:
gyro:
name: Gyro1
target_events:
- mouse:
motion: {}

# Gyro to axis
- name: Gyro Stick
source_event:
gamepad:
gyro:
name: Gyro1
target_events:
- gamepad:
axis:
name: RightStick

# Gyro to key press
- name: Gyro Key
source_event:
gamepad:
gyro:
name: Gyro1
axis: yaw
direction: positive
deadzone: 0.3
target_events:
- keyboard: KeyW

# Key to input chord
- name: Steam QuickAccess Chord
source_event:
gamepad:
button: QuickAccess
target_events:
- gamepad:
button: Guide
- gamepad:
button: South

# Key to DBus event
- name: DBus QuickAccess
source_event:
gamepad:
button: QuickAccess
target_events:
- dbus: ui_quick

# Trigger to button
- name: Trigger to button
source_event:
gamepad:
trigger:
name: LeftTrigger
deadzone: 0.4
target_events:
- gamepad:
button: South
26 changes: 22 additions & 4 deletions rootfs/usr/share/inputplumber/schema/device_profile_v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,7 @@
"additionalProperties": false,
"properties": {
"motion": {
"type": "string",
"enum": [
"continuous"
]
"$ref": "#/definitions/MouseMotionEvent"
},
"button": {
"type": "string",
Expand All @@ -293,6 +290,27 @@
},
"required": []
},
"MouseMotionEvent": {
"title": "MouseMotionEvent",
"type": "object",
"additionalProperties": false,
"properties": {
"direction": {
"type": "string",
"enum": [
"left",
"right",
"up",
"down"
]
},
"speed_pps": {
"type": "number",
"description": "Speed of the target motion event in pixels per second",
"default": 800
}
}
},
"GamepadEvent": {
"title": "GamepadEvent",
"type": "object",
Expand Down
39 changes: 38 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,36 @@ impl ProfileMapping {
}
}

// Mouse event
if let Some(mouse) = self.source_event.mouse.as_ref() {
// Mouse motion
if let Some(motion) = mouse.motion.as_ref() {
// Mouse motion was defined for source event!
if let Some(direction) = motion.direction.as_ref() {
// A direction was defined!
let value = event.get_value();
return match value {
InputValue::Vector2 { x, y } => match direction.as_str() {
// Left should be a negative value
"left" => x.filter(|&x| x <= 0.0).is_some(),
// Right should be a positive value
"right" => x.filter(|&x| x >= 0.0).is_some(),
// Up should be a negative value
"up" => y.filter(|&y| y <= 0.0).is_some(),
// Down should be a positive value
"down" => y.filter(|&y| y >= 0.0).is_some(),
_ => false,
},
// Other values should never be used if this was an axis
_ => false,
};
} else {
// If no direction was defined for mouse motion, then this should match
return true;
}
}
}

// If no other input types were defined in the config, then it counts as
// a match.
true
Expand Down Expand Up @@ -185,7 +215,14 @@ pub struct GyroCapability {
#[serde(rename_all = "snake_case")]
pub struct MouseCapability {
pub button: Option<String>,
pub motion: Option<String>,
pub motion: Option<MouseMotionCapability>,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub struct MouseMotionCapability {
pub direction: Option<String>,
pub speed_pps: Option<u64>,
}

/// Defines a platform match for loading a [CompositeDevice]
Expand Down
9 changes: 0 additions & 9 deletions src/input/composite_device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ pub struct CompositeDevice {
/// Name of the currently loaded [DeviceProfile] for the CompositeDevice.
/// The [DeviceProfile] is used to translate input events.
device_profile: Option<String>,
/// Map of profile capabilities and the capabilities they translate into
device_profile_map: HashMap<Capability, Vec<Capability>>,
/// Map of profile source events to translate to one or more profile mapping
/// configs that define how the source event should be translated.
device_profile_config_map: HashMap<Capability, Vec<ProfileMapping>>,
Expand Down Expand Up @@ -311,7 +309,6 @@ impl CompositeDevice {
capabilities: HashSet::new(),
capability_map,
device_profile: None,
device_profile_map: HashMap::new(),
device_profile_config_map: HashMap::new(),
translatable_capabilities: Vec::new(),
translatable_active_inputs: Vec::new(),
Expand Down Expand Up @@ -1048,7 +1045,6 @@ impl CompositeDevice {
log::debug!("Loading device profile from path: {path}");
// Remove all outdated capability mappings.
log::debug!("Clearing old device profile mappings");
self.device_profile_map.clear();
self.device_profile_config_map.clear();

// Load and parse the device profile
Expand All @@ -1072,11 +1068,6 @@ impl CompositeDevice {
target_events_caps.push(cap);
}

// Insert the target event capabilities to translate to
// TODO: This wont work
self.device_profile_map
.insert(source_event_cap.clone(), target_events_caps);

// Insert the translation config for this event
let config_map = self
.device_profile_config_map
Expand Down

0 comments on commit a0feed7

Please sign in to comment.