stipple 0.0.2

Stipple: a cross-platform, self-drawn UI toolkit in Rust.
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
//! A native iOS backend over the raw Objective-C runtime + UIKit/CoreGraphics.
//!
//! No `objc`/`uikit` crate — direct `objc_msgSend` FFI and framework links, the
//! same approach as the macOS backend (workspace policy in `ROADMAP.md` §1).
//! This module opts into `unsafe` for the crate.
//!
//! `UIApplicationMain` boots the app with a hand-built delegate class; the
//! delegate creates a `UIWindow` hosting a custom `UIView` whose `drawRect:`
//! blits the software [`Pixmap`] as a `CGImage`. `UIApplicationMain` never
//! returns, so the Rust handler (which outlives the call) is reached through a
//! stashed raw pointer.
//!
//! **Verification:** build-checked for `aarch64-apple-ios` by the `mobile`
//! cross-compile CI job; a simulator run + screenshot is a follow-up.
#![allow(unsafe_code)]

use std::cell::RefCell;
use std::ffi::{CString, c_void};

use crate::geometry::{PhysicalSize, ScaleFactor};
use crate::platform::ControlFlow;
use crate::platform::error::PlatformError;
use crate::platform::event::{Event, WindowId};
use crate::platform::window::{Window, WindowAttributes};
use crate::render::{Pixmap, Surface};

type Id = *mut c_void;
type Sel = *mut c_void;
type Class = *mut c_void;

#[repr(C)]
#[derive(Clone, Copy)]
struct CgPoint {
    x: f64,
    y: f64,
}
#[repr(C)]
#[derive(Clone, Copy)]
struct CgSize {
    width: f64,
    height: f64,
}
#[repr(C)]
#[derive(Clone, Copy)]
struct CgRect {
    origin: CgPoint,
    size: CgSize,
}

#[link(name = "objc", kind = "dylib")]
unsafe extern "C" {
    fn objc_getClass(name: *const i8) -> Class;
    fn sel_registerName(name: *const i8) -> Sel;
    fn objc_msgSend();
    fn objc_allocateClassPair(superclass: Class, name: *const i8, extra: usize) -> Class;
    fn objc_registerClassPair(cls: Class);
    fn class_addMethod(cls: Class, name: Sel, imp: *const c_void, types: *const i8) -> bool;
}

#[link(name = "UIKit", kind = "framework")]
unsafe extern "C" {
    fn UIApplicationMain(
        argc: i32,
        argv: *mut *mut i8,
        principal_class_name: Id,
        delegate_class_name: Id,
    ) -> i32;
    fn UIGraphicsGetCurrentContext() -> *mut c_void;
}

#[link(name = "CoreGraphics", kind = "framework")]
unsafe extern "C" {
    fn CGColorSpaceCreateDeviceRGB() -> *mut c_void;
    fn CGColorSpaceRelease(space: *mut c_void);
    fn CGDataProviderCreateWithData(
        info: *mut c_void,
        data: *const c_void,
        size: usize,
        release: *const c_void,
    ) -> *mut c_void;
    fn CGDataProviderRelease(provider: *mut c_void);
    #[allow(clippy::too_many_arguments)]
    fn CGImageCreate(
        width: usize,
        height: usize,
        bits_per_component: usize,
        bits_per_pixel: usize,
        bytes_per_row: usize,
        space: *mut c_void,
        bitmap_info: u32,
        provider: *mut c_void,
        decode: *const f64,
        should_interpolate: bool,
        intent: i32,
    ) -> *mut c_void;
    fn CGImageRelease(image: *mut c_void);
    fn CGContextDrawImage(ctx: *mut c_void, rect: CgRect, image: *mut c_void);
    fn CGContextSaveGState(ctx: *mut c_void);
    fn CGContextRestoreGState(ctx: *mut c_void);
    fn CGContextTranslateCTM(ctx: *mut c_void, tx: f64, ty: f64);
    fn CGContextScaleCTM(ctx: *mut c_void, sx: f64, sy: f64);
}

// kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big — R,G,B,A in memory.
const CG_BITMAP_RGBA8: u32 = 1 | (4 << 12);

fn class(name: &str) -> Class {
    let c = CString::new(name).unwrap();
    unsafe { objc_getClass(c.as_ptr()) }
}
fn sel(name: &str) -> Sel {
    let c = CString::new(name).unwrap();
    unsafe { sel_registerName(c.as_ptr()) }
}

