| 1 | use anyhow::Result; |
| 2 | use std::path::PathBuf; |
| 3 | use std::process::Command; |
| 4 | |
| 5 | fn main() -> Result<()> { |
| 6 | let args: Vec<String> = std::env::args().collect(); |
| 7 | |
| 8 | match args.get(1).map(|s| s.as_str()) { |
| 9 | Some("--init") => return init_config(), |
| 10 | Some("--recompile") => return recompile_config(), |
| 11 | Some("--version") => { |
| 12 | println!("oxwm {}", env!("CARGO_PKG_VERSION")); |
| 13 | return Ok(()); |
| 14 | } |
| 15 | Some("--help") => { |
| 16 | print_help(); |
| 17 | return Ok(()); |
| 18 | } |
| 19 | _ => {} |
| 20 | } |
| 21 | |
| 22 | let cache_binary = get_cache_binary_path(); |
| 23 | |
| 24 | if cache_binary.exists() { |
| 25 | let config_path = get_config_path().join("config.rs"); |
| 26 | if should_recompile(&config_path, &cache_binary)? { |
| 27 | println!("Config changed, recompiling..."); |
| 28 | recompile_config()?; |
| 29 | } |
| 30 | |
| 31 | use std::os::unix::process::CommandExt; |
| 32 | let err = Command::new(&cache_binary).args(&args[1..]).exec(); |
| 33 | anyhow::bail!("Failed to exec user binary: {}", err); |
| 34 | } else { |
| 35 | eprintln!("╔════════════════════════════════════════╗"); |
| 36 | eprintln!("║ OXWM: Running with default config ║"); |
| 37 | eprintln!("╚════════════════════════════════════════╝"); |
| 38 | eprintln!(); |
| 39 | eprintln!("ℹ️ Run 'oxwm --init' to create a custom config"); |
| 40 | eprintln!(); |
| 41 | |
| 42 | let config = oxwm::Config::default(); |
| 43 | |
| 44 | let mut wm = oxwm::window_manager::WindowManager::new(config)?; |
| 45 | let should_restart = wm.run()?; |
| 46 | |
| 47 | drop(wm); |
| 48 | |
| 49 | if should_restart { |
| 50 | use std::os::unix::process::CommandExt; |
| 51 | let err = Command::new(&args[0]).args(&args[1..]).exec(); |
| 52 | eprintln!("Failed to restart: {}", err); |
| 53 | } |
| 54 | |
| 55 | Ok(()) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | #[derive(Debug)] |
| 60 | enum BuildMethod { |
| 61 | NixFlake, |
| 62 | NixBuild, |
| 63 | Cargo, |
| 64 | } |
| 65 | |
| 66 | fn detect_build_method() -> BuildMethod { |
| 67 | let config_dir = get_config_path(); |
| 68 | |
| 69 | if config_dir.join("flake.nix").exists() { |
| 70 | println!("Detected flake.nix, will use nix flake for recompilation"); |
| 71 | return BuildMethod::NixFlake; |
| 72 | } |
| 73 | |
| 74 | if config_dir.join("default.nix").exists() || config_dir.join("shell.nix").exists() { |
| 75 | println!("Detected nix files, will use nix-shell for recompilation"); |
| 76 | return BuildMethod::NixBuild; |
| 77 | } |
| 78 | |
| 79 | println!("Will use cargo for recompilation"); |
| 80 | BuildMethod::Cargo |
| 81 | } |
| 82 | |
| 83 | fn init_config() -> Result<()> { |
| 84 | let config_dir = get_config_path(); |
| 85 | std::fs::create_dir_all(&config_dir)?; |
| 86 | |
| 87 | if config_dir.join("config.rs").exists() { |
| 88 | eprintln!("Config already exists at ~/.config/oxwm/config.rs"); |
| 89 | eprintln!("Remove it first if you want to reinitialize."); |
| 90 | return Ok(()); |
| 91 | } |
| 92 | |
| 93 | let config_template = include_str!("../../templates/config.rs"); |
| 94 | std::fs::write(config_dir.join("config.rs"), config_template)?; |
| 95 | |
| 96 | let main_template = include_str!("../../templates/main.rs"); |
| 97 | std::fs::write(config_dir.join("main.rs"), main_template)?; |
| 98 | |
| 99 | let cargo_toml = r#"[package] |
| 100 | name = "oxwm-user" |
| 101 | version = "0.1.0" |
| 102 | edition = "2024" |
| 103 | |
| 104 | [dependencies] |
| 105 | oxwm = { git = "https://github.com/tonybanters/oxwm" } |
| 106 | anyhow = "1" |
| 107 | |
| 108 | [[bin]] |
| 109 | name = "oxwm-user" |
| 110 | path = "main.rs" |
| 111 | "#; |
| 112 | |
| 113 | std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?; |
| 114 | std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?; |
| 115 | |
| 116 | if is_nixos() { |
| 117 | let shell_nix = r#"{ pkgs ? import <nixpkgs> {} }: |
| 118 | pkgs.mkShell { |
| 119 | buildInputs = with pkgs; [ |
| 120 | rustc |
| 121 | cargo |
| 122 | pkg-config |
| 123 | xorg.libX11 |
| 124 | xorg.libXft |
| 125 | xorg.libXrender |
| 126 | freetype |
| 127 | fontconfig |
| 128 | ]; |
| 129 | } |
| 130 | "#; |
| 131 | std::fs::write(config_dir.join("shell.nix"), shell_nix)?; |
| 132 | println!("✓ Created shell.nix for NixOS"); |
| 133 | } |
| 134 | |
| 135 | println!("✓ Created ~/.config/oxwm/config.rs"); |
| 136 | println!("✓ Created ~/.config/oxwm/main.rs"); |
| 137 | println!("✓ Edit ~/.config/oxwm/config.rs to customize your setup"); |
| 138 | |
| 139 | println!("\nCompiling initial configuration..."); |
| 140 | recompile_config()?; |
| 141 | |
| 142 | println!("\n✓ Setup complete!"); |
| 143 | println!(" Your custom config is now active"); |
| 144 | println!(" Reload config anytime with Mod+Shift+R"); |
| 145 | |
| 146 | Ok(()) |
| 147 | } |
| 148 | |
| 149 | fn recompile_config() -> Result<()> { |
| 150 | let config_dir = get_config_path(); |
| 151 | |
| 152 | if !config_dir.join("config.rs").exists() { |
| 153 | anyhow::bail!("No config found. Run: oxwm --init"); |
| 154 | } |
| 155 | |
| 156 | println!("Compiling oxwm configuration..."); |
| 157 | |
| 158 | let build_method = detect_build_method(); |
| 159 | |
| 160 | let output = match build_method { |
| 161 | BuildMethod::NixFlake => { |
| 162 | println!("Using nix flake build..."); |
| 163 | Command::new("nix") |
| 164 | .args(&["build", ".#", "--no-link"]) |
| 165 | .current_dir(&config_dir) |
| 166 | .output()? |
| 167 | } |
| 168 | BuildMethod::NixBuild => { |
| 169 | println!("Using nix-shell..."); |
| 170 | Command::new("nix-shell") |
| 171 | .args(&["--run", "cargo build --release"]) |
| 172 | .current_dir(&config_dir) |
| 173 | .output()? |
| 174 | } |
| 175 | BuildMethod::Cargo => Command::new("cargo") |
| 176 | .args(&["build", "--release"]) |
| 177 | .current_dir(&config_dir) |
| 178 | .output()?, |
| 179 | }; |
| 180 | |
| 181 | if !output.status.success() { |
| 182 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 183 | eprintln!("\n❌ Compilation failed:\n{}", stderr); |
| 184 | |
| 185 | let _ = Command::new("notify-send") |
| 186 | .args(&[ |
| 187 | "-u", |
| 188 | "critical", |
| 189 | "OXWM Compile Error", |
| 190 | "Check terminal for details", |
| 191 | ]) |
| 192 | .spawn(); |
| 193 | |
| 194 | anyhow::bail!("Failed to compile configuration"); |
| 195 | } |
| 196 | |
| 197 | let source = config_dir.join("target/release/oxwm-user"); |
| 198 | let dest = get_cache_binary_path(); |
| 199 | |
| 200 | std::fs::create_dir_all(dest.parent().unwrap())?; |
| 201 | |
| 202 | match std::fs::copy(&source, &dest) { |
| 203 | Ok(_) => {} |
| 204 | Err(e) if e.raw_os_error() == Some(26) => { |
| 205 | let temp_dest = dest.with_extension("new"); |
| 206 | std::fs::copy(&source, &temp_dest)?; |
| 207 | std::fs::rename(&temp_dest, &dest)?; |
| 208 | } |
| 209 | Err(e) => return Err(e.into()), |
| 210 | } |
| 211 | |
| 212 | println!("✓ Compiled successfully"); |
| 213 | |
| 214 | let _ = Command::new("notify-send") |
| 215 | .args(&[ |
| 216 | "OXWM", |
| 217 | "Configuration recompiled! Hit Mod+Shift+R to restart.", |
| 218 | ]) |
| 219 | .spawn(); |
| 220 | |
| 221 | Ok(()) |
| 222 | } |
| 223 | |
| 224 | fn is_nixos() -> bool { |
| 225 | std::path::Path::new("/etc/NIXOS").exists() |
| 226 | || std::path::Path::new("/run/current-system/nixos-version").exists() |
| 227 | || std::env::var("NIX_PATH").is_ok() |
| 228 | } |
| 229 | |
| 230 | fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { |
| 231 | if !config.exists() { |
| 232 | return Ok(false); |
| 233 | } |
| 234 | |
| 235 | let config_dir = get_config_path(); |
| 236 | let binary_time = std::fs::metadata(binary)?.modified()?; |
| 237 | |
| 238 | let watch_files = [ |
| 239 | "config.rs", |
| 240 | "main.rs", |
| 241 | "Cargo.toml", |
| 242 | "flake.nix", |
| 243 | "shell.nix", |
| 244 | ]; |
| 245 | |
| 246 | for filename in &watch_files { |
| 247 | let path = config_dir.join(filename); |
| 248 | if !path.exists() { |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | let file_time = std::fs::metadata(&path)?.modified()?; |
| 253 | if file_time > binary_time { |
| 254 | return Ok(true); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | Ok(false) |
| 259 | } |
| 260 | |
| 261 | fn get_config_path() -> PathBuf { |
| 262 | dirs::config_dir() |
| 263 | .expect("Could not find config directory") |
| 264 | .join("oxwm") |
| 265 | } |
| 266 | |
| 267 | fn get_cache_binary_path() -> PathBuf { |
| 268 | dirs::cache_dir() |
| 269 | .expect("Could not find cache directory") |
| 270 | .join("oxwm/oxwm-binary") |
| 271 | } |
| 272 | |
| 273 | fn print_help() { |
| 274 | println!("OXWM - A dynamic window manager written in Rust\n"); |
| 275 | println!("USAGE:"); |
| 276 | println!(" oxwm [OPTIONS]\n"); |
| 277 | println!("OPTIONS:"); |
| 278 | println!(" --init Initialize user configuration in ~/.config/oxwm"); |
| 279 | println!(" --recompile Recompile user configuration"); |
| 280 | println!(" --version Print version information"); |
| 281 | println!(" --help Print this help message\n"); |
| 282 | println!("CONFIGURATION:"); |
| 283 | println!(" Without --init: Runs with built-in defaults"); |
| 284 | println!(" With --init: Uses custom config from ~/.config/oxwm/config.rs"); |
| 285 | println!(" Reload hotkey: Mod+Shift+R (auto-recompiles if needed)\n"); |
| 286 | println!("SETUP:"); |
| 287 | println!(" 1. Add 'exec oxwm' to your ~/.xinitrc (works immediately with defaults)"); |
| 288 | println!(" 2. Optionally run 'oxwm --init' to create custom config"); |
| 289 | println!(" 3. Edit ~/.config/oxwm/config.rs to customize"); |
| 290 | println!(" 4. Restart with Mod+Shift+R\n"); |
| 291 | println!("ADVANCED:"); |
| 292 | println!(" Create flake.nix or shell.nix in ~/.config/oxwm to use nix builds"); |
| 293 | } |