Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input: Add a way to add a keyboard to the seat from an xkb::keymap #750

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 53 additions & 21 deletions src/input/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,9 @@ where
unsafe impl<D: SeatHandler> Send for KbdInternal<D> {}

impl<D: SeatHandler + 'static> KbdInternal<D> {
fn new(xkb_config: XkbConfig<'_>, repeat_rate: i32, repeat_delay: i32) -> Result<KbdInternal<D>, ()> {
// we create a new contex for each keyboard because libxkbcommon is actually NOT threadsafe
// so confining it inside the KbdInternal allows us to use Rusts mutability rules to make
// sure nothing goes wrong.
//
// FIXME: This is an issue with the xkbcommon-rs crate that does not reflect this
// non-threadsafety properly.
let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
let keymap = xkb::Keymap::new_from_names(
&context,
&xkb_config.rules,
&xkb_config.model,
&xkb_config.layout,
&xkb_config.variant,
xkb_config.options,
xkb::KEYMAP_COMPILE_NO_FLAGS,
)
.ok_or(())?;
fn new(keymap: xkb::Keymap, repeat_rate: i32, repeat_delay: i32) -> KbdInternal<D> {
let state = xkb::State::new(&keymap);
Ok(KbdInternal {
KbdInternal {
focus: None,
pending_focus: None,
pressed_keys: Vec::new(),
Expand All @@ -119,7 +102,7 @@ impl<D: SeatHandler + 'static> KbdInternal<D> {
repeat_rate,
repeat_delay,
grab: GrabStatus::None,
})
}
}

// return true if modifier state has changed
Expand Down Expand Up @@ -417,10 +400,59 @@ impl<D: SeatHandler + 'static> KeyboardHandle<D> {
"rules" => xkb_config.rules, "model" => xkb_config.model, "layout" => xkb_config.layout,
"variant" => xkb_config.variant, "options" => &xkb_config.options
);
let internal = KbdInternal::new(xkb_config, repeat_rate, repeat_delay).map_err(|_| {

// we create a new contex for each keyboard because libxkbcommon is actually NOT threadsafe
// so confining it inside the KbdInternal allows us to use Rusts mutability rules to make
// sure nothing goes wrong.
//
// FIXME: This is an issue with the xkbcommon-rs crate that does not reflect this
// non-threadsafety properly.
let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
let keymap = xkb::Keymap::new_from_names(
&context,
&xkb_config.rules,
&xkb_config.model,
&xkb_config.layout,
&xkb_config.variant,
xkb_config.options,
xkb::KEYMAP_COMPILE_NO_FLAGS,
)
.ok_or_else(|| {
debug!(log, "Loading keymap failed");
Error::BadKeymap
})?;
Self::new_inner(keymap, repeat_delay, repeat_rate, log)
}

/// Create a keyboard handler from a keymap string
pub(crate) fn from_keymap_string(
string: String,
repeat_delay: i32,
repeat_rate: i32,
logger: &::slog::Logger,
) -> Result<Self, Error> {
let log = logger.new(o!("smithay_module" => "xkbcommon_handler"));
let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);
let keymap = xkb::Keymap::new_from_string(
&context,
string,
xkb::KEYMAP_FORMAT_TEXT_V1,
xkb::KEYMAP_COMPILE_NO_FLAGS,
)
.ok_or_else(|| {
debug!(log, "Loading keymap failed");
Error::BadKeymap
})?;
Self::new_inner(keymap, repeat_delay, repeat_rate, log)
}

fn new_inner(
keymap: xkb::Keymap,
repeat_delay: i32,
repeat_rate: i32,
log: ::slog::Logger,
) -> Result<Self, Error> {
let internal = KbdInternal::new(keymap, repeat_rate, repeat_delay);

info!(log, "Loaded Keymap"; "name" => internal.keymap.layouts().next());

Expand Down
47 changes: 45 additions & 2 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,52 @@ impl<D: SeatHandler + 'static> Seat<D> {
repeat_delay: i32,
repeat_rate: i32,
) -> Result<KeyboardHandle<D>, KeyboardError> {
let mut inner = self.arc.inner.lock().unwrap();
let keyboard =
self::keyboard::KeyboardHandle::new(xkb_config, repeat_delay, repeat_rate, &self.arc.log)?;
self.add_keyboard_inner(&keyboard)?;
Ok(keyboard)
}

/// Adds the keyboard capability to this seat
///
/// Like [`Seat::add_keyboard`] except it takes a keymap as a string
/// instead of a configuration.
///
/// Can be used with `xkbcommon::xkb::Keymap::get_as_string` to copy from
/// an existing keymap. This can not directly take a `Keymap` since it
/// needs to create a `Keymap` with it's own xkb context.
///
/// ``` no_run
/// let keyboard = seat
/// .add_keyboard_from_string(
/// keymap.get_as_string(xkb::KEYMAP_FORMAT_TEXT_V1),
/// 200,
/// 25,
/// )
/// .expect("Failed to initialize the keyboard");
/// ```
///
pub fn add_keyboard_from_keymap_string(
&mut self,
string: String,
repeat_delay: i32,
repeat_rate: i32,
) -> Result<KeyboardHandle<D>, KeyboardError> {
let keyboard = self::keyboard::KeyboardHandle::from_keymap_string(
string,
repeat_delay,
repeat_rate,
&self.arc.log,
)?;
self.add_keyboard_inner(&keyboard)?;
Ok(keyboard)
}

fn add_keyboard_inner(
&mut self,
keyboard: &self::keyboard::KeyboardHandle<D>,
) -> Result<(), KeyboardError> {
let mut inner = self.arc.inner.lock().unwrap();
if inner.keyboard.is_some() {
// there is already a keyboard, remove it and notify the clients
// of the change
Expand All @@ -472,7 +515,7 @@ impl<D: SeatHandler + 'static> Seat<D> {
inner.keyboard = Some(keyboard.clone());
#[cfg(feature = "wayland_frontend")]
inner.send_all_caps();
Ok(keyboard)
Ok(())
}

/// Access the keyboard of this seat if any
Expand Down