Shift init actions up a few calls to prevent over-eager init

This commit is contained in:
Luke D. Jones
2024-03-23 23:27:26 +13:00
parent 4b34ab83fb
commit 637360095c
7 changed files with 43 additions and 51 deletions

View File

@@ -68,7 +68,7 @@ impl CtrlAnime {
let node = if usb.is_some() { let node = if usb.is_some() {
unsafe { Node::Usb(usb.unwrap_unchecked()) } unsafe { Node::Usb(usb.unwrap_unchecked()) }
} else if hid.is_some() { } else if hid.is_some() {
unsafe { Node::Hid(hid.unwrap_unchecked().0) } unsafe { Node::Hid(hid.unwrap_unchecked()) }
} else { } else {
return Err(RogError::Anime(AnimeError::NoDevice)); return Err(RogError::Anime(AnimeError::NoDevice));
}; };

View File

@@ -366,7 +366,7 @@ mod tests {
}; };
let mut controller = CtrlKbdLed { let mut controller = CtrlKbdLed {
led_prod: AuraDevice::X19b6, led_prod: AuraDevice::X19b6,
led_node: LEDNode::Rog(KeyboardLed::default(), HidRaw::new("id_product").unwrap().0), led_node: LEDNode::Rog(KeyboardLed::default(), HidRaw::new("id_product").unwrap()),
supported_data: supported_basic_modes, supported_data: supported_basic_modes,
per_key_mode_active: false, per_key_mode_active: false,
config, config,
@@ -404,7 +404,7 @@ mod tests {
}; };
let mut controller = CtrlKbdLed { let mut controller = CtrlKbdLed {
led_prod: AuraDevice::X19b6, led_prod: AuraDevice::X19b6,
led_node: LEDNode::Rog(KeyboardLed::default(), HidRaw::new("id_product").unwrap().0), led_node: LEDNode::Rog(KeyboardLed::default(), HidRaw::new("id_product").unwrap()),
supported_data: supported_basic_modes, supported_data: supported_basic_modes,
per_key_mode_active: false, per_key_mode_active: false,
config, config,

View File

@@ -85,13 +85,13 @@ impl AuraManager {
}; };
if action == "remove" { if action == "remove" {
dbg!("REMOVING");
if let Some(_) = parent.attribute_value("idProduct") { if let Some(_) = parent.attribute_value("idProduct") {
info!("AuraManager removing: {path:?}"); info!("AuraManager removing: {path:?}");
let conn_copy = conn_copy.clone(); let conn_copy = conn_copy.clone();
let interfaces_copy = interfaces.clone(); let interfaces_copy = interfaces.clone();
tokio::spawn(async move { tokio::spawn(async move {
let mut interfaces = interfaces_copy.lock().await; let mut interfaces = interfaces_copy.lock().await; // hold until completed
interfaces.remove(&path);
let res = conn_copy let res = conn_copy
.object_server() .object_server()
.remove::<CtrlAuraZbus, _>(&path) .remove::<CtrlAuraZbus, _>(&path)
@@ -101,13 +101,11 @@ impl AuraManager {
e e
})?; })?;
info!("AuraManager removed: {path:?}, {res}"); info!("AuraManager removed: {path:?}, {res}");
interfaces.remove(&path);
debug!("Removed {path:?}"); debug!("Removed {path:?}");
Ok::<(), RogError>(()) Ok::<(), RogError>(())
}); });
} }
} else if action == "add" { } else if action == "add" {
dbg!("ADDING");
let id_product = let id_product =
if let Some(id_product) = parent.attribute_value("idProduct") { if let Some(id_product) = parent.attribute_value("idProduct") {
id_product.to_string_lossy().to_string() id_product.to_string_lossy().to_string()
@@ -135,7 +133,7 @@ impl AuraManager {
}; };
let dev_node = if let Some(dev_node) = event.devnode() { let dev_node = if let Some(dev_node) = event.devnode() {
dev_node dev_node.to_owned()
} else { } else {
continue; continue;
}; };
@@ -145,21 +143,23 @@ impl AuraManager {
{ {
// bah... shitty clone TODO: fix // bah... shitty clone TODO: fix
let data_clone = data.clone(); let data_clone = data.clone();
if let Ok(mut ctrl) = let mut conn_copy = conn_copy.clone();
CtrlKbdLed::from_hidraw(raw, path.clone(), &data) let interfaces_copy = interfaces.clone();
{ //
info!( tokio::spawn(async move {
"AuraManager found device at: {dev_node:?}, {path:?}" let mut interfaces = interfaces_copy.lock().await;
); if interfaces.contains(&path) {
let mut conn_copy = conn_copy.clone(); debug!("Already a ctrl at {path:?}");
let interfaces_copy = interfaces.clone(); return Ok(());
// }
tokio::spawn(async move { if let Ok(mut ctrl) =
let mut interfaces = interfaces_copy.lock().await; CtrlKbdLed::from_hidraw(raw, path.clone(), &data_clone)
if interfaces.contains(&path) { {
debug!("Already a ctrl at {path:?}"); info!(
return Ok(()); "AuraManager found device at: {dev_node:?}, \
} {path:?}"
);
debug!("Starting Aura at {path}"); debug!("Starting Aura at {path}");
interfaces.insert(path.clone()); interfaces.insert(path.clone());
let sig_ctx = CtrlAuraZbus::signal_context(&conn_copy)?; let sig_ctx = CtrlAuraZbus::signal_context(&conn_copy)?;
@@ -170,10 +170,10 @@ impl AuraManager {
let sig_ctx = CtrlAuraZbus::signal_context(&conn_copy)?; let sig_ctx = CtrlAuraZbus::signal_context(&conn_copy)?;
start_tasks(zbus, &mut conn_copy, sig_ctx, &path) start_tasks(zbus, &mut conn_copy, sig_ctx, &path)
.await?; .await?;
Ok::<(), RogError>(()) }
}); // Can't get result from here due to Ok::<(), RogError>(())
// MonitorSocket }); // Can't get result from here due to
} // MonitorSocket
} }
} else { } else {
warn!("idProduct:{id_product:?} is unknown, not using") warn!("idProduct:{id_product:?} is unknown, not using")

View File

@@ -24,7 +24,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
return Err("RogError::NoAuraKeyboard".into()); return Err("RogError::NoAuraKeyboard".into());
} }
let node = usb_node.unwrap().0; let node = usb_node.unwrap();
let mut packet: [u8; 64] = [ let mut packet: [u8; 64] = [
0x5a, 0xd1, 0x0d, 0x0e, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5a, 0xd1, 0x0d, 0x0e, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

View File

@@ -26,7 +26,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
return Err("RogError::NoAuraKeyboard".into()); return Err("RogError::NoAuraKeyboard".into());
} }
let node = usb_node.unwrap().0; let node = usb_node.unwrap();
// node.write_bytes(&[0x5a, 0xd1, 0x0a, 0x01])?; // TODO: need to CHECK // node.write_bytes(&[0x5a, 0xd1, 0x0a, 0x01])?; // TODO: need to CHECK
println!("Set mouse mode for 10 seconds"); println!("Set mouse mode for 10 seconds");

View File

@@ -24,7 +24,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
return Err("RogError::NoAuraKeyboard".into()); return Err("RogError::NoAuraKeyboard".into());
} }
let node = usb_node.unwrap().0; let node = usb_node.unwrap();
// node.write_bytes(&[0x5a, 0xd1, 0x0a, 0x01])?; // TODO: need to CHECK // node.write_bytes(&[0x5a, 0xd1, 0x0a, 0x01])?; // TODO: need to CHECK
println!("Set mouse mode for 10 seconds"); println!("Set mouse mode for 10 seconds");

View File

@@ -22,7 +22,7 @@ pub struct HidRaw {
} }
impl HidRaw { impl HidRaw {
pub fn new(id_product: &str) -> Result<(Self, Device)> { pub fn new(id_product: &str) -> Result<Self> {
let mut enumerator = udev::Enumerator::new().map_err(|err| { let mut enumerator = udev::Enumerator::new().map_err(|err| {
warn!("{}", err); warn!("{}", err);
PlatformError::Udev("enumerator failed".into(), err) PlatformError::Udev("enumerator failed".into(), err)
@@ -47,17 +47,12 @@ impl HidRaw {
if parent_id == id_product { if parent_id == id_product {
if let Some(dev_node) = endpoint.devnode() { if let Some(dev_node) = endpoint.devnode() {
info!("Using device at: {:?} for hidraw control", dev_node); info!("Using device at: {:?} for hidraw control", dev_node);
return Ok(( return Ok(Self {
Self { file: RefCell::new(OpenOptions::new().write(true).open(dev_node)?),
file: RefCell::new( devfs_path: dev_node.to_owned(),
OpenOptions::new().write(true).open(dev_node)?, prod_id: id_product.to_string(),
), syspath: endpoint.syspath().into(),
devfs_path: dev_node.to_owned(), });
prod_id: id_product.to_string(),
syspath: endpoint.syspath().into(),
},
usb_device,
));
} }
} }
} }
@@ -70,15 +65,12 @@ impl HidRaw {
"Using device at: {:?} for <TODO: label control> control", "Using device at: {:?} for <TODO: label control> control",
dev_node dev_node
); );
return Ok(( return Ok(Self {
Self { file: RefCell::new(OpenOptions::new().write(true).open(dev_node)?),
file: RefCell::new(OpenOptions::new().write(true).open(dev_node)?), devfs_path: dev_node.to_owned(),
devfs_path: dev_node.to_owned(), prod_id: id_product.to_string(),
prod_id: id_product.to_string(), syspath: endpoint.syspath().into(),
syspath: endpoint.syspath().into(), });
},
endpoint,
));
} }
} }
} }