96 lines
3.0 KiB
Rust
96 lines
3.0 KiB
Rust
//! Capri key names and their X keysyms (also used by the KDE portal).
|
|
use crate::error::{fail, Fail};
|
|
|
|
/// Names the model may use (`ESCAPE` is reserved for the emergency stop).
|
|
pub fn agent_key(name: &str) -> bool {
|
|
name != "ESCAPE" && keysym(name).is_ok()
|
|
}
|
|
|
|
pub fn keysym(name: &str) -> Result<u32, Fail> {
|
|
let n = name.to_ascii_uppercase();
|
|
let b = n.as_bytes();
|
|
if b.len() == 1 && b[0].is_ascii_uppercase() {
|
|
return Ok(b[0].to_ascii_lowercase() as u32);
|
|
}
|
|
if b.len() == 1 && b[0].is_ascii_digit() {
|
|
return Ok(b[0] as u32);
|
|
}
|
|
if let Some(f) = n.strip_prefix('F').and_then(|f| f.parse::<u32>().ok()).filter(|f| (1..=24).contains(f) && !n[1..].starts_with('0')) {
|
|
return Ok(0xFFBE + f - 1);
|
|
}
|
|
Ok(match n.as_str() {
|
|
"CTRL" => 0xFFE3,
|
|
"SHIFT" => 0xFFE1,
|
|
"ALT" => 0xFFE9,
|
|
"WIN" => 0xFFEB,
|
|
"ENTER" => 0xFF0D,
|
|
"TAB" => 0xFF09,
|
|
"SPACE" => 0x20,
|
|
"BACKSPACE" => 0xFF08,
|
|
"DELETE" => 0xFFFF,
|
|
"INSERT" => 0xFF63,
|
|
"HOME" => 0xFF50,
|
|
"END" => 0xFF57,
|
|
"PAGEUP" => 0xFF55,
|
|
"PAGEDOWN" => 0xFF56,
|
|
"LEFT" => 0xFF51,
|
|
"UP" => 0xFF52,
|
|
"RIGHT" => 0xFF53,
|
|
"DOWN" => 0xFF54,
|
|
"ESCAPE" => 0xFF1B,
|
|
_ => return Err(fail("Unsupported keyboard key.")),
|
|
})
|
|
}
|
|
|
|
pub const ESCAPE: u32 = 0xFF1B;
|
|
|
|
/// Keysym typed for one character: LF → Return, TAB → Tab, Latin-1 direct, else Unicode keysym.
|
|
pub fn char_keysym(c: char) -> u32 {
|
|
match c {
|
|
'\n' => 0xFF0D,
|
|
'\t' => 0xFF09,
|
|
c if (' '..='~').contains(&c) || ('\u{a0}'..='\u{ff}').contains(&c) => c as u32,
|
|
c => 0x0100_0000 | c as u32,
|
|
}
|
|
}
|
|
|
|
/// 1..=4 unique key names, upper-cased.
|
|
pub fn key_list(keys: &[String]) -> Result<Vec<String>, Fail> {
|
|
let mut out: Vec<String> = Vec::new();
|
|
for k in keys {
|
|
let k = k.to_ascii_uppercase();
|
|
if !agent_key(&k) {
|
|
return Err(fail("Unsupported keyboard key."));
|
|
}
|
|
if !out.contains(&k) {
|
|
out.push(k);
|
|
}
|
|
}
|
|
if out.is_empty() || out.len() > 4 {
|
|
return Err(fail("Use at most four keys in one chord."));
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn mapping() {
|
|
assert_eq!(keysym("a").unwrap(), 0x61);
|
|
assert_eq!(keysym("7").unwrap(), 0x37);
|
|
assert_eq!(keysym("F1").unwrap(), 0xFFBE);
|
|
assert_eq!(keysym("F24").unwrap(), 0xFFBE + 23);
|
|
assert!(keysym("F25").is_err() && keysym("F01").is_err() && keysym("META").is_err());
|
|
assert_eq!(keysym("PAGEDOWN").unwrap(), 0xFF56);
|
|
assert!(!agent_key("ESCAPE") && agent_key("ENTER"));
|
|
assert_eq!(char_keysym('\n'), 0xFF0D);
|
|
assert_eq!(char_keysym('é'), 0xE9);
|
|
assert_eq!(char_keysym('я'), 0x0100_044F);
|
|
assert_eq!(key_list(&["ctrl".into(), "L".into(), "l".into()]).unwrap(), vec!["CTRL", "L"]);
|
|
assert!(key_list(&["ESCAPE".into()]).is_err());
|
|
assert!(key_list(&["A", "B", "C", "D", "E"].map(String::from)).is_err());
|
|
}
|
|
}
|