tony / oxwm

Branch: d5f7b65
Switch branches/tags
master
tonybtw Fixed zombie processes d5f7b65 2025-12-12
136 lines · 4.7 KB Raw Blame History
1 use std::path::PathBuf;
2
3 fn main() -> Result<(), Box<dyn std::error::Error>> {
4 oxwm::signal::prevent_zombie_processes();
5
6 let arguments: Vec<String> = std::env::args().collect();
7
8 let mut custom_config_path: Option<PathBuf> = None;
9
10 match arguments.get(1).map(|string| string.as_str()) {
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 Some("--init") => {
20 init_config()?;
21 return Ok(());
22 }
23 Some("--config") => {
24 if let Some(path) = arguments.get(2) {
25 custom_config_path = Some(PathBuf::from(path));
26 } else {
27 eprintln!("Error: --config requires a path argument");
28 std::process::exit(1);
29 }
30 }
31 _ => {}
32 }
33
34 let (config, had_broken_config) = load_config(custom_config_path)?;
35
36 let mut window_manager = oxwm::window_manager::WindowManager::new(config)?;
37
38 if had_broken_config {
39 window_manager.show_migration_overlay();
40 }
41
42 let should_restart = window_manager.run()?;
43
44 drop(window_manager);
45
46 if should_restart {
47 use std::os::unix::process::CommandExt;
48 let error = std::process::Command::new(&arguments[0]).args(&arguments[1..]).exec();
49 eprintln!("Failed to restart: {}", error);
50 }
51
52 Ok(())
53 }
54
55 fn load_config(custom_path: Option<PathBuf>) -> Result<(oxwm::Config, bool), Box<dyn std::error::Error>> {
56 let config_path = if let Some(path) = custom_path {
57 path
58 } else {
59 let config_directory = get_config_path();
60 let lua_path = config_directory.join("config.lua");
61
62 if !lua_path.exists() {
63 let ron_path = config_directory.join("config.ron");
64 let had_ron_config = ron_path.exists();
65
66 println!("No config found at {:?}", config_directory);
67 println!("Creating default Lua config...");
68 init_config()?;
69
70 if had_ron_config {
71 println!("\n NOTICE: OXWM has migrated to Lua configuration.");
72 println!(" Your old config.ron has been preserved, but is no longer used.");
73 println!(" Your settings have been reset to defaults.");
74 println!(" Please manually port your configuration to the new Lua format.");
75 println!(" See the new config.lua template for examples.\n");
76 }
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 println!("✓ Config created at {:?}", config_path);
107 println!(" Edit the file and reload with Mod+Shift+R");
108 println!(" No compilation needed - changes take effect immediately!");
109
110 Ok(())
111 }
112
113 fn get_config_path() -> PathBuf {
114 dirs::config_dir()
115 .expect("Could not find config directory")
116 .join("oxwm")
117 }
118
119 fn print_help() {
120 println!("OXWM - A dynamic window manager written in Rust\n");
121 println!("USAGE:");
122 println!(" oxwm [OPTIONS]\n");
123 println!("OPTIONS:");
124 println!(" --init Create default config in ~/.config/oxwm/config.lua");
125 println!(" --config <PATH> Use custom config file");
126 println!(" --version Print version information");
127 println!(" --help Print this help message\n");
128 println!("CONFIG:");
129 println!(" Location: ~/.config/oxwm/config.lua");
130 println!(" Edit the config file and use Mod+Shift+R to reload");
131 println!(" No compilation needed - instant hot-reload!");
132 println!(" LSP support included with oxwm.lua type definitions\n");
133 println!("FIRST RUN:");
134 println!(" Run 'oxwm --init' to create a config file");
135 println!(" Or just start oxwm and it will create one automatically\n");
136 }