All members of WlClient except socket are atomic

This commit is contained in:
2025-05-06 12:38:36 -07:00
parent 375b44beee
commit 9583a87f2f
5 changed files with 77 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
use std::{error::Error, io::Write, sync::atomic::Ordering};
use crate::wayland::{vec_utils::WlMessage, wl_client::WlClient};
use crate::wayland::{surface::UnsetErr, vec_utils::WlMessage, wl_client::WlClient};
const NAMESPACE: &str = "chlorostart";
const OVERLAY: u32 = 3;
@@ -7,7 +7,11 @@ const EXCLUSIVE: u32 = 0; // exclusize keyboard focus
impl WlClient {
pub fn layer_shell_get_layer_surface(&mut self) -> Result<(), Box<dyn Error>> {
let object: u32 = self.layer_shell_id.unwrap();
// TODO: Make sure layer_surface_id isn't already set
let object: u32 = self.layer_shell_id.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("layer_shell_id".to_string()).into());
}
const OPCODE: u16 = 0;
let msg_size: u16 = 28 + (NAMESPACE.len()+1).next_multiple_of(4) as u16;
let output: u32 = 0;
@@ -21,13 +25,19 @@ impl WlClient {
let current_id = self.current_id.fetch_add(1, Ordering::Relaxed) + 1;
request.write_u32(&current_id, &mut offset);
request.write_u32(&self.surface_id.unwrap(), &mut offset);
let surface_id = self.surface_id.load(Ordering::Relaxed);
if surface_id == 0 {
return Err(UnsetErr("surface_id".to_string()).into());
}
request.write_u32(&surface_id, &mut offset);
request.write_u32(&output, &mut offset);
request.write_u32(&OVERLAY, &mut offset);
request.write_string(&NAMESPACE.to_string(), &mut offset);
self.socket.write(&request)?;
self.layer_surface_id = Some(current_id);
self.layer_surface_id.store(current_id, Ordering::Relaxed);
Ok(())
}
@@ -41,7 +51,10 @@ impl WlClient {
// TODO: Resize based on configure
// Ack configure
let object = self.layer_surface_id.unwrap();
let object = self.layer_surface_id.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("layer_surface_id".to_string()).into());
}
const OPCODE: u16 = 6;
const MSG_SIZE: u16 = 12;
@@ -63,7 +76,10 @@ impl WlClient {
}
pub fn layer_surface_set_size(&mut self, width: u32, height: u32) -> Result<(), Box<dyn Error>> {
let object: u32 = self.layer_surface_id.unwrap();
let object: u32 = self.layer_surface_id.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("layer_surface_id".to_string()).into());
}
const OPCODE: u16 = 0;
const MSG_SIZE: u16 = 20;
@@ -83,7 +99,10 @@ impl WlClient {
}
pub fn layer_surface_set_keyboard_interactivity(&mut self) -> Result<(), Box<dyn Error>> {
let object: u32 = self.layer_surface_id.unwrap();
let object: u32 = self.layer_surface_id.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("layer_surface_id".to_string()).into());
}
const OPCODE: u16 = 4;
const MSG_SIZE: u16 = 12;

View File

