nadir-platform 0.4.0

OS layer of the Nadir engine: windows, the event loop, input devices, GL surfaces, audio output and paths.
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
// Copyright 2026 Joseph Anthony Abbott III
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The window and its events, on AppKit.
//!
//! Three classes of ours, built at run time, connect AppKit to the backend:
//!
//! - a content view, which takes the keyboard and swallows key events so
//!   that AppKit does not beep at them, and whose coordinates are flipped,
//!   so that its origin is the top left;
//! - a window delegate, which hears about closing, resizing, the scale
//!   factor, the focus and fullscreen;
//! - an app delegate, which turns Quit (⌘Q, or the Dock) into a request to
//!   close.
//!
//! Each has a variable that points to the [`State`] it shares with the
//! backend. The backend reads key and mouse events itself, in the pump,
//! before it hands each to AppKit: AppKit does not pass the release of a key
//! to the view while ⌘ is held.
//!
//! While the user resizes the window, AppKit runs a loop of its own inside
//! `sendEvent:`, and the pump does not return. Each resize step then runs
//! the modal frame callback, so frames come only while the window changes
//! size. Moving a window does not stop the loop.
//!
//! Typed text is a key press's `characters`, read by the pump too.
//!
//! What this backend does not do yet, because nothing has needed it:
//!
//! - It has no input method: a text being composed is not shown, and
//!   characters that only an input method types (in Chinese or Japanese,
//!   say) are not typed. That needs `NSTextInputClient`.
//! - It does not notice the window being minimized.
//! - It does not put back the two keys that ISO keyboards swap (see the key
//!   table).

use core::any::Any;
use core::cell::{Cell, RefCell};
use core::ffi::{CStr, c_char};
use core::ptr;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicBool, Ordering};

use nadir_core::log::{debug, error};
use nadir_core::{UVec2, Vec2};
// The event types are used as `appkit::NSEventTypeKeyDown`: see `input`.
use nadir_sys::macos::appkit::{
    self, NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSEventMaskAny,
    NSTerminateCancel, NSWindowCollectionBehaviorFullScreenPrimary, NSWindowStyleMaskClosable,
    NSWindowStyleMaskMiniaturizable, NSWindowStyleMaskResizable, NSWindowStyleMaskTitled,
};
use nadir_sys::macos::foundation::{
    CGFloat, NSDefaultRunLoopMode, NSInteger, NSPoint, NSRect, NSSize, NSUInteger,
};
use nadir_sys::macos::objc::{BOOL, Id, Imp, NO, Sel, YES};
use nadir_sys::msg_send;

use super::input::{self, Incoming, Translator};
use super::runtime::{
    self, AutoreleasePool, Method, Owned, RETURNS_BOOL, as_id, class, ns_string, sel,
};
use crate::no_gl::NO_GL;
use crate::{Event, GpuSurface, ModalFrame, PlatformError, WindowConfig};

/// The modal frame callback, as the platform takes it.
type ModalFrameCallback = Box<dyn FnMut(ModalFrame)>;

/// Whether the application has finished launching, which it does once.
static LAUNCHED: AtomicBool = AtomicBool::new(false);

/// Declares the selectors we send, looked up once.
macro_rules! selectors {
    ($($field:ident = $name:literal,)*) => {
        /// The selectors the backend sends.
        #[derive(Clone, Copy)]
        struct Selectors {
            $($field: Sel,)*
        }

        impl Selectors {
            fn new() -> Self {
                Self {
                    $($field: sel($name),)*
                }
            }
        }
    };
}

selectors! {
    alloc = c"alloc",
    init = c"init",
    shared_application = c"sharedApplication",
    set_activation_policy = c"setActivationPolicy:",
    activate_ignoring_other_apps = c"activateIgnoringOtherApps:",
    finish_launching = c"finishLaunching",
    set_main_menu = c"setMainMenu:",
    set_delegate = c"setDelegate:",
    delegate = c"delegate",
    next_event = c"nextEventMatchingMask:untilDate:inMode:dequeue:",
    send_event = c"sendEvent:",
    distant_past = c"distantPast",
    add_item = c"addItem:",
    set_submenu = c"setSubmenu:",
    init_menu_item = c"initWithTitle:action:keyEquivalent:",
    terminate = c"terminate:",
    init_window = c"initWithContentRect:styleMask:backing:defer:",
    init_with_frame = c"initWithFrame:",
    set_released_when_closed = c"setReleasedWhenClosed:",
    set_title = c"setTitle:",
    set_content_view = c"setContentView:",
    make_first_responder = c"makeFirstResponder:",
    set_accepts_mouse_moved_events = c"setAcceptsMouseMovedEvents:",
    set_content_min_size = c"setContentMinSize:",
    set_collection_behavior = c"setCollectionBehavior:",
    set_background_color = c"setBackgroundColor:",
    black_color = c"blackColor",
    center = c"center",
    make_key_and_order_front = c"makeKeyAndOrderFront:",
    toggle_full_screen = c"toggleFullScreen:",
    backing_scale_factor = c"backingScaleFactor",
    close = c"close",
    bounds = c"bounds",
    convert_point = c"convertPoint:fromView:",
    in_live_resize = c"inLiveResize",
    event_type = c"type",
    key_code = c"keyCode",
    characters = c"characters",
    utf8_string = c"UTF8String",
    modifier_flags = c"modifierFlags",
    location_in_window = c"locationInWindow",
    button_number = c"buttonNumber",
    scrolling_delta_x = c"scrollingDeltaX",
    scrolling_delta_y = c"scrollingDeltaY",
    has_precise_scrolling_deltas = c"hasPreciseScrollingDeltas",
    is_direction_inverted_from_device = c"isDirectionInvertedFromDevice",
    window = c"window",
    hide = c"hide",
    unhide = c"unhide",
}

