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.
Browse files
+1 −5 justfile
| @@ -1,8 +1,4 @@ | ||
| 1 | 1 | test: |
| 2 | 2 | pkill Xephyr || true |
| 3 | 3 | Xephyr -screen 1280x800 :1 & sleep 1 |
| 4 | - DISPLAY=:1 cargo run & | |
| 5 | - sleep 1 | |
| 6 | - DISPLAY=:1 xterm & | |
| 7 | - DISPLAY=:1 xclock & | |
| 8 | - wait | |
| 4 | + DISPLAY=:1 cargo run |
+19 −0 src/layout/mod.rs new file mode 100644
| @@ -0,0 +1,19 @@ | ||
| 1 | +pub mod tiling; | |
| 2 | + | |
| 3 | +use x11rb::protocol::xproto::Window; | |
| 4 | + | |
| 5 | +pub trait Layout { | |
| 6 | + fn arrange( | |
| 7 | + &self, | |
| 8 | + windows: &[Window], | |
| 9 | + screen_width: u32, | |
| 10 | + screen_height: u32, | |
| 11 | + ) -> Vec<WindowGeometry>; | |
| 12 | +} | |
| 13 | + | |
| 14 | +pub struct WindowGeometry { | |
| 15 | + pub x_coordinate: i32, | |
| 16 | + pub y_coordinate: i32, | |
| 17 | + pub width: u32, | |
| 18 | + pub height: u32, | |
| 19 | +} |
+47 −0 src/layout/tiling.rs new file mode 100644
| @@ -0,0 +1,47 @@ | ||
| 1 | +use super::{Layout, WindowGeometry}; | |
| 2 | +use x11rb::protocol::xproto::Window; | |
| 3 | + | |
| 4 | +pub struct TilingLayout; | |
| 5 | + | |
| 6 | +impl Layout for TilingLayout { | |
| 7 | + fn arrange( | |
| 8 | + &self, | |
| 9 | + windows: &[Window], | |
| 10 | + screen_width: u32, | |
| 11 | + screen_height: u32, | |
| 12 | + ) -> Vec<WindowGeometry> { | |
| 13 | + let count = windows.len(); | |
| 14 | + if count == 0 { | |
| 15 | + return Vec::new(); | |
| 16 | + } | |
| 17 | + | |
| 18 | + if count == 1 { | |
| 19 | + vec![WindowGeometry { | |
| 20 | + x_coordinate: 0, | |
| 21 | + y_coordinate: 0, | |
| 22 | + width: screen_width, | |
| 23 | + height: screen_height, | |
| 24 | + }] | |
| 25 | + } else { | |
| 26 | + let master_width = screen_width / 2; | |
| 27 | + let mut geometries = vec![WindowGeometry { | |
| 28 | + x_coordinate: 0, | |
| 29 | + y_coordinate: 0, | |
| 30 | + width: master_width, | |
| 31 | + height: screen_height, | |
| 32 | + }]; | |
| 33 | + | |
| 34 | + let stack_height = screen_height / (count - 1) as u32; | |
| 35 | + for i in 1..count { | |
| 36 | + let y_offset = ((i - 1) as u32) * stack_height; | |
| 37 | + geometries.push(WindowGeometry { | |
| 38 | + x_coordinate: master_width as i32, | |
| 39 | + y_coordinate: y_offset as i32, | |
| 40 | + width: master_width, | |
| 41 | + height: stack_height, | |
| 42 | + }); | |
| 43 | + } | |
| 44 | + return geometries; | |
| 45 | + } | |
| 46 | + } | |
| 47 | +} |
+4 −53 src/main.rs
| @@ -1,57 +1,8 @@ | ||
| 1 | 1 | use anyhow::Result; |
| 2 | -use x11rb::connection::Connection; | |
| 3 | -use x11rb::protocol::Event; | |
| 4 | -use x11rb::protocol::xproto::*; | |
| 5 | -use x11rb::rust_connection::RustConnection; | |
| 2 | +mod layout; | |
| 3 | +mod window_manager; | |
| 6 | 4 | |
| 7 | 5 | fn main() -> Result<()> { |
| 8 | - let (connection, screen_number) = x11rb::connect(None)?; | |
| 9 | - let root = connection.setup().roots[screen_number].root; | |
| 10 | - let screen = &connection.setup().roots[screen_number]; | |
| 11 | - | |
| 12 | - connection | |
| 13 | - .change_window_attributes( | |
| 14 | - root, | |
| 15 | - &ChangeWindowAttributesAux::new().event_mask( | |
| 16 | - EventMask::SUBSTRUCTURE_REDIRECT | |
| 17 | - | EventMask::SUBSTRUCTURE_NOTIFY | |
| 18 | - | EventMask::PROPERTY_CHANGE, | |
| 19 | - ), | |
| 20 | - )? | |
| 21 | - .check()?; | |
| 22 | - | |
| 23 | - println!("oxwm started on display {}", screen_number); | |
| 24 | - | |
| 25 | - let mut window_count = 0; | |
| 26 | - loop { | |
| 27 | - let event = connection.wait_for_event()?; | |
| 28 | - println!("event: {:?}", event); | |
| 29 | - match event { | |
| 30 | - Event::MapRequest(event) => { | |
| 31 | - connection.map_window(event.window)?; | |
| 32 | - let x_coordinate = if window_count == 0 { | |
| 33 | - 0 | |
| 34 | - } else { | |
| 35 | - (screen.width_in_pixels / 2) as i32 | |
| 36 | - }; | |
| 37 | - connection.configure_window( | |
| 38 | - event.window, | |
| 39 | - &ConfigureWindowAux::new() | |
| 40 | - .x(x_coordinate) | |
| 41 | - .y(0) | |
| 42 | - .border_width(1) | |
| 43 | - .width((screen.width_in_pixels / 2) as u32) | |
| 44 | - .height(screen.height_in_pixels as u32), | |
| 45 | - )?; | |
| 46 | - window_count += 1; | |
| 47 | - connection.set_input_focus( | |
| 48 | - InputFocus::POINTER_ROOT, | |
| 49 | - event.window, | |
| 50 | - x11rb::CURRENT_TIME, | |
| 51 | - )?; | |
| 52 | - connection.flush()?; | |
| 53 | - } | |
| 54 | - _ => {} | |
| 55 | - } | |
| 56 | - } | |
| 6 | + let mut window_manager = window_manager::WindowManager::new()?; | |
| 7 | + return window_manager.run(); | |
| 57 | 8 | } |
+94 −0 src/window_manager.rs new file mode 100644
| @@ -0,0 +1,94 @@ | ||
| 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 | +} |