From 0be889b75869039c34f7dca064f2b02ef57d9b8e Mon Sep 17 00:00:00 2001 From: chlorospingus Date: Thu, 24 Apr 2025 01:29:54 -0700 Subject: [PATCH] Create wl_surface object and attach already created wl_buffer to it --- src/main.rs | 1 + src/surface.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++ src/wl_client.rs | 20 +++++++++----- src/wl_registry.rs | 45 ++++++++++++++++++++++++++++++-- src/wl_shm.rs | 2 ++ 5 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 src/surface.rs diff --git a/src/main.rs b/src/main.rs index b55dfaf..9aa9f8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod wl_registry; mod wl_shm; mod vec_utils; mod shm; +mod surface; use wl_client::WlClient; fn main() -> Result<(), Box> { diff --git a/src/surface.rs b/src/surface.rs new file mode 100644 index 0000000..2ad7252 --- /dev/null +++ b/src/surface.rs @@ -0,0 +1,65 @@ +use std::{error::Error, io::Write}; + +use crate::{vec_utils::WlMessage, WlClient}; + +#[derive(Debug)] +pub struct UnsetErr (String); + +impl Error for UnsetErr {} +impl std::fmt::Display for UnsetErr { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} not set!", self.0) + } +} + +impl WlClient { + pub fn wl_compositor_create_surface(&mut self) -> Result<(), Box> { + if self.compositor_id.is_none() { + return Err(Box::new(UnsetErr("compositor_id".to_string()))); + } + + let object = self.compositor_id.unwrap(); + const OPCODE: u16 = 0; + const MSG_SIZE: u16 = 12; + + let mut request = vec![0u8; MSG_SIZE as usize]; + let mut offset: usize = 0; + + request.write_u32(&object, &mut offset); + request.write_u16(&OPCODE, &mut offset); + request.write_u16(&MSG_SIZE, &mut offset); + + self.current_id += 1; + request.write_u32(&self.current_id, &mut offset); + + self.socket.write(&request)?; + + self.surface_id = Some(self.current_id); + + Ok(()) + } + + pub fn wl_surface_attach(&mut self) -> Result<(), Box> { + if self.surface_id.is_none() { + return Err(Box::new(UnsetErr("surface_id".to_string()))); + } + let object = self.surface_id.unwrap(); + const OPCODE: u16 = 1; + const MSG_SIZE: u16 = 20; + const X: u32 = 0; + const Y: u32 = 0; + + let mut request = vec![0u8; MSG_SIZE as usize]; + let mut offset: usize = 0; + + request.write_u32(&object, &mut offset); + request.write_u16(&OPCODE, &mut offset); + request.write_u16(&MSG_SIZE, &mut offset); + request.write_u32(&X, &mut offset); + request.write_u32(&Y, &mut offset); + + self.socket.write(&request)?; + + Ok(()) + } +} diff --git a/src/wl_client.rs b/src/wl_client.rs index 96ce870..19254b1 100644 --- a/src/wl_client.rs +++ b/src/wl_client.rs @@ -13,7 +13,11 @@ pub struct WlClient { pub current_id: u32, pub registry_id: Option, pub shm_id: Option, - pub shm_pool: Option + pub shm_pool: Option, + pub buffer_id: Option, + pub compositor_id: Option, + pub surface_id: Option, + pub xdg_wm_base_id: Option, } impl WlClient { @@ -25,11 +29,15 @@ impl WlClient { ))?; let res = WlClient { - socket: sock, - current_id: 1, - registry_id: None, - shm_id: None, - shm_pool: None, + socket: sock, + current_id: 1, + registry_id: None, + shm_id: None, + shm_pool: None, + buffer_id: None, + compositor_id: None, + surface_id: None, + xdg_wm_base_id: None, }; Ok(res) diff --git a/src/wl_registry.rs b/src/wl_registry.rs index 0442512..bc6fae8 100644 --- a/src/wl_registry.rs +++ b/src/wl_registry.rs @@ -2,6 +2,17 @@ use crate::{WlClient, vec_utils::WlMessage}; use std::{io::Write, error::Error}; impl WlClient { + fn init_toplevel(&mut self) -> Result<(), Box> { + println!("----> shm and compositor found!"); + self.wl_shm_create_pool()?; + self.wl_shm_pool_create_buffer(0, 200, 200)?; + self.wl_compositor_create_surface()?; + self.wl_surface_attach()?; + + + Ok(()) + } + pub fn wl_display_get_registry(&mut self) -> Result<(), Box> { const OBJECT: u32 = 1; const OPCODE: u16 = 1; @@ -47,9 +58,39 @@ impl WlClient { &self.current_id.clone() )?; self.shm_id = Some(self.current_id); + if self.compositor_id.is_some() && self.xdg_wm_base_id.is_some() { + self.init_toplevel()?; + } + } - self.wl_shm_create_pool()?; - self.wl_shm_pool_create_buffer(0, 200, 200)?; + if interface == "wl_compositor" { + self.current_id += 1; + self.wl_registry_bind( + &name, + &interface, + &version, + &self.current_id.clone() + )?; + self.compositor_id = Some(self.current_id); + + if self.shm_id.is_some() && self.xdg_wm_base_id.is_some() { + self.init_toplevel()?; + } + } + + if interface == "xdg_wm_base" { + self.current_id += 1; + self.wl_registry_bind( + &name, + &interface, + &version, + &self.current_id.clone() + )?; + self.xdg_wm_base_id = Some(self.current_id); + + if self.shm_id.is_some() && self.compositor_id.is_some() { + self.init_toplevel()?; + } } Ok(()) diff --git a/src/wl_shm.rs b/src/wl_shm.rs index bb34f70..5299a0f 100644 --- a/src/wl_shm.rs +++ b/src/wl_shm.rs @@ -89,6 +89,8 @@ impl WlClient { self.socket.write(&request)?; + self.buffer_id = Some(self.current_id); + Ok(()) }