miniquad 0.4.10

Cross-platform window context and rendering library.
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
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! MacOs implementation is basically a mix between
//! sokol_app's objective C code and Makepad's (<https://github.com/makepad/makepad/blob/live/platform/src/platform/apple>)
//! platform implementation
//!
use {
    crate::{
        conf::{self, AppleGfxApi, Conf},
        event::{EventHandler, KeyCode, KeyMods, TouchPhase},
        fs,
        native::{
            apple::{
                apple_util::{self, *},
                frameworks::{self, *},
            },
            NativeDisplayData,
        },
        native_display,
    },
    std::{
        cell::RefCell,
        os::raw::c_void,
        sync::{mpsc, Arc, Mutex},
        thread::{self},
    },
};

struct MainThreadState {
    quit: bool,
    paused: bool,
    update_requested: bool,
    view: *mut Object,
    keymods: KeyMods,
    cur_msg: Message,
}

struct IosDisplay {
    view: ObjcId,
    view_ctrl: ObjcId,
    _textfield_dlg: ObjcId,
    textfield: ObjcId,
    gfx_api: conf::AppleGfxApi,

    event_handler: Option<Box<dyn EventHandler>>,
    _gles2: bool,
    f: Option<Box<dyn 'static + FnOnce() -> Box<dyn EventHandler>>>,
    state: Arc<Mutex<MainThreadState>>,
}

impl IosDisplay {
    fn show_keyboard(&mut self, show: bool) {
        unsafe {
            if show {
                msg_send_![self.textfield, becomeFirstResponder];
            } else {
                msg_send_![self.textfield, resignFirstResponder];
            }
        }
    }

    fn init_event_handler(&mut self) {
        let f = self.f.take().unwrap();

        if self.gfx_api == AppleGfxApi::OpenGl {
            crate::native::gl::load_gl_funcs(|proc| {
                let name = std::ffi::CString::new(proc).unwrap();

                unsafe { get_proc_address(name.as_ptr() as _) }
            });
        }

        self.event_handler = Some(f());
    }
}

fn get_window_payload(this: &Object) -> &mut IosDisplay {
    unsafe {
        let ptr: *mut c_void = *this.get_ivar("display_ptr");
        &mut *(ptr as *mut IosDisplay)
    }
}

#[derive(Debug, Clone, Copy)]
enum Message {
    Resize {
        width: i32,
        height: i32,
    },
    Touch {
        phase: TouchPhase,
        touch_id: u64,
        x: f32,
        y: f32,
    },
    Character {
        character: u32,
    },
    KeyDown {
        keycode: KeyCode,
    },
    KeyUp {
        keycode: KeyCode,
    },
    Pause,
    Resume,
    Destroy,
}
unsafe impl Send for Message {}

thread_local! {
    static MESSAGES_TX: RefCell<Option<mpsc::Sender<Message>>> = const { RefCell::new(None) };
}

impl MainThreadState {
    fn process_request(&mut self, request: crate::native::Request) {
        use crate::native::Request::*;

        match request {
            ScheduleUpdate => {
                self.update_requested = true;
            }
            SetImePosition { .. } => {
                // IME position control not applicable on iOS
            }
            SetImeEnabled(..) => {
                // IME enable/disable not applicable on iOS
            }
            _ => {}
        }
    }
}

fn send_message(message: Message) {
    MESSAGES_TX.with(|tx| {
        let mut tx = tx.borrow_mut();
        tx.as_mut().unwrap().send(message).unwrap();
    })
}

