2021-01-10 22:11:15 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
2021-06-16 17:12:25 +00:00
|
|
|
use num_traits::*;
|
2021-02-10 07:13:25 +00:00
|
|
|
|
2021-01-10 22:11:15 +00:00
|
|
|
pub mod api;
|
|
|
|
|
|
2021-09-15 09:59:13 +00:00
|
|
|
pub use api::*;
|
2021-06-16 17:12:25 +00:00
|
|
|
use xous::{send_message, Message};
|
|
|
|
|
use xous_ipc::{Buffer, String};
|
2021-04-03 17:42:30 +00:00
|
|
|
|
2021-04-16 11:27:48 +00:00
|
|
|
#[derive(Debug)]
|
2021-04-03 17:42:30 +00:00
|
|
|
pub struct Keyboard {
|
|
|
|
|
conn: xous::CID,
|
|
|
|
|
}
|
|
|
|
|
impl Keyboard {
|
2021-04-03 18:15:46 +00:00
|
|
|
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
2022-01-31 15:13:10 +00:00
|
|
|
REFCOUNT.fetch_add(1, Ordering::Relaxed);
|
2021-04-03 17:42:30 +00:00
|
|
|
let conn = xns.request_connection_blocking(api::SERVER_NAME_KBD).expect("Can't connect to KBD");
|
|
|
|
|
Ok(Keyboard {
|
|
|
|
|
conn,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 09:13:36 +00:00
|
|
|
pub fn register_listener(&self, server_name: &str, action_opcode: usize) {
|
|
|
|
|
let kr = KeyboardRegistration {
|
2021-06-16 17:12:25 +00:00
|
|
|
server_name: String::<64>::from_str(server_name),
|
2021-08-22 09:13:36 +00:00
|
|
|
listener_op_id: action_opcode
|
2021-06-16 17:12:25 +00:00
|
|
|
};
|
2021-08-22 09:13:36 +00:00
|
|
|
let buf = Buffer::into_buf(kr).unwrap();
|
|
|
|
|
buf.lend(self.conn, Opcode::RegisterListener.to_u32().unwrap())
|
|
|
|
|
.expect("couldn't register listener");
|
2021-09-15 09:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn register_raw_listener(&self, server_name: &str, action_opcode: usize) {
|
|
|
|
|
let kr = KeyboardRegistration {
|
|
|
|
|
server_name: String::<64>::from_str(server_name),
|
|
|
|
|
listener_op_id: action_opcode
|
|
|
|
|
};
|
|
|
|
|
let buf = Buffer::into_buf(kr).unwrap();
|
|
|
|
|
buf.lend(self.conn, Opcode::RegisterRawListener.to_u32().unwrap())
|
|
|
|
|
.expect("couldn't register listener");
|
2021-04-03 17:42:30 +00:00
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
|
2021-04-16 11:27:48 +00:00
|
|
|
pub fn set_vibe(&self, enable: bool) -> Result<(), xous::Error> {
|
|
|
|
|
let ena =
|
|
|
|
|
if enable { 1 }
|
|
|
|
|
else { 0 };
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::Vibe.to_usize().unwrap(),
|
|
|
|
|
ena, 0, 0, 0,)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 18:46:37 +00:00
|
|
|
pub fn set_keymap(&self, map: KeyMap) -> Result<(), xous::Error> {
|
|
|
|
|
send_message(self.conn,
|
|
|
|
|
Message::new_scalar(Opcode::SelectKeyMap.to_usize().unwrap(),
|
|
|
|
|
map.into(),
|
|
|
|
|
0, 0, 0)
|
|
|
|
|
).map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
pub fn get_keymap(&self) -> Result<KeyMap, xous::Error> {
|
|
|
|
|
match send_message(self.conn,
|
|
|
|
|
Message::new_blocking_scalar(Opcode::GetKeyMap.to_usize().unwrap(),
|
|
|
|
|
0, 0, 0, 0)
|
|
|
|
|
) {
|
|
|
|
|
Ok(xous::Result::Scalar1(code)) => {
|
|
|
|
|
Ok(code.into())
|
|
|
|
|
}
|
|
|
|
|
_ => Err(xous::Error::InternalError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
2021-04-05 15:50:00 +00:00
|
|
|
pub fn hostmode_inject_key(&self, c: char) {
|
|
|
|
|
send_message(self.conn,
|
2021-04-15 20:11:25 +00:00
|
|
|
Message::new_scalar(Opcode::InjectKey.to_usize().unwrap(),
|
2021-04-05 15:50:00 +00:00
|
|
|
c as u32 as usize, 0, 0, 0
|
|
|
|
|
)).unwrap();
|
|
|
|
|
}
|
2021-01-10 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-21 08:24:30 +00:00
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
|
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
2021-04-03 17:42:30 +00:00
|
|
|
impl Drop for Keyboard {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection.
|
2022-01-31 15:13:10 +00:00
|
|
|
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
|
2021-05-21 08:24:30 +00:00
|
|
|
unsafe{xous::disconnect(self.conn).unwrap();}
|
|
|
|
|
}
|
2021-04-03 17:42:30 +00:00
|
|
|
}
|
|
|
|
|
}
|