keyboard: remove internal xous_names API

This API is now private.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-02-10 15:13:25 +08:00
parent 704c0f47bf
commit 7d40c749f0
3 changed files with 71 additions and 38 deletions

View file

@ -1,9 +1,9 @@
use xous::{Message, ScalarMessage};
use heapless::Vec;
use heapless::consts::*; use heapless::consts::*;
use heapless::Vec;
use xous::{Message, ScalarMessage};
pub const SUBTYPE_REGISTER_BASIC_LISTENER: u16 = 0; pub const REGISTER_BASIC_LISTENER: u32 = 0x1000_0000;
pub const SUBTYPE_REGISTER_RAW_LISTENER: u16 = 1; pub const REGISTER_RAW_LISTENER: u32 = 0x1000_0001;
#[derive(Debug, Default, Copy, Clone)] #[derive(Debug, Default, Copy, Clone)]
pub struct ScanCode { pub struct ScanCode {
@ -31,7 +31,9 @@ pub struct KeyRawStates {
pub keyups: Vec<RowCol, U16>, pub keyups: Vec<RowCol, U16>,
} }
impl KeyRawStates { impl KeyRawStates {
pub fn mid(&self) -> usize { self.mid as usize } pub fn mid(&self) -> usize {
self.mid as usize
}
pub fn new() -> Self { pub fn new() -> Self {
KeyRawStates { KeyRawStates {
@ -121,10 +123,10 @@ pub enum Opcode {
SelectKeyMap(KeyMap), SelectKeyMap(KeyMap),
/// request interpreted ScanCodes to be sent /// request interpreted ScanCodes to be sent
RegisterListener(xous_names::api::Registration), RegisterListener(xous_names::api::XousServerName),
/// request raw keyup/keydown events to be sent /// request raw keyup/keydown events to be sent
RegisterRawListener(xous_names::api::Registration), RegisterRawListener(xous_names::api::XousServerName),
/// set repeat delay, rate; both in ms /// set repeat delay, rate; both in ms
SetRepeat(usize, usize), SetRepeat(usize, usize),
@ -139,37 +141,53 @@ pub enum Opcode {
HostModeInjectKey(char), HostModeInjectKey(char),
} }
impl core::convert::TryFrom<& Message> for Opcode { impl core::convert::TryFrom<&Message> for Opcode {
type Error = &'static str; type Error = &'static str;
fn try_from(message: & Message) -> Result<Self, Self::Error> { fn try_from(message: &Message) -> Result<Self, Self::Error> {
match message { match message {
Message::Scalar(m) => match m.id { Message::Scalar(m) => match m.id {
0 => Ok(Opcode::SelectKeyMap(KeyMap::from(m.arg1))), 0 => Ok(Opcode::SelectKeyMap(KeyMap::from(m.arg1))),
1 => Ok(Opcode::SetRepeat(m.arg1, m.arg2)), 1 => Ok(Opcode::SetRepeat(m.arg1, m.arg2)),
2 => Ok(Opcode::SetChordInterval(m.arg1)), 2 => Ok(Opcode::SetChordInterval(m.arg1)),
xous::names::GID_KEYBOARD_KEYSTATE_EVENT => xous::names::GID_KEYBOARD_KEYSTATE_EVENT => Ok(Opcode::KeyboardEvent([
Ok(Opcode::KeyboardEvent([ if let Some(a) = core::char::from_u32(m.arg1 as u32) {
if let Some(a) = core::char::from_u32(m.arg1 as u32) { a } else { '\u{0000}' }, a
if let Some(a) = core::char::from_u32(m.arg2 as u32) { a } else { '\u{0000}' }, } else {
if let Some(a) = core::char::from_u32(m.arg3 as u32) { a } else { '\u{0000}' }, '\u{0000}'
if let Some(a) = core::char::from_u32(m.arg4 as u32) { a } else { '\u{0000}' } },
])), if let Some(a) = core::char::from_u32(m.arg2 as u32) {
a
} else {
'\u{0000}'
},
if let Some(a) = core::char::from_u32(m.arg3 as u32) {
a
} else {
'\u{0000}'
},
if let Some(a) = core::char::from_u32(m.arg4 as u32) {
a
} else {
'\u{0000}'
},
])),
3 => Ok(Opcode::HostModeInjectKey( 3 => Ok(Opcode::HostModeInjectKey(
if let Some(a) = core::char::from_u32(m.arg1 as u32) {a} else { '\u{0000}'} )), if let Some(a) = core::char::from_u32(m.arg1 as u32) {
a
} else {
'\u{0000}'
},
)),
_ => Err("KBD api: unknown Scalar ID"), _ => Err("KBD api: unknown Scalar ID"),
}, },
Message::Borrow(m) => { Message::Borrow(m) => match m.id as u32 {
if xous_names::api::Registration::match_subtype(m.id, SUBTYPE_REGISTER_BASIC_LISTENER) { REGISTER_BASIC_LISTENER => Ok(Opcode::RegisterListener({
Ok(Opcode::RegisterListener({ unsafe { *((m.buf.as_mut_ptr()) as *mut xous_names::api::XousServerName) }
unsafe { *( (m.buf.as_mut_ptr()) as *mut xous_names::api::Registration) } })),
})) REGISTER_RAW_LISTENER => Ok(Opcode::RegisterRawListener({
} else if xous_names::api::Registration::match_subtype(m.id, SUBTYPE_REGISTER_RAW_LISTENER) { unsafe { *((m.buf.as_mut_ptr()) as *mut xous_names::api::XousServerName) }
Ok(Opcode::RegisterRawListener({ })),
unsafe { *( (m.buf.as_mut_ptr()) as *mut xous_names::api::Registration) } _ => Err("KBD api: unknown Borrow ID"),
}))
} else {
Err("KBD api: unknown Borrow ID")
}
}, },
_ => Err("KBD api: unhandled message type"), _ => Err("KBD api: unhandled message type"),
} }
@ -190,12 +208,15 @@ impl Into<Message> for Opcode {
id: 1, id: 1,
arg1: delay, arg1: delay,
arg2: rate, arg2: rate,
arg3: 0, arg4: 0, arg3: 0,
arg4: 0,
}), }),
Opcode::SetChordInterval(period) => Message::Scalar(ScalarMessage { Opcode::SetChordInterval(period) => Message::Scalar(ScalarMessage {
id: 2, id: 2,
arg1: period, arg1: period,
arg2: 0, arg3: 0, arg4: 0, arg2: 0,
arg3: 0,
arg4: 0,
}), }),
Opcode::KeyboardEvent(keys) => Message::Scalar(ScalarMessage { Opcode::KeyboardEvent(keys) => Message::Scalar(ScalarMessage {
id: xous::names::GID_KEYBOARD_KEYSTATE_EVENT, id: xous::names::GID_KEYBOARD_KEYSTATE_EVENT,
@ -207,7 +228,9 @@ impl Into<Message> for Opcode {
Opcode::HostModeInjectKey(key) => Message::Scalar(ScalarMessage { Opcode::HostModeInjectKey(key) => Message::Scalar(ScalarMessage {
id: 3, id: 3,
arg1: key as u32 as usize, arg1: key as u32 as usize,
arg2: 0, arg3: 0, arg4: 0, arg2: 0,
arg3: 0,
arg4: 0,
}), }),
_ => panic!("KBD api: Opcode type not handled by Into(), refer to helper method"), _ => panic!("KBD api: Opcode type not handled by Into(), refer to helper method"),
} }
@ -237,4 +260,4 @@ impl DecodedOpcode {
} }
} }
} }
*/ */

View file

@ -1,11 +1,21 @@
#![cfg_attr(target_os = "none", no_std)] #![cfg_attr(target_os = "none", no_std)]
use core::convert::TryInto;
use api::{REGISTER_BASIC_LISTENER, REGISTER_RAW_LISTENER};
pub mod api; pub mod api;
fn request(name: &str, kbd_conn: xous::CID, id: u32) -> Result<xous::Result, xous::Error> {
let s: xous_names::api::XousServerName = name.try_into()?;
let sendable = xous::ipc::Sendable::new(s)?;
sendable.lend(kbd_conn, id)
}
pub fn request_events(name: &str, kbd_conn: xous::CID) -> Result<xous::Result, xous::Error> { pub fn request_events(name: &str, kbd_conn: xous::CID) -> Result<xous::Result, xous::Error> {
xous_names::request_core(name, kbd_conn, api::SUBTYPE_REGISTER_BASIC_LISTENER) request(name, kbd_conn, api::REGISTER_BASIC_LISTENER)
} }
pub fn request_raw_events(name: &str, kbd_conn: xous::CID) -> Result<xous::Result, xous::Error> { pub fn request_raw_events(name: &str, kbd_conn: xous::CID) -> Result<xous::Result, xous::Error> {
xous_names::request_core(name, kbd_conn, api::SUBTYPE_REGISTER_RAW_LISTENER) request(name, kbd_conn, api::REGISTER_RAW_LISTENER)
} }

View file

@ -819,11 +819,11 @@ fn xmain() -> ! {
kbd.set_map(map); kbd.set_map(map);
}, },
Opcode::RegisterListener(registration) => { Opcode::RegisterListener(registration) => {
let cid = xous_names::request_connection_blocking(registration.name.to_str()).expect("KBD: can't connect to requested listener for reporting events"); let cid = xous_names::request_connection_blocking(registration.to_str()).expect("KBD: can't connect to requested listener for reporting events");
normal_conns.push(cid).expect("KBD: probably ran out of slots for keyboard event reporting"); normal_conns.push(cid).expect("KBD: probably ran out of slots for keyboard event reporting");
}, },
Opcode::RegisterRawListener(registration) => { Opcode::RegisterRawListener(registration) => {
let cid = xous_names::request_connection_blocking(registration.name.to_str()).expect("KBD: can't connect to requested listener for reporting events"); let cid = xous_names::request_connection_blocking(registration.to_str()).expect("KBD: can't connect to requested listener for reporting events");
raw_conns.push(cid).expect("KBD: probably ran out of slots for raw keyboard event reporting"); raw_conns.push(cid).expect("KBD: probably ran out of slots for raw keyboard event reporting");
}, },
Opcode::SetRepeat(rate, delay) => { Opcode::SetRepeat(rate, delay) => {
@ -876,7 +876,7 @@ fn xmain() -> ! {
} }
} }
let sendable_rs = Sendable::new(rs).expect("KBD: can't create sendable raw codes structure"); let sendable_rs = Sendable::new(rs).expect("KBD: can't create sendable raw codes structure");
sendable_rs.send(*conn, KeyRawStates::new().mid()) // tortured syntax to stick with the abstraction that message ID comes from the struct sendable_rs.send(*conn, KeyRawStates::new().mid() as u32) // tortured syntax to stick with the abstraction that message ID comes from the struct
.expect("KBD: can't send raw code"); .expect("KBD: can't send raw code");
} }
} }