forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.rs
694 lines (615 loc) · 24 KB
/
platform.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
use alloc::rc::Rc;
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, string::String};
use core::ffi::c_void;
use i_slint_core::api::{
LogicalSize, PhysicalPosition, PhysicalSize, Window, WindowPosition, WindowSize,
};
use i_slint_core::graphics::euclid;
use i_slint_core::graphics::IntSize;
use i_slint_core::platform::{Clipboard, Platform, PlatformError};
use i_slint_core::renderer::Renderer;
use i_slint_core::window::ffi::WindowAdapterRcOpaque;
use i_slint_core::window::{WindowAdapter, WindowProperties};
use i_slint_core::SharedString;
type WindowAdapterUserData = *mut c_void;
// FIXME wrapper over &dyn Renderer
#[repr(C)]
pub struct RendererPtr {
_a: *const c_void,
_b: *const c_void,
}
pub struct CppWindowAdapter {
window: Window,
user_data: WindowAdapterUserData,
drop: unsafe extern "C" fn(WindowAdapterUserData),
/// Safety: the returned pointer must live for the lifetime of self
get_renderer_ref: unsafe extern "C" fn(WindowAdapterUserData) -> RendererPtr,
set_visible: unsafe extern "C" fn(WindowAdapterUserData, bool),
request_redraw: unsafe extern "C" fn(WindowAdapterUserData),
size: unsafe extern "C" fn(WindowAdapterUserData) -> IntSize,
set_size: unsafe extern "C" fn(WindowAdapterUserData, IntSize),
update_window_properties: unsafe extern "C" fn(WindowAdapterUserData, &WindowProperties),
position:
unsafe extern "C" fn(WindowAdapterUserData, &mut euclid::default::Point2D<i32>) -> bool,
set_position: unsafe extern "C" fn(WindowAdapterUserData, euclid::default::Point2D<i32>),
}
impl Drop for CppWindowAdapter {
fn drop(&mut self) {
unsafe { (self.drop)(self.user_data) };
}
}
impl WindowAdapter for CppWindowAdapter {
fn window(&self) -> &Window {
&self.window
}
fn set_visible(&self, visible: bool) -> Result<(), PlatformError> {
unsafe { (self.set_visible)(self.user_data, visible) };
Ok(())
}
fn position(&self) -> Option<PhysicalPosition> {
let mut pos = euclid::default::Point2D::<i32>::default();
if unsafe { (self.position)(self.user_data, &mut pos) } {
Some(i_slint_core::graphics::ffi::physical_position_to_api(pos))
} else {
None
}
}
fn set_position(&self, position: WindowPosition) {
let physical_position = i_slint_core::graphics::ffi::physical_position_from_api(
position.to_physical(self.window.scale_factor()),
);
unsafe { (self.set_position)(self.user_data, physical_position) }
}
fn set_size(&self, size: WindowSize) {
let physical_size = i_slint_core::graphics::ffi::physical_size_from_api(
size.to_physical(self.window.scale_factor()),
);
unsafe { (self.set_size)(self.user_data, physical_size) }
}
fn size(&self) -> PhysicalSize {
let s = unsafe { (self.size)(self.user_data) };
PhysicalSize::new(s.width, s.height)
}
fn renderer(&self) -> &dyn Renderer {
unsafe { core::mem::transmute((self.get_renderer_ref)(self.user_data)) }
}
fn request_redraw(&self) {
unsafe { (self.request_redraw)(self.user_data) }
}
fn update_window_properties(&self, properties: WindowProperties<'_>) {
unsafe { (self.update_window_properties)(self.user_data, &properties) }
}
}
#[no_mangle]
pub extern "C" fn slint_window_properties_get_title(wp: &WindowProperties, out: &mut SharedString) {
*out = wp.title();
}
#[no_mangle]
pub extern "C" fn slint_window_properties_get_background(
wp: &WindowProperties,
out: &mut i_slint_core::Brush,
) {
*out = wp.background();
}
#[no_mangle]
pub extern "C" fn slint_window_properties_get_fullscreen(wp: &WindowProperties) -> bool {
wp.is_fullscreen()
}
#[no_mangle]
pub extern "C" fn slint_window_properties_get_minimized(wp: &WindowProperties) -> bool {
wp.is_minimized()
}
#[no_mangle]
pub extern "C" fn slint_window_properties_get_maximized(wp: &WindowProperties) -> bool {
wp.is_maximized()
}
#[repr(C)]
#[derive(Clone, Copy)]
/// a Repr(C) variant of slint::platform::LayoutConstraints
pub struct LayoutConstraintsReprC {
pub min: i_slint_core::graphics::Size,
pub max: i_slint_core::graphics::Size,
pub preferred: i_slint_core::graphics::Size,
pub has_min: bool,
pub has_max: bool,
}
#[no_mangle]
pub extern "C" fn slint_window_properties_get_layout_constraints(
wp: &WindowProperties,
) -> LayoutConstraintsReprC {
let c = wp.layout_constraints();
LayoutConstraintsReprC {
min: i_slint_core::lengths::logical_size_from_api(c.min.unwrap_or_default()).to_untyped(),
max: i_slint_core::lengths::logical_size_from_api(
c.max.unwrap_or(LogicalSize { width: f32::MAX, height: f32::MAX }),
)
.to_untyped(),
preferred: i_slint_core::lengths::logical_size_from_api(c.preferred).to_untyped(),
has_min: c.min.is_some(),
has_max: c.max.is_some(),
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_window_adapter_new(
user_data: WindowAdapterUserData,
drop: unsafe extern "C" fn(WindowAdapterUserData),
get_renderer_ref: unsafe extern "C" fn(WindowAdapterUserData) -> RendererPtr,
set_visible: unsafe extern "C" fn(WindowAdapterUserData, bool),
request_redraw: unsafe extern "C" fn(WindowAdapterUserData),
size: unsafe extern "C" fn(WindowAdapterUserData) -> IntSize,
set_size: unsafe extern "C" fn(WindowAdapterUserData, IntSize),
update_window_properties: unsafe extern "C" fn(WindowAdapterUserData, &WindowProperties),
position: unsafe extern "C" fn(
WindowAdapterUserData,
&mut euclid::default::Point2D<i32>,
) -> bool,
set_position: unsafe extern "C" fn(WindowAdapterUserData, euclid::default::Point2D<i32>),
target: *mut WindowAdapterRcOpaque,
) {
let window = Rc::<CppWindowAdapter>::new_cyclic(|w| CppWindowAdapter {
window: Window::new(w.clone()),
user_data,
drop,
get_renderer_ref,
set_visible,
request_redraw,
size,
set_size,
update_window_properties,
position,
set_position,
});
core::ptr::write(target as *mut Rc<dyn WindowAdapter>, window);
}
type PlatformUserData = *mut c_void;
struct CppPlatform {
user_data: PlatformUserData,
drop: unsafe extern "C" fn(PlatformUserData),
window_factory: unsafe extern "C" fn(PlatformUserData, *mut WindowAdapterRcOpaque),
#[cfg(not(feature = "std"))]
duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64,
// silent the warning despite `Clipboard` is a `#[non_exhaustive]` enum from another crate.
#[allow(improper_ctypes_definitions)]
set_clipboard_text: unsafe extern "C" fn(PlatformUserData, &SharedString, Clipboard),
#[allow(improper_ctypes_definitions)]
clipboard_text: unsafe extern "C" fn(PlatformUserData, &mut SharedString, Clipboard) -> bool,
run_event_loop: unsafe extern "C" fn(PlatformUserData),
quit_event_loop: unsafe extern "C" fn(PlatformUserData),
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque),
}
impl Drop for CppPlatform {
fn drop(&mut self) {
unsafe { (self.drop)(self.user_data) };
}
}
impl Platform for CppPlatform {
fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, PlatformError> {
let mut uninit = core::mem::MaybeUninit::<Rc<dyn WindowAdapter>>::uninit();
unsafe {
(self.window_factory)(
self.user_data,
uninit.as_mut_ptr() as *mut WindowAdapterRcOpaque,
);
Ok(uninit.assume_init())
}
}
#[cfg(not(feature = "std"))]
fn duration_since_start(&self) -> core::time::Duration {
core::time::Duration::from_millis(unsafe { (self.duration_since_start)(self.user_data) })
}
fn run_event_loop(&self) -> Result<(), PlatformError> {
unsafe { (self.run_event_loop)(self.user_data) };
Ok(())
}
fn new_event_loop_proxy(&self) -> Option<Box<dyn i_slint_core::platform::EventLoopProxy>> {
Some(Box::new(CppEventLoopProxy {
user_data: self.user_data,
quit_event_loop: self.quit_event_loop,
invoke_from_event_loop: self.invoke_from_event_loop,
}))
}
fn set_clipboard_text(&self, text: &str, clipboard: Clipboard) {
let shared_text = SharedString::from(text);
unsafe { (self.set_clipboard_text)(self.user_data, &shared_text, clipboard) }
}
fn clipboard_text(&self, clipboard: Clipboard) -> Option<String> {
let mut out_text = SharedString::new();
let status = unsafe { (self.clipboard_text)(self.user_data, &mut out_text, clipboard) };
status.then(|| out_text.into())
}
#[cfg(feature = "esp-println")]
fn debug_log(&self, arguments: core::fmt::Arguments) {
esp_println::println!("{}", arguments);
}
}
struct CppEventLoopProxy {
user_data: PlatformUserData,
quit_event_loop: unsafe extern "C" fn(PlatformUserData),
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque),
}
impl i_slint_core::platform::EventLoopProxy for CppEventLoopProxy {
fn quit_event_loop(&self) -> Result<(), i_slint_core::api::EventLoopError> {
unsafe { (self.quit_event_loop)(self.user_data) };
Ok(())
}
fn invoke_from_event_loop(
&self,
event: Box<dyn FnOnce() + Send>,
) -> Result<(), i_slint_core::api::EventLoopError> {
unsafe {
(self.invoke_from_event_loop)(
self.user_data,
core::mem::transmute::<*mut dyn FnOnce(), PlatformTaskOpaque>(Box::into_raw(event)),
)
};
Ok(())
}
}
unsafe impl Send for CppEventLoopProxy {}
unsafe impl Sync for CppEventLoopProxy {}
// silent the warning depite `Clipboard` is a `#[non_exhaustive]` enum from another crate.
#[allow(improper_ctypes_definitions)]
#[no_mangle]
pub unsafe extern "C" fn slint_platform_register(
user_data: PlatformUserData,
drop: unsafe extern "C" fn(PlatformUserData),
window_factory: unsafe extern "C" fn(PlatformUserData, *mut WindowAdapterRcOpaque),
#[allow(unused)] duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64,
set_clipboard_text: unsafe extern "C" fn(PlatformUserData, &SharedString, Clipboard),
clipboard_text: unsafe extern "C" fn(PlatformUserData, &mut SharedString, Clipboard) -> bool,
run_event_loop: unsafe extern "C" fn(PlatformUserData),
quit_event_loop: unsafe extern "C" fn(PlatformUserData),
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque),
) {
let p = CppPlatform {
user_data,
drop,
window_factory,
#[cfg(not(feature = "std"))]
duration_since_start,
set_clipboard_text,
clipboard_text,
run_event_loop,
quit_event_loop,
invoke_from_event_loop,
};
i_slint_core::platform::set_platform(Box::new(p)).unwrap();
}
#[no_mangle]
pub unsafe extern "C" fn slint_windowrc_has_active_animations(
handle: *const WindowAdapterRcOpaque,
) -> bool {
let window_adapter = &*(handle as *const Rc<dyn WindowAdapter>);
window_adapter.window().has_active_animations()
}
#[no_mangle]
pub extern "C" fn slint_platform_update_timers_and_animations() {
i_slint_core::platform::update_timers_and_animations()
}
/// Returns the duration in millisecond until the next timer or `u64::MAX` if there is no pending timers
#[no_mangle]
pub extern "C" fn slint_platform_duration_until_next_timer_update() -> u64 {
i_slint_core::platform::duration_until_next_timer_update()
.map_or(u64::MAX, |d| d.as_millis() as u64)
}
#[repr(C)]
pub struct PlatformTaskOpaque(*const c_void, *const c_void);
#[no_mangle]
pub unsafe extern "C" fn slint_platform_task_drop(event: PlatformTaskOpaque) {
drop(Box::from_raw(core::mem::transmute::<PlatformTaskOpaque, *mut dyn FnOnce()>(event)));
}
#[no_mangle]
pub unsafe extern "C" fn slint_platform_task_run(event: PlatformTaskOpaque) {
let f = Box::from_raw(core::mem::transmute::<PlatformTaskOpaque, *mut dyn FnOnce()>(event));
f();
}
#[cfg(feature = "renderer-software")]
mod software_renderer {
use super::*;
type SoftwareRendererOpaque = *const c_void;
use i_slint_core::graphics::{IntRect, Rgb8Pixel};
use i_slint_core::software_renderer::{
PhysicalRegion, RepaintBufferType, Rgb565Pixel, SoftwareRenderer,
};
use i_slint_core::SharedVector;
#[no_mangle]
pub unsafe extern "C" fn slint_software_renderer_new(
buffer_age: u32,
) -> SoftwareRendererOpaque {
let repaint_buffer_type = match buffer_age {
0 => RepaintBufferType::NewBuffer,
1 => RepaintBufferType::ReusedBuffer,
2 => RepaintBufferType::SwappedBuffers,
_ => unreachable!(),
};
Box::into_raw(Box::new(SoftwareRenderer::new_with_repaint_buffer_type(repaint_buffer_type)))
as SoftwareRendererOpaque
}
#[no_mangle]
pub unsafe extern "C" fn slint_software_renderer_drop(r: SoftwareRendererOpaque) {
drop(Box::from_raw(r as *mut SoftwareRenderer));
}
#[no_mangle]
pub unsafe extern "C" fn slint_software_renderer_render_rgb8(
r: SoftwareRendererOpaque,
buffer: *mut Rgb8Pixel,
buffer_len: usize,
pixel_stride: usize,
) -> PhysicalRegion {
let buffer = core::slice::from_raw_parts_mut(buffer, buffer_len);
let renderer = &*(r as *const SoftwareRenderer);
renderer.render(buffer, pixel_stride)
}
#[no_mangle]
pub unsafe extern "C" fn slint_software_renderer_render_rgb565(
r: SoftwareRendererOpaque,
buffer: *mut u16,
buffer_len: usize,
pixel_stride: usize,
) -> PhysicalRegion {
let buffer = core::slice::from_raw_parts_mut(buffer as *mut Rgb565Pixel, buffer_len);
let renderer = &*(r as *const SoftwareRenderer);
renderer.render(buffer, pixel_stride)
}
#[cfg(feature = "experimental")]
#[no_mangle]
pub unsafe extern "C" fn slint_software_renderer_render_by_line_rgb565(
r: SoftwareRendererOpaque,
process_line_fn: extern "C" fn(
*mut core::ffi::c_void,
usize,
usize,
usize,
extern "C" fn(*const core::ffi::c_void, *mut u16, usize),
*const core::ffi::c_void,
),
user_data: *mut core::ffi::c_void,
) -> PhysicalRegion {
struct Rgb565Processor {
process_line_fn: extern "C" fn(
*mut core::ffi::c_void,
usize,
usize,
usize,
extern "C" fn(*const core::ffi::c_void, *mut u16, usize),
*const core::ffi::c_void,
),
user_data: *mut core::ffi::c_void,
}
impl i_slint_core::software_renderer::LineBufferProvider for Rgb565Processor {
type TargetPixel = Rgb565Pixel;
fn process_line(
&mut self,
line: usize,
range: core::ops::Range<usize>,
render_fn: impl FnOnce(&mut [Rgb565Pixel]),
) {
self.cpp_process_line(line, range, render_fn);
}
}
impl Rgb565Processor {
fn cpp_process_line<RenderFn: FnOnce(&mut [Rgb565Pixel])>(
&mut self,
line: usize,
range: core::ops::Range<usize>,
render_fn: RenderFn,
) {
let mut render_fn = Some(render_fn);
let render_fn_ptr =
&mut render_fn as *mut Option<RenderFn> as *const core::ffi::c_void;
extern "C" fn cpp_render_line_callback<RenderFn: FnOnce(&mut [Rgb565Pixel])>(
render_fn_ptr: *const core::ffi::c_void,
line_start: *mut u16,
len: usize,
) {
let line_slice = unsafe {
core::slice::from_raw_parts_mut(line_start as *mut Rgb565Pixel, len)
};
let render_fn =
unsafe { (*(render_fn_ptr as *mut Option<RenderFn>)).take().unwrap() };
render_fn(line_slice);
}
(self.process_line_fn)(
self.user_data,
line,
range.start,
range.end,
cpp_render_line_callback::<RenderFn>,
render_fn_ptr,
);
}
}
let renderer = &*(r as *const SoftwareRenderer);
let processor = Rgb565Processor { process_line_fn, user_data };
renderer.render_by_line(processor)
}
#[no_mangle]
pub unsafe extern "C" fn slint_software_renderer_set_rendering_rotation(
r: SoftwareRendererOpaque,
rotation: i32,
) {
use i_slint_core::software_renderer::RenderingRotation;
let renderer = &*(r as *const SoftwareRenderer);
renderer.set_rendering_rotation(match rotation {
90 => RenderingRotation::Rotate90,
180 => RenderingRotation::Rotate180,
270 => RenderingRotation::Rotate270,
_ => RenderingRotation::NoRotation,
});
}
#[no_mangle]
pub unsafe extern "C" fn slint_software_renderer_handle(
r: SoftwareRendererOpaque,
) -> RendererPtr {
let r = (r as *const SoftwareRenderer) as *const dyn Renderer;
core::mem::transmute(r)
}
#[no_mangle]
pub extern "C" fn slint_software_renderer_region_to_rects(
region: &PhysicalRegion,
out: &mut SharedVector<IntRect>,
) {
*out = region
.iter()
.map(|r| euclid::rect(r.0.x, r.0.y, r.1.width as i32, r.1.height as i32))
.collect();
}
}
#[cfg(all(feature = "i-slint-renderer-skia", feature = "raw-window-handle"))]
pub mod skia {
use super::*;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use std::rc::Rc;
struct RawHandlePair((RawWindowHandle, RawDisplayHandle));
impl raw_window_handle::HasDisplayHandle for RawHandlePair {
fn display_handle(
&self,
) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
// Safety: It is assumed that the C++ side keeps the window/display handles alive.
Ok(unsafe { raw_window_handle::DisplayHandle::borrow_raw(self.0 .1) })
}
}
impl raw_window_handle::HasWindowHandle for RawHandlePair {
fn window_handle(
&self,
) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
// Safety: It is assumed that the C++ side keeps the window/display handles alive.
Ok(unsafe { raw_window_handle::WindowHandle::borrow_raw(self.0 .0) })
}
}
struct CppRawHandle(Rc<RawHandlePair>);
impl From<(RawWindowHandle, RawDisplayHandle)> for CppRawHandle {
fn from(pair: (RawWindowHandle, RawDisplayHandle)) -> Self {
Self(Rc::new(RawHandlePair(pair)))
}
}
type CppRawHandleOpaque = *const c_void;
#[no_mangle]
pub unsafe extern "C" fn slint_new_raw_window_handle_win32(
hwnd: *mut c_void,
_hinstance: *mut c_void,
) -> CppRawHandleOpaque {
let handle = CppRawHandle::from((
RawWindowHandle::Win32(raw_window_handle::Win32WindowHandle::new(
(hwnd as isize).try_into().expect("C++: NativeWindowHandle created with null hwnd"),
)),
RawDisplayHandle::Windows(raw_window_handle::WindowsDisplayHandle::new()),
));
Box::into_raw(Box::new(handle)) as CppRawHandleOpaque
}
#[no_mangle]
pub unsafe extern "C" fn slint_new_raw_window_handle_x11_xcb(
window: u32,
visual_id: u32,
connection: *mut c_void,
screen: core::ffi::c_int,
) -> CppRawHandleOpaque {
use raw_window_handle::{XcbDisplayHandle, XcbWindowHandle};
let handle = CppRawHandle::from((
RawWindowHandle::Xcb({
let mut hnd = XcbWindowHandle::new(
window
.try_into()
.expect("C++: NativeWindowHandle created with null xcb window handle"),
);
hnd.visual_id = visual_id.try_into().ok();
hnd
}),
RawDisplayHandle::Xcb(XcbDisplayHandle::new(
Some(core::ptr::NonNull::new_unchecked(connection)),
screen,
)),
));
Box::into_raw(Box::new(handle)) as CppRawHandleOpaque
}
#[no_mangle]
pub unsafe extern "C" fn slint_new_raw_window_handle_x11_xlib(
window: core::ffi::c_ulong,
visual_id: core::ffi::c_ulong,
display: *mut c_void,
screen: core::ffi::c_int,
) -> CppRawHandleOpaque {
use raw_window_handle::{XlibDisplayHandle, XlibWindowHandle};
let handle = CppRawHandle::from((
RawWindowHandle::Xlib({
let mut hnd = XlibWindowHandle::new(window);
hnd.visual_id = visual_id;
hnd
}),
RawDisplayHandle::Xlib(XlibDisplayHandle::new(
Some(core::ptr::NonNull::new_unchecked(display)),
screen,
)),
));
Box::into_raw(Box::new(handle)) as CppRawHandleOpaque
}
#[no_mangle]
pub unsafe extern "C" fn slint_new_raw_window_handle_wayland(
surface: *mut c_void,
display: *mut c_void,
) -> CppRawHandleOpaque {
use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle};
let handle = CppRawHandle::from((
RawWindowHandle::Wayland(WaylandWindowHandle::new(core::ptr::NonNull::new_unchecked(
surface,
))),
RawDisplayHandle::Wayland(WaylandDisplayHandle::new(
core::ptr::NonNull::new_unchecked(display),
)),
));
Box::into_raw(Box::new(handle)) as CppRawHandleOpaque
}
#[no_mangle]
pub unsafe extern "C" fn slint_new_raw_window_handle_appkit(
ns_view: *mut c_void,
_ns_window: *mut c_void,
) -> CppRawHandleOpaque {
use raw_window_handle::{AppKitDisplayHandle, AppKitWindowHandle};
let handle = CppRawHandle::from((
RawWindowHandle::AppKit(AppKitWindowHandle::new(core::ptr::NonNull::new_unchecked(
ns_view,
))),
RawDisplayHandle::AppKit(AppKitDisplayHandle::new()),
));
Box::into_raw(Box::new(handle)) as CppRawHandleOpaque
}
#[no_mangle]
pub unsafe extern "C" fn slint_raw_window_handle_drop(handle: CppRawHandleOpaque) {
drop(Box::from_raw(handle as *mut CppRawHandle))
}
type SkiaRendererOpaque = *const c_void;
type SkiaRenderer = i_slint_renderer_skia::SkiaRenderer;
#[no_mangle]
pub unsafe extern "C" fn slint_skia_renderer_new(
handle_opaque: CppRawHandleOpaque,
size: IntSize,
) -> SkiaRendererOpaque {
let handle = &*(handle_opaque as *const CppRawHandle);
let boxed_renderer: Box<SkiaRenderer> = Box::new(
SkiaRenderer::new(
handle.0.clone(),
handle.0.clone(),
PhysicalSize { width: size.width, height: size.height },
)
.unwrap(),
);
Box::into_raw(boxed_renderer) as SkiaRendererOpaque
}
#[no_mangle]
pub unsafe extern "C" fn slint_skia_renderer_drop(r: SkiaRendererOpaque) {
drop(Box::from_raw(r as *mut SkiaRenderer))
}
#[no_mangle]
pub unsafe extern "C" fn slint_skia_renderer_render(r: SkiaRendererOpaque) {
let r = &*(r as *const SkiaRenderer);
r.render().unwrap();
}
#[no_mangle]
pub unsafe extern "C" fn slint_skia_renderer_handle(r: SkiaRendererOpaque) -> RendererPtr {
let r = (r as *const SkiaRenderer) as *const dyn Renderer;
core::mem::transmute(r)
}
}