/// What the backend and the objects of our classes share.
struct State {
    selectors: Selectors,
    /// The window and its content view. The backend owns both.
    window: Id,
    view: Id,
    translator: RefCell<Translator>,
    /// The events since the last pump.
    events: RefCell<Vec<Event>>,
    fullscreen: Cell<bool>,
    modal_frame: Cell<Option<ModalFrameCallback>>,
    /// A panic caught in a method of ours, which must not unwind into
    /// AppKit. The pump resumes it.
    panic: Cell<Option<Box<dyn Any + Send>>>,
}

impl State {
    /// The window's scale factor.
    fn backing_scale(&self) -> f32 {
        // SAFETY: `-[NSWindow backingScaleFactor]` takes nothing and returns
        // a `CGFloat`.
        let scale =
            unsafe { msg_send![self.window, self.selectors.backing_scale_factor, () -> CGFloat] };
        scale as f32
    }

    /// The content view's bounds, in points.
    fn bounds(&self) -> NSRect {
        // SAFETY: `-[NSView bounds]` takes nothing and returns an `NSRect`.
        unsafe { msg_send![self.view, self.selectors.bounds, () -> NSRect] }
    }

    /// The size of the content view, in physical pixels.
    fn pixel_size(&self) -> UVec2 {
        let size = self.bounds().size;
        let scale = CGFloat::from(self.backing_scale());
        // The casts saturate, and a NaN gives 0.
        UVec2::new(
            (size.width * scale).round() as u32,
            (size.height * scale).round() as u32,
        )
    }

    /// Adds the events of `f` to those of the next pump.
    fn record(&self, f: impl FnOnce(&mut Translator, &mut Vec<Event>)) {
        f(
            &mut self.translator.borrow_mut(),
            &mut self.events.borrow_mut(),
        );
    }

    fn resized(&self) {
        let size = self.pixel_size();
        self.record(|translator, events| translator.resize(size, events));
        // SAFETY: `-[NSView inLiveResize]` takes nothing and returns a
        // `BOOL`.
        let live = unsafe { msg_send![self.view, self.selectors.in_live_resize, () -> BOOL] };
        if live != NO {
            self.run_modal_frame();
        }
    }

    fn rescaled(&self) {
        let scale = self.backing_scale();
        let size = self.pixel_size();
        self.record(|translator, events| {
            translator.set_scale(scale, events);
            translator.resize(size, events);
        });
    }

    /// Runs the modal frame callback, if there is one.
    fn run_modal_frame(&self) {
        let frame = {
            let translator = self.translator.borrow();
            ModalFrame {
                size: translator.size(),
                scale_factor: translator.scale(),
            }
        };
        // The callback is taken out while it runs. If it panics, it is
        // dropped, and no more modal frames run.
        let Some(mut callback) = self.modal_frame.take() else {
            return;
        };
        callback(frame);
        let replaced = self.modal_frame.take();
        self.modal_frame.set(replaced.or(Some(callback)));
    }
}

/// Runs `f` with the state of `this`, an object of one of our classes, if
/// it has one, and keeps any panic for the pump.
///
/// # Safety
///
/// `this` is an object of a class from [`runtime::build_class`] whose state
/// variable is null or points at a live [`State`].
unsafe fn with_state<R>(this: Id, otherwise: R, f: impl FnOnce(&State) -> R) -> R {
    // SAFETY: the caller promises what `runtime::state` needs.
    let Some(state) = (unsafe { runtime::state::<State>(this) }) else {
        return otherwise;
    };
    match panic::catch_unwind(AssertUnwindSafe(|| f(state))) {
        Ok(result) => result,
        Err(payload) => {
            let first = state.panic.take().unwrap_or(payload);
            state.panic.set(Some(first));
            otherwise
        }
    }
}

// -- the content view -------------------------------------------------------

/// `-[NSResponder acceptsFirstResponder]`: the view takes the keyboard.
unsafe extern "C" fn accepts_first_responder(_this: Id, _cmd: Sel) -> BOOL {
    YES
}

