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