Added proper error handling in blocks, and font. added error modal so config syntax error doesnt crash when you reload your oxwm, added keybind overlay modal to show the important keybinds (inspired by niri), moved keysms helper function into const file to reduce code duplication.
Browse files
+1 −0 resources/test-config.lua
| @@ -61,6 +61,7 @@ return { | ||
| 61 | 61 | { modifiers = { "Mod1" }, key = "D", action = "Spawn", arg = { "sh", "-c", "dmenu_run -l 10" } }, |
| 62 | 62 | { modifiers = { "Mod1" }, key = "S", action = "Spawn", arg = { "sh", "-c", "maim -s | xclip -selection clipboard -t image/png" } }, |
| 63 | 63 | { modifiers = { "Mod1" }, key = "Q", action = "KillClient" }, |
| 64 | + { modifiers = { "Mod1", "Shift" }, key = "Slash", action = "ShowKeybindOverlay" }, | |
| 64 | 65 | { modifiers = { "Mod1", "Shift" }, key = "F", action = "ToggleFullScreen" }, |
| 65 | 66 | { modifiers = { "Mod1", "Shift" }, key = "Space", action = "ToggleFloating" }, |
| 66 | 67 | { modifiers = { "Mod1" }, key = "F", action = "ChangeLayout", arg = "normie" }, |
+5 −5 src/bar/blocks/battery.rs
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::Block; |
| 2 | -use anyhow::Result; | |
| 2 | +use crate::errors::BlockError; | |
| 3 | 3 | use std::fs; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| @@ -30,22 +30,22 @@ impl Battery { | ||
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - fn read_file(&self, filename: &str) -> Result<String> { | |
| 33 | + fn read_file(&self, filename: &str) -> Result<String, BlockError> { | |
| 34 | 34 | let path = format!("{}/{}", self.battery_path, filename); |
| 35 | 35 | Ok(fs::read_to_string(path)?.trim().to_string()) |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - fn get_capacity(&self) -> Result<u32> { | |
| 38 | + fn get_capacity(&self) -> Result<u32, BlockError> { | |
| 39 | 39 | Ok(self.read_file("capacity")?.parse()?) |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - fn get_status(&self) -> Result<String> { | |
| 42 | + fn get_status(&self) -> Result<String, BlockError> { | |
| 43 | 43 | self.read_file("status") |
| 44 | 44 | } |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | impl Block for Battery { |
| 48 | - fn content(&mut self) -> Result<String> { | |
| 48 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 49 | 49 | let capacity = self.get_capacity()?; |
| 50 | 50 | let status = self.get_status()?; |
| 51 | 51 |
+2 −2 src/bar/blocks/datetime.rs
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::Block; |
| 2 | -use anyhow::Result; | |
| 2 | +use crate::errors::BlockError; | |
| 3 | 3 | use chrono::Local; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| @@ -22,7 +22,7 @@ impl DateTime { | ||
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | impl Block for DateTime { |
| 25 | - fn content(&mut self) -> Result<String> { | |
| 25 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 26 | 26 | let now = Local::now(); |
| 27 | 27 | let time_str = now.format(&self.time_format).to_string(); |
| 28 | 28 | Ok(self.format_template.replace("{}", &time_str)) |
+3 −3 src/bar/blocks/mod.rs
| @@ -1,4 +1,4 @@ | ||
| 1 | -use anyhow::Result; | |
| 1 | +use crate::errors::BlockError; | |
| 2 | 2 | use std::time::Duration; |
| 3 | 3 | |
| 4 | 4 | mod battery; |
| @@ -12,7 +12,7 @@ use ram::Ram; | ||
| 12 | 12 | use shell::ShellBlock; |
| 13 | 13 | |
| 14 | 14 | pub trait Block { |
| 15 | - fn content(&mut self) -> Result<String>; | |
| 15 | + fn content(&mut self) -> Result<String, BlockError>; | |
| 16 | 16 | fn interval(&self) -> Duration; |
| 17 | 17 | fn color(&self) -> u32; |
| 18 | 18 | } |
| @@ -89,7 +89,7 @@ impl StaticBlock { | ||
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | impl Block for StaticBlock { |
| 92 | - fn content(&mut self) -> Result<String> { | |
| 92 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 93 | 93 | Ok(self.text.clone()) |
| 94 | 94 | } |
| 95 | 95 |
+3 −3 src/bar/blocks/ram.rs
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::Block; |
| 2 | -use anyhow::Result; | |
| 2 | +use crate::errors::BlockError; | |
| 3 | 3 | use std::fs; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| @@ -18,7 +18,7 @@ impl Ram { | ||
| 18 | 18 | } |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - fn get_memory_info(&self) -> Result<(u64, u64, f32)> { | |
| 21 | + fn get_memory_info(&self) -> Result<(u64, u64, f32), BlockError> { | |
| 22 | 22 | let meminfo = fs::read_to_string("/proc/meminfo")?; |
| 23 | 23 | let mut total: u64 = 0; |
| 24 | 24 | let mut available: u64 = 0; |
| @@ -51,7 +51,7 @@ impl Ram { | ||
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | impl Block for Ram { |
| 54 | - fn content(&mut self) -> Result<String> { | |
| 54 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 55 | 55 | let (used, total, percentage) = self.get_memory_info()?; |
| 56 | 56 | |
| 57 | 57 | let used_gb = used as f32 / 1024.0 / 1024.0; |
+14 −3 src/bar/blocks/shell.rs
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::Block; |
| 2 | -use anyhow::Result; | |
| 2 | +use crate::errors::BlockError; | |
| 3 | 3 | use std::process::Command; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| @@ -22,8 +22,19 @@ impl ShellBlock { | ||
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | impl Block for ShellBlock { |
| 25 | - fn content(&mut self) -> Result<String> { | |
| 26 | - let output = Command::new("sh").arg("-c").arg(&self.command).output()?; | |
| 25 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 26 | + let output = Command::new("sh") | |
| 27 | + .arg("-c") | |
| 28 | + .arg(&self.command) | |
| 29 | + .output() | |
| 30 | + .map_err(|e| BlockError::CommandFailed(format!("Failed to execute command: {}", e)))?; | |
| 31 | + | |
| 32 | + if !output.status.success() { | |
| 33 | + return Err(BlockError::CommandFailed(format!( | |
| 34 | + "Command exited with status: {}", | |
| 35 | + output.status | |
| 36 | + ))); | |
| 37 | + } | |
| 27 | 38 | |
| 28 | 39 | let result = String::from_utf8_lossy(&output.stdout).trim().to_string(); |
| 29 | 40 | Ok(self.format.replace("{}", &result)) |
+7 −0 src/bar/font.rs
| @@ -124,6 +124,13 @@ impl FontDraw { | ||
| 124 | 124 | ); |
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | + | |
| 128 | + pub fn flush(&self) { | |
| 129 | + unsafe { | |
| 130 | + let display = x11::xft::XftDrawDisplay(self.xft_draw); | |
| 131 | + x11::xlib::XFlush(display); | |
| 132 | + } | |
| 133 | + } | |
| 127 | 134 | } |
| 128 | 135 | |
| 129 | 136 | impl Drop for FontDraw { |
+10 −8 src/bin/main.rs
| @@ -55,7 +55,6 @@ fn load_config(custom_path: Option<PathBuf>) -> Result<oxwm::Config> { | ||
| 55 | 55 | let config_path = if let Some(path) = custom_path { |
| 56 | 56 | path |
| 57 | 57 | } else { |
| 58 | - // Try to find config.lua first, then config.ron | |
| 59 | 58 | let config_dir = get_config_path(); |
| 60 | 59 | let lua_path = config_dir.join("config.lua"); |
| 61 | 60 | let ron_path = config_dir.join("config.ron"); |
| @@ -75,7 +74,6 @@ fn load_config(custom_path: Option<PathBuf>) -> Result<oxwm::Config> { | ||
| 75 | 74 | let config_str = |
| 76 | 75 | std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?; |
| 77 | 76 | |
| 78 | - // Determine config format based on file extension | |
| 79 | 77 | let is_lua = config_path |
| 80 | 78 | .extension() |
| 81 | 79 | .and_then(|s| s.to_str()) |
| @@ -83,7 +81,9 @@ fn load_config(custom_path: Option<PathBuf>) -> Result<oxwm::Config> { | ||
| 83 | 81 | .unwrap_or(false); |
| 84 | 82 | |
| 85 | 83 | if is_lua { |
| 86 | - oxwm::config::parse_lua_config(&config_str).with_context(|| "Failed to parse Lua config") | |
| 84 | + let config_dir = config_path.parent(); | |
| 85 | + oxwm::config::parse_lua_config(&config_str, config_dir) | |
| 86 | + .with_context(|| "Failed to parse Lua config") | |
| 87 | 87 | } else { |
| 88 | 88 | oxwm::config::parse_config(&config_str).with_context(|| "Failed to parse RON config") |
| 89 | 89 | } |
| @@ -115,7 +115,6 @@ fn migrate_config(custom_path: Option<PathBuf>) -> Result<()> { | ||
| 115 | 115 | let ron_path = if let Some(path) = custom_path { |
| 116 | 116 | path |
| 117 | 117 | } else { |
| 118 | - // Default to ~/.config/oxwm/config.ron | |
| 119 | 118 | get_config_path().join("config.ron") |
| 120 | 119 | }; |
| 121 | 120 | |
| @@ -132,9 +131,8 @@ fn migrate_config(custom_path: Option<PathBuf>) -> Result<()> { | ||
| 132 | 131 | .with_context(|| format!("Failed to read RON config from {:?}", ron_path))?; |
| 133 | 132 | |
| 134 | 133 | let lua_content = oxwm::config::migrate::ron_to_lua(&ron_content) |
| 135 | - .map_err(|e| anyhow::anyhow!("Migration failed: {}", e))?; | |
| 134 | + .with_context(|| "Failed to migrate RON config to Lua")?; | |
| 136 | 135 | |
| 137 | - // Determine output path (same directory, .lua extension) | |
| 138 | 136 | let lua_path = ron_path.with_extension("lua"); |
| 139 | 137 | |
| 140 | 138 | std::fs::write(&lua_path, lua_content) |
| @@ -143,7 +141,9 @@ fn migrate_config(custom_path: Option<PathBuf>) -> Result<()> { | ||
| 143 | 141 | println!("✓ Migration complete!"); |
| 144 | 142 | println!(" Output: {:?}", lua_path); |
| 145 | 143 | println!("\nYour old config.ron is still intact."); |
| 146 | - println!("Review the new config.lua and then you can delete config.ron if everything looks good."); | |
| 144 | + println!( | |
| 145 | + "Review the new config.lua and then you can delete config.ron if everything looks good." | |
| 146 | + ); | |
| 147 | 147 | |
| 148 | 148 | Ok(()) |
| 149 | 149 | } |
| @@ -154,7 +154,9 @@ fn print_help() { | ||
| 154 | 154 | println!(" oxwm [OPTIONS]\n"); |
| 155 | 155 | println!("OPTIONS:"); |
| 156 | 156 | println!(" --init Create default config in ~/.config/oxwm/config.lua"); |
| 157 | - println!(" --migrate [PATH] Convert RON config to Lua (default: ~/.config/oxwm/config.ron)"); | |
| 157 | + println!( | |
| 158 | + " --migrate [PATH] Convert RON config to Lua (default: ~/.config/oxwm/config.ron)" | |
| 159 | + ); | |
| 158 | 160 | println!(" --config <PATH> Use custom config file (.lua or .ron)"); |
| 159 | 161 | println!(" --version Print version information"); |
| 160 | 162 | println!(" --help Print this help message\n"); |
+15 −2 src/config/lua.rs
| @@ -7,9 +7,21 @@ use crate::{ColorScheme, LayoutSymbolOverride}; | ||
| 7 | 7 | use mlua::{Lua, Table, Value}; |
| 8 | 8 | use x11rb::protocol::xproto::KeyButMask; |
| 9 | 9 | |
| 10 | -pub fn parse_lua_config(input: &str) -> Result<crate::Config, ConfigError> { | |
| 10 | +pub fn parse_lua_config( | |
| 11 | + input: &str, | |
| 12 | + config_dir: Option<&std::path::Path>, | |
| 13 | +) -> Result<crate::Config, ConfigError> { | |
| 11 | 14 | let lua = Lua::new(); |
| 12 | 15 | |
| 16 | + if let Some(dir) = config_dir { | |
| 17 | + if let Some(dir_str) = dir.to_str() { | |
| 18 | + let setup_code = format!("package.path = '{}/?.lua;' .. package.path", dir_str); | |
| 19 | + lua.load(&setup_code) | |
| 20 | + .exec() | |
| 21 | + .map_err(|e| ConfigError::LuaError(format!("Failed to set package.path: {}", e)))?; | |
| 22 | + } | |
| 23 | + } | |
| 24 | + | |
| 13 | 25 | let config: Table = lua |
| 14 | 26 | .load(input) |
| 15 | 27 | .eval() |
| @@ -364,6 +376,7 @@ fn string_to_key_action(s: &str) -> Result<KeyAction, ConfigError> { | ||
| 364 | 376 | "FocusMonitor" => KeyAction::FocusMonitor, |
| 365 | 377 | "SmartMoveWin" => KeyAction::SmartMoveWin, |
| 366 | 378 | "ExchangeClient" => KeyAction::ExchangeClient, |
| 379 | + "ShowKeybindOverlay" => KeyAction::ShowKeybindOverlay, | |
| 367 | 380 | "None" => KeyAction::None, |
| 368 | 381 | _ => return Err(ConfigError::UnknownAction(s.to_string())), |
| 369 | 382 | }; |
| @@ -560,7 +573,7 @@ return { | ||
| 560 | 573 | } |
| 561 | 574 | "#; |
| 562 | 575 | |
| 563 | - let config = parse_lua_config(config_str).expect("Failed to parse config"); | |
| 576 | + let config = parse_lua_config(config_str, None).expect("Failed to parse config"); | |
| 564 | 577 | |
| 565 | 578 | assert_eq!(config.border_width, 2); |
| 566 | 579 | assert_eq!(config.border_focused, 0x6dade3); |
+2 −1 src/config/migrate.rs
| @@ -1,6 +1,7 @@ | ||
| 1 | +use crate::errors::ConfigError; | |
| 1 | 2 | use std::collections::HashMap; |
| 2 | 3 | |
| 3 | -pub fn ron_to_lua(ron_content: &str) -> Result<String, String> { | |
| 4 | +pub fn ron_to_lua(ron_content: &str) -> Result<String, ConfigError> { | |
| 4 | 5 | let mut lua_output = String::new(); |
| 5 | 6 | let defines = extract_defines(ron_content); |
| 6 | 7 |
+45 −8 src/errors.rs
| @@ -4,8 +4,8 @@ use std::io; | ||
| 4 | 4 | pub enum WmError { |
| 5 | 5 | X11(X11Error), |
| 6 | 6 | Io(io::Error), |
| 7 | - Anyhow(anyhow::Error), | |
| 8 | 7 | Config(ConfigError), |
| 8 | + Block(BlockError), | |
| 9 | 9 | Autostart(String, io::Error), |
| 10 | 10 | } |
| 11 | 11 | |
| @@ -33,6 +33,16 @@ pub enum ConfigError { | ||
| 33 | 33 | InvalidVariableName(String), |
| 34 | 34 | InvalidDefine(String), |
| 35 | 35 | UndefinedVariable(String), |
| 36 | + MigrationError(String), | |
| 37 | +} | |
| 38 | + | |
| 39 | +#[derive(Debug)] | |
| 40 | +pub enum BlockError { | |
| 41 | + Io(io::Error), | |
| 42 | + ParseInt(std::num::ParseIntError), | |
| 43 | + MissingFile(String), | |
| 44 | + InvalidData(String), | |
| 45 | + CommandFailed(String), | |
| 36 | 46 | } |
| 37 | 47 | |
| 38 | 48 | impl std::fmt::Display for WmError { |
| @@ -40,8 +50,8 @@ impl std::fmt::Display for WmError { | ||
| 40 | 50 | match self { |
| 41 | 51 | Self::X11(error) => write!(f, "{}", error), |
| 42 | 52 | Self::Io(error) => write!(f, "{}", error), |
| 43 | - Self::Anyhow(error) => write!(f, "{}", error), | |
| 44 | 53 | Self::Config(error) => write!(f, "{}", error), |
| 54 | + Self::Block(error) => write!(f, "{}", error), | |
| 45 | 55 | Self::Autostart(command, error) => write!(f, "Failed to spawn autostart command '{}': {}", command, error), |
| 46 | 56 | } |
| 47 | 57 | } |
| @@ -95,12 +105,27 @@ impl std::fmt::Display for ConfigError { | ||
| 95 | 105 | var |
| 96 | 106 | ) |
| 97 | 107 | } |
| 108 | + Self::MigrationError(msg) => write!(f, "Migration error: {}", msg), | |
| 98 | 109 | } |
| 99 | 110 | } |
| 100 | 111 | } |
| 101 | 112 | |
| 102 | 113 | impl std::error::Error for ConfigError {} |
| 103 | 114 | |
| 115 | +impl std::fmt::Display for BlockError { | |
| 116 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| 117 | + match self { | |
| 118 | + Self::Io(err) => write!(f, "Block I/O error: {}", err), | |
| 119 | + Self::ParseInt(err) => write!(f, "Block parse error: {}", err), | |
| 120 | + Self::MissingFile(path) => write!(f, "Block missing file: {}", path), | |
| 121 | + Self::InvalidData(msg) => write!(f, "Block invalid data: {}", msg), | |
| 122 | + Self::CommandFailed(msg) => write!(f, "Block command failed: {}", msg), | |
| 123 | + } | |
| 124 | + } | |
| 125 | +} | |
| 126 | + | |
| 127 | +impl std::error::Error for BlockError {} | |
| 128 | + | |
| 104 | 129 | impl<T: Into<X11Error>> From<T> for WmError { |
| 105 | 130 | fn from(value: T) -> Self { |
| 106 | 131 | Self::X11(value.into()) |
| @@ -113,18 +138,30 @@ impl From<io::Error> for WmError { | ||
| 113 | 138 | } |
| 114 | 139 | } |
| 115 | 140 | |
| 116 | -impl From<anyhow::Error> for WmError { | |
| 117 | - fn from(value: anyhow::Error) -> Self { | |
| 118 | - Self::Anyhow(value) | |
| 119 | - } | |
| 120 | -} | |
| 121 | - | |
| 122 | 141 | impl From<ConfigError> for WmError { |
| 123 | 142 | fn from(value: ConfigError) -> Self { |
| 124 | 143 | Self::Config(value) |
| 125 | 144 | } |
| 126 | 145 | } |
| 127 | 146 | |
| 147 | +impl From<BlockError> for WmError { | |
| 148 | + fn from(value: BlockError) -> Self { | |
| 149 | + Self::Block(value) | |
| 150 | + } | |
| 151 | +} | |
| 152 | + | |
| 153 | +impl From<io::Error> for BlockError { | |
| 154 | + fn from(value: io::Error) -> Self { | |
| 155 | + BlockError::Io(value) | |
| 156 | + } | |
| 157 | +} | |
| 158 | + | |
| 159 | +impl From<std::num::ParseIntError> for BlockError { | |
| 160 | + fn from(value: std::num::ParseIntError) -> Self { | |
| 161 | + BlockError::ParseInt(value) | |
| 162 | + } | |
| 163 | +} | |
| 164 | + | |
| 128 | 165 | impl From<ron::error::SpannedError> for ConfigError { |
| 129 | 166 | fn from(value: ron::error::SpannedError) -> Self { |
| 130 | 167 | ConfigError::ParseError(value) |
+2 −1 src/keyboard/handlers.rs
| @@ -9,7 +9,7 @@ use x11rb::protocol::xproto::*; | ||
| 9 | 9 | use crate::errors::X11Error; |
| 10 | 10 | use crate::keyboard::keysyms::{self, Keysym}; |
| 11 | 11 | |
| 12 | -#[derive(Debug, Copy, Clone, Deserialize)] | |
| 12 | +#[derive(Debug, Copy, Clone, Deserialize, PartialEq)] | |
| 13 | 13 | pub enum KeyAction { |
| 14 | 14 | Spawn, |
| 15 | 15 | KillClient, |
| @@ -29,6 +29,7 @@ pub enum KeyAction { | ||
| 29 | 29 | FocusMonitor, |
| 30 | 30 | SmartMoveWin, |
| 31 | 31 | ExchangeClient, |
| 32 | + ShowKeybindOverlay, | |
| 32 | 33 | None, |
| 33 | 34 | } |
| 34 | 35 |
+58 −0 src/keyboard/keysyms.rs
| @@ -82,3 +82,61 @@ pub const XF86_AUDIO_LOWER_VOLUME: Keysym = 0x1008ff11; | ||
| 82 | 82 | pub const XF86_AUDIO_MUTE: Keysym = 0x1008ff12; |
| 83 | 83 | pub const XF86_MON_BRIGHTNESS_UP: Keysym = 0x1008ff02; |
| 84 | 84 | pub const XF86_MON_BRIGHTNESS_DOWN: Keysym = 0x1008ff03; |
| 85 | + | |
| 86 | +pub fn format_keysym(keysym: Keysym) -> String { | |
| 87 | + match keysym { | |
| 88 | + XK_RETURN => "Return".to_string(), | |
| 89 | + XK_ESCAPE => "Esc".to_string(), | |
| 90 | + XK_SPACE => "Space".to_string(), | |
| 91 | + XK_TAB => "Tab".to_string(), | |
| 92 | + XK_BACKSPACE => "Backspace".to_string(), | |
| 93 | + XK_DELETE => "Del".to_string(), | |
| 94 | + XK_LEFT => "Left".to_string(), | |
| 95 | + XK_RIGHT => "Right".to_string(), | |
| 96 | + XK_UP => "Up".to_string(), | |
| 97 | + XK_DOWN => "Down".to_string(), | |
| 98 | + XK_HOME => "Home".to_string(), | |
| 99 | + XK_END => "End".to_string(), | |
| 100 | + XK_PAGE_UP => "PgUp".to_string(), | |
| 101 | + XK_PAGE_DOWN => "PgDn".to_string(), | |
| 102 | + XK_INSERT => "Ins".to_string(), | |
| 103 | + XK_F1 => "F1".to_string(), | |
| 104 | + XK_F2 => "F2".to_string(), | |
| 105 | + XK_F3 => "F3".to_string(), | |
| 106 | + XK_F4 => "F4".to_string(), | |
| 107 | + XK_F5 => "F5".to_string(), | |
| 108 | + XK_F6 => "F6".to_string(), | |
| 109 | + XK_F7 => "F7".to_string(), | |
| 110 | + XK_F8 => "F8".to_string(), | |
| 111 | + XK_F9 => "F9".to_string(), | |
| 112 | + XK_F10 => "F10".to_string(), | |
| 113 | + XK_F11 => "F11".to_string(), | |
| 114 | + XK_F12 => "F12".to_string(), | |
| 115 | + XK_SLASH => "/".to_string(), | |
| 116 | + XK_COMMA => ",".to_string(), | |
| 117 | + XK_PERIOD => ".".to_string(), | |
| 118 | + XK_MINUS => "-".to_string(), | |
| 119 | + XK_EQUAL => "=".to_string(), | |
| 120 | + XK_GRAVE => "`".to_string(), | |
| 121 | + XK_LEFT_BRACKET => "[".to_string(), | |
| 122 | + XK_RIGHT_BRACKET => "]".to_string(), | |
| 123 | + XK_SEMICOLON => ";".to_string(), | |
| 124 | + XK_APOSTROPHE => "'".to_string(), | |
| 125 | + XK_BACKSLASH => "\\".to_string(), | |
| 126 | + XK_PRINT => "Print".to_string(), | |
| 127 | + XF86_AUDIO_RAISE_VOLUME => "Vol+".to_string(), | |
| 128 | + XF86_AUDIO_LOWER_VOLUME => "Vol-".to_string(), | |
| 129 | + XF86_AUDIO_MUTE => "Mute".to_string(), | |
| 130 | + XF86_MON_BRIGHTNESS_UP => "Bright+".to_string(), | |
| 131 | + XF86_MON_BRIGHTNESS_DOWN => "Bright-".to_string(), | |
| 132 | + XK_A..=XK_Z => { | |
| 133 | + let ch = (keysym - XK_A + b'A' as u32) as u8 as char; | |
| 134 | + ch.to_string() | |
| 135 | + } | |
| 136 | + XK_0..=XK_9 => { | |
| 137 | + let ch = (keysym - XK_0 + b'0' as u32) as u8 as char; | |
| 138 | + ch.to_string() | |
| 139 | + } | |
| 140 | + _ => format!("0x{:x}", keysym), | |
| 141 | + } | |
| 142 | +} |
+1 −0 src/lib.rs
| @@ -4,6 +4,7 @@ pub mod errors; | ||
| 4 | 4 | pub mod keyboard; |
| 5 | 5 | pub mod layout; |
| 6 | 6 | pub mod monitor; |
| 7 | +pub mod overlay; | |
| 7 | 8 | pub mod window_manager; |
| 8 | 9 | |
| 9 | 10 | pub mod prelude { |
+157 −0 src/overlay/error.rs new file mode 100644
| @@ -0,0 +1,157 @@ | ||
| 1 | +use super::{Overlay, OverlayBase}; | |
| 2 | +use crate::bar::font::Font; | |
| 3 | +use crate::errors::X11Error; | |
| 4 | +use std::time::Instant; | |
| 5 | +use x11rb::connection::Connection; | |
| 6 | +use x11rb::protocol::xproto::*; | |
| 7 | +use x11rb::rust_connection::RustConnection; | |
| 8 | + | |
| 9 | +const PADDING: i16 = 20; | |
| 10 | +const LINE_SPACING: i16 = 5; | |
| 11 | +const BORDER_WIDTH: u16 = 2; | |
| 12 | +const BORDER_COLOR: u32 = 0xff5555; | |
| 13 | +const AUTO_DISMISS_SECONDS: u64 = 10; | |
| 14 | + | |
| 15 | +pub struct ErrorOverlay { | |
| 16 | + base: OverlayBase, | |
| 17 | + created_at: Option<Instant>, | |
| 18 | + lines: Vec<String>, | |
| 19 | +} | |
| 20 | + | |
| 21 | +impl ErrorOverlay { | |
| 22 | + pub fn new( | |
| 23 | + connection: &RustConnection, | |
| 24 | + screen: &Screen, | |
| 25 | + screen_num: usize, | |
| 26 | + display: *mut x11::xlib::Display, | |
| 27 | + _font: &Font, | |
| 28 | + _max_width: u16, | |
| 29 | + ) -> Result<Self, X11Error> { | |
| 30 | + let base = OverlayBase::new( | |
| 31 | + connection, | |
| 32 | + screen, | |
| 33 | + screen_num, | |
| 34 | + display, | |
| 35 | + 400, | |
| 36 | + 200, | |
| 37 | + BORDER_WIDTH, | |
| 38 | + BORDER_COLOR, | |
| 39 | + 0x1a1a1a, | |
| 40 | + 0xffffff, | |
| 41 | + )?; | |
| 42 | + | |
| 43 | + Ok(ErrorOverlay { | |
| 44 | + base, | |
| 45 | + created_at: None, | |
| 46 | + lines: Vec::new(), | |
| 47 | + }) | |
| 48 | + } | |
| 49 | + | |
| 50 | + pub fn show_error( | |
| 51 | + &mut self, | |
| 52 | + connection: &RustConnection, | |
| 53 | + font: &Font, | |
| 54 | + error_text: &str, | |
| 55 | + screen_width: u16, | |
| 56 | + screen_height: u16, | |
| 57 | + ) -> Result<(), X11Error> { | |
| 58 | + let max_line_width = (screen_width as i16 - PADDING * 4).max(300) as u16; | |
| 59 | + self.lines = self.wrap_text(error_text, font, max_line_width); | |
| 60 | + | |
| 61 | + let mut content_width = 0u16; | |
| 62 | + for line in &self.lines { | |
| 63 | + let line_width = font.text_width(line); | |
| 64 | + if line_width > content_width { | |
| 65 | + content_width = line_width; | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + let width = content_width + (PADDING as u16 * 2); | |
| 70 | + let line_height = font.height() + LINE_SPACING as u16; | |
| 71 | + let height = (self.lines.len() as u16 * line_height) + (PADDING as u16 * 2); | |
| 72 | + | |
| 73 | + let x = ((screen_width - width) / 2) as i16; | |
| 74 | + let y = ((screen_height - height) / 2) as i16; | |
| 75 | + | |
| 76 | + self.base.configure(connection, x, y, width, height)?; | |
| 77 | + self.base.show(connection)?; | |
| 78 | + self.created_at = Some(Instant::now()); | |
| 79 | + self.draw(connection, font)?; | |
| 80 | + Ok(()) | |
| 81 | + } | |
| 82 | + | |
| 83 | + pub fn should_auto_dismiss(&self) -> bool { | |
| 84 | + if let Some(created_at) = self.created_at { | |
| 85 | + created_at.elapsed().as_secs() >= AUTO_DISMISS_SECONDS | |
| 86 | + } else { | |
| 87 | + false | |
| 88 | + } | |
| 89 | + } | |
| 90 | + | |
| 91 | + fn wrap_text(&self, text: &str, font: &Font, max_width: u16) -> Vec<String> { | |
| 92 | + let mut lines = Vec::new(); | |
| 93 | + for paragraph in text.lines() { | |
| 94 | + if paragraph.trim().is_empty() { | |
| 95 | + lines.push(String::new()); | |
| 96 | + continue; | |
| 97 | + } | |
| 98 | + | |
| 99 | + let words: Vec<&str> = paragraph.split_whitespace().collect(); | |
| 100 | + let mut current_line = String::new(); | |
| 101 | + | |
| 102 | + for word in words { | |
| 103 | + let test_line = if current_line.is_empty() { | |
| 104 | + word.to_string() | |
| 105 | + } else { | |
| 106 | + format!("{} {}", current_line, word) | |
| 107 | + }; | |
| 108 | + if font.text_width(&test_line) <= max_width { | |
| 109 | + current_line = test_line; | |
| 110 | + } else { | |
| 111 | + if !current_line.is_empty() { | |
| 112 | + lines.push(current_line); | |
| 113 | + } | |
| 114 | + current_line = word.to_string(); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + if !current_line.is_empty() { | |
| 118 | + lines.push(current_line); | |
| 119 | + } | |
| 120 | + } | |
| 121 | + lines | |
| 122 | + } | |
| 123 | +} | |
| 124 | + | |
| 125 | +impl Overlay for ErrorOverlay { | |
| 126 | + fn window(&self) -> Window { | |
| 127 | + self.base.window | |
| 128 | + } | |
| 129 | + | |
| 130 | + fn is_visible(&self) -> bool { | |
| 131 | + self.base.is_visible | |
| 132 | + } | |
| 133 | + | |
| 134 | + fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 135 | + self.base.hide(connection)?; | |
| 136 | + self.created_at = None; | |
| 137 | + self.lines.clear(); | |
| 138 | + Ok(()) | |
| 139 | + } | |
| 140 | + | |
| 141 | + fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error> { | |
| 142 | + if !self.base.is_visible { | |
| 143 | + return Ok(()); | |
| 144 | + } | |
| 145 | + self.base.draw_background(connection)?; | |
| 146 | + let line_height = font.height() + LINE_SPACING as u16; | |
| 147 | + let mut y = PADDING + font.ascent(); | |
| 148 | + for line in &self.lines { | |
| 149 | + self.base | |
| 150 | + .font_draw | |
| 151 | + .draw_text(font, self.base.foreground_color, PADDING, y, line); | |
| 152 | + y += line_height as i16; | |
| 153 | + } | |
| 154 | + connection.flush()?; | |
| 155 | + Ok(()) | |
| 156 | + } | |
| 157 | +} |
+296 −0 src/overlay/keybind.rs new file mode 100644
| @@ -0,0 +1,296 @@ | ||
| 1 | +use super::{Overlay, OverlayBase}; | |
| 2 | +use crate::bar::font::Font; | |
| 3 | +use crate::errors::X11Error; | |
| 4 | +use crate::keyboard::KeyAction; | |
| 5 | +use crate::keyboard::handlers::{KeyBinding, KeyPress}; | |
| 6 | +use std::time::Instant; | |
| 7 | +use x11rb::connection::Connection; | |
| 8 | +use x11rb::protocol::xproto::*; | |
| 9 | +use x11rb::rust_connection::RustConnection; | |
| 10 | + | |
| 11 | +const PADDING: i16 = 24; | |
| 12 | +const KEY_ACTION_SPACING: i16 = 20; | |
| 13 | +const LINE_SPACING: i16 = 8; | |
| 14 | +const BORDER_WIDTH: u16 = 4; | |
| 15 | +const BORDER_COLOR: u32 = 0x7fccff; | |
| 16 | +const TITLE_BOTTOM_MARGIN: i16 = 20; | |
| 17 | +const INPUT_SUPPRESS_MS: u128 = 200; | |
| 18 | + | |
| 19 | +pub struct KeybindOverlay { | |
| 20 | + base: OverlayBase, | |
| 21 | + keybindings: Vec<(String, String)>, | |
| 22 | + key_bg_color: u32, | |
| 23 | + modkey: KeyButMask, | |
| 24 | + last_shown_at: Option<Instant>, | |
| 25 | + max_key_width: u16, | |
| 26 | +} | |
| 27 | + | |
| 28 | +impl KeybindOverlay { | |
| 29 | + pub fn new( | |
| 30 | + connection: &RustConnection, | |
| 31 | + screen: &Screen, | |
| 32 | + screen_num: usize, | |
| 33 | + display: *mut x11::xlib::Display, | |
| 34 | + modkey: KeyButMask, | |
| 35 | + ) -> Result<Self, X11Error> { | |
| 36 | + let base = OverlayBase::new( | |
| 37 | + connection, | |
| 38 | + screen, | |
| 39 | + screen_num, | |
| 40 | + display, | |
| 41 | + 800, | |
| 42 | + 600, | |
| 43 | + BORDER_WIDTH, | |
| 44 | + BORDER_COLOR, | |
| 45 | + 0x1a1a1a, | |
| 46 | + 0xffffff, | |
| 47 | + )?; | |
| 48 | + | |
| 49 | + Ok(KeybindOverlay { | |
| 50 | + base, | |
| 51 | + keybindings: Vec::new(), | |
| 52 | + key_bg_color: 0x2a2a2a, | |
| 53 | + modkey, | |
| 54 | + last_shown_at: None, | |
| 55 | + max_key_width: 0, | |
| 56 | + }) | |
| 57 | + } | |
| 58 | + | |
| 59 | + pub fn show( | |
| 60 | + &mut self, | |
| 61 | + connection: &RustConnection, | |
| 62 | + font: &Font, | |
| 63 | + keybindings: &[KeyBinding], | |
| 64 | + screen_width: u16, | |
| 65 | + screen_height: u16, | |
| 66 | + ) -> Result<(), X11Error> { | |
| 67 | + self.keybindings = self.collect_keybindings(keybindings); | |
| 68 | + | |
| 69 | + let title = "Important Keybindings"; | |
| 70 | + let title_width = font.text_width(title); | |
| 71 | + | |
| 72 | + let mut max_key_width = 0u16; | |
| 73 | + let mut max_action_width = 0u16; | |
| 74 | + | |
| 75 | + for (key, action) in &self.keybindings { | |
| 76 | + let key_width = font.text_width(key); | |
| 77 | + let action_width = font.text_width(action); | |
| 78 | + if key_width > max_key_width { | |
| 79 | + max_key_width = key_width; | |
| 80 | + } | |
| 81 | + if action_width > max_action_width { | |
| 82 | + max_action_width = action_width; | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + let content_width = max_key_width + KEY_ACTION_SPACING as u16 + max_action_width; | |
| 87 | + let min_width = title_width.max(content_width); | |
| 88 | + | |
| 89 | + let width = min_width + (PADDING as u16 * 2); | |
| 90 | + | |
| 91 | + let line_height = font.height() + LINE_SPACING as u16; | |
| 92 | + let title_height = font.height() + TITLE_BOTTOM_MARGIN as u16; | |
| 93 | + let height = | |
| 94 | + title_height + (self.keybindings.len() as u16 * line_height) + (PADDING as u16 * 2); | |
| 95 | + | |
| 96 | + let x = ((screen_width - width) / 2) as i16; | |
| 97 | + let y = ((screen_height - height) / 2) as i16; | |
| 98 | + | |
| 99 | + self.base.configure(connection, x, y, width, height)?; | |
| 100 | + | |
| 101 | + self.last_shown_at = Some(Instant::now()); | |
| 102 | + self.max_key_width = max_key_width; | |
| 103 | + | |
| 104 | + self.base.show(connection)?; | |
| 105 | + | |
| 106 | + self.draw(connection, font)?; | |
| 107 | + | |
| 108 | + Ok(()) | |
| 109 | + } | |
| 110 | + | |
| 111 | + pub fn toggle( | |
| 112 | + &mut self, | |
| 113 | + connection: &RustConnection, | |
| 114 | + font: &Font, | |
| 115 | + keybindings: &[KeyBinding], | |
| 116 | + screen_width: u16, | |
| 117 | + screen_height: u16, | |
| 118 | + ) -> Result<(), X11Error> { | |
| 119 | + if self.base.is_visible { | |
| 120 | + self.hide(connection)?; | |
| 121 | + } else { | |
| 122 | + self.show(connection, font, keybindings, screen_width, screen_height)?; | |
| 123 | + } | |
| 124 | + Ok(()) | |
| 125 | + } | |
| 126 | + | |
| 127 | + pub fn should_suppress_input(&self) -> bool { | |
| 128 | + if let Some(shown_at) = self.last_shown_at { | |
| 129 | + shown_at.elapsed().as_millis() < INPUT_SUPPRESS_MS | |
| 130 | + } else { | |
| 131 | + false | |
| 132 | + } | |
| 133 | + } | |
| 134 | + | |
| 135 | + fn collect_keybindings(&self, keybindings: &[KeyBinding]) -> Vec<(String, String)> { | |
| 136 | + let mut result = Vec::new(); | |
| 137 | + | |
| 138 | + let priority_actions = [ | |
| 139 | + KeyAction::ShowKeybindOverlay, | |
| 140 | + KeyAction::Quit, | |
| 141 | + KeyAction::Restart, | |
| 142 | + KeyAction::KillClient, | |
| 143 | + KeyAction::Spawn, | |
| 144 | + KeyAction::ToggleFullScreen, | |
| 145 | + KeyAction::ToggleFloating, | |
| 146 | + KeyAction::CycleLayout, | |
| 147 | + KeyAction::FocusStack, | |
| 148 | + KeyAction::ViewTag, | |
| 149 | + ]; | |
| 150 | + | |
| 151 | + for &action in &priority_actions { | |
| 152 | + let binding = keybindings | |
| 153 | + .iter() | |
| 154 | + .filter(|kb| kb.func == action) | |
| 155 | + .min_by_key(|kb| kb.keys.len()); | |
| 156 | + | |
| 157 | + if let Some(binding) = binding { | |
| 158 | + if !binding.keys.is_empty() { | |
| 159 | + let key_str = self.format_key_combo(&binding.keys[0]); | |
| 160 | + let action_str = self.action_description(binding); | |
| 161 | + result.push((key_str, action_str)); | |
| 162 | + } | |
| 163 | + } | |
| 164 | + } | |
| 165 | + | |
| 166 | + result | |
| 167 | + } | |
| 168 | + | |
| 169 | + fn format_key_combo(&self, key: &KeyPress) -> String { | |
| 170 | + let mut parts = Vec::new(); | |
| 171 | + | |
| 172 | + for modifier in &key.modifiers { | |
| 173 | + let mod_str = match *modifier { | |
| 174 | + m if m == self.modkey => "Mod", | |
| 175 | + KeyButMask::SHIFT => "Shift", | |
| 176 | + KeyButMask::CONTROL => "Ctrl", | |
| 177 | + KeyButMask::MOD1 => "Alt", | |
| 178 | + KeyButMask::MOD4 => "Super", | |
| 179 | + _ => continue, | |
| 180 | + }; | |
| 181 | + parts.push(mod_str.to_string()); | |
| 182 | + } | |
| 183 | + | |
| 184 | + parts.push(crate::keyboard::keysyms::format_keysym(key.keysym)); | |
| 185 | + | |
| 186 | + parts.join(" + ") | |
| 187 | + } | |
| 188 | + | |
| 189 | + fn action_description(&self, binding: &KeyBinding) -> String { | |
| 190 | + use crate::keyboard::Arg; | |
| 191 | + | |
| 192 | + match binding.func { | |
| 193 | + KeyAction::ShowKeybindOverlay => "Show This Keybind Help".to_string(), | |
| 194 | + KeyAction::Quit => "Quit Window Manager".to_string(), | |
| 195 | + KeyAction::Restart => "Restart Window Manager".to_string(), | |
| 196 | + KeyAction::Recompile => "Recompile Window Manager".to_string(), | |
| 197 | + KeyAction::KillClient => "Close Focused Window".to_string(), | |
| 198 | + KeyAction::Spawn => match &binding.arg { | |
| 199 | + Arg::Str(cmd) => format!("Launch: {}", cmd), | |
| 200 | + Arg::Array(arr) if !arr.is_empty() => format!("Launch: {}", arr[0]), | |
| 201 | + _ => "Launch Program".to_string(), | |
| 202 | + }, | |
| 203 | + KeyAction::FocusStack => "Focus Next/Previous Window".to_string(), | |
| 204 | + KeyAction::FocusDirection => "Focus Window in Direction".to_string(), | |
| 205 | + KeyAction::SwapDirection => "Swap Window in Direction".to_string(), | |
| 206 | + KeyAction::ViewTag => match &binding.arg { | |
| 207 | + Arg::Int(n) => format!("View Workspace {}", n), | |
| 208 | + _ => "View Workspace".to_string(), | |
| 209 | + }, | |
| 210 | + KeyAction::MoveToTag => "Move Window to Workspace".to_string(), | |
| 211 | + KeyAction::ToggleGaps => "Toggle Window Gaps".to_string(), | |
| 212 | + KeyAction::ToggleFullScreen => "Toggle Fullscreen".to_string(), | |
| 213 | + KeyAction::ToggleFloating => "Toggle Floating Mode".to_string(), | |
| 214 | + KeyAction::ChangeLayout => "Change Layout".to_string(), | |
| 215 | + KeyAction::CycleLayout => "Cycle Through Layouts".to_string(), | |
| 216 | + KeyAction::FocusMonitor => "Focus Next Monitor".to_string(), | |
| 217 | + KeyAction::SmartMoveWin => "Smart Move Window".to_string(), | |
| 218 | + KeyAction::ExchangeClient => "Exchange Client Windows".to_string(), | |
| 219 | + KeyAction::None => "No Action".to_string(), | |
| 220 | + } | |
| 221 | + } | |
| 222 | +} | |
| 223 | + | |
| 224 | +impl Overlay for KeybindOverlay { | |
| 225 | + fn window(&self) -> Window { | |
| 226 | + self.base.window | |
| 227 | + } | |
| 228 | + | |
| 229 | + fn is_visible(&self) -> bool { | |
| 230 | + self.base.is_visible | |
| 231 | + } | |
| 232 | + | |
| 233 | + fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 234 | + self.base.hide(connection)?; | |
| 235 | + self.last_shown_at = None; | |
| 236 | + self.keybindings.clear(); | |
| 237 | + Ok(()) | |
| 238 | + } | |
| 239 | + | |
| 240 | + fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error> { | |
| 241 | + if !self.base.is_visible { | |
| 242 | + return Ok(()); | |
| 243 | + } | |
| 244 | + | |
| 245 | + self.base.draw_background(connection)?; | |
| 246 | + | |
| 247 | + let title = "Important Keybindings"; | |
| 248 | + let title_width = font.text_width(title); | |
| 249 | + let title_x = ((self.base.width - title_width) / 2) as i16; | |
| 250 | + let title_y = PADDING + font.ascent(); | |
| 251 | + | |
| 252 | + self.base | |
| 253 | + .font_draw | |
| 254 | + .draw_text(font, self.base.foreground_color, title_x, title_y, title); | |
| 255 | + | |
| 256 | + let line_height = font.height() + LINE_SPACING as u16; | |
| 257 | + let mut y = PADDING + font.height() as i16 + TITLE_BOTTOM_MARGIN + font.ascent(); | |
| 258 | + | |
| 259 | + for (key, action) in &self.keybindings { | |
| 260 | + let key_width = font.text_width(key); | |
| 261 | + let key_x = PADDING; | |
| 262 | + | |
| 263 | + connection.change_gc( | |
| 264 | + self.base.graphics_context, | |
| 265 | + &ChangeGCAux::new().foreground(self.key_bg_color), | |
| 266 | + )?; | |
| 267 | + connection.poly_fill_rectangle( | |
| 268 | + self.base.window, | |
| 269 | + self.base.graphics_context, | |
| 270 | + &[Rectangle { | |
| 271 | + x: key_x - 4, | |
| 272 | + y: y - font.ascent() - 2, | |
| 273 | + width: key_width + 8, | |
| 274 | + height: font.height() + 4, | |
| 275 | + }], | |
| 276 | + )?; | |
| 277 | + | |
| 278 | + self.base | |
| 279 | + .font_draw | |
| 280 | + .draw_text(font, self.base.foreground_color, key_x, y, key); | |
| 281 | + | |
| 282 | + let action_x = PADDING + self.max_key_width as i16 + KEY_ACTION_SPACING; | |
| 283 | + self.base | |
| 284 | + .font_draw | |
| 285 | + .draw_text(font, self.base.foreground_color, action_x, y, action); | |
| 286 | + | |
| 287 | + y += line_height as i16; | |
| 288 | + } | |
| 289 | + | |
| 290 | + self.base.font_draw.flush(); | |
| 291 | + | |
| 292 | + connection.flush()?; | |
| 293 | + | |
| 294 | + Ok(()) | |
| 295 | + } | |
| 296 | +} |
+156 −0 src/overlay/mod.rs new file mode 100644
| @@ -0,0 +1,156 @@ | ||
| 1 | +use crate::bar::font::{Font, FontDraw}; | |
| 2 | +use crate::errors::X11Error; | |
| 3 | +use x11rb::COPY_DEPTH_FROM_PARENT; | |
| 4 | +use x11rb::connection::Connection; | |
| 5 | +use x11rb::protocol::xproto::*; | |
| 6 | +use x11rb::rust_connection::RustConnection; | |
| 7 | + | |
| 8 | +pub mod error; | |
| 9 | +pub mod keybind; | |
| 10 | + | |
| 11 | +pub use error::ErrorOverlay; | |
| 12 | +pub use keybind::KeybindOverlay; | |
| 13 | + | |
| 14 | +pub trait Overlay { | |
| 15 | + fn window(&self) -> Window; | |
| 16 | + fn is_visible(&self) -> bool; | |
| 17 | + fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error>; | |
| 18 | + fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error>; | |
| 19 | +} | |
| 20 | + | |
| 21 | +pub struct OverlayBase { | |
| 22 | + pub window: Window, | |
| 23 | + pub width: u16, | |
| 24 | + pub height: u16, | |
| 25 | + pub graphics_context: Gcontext, | |
| 26 | + pub font_draw: FontDraw, | |
| 27 | + pub is_visible: bool, | |
| 28 | + pub background_color: u32, | |
| 29 | + pub foreground_color: u32, | |
| 30 | +} | |
| 31 | + | |
| 32 | +impl OverlayBase { | |
| 33 | + pub fn new( | |
| 34 | + connection: &RustConnection, | |
| 35 | + screen: &Screen, | |
| 36 | + screen_num: usize, | |
| 37 | + display: *mut x11::xlib::Display, | |
| 38 | + width: u16, | |
| 39 | + height: u16, | |
| 40 | + border_width: u16, | |
| 41 | + border_color: u32, | |
| 42 | + background_color: u32, | |
| 43 | + foreground_color: u32, | |
| 44 | + ) -> Result<Self, X11Error> { | |
| 45 | + let window = connection.generate_id()?; | |
| 46 | + let graphics_context = connection.generate_id()?; | |
| 47 | + | |
| 48 | + connection.create_window( | |
| 49 | + COPY_DEPTH_FROM_PARENT, | |
| 50 | + window, | |
| 51 | + screen.root, | |
| 52 | + 0, | |
| 53 | + 0, | |
| 54 | + width, | |
| 55 | + height, | |
| 56 | + border_width, | |
| 57 | + WindowClass::INPUT_OUTPUT, | |
| 58 | + screen.root_visual, | |
| 59 | + &CreateWindowAux::new() | |
| 60 | + .background_pixel(background_color) | |
| 61 | + .border_pixel(border_color) | |
| 62 | + .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS | EventMask::KEY_PRESS) | |
| 63 | + .override_redirect(1), | |
| 64 | + )?; | |
| 65 | + | |
| 66 | + connection.create_gc( | |
| 67 | + graphics_context, | |
| 68 | + window, | |
| 69 | + &CreateGCAux::new() | |
| 70 | + .foreground(foreground_color) | |
| 71 | + .background(background_color), | |
| 72 | + )?; | |
| 73 | + | |
| 74 | + connection.flush()?; | |
| 75 | + | |
| 76 | + let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; | |
| 77 | + let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; | |
| 78 | + | |
| 79 | + let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?; | |
| 80 | + | |
| 81 | + Ok(OverlayBase { | |
| 82 | + window, | |
| 83 | + width, | |
| 84 | + height, | |
| 85 | + graphics_context, | |
| 86 | + font_draw, | |
| 87 | + is_visible: false, | |
| 88 | + background_color, | |
| 89 | + foreground_color, | |
| 90 | + }) | |
| 91 | + } | |
| 92 | + | |
| 93 | + pub fn configure( | |
| 94 | + &mut self, | |
| 95 | + connection: &RustConnection, | |
| 96 | + x: i16, | |
| 97 | + y: i16, | |
| 98 | + width: u16, | |
| 99 | + height: u16, | |
| 100 | + ) -> Result<(), X11Error> { | |
| 101 | + self.width = width; | |
| 102 | + self.height = height; | |
| 103 | + | |
| 104 | + connection.configure_window( | |
| 105 | + self.window, | |
| 106 | + &ConfigureWindowAux::new() | |
| 107 | + .x(x as i32) | |
| 108 | + .y(y as i32) | |
| 109 | + .width(width as u32) | |
| 110 | + .height(height as u32), | |
| 111 | + )?; | |
| 112 | + | |
| 113 | + Ok(()) | |
| 114 | + } | |
| 115 | + | |
| 116 | + pub fn show(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 117 | + connection.configure_window( | |
| 118 | + self.window, | |
| 119 | + &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | |
| 120 | + )?; | |
| 121 | + | |
| 122 | + connection.map_window(self.window)?; | |
| 123 | + connection.flush()?; | |
| 124 | + | |
| 125 | + self.is_visible = true; | |
| 126 | + | |
| 127 | + Ok(()) | |
| 128 | + } | |
| 129 | + | |
| 130 | + pub fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 131 | + if self.is_visible { | |
| 132 | + connection.unmap_window(self.window)?; | |
| 133 | + connection.flush()?; | |
| 134 | + self.is_visible = false; | |
| 135 | + } | |
| 136 | + Ok(()) | |
| 137 | + } | |
| 138 | + | |
| 139 | + pub fn draw_background(&self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 140 | + connection.change_gc( | |
| 141 | + self.graphics_context, | |
| 142 | + &ChangeGCAux::new().foreground(self.background_color), | |
| 143 | + )?; | |
| 144 | + connection.poly_fill_rectangle( | |
| 145 | + self.window, | |
| 146 | + self.graphics_context, | |
| 147 | + &[Rectangle { | |
| 148 | + x: 0, | |
| 149 | + y: 0, | |
| 150 | + width: self.width, | |
| 151 | + height: self.height, | |
| 152 | + }], | |
| 153 | + )?; | |
| 154 | + Ok(()) | |
| 155 | + } | |
| 156 | +} |
+151 −56 src/window_manager.rs
| @@ -6,6 +6,7 @@ use crate::layout::GapConfig; | ||
| 6 | 6 | use crate::layout::tiling::TilingLayout; |
| 7 | 7 | use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout}; |
| 8 | 8 | use crate::monitor::{Monitor, detect_monitors}; |
| 9 | +use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay}; | |
| 9 | 10 | use std::collections::HashSet; |
| 10 | 11 | use std::process::Command; |
| 11 | 12 | use x11rb::cursor::Handle as CursorHandle; |
| @@ -69,6 +70,9 @@ pub struct WindowManager { | ||
| 69 | 70 | display: *mut x11::xlib::Display, |
| 70 | 71 | font: crate::bar::font::Font, |
| 71 | 72 | keychord_state: keyboard::handlers::KeychordState, |
| 73 | + error_message: Option<String>, | |
| 74 | + overlay: ErrorOverlay, | |
| 75 | + keybind_overlay: KeybindOverlay, | |
| 72 | 76 | } |
| 73 | 77 | |
| 74 | 78 | type WmResult<T> = Result<T, WmError>; |
| @@ -156,6 +160,18 @@ impl WindowManager { | ||
| 156 | 160 | |
| 157 | 161 | let atoms = AtomCache::new(&connection)?; |
| 158 | 162 | |
| 163 | + let overlay = ErrorOverlay::new( | |
| 164 | + &connection, | |
| 165 | + &screen, | |
| 166 | + screen_number, | |
| 167 | + display, | |
| 168 | + &font, | |
| 169 | + screen.width_in_pixels, | |
| 170 | + )?; | |
| 171 | + | |
| 172 | + let keybind_overlay = | |
| 173 | + KeybindOverlay::new(&connection, &screen, screen_number, display, config.modkey)?; | |
| 174 | + | |
| 159 | 175 | let mut window_manager = Self { |
| 160 | 176 | config, |
| 161 | 177 | connection, |
| @@ -177,6 +193,9 @@ impl WindowManager { | ||
| 177 | 193 | display, |
| 178 | 194 | font, |
| 179 | 195 | keychord_state: keyboard::handlers::KeychordState::Idle, |
| 196 | + error_message: None, | |
| 197 | + overlay, | |
| 198 | + keybind_overlay, | |
| 180 | 199 | }; |
| 181 | 200 | |
| 182 | 201 | window_manager.scan_existing_windows()?; |
| @@ -225,6 +244,48 @@ impl WindowManager { | ||
| 225 | 244 | Ok(tag_mask(0)) |
| 226 | 245 | } |
| 227 | 246 | |
| 247 | + fn try_reload_config(&mut self) -> Result<(), String> { | |
| 248 | + let config_dir = if let Some(xdg_config) = std::env::var_os("XDG_CONFIG_HOME") { | |
| 249 | + std::path::PathBuf::from(xdg_config).join("oxwm") | |
| 250 | + } else if let Some(home) = std::env::var_os("HOME") { | |
| 251 | + std::path::PathBuf::from(home).join(".config").join("oxwm") | |
| 252 | + } else { | |
| 253 | + return Err("Could not find config directory".to_string()); | |
| 254 | + }; | |
| 255 | + | |
| 256 | + let lua_path = config_dir.join("config.lua"); | |
| 257 | + let ron_path = config_dir.join("config.ron"); | |
| 258 | + | |
| 259 | + let config_path = if lua_path.exists() { | |
| 260 | + lua_path | |
| 261 | + } else if ron_path.exists() { | |
| 262 | + ron_path | |
| 263 | + } else { | |
| 264 | + return Err("No config file found".to_string()); | |
| 265 | + }; | |
| 266 | + | |
| 267 | + let config_str = std::fs::read_to_string(&config_path) | |
| 268 | + .map_err(|e| format!("Failed to read config: {}", e))?; | |
| 269 | + | |
| 270 | + let is_lua = config_path | |
| 271 | + .extension() | |
| 272 | + .and_then(|s| s.to_str()) | |
| 273 | + .map(|s| s == "lua") | |
| 274 | + .unwrap_or(false); | |
| 275 | + | |
| 276 | + let new_config = if is_lua { | |
| 277 | + let config_dir = config_path.parent(); | |
| 278 | + crate::config::parse_lua_config(&config_str, config_dir) | |
| 279 | + .map_err(|e| format!("Config error: {}", e))? | |
| 280 | + } else { | |
| 281 | + crate::config::parse_config(&config_str).map_err(|e| format!("Config error: {}", e))? | |
| 282 | + }; | |
| 283 | + | |
| 284 | + self.config = new_config; | |
| 285 | + self.error_message = None; | |
| 286 | + Ok(()) | |
| 287 | + } | |
| 288 | + | |
| 228 | 289 | fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 229 | 290 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 230 | 291 | let net_client_info = self.atoms.net_client_info; |
| @@ -377,6 +438,10 @@ impl WindowManager { | ||
| 377 | 438 | self.update_bar()?; |
| 378 | 439 | } |
| 379 | 440 | |
| 441 | + if self.overlay.is_visible() && self.overlay.should_auto_dismiss() { | |
| 442 | + let _ = self.overlay.hide(&self.connection); | |
| 443 | + } | |
| 444 | + | |
| 380 | 445 | std::thread::sleep(std::time::Duration::from_millis(100)); |
| 381 | 446 | } |
| 382 | 447 | } |
| @@ -808,7 +873,7 @@ impl WindowManager { | ||
| 808 | 873 | indicator.push('+'); |
| 809 | 874 | } |
| 810 | 875 | |
| 811 | - indicator.push_str(&self.format_keysym(key_press.keysym)); | |
| 876 | + indicator.push_str(&keyboard::keysyms::format_keysym(key_press.keysym)); | |
| 812 | 877 | } |
| 813 | 878 | |
| 814 | 879 | indicator.push('-'); |
| @@ -827,60 +892,6 @@ impl WindowManager { | ||
| 827 | 892 | } |
| 828 | 893 | } |
| 829 | 894 | |
| 830 | - fn format_keysym(&self, keysym: keyboard::keysyms::Keysym) -> String { | |
| 831 | - use keyboard::keysyms::*; | |
| 832 | - | |
| 833 | - match keysym { | |
| 834 | - XK_RETURN => "Return", | |
| 835 | - XK_ESCAPE => "Esc", | |
| 836 | - XK_SPACE => "Space", | |
| 837 | - XK_TAB => "Tab", | |
| 838 | - XK_BACKSPACE => "Backspace", | |
| 839 | - XK_DELETE => "Del", | |
| 840 | - XK_F1 => "F1", | |
| 841 | - XK_F2 => "F2", | |
| 842 | - XK_F3 => "F3", | |
| 843 | - XK_F4 => "F4", | |
| 844 | - XK_F5 => "F5", | |
| 845 | - XK_F6 => "F6", | |
| 846 | - XK_F7 => "F7", | |
| 847 | - XK_F8 => "F8", | |
| 848 | - XK_F9 => "F9", | |
| 849 | - XK_F10 => "F10", | |
| 850 | - XK_F11 => "F11", | |
| 851 | - XK_F12 => "F12", | |
| 852 | - XK_A..=XK_Z | XK_0..=XK_9 => { | |
| 853 | - return char::from_u32(keysym).unwrap_or('?').to_string(); | |
| 854 | - } | |
| 855 | - XK_LEFT => "Left", | |
| 856 | - XK_RIGHT => "Right", | |
| 857 | - XK_UP => "Up", | |
| 858 | - XK_DOWN => "Down", | |
| 859 | - XK_HOME => "Home", | |
| 860 | - XK_END => "End", | |
| 861 | - XK_PAGE_UP => "PgUp", | |
| 862 | - XK_PAGE_DOWN => "PgDn", | |
| 863 | - XK_INSERT => "Ins", | |
| 864 | - XK_MINUS => "-", | |
| 865 | - XK_EQUAL => "=", | |
| 866 | - XK_LEFT_BRACKET => "[", | |
| 867 | - XK_RIGHT_BRACKET => "]", | |
| 868 | - XK_SEMICOLON => ";", | |
| 869 | - XK_APOSTROPHE => "'", | |
| 870 | - XK_GRAVE => "`", | |
| 871 | - XK_BACKSLASH => "\\", | |
| 872 | - XK_COMMA => ",", | |
| 873 | - XK_PERIOD => ".", | |
| 874 | - XK_SLASH => "/", | |
| 875 | - XF86_AUDIO_RAISE_VOLUME => "Vol+", | |
| 876 | - XF86_AUDIO_LOWER_VOLUME => "Vol-", | |
| 877 | - XF86_AUDIO_MUTE => "Mute", | |
| 878 | - XF86_MON_BRIGHTNESS_UP => "Bri+", | |
| 879 | - XF86_MON_BRIGHTNESS_DOWN => "Bri-", | |
| 880 | - _ => "?", | |
| 881 | - } | |
| 882 | - .to_string() | |
| 883 | - } | |
| 884 | 895 | |
| 885 | 896 | fn update_bar(&mut self) -> WmResult<()> { |
| 886 | 897 | let layout_symbol = self.get_layout_symbol(); |
| @@ -1020,6 +1031,16 @@ impl WindowManager { | ||
| 1020 | 1031 | self.focus_monitor(*direction)?; |
| 1021 | 1032 | } |
| 1022 | 1033 | } |
| 1034 | + KeyAction::ShowKeybindOverlay => { | |
| 1035 | + let monitor = &self.monitors[self.selected_monitor]; | |
| 1036 | + self.keybind_overlay.toggle( | |
| 1037 | + &self.connection, | |
| 1038 | + &self.font, | |
| 1039 | + &self.config.keybindings, | |
| 1040 | + monitor.width as u16, | |
| 1041 | + monitor.height as u16, | |
| 1042 | + )?; | |
| 1043 | + } | |
| 1023 | 1044 | KeyAction::None => {} |
| 1024 | 1045 | } |
| 1025 | 1046 | Ok(()) |
| @@ -1610,6 +1631,60 @@ impl WindowManager { | ||
| 1610 | 1631 | |
| 1611 | 1632 | fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { |
| 1612 | 1633 | match event { |
| 1634 | + Event::KeyPress(ref e) if e.event == self.overlay.window() => { | |
| 1635 | + if self.overlay.is_visible() { | |
| 1636 | + let _ = self.overlay.hide(&self.connection); | |
| 1637 | + } | |
| 1638 | + return Ok(None); | |
| 1639 | + } | |
| 1640 | + Event::ButtonPress(ref e) if e.event == self.overlay.window() => { | |
| 1641 | + if self.overlay.is_visible() { | |
| 1642 | + let _ = self.overlay.hide(&self.connection); | |
| 1643 | + } | |
| 1644 | + return Ok(None); | |
| 1645 | + } | |
| 1646 | + Event::Expose(ref e) if e.window == self.overlay.window() => { | |
| 1647 | + if self.overlay.is_visible() { | |
| 1648 | + let _ = self.overlay.draw(&self.connection, &self.font); | |
| 1649 | + } | |
| 1650 | + return Ok(None); | |
| 1651 | + } | |
| 1652 | + Event::KeyPress(ref e) if e.event == self.keybind_overlay.window() => { | |
| 1653 | + if self.keybind_overlay.is_visible() | |
| 1654 | + && !self.keybind_overlay.should_suppress_input() | |
| 1655 | + { | |
| 1656 | + use crate::keyboard::keysyms; | |
| 1657 | + let keyboard_mapping = self | |
| 1658 | + .connection | |
| 1659 | + .get_keyboard_mapping( | |
| 1660 | + self.connection.setup().min_keycode, | |
| 1661 | + self.connection.setup().max_keycode | |
| 1662 | + - self.connection.setup().min_keycode | |
| 1663 | + + 1, | |
| 1664 | + )? | |
| 1665 | + .reply()?; | |
| 1666 | + | |
| 1667 | + let min_keycode = self.connection.setup().min_keycode; | |
| 1668 | + let keysyms_per_keycode = keyboard_mapping.keysyms_per_keycode; | |
| 1669 | + let index = (e.detail - min_keycode) as usize * keysyms_per_keycode as usize; | |
| 1670 | + | |
| 1671 | + if let Some(&keysym) = keyboard_mapping.keysyms.get(index) { | |
| 1672 | + if keysym == keysyms::XK_ESCAPE || keysym == keysyms::XK_Q { | |
| 1673 | + let _ = self.keybind_overlay.hide(&self.connection); | |
| 1674 | + } | |
| 1675 | + } | |
| 1676 | + } | |
| 1677 | + return Ok(None); | |
| 1678 | + } | |
| 1679 | + Event::ButtonPress(ref e) if e.event == self.keybind_overlay.window() => { | |
| 1680 | + return Ok(None); | |
| 1681 | + } | |
| 1682 | + Event::Expose(ref e) if e.window == self.keybind_overlay.window() => { | |
| 1683 | + if self.keybind_overlay.is_visible() { | |
| 1684 | + let _ = self.keybind_overlay.draw(&self.connection, &self.font); | |
| 1685 | + } | |
| 1686 | + return Ok(None); | |
| 1687 | + } | |
| 1613 | 1688 | Event::MapRequest(event) => { |
| 1614 | 1689 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| 1615 | 1690 | Ok(attrs) => attrs, |
| @@ -1705,7 +1780,27 @@ impl WindowManager { | ||
| 1705 | 1780 | |
| 1706 | 1781 | match action { |
| 1707 | 1782 | KeyAction::Quit => return Ok(Some(false)), |
| 1708 | - KeyAction::Restart => return Ok(Some(true)), | |
| 1783 | + KeyAction::Restart => match self.try_reload_config() { | |
| 1784 | + Ok(()) => { | |
| 1785 | + self.gaps_enabled = self.config.gaps_enabled; | |
| 1786 | + self.error_message = None; | |
| 1787 | + let _ = self.overlay.hide(&self.connection); | |
| 1788 | + self.apply_layout()?; | |
| 1789 | + self.update_bar()?; | |
| 1790 | + } | |
| 1791 | + Err(err) => { | |
| 1792 | + self.error_message = Some(err.clone()); | |
| 1793 | + let screen_width = self.screen.width_in_pixels; | |
| 1794 | + let screen_height = self.screen.height_in_pixels; | |
| 1795 | + let _ = self.overlay.show_error( | |
| 1796 | + &self.connection, | |
| 1797 | + &self.font, | |
| 1798 | + &err, | |
| 1799 | + screen_width, | |
| 1800 | + screen_height, | |
| 1801 | + ); | |
| 1802 | + } | |
| 1803 | + }, | |
| 1709 | 1804 | _ => self.handle_key_action(action, &arg)?, |
| 1710 | 1805 | } |
| 1711 | 1806 | } |
+1 −0 templates/config.lua
| @@ -54,6 +54,7 @@ return { | ||
| 54 | 54 | { modifiers = { "Mod4" }, key = "D", action = "Spawn", arg = { "sh", "-c", "dmenu_run -l 10" } }, |
| 55 | 55 | { modifiers = { "Mod4" }, key = "S", action = "Spawn", arg = { "sh", "-c", "maim -s | xclip -selection clipboard -t image/png" } }, |
| 56 | 56 | { modifiers = { "Mod4" }, key = "Q", action = "KillClient" }, |
| 57 | + { modifiers = { "Mod4", "Shift" }, key = "Slash", action = "ShowKeybindOverlay" }, | |
| 57 | 58 | { modifiers = { "Mod4", "Shift" }, key = "F", action = "ToggleFullScreen" }, |
| 58 | 59 | { modifiers = { "Mod4", "Shift" }, key = "Space", action = "ToggleFloating" }, |
| 59 | 60 | { modifiers = { "Mod4" }, key = "F", action = "ChangeLayout", arg = "normie" }, |