1
0
Fork 0
forked from wry/wry

keyboard: replace xkbcommon by kbvm

This commit is contained in:
Julian Orth 2025-01-07 20:03:49 +01:00
parent 51ceba72b0
commit 541a7b5ebc
23 changed files with 532 additions and 738 deletions

View file

@ -1,13 +1,22 @@
use {
crate::utils::vecset::VecSet,
crate::utils::{oserror::OsError, vecset::VecSet},
kbvm::Components,
std::{
cell::{Ref, RefCell},
rc::Rc,
},
uapi::OwnedFd,
thiserror::Error,
uapi::{c, Errno, OwnedFd},
};
#[derive(Debug, Error)]
pub enum KeyboardError {
#[error("Could not create a keymap memfd")]
KeymapMemfd(#[source] OsError),
#[error("Could not copy the keymap")]
KeymapCopy(#[source] OsError),
}
linear_ids!(KeyboardStateIds, KeyboardStateId, u64);
pub struct KeyboardState {
@ -27,3 +36,23 @@ impl DynKeyboardState for RefCell<KeyboardState> {
self.borrow()
}
}
impl KeyboardState {
pub fn create_new_keymap_fd(&self) -> Result<Rc<OwnedFd>, KeyboardError> {
let fd = match uapi::memfd_create("shared-keymap", c::MFD_CLOEXEC) {
Ok(fd) => fd,
Err(e) => return Err(KeyboardError::KeymapMemfd(e.into())),
};
let target = self.map_len as c::off_t;
let mut pos = 0;
while pos < target {
let rem = target - pos;
let res = uapi::sendfile(fd.raw(), self.map.raw(), Some(&mut pos), rem as usize);
match res {
Ok(_) | Err(Errno(c::EINTR)) => {}
Err(e) => return Err(KeyboardError::KeymapCopy(e.into())),
}
}
Ok(Rc::new(fd))
}
}