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.
This commit is contained in:
LGUG2Z committed 2025-09-25 07:30:28 -07:00
1 parent 3a24b51111
commit e478ab8d0a
5 files changed
+64 -13

No files matched your search

+2 -2
View File
@@ -284,8 +284,8 @@ pub enum SocketMessage {
EnsureNamedWorkspaces(usize, Vec<String>),
NewWorkspace,
ToggleTiling,
// Stop,
// StopIgnoreRestore,
Stop,
StopIgnoreRestore,
TogglePause,
Retile,
RetileWithResizeDimensions,
+1 -1
View File
@@ -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);
}
+6
View File
@@ -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(
+32
View File
@@ -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");
+23 -10
View File
@@ -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");