tony / oxwm

Branch: 506050a
Switch branches/tags
master
Scott DeJong Setting config.path to enable hot-reload of changed config file 506050a 2025-12-18
196 lines · 6.4 KB Raw Blame History
1 use std::path::Path;
2 use std::path::PathBuf;
3
4 static CONFIG_FILE: &str = "config.lua";
5 static TEMPLATE: &str = include_str!("../../templates/config.lua");
6
7 enum Args {
8 Exit,
9 Arguments(Vec<String>),
10 Error(String),
11 }
12
13 fn main() {
14 let arguments = match process_args() {
15 Args::Exit => return,
16 Args::Arguments(v) => v,
17 Args::Error(e) => panic!("Error: Could not get valid arguments:\n{}", e),
18 };
19
20 let (config, had_broken_config) = match load_config(arguments.get(2)) {
21 Ok((c, hbc)) => (c, hbc),
22 Err(e) => panic!("Error: Could not load config:\n{}", e),
23 };
24
25 let mut window_manager = match oxwm::window_manager::WindowManager::new(config) {
26 Ok(wm) => wm,
27 Err(e) => panic!("Error: Could not start window manager:\n{}", e),
28 };
29
30 if had_broken_config {
31 window_manager.show_migration_overlay();
32 }
33
34 let should_restart = match window_manager.run() {
35 Ok(sr) => sr,
36 Err(e) => panic!("Error: Could not determine restart\n{}", e),
37 };
38
39 drop(window_manager);
40
41 if should_restart {
42 use std::os::unix::process::CommandExt;
43 let error = std::process::Command::new(&arguments[0])
44 .args(&arguments[1..])
45 .exec();
46 eprintln!("Error: Failed to restart: {}", error);
47 }
48 }
49
50 fn load_config(
51 config_path: Option<&String>,
52 ) -> Result<(oxwm::Config, bool), Box<dyn std::error::Error>> {
53 let path = match config_path {
54 None => {
55 let config_path = get_config_path().join(CONFIG_FILE);
56 check_convert(&config_path)
57 .map_err(|error| format!("Error: Failed to check old config:\n{}", error))?;
58 config_path
59 }
60 Some(p) => PathBuf::from(p),
61 };
62
63 let config_string = std::fs::read_to_string(&path)
64 .map_err(|error| format!("Error: Failed to read config file:\n{}", error))?;
65
66 let config_directory = path.parent();
67
68 let (mut config, had_broken_config) =
69 match oxwm::config::parse_lua_config(&config_string, config_directory) {
70 Ok(config) => (config, false),
71 Err(_error) => {
72 let config = oxwm::config::parse_lua_config(TEMPLATE, None).map_err(|error| {
73 format!("Error: Failed to parse default template config:\n{}", error)
74 })?;
75 (config, true)
76 }
77 };
78 config.path = Some(path);
79 Ok((config, had_broken_config))
80 }
81
82 fn init_config() -> Result<(), Box<dyn std::error::Error>> {
83 let config_directory = get_config_path();
84 std::fs::create_dir_all(&config_directory)?;
85
86 let config_template = TEMPLATE;
87 let config_path = config_directory.join(CONFIG_FILE);
88 std::fs::write(&config_path, config_template)?;
89
90 println!("✓ Config created at {:?}", config_path);
91 println!(" Edit the file and reload with Mod+Shift+R");
92 println!(" No compilation needed - changes take effect immediately!");
93
94 Ok(())
95 }
96
97 fn get_config_path() -> PathBuf {
98 dirs::config_dir()
99 .expect("Could not find config directory")
100 .join("oxwm")
101 }
102
103 fn print_help() {
104 println!("OXWM - A dynamic window manager written in Rust\n");
105 println!("USAGE:");
106 println!(" oxwm [OPTIONS]\n");
107 println!("OPTIONS:");
108 println!(" --init Create default config in ~/.config/oxwm/config.lua");
109 println!(" --config <PATH> Use custom config file");
110 println!(" --version Print version information");
111 println!(" --help Print this help message\n");
112 println!("CONFIG:");
113 println!(" Location: ~/.config/oxwm/config.lua");
114 println!(" Edit the config file and use Mod+Shift+R to reload");
115 println!(" No compilation needed - instant hot-reload!");
116 println!(" LSP support included with oxwm.lua type definitions\n");
117 println!("FIRST RUN:");
118 println!(" Run 'oxwm --init' to create a config file");
119 println!(" Or just start oxwm and it will create one automatically\n");
120 }
121
122 fn process_args() -> Args {
123 let mut args = std::env::args();
124 let name = match args.next() {
125 Some(n) => n,
126 None => return Args::Error("Error: Program name can't be extracted from args".to_string()),
127 };
128 let switch = args.next();
129 let path = args.next();
130
131 let switch = match switch {
132 Some(s) => s,
133 None => return Args::Arguments(vec![name]),
134 };
135
136 match switch.as_str() {
137 "--version" => {
138 println!("{name} {}", env!("CARGO_PKG_VERSION"));
139 Args::Exit
140 }
141 "--help" => {
142 print_help();
143 Args::Exit
144 }
145 "--init" => match init_config() {
146 Ok(_) => Args::Exit,
147 Err(e) => Args::Error(format!("Error: Failed to create default config:\n{e}")),
148 },
149 "--config" => match check_custom_config(path) {
150 Ok(p) => Args::Arguments(vec![name, switch, p]),
151 Err(e) => Args::Error(e),
152 },
153 _ => Args::Error(format!("Error: {switch} is an unknown argument")),
154 }
155 }
156
157 fn check_custom_config(path: Option<String>) -> Result<String, String> {
158 let path = match path {
159 Some(p) => p,
160 None => {
161 return Err("Error: --config requires a valid path argument".to_string());
162 }
163 };
164
165 match std::fs::exists(&path) {
166 Ok(b) => match b {
167 true => Ok(path),
168 false => Err(format!("Error: {path} does not exist")),
169 },
170 Err(e) => Err(format!("Error: Failed to check config exists:\n{e}")),
171 }
172 }
173
174 fn check_convert(path: &Path) -> Result<(), &str> {
175 let config_directory = get_config_path();
176
177 if !path.exists() {
178 let ron_path = config_directory.join("config.ron");
179 let had_ron_config = ron_path.exists();
180
181 println!("No config found at {:?}", config_directory);
182 println!("Creating default Lua config...");
183 if init_config().is_err() {
184 return Err("Error: Failed to create default lua");
185 }
186
187 if had_ron_config {
188 println!("\n NOTICE: OXWM has migrated to Lua configuration.");
189 println!(" Your old config.ron has been preserved, but is no longer used.");
190 println!(" Your settings have been reset to defaults.");
191 println!(" Please manually port your configuration to the new Lua format.");
192 println!(" See the new config.lua template for examples.\n");
193 }
194 }
195 Ok(())
196 }