Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ unsafe impl<T> rwh_05::HasRawDisplayHandle for EventLoop<T> {
}

#[cfg(any(x11_platform, wayland_platform))]
#[cfg(not(target_os = "haiku"))]
impl<T> AsFd for EventLoop<T> {
/// Get the underlying [EventLoop]'s `fd` which you can register
/// into other event loop, like [`calloop`] or [`mio`]. When doing so, the
Expand All @@ -352,6 +353,7 @@ impl<T> AsFd for EventLoop<T> {
}

#[cfg(any(x11_platform, wayland_platform))]
#[cfg(not(target_os = "haiku"))]
impl<T> AsRawFd for EventLoop<T> {
/// Get the underlying [EventLoop]'s raw `fd` which you can register
/// into other event loop, like [`calloop`] or [`mio`]. When doing so, the
Expand Down
7 changes: 7 additions & 0 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,12 +832,14 @@ impl<T: 'static> EventLoop<T> {
}
}

#[cfg(not(target_os = "haiku"))]
impl<T> AsFd for EventLoop<T> {
fn as_fd(&self) -> BorrowedFd<'_> {
x11_or_wayland!(match self; EventLoop(evlp) => evlp.as_fd())
}
}

#[cfg(not(target_os = "haiku"))]
impl<T> AsRawFd for EventLoop<T> {
fn as_raw_fd(&self) -> RawFd {
x11_or_wayland!(match self; EventLoop(evlp) => evlp.as_raw_fd())
Expand Down Expand Up @@ -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() }
}
21 changes: 20 additions & 1 deletion src/platform_impl/linux/wayland/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,14 @@ impl<T: 'static> EventLoop<T> {
}
}

#[cfg(not(target_os = "haiku"))]
impl<T> AsFd for EventLoop<T> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.event_loop.as_fd()
}
}

#[cfg(not(target_os = "haiku"))]
impl<T> AsRawFd for EventLoop<T> {
fn as_raw_fd(&self) -> RawFd {
self.event_loop.as_raw_fd()
Expand Down Expand Up @@ -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 },
};
Expand Down Expand Up @@ -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))
}
11 changes: 11 additions & 0 deletions src/platform_impl/linux/wayland/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<super::types::haiku_decoration::HaikuDecorationManager>,
/// The WlRegistry.
pub registry_state: RegistryState,

Expand Down Expand Up @@ -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),
Expand Down
29 changes: 29 additions & 0 deletions src/platform_impl/linux/wayland/types/haiku_decoration.rs
Original file line number Diff line number Diff line change
@@ -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<WinitState>) -> Result<Self, BindError> {
globals.bind(queue, 1..=1, ()).map(Self)
}

pub fn create(
&self,
surface: &WlSurface,
queue: &QueueHandle<WinitState>,
) -> OrgKdeKwinServerDecoration {
let decoration = self.0.create(surface, queue, ());
decoration.request_mode(2);
decoration
}
}

delegate_noop!(WinitState: ignore OrgKdeKwinServerDecorationManager);
delegate_noop!(WinitState: ignore OrgKdeKwinServerDecoration);
3 changes: 3 additions & 0 deletions src/platform_impl/linux/wayland/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
28 changes: 28 additions & 0 deletions src/platform_impl/linux/wayland/window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const MIN_WINDOW_SIZE: LogicalSize<u32> = 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<wayland_protocols_plasma::server_decoration::client::org_kde_kwin_server_decoration::OrgKdeKwinServerDecoration>,
/// The connection to Wayland server.
pub connection: Connection,

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -267,6 +274,15 @@ impl WindowState {
shm: &Shm,
subcompositor: &Option<Arc<SubcompositorState>>,
) -> 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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 5 additions & 1 deletion src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -618,12 +620,14 @@ impl<T: 'static> EventLoop<T> {
}
}

#[cfg(not(target_os = "haiku"))]
impl<T> AsFd for EventLoop<T> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.event_loop.as_fd()
}
}

#[cfg(not(target_os = "haiku"))]
impl<T> AsRawFd for EventLoop<T> {
fn as_raw_fd(&self) -> RawFd {
self.event_loop.as_raw_fd()
Expand Down
Loading