/// `-[NSView isFlipped]`: the view's origin is its top left.
unsafe extern "C" fn is_flipped(_this: Id, _cmd: Sel) -> BOOL {
    YES
}

/// `keyDown:`, `keyUp:` and `flagsChanged:`. The pump has read the event
/// already; this keeps AppKit from beeping at a key nobody handled.
unsafe extern "C" fn ignore_event(_this: Id, _cmd: Sel, _event: Id) {}

// -- the window delegate ----------------------------------------------------

/// `-windowShouldClose:`: asks the game, and leaves the window open.
unsafe extern "C" fn window_should_close(this: Id, _cmd: Sel, _sender: Id) -> BOOL {
    // SAFETY: AppKit calls this for our window delegate, whose state the
    // backend keeps alive while it is set.
    unsafe {
        with_state(this, NO, |state| {
            state.record(Translator::close);
            NO
        })
    }
}

/// `-windowDidResize:`.
unsafe extern "C" fn window_did_resize(this: Id, _cmd: Sel, _notification: Id) {
    // SAFETY: as in `window_should_close`.
    unsafe { with_state(this, (), State::resized) };
}

/// `-windowDidChangeBackingProperties:`: the scale factor may have changed.
unsafe extern "C" fn window_did_change_backing_properties(this: Id, _cmd: Sel, _notification: Id) {
    // SAFETY: as in `window_should_close`.
    unsafe { with_state(this, (), State::rescaled) };
}

/// `-windowDidBecomeKey:`: the window has the keyboard focus.
unsafe extern "C" fn window_did_become_key(this: Id, _cmd: Sel, _notification: Id) {
    // SAFETY: as in `window_should_close`.
    unsafe {
        with_state(this, (), |state| {
            state.record(|translator, events| translator.focus(true, events));
        });
    };
}

/// `-windowDidResignKey:`: the window lost the keyboard focus.
unsafe extern "C" fn window_did_resign_key(this: Id, _cmd: Sel, _notification: Id) {
    // SAFETY: as in `window_should_close`.
    unsafe {
        with_state(this, (), |state| {
            state.record(|translator, events| translator.focus(false, events));
        });
    };
}

/// `-windowDidEnterFullScreen:`.
unsafe extern "C" fn window_did_enter_full_screen(this: Id, _cmd: Sel, _notification: Id) {
    // SAFETY: as in `window_should_close`.
    unsafe { with_state(this, (), |state| state.fullscreen.set(true)) };
}

/// `-windowDidExitFullScreen:`.
unsafe extern "C" fn window_did_exit_full_screen(this: Id, _cmd: Sel, _notification: Id) {
    // SAFETY: as in `window_should_close`.
    unsafe { with_state(this, (), |state| state.fullscreen.set(false)) };
}

// -- the app delegate -------------------------------------------------------

/// `-applicationShouldTerminate:`: asks the game, and keeps the app running.
unsafe extern "C" fn application_should_terminate(this: Id, _cmd: Sel, _sender: Id) -> NSUInteger {
    // SAFETY: as in `window_should_close`, for our app delegate.
    unsafe {
        with_state(this, NSTerminateCancel, |state| {
            state.record(Translator::close);
            NSTerminateCancel
        })
    }
}

/// Casts a method's function to the untyped `IMP` that the runtime stores.
macro_rules! imp {
    ($function:expr, $type:ty) => {{
        let function: $type = $function;
        // SAFETY: all function pointers have the same size and
        // representation, and the runtime calls the method through the type
        // its encoding gives, which is this one.
        unsafe { core::mem::transmute::<$type, Imp>(function) }
    }};
}

/// Our three classes.
struct Classes {
    view: Id,
    window_delegate: Id,
    app_delegate: Id,
}

impl Classes {
    /// Returns our classes, and makes them the first time.
    fn get() -> Result<Self, PlatformError> {
        type Getter = unsafe extern "C" fn(Id, Sel) -> BOOL;
        type Handler = unsafe extern "C" fn(Id, Sel, Id);
        let event = |selector: &'static core::ffi::CStr| Method {
            selector,
            imp: imp!(ignore_event, Handler),
            result: "v",
            arguments: "@",
        };
        let notification = |selector, function: Handler| Method {
            selector,
            imp: imp!(function, Handler),
            result: "v",
            arguments: "@",
        };
        let view_methods = [
            Method {
                selector: c"acceptsFirstResponder",
                imp: imp!(accepts_first_responder, Getter),
                result: RETURNS_BOOL,
                arguments: "",
            },
            Method {
                selector: c"isFlipped",
                imp: imp!(is_flipped, Getter),
                result: RETURNS_BOOL,
                arguments: "",
            },
            event(c"keyDown:"),
            event(c"keyUp:"),
            event(c"flagsChanged:"),
        ];
        let window_delegate_methods = [
            Method {
                selector: c"windowShouldClose:",
                imp: imp!(
                    window_should_close,
                    unsafe extern "C" fn(Id, Sel, Id) -> BOOL
                ),
                result: RETURNS_BOOL,
                arguments: "@",
            },
            notification(c"windowDidResize:", window_did_resize),
            notification(
                c"windowDidChangeBackingProperties:",
                window_did_change_backing_properties,
            ),
            notification(c"windowDidBecomeKey:", window_did_become_key),
            notification(c"windowDidResignKey:", window_did_resign_key),
            notification(c"windowDidEnterFullScreen:", window_did_enter_full_screen),
            notification(c"windowDidExitFullScreen:", window_did_exit_full_screen),
        ];
        let app_delegate_methods = [Method {
            selector: c"applicationShouldTerminate:",
            imp: imp!(
                application_should_terminate,
                unsafe extern "C" fn(Id, Sel, Id) -> NSUInteger
            ),
            // `NSUInteger` is an `unsigned long`, 64 bits.
            result: "Q",
            arguments: "@",
        }];
        // In each class, each function has the signature its encoding
        // describes, and is sound for any instance: an instance without a
        // state does nothing.
        // SAFETY: as above.
        let view = unsafe { runtime::build_class(c"NadirView", c"NSView", &view_methods)? };
        // SAFETY: as above.
        let window_delegate = unsafe {
            runtime::build_class(
                c"NadirWindowDelegate",
                c"NSObject",
                &window_delegate_methods,
            )?
        };
        // SAFETY: as above.
        let app_delegate = unsafe {
            runtime::build_class(c"NadirAppDelegate", c"NSObject", &app_delegate_methods)?
        };
        Ok(Self {
            view: as_id(view),
            window_delegate: as_id(window_delegate),
            app_delegate: as_id(app_delegate),
        })
    }
}

/// The AppKit window of the game.
pub(crate) struct Backend {
    selectors: Selectors,
    /// The shared application. It is not ours to release.
    app: Id,
    window: Owned,
    view: Owned,
    window_delegate: Owned,
    app_delegate: Owned,
    cursor_visible: bool,
    /// Dropped after the objects stop pointing at it: see `Drop`.
    state: Box<State>,
}

impl Backend {
    pub(crate) fn new(config: &WindowConfig) -> Result<Self, PlatformError> {
        let _pool = AutoreleasePool::new();
        if !runtime::is_main_thread() {
            return Err(PlatformError::NotMainThread);
        }
        let sel = Selectors::new();
        let classes = Classes::get()?;

        // SAFETY: `+[NSApplication sharedApplication]` takes nothing and
        // returns the application, making it the first time.
        let app =
            unsafe { msg_send![as_id(class(c"NSApplication")?), sel.shared_application, () -> Id] };
        // SAFETY: `-setActivationPolicy:(NSApplicationActivationPolicy)`
        // returns a `BOOL`. A program started from a terminal is no app
        // until this.
        unsafe {
            msg_send![
                app,
                sel.set_activation_policy,
                (NSApplicationActivationPolicyRegular => NSInteger) -> BOOL
            ]
        };
        if !LAUNCHED.swap(true, Ordering::Relaxed) {
            set_main_menu(app, sel, &config.title)?;
            // SAFETY: `-finishLaunching` takes nothing and returns nothing.
            unsafe { msg_send![app, sel.finish_launching, () -> ()] };
        }

        let content = NSRect {
            origin: NSPoint::default(),
            size: NSSize {
                width: CGFloat::from(config.size.x.max(1)),
                height: CGFloat::from(config.size.y.max(1)),
            },
        };
        let mut style =
            NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
        if config.resizable {
            style |= NSWindowStyleMaskResizable;
        }
        // SAFETY: `+alloc` takes nothing and returns a new, uninitialized
        // object.
        let blank = unsafe { msg_send![as_id(class(c"NSWindow")?), sel.alloc, () -> Id] };
        // SAFETY: `-[NSWindow initWithContentRect:(NSRect)
        // styleMask:(NSWindowStyleMask)style backing:(NSBackingStoreType)
        // defer:(BOOL)]` returns the window, holding a reference for us.
        let window = unsafe {
            msg_send![
                blank,
                sel.init_window,
                (content => NSRect, style => NSUInteger, NSBackingStoreBuffered => NSUInteger, NO => BOOL) -> Id
            ]
        };
        // SAFETY: the window is new, and its reference is ours.
        let window = unsafe { Owned::new(window) };
        if window.id().is_null() {
            return Err(PlatformError::CallFailed {
                call: "-[NSWindow initWithContentRect:styleMask:backing:defer:]",
            });
        }
        let view = new_object(classes.view, sel, Some(content))?;
        let window_delegate = new_object(classes.window_delegate, sel, None)?;
        let app_delegate = new_object(classes.app_delegate, sel, None)?;

        let mut backend = Self {
            selectors: sel,
            app,
            state: Box::new(State {
                selectors: sel,
                window: window.id(),
                view: view.id(),
                translator: RefCell::new(Translator::new(config.size, 1.0)),
                events: RefCell::new(Vec::new()),
                fullscreen: Cell::new(false),
                modal_frame: Cell::new(None),
                panic: Cell::new(None),
            }),
            window,
            view,
            window_delegate,
            app_delegate,
            cursor_visible: true,
        };
        // From here on, dropping `backend` detaches the objects from the
        // state, and closes the window.
        let state: *const State = &raw const *backend.state;
        for object in [
            backend.view.id(),
            backend.window_delegate.id(),
            backend.app_delegate.id(),
        ] {
            // SAFETY: the objects are of our classes, and the boxed state
            // outlives the time they point at it: `Drop` detaches them first.
            unsafe { runtime::set_state(object, state) };
        }
        let window = backend.window.id();
        // The window does not keep its delegate, or the app its own: the
        // backend does, as long as they are set.
        // SAFETY: `-setDelegate:(id)` returns nothing; ours answers the
        // delegate methods it has, and the others are optional.
        unsafe { msg_send![window, sel.set_delegate, (backend.window_delegate.id() => Id) -> ()] };
        // SAFETY: as above, for the app.
        unsafe { msg_send![app, sel.set_delegate, (backend.app_delegate.id() => Id) -> ()] };
        backend.configure_window(config)?;

        // The window is on its screen now, at its scale. What the delegate
        // heard while it was set up is old news.
        let scale = backend.state.backing_scale();
        let size = backend.state.pixel_size();
        *backend.state.translator.borrow_mut() = Translator::new(size, scale);
        backend.state.events.borrow_mut().clear();
        // Showing the window makes it key, before any pump; the translator
        // is ready for the news.
        // SAFETY: `-makeKeyAndOrderFront:(id)sender` returns nothing.
        unsafe { msg_send![window, sel.make_key_and_order_front, (ptr::null_mut() => Id) -> ()] };
        // SAFETY: `-activateIgnoringOtherApps:(BOOL)` returns nothing. It
        // brings the game in front of the terminal that started it.
        unsafe { msg_send![app, sel.activate_ignoring_other_apps, (YES => BOOL) -> ()] };
        if config.fullscreen {
            backend.set_fullscreen(true);
        }
        debug!(
            "opened an AppKit window of {}x{} pixels at scale {scale}",
            size.x, size.y
        );
        Ok(backend)
    }

