2021-01-10 22:11:15 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
|
|
|
|
mod api;
|
|
|
|
|
use api::*;
|
2022-02-02 19:26:18 +00:00
|
|
|
mod mappings;
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-04-04 13:25:26 +00:00
|
|
|
use log::info;
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-06-16 17:12:25 +00:00
|
|
|
use num_traits::*;
|
2021-04-03 17:42:30 +00:00
|
|
|
use xous_ipc::Buffer;
|
2022-02-02 18:46:37 +00:00
|
|
|
use xous::{CID, msg_scalar_unpack, msg_blocking_scalar_unpack};
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(any(target_os = "none", target_os = "xous"))]
|
2021-01-10 22:11:15 +00:00
|
|
|
mod implementation {
|
|
|
|
|
use utralib::generated::*;
|
2021-08-22 09:13:36 +00:00
|
|
|
use crate::{RowCol, KeyRawStates, api::*};
|
2022-02-02 19:26:18 +00:00
|
|
|
use crate::mappings::*;
|
2021-04-03 17:42:30 +00:00
|
|
|
use ticktimer_server::Ticktimer;
|
|
|
|
|
use xous::CID;
|
2021-04-04 13:25:26 +00:00
|
|
|
use num_traits::ToPrimitive;
|
2021-05-01 17:38:44 +00:00
|
|
|
use susres::{RegManager, RegOrField, SuspendResume};
|
2021-08-22 09:13:36 +00:00
|
|
|
use std::collections::HashSet;
|
2022-02-02 18:46:37 +00:00
|
|
|
use std::convert::TryInto;
|
2021-04-03 17:42:30 +00:00
|
|
|
|
2021-01-10 22:11:15 +00:00
|
|
|
/// note: the code is structured to use at most 16 rows or 16 cols
|
|
|
|
|
const KBD_ROWS: usize = 9;
|
|
|
|
|
const KBD_COLS: usize = 10;
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) struct Keyboard {
|
2021-04-03 17:42:30 +00:00
|
|
|
conn: CID,
|
2021-01-10 22:11:15 +00:00
|
|
|
csr: utralib::CSR<u32>,
|
2021-04-04 10:23:26 +00:00
|
|
|
/// where the interrupt handler copies the new state
|
2021-08-22 09:13:36 +00:00
|
|
|
new_state: HashSet::<RowCol>,
|
2021-04-03 17:42:30 +00:00
|
|
|
/// remember the last key states
|
2021-08-22 09:13:36 +00:00
|
|
|
last_state: HashSet::<RowCol>,
|
2021-01-10 22:11:15 +00:00
|
|
|
/// connection to the timer for real-time events
|
2021-03-28 10:14:41 +00:00
|
|
|
ticktimer: Ticktimer,
|
2021-01-10 22:11:15 +00:00
|
|
|
/// mapping for ScanCode translation
|
|
|
|
|
map: KeyMap,
|
|
|
|
|
/// delay in ms before a key is considered to be repeating
|
2021-02-14 20:42:30 +00:00
|
|
|
delay: u32,
|
2021-01-10 22:11:15 +00:00
|
|
|
/// rate in ms for repeating a key
|
2021-02-14 20:42:30 +00:00
|
|
|
rate: u32,
|
2021-01-10 22:11:15 +00:00
|
|
|
/// shift key state
|
|
|
|
|
shift_down: bool,
|
|
|
|
|
shift_up: bool,
|
|
|
|
|
/// alt key state
|
|
|
|
|
alt_down: bool,
|
|
|
|
|
alt_up: bool,
|
|
|
|
|
/// timestamp to track repeating key interval
|
|
|
|
|
rate_timestamp: u64,
|
|
|
|
|
/// track the last key held down, which lacks a hold alternate meaning, for repeating
|
|
|
|
|
repeating_key: Option<char>,
|
|
|
|
|
/// timestamp timekeeper for chording / hold key
|
|
|
|
|
chord_timestamp: u64,
|
|
|
|
|
/// chording sample interval
|
2021-02-14 20:42:30 +00:00
|
|
|
chord_interval: u32,
|
2021-01-10 22:11:15 +00:00
|
|
|
/// chord state array
|
|
|
|
|
chord: [[bool; KBD_COLS]; KBD_ROWS],
|
2021-04-16 10:01:48 +00:00
|
|
|
/// memoize number of keys that have been pressed
|
|
|
|
|
chord_active: u32,
|
|
|
|
|
/// indicate if the chord has been captured. Once captured, further presses are ignored, until all keys are let up.
|
|
|
|
|
chord_captured: bool,
|
2021-05-01 17:38:44 +00:00
|
|
|
susres: RegManager::<{utra::keyboard::KEYBOARD_NUMREGS}>,
|
2022-02-02 18:46:37 +00:00
|
|
|
/// a field used for debugging various keyboard issues, especially with the interrupt handler
|
THE BALDEST YAK: keyboard injection now working
I wrote in a commit yesterday how there was no ROI on doing this
yet somehow I kept on doing it. I blame poor impulse control.
Turns out there was a series of race conditions and bugs that
caused this thing to "mostly work" on the first go at it but
really hard to debug and get very right.
1. Litex UART has a weird undocumented API. You advance the Rx
FIFO by writing `1` to the PENDING bit, and no other way. However,
you determine if there are any more characters to receive by
reading the EMPTY bit and seeing if it is 1. The previous method
wrote `1` to the PENDING bit and then used the PENDING bit to
see if a character was available, under the theory that the
PENDING bit would be set again if there was still a character.
This is somewhat true, except that the PENDING bit is set only
some amount of time later which is about comparable to a few
CPU instructions, so if you had a fast loop path the PENDING
bit would show as cleared and you'd exit, which would cause
no further interrupts to fire because the interrupt was
effecitvely not acknowledged.
This isn't written up anywhere but I figured this out by
just reading the source code for Litex UART and BIOS:
https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279
https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44
2. I had the Key Injector IP block in the `sys` domain, which
would have its clock cut when the CPU went to sleep, which would
cause input characters to disappear when the CPU went into power
saving. moving this to "always on" domain fixes that.
3. I had the UART PHY in the always on block (so you could
send or receive exactly one character), but not the
separate UART block (which contained the FIFOs).
The UART block would also go into power
savings, and this would cause FIFO entries to go missing.
4. Escape sequences get sent as a burst of characters. The Key Injector
originally did not have a FIFO of its own, so it would only
remember the *last* key of the sequence that was written, causing
some keys to be dropped occassionally. Adding a 16-deep FIFO
to the injector fixes this.
5. The code I had to read out the injector's results was designed with
the "strobe" bit (to indicate that the FIFO had characters) in the
same regsiter as the character itself. Thus the loop that 1. checked
if the fifo was empty then 2. read and forwarded the character
would actually perform two reads from the FIFO. However, this "mostly worked"
I think because the FIFO retains the last value that was written
even when it was empty, so if only one character was read at a time
everything was fine. It only didn't work when you stuffed two characters
in there, as you'd drop the first one. Reading the FIFO once and
then interpreting the code with a bitmask fixes this.
6. The escape sequences, in their full glory with no dropped characters,
required a refactored parsing routine, which I have now added.
7. backspace->delete mappings, the bane of computer engineers since 1960.
OK. very bald yak. Here I lay down the shears next to several
mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
|
|
|
pub debug: usize,
|
2022-02-02 18:46:37 +00:00
|
|
|
/// a slice that contains the location of the early-boot keyboard settings
|
|
|
|
|
settings: xous::MemoryRange,
|
|
|
|
|
/// a handle to the spinor block so we can update our settings
|
|
|
|
|
spinor: spinor::Spinor,
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-03 17:42:30 +00:00
|
|
|
fn handle_kbd(_irq_no: usize, arg: *mut usize) {
|
|
|
|
|
let kbd = unsafe { &mut *(arg as *mut Keyboard) };
|
2022-01-27 19:36:14 +00:00
|
|
|
let pending = kbd.csr.r(utra::keyboard::EV_PENDING);
|
2021-04-15 20:11:25 +00:00
|
|
|
if kbd.csr.rf(utra::keyboard::EV_PENDING_KEYPRESSED) != 0 {
|
2021-08-22 09:13:36 +00:00
|
|
|
// scan the entire key matrix and return the list of keys that are currently
|
|
|
|
|
// pressed as key codes. If we missed an interrupt, then we missed the key...
|
|
|
|
|
kbd.new_state.clear();
|
|
|
|
|
for r in 0..KBD_ROWS {
|
|
|
|
|
let cols: u16 = kbd_getrow(kbd, r as u8);
|
2021-08-22 21:12:21 +00:00
|
|
|
if cols != 0 {
|
|
|
|
|
for c in 0..KBD_COLS {
|
|
|
|
|
if (cols & (1 << c)) != 0 {
|
|
|
|
|
kbd.new_state.insert(
|
|
|
|
|
RowCol{r: r as _, c: c as _}
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-15 20:11:25 +00:00
|
|
|
xous::try_send_message(kbd.conn,
|
|
|
|
|
xous::Message::new_scalar(Opcode::HandlerTrigger.to_usize().unwrap(), 0, 0, 0, 0)).ok();
|
|
|
|
|
}
|
|
|
|
|
if kbd.csr.rf(utra::keyboard::EV_PENDING_INJECT) != 0 {
|
THE BALDEST YAK: keyboard injection now working
I wrote in a commit yesterday how there was no ROI on doing this
yet somehow I kept on doing it. I blame poor impulse control.
Turns out there was a series of race conditions and bugs that
caused this thing to "mostly work" on the first go at it but
really hard to debug and get very right.
1. Litex UART has a weird undocumented API. You advance the Rx
FIFO by writing `1` to the PENDING bit, and no other way. However,
you determine if there are any more characters to receive by
reading the EMPTY bit and seeing if it is 1. The previous method
wrote `1` to the PENDING bit and then used the PENDING bit to
see if a character was available, under the theory that the
PENDING bit would be set again if there was still a character.
This is somewhat true, except that the PENDING bit is set only
some amount of time later which is about comparable to a few
CPU instructions, so if you had a fast loop path the PENDING
bit would show as cleared and you'd exit, which would cause
no further interrupts to fire because the interrupt was
effecitvely not acknowledged.
This isn't written up anywhere but I figured this out by
just reading the source code for Litex UART and BIOS:
https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279
https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44
2. I had the Key Injector IP block in the `sys` domain, which
would have its clock cut when the CPU went to sleep, which would
cause input characters to disappear when the CPU went into power
saving. moving this to "always on" domain fixes that.
3. I had the UART PHY in the always on block (so you could
send or receive exactly one character), but not the
separate UART block (which contained the FIFOs).
The UART block would also go into power
savings, and this would cause FIFO entries to go missing.
4. Escape sequences get sent as a burst of characters. The Key Injector
originally did not have a FIFO of its own, so it would only
remember the *last* key of the sequence that was written, causing
some keys to be dropped occassionally. Adding a 16-deep FIFO
to the injector fixes this.
5. The code I had to read out the injector's results was designed with
the "strobe" bit (to indicate that the FIFO had characters) in the
same regsiter as the character itself. Thus the loop that 1. checked
if the fifo was empty then 2. read and forwarded the character
would actually perform two reads from the FIFO. However, this "mostly worked"
I think because the FIFO retains the last value that was written
even when it was empty, so if only one character was read at a time
everything was fine. It only didn't work when you stuffed two characters
in there, as you'd drop the first one. Reading the FIFO once and
then interpreting the code with a bitmask fixes this.
6. The escape sequences, in their full glory with no dropped characters,
required a refactored parsing routine, which I have now added.
7. backspace->delete mappings, the bane of computer engineers since 1960.
OK. very bald yak. Here I lay down the shears next to several
mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
|
|
|
loop {
|
|
|
|
|
let char_reg = kbd.csr.r(utra::keyboard::UART_CHAR);
|
|
|
|
|
if char_reg & kbd.csr.ms(utra::keyboard::UART_CHAR_STB, 1) != 0 {
|
|
|
|
|
xous::try_send_message(kbd.conn,
|
|
|
|
|
xous::Message::new_scalar(Opcode::InjectKey.to_usize().unwrap(), (char_reg & 0xff) as _, 0, 0, 0)
|
|
|
|
|
).ok();
|
|
|
|
|
kbd.debug += 1;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-01-27 19:36:14 +00:00
|
|
|
}
|
2021-04-15 20:11:25 +00:00
|
|
|
}
|
2022-01-27 19:36:14 +00:00
|
|
|
kbd.csr.wo(utra::keyboard::EV_PENDING, pending);
|
2021-04-03 17:42:30 +00:00
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
/// get the column activation contents of the given row
|
|
|
|
|
/// row is coded as a binary number, so the result of kbd_rowchange has to be decoded from a binary
|
|
|
|
|
/// vector of rows to a set of numbers prior to using this function
|
|
|
|
|
fn kbd_getrow(kbd: &Keyboard, row: u8) -> u16 {
|
|
|
|
|
match row {
|
|
|
|
|
0 => kbd.csr.rf(utra::keyboard::ROW0DAT_ROW0DAT) as u16,
|
|
|
|
|
1 => kbd.csr.rf(utra::keyboard::ROW1DAT_ROW1DAT) as u16,
|
|
|
|
|
2 => kbd.csr.rf(utra::keyboard::ROW2DAT_ROW2DAT) as u16,
|
|
|
|
|
3 => kbd.csr.rf(utra::keyboard::ROW3DAT_ROW3DAT) as u16,
|
|
|
|
|
4 => kbd.csr.rf(utra::keyboard::ROW4DAT_ROW4DAT) as u16,
|
|
|
|
|
5 => kbd.csr.rf(utra::keyboard::ROW5DAT_ROW5DAT) as u16,
|
|
|
|
|
6 => kbd.csr.rf(utra::keyboard::ROW6DAT_ROW6DAT) as u16,
|
|
|
|
|
7 => kbd.csr.rf(utra::keyboard::ROW7DAT_ROW7DAT) as u16,
|
|
|
|
|
8 => kbd.csr.rf(utra::keyboard::ROW8DAT_ROW8DAT) as u16,
|
|
|
|
|
_ => 0
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-03 17:42:30 +00:00
|
|
|
|
2021-01-10 22:11:15 +00:00
|
|
|
impl Keyboard {
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn new(sid: xous::SID) -> Keyboard {
|
2021-01-10 22:11:15 +00:00
|
|
|
let csr = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utra::keyboard::HW_KEYBOARD_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map Keyboard CSR range");
|
2022-02-02 18:46:37 +00:00
|
|
|
let setting_page = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new((xous::EARLY_SETTINGS + xous::FLASH_PHYS_BASE) as usize),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map Keyboard pre-boot setting");
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-04-03 17:42:30 +00:00
|
|
|
let ticktimer = ticktimer_server::Ticktimer::new().expect("couldn't connect to ticktimer");
|
2021-03-28 20:35:11 +00:00
|
|
|
let timestamp = ticktimer.elapsed_ms();
|
2022-02-02 18:46:37 +00:00
|
|
|
let settings: &[u8] = setting_page.as_slice();
|
2021-04-03 17:42:30 +00:00
|
|
|
|
2021-09-07 11:11:54 +00:00
|
|
|
let default_map = if cfg!(feature = "braille") {
|
|
|
|
|
KeyMap::Braille
|
|
|
|
|
} else {
|
2022-02-02 18:46:37 +00:00
|
|
|
// the boot setting is intended to only contain the keyboard mapping...soooooooo.....we manage it as such
|
|
|
|
|
// if feature creep causes more stuff to end up here this should be made into something more generic.
|
|
|
|
|
// But! If you think you need to slap something in the boot setting, think again. This is an unverified,
|
|
|
|
|
// unencrypted and plaintext bit of memory that anyone can read or mess with. The only reason the keyboard
|
|
|
|
|
// layout is kept here is because you want your keyboard to be in the right language before typing in the
|
|
|
|
|
// password to unlock the PDDB, which is *actually* where all the user settings should be located.
|
|
|
|
|
let code = u32::from_le_bytes(settings[..4].try_into().unwrap());
|
|
|
|
|
KeyMap::from(code as usize)
|
2021-09-07 11:11:54 +00:00
|
|
|
};
|
2022-02-02 18:46:37 +00:00
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
2021-09-07 11:11:54 +00:00
|
|
|
|
2021-04-03 17:42:30 +00:00
|
|
|
let mut kbd = Keyboard {
|
|
|
|
|
conn: xous::connect(sid).unwrap(),
|
2021-01-10 22:11:15 +00:00
|
|
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
2021-08-22 09:13:36 +00:00
|
|
|
new_state: HashSet::with_capacity(16), // pre-allocate space since this has to work in an interrupt context
|
|
|
|
|
last_state: HashSet::with_capacity(16),
|
2021-04-03 17:42:30 +00:00
|
|
|
ticktimer,
|
2021-09-07 11:11:54 +00:00
|
|
|
map: default_map,
|
2021-01-10 22:11:15 +00:00
|
|
|
delay: 500,
|
|
|
|
|
rate: 20,
|
|
|
|
|
shift_down: false,
|
|
|
|
|
shift_up: false,
|
|
|
|
|
alt_down: false,
|
|
|
|
|
alt_up: false,
|
|
|
|
|
repeating_key: None,
|
|
|
|
|
rate_timestamp: timestamp,
|
|
|
|
|
chord_timestamp: timestamp,
|
2021-04-16 10:01:48 +00:00
|
|
|
chord_interval: 50,
|
2021-01-10 22:11:15 +00:00
|
|
|
chord: [[false; KBD_COLS]; KBD_ROWS],
|
2021-04-16 10:01:48 +00:00
|
|
|
chord_active: 0,
|
|
|
|
|
chord_captured: false,
|
2021-05-01 17:38:44 +00:00
|
|
|
susres: RegManager::new(csr.as_mut_ptr() as *mut u32),
|
THE BALDEST YAK: keyboard injection now working
I wrote in a commit yesterday how there was no ROI on doing this
yet somehow I kept on doing it. I blame poor impulse control.
Turns out there was a series of race conditions and bugs that
caused this thing to "mostly work" on the first go at it but
really hard to debug and get very right.
1. Litex UART has a weird undocumented API. You advance the Rx
FIFO by writing `1` to the PENDING bit, and no other way. However,
you determine if there are any more characters to receive by
reading the EMPTY bit and seeing if it is 1. The previous method
wrote `1` to the PENDING bit and then used the PENDING bit to
see if a character was available, under the theory that the
PENDING bit would be set again if there was still a character.
This is somewhat true, except that the PENDING bit is set only
some amount of time later which is about comparable to a few
CPU instructions, so if you had a fast loop path the PENDING
bit would show as cleared and you'd exit, which would cause
no further interrupts to fire because the interrupt was
effecitvely not acknowledged.
This isn't written up anywhere but I figured this out by
just reading the source code for Litex UART and BIOS:
https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279
https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44
2. I had the Key Injector IP block in the `sys` domain, which
would have its clock cut when the CPU went to sleep, which would
cause input characters to disappear when the CPU went into power
saving. moving this to "always on" domain fixes that.
3. I had the UART PHY in the always on block (so you could
send or receive exactly one character), but not the
separate UART block (which contained the FIFOs).
The UART block would also go into power
savings, and this would cause FIFO entries to go missing.
4. Escape sequences get sent as a burst of characters. The Key Injector
originally did not have a FIFO of its own, so it would only
remember the *last* key of the sequence that was written, causing
some keys to be dropped occassionally. Adding a 16-deep FIFO
to the injector fixes this.
5. The code I had to read out the injector's results was designed with
the "strobe" bit (to indicate that the FIFO had characters) in the
same regsiter as the character itself. Thus the loop that 1. checked
if the fifo was empty then 2. read and forwarded the character
would actually perform two reads from the FIFO. However, this "mostly worked"
I think because the FIFO retains the last value that was written
even when it was empty, so if only one character was read at a time
everything was fine. It only didn't work when you stuffed two characters
in there, as you'd drop the first one. Reading the FIFO once and
then interpreting the code with a bitmask fixes this.
6. The escape sequences, in their full glory with no dropped characters,
required a refactored parsing routine, which I have now added.
7. backspace->delete mappings, the bane of computer engineers since 1960.
OK. very bald yak. Here I lay down the shears next to several
mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
|
|
|
debug: 0,
|
2022-02-02 18:46:37 +00:00
|
|
|
settings: setting_page,
|
|
|
|
|
spinor: spinor::Spinor::new(&xns).unwrap(),
|
2021-01-10 22:11:15 +00:00
|
|
|
};
|
|
|
|
|
|
2021-04-03 17:42:30 +00:00
|
|
|
xous::claim_interrupt(
|
|
|
|
|
utra::keyboard::KEYBOARD_IRQ,
|
|
|
|
|
handle_kbd,
|
|
|
|
|
(&mut kbd) as *mut Keyboard as *mut usize,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't claim irq");
|
2021-04-15 20:11:25 +00:00
|
|
|
kbd.csr.wo(utra::keyboard::EV_PENDING, kbd.csr.r(utra::keyboard::EV_PENDING)); // clear in case it's pending for some reason
|
|
|
|
|
kbd.csr.wo(utra::keyboard::EV_ENABLE,
|
|
|
|
|
kbd.csr.ms(utra::keyboard::EV_ENABLE_KEYPRESSED, 1) |
|
|
|
|
|
kbd.csr.ms(utra::keyboard::EV_ENABLE_INJECT, 1)
|
|
|
|
|
);
|
2021-01-11 16:00:57 +00:00
|
|
|
|
2021-05-01 17:38:44 +00:00
|
|
|
kbd.susres.push_fixed_value(RegOrField::Reg(utra::keyboard::EV_PENDING), 0xFFFF_FFFF);
|
|
|
|
|
kbd.susres.push(RegOrField::Reg(utra::keyboard::EV_ENABLE), None);
|
|
|
|
|
|
2021-01-10 22:11:15 +00:00
|
|
|
kbd
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn suspend(&mut self) {
|
2021-05-01 17:38:44 +00:00
|
|
|
self.susres.suspend();
|
|
|
|
|
self.csr.wo(utra::keyboard::EV_ENABLE, 0);
|
|
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn resume(&mut self) {
|
2021-05-01 17:38:44 +00:00
|
|
|
self.susres.resume();
|
2021-07-13 15:10:10 +00:00
|
|
|
|
|
|
|
|
// clear the keyboard state vectors -- actually, if a key was being pressed at the time of suspend
|
|
|
|
|
// it's not really relevant anymore; let's throw everything away and start from a clean slate.
|
|
|
|
|
self.new_state.clear();
|
|
|
|
|
self.last_state.clear();
|
|
|
|
|
self.shift_down = false;
|
|
|
|
|
self.shift_up = false;
|
|
|
|
|
self.alt_down = false;
|
|
|
|
|
self.alt_up = false;
|
|
|
|
|
self.repeating_key = None;
|
|
|
|
|
self.chord_captured = false;
|
|
|
|
|
self.chord_active = 0;
|
|
|
|
|
self.chord = [[false; KBD_COLS]; KBD_ROWS];
|
2021-08-22 21:12:21 +00:00
|
|
|
|
|
|
|
|
// ensure interrupts are re-enabled -- this could /shouldn't/ be necessary but we're having
|
|
|
|
|
// some strange resume behavior, trying to see if this resolves it.
|
|
|
|
|
self.csr.wo(utra::keyboard::EV_PENDING, self.csr.r(utra::keyboard::EV_PENDING));
|
|
|
|
|
self.csr.wo(utra::keyboard::EV_ENABLE,
|
|
|
|
|
self.csr.ms(utra::keyboard::EV_ENABLE_KEYPRESSED, 1) |
|
|
|
|
|
self.csr.ms(utra::keyboard::EV_ENABLE_INJECT, 1)
|
|
|
|
|
);
|
2021-05-01 17:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn set_map(&mut self, map: KeyMap) {
|
2022-02-02 18:46:37 +00:00
|
|
|
let code_usize: usize = map.into();
|
|
|
|
|
let code = (code_usize as u32).to_le_bytes();
|
|
|
|
|
let settings: &[u8] = self.settings.as_slice();
|
|
|
|
|
self.spinor.patch(settings, xous::EARLY_SETTINGS,
|
|
|
|
|
&code, 0
|
|
|
|
|
).expect("couldn't patch our keyboard code");
|
2021-01-10 22:11:15 +00:00
|
|
|
self.map = map;
|
|
|
|
|
}
|
2022-02-02 18:46:37 +00:00
|
|
|
pub(crate) fn get_map(&mut self) -> KeyMap {
|
|
|
|
|
// refresh the map from the setting in the FLASH
|
|
|
|
|
let settings: &[u8] = self.settings.as_slice();
|
|
|
|
|
let code = u32::from_le_bytes(settings[..4].try_into().unwrap());
|
|
|
|
|
self.map = KeyMap::from(code as usize);
|
|
|
|
|
self.map
|
|
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn set_repeat(&mut self, rate: u32, delay: u32) {
|
2021-01-10 22:11:15 +00:00
|
|
|
self.rate = rate;
|
|
|
|
|
self.delay = delay;
|
|
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn set_chord_interval(&mut self, delay: u32) {
|
2021-01-10 22:11:15 +00:00
|
|
|
self.chord_interval = delay;
|
|
|
|
|
}
|
2021-08-22 10:52:33 +00:00
|
|
|
pub(crate) fn get_repeat_check_interval(&self) -> u32 {
|
|
|
|
|
self.rate
|
|
|
|
|
}
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-08-22 21:12:21 +00:00
|
|
|
pub(crate) fn poll(&mut self) {
|
|
|
|
|
// disable the interrupt while we're polling, to avoid a race condition...
|
2022-01-27 19:36:14 +00:00
|
|
|
self.csr.rmwf(utra::keyboard::EV_ENABLE_KEYPRESSED, 0);
|
2021-08-22 21:12:21 +00:00
|
|
|
self.new_state.clear();
|
|
|
|
|
for r in 0..KBD_ROWS {
|
|
|
|
|
let cols: u16 = kbd_getrow(self, r as u8);
|
|
|
|
|
if cols != 0 {
|
|
|
|
|
for c in 0..KBD_COLS {
|
|
|
|
|
if (cols & (1 << c)) != 0 {
|
|
|
|
|
self.new_state.insert(
|
|
|
|
|
RowCol{r: r as _, c: c as _}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if self.csr.rf(utra::keyboard::EV_ENABLE_KEYPRESSED) != 0 {
|
|
|
|
|
log::warn!("kbd interrupt was set when we didn't expect it, clearing anyways...");
|
|
|
|
|
self.csr.wfo(utra::keyboard::EV_PENDING_KEYPRESSED, 1);
|
|
|
|
|
}
|
2022-01-27 19:36:14 +00:00
|
|
|
self.csr.rmwf(utra::keyboard::EV_ENABLE_KEYPRESSED, 1);
|
2021-08-22 21:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn update(&mut self) -> KeyRawStates {
|
2021-01-10 22:11:15 +00:00
|
|
|
// EV_PENDING_KEYPRESSED effectively does an XOR of the previous keyboard state
|
|
|
|
|
// to the current state, which is why update() does not repeatedly issue results
|
|
|
|
|
// for keys that are pressed & held.
|
2021-08-23 17:48:30 +00:00
|
|
|
log::trace!("update new_state: {:?}", self.new_state);
|
|
|
|
|
log::trace!("update last_state: {:?}", self.last_state);
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
let mut krs = KeyRawStates::new();
|
2021-04-04 10:23:26 +00:00
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
// compute the key-ups: this would be codes that are in the last_state, but not in the incoming
|
|
|
|
|
// new_state
|
|
|
|
|
for &rc in self.last_state.difference(&self.new_state) {
|
|
|
|
|
krs.keyups.push(rc);
|
2021-04-04 10:23:26 +00:00
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
|
|
|
|
|
// compute key-downs: codes that are in the new_state, but not in last_state
|
|
|
|
|
for &rc in self.new_state.difference(&self.last_state) {
|
|
|
|
|
krs.keydowns.push(rc);
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2021-04-04 10:23:26 +00:00
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
self.last_state.clear();
|
|
|
|
|
for &rc in self.new_state.iter() {
|
|
|
|
|
self.last_state.insert(rc);
|
|
|
|
|
}
|
2021-04-04 10:23:26 +00:00
|
|
|
|
2021-08-23 17:48:30 +00:00
|
|
|
log::trace!("krs: {:?}", krs);
|
2021-04-04 10:23:26 +00:00
|
|
|
krs
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn track_chord(&mut self, krs: &KeyRawStates) -> Vec<char> {
|
2021-01-10 22:11:15 +00:00
|
|
|
/*
|
|
|
|
|
Chording algorithm:
|
|
|
|
|
|
|
|
|
|
1. Wait for first keydown event to happen; record as pressed in table
|
|
|
|
|
2. Start chording timer
|
|
|
|
|
3. Record press/unpress in table
|
|
|
|
|
4. Wait for chording timer to timeout
|
|
|
|
|
5. Extract chord state and turn into scancode using lookup table
|
|
|
|
|
6. Return scancodes
|
|
|
|
|
*/
|
2021-04-16 10:01:48 +00:00
|
|
|
let was_idle = self.chord_active == 0;
|
2021-08-22 09:13:36 +00:00
|
|
|
for rc in krs.keydowns.iter() {
|
2022-03-17 07:20:20 +00:00
|
|
|
log::info!("keydown r: {} c: {}", rc.r, rc.c);
|
2021-08-22 09:13:36 +00:00
|
|
|
self.chord[rc.r as usize][rc.c as usize] = true;
|
|
|
|
|
self.chord_active += 1;
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2022-03-17 09:56:52 +00:00
|
|
|
log::trace!("self.chord: {:?}", self.chord);
|
2021-08-03 17:25:32 +00:00
|
|
|
let mut keystates: Vec<char> = Vec::new();
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-04-16 10:01:48 +00:00
|
|
|
let now = self.ticktimer.elapsed_ms();
|
|
|
|
|
if was_idle && self.chord_active != 0 {
|
|
|
|
|
// "rising edge" of chord_active
|
|
|
|
|
self.chord_timestamp = now; // record the beginning of the chord active interval
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.chord_active != 0 && ((now - self.chord_timestamp) >= self.chord_interval as u64) && !self.chord_captured {
|
|
|
|
|
self.chord_captured = true;
|
2022-03-17 09:56:52 +00:00
|
|
|
log::trace!("interpreting chords");
|
2021-04-16 10:01:48 +00:00
|
|
|
// extract chord state
|
|
|
|
|
/*
|
|
|
|
|
keyboard:
|
|
|
|
|
2 1 0 space 3 4 5
|
|
|
|
|
braille dots:
|
|
|
|
|
0 3
|
|
|
|
|
1 4
|
|
|
|
|
2 5
|
|
|
|
|
*/
|
2022-03-17 07:20:20 +00:00
|
|
|
/*
|
|
|
|
|
7/5 0/1 1/2 5/7 4/8 8/6
|
|
|
|
|
2/3 2/3
|
|
|
|
|
8/0 6/4 3/9
|
|
|
|
|
8/3 5/2 3/6
|
|
|
|
|
8/2
|
|
|
|
|
*/
|
2021-04-16 10:01:48 +00:00
|
|
|
let keys: [bool; 6] = [
|
|
|
|
|
self.chord[1][2],
|
|
|
|
|
self.chord[0][1],
|
2022-03-17 07:20:20 +00:00
|
|
|
self.chord[7][5],
|
|
|
|
|
self.chord[5][7],
|
|
|
|
|
self.chord[4][8],
|
|
|
|
|
self.chord[8][6],
|
2021-04-16 10:01:48 +00:00
|
|
|
];
|
|
|
|
|
let mut keycode: usize = 0;
|
|
|
|
|
for i in 0..keys.len() {
|
|
|
|
|
if keys[i] {
|
|
|
|
|
keycode |= 1 << i;
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2021-04-16 10:01:48 +00:00
|
|
|
}
|
2022-03-17 09:56:52 +00:00
|
|
|
log::trace!("keycode: 0x{:x}", keycode);
|
2021-04-16 10:01:48 +00:00
|
|
|
let keychar = match keycode {
|
|
|
|
|
0b000_001 => Some('a'),
|
|
|
|
|
0b000_011 => Some('b'),
|
|
|
|
|
0b001_001 => Some('c'),
|
|
|
|
|
0b011_001 => Some('d'),
|
|
|
|
|
0b010_001 => Some('e'),
|
|
|
|
|
0b001_011 => Some('f'),
|
|
|
|
|
0b011_011 => Some('g'),
|
|
|
|
|
0b010_011 => Some('h'),
|
|
|
|
|
0b001_010 => Some('i'),
|
|
|
|
|
0b011_010 => Some('j'),
|
|
|
|
|
|
|
|
|
|
0b000_101 => Some('k'),
|
|
|
|
|
0b000_111 => Some('l'),
|
|
|
|
|
0b001_101 => Some('m'),
|
|
|
|
|
0b011_101 => Some('n'),
|
|
|
|
|
0b010_101 => Some('o'),
|
|
|
|
|
0b001_111 => Some('p'),
|
|
|
|
|
0b011_111 => Some('q'),
|
|
|
|
|
0b010_111 => Some('r'),
|
|
|
|
|
0b001_110 => Some('s'),
|
|
|
|
|
0b011_110 => Some('t'),
|
|
|
|
|
|
|
|
|
|
0b100_101 => Some('u'),
|
|
|
|
|
0b100_111 => Some('v'),
|
|
|
|
|
0b101_101 => Some('x'),
|
|
|
|
|
0b111_101 => Some('y'),
|
|
|
|
|
0b110_101 => Some('z'),
|
|
|
|
|
//0b101_111 => Some(''),
|
|
|
|
|
//0b111_111 => Some(''),
|
|
|
|
|
//0b1010_111 => Some(''),
|
|
|
|
|
//0b101_110 => Some(''),
|
|
|
|
|
0b111_010 => Some('w'),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
if let Some(key) = keychar {
|
2021-08-03 17:25:32 +00:00
|
|
|
keystates.push(key);
|
2021-04-16 10:01:48 +00:00
|
|
|
}
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-04-16 10:01:48 +00:00
|
|
|
let up = self.chord[6][4];
|
2021-08-03 17:25:32 +00:00
|
|
|
if up { keystates.push('↑'); }
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-04-16 10:01:48 +00:00
|
|
|
let left = self.chord[8][3];
|
2021-08-03 17:25:32 +00:00
|
|
|
if left { keystates.push('←'); }
|
2021-04-16 10:01:48 +00:00
|
|
|
let right = self.chord[3][6];
|
2021-08-03 17:25:32 +00:00
|
|
|
if right { keystates.push('→'); }
|
2021-04-16 10:01:48 +00:00
|
|
|
let down = self.chord[8][2];
|
2021-08-03 17:25:32 +00:00
|
|
|
if down { keystates.push('↓'); }
|
2021-04-16 10:01:48 +00:00
|
|
|
let center = self.chord[5][2];
|
2021-08-03 17:25:32 +00:00
|
|
|
if center { keystates.push('∴'); }
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-04-16 10:01:48 +00:00
|
|
|
let space = self.chord[2][3];
|
2021-08-03 17:25:32 +00:00
|
|
|
if space { keystates.push(' '); }
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2022-03-17 07:20:20 +00:00
|
|
|
let esc = self.chord[8][0];
|
2021-04-16 10:01:48 +00:00
|
|
|
let bs: char = 0x8_u8.into(); // back space
|
2021-08-03 17:25:32 +00:00
|
|
|
if esc { keystates.push(bs); }
|
2021-04-15 21:34:08 +00:00
|
|
|
|
2022-03-17 07:20:20 +00:00
|
|
|
let func = self.chord[3][9];
|
2021-04-16 10:01:48 +00:00
|
|
|
let cr: char = 0xd_u8.into(); // carriage return
|
2021-08-03 17:25:32 +00:00
|
|
|
if func { keystates.push(cr); }
|
2021-04-15 21:34:08 +00:00
|
|
|
|
2022-03-17 09:56:52 +00:00
|
|
|
log::debug!("up {}, left {}, right {}, down, {}, center, {}, space {}, esc {}, func {}",
|
2021-04-16 10:01:48 +00:00
|
|
|
up, left, right, down, center, space, esc, func);
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
for rc in krs.keyups.iter() {
|
|
|
|
|
self.chord[rc.r as usize][rc.c as usize] = false;
|
|
|
|
|
if self.chord_active > 0 {
|
|
|
|
|
self.chord_active -= 1;
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("received more keyups than we had keydowns!")
|
2021-04-15 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-16 10:01:48 +00:00
|
|
|
if self.chord_active == 0 {
|
|
|
|
|
self.chord_captured = false;
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keystates
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) fn track_keys(&mut self, krs: &KeyRawStates) -> Vec<char> {
|
2021-01-10 22:11:15 +00:00
|
|
|
/*
|
|
|
|
|
"conventional" keyboard algorithm. The goals of this are to differentiate
|
|
|
|
|
the cases of "shift", "alt", and "hold".
|
|
|
|
|
|
|
|
|
|
thus, we check for the special-case of shift/alt in the keydowns/keyups vectors, and
|
|
|
|
|
track them as separate modifiers
|
|
|
|
|
|
|
|
|
|
then for all others, we note the down time, and compare it to the current time
|
|
|
|
|
to determine if a "hold" modifier applies
|
|
|
|
|
*/
|
2021-08-03 17:25:32 +00:00
|
|
|
let mut ks: Vec<char> = Vec::new();
|
2021-01-10 22:11:15 +00:00
|
|
|
|
|
|
|
|
// first check for shift and alt keys
|
2021-08-22 09:13:36 +00:00
|
|
|
for rc in krs.keydowns.iter() {
|
|
|
|
|
match self.map {
|
|
|
|
|
KeyMap::Azerty => {
|
|
|
|
|
if (rc.r == 8) && (rc.c == 5) { // left shift (orange)
|
|
|
|
|
if self.alt_up == false {
|
|
|
|
|
self.alt_down = true;
|
|
|
|
|
} else {
|
|
|
|
|
self.alt_up = false;
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
} else if (rc.r == 8) && (rc.c == 9) { // right shift (yellow)
|
|
|
|
|
if self.shift_up == false {
|
|
|
|
|
self.shift_down = true;
|
|
|
|
|
} else {
|
|
|
|
|
self.shift_up = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => { // the rest just have one color of shift
|
|
|
|
|
if ((rc.r == 8) && (rc.c == 5)) || ((rc.r == 8) && (rc.c == 9)) {
|
|
|
|
|
// if the shift key was tapped twice, remove the shift modifier
|
|
|
|
|
if self.shift_up == false {
|
|
|
|
|
//info!("shift down true");
|
|
|
|
|
self.shift_down = true;
|
|
|
|
|
} else {
|
|
|
|
|
//info!("shift up false");
|
|
|
|
|
self.shift_up = false;
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
let mut keyups_noshift: Vec::<RowCol> = Vec::new();
|
|
|
|
|
for &rc in krs.keyups.iter() {
|
|
|
|
|
match self.map {
|
|
|
|
|
KeyMap::Azerty => {
|
|
|
|
|
if (rc.r == 8) && (rc.c == 5) { // left shift (orange)
|
|
|
|
|
if self.alt_down {
|
|
|
|
|
self.alt_up = true;
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
self.alt_down = false;
|
|
|
|
|
} else if (rc.r == 8) && (rc.c == 9) { // right shift (yellow)
|
|
|
|
|
if self.shift_down {
|
|
|
|
|
self.shift_up = true;
|
|
|
|
|
}
|
|
|
|
|
self.shift_down = false;
|
|
|
|
|
} else {
|
|
|
|
|
keyups_noshift.push(RowCol{r: rc.r as _, c: rc.c as _});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => { // the rest just have one color of shift
|
|
|
|
|
if ((rc.r == 8) && (rc.c == 5)) || ((rc.r == 8) && (rc.c == 9)) {
|
|
|
|
|
// only set the shift-up if we didn't previously clear it with a double-tap of shift
|
|
|
|
|
if self.shift_down {
|
|
|
|
|
//info!("shift up true");
|
|
|
|
|
self.shift_up = true;
|
|
|
|
|
}
|
|
|
|
|
//info!("shift down false");
|
|
|
|
|
self.shift_down = false;
|
|
|
|
|
} else {
|
|
|
|
|
//info!("adding non-shift entry {:?}", rc);
|
|
|
|
|
keyups_noshift.push(RowCol{r: rc.r as _, c: rc.c as _});
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-10 22:11:15 +00:00
|
|
|
|
|
|
|
|
// interpret keys in the context of the shift/alt modifiers
|
2021-08-22 09:13:36 +00:00
|
|
|
if !krs.keydowns.is_empty() {
|
2021-03-28 20:35:11 +00:00
|
|
|
self.chord_timestamp = self.ticktimer.elapsed_ms();
|
2021-08-22 09:13:36 +00:00
|
|
|
}
|
|
|
|
|
for &rc in krs.keydowns.iter() {
|
|
|
|
|
let code = match self.map {
|
|
|
|
|
KeyMap::Qwerty => map_qwerty(rc),
|
|
|
|
|
KeyMap::Dvorak => map_dvorak(rc),
|
2022-02-02 19:26:18 +00:00
|
|
|
KeyMap::Azerty => map_azerty(rc),
|
|
|
|
|
KeyMap::Qwertz => map_qwertz(rc),
|
2021-08-22 09:13:36 +00:00
|
|
|
_ => ScanCode {key: None, shift: None, hold: None, alt: None},
|
|
|
|
|
};
|
2022-03-26 16:19:55 +00:00
|
|
|
if code.hold == None
|
|
|
|
|
&& !((rc.r == 5) && (rc.c == 2)) // scan code for the menu key
|
|
|
|
|
{ // if there isn't a pre-defined meaning if the key is held *and* it's not the menu key: it's a repeating key
|
2021-08-22 09:13:36 +00:00
|
|
|
if let Some(key) = code.key {
|
|
|
|
|
self.repeating_key = Some(key);
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
|
2021-03-28 20:35:11 +00:00
|
|
|
let now = self.ticktimer.elapsed_ms();
|
2021-01-10 22:11:15 +00:00
|
|
|
let hold: bool;
|
|
|
|
|
if (now - self.chord_timestamp) >= self.delay as u64 {
|
|
|
|
|
if self.rate_timestamp <= self.chord_timestamp {
|
|
|
|
|
self.rate_timestamp = now;
|
|
|
|
|
}
|
|
|
|
|
hold = true;
|
|
|
|
|
} else {
|
|
|
|
|
hold = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
for &rc in keyups_noshift.iter() {
|
|
|
|
|
// info!("interpreting keyups_noshift entry {:?}", rc);
|
|
|
|
|
let code = match self.map {
|
|
|
|
|
KeyMap::Qwerty => map_qwerty(rc),
|
|
|
|
|
KeyMap::Dvorak => map_dvorak(rc),
|
2022-02-02 19:39:28 +00:00
|
|
|
KeyMap::Azerty => map_azerty(rc),
|
|
|
|
|
KeyMap::Qwertz => map_qwertz(rc),
|
2021-08-22 09:13:36 +00:00
|
|
|
_ => ScanCode {key: None, shift: None, hold: None, alt: None},
|
|
|
|
|
};
|
|
|
|
|
// delete the key repeat if there is one
|
|
|
|
|
if code.hold == None {
|
|
|
|
|
if let Some(key) = code.key {
|
|
|
|
|
if let Some(rk) = self.repeating_key {
|
|
|
|
|
if rk == key {
|
|
|
|
|
self.repeating_key = None;
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
}
|
2021-01-11 16:00:57 +00:00
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
match self.map {
|
|
|
|
|
KeyMap::Azerty => {
|
|
|
|
|
if self.shift_down || self.shift_up {
|
|
|
|
|
if let Some(shiftcode) = code.shift {
|
|
|
|
|
ks.push(shiftcode);
|
|
|
|
|
} else if let Some(keycode) = code.key {
|
|
|
|
|
ks.push(keycode);
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
self.shift_down = false;
|
|
|
|
|
self.shift_up = false;
|
|
|
|
|
} else if self.alt_down || self.alt_up {
|
|
|
|
|
if let Some(altcode) = code.alt {
|
|
|
|
|
ks.push(altcode);
|
|
|
|
|
} else if let Some(shiftcode) = code.shift {
|
|
|
|
|
ks.push(shiftcode);
|
|
|
|
|
} else if let Some(keycode) = code.key {
|
|
|
|
|
ks.push(keycode);
|
|
|
|
|
}
|
|
|
|
|
self.alt_down = false;
|
|
|
|
|
self.alt_up = false;
|
|
|
|
|
} else if hold {
|
|
|
|
|
if let Some(holdcode) = code.hold {
|
|
|
|
|
ks.push(holdcode);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if let Some(keycode) = code.key {
|
|
|
|
|
ks.push(keycode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
if self.shift_down || self.alt_down || self.shift_up || self.alt_up {
|
|
|
|
|
if let Some(shiftcode) = code.shift {
|
|
|
|
|
ks.push(shiftcode);
|
|
|
|
|
} else if let Some(keycode) = code.key {
|
|
|
|
|
ks.push(keycode);
|
|
|
|
|
}
|
|
|
|
|
self.shift_down = false;
|
|
|
|
|
self.alt_down = false;
|
|
|
|
|
self.shift_up = false;
|
|
|
|
|
self.alt_up = false;
|
|
|
|
|
} else if hold {
|
|
|
|
|
if let Some(holdcode) = code.hold {
|
|
|
|
|
ks.push(holdcode);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if let Some(keycode) = code.key {
|
|
|
|
|
// info!("appeding normal key '{}'", keycode);
|
|
|
|
|
ks.push(keycode);
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if we're in a key hold state, we've passed the rate timestamp point, and there's a repeating key defined
|
|
|
|
|
if hold && ((now - self.rate_timestamp) >= self.rate as u64) && self.repeating_key.is_some() {
|
|
|
|
|
self.rate_timestamp = now;
|
|
|
|
|
if let Some(repeatkey) = self.repeating_key {
|
2021-08-03 17:25:32 +00:00
|
|
|
ks.push(repeatkey);
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ks
|
|
|
|
|
}
|
2021-08-22 10:52:33 +00:00
|
|
|
pub fn is_repeating_key(&self) -> bool {
|
|
|
|
|
self.repeating_key.is_some()
|
|
|
|
|
}
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// a stub to try to avoid breaking hosted mode for as long as possible.
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
2021-01-10 22:11:15 +00:00
|
|
|
mod implementation {
|
2021-08-22 09:13:36 +00:00
|
|
|
use crate::*;
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-03-03 14:50:46 +00:00
|
|
|
#[allow(dead_code)]
|
2021-08-22 09:13:36 +00:00
|
|
|
pub(crate) struct Keyboard {
|
2021-04-03 17:42:30 +00:00
|
|
|
cid: xous::CID,
|
2021-01-10 22:11:15 +00:00
|
|
|
map: KeyMap,
|
2021-02-17 08:58:46 +00:00
|
|
|
rate: u32,
|
|
|
|
|
delay: u32,
|
|
|
|
|
chord_interval: u32,
|
2022-01-30 07:35:44 +00:00
|
|
|
pub debug: u32,
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Keyboard {
|
2021-04-03 17:42:30 +00:00
|
|
|
pub fn new(sid: xous::SID) -> Keyboard {
|
2021-01-10 22:11:15 +00:00
|
|
|
Keyboard {
|
2021-04-03 17:42:30 +00:00
|
|
|
cid: xous::connect(sid).unwrap(),
|
2021-01-10 22:11:15 +00:00
|
|
|
map: KeyMap::Qwerty,
|
|
|
|
|
rate: 20,
|
|
|
|
|
delay: 200,
|
|
|
|
|
chord_interval: 50,
|
2022-01-30 07:35:44 +00:00
|
|
|
debug: 0,
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-01 17:38:44 +00:00
|
|
|
pub fn suspend(&self) {
|
|
|
|
|
}
|
|
|
|
|
pub fn resume(&self) {
|
|
|
|
|
}
|
2021-01-10 22:11:15 +00:00
|
|
|
|
|
|
|
|
pub fn set_map(&mut self, map: KeyMap) {
|
|
|
|
|
self.map = map;
|
|
|
|
|
}
|
|
|
|
|
pub fn get_map(&self) -> KeyMap {self.map}
|
|
|
|
|
|
2021-04-05 15:50:00 +00:00
|
|
|
pub fn update(&self) -> KeyRawStates {
|
|
|
|
|
KeyRawStates::new()
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub fn track_chord(&mut self, _krs: &KeyRawStates) -> Vec<char> {
|
2021-01-11 20:20:37 +00:00
|
|
|
Vec::new()
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub fn track_keys(&mut self, _rs: &KeyRawStates) -> Vec<char> {
|
2021-01-11 20:20:37 +00:00
|
|
|
Vec::new()
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-14 20:42:30 +00:00
|
|
|
pub fn set_repeat(&mut self, rate: u32, delay: u32) {
|
2021-01-10 22:11:15 +00:00
|
|
|
self.rate = rate;
|
|
|
|
|
self.delay = delay;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 20:42:30 +00:00
|
|
|
pub fn set_chord_interval(&mut self, delay: u32) {
|
2021-01-10 22:11:15 +00:00
|
|
|
self.chord_interval = delay;
|
|
|
|
|
}
|
2021-08-24 15:34:33 +00:00
|
|
|
|
|
|
|
|
pub fn is_repeating_key(&self) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn get_repeat_check_interval(&self) -> u32 {
|
|
|
|
|
self.rate
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn poll(&mut self) {}
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
|
|
|
|
use crate::implementation::Keyboard;
|
|
|
|
|
log_server::init_wait().unwrap();
|
2021-04-16 10:01:48 +00:00
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
2021-04-03 17:42:30 +00:00
|
|
|
info!("my PID is {}", xous::process::id());
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-03-29 18:13:23 +00:00
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
2021-06-15 09:41:14 +00:00
|
|
|
// connections expected:
|
|
|
|
|
// - GAM
|
|
|
|
|
// - graphics (if building for hosted mode)
|
2021-09-17 18:22:35 +00:00
|
|
|
// - oqc (for factory test)
|
2022-02-02 18:46:37 +00:00
|
|
|
// - status sub system (for setting the layout)
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(any(target_os = "none", target_os = "xous"))]
|
2021-09-15 09:59:13 +00:00
|
|
|
let kbd_sid = xns.register_name(api::SERVER_NAME_KBD, Some(3)).expect("can't register server");
|
2022-02-02 18:46:37 +00:00
|
|
|
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
|
|
|
|
let kbd_sid = xns.register_name(api::SERVER_NAME_KBD, Some(4)).expect("can't register server");
|
2021-04-03 17:42:30 +00:00
|
|
|
log::trace!("registered with NS -- {:?}", kbd_sid);
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-01-13 07:04:14 +00:00
|
|
|
// Create a new kbd object
|
2021-04-03 17:42:30 +00:00
|
|
|
let mut kbd = Keyboard::new(kbd_sid);
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-05-01 17:38:44 +00:00
|
|
|
// register a suspend/resume listener
|
2021-08-22 10:52:33 +00:00
|
|
|
let self_cid = xous::connect(kbd_sid).expect("couldn't create suspend callback connection");
|
2022-01-10 05:45:56 +00:00
|
|
|
let mut susres = susres::Susres::new(None, &xns, Opcode::SuspendResume as u32, self_cid).expect("couldn't create suspend/resume object");
|
2021-08-22 10:52:33 +00:00
|
|
|
|
|
|
|
|
// start a thread that can ping the keyboard loop when a key is held down
|
|
|
|
|
let ticktimer = ticktimer_server::Ticktimer::new().unwrap();
|
2021-05-01 17:38:44 +00:00
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
let mut listener_conn: Option<CID> = None;
|
|
|
|
|
let mut listener_op: Option<usize> = None;
|
2021-09-15 09:59:13 +00:00
|
|
|
let mut raw_listener_conn: Option<CID> = None;
|
|
|
|
|
let mut raw_listener_op: Option<u32> = None;
|
2021-01-10 22:11:15 +00:00
|
|
|
|
2021-04-16 11:27:48 +00:00
|
|
|
let mut vibe = false;
|
2022-01-11 04:02:27 +00:00
|
|
|
let llio = llio::Llio::new(&xns);
|
2021-08-23 17:48:30 +00:00
|
|
|
/*{
|
2021-08-22 21:12:21 +00:00
|
|
|
log::warn!("kbd server is overriding WFI for debugging, remember to disable for production");
|
|
|
|
|
llio.wfi_override(true).unwrap();
|
2021-08-23 17:48:30 +00:00
|
|
|
}*/
|
THE BALDEST YAK: keyboard injection now working
I wrote in a commit yesterday how there was no ROI on doing this
yet somehow I kept on doing it. I blame poor impulse control.
Turns out there was a series of race conditions and bugs that
caused this thing to "mostly work" on the first go at it but
really hard to debug and get very right.
1. Litex UART has a weird undocumented API. You advance the Rx
FIFO by writing `1` to the PENDING bit, and no other way. However,
you determine if there are any more characters to receive by
reading the EMPTY bit and seeing if it is 1. The previous method
wrote `1` to the PENDING bit and then used the PENDING bit to
see if a character was available, under the theory that the
PENDING bit would be set again if there was still a character.
This is somewhat true, except that the PENDING bit is set only
some amount of time later which is about comparable to a few
CPU instructions, so if you had a fast loop path the PENDING
bit would show as cleared and you'd exit, which would cause
no further interrupts to fire because the interrupt was
effecitvely not acknowledged.
This isn't written up anywhere but I figured this out by
just reading the source code for Litex UART and BIOS:
https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279
https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44
2. I had the Key Injector IP block in the `sys` domain, which
would have its clock cut when the CPU went to sleep, which would
cause input characters to disappear when the CPU went into power
saving. moving this to "always on" domain fixes that.
3. I had the UART PHY in the always on block (so you could
send or receive exactly one character), but not the
separate UART block (which contained the FIFOs).
The UART block would also go into power
savings, and this would cause FIFO entries to go missing.
4. Escape sequences get sent as a burst of characters. The Key Injector
originally did not have a FIFO of its own, so it would only
remember the *last* key of the sequence that was written, causing
some keys to be dropped occassionally. Adding a 16-deep FIFO
to the injector fixes this.
5. The code I had to read out the injector's results was designed with
the "strobe" bit (to indicate that the FIFO had characters) in the
same regsiter as the character itself. Thus the loop that 1. checked
if the fifo was empty then 2. read and forwarded the character
would actually perform two reads from the FIFO. However, this "mostly worked"
I think because the FIFO retains the last value that was written
even when it was empty, so if only one character was read at a time
everything was fine. It only didn't work when you stuffed two characters
in there, as you'd drop the first one. Reading the FIFO once and
then interpreting the code with a bitmask fixes this.
6. The escape sequences, in their full glory with no dropped characters,
required a refactored parsing routine, which I have now added.
7. backspace->delete mappings, the bane of computer engineers since 1960.
OK. very bald yak. Here I lay down the shears next to several
mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
|
|
|
let mut esc_index: Option<usize> = None;
|
|
|
|
|
let mut esc_chars = [0u8; 16];
|
2021-03-01 07:08:08 +00:00
|
|
|
|
2021-04-16 11:27:48 +00:00
|
|
|
log::trace!("starting main loop");
|
2021-01-10 22:11:15 +00:00
|
|
|
loop {
|
2021-04-03 17:42:30 +00:00
|
|
|
let msg = xous::receive_message(kbd_sid).unwrap(); // this blocks until we get a message
|
|
|
|
|
log::trace!("Message: {:?}", msg);
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
2021-05-01 17:38:44 +00:00
|
|
|
Some(Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
|
|
|
|
kbd.suspend();
|
|
|
|
|
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
|
|
|
|
|
kbd.resume();
|
|
|
|
|
}),
|
2021-04-16 11:27:48 +00:00
|
|
|
Some(Opcode::Vibe) => msg_scalar_unpack!(msg, ena, _, _, _, {
|
|
|
|
|
if ena != 0 { vibe = true }
|
|
|
|
|
else { vibe = false }
|
|
|
|
|
}),
|
2021-06-16 17:12:25 +00:00
|
|
|
Some(Opcode::RegisterListener) => {
|
2021-08-22 09:13:36 +00:00
|
|
|
let buffer = unsafe{Buffer::from_memory_message(msg.body.memory_message().unwrap())};
|
|
|
|
|
let kr = buffer.as_flat::<KeyboardRegistration, _>().unwrap();
|
|
|
|
|
match xns.request_connection_blocking(kr.server_name.as_str()) {
|
|
|
|
|
Ok(cid) => {
|
|
|
|
|
listener_conn = Some(cid);
|
|
|
|
|
listener_op = Some(kr.listener_op_id as usize);
|
2021-06-16 17:12:25 +00:00
|
|
|
}
|
2021-08-22 09:13:36 +00:00
|
|
|
Err(e) => {
|
|
|
|
|
log::error!("couldn't connect to listener: {:?}", e);
|
|
|
|
|
listener_conn = None;
|
|
|
|
|
listener_op = None;
|
2021-06-16 17:12:25 +00:00
|
|
|
}
|
2021-04-03 17:42:30 +00:00
|
|
|
}
|
2021-06-16 17:12:25 +00:00
|
|
|
},
|
2021-09-15 09:59:13 +00:00
|
|
|
Some(Opcode::RegisterRawListener) => {
|
|
|
|
|
let buffer = unsafe{Buffer::from_memory_message(msg.body.memory_message().unwrap())};
|
|
|
|
|
let kr = buffer.as_flat::<KeyboardRegistration, _>().unwrap();
|
|
|
|
|
match xns.request_connection_blocking(kr.server_name.as_str()) {
|
|
|
|
|
Ok(cid) => {
|
|
|
|
|
raw_listener_conn = Some(cid);
|
|
|
|
|
raw_listener_op = Some(kr.listener_op_id as u32);
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::error!("couldn't connect to listener: {:?}", e);
|
|
|
|
|
raw_listener_conn = None;
|
|
|
|
|
raw_listener_op = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-04-03 17:42:30 +00:00
|
|
|
Some(Opcode::SelectKeyMap) => msg_scalar_unpack!(msg, km, _, _, _, {
|
|
|
|
|
kbd.set_map(KeyMap::from(km))
|
|
|
|
|
}),
|
2022-02-02 18:46:37 +00:00
|
|
|
Some(Opcode::GetKeyMap) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
|
|
|
|
|
xous::return_scalar(msg.sender,
|
|
|
|
|
kbd.get_map().into()
|
|
|
|
|
).expect("can't retrieve keymap");
|
|
|
|
|
}),
|
2021-04-03 17:42:30 +00:00
|
|
|
Some(Opcode::SetRepeat) => msg_scalar_unpack!(msg, rate, delay, _, _, {
|
|
|
|
|
kbd.set_repeat(rate as u32, delay as u32);
|
|
|
|
|
}),
|
|
|
|
|
Some(Opcode::SetChordInterval) => msg_scalar_unpack!(msg, delay, _, _, _, {
|
|
|
|
|
kbd.set_chord_interval(delay as u32);
|
|
|
|
|
}),
|
2021-08-29 05:28:06 +00:00
|
|
|
Some(Opcode::InjectKey) => msg_scalar_unpack!(msg, k, _, _, _, {
|
|
|
|
|
// key substitutions to help things work better
|
THE BALDEST YAK: keyboard injection now working
I wrote in a commit yesterday how there was no ROI on doing this
yet somehow I kept on doing it. I blame poor impulse control.
Turns out there was a series of race conditions and bugs that
caused this thing to "mostly work" on the first go at it but
really hard to debug and get very right.
1. Litex UART has a weird undocumented API. You advance the Rx
FIFO by writing `1` to the PENDING bit, and no other way. However,
you determine if there are any more characters to receive by
reading the EMPTY bit and seeing if it is 1. The previous method
wrote `1` to the PENDING bit and then used the PENDING bit to
see if a character was available, under the theory that the
PENDING bit would be set again if there was still a character.
This is somewhat true, except that the PENDING bit is set only
some amount of time later which is about comparable to a few
CPU instructions, so if you had a fast loop path the PENDING
bit would show as cleared and you'd exit, which would cause
no further interrupts to fire because the interrupt was
effecitvely not acknowledged.
This isn't written up anywhere but I figured this out by
just reading the source code for Litex UART and BIOS:
https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279
https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44
2. I had the Key Injector IP block in the `sys` domain, which
would have its clock cut when the CPU went to sleep, which would
cause input characters to disappear when the CPU went into power
saving. moving this to "always on" domain fixes that.
3. I had the UART PHY in the always on block (so you could
send or receive exactly one character), but not the
separate UART block (which contained the FIFOs).
The UART block would also go into power
savings, and this would cause FIFO entries to go missing.
4. Escape sequences get sent as a burst of characters. The Key Injector
originally did not have a FIFO of its own, so it would only
remember the *last* key of the sequence that was written, causing
some keys to be dropped occassionally. Adding a 16-deep FIFO
to the injector fixes this.
5. The code I had to read out the injector's results was designed with
the "strobe" bit (to indicate that the FIFO had characters) in the
same regsiter as the character itself. Thus the loop that 1. checked
if the fifo was empty then 2. read and forwarded the character
would actually perform two reads from the FIFO. However, this "mostly worked"
I think because the FIFO retains the last value that was written
even when it was empty, so if only one character was read at a time
everything was fine. It only didn't work when you stuffed two characters
in there, as you'd drop the first one. Reading the FIFO once and
then interpreting the code with a bitmask fixes this.
6. The escape sequences, in their full glory with no dropped characters,
required a refactored parsing routine, which I have now added.
7. backspace->delete mappings, the bane of computer engineers since 1960.
OK. very bald yak. Here I lay down the shears next to several
mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
|
|
|
// 1b5b317e = home
|
|
|
|
|
// 1b5b44 = left
|
|
|
|
|
// 1b5b43 = right
|
|
|
|
|
// 1b5b41 = up
|
|
|
|
|
// 1b5b42 = down
|
|
|
|
|
log::trace!("{:x} {}", k, kbd.debug);
|
|
|
|
|
kbd.debug = 0;
|
|
|
|
|
|
|
|
|
|
let key = match esc_index {
|
|
|
|
|
Some(i) => {
|
|
|
|
|
esc_chars[i] = (k & 0xff) as u8;
|
|
|
|
|
match esc_match(&esc_chars[..i + 1]) {
|
|
|
|
|
Ok(m) => {
|
|
|
|
|
if let Some(code) = m {
|
|
|
|
|
// Ok(Some(code)) is a character found
|
|
|
|
|
esc_chars = [0u8; 16];
|
|
|
|
|
esc_index = None;
|
|
|
|
|
code
|
|
|
|
|
} else {
|
|
|
|
|
// Ok(None) means we're still accumulating characters
|
|
|
|
|
if i + 1 < esc_chars.len() {
|
|
|
|
|
esc_index = Some(i + 1);
|
|
|
|
|
} else {
|
|
|
|
|
esc_index = None;
|
|
|
|
|
esc_chars = [0u8; 16];
|
|
|
|
|
}
|
|
|
|
|
'\u{0000}'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// invalid sequence encountered, abort
|
|
|
|
|
Err(_) => {
|
|
|
|
|
log::warn!("Unhandled escape sequence: {:x?}", &esc_chars[..i+1]);
|
|
|
|
|
esc_chars = [0u8; 16];
|
|
|
|
|
esc_index = None;
|
|
|
|
|
'\u{0000}'
|
|
|
|
|
}
|
2021-08-29 05:28:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
THE BALDEST YAK: keyboard injection now working
I wrote in a commit yesterday how there was no ROI on doing this
yet somehow I kept on doing it. I blame poor impulse control.
Turns out there was a series of race conditions and bugs that
caused this thing to "mostly work" on the first go at it but
really hard to debug and get very right.
1. Litex UART has a weird undocumented API. You advance the Rx
FIFO by writing `1` to the PENDING bit, and no other way. However,
you determine if there are any more characters to receive by
reading the EMPTY bit and seeing if it is 1. The previous method
wrote `1` to the PENDING bit and then used the PENDING bit to
see if a character was available, under the theory that the
PENDING bit would be set again if there was still a character.
This is somewhat true, except that the PENDING bit is set only
some amount of time later which is about comparable to a few
CPU instructions, so if you had a fast loop path the PENDING
bit would show as cleared and you'd exit, which would cause
no further interrupts to fire because the interrupt was
effecitvely not acknowledged.
This isn't written up anywhere but I figured this out by
just reading the source code for Litex UART and BIOS:
https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279
https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44
2. I had the Key Injector IP block in the `sys` domain, which
would have its clock cut when the CPU went to sleep, which would
cause input characters to disappear when the CPU went into power
saving. moving this to "always on" domain fixes that.
3. I had the UART PHY in the always on block (so you could
send or receive exactly one character), but not the
separate UART block (which contained the FIFOs).
The UART block would also go into power
savings, and this would cause FIFO entries to go missing.
4. Escape sequences get sent as a burst of characters. The Key Injector
originally did not have a FIFO of its own, so it would only
remember the *last* key of the sequence that was written, causing
some keys to be dropped occassionally. Adding a 16-deep FIFO
to the injector fixes this.
5. The code I had to read out the injector's results was designed with
the "strobe" bit (to indicate that the FIFO had characters) in the
same regsiter as the character itself. Thus the loop that 1. checked
if the fifo was empty then 2. read and forwarded the character
would actually perform two reads from the FIFO. However, this "mostly worked"
I think because the FIFO retains the last value that was written
even when it was empty, so if only one character was read at a time
everything was fine. It only didn't work when you stuffed two characters
in there, as you'd drop the first one. Reading the FIFO once and
then interpreting the code with a bitmask fixes this.
6. The escape sequences, in their full glory with no dropped characters,
required a refactored parsing routine, which I have now added.
7. backspace->delete mappings, the bane of computer engineers since 1960.
OK. very bald yak. Here I lay down the shears next to several
mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
|
|
|
_ => {
|
|
|
|
|
if k == 0x1b {
|
|
|
|
|
esc_index = Some(1);
|
|
|
|
|
esc_chars = [0u8; 16]; // clear the full search array with every escape sequence init
|
|
|
|
|
esc_chars[0] = 0x1b;
|
|
|
|
|
'\u{0000}'
|
|
|
|
|
} else {
|
|
|
|
|
let bs_del_fix = if k == 0x7f {
|
|
|
|
|
0x08
|
|
|
|
|
} else {
|
|
|
|
|
k
|
|
|
|
|
};
|
|
|
|
|
core::char::from_u32(bs_del_fix as u32).unwrap_or('\u{0000}')
|
2021-08-29 05:28:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
if let Some(conn) = listener_conn {
|
2022-01-27 19:36:14 +00:00
|
|
|
if key != '\u{0000}' {
|
|
|
|
|
log::info!("injecting key '{}'({:x})", key, key as u32); // always be noisy about this, it's an exploit path
|
|
|
|
|
xous::send_message(conn,
|
|
|
|
|
xous::Message::new_scalar(listener_op.unwrap(),
|
|
|
|
|
key as u32 as usize,
|
|
|
|
|
'\u{0000}' as u32 as usize,
|
|
|
|
|
'\u{0000}' as u32 as usize,
|
|
|
|
|
'\u{0000}' as u32 as usize,
|
|
|
|
|
)
|
|
|
|
|
).unwrap();
|
|
|
|
|
}
|
2021-04-04 10:23:26 +00:00
|
|
|
}
|
2021-04-03 17:42:30 +00:00
|
|
|
}),
|
2021-04-04 10:23:26 +00:00
|
|
|
Some(Opcode::HandlerTrigger) => {
|
|
|
|
|
let rawstates = kbd.update();
|
2021-04-03 17:42:30 +00:00
|
|
|
|
2021-09-15 09:59:13 +00:00
|
|
|
if raw_listener_conn.is_some() && raw_listener_op.is_some()
|
|
|
|
|
&& (rawstates.keydowns.len() > 0 || rawstates.keyups.len() > 0)
|
|
|
|
|
{
|
|
|
|
|
// manual serialization of KeyRawStates, because rkyv can't derive a serializer.
|
2021-09-17 18:22:35 +00:00
|
|
|
let mut krs_ser: [(u8, u8); 32] = [(255, 255); 32];
|
|
|
|
|
for (&src, (r, c)) in rawstates.keydowns.iter().zip(krs_ser[..16].iter_mut()) {
|
|
|
|
|
*r = src.r;
|
|
|
|
|
*c = src.c;
|
2021-09-15 09:59:13 +00:00
|
|
|
}
|
2021-09-17 18:22:35 +00:00
|
|
|
for (&src, (r, c)) in rawstates.keyups.iter().zip(krs_ser[16..].iter_mut()) {
|
|
|
|
|
*r = src.r;
|
|
|
|
|
*c = src.c;
|
2021-09-15 09:59:13 +00:00
|
|
|
}
|
2021-09-17 18:22:35 +00:00
|
|
|
|
|
|
|
|
let buf = Buffer::into_buf(krs_ser).or(Err(xous::Error::InternalError)).expect("couldn't serialize krs buffer");
|
|
|
|
|
buf.lend(raw_listener_conn.unwrap(), raw_listener_op.unwrap()).expect("couldn't send raw scancodes");
|
2021-09-15 09:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-03 17:42:30 +00:00
|
|
|
// interpret scancodes
|
|
|
|
|
// the track_* functions track the keyup/keydowns to modify keys with shift, hold, and chord state
|
2021-08-03 17:25:32 +00:00
|
|
|
let kc: Vec<char> = match kbd.get_map() {
|
2021-04-03 17:42:30 +00:00
|
|
|
KeyMap::Braille => {
|
2021-08-22 09:13:36 +00:00
|
|
|
kbd.track_chord(&rawstates)
|
2021-04-03 17:42:30 +00:00
|
|
|
},
|
|
|
|
|
_ => {
|
2021-08-22 09:13:36 +00:00
|
|
|
kbd.track_keys(&rawstates)
|
2021-04-03 17:42:30 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// send keys, if any
|
2021-08-22 09:13:36 +00:00
|
|
|
if kc.len() > 0 && listener_conn.is_some() && listener_op.is_some() {
|
2021-04-16 11:27:48 +00:00
|
|
|
if vibe {
|
|
|
|
|
llio.vibe(llio::VibePattern::Short).unwrap();
|
|
|
|
|
}
|
2021-09-17 18:22:35 +00:00
|
|
|
for kv in kc.chunks(4) {
|
|
|
|
|
let mut keys: [char; 4] = ['\u{0000}', '\u{0000}', '\u{0000}', '\u{0000}'];
|
|
|
|
|
for i in 0..kv.len() {
|
|
|
|
|
keys[i] = kv[i];
|
|
|
|
|
}
|
|
|
|
|
log::trace!("sending keys {:?}", keys);
|
|
|
|
|
xous::send_message(listener_conn.unwrap(),
|
|
|
|
|
xous::Message::new_scalar(
|
|
|
|
|
listener_op.unwrap(),
|
|
|
|
|
keys[0] as u32 as usize,
|
|
|
|
|
keys[1] as u32 as usize,
|
|
|
|
|
keys[2] as u32 as usize,
|
|
|
|
|
keys[3] as u32 as usize,
|
|
|
|
|
)
|
|
|
|
|
).expect("couldn't send key codes to listener");
|
2021-04-03 17:42:30 +00:00
|
|
|
}
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2021-08-22 10:52:33 +00:00
|
|
|
// as long as we have a keydown, keep pinging the loop at a high rate. this consumes more power, but keydowns are relatively rare.
|
|
|
|
|
if kbd.is_repeating_key() {
|
2021-08-23 17:48:30 +00:00
|
|
|
log::trace!("keydowns hold");
|
2021-08-22 10:52:33 +00:00
|
|
|
// fire a second call to check if we should transition to a repeating state
|
|
|
|
|
ticktimer.sleep_ms(kbd.get_repeat_check_interval() as _).unwrap();
|
2021-08-22 21:12:21 +00:00
|
|
|
kbd.poll();
|
2021-08-22 10:52:33 +00:00
|
|
|
xous::send_message(self_cid,
|
|
|
|
|
xous::Message::new_scalar(Opcode::HandlerTrigger.to_usize().unwrap(), 0, 0, 0, 0)
|
|
|
|
|
).unwrap();
|
|
|
|
|
}
|
2021-01-10 22:11:15 +00:00
|
|
|
},
|
2021-04-05 15:50:00 +00:00
|
|
|
None => {log::error!("couldn't convert opcode"); break}
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
2021-03-01 07:08:08 +00:00
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
log::trace!("main loop exit, destroying servers");
|
|
|
|
|
xns.unregister_server(kbd_sid).unwrap();
|
|
|
|
|
xous::destroy_server(kbd_sid).unwrap();
|
|
|
|
|
log::trace!("quitting");
|
2021-05-20 03:01:00 +00:00
|
|
|
xous::terminate_process(0)
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
THE BALDEST YAK: keyboard injection now working
I wrote in a commit yesterday how there was no ROI on doing this
yet somehow I kept on doing it. I blame poor impulse control.
Turns out there was a series of race conditions and bugs that
caused this thing to "mostly work" on the first go at it but
really hard to debug and get very right.
1. Litex UART has a weird undocumented API. You advance the Rx
FIFO by writing `1` to the PENDING bit, and no other way. However,
you determine if there are any more characters to receive by
reading the EMPTY bit and seeing if it is 1. The previous method
wrote `1` to the PENDING bit and then used the PENDING bit to
see if a character was available, under the theory that the
PENDING bit would be set again if there was still a character.
This is somewhat true, except that the PENDING bit is set only
some amount of time later which is about comparable to a few
CPU instructions, so if you had a fast loop path the PENDING
bit would show as cleared and you'd exit, which would cause
no further interrupts to fire because the interrupt was
effecitvely not acknowledged.
This isn't written up anywhere but I figured this out by
just reading the source code for Litex UART and BIOS:
https://github.com/enjoy-digital/litex/blob/e8be3915041707860e70b15d062661fc1f67a09f/litex/soc/cores/uart.py#L264-L279
https://github.com/enjoy-digital/litex/blob/ccd3ab17be1f8770c59ec1c62e0285d3bac3cc26/litex/soc/software/libbase/uart.c#L38-L44
2. I had the Key Injector IP block in the `sys` domain, which
would have its clock cut when the CPU went to sleep, which would
cause input characters to disappear when the CPU went into power
saving. moving this to "always on" domain fixes that.
3. I had the UART PHY in the always on block (so you could
send or receive exactly one character), but not the
separate UART block (which contained the FIFOs).
The UART block would also go into power
savings, and this would cause FIFO entries to go missing.
4. Escape sequences get sent as a burst of characters. The Key Injector
originally did not have a FIFO of its own, so it would only
remember the *last* key of the sequence that was written, causing
some keys to be dropped occassionally. Adding a 16-deep FIFO
to the injector fixes this.
5. The code I had to read out the injector's results was designed with
the "strobe" bit (to indicate that the FIFO had characters) in the
same regsiter as the character itself. Thus the loop that 1. checked
if the fifo was empty then 2. read and forwarded the character
would actually perform two reads from the FIFO. However, this "mostly worked"
I think because the FIFO retains the last value that was written
even when it was empty, so if only one character was read at a time
everything was fine. It only didn't work when you stuffed two characters
in there, as you'd drop the first one. Reading the FIFO once and
then interpreting the code with a bitmask fixes this.
6. The escape sequences, in their full glory with no dropped characters,
required a refactored parsing routine, which I have now added.
7. backspace->delete mappings, the bane of computer engineers since 1960.
OK. very bald yak. Here I lay down the shears next to several
mooshi pillows stuffed with the finest Yak hair.
2022-01-28 07:17:47 +00:00
|
|
|
|
|
|
|
|
fn esc_match(esc_chars: &[u8]) -> Result<Option<char>, ()> {
|
|
|
|
|
let mut extended = Vec::<u8>::new();
|
|
|
|
|
for (i, &c) in esc_chars.iter().enumerate() {
|
|
|
|
|
match i {
|
|
|
|
|
0 => if c != 0x1b { return Err(()) },
|
|
|
|
|
1 => if c != 0x5b { return Err(()) },
|
|
|
|
|
2 => match c {
|
|
|
|
|
0x41 => return Ok(Some('↑')),
|
|
|
|
|
0x42 => return Ok(Some('↓')),
|
|
|
|
|
0x43 => return Ok(Some('→')),
|
|
|
|
|
0x44 => return Ok(Some('←')),
|
|
|
|
|
0x7e => return Err(()), // premature end
|
|
|
|
|
_ => extended.push(c)
|
|
|
|
|
}
|
|
|
|
|
_ => if c == 0x7e {
|
|
|
|
|
if extended.len() == 1 {
|
|
|
|
|
if extended[0] == 0x31 {
|
|
|
|
|
return Ok(Some('∴'))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return Err(()) // code unrecognized
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
extended.push(c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|