| 1 | use crate::keys; |
| 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 | layout: Box<dyn Layout>, |
| 19 | } |
| 20 | |
| 21 | impl WindowManager { |
| 22 | pub fn new() -> Result<Self> { |
| 23 | let (connection, screen_number) = x11rb::connect(None)?; |
| 24 | let root = connection.setup().roots[screen_number].root; |
| 25 | let screen = connection.setup().roots[screen_number].clone(); |
| 26 | |
| 27 | connection |
| 28 | .change_window_attributes( |
| 29 | root, |
| 30 | &ChangeWindowAttributesAux::new().event_mask( |
| 31 | EventMask::SUBSTRUCTURE_REDIRECT |
| 32 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 33 | | EventMask::PROPERTY_CHANGE |
| 34 | | EventMask::KEY_PRESS, |
| 35 | ), |
| 36 | )? |
| 37 | .check()?; |
| 38 | |
| 39 | return Ok(Self { |
| 40 | connection, |
| 41 | screen_number, |
| 42 | root, |
| 43 | screen, |
| 44 | windows: Vec::new(), |
| 45 | layout: Box::new(TilingLayout), |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | pub fn run(&mut self) -> Result<()> { |
| 50 | println!("oxwm started on display {}", self.screen_number); |
| 51 | |
| 52 | keys::setup_keybinds(&self.connection, self.root)?; |
| 53 | |
| 54 | loop { |
| 55 | let event = self.connection.wait_for_event()?; |
| 56 | println!("event: {:?}", event); |
| 57 | self.handle_event(event)?; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | fn handle_event(&mut self, event: Event) -> Result<()> { |
| 62 | match event { |
| 63 | Event::MapRequest(event) => { |
| 64 | self.connection.map_window(event.window)?; |
| 65 | self.windows.push(event.window); |
| 66 | self.apply_layout()?; |
| 67 | self.connection.set_input_focus( |
| 68 | InputFocus::POINTER_ROOT, |
| 69 | event.window, |
| 70 | x11rb::CURRENT_TIME, |
| 71 | )?; |
| 72 | self.connection.flush()?; |
| 73 | } |
| 74 | Event::KeyPress(event) => { |
| 75 | println!("KeyPress event received!"); |
| 76 | keys::handle_key_press(&self.connection, event)?; |
| 77 | } |
| 78 | _ => {} |
| 79 | } |
| 80 | return Ok(()); |
| 81 | } |
| 82 | |
| 83 | fn apply_layout(&self) -> Result<()> { |
| 84 | let screen_width = self.screen.width_in_pixels as u32; |
| 85 | let screen_height = self.screen.height_in_pixels as u32; |
| 86 | |
| 87 | let geometries = self |
| 88 | .layout |
| 89 | .arrange(&self.windows, screen_width, screen_height); |
| 90 | |
| 91 | for (window, geometry) in self.windows.iter().zip(geometries.iter()) { |
| 92 | self.connection.configure_window( |
| 93 | *window, |
| 94 | &ConfigureWindowAux::new() |
| 95 | .x(geometry.x_coordinate) |
| 96 | .y(geometry.y_coordinate) |
| 97 | .width(geometry.width) |
| 98 | .height(geometry.height) |
| 99 | .border_width(1), |
| 100 | )?; |
| 101 | } |
| 102 | return Ok(()); |
| 103 | } |
| 104 | } |