pub fn define_glk_or_mtk_view(superclass: &Class) -> *const Class {
    let mut decl = ClassDecl::new("QuadView", superclass).unwrap();

    fn on_touch(this: &Object, event: ObjcId, phase: TouchPhase) {
        unsafe {
            let enumerator: ObjcId = msg_send![event, allTouches];
            let size: u64 = msg_send![enumerator, count];
            let enumerator: ObjcId = msg_send![enumerator, objectEnumerator];

            for _ in 0..size {
                let ios_touch: ObjcId = msg_send![enumerator, nextObject];
                // Use the UITouch pointer as a stable ID instead of loop index
                let touch_id = ios_touch as u64;
                let mut ios_pos: NSPoint = msg_send![ios_touch, locationInView: this];

                if native_display().lock().unwrap().high_dpi {
                    let main_screen: ObjcId = msg_send![class!(UIScreen), mainScreen];
                    let scale: f64 = msg_send![main_screen, scale];

                    ios_pos.x *= scale;
                    ios_pos.y *= scale;
                } else {
                    let content_scale_factor: f64 = msg_send![this, contentScaleFactor];
                    ios_pos.x *= content_scale_factor;
                    ios_pos.y *= content_scale_factor;
                }

                send_message(Message::Touch {
                    phase,
                    touch_id,
                    x: ios_pos.x as f32,
                    y: ios_pos.y as f32,
                });
            }
        }
    }
    extern "C" fn touches_began(this: &Object, _: Sel, _: ObjcId, event: ObjcId) {
        on_touch(this, event, TouchPhase::Started);
    }

    extern "C" fn touches_moved(this: &Object, _: Sel, _: ObjcId, event: ObjcId) {
        on_touch(this, event, TouchPhase::Moved);
    }

    extern "C" fn touches_ended(this: &Object, _: Sel, _: ObjcId, event: ObjcId) {
        on_touch(this, event, TouchPhase::Ended);
    }

    extern "C" fn touches_canceled(this: &Object, _: Sel, _: ObjcId, event: ObjcId) {
        on_touch(this, event, TouchPhase::Cancelled);
    }

    extern "C" fn process_message(this: &Object, _: Sel, _: ObjcId) {
        let payload = get_window_payload(this);
        if payload.event_handler.is_none() {
            payload.init_event_handler();
        }
        let msg = {
            let state = payload.state.lock().unwrap();
            state.cur_msg
        };
        match msg {
            Message::Pause => {
                let mut state = payload.state.lock().unwrap();
                state.paused = true;
            }
            Message::Resume => {
                let mut state = payload.state.lock().unwrap();
                state.paused = false;
            }
            Message::Destroy => {
                let mut state = payload.state.lock().unwrap();
                state.quit = true;
            }
            Message::Touch {
                phase,
                touch_id,
                x,
                y,
            } => {
                if let Some(ref mut event_handler) = payload.event_handler {
                    event_handler.touch_event(phase, touch_id, x, y);
                }
            }
            Message::Character { character } => {
                if let Some(character) = char::from_u32(character) {
                    if let Some(ref mut event_handler) = payload.event_handler {
                        event_handler.char_event(character, Default::default(), false);
                    }
                }
            }
            Message::KeyDown { keycode } => {
                let mut state = payload.state.lock().unwrap();
                match keycode {
                    KeyCode::LeftShift | KeyCode::RightShift => state.keymods.shift = true,
                    KeyCode::LeftControl | KeyCode::RightControl => state.keymods.ctrl = true,
                    KeyCode::LeftAlt | KeyCode::RightAlt => state.keymods.alt = true,
                    KeyCode::LeftSuper | KeyCode::RightSuper => state.keymods.logo = true,
                    _ => {}
                }
                if let Some(ref mut event_handler) = payload.event_handler {
                    event_handler.key_down_event(keycode, state.keymods, false);
                }
            }
            Message::KeyUp { keycode } => {
                let mut state = payload.state.lock().unwrap();
                match keycode {
                    KeyCode::LeftShift | KeyCode::RightShift => state.keymods.shift = false,
                    KeyCode::LeftControl | KeyCode::RightControl => state.keymods.ctrl = false,
                    KeyCode::LeftAlt | KeyCode::RightAlt => state.keymods.alt = false,
                    KeyCode::LeftSuper | KeyCode::RightSuper => state.keymods.logo = false,
                    _ => {}
                }
                if let Some(ref mut event_handler) = payload.event_handler {
                    event_handler.key_up_event(keycode, state.keymods);
                }
            }
            Message::Resize { width, height } => {
                if let Some(ref mut event_handler) = payload.event_handler {
                    event_handler.resize_event(width as _, height as _);
                }
            }
        }
    }

    unsafe {
        decl.add_method(sel!(isOpaque), yes as extern "C" fn(&Object, Sel) -> BOOL);
        decl.add_method(
            sel!(touchesBegan: withEvent:),
            touches_began as extern "C" fn(&Object, Sel, ObjcId, ObjcId),
        );
        decl.add_method(
            sel!(touchesMoved: withEvent:),
            touches_moved as extern "C" fn(&Object, Sel, ObjcId, ObjcId),
        );
        decl.add_method(
            sel!(touchesEnded: withEvent:),
            touches_ended as extern "C" fn(&Object, Sel, ObjcId, ObjcId),
        );
        decl.add_method(
            sel!(touchesCanceled: withEvent:),
            touches_canceled as extern "C" fn(&Object, Sel, ObjcId, ObjcId),
        );
        decl.add_method(
            sel!(processMessage:),
            process_message as extern "C" fn(&Object, Sel, ObjcId),
        );
    }

    decl.add_ivar::<*mut c_void>("display_ptr");
    decl.register()
}

