Abolish Color struct in favour of plain u32s

This commit is contained in:
2025-04-26 00:55:49 -07:00
parent 0687016677
commit 0244bba6e7
3 changed files with 17 additions and 37 deletions

View File

@@ -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 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)] #[derive(Clone)]
pub struct ShmPool { pub struct ShmPool {
pub fd: i32, pub fd: i32,
@@ -52,12 +50,12 @@ impl ShmPool {
Ok(()) Ok(())
} }
pub fn write(&mut self, data: &Vec<Color>, offset: isize) -> std::io::Result<()> { pub fn write(&mut self, data: &Vec<u32>, offset: isize) -> std::io::Result<()> {
// TODO: Bounds check // TODO: Bounds check
unsafe { unsafe {
std::ptr::copy_nonoverlapping( std::ptr::copy_nonoverlapping(
data.as_ptr().offset(offset) as *const Color, // src: data as *const Color data.as_ptr() as *const u32, // src: data as *const u32
self.addr.offset(2) as *mut Color, // dst: ShmPool address as *mut Color self.addr.offset(offset*4) as *mut u32, // dst: ShmPool address as *mut u32
data.len() data.len()
); );
} }

View File

@@ -8,35 +8,13 @@ struct WlHeader {
size: u16, size: u16,
} }
#[derive(Clone)] pub mod color {
pub struct Color { pub const WHITE: u32 = 0xffffffff;
pub alpha: u8, pub const BLACK: u32 = 0xff000000;
pub red: u8, pub const RED: u32 = 0xffff0000;
pub green: u8, pub const GREEN: u32 = 0xff00ff00;
pub blue: u8, pub const BLUE: u32 = 0xff0000ff;
} pub const SAPPHIRE: u32 = 0xff74c7ec;
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 struct WlClient { pub struct WlClient {

View File

@@ -1,5 +1,5 @@
use std::{error::Error, io::Write, os::unix::net::SocketAncillary, u8}; 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; const STRIDE: usize = 4;
@@ -13,8 +13,12 @@ impl WlClient {
self.current_id += 1; self.current_id += 1;
self.shm_pool = Some(shm::ShmPool::new(width * height * STRIDE, self.current_id)?); self.shm_pool = Some(shm::ShmPool::new(width * height * STRIDE, self.current_id)?);
let data: Vec<Color> = vec![Color::RED; width * height]; self.shm_pool.as_mut().unwrap().write(&vec![color::RED; width * height/6], 0)?;
self.shm_pool.as_mut().unwrap().write(&data, 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()))?; let object = self.shm_id.ok_or(UnsetErr("shm_id".to_string()))?;
const OPCODE: u16 = 0; const OPCODE: u16 = 0;