| 1 | use crate::keyboard::{self, Arg, KeyAction}; |
| 2 | use crate::layout::Layout; |
| 3 | use crate::layout::tiling::TilingLayout; |
| 4 | |
| 5 | use anyhow::Result; |
| 6 | |
| 7 | use x11rb::connection::Connection; |
| 8 | use x11rb::protocol::Event; |
| 9 | use x11rb::protocol::xproto::*; |
| 10 | use x11rb::rust_connection::RustConnection; |
| 11 | |
| 12 | pub struct WindowManager { |
| 13 | connection: RustConnection, |
| 14 | screen_number: usize, |
| 15 | root: Window, |
| 16 | screen: Screen, |
| 17 | windows: Vec<Window>, |
| 18 | focused_window: Option<Window>, |
| 19 | layout: Box<dyn Layout>, |
| 20 | } |
| 21 | |
| 22 | impl WindowManager { |
| 23 | pub fn new() -> Result<Self> { |
| 24 | let (connection, screen_number) = x11rb::connect(None)?; |
| 25 | let root = connection.setup().roots[screen_number].root; |
| 26 | let screen = connection.setup().roots[screen_number].clone(); |
| 27 | |
| 28 | connection |
| 29 | .change_window_attributes( |
| 30 | root, |
| 31 | &ChangeWindowAttributesAux::new().event_mask( |
| 32 | EventMask::SUBSTRUCTURE_REDIRECT |
| 33 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 34 | | EventMask::PROPERTY_CHANGE |
| 35 | | EventMask::KEY_PRESS, |
| 36 | ), |
| 37 | )? |
| 38 | .check()?; |
| 39 | |
| 40 | return Ok(Self { |
| 41 | connection, |
| 42 | screen_number, |
| 43 | root, |
| 44 | screen, |
| 45 | windows: Vec::new(), |
| 46 | focused_window: None, |
| 47 | layout: Box::new(TilingLayout), |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | pub fn run(&mut self) -> Result<()> { |
| 52 | println!("oxwm started on display {}", self.screen_number); |
| 53 | |
| 54 | keyboard::setup_keybinds(&self.connection, self.root)?; |
| 55 | |
| 56 | loop { |
| 57 | let event = self.connection.wait_for_event()?; |
| 58 | println!("event: {:?}", event); |
| 59 | self.handle_event(event)?; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> { |
| 64 | match action { |
| 65 | KeyAction::Spawn => { |
| 66 | if let Arg::Str(command) = arg { |
| 67 | println!("Spawning: {}", command); |
| 68 | std::process::Command::new(command).spawn()?; |
| 69 | } |
| 70 | } |
| 71 | KeyAction::KillClient => { |
| 72 | println!("Closing focused window"); |
| 73 | if let Some(focused) = self.focused_window { |
| 74 | match self.connection.kill_client(focused) { |
| 75 | Ok(_) => { |
| 76 | self.connection.flush()?; |
| 77 | println!("Killed window {}", focused); |
| 78 | } |
| 79 | Err(e) => { |
| 80 | println!("Failed to kill window {}: {}", focused, e); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | KeyAction::FocusStack => { |
| 86 | if let Arg::Int(direction) = arg { |
| 87 | println!("FocusStack called with direction: {}", direction); |
| 88 | println!("Windows: {:?}", self.windows); |
| 89 | println!("Current focused: {:?}", self.focused_window); |
| 90 | self.cycle_focus(*direction)?; |
| 91 | println!("New focused: {:?}", self.focused_window); |
| 92 | } |
| 93 | } |
| 94 | KeyAction::Quit => { |
| 95 | println!("Quitting window manager"); |
| 96 | std::process::exit(0); |
| 97 | } |
| 98 | KeyAction::None => { |
| 99 | //no-op |
| 100 | } |
| 101 | } |
| 102 | Ok(()) |
| 103 | } |
| 104 | |
| 105 | pub fn cycle_focus(&mut self, direction: i32) -> Result<()> { |
| 106 | if self.windows.is_empty() { |
| 107 | return Ok(()); |
| 108 | } |
| 109 | |
| 110 | let next_window = if let Some(current) = self.focused_window { |
| 111 | if let Some(current_index) = self.windows.iter().position(|&w| w == current) { |
| 112 | let next_index = if direction > 0 { |
| 113 | (current_index + 1) % self.windows.len() |
| 114 | } else { |
| 115 | if current_index == 0 { |
| 116 | self.windows.len() - 1 |
| 117 | } else { |
| 118 | current_index - 1 |
| 119 | } |
| 120 | }; |
| 121 | self.windows[next_index] |
| 122 | } else { |
| 123 | self.windows[0] |
| 124 | } |
| 125 | } else { |
| 126 | self.windows[0] |
| 127 | }; |
| 128 | |
| 129 | self.set_focus(Some(next_window))?; |
| 130 | Ok(()) |
| 131 | } |
| 132 | |
| 133 | pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> { |
| 134 | println!("set_focus called with: {:?}", window); |
| 135 | self.focused_window = window; |
| 136 | |
| 137 | if let Some(win) = window { |
| 138 | self.connection |
| 139 | .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?; |
| 140 | self.connection.flush()?; |
| 141 | } |
| 142 | |
| 143 | self.update_focus_visuals()?; |
| 144 | Ok(()) |
| 145 | } |
| 146 | |
| 147 | fn update_focus_visuals(&self) -> Result<()> { |
| 148 | println!("Updating focus visuals for {} windows", self.windows.len()); |
| 149 | for &window in &self.windows { |
| 150 | let (border_color, border_width) = if self.focused_window == Some(window) { |
| 151 | println!("Window {} is FOCUSED (red border)", window); |
| 152 | (0xff0000, 2) |
| 153 | } else { |
| 154 | println!("Window {} is unfocused (gray border)", window); |
| 155 | (0x888888, 2) |
| 156 | }; |
| 157 | |
| 158 | self.connection.configure_window( |
| 159 | window, |
| 160 | &ConfigureWindowAux::new().border_width(border_width), |
| 161 | )?; |
| 162 | |
| 163 | self.connection.change_window_attributes( |
| 164 | window, |
| 165 | &ChangeWindowAttributesAux::new().border_pixel(border_color), |
| 166 | )?; |
| 167 | } |
| 168 | self.connection.flush()?; |
| 169 | Ok(()) |
| 170 | } |
| 171 | |
| 172 | fn handle_event(&mut self, event: Event) -> Result<()> { |
| 173 | match event { |
| 174 | Event::MapRequest(event) => { |
| 175 | self.connection.map_window(event.window)?; |
| 176 | self.windows.push(event.window); |
| 177 | self.apply_layout()?; |
| 178 | self.set_focus(Some(event.window))?; |
| 179 | } |
| 180 | Event::UnmapNotify(event) => { |
| 181 | if self.windows.contains(&event.window) { |
| 182 | println!("Window {} unmapped, removing from layout", event.window); |
| 183 | self.remove_window(event.window)?; |
| 184 | } |
| 185 | } |
| 186 | Event::DestroyNotify(event) => { |
| 187 | if self.windows.contains(&event.window) { |
| 188 | println!("Window {} destroyed, removing from layout", event.window); |
| 189 | self.remove_window(event.window)?; |
| 190 | } |
| 191 | } |
| 192 | Event::KeyPress(event) => { |
| 193 | let (action, arg) = keyboard::handle_key_press(event)?; |
| 194 | self.handle_key_action(action, arg)?; |
| 195 | } |
| 196 | _ => {} |
| 197 | } |
| 198 | Ok(()) |
| 199 | } |
| 200 | |
| 201 | fn apply_layout(&self) -> Result<()> { |
| 202 | let screen_width = self.screen.width_in_pixels as u32; |
| 203 | let screen_height = self.screen.height_in_pixels as u32; |
| 204 | let border_width = 2u32; |
| 205 | |
| 206 | let geometries = self |
| 207 | .layout |
| 208 | .arrange(&self.windows, screen_width, screen_height); |
| 209 | |
| 210 | for (window, geometry) in self.windows.iter().zip(geometries.iter()) { |
| 211 | // Adjust for full borders |
| 212 | let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 213 | let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 214 | |
| 215 | self.connection.configure_window( |
| 216 | *window, |
| 217 | &ConfigureWindowAux::new() |
| 218 | .x(geometry.x_coordinate) |
| 219 | .y(geometry.y_coordinate) |
| 220 | .width(adjusted_width) |
| 221 | .height(adjusted_height), |
| 222 | )?; |
| 223 | } |
| 224 | self.connection.flush()?; |
| 225 | return Ok(()); |
| 226 | } |
| 227 | |
| 228 | fn remove_window(&mut self, window: Window) -> Result<()> { |
| 229 | let initial_count = self.windows.len(); |
| 230 | self.windows.retain(|&w| w != window); |
| 231 | |
| 232 | if self.windows.len() < initial_count { |
| 233 | println!("Removed window {} from management", window); |
| 234 | |
| 235 | if self.focused_window == Some(window) { |
| 236 | let new_focus = if self.windows.is_empty() { |
| 237 | None |
| 238 | } else { |
| 239 | Some(self.windows[self.windows.len() - 1]) |
| 240 | }; |
| 241 | self.set_focus(new_focus)?; |
| 242 | } |
| 243 | |
| 244 | self.apply_layout()?; |
| 245 | } |
| 246 | |
| 247 | Ok(()) |
| 248 | } |
| 249 | } |