| 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!("No configuration found."); |
| 36 | eprintln!("Run: oxwm --init"); |
| 37 | eprintln!("Then add 'exec oxwm' to your ~/.xinitrc"); |
| 38 | std::process::exit(1); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn init_config() -> Result<()> { |
| 43 | let config_dir = get_config_path(); |
| 44 | std::fs::create_dir_all(&config_dir)?; |
| 45 | |
| 46 | if config_dir.join("config.rs").exists() { |
| 47 | eprintln!("Config already exists at ~/.config/oxwm/config.rs"); |
| 48 | eprintln!("Remove it first if you want to reinitialize."); |
| 49 | return Ok(()); |
| 50 | } |
| 51 | |
| 52 | let config_template = include_str!("../../templates/config.rs"); |
| 53 | std::fs::write(config_dir.join("config.rs"), config_template)?; |
| 54 | |
| 55 | let main_template = include_str!("../../templates/main.rs"); |
| 56 | std::fs::write(config_dir.join("main.rs"), main_template)?; |
| 57 | |
| 58 | let cargo_toml = r#"[package] |
| 59 | name = "oxwm-user" |
| 60 | version = "0.1.0" |
| 61 | edition = "2024" |
| 62 | |
| 63 | [dependencies] |
| 64 | oxwm = { git = "https://github.com/tonybanters/oxwm" } |
| 65 | anyhow = "1" |
| 66 | |
| 67 | [[bin]] |
| 68 | name = "oxwm-user" |
| 69 | path = "main.rs" |
| 70 | "#; |
| 71 | |
| 72 | std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?; |
| 73 | |
| 74 | std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?; |
| 75 | |
| 76 | println!("✓ Created ~/.config/oxwm/config.rs"); |
| 77 | println!("✓ Created ~/.config/oxwm/main.rs"); |
| 78 | println!("✓ Edit ~/.config/oxwm/config.rs to customize your setup"); |
| 79 | |
| 80 | println!("\nCompiling initial configuration..."); |
| 81 | recompile_config()?; |
| 82 | |
| 83 | println!("\n✓ Setup complete!"); |
| 84 | println!(" Add 'exec oxwm' to your ~/.xinitrc"); |
| 85 | println!(" Reload config anytime with Mod+Shift+R"); |
| 86 | |
| 87 | Ok(()) |
| 88 | } |
| 89 | |
| 90 | fn recompile_config() -> Result<()> { |
| 91 | let config_dir = get_config_path(); |
| 92 | |
| 93 | if !config_dir.join("config.rs").exists() { |
| 94 | anyhow::bail!("No config found. Run: oxwm --init"); |
| 95 | } |
| 96 | |
| 97 | println!("Compiling oxwm configuration..."); |
| 98 | |
| 99 | let output = Command::new("cargo") |
| 100 | .args(&["build", "--release"]) |
| 101 | .current_dir(&config_dir) |
| 102 | .output()?; |
| 103 | |
| 104 | if !output.status.success() { |
| 105 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 106 | eprintln!("\n❌ Compilation failed:\n{}", stderr); |
| 107 | |
| 108 | let _ = Command::new("notify-send") |
| 109 | .args(&[ |
| 110 | "-u", |
| 111 | "critical", |
| 112 | "OXWM Compile Error", |
| 113 | "Check terminal for details", |
| 114 | ]) |
| 115 | .spawn(); |
| 116 | |
| 117 | anyhow::bail!("Failed to compile configuration"); |
| 118 | } |
| 119 | |
| 120 | let source = config_dir.join("target/release/oxwm-user"); |
| 121 | let dest = get_cache_binary_path(); |
| 122 | |
| 123 | std::fs::create_dir_all(dest.parent().unwrap())?; |
| 124 | std::fs::copy(&source, &dest)?; |
| 125 | |
| 126 | println!("✓ Compiled successfully"); |
| 127 | |
| 128 | let _ = Command::new("notify-send") |
| 129 | .args(&[ |
| 130 | "OXWM", |
| 131 | "Configuration recompiled! Hit Mod+Shift+R to restart.", |
| 132 | ]) |
| 133 | .spawn(); |
| 134 | |
| 135 | Ok(()) |
| 136 | } |
| 137 | |
| 138 | fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> { |
| 139 | if !config.exists() { |
| 140 | return Ok(false); |
| 141 | } |
| 142 | |
| 143 | let config_dir = get_config_path(); |
| 144 | let binary_time = std::fs::metadata(binary)?.modified()?; |
| 145 | |
| 146 | let watch_files = ["config.rs", "main.rs", "Cargo.toml"]; |
| 147 | |
| 148 | for filename in &watch_files { |
| 149 | let path = config_dir.join(filename); |
| 150 | if !path.exists() { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | let file_time = std::fs::metadata(&path)?.modified()?; |
| 155 | if file_time > binary_time { |
| 156 | return Ok(true); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | Ok(false) |
| 161 | } |
| 162 | |
| 163 | fn get_config_path() -> PathBuf { |
| 164 | dirs::config_dir() |
| 165 | .expect("Could not find config directory") |
| 166 | .join("oxwm") |
| 167 | } |
| 168 | |
| 169 | fn get_cache_binary_path() -> PathBuf { |
| 170 | dirs::cache_dir() |
| 171 | .expect("Could not find cache directory") |
| 172 | .join("oxwm/oxwm-binary") |
| 173 | } |
| 174 | |
| 175 | fn print_help() { |
| 176 | println!("OXWM - A dynamic window manager written in Rust\n"); |
| 177 | println!("USAGE:"); |
| 178 | println!(" oxwm [OPTIONS]\n"); |
| 179 | println!("OPTIONS:"); |
| 180 | println!(" --init Initialize user configuration in ~/.config/oxwm"); |
| 181 | println!(" --recompile Recompile user configuration"); |
| 182 | println!(" --version Print version information"); |
| 183 | println!(" --help Print this help message\n"); |
| 184 | println!("SETUP:"); |
| 185 | println!(" 1. Run 'oxwm --init' to create your config"); |
| 186 | println!(" 2. Edit ~/.config/oxwm/config.rs"); |
| 187 | println!(" 3. Add 'exec oxwm' to your ~/.xinitrc"); |
| 188 | println!(" 4. Start X with 'startx'\n"); |
| 189 | println!("CONFIGURATION:"); |
| 190 | println!(" Config location: ~/.config/oxwm/config.rs"); |
| 191 | println!(" Reload hotkey: Mod+Shift+R (auto-recompiles if needed)"); |
| 192 | } |