diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index b5f3f6b9b4..20feeef5c3 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -9,8 +9,6 @@ use super::{ GenericEventCookie, ImeReceiver, ScrollOrientation, UnownedWindow, WindowId, XExtension, }; -use util::modifiers::{ModifierKeyState, ModifierKeymap}; - use crate::platform_impl::platform::x11::ime::{ImeEvent, ImeEventReceiver, ImeRequest}; use crate::{ dpi::{PhysicalPosition, PhysicalSize}, @@ -38,8 +36,6 @@ pub(super) struct EventProcessor { pub(super) xkbext: XExtension, pub(super) target: Rc>, pub(super) kb_state: KbState, - pub(super) mod_keymap: ModifierKeymap, - pub(super) device_mod_state: ModifierKeyState, pub(super) pending_mod_change: Option, // Number of touch events currently in progress pub(super) num_touch: u32, @@ -142,16 +138,6 @@ impl EventProcessor { return; } - macro_rules! assert_mods_eq { - ($old:expr, $new:expr) => { - if $old != $new { - println!("New modifier tracking differed from the old one."); - println!("Old: {:?}", $old); - println!("New: {:?}", $new); - } - } - } - let event_type = xev.get_type(); match event_type { ffi::ClientMessage => { @@ -594,9 +580,7 @@ impl EventProcessor { return; } - let old_modifiers = ModifiersState::from_x11(&xev.mods); let modifiers = self.kb_state.mods_state().into(); - assert_mods_eq!(old_modifiers, modifiers); let state = if xev.evtype == ffi::XI_ButtonPress { Pressed @@ -672,9 +656,7 @@ impl EventProcessor { let window_id = mkwid(xev.event); let new_cursor_pos = (xev.event_x, xev.event_y); - let old_modifiers = ModifiersState::from_x11(&xev.mods); let modifiers = self.kb_state.mods_state().into(); - assert_mods_eq!(old_modifiers, modifiers); let cursor_moved = self.with_window(xev.event, |window| { let mut shared_state_lock = window.shared_state.lock(); @@ -835,10 +817,7 @@ impl EventProcessor { .focus(xev.event) .expect("Failed to focus input context"); - let old_modifiers = ModifiersState::from_x11(&xev.mods); - self.device_mod_state.update_state(&old_modifiers, None); let modifiers = self.kb_state.mods_state().into(); - assert_mods_eq!(old_modifiers, modifiers); if self.active_window != Some(xev.event) { self.active_window = Some(xev.event); @@ -884,8 +863,6 @@ impl EventProcessor { window_id, ElementState::Pressed, &mut self.kb_state, - &self.mod_keymap, - &mut self.device_mod_state, &mut callback, ); } @@ -912,8 +889,6 @@ impl EventProcessor { window_id, ElementState::Released, &mut self.kb_state, - &self.mod_keymap, - &mut self.device_mod_state, &mut callback, ); @@ -940,9 +915,7 @@ impl EventProcessor { }; if self.window_exists(xev.event) { let id = xev.detail as u64; - let old_modifiers = ModifiersState::from_x11(&xev.mods); let modifiers = self.kb_state.mods_state().into(); - assert_mods_eq!(old_modifiers, modifiers); let location = PhysicalPosition::new(xev.event_x as f64, xev.event_y as f64); @@ -1113,16 +1086,6 @@ impl EventProcessor { state, }), }); - - if let Some(modifier) = - self.mod_keymap.get_modifier(keycode as ffi::KeyCode) - { - self.device_mod_state.key_event( - state, - keycode as ffi::KeyCode, - modifier, - ); - } } ffi::XI_HierarchyChanged => { @@ -1330,8 +1293,6 @@ impl EventProcessor { window_id: crate::window::WindowId, state: ElementState, kb_state: &mut KbState, - mod_keymap: &ModifierKeymap, - device_mod_state: &mut ModifierKeyState, callback: &mut F, ) where F: FnMut(Event<'_, T>), @@ -1354,14 +1315,6 @@ impl EventProcessor { let (key_without_modifiers, _) = ker.key_without_modifiers(); let text_with_all_modifiers = ker.text_with_all_modifiers(); - if let Some(modifier) = mod_keymap.get_modifier(keycode as ffi::KeyCode) { - device_mod_state.key_event( - ElementState::Pressed, - keycode as ffi::KeyCode, - modifier, - ); - } - callback(Event::WindowEvent { window_id, event: WindowEvent::KeyboardInput { diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 7c3c2d876b..1317b101f0 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -44,7 +44,6 @@ use self::{ dnd::{Dnd, DndState}, event_processor::EventProcessor, ime::{Ime, ImeCreationError, ImeReceiver, ImeRequest, ImeSender}, - util::modifiers::ModifierKeymap, }; use super::common::xkb_state::KbState; use crate::{ @@ -241,9 +240,6 @@ impl EventLoop { xconn.update_cached_wm_info(root); - let mut mod_keymap = ModifierKeymap::new(); - mod_keymap.reset_from_x_connection(&xconn); - let poll = Poll::new().unwrap(); let waker = Arc::new(Waker::new(poll.registry(), USER_REDRAW_TOKEN).unwrap()); @@ -292,8 +288,6 @@ impl EventLoop { xi2ext, xkbext, kb_state, - mod_keymap, - device_mod_state: Default::default(), pending_mod_change: Default::default(), num_touch: 0, first_touch: None, diff --git a/src/platform_impl/linux/x11/util/mod.rs b/src/platform_impl/linux/x11/util/mod.rs index df3d89138a..0eed788e5f 100644 --- a/src/platform_impl/linux/x11/util/mod.rs +++ b/src/platform_impl/linux/x11/util/mod.rs @@ -11,7 +11,6 @@ mod icon; mod input; pub mod keys; mod memory; -pub mod modifiers; mod randr; mod window_property; mod wm; diff --git a/src/platform_impl/linux/x11/util/modifiers.rs b/src/platform_impl/linux/x11/util/modifiers.rs deleted file mode 100644 index 31db220263..0000000000 --- a/src/platform_impl/linux/x11/util/modifiers.rs +++ /dev/null @@ -1,190 +0,0 @@ -use std::{collections::HashMap, slice}; - -use super::*; - -use crate::event::ElementState; -use crate::keyboard::ModifiersState; - -// Offsets within XModifierKeymap to each set of keycodes. -// We are only interested in Shift, Control, Alt, and Logo. -// -// There are 8 sets total. The order of keycode sets is: -// Shift, Lock, Control, Mod1 (Alt), Mod2, Mod3, Mod4 (Logo), Mod5 -// -// https://tronche.com/gui/x/xlib/input/XSetModifierMapping.html -const SHIFT_OFFSET: usize = 0; -const CONTROL_OFFSET: usize = 2; -const ALT_OFFSET: usize = 3; -const LOGO_OFFSET: usize = 6; -const NUM_MODS: usize = 8; - -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub enum Modifier { - Alt, - Ctrl, - Shift, - Logo, -} - -#[derive(Debug, Default)] -pub struct ModifierKeymap { - // Maps keycodes to modifiers - keys: HashMap, -} - -#[derive(Clone, Debug, Default)] -pub struct ModifierKeyState { - // Contains currently pressed modifier keys and their corresponding modifiers - keys: HashMap, - state: ModifiersState, -} - -impl ModifierKeymap { - pub fn new() -> ModifierKeymap { - ModifierKeymap::default() - } - - pub fn get_modifier(&self, keycode: ffi::KeyCode) -> Option { - self.keys.get(&keycode).cloned() - } - - pub fn reset_from_x_connection(&mut self, xconn: &XConnection) { - unsafe { - let keymap = (xconn.xlib.XGetModifierMapping)(xconn.display); - - if keymap.is_null() { - panic!("failed to allocate XModifierKeymap"); - } - - self.reset_from_x_keymap(&*keymap); - - (xconn.xlib.XFreeModifiermap)(keymap); - } - } - - pub fn reset_from_x_keymap(&mut self, keymap: &ffi::XModifierKeymap) { - let keys_per_mod = keymap.max_keypermod as usize; - - let keys = unsafe { - slice::from_raw_parts(keymap.modifiermap as *const _, keys_per_mod * NUM_MODS) - }; - - self.keys.clear(); - - self.read_x_keys(keys, SHIFT_OFFSET, keys_per_mod, Modifier::Shift); - self.read_x_keys(keys, CONTROL_OFFSET, keys_per_mod, Modifier::Ctrl); - self.read_x_keys(keys, ALT_OFFSET, keys_per_mod, Modifier::Alt); - self.read_x_keys(keys, LOGO_OFFSET, keys_per_mod, Modifier::Logo); - } - - fn read_x_keys( - &mut self, - keys: &[ffi::KeyCode], - offset: usize, - keys_per_mod: usize, - modifier: Modifier, - ) { - let start = offset * keys_per_mod; - let end = start + keys_per_mod; - - for &keycode in &keys[start..end] { - if keycode != 0 { - self.keys.insert(keycode, modifier); - } - } - } -} - -impl ModifierKeyState { - pub fn update_keymap(&mut self, mods: &ModifierKeymap) { - self.keys.retain(|k, v| { - if let Some(m) = mods.get_modifier(*k) { - *v = m; - true - } else { - false - } - }); - - self.reset_state(); - } - - pub fn update_state( - &mut self, - state: &ModifiersState, - except: Option, - ) -> Option { - let mut new_state = *state; - - match except { - Some(Modifier::Alt) => new_state.set(ModifiersState::ALT, self.state.alt_key()), - Some(Modifier::Ctrl) => { - new_state.set(ModifiersState::CONTROL, self.state.control_key()) - } - Some(Modifier::Shift) => new_state.set(ModifiersState::SHIFT, self.state.shift_key()), - Some(Modifier::Logo) => new_state.set(ModifiersState::SUPER, self.state.super_key()), - None => (), - } - - if self.state == new_state { - None - } else { - self.keys.retain(|_k, v| get_modifier(&new_state, *v)); - self.state = new_state; - Some(new_state) - } - } - - pub fn modifiers(&self) -> ModifiersState { - self.state - } - - pub fn key_event(&mut self, state: ElementState, keycode: ffi::KeyCode, modifier: Modifier) { - match state { - ElementState::Pressed => self.key_press(keycode, modifier), - ElementState::Released => self.key_release(keycode), - } - } - - pub fn key_press(&mut self, keycode: ffi::KeyCode, modifier: Modifier) { - self.keys.insert(keycode, modifier); - - set_modifier(&mut self.state, modifier, true); - } - - pub fn key_release(&mut self, keycode: ffi::KeyCode) { - if let Some(modifier) = self.keys.remove(&keycode) { - if !self.keys.values().any(|&m| m == modifier) { - set_modifier(&mut self.state, modifier, false); - } - } - } - - fn reset_state(&mut self) { - let mut new_state = ModifiersState::default(); - - for &m in self.keys.values() { - set_modifier(&mut new_state, m, true); - } - - self.state = new_state; - } -} - -fn get_modifier(state: &ModifiersState, modifier: Modifier) -> bool { - match modifier { - Modifier::Alt => state.alt_key(), - Modifier::Ctrl => state.control_key(), - Modifier::Shift => state.shift_key(), - Modifier::Logo => state.super_key(), - } -} - -fn set_modifier(state: &mut ModifiersState, modifier: Modifier, value: bool) { - match modifier { - Modifier::Alt => state.set(ModifiersState::ALT, value), - Modifier::Ctrl => state.set(ModifiersState::CONTROL, value), - Modifier::Shift => state.set(ModifiersState::SHIFT, value), - Modifier::Logo => state.set(ModifiersState::SUPER, value), - } -}