Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XkbContext::update_mask and XkbContext::set_update_key #1597

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions src/input/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ pub(crate) struct KbdInternal<D: SeatHandler> {
led_mapping: LedMapping,
pub(crate) led_state: LedState,
grab: GrabStatus<dyn KeyboardGrab<D>>,
update_key: bool,
}

// focus_hook does not implement debug, so we have to impl Debug manually
Expand Down Expand Up @@ -266,6 +267,7 @@ impl<D: SeatHandler + 'static> KbdInternal<D> {
led_mapping,
led_state,
grab: GrabStatus::None,
update_key: true,
})
}

Expand All @@ -283,17 +285,18 @@ impl<D: SeatHandler + 'static> KbdInternal<D> {
}
};

// update state
// Offset the keycode by 8, as the evdev XKB rules reflect X's
// broken keycode system, which starts at 8.
let mut xkb = self.xkb.lock().unwrap();
let state_components = xkb.state.update_key(keycode, direction);
let modifiers_changed = state_components != 0;
if modifiers_changed {
self.mods_state.update_with(&xkb.state);
if self.update_key {
let mut xkb = self.xkb.lock().unwrap();
let state_components = xkb.state.update_key(keycode, direction);
let modifiers_changed = state_components != 0;
if modifiers_changed {
self.mods_state.update_with(&xkb.state);
}
let leds_changed = self.led_state.update_with(&xkb.state, &self.led_mapping);
(modifiers_changed, leds_changed)
} else {
(false, false)
}
let leds_changed = self.led_state.update_with(&xkb.state, &self.led_mapping);
(modifiers_changed, leds_changed)
}

fn with_grab<F>(&mut self, data: &mut D, seat: &Seat<D>, f: F)
Expand Down Expand Up @@ -491,18 +494,16 @@ impl<'a> XkbContext<'a> {
self.xkb
}

/// Set layout of the keyboard to the given index.
pub fn set_layout(&mut self, layout: Layout) {
/// Set explict modifier masks and layout
///
/// This should generally be used with [`KeyboardHandle::set_update_key`] `false` to disable
/// automatic updates to the xkb state on key presses.
pub fn update_mask(&mut self, depressed_mods: u32, latched_mods: u32, locked_mods: u32, layout: Layout) {
let mut xkb = self.xkb.lock().unwrap();

let state = xkb.state.update_mask(
self.mods_state.serialized.depressed,
self.mods_state.serialized.latched,
self.mods_state.serialized.locked,
0,
0,
layout.0,
);
let state = xkb
.state
.update_mask(depressed_mods, latched_mods, locked_mods, 0, 0, layout.0);

if state != 0 {
self.mods_state.update_with(&xkb.state);
Expand All @@ -512,6 +513,16 @@ impl<'a> XkbContext<'a> {
*self.leds_changed = self.leds_state.update_with(&xkb.state, self.leds_mapping);
}

/// Set layout of the keyboard to the given index.
pub fn set_layout(&mut self, layout: Layout) {
self.update_mask(
self.mods_state.serialized.depressed,
self.mods_state.serialized.latched,
self.mods_state.serialized.locked,
layout,
);
}

/// Switches layout forward cycling when it reaches the end.
pub fn cycle_next_layout(&mut self) {
let xkb = self.xkb.lock().unwrap();
Expand Down Expand Up @@ -771,8 +782,10 @@ impl<D: SeatHandler + 'static> KeyboardHandle<D> {
let mut internal = self.arc.internal.lock().unwrap();

let mut state = xkb::State::new(&keymap);
for key in &internal.pressed_keys {
state.update_key(*key, xkb::KeyDirection::Down);
if internal.update_key {
for key in &internal.pressed_keys {
state.update_key(*key, xkb::KeyDirection::Down);
}
}

let led_mapping = LedMapping::from_keymap(&keymap);
Expand Down Expand Up @@ -1042,6 +1055,15 @@ impl<D: SeatHandler + 'static> KeyboardHandle<D> {
}
}

/// If `true` (the default), each keypress with update the xkb state.
///
/// If set to `false`, this will not happen, and something like [`XkbContext::update_mask`]
/// should instead be used to update this automatically.
pub fn set_update_key(&self, value: bool) {
let mut guard = self.arc.internal.lock().unwrap();
guard.update_key = value;
}

/// Set the current focus of this keyboard
///
/// If the new focus is different from the previous one, any previous focus
Expand Down
Loading