read_loop now happens on its own thread, replace WlClient::new() with blocking run()

This commit is contained in:
2025-05-08 00:32:17 -07:00
parent 6cd5d1f256
commit bde60fff90
6 changed files with 35 additions and 34 deletions

View File

@@ -6,7 +6,7 @@ use wayland::wl_client::WlClient;
mod graphics; mod graphics;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let mut wl_client = WlClient::new()?; let mut wl_client = WlClient::run()?;
Ok(()) Ok(())
} }

View File

@@ -6,7 +6,7 @@ const OVERLAY: u32 = 3;
const EXCLUSIVE: u32 = 0; // exclusize keyboard focus const EXCLUSIVE: u32 = 0; // exclusize keyboard focus
impl WlClient { impl WlClient {
pub fn layer_shell_get_layer_surface(&mut self) -> Result<(), Box<dyn Error>> { pub fn layer_shell_get_layer_surface(&self) -> Result<(), Box<dyn Error>> {
// TODO: Make sure layer_surface_id isn't already set // TODO: Make sure layer_surface_id isn't already set
let object: u32 = self.layer_shell_id.load(Ordering::Relaxed); let object: u32 = self.layer_shell_id.load(Ordering::Relaxed);
if object == 0 { if object == 0 {
@@ -42,7 +42,7 @@ impl WlClient {
Ok(()) Ok(())
} }
pub fn layer_surface_configure(&mut self, event: &Vec<u8>) -> Result<(), Box<dyn Error>> { pub fn layer_surface_configure(&self, event: &Vec<u8>) -> Result<(), Box<dyn Error>> {
let mut offset: usize = 0; let mut offset: usize = 0;
let serial = event.read_u32(&mut offset); let serial = event.read_u32(&mut offset);
let width = event.read_u32(&mut offset); let width = event.read_u32(&mut offset);
@@ -75,7 +75,7 @@ impl WlClient {
Ok(()) Ok(())
} }
pub fn layer_surface_set_size(&mut self, width: u32, height: u32) -> Result<(), Box<dyn Error>> { pub fn layer_surface_set_size(&self, width: u32, height: u32) -> Result<(), Box<dyn Error>> {
let object: u32 = self.layer_surface_id.load(Ordering::Relaxed); let object: u32 = self.layer_surface_id.load(Ordering::Relaxed);
if object == 0 { if object == 0 {
return Err(UnsetErr("layer_surface_id".to_string()).into()); return Err(UnsetErr("layer_surface_id".to_string()).into());
@@ -98,7 +98,7 @@ impl WlClient {
Ok(()) Ok(())
} }
pub fn layer_surface_set_keyboard_interactivity(&mut self) -> Result<(), Box<dyn Error>> { pub fn layer_surface_set_keyboard_interactivity(&self) -> Result<(), Box<dyn Error>> {
let object: u32 = self.layer_surface_id.load(Ordering::Relaxed); let object: u32 = self.layer_surface_id.load(Ordering::Relaxed);
if object == 0 { if object == 0 {
return Err(UnsetErr("layer_surface_id".to_string()).into()); return Err(UnsetErr("layer_surface_id".to_string()).into());

View File

@@ -15,7 +15,7 @@ impl fmt::Display for UnsetErr {
} }
impl WlClient { impl WlClient {
pub fn wl_compositor_create_surface(&mut self) -> Result<(), Box<dyn Error>> { pub fn wl_compositor_create_surface(&self) -> Result<(), Box<dyn Error>> {
let object = self.compositor_id.load(Ordering::Relaxed); let object = self.compositor_id.load(Ordering::Relaxed);
if object == 0 { if object == 0 {
return Err(UnsetErr("compositor_id".to_string()).into()); return Err(UnsetErr("compositor_id".to_string()).into());
@@ -40,7 +40,7 @@ impl WlClient {
Ok(()) Ok(())
} }
pub fn wl_surface_attach(&mut self) -> Result<(), Box<dyn Error>> { pub fn wl_surface_attach(&self) -> Result<(), Box<dyn Error>> {
let object = self.surface_id.load(Ordering::Relaxed); let object = self.surface_id.load(Ordering::Relaxed);
if object == 0 { if object == 0 {
return Err(UnsetErr("surface_id".to_string()).into()); return Err(UnsetErr("surface_id".to_string()).into());
@@ -69,7 +69,7 @@ impl WlClient {
Ok(()) Ok(())
} }
pub fn wl_surface_commit(&mut self) -> Result<(), Box<dyn Error>> { pub fn wl_surface_commit(&self) -> Result<(), Box<dyn Error>> {
let object = self.surface_id.load(Ordering::Relaxed); let object = self.surface_id.load(Ordering::Relaxed);
if object == 0 { if object == 0 {
return Err(UnsetErr("surface_id".to_string()).into()); return Err(UnsetErr("surface_id".to_string()).into());
@@ -89,7 +89,7 @@ impl WlClient {
Ok(()) Ok(())
} }
pub fn xdg_wm_base_pong(&mut self, event: &Vec<u8>) -> Result<(), Box<dyn Error>> { pub fn xdg_wm_base_pong(&self, event: &Vec<u8>) -> Result<(), Box<dyn Error>> {
let object = self.xdg_wm_base_id.load(Ordering::Relaxed); let object = self.xdg_wm_base_id.load(Ordering::Relaxed);
if object == 0 { if object == 0 {
return Err(UnsetErr("xdg_wm_base_id".to_string()).into()); return Err(UnsetErr("xdg_wm_base_id".to_string()).into());

View File

@@ -1,4 +1,4 @@
use std::{env::var, error::Error, fmt::Debug, io::Read, os::unix::net::UnixStream, sync::{atomic::{AtomicU32, Ordering}, Arc, Mutex}, thread, u32}; use std::{env::var, error::Error, fmt::Debug, io::Read, os::unix::net::UnixStream, sync::{atomic::{AtomicU32, Ordering}, Arc, Mutex}, thread::{self, JoinHandle}, u32};
use crate::wayland::shm; use crate::wayland::shm;
@@ -23,14 +23,14 @@ pub struct WlClient {
} }
impl WlClient { impl WlClient {
pub fn new() -> Result<Self, Box<dyn Error>> { pub fn run() -> Result<Arc<Self>, Box<dyn Error>> {
let sock = UnixStream::connect(format!( let sock = UnixStream::connect(format!(
"{}/{}", "{}/{}",
var("XDG_RUNTIME_DIR")?, var("XDG_RUNTIME_DIR")?,
var("WAYLAND_DISPLAY")? var("WAYLAND_DISPLAY")?
))?; ))?;
let mut wl_client = WlClient { let mut wl_client = Arc::new(WlClient {
socket: Mutex::new(sock), socket: Mutex::new(sock),
current_id: AtomicU32::from(1), current_id: AtomicU32::from(1),
registry_id: AtomicU32::from(0), registry_id: AtomicU32::from(0),
@@ -42,21 +42,23 @@ impl WlClient {
xdg_wm_base_id: AtomicU32::from(0), xdg_wm_base_id: AtomicU32::from(0),
layer_shell_id: AtomicU32::from(0), layer_shell_id: AtomicU32::from(0),
layer_surface_id: AtomicU32::from(0), layer_surface_id: AtomicU32::from(0),
}; });
wl_client.wl_display_get_registry()?; let mut wl_client2 = wl_client.clone();
thread::spawn(move || { wl_client.wl_display_get_registry();
}).join(); let readloop = thread::spawn(move || {
loop {
wl_client2.read_event();
}
});
loop { readloop.join();
wl_client.read_event();
}
Ok(wl_client) Ok(wl_client)
} }
pub fn read_event(&mut self) -> Result<(), Box<dyn Error>> { pub fn read_event(&self) -> Result<(), Box<dyn Error>> {
// TODO: Don't realloc header and event // TODO: Don't realloc header and event
let mut header = vec![0u8; 8]; let mut header = vec![0u8; 8];
@@ -73,6 +75,12 @@ impl WlClient {
socket.read_exact(&mut event)?; socket.read_exact(&mut event)?;
drop(socket); drop(socket);
println!(
"Received event:\n\tObject: {}\n\tOpcode: {}\n\tSize: {}",
header.object,
header.opcode,
header.size
);
if header.object == self.registry_id.load(Ordering::Relaxed) && header.opcode == 0 { // wl_registry::global if header.object == self.registry_id.load(Ordering::Relaxed) && header.opcode == 0 { // wl_registry::global
self.wl_registry_global(&event)?; self.wl_registry_global(&event)?;
} }
@@ -94,15 +102,8 @@ impl WlClient {
else if header.object == self.surface_id.load(Ordering::Relaxed) && 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())); println!("Preferred buffer transform: {}", i32::from_ne_bytes(event[0..4].try_into().unwrap()));
} }
else {
println!(
"Received event:\n\tObject: {}\n\tOpcode: {}\n\tSize: {}",
header.object,
header.opcode,
header.size
);
}
Ok(()) Ok(())
} }
} }

View File

@@ -2,7 +2,7 @@ use crate::wayland::{surface::UnsetErr, vec_utils::WlMessage, wl_client::WlClien
use std::{error::Error, io::Write, sync::atomic::{AtomicU32, Ordering}}; use std::{error::Error, io::Write, sync::atomic::{AtomicU32, Ordering}};
impl WlClient { impl WlClient {
fn init_toplevel(&mut self) -> Result<(), Box<dyn Error>> { fn init_toplevel(&self) -> Result<(), Box<dyn Error>> {
if self.shm_id.load(Ordering::Relaxed) == 0 { if self.shm_id.load(Ordering::Relaxed) == 0 {
return Err(Box::new(UnsetErr("shm_id".to_string()))); return Err(Box::new(UnsetErr("shm_id".to_string())));
} }
@@ -29,7 +29,7 @@ impl WlClient {
Ok(()) Ok(())
} }
pub fn wl_display_get_registry(&mut self) -> Result<(), Box<dyn Error>> { pub fn wl_display_get_registry(&self) -> Result<(), Box<dyn Error>> {
const OBJECT: u32 = 1; const OBJECT: u32 = 1;
const OPCODE: u16 = 1; const OPCODE: u16 = 1;
const MSG_SIZE: u16 = 12; const MSG_SIZE: u16 = 12;
@@ -50,7 +50,7 @@ impl WlClient {
Ok(()) Ok(())
} }
pub fn wl_registry_global(&mut self, event: &Vec<u8>) -> Result<(), Box<dyn Error>> { pub fn wl_registry_global(&self, event: &Vec<u8>) -> Result<(), Box<dyn Error>> {
let mut offset: usize = 0; let mut offset: usize = 0;
let name = event.read_u32(&mut offset); let name = event.read_u32(&mut offset);
@@ -118,7 +118,7 @@ impl WlClient {
} }
pub fn wl_registry_bind( pub fn wl_registry_bind(
&mut self, &self,
name: &u32, name: &u32,
interface: &String, interface: &String,
version: &u32, version: &u32,

View File

@@ -9,7 +9,7 @@ impl WlClient {
println!("Received pixel format: {:x}", event.read_u32(&mut offset)); println!("Received pixel format: {:x}", event.read_u32(&mut offset));
} }
pub fn wl_shm_create_pool(&mut self, width: usize, height: usize) -> Result<(), Box<dyn Error>> { pub fn wl_shm_create_pool(&self, width: usize, height: usize) -> Result<(), Box<dyn Error>> {
let mut shm_pool = self.shm_pool.lock().unwrap(); let mut shm_pool = self.shm_pool.lock().unwrap();
let current_id = self.current_id.fetch_add(1, Ordering::Relaxed) + 1; let current_id = self.current_id.fetch_add(1, Ordering::Relaxed) + 1;
*shm_pool = Some(shm::ShmPool::new(width, height, current_id)?); *shm_pool = Some(shm::ShmPool::new(width, height, current_id)?);
@@ -56,7 +56,7 @@ impl WlClient {
} }
pub fn wl_shm_pool_create_buffer( pub fn wl_shm_pool_create_buffer(
&mut self, &self,
shm_offset: u32, shm_offset: u32,
width: u32, width: u32,
height: u32 height: u32