Skip to content

Commit f7bf833

Browse files
committed
Add Wayland support (#3)
- Implement Wayland surface creation - Add checked pointer unwrapping and error handling for null pointers
1 parent 2094795 commit f7bf833

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

ffi/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ pub extern "C" fn processing_init() {
2828
#[unsafe(no_mangle)]
2929
pub extern "C" fn processing_create_surface(
3030
window_handle: u64,
31+
display_handle: u64,
3132
width: u32,
3233
height: u32,
3334
scale_factor: f32,
3435
) -> u64 {
3536
error::clear_error();
36-
error::check(|| renderer::create_surface(window_handle, width, height, scale_factor))
37-
.unwrap_or(0)
37+
error::check(|| {
38+
renderer::create_surface(window_handle, display_handle, width, height, scale_factor)
39+
})
40+
.unwrap_or(0)
3841
}
3942

4043
/// Destroy the surface associated with the given window ID.

renderer/src/lib.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub mod error;
22
pub mod render;
33

4-
use std::{cell::RefCell, num::NonZero, sync::OnceLock};
4+
use std::{cell::RefCell, ffi::c_void, num::NonZero, ptr::NonNull, sync::OnceLock};
55

66
use bevy::{
77
app::{App, AppExit},
@@ -95,6 +95,7 @@ impl HasDisplayHandle for GlfwWindow {
9595
/// actually create the surface.
9696
pub fn create_surface(
9797
window_handle: u64,
98+
display_handle: u64,
9899
width: u32,
99100
height: u32,
100101
scale_factor: f32,
@@ -132,7 +133,7 @@ pub fn create_surface(
132133
}
133134
};
134135

135-
let window = AppKitWindowHandle::new(std::ptr::NonNull::new(ns_view_ptr).unwrap());
136+
let window = AppKitWindowHandle::new(NonNull::new(ns_view_ptr).unwrap());
136137
let display = AppKitDisplayHandle::new();
137138
(
138139
RawWindowHandle::AppKit(window),
@@ -178,8 +179,30 @@ pub fn create_surface(
178179
};
179180

180181
#[cfg(target_os = "linux")]
181-
let (raw_window_handle, raw_display_handle) =
182-
{ todo!("implement linux raw window handle conversion") };
182+
let (raw_window_handle, raw_display_handle) = {
183+
use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle};
184+
185+
if window_handle == 0 {
186+
return Err(error::ProcessingError::HandleError(
187+
HandleError::Unavailable,
188+
));
189+
}
190+
let window_handle_ptr = NonNull::new(window_handle as *mut c_void).unwrap();
191+
let window = WaylandWindowHandle::new(window_handle_ptr);
192+
193+
if display_handle == 0 {
194+
return Err(error::ProcessingError::HandleError(
195+
HandleError::Unavailable,
196+
));
197+
}
198+
let display_handle_ptr = NonNull::new(display_handle as *mut c_void).unwrap();
199+
let display = WaylandDisplayHandle::new(display_handle_ptr);
200+
201+
(
202+
RawWindowHandle::Wayland(window),
203+
RawDisplayHandle::Wayland(display),
204+
)
205+
};
183206

184207
let glfw_window = GlfwWindow {
185208
window_handle: raw_window_handle,

0 commit comments

Comments
 (0)