diff --git a/espanso/src/cli/worker/engine/keyboard_layout_util.rs b/espanso/src/cli/worker/engine/keyboard_layout_util.rs new file mode 100644 index 0000000..4db3648 --- /dev/null +++ b/espanso/src/cli/worker/engine/keyboard_layout_util.rs @@ -0,0 +1,75 @@ +/* + * This file is part of espanso. + * + * Copyright (C) 2019-2021 Federico Terzi + * + * espanso is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * espanso is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with espanso. If not, see . + */ + +use espanso_config::config::{Config, RMLVOConfig}; +use log::{info, warn}; + +fn generate_rmlvo_config(config: &dyn Config) -> Option { + // Not needed on Windows and macOS + if !cfg!(target_os = "linux") { + return None; + } + + // Not needed on X11 + if !cfg!(feature = "wayland") { + return None; + } + + if let Some(keyboard_config) = config.keyboard_layout() { + Some(keyboard_config) + } else if let Some(active_layout) = espanso_detect::get_active_layout() { + Some(RMLVOConfig { + layout: Some(active_layout), + ..Default::default() + }) + } else { + warn!("unable to determine keyboard layout automatically, please explicitly specify it in the configuration."); + None + } +} + +pub fn generate_detect_rmlvo(config: &dyn Config) -> Option { + generate_rmlvo_config(config) + .map(|config| { + info!("detection module will use this keyboard layout: {}", config); + config + }) + .map(|config| espanso_detect::KeyboardConfig { + rules: config.rules, + model: config.model, + layout: config.layout, + variant: config.variant, + options: config.options, + }) +} + +pub fn generate_inject_rmlvo(config: &dyn Config) -> Option { + generate_rmlvo_config(config) + .map(|config| { + info!("inject module will use this keyboard layout: {}", config); + config + }) + .map(|config| espanso_inject::KeyboardConfig { + rules: config.rules, + model: config.model, + layout: config.layout, + variant: config.variant, + options: config.options, + }) +} diff --git a/espanso/src/cli/worker/engine/mod.rs b/espanso/src/cli/worker/engine/mod.rs index f7dc7a7..b4f53a5 100644 --- a/espanso/src/cli/worker/engine/mod.rs +++ b/espanso/src/cli/worker/engine/mod.rs @@ -48,6 +48,7 @@ use super::secure_input::SecureInputEvent; pub mod dispatch; pub mod funnel; pub mod process; +mod keyboard_layout_util; #[allow(clippy::too_many_arguments)] pub fn initialize_and_spawn( @@ -90,6 +91,7 @@ pub fn initialize_and_spawn( let (detect_source, modifier_state_store, sequencer, key_state_store) = super::engine::funnel::init_and_spawn(SourceCreationOptions { use_evdev: use_evdev_backend, + evdev_keyboard_rmlvo: keyboard_layout_util::generate_detect_rmlvo(&*config_manager.default()), ..Default::default() }) .expect("failed to initialize detector module"); @@ -130,6 +132,7 @@ pub fn initialize_and_spawn( let injector = espanso_inject::get_injector(InjectorCreationOptions { use_evdev: use_evdev_backend, keyboard_state_provider: key_state_store.map(|store| Box::new(store) as Box), + evdev_keyboard_rmlvo: keyboard_layout_util::generate_inject_rmlvo(&*config_manager.default()), ..Default::default() }) .expect("failed to initialize injector module"); // TODO: handle the options