Skip to content

Commit df83957

Browse files
committed
fix(Gamepad Reordering): requeue gamepad reordering requests if one is in progress
1 parent 8fb2ceb commit df83957

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/input/manager.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub enum ManagerCommand {
114114
SetGamepadOrder {
115115
dbus_paths: Vec<String>,
116116
},
117+
GamepadReorderingFinished,
117118
}
118119

119120
/// Manages input devices
@@ -170,6 +171,8 @@ pub struct Manager {
170171
/// List of composite device dbus paths with gamepad devices in player order.
171172
/// E.g. ["/org/shadowblip/InputPlumber/CompositeDevice0"]
172173
target_gamepad_order: Vec<String>,
174+
/// Whether or not target gamepad reordering is changing.
175+
target_gamepad_order_changing: bool,
173176
/// Defines whether or not InputPlumber should try to automatically manage all
174177
/// input devices that have a [CompositeDeviceConfig] definition
175178
manage_all_devices: bool,
@@ -210,6 +213,7 @@ impl Manager {
210213
composite_device_targets: HashMap::new(),
211214
manage_all_devices: false,
212215
target_gamepad_order: vec![],
216+
target_gamepad_order_changing: false,
213217
}
214218
}
215219

@@ -515,6 +519,10 @@ impl Manager {
515519
ManagerCommand::SetGamepadOrder { dbus_paths } => {
516520
self.set_gamepad_order(dbus_paths).await;
517521
}
522+
ManagerCommand::GamepadReorderingFinished => {
523+
log::info!("Finished reordering target devices");
524+
self.target_gamepad_order_changing = false;
525+
}
518526
}
519527
}
520528

@@ -1809,6 +1817,17 @@ impl Manager {
18091817
/// Set the player order of the given composite device paths. Each device
18101818
/// will be suspended and resumed in player order.
18111819
async fn set_gamepad_order(&mut self, order: Vec<String>) {
1820+
// If gamepad reordering is already in progress, requeue the request
1821+
let tx = self.tx.clone();
1822+
if self.target_gamepad_order_changing {
1823+
log::debug!("Gamepad reordering in progress. Requeuing reordering request.");
1824+
tokio::task::spawn(async move {
1825+
let _ = tx
1826+
.send(ManagerCommand::SetGamepadOrder { dbus_paths: order })
1827+
.await;
1828+
});
1829+
return;
1830+
}
18121831
log::info!("Setting player order to: {order:?}");
18131832

18141833
// Ensure the given paths are valid composite device paths
@@ -1829,6 +1848,8 @@ impl Manager {
18291848
.map(|path| self.composite_devices.get(&path).unwrap().clone())
18301849
.collect();
18311850

1851+
self.target_gamepad_order_changing = true;
1852+
18321853
tokio::task::spawn(async move {
18331854
// Suspend all composite devices
18341855
for device in devices.iter() {
@@ -1848,8 +1869,11 @@ impl Manager {
18481869
}
18491870
tokio::time::sleep(Duration::from_millis(100)).await;
18501871
}
1851-
});
18521872

1853-
//
1873+
// Notify the manager that gamepad reordering has completed
1874+
if let Err(e) = tx.send(ManagerCommand::GamepadReorderingFinished).await {
1875+
log::error!("Failed to signal gamepad reordering finished. This is bad: {e:?}");
1876+
}
1877+
});
18541878
}
18551879
}

0 commit comments

Comments
 (0)