unsafe fn get_proc_address(name: *const u8) -> Option<unsafe extern "C" fn()> {
    mod libc {
        use std::ffi::{c_char, c_int, c_void};

        pub const RTLD_LAZY: c_int = 1;
        extern "C" {
            pub fn dlopen(filename: *const c_char, flag: c_int) -> *mut c_void;
            pub fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
        }
    }
    static mut OPENGL: *mut std::ffi::c_void = std::ptr::null_mut();

    if OPENGL.is_null() {
        OPENGL = libc::dlopen(
            b"/System/Library/Frameworks/OpenGLES.framework/OpenGLES\0".as_ptr() as _,
            libc::RTLD_LAZY,
        );
    }

    assert!(!OPENGL.is_null());

    let symbol = libc::dlsym(OPENGL, name as _);
    if symbol.is_null() {
        return None;
    }
    Some(unsafe { std::mem::transmute_copy(&symbol) })
}

pub fn define_glk_or_mtk_view_dlg(superclass: &Class) -> *const Class {
    let mut decl = ClassDecl::new("QuadViewDlg", superclass).unwrap();

    extern "C" fn draw_in_rect(this: &Object, _: Sel, _: ObjcId, _: ObjcId) {
        let payload = get_window_payload(this);
        if payload.event_handler.is_none() {
            payload.init_event_handler();
        }

        let main_screen: ObjcId = unsafe { msg_send![class!(UIScreen), mainScreen] };
        let screen_rect: NSRect = unsafe { msg_send![main_screen, bounds] };
        let high_dpi = native_display().lock().unwrap().high_dpi;

        let (screen_width, screen_height) = if high_dpi {
            let scale: f64 = unsafe { msg_send![main_screen, scale] };

            (
                (screen_rect.size.width * scale) as i32,
                (screen_rect.size.height * scale) as i32,
            )
        } else {
            let content_scale_factor: f64 = unsafe { msg_send![payload.view, contentScaleFactor] };
            (
                (screen_rect.size.width * content_scale_factor) as i32,
                (screen_rect.size.height * content_scale_factor) as i32,
            )
        };

        if native_display().lock().unwrap().screen_width != screen_width
            || native_display().lock().unwrap().screen_height != screen_height
        {
            {
                let mut d = native_display().lock().unwrap();
                d.screen_width = screen_width;
                d.screen_height = screen_height;
            }
            send_message(Message::Resize {
                width: screen_width,
                height: screen_height,
            });
        }

        if let Some(ref mut event_handler) = payload.event_handler {
            event_handler.update();
            event_handler.draw();
            let mut s = payload.state.lock().unwrap();
            s.update_requested = false;
        }
    }
    // wrapper to make sel! macros happy
    extern "C" fn draw_in_rect2(this: &Object, s: Sel, o: ObjcId) {
        draw_in_rect(this, s, o, nil);
    }

    unsafe {
        decl.add_method(
            sel!(glkView: drawInRect:),
            draw_in_rect as extern "C" fn(&Object, Sel, ObjcId, ObjcId),
        );

        decl.add_method(
            sel!(drawInMTKView:),
            draw_in_rect2 as extern "C" fn(&Object, Sel, ObjcId),
        );
    }

    decl.add_ivar::<*mut c_void>("display_ptr");
    decl.register()
}

