Skip to main content

tgui_winit_ohos/
lib.rs

1//! OpenHarmony backend for the split Winit architecture.
2
3mod app;
4mod event_loop;
5pub mod ffi;
6mod keycodes;
7#[doc(hidden)]
8pub mod log;
9
10pub use crate::app::{
11    OhosApp, OhosKeyAction, OhosMouseAction, OhosMouseButton, OhosPointerSource, OhosTouchAction,
12};
13pub use crate::event_loop::{
14    ActiveEventLoop, EventLoop, EventLoopBuilder, EventLoopBuilderExtOhos,
15    PlatformSpecificEventLoopAttributes, PlatformSpecificWindowAttributes, Window,
16};
17pub use crate::log::{
18    DEFAULT_LOG_DOMAIN, DEFAULT_LOG_TAG, LOG_PREFIX, OhosLogLevel, deveco_log, deveco_log_with,
19    deveco_log_with_level,
20};
21pub use winit_core::event_loop::EventLoopProxy;
22
23use std::ffi::c_void;
24
25use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;
26use winit_core::window::Window as CoreWindow;
27
28pub trait ActiveEventLoopExtOhos {
29    fn ohos_app(&self) -> &OhosApp;
30}
31
32impl ActiveEventLoopExtOhos for dyn CoreActiveEventLoop + '_ {
33    fn ohos_app(&self) -> &OhosApp {
34        let event_loop = self
35            .cast_ref::<crate::event_loop::ActiveEventLoop>()
36            .expect("ActiveEventLoop is not an OHOS backend instance");
37        event_loop.ohos_app()
38    }
39}
40
41pub trait WindowExtOhos {
42    fn xcomponent_ptr(&self) -> *mut c_void;
43    fn native_window_ptr(&self) -> *mut c_void;
44    fn density_scale(&self) -> f64;
45    fn font_scale(&self) -> f64;
46}
47
48impl WindowExtOhos for dyn CoreWindow + '_ {
49    fn xcomponent_ptr(&self) -> *mut c_void {
50        let window = self
51            .cast_ref::<crate::event_loop::Window>()
52            .expect("Window is not an OHOS backend instance");
53        window.xcomponent_ptr()
54    }
55
56    fn native_window_ptr(&self) -> *mut c_void {
57        let window = self
58            .cast_ref::<crate::event_loop::Window>()
59            .expect("Window is not an OHOS backend instance");
60        window.native_window_ptr()
61    }
62
63    fn density_scale(&self) -> f64 {
64        let window = self
65            .cast_ref::<crate::event_loop::Window>()
66            .expect("Window is not an OHOS backend instance");
67        window.density_scale()
68    }
69
70    fn font_scale(&self) -> f64 {
71        let window = self
72            .cast_ref::<crate::event_loop::Window>()
73            .expect("Window is not an OHOS backend instance");
74        window.font_scale()
75    }
76}
77
78#[macro_export]
79macro_rules! export_ohos_winit_app {
80    ($factory:path) => {
81        fn __tgui_winit_ohos_factory() -> Box<dyn $crate::ffi::RuntimeApplication> {
82            Box::new($factory())
83        }
84
85        #[unsafe(no_mangle)]
86        pub extern "C" fn ohos_winit_runtime_new() -> *mut ::std::ffi::c_void {
87            $crate::ffi::runtime_new(__tgui_winit_ohos_factory) as *mut ::std::ffi::c_void
88        }
89
90        #[unsafe(no_mangle)]
91        pub unsafe extern "C" fn ohos_winit_runtime_free(runtime: *mut ::std::ffi::c_void) {
92            unsafe { $crate::ffi::runtime_free(runtime.cast()) }
93        }
94
95        #[unsafe(no_mangle)]
96        pub unsafe extern "C" fn ohos_winit_runtime_log(message: *const ::std::ffi::c_char) {
97            unsafe { $crate::log::deveco_log_from_c(message) }
98        }
99
100        #[unsafe(no_mangle)]
101        pub unsafe extern "C" fn ohos_winit_runtime_surface_created(
102            runtime: *const ::std::ffi::c_void,
103            xcomponent: *mut ::std::ffi::c_void,
104            native_window: *mut ::std::ffi::c_void,
105            width: u32,
106            height: u32,
107            density_scale: f64,
108            font_scale: f64,
109        ) {
110            unsafe {
111                $crate::ffi::runtime_surface_created(
112                    runtime.cast(),
113                    xcomponent,
114                    native_window,
115                    width,
116                    height,
117                    density_scale,
118                    font_scale,
119                )
120            }
121        }
122
123        #[unsafe(no_mangle)]
124        pub unsafe extern "C" fn ohos_winit_runtime_surface_changed(
125            runtime: *const ::std::ffi::c_void,
126            xcomponent: *mut ::std::ffi::c_void,
127            native_window: *mut ::std::ffi::c_void,
128            width: u32,
129            height: u32,
130            density_scale: f64,
131            font_scale: f64,
132        ) {
133            unsafe {
134                $crate::ffi::runtime_surface_changed(
135                    runtime.cast(),
136                    xcomponent,
137                    native_window,
138                    width,
139                    height,
140                    density_scale,
141                    font_scale,
142                )
143            }
144        }
145
146        #[unsafe(no_mangle)]
147        pub unsafe extern "C" fn ohos_winit_runtime_surface_destroyed(
148            runtime: *const ::std::ffi::c_void,
149        ) {
150            unsafe { $crate::ffi::runtime_surface_destroyed(runtime.cast()) }
151        }
152
153        #[unsafe(no_mangle)]
154        pub unsafe extern "C" fn ohos_winit_runtime_focus(
155            runtime: *const ::std::ffi::c_void,
156            focused: bool,
157        ) {
158            unsafe { $crate::ffi::runtime_focus(runtime.cast(), focused) }
159        }
160
161        #[unsafe(no_mangle)]
162        pub unsafe extern "C" fn ohos_winit_runtime_visibility(
163            runtime: *const ::std::ffi::c_void,
164            visible: bool,
165        ) {
166            unsafe { $crate::ffi::runtime_visibility(runtime.cast(), visible) }
167        }
168
169        #[unsafe(no_mangle)]
170        pub unsafe extern "C" fn ohos_winit_runtime_low_memory(runtime: *const ::std::ffi::c_void) {
171            unsafe { $crate::ffi::runtime_low_memory(runtime.cast()) }
172        }
173
174        #[unsafe(no_mangle)]
175        pub unsafe extern "C" fn ohos_winit_runtime_frame(runtime: *const ::std::ffi::c_void) {
176            unsafe { $crate::ffi::runtime_frame(runtime.cast()) }
177        }
178
179        #[unsafe(no_mangle)]
180        pub unsafe extern "C" fn ohos_winit_runtime_key(
181            runtime: *const ::std::ffi::c_void,
182            action: u32,
183            key_code: u32,
184            repeat: bool,
185            device_id: i64,
186        ) {
187            unsafe { $crate::ffi::runtime_key(runtime.cast(), action, key_code, repeat, device_id) }
188        }
189
190        #[unsafe(no_mangle)]
191        pub unsafe extern "C" fn ohos_winit_runtime_touch(
192            runtime: *const ::std::ffi::c_void,
193            action: u32,
194            source: u32,
195            finger_id: u64,
196            x: f64,
197            y: f64,
198            force: f64,
199            has_force: bool,
200            device_id: i64,
201            primary: bool,
202        ) {
203            unsafe {
204                $crate::ffi::runtime_touch(
205                    runtime.cast(),
206                    action,
207                    source,
208                    finger_id,
209                    x,
210                    y,
211                    force,
212                    has_force,
213                    device_id,
214                    primary,
215                )
216            }
217        }
218
219        #[unsafe(no_mangle)]
220        pub unsafe extern "C" fn ohos_winit_runtime_mouse(
221            runtime: *const ::std::ffi::c_void,
222            action: u32,
223            button: u32,
224            has_button: bool,
225            x: f64,
226            y: f64,
227            delta_x: f64,
228            delta_y: f64,
229            device_id: i64,
230            primary: bool,
231        ) {
232            unsafe {
233                $crate::ffi::runtime_mouse(
234                    runtime.cast(),
235                    action,
236                    button,
237                    has_button,
238                    x,
239                    y,
240                    delta_x,
241                    delta_y,
242                    device_id,
243                    primary,
244                )
245            }
246        }
247    };
248}