    /// Sets what the window looks like, and how it behaves, before it is
    /// shown.
    fn configure_window(&self, config: &WindowConfig) -> Result<(), PlatformError> {
        let (window, sel) = (self.window.id(), self.selectors);
        let title = ns_string(&config.title)?;
        // SAFETY: `-setReleasedWhenClosed:(BOOL)` returns nothing. The
        // backend releases the window itself.
        unsafe { msg_send![window, sel.set_released_when_closed, (NO => BOOL) -> ()] };
        // SAFETY: `-setTitle:(NSString *)` copies the string and returns
        // nothing.
        unsafe { msg_send![window, sel.set_title, (title.id() => Id) -> ()] };
        // SAFETY: `-setContentView:(NSView *)` keeps the view and returns
        // nothing.
        unsafe { msg_send![window, sel.set_content_view, (self.view.id() => Id) -> ()] };
        // SAFETY: `-makeFirstResponder:(NSResponder *)` returns a `BOOL`.
        unsafe { msg_send![window, sel.make_first_responder, (self.view.id() => Id) -> BOOL] };
        // SAFETY: `-setAcceptsMouseMovedEvents:(BOOL)` returns nothing.
        unsafe { msg_send![window, sel.set_accepts_mouse_moved_events, (YES => BOOL) -> ()] };
        let min = NSSize {
            width: CGFloat::from(config.min_size.x),
            height: CGFloat::from(config.min_size.y),
        };
        // SAFETY: `-setContentMinSize:(NSSize)` returns nothing. A zero is
        // no limit in that direction.
        unsafe { msg_send![window, sel.set_content_min_size, (min => NSSize) -> ()] };
        // A window that may not be resized may still go fullscreen.
        // SAFETY: `-setCollectionBehavior:(NSWindowCollectionBehavior)`
        // returns nothing.
        unsafe {
            msg_send![
                window,
                sel.set_collection_behavior,
                (NSWindowCollectionBehaviorFullScreenPrimary => NSUInteger) -> ()
            ]
        };
        // SAFETY: `+[NSColor blackColor]` takes nothing and returns a color
        // that we do not own.
        let black = unsafe { msg_send![as_id(class(c"NSColor")?), sel.black_color, () -> Id] };
        // SAFETY: `-setBackgroundColor:(NSColor *)` keeps the color and
        // returns nothing.
        unsafe { msg_send![window, sel.set_background_color, (black => Id) -> ()] };
        // SAFETY: `-center` takes nothing and returns nothing.
        unsafe { msg_send![window, sel.center, () -> ()] };
        Ok(())
    }

