nandi/jolt-nativepublic Fork 0
8e8cd5192dc161b423c0ee5dd41a7a058b24b409
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Give the terminal's entry a caret that means what it says 361b4dc · on 8e8cd5192dc161b423c0ee5dd41a7a058b24b409 · nandi · 9d ago
lib.rs · 509 lines · 15.9 KBRust Blame HistoryRaw
  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
//! A terminal backend for glimmer, behind a C ABI — `libjolttui.so`.
//!
//! [`glimmer-tui`](https://github.com/jolt-lang/glimmer-tui) is the design this
//! follows: the same tags, the same props, the same keyboard, and the same rule
//! that painting goes through a grid so a test needs no terminal. What is
//! different is where the widget layer lives. There it is jolt over ncurses;
//! here it is Rust behind the same retained-tree ABI `libvidya` already
//! exports, so one glimmer backend on the jolt side can drive a GPU window or a
//! terminal by naming a different shared object.
//!
//! That split is the point. A reconciler needs widgets to patch, and a terminal
//! has none — so the tree lives down here, and FFI traffic tracks *edits*
//! rather than frames: a static screen costs no crossings per frame, and only
//! what the reconciler actually changed is sent.
//!
//! Rules inherited from this workspace's ABI:
//!
//! * one session per process;
//! * every call stays on the thread that opened it — the session lives in
//!   thread-local storage, so a call from another thread is inert rather than
//!   unsound;
//! * only integers, doubles, and UTF-8 byte strings cross, and a returned
//!   string is borrowed until the next one of its family;
//! * nothing calls back. Interactions queue, and the caller polls.
//!
//! Handlers never cross the boundary: a node reports that it was clicked, and
//! the caller looks up whose `:on-click` that was.

// Without the terminal feature the flush path is gone, and with it the only
// caller of a handful of screen and session accessors. They are the ABI's
// vocabulary, not dead code, so a headless build does not warn about them.
#![cfg_attr(not(feature = "terminal"), allow(dead_code))]

mod entry;
mod graphics;
mod keys;
mod layout;
mod paint;
mod screen;
#[cfg(feature = "terminal")]
mod term;
mod tree;
mod ui;

#[cfg(test)]
mod tests;

use std::cell::RefCell;
use std::ffi::{c_char, c_double, c_int};

use jolt_abi::{borrowed, empty_str, guard, Scratch};
use tree::Value;
use ui::Ui;

/// The reads: a prop, a tag, a line of the screen. One scratch, so a caller
/// holding a tag pointer across a prop read gets the documented lifetime and
/// not a surprise.
static READS: Scratch = Scratch::new();
/// Dumps are their own family: a dump is usually being printed beside the
/// props it mentions.
static DUMPS: Scratch = Scratch::new();
/// The event name and the event text are read one after the other by every
/// caller there will ever be, so they cannot share a scratch.
static NAMES: Scratch = Scratch::new();
static EVENTS: Scratch = Scratch::new();

struct Session {
    ui: Ui,
    #[cfg(feature = "terminal")]
    term: Option<term::Term>,
}

thread_local! {
    /// The process's session, owned by the thread that opened it.
    static SESSION: RefCell<Option<Session>> = const { RefCell::new(None) };
}

fn with<R: Copy>(fallback: R, f: impl FnOnce(&mut Session) -> R) -> R {
    guard(fallback, || {
        SESSION.with_borrow_mut(|slot| match slot.as_mut() {
            Some(session) => f(session),
            None => fallback,
        })
    })
}

/// Most of this ABI is a call on the tree with a session around it.
fn with_ui<R: Copy>(fallback: R, f: impl FnOnce(&mut Ui) -> R) -> R {
    with(fallback, |session| f(&mut session.ui))
}

// ── the session ─────────────────────────────────────────────────────────────

