|
|
@@ -1,13 +1,10 @@ |
| 1 |
1 |
use anyhow::Result; |
| 2 |
2 |
use std::path::PathBuf; |
| 3 |
|
-use std::process::Command; |
| 4 |
3 |
|
| 5 |
4 |
fn main() -> Result<()> { |
| 6 |
5 |
let args: Vec<String> = std::env::args().collect(); |
| 7 |
6 |
|
| 8 |
7 |
match args.get(1).map(|s| s.as_str()) { |
| 9 |
|
- Some("--init") => return init_config(), |
| 10 |
|
- Some("--recompile") => return recompile_config(), |
| 11 |
8 |
Some("--version") => { |
| 12 |
9 |
println!("oxwm {}", env!("CARGO_PKG_VERSION")); |
| 13 |
10 |
return Ok(()); |
|
|
@@ -16,65 +13,79 @@ fn main() -> Result<()> { |
| 16 |
13 |
print_help(); |
| 17 |
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 |
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() { |
| 26 |
|
- init_config()?; |
| 27 |
|
- notify( |
| 28 |
|
- "OXWM First Run", |
| 29 |
|
- "Config created at ~/.config/oxwm/config.rs\nEdit and reload with Mod+Shift+R", |
| 30 |
|
- ); |
| 31 |
|
- } else { |
| 32 |
|
- let config_dir = get_config_path(); |
| 33 |
|
- if !config_dir.join("Cargo.toml").exists() || !config_dir.join("main.rs").exists() { |
| 34 |
|
- ensure_build_files(&config_dir)?; |
|
32 |
+ if config_path.exists() && user_binary.exists() { |
|
33 |
+ if !should_recompile(&config_path, &user_binary)? { |
|
34 |
+ use std::os::unix::process::CommandExt; |
|
35 |
+ let err = std::process::Command::new(&user_binary) |
|
36 |
+ .args(&args[1..]) |
|
37 |
+ .exec(); |
|
38 |
+ eprintln!("Failed to exec user binary: {}", err); |
|
39 |
+ std::process::exit(1); |
| 35 |
40 |
} |
| 36 |
41 |
} |
| 37 |
42 |
|
| 38 |
|
- if !cache_binary.exists() || should_recompile(&config_path, &cache_binary)? { |
| 39 |
|
- if let Err(e) = recompile_config() { |
| 40 |
|
- eprintln!("\nCompilation failed: {}", e); |
| 41 |
|
- eprintln!("\nYour config may be incompatible with this version of OXWM."); |
| 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 |
|
- } |
|
43 |
+ if !config_path.exists() { |
|
44 |
+ eprintln!("No config found, creating default at ~/.config/oxwm/config.rs"); |
|
45 |
+ init_config()?; |
|
46 |
+ eprintln!("✓ Edit ~/.config/oxwm/config.rs and run 'oxwm --recompile'"); |
| 48 |
47 |
} |
| 49 |
48 |
|
| 50 |
|
- use std::os::unix::process::CommandExt; |
| 51 |
|
- let err = Command::new(&cache_binary).args(&args[1..]).exec(); |
| 52 |
|
- eprintln!("Failed to exec: {}", err); |
| 53 |
|
- std::process::exit(1); |
|
49 |
+ let config = oxwm::Config::default(); |
|
50 |
+ run_wm_with_config(config, &args)?; |
|
51 |
+ |
|
52 |
+ Ok(()) |
| 54 |
53 |
} |
| 55 |
54 |
|
| 56 |
|
-#[derive(Debug)] |
| 57 |
|
-enum BuildMethod { |
| 58 |
|
- NixFlake, |
| 59 |
|
- NixBuild, |
| 60 |
|
- Cargo, |
|
55 |
+fn run_wm_with_config(config: oxwm::Config, args: &[String]) -> Result<()> { |
|
56 |
+ let mut wm = oxwm::window_manager::WindowManager::new(config)?; |
|
57 |
+ let should_restart = wm.run()?; |
|
58 |
+ |
|
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 |
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() { |
| 67 |
|
- println!("Detected flake.nix, will use nix flake for recompilation"); |
| 68 |
|
- return BuildMethod::NixFlake; |
| 69 |
|
- } |
|
74 |
+ let watch_files = ["config.rs", "Cargo.toml"]; |
| 70 |
75 |
|
| 71 |
|
- if config_dir.join("default.nix").exists() || config_dir.join("shell.nix").exists() { |
| 72 |
|
- println!("Detected nix files, will use nix-shell for recompilation"); |
| 73 |
|
- return BuildMethod::NixBuild; |
|
76 |
+ for filename in &watch_files { |
|
77 |
+ let path = config_dir.join(filename); |
|
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"); |
| 77 |
|
- BuildMethod::Cargo |
|
88 |
+ Ok(false) |
| 78 |
89 |
} |
| 79 |
90 |
|
| 80 |
91 |
fn init_config() -> Result<()> { |
|
|
@@ -84,12 +95,6 @@ fn init_config() -> Result<()> { |
| 84 |
95 |
let config_template = include_str!("../../templates/config.rs"); |
| 85 |
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 |
98 |
let main_template = include_str!("../../templates/main.rs"); |
| 94 |
99 |
std::fs::write(config_dir.join("main.rs"), main_template)?; |
| 95 |
100 |
|
|
|
@@ -126,6 +131,7 @@ pkgs.mkShell { |
| 126 |
131 |
} |
| 127 |
132 |
"#; |
| 128 |
133 |
std::fs::write(config_dir.join("shell.nix"), shell_nix)?; |
|
134 |
+ println!("✓ Created shell.nix for NixOS"); |
| 129 |
135 |
} |
| 130 |
136 |
|
| 131 |
137 |
Ok(()) |
|
|
@@ -145,19 +151,19 @@ fn recompile_config() -> Result<()> { |
| 145 |
151 |
let output = match build_method { |
| 146 |
152 |
BuildMethod::NixFlake => { |
| 147 |
153 |
println!("Using nix flake build..."); |
| 148 |
|
- Command::new("nix") |
|
154 |
+ std::process::Command::new("nix") |
| 149 |
155 |
.args(&["build", ".#", "--no-link"]) |
| 150 |
156 |
.current_dir(&config_dir) |
| 151 |
157 |
.output()? |
| 152 |
158 |
} |
| 153 |
159 |
BuildMethod::NixBuild => { |
| 154 |
160 |
println!("Using nix-shell..."); |
| 155 |
|
- Command::new("nix-shell") |
|
161 |
+ std::process::Command::new("nix-shell") |
| 156 |
162 |
.args(&["--run", "cargo build --release"]) |
| 157 |
163 |
.current_dir(&config_dir) |
| 158 |
164 |
.output()? |
| 159 |
165 |
} |
| 160 |
|
- BuildMethod::Cargo => Command::new("cargo") |
|
166 |
+ BuildMethod::Cargo => std::process::Command::new("cargo") |
| 161 |
167 |
.args(&["build", "--release"]) |
| 162 |
168 |
.current_dir(&config_dir) |
| 163 |
169 |
.output()?, |
|
|
@@ -166,72 +172,49 @@ fn recompile_config() -> Result<()> { |
| 166 |
172 |
if !output.status.success() { |
| 167 |
173 |
let stderr = String::from_utf8_lossy(&output.stderr); |
| 168 |
174 |
eprintln!("\n❌ Compilation failed:\n{}", stderr); |
| 169 |
|
- |
| 170 |
|
- notify_error("OXWM Compile Error", "Check terminal for details"); |
| 171 |
|
- |
| 172 |
175 |
anyhow::bail!("Failed to compile configuration"); |
| 173 |
176 |
} |
| 174 |
177 |
|
| 175 |
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 |
181 |
std::fs::create_dir_all(dest.parent().unwrap())?; |
|
182 |
+ std::fs::copy(&source, &dest)?; |
| 179 |
183 |
|
| 180 |
|
- match std::fs::copy(&source, &dest) { |
| 181 |
|
- Ok(_) => {} |
| 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 |
|
- ); |
|
184 |
+ println!("✓ Compiled successfully to {}", dest.display()); |
|
185 |
+ println!(" Restart oxwm to use new config"); |
| 196 |
186 |
|
| 197 |
187 |
Ok(()) |
| 198 |
188 |
} |
| 199 |
189 |
|
| 200 |
|
-fn is_nixos() -> bool { |
| 201 |
|
- std::path::Path::new("/etc/NIXOS").exists() |
| 202 |
|
- || std::path::Path::new("/run/current-system/nixos-version").exists() |
| 203 |
|
- || std::env::var("NIX_PATH").is_ok() |
|
190 |
+#[derive(Debug)] |
|
191 |
+enum BuildMethod { |
|
192 |
+ NixFlake, |
|
193 |
+ NixBuild, |
|
194 |
+ Cargo, |
| 204 |
195 |
} |
| 205 |
196 |
|
| 206 |
|
-fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { |
| 207 |
|
- if !config.exists() { |
| 208 |
|
- return Ok(false); |
| 209 |
|
- } |
| 210 |
|
- |
|
197 |
+fn detect_build_method() -> BuildMethod { |
| 211 |
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 { |
| 223 |
|
- let path = config_dir.join(filename); |
| 224 |
|
- if !path.exists() { |
| 225 |
|
- continue; |
| 226 |
|
- } |
|
200 |
+ if config_dir.join("flake.nix").exists() { |
|
201 |
+ println!("Detected flake.nix, will use nix flake for recompilation"); |
|
202 |
+ return BuildMethod::NixFlake; |
|
203 |
+ } |
| 227 |
204 |
|
| 228 |
|
- let file_time = std::fs::metadata(&path)?.modified()?; |
| 229 |
|
- if file_time > binary_time { |
| 230 |
|
- return Ok(true); |
| 231 |
|
- } |
|
205 |
+ if config_dir.join("default.nix").exists() || config_dir.join("shell.nix").exists() { |
|
206 |
+ println!("Detected nix files, will use nix-shell for recompilation"); |
|
207 |
+ return BuildMethod::NixBuild; |
| 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 |
220 |
fn get_config_path() -> PathBuf { |
|
|
@@ -240,20 +223,8 @@ fn get_config_path() -> PathBuf { |
| 240 |
223 |
.join("oxwm") |
| 241 |
224 |
} |
| 242 |
225 |
|
| 243 |
|
-fn get_cache_binary_path() -> PathBuf { |
| 244 |
|
- dirs::cache_dir() |
| 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(); |
|
226 |
+fn get_user_binary_path() -> PathBuf { |
|
227 |
+ get_config_path().join("oxwm-user") |
| 257 |
228 |
} |
| 258 |
229 |
|
| 259 |
230 |
fn print_help() { |
|
|
@@ -261,12 +232,12 @@ fn print_help() { |
| 261 |
232 |
println!("USAGE:"); |
| 262 |
233 |
println!(" oxwm [OPTIONS]\n"); |
| 263 |
234 |
println!("OPTIONS:"); |
| 264 |
|
- println!(" --init Recreate config template in ~/.config/oxwm"); |
| 265 |
|
- println!(" --recompile Manually recompile configuration"); |
|
235 |
+ println!(" --init Create default config in ~/.config/oxwm"); |
|
236 |
+ println!(" --recompile Recompile user configuration"); |
| 266 |
237 |
println!(" --version Print version information"); |
| 267 |
238 |
println!(" --help Print this help message\n"); |
| 268 |
|
- println!("FIRST RUN:"); |
| 269 |
|
- println!(" Just run 'oxwm' - config will be auto-generated at:"); |
| 270 |
|
- println!(" ~/.config/oxwm/config.rs"); |
| 271 |
|
- println!("\n Edit your config and reload with Mod+Shift+R\n"); |
|
239 |
+ println!("CONFIG:"); |
|
240 |
+ println!(" First run: Creates config at ~/.config/oxwm/config.rs"); |
|
241 |
+ println!(" Edit your config and run 'oxwm --recompile'"); |
|
242 |
+ println!(" Use Mod+Shift+R to hot-reload after recompiling\n"); |
| 272 |
243 |
} |