From 0244bba6e79684b3ffd2167874db51cf05084371 Mon Sep 17 00:00:00 2001 From: chlorospingus Date: Sat, 26 Apr 2025 00:55:49 -0700 Subject: [PATCH] Abolish Color struct in favour of plain u32s --- src/shm.rs | 8 +++----- src/wl_client.rs | 36 +++++++----------------------------- src/wl_shm.rs | 10 +++++++--- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/shm.rs b/src/shm.rs index 3ec5532..28ca335 100644 --- a/src/shm.rs +++ b/src/shm.rs @@ -1,7 +1,5 @@ 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, @@ -52,12 +50,12 @@ impl ShmPool { Ok(()) } - pub fn write(&mut self, data: &Vec, offset: isize) -> std::io::Result<()> { + pub fn write(&mut self, data: &Vec, offset: isize) -> std::io::Result<()> { // TODO: Bounds check unsafe { std::ptr::copy_nonoverlapping( - 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.as_ptr() as *const u32, // src: data as *const u32 + self.addr.offset(offset*4) as *mut u32, // dst: ShmPool address as *mut u32 data.len() ); } diff --git a/src/wl_client.rs b/src/wl_client.rs index 69a7985..1ad1690 100644 --- a/src/wl_client.rs +++ b/src/wl_client.rs @@ -8,35 +8,13 @@ struct WlHeader { 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 const BLACK: Self = Self { - alpha: 0xff, - red: 0, - green: 0, - blue: 0, - }; +pub mod color { + pub const WHITE: u32 = 0xffffffff; + pub const BLACK: u32 = 0xff000000; + pub const RED: u32 = 0xffff0000; + pub const GREEN: u32 = 0xff00ff00; + pub const BLUE: u32 = 0xff0000ff; + pub const SAPPHIRE: u32 = 0xff74c7ec; } pub struct WlClient { diff --git a/src/wl_shm.rs b/src/wl_shm.rs index 3abfebd..d7068a4 100644 --- a/src/wl_shm.rs +++ b/src/wl_shm.rs @@ -1,5 +1,5 @@ use std::{error::Error, io::Write, os::unix::net::SocketAncillary, u8}; -use crate::{shm, surface::UnsetErr, vec_utils::WlMessage, wl_client::Color, WlClient}; +use crate::{shm, surface::UnsetErr, vec_utils::WlMessage, wl_client::color, WlClient}; const STRIDE: usize = 4; @@ -13,8 +13,12 @@ impl WlClient { self.current_id += 1; self.shm_pool = Some(shm::ShmPool::new(width * height * STRIDE, self.current_id)?); - let data: Vec = vec![Color::RED; width * height]; - self.shm_pool.as_mut().unwrap().write(&data, 0)?; + self.shm_pool.as_mut().unwrap().write(&vec![color::RED; width * height/6], 0)?; + self.shm_pool.as_mut().unwrap().write(&vec![color::GREEN; width * height/6], (width*height*1/6) as isize)?; + self.shm_pool.as_mut().unwrap().write(&vec![color::BLUE; width * height/6], (width*height*2/6) as isize)?; + self.shm_pool.as_mut().unwrap().write(&vec![color::WHITE; width * height/6], (width*height*3/6) as isize)?; + self.shm_pool.as_mut().unwrap().write(&vec![color::BLACK; width * height/6], (width*height*4/6) as isize)?; + self.shm_pool.as_mut().unwrap().write(&vec![color::SAPPHIRE;width * height/6], (width*height*5/6) as isize)?; let object = self.shm_id.ok_or(UnsetErr("shm_id".to_string()))?; const OPCODE: u16 = 0;