tony / oxwm

Branch: 06b24c8
Switch branches/tags
master
tonybanters removed unneeded functions, fixed recompile function (again?) 23924cf 2025-10-13
229 lines · 6.2 KB Raw Blame History
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 config_dir = get_config_path();
30 let user_bin = config_dir.join("target/release/oxwm-user");
31
32 if user_bin.exists() {
33 use std::os::unix::process::CommandExt;
34 let err = std::process::Command::new(&user_bin)
35 .args(&args[1..])
36 .exec();
37 eprintln!("Failed to exec user binary: {}", err);
38 std::process::exit(1);
39 }
40
41 let config = oxwm::Config::default();
42 run_wm_with_config(config, &args)?;
43
44 Ok(())
45 }
46
47 fn run_wm_with_config(config: oxwm::Config, args: &[String]) -> Result<()> {
48 let mut wm = oxwm::window_manager::WindowManager::new(config)?;
49 let should_restart = wm.run()?;
50
51 drop(wm);
52
53 if should_restart {
54 use std::os::unix::process::CommandExt;
55 let err = std::process::Command::new(&args[0]).args(&args[1..]).exec();
56 eprintln!("Failed to restart: {}", err);
57 }
58
59 Ok(())
60 }
61
62 fn should_recompile(config: &PathBuf, binary: &PathBuf) -> Result<bool> {
63 let config_dir = get_config_path();
64 let binary_time = std::fs::metadata(binary)?.modified()?;
65
66 let watch_files = ["config.rs", "Cargo.toml"];
67
68 for filename in &watch_files {
69 let path = config_dir.join(filename);
70 if !path.exists() {
71 continue;
72 }
73
74 let file_time = std::fs::metadata(&path)?.modified()?;
75 if file_time > binary_time {
76 return Ok(true);
77 }
78 }
79
80 Ok(false)
81 }
82
83 fn init_config() -> Result<()> {
84 let config_dir = get_config_path();
85 std::fs::create_dir_all(&config_dir)?;
86
87 let config_template = include_str!("../../templates/config.rs");
88 std::fs::write(config_dir.join("config.rs"), config_template)?;
89
90 let main_template = include_str!("../../templates/main.rs");
91 std::fs::write(config_dir.join("main.rs"), main_template)?;
92
93 let cargo_toml = r#"[package]
94 name = "oxwm-user"
95 version = "0.1.0"
96 edition = "2024"
97
98 [dependencies]
99 oxwm = { git = "https://github.com/tonybanters/oxwm" }
100 anyhow = "1"
101
102 [[bin]]
103 name = "oxwm-user"
104 path = "main.rs"
105 "#;
106
107 std::fs::write(config_dir.join("Cargo.toml"), cargo_toml)?;
108 std::fs::write(config_dir.join(".gitignore"), "target/\nCargo.lock\n")?;
109
110 if is_nixos() {
111 let shell_nix = r#"{ pkgs ? import <nixpkgs> {} }:
112 pkgs.mkShell {
113 buildInputs = with pkgs; [
114 rustc
115 cargo
116 pkg-config
117 xorg.libX11
118 xorg.libXft
119 xorg.libXrender
120 freetype
121 fontconfig
122 ];
123 }
124 "#;
125 std::fs::write(config_dir.join("shell.nix"), shell_nix)?;
126 println!("āœ“ Created shell.nix for NixOS");
127 }
128
129 Ok(())
130 }
131
132 fn recompile_config() -> Result<()> {
133 let config_dir = get_config_path();
134
135 if !config_dir.join("config.rs").exists() {
136 anyhow::bail!("No config found. Run: oxwm --init");
137 }
138
139 println!("Compiling oxwm configuration...");
140
141 let build_method = detect_build_method();
142
143 let output = match build_method {
144 BuildMethod::NixFlake => {
145 println!("Using nix flake build...");
146 std::process::Command::new("nix")
147 .args(&["build", ".#", "--no-link"])
148 .current_dir(&config_dir)
149 .output()?
150 }
151 BuildMethod::NixBuild => {
152 println!("Using nix-shell...");
153 std::process::Command::new("nix-shell")
154 .args(&["--run", "cargo build --release"])
155 .current_dir(&config_dir)
156 .output()?
157 }
158 BuildMethod::Cargo => std::process::Command::new("cargo")
159 .args(&["build", "--release"])
160 .current_dir(&config_dir)
161 .output()?,
162 };
163
164 if !output.status.success() {
165 let stderr = String::from_utf8_lossy(&output.stderr);
166 eprintln!("\nāŒ Compilation failed:\n{}", stderr);
167 anyhow::bail!("Failed to compile configuration");
168 }
169
170 println!("āœ“ Compiled successfully.");
171 println!(" Restart oxwm to use new config");
172
173 Ok(())
174 }
175
176 #[derive(Debug)]
177 enum BuildMethod {
178 NixFlake,
179 NixBuild,
180 Cargo,
181 }
182
183 fn detect_build_method() -> BuildMethod {
184 let config_dir = get_config_path();
185
186 if config_dir.join("flake.nix").exists() {
187 println!("Detected flake.nix, will use nix flake for recompilation");
188 return BuildMethod::NixFlake;
189 }
190
191 if config_dir.join("default.nix").exists() || config_dir.join("shell.nix").exists() {
192 println!("Detected nix files, will use nix-shell for recompilation");
193 return BuildMethod::NixBuild;
194 }
195
196 println!("Will use cargo for recompilation");
197 BuildMethod::Cargo
198 }
199
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()
204 }
205
206 fn get_config_path() -> PathBuf {
207 dirs::config_dir()
208 .expect("Could not find config directory")
209 .join("oxwm")
210 }
211
212 fn get_user_binary_path() -> PathBuf {
213 get_config_path().join("oxwm-user")
214 }
215
216 fn print_help() {
217 println!("OXWM - A dynamic window manager written in Rust\n");
218 println!("USAGE:");
219 println!(" oxwm [OPTIONS]\n");
220 println!("OPTIONS:");
221 println!(" --init Create default config in ~/.config/oxwm");
222 println!(" --recompile Recompile user configuration");
223 println!(" --version Print version information");
224 println!(" --help Print this help message\n");
225 println!("CONFIG:");
226 println!(" First run: Creates config at ~/.config/oxwm/config.rs");
227 println!(" Edit your config and run 'oxwm --recompile'");
228 println!(" Use Mod+Shift+R to hot-reload after recompiling\n");
229 }