Use color struct to define argb colors

Also impl Debug for WlClient, preferred buffer scale and transform
This commit is contained in:
2025-04-25 17:57:20 -07:00
parent 0cf379c6f7
commit fea56134b0
4 changed files with 81 additions and 33 deletions

View File

@@ -4,7 +4,7 @@ use crate::{surface::UnsetErr, vec_utils::WlMessage, WlClient};
const NAMESPACE: &str = "chlorostart";
const OVERLAY: u32 = 3;
const STRIDE: usize = 4;
const EXCLUSIVE: u32 = 1; // exclusize keyboard focus
const EXCLUSIVE: u32 = 0; // exclusize keyboard focus
impl WlClient {
pub fn layer_shell_get_layer_surface(&mut self) -> Result<(), Box<dyn Error>> {

View File

@@ -1,5 +1,7 @@
use libc::{c_void, ftruncate, mmap, munmap, shm_open, shm_unlink, MAP_FAILED, MAP_SHARED, O_CREAT, O_EXCL, O_RDWR, PROT_READ, PROT_WRITE};
use crate::wl_client::Color;
#[derive(Clone)]
pub struct ShmPool {
pub fd: i32,
@@ -33,8 +35,6 @@ impl ShmPool {
return Err(std::io::Error::last_os_error())
}
unsafe { std::ptr::copy_nonoverlapping(vec![u8::MAX; size].as_ptr(), addr as *mut u8, size); }
Ok(ShmPool {
fd,
id,
@@ -52,12 +52,12 @@ impl ShmPool {
Ok(())
}
pub fn write(&mut self, data: &Vec<u8>, offset: usize) -> std::io::Result<()> {
pub fn write(&mut self, data: &Vec<Color>, offset: isize) -> std::io::Result<()> {
// TODO: Bounds check
unsafe {
std::ptr::copy_nonoverlapping(
data.as_ptr(), // src: data as *const u8
self.addr as *mut u8, // dst: ShmPool address as *mut u8
data.as_ptr().offset(offset) as *const Color, // src: data as *const Color
self.addr.offset(2) as *mut Color, // dst: ShmPool address as *mut Color
data.len()
);
}

View File

@@ -1,11 +1,35 @@
use std::{env::var, error::Error, io::Read, os::unix::net::UnixStream, u32};
use std::{env::var, error::Error, fmt::Debug, io::Read, os::unix::net::UnixStream, u32};
use crate::shm;
use crate::{shm, vec_utils::WlMessage};
struct WlHeader {
object: u32,
opcode: u16,
size: u16
size: u16,
}
#[derive(Clone)]
pub struct Color {
pub alpha: u8,
pub red: u8,
pub green: u8,
pub blue: u8,
}
impl Color {
pub const WHITE: Self = Self {
alpha: u8::MAX,
red: u8::MAX,
green: u8::MAX,
blue: u8::MAX,
};
pub const RED: Self = Self {
alpha: 0xff,
red: 0xff,
green: 0,
blue: 0,
};
}
pub struct WlClient {
@@ -77,6 +101,13 @@ impl WlClient {
else if Some(header.object) == self.layer_surface_id && header.opcode == 0 { // zwlr_layer_surface::configure
self.layer_surface_configure(&event)?;
}
else if Some(header.object) == self.surface_id && header.opcode == 2 { // wl_surface::preferred_buffer_scale
println!("Preferred buffer scale: {}", i32::from_ne_bytes(event[0..4].try_into().unwrap()));
}
else if Some(header.object) == self.surface_id && header.opcode == 3 { // wl_surface::preferred_buffer_transform
println!("Preferred buffer transform: {}", i32::from_ne_bytes(event[0..4].try_into().unwrap()));
dbg!(self);
}
else {
println!(
"Received event:\n\tObject: {}\n\tOpcode: {}\n\tSize: {}",
@@ -89,3 +120,30 @@ impl WlClient {
Ok(())
}
}
impl Debug for WlClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,
"WlClient {{
current_id: {},
registry_id: {},
shm_id: {},
buffer_id: {},
compositor_id: {},
surface_id: {},
xdg_wm_base_id: {},
layer_shell_id: {},
layer_surface_id: {},
}}",
self.current_id,
self.registry_id.unwrap_or(0),
self.shm_id.unwrap_or(0),
self.buffer_id.unwrap_or(0),
self.compositor_id.unwrap_or(0),
self.surface_id.unwrap_or(0),
self.xdg_wm_base_id.unwrap_or(0),
self.layer_shell_id.unwrap_or(0),
self.layer_surface_id.unwrap_or(0),
)
}
}

View File

@@ -1,5 +1,5 @@
use std::{io::Write, error::Error, os::unix::net::SocketAncillary};
use crate::{WlClient, vec_utils::WlMessage, shm};
use std::{error::Error, io::Write, os::unix::net::SocketAncillary, u8};
use crate::{shm, surface::UnsetErr, vec_utils::WlMessage, wl_client::Color, WlClient};
const STRIDE: usize = 4;
@@ -9,21 +9,14 @@ impl WlClient {
println!("Received pixel format: {:x}", event.read_u32(&mut offset));
}
pub fn wl_shm_create_pool(&mut self, width: usize, height: usize) -> Result<(), String> {
pub fn wl_shm_create_pool(&mut self, width: usize, height: usize) -> Result<(), Box<dyn Error>> {
self.current_id += 1;
self.shm_pool = Some(match shm::ShmPool::new(width * height * STRIDE, self.current_id) {
Ok(val) => val,
Err(err) => {
return Err(err.to_string());
}
});
self.shm_pool = Some(shm::ShmPool::new(width * height * STRIDE, self.current_id)?);
let object = match self.shm_id {
Some(val) => val,
None => {
return Err("error in wl_shm_create_pool: shm_id not set!".to_string());
}
};
let data: Vec<Color> = vec![Color::RED; width * height];
self.shm_pool.as_mut().unwrap().write(&data, 0)?;
let object = self.shm_id.ok_or(UnsetErr("shm_id".to_string()))?;
const OPCODE: u16 = 0;
const REQ_SIZE: u16 = 16;
let id = self.shm_pool.as_ref().unwrap().id;
@@ -44,18 +37,15 @@ impl WlClient {
let mut ancillary_buf = [0u8; 32];
let mut ancillary = SocketAncillary::new(&mut ancillary_buf[..]);
if !ancillary.add_fds(&fds[..]) {
return Err("Error in wl_shm_create_pool: ancillary.add_fds failed".to_string());
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to add FDs to ancillary data",
).into());
}
match self.socket.send_vectored_with_ancillary(&[std::io::IoSlice::new(&request)], &mut ancillary) {
Ok(bytes) => {
assert!(bytes == REQ_SIZE as usize);
}
Err(err) => {
return Err(err.to_string());
}
};
self.socket.send_vectored_with_ancillary(&[std::io::IoSlice::new(&request)], &mut ancillary)?;
Ok(())
}