/// Take the terminal. `mouse` non-zero turns on mouse reporting. 1 on success,
/// 0 if a session is already open or the terminal refused raw mode.
#[no_mangle]
pub extern "C" fn tui_open(mouse: c_int) -> c_int {
    guard(0, || {
        SESSION.with_borrow_mut(|slot| {
            if slot.is_some() {
                log::error!("jolt-tui: a session is already open");
                return 0;
            }
            #[cfg(feature = "terminal")]
            {
                match term::Term::open(mouse != 0) {
                    Ok(term) => {
                        let (w, h) = term.size();
                        *slot = Some(Session {
                            ui: Ui::new(w, h),
                            term: Some(term),
                        });
                        1
                    }
                    Err(e) => {
                        log::error!("jolt-tui: could not take the terminal: {e}");
                        0
                    }
                }
            }
            #[cfg(not(feature = "terminal"))]
            {
                let _ = mouse;
                log::error!("jolt-tui: built without the terminal feature");
                0
            }
        })
    })
}

/// Open a session with no terminal at all, at a fixed size.
///
/// The whole widget layer works here — layout, painting, focus, keys fed with
/// `tui_feed_key` — and `tui_screen_line` reads the result back. This is what a
/// test suite and CI use, and it is the same code path a real session paints
/// through, not a second implementation of it.
#[no_mangle]
pub extern "C" fn tui_headless(width: c_int, height: c_int) -> c_int {
    guard(0, || {
        SESSION.with_borrow_mut(|slot| {
            if slot.is_some() {
                return 0;
            }
            *slot = Some(Session {
                ui: Ui::new(
                    width.clamp(1, u16::MAX as c_int) as u16,
                    height.clamp(1, u16::MAX as c_int) as u16,
                ),
                #[cfg(feature = "terminal")]
                term: None,
            });
            1
        })
    })
}

/// Give the terminal back and drop the tree. Safe to call twice.
#[no_mangle]
pub extern "C" fn tui_close() {
    guard((), || {
        SESSION.with_borrow_mut(|slot| {
            #[cfg(feature = "terminal")]
            if let Some(session) = slot.as_mut() {
                if let Some(term) = session.term.as_mut() {
                    term.close();
                }
            }
            *slot = None;
        })
    })
}

#[no_mangle]
pub extern "C" fn tui_should_close() -> c_int {
    with_ui(1, |ui| ui.should_close() as c_int)
}

#[no_mangle]
pub extern "C" fn tui_quit() {
    with_ui((), |ui| ui.quit())
}

/// Wait up to `timeout_ms` for input, then handle everything that arrived.
/// Answers how many things it handled, so a caller can skip a repaint when
/// nothing happened. Inert in a headless session, which is fed by hand.
#[no_mangle]
pub extern "C" fn tui_tick(timeout_ms: c_int) -> c_int {
    with(0, |session| {
        #[cfg(feature = "terminal")]
        {
            let Some(term) = session.term.as_mut() else {
                return 0;
            };
            let inputs = term.poll(timeout_ms.max(0) as u64);
            let mut handled = 0;
            for input in inputs {
                handled += 1;
                match input {
                    term::Input::Key(name) => {
                        session.ui.key(&name);
                    }
                    term::Input::Click(x, y) => {
                        session.ui.click(x, y);
                    }
                    term::Input::Wheel(x, y, by) => {
                        session.ui.wheel(x, y, by);
                    }
                    term::Input::Resize(w, h) => session.ui.resize(w, h),
                }
            }
            handled
        }
        #[cfg(not(feature = "terminal"))]
        {
            let _ = (session, timeout_ms);
            0
        }
    })
}

/// Lay the tree out, paint it, and send what changed. A headless session paints
/// and stops there.
#[no_mangle]
pub extern "C" fn tui_frame() {
    with((), |session| {
        session.ui.frame();
        #[cfg(feature = "terminal")]
        if let Some(term) = session.term.as_mut() {
            let cursor = session.ui.cursor();
            let images = session.ui.images().to_vec();
            if let Err(e) = term.flush(&session.ui.screen, cursor, &images) {
                log::error!("jolt-tui: could not write a frame: {e}");
            }
        }
    })
}

#[no_mangle]
pub extern "C" fn tui_screen_width() -> c_int {
    with_ui(0, |ui| ui.screen.width() as c_int)
}

#[no_mangle]
pub extern "C" fn tui_screen_height() -> c_int {
    with_ui(0, |ui| ui.screen.height() as c_int)
}

