From dc2bc46167cfabcf59f3a8abf20fcb548d4ea24c Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Wed, 9 Oct 2019 15:38:21 +0000 Subject: [PATCH] keymap: Simplified key state passing There's no need to treat states as a shared resource before they are placed inside buttons. --- src/data.rs | 19 ++++++++++++++----- src/keyboard.rs | 6 ++---- src/util.rs | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/data.rs b/src/data.rs index 44cf2268..d0631621 100644 --- a/src/data.rs +++ b/src/data.rs @@ -19,6 +19,7 @@ use ::keyboard::{ }; use ::resources; use ::util::c::as_str; +use ::util::hash_map_map; use ::xdg; // traits, derives @@ -346,23 +347,31 @@ impl Layout { }; ( name.into(), - Rc::new(RefCell::new(KeyState { + KeyState { pressed: false, locked: false, keycodes, action, - })) + } ) }); - let button_states = - HashMap::>>::from_iter( + let button_states + = HashMap::::from_iter( button_states ); // TODO: generate from symbols let keymap_str = generate_keymap(&button_states)?; + let button_states_cache = hash_map_map( + button_states, + |name, state| {( + name, + Rc::new(RefCell::new(state)) + )} + ); + let views = HashMap::from_iter( self.views.iter().map(|(name, view)| {( name.clone(), @@ -386,7 +395,7 @@ impl Layout { &self.buttons, &self.outlines, name, - button_states.get(name.into()) + button_states_cache.get(name.into()) .expect("Button state not created") .clone() )) diff --git a/src/keyboard.rs b/src/keyboard.rs index 1041b8fb..9150e0c1 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -1,6 +1,5 @@ /*! State of the emulated keyboard and keys */ -use std::cell::RefCell; use std::collections::HashMap; use std::fmt; use std::io; @@ -151,7 +150,7 @@ impl From for FormattingError { /// Generates a de-facto single level keymap. TODO: actually drop second level pub fn generate_keymap( - keystates: &HashMap::>> + keystates: &HashMap:: ) -> Result { let mut buf: Vec = Vec::new(); writeln!( @@ -164,7 +163,6 @@ pub fn generate_keymap( )?; for (name, state) in keystates.iter() { - let state = state.borrow(); if let Action::Submit { text: _, keys } = &state.action { if let 0 = keys.len() { eprintln!("Key {} has no keysyms", name); }; for (named_keysym, keycode) in keys.iter().zip(&state.keycodes) { @@ -191,7 +189,7 @@ pub fn generate_keymap( )?; for (name, state) in keystates.iter() { - if let Action::Submit { text: _, keys } = &state.borrow().action { + if let Action::Submit { text: _, keys } = &state.action { for keysym in keys.iter() { write!( buf, diff --git a/src/util.rs b/src/util.rs index 9289a40d..59b9f51b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,3 +1,8 @@ +/*! Assorted helpers */ +use std::collections::HashMap; + +use std::iter::FromIterator; + pub mod c { use std::cell::RefCell; use std::ffi::{ CStr, CString }; @@ -101,3 +106,13 @@ pub mod c { } } } + +pub fn hash_map_map(map: HashMap, mut f: F) + -> HashMap + where F: FnMut(K, V) -> (K1, V1), + K1: std::cmp::Eq + std::hash::Hash +{ + HashMap::from_iter( + map.into_iter().map(|(key, value)| f(key, value)) + ) +}