Skip to content

Commit 9487b0f

Browse files
f-trycuaclaude
andauthored
fix(cua-driver): wait for EIS to acknowledge libei input before replying (trycua#4880)
The libei worker replied as soon as input requests were flushed to the socket. A flush is not delivery: the caller's focus guard could restore the user's window while the compositor still had the input queued, and the input then landed in the wrong app. After each input command, send ei_connection.sync and reply only when EIS answers its callback, which it does after processing every earlier request. If no answer comes within 5 s, fail with "delivery is unknown" instead of reporting success. WaitReady and failed commands reply immediately as before. Ported from T3 Code's Linux patch to Cua Driver 0.34.0 (pingdotgg/t3code#16975, MIT, Copyright (c) 2026 T3 Tools Inc.). Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 07cbc6c commit 9487b0f

1 file changed

Lines changed: 59 additions & 37 deletions

File tree

  • libs/cua-driver/rust/crates/platform-linux/src/wayland

‎libs/cua-driver/rust/crates/platform-linux/src/wayland/libei.rs‎

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
//! handshake → connection.seat events → seat.bind(capabilities) →
1818
//! device.frame + emulate events.
1919
//! 4. Public API sends commands over a crossbeam-channel and blocks
20-
//! until the worker reports the request was flushed to the EIS
21-
//! server.
20+
//! until EIS acknowledges the preceding input through ei_connection.sync.
2221
//!
2322
//! Persistence: ashpd `PersistMode::ExplicitlyRevoked` keeps the user's consent
2423
//! until they revoke it in desktop settings. The restore token is stored at
@@ -607,6 +606,16 @@ fn run_calloop(context: reis::ei::Context, rx: Receiver<Cmd>) -> anyhow::Result<
607606
pending = still;
608607
}
609608

609+
state.pending_sync.retain(|_, (reply, sent)| {
610+
if sent.elapsed() < std::time::Duration::from_secs(5) {
611+
return true;
612+
}
613+
let _ = reply.send(Err(anyhow::anyhow!(
614+
"EIS did not acknowledge input within 5s; delivery is unknown"
615+
)));
616+
false
617+
});
618+
610619
// The handle_command path may have queued frame() requests; flush
611620
// them once per loop iteration so the EIS server sees them.
612621
if let Some(ctx) = state.context.as_ref() {
@@ -620,6 +629,8 @@ fn run_calloop(context: reis::ei::Context, rx: Receiver<Cmd>) -> anyhow::Result<
620629
#[derive(Default)]
621630
struct EisState {
622631
context: Option<reis::ei::Context>,
632+
connection: Option<reis::ei::Connection>,
633+
pending_sync: HashMap<reis::ei::Callback, (Sender<anyhow::Result<()>>, std::time::Instant)>,
623634
seats: HashMap<reis::ei::Seat, SeatData>,
624635
devices: HashMap<reis::ei::Device, DeviceData>,
625636
sequence: u32,
@@ -737,17 +748,25 @@ impl EisState {
737748
handshake.finish();
738749
}
739750
}
740-
Event::Connection(_conn, ev) => match ev {
741-
reis::ei::connection::Event::Seat { seat } => {
742-
self.seats.insert(seat, SeatData::default());
751+
Event::Callback(callback, reis::ei::callback::Event::Done { .. }) => {
752+
if let Some((reply, _)) = self.pending_sync.remove(&callback) {
753+
let _ = reply.send(Ok(()));
743754
}
744-
// The EIS server pings to check liveness; failing to answer
745-
// makes it drop the connection. Mirror reis's examples.
746-
reis::ei::connection::Event::Ping { ping } => {
747-
ping.done(0);
755+
}
756+
Event::Connection(conn, ev) => {
757+
self.connection = Some(conn);
758+
match ev {
759+
reis::ei::connection::Event::Seat { seat } => {
760+
self.seats.insert(seat, SeatData::default());
761+
}
762+
// The EIS server pings to check liveness; failing to answer
763+
// makes it drop the connection. Mirror reis's examples.
764+
reis::ei::connection::Event::Ping { ping } => {
765+
ping.done(0);
766+
}
767+
_ => {}
748768
}
749-
_ => {}
750-
},
769+
}
751770
Event::Seat(seat, ev) => {
752771
let data = self.seats.entry(seat.clone()).or_default();
753772
match ev {
@@ -945,33 +964,36 @@ impl EisState {
945964

946965
fn handle_command(&mut self, cmd: Cmd) {
947966
let result = self.run_command(&cmd);
948-
// Send the reply on whichever channel this command carries.
949-
match cmd {
950-
Cmd::WaitReady { reply, .. } => {
951-
let _ = reply.send(result);
952-
}
953-
Cmd::Click { reply, .. } => {
954-
let _ = reply.send(result);
955-
}
956-
Cmd::MoveAbsolute { reply, .. } => {
957-
let _ = reply.send(result);
958-
}
959-
Cmd::Scroll { reply, .. } => {
960-
let _ = reply.send(result);
961-
}
962-
Cmd::TypeText { reply, .. } => {
963-
let _ = reply.send(result);
964-
}
965-
Cmd::PressKey { reply, .. } => {
966-
let _ = reply.send(result);
967-
}
968-
Cmd::KeySequence { reply, .. } => {
969-
let _ = reply.send(result);
970-
}
971-
Cmd::Drag { reply, .. } => {
972-
let _ = reply.send(result);
967+
let needs_sync = !matches!(cmd, Cmd::WaitReady { .. });
968+
let reply = match cmd {
969+
Cmd::WaitReady { reply, .. }
970+
| Cmd::Click { reply, .. }
971+
| Cmd::MoveAbsolute { reply, .. }
972+
| Cmd::Scroll { reply, .. }
973+
| Cmd::TypeText { reply, .. }
974+
| Cmd::PressKey { reply, .. }
975+
| Cmd::KeySequence { reply, .. }
976+
| Cmd::Drag { reply, .. } => reply,
977+
Cmd::Shutdown => return,
978+
};
979+
if result.is_ok() && needs_sync {
980+
if let (Some(connection), Some(context)) = (&self.connection, &self.context) {
981+
// A socket flush is not delivery. Hold the caller's focus guard
982+
// until EIS acknowledges all preceding input frames.
983+
let callback = connection.sync(1);
984+
if let Err(error) = context.flush() {
985+
let _ = reply.send(Err(error.into()));
986+
return;
987+
}
988+
self.pending_sync
989+
.insert(callback, (reply, std::time::Instant::now()));
990+
} else {
991+
let _ = reply.send(Err(anyhow::anyhow!(
992+
"EIS connection unavailable for input acknowledgement"
993+
)));
973994
}
974-
Cmd::Shutdown => {}
995+
} else {
996+
let _ = reply.send(result);
975997
}
976998
}
977999

0 commit comments

Comments
 (0)