| 1 | use anyhow::Result; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | fn main() -> Result<()> { |
| 5 | let args: Vec<String> = std::env::args().collect(); |
| 6 | |
| 7 | match args.get(1).map(|s| s.as_str()) { |
| 8 | Some("--version") => { |
| 9 | println!("oxwm {}", env!("CARGO_PKG_VERSION")); |
| 10 | return Ok(()); |
| 11 | } |
| 12 | Some("--help") => { |
| 13 | print_help(); |
| 14 | return Ok(()); |
| 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 | } |
| 26 | _ => {} |
| 27 | } |
| 28 | |
| 29 | let user_binary = get_user_binary_path(); |
| 30 | let config_path = get_config_path().join("config.rs"); |
| 31 | |
| 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); |
| 40 | } |
| 41 | } |
| 42 | |
| 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'"); |
| 47 | } |
| 48 | |
| 49 | let config = oxwm::Config::default(); |
| 50 | run_wm_with_config(config, &args)?; |
| 51 | |
| 52 | Ok(()) |
| 53 | } |
| 54 | |
| 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(()) |
| 68 | } |
| 69 | |
| 70 | fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { |
| 71 | let config_dir = get_config_path(); |
| 72 | let binary_time = std::fs::metadata(binary)?.modified()?; |
| 73 | |
| 74 | let watch_files = ["config.rs", "Cargo.toml"]; |
| 75 | |
| 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 | } |
| 86 | } |
| 87 | |
| 88 | Ok(false) |
| 89 | } |
| 90 | |
| 91 | fn init_config() -> Result<()> { |
| 92 | let config_dir = get_config_path(); |
| 93 | std::fs::create_dir_all(&config_dir)?; |
| 94 | |
| 95 | let config_template = include_str!("../../templates/config.rs"); |
| 96 | std::fs::write(config_dir.join("config.rs"), config_template)?; |
| 97 | |
| 98 | let main_template = include_str!("../../templates/main.rs"); |
| 99 | std::fs::write(config_dir.join("main.rs"), main_template)?; |
| 100 | |
| 101 | let cargo_toml = r#"[package] |
| 102 | name = "oxwm-user" |
| 103 | version = "0.1.0" |
| 104 | edition = "2024" |
| 105 | |
| 106 | [dependencies] |
| 107 | oxwm = { git = "https://github.com/tonybanters/oxwm" } |
| 108 | anyhow = "1" |
| 109 | |
| 110 | [[bin]] |
| 111 | name = "oxwm-user" |
| 112 | path = "main.rs" |
| 113 | "#; |
| 114 | |
| 115 | std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?; |
| 116 | std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?; |
| 117 | |
| 118 | if is_nixos() { |
| 119 | let shell_nix = r#"{ pkgs ? import <nixpkgs> {} }: |
| 120 | pkgs.mkShell { |
| 121 | buildInputs = with pkgs; [ |
| 122 | rustc |
| 123 | cargo |
| 124 | pkg-config |
| 125 | xorg.libX11 |
| 126 | xorg.libXft |
| 127 | xorg.libXrender |
| 128 | freetype |
| 129 | fontconfig |
| 130 | ]; |
| 131 | } |
| 132 | "#; |
| 133 | std::fs::write(config_dir.join("shell.nix"), shell_nix)?; |
| 134 | println!("✓ Created shell.nix for NixOS"); |
| 135 | } |
| 136 | |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
| 140 | fn recompile_config() -> Result<()> { |
| 141 | let config_dir = get_config_path(); |
| 142 | |
| 143 | if !config_dir.join("config.rs").exists() { |
| 144 | anyhow::bail!("No config found. Run: oxwm --init"); |
| 145 | } |
| 146 | |
| 147 | println!("Compiling oxwm configuration..."); |
| 148 | |
| 149 | let build_method = detect_build_method(); |
| 150 | |
| 151 | let output = match build_method { |
| 152 | BuildMethod::NixFlake => { |
| 153 | println!("Using nix flake build..."); |
| 154 | std::process::Command::new("nix") |
| 155 | .args(&["build", ".#", "--no-link"]) |
| 156 | .current_dir(&config_dir) |
| 157 | .output()? |
| 158 | } |
| 159 | BuildMethod::NixBuild => { |
| 160 | println!("Using nix-shell..."); |
| 161 | std::process::Command::new("nix-shell") |
| 162 | .args(&["--run", "cargo build --release"]) |
| 163 | .current_dir(&config_dir) |
| 164 | .output()? |
| 165 | } |
| 166 | BuildMethod::Cargo => std::process::Command::new("cargo") |
| 167 | .args(&["build", "--release"]) |
| 168 | .current_dir(&config_dir) |
| 169 | .output()?, |
| 170 | }; |
| 171 | |
| 172 | if !output.status.success() { |
| 173 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 174 | eprintln!("\n❌ Compilation failed:\n{}", stderr); |
| 175 | anyhow::bail!("Failed to compile configuration"); |
| 176 | } |
| 177 | |
| 178 | let source = config_dir.join("target/release/oxwm-user"); |
| 179 | let dest = get_user_binary_path(); |
| 180 | |
| 181 | std::fs::create_dir_all(dest.parent().unwrap())?; |
| 182 | std::fs::copy(&source, &dest)?; |
| 183 | |
| 184 | println!("✓ Compiled successfully to {}", dest.display()); |
| 185 | println!(" Restart oxwm to use new config"); |
| 186 | |
| 187 | Ok(()) |
| 188 | } |
| 189 | |
| 190 | #[derive(Debug)] |
| 191 | enum BuildMethod { |
| 192 | NixFlake, |
| 193 | NixBuild, |
| 194 | Cargo, |
| 195 | } |
| 196 | |
| 197 | fn detect_build_method() -> BuildMethod { |
| 198 | let config_dir = get_config_path(); |
| 199 | |
| 200 | if config_dir.join("flake.nix").exists() { |
| 201 | println!("Detected flake.nix, will use nix flake for recompilation"); |
| 202 | return BuildMethod::NixFlake; |
| 203 | } |
| 204 | |
| 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; |
| 208 | } |
| 209 | |
| 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() |
| 218 | } |
| 219 | |
| 220 | fn get_config_path() -> PathBuf { |
| 221 | dirs::config_dir() |
| 222 | .expect("Could not find config directory") |
| 223 | .join("oxwm") |
| 224 | } |
| 225 | |
| 226 | fn get_user_binary_path() -> PathBuf { |
| 227 | get_config_path().join("oxwm-user") |
| 228 | } |
| 229 | |
| 230 | fn print_help() { |
| 231 | println!("OXWM - A dynamic window manager written in Rust\n"); |
| 232 | println!("USAGE:"); |
| 233 | println!(" oxwm [OPTIONS]\n"); |
| 234 | println!("OPTIONS:"); |
| 235 | println!(" --init Create default config in ~/.config/oxwm"); |
| 236 | println!(" --recompile Recompile user configuration"); |
| 237 | println!(" --version Print version information"); |
| 238 | println!(" --help Print this help message\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"); |
| 243 | } |