unsafe fn msg_id(obj: Id, s: Sel) -> Id {
    let f: unsafe extern "C" fn(Id, Sel) -> Id =
        unsafe { std::mem::transmute(objc_msgSend as *const c_void) };
    unsafe { f(obj, s) }
}
unsafe fn msg_void(obj: Id, s: Sel) {
    let f: unsafe extern "C" fn(Id, Sel) =
        unsafe { std::mem::transmute(objc_msgSend as *const c_void) };
    unsafe { f(obj, s) }
}
unsafe fn msg_void_id(obj: Id, s: Sel, a: Id) {
    let f: unsafe extern "C" fn(Id, Sel, Id) =
        unsafe { std::mem::transmute(objc_msgSend as *const c_void) };
    unsafe { f(obj, s, a) }
}
unsafe fn msg_void_bool(obj: Id, s: Sel, a: bool) {
    let f: unsafe extern "C" fn(Id, Sel, bool) =
        unsafe { std::mem::transmute(objc_msgSend as *const c_void) };
    unsafe { f(obj, s, a) }
}
unsafe fn msg_rect(obj: Id, s: Sel) -> CgRect {
    let f: unsafe extern "C" fn(Id, Sel) -> CgRect =
        unsafe { std::mem::transmute(objc_msgSend as *const c_void) };
    unsafe { f(obj, s) }
}
unsafe fn msg_alloc_init_frame(cls: Class, frame: CgRect) -> Id {
    unsafe {
        let alloc = msg_id(cls, sel("alloc"));
        let f: unsafe extern "C" fn(Id, Sel, CgRect) -> Id =
            std::mem::transmute(objc_msgSend as *const c_void);
        f(alloc, sel("initWithFrame:"), frame)
    }
}

/// `[NSString stringWithUTF8String:s]`.
fn nsstring(s: &str) -> Id {
    let c = CString::new(s).unwrap();
    unsafe {
        let f: unsafe extern "C" fn(Id, Sel, *const i8) -> Id =
            std::mem::transmute(objc_msgSend as *const c_void);
        f(class("NSString"), sel("stringWithUTF8String:"), c.as_ptr())
    }
}

// ---- Shared state -----------------------------------------------------------

type DynHandler = *mut dyn FnMut(Event, &dyn Window) -> ControlFlow;

#[derive(Default)]
struct IosCtx {
    fb: Vec<u8>, // straight RGBA8, top row first
    w: usize,
    h: usize,
    view: Id,
    window: Id,
    size: PhysicalSize,
    /// Raw pointer to the (type-erased) handler. `UIApplicationMain` never
    /// returns, so the handler in `run`'s frame lives for the process lifetime.
    handler: *mut DynHandler,
}

thread_local! {
    static CTX: RefCell<IosCtx> = RefCell::new(IosCtx::default());
}

/// `drawRect:` for our UIView subclass: blit the framebuffer as a CGImage.
extern "C" fn draw_rect(_this: Id, _cmd: Sel, _rect: CgRect) {
    CTX.with(|c| {
        let c = c.borrow();
        if c.fb.is_empty() {
            return;
        }
        unsafe {
            let cg = UIGraphicsGetCurrentContext();
            if cg.is_null() {
                return;
            }
            let space = CGColorSpaceCreateDeviceRGB();
            let provider = CGDataProviderCreateWithData(
                std::ptr::null_mut(),
                c.fb.as_ptr() as *const c_void,
                c.fb.len(),
                std::ptr::null(),
            );
            let image = CGImageCreate(
                c.w,
                c.h,
                8,
                32,
                c.w * 4,
                space,
                CG_BITMAP_RGBA8,
                provider,
                std::ptr::null(),
                false,
                0,
            );
            let rect = CgRect {
                origin: CgPoint { x: 0.0, y: 0.0 },
                size: CgSize {
                    width: c.w as f64,
                    height: c.h as f64,
                },
            };
            // The UIView draw context draws CGImages bottom-up; flip the CTM so
            // our top-row-first framebuffer lands upright (as in the macОS view).
            CGContextSaveGState(cg);
            CGContextTranslateCTM(cg, 0.0, c.h as f64);
            CGContextScaleCTM(cg, 1.0, -1.0);
            CGContextDrawImage(cg, rect, image);
            CGContextRestoreGState(cg);
            CGImageRelease(image);
            CGDataProviderRelease(provider);
            CGColorSpaceRelease(space);
        }
    });
}

/// `application:didFinishLaunchingWithOptions:` — build the window + view, draw
/// the first frame, and show it.
extern "C" fn did_finish_launching(_this: Id, _cmd: Sel, _app: Id, _opts: Id) -> bool {
    unsafe {
        let bounds = msg_rect(msg_id(class("UIScreen"), sel("mainScreen")), sel("bounds"));
        let window = msg_alloc_init_frame(class("UIWindow"), bounds);
        let view = msg_alloc_init_frame(register_view_class(), bounds);
        let vc = msg_id(msg_id(class("UIViewController"), sel("alloc")), sel("init"));
        msg_void_id(vc, sel("setView:"), view);
        msg_void_id(window, sel("setRootViewController:"), vc);

        CTX.with(|c| {
            let mut c = c.borrow_mut();
            c.view = view;
            c.window = window;
        });

        // Draw the first frame through the Stipple handler, then show the window.
        let win = IosWindow {
            size: CTX.with(|c| c.borrow().size),
        };
        call_handler(Event::RedrawRequested, &win);
        msg_void(window, sel("makeKeyAndVisible"));
        msg_void_bool(view, sel("setNeedsDisplay"), true);

        // Runtime marker for CI: confirms the UIKit backend booted, built the
        // window, and the handler rendered a frame into the shared framebuffer.
        // Written to the app's tmp dir (which CI reads back from the simulator
        // container) since app stdout isn't reliably captured headlessly.
        let (w, h) = CTX.with(|c| {
            let c = c.borrow();
            (c.w, c.h)
        });
        let marker = format!("Stipple iOS: window shown, framebuffer {w}x{h}");
        println!("{marker}");
        let _ = std::fs::write(std::env::temp_dir().join("stipple-ios.txt"), &marker);
    }
    true
}