    pub(crate) fn pump_events(&mut self, out: &mut Vec<Event>) {
        let _pool = AutoreleasePool::new();
        let sel = self.selectors;
        if let Ok(date) = class(c"NSDate") {
            self.dispatch(date, sel);
        }
        self.state.translator.borrow_mut().begin_pump(out);
        out.append(&mut self.state.events.borrow_mut());
    }

    /// Takes every waiting event off the queue, reads it, and hands it to
    /// AppKit.
    fn dispatch(&mut self, date: nadir_sys::macos::objc::Class, sel: Selectors) {
        // SAFETY: `+[NSDate distantPast]` takes nothing and returns a date
        // that the pool owns.
        let past = unsafe { msg_send![as_id(date), sel.distant_past, () -> Id] };
        // SAFETY: Foundation defines the constant, and nothing changes it.
        let mode = unsafe { NSDefaultRunLoopMode };
        loop {
            // SAFETY: `-[NSApplication nextEventMatchingMask:(NSEventMask)
            // untilDate:(NSDate *) inMode:(NSRunLoopMode)
            // dequeue:(BOOL)]` returns an event that the pool owns, or nil.
            // A date in the past makes it wait for nothing.
            let event = unsafe {
                msg_send![
                    self.app,
                    sel.next_event,
                    (NSEventMaskAny => u64, past => Id, mode => Id, YES => BOOL) -> Id
                ]
            };
            if event.is_null() {
                break;
            }
            if let Some(incoming) = self.decode(event) {
                self.state
                    .record(|translator, events| translator.handle(incoming, events));
                // What the key typed comes after it.
                if let Incoming::Key { pressed: true, .. } = incoming {
                    self.record_text(event);
                }
            }
            // SAFETY: `-sendEvent:(NSEvent *)` returns nothing. It is where
            // AppKit runs its own loop while the window is resized.
            unsafe { msg_send![self.app, sel.send_event, (event => Id) -> ()] };
            self.resume_panic();
        }
    }

    /// Reads a key or mouse event for our window. `None` for any other.
    fn decode(&self, event: Id) -> Option<Incoming> {
        let sel = self.selectors;
        // SAFETY: `-[NSEvent window]` takes nothing and returns the window
        // the event is for, or nil.
        let window = unsafe { msg_send![event, sel.window, () -> Id] };
        if window != self.window.id() {
            return None;
        }
        // SAFETY: `-[NSEvent type]` takes nothing and returns an
        // `NSEventType`, which is an `NSUInteger`.
        let event_type = unsafe { msg_send![event, sel.event_type, () -> NSUInteger] };
        match event_type {
            appkit::NSEventTypeKeyDown | appkit::NSEventTypeKeyUp => {
                // SAFETY: a key event's `-keyCode` returns an `unsigned
                // short`.
                let code = unsafe { msg_send![event, sel.key_code, () -> u16] };
                Some(Incoming::Key {
                    pressed: event_type == appkit::NSEventTypeKeyDown,
                    code,
                })
            }
            appkit::NSEventTypeFlagsChanged => {
                // SAFETY: as above, for a flags-changed event.
                let code = unsafe { msg_send![event, sel.key_code, () -> u16] };
                // SAFETY: `-modifierFlags` takes nothing and returns an
                // `NSEventModifierFlags`, which is an `NSUInteger`.
                let flags = unsafe { msg_send![event, sel.modifier_flags, () -> NSUInteger] };
                Some(Incoming::FlagsChanged { code, flags })
            }
            appkit::NSEventTypeMouseMoved
            | appkit::NSEventTypeLeftMouseDragged
            | appkit::NSEventTypeRightMouseDragged
            | appkit::NSEventTypeOtherMouseDragged => {
                let (position, inside) = self.position(event);
                // Outside the view, only a drag counts: the button that went
                // down inside is still down.
                (inside || event_type != appkit::NSEventTypeMouseMoved)
                    .then_some(Incoming::Motion { position })
            }
            appkit::NSEventTypeScrollWheel => {
                // SAFETY: a scroll event's `-scrollingDeltaX` and
                // `-scrollingDeltaY` return `CGFloat`s, and
                // `-hasPreciseScrollingDeltas` and
                // `-isDirectionInvertedFromDevice` `BOOL`s.
                let dx = unsafe { msg_send![event, sel.scrolling_delta_x, () -> CGFloat] };
                // SAFETY: as above.
                let dy = unsafe { msg_send![event, sel.scrolling_delta_y, () -> CGFloat] };
                // SAFETY: as above.
                let precise =
                    unsafe { msg_send![event, sel.has_precise_scrolling_deltas, () -> BOOL] };
                // SAFETY: as above.
                let inverted =
                    unsafe { msg_send![event, sel.is_direction_inverted_from_device, () -> BOOL] };
                Some(Incoming::Wheel(input::wheel_lines(
                    dx,
                    dy,
                    precise != NO,
                    inverted != NO,
                )))
            }
            _ if input::is_button_event(event_type) => {
                // SAFETY: a button event's `-buttonNumber` returns an
                // `NSInteger`.
                let number = unsafe { msg_send![event, sel.button_number, () -> NSInteger] };
                let (pressed, button) = input::mouse_button(event_type, number)?;
                let (position, inside) = self.position(event);
                // A click on the title bar is the window's, not the game's.
                if pressed && !inside {
                    return None;
                }
                Some(Incoming::Button {
                    pressed,
                    button,
                    position,
                })
            }
            _ => None,
        }
    }

