tony / oxwm

Branch: 6aeb5aa
Switch branches/tags
master
tonybtw Added layout module, and dynamic tiling layout instead of hard coded layout. Changed just file to just open xephyr, need to run xclock from a 2nd terminal. 2456898 2025-09-26
94 lines · 2.8 KB Raw Blame History
1 use crate::layout::Layout;
2 use crate::layout::tiling::TilingLayout;
3
4 use anyhow::Result;
5
6 use x11rb::connection::Connection;
7 use x11rb::protocol::Event;
8 use x11rb::protocol::xproto::*;
9 use x11rb::rust_connection::RustConnection;
10
11 pub struct WindowManager {
12 connection: RustConnection,
13 screen_number: usize,
14 screen: Screen,
15 windows: Vec<Window>,
16 layout: Box<dyn Layout>,
17 }
18
19 impl WindowManager {
20 pub fn new() -> Result<Self> {
21 let (connection, screen_number) = x11rb::connect(None)?;
22 let root = connection.setup().roots[screen_number].root;
23 let screen = connection.setup().roots[screen_number].clone();
24
25 connection
26 .change_window_attributes(
27 root,
28 &ChangeWindowAttributesAux::new().event_mask(
29 EventMask::SUBSTRUCTURE_REDIRECT
30 | EventMask::SUBSTRUCTURE_NOTIFY
31 | EventMask::PROPERTY_CHANGE,
32 ),
33 )?
34 .check()?;
35
36 return Ok(Self {
37 connection,
38 screen_number,
39 screen,
40 windows: Vec::new(),
41 layout: Box::new(TilingLayout),
42 });
43 }
44
45 pub fn run(&mut self) -> Result<()> {
46 println!("oxwm started on display {}", self.screen_number);
47
48 loop {
49 let event = self.connection.wait_for_event()?;
50 println!("event: {:?}", event);
51 self.handle_event(event)?;
52 }
53 }
54
55 fn handle_event(&mut self, event: Event) -> Result<()> {
56 match event {
57 Event::MapRequest(event) => {
58 self.connection.map_window(event.window)?;
59 self.windows.push(event.window);
60 self.apply_layout()?;
61 self.connection.set_input_focus(
62 InputFocus::POINTER_ROOT,
63 event.window,
64 x11rb::CURRENT_TIME,
65 )?;
66 self.connection.flush()?;
67 }
68 _ => {}
69 }
70 return Ok(());
71 }
72
73 fn apply_layout(&self) -> Result<()> {
74 let screen_width = self.screen.width_in_pixels as u32;
75 let screen_height = self.screen.height_in_pixels as u32;
76
77 let geometries = self
78 .layout
79 .arrange(&self.windows, screen_width, screen_height);
80
81 for (window, geometry) in self.windows.iter().zip(geometries.iter()) {
82 self.connection.configure_window(
83 *window,
84 &ConfigureWindowAux::new()
85 .x(geometry.x_coordinate)
86 .y(geometry.y_coordinate)
87 .width(geometry.width)
88 .height(geometry.height)
89 .border_width(1),
90 )?;
91 }
92 return Ok(());
93 }
94 }