From 474f7e26d8f0084e914d2627db4d44a2e2258a43 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 22 Sep 2025 15:42:19 -0700 Subject: [PATCH] feat(wm): ensure we don't run multiple komorebi procs --- Cargo.lock | 104 +++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + dependencies.json | 1 + komorebi/Cargo.toml | 1 + komorebi/src/main.rs | 15 +++++++ 5 files changed, 119 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce355d15..5cd044cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -772,7 +772,7 @@ dependencies = [ "js-sys", "log", "wasm-bindgen", - "windows-core", + "windows-core 0.62.0", ] [[package]] @@ -1022,6 +1022,7 @@ dependencies = [ "serde_json_lenient", "serde_with", "strum", + "sysinfo", "thiserror", "tracing", "tracing-subscriber", @@ -1251,6 +1252,15 @@ dependencies = [ "walkdir", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + [[package]] name = "nu-ansi-term" version = "0.50.1" @@ -2159,6 +2169,19 @@ dependencies = [ "syn", ] +[[package]] +name = "sysinfo" +version = "0.34.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4b93974b3d3aeaa036504b8eefd4c039dced109171c1ae973f1dc63b2c7e4b2" +dependencies = [ + "libc", + "memchr", + "ntapi", + "objc2-core-foundation", + "windows", +] + [[package]] name = "system-configuration" version = "0.6.1" @@ -2618,6 +2641,22 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.11" @@ -2627,19 +2666,58 @@ dependencies = [ "windows-sys 0.61.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" +dependencies = [ + "windows-core 0.57.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d" +dependencies = [ + "windows-implement 0.57.0", + "windows-interface 0.57.0", + "windows-result 0.1.2", + "windows-targets 0.52.6", +] + [[package]] name = "windows-core" version = "0.62.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57fe7168f7de578d2d8a05b07fd61870d2e73b4020e9f49aa00da8471723497c" dependencies = [ - "windows-implement", - "windows-interface", + "windows-implement 0.60.0", + "windows-interface 0.59.1", "windows-link 0.2.0", "windows-result 0.4.0", "windows-strings 0.5.0", ] +[[package]] +name = "windows-implement" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-implement" version = "0.60.0" @@ -2651,6 +2729,17 @@ dependencies = [ "syn", ] +[[package]] +name = "windows-interface" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-interface" version = "0.59.1" @@ -2685,6 +2774,15 @@ dependencies = [ "windows-strings 0.4.2", ] +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-result" version = "0.3.4" diff --git a/Cargo.toml b/Cargo.toml index 5581106a..3dff4a05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,5 @@ lazy_static = "1" pastey = "0.1" serde = { version = "1", features = ["derive"] } serde_json = { package = "serde_json_lenient", version = "0.2" } +sysinfo = "0.34" thiserror = "2" \ No newline at end of file diff --git a/dependencies.json b/dependencies.json index 5a21e314..b9a067e5 100644 --- a/dependencies.json +++ b/dependencies.json @@ -324,6 +324,7 @@ "strum_macros 0.27.2 registry+https://github.com/rust-lang/crates.io-index", "syn 2.0.106 registry+https://github.com/rust-lang/crates.io-index", "synstructure 0.13.2 registry+https://github.com/rust-lang/crates.io-index", + "sysinfo 0.34.2 registry+https://github.com/rust-lang/crates.io-index", "system-configuration 0.6.1 registry+https://github.com/rust-lang/crates.io-index", "system-configuration-sys 0.6.0 registry+https://github.com/rust-lang/crates.io-index", "tempfile 3.22.0 registry+https://github.com/rust-lang/crates.io-index", diff --git a/komorebi/Cargo.toml b/komorebi/Cargo.toml index 1f73abfe..faf322d8 100644 --- a/komorebi/Cargo.toml +++ b/komorebi/Cargo.toml @@ -28,6 +28,7 @@ serde = { workspace = true } serde_json = { workspace = true } serde_with = "3" strum = { version = "0.27", features = ["derive"] } +sysinfo = { workspace = true } thiserror = { workspace = true } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } \ No newline at end of file diff --git a/komorebi/src/main.rs b/komorebi/src/main.rs index ce2eb93b..75b49875 100644 --- a/komorebi/src/main.rs +++ b/komorebi/src/main.rs @@ -32,6 +32,8 @@ use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use std::sync::mpsc; +use sysinfo::Process; +use sysinfo::ProcessesToUpdate; use tracing_subscriber::EnvFilter; fn check_permissions() -> eyre::Result<()> { @@ -121,6 +123,19 @@ struct Opts { fn main() -> eyre::Result<()> { let opts: Opts = Opts::parse(); setup()?; + + let mut system = sysinfo::System::new(); + system.refresh_processes(ProcessesToUpdate::All, true); + + let matched_procs: Vec<&Process> = system.processes_by_name("komorebi".as_ref()).collect(); + + if matched_procs.len() > 1 { + tracing::error!( + "komorebi is already running, please exit the existing process before starting a new one" + ); + std::process::exit(1); + } + check_permissions()?; if !DATA_DIR.is_dir() {