Customise initial help for laptop

This commit is contained in:
Luke D Jones
2021-03-09 23:23:23 +13:00
parent 2e4ce27f6b
commit 60014b8a40
7 changed files with 134 additions and 65 deletions

1
Cargo.lock generated
View File

@@ -35,6 +35,7 @@ dependencies = [
name = "asusctl" name = "asusctl"
version = "3.1.0" version = "3.1.0"
dependencies = [ dependencies = [
"daemon",
"gumdrop", "gumdrop",
"rog_dbus", "rog_dbus",
"rog_types", "rog_types",

View File

@@ -11,6 +11,7 @@ edition = "2018"
serde_json = "^1.0" serde_json = "^1.0"
rog_dbus = { path = "../rog-dbus" } rog_dbus = { path = "../rog-dbus" }
rog_types = { path = "../rog-types" } rog_types = { path = "../rog-types" }
daemon = { path = "../daemon" }
gumdrop = "^0.8" gumdrop = "^0.8"
yansi-term = "^0.1" yansi-term = "^0.1"

View File

@@ -1,3 +1,7 @@
use daemon::{
ctrl_fan_cpu::FanCpuSupportedFunctions, ctrl_leds::LedSupportedFunctions,
ctrl_supported::SupportedFunctions,
};
use gumdrop::{Opt, Options}; use gumdrop::{Opt, Options};
use rog_dbus::AuraDbusClient; use rog_dbus::AuraDbusClient;
use rog_types::{ use rog_types::{
@@ -104,8 +108,7 @@ struct BiosCommand {
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args: Vec<String> = args().collect(); let mut args: Vec<String> = args().skip(1).collect();
args.remove(0);
let parsed: CLIStart; let parsed: CLIStart;
let missing_argument_k = gumdrop::Error::missing_argument(Opt::Short('k')); let missing_argument_k = gumdrop::Error::missing_argument(Opt::Short('k'));
@@ -125,64 +128,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
} }
if parsed.help_requested() { let (dbus, _) = AuraDbusClient::new()?;
// As help option don't work with `parse_args_default`
// we will call `parse_args_default_or_exit` instead let supported_tmp = dbus.proxies().supported().get_supported_functions()?;
CLIStart::parse_args_default_or_exit(); let supported = serde_json::from_str::<SupportedFunctions>(&supported_tmp)?;
if parsed.help {
print_supported_help(&supported, &parsed);
std::process::exit(1);
} }
if parsed.version { if parsed.version {
println!(" asusctl version {}", env!("CARGO_PKG_VERSION")); println!(" asusctl v{}", env!("CARGO_PKG_VERSION"));
println!(" rog-dbus version {}", rog_dbus::VERSION); println!(" rog-dbus v{}", rog_dbus::VERSION);
println!("rog-types version {}", rog_types::VERSION); println!("rog-types v{}", rog_types::VERSION);
return Ok(());
} }
let (dbus, _) = AuraDbusClient::new()?;
match parsed.command { match parsed.command {
Some(CliCommand::LedMode(mode)) => { Some(CliCommand::LedMode(mode)) => handle_led_mode(&dbus, &supported.keyboard_led, &mode)?,
if (mode.command.is_none() && !mode.prev_mode && !mode.next_mode) || mode.help { Some(CliCommand::Profile(cmd)) => handle_profile(&dbus, &supported.fan_cpu_ctrl, &cmd)?,
println!("Missing arg or command\n\n{}", mode.self_usage());
if let Some(lst) = mode.self_command_list() {
println!("\n{}", lst);
}
println!("\nHelp can also be requested on modes, e.g: static --help");
}
if mode.next_mode && mode.prev_mode {
println!("Please specify either next or previous")
}
if mode.next_mode {
dbus.proxies().led().next_led_mode()?;
} else if mode.prev_mode {
dbus.proxies().led().prev_led_mode()?;
} else if let Some(command) = mode.command {
dbus.proxies().led().set_led_mode(&command.into())?
}
}
Some(CliCommand::Profile(cmd)) => {
if (!cmd.next
&& !cmd.create
&& cmd.curve.is_none()
&& cmd.max_percentage.is_none()
&& cmd.min_percentage.is_none()
&& cmd.preset.is_none()
&& cmd.profile.is_none()
&& cmd.turbo.is_none())
|| cmd.help
{
println!("Missing arg or command\n\n{}", cmd.self_usage());
if let Some(lst) = cmd.self_command_list() {
println!("\n{}", lst);
}
}
if cmd.next {
dbus.proxies().profile().next_fan()?;
} else {
dbus.proxies()
.profile()
.write_command(&ProfileEvent::Cli(cmd))?
}
}
Some(CliCommand::Graphics(cmd)) => do_gfx(cmd, &dbus)?, Some(CliCommand::Graphics(cmd)) => do_gfx(cmd, &dbus)?,
Some(CliCommand::AniMe(cmd)) => { Some(CliCommand::AniMe(cmd)) => {
if (cmd.command.is_none() && cmd.boot.is_none() && cmd.turn.is_none()) || cmd.help { if (cmd.command.is_none() && cmd.boot.is_none() && cmd.turn.is_none()) || cmd.help {
@@ -282,6 +247,37 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
fn print_supported_help(supported: &SupportedFunctions, parsed: &CLIStart) {
// As help option don't work with `parse_args_default`
// we will call `parse_args_default_or_exit` instead
println!("{}", parsed.self_usage());
// command strings are in order of the struct
let commands: Vec<String> = CliCommand::usage().lines().map(|s| s.to_string()).collect();
if !supported.fan_cpu_ctrl.stock_fan_modes {
println!("Note: Fan mode control is not supported by this laptop");
}
if !supported.charge_ctrl.charge_level_set {
println!("Note: Charge control is not supported by this laptop");
}
println!("\nCommands available");
if supported.keyboard_led.stock_led_modes.is_some() {
println!("{}", commands[0]);
}
if supported.fan_cpu_ctrl.stock_fan_modes || supported.fan_cpu_ctrl.fan_curve_set {
println!("{}", commands[1]);
}
// graphics
println!("{}", commands[2]);
if supported.anime_ctrl.0 {
println!("{}", commands[3]);
}
if supported.rog_bios_ctrl.dedicated_gfx_toggle || supported.rog_bios_ctrl.post_sound_toggle {
println!("{}", commands[4]);
}
}
fn do_gfx( fn do_gfx(
command: GraphicsCommand, command: GraphicsCommand,
dbus_client: &AuraDbusClient, dbus_client: &AuraDbusClient,
@@ -331,3 +327,68 @@ fn do_gfx_action(no_confirm: bool, ask_msg: &str, ok_msg: &str) -> bool {
} }
false false
} }
fn handle_led_mode(
dbus: &AuraDbusClient,
supported: &LedSupportedFunctions,
mode: &LedModeCommand,
) -> Result<(), Box<dyn std::error::Error>> {
if mode.command.is_none() && !mode.prev_mode && !mode.next_mode {
if !mode.help {
println!("Missing arg or command\n");
}
println!("{}", mode.self_usage());
if let Some(lst) = mode.self_command_list() {
println!("\n{}", lst);
}
println!("\nHelp can also be requested on modes, e.g: static --help");
std::process::exit(1);
}
if mode.next_mode && mode.prev_mode {
println!("Please specify either next or previous")
}
if mode.next_mode {
dbus.proxies().led().next_led_mode()?;
} else if mode.prev_mode {
dbus.proxies().led().prev_led_mode()?;
} else if let Some(command) = mode.command.as_ref() {
dbus.proxies().led().set_led_mode(&command.into())?
}
Ok(())
}
fn handle_profile(
dbus: &AuraDbusClient,
supported: &FanCpuSupportedFunctions,
cmd: &ProfileCommand,
) -> Result<(), Box<dyn std::error::Error>> {
if !cmd.next
&& !cmd.create
&& cmd.curve.is_none()
&& cmd.max_percentage.is_none()
&& cmd.min_percentage.is_none()
&& cmd.preset.is_none()
&& cmd.profile.is_none()
&& cmd.turbo.is_none()
{
if !cmd.help {
println!("Missing arg or command\n");
}
println!("{}", cmd.self_usage());
if let Some(lst) = cmd.self_command_list() {
println!("\n{}", lst);
}
std::process::exit(1);
}
if cmd.next {
dbus.proxies().profile().next_fan()?;
} else {
dbus.proxies()
.profile()
.write_command(&ProfileEvent::Cli(cmd.clone()))?
}
Ok(())
}

View File

@@ -30,7 +30,7 @@ use crate::GetSupported;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct AnimeSupportedFunctions(bool); pub struct AnimeSupportedFunctions(pub bool);
impl GetSupported for CtrlAnimeDisplay { impl GetSupported for CtrlAnimeDisplay {
type A = AnimeSupportedFunctions; type A = AnimeSupportedFunctions;

View File

@@ -15,11 +15,11 @@ use crate::{
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct SupportedFunctions { pub struct SupportedFunctions {
anime_ctrl: AnimeSupportedFunctions, pub anime_ctrl: AnimeSupportedFunctions,
charge_ctrl: ChargeSupportedFunctions, pub charge_ctrl: ChargeSupportedFunctions,
fan_cpu_ctrl: FanCpuSupportedFunctions, pub fan_cpu_ctrl: FanCpuSupportedFunctions,
keyboard_led: LedSupportedFunctions, pub keyboard_led: LedSupportedFunctions,
rog_bios_ctrl: RogBiosSupportedFunctions, pub rog_bios_ctrl: RogBiosSupportedFunctions,
} }
#[dbus_interface(name = "org.asuslinux.Daemon")] #[dbus_interface(name = "org.asuslinux.Daemon")]

View File

@@ -223,6 +223,12 @@ impl From<SetAuraBuiltin> for AuraModes {
} }
} }
impl From<&SetAuraBuiltin> for AuraModes {
fn from(mode: &SetAuraBuiltin) -> Self {
mode.clone().into()
}
}
/// Very specific mode conversion required because numbering isn't linear /// Very specific mode conversion required because numbering isn't linear
impl From<AuraModes> for u8 { impl From<AuraModes> for u8 {
fn from(mode: AuraModes) -> Self { fn from(mode: AuraModes) -> Self {

View File

@@ -81,16 +81,16 @@ pub struct ProfileCommand {
#[options(help = "create the profile if it doesn't exist")] #[options(help = "create the profile if it doesn't exist")]
pub create: bool, pub create: bool,
#[options(help = "enable or disable cpu turbo")] #[options(meta = "", help = "enable or disable cpu turbo")]
pub turbo: Option<bool>, pub turbo: Option<bool>,
#[options(help = "set min cpu scaling (intel)")] #[options(meta = "", help = "set min cpu scaling (intel)")]
pub min_percentage: Option<u8>, pub min_percentage: Option<u8>,
#[options(help = "set max cpu scaling (intel)")] #[options(meta = "", help = "set max cpu scaling (intel)")]
pub max_percentage: Option<u8>, pub max_percentage: Option<u8>,
#[options(meta = "PWR", help = "<silent, normal, boost>")] #[options(meta = "", help = "<silent, normal, boost>")]
pub preset: Option<FanLevel>, pub preset: Option<FanLevel>,
#[options(parse(try_from_str = "parse_fan_curve"), help = "set fan curve")] #[options(meta = "", parse(try_from_str = "parse_fan_curve"), help = "set fan curve")]
pub curve: Option<Curve>, pub curve: Option<Curve>,
#[options(free)] #[options(free)]
pub profile: Option<String>, pub profile: Option<String>,