Skip to content

Commit

Permalink
linuxkms: Initialize xkb lazily
Browse files Browse the repository at this point in the history
This has two benefits:

- It improves start-up time for device that are touch-only because
  the xkb parsing is delayed until a keyboard is attached and key is
  pressed
- It fixes #3678 by working around the situation where xkb would
  crash if the xkbcommon data files (in /usr/share/X11/xkb) aren't
  there. Sadly, no error is reported in that case (that we could
  handle), instead just crashes.
  • Loading branch information
tronical committed Oct 16, 2023
1 parent 166f19a commit d9672c5
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions internal/backends/linuxkms/calloop_backend/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct LibInputHandler<'a> {
mouse_pos: Pin<Rc<Property<Option<LogicalPosition>>>>,
last_touch_pos: LogicalPosition,
window: &'a i_slint_core::api::Window,
keystate: xkb::State,
keystate: Option<xkb::State>,
}

impl<'a> LibInputHandler<'a> {
Expand All @@ -74,11 +74,6 @@ impl<'a> LibInputHandler<'a> {
});
libinput.udev_assign_seat(&seat_name).unwrap();

let xkb_context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
let keymap = xkb::Keymap::new_from_names(&xkb_context, "", "", "", "", None, 0)
.ok_or_else(|| format!("Error compiling keymap"))?;
let keystate = xkb::State::new(&keymap);

let mouse_pos_property = Rc::pin(Property::new(None));

let handler = Self {
Expand All @@ -87,7 +82,7 @@ impl<'a> LibInputHandler<'a> {
mouse_pos: mouse_pos_property.clone(),
last_touch_pos: Default::default(),
window,
keystate,
keystate: Default::default(),
};

event_loop_handle
Expand Down Expand Up @@ -209,21 +204,27 @@ impl<'a> calloop::EventSource for LibInputHandler<'a> {
let key_code = xkb::Keycode::new(key_event.key() + 8);
let state = key_event.key_state();

let sym = self.keystate.key_get_one_sym(key_code);
let xkb_key_state = self.keystate.get_or_insert_with(|| {
let xkb_context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
let keymap =
xkb::Keymap::new_from_names(&xkb_context, "", "", "", "", None, 0)
.expect("Error compiling keymap");
xkb::State::new(&keymap)
});

let sym = xkb_key_state.key_get_one_sym(key_code);

self.keystate.update_key(
xkb_key_state.update_key(
key_code,
match state {
input::event::tablet_pad::KeyState::Pressed => xkb::KeyDirection::Down,
input::event::tablet_pad::KeyState::Released => xkb::KeyDirection::Up,
},
);

let control = self
.keystate
let control = xkb_key_state
.mod_name_is_active(xkb::MOD_NAME_CTRL, xkb::STATE_MODS_EFFECTIVE);
let alt = self
.keystate
let alt = xkb_key_state
.mod_name_is_active(xkb::MOD_NAME_ALT, xkb::STATE_MODS_EFFECTIVE);

if state == KeyState::Pressed {
Expand Down

0 comments on commit d9672c5

Please sign in to comment.