Create wl_surface object and attach already created wl_buffer to it

This commit is contained in:
2025-04-24 01:29:54 -07:00
parent aed1525c06
commit 0be889b758
5 changed files with 125 additions and 8 deletions

View File

@@ -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<dyn Error>> {

65
src/surface.rs Normal file
View File

@@ -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<dyn Error>> {
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<dyn Error>> {
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(())
}
}

View File

@@ -13,7 +13,11 @@ pub struct WlClient {
pub current_id: u32,
pub registry_id: Option<u32>,
pub shm_id: Option<u32>,
pub shm_pool: Option<shm::ShmPool>
pub shm_pool: Option<shm::ShmPool>,
pub buffer_id: Option<u32>,
pub compositor_id: Option<u32>,
pub surface_id: Option<u32>,
pub xdg_wm_base_id: Option<u32>,
}
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)

View File

@@ -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<dyn Error>> {
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<dyn Error>> {
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(())

View File

@@ -89,6 +89,8 @@ impl WlClient {
self.socket.write(&request)?;
self.buffer_id = Some(self.current_id);
Ok(())
}