@@ -16,11 +16,10 @@ impl fmt::Display for UnsetErr {
impl WlClient {
pub fn wl_compositor_create_surface(&mut self) -> Result<(), Box<dyn Error>> {
if self.compositor_id.is_none() {
let object = self.compositor_id.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("compositor_id".to_string()).into());
}
let object = self.compositor_id.unwrap();
const OPCODE: u16 = 0;
const MSG_SIZE: u16 = 12;
@@ -36,19 +35,22 @@ impl WlClient {
self.socket.write(&request)?;
self.surface_id = Some(current_id);
self.surface_id.store(current_id, Ordering::Relaxed);
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.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("surface_id".to_string()).into());
}
let object = self.surface_id.unwrap();
const OPCODE: u16 = 1;
const MSG_SIZE: u16 = 20;
let buffer = self.buffer_id.unwrap();
let buffer = self.buffer_id.load(Ordering::Relaxed);
if buffer == 0 {
return Err(UnsetErr("buffer_id".to_string()).into());
}
const X: u32 = 0;
const Y: u32 = 0;
@@ -68,7 +70,10 @@ impl WlClient {
}
pub fn wl_surface_commit(&mut self) -> Result<(), Box<dyn Error>> {
let object = self.surface_id.unwrap();
let object = self.surface_id.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("surface_id".to_string()).into());
}
const OPCODE: u16 = 6;
const MSG_SIZE: u16 = 8;
@@ -85,10 +90,10 @@ impl WlClient {
}
pub fn xdg_wm_base_pong(&mut self, event: &Vec<u8>) -> Result<(), Box<dyn Error>> {
if self.xdg_wm_base_id.is_none() {
return Err(Box::new(UnsetErr("xdg_wm_base_id".to_string())));
let object = self.xdg_wm_base_id.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("xdg_wm_base_id".to_string()).into());
}
let object = self.xdg_wm_base_id.unwrap();
const OPCODE: u16 = 3;
const MSG_SIZE: u16 = 12;
let serial = u32::from_ne_bytes(event[0..4].try_into().unwrap());

View File

