diff --git a/src/main.rs b/src/main.rs index c762ad9..16d7a8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use wayland::wl_client::WlClient; mod graphics; fn main() -> Result<(), Box> { - let mut wl_client = WlClient::new()?; + let mut wl_client = WlClient::run()?; Ok(()) } diff --git a/src/wayland/layer_shell.rs b/src/wayland/layer_shell.rs index e6d89bc..4dcc3b7 100644 --- a/src/wayland/layer_shell.rs +++ b/src/wayland/layer_shell.rs @@ -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> { + pub fn layer_shell_get_layer_surface(&self) -> Result<(), Box> { // 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) -> Result<(), Box> { + pub fn layer_surface_configure(&self, event: &Vec) -> Result<(), Box> { 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> { + pub fn layer_surface_set_size(&self, width: u32, height: u32) -> Result<(), Box> { 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> { + pub fn layer_surface_set_keyboard_interactivity(&self) -> Result<(), Box> { let object: u32 = self.layer_surface_id.load(Ordering::Relaxed); if object == 0 { return Err(UnsetErr("layer_surface_id".to_string()).into()); diff --git a/src/wayland/surface.rs b/src/wayland/surface.rs index 4c9cbd5..d8cad55 100644 --- a/src/wayland/surface.rs +++ b/src/wayland/surface.rs @@ -15,7 +15,7 @@ impl fmt::Display for UnsetErr { } impl WlClient { - pub fn wl_compositor_create_surface(&mut self) -> Result<(), Box> { + pub fn wl_compositor_create_surface(&self) -> Result<(), Box> { 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> { + pub fn wl_surface_attach(&self) -> Result<(), Box> { 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> { + pub fn wl_surface_commit(&self) -> Result<(), Box> { 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) -> Result<(), Box> { + pub fn xdg_wm_base_pong(&self, event: &Vec) -> Result<(), Box> { let object = self.xdg_wm_base_id.load(Ordering::Relaxed); if object == 0 { return Err(UnsetErr("xdg_wm_base_id".to_string()).into()); diff --git a/src/wayland/wl_client.rs b/src/wayland/wl_client.rs index d45e660..0c24869 100644 --- a/src/wayland/wl_client.rs +++ b/src/wayland/wl_client.rs @@ -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> { + pub fn run() -> Result, Box> { 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> { + pub fn read_event(&self) -> Result<(), Box> { // 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(()) } } + diff --git a/src/wayland/wl_registry.rs b/src/wayland/wl_registry.rs index ea7c797..29004be 100644 --- a/src/wayland/wl_registry.rs +++ b/src/wayland/wl_registry.rs @@ -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> { + fn init_toplevel(&self) -> Result<(), Box> { 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> { + pub fn wl_display_get_registry(&self) -> Result<(), Box> { 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) -> Result<(), Box> { + pub fn wl_registry_global(&self, event: &Vec) -> Result<(), Box> { 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, diff --git a/src/wayland/wl_shm.rs b/src/wayland/wl_shm.rs index 22f7820..97919e5 100644 --- a/src/wayland/wl_shm.rs +++ b/src/wayland/wl_shm.rs @@ -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> { + pub fn wl_shm_create_pool(&self, width: usize, height: usize) -> Result<(), Box> { 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