tony / oxwm

Branch: d5b3499
Switch branches/tags
master
tonybtw Added blocks... lol d5b3499 2025-10-04
375 lines · 11.5 KB Raw Blame History
1 use crate::bar::Bar;
2 use crate::config::{BORDER_FOCUSED, BORDER_UNFOCUSED, BORDER_WIDTH, TAG_COUNT};
3 use crate::keyboard::{self, Arg, KeyAction};
4 use crate::layout::tiling::TilingLayout;
5 use crate::layout::Layout;
6 use anyhow::Result;
7
8 use x11rb::connection::Connection;
9 use x11rb::protocol::xproto::*;
10 use x11rb::protocol::Event;
11 use x11rb::rust_connection::RustConnection;
12
13 pub type TagMask = u32;
14 pub fn tag_mask(tag: usize) -> TagMask {
15 1 << tag
16 }
17
18 pub struct WindowManager {
19 connection: RustConnection,
20 screen_number: usize,
21 root: Window,
22 screen: Screen,
23 windows: Vec<Window>,
24 focused_window: Option<Window>,
25 layout: Box<dyn Layout>,
26 window_tags: std::collections::HashMap<Window, TagMask>,
27 selected_tags: TagMask,
28 bar: Bar,
29 }
30
31 impl WindowManager {
32 pub fn new() -> Result<Self> {
33 let (connection, screen_number) = x11rb::connect(None)?;
34 let root = connection.setup().roots[screen_number].root;
35 let screen = connection.setup().roots[screen_number].clone();
36
37 connection
38 .change_window_attributes(
39 root,
40 &ChangeWindowAttributesAux::new().event_mask(
41 EventMask::SUBSTRUCTURE_REDIRECT
42 | EventMask::SUBSTRUCTURE_NOTIFY
43 | EventMask::PROPERTY_CHANGE
44 | EventMask::KEY_PRESS,
45 ),
46 )?
47 .check()?;
48
49 let bar = Bar::new(&connection, &screen, screen_number)?;
50
51 return Ok(Self {
52 connection,
53 screen_number,
54 root,
55 screen,
56 windows: Vec::new(),
57 focused_window: None,
58 layout: Box::new(TilingLayout),
59 window_tags: std::collections::HashMap::new(),
60 selected_tags: tag_mask(0),
61 bar,
62 });
63 }
64
65 pub fn run(&mut self) -> Result<()> {
66 println!("oxwm started on display {}", self.screen_number);
67
68 keyboard::setup_keybinds(&self.connection, self.root)?;
69
70 self.update_bar()?;
71
72 loop {
73 self.bar.update_blocks()?;
74
75 if let Ok(Some(event)) = self.connection.poll_for_event() {
76 self.handle_event(event)?;
77 }
78
79 if self.bar.needs_redraw() {
80 self.update_bar()?;
81 }
82
83 std::thread::sleep(std::time::Duration::from_millis(10));
84 }
85 }
86
87 // pub fn run(&mut self) -> Result<()> {
88 // println!("oxwm started on display {}", self.screen_number);
89 //
90 // keyboard::setup_keybinds(&self.connection, self.root)?;
91 //
92 // self.update_bar()?;
93 //
94 // loop {
95 // let event = self.connection.wait_for_event()?;
96 // self.handle_event(event)?;
97 // }
98 // }
99
100 fn update_bar(&mut self) -> Result<()> {
101 let mut occupied_tags: TagMask = 0;
102 for &tags in self.window_tags.values() {
103 occupied_tags |= tags;
104 }
105
106 self.bar.invalidate();
107 self.bar
108 .draw(&self.connection, self.selected_tags, occupied_tags)?;
109 Ok(())
110 }
111
112 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
113 match action {
114 KeyAction::Spawn => match arg {
115 Arg::Str(command) => {
116 std::process::Command::new(command).spawn()?;
117 }
118 Arg::Array(cmd) => {
119 if let Some((program, args)) = cmd.split_first() {
120 std::process::Command::new(program).args(args).spawn()?;
121 }
122 }
123 _ => {}
124 },
125 KeyAction::KillClient => {
126 if let Some(focused) = self.focused_window {
127 match self.connection.kill_client(focused) {
128 Ok(_) => {
129 self.connection.flush()?;
130 }
131 Err(e) => {
132 eprintln!("Failed to kill window {}: {}", focused, e);
133 }
134 }
135 }
136 }
137 KeyAction::FocusStack => {
138 if let Arg::Int(direction) = arg {
139 self.cycle_focus(*direction)?;
140 }
141 }
142 KeyAction::Quit => {
143 std::process::exit(0);
144 }
145 KeyAction::ViewTag => {
146 if let Arg::Int(tag_index) = arg {
147 self.view_tag(*tag_index as usize)?;
148 }
149 }
150 KeyAction::MoveToTag => {
151 if let Arg::Int(tag_index) = arg {
152 self.move_to_tag(*tag_index as usize)?;
153 }
154 }
155 KeyAction::None => {
156 //no-op
157 }
158 }
159 Ok(())
160 }
161
162 fn is_window_visible(&self, window: Window) -> bool {
163 if let Some(&tags) = self.window_tags.get(&window) {
164 (tags & self.selected_tags) != 0
165 } else {
166 false
167 }
168 }
169
170 fn visible_windows(&self) -> Vec<Window> {
171 self.windows
172 .iter()
173 .filter(|&&w| self.is_window_visible(w))
174 .copied()
175 .collect()
176 }
177
178 fn update_window_visibility(&self) -> Result<()> {
179 for &window in &self.windows {
180 if self.is_window_visible(window) {
181 self.connection.map_window(window)?;
182 } else {
183 self.connection.unmap_window(window)?;
184 }
185 }
186 self.connection.flush()?;
187 Ok(())
188 }
189
190 pub fn view_tag(&mut self, tag_index: usize) -> Result<()> {
191 if tag_index >= TAG_COUNT {
192 return Ok(());
193 }
194
195 self.selected_tags = tag_mask(tag_index);
196 self.update_window_visibility()?;
197 self.apply_layout()?;
198 self.update_bar()?;
199
200 let visible = self.visible_windows();
201 self.set_focus(visible.first().copied())?;
202
203 Ok(())
204 }
205
206 pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> {
207 if tag_index >= TAG_COUNT {
208 return Ok(());
209 }
210
211 if let Some(focused) = self.focused_window {
212 let mask = tag_mask(tag_index);
213 self.window_tags.insert(focused, mask);
214 self.update_window_visibility()?;
215 self.apply_layout()?;
216 self.update_bar()?;
217 }
218
219 Ok(())
220 }
221
222 pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
223 let visible = self.visible_windows();
224
225 if visible.is_empty() {
226 return Ok(());
227 }
228
229 let next_window = if let Some(current) = self.focused_window {
230 if let Some(current_index) = visible.iter().position(|&w| w == current) {
231 let next_index = if direction > 0 {
232 (current_index + 1) % visible.len()
233 } else {
234 (current_index + visible.len() - 1) % visible.len()
235 };
236 visible[next_index]
237 } else {
238 visible[0]
239 }
240 } else {
241 visible[0]
242 };
243
244 self.set_focus(Some(next_window))?;
245 Ok(())
246 }
247
248 pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
249 self.focused_window = window;
250
251 if let Some(win) = window {
252 self.connection
253 .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
254 self.connection.flush()?;
255 }
256
257 self.update_focus_visuals()?;
258 Ok(())
259 }
260
261 fn update_focus_visuals(&self) -> Result<()> {
262 for &window in &self.windows {
263 let (border_color, border_width) = if self.focused_window == Some(window) {
264 (BORDER_FOCUSED, BORDER_WIDTH)
265 } else {
266 (BORDER_UNFOCUSED, BORDER_WIDTH)
267 };
268
269 self.connection.configure_window(
270 window,
271 &ConfigureWindowAux::new().border_width(border_width),
272 )?;
273
274 self.connection.change_window_attributes(
275 window,
276 &ChangeWindowAttributesAux::new().border_pixel(border_color),
277 )?;
278 }
279 self.connection.flush()?;
280 Ok(())
281 }
282
283 fn handle_event(&mut self, event: Event) -> Result<()> {
284 match event {
285 Event::MapRequest(event) => {
286 self.connection.map_window(event.window)?;
287 self.windows.push(event.window);
288 self.window_tags.insert(event.window, self.selected_tags);
289 self.apply_layout()?;
290 self.update_bar()?;
291 self.set_focus(Some(event.window))?;
292 }
293 Event::UnmapNotify(event) => {
294 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
295 self.remove_window(event.window)?;
296 }
297 }
298 Event::DestroyNotify(event) => {
299 if self.windows.contains(&event.window) {
300 self.remove_window(event.window)?;
301 }
302 }
303 Event::KeyPress(event) => {
304 let (action, arg) = keyboard::handle_key_press(event)?;
305 self.handle_key_action(action, arg)?;
306 }
307 Event::ButtonPress(event) => {
308 if event.event == self.bar.window() {
309 if let Some(tag_index) = self.bar.handle_click(event.event_x) {
310 self.view_tag(tag_index)?;
311 }
312 }
313 }
314 Event::Expose(event) => {
315 if event.window == self.bar.window() {
316 self.bar.invalidate();
317 self.update_bar()?;
318 }
319 }
320 _ => {}
321 }
322 Ok(())
323 }
324
325 fn apply_layout(&self) -> Result<()> {
326 let screen_width = self.screen.width_in_pixels as u32;
327 let screen_height = self.screen.height_in_pixels as u32;
328 let border_width = BORDER_WIDTH;
329
330 let bar_height = self.bar.height() as u32;
331 let usable_height = screen_height.saturating_sub(bar_height);
332
333 let visible = self.visible_windows();
334 let geometries = self.layout.arrange(&visible, screen_width, usable_height);
335
336 for (window, geometry) in visible.iter().zip(geometries.iter()) {
337 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
338 let adjusted_height = geometry.height.saturating_sub(2 * border_width);
339
340 let adjusted_y = geometry.y_coordinate + bar_height as i32;
341
342 self.connection.configure_window(
343 *window,
344 &ConfigureWindowAux::new()
345 .x(geometry.x_coordinate)
346 .y(adjusted_y)
347 .width(adjusted_width)
348 .height(adjusted_height),
349 )?;
350 }
351 self.connection.flush()?;
352 Ok(())
353 }
354
355 fn remove_window(&mut self, window: Window) -> Result<()> {
356 let initial_count = self.windows.len();
357 self.windows.retain(|&w| w != window);
358 self.window_tags.remove(&window);
359
360 if self.windows.len() < initial_count {
361 if self.focused_window == Some(window) {
362 let new_focus = if self.windows.is_empty() {
363 None
364 } else {
365 Some(self.windows[self.windows.len() - 1])
366 };
367 self.set_focus(new_focus)?;
368 }
369
370 self.apply_layout()?;
371 self.update_bar()?;
372 }
373 Ok(())
374 }
375 }