@@ -12,14 +12,14 @@ pub struct WlClient {
pub socket: UnixStream,
pub current_id: AtomicU32,
pub registry_id: AtomicU32,
pub shm_id: Option<u32>,
pub shm_id: AtomicU32,
pub shm_pool: Arc<Mutex<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>,
pub layer_shell_id: Option<u32>,
pub layer_surface_id: Option<u32>,
pub buffer_id: AtomicU32,
pub compositor_id: AtomicU32,
pub surface_id: AtomicU32,
pub xdg_wm_base_id: AtomicU32,
pub layer_shell_id: AtomicU32,
pub layer_surface_id: AtomicU32,
}
impl WlClient {
@@ -34,14 +34,14 @@ impl WlClient {
socket: sock,
current_id: AtomicU32::from(1),
registry_id: AtomicU32::from(0),
shm_id: None,
shm_id: AtomicU32::from(0),
shm_pool: Arc::new(Mutex::new(None)),
buffer_id: None,
compositor_id: None,
surface_id: None,
xdg_wm_base_id: None,
layer_shell_id: None,
layer_surface_id: None,
buffer_id: AtomicU32::from(0),
compositor_id: AtomicU32::from(0),
surface_id: AtomicU32::from(0),
xdg_wm_base_id: AtomicU32::from(0),
layer_shell_id: AtomicU32::from(0),
layer_surface_id: AtomicU32::from(0),
};
let shm_pool = wl_client.shm_pool.clone();
@@ -79,22 +79,22 @@ impl WlClient {
if header.object == self.registry_id.load(Ordering::Relaxed) && header.opcode == 0 { // wl_registry::global
self.wl_registry_global(&event)?;
}
else if header.object == 1 && header.opcode == 0 { // wl_display::error
else if header.object == self.registry_id.load(Ordering::Relaxed) && header.opcode == 0 { // wl_display::error
WlClient::wl_display_error(&event);
}
else if self.shm_id.is_some() && header.object == self.shm_id.unwrap() && header.opcode == 0 { // wl_shm::format
else if header.object == self.shm_id.load(Ordering::Relaxed) && header.opcode == 0 { // wl_shm::format
WlClient::wl_shm_format(&event);
}
else if self.xdg_wm_base_id.is_some() && header.object == self.xdg_wm_base_id.unwrap() && header.opcode == 0 { // xdg_wm_base::ping
else if header.object == self.xdg_wm_base_id.load(Ordering::Relaxed) && header.opcode == 0 { // xdg_wm_base::ping
self.xdg_wm_base_pong(&event)?;
}
else if Some(header.object) == self.layer_surface_id && header.opcode == 0 { // zwlr_layer_surface::configure
else if header.object == self.layer_surface_id.load(Ordering::Relaxed) && 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
else if header.object == self.surface_id.load(Ordering::Relaxed) && 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
else if header.object == self.surface_id.load(Ordering::Relaxed) && header.opcode == 3 { // wl_surface::preferred_buffer_transform
println!("Preferred buffer transform: {}", i32::from_ne_bytes(event[0..4].try_into().unwrap()));
}
else {

View File

@@ -3,16 +3,16 @@ use std::{error::Error, io::Write, sync::atomic::{AtomicU32, Ordering}};
impl WlClient {
fn init_toplevel(&mut self) -> Result<(), Box<dyn Error>> {
if self.shm_id.is_none() {
if self.shm_id.load(Ordering::Relaxed) == 0 {
return Err(Box::new(UnsetErr("shm_id".to_string())));
}
if self.compositor_id.is_none() {
if self.compositor_id.load(Ordering::Relaxed) == 0 {
return Err(Box::new(UnsetErr("compositor_id".to_string())));
}
if self.xdg_wm_base_id.is_none() {
if self.xdg_wm_base_id.load(Ordering::Relaxed) == 0 {
return Err(Box::new(UnsetErr("xdg_wm_base_id".to_string())));
}
if self.layer_shell_id.is_none() {
if self.layer_shell_id.load(Ordering::Relaxed) == 0 {
return Err(UnsetErr("layer_shell_id".to_string()).into());
}
println!("Initializing toplevel!");
@@ -45,7 +45,7 @@ impl WlClient {
request.write_u32(&current_id, &mut offset);
self.socket.write(&request)?;
self.registry_id = AtomicU32::from(current_id);
self.registry_id.store(current_id, Ordering::Relaxed);
Ok(())
}
@@ -74,7 +74,7 @@ impl WlClient {
&version,
&current_id
)?;
self.shm_id = Some(current_id);
self.shm_id.store(current_id, Ordering::Relaxed);
self.init_toplevel().unwrap_or_else(|err| {eprintln!("{}", err)});
}
@@ -86,7 +86,7 @@ impl WlClient {
&version,
&current_id
)?;
self.compositor_id = Some(current_id);
self.compositor_id.store(current_id, Ordering::Relaxed);
self.init_toplevel().unwrap_or_else(|err| {eprintln!("{}", err)});
}
@@ -98,7 +98,7 @@ impl WlClient {
&version,
&current_id
)?;
self.xdg_wm_base_id = Some(current_id);
self.xdg_wm_base_id.store(current_id, Ordering::Relaxed);
self.init_toplevel().unwrap_or_else(|err| {eprintln!("{}", err)});
}
@@ -110,7 +110,7 @@ impl WlClient {
&version,
&current_id
)?;
self.layer_shell_id = Some(current_id);
self.layer_shell_id.store(current_id, Ordering::Relaxed);
self.init_toplevel().unwrap_or_else(|err| {eprintln!("{}", err)});
}

View File

@@ -18,7 +18,10 @@ impl WlClient {
shm_pool.as_mut().unwrap().circle(300, 300, 200, 0xff0000ff);
shm_pool.as_mut().unwrap().rounded_rectangle(450, 400, 60, 40, 16, 0xffffff00);
let object = self.shm_id.ok_or(UnsetErr("shm_id".to_string()))?;
let object = self.shm_id.load(Ordering::Relaxed);
if object == 0 {
return Err(UnsetErr("shm_id".to_string()).into());
}
const OPCODE: u16 = 0;
const REQ_SIZE: u16 = 16;
let id = shm_pool.as_ref().unwrap().id;
@@ -83,7 +86,7 @@ impl WlClient {
self.socket.write(&request)?;
self.buffer_id = Some(current_id);
self.buffer_id.store(current_id, Ordering::Relaxed);
Ok(())
}