diff --git a/src/event_loop.rs b/src/event_loop.rs index 233374beec..c7231d1ee1 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -338,6 +338,7 @@ unsafe impl rwh_05::HasRawDisplayHandle for EventLoop { } #[cfg(any(x11_platform, wayland_platform))] +#[cfg(not(target_os = "haiku"))] impl AsFd for EventLoop { /// Get the underlying [EventLoop]'s `fd` which you can register /// into other event loop, like [`calloop`] or [`mio`]. When doing so, the @@ -352,6 +353,7 @@ impl AsFd for EventLoop { } #[cfg(any(x11_platform, wayland_platform))] +#[cfg(not(target_os = "haiku"))] impl AsRawFd for EventLoop { /// Get the underlying [EventLoop]'s raw `fd` which you can register /// into other event loop, like [`calloop`] or [`mio`]. When doing so, the diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index bc0e71c80c..618a02e3e5 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -832,12 +832,14 @@ impl EventLoop { } } +#[cfg(not(target_os = "haiku"))] impl AsFd for EventLoop { fn as_fd(&self) -> BorrowedFd<'_> { x11_or_wayland!(match self; EventLoop(evlp) => evlp.as_fd()) } } +#[cfg(not(target_os = "haiku"))] impl AsRawFd for EventLoop { fn as_raw_fd(&self) -> RawFd { x11_or_wayland!(match self; EventLoop(evlp) => evlp.as_raw_fd()) @@ -1042,3 +1044,8 @@ fn is_main_thread() -> bool { fn is_main_thread() -> bool { std::thread::current().name() == Some("main") } + +#[cfg(target_os = "haiku")] +fn is_main_thread() -> bool { + unsafe { libc::find_thread(std::ptr::null()) == libc::getpid() } +} diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index fef13f4558..aeefd4f78d 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -619,12 +619,14 @@ impl EventLoop { } } +#[cfg(not(target_os = "haiku"))] impl AsFd for EventLoop { fn as_fd(&self) -> BorrowedFd<'_> { self.event_loop.as_fd() } } +#[cfg(not(target_os = "haiku"))] impl AsRawFd for EventLoop { fn as_raw_fd(&self) -> RawFd { self.event_loop.as_raw_fd() @@ -749,7 +751,7 @@ impl PumpEventNotifier { let control = Arc::new((Mutex::new(PumpEventNotifierAction::Pause), Condvar::new())); let control_thread = Arc::clone(&control); - let (read, write) = match pipe::pipe_with(PipeFlags::CLOEXEC | PipeFlags::NONBLOCK) { + let (read, write) = match make_notifier_pipe() { Ok((read, write)) => (read, write), Err(_) => return Self { control, handle: None, worker_waker: None }, }; @@ -808,3 +810,20 @@ enum PumpEventNotifierAction { /// Shutdown the thread. Shutdown, } + +#[cfg(not(target_os = "haiku"))] +fn make_notifier_pipe() -> rustix::io::Result<(OwnedFd, OwnedFd)> { + pipe::pipe_with(PipeFlags::CLOEXEC | PipeFlags::NONBLOCK) +} + +#[cfg(target_os = "haiku")] +fn make_notifier_pipe() -> rustix::io::Result<(OwnedFd, OwnedFd)> { + use rustix::fs::{fcntl_getfl, fcntl_setfl, OFlags}; + use rustix::io::{fcntl_getfd, fcntl_setfd, FdFlags}; + let (read, write) = pipe::pipe()?; + for fd in [&read, &write] { + fcntl_setfd(fd, fcntl_getfd(fd)? | FdFlags::CLOEXEC)?; + fcntl_setfl(fd, fcntl_getfl(fd)? | OFlags::NONBLOCK)?; + } + Ok((read, write)) +} diff --git a/src/platform_impl/linux/wayland/state.rs b/src/platform_impl/linux/wayland/state.rs index 13ef99c26a..87352e84a8 100644 --- a/src/platform_impl/linux/wayland/state.rs +++ b/src/platform_impl/linux/wayland/state.rs @@ -39,6 +39,8 @@ use crate::platform_impl::OsError; /// Winit's Wayland state. pub struct WinitState { + #[cfg(target_os = "haiku")] + pub haiku_decoration_manager: Option, /// The WlRegistry. pub registry_state: RegistryState, @@ -159,6 +161,15 @@ impl WinitState { let custom_cursor_pool = Arc::new(Mutex::new(SlotPool::new(2, &shm).unwrap())); Ok(Self { + #[cfg(target_os = "haiku")] + haiku_decoration_manager: if globals.contents().with_list(|globals| { + globals.iter().any(|global| global.interface == "zxdg_decoration_manager_v1") + }) { + None + } else { + super::types::haiku_decoration::HaikuDecorationManager::bind(globals, queue_handle) + .ok() + }, registry_state, compositor_state: Arc::new(compositor_state), subcompositor_state: subcompositor_state.map(Arc::new), diff --git a/src/platform_impl/linux/wayland/types/haiku_decoration.rs b/src/platform_impl/linux/wayland/types/haiku_decoration.rs new file mode 100644 index 0000000000..0fc668a51c --- /dev/null +++ b/src/platform_impl/linux/wayland/types/haiku_decoration.rs @@ -0,0 +1,29 @@ +//! Native Haiku window borders through the legacy KDE decoration protocol. + +use crate::platform_impl::wayland::state::WinitState; +use sctk::reexports::client::globals::{BindError, GlobalList}; +use sctk::reexports::client::protocol::wl_surface::WlSurface; +use sctk::reexports::client::{delegate_noop, QueueHandle}; +use wayland_protocols_plasma::server_decoration::client::org_kde_kwin_server_decoration::OrgKdeKwinServerDecoration; +use wayland_protocols_plasma::server_decoration::client::org_kde_kwin_server_decoration_manager::OrgKdeKwinServerDecorationManager; + +pub struct HaikuDecorationManager(OrgKdeKwinServerDecorationManager); + +impl HaikuDecorationManager { + pub fn bind(globals: &GlobalList, queue: &QueueHandle) -> Result { + globals.bind(queue, 1..=1, ()).map(Self) + } + + pub fn create( + &self, + surface: &WlSurface, + queue: &QueueHandle, + ) -> OrgKdeKwinServerDecoration { + let decoration = self.0.create(surface, queue, ()); + decoration.request_mode(2); + decoration + } +} + +delegate_noop!(WinitState: ignore OrgKdeKwinServerDecorationManager); +delegate_noop!(WinitState: ignore OrgKdeKwinServerDecoration); diff --git a/src/platform_impl/linux/wayland/types/mod.rs b/src/platform_impl/linux/wayland/types/mod.rs index 77e67f48be..37f71bf581 100644 --- a/src/platform_impl/linux/wayland/types/mod.rs +++ b/src/platform_impl/linux/wayland/types/mod.rs @@ -5,3 +5,6 @@ pub mod kwin_blur; pub mod wp_fractional_scaling; pub mod wp_viewporter; pub mod xdg_activation; + +#[cfg(target_os = "haiku")] +pub mod haiku_decoration; diff --git a/src/platform_impl/linux/wayland/window/state.rs b/src/platform_impl/linux/wayland/window/state.rs index 1ef7a0656c..4b64dfeb79 100644 --- a/src/platform_impl/linux/wayland/window/state.rs +++ b/src/platform_impl/linux/wayland/window/state.rs @@ -54,6 +54,8 @@ const MIN_WINDOW_SIZE: LogicalSize = LogicalSize::new(2, 1); /// The state of the window which is being updated from the [`WinitState`]. pub struct WindowState { + #[cfg(target_os = "haiku")] + haiku_decoration: Option, /// The connection to Wayland server. pub connection: Connection, @@ -184,6 +186,11 @@ impl WindowState { .map(|fsm| fsm.fractional_scaling(window.wl_surface(), queue_handle)); Self { + #[cfg(target_os = "haiku")] + haiku_decoration: winit_state + .haiku_decoration_manager + .as_ref() + .map(|manager| manager.create(window.wl_surface(), queue_handle)), blur: None, blur_manager: winit_state.kwin_blur_manager.clone(), compositor, @@ -267,6 +274,15 @@ impl WindowState { shm: &Shm, subcompositor: &Option>, ) -> bool { + #[cfg(target_os = "haiku")] + let configure = { + let mut configure = configure; + if self.haiku_decoration.is_some() { + configure.decoration_mode = DecorationMode::Server; + } + configure + }; + // NOTE: when using fractional scaling or wl_compositor@v6 the scaling // should be delivered before the first configure, thus apply it to // properly scale the physical sizes provided by the users. @@ -586,6 +602,10 @@ impl WindowState { #[inline] pub fn is_decorated(&mut self) -> bool { + #[cfg(target_os = "haiku")] + if self.haiku_decoration.is_some() { + return self.decorate; + } let csd = self .last_configure .as_ref() @@ -986,6 +1006,10 @@ impl WindowState { } self.decorate = decorate; + #[cfg(target_os = "haiku")] + if let Some(decoration) = &self.haiku_decoration { + decoration.request_mode(if decorate { 2 } else { 1 }); + } match self.last_configure.as_ref().map(|configure| configure.decoration_mode) { Some(DecorationMode::Server) if !self.decorate => { @@ -1149,6 +1173,10 @@ impl WindowState { impl Drop for WindowState { fn drop(&mut self) { + #[cfg(target_os = "haiku")] + if let Some(decoration) = &self.haiku_decoration { + decoration.release(); + } if let Some(blur) = self.blur.take() { blur.release(); } diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index f29b314a7c..0c0b246bf7 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -5,7 +5,9 @@ use std::marker::PhantomData; use std::mem::MaybeUninit; use std::ops::Deref; use std::os::raw::*; -use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; +#[cfg(not(target_os = "haiku"))] +use std::os::unix::io::{AsFd, RawFd}; +use std::os::unix::io::{AsRawFd, BorrowedFd}; use std::sync::mpsc::{self, Receiver, Sender, TryRecvError}; use std::sync::{Arc, Weak}; use std::time::{Duration, Instant}; @@ -618,12 +620,14 @@ impl EventLoop { } } +#[cfg(not(target_os = "haiku"))] impl AsFd for EventLoop { fn as_fd(&self) -> BorrowedFd<'_> { self.event_loop.as_fd() } } +#[cfg(not(target_os = "haiku"))] impl AsRawFd for EventLoop { fn as_raw_fd(&self) -> RawFd { self.event_loop.as_raw_fd()