From d90f80c1ca222c80847eea5ae91eadd807a5f4e3 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Wed, 15 Oct 2025 14:10:33 +0200 Subject: [PATCH] cli: add dump-x11 command --- Cargo.lock | 110 ++++++++++------------------------ kbvm-cli/Cargo.toml | 2 + kbvm-cli/src/cli.rs | 15 ++++- kbvm-cli/src/compile_rmlvo.rs | 10 +++- kbvm-cli/src/compile_xkb.rs | 9 ++- kbvm-cli/src/dump_x11.rs | 43 +++++++++++++ kbvm-cli/src/main.rs | 2 + 7 files changed, 106 insertions(+), 85 deletions(-) create mode 100644 kbvm-cli/src/dump_x11.rs diff --git a/Cargo.lock b/Cargo.lock index ea3afb4..407c120 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -385,12 +385,12 @@ dependencies = [ [[package]] name = "gethostname" -version = "0.4.3" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818" +checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8" dependencies = [ - "libc", - "windows-targets 0.48.5", + "rustix", + "windows-link", ] [[package]] @@ -575,6 +575,7 @@ dependencies = [ "utf8-console", "wl-client", "wl-client-builder", + "x11rb", ] [[package]] @@ -605,7 +606,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34" dependencies = [ "cfg-if", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -636,9 +637,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.15" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" [[package]] name = "lock_api" @@ -734,7 +735,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -958,9 +959,9 @@ checksum = "1c9afdcca45aed5b357d64e0d033f6400c07a668aa4f9b92d25d39a884889457" [[package]] name = "rustix" -version = "0.38.43" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6" +checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" dependencies = [ "bitflags", "errno", @@ -1328,13 +1329,19 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -1343,22 +1350,7 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", + "windows-targets", ] [[package]] @@ -1367,46 +1359,28 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -1419,48 +1393,24 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - [[package]] name = "windows_x86_64_msvc" version = "0.52.6" @@ -1515,9 +1465,9 @@ dependencies = [ [[package]] name = "x11rb" -version = "0.13.1" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d91ffca73ee7f68ce055750bf9f6eca0780b8c85eff9bc046a3b0da41755e12" +checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414" dependencies = [ "gethostname", "rustix", @@ -1526,9 +1476,9 @@ dependencies = [ [[package]] name = "x11rb-protocol" -version = "0.13.1" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec107c4503ea0b4a98ef47356329af139c0a4f7750e621cf2973cd3385ebcb3d" +checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd" [[package]] name = "zerocopy" diff --git a/kbvm-cli/Cargo.toml b/kbvm-cli/Cargo.toml index 06987d4..0739497 100644 --- a/kbvm-cli/Cargo.toml +++ b/kbvm-cli/Cargo.toml @@ -32,6 +32,8 @@ utf8-console = "0.1.0" [target.'cfg(unix)'.dependencies] wl-client = "0.2.0" +x11rb = { version = "0.13.2", default-features = false } +kbvm = { version = "0.1.5", path = "../kbvm", features = ["x11"] } [target.'cfg(target_os = "linux")'.dependencies] uapi = "0.2.13" diff --git a/kbvm-cli/src/cli.rs b/kbvm-cli/src/cli.rs index 7a5825b..a8779d9 100644 --- a/kbvm-cli/src/cli.rs +++ b/kbvm-cli/src/cli.rs @@ -1,4 +1,6 @@ #[cfg(unix)] +use crate::dump_x11::{self, DumpX11Args}; +#[cfg(unix)] use crate::test_wayland::{self, TestWaylandArgs}; use { crate::{ @@ -31,6 +33,9 @@ enum Cmd { CompileXkb(CompileXkbArgs), /// Generate shell completion scripts for kbvm. GenerateCompletion(GenerateArgs), + /// Loads the keymap from the X server and writes it to stdout. + #[cfg(unix)] + DumpX11(DumpX11Args), } #[derive(Args, Debug, Default)] @@ -44,6 +49,10 @@ pub struct CompileArgs { /// Append an include path. #[clap(long, value_hint = ValueHint::DirPath)] pub append_include: Vec, +} + +#[derive(Args, Debug, Default)] +pub struct FormatArgs { /// Don't include key actions or behaviors. #[clap(long)] pub lookup_only: bool, @@ -64,8 +73,10 @@ impl CompileArgs { builder.append_path(dir); } } +} - pub fn apply2<'a>(&self, mut formatter: Formatter<'a>) -> Formatter<'a> { +impl FormatArgs { + pub fn apply<'a>(&self, mut formatter: Formatter<'a>) -> Formatter<'a> { if self.lookup_only { formatter = formatter.lookup_only(true); } @@ -85,5 +96,7 @@ pub fn main() { Cmd::CompileRmlvo(args) => compile_rmlvo::main(args), Cmd::CompileXkb(args) => compile_xkb::main(args), Cmd::GenerateCompletion(args) => generate::main(args), + #[cfg(unix)] + Cmd::DumpX11(args) => dump_x11::main(args), } } diff --git a/kbvm-cli/src/compile_rmlvo.rs b/kbvm-cli/src/compile_rmlvo.rs index 2e504aa..71655c8 100644 --- a/kbvm-cli/src/compile_rmlvo.rs +++ b/kbvm-cli/src/compile_rmlvo.rs @@ -1,5 +1,9 @@ use { - crate::{cli::CompileArgs, compile_xkb::format_keymap, expand_rmlvo::RmlvoArgs}, + crate::{ + cli::{CompileArgs, FormatArgs}, + compile_xkb::format_keymap, + expand_rmlvo::RmlvoArgs, + }, clap::Args, kbvm::xkb::{Context, diagnostic::WriteToLog}, }; @@ -9,6 +13,8 @@ pub struct CompileRmlvoArgs { #[clap(flatten)] compile_args: CompileArgs, #[clap(flatten)] + format_args: FormatArgs, + #[clap(flatten)] rmlvo: RmlvoArgs, } @@ -24,5 +30,5 @@ pub fn main(args: CompileRmlvoArgs) { groups.as_deref(), options.as_deref(), ); - format_keymap(args.compile_args.apply2(expanded.format())); + format_keymap(args.format_args.apply(expanded.format())); } diff --git a/kbvm-cli/src/compile_xkb.rs b/kbvm-cli/src/compile_xkb.rs index eef3753..904219f 100644 --- a/kbvm-cli/src/compile_xkb.rs +++ b/kbvm-cli/src/compile_xkb.rs @@ -1,5 +1,8 @@ use { - crate::{cli::CompileArgs, utils::read_path}, + crate::{ + cli::{CompileArgs, FormatArgs}, + utils::read_path, + }, clap::{Args, ValueHint}, error_reporter::Report, kbvm::xkb::{Context, diagnostic::WriteToLog}, @@ -14,6 +17,8 @@ use { pub struct CompileXkbArgs { #[clap(flatten)] compile_args: CompileArgs, + #[clap(flatten)] + format_args: FormatArgs, /// The path to the keymap. /// /// If the path is not specified or if the path is `-`, the keymap is read from stdin. @@ -30,7 +35,7 @@ pub fn main(args: CompileXkbArgs) { let expanded = context.keymap_from_bytes(WriteToLog, Some(path.as_ref()), &source); match expanded { Ok(map) => { - format_keymap(args.compile_args.apply2(map.format())); + format_keymap(args.format_args.apply(map.format())); } Err(_) => { log::error!("could not compile keymap"); diff --git a/kbvm-cli/src/dump_x11.rs b/kbvm-cli/src/dump_x11.rs new file mode 100644 index 0000000..3cc0396 --- /dev/null +++ b/kbvm-cli/src/dump_x11.rs @@ -0,0 +1,43 @@ +use { + crate::{cli::FormatArgs, compile_xkb::format_keymap}, + clap::Args, + error_reporter::Report, + kbvm::xkb::x11::KbvmX11Ext, +}; + +#[derive(Args, Debug, Default)] +pub struct DumpX11Args { + #[clap(flatten)] + format_args: FormatArgs, +} + +pub fn main(args: DumpX11Args) { + let (con, _) = match x11rb::connect(None) { + Ok(c) => c, + Err(e) => { + log::error!("could not connect to X11: {}", Report::new(e)); + std::process::exit(1); + } + }; + if let Err(e) = con.setup_xkb_extension() { + log::error!("could not enable the XKB extension: {}", Report::new(e)); + std::process::exit(1); + } + let dev_id = match con.get_xkb_core_device_id() { + Ok(i) => i, + Err(e) => { + log::error!("could not retrieve the core device ID: {}", Report::new(e)); + std::process::exit(1); + } + }; + let expanded = con.get_xkb_keymap(dev_id); + match expanded { + Ok(map) => { + format_keymap(args.format_args.apply(map.format())); + } + Err(e) => { + log::error!("could not retrieve the keymap from X11: {}", Report::new(e)); + std::process::exit(1); + } + } +} diff --git a/kbvm-cli/src/main.rs b/kbvm-cli/src/main.rs index 20c800f..7909058 100644 --- a/kbvm-cli/src/main.rs +++ b/kbvm-cli/src/main.rs @@ -6,6 +6,8 @@ use {error_reporter::Report, log::LevelFilter}; mod cli; mod compile_rmlvo; mod compile_xkb; +#[cfg(unix)] +mod dump_x11; mod evdev; mod expand_rmlvo; mod generate;