tony / oxwm

Removed sus caching logic. Browse files
tonybtw committed 2025-10-13 parent 18f19a1 commit 40d6e3c2c528fa0ad1f9725b7063adbc9c1f53f3
Showing 3 changed files with 113 additions and 144 deletions. UnifiedSplit
+16 −4 justfile
@@ -15,11 +15,23 @@ uninstall:
15 clean: 15 clean:
16 cargo clean 16 cargo clean
17 17
18 +test-clean:
19 + pkill Xephyr || true
20 + rm -rf ~/.config/oxwm
21 + Xephyr -screen 1280x800 :1 & sleep 1
22 + DISPLAY=:1 cargo run --release
23 +
18 test: 24 test:
19 - pkill Xephyr || true 25 + pkill Xephyr || true
20 - rm -rf ~/.cache/oxwm 26 + rm -f ~/.config/oxwm/oxwm-user
21 - Xephyr -screen 1280x800 :1 & sleep 1 27 + Xephyr -screen 1280x800 :1 & sleep 1
22 - DISPLAY=:1 cargo run --release 28 + DISPLAY=:1 cargo run --release
29 +
30 +test-user:
31 + pkill Xephyr || true
32 + cargo run --release -- --recompile
33 + Xephyr -screen 1280x800 :1 & sleep 1
34 + DISPLAY=:1 ~/.config/oxwm/oxwm-user
23 35
24 init: 36 init:
25 cargo run -- --init 37 cargo run -- --init
+91 −120 src/bin/main.rs
@@ -1,13 +1,10 @@
1 use anyhow::Result; 1 use anyhow::Result;
2 use std::path::PathBuf; 2 use std::path::PathBuf;
3 -use std::process::Command;
4 3
5 fn main() -> Result<()> { 4 fn main() -> Result<()> {
6 let args: Vec<String> = std::env::args().collect(); 5 let args: Vec<String> = std::env::args().collect();
7 6
8 match args.get(1).map(|s| s.as_str()) { 7 match args.get(1).map(|s| s.as_str()) {
9 - Some("--init") => return init_config(),
10 - Some("--recompile") => return recompile_config(),
11 Some("--version") => { 8 Some("--version") => {
12 println!("oxwm {}", env!("CARGO_PKG_VERSION")); 9 println!("oxwm {}", env!("CARGO_PKG_VERSION"));
13 return Ok(()); 10 return Ok(());
@@ -16,65 +13,79 @@ fn main() -> Result<()> {
16 print_help(); 13 print_help();
17 return Ok(()); 14 return Ok(());
18 } 15 }
16 + Some("--init") => {
17 + init_config()?;
18 + println!("✓ Config created at ~/.config/oxwm/config.rs");
19 + println!(" Edit and reload with Mod+Shift+R");
20 + return Ok(());
21 + }
22 + Some("--recompile") => {
23 + recompile_config()?;
24 + return Ok(());
25 + }
19 _ => {} 26 _ => {}
20 } 27 }
21 28
29 + let user_binary = get_user_binary_path();
22 let config_path = get_config_path().join("config.rs"); 30 let config_path = get_config_path().join("config.rs");
23 - let cache_binary = get_cache_binary_path();
24 31
25 - if !config_path.exists() { 32 + if config_path.exists() && user_binary.exists() {
26 - init_config()?; 33 + if !should_recompile(&config_path, &user_binary)? {
27 - notify( 34 + use std::os::unix::process::CommandExt;
28 - "OXWM First Run", 35 + let err = std::process::Command::new(&user_binary)
29 - "Config created at ~/.config/oxwm/config.rs\nEdit and reload with Mod+Shift+R", 36 + .args(&args[1..])
30 - ); 37 + .exec();
31 - } else { 38 + eprintln!("Failed to exec user binary: {}", err);
32 - let config_dir = get_config_path(); 39 + std::process::exit(1);
33 - if !config_dir.join("Cargo.toml").exists() || !config_dir.join("main.rs").exists() {
34 - ensure_build_files(&config_dir)?;
35 } 40 }
36 } 41 }
37 42
38 - if !cache_binary.exists() || should_recompile(&config_path, &cache_binary)? { 43 + if !config_path.exists() {
39 - if let Err(e) = recompile_config() { 44 + eprintln!("No config found, creating default at ~/.config/oxwm/config.rs");
40 - eprintln!("\nCompilation failed: {}", e); 45 + init_config()?;
41 - eprintln!("\nYour config may be incompatible with this version of OXWM."); 46 + eprintln!("✓ Edit ~/.config/oxwm/config.rs and run 'oxwm --recompile'");
42 - eprintln!("Options:");
43 - eprintln!(" 1. Fix the errors in ~/.config/oxwm/config.rs");
44 - eprintln!(" 2. Run 'oxwm --init' to regenerate default config");
45 - eprintln!("\nBackup your config first if you have customizations!");
46 - std::process::exit(1);
47 - }
48 } 47 }
49 48
50 - use std::os::unix::process::CommandExt; 49 + let config = oxwm::Config::default();
51 - let err = Command::new(&cache_binary).args(&args[1..]).exec(); 50 + run_wm_with_config(config, &args)?;
52 - eprintln!("Failed to exec: {}", err); 51 +
53 - std::process::exit(1); 52 + Ok(())
54 } 53 }
55 54
56 -#[derive(Debug)] 55 +fn run_wm_with_config(config: oxwm::Config, args: &[String]) -> Result<()> {
57 -enum BuildMethod { 56 + let mut wm = oxwm::window_manager::WindowManager::new(config)?;
58 - NixFlake, 57 + let should_restart = wm.run()?;
59 - NixBuild, 58 +
60 - Cargo, 59 + drop(wm);
60 +
61 + if should_restart {
62 + use std::os::unix::process::CommandExt;
63 + let err = std::process::Command::new(&args[0]).args(&args[1..]).exec();
64 + eprintln!("Failed to restart: {}", err);
65 + }
66 +
67 + Ok(())
61 } 68 }
62 69
63 -fn detect_build_method() -> BuildMethod { 70 +fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> {
64 let config_dir = get_config_path(); 71 let config_dir = get_config_path();
72 + let binary_time = std::fs::metadata(binary)?.modified()?;
65 73
66 - if config_dir.join("flake.nix").exists() { 74 + let watch_files = ["config.rs", "Cargo.toml"];
67 - println!("Detected flake.nix, will use nix flake for recompilation");
68 - return BuildMethod::NixFlake;
69 - }
70 75
71 - if config_dir.join("default.nix").exists() || config_dir.join("shell.nix").exists() { 76 + for filename in &watch_files {
72 - println!("Detected nix files, will use nix-shell for recompilation"); 77 + let path = config_dir.join(filename);
73 - return BuildMethod::NixBuild; 78 + if !path.exists() {
79 + continue;
80 + }
81 +
82 + let file_time = std::fs::metadata(&path)?.modified()?;
83 + if file_time > binary_time {
84 + return Ok(true);
85 + }
74 } 86 }
75 87
76 - println!("Will use cargo for recompilation"); 88 + Ok(false)
77 - BuildMethod::Cargo
78 } 89 }
79 90
80 fn init_config() -> Result<()> { 91 fn init_config() -> Result<()> {
@@ -84,12 +95,6 @@ fn init_config() -> Result<()> {
84 let config_template = include_str!("../../templates/config.rs"); 95 let config_template = include_str!("../../templates/config.rs");
85 std::fs::write(config_dir.join("config.rs"), config_template)?; 96 std::fs::write(config_dir.join("config.rs"), config_template)?;
86 97
87 - ensure_build_files(&config_dir)?;
88 -
89 - Ok(())
90 -}
91 -
92 -fn ensure_build_files(config_dir: &PathBuf) -> Result<()> {
93 let main_template = include_str!("../../templates/main.rs"); 98 let main_template = include_str!("../../templates/main.rs");
94 std::fs::write(config_dir.join("main.rs"), main_template)?; 99 std::fs::write(config_dir.join("main.rs"), main_template)?;
95 100
@@ -126,6 +131,7 @@ pkgs.mkShell {
126 } 131 }
127 "#; 132 "#;
128 std::fs::write(config_dir.join("shell.nix"), shell_nix)?; 133 std::fs::write(config_dir.join("shell.nix"), shell_nix)?;
134 + println!("✓ Created shell.nix for NixOS");
129 } 135 }
130 136
131 Ok(()) 137 Ok(())
@@ -145,19 +151,19 @@ fn recompile_config() -> Result<()> {
145 let output = match build_method { 151 let output = match build_method {
146 BuildMethod::NixFlake => { 152 BuildMethod::NixFlake => {
147 println!("Using nix flake build..."); 153 println!("Using nix flake build...");
148 - Command::new("nix") 154 + std::process::Command::new("nix")
149 .args(&["build", ".#", "--no-link"]) 155 .args(&["build", ".#", "--no-link"])
150 .current_dir(&config_dir) 156 .current_dir(&config_dir)
151 .output()? 157 .output()?
152 } 158 }
153 BuildMethod::NixBuild => { 159 BuildMethod::NixBuild => {
154 println!("Using nix-shell..."); 160 println!("Using nix-shell...");
155 - Command::new("nix-shell") 161 + std::process::Command::new("nix-shell")
156 .args(&["--run", "cargo build --release"]) 162 .args(&["--run", "cargo build --release"])
157 .current_dir(&config_dir) 163 .current_dir(&config_dir)
158 .output()? 164 .output()?
159 } 165 }
160 - BuildMethod::Cargo => Command::new("cargo") 166 + BuildMethod::Cargo => std::process::Command::new("cargo")
161 .args(&["build", "--release"]) 167 .args(&["build", "--release"])
162 .current_dir(&config_dir) 168 .current_dir(&config_dir)
163 .output()?, 169 .output()?,
@@ -166,72 +172,49 @@ fn recompile_config() -> Result<()> {
166 if !output.status.success() { 172 if !output.status.success() {
167 let stderr = String::from_utf8_lossy(&output.stderr); 173 let stderr = String::from_utf8_lossy(&output.stderr);
168 eprintln!("\n❌ Compilation failed:\n{}", stderr); 174 eprintln!("\n❌ Compilation failed:\n{}", stderr);
169 -
170 - notify_error("OXWM Compile Error", "Check terminal for details");
171 -
172 anyhow::bail!("Failed to compile configuration"); 175 anyhow::bail!("Failed to compile configuration");
173 } 176 }
174 177
175 let source = config_dir.join("target/release/oxwm-user"); 178 let source = config_dir.join("target/release/oxwm-user");
176 - let dest = get_cache_binary_path(); 179 + let dest = get_user_binary_path();
177 180
178 std::fs::create_dir_all(dest.parent().unwrap())?; 181 std::fs::create_dir_all(dest.parent().unwrap())?;
182 + std::fs::copy(&source, &dest)?;
179 183
180 - match std::fs::copy(&source, &dest) { 184 + println!("✓ Compiled successfully to {}", dest.display());
181 - Ok(_) => {} 185 + println!(" Restart oxwm to use new config");
182 - Err(e) if e.raw_os_error() == Some(26) => {
183 - let temp_dest = dest.with_extension("new");
184 - std::fs::copy(&source, &temp_dest)?;
185 - std::fs::rename(&temp_dest, &dest)?;
186 - }
187 - Err(e) => return Err(e.into()),
188 - }
189 -
190 - println!("✓ Compiled successfully");
191 -
192 - notify(
193 - "OXWM",
194 - "Configuration recompiled! Hit Mod+Shift+R to restart.",
195 - );
196 186
197 Ok(()) 187 Ok(())
198 } 188 }
199 189
200 -fn is_nixos() -> bool { 190 +#[derive(Debug)]
201 - std::path::Path::new("/etc/NIXOS").exists() 191 +enum BuildMethod {
202 - || std::path::Path::new("/run/current-system/nixos-version").exists() 192 + NixFlake,
203 - || std::env::var("NIX_PATH").is_ok() 193 + NixBuild,
194 + Cargo,
204 } 195 }
205 196
206 -fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { 197 +fn detect_build_method() -> BuildMethod {
207 - if !config.exists() {
208 - return Ok(false);
209 - }
210 -
211 let config_dir = get_config_path(); 198 let config_dir = get_config_path();
212 - let binary_time = std::fs::metadata(binary)?.modified()?;
213 -
214 - let watch_files = [
215 - "config.rs",
216 - "main.rs",
217 - "Cargo.toml",
218 - "flake.nix",
219 - "shell.nix",
220 - ];
221 199
222 - for filename in &watch_files { 200 + if config_dir.join("flake.nix").exists() {
223 - let path = config_dir.join(filename); 201 + println!("Detected flake.nix, will use nix flake for recompilation");
224 - if !path.exists() { 202 + return BuildMethod::NixFlake;
225 - continue; 203 + }
226 - }
227 204
228 - let file_time = std::fs::metadata(&path)?.modified()?; 205 + if config_dir.join("default.nix").exists() || config_dir.join("shell.nix").exists() {
229 - if file_time > binary_time { 206 + println!("Detected nix files, will use nix-shell for recompilation");
230 - return Ok(true); 207 + return BuildMethod::NixBuild;
231 - }
232 } 208 }
233 209
234 - Ok(false) 210 + println!("Will use cargo for recompilation");
211 + BuildMethod::Cargo
212 +}
213 +
214 +fn is_nixos() -> bool {
215 + std::path::Path::new("/etc/NIXOS").exists()
216 + || std::path::Path::new("/run/current-system/nixos-version").exists()
217 + || std::env::var("NIX_PATH").is_ok()
235 } 218 }
236 219
237 fn get_config_path() -> PathBuf { 220 fn get_config_path() -> PathBuf {
@@ -240,20 +223,8 @@ fn get_config_path() -> PathBuf {
240 .join("oxwm") 223 .join("oxwm")
241 } 224 }
242 225
243 -fn get_cache_binary_path() -> PathBuf { 226 +fn get_user_binary_path() -> PathBuf {
244 - dirs::cache_dir() 227 + get_config_path().join("oxwm-user")
245 - .expect("Could not find cache directory")
246 - .join("oxwm/oxwm-binary")
247 -}
248 -
249 -fn notify(title: &str, body: &str) {
250 - let _ = Command::new("notify-send").args(&[title, body]).spawn();
251 -}
252 -
253 -fn notify_error(title: &str, body: &str) {
254 - let _ = Command::new("notify-send")
255 - .args(&["-u", "critical", title, body])
256 - .spawn();
257 } 228 }
258 229
259 fn print_help() { 230 fn print_help() {
@@ -261,12 +232,12 @@ fn print_help() {
261 println!("USAGE:"); 232 println!("USAGE:");
262 println!(" oxwm [OPTIONS]\n"); 233 println!(" oxwm [OPTIONS]\n");
263 println!("OPTIONS:"); 234 println!("OPTIONS:");
264 - println!(" --init Recreate config template in ~/.config/oxwm"); 235 + println!(" --init Create default config in ~/.config/oxwm");
265 - println!(" --recompile Manually recompile configuration"); 236 + println!(" --recompile Recompile user configuration");
266 println!(" --version Print version information"); 237 println!(" --version Print version information");
267 println!(" --help Print this help message\n"); 238 println!(" --help Print this help message\n");
268 - println!("FIRST RUN:"); 239 + println!("CONFIG:");
269 - println!(" Just run 'oxwm' - config will be auto-generated at:"); 240 + println!(" First run: Creates config at ~/.config/oxwm/config.rs");
270 - println!(" ~/.config/oxwm/config.rs"); 241 + println!(" Edit your config and run 'oxwm --recompile'");
271 - println!("\n Edit your config and reload with Mod+Shift+R\n"); 242 + println!(" Use Mod+Shift+R to hot-reload after recompiling\n");
272 } 243 }
+6 −20 src/window_manager.rs
@@ -300,13 +300,9 @@ impl WindowManager {
300 } 300 }
301 301
302 fn handle_restart(&self) -> Result<bool> { 302 fn handle_restart(&self) -> Result<bool> {
303 - if !can_recompile() { 303 + let user_binary = get_config_path().join("oxwm-user");
304 - eprintln!("Error: cargo not found. Install rust toolchain.");
305 - notify_error("OXWM", "Cannot recompile: cargo not installed");
306 - return Ok(false);
307 - }
308 304
309 - if self.needs_recompile()? { 305 + if user_binary.exists() && self.needs_recompile()? {
310 println!("Config changed, recompiling..."); 306 println!("Config changed, recompiling...");
311 self.recompile()?; 307 self.recompile()?;
312 } 308 }
@@ -316,7 +312,7 @@ impl WindowManager {
316 312
317 fn needs_recompile(&self) -> Result<bool> { 313 fn needs_recompile(&self) -> Result<bool> {
318 let config_dir = get_config_path(); 314 let config_dir = get_config_path();
319 - let binary_path = get_cache_binary_path(); 315 + let binary_path = get_user_binary_path();
320 316
321 if !binary_path.exists() { 317 if !binary_path.exists() {
322 return Ok(true); 318 return Ok(true);
@@ -360,19 +356,11 @@ impl WindowManager {
360 } 356 }
361 357
362 let source = config_dir.join("target/release/oxwm-user"); 358 let source = config_dir.join("target/release/oxwm-user");
363 - let dest = get_cache_binary_path(); 359 + let dest = get_user_binary_path();
364 - let backup = dest.with_extension("old");
365 360
366 std::fs::create_dir_all(dest.parent().unwrap())?; 361 std::fs::create_dir_all(dest.parent().unwrap())?;
367 -
368 - if dest.exists() {
369 - std::fs::rename(&dest, &backup)?;
370 - }
371 -
372 std::fs::copy(&source, &dest)?; 362 std::fs::copy(&source, &dest)?;
373 363
374 - let _ = std::fs::remove_file(&backup);
375 -
376 notify("OXWM", "Recompiled successfully! Restarting..."); 364 notify("OXWM", "Recompiled successfully! Restarting...");
377 365
378 Ok(()) 366 Ok(())
@@ -943,10 +931,8 @@ fn get_config_path() -> PathBuf {
943 .join("oxwm") 931 .join("oxwm")
944 } 932 }
945 933
946 -fn get_cache_binary_path() -> PathBuf { 934 +fn get_user_binary_path() -> PathBuf {
947 - dirs::cache_dir() 935 + get_config_path().join("oxwm-user")
948 - .expect("Could not find cache directory")
949 - .join("oxwm/oxwm-binary")
950 } 936 }
951 937
952 fn can_recompile() -> bool { 938 fn can_recompile() -> bool {