tony / oxwm

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