From 6bea5a072c3c1ef4c3d0dbbc62533a3714cdc006 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sun, 7 Sep 2025 12:28:57 +0200 Subject: [PATCH] kbvm: identify keymaps by their hash --- Cargo.lock | 20 ++++++++++++++++++++ Cargo.toml | 1 + src/kbvm.rs | 11 ++++------- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca363744..fd6c166c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,6 +173,19 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" +[[package]] +name = "blake3" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3888aaa89e4b2a40fca9848e400f6a658a5a3978de7be858e209cafa8be9a4a0" +dependencies = [ + "arrayref", + "arrayvec", + "cc", + "cfg-if", + "constant_time_eq", +] + [[package]] name = "bstr" version = "1.11.3" @@ -295,6 +308,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "constant_time_eq" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6" + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -587,6 +606,7 @@ dependencies = [ "ash", "backtrace", "bincode", + "blake3", "bstr", "byteorder", "cc", diff --git a/Cargo.toml b/Cargo.toml index 5a0d9519..0898817d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,6 +67,7 @@ regex = "1.11.1" cfg-if = "1.0.0" opera = "1.0.1" with_builtin_macros = "0.1.0" +blake3 = "1.8.2" [build-dependencies] repc = "0.1.1" diff --git a/src/kbvm.rs b/src/kbvm.rs index 992cf8a7..85c38689 100644 --- a/src/kbvm.rs +++ b/src/kbvm.rs @@ -33,21 +33,18 @@ pub enum KbvmError { pub struct KbvmContext { pub ctx: xkb::Context, - pub ids: KbvmMapIds, } impl Default for KbvmContext { fn default() -> Self { let mut ctx = xkb::Context::builder(); ctx.enable_environment(true); - Self { - ctx: ctx.build(), - ids: Default::default(), - } + Self { ctx: ctx.build() } } } -linear_ids!(KbvmMapIds, KbvmMapId, u64); +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +pub struct KbvmMapId([u8; 32]); pub struct KbvmMap { pub id: KbvmMapId, @@ -90,7 +87,7 @@ impl KbvmContext { .map_err(KbvmError::CouldNotParseKeymap)?; let builder = map.to_builder(); Ok(Rc::new(KbvmMap { - id: self.ids.next(), + id: KbvmMapId(*blake3::hash(keymap).as_bytes()), state_machine: builder.build_state_machine(), map: create_keymap_memfd(&map, false).map_err(KbvmError::KeymapMemfd)?, xwayland_map: create_keymap_memfd(&map, true).map_err(KbvmError::KeymapMemfd)?,