// metal or opengl view and the objects required to collect all the window events
struct View {
    view: ObjcId,
    view_dlg: ObjcId,
    view_ctrl: ObjcId,
    // this view failed to create gles3 context, but succeeded with gles2
    _gles2: bool,
}

unsafe fn create_opengl_view(screen_rect: NSRect, _sample_count: i32, high_dpi: bool) -> View {
    let glk_view_obj: ObjcId = msg_send![define_glk_or_mtk_view(class!(GLKView)), alloc];
    let glk_view_obj: ObjcId = msg_send![glk_view_obj, initWithFrame: screen_rect];

    let glk_view_dlg_obj: ObjcId = msg_send![define_glk_or_mtk_view_dlg(class!(NSObject)), alloc];
    let glk_view_dlg_obj: ObjcId = msg_send![glk_view_dlg_obj, init];

    let eagl_context_obj: ObjcId = msg_send![class!(EAGLContext), alloc];
    let mut eagl_context_obj: ObjcId = msg_send![eagl_context_obj, initWithAPI: 3];
    let mut gles2 = false;
    if eagl_context_obj.is_null() {
        eagl_context_obj = msg_send![eagl_context_obj, initWithAPI: 2];
        gles2 = true;
    }

    msg_send_![
        glk_view_obj,
        setDrawableColorFormat: frameworks::GLKViewDrawableColorFormatRGBA8888
    ];
    msg_send_![
        glk_view_obj,
        setDrawableDepthFormat: frameworks::GLKViewDrawableDepthFormat::Format24 as i32
    ];
    msg_send_![
        glk_view_obj,
        setDrawableStencilFormat: frameworks::GLKViewDrawableStencilFormat::FormatNone as i32
    ];
    msg_send_![glk_view_obj, setContext: eagl_context_obj];

    msg_send_![glk_view_obj, setDelegate: glk_view_dlg_obj];
    msg_send_![glk_view_obj, setEnableSetNeedsDisplay: YES];
    msg_send_![glk_view_obj, setUserInteractionEnabled: YES];
    msg_send_![glk_view_obj, setMultipleTouchEnabled: YES];
    if high_dpi {
        let main_screen: ObjcId = msg_send![class!(UIScreen), mainScreen];
        let scale: f64 = msg_send![main_screen, scale];

        msg_send_![glk_view_obj, setContentScaleFactor: scale];
    } else {
        msg_send_![glk_view_obj, setContentScaleFactor: 1.0];
    }

    let view_ctrl_obj: ObjcId = msg_send![class!(UIViewController), alloc];
    let view_ctrl_obj: ObjcId = msg_send![view_ctrl_obj, init];

    msg_send_![view_ctrl_obj, setView: glk_view_obj];

    View {
        view: glk_view_obj,
        view_dlg: glk_view_dlg_obj,
        view_ctrl: view_ctrl_obj,
        _gles2: gles2,
    }
}

