tony / oxwm

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