tony / oxwm

Branch: f82213a
Switch branches/tags
master
tonybtw Addressed let/ok comment and surpressed dead code warning on unused function that will be re-implemented later. f82213a 2025-10-25
1526 lines · 49.2 KB Raw Blame History
1 use crate::Config;
2 use crate::bar::Bar;
3 use crate::errors::WmError;
4 use crate::keyboard::{self, Arg, KeyAction, handlers};
5 use crate::layout::GapConfig;
6 use crate::layout::tiling::TilingLayout;
7 use crate::layout::{Layout, layout_from_str, next_layout};
8 use crate::monitor::{Monitor, detect_monitors};
9 use std::collections::HashSet;
10 use x11rb::cursor::Handle as CursorHandle;
11
12 use x11rb::connection::Connection;
13 use x11rb::protocol::Event;
14 use x11rb::protocol::xproto::*;
15 use x11rb::rust_connection::RustConnection;
16
17 pub type TagMask = u32;
18 pub fn tag_mask(tag: usize) -> TagMask {
19 1 << tag
20 }
21
22 struct AtomCache {
23 net_current_desktop: Atom,
24 net_client_info: Atom,
25 wm_state: Atom,
26 }
27
28 impl AtomCache {
29 fn new(connection: &RustConnection) -> WmResult<Self> {
30 let net_current_desktop = connection
31 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
32 .reply()?
33 .atom;
34
35 let net_client_info = connection
36 .intern_atom(false, b"_NET_CLIENT_INFO")?
37 .reply()?
38 .atom;
39
40 let wm_state = connection.intern_atom(false, b"WM_STATE")?.reply()?.atom;
41
42 Ok(Self {
43 net_current_desktop,
44 net_client_info,
45 wm_state,
46 })
47 }
48 }
49
50 pub struct WindowManager {
51 config: Config,
52 connection: RustConnection,
53 screen_number: usize,
54 root: Window,
55 screen: Screen,
56 windows: Vec<Window>,
57 layout: Box<dyn Layout>,
58 window_tags: std::collections::HashMap<Window, TagMask>,
59 window_monitor: std::collections::HashMap<Window, usize>,
60 window_geometries: std::collections::HashMap<Window, (i16, i16, u16, u16)>,
61 gaps_enabled: bool,
62 fullscreen_window: Option<Window>,
63 floating_windows: HashSet<Window>,
64 bars: Vec<Bar>,
65 monitors: Vec<Monitor>,
66 selected_monitor: usize,
67 atoms: AtomCache,
68 previous_focused: Option<Window>,
69 display: *mut x11::xlib::Display,
70 font: crate::bar::font::Font,
71 }
72
73 type WmResult<T> = Result<T, WmError>;
74
75 impl WindowManager {
76 pub fn new(config: Config) -> WmResult<Self> {
77 let (connection, screen_number) = x11rb::connect(None)?;
78 let root = connection.setup().roots[screen_number].root;
79 let screen = connection.setup().roots[screen_number].clone();
80
81 let normal_cursor = CursorHandle::new(
82 &connection,
83 screen_number,
84 &x11rb::resource_manager::new_from_default(&connection)?,
85 )?
86 .reply()?
87 .load_cursor(&connection, "left_ptr")?;
88
89 connection
90 .change_window_attributes(
91 root,
92 &ChangeWindowAttributesAux::new()
93 .cursor(normal_cursor)
94 .event_mask(
95 EventMask::SUBSTRUCTURE_REDIRECT
96 | EventMask::SUBSTRUCTURE_NOTIFY
97 | EventMask::PROPERTY_CHANGE
98 | EventMask::KEY_PRESS
99 | EventMask::BUTTON_PRESS
100 | EventMask::POINTER_MOTION,
101 ),
102 )?
103 .check()?;
104
105 connection.grab_button(
106 false,
107 root,
108 EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
109 GrabMode::SYNC,
110 GrabMode::ASYNC,
111 x11rb::NONE,
112 x11rb::NONE,
113 ButtonIndex::M1,
114 u16::from(config.modkey).into(),
115 )?;
116
117 connection.grab_button(
118 false,
119 root,
120 EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
121 GrabMode::SYNC,
122 GrabMode::ASYNC,
123 x11rb::NONE,
124 x11rb::NONE,
125 ButtonIndex::M3,
126 u16::from(config.modkey).into(),
127 )?;
128
129 let monitors = detect_monitors(&connection, &screen, root)?;
130
131 let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
132 if display.is_null() {
133 return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed));
134 }
135
136 let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?;
137
138 let mut bars = Vec::new();
139 for monitor in monitors.iter() {
140 let bar = Bar::new(
141 &connection,
142 &screen,
143 screen_number,
144 &config,
145 display,
146 &font,
147 monitor.x as i16,
148 monitor.y as i16,
149 monitor.width as u16,
150 )?;
151 bars.push(bar);
152 }
153
154 let gaps_enabled = config.gaps_enabled;
155
156 let atoms = AtomCache::new(&connection)?;
157
158 let mut window_manager = Self {
159 config,
160 connection,
161 screen_number,
162 root,
163 screen,
164 windows: Vec::new(),
165 layout: Box::new(TilingLayout),
166 window_tags: std::collections::HashMap::new(),
167 window_monitor: std::collections::HashMap::new(),
168 window_geometries: std::collections::HashMap::new(),
169 gaps_enabled,
170 fullscreen_window: None,
171 floating_windows: HashSet::new(),
172 bars,
173 monitors,
174 selected_monitor: 0,
175 atoms,
176 previous_focused: None,
177 display,
178 font,
179 };
180
181 window_manager.scan_existing_windows()?;
182 window_manager.update_bar()?;
183
184 Ok(window_manager)
185 }
186
187 /**
188 *
189 * This function is deprecated for now, but will potentially be used in the future.
190 *
191 */
192 fn _get_saved_selected_tags(
193 connection: &RustConnection,
194 root: Window,
195 tag_count: usize,
196 ) -> WmResult<TagMask> {
197 let net_current_desktop = connection
198 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
199 .reply()?
200 .atom;
201
202 match connection
203 .get_property(false, root, net_current_desktop, AtomEnum::CARDINAL, 0, 1)?
204 .reply()
205 {
206 Ok(prop) if prop.value.len() >= 4 => {
207 let desktop = u32::from_ne_bytes([
208 prop.value[0],
209 prop.value[1],
210 prop.value[2],
211 prop.value[3],
212 ]);
213 if desktop < tag_count as u32 {
214 let mask = tag_mask(desktop as usize);
215 return Ok(mask);
216 }
217 }
218 Ok(_) => {}
219 Err(e) => {
220 eprintln!("No _NET_CURRENT_DESKTOP property ({})", e);
221 }
222 }
223 Ok(tag_mask(0))
224 }
225
226 fn scan_existing_windows(&mut self) -> WmResult<()> {
227 let tree = self.connection.query_tree(self.root)?.reply()?;
228 let net_client_info = self.atoms.net_client_info;
229 let wm_state_atom = self.atoms.wm_state;
230
231 for &window in &tree.children {
232 if self.bars.iter().any(|bar| bar.window() == window) {
233 continue;
234 }
235
236 let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() else {
237 continue;
238 };
239
240 if attrs.override_redirect {
241 continue;
242 }
243
244 if attrs.map_state == MapState::VIEWABLE {
245 let tag = self.get_saved_tag(window, net_client_info)?;
246 self.windows.push(window);
247 self.window_tags.insert(window, tag);
248 self.window_monitor.insert(window, self.selected_monitor);
249 continue;
250 }
251
252 if attrs.map_state == MapState::UNMAPPED {
253 let has_wm_state = self
254 .connection
255 .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)?
256 .reply()
257 .is_ok_and(|prop| !prop.value.is_empty());
258
259 if !has_wm_state {
260 continue;
261 }
262
263 let has_wm_class = self
264 .connection
265 .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)?
266 .reply()
267 .is_ok_and(|prop| !prop.value.is_empty());
268
269 if has_wm_class {
270 let tag = self.get_saved_tag(window, net_client_info)?;
271 self.windows.push(window);
272 self.window_tags.insert(window, tag);
273 self.window_monitor.insert(window, self.selected_monitor);
274 }
275 }
276 }
277
278 if let Some(&first) = self.windows.first() {
279 self.set_focus(first)?;
280 }
281
282 self.apply_layout()?;
283 Ok(())
284 }
285
286 fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> {
287 match self
288 .connection
289 .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)?
290 .reply()
291 {
292 Ok(prop) if prop.value.len() >= 4 => {
293 let tags = u32::from_ne_bytes([
294 prop.value[0],
295 prop.value[1],
296 prop.value[2],
297 prop.value[3],
298 ]);
299
300 if tags != 0 && tags < (1 << self.config.tags.len()) {
301 return Ok(tags);
302 }
303 }
304 Ok(_) => {}
305 Err(e) => {
306 eprintln!("No _NET_CLIENT_INFO property ({})", e);
307 }
308 }
309
310 Ok(self
311 .monitors
312 .get(self.selected_monitor)
313 .map(|m| m.selected_tags)
314 .unwrap_or(tag_mask(0)))
315 }
316
317 fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> {
318 let net_client_info = self.atoms.net_client_info;
319
320 let bytes = tag.to_ne_bytes().to_vec();
321
322 self.connection.change_property(
323 PropMode::REPLACE,
324 window,
325 net_client_info,
326 AtomEnum::CARDINAL,
327 32,
328 1,
329 &bytes,
330 )?;
331
332 self.connection.flush()?;
333 Ok(())
334 }
335
336 fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> {
337 let wm_state_atom = self.atoms.wm_state;
338
339 let data = [state, 0u32];
340 let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect();
341
342 self.connection.change_property(
343 PropMode::REPLACE,
344 window,
345 wm_state_atom,
346 wm_state_atom,
347 32,
348 2,
349 &bytes,
350 )?;
351
352 self.connection.flush()?;
353 Ok(())
354 }
355
356 pub fn run(&mut self) -> WmResult<bool> {
357 println!("oxwm started on display {}", self.screen_number);
358
359 keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?;
360 self.update_bar()?;
361
362 loop {
363 while let Some(event) = self.connection.poll_for_event()? {
364 if let Some(should_restart) = self.handle_event(event)? {
365 return Ok(should_restart);
366 }
367 }
368
369 if let Some(bar) = self.bars.get_mut(self.selected_monitor) {
370 bar.update_blocks();
371 }
372
373 if self.bars.iter().any(|bar| bar.needs_redraw()) {
374 self.update_bar()?;
375 }
376
377 std::thread::sleep(std::time::Duration::from_millis(100));
378 }
379 }
380
381 fn toggle_floating(&mut self) -> WmResult<()> {
382 if let Some(focused) = self
383 .monitors
384 .get(self.selected_monitor)
385 .and_then(|m| m.focused_window)
386 {
387 if self.floating_windows.contains(&focused) {
388 self.floating_windows.remove(&focused);
389 self.apply_layout()?;
390 } else {
391 let float_width = (self.screen.width_in_pixels / 2) as u32;
392 let float_height = (self.screen.height_in_pixels / 2) as u32;
393
394 let border_width = self.config.border_width;
395
396 let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32;
397 let center_height =
398 ((self.screen.height_in_pixels - float_height as u16) / 2) as i32;
399
400 self.connection.configure_window(
401 focused,
402 &ConfigureWindowAux::new()
403 .x(center_width)
404 .y(center_height)
405 .width(float_width)
406 .height(float_height)
407 .border_width(border_width)
408 .stack_mode(StackMode::ABOVE),
409 )?;
410
411 self.floating_windows.insert(focused);
412 self.apply_layout()?;
413 self.connection.flush()?;
414 }
415 }
416 Ok(())
417 }
418
419 fn smart_move_window(&mut self, direction: i32) -> WmResult<()> {
420 let focused = match self
421 .monitors
422 .get(self.selected_monitor)
423 .and_then(|m| m.focused_window)
424 {
425 Some(win) => win,
426 None => return Ok(()),
427 };
428
429 if self.fullscreen_window == Some(focused) {
430 return Ok(());
431 }
432
433 if !self.floating_windows.contains(&focused) {
434 let float_width = (self.screen.width_in_pixels / 2) as u32;
435 let float_height = (self.screen.height_in_pixels / 2) as u32;
436 let border_width = self.config.border_width;
437 let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32;
438 let center_height = ((self.screen.height_in_pixels - float_height as u16) / 2) as i32;
439
440 self.connection.configure_window(
441 focused,
442 &ConfigureWindowAux::new()
443 .x(center_width)
444 .y(center_height)
445 .width(float_width)
446 .height(float_height)
447 .border_width(border_width)
448 .stack_mode(StackMode::ABOVE),
449 )?;
450 self.floating_windows.insert(focused);
451 }
452
453 let current_geom = match self.connection.get_geometry(focused)?.reply() {
454 Ok(geom) => geom,
455 Err(_) => return Ok(()),
456 };
457
458 let c_x = current_geom.x as i32;
459 let c_y = current_geom.y as i32;
460 let c_width = current_geom.width as i32;
461 let c_height = current_geom.height as i32;
462
463 let monitor = match self.monitors.get(self.selected_monitor) {
464 Some(m) => m,
465 None => return Ok(()),
466 };
467
468 let (gap_ih, gap_iv, gap_oh, gap_ov) = if self.gaps_enabled {
469 (
470 self.config.gap_inner_horizontal as i32,
471 self.config.gap_inner_vertical as i32,
472 self.config.gap_outer_horizontal as i32,
473 self.config.gap_outer_vertical as i32,
474 )
475 } else {
476 (0, 0, 0, 0)
477 };
478
479 let (new_x, new_y) = match direction {
480 0 => {
481 // UP
482 let mut target = i32::MIN;
483 let top = c_y;
484 let mut ny = c_y - (monitor.height as i32 / 4);
485
486 for &other_window in &self.windows {
487 if other_window == focused {
488 continue;
489 }
490 if !self.floating_windows.contains(&other_window) {
491 continue;
492 }
493 if !self.is_window_visible(other_window) {
494 continue;
495 }
496 let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0);
497 if other_mon != self.selected_monitor {
498 continue;
499 }
500
501 let other_geom = match self.connection.get_geometry(other_window)?.reply() {
502 Ok(geom) => geom,
503 Err(_) => continue,
504 };
505
506 let o_x = other_geom.x as i32;
507 let o_y = other_geom.y as i32;
508 let o_width = other_geom.width as i32;
509 let o_height = other_geom.height as i32;
510
511 if c_x + c_width < o_x || c_x > o_x + o_width {
512 continue;
513 }
514
515 let bottom = o_y + o_height + gap_iv;
516 if top > bottom && ny < bottom {
517 target = target.max(bottom);
518 }
519 }
520
521 if target != i32::MIN {
522 ny = target;
523 }
524 ny = ny.max(monitor.y as i32 + gap_ov);
525 (c_x, ny)
526 }
527 1 => {
528 // DOWN
529 let mut target = i32::MAX;
530 let bottom = c_y + c_height;
531 let mut ny = c_y + (monitor.height as i32 / 4);
532
533 for &other_window in &self.windows {
534 if other_window == focused {
535 continue;
536 }
537 if !self.floating_windows.contains(&other_window) {
538 continue;
539 }
540 if !self.is_window_visible(other_window) {
541 continue;
542 }
543 let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0);
544 if other_mon != self.selected_monitor {
545 continue;
546 }
547
548 let other_geom = match self.connection.get_geometry(other_window)?.reply() {
549 Ok(geom) => geom,
550 Err(_) => continue,
551 };
552
553 let o_x = other_geom.x as i32;
554 let o_y = other_geom.y as i32;
555 let o_width = other_geom.width as i32;
556
557 if c_x + c_width < o_x || c_x > o_x + o_width {
558 continue;
559 }
560
561 let top = o_y - gap_iv;
562 if bottom < top && (ny + c_height) > top {
563 target = target.min(top - c_height);
564 }
565 }
566
567 if target != i32::MAX {
568 ny = target;
569 }
570 ny = ny.min(monitor.y as i32 + monitor.height as i32 - c_height - gap_ov);
571 (c_x, ny)
572 }
573 2 => {
574 // LEFT
575 let mut target = i32::MIN;
576 let left = c_x;
577 let mut nx = c_x - (monitor.width as i32 / 6);
578
579 for &other_window in &self.windows {
580 if other_window == focused {
581 continue;
582 }
583 if !self.floating_windows.contains(&other_window) {
584 continue;
585 }
586 if !self.is_window_visible(other_window) {
587 continue;
588 }
589 let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0);
590 if other_mon != self.selected_monitor {
591 continue;
592 }
593
594 let other_geom = match self.connection.get_geometry(other_window)?.reply() {
595 Ok(geom) => geom,
596 Err(_) => continue,
597 };
598
599 let o_x = other_geom.x as i32;
600 let o_y = other_geom.y as i32;
601 let o_width = other_geom.width as i32;
602 let o_height = other_geom.height as i32;
603
604 if c_y + c_height < o_y || c_y > o_y + o_height {
605 continue;
606 }
607
608 let right = o_x + o_width + gap_ih;
609 if left > right && nx < right {
610 target = target.max(right);
611 }
612 }
613
614 if target != i32::MIN {
615 nx = target;
616 }
617 nx = nx.max(monitor.x as i32 + gap_oh);
618 (nx, c_y)
619 }
620 3 => {
621 // RIGHT
622 let mut target = i32::MAX;
623 let right = c_x + c_width;
624 let mut nx = c_x + (monitor.width as i32 / 6);
625
626 for &other_window in &self.windows {
627 if other_window == focused {
628 continue;
629 }
630 if !self.floating_windows.contains(&other_window) {
631 continue;
632 }
633 if !self.is_window_visible(other_window) {
634 continue;
635 }
636 let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0);
637 if other_mon != self.selected_monitor {
638 continue;
639 }
640
641 let other_geom = match self.connection.get_geometry(other_window)?.reply() {
642 Ok(geom) => geom,
643 Err(_) => continue,
644 };
645
646 let o_x = other_geom.x as i32;
647 let o_y = other_geom.y as i32;
648 let o_height = other_geom.height as i32;
649
650 if c_y + c_height < o_y || c_y > o_y + o_height {
651 continue;
652 }
653
654 let left = o_x - gap_ih;
655 if right < left && (nx + c_width) > left {
656 target = target.min(left - c_width);
657 }
658 }
659
660 if target != i32::MAX {
661 nx = target;
662 }
663 nx = nx.min(monitor.x as i32 + monitor.width as i32 - c_width - gap_oh);
664 (nx, c_y)
665 }
666 _ => return Ok(()),
667 };
668
669 self.connection.configure_window(
670 focused,
671 &ConfigureWindowAux::new()
672 .x(new_x)
673 .y(new_y)
674 .stack_mode(StackMode::ABOVE),
675 )?;
676
677 self.connection.flush()?;
678 Ok(())
679 }
680
681 fn exchange_client(&mut self, direction: i32) -> WmResult<()> {
682 let focused = match self
683 .monitors
684 .get(self.selected_monitor)
685 .and_then(|m| m.focused_window)
686 {
687 Some(win) => win,
688 None => return Ok(()),
689 };
690
691 if self.fullscreen_window == Some(focused) || self.floating_windows.contains(&focused) {
692 return Ok(());
693 }
694
695 let visible = self.visible_windows();
696 if visible.len() < 2 {
697 return Ok(());
698 }
699
700 let current_idx = match visible.iter().position(|&w| w == focused) {
701 Some(idx) => idx,
702 None => return Ok(()),
703 };
704
705 let target_idx = match direction {
706 0 | 2 => {
707 // UP or LEFT - previous in stack
708 if current_idx == 0 {
709 visible.len() - 1
710 } else {
711 current_idx - 1
712 }
713 }
714 1 | 3 => {
715 // DOWN or RIGHT - next in stack
716 (current_idx + 1) % visible.len()
717 }
718 _ => return Ok(()),
719 };
720
721 let target = visible[target_idx];
722
723 let focused_pos = self.windows.iter().position(|&w| w == focused);
724 let target_pos = self.windows.iter().position(|&w| w == target);
725
726 if let (Some(f_pos), Some(t_pos)) = (focused_pos, target_pos) {
727 self.windows.swap(f_pos, t_pos);
728
729 self.apply_layout()?;
730
731 self.set_focus(focused)?;
732
733 if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() {
734 self.connection.warp_pointer(
735 x11rb::NONE,
736 focused,
737 0,
738 0,
739 0,
740 0,
741 geometry.width as i16 / 2,
742 geometry.height as i16 / 2,
743 )?;
744 }
745 }
746
747 Ok(())
748 }
749
750 fn toggle_fullscreen(&mut self) -> WmResult<()> {
751 if let Some(focused) = self
752 .monitors
753 .get(self.selected_monitor)
754 .and_then(|m| m.focused_window)
755 {
756 if self.fullscreen_window == Some(focused) {
757 self.fullscreen_window = None;
758
759 for bar in &self.bars {
760 self.connection.map_window(bar.window())?;
761 }
762
763 self.apply_layout()?;
764 } else {
765 self.fullscreen_window = Some(focused);
766
767 if let Some(bar) = self.bars.get(self.selected_monitor) {
768 self.connection.unmap_window(bar.window())?;
769 }
770
771 let monitor = &self.monitors[self.selected_monitor];
772 let screen_width = monitor.width;
773 let screen_height = monitor.height;
774
775 self.connection.configure_window(
776 focused,
777 &ConfigureWindowAux::new()
778 .x(monitor.x)
779 .y(monitor.y)
780 .width(screen_width)
781 .height(screen_height)
782 .border_width(0)
783 .stack_mode(StackMode::ABOVE),
784 )?;
785
786 self.connection.flush()?;
787 }
788 }
789 Ok(())
790 }
791
792 fn update_bar(&mut self) -> WmResult<()> {
793 for (monitor_index, monitor) in self.monitors.iter().enumerate() {
794 if let Some(bar) = self.bars.get_mut(monitor_index) {
795 let mut occupied_tags: TagMask = 0;
796 for (&window, &tags) in &self.window_tags {
797 if self.window_monitor.get(&window).copied().unwrap_or(0) == monitor_index {
798 occupied_tags |= tags;
799 }
800 }
801
802 let draw_blocks = monitor_index == self.selected_monitor;
803 bar.invalidate();
804 bar.draw(
805 &self.connection,
806 &self.font,
807 self.display,
808 monitor.selected_tags,
809 occupied_tags,
810 draw_blocks,
811 )?;
812 }
813 }
814 Ok(())
815 }
816
817 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> {
818 match action {
819 KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?,
820 KeyAction::KillClient => {
821 if let Some(focused) = self
822 .monitors
823 .get(self.selected_monitor)
824 .and_then(|m| m.focused_window)
825 {
826 match self.connection.kill_client(focused) {
827 Ok(_) => {
828 self.connection.flush()?;
829 }
830 Err(e) => {
831 eprintln!("Failed to kill window {}: {}", focused, e);
832 }
833 }
834 }
835 }
836 KeyAction::ToggleFullScreen => {
837 self.toggle_fullscreen()?;
838 }
839 KeyAction::ChangeLayout => {
840 if let Arg::Str(layout_name) = arg {
841 match layout_from_str(layout_name) {
842 Ok(layout) => {
843 self.layout = layout;
844 self.apply_layout()?;
845 }
846 Err(e) => eprintln!("Failed to change layout: {}", e),
847 }
848 }
849 }
850 KeyAction::CycleLayout => {
851 let current_name = self.layout.name();
852 let next_name = next_layout(current_name);
853 match layout_from_str(next_name) {
854 Ok(layout) => {
855 self.layout = layout;
856 self.apply_layout()?;
857 }
858 Err(e) => eprintln!("Failed to cycle layout: {}", e),
859 }
860 }
861 KeyAction::ToggleFloating => {
862 self.toggle_floating()?;
863 }
864
865 KeyAction::SmartMoveWin => {
866 if let Arg::Int(direction) = arg {
867 self.smart_move_window(*direction)?;
868 }
869 }
870
871 KeyAction::ExchangeClient => {
872 if let Arg::Int(direction) = arg {
873 self.exchange_client(*direction)?;
874 }
875 }
876
877 KeyAction::FocusStack => {
878 if let Arg::Int(direction) = arg {
879 self.cycle_focus(*direction)?;
880 }
881 }
882 KeyAction::Quit | KeyAction::Restart => {
883 // Handled in handle_event
884 }
885 KeyAction::Recompile => {
886 match std::process::Command::new("oxwm")
887 .arg("--recompile")
888 .spawn()
889 {
890 Ok(_) => eprintln!("Recompiling in background"),
891 Err(e) => eprintln!("Failed to spawn recompile: {}", e),
892 }
893 }
894 KeyAction::ViewTag => {
895 if let Arg::Int(tag_index) = arg {
896 self.view_tag(*tag_index as usize)?;
897 }
898 }
899 KeyAction::MoveToTag => {
900 if let Arg::Int(tag_index) = arg {
901 self.move_to_tag(*tag_index as usize)?;
902 }
903 }
904 KeyAction::ToggleGaps => {
905 self.gaps_enabled = !self.gaps_enabled;
906 self.apply_layout()?;
907 }
908 KeyAction::FocusMonitor => {
909 if let Arg::Int(direction) = arg {
910 self.focus_monitor(*direction)?;
911 }
912 }
913 KeyAction::None => {}
914 }
915 Ok(())
916 }
917
918 fn focus_monitor(&mut self, direction: i32) -> WmResult<()> {
919 if self.monitors.is_empty() {
920 return Ok(());
921 }
922
923 let new_monitor = if direction > 0 {
924 (self.selected_monitor + 1) % self.monitors.len()
925 } else {
926 (self.selected_monitor + self.monitors.len() - 1) % self.monitors.len()
927 };
928
929 if new_monitor == self.selected_monitor {
930 return Ok(());
931 }
932
933 self.selected_monitor = new_monitor;
934
935 self.update_bar()?;
936
937 let visible = self.visible_windows_on_monitor(new_monitor);
938 if let Some(&win) = visible.first() {
939 self.set_focus(win)?;
940 }
941
942 Ok(())
943 }
944
945 fn is_window_visible(&self, window: Window) -> bool {
946 let window_mon = self.window_monitor.get(&window).copied().unwrap_or(0);
947
948 if let Some(&tags) = self.window_tags.get(&window) {
949 let monitor = self.monitors.get(window_mon);
950 let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0);
951 (tags & selected_tags) != 0
952 } else {
953 false
954 }
955 }
956
957 fn visible_windows(&self) -> Vec<Window> {
958 self.windows
959 .iter()
960 .filter(|&&w| self.is_window_visible(w))
961 .copied()
962 .collect()
963 }
964
965 fn visible_windows_on_monitor(&self, monitor_index: usize) -> Vec<Window> {
966 self.windows
967 .iter()
968 .filter(|&&w| {
969 let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0);
970 if window_mon != monitor_index {
971 return false;
972 }
973 if let Some(&tags) = self.window_tags.get(&w) {
974 let monitor = self.monitors.get(monitor_index);
975 let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0);
976 (tags & selected_tags) != 0
977 } else {
978 false
979 }
980 })
981 .copied()
982 .collect()
983 }
984
985 fn get_monitor_at_point(&self, x: i32, y: i32) -> Option<usize> {
986 self.monitors
987 .iter()
988 .position(|mon| mon.contains_point(x, y))
989 }
990
991 fn update_window_visibility(&self) -> WmResult<()> {
992 for &window in &self.windows {
993 if self.is_window_visible(window) {
994 self.connection.map_window(window)?;
995 } else {
996 self.connection.unmap_window(window)?;
997 }
998 }
999 self.connection.flush()?;
1000 Ok(())
1001 }
1002
1003 pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> {
1004 if tag_index >= self.config.tags.len() {
1005 return Ok(());
1006 }
1007
1008 if self.fullscreen_window.is_some() {
1009 self.fullscreen_window = None;
1010 for bar in &self.bars {
1011 self.connection.map_window(bar.window())?;
1012 }
1013 }
1014
1015 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
1016 monitor.selected_tags = tag_mask(tag_index);
1017 }
1018
1019 self.save_selected_tags()?;
1020
1021 self.update_window_visibility()?;
1022 self.apply_layout()?;
1023 self.update_bar()?;
1024
1025 let visible = self.visible_windows_on_monitor(self.selected_monitor);
1026 if let Some(&win) = visible.first() {
1027 self.set_focus(win)?;
1028 }
1029
1030 Ok(())
1031 }
1032
1033 fn save_selected_tags(&self) -> WmResult<()> {
1034 let net_current_desktop = self.atoms.net_current_desktop;
1035
1036 let selected_tags = self
1037 .monitors
1038 .get(self.selected_monitor)
1039 .map(|m| m.selected_tags)
1040 .unwrap_or(tag_mask(0));
1041 let desktop = selected_tags.trailing_zeros();
1042
1043 let bytes = (desktop as u32).to_ne_bytes();
1044 self.connection.change_property(
1045 PropMode::REPLACE,
1046 self.root,
1047 net_current_desktop,
1048 AtomEnum::CARDINAL,
1049 32,
1050 1,
1051 &bytes,
1052 )?;
1053
1054 self.connection.flush()?;
1055 Ok(())
1056 }
1057
1058 pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> {
1059 if tag_index >= self.config.tags.len() {
1060 return Ok(());
1061 }
1062
1063 if let Some(focused) = self
1064 .monitors
1065 .get(self.selected_monitor)
1066 .and_then(|m| m.focused_window)
1067 {
1068 let mask = tag_mask(tag_index);
1069 self.window_tags.insert(focused, mask);
1070
1071 let _ = self.save_client_tag(focused, mask);
1072
1073 self.update_window_visibility()?;
1074 self.apply_layout()?;
1075 self.update_bar()?;
1076 }
1077
1078 Ok(())
1079 }
1080
1081 pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> {
1082 let visible = self.visible_windows();
1083
1084 if visible.is_empty() {
1085 return Ok(());
1086 }
1087
1088 let current = self
1089 .monitors
1090 .get(self.selected_monitor)
1091 .and_then(|m| m.focused_window);
1092
1093 let next_window = if let Some(current) = current {
1094 if let Some(current_index) = visible.iter().position(|&w| w == current) {
1095 let next_index = if direction > 0 {
1096 (current_index + 1) % visible.len()
1097 } else {
1098 (current_index + visible.len() - 1) % visible.len()
1099 };
1100 visible[next_index]
1101 } else {
1102 visible[0]
1103 }
1104 } else {
1105 visible[0]
1106 };
1107
1108 self.set_focus(next_window)?;
1109 Ok(())
1110 }
1111
1112 pub fn set_focus(&mut self, window: Window) -> WmResult<()> {
1113 let old_focused = self.previous_focused;
1114
1115 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
1116 monitor.focused_window = Some(window);
1117 }
1118
1119 self.connection
1120 .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?;
1121 self.connection.flush()?;
1122
1123 self.update_focus_visuals(old_focused, window)?;
1124 self.previous_focused = Some(window);
1125 Ok(())
1126 }
1127
1128 fn update_focus_visuals(
1129 &self,
1130 old_focused: Option<Window>,
1131 new_focused: Window,
1132 ) -> WmResult<()> {
1133 if let Some(old_win) = old_focused {
1134 if old_win != new_focused {
1135 self.connection.configure_window(
1136 old_win,
1137 &ConfigureWindowAux::new().border_width(self.config.border_width),
1138 )?;
1139
1140 self.connection.change_window_attributes(
1141 old_win,
1142 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused),
1143 )?;
1144 }
1145 }
1146
1147 self.connection.configure_window(
1148 new_focused,
1149 &ConfigureWindowAux::new().border_width(self.config.border_width),
1150 )?;
1151
1152 self.connection.change_window_attributes(
1153 new_focused,
1154 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused),
1155 )?;
1156
1157 self.connection.flush()?;
1158 Ok(())
1159 }
1160
1161 fn move_mouse(&mut self, window: Window) -> WmResult<()> {
1162 self.floating_windows.insert(window);
1163
1164 let geometry = self.connection.get_geometry(window)?.reply()?;
1165
1166 self.connection
1167 .grab_pointer(
1168 false,
1169 self.root,
1170 (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(),
1171 GrabMode::ASYNC,
1172 GrabMode::ASYNC,
1173 x11rb::NONE,
1174 x11rb::NONE,
1175 x11rb::CURRENT_TIME,
1176 )?
1177 .reply()?;
1178
1179 let pointer = self.connection.query_pointer(self.root)?.reply()?;
1180 let (start_x, start_y) = (pointer.root_x, pointer.root_y);
1181
1182 self.connection.configure_window(
1183 window,
1184 &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE),
1185 )?;
1186
1187 loop {
1188 let event = self.connection.wait_for_event()?;
1189 match event {
1190 Event::MotionNotify(e) => {
1191 let new_x = geometry.x + (e.root_x - start_x);
1192 let new_y = geometry.y + (e.root_y - start_y);
1193 self.connection.configure_window(
1194 window,
1195 &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32),
1196 )?;
1197 self.connection.flush()?;
1198 }
1199 Event::ButtonRelease(_) => break,
1200 _ => {}
1201 }
1202 }
1203
1204 self.connection
1205 .ungrab_pointer(x11rb::CURRENT_TIME)?
1206 .check()?;
1207 self.connection
1208 .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)?
1209 .check()?;
1210
1211 Ok(())
1212 }
1213
1214 fn resize_mouse(&mut self, window: Window) -> WmResult<()> {
1215 self.floating_windows.insert(window);
1216
1217 let geometry = self.connection.get_geometry(window)?.reply()?;
1218
1219 self.connection.warp_pointer(
1220 x11rb::NONE,
1221 window,
1222 0,
1223 0,
1224 0,
1225 0,
1226 geometry.width as i16,
1227 geometry.height as i16,
1228 )?;
1229
1230 self.connection
1231 .grab_pointer(
1232 false,
1233 self.root,
1234 (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(),
1235 GrabMode::ASYNC,
1236 GrabMode::ASYNC,
1237 x11rb::NONE,
1238 x11rb::NONE,
1239 x11rb::CURRENT_TIME,
1240 )?
1241 .reply()?;
1242
1243 self.connection.configure_window(
1244 window,
1245 &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE),
1246 )?;
1247
1248 loop {
1249 let event = self.connection.wait_for_event()?;
1250 match event {
1251 Event::MotionNotify(e) => {
1252 let new_width = (e.root_x - geometry.x).max(1) as u32;
1253 let new_height = (e.root_y - geometry.y).max(1) as u32;
1254
1255 self.connection.configure_window(
1256 window,
1257 &ConfigureWindowAux::new()
1258 .width(new_width)
1259 .height(new_height),
1260 )?;
1261 self.connection.flush()?;
1262 }
1263 Event::ButtonRelease(_) => break,
1264 _ => {}
1265 }
1266 }
1267
1268 self.connection
1269 .ungrab_pointer(x11rb::CURRENT_TIME)?
1270 .check()?;
1271 self.connection
1272 .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)?
1273 .check()?;
1274
1275 Ok(())
1276 }
1277
1278 fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> {
1279 match event {
1280 Event::MapRequest(event) => {
1281 let attrs = match self.connection.get_window_attributes(event.window)?.reply() {
1282 Ok(attrs) => attrs,
1283 Err(_) => return Ok(None),
1284 };
1285
1286 if attrs.override_redirect {
1287 return Ok(None);
1288 }
1289
1290 if self.windows.contains(&event.window) {
1291 return Ok(None);
1292 }
1293
1294 if let Ok(geom) = self.connection.get_geometry(event.window)?.reply() {
1295 self.window_geometries
1296 .insert(event.window, (geom.x, geom.y, geom.width, geom.height));
1297 }
1298
1299 self.connection.map_window(event.window)?;
1300 self.connection.change_window_attributes(
1301 event.window,
1302 &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW),
1303 )?;
1304
1305 let selected_tags = self
1306 .monitors
1307 .get(self.selected_monitor)
1308 .map(|m| m.selected_tags)
1309 .unwrap_or(tag_mask(0));
1310
1311 self.windows.push(event.window);
1312 self.window_tags.insert(event.window, selected_tags);
1313 self.window_monitor
1314 .insert(event.window, self.selected_monitor);
1315 self.set_wm_state(event.window, 1)?;
1316 let _ = self.save_client_tag(event.window, selected_tags);
1317
1318 self.apply_layout()?;
1319 self.update_bar()?;
1320 self.set_focus(event.window)?;
1321 }
1322 Event::UnmapNotify(event) => {
1323 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
1324 self.remove_window(event.window)?;
1325 }
1326 }
1327 Event::DestroyNotify(event) => {
1328 if self.windows.contains(&event.window) {
1329 self.remove_window(event.window)?;
1330 }
1331 }
1332 Event::EnterNotify(event) => {
1333 if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL {
1334 return Ok(None);
1335 }
1336 if self.windows.contains(&event.event) {
1337 self.set_focus(event.event)?;
1338 }
1339 }
1340 Event::MotionNotify(event) => {
1341 if event.event != self.root {
1342 return Ok(None);
1343 }
1344
1345 if let Some(monitor_index) =
1346 self.get_monitor_at_point(event.root_x as i32, event.root_y as i32)
1347 {
1348 if monitor_index != self.selected_monitor {
1349 self.selected_monitor = monitor_index;
1350 self.update_bar()?;
1351
1352 let visible = self.visible_windows_on_monitor(monitor_index);
1353 if let Some(&win) = visible.first() {
1354 self.set_focus(win)?;
1355 }
1356 }
1357 }
1358 }
1359 Event::KeyPress(event) => {
1360 let (action, arg) = keyboard::handle_key_press(event, &self.config.keybindings);
1361 match action {
1362 KeyAction::Quit => return Ok(Some(false)),
1363 KeyAction::Restart => return Ok(Some(true)),
1364 _ => self.handle_key_action(action, &arg)?,
1365 }
1366 }
1367 Event::ButtonPress(event) => {
1368 let is_bar_click = self
1369 .bars
1370 .iter()
1371 .enumerate()
1372 .find(|(_, bar)| bar.window() == event.event);
1373
1374 if let Some((monitor_index, bar)) = is_bar_click {
1375 if let Some(tag_index) = bar.handle_click(event.event_x) {
1376 if monitor_index != self.selected_monitor {
1377 self.selected_monitor = monitor_index;
1378 }
1379 self.view_tag(tag_index)?;
1380 }
1381 } else if event.child != x11rb::NONE {
1382 self.set_focus(event.child)?;
1383
1384 if event.detail == ButtonIndex::M1.into() {
1385 self.move_mouse(event.child)?;
1386 } else if event.detail == ButtonIndex::M3.into() {
1387 self.resize_mouse(event.child)?;
1388 }
1389 }
1390 }
1391 Event::Expose(event) => {
1392 for bar in &mut self.bars {
1393 if event.window == bar.window() {
1394 bar.invalidate();
1395 self.update_bar()?;
1396 break;
1397 }
1398 }
1399 }
1400 _ => {}
1401 }
1402 Ok(None)
1403 }
1404
1405 fn apply_layout(&self) -> WmResult<()> {
1406 if self.fullscreen_window.is_some() {
1407 return Ok(());
1408 }
1409
1410 if self.layout.name() == crate::layout::NORMIE {
1411 return Ok(());
1412 }
1413
1414 let border_width = self.config.border_width;
1415
1416 let gaps = if self.gaps_enabled {
1417 GapConfig {
1418 inner_horizontal: self.config.gap_inner_horizontal,
1419 inner_vertical: self.config.gap_inner_vertical,
1420 outer_horizontal: self.config.gap_outer_horizontal,
1421 outer_vertical: self.config.gap_outer_vertical,
1422 }
1423 } else {
1424 GapConfig {
1425 inner_horizontal: 0,
1426 inner_vertical: 0,
1427 outer_horizontal: 0,
1428 outer_vertical: 0,
1429 }
1430 };
1431
1432 for (monitor_index, monitor) in self.monitors.iter().enumerate() {
1433 let visible: Vec<Window> = self
1434 .windows
1435 .iter()
1436 .filter(|&&w| {
1437 let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0);
1438 if window_mon != monitor_index {
1439 return false;
1440 }
1441 if self.floating_windows.contains(&w) {
1442 return false;
1443 }
1444 if let Some(&tags) = self.window_tags.get(&w) {
1445 (tags & monitor.selected_tags) != 0
1446 } else {
1447 false
1448 }
1449 })
1450 .copied()
1451 .collect();
1452
1453 let bar_height = self
1454 .bars
1455 .get(monitor_index)
1456 .map(|b| b.height() as u32)
1457 .unwrap_or(0);
1458 let usable_height = monitor.height.saturating_sub(bar_height);
1459
1460 let geometries = self
1461 .layout
1462 .arrange(&visible, monitor.width, usable_height, &gaps);
1463
1464 for (window, geometry) in visible.iter().zip(geometries.iter()) {
1465 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
1466 let adjusted_height = geometry.height.saturating_sub(2 * border_width);
1467
1468 let adjusted_x = geometry.x_coordinate + monitor.x;
1469 let adjusted_y = geometry.y_coordinate + monitor.y + bar_height as i32;
1470
1471 self.connection.configure_window(
1472 *window,
1473 &ConfigureWindowAux::new()
1474 .x(adjusted_x)
1475 .y(adjusted_y)
1476 .width(adjusted_width)
1477 .height(adjusted_height),
1478 )?;
1479 }
1480 }
1481
1482 self.connection.flush()?;
1483 Ok(())
1484 }
1485
1486 pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> {
1487 self.layout = Box::new(new_layout);
1488 self.apply_layout()?;
1489 Ok(())
1490 }
1491
1492 fn remove_window(&mut self, window: Window) -> WmResult<()> {
1493 let initial_count = self.windows.len();
1494 self.windows.retain(|&w| w != window);
1495 self.window_tags.remove(&window);
1496 self.window_monitor.remove(&window);
1497 self.window_geometries.remove(&window);
1498 self.floating_windows.remove(&window);
1499
1500 if self.fullscreen_window == Some(window) {
1501 self.fullscreen_window = None;
1502 for bar in &self.bars {
1503 self.connection.map_window(bar.window())?;
1504 }
1505 }
1506
1507 if self.windows.len() < initial_count {
1508 let focused = self
1509 .monitors
1510 .get(self.selected_monitor)
1511 .and_then(|m| m.focused_window);
1512 if focused == Some(window) {
1513 let visible = self.visible_windows_on_monitor(self.selected_monitor);
1514 if let Some(&new_win) = visible.last() {
1515 self.set_focus(new_win)?;
1516 } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
1517 monitor.focused_window = None;
1518 }
1519 }
1520
1521 self.apply_layout()?;
1522 self.update_bar()?;
1523 }
1524 Ok(())
1525 }
1526 }