use teleia::*; use std::io::Write; use std::os::fd::FromRawFd; use std::os::unix::net::{AncillaryData, SocketAncillary}; use std::sync::mpsc::{Receiver, channel}; use std::{os::unix::net::UnixDatagram, thread::{spawn, JoinHandle}}; use glow::HasContext; use crate::ffi::egl; use crate::ffi::glfw; const KEY: usize = 42; const SOCK_PATH: &'static str = "/tmp/newton.sock"; pub struct Server { pub tex: texture::Texture, thread: JoinHandle<()>, receiver: Receiver, } impl Server { pub fn new(ctx: &context::Context) -> Self { std::fs::remove_file(SOCK_PATH).expect("failed to remove socket"); let sock = UnixDatagram::bind(SOCK_PATH).expect("failed to bind texture server socket"); let (sender, receiver) = channel::(); let thread = spawn(move || { let mut ancillary_buf = [0; 128]; let mut ancillary = SocketAncillary::new(&mut ancillary_buf); loop { match sock.recv_vectored_with_ancillary(&mut [], &mut ancillary) { Ok(_) => { for ar in ancillary.messages() { match ar { Ok(AncillaryData::ScmRights(mut sr)) => { if let Some(fd) = sr.next() { if let Err(e) = sender.send(fd) { log::warn!("error on channel: {:?}", e); } } }, Ok(_) => log::info!("message is not SCM_RIGHTS"), Err(e) => log::warn!("failed to read ancillary message: {:?}", e), } } }, Err(e) => log::warn!("texture server failed to receive: {}", e), } } }); Self { tex: texture::Texture::new_empty(ctx), thread, receiver, } } pub fn update(&self, ctx: &context::Context) { if let Ok(fd) = self.receiver.try_recv() { log::info!("received file descriptor: {}", fd); unsafe { let window = ctx.window.borrow_mut().0.ptr; let d = glfw::glfwGetEGLDisplay(); let c = glfw::glfwGetEGLContext(window); let img = egl::eglCreateImage(d, c, egl::EGL_LINUX_DMA_BUF_EXT, std::ptr::null_mut(), [ egl::EGL_WIDTH as _, 16, egl::EGL_HEIGHT as _, 16, egl::EGL_LINUX_DRM_FOURCC_EXT as _, 875708993, egl::EGL_DMA_BUF_PLANE0_FD_EXT as _, fd as _, egl::EGL_DMA_BUF_PLANE0_OFFSET_EXT as _, 0, egl::EGL_DMA_BUF_PLANE0_PITCH_EXT as _, 128, egl::EGL_NONE as _, ].as_ptr()); self.tex.bind(ctx); egl::glEGLImageTargetTexture2DOES(glow::TEXTURE_2D, img); log::info!("set up texture"); } } } }