Skip to content

Commit

Permalink
Merge pull request #4 from rageagainsthepc/background-flag
Browse files Browse the repository at this point in the history
Hide call to FreeConsole behind cli flag
  • Loading branch information
rageagainsthepc authored Dec 30, 2021
2 parents d4262ee + 99603cc commit 92895a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ampsim-launcher"
version = "0.1.3"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
21 changes: 10 additions & 11 deletions src/launch.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use stable_eyre::{
eyre::{bail, eyre},
Report, Result,
};
use stable_eyre::{eyre::eyre, Report, Result};
use std::path::Path;
use std::process::Command;
use std::ptr;
Expand Down Expand Up @@ -61,9 +58,9 @@ fn switch_power_plan(plan_guid: &GUID) -> Result<()> {
fn elevate_process_priority(id: u32) -> Result<()> {
unsafe {
let proc_handle = OpenProcess(PROCESS_SET_INFORMATION, false, id);
if proc_handle.is_invalid() {
bail!("Unable to open process")
}
proc_handle
.ok()
.map_err(|e| eyre!(e.message()).wrap_err("Unable to open process"))?;

let success = SetPriorityClass(proc_handle, HIGH_PRIORITY_CLASS);
success
Expand Down Expand Up @@ -100,12 +97,14 @@ fn end_background_mode() -> Result<()> {
}
}

fn spawn_child_and_wait(cmd: &mut Command) -> Result<()> {
fn spawn_child_and_wait(cmd: &mut Command, no_console: bool) -> Result<()> {
let mut child = cmd
.spawn()
.map_err(|e| Report::from(e).wrap_err("Unable to launch target process"))?;
// apparently, detaching from the console will prevent spawning child processes
hide_console_window();
if no_console {
hide_console_window();
}
elevate_process_priority(child.id())?;

begin_background_mode()?;
Expand All @@ -115,14 +114,14 @@ fn spawn_child_and_wait(cmd: &mut Command) -> Result<()> {
Ok(())
}

pub(crate) fn launch(program: &Path) -> Result<()> {
pub(crate) fn launch(program: &Path, no_console: bool) -> Result<()> {
let active_power_plan_guid = get_active_power_plan()?;

let high_perf_plan = GUID::try_from(HIGH_PERF_PLAN_GUID).unwrap();
switch_power_plan(&high_perf_plan)?;

let mut cmd = Command::new(program);
match spawn_child_and_wait(&mut cmd) {
match spawn_child_and_wait(&mut cmd, no_console) {
Ok(()) => switch_power_plan(&active_power_plan_guid),
Err(e) => match switch_power_plan(&active_power_plan_guid) {
Ok(()) => Err(e),
Expand Down
5 changes: 4 additions & 1 deletion src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use stable_eyre::{eyre::bail, Result};

pub(crate) fn make_link(target: &Path, location: &Path) -> Result<()> {
let mut link = ShellLink::new(std::env::current_exe()?)?;
link.set_arguments(Some(format!("-e launch \"{}\"", target.to_string_lossy())));
link.set_arguments(Some(format!(
"--background launch \"{}\"",
target.to_string_lossy()
)));
match target.parent() {
Some(parent) => link.set_working_dir(Some(parent.to_str().unwrap().to_string())),
None => bail!("Unable to determine parent directory of shortcut target"),
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn run_subcommand(matches: &ArgMatches) -> Result<()> {
bail!("Program must exist")
}

launch(&program)?;
launch(&program, matches.is_present("background"))?;
}
Some(("link", sub_matches)) => {
let target = Path::new(
Expand Down Expand Up @@ -64,7 +64,7 @@ fn main() -> Result<()> {

let matches = App::new("ampsim_starter")
.about("A tool for launching programs with optimized performance")
.arg(arg!(-e --errorbox "Display error messages in a graphical message box"))
.arg(arg!(-b --background "Activate background mode (errors as message boxes, hidden console window)"))
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
App::new("launch")
Expand All @@ -82,7 +82,7 @@ fn main() -> Result<()> {
.get_matches();

let result = run_subcommand(&matches);
if matches.is_present("errorbox") {
if matches.is_present("background") {
match result {
Ok(()) => (),
Err(ref e) => display_error_box(format!("{:#}", e).as_str()),
Expand Down

0 comments on commit 92895a0

Please sign in to comment.