| 1 | use crate::keyboard; |
| 2 | // use crate::keys; |
| 3 | use crate::layout::Layout; |
| 4 | use crate::layout::tiling::TilingLayout; |
| 5 | |
| 6 | use anyhow::Result; |
| 7 | |
| 8 | use x11rb::connection::Connection; |
| 9 | use x11rb::protocol::Event; |
| 10 | use x11rb::protocol::xproto::*; |
| 11 | use x11rb::rust_connection::RustConnection; |
| 12 | |
| 13 | pub struct WindowManager { |
| 14 | connection: RustConnection, |
| 15 | screen_number: usize, |
| 16 | root: Window, |
| 17 | screen: Screen, |
| 18 | windows: Vec<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 | layout: Box::new(TilingLayout), |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | pub fn run(&mut self) -> Result<()> { |
| 51 | println!("oxwm started on display {}", self.screen_number); |
| 52 | |
| 53 | keyboard::setup_keybinds(&self.connection, self.root)?; |
| 54 | |
| 55 | loop { |
| 56 | let event = self.connection.wait_for_event()?; |
| 57 | println!("event: {:?}", event); |
| 58 | self.handle_event(event)?; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn handle_event(&mut self, event: Event) -> Result<()> { |
| 63 | match event { |
| 64 | Event::MapRequest(event) => { |
| 65 | self.connection.map_window(event.window)?; |
| 66 | self.windows.push(event.window); |
| 67 | self.apply_layout()?; |
| 68 | self.connection.set_input_focus( |
| 69 | InputFocus::POINTER_ROOT, |
| 70 | event.window, |
| 71 | x11rb::CURRENT_TIME, |
| 72 | )?; |
| 73 | self.connection.flush()?; |
| 74 | } |
| 75 | Event::KeyPress(event) => { |
| 76 | println!("KeyPress event received!"); |
| 77 | keyboard::handle_key_press(&self.connection, event)?; |
| 78 | } |
| 79 | _ => {} |
| 80 | } |
| 81 | return Ok(()); |
| 82 | } |
| 83 | |
| 84 | fn apply_layout(&self) -> Result<()> { |
| 85 | let screen_width = self.screen.width_in_pixels as u32; |
| 86 | let screen_height = self.screen.height_in_pixels as u32; |
| 87 | |
| 88 | let geometries = self |
| 89 | .layout |
| 90 | .arrange(&self.windows, screen_width, screen_height); |
| 91 | |
| 92 | for (window, geometry) in self.windows.iter().zip(geometries.iter()) { |
| 93 | self.connection.configure_window( |
| 94 | *window, |
| 95 | &ConfigureWindowAux::new() |
| 96 | .x(geometry.x_coordinate) |
| 97 | .y(geometry.y_coordinate) |
| 98 | .width(geometry.width) |
| 99 | .height(geometry.height) |
| 100 | .border_width(1), |
| 101 | )?; |
| 102 | } |
| 103 | return Ok(()); |
| 104 | } |
| 105 | } |