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 @@ const OVERLAY: u32 = 3;
const EXCLUSIVE: u32 = 0; // exclusize keyboard focus
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
let object: u32 = self.layer_shell_id.load(Ordering::Relaxed);
if object == 0 {
@@ -42,7 +42,7 @@ impl WlClient {
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 serial = event.read_u32(&mut offset);
let width = event.read_u32(&mut offset);
@@ -75,7 +75,7 @@ impl WlClient {
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);
if object == 0 {
return Err(UnsetErr("layer_surface_id".to_string()).into());
@@ -98,7 +98,7 @@ impl WlClient {
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);
if object == 0 {
return Err(UnsetErr("layer_surface_id".to_string()).into());

View File

@@ -15,7 +15,7 @@ impl fmt::Display for UnsetErr {
}
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);
if object == 0 {
return Err(UnsetErr("compositor_id".to_string()).into());
@@ -40,7 +40,7 @@ impl WlClient {
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);
if object == 0 {
return Err(UnsetErr("surface_id".to_string()).into());
@@ -69,7 +69,7 @@ impl WlClient {
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);
if object == 0 {
return Err(UnsetErr("surface_id".to_string()).into());
@@ -89,7 +89,7 @@ impl WlClient {
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);
if object == 0 {
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;
@@ -23,14 +23,14 @@ pub struct 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!(
"{}/{}",
var("XDG_RUNTIME_DIR")?,
var("WAYLAND_DISPLAY")?
))?;
let mut wl_client = WlClient {
let mut wl_client = Arc::new(WlClient {
socket: Mutex::new(sock),
current_id: AtomicU32::from(1),
registry_id: AtomicU32::from(0),
@@ -42,21 +42,23 @@ impl WlClient {
xdg_wm_base_id: AtomicU32::from(0),
layer_shell_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 || {
}).join();
wl_client.wl_display_get_registry();
let readloop = thread::spawn(move || {
loop {
wl_client2.read_event();
}
});
loop {
wl_client.read_event();
}
readloop.join();
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
let mut header = vec![0u8; 8];
@@ -73,6 +75,12 @@ impl WlClient {
socket.read_exact(&mut event)?;
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
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
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(())
}
}

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}};
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 {
return Err(Box::new(UnsetErr("shm_id".to_string())));
}
@@ -29,7 +29,7 @@ impl WlClient {
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 OPCODE: u16 = 1;
const MSG_SIZE: u16 = 12;
@@ -50,7 +50,7 @@ impl WlClient {
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 name = event.read_u32(&mut offset);
@@ -118,7 +118,7 @@ impl WlClient {
}
pub fn wl_registry_bind(
&mut self,
&self,
name: &u32,
interface: &String,
version: &u32,

View File

@@ -9,7 +9,7 @@ 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<(), 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 current_id = self.current_id.fetch_add(1, Ordering::Relaxed) + 1;
*shm_pool = Some(shm::ShmPool::new(width, height, current_id)?);
@@ -56,7 +56,7 @@ impl WlClient {
}
pub fn wl_shm_pool_create_buffer(
&mut self,
&self,
shm_offset: u32,
width: u32,
height: u32