Compose key handling
This commit is contained in:
parent
1f21d29bd2
commit
97f330c7b9
|
|
@ -20,6 +20,7 @@ use core::cell::RefCell;
|
||||||
use core::slice;
|
use core::slice;
|
||||||
|
|
||||||
use alloc::boxed::Box;
|
use alloc::boxed::Box;
|
||||||
|
use alloc::string::String;
|
||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
|
|
@ -61,7 +62,7 @@ use slint::ComponentHandle;
|
||||||
use slint::platform::software_renderer::Rgb565Pixel;
|
use slint::platform::software_renderer::Rgb565Pixel;
|
||||||
use static_cell::StaticCell;
|
use static_cell::StaticCell;
|
||||||
use ui::AppWindow;
|
use ui::AppWindow;
|
||||||
use xkbcommon::xkb::{self, KeyDirection, ModIndex};
|
use xkbcommon::xkb::{self, FeedResult, KeyDirection, Keysym, Status};
|
||||||
use {esp_alloc as _, esp_backtrace as _};
|
use {esp_alloc as _, esp_backtrace as _};
|
||||||
|
|
||||||
use crate::matrix::IoeMatrix;
|
use crate::matrix::IoeMatrix;
|
||||||
|
|
@ -461,13 +462,50 @@ async fn main(_spawner: Spawner) {
|
||||||
let report = KEYBOARD_REPORT_PROXY.receive().await;
|
let report = KEYBOARD_REPORT_PROXY.receive().await;
|
||||||
|
|
||||||
if let Report::KeyboardReport(report) = &report {
|
if let Report::KeyboardReport(report) = &report {
|
||||||
warn!("MODIFIERS = {:02x}", report.modifier);
|
const KEYCODES_LEN_MODIFIERS: usize = 8;
|
||||||
const RMK_ALT: u8 = 0xE2;
|
const KEYCODES_LEN_REGULAR: usize = 6;
|
||||||
// TODO: Process modifiers
|
const KEYCODES_LEN: usize = KEYCODES_LEN_MODIFIERS + KEYCODES_LEN_REGULAR;
|
||||||
|
|
||||||
for (keycode_old, &keycode_new) in
|
let mut pressed_keys = rmk::heapless::Vec::<u8, KEYCODES_LEN>::new();
|
||||||
core::iter::zip(&mut previous_state.keycodes, &report.keycodes)
|
let mut released_keys = rmk::heapless::Vec::<u8, KEYCODES_LEN>::new();
|
||||||
|
|
||||||
|
let pressed_mods_bits = !previous_state.modifier & report.modifier;
|
||||||
|
let released_mods_bits = previous_state.modifier & !report.modifier;
|
||||||
|
|
||||||
|
for index in 0..KEYCODES_LEN_MODIFIERS {
|
||||||
|
const USB_HID_LEFT_CTRL: u8 = 0xE0;
|
||||||
|
let mod_bit = 1_u8 << index;
|
||||||
|
let mod_keycode = USB_HID_LEFT_CTRL + index as u8;
|
||||||
|
|
||||||
|
if pressed_mods_bits & mod_bit != 0 {
|
||||||
|
pressed_keys.push(mod_keycode).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
if released_mods_bits & mod_bit != 0 {
|
||||||
|
released_keys.push(mod_keycode).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: This currently depends on pressed keys not changing position in the array.
|
||||||
|
// Should be made independent of that.
|
||||||
|
for (&keycode_old, &keycode_new) in
|
||||||
|
core::iter::zip(&previous_state.keycodes, &report.keycodes)
|
||||||
{
|
{
|
||||||
|
if keycode_old == keycode_new {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if keycode_old != 0 {
|
||||||
|
released_keys.push(keycode_old).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
if keycode_new != 0 {
|
||||||
|
pressed_keys.push(keycode_new).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
previous_state = report.clone();
|
||||||
|
|
||||||
fn into_xkb_keycode(rmk_keycode: u8) -> xkb::Keycode {
|
fn into_xkb_keycode(rmk_keycode: u8) -> xkb::Keycode {
|
||||||
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/hid/hid-input.c?id=refs/tags/v6.18#n27
|
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/hid/hid-input.c?id=refs/tags/v6.18#n27
|
||||||
const UNK: u8 = 240;
|
const UNK: u8 = 240;
|
||||||
|
|
@ -495,40 +533,53 @@ async fn main(_spawner: Spawner) {
|
||||||
|
|
||||||
// TODO: The combination of these two operations should be precomputed
|
// TODO: The combination of these two operations should be precomputed
|
||||||
// in a const expr into a single look-up table.
|
// in a const expr into a single look-up table.
|
||||||
xkb::Keycode::new(
|
xkb::Keycode::new((HID_KEYBOARD[rmk_keycode as usize] + MIN_KEYCODE) as u32)
|
||||||
(HID_KEYBOARD[rmk_keycode as usize] + MIN_KEYCODE) as u32,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if *keycode_old == 0 && keycode_new == 0 {
|
for keycode in released_keys {
|
||||||
continue;
|
warn!("Release: 0x{:02x} ({})", keycode, keycode);
|
||||||
|
state.update_key(into_xkb_keycode(keycode), KeyDirection::Up);
|
||||||
}
|
}
|
||||||
|
|
||||||
if keycode_new == 0 || ((*keycode_old != 0) && *keycode_old != keycode_new)
|
for keycode in pressed_keys {
|
||||||
|
warn!("Pressed: 0x{:02x} ({})", keycode, keycode);
|
||||||
|
let keycode_xkb = into_xkb_keycode(keycode);
|
||||||
|
let sym = state.key_get_one_sym(keycode_xkb);
|
||||||
|
|
||||||
|
let result: Option<(Keysym, Option<String>)> = match compose_state.feed(sym)
|
||||||
{
|
{
|
||||||
warn!("Release: 0x{:02x} ({})", *keycode_old, *keycode_old);
|
FeedResult::Ignored => {
|
||||||
state.update_key(into_xkb_keycode(*keycode_old), KeyDirection::Up);
|
let string = state.key_get_utf8(keycode_xkb);
|
||||||
|
Some((sym, Some(string)))
|
||||||
}
|
}
|
||||||
|
FeedResult::Accepted => {
|
||||||
if *keycode_old == 0 || ((keycode_new != 0) && *keycode_old != keycode_new)
|
let status = compose_state.status();
|
||||||
{
|
warn!("Compose status: {status:?}");
|
||||||
let keycode_new_xkb = into_xkb_keycode(keycode_new);
|
match status {
|
||||||
let syms = state.key_get_syms(keycode_new_xkb);
|
Status::Nothing => {
|
||||||
|
let string = state.key_get_utf8(keycode_xkb);
|
||||||
for sym in syms {
|
Some((sym, Some(string)))
|
||||||
compose_state.feed(*sym);
|
|
||||||
}
|
}
|
||||||
|
Status::Composing => None,
|
||||||
let string = compose_state.utf8();
|
Status::Composed => {
|
||||||
|
let sym = compose_state.keysym().unwrap_or_default();
|
||||||
warn!("Pressed: 0x{:02x} ({})", keycode_new, keycode_new);
|
let string = compose_state.utf8().unwrap_or_default();
|
||||||
warn!("Syms: {syms:?}");
|
Some((sym, Some(string)))
|
||||||
warn!("String: {string:?}");
|
|
||||||
|
|
||||||
state.update_key(keycode_new_xkb, KeyDirection::Down);
|
|
||||||
}
|
}
|
||||||
|
Status::Cancelled => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
*keycode_old = keycode_new;
|
if let Some((sym, string)) = result {
|
||||||
|
// Change `Some("")` into `None`.
|
||||||
|
let string = string.filter(|string| !string.is_empty());
|
||||||
|
|
||||||
|
warn!("Sym: {sym:?}");
|
||||||
|
warn!("String: {:?}", string.as_ref());
|
||||||
|
|
||||||
|
state.update_key(keycode_xkb, KeyDirection::Down);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue