strata-ui-platform 0.1.1

Native Hardware Abstraction Layer for Android, iOS, macOS, Windows, Linux, and Web
Documentation
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
//! Native Android NDK NativeActivity Windowing and Event Loop.
//!
//! Provides a zero-dependency Android NDK NativeActivity implementation backed by
//! `ANativeWindow`, `android_app` lifecycle state machine (`APP_CMD_INIT_WINDOW`,
//! `APP_CMD_TERM_WINDOW`), touch motion event translation (`AMotionEvent`),
//! and Vulkan / software surface swapchain presentation.

#![cfg(target_os = "android")]

use crate::events::{KeyCode, Modifiers, MouseButton, PlatformEvent};
use crate::window::{
    CursorIcon, EventLoop, EventLoopControl, PlatformWindow, WindowConfig, WindowMode,
};
use std::collections::VecDeque;
use std::ffi::c_void;

// Android NDK lifecycle command constants
pub const APP_CMD_INPUT_CHANGED: i32 = 0;
pub const APP_CMD_INIT_WINDOW: i32 = 1;
pub const APP_CMD_TERM_WINDOW: i32 = 2;
pub const APP_CMD_WINDOW_RESIZED: i32 = 3;
pub const APP_CMD_WINDOW_REDRAW_NEEDED: i32 = 4;
pub const APP_CMD_CONTENT_RECT_CHANGED: i32 = 5;
pub const APP_CMD_GAINED_FOCUS: i32 = 6;
pub const APP_CMD_LOST_FOCUS: i32 = 7;
pub const APP_CMD_CONFIG_CHANGED: i32 = 8;
pub const APP_CMD_LOW_MEMORY: i32 = 9;
pub const APP_CMD_START: i32 = 10;
pub const APP_CMD_RESUME: i32 = 11;
pub const APP_CMD_SAVE_STATE: i32 = 12;
pub const APP_CMD_PAUSE: i32 = 13;
pub const APP_CMD_STOP: i32 = 14;
pub const APP_CMD_DESTROY: i32 = 15;

// Android MotionEvent actions
pub const AMOTION_EVENT_ACTION_MASK: i32 = 0xff;
pub const AMOTION_EVENT_ACTION_DOWN: i32 = 0;
pub const AMOTION_EVENT_ACTION_UP: i32 = 1;
pub const AMOTION_EVENT_ACTION_MOVE: i32 = 2;
pub const AMOTION_EVENT_ACTION_CANCEL: i32 = 3;
pub const AMOTION_EVENT_ACTION_POINTER_DOWN: i32 = 5;
pub const AMOTION_EVENT_ACTION_POINTER_UP: i32 = 6;

// Android KeyEvent actions
pub const AKEY_EVENT_ACTION_DOWN: i32 = 0;
pub const AKEY_EVENT_ACTION_UP: i32 = 1;

// Android KeyCodes
pub const AKEYCODE_BACK: i32 = 4;
pub const AKEYCODE_VOLUME_UP: i32 = 24;
pub const AKEYCODE_VOLUME_DOWN: i32 = 25;
pub const AKEYCODE_POWER: i32 = 26;

/// Raw Android NDK `ANativeWindow_Buffer` structure.
#[repr(C)]
pub struct ANativeWindowBuffer {
    pub width: i32,
    pub height: i32,
    pub stride: i32,
    pub format: i32,
    pub bits: *mut c_void,
    pub reserved: [u32; 6],
}

/// Raw Android NDK `ARect` structure.
#[repr(C)]
pub struct ARect {
    pub left: i32,
    pub top: i32,
    pub right: i32,
    pub bottom: i32,
}

extern "C" {
    fn ANativeWindow_getWidth(window: *mut c_void) -> i32;
    fn ANativeWindow_getHeight(window: *mut c_void) -> i32;
    fn ANativeWindow_setBuffersGeometry(
        window: *mut c_void,
        width: i32,
        height: i32,
        format: i32,
    ) -> i32;
    fn ANativeWindow_lock(
        window: *mut c_void,
        out_buffer: *mut ANativeWindowBuffer,
        in_out_dirty_bounds: *mut ARect,
    ) -> i32;
    fn ANativeWindow_unlockAndPost(window: *mut c_void) -> i32;
    fn ANativeWindow_release(window: *mut c_void);
}

/// Native Android application window holding active `ANativeWindow` handle and lifecycle state.
pub struct AndroidWindow {
    native_window: *mut c_void,
    config: WindowConfig,
    scale_factor: f32,
    is_focused: bool,
    cursor: CursorIcon,
    redraw_requested: bool,
    event_queue: VecDeque<PlatformEvent>,
}