unsafe fn create_metal_view(screen_rect: NSRect, _sample_count: i32, _high_dpi: bool) -> View {
    let mtk_view_obj: ObjcId = msg_send![define_glk_or_mtk_view(class!(MTKView)), alloc];
    let mtk_view_obj: ObjcId = msg_send![mtk_view_obj, initWithFrame: screen_rect];

    let mtk_view_dlg_obj: ObjcId = msg_send![define_glk_or_mtk_view_dlg(class!(NSObject)), alloc];
    let mtk_view_dlg_obj: ObjcId = msg_send![mtk_view_dlg_obj, init];

    let view_ctrl_obj: ObjcId = msg_send![class!(UIViewController), alloc];
    let view_ctrl_obj: ObjcId = msg_send![view_ctrl_obj, init];

    msg_send_![view_ctrl_obj, setView: mtk_view_obj];

    msg_send_![mtk_view_obj, setEnableSetNeedsDisplay: YES];
    msg_send_![mtk_view_obj, setPaused: YES];
    msg_send_![mtk_view_obj, setPreferredFramesPerSecond:60];
    msg_send_![mtk_view_obj, setDelegate: mtk_view_dlg_obj];
    let device = MTLCreateSystemDefaultDevice();
    msg_send_![mtk_view_obj, setDevice: device];
    msg_send_![mtk_view_obj, setUserInteractionEnabled: YES];

    View {
        view: mtk_view_obj,
        view_dlg: mtk_view_dlg_obj,
        view_ctrl: view_ctrl_obj,

        _gles2: false,
    }
}

struct IosClipboard;
impl crate::native::Clipboard for IosClipboard {
    fn get(&mut self) -> Option<String> {
        None
    }
    fn set(&mut self, _data: &str) {}
}

