Add "info" output for gfx driver check

This commit is contained in:
Luke D Jones
2020-09-22 09:52:22 +12:00
parent d4702a166a
commit 368d279ca5
15 changed files with 319 additions and 135 deletions

View File

@@ -19,7 +19,7 @@ use config::Config;
use crate::error::RogError;
use zbus::ObjectServer;
pub static VERSION: &str = "2.0.1";
pub static VERSION: &str = "2.0.2";
pub trait Reloadable {
fn reload(&mut self) -> Result<(), RogError>;

View File

@@ -8,6 +8,7 @@ use daemon::ctrl_fan_cpu::FanLevel;
use gumdrop::Options;
use log::LevelFilter;
use std::io::Write;
use std::process::Command;
use yansi_term::Colour::Green;
use yansi_term::Colour::Red;
@@ -23,18 +24,18 @@ struct CLIStart {
pwr_profile: Option<FanLevel>,
#[options(meta = "CHRG", help = "<20-100>")]
chg_limit: Option<u8>,
#[options(help = "Set graphics mode: <nvidia, hybrid, compute, integrated>")]
graphics: Option<GfxVendors>,
#[options(command)]
command: Option<Command>,
command: Option<CliCommand>,
}
#[derive(Options)]
enum Command {
enum CliCommand {
#[options(help = "Set the keyboard lighting from built-in modes")]
LedMode(LedModeCommand),
#[options(help = "Create and configure profiles")]
Profile(ProfileCommand),
#[options(help = "Set the graphics mode")]
Graphics(GraphicsCommand),
}
#[derive(Options)]
@@ -45,6 +46,20 @@ struct LedModeCommand {
command: Option<SetAuraBuiltin>,
}
#[derive(Options)]
struct GraphicsCommand {
#[options(help = "print help message")]
help: bool,
#[options(help = "Set graphics mode: <nvidia, hybrid, compute, integrated>")]
mode: Option<GfxVendors>,
#[options(help = "Get the current mode")]
get: bool,
#[options(help = "Get the current power status")]
pow: bool,
#[options(help = "Do not ask for confirmation")]
force: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut logger = env_logger::Builder::new();
logger
@@ -62,14 +77,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let writer = AuraDbusClient::new()?;
match parsed.command {
Some(Command::LedMode(mode)) => {
Some(CliCommand::LedMode(mode)) => {
if let Some(command) = mode.command {
writer.write_builtin_mode(&command.into())?
}
}
Some(Command::Profile(command)) => {
Some(CliCommand::Profile(command)) => {
writer.write_profile_command(&ProfileEvent::Cli(command))?
}
Some(CliCommand::Graphics(command)) => do_gfx(command, &writer)?,
None => (),
}
@@ -82,52 +98,94 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Some(chg_limit) = parsed.chg_limit {
writer.write_charge_limit(chg_limit)?;
}
if let Some(gfx) = parsed.graphics {
Ok(())
}
fn do_gfx(
command: GraphicsCommand,
writer: &AuraDbusClient,
) -> Result<(), Box<dyn std::error::Error>> {
if let Some(mode) = command.mode {
println!("Updating settings, please wait...");
println!("If this takes longer than 30s, ctrl+c then check journalctl");
writer.write_gfx_mode(gfx)?;
writer.write_gfx_mode(mode)?;
let res = writer.wait_gfx_changed()?;
match res.as_str() {
"reboot" => println!(
"{}\n{}",
Green.paint("\nGraphics vendor mode changed successfully\n"),
Red.paint("\nPlease reboot to complete switch\n")
),
"reboot" => {
println!(
"{}",
Green.paint("\nGraphics vendor mode changed successfully\n"),
);
do_gfx_action(
command.force,
Command::new("systemctl").arg("reboot"),
"Reboot Linux PC",
"Please reboot when ready",
)?;
}
"restartx" => {
println!(
"{}",
Green.paint("\nGraphics vendor mode changed successfully\n")
);
restart_x()?;
do_gfx_action(
command.force,
Command::new("systemctl")
.arg("restart")
.arg("display-manager.service"),
"Restart display-manager server",
"Please restart display-manager when ready",
)?;
std::process::exit(1)
}
_ => std::process::exit(-1),
}
std::process::exit(-1)
}
Ok(())
}
fn restart_x() -> Result<(), Box<dyn std::error::Error>> {
println!("Restart X server? y/n");
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).expect("Input failed");
let input = buf.chars().next().unwrap() as char;
if input == 'Y' || input == 'y' {
println!("Restarting X server");
let status = std::process::Command::new("systemctl")
.arg("restart")
.arg("display-manager.service")
.status()?;
if !status.success() {
println!("systemctl: display-manager returned with {}", status);
if command.get {
let res = writer.get_gfx_mode()?;
println!("Current graphics mode: {}", res);
}
if command.pow {
let res = writer.get_gfx_pwr()?;
if res.contains("active") {
println!("Current power status: {}", Red.paint(&format!("{}", res)));
} else {
println!("Current power status: {}", Green.paint(&format!("{}", res)));
}
} else {
println!("{}", Red.paint("Cancelled. Please restart X when ready"));
}
Ok(())
}
fn do_gfx_action(
no_confirm: bool,
command: &mut Command,
ask_msg: &str,
cancel_msg: &str,
) -> Result<(), Box<dyn std::error::Error>> {
println!("{}? y/n", ask_msg);
let mut buf = String::new();
if no_confirm {
let status = command.status()?;
if !status.success() {
println!("systemctl: returned with {}", status);
}
}
std::io::stdin().read_line(&mut buf).expect("Input failed");
let input = buf.chars().next().unwrap() as char;
if input == 'Y' || input == 'y' || no_confirm {
let status = command.status()?;
if !status.success() {
println!("systemctl: returned with {}", status);
}
} else {
println!("{}", Red.paint(&format!("{}", cancel_msg)));
}
Ok(())
}