impl AndroidWindow {
    /// Creates a new Android platform window instance wrapping the given `ANativeWindow`.
    pub fn new(native_window: *mut c_void, config: WindowConfig) -> Result<Self, String> {
        let (width, height) = if !native_window.is_null() {
            unsafe {
                let w = ANativeWindow_getWidth(native_window);
                let h = ANativeWindow_getHeight(native_window);
                (w.max(1) as u32, h.max(1) as u32)
            }
        } else {
            (config.width, config.height)
        };

        let mut actual_config = config;
        actual_config.width = width;
        actual_config.height = height;

        Ok(Self {
            native_window,
            config: actual_config,
            scale_factor: 2.625, // Standard Android xxhdpi default
            is_focused: true,
            cursor: CursorIcon::Default,
            redraw_requested: true,
            event_queue: VecDeque::new(),
        })
    }

    /// Returns the raw `ANativeWindow` pointer for EGL / Vulkan surface creation.
    pub fn raw_window(&self) -> *mut c_void {
        self.native_window
    }

    /// Updates the active `ANativeWindow` pointer on `APP_CMD_INIT_WINDOW` or `APP_CMD_TERM_WINDOW`.
    pub fn set_native_window(&mut self, window: *mut c_void) {
        self.native_window = window;
        if !window.is_null() {
            unsafe {
                let w = ANativeWindow_getWidth(window) as u32;
                let h = ANativeWindow_getHeight(window) as u32;
                if w > 0 && h > 0 {
                    self.config.width = w;
                    self.config.height = h;
                    self.event_queue.push_back(PlatformEvent::WindowResized {
                        width: w,
                        height: h,
                    });
                }
            }
        }
    }

    /// Presents a 32-bit RGBA/BGRA software buffer directly to the `ANativeWindow`.
    pub fn present_bgra(&self, pixels: &[u8], width: u32, height: u32) -> Result<(), String> {
        if self.native_window.is_null() || pixels.is_empty() {
            return Ok(());
        }

        unsafe {
            // WINDOW_FORMAT_RGBA_8888 = 1
            ANativeWindow_setBuffersGeometry(self.native_window, width as i32, height as i32, 1);

            let mut buffer: ANativeWindowBuffer = std::mem::zeroed();
            let res = ANativeWindow_lock(self.native_window, &mut buffer, std::ptr::null_mut());
            if res != 0 {
                return Err(format!("ANativeWindow_lock failed with code {}", res));
            }

            if !buffer.bits.is_null() && buffer.width > 0 && buffer.height > 0 {
                let dst_stride = buffer.stride as usize * 4;
                let src_stride = width as usize * 4;
                let copy_w = src_stride.min(dst_stride);
                let copy_h = (height as usize).min(buffer.height as usize);

                let dst_ptr = buffer.bits as *mut u8;
                for row in 0..copy_h {
                    let src_row = &pixels[row * src_stride..row * src_stride + copy_w];
                    let dst_row = dst_ptr.add(row * dst_stride);
                    std::ptr::copy_nonoverlapping(src_row.as_ptr(), dst_row, copy_w);
                }
            }

            ANativeWindow_unlockAndPost(self.native_window);
        }

        Ok(())
    }

    /// Dispatches an Android lifecycle command (`APP_CMD_*`) into the window event queue.
    pub fn handle_app_cmd(&mut self, cmd: i32) {
        match cmd {
            APP_CMD_GAINED_FOCUS => {
                self.is_focused = true;
                self.event_queue
                    .push_back(PlatformEvent::WindowFocused(true));
            }
            APP_CMD_LOST_FOCUS => {
                self.is_focused = false;
                self.event_queue
                    .push_back(PlatformEvent::WindowFocused(false));
            }
            APP_CMD_WINDOW_REDRAW_NEEDED => {
                self.redraw_requested = true;
                self.event_queue.push_back(PlatformEvent::RedrawRequested);
            }
            APP_CMD_WINDOW_RESIZED | APP_CMD_CONFIG_CHANGED => {
                if !self.native_window.is_null() {
                    unsafe {
                        let w = ANativeWindow_getWidth(self.native_window) as u32;
                        let h = ANativeWindow_getHeight(self.native_window) as u32;
                        if w > 0 && h > 0 {
                            self.config.width = w;
                            self.config.height = h;
                            self.event_queue.push_back(PlatformEvent::WindowResized {
                                width: w,
                                height: h,
                            });
                        }
                    }
                }
            }
            APP_CMD_DESTROY => {
                self.event_queue
                    .push_back(PlatformEvent::WindowCloseRequested);
            }
            _ => {}
        }
    }

    /// Dispatches an Android motion touch event into the window event queue.
    pub fn handle_touch(&mut self, action: i32, x: f32, y: f32) {
        let action_masked = action & AMOTION_EVENT_ACTION_MASK;
        match action_masked {
            AMOTION_EVENT_ACTION_DOWN | AMOTION_EVENT_ACTION_POINTER_DOWN => {
                self.event_queue.push_back(PlatformEvent::MouseDown {
                    x,
                    y,
                    button: MouseButton::Left,
                });
            }
            AMOTION_EVENT_ACTION_MOVE => {
                self.event_queue
                    .push_back(PlatformEvent::MouseMove { x, y });
            }
            AMOTION_EVENT_ACTION_UP | AMOTION_EVENT_ACTION_POINTER_UP => {
                self.event_queue.push_back(PlatformEvent::MouseUp {
                    x,
                    y,
                    button: MouseButton::Left,
                });
            }
            AMOTION_EVENT_ACTION_CANCEL => {
                self.event_queue.push_back(PlatformEvent::CursorLeft);
            }
            _ => {}
        }
    }