pub fn define_app_delegate() -> *const Class {
    let superclass = class!(NSObject);
    let mut decl = ClassDecl::new("NSAppDelegate", superclass).unwrap();

    extern "C" fn did_finish_launching_with_options(
        _: &Object,
        _: Sel,
        _: ObjcId,
        _: ObjcId,
    ) -> BOOL {
        unsafe {
            let (f, conf) = RUN_ARGS.take().unwrap();

            let main_screen: ObjcId = msg_send![class!(UIScreen), mainScreen];
            let screen_rect: NSRect = msg_send![main_screen, bounds];

            let (_, _) = if conf.high_dpi {
                let scale: f64 = msg_send![main_screen, scale];

                (
                    (screen_rect.size.width * scale) as i32,
                    (screen_rect.size.height * scale) as i32,
                )
            } else {
                (
                    screen_rect.size.width as i32,
                    screen_rect.size.height as i32,
                )
            };

            let window_obj: ObjcId = msg_send![class!(UIWindow), alloc];
            let window_obj: ObjcId = msg_send![window_obj, initWithFrame: screen_rect];

            let view = match conf.platform.apple_gfx_api {
                AppleGfxApi::OpenGl => {
                    create_opengl_view(screen_rect, conf.sample_count, conf.high_dpi)
                }
                AppleGfxApi::Metal => {
                    create_metal_view(screen_rect, conf.sample_count, conf.high_dpi)
                }
            };

            let (textfield_dlg, textfield) = {
                let textfield_dlg = msg_send_![msg_send_![define_textfield_dlg(), alloc], init];
                let textfield = msg_send_![
                    msg_send_![class!(UITextField), alloc],
                    initWithFrame:NSRect::new(10.0, 10.0, 100.0, 50.0)];
                msg_send_![textfield, setAutocapitalizationType:0]; // UITextAutocapitalizationTypeNone
                msg_send_![textfield, setAutocorrectionType:1]; // UITextAutocorrectionTypeNo
                msg_send_![textfield, setSpellCheckingType:1]; // UITextSpellCheckingTypeNo
                msg_send_![textfield, setHidden: YES];
                msg_send_![textfield, setDelegate: textfield_dlg];
                // to make backspace work - with empty text there is no event on text removal
                msg_send_![textfield, setText: apple_util::str_to_nsstring("x")];
                msg_send_![view.view, addSubview: textfield];

                let notification_center = msg_send_![class!(NSNotificationCenter), defaultCenter];
                msg_send_![notification_center, addObserver:textfield_dlg
                           selector:sel!(keyboardWasShown:)
                           name:UIKeyboardDidShowNotification object:nil];
                msg_send_![notification_center, addObserver:textfield_dlg
                           selector:sel!(keyboardWillBeHidden:)
                           name:UIKeyboardWillHideNotification object:nil];
                msg_send_![notification_center, addObserver:textfield_dlg
                           selector:sel!(keyboardDidChangeFrame:)
                           name:UIKeyboardDidChangeFrameNotification object:nil];
                (textfield_dlg, textfield)
            };

            let (tx, rx) = std::sync::mpsc::channel();

            MESSAGES_TX.with(move |messages_tx| *messages_tx.borrow_mut() = Some(tx));

            let clipboard = Box::new(IosClipboard);
            let (tx, requests_rx) = std::sync::mpsc::channel();
            crate::set_display(NativeDisplayData {
                high_dpi: conf.high_dpi,
                gfx_api: conf.platform.apple_gfx_api,
                blocking_event_loop: conf.platform.blocking_event_loop,
                view: view.view,
                ..NativeDisplayData::new(conf.window_width, conf.window_height, tx, clipboard)
            });

            let state_original = Arc::new(Mutex::new(MainThreadState {
                quit: false,
                paused: true,
                update_requested: true,
                view: view.view,
                keymods: KeyMods {
                    shift: false,
                    ctrl: false,
                    alt: false,
                    logo: false,
                },
                cur_msg: Message::Resume,
            }));

            let payload = Box::new(IosDisplay {
                view: view.view,
                view_ctrl: view.view_ctrl,
                textfield,
                _textfield_dlg: textfield_dlg,
                gfx_api: conf.platform.apple_gfx_api,

                f: Some(Box::new(f)),
                event_handler: None,
                _gles2: view._gles2,
                state: state_original.clone(),
            });
            let payload_ptr = Box::into_raw(payload) as *mut std::ffi::c_void;

            (*view.view).set_ivar("display_ptr", payload_ptr);
            (*view.view_dlg).set_ivar("display_ptr", payload_ptr);
            (*textfield_dlg).set_ivar("display_ptr", payload_ptr);

            msg_send_![window_obj, addSubview: view.view];

            msg_send_![window_obj, setRootViewController: view.view_ctrl];

            msg_send_![window_obj, makeKeyAndVisible];

            struct SendHack<F>(F);
            unsafe impl<F> Send for SendHack<F> {}

            let state = SendHack(state_original.clone());
            thread::spawn(move || {
                let s = state.0;

                loop {
                    while let Ok(request) = requests_rx.try_recv() {
                        s.lock().unwrap().process_request(request);
                    }

                    let block_on_wait = {
                        let s = s.lock().unwrap();
                        (conf.platform.blocking_event_loop && !s.update_requested) || s.paused
                    };

                    if block_on_wait {
                        let res = rx.recv();

                        if let Ok(msg) = res {
                            let view;
                            {
                                let mut s = s.lock().unwrap();
                                view = s.view;
                                s.cur_msg = msg;
                            }
                            msg_send_![&*view, performSelectorOnMainThread:sel!(processMessage:) withObject:nil waitUntilDone:YES];
                        }
                    } else {
                        // process all the messages from the main thread
                        while let Ok(msg) = rx.try_recv() {
                            let view;
                            {
                                let mut s = s.lock().unwrap();
                                view = s.view;
                                s.cur_msg = msg;
                            }
                            msg_send_![&*view, performSelectorOnMainThread:sel!(processMessage:) withObject:nil waitUntilDone:YES];
                        }
                    }

                    let update_requested;
                    let view;
                    {
                        let s = s.lock().unwrap();
                        update_requested = s.update_requested;
                        view = s.view;
                    }

                    if !conf.platform.blocking_event_loop || update_requested {
                        match conf.platform.apple_gfx_api {
                            AppleGfxApi::OpenGl => {
                                // Why it differs from Metal? I don't realy know. Looks like a bug.
                                // Somehow it needs `setNeedsDisplay` to redraw after touch.
                                // With plain `display` it draws only after another touch.
                                // But when it's not blocking_event_loop it makes fps really drop with `setNeedsDisplay`.
                                // I hope it will work the same on the real device.
                                if conf.platform.blocking_event_loop {
                                    msg_send_![&*view, performSelectorOnMainThread:sel!(setNeedsDisplay) withObject:nil waitUntilDone:NO];
                                } else {
                                    msg_send_![&*view, performSelectorOnMainThread:sel!(display) withObject:nil waitUntilDone:YES];
                                }
                            }
                            AppleGfxApi::Metal => {
                                msg_send_![&*view, performSelectorOnMainThread:sel!(setNeedsDisplay) withObject:nil waitUntilDone:NO];
                            }
                        }
                    }

                    thread::yield_now();
                }
            });
        }
        YES
    }

    extern "C" fn application_did_become_active(_: &Object, _: Sel, _: ObjcId) {
        send_message(Message::Resume);
    }

    extern "C" fn application_will_resign_active(_: &Object, _: Sel, _: ObjcId) {
        send_message(Message::Pause);
    }

    unsafe {
        decl.add_method(
            sel!(application: didFinishLaunchingWithOptions:),
            did_finish_launching_with_options
                as extern "C" fn(&Object, Sel, ObjcId, ObjcId) -> BOOL,
        );
        decl.add_method(
            sel!(applicationDidBecomeActive:),
            application_did_become_active as extern "C" fn(&Object, Sel, ObjcId),
        );
        decl.add_method(
            sel!(applicationWillResignActive:),
            application_will_resign_active as extern "C" fn(&Object, Sel, ObjcId),
        );
    }
    decl.register()
}

