From e478ab8d0a1889459cb0f02710402cf0c7adcf24 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Thu, 25 Sep 2025 07:30:25 -0700 Subject: [PATCH] feat(wm): implement stop handlers This commit implements the Stop and StopIgnoreRestore SocketMessage variants and sends them from the komorebic stop command. The ctrl-c handler in main.rs now exits with code 130, while the Stop handlers will exit cleanly with code 0. --- komorebi/src/core/mod.rs | 4 ++-- komorebi/src/main.rs | 2 +- komorebi/src/process_command.rs | 6 ++++++ komorebi/src/window_manager.rs | 32 ++++++++++++++++++++++++++++++++ komorebic/src/main.rs | 33 +++++++++++++++++++++++---------- 5 files changed, 64 insertions(+), 13 deletions(-) diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 8ab4be93..35d34e9c 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -284,8 +284,8 @@ pub enum SocketMessage { EnsureNamedWorkspaces(usize, Vec), NewWorkspace, ToggleTiling, - // Stop, - // StopIgnoreRestore, + Stop, + StopIgnoreRestore, TogglePause, Retile, RetileWithResizeDimensions, diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index 64111ca4..e06b0026 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -293,5 +293,5 @@ fn main() -> eyre::Result<()> { let socket = DATA_DIR.join("komorebi.sock"); let _ = std::fs::remove_file(socket); - Ok(()) + std::process::exit(130); } diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index c525ff54..0f74b17f 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -1511,6 +1511,12 @@ impl WindowManager { let mut sockets = SUBSCRIPTION_SOCKETS.lock(); sockets.remove(socket); } + SocketMessage::Stop => { + self.stop(false)?; + } + SocketMessage::StopIgnoreRestore => { + self.stop(true)?; + } } notify_subscribers( diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index d22e7455..1dcba0e0 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -2,6 +2,7 @@ use crate::CoreFoundationRunLoop; use crate::DATA_DIR; use crate::LibraryError; use crate::REGEX_IDENTIFIERS; +use crate::SUBSCRIPTION_SOCKETS; use crate::WORKSPACE_MATCHING_RULES; use crate::accessibility::AccessibilityApi; use crate::application::Application; @@ -43,8 +44,10 @@ use std::collections::HashMap; use std::collections::HashSet; use std::collections::hash_map::Entry; use std::io::ErrorKind; +use std::net::Shutdown; use std::num::NonZeroUsize; use std::os::unix::net::UnixListener; +use std::os::unix::net::UnixStream; use std::path::PathBuf; use std::sync::Arc; @@ -851,6 +854,35 @@ impl WindowManager { workspace.new_container_for_focused_window()?; self.update_focused_workspace(self.mouse_follows_focus, false) } + + #[tracing::instrument(skip(self))] + pub fn stop(&mut self, ignore_restore: bool) -> eyre::Result<()> { + tracing::info!( + "received stop command, restoring all hidden windows and terminating process" + ); + + // TODO: figure out if we wanna do state restores in macOS + // let state = &State::from(&*self); + // std::fs::write( + // DATA_DIR.join("komorebi.state.json"), + // serde_json::to_string_pretty(&state)?, + // )?; + + self.restore_all_windows(ignore_restore)?; + + let sockets = SUBSCRIPTION_SOCKETS.lock(); + for path in (*sockets).values() { + if let Ok(stream) = UnixStream::connect(path) { + stream.shutdown(Shutdown::Both)?; + } + } + + let socket = DATA_DIR.join("komorebi.sock"); + let _ = std::fs::remove_file(socket); + + std::process::exit(0) + } + #[tracing::instrument(skip(self))] pub fn restore_all_windows(&mut self, ignore_restore: bool) -> eyre::Result<()> { tracing::info!("restoring all hidden windows"); diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 1b19e894..29caf8fe 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -33,7 +33,6 @@ use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use std::time::Duration; use sysinfo::ProcessesToUpdate; -use sysinfo::Signal; lazy_static! { static ref HAS_CUSTOM_CONFIG_HOME: AtomicBool = AtomicBool::new(false); @@ -554,6 +553,13 @@ struct Start { // clean_state: bool, } +#[derive(Parser)] +struct Stop { + /// Do not restore windows after stopping komorebi + #[clap(long, hide = true)] + ignore_restore: bool, +} + #[derive(Parser)] struct EagerFocus { /// Case-sensitive exe identifier @@ -584,7 +590,7 @@ enum SubCommand { /// Stop the komorebi.exe process and restore all hidden windows // komorebi for Windows signature // Stop(Stop), - Stop, + Stop(Stop), // /// Kill background processes started by komorebic // Kill(Kill), // /// Check komorebi configuration and related files for common errors @@ -1195,15 +1201,22 @@ fn main() -> eyre::Result<()> { "* Read the docs https://lgug2z.github.io/komorebi - Quickly search through all komorebic commands" ); } - SubCommand::Stop => { - let mut system = sysinfo::System::new_all(); - system.refresh_processes(ProcessesToUpdate::All, true); - - let running = system.processes_by_exact_name("komorebi".as_ref()).next(); - - if let Some(process) = running { - process.kill_with(Signal::Interrupt); + SubCommand::Stop(arg) => { + if arg.ignore_restore { + send_message(&SocketMessage::StopIgnoreRestore)?; + } else { + send_message(&SocketMessage::Stop)?; } + + // TODO: see if we need a force quit sometimes like we do on Windows + // let mut system = sysinfo::System::new_all(); + // system.refresh_processes(ProcessesToUpdate::All, true); + // + // let running = system.processes_by_exact_name("komorebi".as_ref()).next(); + // + // if let Some(process) = running { + // process.kill_with(Signal::Interrupt); + // } } SubCommand::Configuration => { let static_config = HOME_DIR.join("komorebi.json");