    /// Dispatches an Android key event into the window event queue.
    pub fn handle_key(&mut self, action: i32, keycode: i32) {
        if keycode == AKEYCODE_BACK {
            if action == AKEY_EVENT_ACTION_DOWN {
                self.event_queue.push_back(PlatformEvent::KeyDown {
                    key: KeyCode::Escape,
                    modifiers: Modifiers::default(),
                });
            } else if action == AKEY_EVENT_ACTION_UP {
                self.event_queue.push_back(PlatformEvent::KeyUp {
                    key: KeyCode::Escape,
                    modifiers: Modifiers::default(),
                });
            }
        }
    }
}

impl Drop for AndroidWindow {
    fn drop(&mut self) {
        if !self.native_window.is_null() {
            unsafe {
                ANativeWindow_release(self.native_window);
            }
        }
    }
}

impl PlatformWindow for AndroidWindow {
    fn title(&self) -> &str {
        &self.config.title
    }

    fn set_title(&mut self, title: &str) {
        self.config.title = title.to_string();
    }

    fn size(&self) -> (u32, u32) {
        (self.config.width, self.config.height)
    }

    fn set_size(&mut self, width: u32, height: u32) {
        self.config.width = width;
        self.config.height = height;
        self.push_event(PlatformEvent::WindowResized { width, height });
    }

    fn scale_factor(&self) -> f32 {
        self.scale_factor
    }

    fn set_scale_factor(&mut self, scale_factor: f32) {
        self.scale_factor = scale_factor;
        self.push_event(PlatformEvent::ScaleFactorChanged { scale_factor });
    }

    fn mode(&self) -> WindowMode {
        self.config.mode
    }

    fn set_mode(&mut self, mode: WindowMode) {
        self.config.mode = mode;
    }

    fn is_focused(&self) -> bool {
        self.is_focused
    }

    fn set_focused(&mut self, focused: bool) {
        self.is_focused = focused;
        self.push_event(PlatformEvent::WindowFocused(focused));
    }

    fn cursor(&self) -> CursorIcon {
        self.cursor
    }

    fn set_cursor(&mut self, cursor: CursorIcon) {
        self.cursor = cursor;
    }

    fn request_redraw(&mut self) {
        self.redraw_requested = true;
    }

    fn is_redraw_requested(&self) -> bool {
        self.redraw_requested
    }

    fn poll_events(&mut self) -> Vec<PlatformEvent> {
        self.event_queue.drain(..).collect()
    }

    fn push_event(&mut self, event: PlatformEvent) {
        self.event_queue.push_back(event);
    }
}

impl crate::window::NativeSurfaceWindow for AndroidWindow {
    fn present_surface(
        &mut self,
        pixels: &[u8],
        width: u32,
        height: u32,
    ) -> Result<(), crate::surface::PresentError> {
        self.present_bgra(pixels, width, height)
            .map_err(crate::surface::PresentError::Custom)
    }
}

/// Native Android NDK event loop driving event dispatch and frame pacing.
pub struct AndroidEventLoop {
    pub window: AndroidWindow,
    pub control: EventLoopControl,
}

impl AndroidEventLoop {
    /// Creates a new event loop driving the given Android window.
    pub fn new(window: AndroidWindow) -> Self {
        Self {
            window,
            control: EventLoopControl::Poll,
        }
    }
}

impl EventLoop for AndroidEventLoop {
    fn run<F>(&mut self, mut handler: F)
    where
        F: FnMut(PlatformEvent, &mut EventLoopControl),
    {
        while self.control != EventLoopControl::Exit {
            let events = self.window.poll_events();
            for event in events {
                handler(event, &mut self.control);
                if self.control == EventLoopControl::Exit {
                    break;
                }
            }

            if self.window.is_redraw_requested() {
                self.window.redraw_requested = false;
                handler(PlatformEvent::RedrawRequested, &mut self.control);
            }

            match self.control {
                EventLoopControl::Poll => std::thread::yield_now(),
                EventLoopControl::Wait => std::thread::sleep(std::time::Duration::from_millis(150)),
                EventLoopControl::WaitUntil(instant) => {
                    let now = std::time::Instant::now();
                    if let Some(dur) = instant.checked_duration_since(now) {
                        std::thread::sleep(dur.min(std::time::Duration::from_millis(150)));
                    }
                }
                EventLoopControl::Exit => break,
            }
        }
    }
}