pub mod dmabuf;
#[cfg(unix)]
mod unix_socket;
mod wire;
pub use dmabuf::{DmaBufPlaneDesc, WaylandDmaBufDriver, WaylandDmaBufPacket};
use std::string::String;
use std::time::{Duration, Instant};
use std::vec::Vec;
pub use super::shm_driver::{
decode_linux_keycode, decode_linux_modifier, write_uncompressed_png, ShmSlot, ShmSurfaceDriver,
SoftwareSurfaceDriver, WaylandSurfaceDriver, WlBuffer, WlSurface,
};
pub use wire::{
SurfaceId, WaylandSurfaceRole, WaylandWireEvent, WaylandWireState, WlClientEvent,
};
use crate::prelude::*;
pub struct WaylandPlatform {
pub role: WaylandSurfaceRole,
width: usize,
height: usize,
start_instant: Instant,
pub shm_driver: WaylandSurfaceDriver,
pub wl_surface: WlSurface,
pub wire_state: WaylandWireState,
pub pending_events: Vec<WaylandWireEvent>,
pub close_requested: bool,
#[cfg(unix)]
compositor_conn: Option<unix_socket::WaylandCompositorConnection>,
}
impl WaylandPlatform {
pub fn new(role: WaylandSurfaceRole, width: usize, height: usize) -> Self {
let w = width.max(1);
let h = height.max(1);
#[allow(unused_mut)]
let mut wire_state = WaylandWireState::new();
#[cfg(unix)]
let compositor_conn = if std::env::var("VELOX_WAYLAND_AUTO_CONNECT").as_deref() == Ok("1") {
unix_socket::WaylandCompositorConnection::try_connect(
&role,
w,
h,
&mut wire_state,
)
} else {
None
};
Self {
role,
width: w,
height: h,
start_instant: Instant::now(),
shm_driver: WaylandSurfaceDriver::new(w, h),
wl_surface: WlSurface::new(),
wire_state,
pending_events: Vec::new(),
close_requested: false,
#[cfg(unix)]
compositor_conn,
}
}
pub fn new_connected(role: WaylandSurfaceRole, width: usize, height: usize) -> Self {
let mut plat = Self::new(role, width, height);
let _ = plat.connect_to_compositor();
plat
}
pub fn connect_to_compositor(&mut self) -> bool {
#[cfg(unix)]
{
if self.compositor_conn.is_none() {
self.compositor_conn = unix_socket::WaylandCompositorConnection::try_connect(
&self.role,
self.width,
self.height,
&mut self.wire_state,
);
}
self.compositor_conn.is_some()
}
#[cfg(not(unix))]
{
false
}
}
#[inline]
pub fn is_connected_to_compositor(&self) -> bool {
#[cfg(unix)]
{
self.compositor_conn.is_some()
}
#[cfg(not(unix))]
{
false
}
}
pub fn new_layer_shell(
namespace: impl Into<String>,
width: usize,
height: usize,
exclusive_zone: i16,
) -> Self {
Self::new(
WaylandSurfaceRole::LayerShell {
namespace: namespace.into(),
exclusive_zone,
},
width,
height,
)
}
pub fn new_subsurface_decoration(parent_surface_id: u32, width: usize, height: usize) -> Self {
Self::new(
WaylandSurfaceRole::SubSurfaceDecoration { parent_surface_id },
width,
height,
)
}
pub fn new_xdg_toplevel(
title: impl Into<String>,
app_id: impl Into<String>,
width: usize,
height: usize,
) -> Self {
Self::new(
WaylandSurfaceRole::XdgToplevel {
title: title.into(),
app_id: app_id.into(),
},
width,
height,
)
}
pub fn feed_wire_bytes(&mut self, engine: &mut VeloxEngine, bytes: &[u8]) {
let evs = self.wire_state.feed_incoming_bytes(bytes);
self.process_wire_events(engine, evs);
}
fn process_wire_events(&mut self, engine: &mut VeloxEngine, events: Vec<WaylandWireEvent>) {
for ev in events {
match &ev {
WaylandWireEvent::Configure { width, height, .. } => {
if *width > 0 && *height > 0 && (*width != self.width || *height != self.height)
{
self.handle_configure(engine, *width as i16, *height as i16);
}
}
WaylandWireEvent::BufferReleased { buffer_id } => {
self.shm_driver.release_buffer(*buffer_id);
}
WaylandWireEvent::SeatInput(input_ev) => {
if matches!(input_ev, InputEvent::PointerDown { .. }) {
engine.set_pointer_kind(PointerKind::Mouse);
}
engine.dispatch_event(*input_ev);
}
WaylandWireEvent::CloseRequested => {
self.close_requested = true;
}
_ => {}
}
self.pending_events.push(ev);
}
}
pub fn handle_configure(&mut self, engine: &mut VeloxEngine, width: i16, height: i16) {
let w = width.max(16) as usize;
let h = height.max(16) as usize;
self.width = w;
self.height = h;
engine.resize(w as i16, h as i16);
self.shm_driver.resize(w, h);
self.wl_surface = WlSurface::new();
#[cfg(unix)]
if let Some(conn) = &mut self.compositor_conn {
conn.resize_pool(w, h, &mut self.wire_state);
} else {
self.wire_state.encode_destroy_double_buffers();
self.wire_state.encode_create_double_buffers(w, h);
}
#[cfg(not(unix))]
{
self.wire_state.encode_destroy_double_buffers();
self.wire_state.encode_create_double_buffers(w, h);
}
}
#[inline]
pub fn front_pixels(&self) -> &[Xrgb8888] {
self.shm_driver.front_pixels()
}
#[inline]
pub fn with_backend(mut self, backend: super::DesktopRenderBackend) -> Self {
self.shm_driver.set_backend(backend);
self
}
#[inline]
pub fn set_backend(&mut self, backend: super::DesktopRenderBackend) {
self.shm_driver.set_backend(backend);
}
#[inline]
pub const fn backend(&self) -> super::DesktopRenderBackend {
self.shm_driver.backend()
}
#[inline]
pub fn committed_damage(&self) -> &[Region] {
&self.wl_surface.committed_damage
}
}
impl PlatformSurface for WaylandPlatform {
#[inline(always)]
fn surface_size(&self) -> (i16, i16) {
(self.width as i16, self.height as i16)
}
fn flush_frame_dt(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> FlushReport {
if self.is_connected_to_compositor() && self.shm_driver.slots.iter().all(|s| s.in_use) {
return FlushReport::default();
}
let prev_buf = self.wl_surface.attached_buffer_id;
if self
.shm_driver
.flush_frame_dt(engine, &mut self.wl_surface, dt_ms)
{
let active_slot = self.shm_driver.active_slot_idx;
let dmg = self.wl_surface.committed_damage.clone();
#[cfg(unix)]
if let Some(conn) = &mut self.compositor_conn {
conn.commit_slot_damage(
active_slot,
&self.shm_driver.slots[active_slot].pixels,
&self.shm_driver.last_raster_regions,
&dmg,
&mut self.wire_state,
);
} else {
if let Some(old_id) = prev_buf {
self.shm_driver.release_buffer(old_id);
}
if self.wire_state.outbound_tx.len() > 4096 {
self.wire_state.outbound_tx.clear();
}
self.wire_state
.encode_surface_commit_damage(active_slot, &dmg);
}
#[cfg(not(unix))]
{
if let Some(old_id) = prev_buf {
self.shm_driver.release_buffer(old_id);
}
if self.wire_state.outbound_tx.len() > 4096 {
self.wire_state.outbound_tx.clear();
}
self.wire_state
.encode_surface_commit_damage(active_slot, &dmg);
}
let px: usize = dmg
.iter()
.map(|r| (r.w.max(0) as usize) * (r.h.max(0) as usize))
.sum();
FlushReport {
dirty_regions: dmg.len(),
strips_or_buffers: 1,
pixels_updated: px,
}
} else {
FlushReport::default()
}
}
}
impl Platform for WaylandPlatform {
#[inline]
fn duration_since_start(&self) -> Duration {
self.start_instant.elapsed()
}
fn step_platform(&mut self, engine: &mut VeloxEngine, dt_ms: u16) -> PlatformControlFlow {
#[cfg(unix)]
if let Some(conn) = &mut self.compositor_conn {
let evs = conn.poll_wire(&mut self.wire_state);
self.process_wire_events(engine, evs);
}
if self.close_requested {
return PlatformControlFlow::Exit;
}
let report = self.flush_frame_dt(engine, dt_ms);
if report.has_updates() || engine.needs_redraw() {
PlatformControlFlow::Poll
} else {
PlatformControlFlow::Wait
}
}
}