/// One painted row as text, trailing blanks trimmed — what a test asserts on,
/// and what a bug report pastes. Borrowed until the next read.
#[no_mangle]
pub extern "C" fn tui_screen_line(y: c_int) -> *const c_char {
    with_ui(empty_str(), |ui| {
        if y < 0 {
            return empty_str();
        }
        READS.lend(ui.screen.line(y as u16))
    })
}

// ── input by hand ───────────────────────────────────────────────────────────

/// Feed one key by name — `"ctrl+u"`, `"page-down"`, `"a"` — as if the terminal
/// had sent it. Answers 1 when the backend acted on it and 0 when it went out
/// as a `key` event instead.
///
/// # Safety
/// `name` is null or a NUL-terminated UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn tui_feed_key(name: *const c_char) -> c_int {
    let name = borrowed(name);
    with_ui(0, |ui| ui.key(&name) as c_int)
}

#[no_mangle]
pub extern "C" fn tui_feed_click(x: c_int, y: c_int) -> c_int {
    with_ui(0, |ui| {
        if x < 0 || y < 0 {
            return 0;
        }
        ui.click(x as u16, y as u16) as c_int
    })
}

#[no_mangle]
pub extern "C" fn tui_feed_wheel(x: c_int, y: c_int, by: c_int) -> c_int {
    with_ui(0, |ui| {
        if x < 0 || y < 0 {
            return 0;
        }
        ui.wheel(x as u16, y as u16, by) as c_int
    })
}

/// The focused node, 0 for none.
#[no_mangle]
pub extern "C" fn tui_focus() -> c_int {
    with_ui(0, |ui| ui.focus() as c_int)
}

// ── the tree ────────────────────────────────────────────────────────────────

#[no_mangle]
pub extern "C" fn tui_tree_root() -> c_int {
    with_ui(0, |ui| ui.tree.root() as c_int)
}

/// # Safety
/// `tag` is null or a NUL-terminated UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn tui_node_new(tag: *const c_char) -> c_int {
    let tag = borrowed(tag);
    with_ui(0, |ui| ui.tree.new_node(&tag) as c_int)
}

#[no_mangle]
pub extern "C" fn tui_node_free(node: c_int) {
    with_ui((), |ui| ui.tree.free_node(node.max(0) as u32))
}

#[no_mangle]
pub extern "C" fn tui_node_exists(node: c_int) -> c_int {
    with_ui(0, |ui| ui.tree.exists(node.max(0) as u32) as c_int)
}

/// # Safety
/// `key` and `value` are null or NUL-terminated UTF-8 strings.
#[no_mangle]
pub unsafe extern "C" fn tui_node_set_str(node: c_int, key: *const c_char, value: *const c_char) {
    let (key, value) = (borrowed(key), borrowed(value));
    with_ui((), |ui| {
        ui.tree.set(node.max(0) as u32, &key, Value::Str(value))
    })
}

/// # Safety
/// `key` is null or a NUL-terminated UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn tui_node_set_num(node: c_int, key: *const c_char, value: c_double) {
    let key = borrowed(key);
    with_ui((), |ui| {
        ui.tree.set(node.max(0) as u32, &key, Value::Num(value))
    })
}

/// # Safety
/// `key` is null or a NUL-terminated UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn tui_node_set_bool(node: c_int, key: *const c_char, value: c_int) {
    let key = borrowed(key);
    with_ui((), |ui| {
        ui.tree
            .set(node.max(0) as u32, &key, Value::Bool(value != 0))
    })
}

#[no_mangle]
pub extern "C" fn tui_node_clear_props(node: c_int) {
    with_ui((), |ui| ui.tree.clear_props(node.max(0) as u32))
}

/// # Safety
/// `key` is null or a NUL-terminated UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn tui_node_get_str(node: c_int, key: *const c_char) -> *const c_char {
    let key = borrowed(key);
    with_ui(empty_str(), |ui| {
        match ui.tree.get(node.max(0) as u32, &key) {
            Some(Value::Str(text)) => READS.lend(text.clone()),
            _ => empty_str(),
        }
    })
}

/// # Safety
/// `key` is null or a NUL-terminated UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn tui_node_get_num(node: c_int, key: *const c_char) -> c_double {
    let key = borrowed(key);
    with_ui(0.0, |ui| match ui.tree.get(node.max(0) as u32, &key) {
        Some(Value::Num(n)) => *n,
        Some(Value::Bool(b)) => *b as i32 as f64,
        _ => 0.0,
    })
}

