diff --git a/espanso-detect/src/layout/mod.rs b/espanso-detect/src/layout/mod.rs new file mode 100644 index 0000000..1a2d54e --- /dev/null +++ b/espanso-detect/src/layout/mod.rs @@ -0,0 +1,40 @@ +/* + * 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 . + */ + +#[cfg(target_os = "linux")] +#[cfg(not(feature = "wayland"))] +mod x11; + +#[cfg(target_os = "linux")] +#[cfg(not(feature = "wayland"))] +pub fn get_active_layout() -> Option { + x11::get_active_layout() +} + +#[cfg(target_os = "linux")] +#[cfg(feature = "wayland")] +pub fn get_active_layout() -> Option { + todo!() +} + +#[cfg(not(target_os = "linux"))] +pub fn get_active_layout() -> Option { + // Not available on Windows and macOS yet + None +} \ No newline at end of file diff --git a/espanso-detect/src/layout/x11.rs b/espanso-detect/src/layout/x11.rs new file mode 100644 index 0000000..fe3018c --- /dev/null +++ b/espanso-detect/src/layout/x11.rs @@ -0,0 +1,43 @@ +/* + * 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 std::process::Command; + +use log::error; + +pub fn get_active_layout() -> Option { + match Command::new("setxkbmap") + .arg("-query") + .output() + { + Ok(output) => { + let output_str = String::from_utf8_lossy(&output.stdout); + let layout_line = output_str.lines().find(|line| line.contains("layout:"))?; + let layout_raw: Vec<&str> = layout_line.split("layout:").collect(); + Some(layout_raw.get(1)?.trim().to_string()) + }, + Err(err) => { + error!( + "unable to retrieve current keyboard layout with 'setxkbmap': {}", + err + ); + None + } + } +} diff --git a/espanso-detect/src/lib.rs b/espanso-detect/src/lib.rs index dcc1d20..60ad3f3 100644 --- a/espanso-detect/src/lib.rs +++ b/espanso-detect/src/lib.rs @@ -23,6 +23,7 @@ use log::info; pub mod event; pub mod hotkey; +pub mod layout; #[cfg(target_os = "windows")] pub mod win32; @@ -113,3 +114,5 @@ pub fn get_source(options: SourceCreationOptions) -> Result> { info!("using EVDEVSource"); Ok(Box::new(evdev::EVDEVSource::new(options))) } + +pub use layout::get_active_layout; \ No newline at end of file