From 793e81d43d33fa89d852f3556a61be7e654c893a Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 20 Apr 2024 13:26:09 -0700 Subject: [PATCH] fix(cli): use force-quit if stop signal fails This commit ensures that the komorebic stop command will force-quit komorebi if the Stop SocketMessage handler fails. --- Cargo.toml | 1 + komorebi/Cargo.toml | 2 +- komorebi/src/static_config.rs | 2 +- komorebi/src/workspace.rs | 11 +++++++---- komorebic/Cargo.toml | 2 +- komorebic/src/main.rs | 28 ++++++++++++++++++++++++++++ 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 68242582..160ec357 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ dunce = "1" dirs = "5" color-eyre = "0.6" serde_json = { package = "serde_json_lenient", version = "0.1" } +sysinfo = "0.30" [workspace.dependencies.windows] version = "0.54" diff --git a/komorebi/Cargo.toml b/komorebi/Cargo.toml index 45f87b0a..e1e6d4d4 100644 --- a/komorebi/Cargo.toml +++ b/komorebi/Cargo.toml @@ -35,7 +35,7 @@ schemars = "0.8" serde = { version = "1", features = ["derive"] } serde_json = { workspace = true } strum = { version = "0.26", features = ["derive"] } -sysinfo = "0.30" +sysinfo = { workspace = true } tracing = "0.1" tracing-appender = "0.2" tracing-subscriber = { version = "0.3", features = ["env-filter"] } diff --git a/komorebi/src/static_config.rs b/komorebi/src/static_config.rs index 7e8c3ab1..4a6a4346 100644 --- a/komorebi/src/static_config.rs +++ b/komorebi/src/static_config.rs @@ -727,7 +727,7 @@ impl StaticConfig { value.apply_globals()?; - let stackbar_mode = STACKBAR_MODE.lock().clone(); + let stackbar_mode = *STACKBAR_MODE.lock(); for m in wm.monitors_mut() { for w in m.workspaces_mut() { diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index eb2384de..0a7ab254 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -327,10 +327,13 @@ impl Workspace { } if let Some(stackbar) = container_topbar { - if let Ok(_) = stackbar.set_position( - &stackbar.get_position_from_container_layout(layout), - false, - ) { + if stackbar + .set_position( + &stackbar.get_position_from_container_layout(layout), + false, + ) + .is_ok() + { stackbar.update(&container_windows, focused_hwnd)?; let tab_height = STACKBAR_TAB_HEIGHT.load(Ordering::SeqCst); let total_height = tab_height + container_padding; diff --git a/komorebic/Cargo.toml b/komorebic/Cargo.toml index 292ec7cd..3296956b 100644 --- a/komorebic/Cargo.toml +++ b/komorebic/Cargo.toml @@ -28,7 +28,7 @@ reqwest = { version = "0.12", features = ["blocking"] } serde = { version = "1", features = ["derive"] } serde_json = { workspace = true } serde_yaml = "0.9" -sysinfo = "0.30" +sysinfo = { workspace = true } thiserror = "1" uds_windows = "1" which = "6" diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 7946d19f..934b9c33 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -1897,6 +1897,34 @@ Stop-Process -Name:whkd -ErrorAction SilentlyContinue } send_message(&SocketMessage::Stop.as_bytes()?)?; + let mut system = sysinfo::System::new_all(); + system.refresh_processes(); + + if system.processes_by_name("komorebi.exe").count() >= 1 { + println!("komorebi is still running, attempting to force-quit"); + + let script = r" +Stop-Process -Name:komorebi -ErrorAction SilentlyContinue + "; + match powershell_script::run(script) { + Ok(_) => { + println!("{script}"); + + let hwnd_json = DATA_DIR.join("komorebi.hwnd.json"); + + let file = File::open(hwnd_json)?; + let reader = BufReader::new(file); + let hwnds: Vec = serde_json::from_reader(reader)?; + + for hwnd in hwnds { + restore_window(HWND(hwnd)); + } + } + Err(error) => { + println!("Error: {error}"); + } + } + } } SubCommand::FloatRule(arg) => { send_message(&SocketMessage::FloatRule(arg.identifier, arg.id).as_bytes()?)?;