diff --git a/asusctl/src/main.rs b/asusctl/src/main.rs index 534f0ce4..f059c315 100644 --- a/asusctl/src/main.rs +++ b/asusctl/src/main.rs @@ -13,6 +13,7 @@ use rog_anime::{AnimTime, AnimeDataBuffer, AnimeDiagonal, AnimeGif, AnimeImage, use rog_aura::keyboard::{AuraPowerState, LaptopAuraPower}; use rog_aura::{self, AuraEffect, PowerZones}; use rog_dbus::asus_armoury::AsusArmouryProxyBlocking; +use rog_dbus::find_iface_blocking; use rog_dbus::list_iface_blocking; use rog_dbus::scsi_aura::ScsiAuraProxyBlocking; use rog_dbus::zbus_anime::AnimeProxyBlocking; @@ -26,7 +27,6 @@ use rog_profiles::error::ProfileError; use rog_scsi::AuraMode; use ron::ser::PrettyConfig; use scsi_cli::ScsiCommand; -use zbus::blocking::proxy::ProxyImpl; use zbus::blocking::Connection; use crate::cli_opts::*; @@ -149,44 +149,6 @@ fn check_service(name: &str) -> bool { false } -fn find_iface(iface_name: &str) -> Result, Box> -where - T: ProxyImpl<'static> + From>, -{ - let conn = zbus::blocking::Connection::system()?; - let f = zbus::blocking::fdo::ObjectManagerProxy::new(&conn, "xyz.ljones.Asusd", "/")?; - let interfaces = f.get_managed_objects()?; - let mut paths = Vec::new(); - for v in interfaces.iter() { - // let o: Vec = v.1.keys().map(|e| - // e.to_owned()).collect(); println!("{}, {:?}", v.0, o); - for k in v.1.keys() { - if k.as_str() == iface_name { - // println!("Found {iface_name} device at {}, {}", v.0, k); - paths.push(v.0.clone()); - } - } - } - if paths.len() > 1 { - println!("Multiple asusd interfaces devices found"); - } - if !paths.is_empty() { - let mut ctrl = Vec::new(); - paths.sort_by(|a, b| a.cmp(b)); - for path in paths { - ctrl.push( - T::builder(&conn) - .path(path.clone())? - .destination("xyz.ljones.Asusd")? - .build()?, - ); - } - return Ok(ctrl); - } - - Err(format!("Did not find {iface_name}").into()) -} - fn do_parsed( parsed: &CliStart, supported_interfaces: &[String], @@ -259,7 +221,7 @@ fn handle_info( "Supported Platform Properties:\n{:#?}", supported_properties ); - if let Ok(aura) = find_iface::("xyz.ljones.Aura") { + if let Ok(aura) = find_iface_blocking::("xyz.ljones.Aura") { // TODO: multiple RGB check if let Some(first_aura) = aura.first() { let bright = first_aura.supported_brightness()?; @@ -286,7 +248,7 @@ fn handle_backlight(cmd: &BacklightCommand) -> Result<(), Box("xyz.ljones.Backlight")?; + let backlights = find_iface_blocking::("xyz.ljones.Backlight")?; for backlight in backlights { println!("Current screenpad settings:"); println!(" Brightness: {}", backlight.screenpad_brightness()?); @@ -300,7 +262,7 @@ fn handle_backlight(cmd: &BacklightCommand) -> Result<(), Box("xyz.ljones.Backlight")?; + let backlights = find_iface_blocking::("xyz.ljones.Backlight")?; for backlight in backlights { if let Some(brightness) = cmd.screenpad_brightness { backlight.set_screenpad_brightness(brightness)?; @@ -319,7 +281,7 @@ fn handle_backlight(cmd: &BacklightCommand) -> Result<(), Box Result<(), Box> { - let Ok(aura_proxies) = find_iface::("xyz.ljones.Aura") else { + let Ok(aura_proxies) = find_iface_blocking::("xyz.ljones.Aura") else { println!("No aura interface found"); return Ok(()); }; @@ -374,7 +336,7 @@ fn handle_anime(cmd: &AnimeCommand) -> Result<(), Box> { println!("Missing arg or command; run 'asusctl anime --help' for usage"); } - let animes = find_iface::("xyz.ljones.Anime").map_err(|e| { + let animes = find_iface_blocking::("xyz.ljones.Anime").map_err(|e| { error!("Did not find any interface for xyz.ljones.Anime: {e:?}"); e })?; @@ -557,7 +519,7 @@ fn handle_scsi(cmd: &ScsiCommand) -> Result<(), Box> { println!("Missing arg or command; run 'asusctl scsi --help' for usage"); } - let scsis = find_iface::("xyz.ljones.ScsiAura")?; + let scsis = find_iface_blocking::("xyz.ljones.ScsiAura")?; for scsi in scsis { if let Some(enable) = cmd.enable { @@ -621,7 +583,7 @@ fn handle_led_mode(mode: &LedModeCommand) -> Result<(), Box("xyz.ljones.Aura") { + if let Ok(aura) = find_iface_blocking::("xyz.ljones.Aura") { if let Some(first_aura) = aura.first() { let modes = first_aura.supported_basic_modes()?; println!("Available modes:"); @@ -637,7 +599,7 @@ fn handle_led_mode(mode: &LedModeCommand) -> Result<(), Box("xyz.ljones.Aura")?; + let aura = find_iface_blocking::("xyz.ljones.Aura")?; if mode.next_mode { for aura in aura { let mode = aura.led_mode()?; @@ -678,7 +640,7 @@ fn handle_led_mode(mode: &LedModeCommand) -> Result<(), Box Result<(), Box> { - let aura = find_iface::("xyz.ljones.Aura")?; + let aura = find_iface_blocking::("xyz.ljones.Aura")?; for aura in aura { let dev_type = aura.device_type()?; if !dev_type.is_old_laptop() && !dev_type.is_tuf_laptop() { @@ -735,7 +697,7 @@ fn handle_led_power_1_do_1866( } fn handle_led_power2(power: &LedPowerCommand2) -> Result<(), Box> { - let aura = find_iface::("xyz.ljones.Aura")?; + let aura = find_iface_blocking::("xyz.ljones.Aura")?; for aura in aura { let dev_type = aura.device_type()?; if !dev_type.is_new_laptop() { @@ -1031,7 +993,9 @@ fn handle_armoury_command( // If nested subcommand provided, handle set/get/list. match &cmd.command { ArmourySubCommand::List(_) => { - if let Ok(attrs) = find_iface::("xyz.ljones.AsusArmoury") { + if let Ok(attrs) = + find_iface_blocking::("xyz.ljones.AsusArmoury") + { for attr in attrs.iter() { print_firmware_attr(attr)?; } @@ -1040,7 +1004,7 @@ fn handle_armoury_command( } ArmourySubCommand::Get(g) => { let mut found = false; - let attrs = find_iface::("xyz.ljones.AsusArmoury") + let attrs = find_iface_blocking::("xyz.ljones.AsusArmoury") .map_err(|e| format!("Could not reach asusd armoury interface: {e}"))?; for attr in attrs.iter() { let name = attr.name()?; @@ -1056,7 +1020,7 @@ fn handle_armoury_command( } ArmourySubCommand::Set(s) => { let mut found = false; - let attrs = find_iface::("xyz.ljones.AsusArmoury") + let attrs = find_iface_blocking::("xyz.ljones.AsusArmoury") .map_err(|e| format!("Could not reach asusd armoury interface: {e}"))?; for attr in attrs.iter() { let name = attr.name()?; diff --git a/asusctl/src/slash_cli.rs b/asusctl/src/slash_cli.rs index cbc6e30a..f690d688 100644 --- a/asusctl/src/slash_cli.rs +++ b/asusctl/src/slash_cli.rs @@ -1,7 +1,7 @@ use argh::FromArgs; +use rog_dbus::find_iface_blocking; use rog_dbus::zbus_slash::SlashProxyBlocking; use rog_slash::SlashMode; -use zbus::blocking::Connection; #[derive(FromArgs, Debug)] #[argh(subcommand, name = "slash", description = "slash ledbar commands")] @@ -79,73 +79,72 @@ pub fn handle_slash_set(cmd: &SlashSetCommand) -> Result<(), Box("xyz.ljones.Slash")?; + for proxy in &slashes { + if cmd.enable { + proxy.set_enabled(true)?; + } + if cmd.disable { + proxy.set_enabled(false)?; + } + if let Some(brightness) = cmd.brightness { + proxy.set_brightness(brightness)?; + } + if let Some(interval) = cmd.interval { + proxy.set_interval(interval)?; + } + if let Some(slash_mode) = cmd.mode { + proxy.set_mode(slash_mode)?; + } + if let Some(show) = cmd.show_on_boot { + proxy.set_show_on_boot(show)?; + } + if let Some(show) = cmd.show_on_shutdown { + proxy.set_show_on_shutdown(show)?; + } + if let Some(show) = cmd.show_on_sleep { + proxy.set_show_on_sleep(show)?; + } + if let Some(show) = cmd.show_on_battery { + proxy.set_show_on_battery(show)?; + } + if let Some(show) = cmd.show_battery_warning { + proxy.set_show_battery_warning(show)?; + } } Ok(()) } pub fn handle_slash_get() -> Result<(), Box> { - let conn = Connection::system()?; - let proxy = SlashProxyBlocking::new(&conn) - .map_err(|e| format!("Failed to connect to Slash interface: {e}"))?; - - let enabled = proxy.enabled()?; - let brightness = proxy.brightness()?; - let interval = proxy.interval()?; - let mode = proxy.mode()?; - let show_on_boot = proxy.show_on_boot()?; - let show_on_shutdown = proxy.show_on_shutdown()?; - let show_on_sleep = proxy.show_on_sleep()?; - let show_on_battery = proxy.show_on_battery()?; - let show_battery_warning = proxy.show_battery_warning()?; - - println!( - "Slash LED: {}", - if enabled { "enabled" } else { "disabled" } - ); - println!("Brightness: {}", brightness); - println!("Interval: {}", interval); - println!("Mode: {}", mode); - println!("Show on boot: {}", show_on_boot); - println!("Show on shutdown: {}", show_on_shutdown); - println!("Show on sleep: {}", show_on_sleep); - println!("Show on battery: {}", show_on_battery); - println!("Show battery warning: {}", show_battery_warning); + let slashes = find_iface_blocking::("xyz.ljones.Slash")?; + for proxy in &slashes { + let enabled = proxy.enabled()?; + let brightness = proxy.brightness()?; + let interval = proxy.interval()?; + let mode = proxy.mode()?; + let show_on_boot = proxy.show_on_boot()?; + let show_on_shutdown = proxy.show_on_shutdown()?; + let show_on_sleep = proxy.show_on_sleep()?; + let show_on_battery = proxy.show_on_battery()?; + let show_battery_warning = proxy.show_battery_warning()?; + + println!( + "Slash LED: {}", + if enabled { "enabled" } else { "disabled" } + ); + println!("Brightness: {}", brightness); + println!("Interval: {}", interval); + println!("Mode: {}", mode); + println!("Show on boot: {}", show_on_boot); + println!("Show on shutdown: {}", show_on_shutdown); + println!("Show on sleep: {}", show_on_sleep); + println!("Show on battery: {}", show_on_battery); + println!("Show battery warning: {}", show_battery_warning); + } Ok(()) } diff --git a/asusctl/src/xgm_led_cli.rs b/asusctl/src/xgm_led_cli.rs index 4e6bfe86..f80af7e1 100644 --- a/asusctl/src/xgm_led_cli.rs +++ b/asusctl/src/xgm_led_cli.rs @@ -1,22 +1,24 @@ use crate::cli_opts::XgmLedSubCommand; +use rog_dbus::find_iface_blocking; use rog_dbus::zbus_xgm_led::XgmLedProxyBlocking; pub fn handle_xgm_led(cmd: &XgmLedSubCommand) -> Result<(), Box> { - let proxy = XgmLedProxyBlocking::new(&zbus::blocking::Connection::system()?) - .map_err(|e| format!("Failed to connect to XG Mobile LED interface: {e}"))?; + let xgm_leds = find_iface_blocking::("xyz.ljones.XgmLed")?; - match cmd { - XgmLedSubCommand::Get(_) => { - let enabled = proxy.xgm_led_enabled()?; - println!("XG Mobile LED: {}", if enabled { "ON" } else { "OFF" }); - } - XgmLedSubCommand::Set(cmd) => { - let enabled = cmd.value != 0; - proxy.set_xgm_led_enabled(enabled)?; - println!( - "XG Mobile LED set to {}", - if enabled { "ON" } else { "OFF" } - ); + for proxy in &xgm_leds { + match cmd { + XgmLedSubCommand::Get(_) => { + let enabled = proxy.xgm_led_enabled()?; + println!("XG Mobile LED: {}", if enabled { "ON" } else { "OFF" }); + } + XgmLedSubCommand::Set(cmd) => { + let enabled = cmd.value != 0; + proxy.set_xgm_led_enabled(enabled)?; + println!( + "XG Mobile LED set to {}", + if enabled { "ON" } else { "OFF" } + ); + } } } diff --git a/rog-dbus/Cargo.toml b/rog-dbus/Cargo.toml index 29c7759d..e9e4f20a 100644 --- a/rog-dbus/Cargo.toml +++ b/rog-dbus/Cargo.toml @@ -18,4 +18,5 @@ rog_aura = { path = "../rog-aura" } rog_profiles = { path = "../rog-profiles" } rog_platform = { path = "../rog-platform" } zbus.workspace = true +log.workspace = true diff --git a/rog-dbus/src/lib.rs b/rog-dbus/src/lib.rs index 19cfc817..78fff969 100644 --- a/rog-dbus/src/lib.rs +++ b/rog-dbus/src/lib.rs @@ -23,6 +23,8 @@ pub fn list_iface_blocking() -> Result, Box> ifaces.push(k.to_string()); } } + ifaces.sort(); + ifaces.dedup(); Ok(ifaces) } @@ -73,7 +75,7 @@ where } } if paths.len() > 1 { - println!("Multiple asusd interfaces devices found"); + log::warn!("Multiple asusd interfaces devices found"); } if !paths.is_empty() { let mut ctrl = Vec::new(); @@ -92,3 +94,38 @@ where Err(format!("Did not find {iface_name}").into()) } + +pub fn find_iface_blocking(iface_name: &str) -> Result, Box> +where + T: zbus::blocking::proxy::ProxyImpl<'static> + From>, +{ + let conn = zbus::blocking::Connection::system()?; + let f = zbus::blocking::fdo::ObjectManagerProxy::new(&conn, "xyz.ljones.Asusd", "/")?; + let interfaces = f.get_managed_objects()?; + let mut paths = Vec::new(); + for v in interfaces.iter() { + for k in v.1.keys() { + if k.as_str() == iface_name { + paths.push(v.0.clone()); + } + } + } + if paths.len() > 1 { + log::warn!("Multiple asusd interfaces devices found"); + } + if !paths.is_empty() { + let mut ctrl = Vec::new(); + paths.sort_by(|a, b| a.cmp(b)); + for path in paths { + ctrl.push( + T::builder(&conn) + .path(path.clone())? + .destination("xyz.ljones.Asusd")? + .build()?, + ); + } + return Ok(ctrl); + } + + Err(format!("Did not find {iface_name}").into()) +}