    /// Reports what a key press for our window typed: its `-characters`.
    fn record_text(&self, event: Id) {
        let sel = self.selectors;
        // SAFETY: `-modifierFlags` takes nothing and returns an
        // `NSEventModifierFlags`, which is an `NSUInteger`.
        let flags = unsafe { msg_send![event, sel.modifier_flags, () -> NSUInteger] };
        // SAFETY: a key event's `-characters` takes nothing and returns an
        // `NSString` that the pool owns, or nil.
        let characters = unsafe { msg_send![event, sel.characters, () -> Id] };
        if characters.is_null() {
            return;
        }
        // SAFETY: `-[NSString UTF8String]` takes nothing and returns a
        // NUL-terminated UTF-8 string, or null if it cannot be converted.
        // The string may live less long than the `NSString`, no longer than
        // the pool that holds both: it is read here, at once.
        let bytes = unsafe { msg_send![characters, sel.utf8_string, () -> *const c_char] };
        if bytes.is_null() {
            return;
        }
        // SAFETY: the string is NUL-terminated, and is read before anything
        // else runs that could free it: nothing of AppKit runs until the
        // copy into events below is done.
        let Ok(text) = unsafe { CStr::from_ptr(bytes) }.to_str() else {
            return;
        };
        self.state
            .record(|translator, events| translator.text(text, flags, events));
    }

    /// Where a mouse event happened, in physical pixels from the top left of
    /// the view, and whether that is inside it.
    fn position(&self, event: Id) -> (Vec2, bool) {
        let sel = self.selectors;
        // SAFETY: a mouse event's `-locationInWindow` returns an `NSPoint`.
        let in_window = unsafe { msg_send![event, sel.location_in_window, () -> NSPoint] };
        // SAFETY: `-[NSView convertPoint:(NSPoint) fromView:(NSView *)]`
        // returns an `NSPoint`; a nil view means the window's coordinates.
        let point = unsafe {
            msg_send![
                self.view.id(),
                sel.convert_point,
                (in_window => NSPoint, ptr::null_mut() => Id) -> NSPoint
            ]
        };
        let bounds = self.state.bounds();
        let inside = (0.0..bounds.size.width).contains(&point.x)
            && (0.0..bounds.size.height).contains(&point.y);
        (input::to_pixels(point, self.state.backing_scale()), inside)
    }

    pub(crate) fn window_size(&self) -> UVec2 {
        self.state.translator.borrow().size()
    }

    pub(crate) fn scale_factor(&self) -> f32 {
        self.state.translator.borrow().scale()
    }

    pub(crate) fn set_fullscreen(&mut self, on: bool) {
        if on == self.state.fullscreen.get() {
            return;
        }
        // Fullscreen takes an animation; the delegate hears when it is done,
        // and this keeps a second request from undoing the first meanwhile.
        self.state.fullscreen.set(on);
        // SAFETY: `-toggleFullScreen:(id)sender` returns nothing.
        unsafe {
            msg_send![
                self.window.id(),
                self.selectors.toggle_full_screen,
                (ptr::null_mut() => Id) -> ()
            ]
        };
    }

    pub(crate) fn set_title(&mut self, title: &str) {
        let title = match ns_string(title) {
            Ok(title) => title,
            Err(error) => {
                error!("could not set the window title: {error}");
                return;
            }
        };
        // SAFETY: `-setTitle:(NSString *)` copies the string and returns
        // nothing.
        unsafe { msg_send![self.window.id(), self.selectors.set_title, (title.id() => Id) -> ()] };
    }

    pub(crate) fn set_cursor_visible(&mut self, visible: bool) {
        if visible == self.cursor_visible {
            return;
        }
        let Ok(cursor) = class(c"NSCursor") else {
            return;
        };
        let selector = if visible {
            self.selectors.unhide
        } else {
            self.selectors.hide
        };
        // `+[NSCursor hide]` and `+unhide` count, like `ShowCursor` on
        // Windows; this is the only code that calls them, once each way.
        // SAFETY: both take nothing and return nothing.
        unsafe { msg_send![as_id(cursor), selector, () -> ()] };
        self.cursor_visible = visible;
    }

    pub(crate) fn set_modal_frame_callback(&mut self, callback: ModalFrameCallback) {
        self.state.modal_frame.set(Some(callback));
    }

