tony / oxwm

Branch: 1a68569
Switch branches/tags
master
tonybanters - Event loop now responsive (16ms vs 100ms) - Geometry caching eliminates network overhead - Code duplication eliminated - Error handling consistent and visible 1947670 2025-11-17
161 lines · 5.4 KB Raw Blame History
1 use std::path::PathBuf;
2
3 fn main() -> Result<(), Box<dyn std::error::Error>> {
4 let arguments: Vec<String> = std::env::args().collect();
5
6 let mut custom_config_path: Option<PathBuf> = None;
7
8 match arguments.get(1).map(|string| string.as_str()) {
9 Some("--version") => {
10 println!("oxwm {}", env!("CARGO_PKG_VERSION"));
11 return Ok(());
12 }
13 Some("--help") => {
14 print_help();
15 return Ok(());
16 }
17 Some("--init") => {
18 init_config()?;
19 return Ok(());
20 }
21 Some("--config") => {
22 if let Some(path) = arguments.get(2) {
23 custom_config_path = Some(PathBuf::from(path));
24 } else {
25 eprintln!("Error: --config requires a path argument");
26 std::process::exit(1);
27 }
28 }
29 _ => {}
30 }
31
32 let (config, had_broken_config) = load_config(custom_config_path)?;
33
34 let mut window_manager = oxwm::window_manager::WindowManager::new(config)?;
35
36 if had_broken_config {
37 window_manager.show_migration_overlay();
38 }
39
40 let should_restart = window_manager.run()?;
41
42 drop(window_manager);
43
44 if should_restart {
45 use std::os::unix::process::CommandExt;
46 let error = std::process::Command::new(&arguments[0]).args(&arguments[1..]).exec();
47 eprintln!("Failed to restart: {}", error);
48 }
49
50 Ok(())
51 }
52
53 fn load_config(custom_path: Option<PathBuf>) -> Result<(oxwm::Config, bool), Box<dyn std::error::Error>> {
54 let config_path = if let Some(path) = custom_path {
55 path
56 } else {
57 let config_directory = get_config_path();
58 let lua_path = config_directory.join("config.lua");
59
60 if !lua_path.exists() {
61 let ron_path = config_directory.join("config.ron");
62 let had_ron_config = ron_path.exists();
63
64 println!("No config found at {:?}", config_directory);
65 println!("Creating default Lua config...");
66 init_config()?;
67
68 if had_ron_config {
69 println!("\n NOTICE: OXWM has migrated to Lua configuration.");
70 println!(" Your old config.ron has been preserved, but is no longer used.");
71 println!(" Your settings have been reset to defaults.");
72 println!(" Please manually port your configuration to the new Lua format.");
73 println!(" See the new config.lua template for examples.\n");
74 }
75 } else {
76 update_lsp_files()?;
77 }
78
79 lua_path
80 };
81
82 let config_string = std::fs::read_to_string(&config_path)
83 .map_err(|error| format!("Failed to read config file: {}", error))?;
84
85 let config_directory = config_path.parent();
86
87 match oxwm::config::parse_lua_config(&config_string, config_directory) {
88 Ok(config) => Ok((config, false)),
89 Err(_error) => {
90 let template = include_str!("../../templates/config.lua");
91 let config = oxwm::config::parse_lua_config(template, None)
92 .map_err(|error| format!("Failed to parse default template config: {}", error))?;
93 Ok((config, true))
94 }
95 }
96 }
97
98 fn init_config() -> Result<(), Box<dyn std::error::Error>> {
99 let config_directory = get_config_path();
100 std::fs::create_dir_all(&config_directory)?;
101
102 let config_template = include_str!("../../templates/config.lua");
103 let config_path = config_directory.join("config.lua");
104 std::fs::write(&config_path, config_template)?;
105
106 update_lsp_files()?;
107
108 println!("✓ Config created at {:?}", config_path);
109 println!("✓ LSP definitions installed at {:?}/lib/oxwm.lua", config_directory);
110 println!(" Edit the file and reload with Mod+Shift+R");
111 println!(" No compilation needed - changes take effect immediately!");
112
113 Ok(())
114 }
115
116 fn update_lsp_files() -> Result<(), Box<dyn std::error::Error>> {
117 let config_directory = get_config_path();
118
119 let library_directory = config_directory.join("lib");
120 std::fs::create_dir_all(&library_directory)?;
121
122 let oxwm_lua_template = include_str!("../../templates/oxwm.lua");
123 let oxwm_lua_path = library_directory.join("oxwm.lua");
124 std::fs::write(&oxwm_lua_path, oxwm_lua_template)?;
125
126 let luarc_content = r#"{
127 "workspace.library": [
128 "lib"
129 ]
130 }
131 "#;
132 let luarc_path = config_directory.join(".luarc.json");
133 std::fs::write(&luarc_path, luarc_content)?;
134
135 Ok(())
136 }
137
138 fn get_config_path() -> PathBuf {
139 dirs::config_dir()
140 .expect("Could not find config directory")
141 .join("oxwm")
142 }
143
144 fn print_help() {
145 println!("OXWM - A dynamic window manager written in Rust\n");
146 println!("USAGE:");
147 println!(" oxwm [OPTIONS]\n");
148 println!("OPTIONS:");
149 println!(" --init Create default config in ~/.config/oxwm/config.lua");
150 println!(" --config <PATH> Use custom config file");
151 println!(" --version Print version information");
152 println!(" --help Print this help message\n");
153 println!("CONFIG:");
154 println!(" Location: ~/.config/oxwm/config.lua");
155 println!(" Edit the config file and use Mod+Shift+R to reload");
156 println!(" No compilation needed - instant hot-reload!");
157 println!(" LSP support included with oxwm.lua type definitions\n");
158 println!("FIRST RUN:");
159 println!(" Run 'oxwm --init' to create a config file");
160 println!(" Or just start oxwm and it will create one automatically\n");
161 }