/// Invoke the stashed Rust handler (if any).
fn call_handler(event: Event, win: &dyn Window) {
    let ptr = CTX.with(|c| c.borrow().handler);
    if ptr.is_null() {
        return;
    }
    unsafe {
        let fat = *ptr;
        let handler: &mut dyn FnMut(Event, &dyn Window) -> ControlFlow = &mut *fat;
        let _ = handler(event, win);
    }
}

fn register_view_class() -> Class {
    let name = c"StippleUIView";
    unsafe {
        let existing = objc_getClass(name.as_ptr());
        if !existing.is_null() {
            return existing;
        }
        let cls = objc_allocateClassPair(class("UIView"), name.as_ptr(), 0);
        let draw_types = c"v@:{CGRect={CGPoint=dd}{CGSize=dd}}";
        class_addMethod(
            cls,
            sel("drawRect:"),
            draw_rect as *const c_void,
            draw_types.as_ptr(),
        );
        objc_registerClassPair(cls);
        cls
    }
}

fn register_app_delegate() -> Class {
    let name = c"StippleAppDelegate";
    unsafe {
        let existing = objc_getClass(name.as_ptr());
        if !existing.is_null() {
            return existing;
        }
        let cls = objc_allocateClassPair(class("UIResponder"), name.as_ptr(), 0);
        class_addMethod(
            cls,
            sel("application:didFinishLaunchingWithOptions:"),
            did_finish_launching as *const c_void,
            c"c@:@@".as_ptr(),
        );
        objc_registerClassPair(cls);
        cls
    }
}

#[derive(Debug)]
struct IosWindow {
    size: PhysicalSize,
}

impl Window for IosWindow {
    fn id(&self) -> WindowId {
        WindowId(1)
    }
    fn inner_size(&self) -> PhysicalSize {
        self.size
    }
    fn scale_factor(&self) -> ScaleFactor {
        ScaleFactor::IDENTITY
    }
    fn request_redraw(&self) {
        CTX.with(|c| {
            let view = c.borrow().view;
            if !view.is_null() {
                unsafe { msg_void_bool(view, sel("setNeedsDisplay"), true) };
            }
        });
    }
    fn set_title(&self, _title: &str) {}
    fn create_surface(&self) -> Box<dyn Surface> {
        Box::new(IosSurface { size: self.size })
    }
}

#[derive(Debug)]
struct IosSurface {
    size: PhysicalSize,
}

impl Surface for IosSurface {
    fn resize(&mut self, size: PhysicalSize) {
        self.size = size;
    }
    fn size(&self) -> PhysicalSize {
        self.size
    }
    fn present(&mut self, pixmap: &Pixmap, _damage: &[crate::geometry::Rect]) {
        let size = pixmap.size();
        CTX.with(|c| {
            let mut c = c.borrow_mut();
            c.fb = pixmap.as_bytes().to_vec();
            c.w = size.width as usize;
            c.h = size.height as usize;
            if !c.view.is_null() {
                unsafe { msg_void_bool(c.view, sel("setNeedsDisplay"), true) };
            }
        });
    }
}

/// Boot the iOS app: stash the handler, register our classes, and hand control
/// to `UIApplicationMain` (which does not return).
pub fn run<H>(attrs: WindowAttributes, mut handler: H) -> Result<(), PlatformError>
where
    H: FnMut(Event, &dyn Window) -> ControlFlow,
{
    let size = ScaleFactor::IDENTITY.to_physical(attrs.logical_size);

    // Type-erase the handler and stash a raw pointer. `H` is not `'static`, but
    // `UIApplicationMain` never returns, so `handler` lives for the process
    // lifetime — we launder the borrow lifetime to the `'static` raw pointer the
    // stash holds, which is therefore sound in practice.
    let dyn_ref: &mut (dyn FnMut(Event, &dyn Window) -> ControlFlow + '_) = &mut handler;
    let fat: DynHandler = unsafe { std::mem::transmute(dyn_ref) };
    let boxed: *mut DynHandler = Box::into_raw(Box::new(fat));

    CTX.with(|c| {
        let mut c = c.borrow_mut();
        c.size = size;
        c.handler = boxed;
    });

    register_view_class();
    register_app_delegate();

    unsafe {
        let delegate_name = nsstring("StippleAppDelegate");
        // UIApplicationMain owns argv for the process lifetime; pass none.
        UIApplicationMain(0, std::ptr::null_mut(), std::ptr::null_mut(), delegate_name);
    }
    Ok(())
}