From 045e92be1b8d0c303cc1cb5956b404b0461431ee Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 23 Jan 2024 14:26:53 -0700 Subject: [PATCH] x11: always trust key event modifiers over xkd modifiers in discussion: https://github.com/ibus/ibus/issues/2600#issuecomment-1904322441 the summary is that the StateNotify event which is used to update the xkd modifier state is sourced directly from the X server and doesn't account for any state maintained by the IME. The key press and release events do include the correct state, so we should always prefer to use the modifiers from those events on X11. Under wayland, we continue to use the kbd state. refs: https://github.com/ibus/ibus/issues/2600 refs: https://github.com/wez/wezterm/pull/4151 --- window/src/os/x11/keyboard.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/window/src/os/x11/keyboard.rs b/window/src/os/x11/keyboard.rs index 678204363..0715ecd5f 100644 --- a/window/src/os/x11/keyboard.rs +++ b/window/src/os/x11/keyboard.rs @@ -163,7 +163,8 @@ impl KeyboardWithFallback { events: &mut WindowEventSender, ) -> Option { let want_repeat = self.selected.wayland_key_repeats(code); - self.process_key_event_impl(code + 8, Modifiers::default(), pressed, events, want_repeat) + let raw_modifiers = self.get_key_modifiers(); + self.process_key_event_impl(code + 8, raw_modifiers, pressed, events, want_repeat) } /// Compute the Modifier mask equivalent from the button mask @@ -215,32 +216,22 @@ impl KeyboardWithFallback { ); } + // for X11 we always pass down raw_modifiers from the incoming + // key event to use in preference to whatever is computed by XKB. + // The reason is that the update_state() call triggered by the XServer + // doesn't know about state managed by the IME, so we cannot trust + // that the modifiers are right. + // fn process_key_event_impl( &self, xcode: xkb::Keycode, - modifiers_from_event: Modifiers, + raw_modifiers: Modifiers, pressed: bool, events: &mut WindowEventSender, want_repeat: bool, ) -> Option { let phys_code = self.selected.phys_code_map.borrow().get(&xcode).copied(); - let modifiers_from_state = self.get_key_modifiers(); - // When XKB doesn't have modifiers in its state but there are modifiers - // given in the X event, use the values from the X event. - // - // This can happen (FIXME: WHY) for example when Espanso - // (a text expander/injector) simulate inputs. - // - // This also happens intermittently for wez on an Ubuntu system where - // the XServer clears the mask state seemingly at the wrong time via - // a call into process_xkb_event that zeroes out all the modifier states. - let raw_modifiers = if modifiers_from_state.is_empty() { - modifiers_from_event - } else { - modifiers_from_state - }; - let leds = self.get_led_status(); let xsym = self.selected.state.borrow().key_get_one_sym(xcode);