claim and release keyboard to read or Vol/Usb fails

This commit is contained in:
Luke
2020-04-19 09:20:12 +12:00
parent 11206e2209
commit cfddb6077a
2 changed files with 12 additions and 3 deletions

View File

@@ -95,7 +95,7 @@ impl Daemon {
}); });
// READ KEYBOARD // READ KEYBOARD
// TODO: this needs to move to a thread, but there is unsafety // TODO: this needs to move to a thread, but there is unsafety
let borrowed_daemon = daemon.borrow(); let mut borrowed_daemon = daemon.borrow_mut();
match borrowed_daemon.rogcore.poll_keyboard(&mut key_buf) { match borrowed_daemon.rogcore.poll_keyboard(&mut key_buf) {
Ok(read) => { Ok(read) => {
// Doing this because the Laptop trait takes RogCore, but RogCore contains laptop // Doing this because the Laptop trait takes RogCore, but RogCore contains laptop

View File

@@ -32,6 +32,7 @@ pub struct RogCore {
handle: DeviceHandle<rusb::GlobalContext>, handle: DeviceHandle<rusb::GlobalContext>,
initialised: bool, initialised: bool,
led_interface_num: u8, led_interface_num: u8,
keys_interface_num: u8,
config: Config, config: Config,
laptop: RefCell<Box<dyn Laptop>>, laptop: RefCell<Box<dyn Laptop>>,
} }
@@ -46,10 +47,13 @@ impl RogCore {
let dev_config = dev_handle.device().config_descriptor(0).unwrap(); let dev_config = dev_handle.device().config_descriptor(0).unwrap();
// Interface with outputs // Interface with outputs
let mut led_interface_num = 0; let mut led_interface_num = 0;
let mut keys_interface_num = 0;
for iface in dev_config.interfaces() { for iface in dev_config.interfaces() {
for desc in iface.descriptors() { for desc in iface.descriptors() {
for endpoint in desc.endpoint_descriptors() { for endpoint in desc.endpoint_descriptors() {
if endpoint.address() == laptop.led_iface_num() { if endpoint.address() == 0x83 {
keys_interface_num = desc.interface_number();
} else if endpoint.address() == laptop.led_iface_num() {
led_interface_num = desc.interface_number(); led_interface_num = desc.interface_number();
break; break;
} }
@@ -63,6 +67,7 @@ impl RogCore {
handle: dev_handle, handle: dev_handle,
initialised: false, initialised: false,
led_interface_num, led_interface_num,
keys_interface_num,
config: Config::default().read(), config: Config::default().read(),
laptop: RefCell::new(laptop), laptop: RefCell::new(laptop),
}) })
@@ -143,20 +148,24 @@ impl RogCore {
Err(AuraError::NotSupported) Err(AuraError::NotSupported)
} }
pub fn poll_keyboard(&self, buf: &mut [u8; 32]) -> Result<Option<usize>, AuraError> { pub fn poll_keyboard(&mut self, buf: &mut [u8; 32]) -> Result<Option<usize>, AuraError> {
self.handle.claim_interface(self.keys_interface_num)?;
match self match self
.handle .handle
.read_interrupt(0x83, buf, Duration::from_micros(10)) .read_interrupt(0x83, buf, Duration::from_micros(10))
{ {
Ok(o) => { Ok(o) => {
if buf[0] == self.laptop.borrow().hotkey_group_byte() { if buf[0] == self.laptop.borrow().hotkey_group_byte() {
self.handle.release_interface(self.keys_interface_num)?;
return Ok(Some(o)); return Ok(Some(o));
} }
} }
Err(err) => { Err(err) => {
self.handle.release_interface(self.keys_interface_num)?;
return Err(AuraError::from(err)); return Err(AuraError::from(err));
} }
} }
self.handle.release_interface(self.keys_interface_num)?;
Ok(None) Ok(None)
} }