/// # Safety
/// `key` is null or a NUL-terminated UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn tui_node_get_bool(node: c_int, key: *const c_char) -> c_int {
    let key = borrowed(key);
    with_ui(0, |ui| match ui.tree.get(node.max(0) as u32, &key) {
        Some(Value::Bool(b)) => *b as c_int,
        Some(Value::Num(n)) => (*n != 0.0) as c_int,
        _ => 0,
    })
}

#[no_mangle]
pub extern "C" fn tui_node_tag(node: c_int) -> *const c_char {
    with_ui(empty_str(), |ui| {
        READS.lend(ui.tree.tag_name(node.max(0) as u32).to_owned())
    })
}

/// The node this one hangs off, 0 when it is unparented or is the window.
#[no_mangle]
pub extern "C" fn tui_node_parent(node: c_int) -> c_int {
    with_ui(0, |ui| ui.tree.parent(node.max(0) as u32) as c_int)
}

#[no_mangle]
pub extern "C" fn tui_node_child_count(node: c_int) -> c_int {
    with_ui(0, |ui| ui.tree.child_count(node.max(0) as u32) as c_int)
}

#[no_mangle]
pub extern "C" fn tui_node_child_at(node: c_int, index: c_int) -> c_int {
    with_ui(0, |ui| {
        if index < 0 {
            return 0;
        }
        ui.tree.child_at(node.max(0) as u32, index as usize) as c_int
    })
}

#[no_mangle]
pub extern "C" fn tui_node_append(parent: c_int, child: c_int) -> c_int {
    with_ui(0, |ui| {
        ui.tree.append(parent.max(0) as u32, child.max(0) as u32) as c_int
    })
}

#[no_mangle]
pub extern "C" fn tui_node_remove(parent: c_int, child: c_int) {
    with_ui((), |ui| {
        ui.tree.remove(parent.max(0) as u32, child.max(0) as u32)
    })
}

#[no_mangle]
pub extern "C" fn tui_node_insert_after(parent: c_int, child: c_int, sibling: c_int) -> c_int {
    with_ui(0, |ui| {
        ui.tree.insert_after(
            parent.max(0) as u32,
            child.max(0) as u32,
            sibling.max(0) as u32,
        ) as c_int
    })
}

#[no_mangle]
pub extern "C" fn tui_node_replace(parent: c_int, old_child: c_int, new_child: c_int) -> c_int {
    with_ui(0, |ui| {
        ui.tree.replace(
            parent.max(0) as u32,
            old_child.max(0) as u32,
            new_child.max(0) as u32,
        ) as c_int
    })
}

/// The subtree at `node` as pretty-printed hiccup; `node` 0 means the root, so
/// `tui_tree_dump(0)` is the whole window.
#[no_mangle]
pub extern "C" fn tui_tree_dump(node: c_int) -> *const c_char {
    with_ui(empty_str(), |ui| {
        let id = if node <= 0 {
            ui.tree.root()
        } else {
            node as u32
        };
        DUMPS.lend(ui.tree.dump(id))
    })
}

// ── events ──────────────────────────────────────────────────────────────────

#[no_mangle]
pub extern "C" fn tui_tree_poll_event() -> c_int {
    with_ui(0, |ui| ui.tree.poll() as c_int)
}

#[no_mangle]
pub extern "C" fn tui_tree_event_node() -> c_int {
    with_ui(0, |ui| {
        ui.tree.current().map_or(0, |event| event.node as c_int)
    })
}

#[no_mangle]
pub extern "C" fn tui_tree_event_name() -> *const c_char {
    with_ui(empty_str(), |ui| match ui.tree.current() {
        Some(event) => NAMES.lend(event.name),
        None => empty_str(),
    })
}

#[no_mangle]
pub extern "C" fn tui_tree_event_text() -> *const c_char {
    with_ui(empty_str(), |ui| match ui.tree.current() {
        Some(event) => EVENTS.lend(event.text.clone()),
        None => empty_str(),
    })
}

#[no_mangle]
pub extern "C" fn tui_tree_event_num() -> c_double {
    with_ui(0.0, |ui| ui.tree.current().map_or(0.0, |event| event.num))
}