tony / oxwm

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