    pub(crate) fn gpu_surface(&self) -> GpuSurface<'_> {
        // No OpenGL context yet.
        GpuSurface::Gl(&NO_GL)
    }

    /// Unwinds with a panic that a method of ours caught, if it caught one.
    fn resume_panic(&self) {
        if let Some(payload) = self.state.panic.take() {
            panic::resume_unwind(payload);
        }
    }
}

impl Drop for Backend {
    fn drop(&mut self) {
        let _pool = AutoreleasePool::new();
        let sel = self.selectors;
        if !self.cursor_visible {
            self.set_cursor_visible(true);
        }
        // Detach everything from the state first: AppKit may keep the
        // objects past their release, and call them.
        for object in [
            self.view.id(),
            self.window_delegate.id(),
            self.app_delegate.id(),
        ] {
            // SAFETY: the objects are of our classes, and null points at no
            // state.
            unsafe { runtime::set_state::<State>(object, ptr::null()) };
        }
        let window = self.window.id();
        // SAFETY: `-setDelegate:(id)` returns nothing; nil removes ours.
        unsafe { msg_send![window, sel.set_delegate, (ptr::null_mut() => Id) -> ()] };
        // SAFETY: `-[NSApplication delegate]` takes nothing and returns the
        // delegate.
        let app_delegate = unsafe { msg_send![self.app, sel.delegate, () -> Id] };
        if app_delegate == self.app_delegate.id() {
            // SAFETY: as for the window.
            unsafe { msg_send![self.app, sel.set_delegate, (ptr::null_mut() => Id) -> ()] };
        }
        // SAFETY: `-close` takes nothing and returns nothing. The window
        // was told not to release itself.
        unsafe { msg_send![window, sel.close, () -> ()] };
        // The fields release the objects after this, and the state goes
        // last.
    }
}

/// Makes an object of one of our classes: `alloc`, then `initWithFrame:`
/// for a view, or `init`.
fn new_object(class: Id, sel: Selectors, frame: Option<NSRect>) -> Result<Owned, PlatformError> {
    // SAFETY: `+alloc` takes nothing and returns a new, uninitialized
    // object.
    let blank = unsafe { msg_send![class, sel.alloc, () -> Id] };
    let object = match frame {
        // SAFETY: `-[NSView initWithFrame:(NSRect)]` returns the view,
        // holding a reference for us.
        Some(frame) => unsafe { msg_send![blank, sel.init_with_frame, (frame => NSRect) -> Id] },
        // SAFETY: `-[NSObject init]` takes nothing and returns the object,
        // holding a reference for us.
        None => unsafe { msg_send![blank, sel.init, () -> Id] },
    };
    // SAFETY: the object is new, and its reference is ours.
    let object = unsafe { Owned::new(object) };
    if object.id().is_null() {
        return Err(PlatformError::CallFailed { call: "-init" });
    }
    Ok(object)
}

/// Gives the app a menu bar with one menu, the app's, holding Quit (⌘Q).
/// Quit asks the app delegate, which turns it into a request to close.
fn set_main_menu(app: Id, sel: Selectors, title: &str) -> Result<(), PlatformError> {
    let menu_class = as_id(class(c"NSMenu")?);
    let item_class = as_id(class(c"NSMenuItem")?);
    let menu_bar = new_object(menu_class, sel, None)?;
    let app_item = new_object(item_class, sel, None)?;
    let app_menu = new_object(menu_class, sel, None)?;
    let quit_title = ns_string(&format!("Quit {title}"))?;
    let key = ns_string("q")?;
    // SAFETY: `+alloc` takes nothing and returns a new, uninitialized
    // object.
    let blank = unsafe { msg_send![item_class, sel.alloc, () -> Id] };
    // SAFETY: `-[NSMenuItem initWithTitle:(NSString *) action:(SEL)
    // keyEquivalent:(NSString *)]` returns the item, holding a reference for
    // us. It copies the strings.
    let quit = unsafe {
        msg_send![
            blank,
            sel.init_menu_item,
            (quit_title.id() => Id, sel.terminate => Sel, key.id() => Id) -> Id
        ]
    };
    // SAFETY: the item is new, and its reference is ours.
    let quit = unsafe { Owned::new(quit) };
    // Each menu keeps its items, an item its submenu, and the app its menu
    // bar, so ours can all be released after this.
    // SAFETY: `-[NSMenu addItem:(NSMenuItem *)]` returns nothing.
    unsafe { msg_send![app_menu.id(), sel.add_item, (quit.id() => Id) -> ()] };
    // SAFETY: `-[NSMenuItem setSubmenu:(NSMenu *)]` returns nothing.
    unsafe { msg_send![app_item.id(), sel.set_submenu, (app_menu.id() => Id) -> ()] };
    // SAFETY: as for `addItem:` above.
    unsafe { msg_send![menu_bar.id(), sel.add_item, (app_item.id() => Id) -> ()] };
    // SAFETY: `-[NSApplication setMainMenu:(NSMenu *)]` returns nothing.
    unsafe { msg_send![app, sel.set_main_menu, (menu_bar.id() => Id) -> ()] };
    Ok(())
}