fn define_textfield_dlg() -> *const Class {
    let superclass = class!(NSObject);
    let mut decl = ClassDecl::new("NSTexfieldDlg", superclass).unwrap();

    // those 3 callbacks are for resizing the canvas when keyboard is opened
    // which is not currenlty supported by miniquad
    extern "C" fn keyboard_was_shown(_: &Object, _: Sel, _notif: ObjcId) {}
    extern "C" fn keyboard_will_be_hidden(_: &Object, _: Sel, _notif: ObjcId) {}
    extern "C" fn keyboard_did_change_frame(_: &Object, _: Sel, _notif: ObjcId) {}

    extern "C" fn should_change_characters_in_range(
        _: &Object,
        _: Sel,
        _textfield: ObjcId,
        _range: NSRange,
        string: ObjcId,
    ) -> BOOL {
        unsafe {
            let len: u64 = msg_send![string, length];
            if len > 0 {
                for i in 0..len {
                    let c: u16 = msg_send![string, characterAtIndex: i];

                    match c {
                        c if c >= 32 && !(0xD800..=0xDFFF).contains(&c) => {
                            send_message(Message::Character {
                                character: c as u32,
                            })
                        }
                        10 => {
                            send_message(Message::KeyDown {
                                keycode: crate::event::KeyCode::Enter,
                            });
                            send_message(Message::KeyUp {
                                keycode: crate::event::KeyCode::Enter,
                            });
                        }
                        32 => {
                            send_message(Message::Character {
                                character: ' ' as u32,
                            });
                            send_message(Message::KeyDown {
                                keycode: crate::event::KeyCode::Space,
                            });
                            send_message(Message::KeyUp {
                                keycode: crate::event::KeyCode::Space,
                            });
                        }
                        _ => {}
                    }
                }
            } else {
                send_message(Message::KeyDown {
                    keycode: crate::event::KeyCode::Backspace,
                });
                send_message(Message::KeyUp {
                    keycode: crate::event::KeyCode::Backspace,
                });
            }
        }
        NO
    }

    unsafe {
        decl.add_method(
            sel!(keyboardWasShown:),
            keyboard_was_shown as extern "C" fn(&Object, Sel, ObjcId),
        );
        decl.add_method(
            sel!(keyboardWillBeHidden:),
            keyboard_will_be_hidden as extern "C" fn(&Object, Sel, ObjcId),
        );
        decl.add_method(
            sel!(keyboardDidChangeFrame:),
            keyboard_did_change_frame as extern "C" fn(&Object, Sel, ObjcId),
        );
        decl.add_method(
            sel!(textField: shouldChangeCharactersInRange: replacementString:),
            should_change_characters_in_range
                as extern "C" fn(&Object, Sel, ObjcId, NSRange, ObjcId) -> BOOL,
        );
    }
    decl.add_ivar::<*mut c_void>("display_ptr");
    decl.register()
}

