inker 0.2.0

Modular engine/renderer controller — selects and orchestrates content engines (genet HTML lanes, nematic smolweb, scrying/graft/weld surface engines).
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// SPDX-License-Identifier: MPL-2.0
//! Inker's platform browser bindings on the shared surface protocol. Native
//! frame conversion is supplied by the engine/host pair that owns its handles;
//! this layer never promotes borrowed handles into owned shared frames.
use crate::{
    KeyboardEvent, KeyboardModifiers, MouseButton, MouseEvent, MouseEventKind, NavigationEvent,
    PhysicalPosition, SurfaceProducer, WebSurfaceEvent,
};
use std::{collections::VecDeque, time::Duration};
use webcue::*;

/// The engine/host binding must return a non-blocking, move-owned frame with
/// this stamp. It retains the native resource and synchronization until import.
pub type FrameReader =
    Box<dyn FnMut(&mut dyn SurfaceProducer, FrameStamp) -> Result<Option<Frame>>>;
pub struct BrowserSurface {
    producer: Box<dyn SurfaceProducer>,
    protocol: Protocol,
    report: CapabilityReport,
    viewport: Viewport,
    visible: bool,
    commands: VecDeque<(RequestKey, Command)>,
    frames: FrameReader,
    revision: u64,
    turn: Option<Box<dyn FnMut(Duration) -> Result<()>>>,
}
fn backend(e: impl std::fmt::Display) -> Error {
    Error::Backend(e.to_string())
}
fn support(s: crate::WebFeatureStatus) -> Support {
    match s {
        crate::WebFeatureStatus::Supported => Support::Supported,
        crate::WebFeatureStatus::Partial { detail } => Support::Partial(detail),
        crate::WebFeatureStatus::Unsupported { reason } => Support::Unsupported(reason),
    }
}
impl BrowserSurface {
    pub fn new(
        mut producer: Box<dyn SurfaceProducer>,
        scope: Scope,
        viewport: Viewport,
        presentation: PresentationMode,
        frames: FrameReader,
    ) -> Result<Self> {
        let caps = producer
            .as_web_surface()
            .ok_or_else(|| Error::Unsupported("browser control binding required".into()))?
            .capabilities();
        let report=CapabilityReport::new(BackendIdentity{engine:caps.backend_name.clone(),version:caps.backend_version.clone(),platform:crate::cue::platform_id().into(),build_features:vec!["inker-platform-binding".into()]},
            presentation,Evidence::Constructed,SemanticProtocol::Unavailable,Feature::ALL.map(|f|(f,match f {
                Feature::Navigation|Feature::Reload|Feature::Stop|Feature::History=>support(caps.document.navigation.clone()),
                Feature::InlineHtml=>Support::Partial("inline HTML dispatch; base address remains engine-owned".into()),
                Feature::Zoom=>support(caps.document.page_zoom.clone()),
                Feature::Mouse=>support(caps.pointer.mouse.clone()),
                Feature::Scroll|Feature::Focus=>Support::Partial("dispatch through the installed platform binding; explicit refusal is preserved".into()),
                Feature::Keyboard|Feature::NativeKeyboard|Feature::Text=>Support::Partial("virtual key and composed text fields; repeat and independent native character fields are unavailable in the platform extension".into()),
                _=>Support::Unsupported("use the explicit platform extension; this shared adapter does not expose this operation".into()),
            })))?;
        Self::new_with_report(producer, scope, viewport, report, frames)
    }
    /// Bind a concrete producer with its complete, effective capability report.
    /// The report describes this installed control/frame binding, including
    /// refusals that the older platform vocabulary cannot name independently.
    pub fn new_with_report(
        mut producer: Box<dyn SurfaceProducer>,
        scope: Scope,
        viewport: Viewport,
        report: CapabilityReport,
        frames: FrameReader,
    ) -> Result<Self> {
        let viewport = viewport.validate()?;
        if report.presentation == PresentationMode::HostDevice
            || report.semantics != SemanticProtocol::Unavailable
            || report.features().any(|(feature, support)| {
                support.permits()
                    && !matches!(
                        feature,
                        Feature::Navigation
                            | Feature::Reload
                            | Feature::Stop
                            | Feature::History
                            | Feature::InlineHtml
                            | Feature::Zoom
                            | Feature::Mouse
                            | Feature::Scroll
                            | Feature::Focus
                            | Feature::Keyboard
                            | Feature::NativeKeyboard
                            | Feature::Text
                    )
            })
        {
            return Err(Error::Invalid("report advertises an operation or frame direction absent from this browser binding".into()));
        }
        if producer.as_web_surface().is_none() {
            return Err(Error::Unsupported(
                "browser control binding required".into(),
            ));
        }
        producer
            .resize(viewport.width, viewport.height)
            .map_err(backend)?;
        Ok(Self {
            producer,
            protocol: Protocol::new(scope, 256)?,
            report,
            viewport,
            visible: true,
            commands: VecDeque::new(),
            frames,
            revision: 0,
            turn: None,
        })
    }
    /// Install the concrete engine's bounded UI/process turn. It runs through
    /// `Surface::advance` even when frame acquisition is hidden or suspended.
    pub fn with_engine_turn(mut self, turn: impl FnMut(Duration) -> Result<()> + 'static) -> Self {
        self.turn = Some(Box::new(turn));
        self
    }
    /// Advanced browser/native embedding extensions retain their original owner.
    /// The host must not independently drain their event queue.
    pub fn platform(&self) -> &(dyn SurfaceProducer + 'static) {
        self.producer.as_ref()
    }
    pub fn viewport(&self) -> Viewport {
        self.viewport
    }
    /// Latest shared observation, without a second platform event drain.
    pub fn text_input_state(&self) -> Option<&TextInputState> {
        self.protocol.text_input_state()
    }
    pub fn platform_mut(&mut self) -> &mut (dyn SurfaceProducer + 'static) {
        self.producer.as_mut()
    }
    fn position(&self, x: f32, y: f32) -> PhysicalPosition {
        PhysicalPosition {
            x: x * self.viewport.scale,
            y: y * self.viewport.scale,
        }
    }
    fn key(
        &mut self,
        key: u32,
        scan: u32,
        pressed: bool,
        text: Option<String>,
        modifiers: SessionModifiers,
    ) -> Result<()> {
        self.producer
            .send_keyboard_input(KeyboardEvent {
                key_code: key,
                scan_code: scan,
                pressed,
                text,
                modifiers: KeyboardModifiers {
                    shift: modifiers.shift,
                    ctrl: modifiers.control,
                    alt: modifiers.alt,
                    meta: modifiers.meta,
                },
            })
            .map_err(backend)
    }
    fn drain_callbacks(&mut self) -> Result<bool> {
        // Reserve before consuming a callback. A full mailbox leaves it with
        // its producer, preserving supplier order instead of dropping events.
        for _ in 0..64 {
            self.protocol.ensure_observations(2)?;
            let event = self
                .producer
                .as_web_surface()
                .and_then(|web| web.poll_web_event());
            let Some(event) = event else { return Ok(true) };
            match event {
                WebSurfaceEvent::Navigation(NavigationEvent::Started { url }) => self
                    .protocol
                    .emit(Event::NavigationStarted { address: url })?,
                WebSurfaceEvent::Navigation(NavigationEvent::Committed { url })
                | WebSurfaceEvent::AddressChanged { url } => {
                    self.protocol.emit(Event::AddressChanged(url))?
                },
                WebSurfaceEvent::Navigation(NavigationEvent::Finished { url, title }) => {
                    if let Some(title) = title {
                        self.protocol.emit(Event::TitleChanged(title))?;
                    }
                    self.protocol.emit(Event::NavigationFinished {
                        address: url,
                        success: true,
                    })?;
                },
                WebSurfaceEvent::Navigation(NavigationEvent::Failed { url, .. }) => {
                    self.protocol.emit(Event::NavigationFinished {
                        address: url,
                        success: false,
                    })?
                },
                WebSurfaceEvent::TitleChanged { title } => {
                    self.protocol.emit(Event::TitleChanged(title))?
                },
                WebSurfaceEvent::WebMessage(message) => self.protocol.emit(Event::Message(
                    serde_json::to_string(&message).map_err(backend)?,
                ))?,
                other => self.protocol.emit(Event::Message(
                    serde_json::to_string(&other).map_err(backend)?,
                ))?,
            }
        }
        Ok(false)
    }
    fn execute(&mut self, command: Command) -> Result<Reply> {
        if let Command::Zoom(factor) = command {
            return self
                .producer
                .set_page_zoom(factor)
                .map(Reply::Zoom)
                .map_err(backend);
        }
        let web = self
            .producer
            .as_web_surface()
            .ok_or_else(|| Error::Unsupported("browser control binding unavailable".into()))?;
        match command {
            Command::Navigate { address } => web.navigate_to_url(&address),
            Command::LoadHtml { html, .. } => web.navigate_to_string(&html),
            Command::Reload => web.reload(),
            Command::Stop => web.stop(),
            Command::Back => web.go_back(),
            Command::Forward => web.go_forward(),
            _ => {
                return Err(Error::Unsupported(
                    "shared operation not exposed by this binding".into(),
                ));
            },
        }
        .map_err(backend)?;
        Ok(Reply::Dispatched)
    }
}
impl Surface for BrowserSurface {
    fn scope(&self) -> Scope {
        self.protocol.scope()
    }
    fn capabilities(&self) -> &CapabilityReport {
        &self.report
    }
    fn set_viewport(&mut self, v: Viewport) -> Result<()> {
        self.protocol.ensure_live()?;
        let v = v.validate()?;
        if self.viewport != v {
            self.producer.resize(v.width, v.height).map_err(backend)?;
            self.viewport = v;
        }
        Ok(())
    }
    fn set_visible(&mut self, visible: bool) -> Result<()> {
        self.protocol.ensure_live()?;
        self.visible = visible;
        Ok(())
    }
    fn input(&mut self, input: Input) -> Result<()> {
        self.protocol.ensure_live()?;
        if !self.visible {
            return Err(Error::Invalid("surface is hidden".into()));
        }
        let feature = match &input {
            Input::Scroll { .. } => Feature::Scroll,
            Input::NativeKey(_) => Feature::NativeKeyboard,
            Input::Document(input) => match input {
                SessionInput::PointerMoved { .. } | SessionInput::PointerButton { .. } => {
                    Feature::Mouse
                },
                SessionInput::Text(_) | SessionInput::KeyboardText(_) => Feature::Text,
                SessionInput::Key { .. } => Feature::Keyboard,
                SessionInput::Ime(_) => Feature::Ime,
                SessionInput::Focus(_) | SessionInput::FocusMove(_) | SessionInput::Cancel => {
                    Feature::Focus
                },
            },
        };
        self.report.require(feature)?;
        match input {
            Input::Scroll {
                x,
                y,
                dx,
                dy,
                modifiers,
            } => self
                .producer
                .send_wheel_input(crate::SurfaceWheelEvent {
                    position: self.position(x, y),
                    delta: (dx * self.viewport.scale, dy * self.viewport.scale),
                    modifiers: KeyboardModifiers {
                        shift: modifiers.shift,
                        ctrl: modifiers.control,
                        alt: modifiers.alt,
                        meta: modifiers.meta,
                    },
                })
                .map_err(backend),
            Input::NativeKey(k) => {
                if k.repeat || k.characters_ignoring_modifiers != k.characters {
                    return Err(Error::Unsupported("native repeat/alternate character fields are not represented by the platform extension".into()));
                }
                self.key(
                    k.key_code,
                    k.scan_code,
                    k.pressed,
                    Some(k.characters),
                    k.modifiers,
                )
            },
            Input::Document(input) => match input {
                SessionInput::PointerMoved { x, y, modifiers }
                    if modifiers == SessionModifiers::default() =>
                {
                    self.producer
                        .send_mouse_input(MouseEvent {
                            position: self.position(x, y),
                            button: None,
                            kind: MouseEventKind::Moved,
                        })
                        .map_err(backend)
                },
                SessionInput::PointerButton {
                    x,
                    y,
                    button,
                    state,
                    modifiers,
                } if modifiers == SessionModifiers::default() => self
                    .producer
                    .send_mouse_input(MouseEvent {
                        position: self.position(x, y),
                        button: Some(match button {
                            SessionPointerButton::Primary => MouseButton::Left,
                            SessionPointerButton::Secondary => MouseButton::Right,
                            SessionPointerButton::Auxiliary => MouseButton::Middle,
                        }),
                        kind: if state == SessionButtonState::Pressed {
                            MouseEventKind::Pressed
                        } else {
                            MouseEventKind::Released
                        },
                    })
                    .map_err(backend),
                SessionInput::Focus(focused) => self.producer.set_focus(focused).map_err(backend),
                SessionInput::Text(text) | SessionInput::KeyboardText(text) => {
                    self.key(0, 0, true, Some(text), SessionModifiers::default())
                },
                SessionInput::Key {
                    key,
                    state,
                    modifiers,
                    repeat: false,
                } => {
                    let code = match key {
                        SessionKey::Enter => 13,
                        SessionKey::Tab => 9,
                        SessionKey::Backspace => 8,
                        SessionKey::Delete => 46,
                        SessionKey::Escape => 27,
                        SessionKey::Space => 32,
                        SessionKey::ArrowLeft => 37,
                        SessionKey::ArrowUp => 38,
                        SessionKey::ArrowRight => 39,
                        SessionKey::ArrowDown => 40,
                        SessionKey::Home => 36,
                        SessionKey::End => 35,
                        SessionKey::PageUp => 33,
                        SessionKey::PageDown => 34,
                        _ => {
                            return Err(Error::Unsupported(
                                "use NativeKey for platform character keys".into(),
                            ));
                        },
                    };
                    self.key(
                        code,
                        0,
                        state == SessionButtonState::Pressed,
                        None,
                        modifiers,
                    )
                },
                _ => Err(Error::Unsupported(
                    "input fields are not losslessly represented by this platform binding".into(),
                )),
            },
        }
    }
    fn request(&mut self, id: RequestId, command: Command) -> Result<RequestKey> {
        self.protocol.ensure_live()?;
        self.report.require(command.feature())?;
        if let Command::LoadHtml { address, .. } = &command
            && !address.is_empty()
        {
            return Err(Error::Unsupported(
                "inline base address is not represented by this platform binding".into(),
            ));
        }
        if matches!(command,Command::Zoom(f) if !f.is_finite()||f<=0.) {
            return Err(Error::Invalid("invalid zoom".into()));
        }
        if self.commands.len() >= 128 {
            return Err(Error::Backpressure);
        }
        let key = self.protocol.accept(id)?;
        self.commands.push_back((key, command));
        Ok(key)
    }
    fn semantic_action(&mut self, _: SemanticAction) -> Result<()> {
        self.protocol.ensure_live()?;
        Err(Error::Unsupported(
            "use the native semantic platform extension".into(),
        ))
    }
    fn advance(&mut self, now: Duration) -> Result<()> {
        self.protocol.ensure_live()?;
        // Preserve callback order within a bounded batch, but do not let a
        // continuously busy page starve host navigation or stop dispatch.
        self.drain_callbacks()?;
        if let Some((key, command)) = self.commands.pop_front() {
            let result = self.execute(command);
            self.protocol.settle(key, result)?;
        }
        if let Some(turn) = &mut self.turn {
            turn(now)?;
        }
        self.drain_callbacks()?;
        self.protocol.ensure_observations(1)?;
        if let Some(cursor) = self.producer.poll_cursor_shape() {
            use crate::CursorShape::*;
            let cursor = match cursor {
                Default => "default",
                Text => "text",
                Pointer => "pointer",
                Grab => "grab",
                Grabbing => "grabbing",
                Crosshair => "crosshair",
                Move => "move",
                ResizeNs => "ns-resize",
                ResizeEw => "ew-resize",
                ResizeNesw => "nesw-resize",
                ResizeNwse => "nwse-resize",
                NotAllowed => "not-allowed",
                Hidden => "none",
            };
            self.protocol.emit(Event::CursorChanged(cursor.into()))?;
        }
        Ok(())
    }
    fn poll_event(&mut self) -> Option<Envelope> {
        self.protocol.poll()
    }
    fn retire(&mut self) -> Result<()> {
        self.commands.clear();
        self.visible = false;
        self.protocol.retire()
    }
}
impl FrameSource for BrowserSurface {
    fn try_acquire_frame(&mut self) -> Result<Option<Frame>> {
        self.protocol.ensure_live()?;
        if !self.visible {
            return Ok(None);
        }
        let revision = self.revision.checked_add(1).ok_or(Error::Exhausted)?;
        let stamp = FrameStamp {
            scope: self.scope(),
            allocation_epoch: revision,
            content_revision: revision,
        };
        let frame = (self.frames)(self.producer.as_mut(), stamp)?;
        if let Some(frame) = &frame {
            let actual = match frame {
                Frame::Native(frame)
                    if self.report.presentation == PresentationMode::NativeFrame =>
                {
                    frame.stamp
                },
                Frame::Cpu(frame) if self.report.presentation == PresentationMode::CpuRgba => {
                    frame.validate()?;
                    if frame.size.width != self.viewport.width
                        || frame.size.height != self.viewport.height
                    {
                        return Err(Error::Invalid("frame extent differs from viewport".into()));
                    }
                    frame.stamp
                },
                Frame::Overlay if self.report.presentation == PresentationMode::NativeOverlay => {
                    stamp
                },
                _ => {
                    return Err(Error::Invalid(
                        "frame binding disagrees with declared presentation".into(),
                    ));
                },
            };
            if actual.scope != stamp.scope || actual.content_revision != stamp.content_revision {
                return Err(Error::Stale);
            }
            self.revision = revision;
        }
        Ok(frame)
    }
}

#[cfg(test)]
mod tests;