pub fn log(message: &str) {
    let nsstring = apple_util::str_to_nsstring(message);
    let _: () = unsafe { frameworks::NSLog(nsstring) };
}

pub fn load_file<F: Fn(crate::fs::Response) + 'static>(path: &str, on_loaded: F) {
    let path = std::path::Path::new(&path);
    let path_without_extension = path.with_extension("");
    let path_without_extension = path_without_extension.to_str().unwrap();
    let extension = path.extension().unwrap_or_default().to_str().unwrap();

    unsafe {
        let nsstring = apple_util::str_to_nsstring(&format!(
            "loading: {} {}",
            path_without_extension, extension
        ));
        let _: () = frameworks::NSLog(nsstring);

        let main_bundle: ObjcId = msg_send![class!(NSBundle), mainBundle];
        let resource = apple_util::str_to_nsstring(path_without_extension);
        let type_ = apple_util::str_to_nsstring(extension);
        let file_path: ObjcId = msg_send![main_bundle, pathForResource:resource ofType:type_];
        if file_path.is_null() {
            on_loaded(Err(fs::Error::IOSAssetNoSuchFile));
            return;
        }
        let file_data: ObjcId = msg_send![class!(NSData), dataWithContentsOfFile: file_path];
        if file_data.is_null() {
            on_loaded(Err(fs::Error::IOSAssetNoData));
            return;
        }
        let bytes: *mut u8 = msg_send![file_data, bytes];
        if bytes.is_null() {
            on_loaded(Err(fs::Error::IOSAssetNoData));
            return;
        }
        let length: usize = msg_send![file_data, length];
        let slice = std::slice::from_raw_parts(bytes, length);
        on_loaded(Ok(slice.to_vec()))
    }
}

// this is the way to pass argument to UiApplicationMain
// this static will be used exactly once, to .take() the "run" arguments
#[allow(clippy::type_complexity)]
static mut RUN_ARGS: Option<(Box<dyn FnOnce() -> Box<dyn EventHandler>>, Conf)> = None;

pub unsafe fn run<F>(conf: Conf, f: F)
where
    F: 'static + FnOnce() -> Box<dyn EventHandler>,
{
    RUN_ARGS = Some((Box::new(f), conf));

    std::panic::set_hook(Box::new(|info| {
        let nsstring = apple_util::str_to_nsstring(&format!("{:?}", info));
        let _: () = frameworks::NSLog(nsstring);
    }));

    let argc = 1;
    let mut argv = b"Miniquad\0" as *const u8 as *mut i8;

    let class: ObjcId = msg_send!(define_app_delegate(), class);
    let class_string = frameworks::NSStringFromClass(class as _);

    UIApplicationMain(argc, &mut argv, nil, class_string);
}