aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / ui / allen_ui.c
| 1 | /* allen_ui.c -- Allen's home; see allen_ui.h. |
| 2 | * |
| 3 | * Layers: the night (ui/night.c) keeps the sky in its own full-screen |
| 4 | * buffer; `back` is where a frame is put together: for each changed part of |
| 5 | * the screen the sky as it looks now, then the lights in it, Allen, his |
| 6 | * words, the bar, all added in order and clipped to that part, which is |
| 7 | * then copied to the screen. Colours are 0x00RRGGBB in RAM; the copy swaps |
| 8 | * red and blue for an RGBX framebuffer. |
| 9 | * |
| 10 | * Which parts change is kept on a grid of tiles (64 x 32 px): each frame |
| 11 | * marks the tiles under anything that moved, and each row of marked tiles |
| 12 | * becomes a strip. Strips never overlap, so they can be drawn by any core in |
| 13 | * any order: a frame publishes its strips in one word (count and next index) |
| 14 | * that cores claim one at a time. |
| 15 | * |
| 16 | * Calls from the keyboard side (input, said, map, wake) only leave a note |
| 17 | * under a short lock; the next frame picks it up. The layout is designed on |
| 18 | * a 1920 x 1080 grid and scaled to the screen (k, 1/256). */ |
| 19 | #include "allen_ui.h" |
| 20 | #include "allen_font.h" |
| 21 | #include "canvas.h" |
| 22 | #include "sky_real.h" |
| 23 | |
| 24 | /* Sky regions for "somewhere new" mode (real celestial objects, J2000 coordinates). |
| 25 | * Each start opens on the next region in this list, cycling. */ |
| 26 | #define SKY_NREGIONS 12 |
| 27 | static const int32_t sky_region_ra_udeg[SKY_NREGIONS] = { |
| 28 | 266417000, /* galactic centre */ |
| 29 | 83822000, /* Orion / M42 */ |
| 30 | 161265000, /* Carina Nebula */ |
| 31 | 56750000, /* Pleiades */ |
| 32 | 10684000, /* Andromeda (M31) */ |
| 33 | 80894000, /* Large Magellanic Cloud */ |
| 34 | 113697000, /* GW150914 region */ |
| 35 | 305557000, /* Cygnus / Northern Cross */ |
| 36 | 274754000, /* Sagittarius star cloud */ |
| 37 | 247352000, /* Scorpius / Antares */ |
| 38 | 13190000, /* Small Magellanic Cloud */ |
| 39 | 0, /* north celestial pole */ |
| 40 | }; |
| 41 | static const int32_t sky_region_dec_udeg[SKY_NREGIONS] = { |
| 42 | -28936000, /* galactic centre */ |
| 43 | -5391000, /* Orion / M42 */ |
| 44 | -59688000, /* Carina Nebula */ |
| 45 | 24117000, /* Pleiades */ |
| 46 | 41269000, /* Andromeda (M31) */ |
| 47 | -69756000, /* Large Magellanic Cloud */ |
| 48 | -72757000, /* GW150914 region */ |
| 49 | 40734000, /* Cygnus */ |
| 50 | -18565000, /* Sagittarius */ |
| 51 | -26432000, /* Scorpius */ |
| 52 | -72830000, /* Small Magellanic Cloud */ |
| 53 | 90000000, /* north celestial pole */ |
| 54 | }; |
| 55 | #define SKY_DRIFT_URA_PER_S 720000 /* 0.72 deg/min drift in RA (slow pan) */ |
| 56 | #define SKY_REBUILD_MS 10000 /* rebuild the sky every 10 seconds */ |
| 57 | #define SKY_FOV_MDEG 80000 /* 80 degrees field of view */ |
| 58 | #include "flow.h" |
| 59 | #include "mind.h" |
| 60 | #include "night.h" |
| 61 | #include "paint.h" |
| 62 | #include "../svc/allen_code.h" |
| 63 | #include "../core/spinlock.h" |
| 64 | |
| 65 | #define TEXT_MAX 160 |
| 66 | #define LINES_MAX 5 |
| 67 | #define TW 64 |
| 68 | #define TH 32 |
| 69 | #define TILES_MAX 16384u |
| 70 | #define JOBS_MAX 1200u |
| 71 | #define FRAME_MS 16u |
| 72 | |
| 73 | #define BORN_MS (CK_NIGHT_MERGE_MS + 300u) /* Allen starts to gather */ |
| 74 | #define FORMED_MS (BORN_MS + CK_MIND_FORM_MS) |
| 75 | #define LETTER_MS 35u |
| 76 | #define SLIDE_MS 400u /* a line that grows while it is centred glides to its new place */ |
| 77 | #define BAR_REST_MS 9000u /* the bar fades this long after the last thing happened */ |
| 78 | #define DUSK_MS 2800u /* the stars fade, Allen folds to a light line, then the line closes */ |
| 79 | #define DAWN_MS 900u /* waking: the sky comes back, calmly */ |
| 80 | #define MAP_SHOW_MS 9000u |
| 81 | #define PANEL_STEP_MS 600u /* the panel's lines ease in this far apart */ |
| 82 | #define PL_MAX 96 |
| 83 | #define PC_MAX 32 |
| 84 | |
| 85 | /* the setup wizard's panel, as the keyboard side left it and as it is drawn */ |
| 86 | struct panel { |
| 87 | char line[CK_ALLEN_UI_PANEL_LINES][PL_MAX]; |
| 88 | unsigned nline; |
| 89 | char choice[CK_ALLEN_UI_CHOICES][PC_MAX]; |
| 90 | unsigned nchoice; |
| 91 | int lit; |
| 92 | char hint[PL_MAX], aside[64], prompt[48]; |
| 93 | int on; |
| 94 | }; |
| 95 | |
| 96 | struct words { |
| 97 | char text[TEXT_MAX]; |
| 98 | unsigned len, lines, st[LINES_MAX], ln[LINES_MAX]; |
| 99 | int32_t x[LINES_MAX]; /* where each line starts, px */ |
| 100 | uint32_t from; /* the first letter's start, ms */ |
| 101 | uint32_t done; /* the last letter settled */ |
| 102 | uint32_t fade; /* fading from here (0: not yet) */ |
| 103 | uint32_t at[TEXT_MAX]; /* when each letter starts, ms: more words join without the first ones starting over */ |
| 104 | int32_t x0[LINES_MAX]; /* where each line was before the last join: it slides to x over SLIDE_MS */ |
| 105 | uint32_t slide_t; |
| 106 | ck_rect box; |
| 107 | }; |
| 108 | |
| 109 | /* what the keyboard side has left for the next frame */ |
| 110 | static struct { |
| 111 | struct ck_spinlock lock; /* core/spinlock.h, _nomask: see lock() */ |
| 112 | volatile int refresh; |
| 113 | unsigned input_gen, said_gen, map_gen, wake_gen; |
| 114 | unsigned said_input_gen; /* input_gen when the last words came: keys after them stay in the bar */ |
| 115 | int sleep; |
| 116 | unsigned sleep_gen; |
| 117 | char input[TEXT_MAX]; |
| 118 | unsigned input_len; |
| 119 | char line[TEXT_MAX], reply[TEXT_MAX]; |
| 120 | int has_line; |
| 121 | int more; /* the words are the last ones with more at the end: they join, they do not start over */ |
| 122 | int think; /* the line went in but the answer is still coming: he thinks until it does */ |
| 123 | const char *footer; |
| 124 | unsigned n, ne, total, links; |
| 125 | int lit; |
| 126 | uint8_t ea[CK_ALLEN_UI_MAP_LINKS], eb[CK_ALLEN_UI_MAP_LINKS]; |
| 127 | char label[TEXT_MAX]; |
| 128 | ck_scene scene; /* what the model painted with its answer (svc/allen_scene.h) */ |
| 129 | unsigned scene_gen; |
| 130 | struct panel panel; |
| 131 | unsigned panel_gen; |
| 132 | /* code from the Spark */ |
| 133 | unsigned code_gen, code_len; |
| 134 | int code_on, working; |
| 135 | uint32_t code_top, code_rows, code_page; /* top row set by the keys; rows and page told back by the frame */ |
| 136 | char code_lang[16]; |
| 137 | } note; |
| 138 | static char note_code[CK_ALLEN_UI_CODE_MAX + 1]; /* written under the lock, copied by the frame */ |
| 139 | |
| 140 | static struct { |
| 141 | int started, on; |
| 142 | volatile uint32_t *fb; |
| 143 | int32_t w, h; |
| 144 | uint32_t pitch, format, k; |
| 145 | uint32_t *back; |
| 146 | ck_canvas cv; |
| 147 | ck_night night; |
| 148 | ck_mind mind; |
| 149 | ck_flow flow; /* the water and the ink round him */ |
| 150 | uint32_t dim; /* 0 the sky, DIM_FULL dark (1/256 steps of 1/256): how far the dusk has gone, or how much of the dawn is left */ |
| 151 | uint32_t dusk_t; /* when the exit began (ms; zero is a valid start) */ |
| 152 | void *keep; /* the frozen full-resolution scene used during the exit */ |
| 153 | int dusk_active; |
| 154 | ck_mind exit_origin, exit_mind; |
| 155 | uint8_t exit_lut[16][256]; |
| 156 | uint32_t exit_keep, exit_line_alpha; |
| 157 | int32_t exit_half; |
| 158 | uint32_t dusk_frames, dusk_slowest, dusk_ticks; /* the dusk's own frame times (microseconds) */ |
| 159 | int thinking; /* an answer is on its way from further off; he thinks until it arrives */ |
| 160 | ck_af_face voice, body, small; |
| 161 | uint64_t hz, t0; |
| 162 | uint64_t (*clock)(void); |
| 163 | uint32_t t, last; /* this frame's time and the last frame's, ms */ |
| 164 | int32_t cx, cy; |
| 165 | /* sky mode and real-time tracking */ |
| 166 | uint8_t sky_mode; /* 0=default/GW150914, 1=above_me, 2=somewhere_new */ |
| 167 | uint8_t sky_region; /* current region for somewhere_new */ |
| 168 | int32_t sky_lat_udeg; /* latitude for above_me mode */ |
| 169 | int32_t sky_lon_udeg; /* east longitude for above_me mode */ |
| 170 | int64_t sky_unix_s; /* RTC unix time at sky_tsc */ |
| 171 | uint64_t sky_tsc; /* TSC when sky_unix_s was read */ |
| 172 | uint64_t sky_view_tsc; /* TSC when the sky was last rebuilt */ |
| 173 | uint64_t tsc_now; /* TSC at this tick (from ck_allen_ui_tick argument) */ |
| 174 | /* the bar */ |
| 175 | const char *footer; |
| 176 | ck_rect bar, bar_box; |
| 177 | uint32_t bar_a, bar_was, wake_t, quiet_t; /* bar_a: 0..256; quiet_t: when things last happened */ |
| 178 | char input[TEXT_MAX]; |
| 179 | unsigned input_len; |
| 180 | uint32_t key_t; |
| 181 | unsigned seen_input, seen_said, seen_map, seen_wake; |
| 182 | int asleep; /* dark screen, nothing drawn but the mark */ |
| 183 | uint32_t black_presented; /* release-published only after a complete black frame */ |
| 184 | unsigned seen_sleep; |
| 185 | int input_moved; |
| 186 | /* the conversation */ |
| 187 | struct words now, next; /* now: on screen; next: what Allen will say */ |
| 188 | uint32_t speak_t; /* when next starts (0: nothing waiting) */ |
| 189 | char guest[TEXT_MAX]; |
| 190 | unsigned guest_len; |
| 191 | char hint[40]; /* "Speak to Allen", or whatever he is called now */ |
| 192 | unsigned hint_len; |
| 193 | int32_t guest_x; |
| 194 | uint32_t guest_t; /* when the guest's line began to rise (0: none) */ |
| 195 | uint32_t level, word_t; /* level: the voice inside Allen, 0..256 */ |
| 196 | ck_rect guest_box, words_was; |
| 197 | /* the notes constellation */ |
| 198 | unsigned n, ne, total, links; |
| 199 | int lit; |
| 200 | uint8_t ea[CK_ALLEN_UI_MAP_LINKS], eb[CK_ALLEN_UI_MAP_LINKS]; |
| 201 | char label[TEXT_MAX]; |
| 202 | unsigned label_len; |
| 203 | int32_t mx, my, mr; |
| 204 | uint32_t map_t, map_a, map_was; /* map_t: when it was last shown (0: never) */ |
| 205 | ck_rect map_box; |
| 206 | /* the setup wizard's panel */ |
| 207 | struct panel pan; |
| 208 | unsigned seen_panel; |
| 209 | uint32_t pl_t[CK_ALLEN_UI_PANEL_LINES]; /* when each line starts to ease in */ |
| 210 | uint32_t pan_a, pan_was, lit_t; |
| 211 | int lit_was; |
| 212 | ck_rect pan_box, row_box; |
| 213 | /* code from the Spark */ |
| 214 | unsigned seen_code; |
| 215 | int code_on, working; |
| 216 | uint32_t code_a, code_was, code_len, code_rows, code_top, code_page, code_cols, code_lines; |
| 217 | int32_t code_cw; /* one letter's width, 1/256 px */ |
| 218 | int32_t code_x, code_y, code_lh, code_w, code_h; /* the text area */ |
| 219 | ck_rect code_box; |
| 220 | char code_lang[16]; |
| 221 | /* the frame */ |
| 222 | uint8_t tiles[TILES_MAX]; |
| 223 | int32_t tx, ty; |
| 224 | ck_rect jobs[JOBS_MAX]; |
| 225 | volatile uint64_t ticket; /* count << 16 | next; claimed with compare-and-swap */ |
| 226 | volatile uint32_t done; |
| 227 | ck_rect flash_was, lens_was; |
| 228 | int32_t ripple_was; |
| 229 | uint32_t frames, slowest, total_ticks; |
| 230 | } ui; |
| 231 | |
| 232 | /* the code shown and its rows (svc/allen_code.h ck_code_rows) */ |
| 233 | #define CODE_ROWS_MAX 4096u |
| 234 | static char code_txt[CK_ALLEN_UI_CODE_MAX + 1]; |
| 235 | static uint32_t code_st[CODE_ROWS_MAX]; |
| 236 | static uint16_t code_ln[CODE_ROWS_MAX], code_no[CODE_ROWS_MAX]; |
| 237 | |
| 238 | /* Allen's canvas: the things the model asks to be shown round him (ui/canvas.c) */ |
| 239 | static ck_allen_canvas stage; |
| 240 | static unsigned seen_scene; |
| 241 | |
| 242 | #define S(v) ((int32_t)(((int64_t)(v) * (int64_t)ui.k + 128) / 256)) |
| 243 | #define DIM_FULL (256u << 8) |
| 244 | |
| 245 | static void relax(void) |
| 246 | { |
| 247 | #if defined(__x86_64__) |
| 248 | __builtin_ia32_pause(); |
| 249 | #elif defined(__aarch64__) |
| 250 | __asm__ volatile("yield"); |
| 251 | #endif |
| 252 | } |
| 253 | |
| 254 | /* The kernel's shared ticket lock without the interrupt mask: no interrupt handler takes the note lock, and on the |
| 255 | * iMac the drawing core that takes it keeps interrupts on for TLB-shootdown IPIs (core/spinlock.h, _nomask). */ |
| 256 | static void lock(void) { ck_spin_lock_nomask(¬e.lock); } |
| 257 | static void unlock(void) { ck_spin_unlock_nomask(¬e.lock); } |
| 258 | |
| 259 | static uint32_t ms(uint64_t now) { return (uint32_t)((now - ui.t0) * 1000u / ui.hz); } |
| 260 | |
| 261 | static unsigned copy_len(const char *s) |
| 262 | { |
| 263 | unsigned n = 0; |
| 264 | while (s && s[n]) |
| 265 | n++; |
| 266 | return n; |
| 267 | } |
| 268 | |
| 269 | static unsigned copy(char *d, const char *s, unsigned max) |
| 270 | { |
| 271 | unsigned n = 0; |
| 272 | while (s && n < max - 1 && s[n]) |
| 273 | d[n] = s[n], n++; |
| 274 | d[n] = 0; |
| 275 | return n; |
| 276 | } |
| 277 | |
| 278 | /* ---- text ---- */ |
| 279 | |
| 280 | /* Word-wraps s (n letters) to width px with face f: line starts and lengths. */ |
| 281 | static unsigned wrap(ck_af_face *f, const char *s, unsigned n, int32_t width, unsigned *st, unsigned *ln) |
| 282 | { |
| 283 | unsigned lines = 0, i = 0; |
| 284 | while (i < n && lines < LINES_MAX) { |
| 285 | while (i < n && s[i] == ' ') |
| 286 | i++; |
| 287 | unsigned start = i, last_space = 0, j = i; |
| 288 | int32_t w = 0; |
| 289 | while (j < n) { |
| 290 | int32_t aw = (int32_t)((ck_af_advance(f, s[j]) + 255) / 256); |
| 291 | if (w + aw > width && j > start) |
| 292 | break; |
| 293 | if (s[j] == ' ') |
| 294 | last_space = j; |
| 295 | w += aw; |
| 296 | j++; |
| 297 | } |
| 298 | if (j < n && last_space > start) |
| 299 | j = last_space; |
| 300 | st[lines] = start; |
| 301 | ln[lines] = j - start; |
| 302 | lines++; |
| 303 | i = j; |
| 304 | } |
| 305 | return lines; |
| 306 | } |
| 307 | |
| 308 | static int32_t text(ck_canvas *cv, const ck_rect *cl, ck_af_face *f, int32_t x, int32_t y, const char *s, unsigned n, |
| 309 | uint32_t color, uint32_t a) |
| 310 | { |
| 311 | uint64_t pen = (uint64_t)x * 256; |
| 312 | for (unsigned i = 0; i < n; i++) |
| 313 | pen = ck_letter(cv, cl, f, pen, y, s[i], color, a, 0); |
| 314 | return (int32_t)((pen + 255) / 256); |
| 315 | } |
| 316 | |
| 317 | static int32_t line_h(void) { return S(40); } |
| 318 | static int32_t words_top(void) { return ui.cy + S(250); } |
| 319 | |
| 320 | static void lay_out(struct words *w) |
| 321 | { |
| 322 | w->lines = wrap(&ui.voice, w->text, w->len, ui.code_on ? S(520) : S(900), w->st, w->ln); |
| 323 | int32_t top = words_top(), x0 = ui.w, x1 = 0; |
| 324 | for (unsigned i = 0; i < w->lines; i++) { |
| 325 | int32_t lw = (int32_t)ck_af_measure(&ui.voice, w->text + w->st[i], w->ln[i]); |
| 326 | w->x[i] = (ui.w - lw) / 2; |
| 327 | x0 = ck_min(x0, w->x[i]), x1 = ck_max(x1, w->x[i] + lw); |
| 328 | } |
| 329 | w->box = w->lines ? (ck_rect){x0 - S(8), top - S(34), x1 - x0 + S(16), (int32_t)w->lines * line_h() + S(30)} |
| 330 | : (ck_rect){0, 0, 0, 0}; |
| 331 | unsigned last = w->lines ? w->st[w->lines - 1] + w->ln[w->lines - 1] : 0; |
| 332 | w->done = (last ? w->at[last - 1] + LETTER_MS : w->from) + 700; |
| 333 | } |
| 334 | |
| 335 | /* where line li starts at t: a line that grew since the last join glides from where it was */ |
| 336 | static int32_t line_x(const struct words *w, unsigned li, uint32_t t) |
| 337 | { |
| 338 | int32_t k = 256 - (int32_t)ck_smooth(ck_prog(t, w->slide_t, w->slide_t + SLIDE_MS)); |
| 339 | return w->x[li] + (int32_t)((int64_t)(w->x0[li] - w->x[li]) * k >> 8); |
| 340 | } |
| 341 | |
| 342 | /* the letters from i on start one after another from t0 */ |
| 343 | static void time_letters(struct words *w, unsigned i, uint32_t t0) |
| 344 | { |
| 345 | for (; i < w->len; i++) |
| 346 | w->at[i] = t0, t0 += LETTER_MS; |
| 347 | } |
| 348 | |
| 349 | /* More of the words already up (or about to be). 0: these are not those words, |
| 350 | * and the caller starts them over. */ |
| 351 | static int join_words(const char *reply, uint32_t t) |
| 352 | { |
| 353 | struct words *w = ui.speak_t ? &ui.next : &ui.now; |
| 354 | unsigned n = copy_len(reply); |
| 355 | if (!w->len || (w == &ui.now && w->fade) || n < w->len || n >= TEXT_MAX) |
| 356 | return 0; |
| 357 | for (unsigned i = 0; i < w->len; i++) |
| 358 | if (w->text[i] != reply[i]) |
| 359 | return 0; |
| 360 | if (w == &ui.next) { /* still waiting to speak: it speaks all of it when it does */ |
| 361 | w->len = copy(w->text, reply, TEXT_MAX); |
| 362 | return 1; |
| 363 | } |
| 364 | unsigned old = w->len, old_lines = w->lines; |
| 365 | uint32_t start = (uint32_t)ck_max((int32_t)t, (int32_t)(w->at[old - 1] + LETTER_MS)); |
| 366 | int32_t was[LINES_MAX]; |
| 367 | for (unsigned i = 0; i < old_lines; i++) |
| 368 | was[i] = line_x(w, i, t); |
| 369 | w->len = copy(w->text, reply, TEXT_MAX); |
| 370 | time_letters(w, old, start); |
| 371 | lay_out(w); |
| 372 | for (unsigned i = 0; i < LINES_MAX; i++) |
| 373 | w->x0[i] = i < old_lines ? was[i] : w->x[i]; |
| 374 | w->slide_t = t; |
| 375 | for (unsigned i = 0; i < old_lines && i < w->lines; i++) { /* a gliding line draws all the way from x0 */ |
| 376 | int32_t lw = (int32_t)ck_af_measure(&ui.voice, w->text + w->st[i], w->ln[i]); |
| 377 | w->box = ck_union(w->box, (ck_rect){w->x0[i] - S(8), w->box.y, lw + S(16), w->box.h}); |
| 378 | } |
| 379 | if (ui.mind.mode != CK_MIND_SPEAK) |
| 380 | ck_mind_mode(&ui.mind, CK_MIND_SPEAK, t); |
| 381 | return 1; |
| 382 | } |
| 383 | |
| 384 | /* how long words stay once said: time to read them */ |
| 385 | static uint32_t read_ms(const struct words *w) { return 6000u + w->len * 70u; } |
| 386 | |
| 387 | static uint32_t words_alpha(const struct words *w, uint32_t t) |
| 388 | { |
| 389 | if (!w->len) |
| 390 | return 0; |
| 391 | if (ui.pan.on && !w->fade) /* a question is up: his words stay with it */ |
| 392 | return 256; |
| 393 | uint32_t out = w->fade ? w->fade : w->done + read_ms(w); |
| 394 | return 256 - ck_smooth(ck_prog(t, out, out + 450)); |
| 395 | } |
| 396 | |
| 397 | static void draw_words(const struct words *w, const ck_rect *r, uint32_t t) |
| 398 | { |
| 399 | uint32_t all = words_alpha(w, t); |
| 400 | if (!all || !ck_meets(w->box, r)) |
| 401 | return; |
| 402 | int32_t top = words_top(); |
| 403 | for (unsigned li = 0; li < w->lines; li++) { |
| 404 | uint64_t pen = (uint64_t)line_x(w, li, t) * 256; |
| 405 | int32_t y = top + (int32_t)li * line_h(); |
| 406 | for (unsigned j = 0; j < w->ln[li]; j++) { |
| 407 | unsigned i = w->st[li] + j; |
| 408 | uint32_t at = w->at[i]; |
| 409 | if (t <= at) /* not yet: but keep the pen moving so the line stays centred */ |
| 410 | break; |
| 411 | uint32_t age = t - at, a = ck_smooth(ck_prog(age, 0, 260)); |
| 412 | uint32_t c = ck_mix(CK_RGB(255, 212, 150), CK_RGB(234, 238, 252), ck_smooth(ck_prog(age, 0, 900))); |
| 413 | int32_t rise = (int32_t)((int64_t)S(6) * (256 - ck_ease_out(ck_prog(age, 0, 360))) >> 8); |
| 414 | pen = ck_letter(&ui.cv, r, &ui.voice, pen, y + rise, w->text[i], c, a * all >> 8, 0); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* ---- the bar ---- */ |
| 420 | |
| 421 | static void draw_bar(const ck_rect *r, uint32_t t) |
| 422 | { |
| 423 | uint32_t A = ui.bar_a; |
| 424 | const char *hint = ui.pan.on && ui.pan.prompt[0] ? ui.pan.prompt : ui.hint; |
| 425 | unsigned hint_len = hint == ui.hint ? ui.hint_len : (unsigned)copy_len(hint); |
| 426 | if (!A || !ck_meets(ui.bar_box, r)) |
| 427 | return; |
| 428 | ck_rect b = ui.bar, d = ck_grow(b, S(14)); |
| 429 | if (!ck_clip(&d, r)) |
| 430 | d.w = 0; |
| 431 | int32_t rad = b.h / 2, typing = ui.input_len > 0; |
| 432 | for (int32_t y = d.y; d.w && y < d.y + d.h; y++) { |
| 433 | uint32_t *row = ui.back + (size_t)y * (size_t)ui.w; |
| 434 | for (int32_t x = d.x; x < d.x + d.w; x++) { |
| 435 | int32_t sd = ck_sd_rrect(x, y, &b, rad); |
| 436 | uint32_t in = ck_cover(sd); |
| 437 | if (in) |
| 438 | row[x] = ck_mix(row[x], CK_RGB(6, 8, 18), (in * 150 >> 8) * A >> 8); |
| 439 | int32_t edge = ck_abs(sd + 8); |
| 440 | if (edge < 16) |
| 441 | row[x] = ck_add(row[x], CK_RGB(255, 255, 255), (uint32_t)(16 - edge) * (typing ? 4u : 3u) * A >> 8); |
| 442 | if (typing && sd > 0 && sd < S(14) * 16) { /* a soft blue glow round it while typing */ |
| 443 | uint32_t g = 256 - (uint32_t)(sd * 256 / (S(14) * 16)); |
| 444 | row[x] = ck_add(row[x], CK_RGB(110, 130, 255), ((g * g >> 8) * 34 >> 8) * A >> 8); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | int32_t pad = S(30), base = b.y + b.h / 2 + S(7); |
| 449 | ck_rect tc = {b.x + pad, b.y, b.w - 2 * pad, b.h}; |
| 450 | if (!ck_clip(&tc, r)) |
| 451 | tc.w = 0; |
| 452 | int32_t x = b.x + pad; |
| 453 | if (!ui.input_len) { |
| 454 | if (tc.w) |
| 455 | text(&ui.cv, &tc, &ui.body, x, base, hint, hint_len, CK_RGB(150, 160, 196), 192 * A >> 8); |
| 456 | } else { |
| 457 | int32_t avail = b.w - 2 * pad - S(12); |
| 458 | unsigned from = 0; |
| 459 | while (from < ui.input_len && |
| 460 | (int32_t)ck_af_measure(&ui.body, ui.input + from, ui.input_len - from) > avail) |
| 461 | from++; |
| 462 | if (tc.w) |
| 463 | x = text(&ui.cv, &tc, &ui.body, x, base, ui.input + from, ui.input_len - from, CK_RGB(236, 240, 252), |
| 464 | A); |
| 465 | else |
| 466 | x += (int32_t)ck_af_measure(&ui.body, ui.input + from, ui.input_len - from); |
| 467 | } |
| 468 | /* the caret breathes rather than blinks */ |
| 469 | uint32_t ca = (uint32_t)(16384 + ck_sin((t - ui.key_t) * 65536u / 1100u + 16384u)) * 200 >> 15; |
| 470 | if (t - ui.key_t < 500) |
| 471 | ca = 200; |
| 472 | ck_rect caret = {x + S(3), b.y + b.h / 2 - S(12), ck_max(S(2), 2), S(24)}; |
| 473 | if (ck_clip(&caret, r)) |
| 474 | for (int32_t y = caret.y; y < caret.y + caret.h; y++) |
| 475 | for (int32_t xx = caret.x; xx < caret.x + caret.w; xx++) { |
| 476 | uint32_t *p = ui.back + (size_t)y * (size_t)ui.w + xx; |
| 477 | *p = ck_add(*p, CK_RGB(150, 170, 255), (40 + ca) * A >> 8); |
| 478 | } |
| 479 | if (ui.footer && !ui.input_len && !ui.pan.on) { |
| 480 | unsigned n = 0; |
| 481 | while (ui.footer[n]) |
| 482 | n++; |
| 483 | int32_t fw = (int32_t)ck_af_measure(&ui.small, ui.footer, n); |
| 484 | text(&ui.cv, r, &ui.small, (ui.w - fw) / 2, b.y + b.h + S(34), ui.footer, n, CK_RGB(130, 140, 176), |
| 485 | 222 * A >> 8); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /* ---- the setup wizard's panel ---- */ |
| 490 | |
| 491 | static int32_t pan_line_y(unsigned i) { return words_top() + S(76) + (int32_t)i * S(31); } |
| 492 | static int choices_up(void) { return ui.pan.on && ui.pan.nchoice && !ui.pan.prompt[0]; } |
| 493 | |
| 494 | /* one choice: a soft pill, lit (l 0..256) from a faint outline to a bright, blue-glowing one */ |
| 495 | static void draw_pill(const ck_rect *r, const ck_rect *p, const char *s, unsigned n, uint32_t l, uint32_t A) |
| 496 | { |
| 497 | ck_rect d = ck_grow(*p, S(16)); |
| 498 | if (!ck_clip(&d, r)) |
| 499 | return; |
| 500 | int32_t rad = p->h / 2; |
| 501 | for (int32_t y = d.y; y < d.y + d.h; y++) { |
| 502 | uint32_t *row = ui.back + (size_t)y * (size_t)ui.w; |
| 503 | for (int32_t x = d.x; x < d.x + d.w; x++) { |
| 504 | int32_t sd = ck_sd_rrect(x, y, p, rad); |
| 505 | uint32_t in = ck_cover(sd); |
| 506 | if (in) { |
| 507 | row[x] = ck_mix(row[x], CK_RGB(6, 8, 18), (in * 140 >> 8) * A >> 8); |
| 508 | row[x] = ck_add(row[x], CK_RGB(70, 90, 210), (in * (40 * l >> 8) >> 8) * A >> 8); |
| 509 | } |
| 510 | int32_t edge = ck_abs(sd + 8); |
| 511 | if (edge < 16) |
| 512 | row[x] = ck_add(row[x], CK_RGB(255, 255, 255), (uint32_t)(16 - edge) * (2u + (4u * l >> 8)) * A >> 8); |
| 513 | if (l && sd > 0 && sd < S(16) * 16) { |
| 514 | uint32_t g = 256 - (uint32_t)(sd * 256 / (S(16) * 16)); |
| 515 | row[x] = ck_add(row[x], CK_RGB(110, 130, 255), ((g * g >> 8) * 40 >> 8) * l >> 8); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | int32_t tw = (int32_t)ck_af_measure(&ui.body, s, n); |
| 520 | uint32_t c = ck_mix(CK_RGB(160, 168, 204), CK_RGB(244, 246, 255), l); |
| 521 | text(&ui.cv, r, &ui.body, p->x + (p->w - tw) / 2, p->y + p->h / 2 + S(7), s, n, c, (184 + (72 * l >> 8)) * A >> 8); |
| 522 | } |
| 523 | |
| 524 | static void draw_panel(const ck_rect *r, uint32_t t) |
| 525 | { |
| 526 | uint32_t A = ui.pan_a; |
| 527 | if (!A) |
| 528 | return; |
| 529 | if (ck_meets(ui.pan_box, r)) |
| 530 | for (unsigned i = 0; i < ui.pan.nline; i++) { |
| 531 | if (t <= ui.pl_t[i]) |
| 532 | continue; |
| 533 | uint32_t age = t - ui.pl_t[i], a = ck_smooth(ck_prog(age, 0, 420)); |
| 534 | int32_t rise = (int32_t)((int64_t)S(6) * (256 - ck_ease_out(ck_prog(age, 0, 520))) >> 8); |
| 535 | unsigned n = copy_len(ui.pan.line[i]); |
| 536 | int32_t lw = (int32_t)ck_af_measure(&ui.body, ui.pan.line[i], n); |
| 537 | text(&ui.cv, r, &ui.body, (ui.w - lw) / 2, pan_line_y(i) + rise, ui.pan.line[i], n, CK_RGB(206, 212, 238), |
| 538 | (a * 230 >> 8) * A >> 8); |
| 539 | } |
| 540 | if (!choices_up() || !ck_meets(ui.row_box, r)) |
| 541 | return; |
| 542 | /* the row: centred where the bar sits */ |
| 543 | int32_t pad = S(28), gap = S(20), h = ui.bar.h - S(8), total = 0, wd[CK_ALLEN_UI_CHOICES]; |
| 544 | for (unsigned i = 0; i < ui.pan.nchoice; i++) { |
| 545 | wd[i] = (int32_t)ck_af_measure(&ui.body, ui.pan.choice[i], copy_len(ui.pan.choice[i])) + 2 * pad; |
| 546 | total += wd[i] + (i ? gap : 0); |
| 547 | } |
| 548 | int32_t x = (ui.w - total) / 2, y = ui.bar.y + (ui.bar.h - h) / 2; |
| 549 | uint32_t ease = ck_smooth(ck_prog(t, ui.lit_t, ui.lit_t + 220)); |
| 550 | for (unsigned i = 0; i < ui.pan.nchoice; i++) { |
| 551 | uint32_t l = (int)i == ui.pan.lit ? ease : (int)i == ui.lit_was ? 256 - ease : 0; |
| 552 | ck_rect p = {x, y, wd[i], h}; |
| 553 | draw_pill(r, &p, ui.pan.choice[i], copy_len(ui.pan.choice[i]), l, A); |
| 554 | x += wd[i] + gap; |
| 555 | } |
| 556 | if (ui.pan.hint[0]) { |
| 557 | unsigned n = copy_len(ui.pan.hint); |
| 558 | int32_t hw = (int32_t)ck_af_measure(&ui.small, ui.pan.hint, n); |
| 559 | text(&ui.cv, r, &ui.small, (ui.w - hw) / 2, ui.bar.y + ui.bar.h + S(34), ui.pan.hint, n, CK_RGB(170, 178, 210), |
| 560 | 184 * A >> 8); |
| 561 | } |
| 562 | if (ui.pan.aside[0]) { |
| 563 | unsigned n = copy_len(ui.pan.aside); |
| 564 | int32_t aw = (int32_t)ck_af_measure(&ui.small, ui.pan.aside, n); |
| 565 | text(&ui.cv, r, &ui.small, (ui.w - aw) / 2, ui.bar.y + ui.bar.h + S(64), ui.pan.aside, n, CK_RGB(130, 140, 176), |
| 566 | 222 * A >> 8); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* ---- code from the Spark ---- */ |
| 571 | |
| 572 | /* one letter centred in its cell, so every letter takes one width */ |
| 573 | static void code_letters(const ck_rect *r, int32_t x, int32_t y, const char *s, unsigned n, uint32_t color, uint32_t a) |
| 574 | { |
| 575 | for (unsigned i = 0; i < n; i++) { |
| 576 | char ch = s[i]; |
| 577 | if (ch == ' ') |
| 578 | continue; |
| 579 | int64_t cell = (int64_t)x * 256 + (int64_t)i * ui.code_cw; |
| 580 | int64_t pen = cell + (ui.code_cw - (int32_t)ck_af_advance(&ui.small, ch)) / 2; |
| 581 | (void)ck_letter(&ui.cv, r, &ui.small, (uint64_t)(pen < 0 ? 0 : pen), y, ch, color, a, 0); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static void draw_code(const ck_rect *r) |
| 586 | { |
| 587 | uint32_t A = ui.code_a; |
| 588 | if (!A || !ck_meets(ui.code_box, r)) |
| 589 | return; |
| 590 | /* a soft pool of shade under the text, fading out over the margin: no edge, no box */ |
| 591 | ck_rect in = {ui.code_x - S(36), ui.code_y - S(56), ui.code_w + S(56), ui.code_h + S(100)}; |
| 592 | int32_t m = S(48); |
| 593 | ck_rect d = ck_grow(in, m); |
| 594 | if (ck_clip(&d, r)) |
| 595 | for (int32_t y = d.y; y < d.y + d.h; y++) { |
| 596 | uint32_t *row = ui.back + (size_t)y * (size_t)ui.w; |
| 597 | int32_t dy = y < in.y ? in.y - y : y >= in.y + in.h ? y - (in.y + in.h - 1) : 0; |
| 598 | for (int32_t x = d.x; x < d.x + d.w; x++) { |
| 599 | int32_t dx = x < in.x ? in.x - x : x >= in.x + in.w ? x - (in.x + in.w - 1) : 0; |
| 600 | int32_t far = dx > dy ? dx + dy / 2 : dy + dx / 2; |
| 601 | if (far >= m) |
| 602 | continue; |
| 603 | uint32_t k = 256 - ck_smooth(ck_prog(far, 0, m)); |
| 604 | row[x] = ck_mix(row[x], CK_RGB(6, 8, 18), (k * 212 >> 8) * A >> 8); |
| 605 | } |
| 606 | } |
| 607 | /* the head: the language and the size, in the hint colour */ |
| 608 | char head[64]; |
| 609 | unsigned hn = 0; |
| 610 | const char *lang = ui.code_lang[0] ? ui.code_lang : "code"; |
| 611 | for (unsigned i = 0; lang[i] && hn < 20; i++) |
| 612 | head[hn++] = lang[i]; |
| 613 | const char *mid = ", "; |
| 614 | for (unsigned i = 0; mid[i]; i++) |
| 615 | head[hn++] = mid[i]; |
| 616 | char num[12]; |
| 617 | unsigned nn = 0; |
| 618 | uint32_t v = ui.code_lines; |
| 619 | do |
| 620 | num[nn++] = (char)('0' + v % 10); |
| 621 | while ((v /= 10) && nn < sizeof num); |
| 622 | while (nn) |
| 623 | head[hn++] = num[--nn]; |
| 624 | const char *tail = ui.code_lines == 1 ? " line" : " lines"; |
| 625 | for (unsigned i = 0; tail[i]; i++) |
| 626 | head[hn++] = tail[i]; |
| 627 | text(&ui.cv, r, &ui.small, ui.code_x, ui.code_y - S(22), head, hn, CK_RGB(150, 160, 196), 192 * A >> 8); |
| 628 | /* the rows: the line number at the left of each line's first row, the code after it */ |
| 629 | int32_t num_w = 5 * ui.code_cw / 256; |
| 630 | for (uint32_t i = 0; i < ui.code_page && ui.code_top + i < ui.code_rows; i++) { |
| 631 | uint32_t k = ui.code_top + i; |
| 632 | int32_t base = ui.code_y + (int32_t)i * ui.code_lh + ui.code_lh - S(5); |
| 633 | ck_rect line_box = {ui.code_x - S(4), base - ui.code_lh, ui.code_w + S(8), ui.code_lh + S(6)}; |
| 634 | if (!ck_meets(line_box, r)) |
| 635 | continue; |
| 636 | if (code_no[k]) { |
| 637 | char ln[6]; |
| 638 | unsigned n = 0; |
| 639 | uint32_t no = code_no[k]; |
| 640 | do |
| 641 | ln[n++] = (char)('0' + no % 10); |
| 642 | while ((no /= 10) && n < 5); |
| 643 | char rev[6]; |
| 644 | for (unsigned j = 0; j < n; j++) |
| 645 | rev[j] = ln[n - 1 - j]; |
| 646 | code_letters(r, ui.code_x + (int32_t)(4 - n) * ui.code_cw / 256, base, rev, n, CK_RGB(130, 140, 176), |
| 647 | 222 * A >> 8); |
| 648 | } |
| 649 | code_letters(r, ui.code_x + num_w, base, code_txt + code_st[k], code_ln[k], CK_RGB(236, 240, 252), |
| 650 | 236 * A >> 8); |
| 651 | } |
| 652 | /* where the page sits in the whole: one thread of light down the left side, brighter for the rows shown */ |
| 653 | int32_t tx = ui.code_x - S(22), th = ui.code_h; |
| 654 | uint32_t rows = ui.code_rows ? ui.code_rows : 1; |
| 655 | int32_t y0 = ui.code_y + (int32_t)((int64_t)th * ui.code_top / rows); |
| 656 | int32_t y1 = ui.code_y + (int32_t)((int64_t)th * ck_min((int32_t)(ui.code_top + ui.code_page), (int32_t)rows) / rows); |
| 657 | ck_rect tr = {tx - S(1), ui.code_y, ck_max(S(2), 2), th}; |
| 658 | if (ck_clip(&tr, r)) |
| 659 | for (int32_t y = tr.y; y < tr.y + tr.h; y++) { |
| 660 | uint32_t *row = ui.back + (size_t)y * (size_t)ui.w; |
| 661 | uint32_t a = y >= y0 && y < y1 ? 150u : 40u; |
| 662 | for (int32_t x = tr.x; x < tr.x + tr.w; x++) |
| 663 | row[x] = ck_add(row[x], CK_RGB(150, 170, 255), a * A >> 8); |
| 664 | } |
| 665 | /* how to read it, quietly under it */ |
| 666 | static const char help[] = "Up, Down, Page Up, Page Down to read. Escape puts it away."; |
| 667 | text(&ui.cv, r, &ui.small, ui.code_x, ui.code_y + ui.code_h + S(30), help, sizeof help - 1, CK_RGB(130, 140, 176), |
| 668 | 222 * A >> 8); |
| 669 | } |
| 670 | |
| 671 | /* ---- the guest's line rising into Allen, and the thoughts it becomes ---- */ |
| 672 | |
| 673 | static void curve(unsigned i, uint32_t t, int32_t *x, int32_t *y, uint32_t *a) |
| 674 | { |
| 675 | /* thought i leaves the line at its own place and flies on a curve to the heart */ |
| 676 | uint32_t start = ui.guest_t + 150 + i * 40, dur = 850 + ck_hash((int32_t)i, 1, 5) % 300; |
| 677 | int32_t p = ck_prog(t, start, start + dur); |
| 678 | *a = t < start || p >= 256 ? 0 : 230; |
| 679 | int32_t gw = (int32_t)ck_af_measure(&ui.body, ui.guest, ui.guest_len); |
| 680 | int32_t x0 = (ui.guest_x + gw * (int32_t)(2 * i + 1) / 18) * 16, y0 = (ui.bar.y + ui.bar.h / 2 - S(60)) * 16; |
| 681 | int32_t x2 = ui.mind.hx, y2 = ui.mind.hy; |
| 682 | int32_t side = (int32_t)(ck_hash((int32_t)i, 2, 5) % 2u) ? 1 : -1; |
| 683 | int32_t x1 = (x0 + x2) / 2 + side * S(160 + (int32_t)(ck_hash((int32_t)i, 3, 5) % 140u)) * 16, y1 = (y0 + y2) / 2; |
| 684 | uint32_t e = ck_smooth(p), f = 256 - e; |
| 685 | *x = (int32_t)(((int64_t)x0 * f * f + 2 * (int64_t)x1 * f * e + (int64_t)x2 * e * e) >> 16); |
| 686 | *y = (int32_t)(((int64_t)y0 * f * f + 2 * (int64_t)y1 * f * e + (int64_t)y2 * e * e) >> 16); |
| 687 | if (p > 200) |
| 688 | *a = *a * (uint32_t)(256 - p) / 56; |
| 689 | } |
| 690 | |
| 691 | static ck_rect guest_area(void) |
| 692 | { |
| 693 | if (!ui.guest_t) |
| 694 | return (ck_rect){0, 0, 0, 0}; |
| 695 | int32_t y0 = ui.cy - S(40), y1 = ui.bar.y + ui.bar.h; |
| 696 | return (ck_rect){ui.w / 2 - S(720), y0, S(1440), y1 - y0}; |
| 697 | } |
| 698 | |
| 699 | static void draw_guest(const ck_rect *r, uint32_t t) |
| 700 | { |
| 701 | if (!ui.guest_t || t - ui.guest_t > 2400 || !ck_meets(ui.guest_box, r)) |
| 702 | return; |
| 703 | uint32_t p = (uint32_t)ck_prog(t, ui.guest_t, ui.guest_t + 900); |
| 704 | if (p < 256) { |
| 705 | int32_t y = ui.bar.y + ui.bar.h / 2 + S(7) - (int32_t)((int64_t)S(90) * ck_ease_out((int32_t)p) >> 8); |
| 706 | text(&ui.cv, r, &ui.body, ui.guest_x, y, ui.guest, ui.guest_len, CK_RGB(196, 208, 255), (256 - p) * 210 >> 8); |
| 707 | } |
| 708 | for (unsigned i = 0; i < 9; i++) { |
| 709 | int32_t x, y; |
| 710 | uint32_t a; |
| 711 | curve(i, t, &x, &y, &a); |
| 712 | if (!a) |
| 713 | continue; |
| 714 | ck_glow(&ui.cv, r, x, y, S(9) * 16, CK_RGB(150, 170, 255), a / 4); |
| 715 | ck_glow(&ui.cv, r, x, y, S(3) * 16, CK_RGB(255, 236, 210), a); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /* ---- the notes constellation ---- */ |
| 720 | |
| 721 | static unsigned put_num(char *s, unsigned v) |
| 722 | { |
| 723 | char t[10]; |
| 724 | unsigned n = 0, k = 0; |
| 725 | do |
| 726 | t[n++] = (char)('0' + v % 10); |
| 727 | while ((v /= 10) && n < sizeof t); |
| 728 | while (n) |
| 729 | s[k++] = t[--n]; |
| 730 | return k; |
| 731 | } |
| 732 | |
| 733 | /* where star j sits, 16ths of a pixel: the newest at the centre, the rest on |
| 734 | * a golden-angle spiral spread by the square root of the order */ |
| 735 | static void map_point(unsigned j, int32_t *x, int32_t *y) |
| 736 | { |
| 737 | unsigned spread = ui.n > 7 ? ui.n - 1 : 6; |
| 738 | int64_t rad = (int64_t)ui.mr * 16 * (int64_t)ck_isqrt((uint64_t)j * 65536u / spread) / 256; |
| 739 | uint32_t ang = j * 25033u; |
| 740 | *x = ui.mx * 16 + 8 + (int32_t)(rad * ck_cos(ang) >> 14); |
| 741 | *y = ui.my * 16 + 8 + (int32_t)(rad * ck_sin(ang) >> 14); |
| 742 | } |
| 743 | |
| 744 | static void draw_map(const ck_rect *r, uint32_t t) |
| 745 | { |
| 746 | uint32_t A = ui.map_a; |
| 747 | if (!A || !ui.n || !ck_meets(ui.map_box, r)) |
| 748 | return; |
| 749 | for (unsigned i = 0; i < ui.ne; i++) { |
| 750 | int32_t x0, y0, x1, y1; |
| 751 | map_point(ui.ea[i], &x0, &y0); |
| 752 | map_point(ui.eb[i], &x1, &y1); |
| 753 | ck_thread(&ui.cv, r, x0, y0, x1, y1, (int32_t)ui.k / 16, CK_RGB(130, 150, 255), 70 * A >> 8, 70 * A >> 8); |
| 754 | } |
| 755 | for (unsigned j = 0; j < ui.n; j++) { |
| 756 | int32_t x, y; |
| 757 | map_point(j, &x, &y); |
| 758 | int lit = (int)j == ui.lit; |
| 759 | uint32_t pulse = lit ? (uint32_t)(16384 + ck_sin((t - ui.map_t) * 65536u / 1400u)) * 60 >> 15 : 0; |
| 760 | uint32_t c = lit ? CK_RGB(255, 214, 160) : CK_RGB(206, 220, 255); |
| 761 | ck_glow(&ui.cv, r, x, y, S(lit ? 16 : 8) * 16, c, (lit ? 70 + pulse : 30) * A >> 8); |
| 762 | ck_glow(&ui.cv, r, x, y, S(lit ? 4 : 3) * 16, c, (lit ? 255 : 190) * A >> 8); |
| 763 | } |
| 764 | /* the spark that carried the note out from Allen */ |
| 765 | if (ui.lit >= 0 && t - ui.map_t < 1100) { |
| 766 | int32_t lx, ly; |
| 767 | map_point((unsigned)ui.lit, &lx, &ly); |
| 768 | int32_t p = ck_prog(t, ui.map_t, ui.map_t + 900); |
| 769 | for (int k = 0; k < 4 && p < 256; k++) { |
| 770 | uint32_t e = ck_smooth(ck_clamp(p - k * 18, 0, 256)); |
| 771 | int32_t x = ui.mind.hx + (int32_t)((int64_t)(lx - ui.mind.hx) * e >> 8); |
| 772 | int32_t y = ui.mind.hy + (int32_t)((int64_t)(ly - ui.mind.hy) * e >> 8) - |
| 773 | (int32_t)((int64_t)S(60) * 16 * (e * (256 - e) >> 6) >> 8); |
| 774 | ck_glow(&ui.cv, r, x, y, S(5) * 16, CK_RGB(255, 222, 176), 220u >> k); |
| 775 | } |
| 776 | } |
| 777 | char line[48]; |
| 778 | unsigned n = put_num(line, ui.total); |
| 779 | n += copy(line + n, ui.total == 1 ? " note, " : " notes, ", 16); |
| 780 | n += put_num(line + n, ui.links); |
| 781 | n += copy(line + n, ui.links == 1 ? " link" : " links", 16); |
| 782 | int32_t hw = (int32_t)ck_af_measure(&ui.small, "Notes", 5); |
| 783 | text(&ui.cv, r, &ui.small, ui.mx - hw / 2, ui.my - ui.mr - S(34), "Notes", 5, CK_RGB(170, 180, 214), 184 * A >> 8); |
| 784 | int32_t lw = (int32_t)ck_af_measure(&ui.small, line, n); |
| 785 | text(&ui.cv, r, &ui.small, ui.mx - lw / 2, ui.my + ui.mr + S(40), line, n, CK_RGB(150, 160, 196), 192 * A >> 8); |
| 786 | if (ui.lit >= 0 && ui.label_len) { |
| 787 | char cut[TEXT_MAX + 4]; |
| 788 | unsigned m = ui.label_len; |
| 789 | int32_t room = S(300); |
| 790 | while (m > 0 && (int32_t)ck_af_measure(&ui.small, ui.label, m) + (m < ui.label_len ? S(14) : 0) > room) |
| 791 | m--; |
| 792 | for (unsigned i = 0; i < m; i++) |
| 793 | cut[i] = ui.label[i]; |
| 794 | if (m < ui.label_len) |
| 795 | cut[m++] = '.', cut[m++] = '.', cut[m++] = '.'; |
| 796 | int32_t cw = (int32_t)ck_af_measure(&ui.small, cut, m); |
| 797 | text(&ui.cv, r, &ui.small, ui.mx - cw / 2, ui.my + ui.mr + S(68), cut, m, CK_RGB(255, 222, 180), 200 * A >> 8); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /* ---- the flash of the merger ---- */ |
| 802 | |
| 803 | static int flash(uint32_t t, int32_t *rad, uint32_t *a) |
| 804 | { |
| 805 | if (t < CK_NIGHT_MERGE_MS || t >= CK_NIGHT_MERGE_MS + 1600) |
| 806 | return 0; |
| 807 | uint32_t tm = t - CK_NIGHT_MERGE_MS; |
| 808 | *rad = (S(40) + (int32_t)((int64_t)S(220) * ck_ease_out(ck_prog(tm, 0, 1200)) >> 8)) * 16; |
| 809 | uint32_t rise = ck_smooth(ck_prog(tm, 0, 120)); |
| 810 | *a = rise * (ck_exp_neg(tm > 120 ? (tm - 120) * 256 / 450 : 0) >> 8) * 230 >> 16; |
| 811 | return *a > 0; |
| 812 | } |
| 813 | |
| 814 | /* The flash fills everything at its height; then the remaining hole's shadow |
| 815 | * takes its black back (within 300 ms), so the light rings it instead of |
| 816 | * filling it. */ |
| 817 | static void unflash(const ck_rect *r, uint32_t t) |
| 818 | { |
| 819 | uint32_t tm = t - CK_NIGHT_MERGE_MS; |
| 820 | if (ui.night.holes != 1 || tm < 100) |
| 821 | return; |
| 822 | uint32_t keep = 256 - ck_smooth(ck_prog(tm, 100, 400)); /* of the flash, inside the shadow */ |
| 823 | int32_t hx = ui.night.hole[0].x, hy = ui.night.hole[0].y, rs = ui.night.hole[0].rs; |
| 824 | ck_rect box = {(hx - rs) >> 4, (hy - rs) >> 4, (2 * rs >> 4) + 2, (2 * rs >> 4) + 2}; |
| 825 | if (rs <= 16 || !ck_clip(&box, r)) |
| 826 | return; |
| 827 | for (int32_t y = box.y; y < box.y + box.h; y++) |
| 828 | for (int32_t x = box.x; x < box.x + box.w; x++) { |
| 829 | int32_t dx = x * 16 + 8 - hx, dy = y * 16 + 8 - hy; |
| 830 | uint32_t d = ck_isqrt((uint32_t)(dx * dx + dy * dy)); /* 16ths of a pixel */ |
| 831 | if (d >= (uint32_t)rs) |
| 832 | continue; |
| 833 | uint32_t in = (uint32_t)rs - d >= 16 ? 256 : ((uint32_t)rs - d) * 16; /* a pixel's soft edge */ |
| 834 | uint32_t k = 256 - (in * (256 - keep) >> 8); |
| 835 | ui.back[(size_t)y * (size_t)ui.w + x] = ck_scale(ui.back[(size_t)y * (size_t)ui.w + x], k); |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | static ck_rect flash_box(uint32_t t) |
| 840 | { |
| 841 | int32_t rad; |
| 842 | uint32_t a; |
| 843 | if (!flash(t, &rad, &a)) |
| 844 | return (ck_rect){0, 0, 0, 0}; |
| 845 | int32_t r = (rad >> 4) + 2; |
| 846 | return (ck_rect){ui.cx - r, ui.cy - r, 2 * r + 1, 2 * r + 1}; |
| 847 | } |
| 848 | |
| 849 | /* ---- putting a part of the screen together ---- */ |
| 850 | |
| 851 | static void present(const ck_rect *r) |
| 852 | { |
| 853 | for (int32_t y = r->y; y < r->y + r->h; y++) { |
| 854 | const uint32_t *src = ui.back + (size_t)y * (size_t)ui.w + r->x; |
| 855 | volatile uint32_t *dst = ui.fb + (size_t)y * ui.pitch + r->x; |
| 856 | if (ui.format == 1) |
| 857 | for (int32_t x = 0; x < r->w; x++) |
| 858 | dst[x] = src[x]; |
| 859 | else |
| 860 | for (int32_t x = 0; x < r->w; x++) { |
| 861 | uint32_t c = src[x]; |
| 862 | dst[x] = (c & 0xff00u) | (c >> 16 & 0xffu) | (c & 0xffu) << 16; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* Asleep: pure black, nothing drawn (the panel may be switched off behind it). */ |
| 868 | static void dark(const ck_rect *r) |
| 869 | { |
| 870 | for (int32_t y = r->y; y < r->y + r->h; y++) |
| 871 | for (int32_t x = 0; x < r->w; x++) |
| 872 | ui.back[(size_t)y * (size_t)ui.w + r->x + x] = 0; |
| 873 | } |
| 874 | |
| 875 | /* Dawn: the picture comes back from black over DAWN_MS, everything already moving under it. */ |
| 876 | static void dawn(const ck_rect *r) |
| 877 | { |
| 878 | uint32_t keep = 256 - (ui.dim >> 8); |
| 879 | for (int32_t y = r->y; y < r->y + r->h; y++) { |
| 880 | uint32_t *px = ui.back + (size_t)y * (size_t)ui.w + r->x; |
| 881 | for (int32_t x = 0; x < r->w; x++) |
| 882 | px[x] = ck_scale(px[x], keep); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /* Smooth Q16 progress avoids the coarse position steps of the former Q8 warp. */ |
| 887 | static uint32_t exit_ease(uint32_t t, uint32_t begin, uint32_t end) |
| 888 | { |
| 889 | if (t <= begin) |
| 890 | return 0; |
| 891 | if (t >= end) |
| 892 | return 65536; |
| 893 | uint64_t q = (uint64_t)(t - begin) * 65536u / (end - begin); |
| 894 | return (uint32_t)(q * q * (196608u - 2u * q) >> 32); |
| 895 | } |
| 896 | |
| 897 | /* Freeze the current scene once. Remove only the original figure from that |
| 898 | * backing picture; its actual geometry is drawn separately as it folds. */ |
| 899 | static void exit_capture(void) |
| 900 | { |
| 901 | for (size_t i = 0; i < (size_t)ui.w * ui.h; i++) |
| 902 | ((uint32_t *)ui.keep)[i] = ui.back[i]; |
| 903 | ui.exit_origin = ui.mind; |
| 904 | ck_canvas backdrop = {(uint32_t *)ui.keep, ui.w, ui.h}; |
| 905 | ck_rect all = {0, 0, ui.w, ui.h}, box = ck_grow(ui.mind.box, 2); |
| 906 | if (ck_clip(&box, &all)) { |
| 907 | ck_night_base(&ui.night, &backdrop, &box); |
| 908 | ck_night_lights(&ui.night, &backdrop, &box, 0, 0); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | static void exit_at(uint32_t d) |
| 913 | { |
| 914 | static const uint8_t bayer[16] = {0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5}; |
| 915 | ui.exit_keep = 65536u - exit_ease(d, 0, 1600); |
| 916 | /* Per-frame lookup tables, not per-pixel transfer functions. Stable ordered |
| 917 | * dithering retains the faint stars without adding a new banded veil. */ |
| 918 | for (unsigned phase = 0; phase < 16; phase++) |
| 919 | for (unsigned c = 0; c < 256; c++) { |
| 920 | uint32_t light = (uint32_t)((uint64_t)ck_sky_lin[c] * ui.exit_keep >> 16); |
| 921 | uint32_t encoded = ck_sky_srgb16[light >= 32767u ? 4095u : light >> 3]; |
| 922 | ui.exit_lut[phase][c] = ui.exit_keep == 65536 ? (uint8_t)c : |
| 923 | ui.exit_keep == 0 ? 0 : (uint8_t)ck_min((encoded + bayer[phase]) >> 4, 255); |
| 924 | } |
| 925 | uint32_t flat = 65536u - exit_ease(d, 100, 1200); |
| 926 | uint32_t width = 65536u - exit_ease(d, 1200, 2450); |
| 927 | ui.exit_mind = ui.exit_origin; |
| 928 | ck_mind *m = &ui.exit_mind; |
| 929 | int32_t cx = ui.cx * 16, cy = ui.cy * 16; |
| 930 | for (unsigned i = 0; i < CK_MIND_NODES; i++) { |
| 931 | m->px[i] = cx + (int32_t)((int64_t)(m->px[i] - cx) * width / 65536); |
| 932 | m->py[i] = cy + (int32_t)((int64_t)(m->py[i] - cy) * flat / 65536); |
| 933 | } |
| 934 | m->hx = cx + (int32_t)((int64_t)(m->hx - cx) * width / 65536); |
| 935 | m->hy = cy + (int32_t)((int64_t)(m->hy - cy) * flat / 65536); |
| 936 | m->alpha = (uint32_t)((uint64_t)m->alpha * (65536u - exit_ease(d, 1000, 1450)) >> 16); |
| 937 | /* All points move inward, so the original bounds still contain them. */ |
| 938 | ui.exit_half = ck_max(8, (int32_t)((int64_t)ui.exit_origin.R * 16 * width / 65536)); |
| 939 | ui.exit_line_alpha = (uint32_t)((uint64_t)exit_ease(d, 850, 1250) * |
| 940 | (65536u - exit_ease(d, 2300, 2700)) >> 24); |
| 941 | } |
| 942 | |
| 943 | static void exit_draw(const ck_rect *r) |
| 944 | { |
| 945 | const uint32_t *saved = ui.keep; |
| 946 | for (int32_t y = r->y; y < r->y + r->h; y++) { |
| 947 | size_t row = (size_t)y * ui.w; |
| 948 | for (int32_t x = r->x; x < r->x + r->w; x++) { |
| 949 | uint32_t c = saved[row + x]; |
| 950 | const uint8_t *lut = ui.exit_lut[(y & 3) * 4 + (x & 3)]; |
| 951 | ui.back[row + x] = (uint32_t)lut[c >> 16 & 255] << 16 | |
| 952 | (uint32_t)lut[c >> 8 & 255] << 8 | lut[c & 255]; |
| 953 | } |
| 954 | } |
| 955 | ck_mind_draw(&ui.exit_mind, &ui.cv, r); |
| 956 | if (ui.exit_line_alpha) { |
| 957 | int32_t x = ui.cx * 16, y = ui.cy * 16; |
| 958 | ck_thread(&ui.cv, r, x - ui.exit_half, y, x + ui.exit_half, y, |
| 959 | ck_max(16, S(2) * 16), CK_RGB(112, 188, 255), |
| 960 | ui.exit_line_alpha, ui.exit_line_alpha); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | static void compose(const ck_rect *r) |
| 965 | { |
| 966 | uint32_t t = ui.t; |
| 967 | if (ui.asleep && ui.dim == DIM_FULL) { |
| 968 | dark(r); |
| 969 | return; |
| 970 | } |
| 971 | if (ui.asleep && ui.dusk_active) { |
| 972 | exit_draw(r); |
| 973 | return; |
| 974 | } |
| 975 | ck_night_base(&ui.night, &ui.cv, r); |
| 976 | /* Keep small star twinkles; speech never lights up the surrounding sky. */ |
| 977 | ck_night_lights(&ui.night, &ui.cv, r, 0, 0); |
| 978 | /* Keep the starfield clear: flow still animates Allen, but adds no ink overlay. */ |
| 979 | int32_t rad; |
| 980 | uint32_t a; |
| 981 | if (flash(t, &rad, &a)) { |
| 982 | ck_glow(&ui.cv, r, ui.cx * 16 + 8, ui.cy * 16 + 8, rad, CK_RGB(200, 210, 255), a / 3); |
| 983 | ck_glow(&ui.cv, r, ui.cx * 16 + 8, ui.cy * 16 + 8, rad / 3, CK_RGB(255, 240, 224), a); |
| 984 | unflash(r, t); |
| 985 | } |
| 986 | draw_map(r, t); |
| 987 | ck_allen_canvas_draw(&stage, &ui.cv, r); |
| 988 | draw_code(r); |
| 989 | ck_mind_draw(&ui.mind, &ui.cv, r); |
| 990 | draw_guest(r, t); |
| 991 | draw_words(&ui.now, r, t); |
| 992 | draw_panel(r, t); |
| 993 | if (!choices_up()) |
| 994 | draw_bar(r, t); |
| 995 | if (ui.dim) |
| 996 | dawn(r); |
| 997 | present(r); |
| 998 | } |
| 999 | |
| 1000 | /* ---- the tiles that changed ---- */ |
| 1001 | |
| 1002 | static void mark(ck_rect r) |
| 1003 | { |
| 1004 | ck_rect all = {0, 0, ui.w, ui.h}; |
| 1005 | if (r.w <= 0 || r.h <= 0 || !ck_clip(&r, &all)) |
| 1006 | return; |
| 1007 | for (int32_t ty = r.y / TH; ty <= (r.y + r.h - 1) / TH; ty++) |
| 1008 | for (int32_t tx = r.x / TW; tx <= (r.x + r.w - 1) / TW; tx++) |
| 1009 | ui.tiles[ty * ui.tx + tx] = 1; |
| 1010 | } |
| 1011 | |
| 1012 | static void mark_all(void) |
| 1013 | { |
| 1014 | for (int32_t i = 0; i < ui.tx * ui.ty; i++) |
| 1015 | ui.tiles[i] = 1; |
| 1016 | } |
| 1017 | |
| 1018 | /* the tiles the ripple's band crosses, between radius lo and hi */ |
| 1019 | static void mark_ring(int32_t lo, int32_t hi) |
| 1020 | { |
| 1021 | if (hi <= 0) |
| 1022 | return; |
| 1023 | lo = ck_max(lo, 0); |
| 1024 | int64_t lo2 = (int64_t)lo * lo, hi2 = (int64_t)hi * hi; |
| 1025 | for (int32_t ty = 0; ty < ui.ty; ty++) |
| 1026 | for (int32_t tx = 0; tx < ui.tx; tx++) { |
| 1027 | int32_t x0 = tx * TW, y0 = ty * TH, x1 = x0 + TW, y1 = y0 + TH; |
| 1028 | int64_t nx = ck_clamp(ui.cx, x0, x1) - ui.cx, ny = ck_clamp(ui.cy, y0, y1) - ui.cy; |
| 1029 | int64_t fx = ck_max(ck_abs(x0 - ui.cx), ck_abs(x1 - ui.cx)), |
| 1030 | fy = ck_max(ck_abs(y0 - ui.cy), ck_abs(y1 - ui.cy)); |
| 1031 | if (nx * nx + ny * ny <= hi2 && fx * fx + fy * fy >= lo2) |
| 1032 | ui.tiles[ty * ui.tx + tx] = 1; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | static unsigned strips(void) |
| 1037 | { |
| 1038 | unsigned n = 0; |
| 1039 | for (int32_t ty = 0; ty < ui.ty; ty++) { |
| 1040 | uint8_t *row = ui.tiles + ty * ui.tx; |
| 1041 | int32_t runs = 0, first = -1, last = -1; |
| 1042 | for (int32_t tx = 0; tx < ui.tx; tx++) |
| 1043 | if (row[tx]) { |
| 1044 | if (first < 0) |
| 1045 | first = tx; |
| 1046 | if (tx == 0 || !row[tx - 1]) |
| 1047 | runs++; |
| 1048 | last = tx; |
| 1049 | } |
| 1050 | if (first < 0) |
| 1051 | continue; |
| 1052 | int32_t y = ty * TH, h = ck_min(TH, ui.h - y); |
| 1053 | if (runs > 6 || n + (unsigned)runs > JOBS_MAX) { /* one strip for the row */ |
| 1054 | int32_t x = first * TW; |
| 1055 | ui.jobs[n++] = (ck_rect){x, y, ck_min((last + 1) * TW, ui.w) - x, h}; |
| 1056 | } else |
| 1057 | for (int32_t tx = 0; tx < ui.tx; tx++) { |
| 1058 | if (!row[tx] || (tx > 0 && row[tx - 1])) |
| 1059 | continue; |
| 1060 | int32_t end = tx; |
| 1061 | while (end + 1 < ui.tx && row[end + 1]) |
| 1062 | end++; |
| 1063 | ui.jobs[n++] = (ck_rect){tx * TW, y, ck_min((end + 1) * TW, ui.w) - tx * TW, h}; |
| 1064 | } |
| 1065 | for (int32_t tx = 0; tx < ui.tx; tx++) |
| 1066 | row[tx] = 0; |
| 1067 | if (n >= JOBS_MAX) |
| 1068 | break; |
| 1069 | } |
| 1070 | return n; |
| 1071 | } |
| 1072 | |
| 1073 | int ck_allen_ui_help(void) |
| 1074 | { |
| 1075 | for (;;) { |
| 1076 | uint64_t v = __atomic_load_n(&ui.ticket, __ATOMIC_ACQUIRE); |
| 1077 | uint32_t next = (uint32_t)(v & 0xffffu), count = (uint32_t)(v >> 16 & 0xffffu); |
| 1078 | if (next >= count) |
| 1079 | return 0; |
| 1080 | if (__atomic_compare_exchange_n(&ui.ticket, &v, v + 1, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { |
| 1081 | compose(&ui.jobs[next]); |
| 1082 | __atomic_fetch_add(&ui.done, 1, __ATOMIC_RELEASE); |
| 1083 | return 1; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /* Present only after all shutdown strips are composed. This bounds mixed |
| 1089 | * frames; it is a CPU copy to GOP, not a vblank/page-flip guarantee. */ |
| 1090 | static void present_exit(unsigned n) |
| 1091 | { |
| 1092 | for (unsigned i = 0; i < n; i++) |
| 1093 | present(&ui.jobs[i]); |
| 1094 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 1095 | } |
| 1096 | |
| 1097 | /* ---- the frame ---- */ |
| 1098 | |
| 1099 | /* what the keyboard side left, taken under the lock */ |
| 1100 | static void take_notes(uint32_t t) |
| 1101 | { |
| 1102 | lock(); |
| 1103 | if (note.refresh) |
| 1104 | note.refresh = 0, mark_all(); |
| 1105 | if (note.wake_gen != ui.seen_wake) { |
| 1106 | ui.seen_wake = note.wake_gen; |
| 1107 | ui.footer = note.footer; |
| 1108 | if (!ui.on) |
| 1109 | ui.on = 1, ui.wake_t = t, ui.quiet_t = ck_max((int32_t)t, (int32_t)FORMED_MS); |
| 1110 | } |
| 1111 | if (note.sleep_gen != ui.seen_sleep) { |
| 1112 | ui.seen_sleep = note.sleep_gen; |
| 1113 | ui.asleep = note.sleep; |
| 1114 | mark_all(); /* the whole screen changes either way */ |
| 1115 | } |
| 1116 | if (note.input_gen != ui.seen_input) { |
| 1117 | ui.seen_input = note.input_gen; |
| 1118 | if (note.input_len > ui.input_len) |
| 1119 | ck_mind_key(&ui.mind, t), ck_flow_key(&ui.flow, t); |
| 1120 | ui.input_len = note.input_len; |
| 1121 | for (unsigned i = 0; i < ui.input_len; i++) |
| 1122 | ui.input[i] = note.input[i]; |
| 1123 | ui.key_t = t, ui.quiet_t = t, ui.input_moved = 1; |
| 1124 | } |
| 1125 | ui.working = note.working; |
| 1126 | if (note.code_gen != ui.seen_code) { |
| 1127 | ui.seen_code = note.code_gen; |
| 1128 | if (note.code_on) { |
| 1129 | for (unsigned i = 0; i < note.code_len; i++) |
| 1130 | code_txt[i] = note_code[i]; |
| 1131 | code_txt[note.code_len] = 0; |
| 1132 | ui.code_len = note.code_len; |
| 1133 | copy(ui.code_lang, note.code_lang, sizeof ui.code_lang); |
| 1134 | ui.code_rows = ck_code_rows(code_txt, ui.code_len, ui.code_cols, code_st, code_ln, code_no, CODE_ROWS_MAX); |
| 1135 | ui.code_lines = ck_code_lines(code_txt, ui.code_len); |
| 1136 | note.code_top = 0; |
| 1137 | note.code_rows = ui.code_rows; |
| 1138 | note.code_page = ui.code_page; |
| 1139 | } |
| 1140 | ui.code_on = note.code_on; |
| 1141 | mark(ui.code_box); |
| 1142 | } |
| 1143 | if (note.code_top != ui.code_top) { |
| 1144 | ui.code_top = note.code_top; |
| 1145 | mark(ui.code_box); |
| 1146 | } |
| 1147 | if (note.said_gen != ui.seen_said) { |
| 1148 | ui.seen_said = note.said_gen; |
| 1149 | if (note.more && join_words(note.reply, t)) { |
| 1150 | ui.quiet_t = t; /* more of the words on screen: they go on, nothing starts over */ |
| 1151 | } else { |
| 1152 | if (note.has_line) { |
| 1153 | ui.guest_len = copy(ui.guest, note.line, TEXT_MAX); |
| 1154 | ui.guest_x = ui.bar.x + S(30); |
| 1155 | ui.guest_t = t; |
| 1156 | ck_allen_canvas_clear(&stage, t); /* a new line: what was shown for the last one goes */ |
| 1157 | } |
| 1158 | /* what is on screen fades; Allen thinks, then speaks. With no reply |
| 1159 | * yet he keeps thinking (the water turns) until it comes. */ |
| 1160 | if (ui.now.len && !ui.now.fade) |
| 1161 | ui.now.fade = t; |
| 1162 | ui.next = (struct words){0}; |
| 1163 | if (note.think) { |
| 1164 | ui.speak_t = 0; |
| 1165 | ui.thinking = 1; |
| 1166 | } else { |
| 1167 | ui.next.len = copy(ui.next.text, note.reply, TEXT_MAX); |
| 1168 | uint32_t think = |
| 1169 | ui.thinking ? 400u : note.has_line ? 900u + ck_min((int32_t)ui.next.len * 6, 700) : 500u; |
| 1170 | ui.speak_t = ck_max((int32_t)(t + think), (int32_t)FORMED_MS + 300); |
| 1171 | ui.thinking = 0; |
| 1172 | } |
| 1173 | ui.input_len = 0; |
| 1174 | if (note.input_gen != note.said_input_gen) { /* typed after the words came, in the same frame */ |
| 1175 | ui.input_len = note.input_len; |
| 1176 | for (unsigned i = 0; i < ui.input_len; i++) |
| 1177 | ui.input[i] = note.input[i]; |
| 1178 | } |
| 1179 | ui.input_moved = 1; |
| 1180 | ui.quiet_t = t; |
| 1181 | ck_mind_mode(&ui.mind, CK_MIND_THINK, t); |
| 1182 | } |
| 1183 | } |
| 1184 | if (note.panel_gen != ui.seen_panel) { |
| 1185 | ui.seen_panel = note.panel_gen; |
| 1186 | const struct panel *p = ¬e.panel; |
| 1187 | uint32_t at = (uint32_t)ck_max((int32_t)t + 300, (int32_t)ui.speak_t); /* after the words have started */ |
| 1188 | for (unsigned i = 0; i < p->nline; i++) { |
| 1189 | int kept = ui.pan.on && i < ui.pan.nline; |
| 1190 | for (unsigned k = 0; kept && k < PL_MAX; k++) { |
| 1191 | if (ui.pan.line[i][k] != p->line[i][k]) |
| 1192 | kept = 0; |
| 1193 | if (!p->line[i][k]) |
| 1194 | break; |
| 1195 | } |
| 1196 | if (kept) |
| 1197 | at = ui.pl_t[i]; |
| 1198 | else |
| 1199 | ui.pl_t[i] = at = ck_max((int32_t)at, (int32_t)t + (i ? 0 : 300)); |
| 1200 | at += PANEL_STEP_MS; |
| 1201 | } |
| 1202 | if (p->lit != ui.pan.lit || !ui.pan.on) |
| 1203 | ui.lit_was = ui.pan.on ? ui.pan.lit : -1, ui.lit_t = t; |
| 1204 | if (ui.pan.on && !p->on && ui.now.len && !ui.now.fade) |
| 1205 | ui.now.fade = t; /* the panel goes, and the words held with it go too */ |
| 1206 | ui.pan = *p; |
| 1207 | ui.quiet_t = t; |
| 1208 | mark(ui.pan_box); |
| 1209 | mark(ui.row_box); |
| 1210 | } |
| 1211 | if (note.map_gen != ui.seen_map) { |
| 1212 | ui.seen_map = note.map_gen; |
| 1213 | ui.n = note.n, ui.ne = note.ne, ui.total = note.total, ui.links = note.links, ui.lit = note.lit; |
| 1214 | for (unsigned i = 0; i < ui.ne; i++) |
| 1215 | ui.ea[i] = note.ea[i], ui.eb[i] = note.eb[i]; |
| 1216 | ui.label_len = copy(ui.label, note.label, TEXT_MAX); |
| 1217 | if (ui.lit >= 0 && ui.n) |
| 1218 | ui.map_t = t ? t : 1; |
| 1219 | mark(ui.map_box); |
| 1220 | } |
| 1221 | if (note.scene_gen != seen_scene) { /* the things come as he starts to speak */ |
| 1222 | seen_scene = note.scene_gen; |
| 1223 | ck_allen_canvas_show(&stage, ¬e.scene, ui.speak_t ? ui.speak_t : t); |
| 1224 | } |
| 1225 | unlock(); |
| 1226 | } |
| 1227 | |
| 1228 | static uint32_t toward(uint32_t cur, uint32_t want, uint32_t dt, uint32_t full_ms) |
| 1229 | { |
| 1230 | uint32_t step = (uint32_t)((uint64_t)256 * dt / full_ms); |
| 1231 | step = step || !dt ? step : 1; |
| 1232 | if (cur < want) |
| 1233 | return cur + step < want ? cur + step : want; |
| 1234 | return cur > want + step ? cur - step : want; |
| 1235 | } |
| 1236 | |
| 1237 | /* moves everything to t and marks what changed */ |
| 1238 | static void advance(uint32_t t) |
| 1239 | { |
| 1240 | uint32_t prev = ui.last, dt = t - prev; |
| 1241 | take_notes(t); |
| 1242 | if (dt > 100) |
| 1243 | dt = 100; /* after a long sleep the first frame moves things on by a moment, not by the whole sleep */ |
| 1244 | /* A frozen background fade and a small geometry fold; no full-screen warp. */ |
| 1245 | uint32_t dim_was = ui.dim; |
| 1246 | if (ui.asleep) { |
| 1247 | if (!ui.dusk_active) { |
| 1248 | ui.dusk_active = 1; |
| 1249 | ui.dusk_t = t; |
| 1250 | exit_capture(); |
| 1251 | } |
| 1252 | uint32_t d = t - ui.dusk_t; |
| 1253 | ui.dim = d >= DUSK_MS ? DIM_FULL : (uint32_t)((uint64_t)DIM_FULL * d / DUSK_MS); |
| 1254 | if (ui.dim != DIM_FULL || dim_was != DIM_FULL) { |
| 1255 | if (d <= 1600 || prev - ui.dusk_t < 1600) |
| 1256 | mark_all(); /* erase the last stars once before limiting work to the line */ |
| 1257 | else |
| 1258 | mark((ck_rect){ui.cx - ui.exit_origin.R - 4, ui.cy - 4, |
| 1259 | 2 * ui.exit_origin.R + 8, 8}); |
| 1260 | } |
| 1261 | if (ui.dim == DIM_FULL) |
| 1262 | return; |
| 1263 | exit_at(d); |
| 1264 | return; |
| 1265 | } |
| 1266 | /* the dawn: the sky comes back over DAWN_MS with everything moving under it. Woken during the dusk, it |
| 1267 | * starts from the dark rather than from the falling picture. */ |
| 1268 | if (ui.dusk_active) |
| 1269 | ui.dusk_active = 0, ui.dusk_t = 0, ui.dim = DIM_FULL; |
| 1270 | uint32_t dim_step = (uint32_t)((uint64_t)DIM_FULL * dt / DAWN_MS); |
| 1271 | ui.dim = ui.dim > dim_step ? ui.dim - dim_step : 0; |
| 1272 | if (ui.dim != dim_was || ui.dim) |
| 1273 | mark_all(); |
| 1274 | ck_flow_mode(&ui.flow, |
| 1275 | ui.asleep || !ui.on || t < FORMED_MS ? CK_FLOW_SLEEP |
| 1276 | : ui.mind.mode == CK_MIND_THINK ? CK_FLOW_THINK |
| 1277 | : ui.mind.mode == CK_MIND_SPEAK ? CK_FLOW_SPEAK |
| 1278 | : ui.mind.mode == CK_MIND_LISTEN ? CK_FLOW_LISTEN |
| 1279 | : CK_FLOW_IDLE, |
| 1280 | t); |
| 1281 | /* Sky view update: for above_me recompute sidereal time and zenith; |
| 1282 | * for somewhere_new use the region centre + slow drift. Rebuild every |
| 1283 | * SKY_REBUILD_MS or when sky_mode changes (sky_view_tsc=0). */ |
| 1284 | if (ui.sky_mode && (ui.tsc_now - ui.sky_view_tsc > SKY_REBUILD_MS * (ui.hz / 1000) || ui.sky_view_tsc == 0)) { |
| 1285 | ck_sky_view new_view; |
| 1286 | int32_t ra_udeg, dec_udeg; |
| 1287 | if (ui.sky_mode == 1 && ui.sky_tsc && ui.hz) { |
| 1288 | /* above me: compute zenith from current unix time */ |
| 1289 | uint64_t elapsed = ui.tsc_now - ui.sky_tsc; |
| 1290 | int64_t unix_now = ui.sky_unix_s + (int64_t)(elapsed / ui.hz); |
| 1291 | int16_t zenith[3]; |
| 1292 | ck_sky_zenith(unix_now, ui.sky_lat_udeg, ui.sky_lon_udeg, zenith, &ra_udeg, &dec_udeg); |
| 1293 | ck_sky_view_at(&new_view, ra_udeg, dec_udeg, SKY_FOV_MDEG, 0); |
| 1294 | ck_night_view(&ui.night, &new_view); |
| 1295 | } else if (ui.sky_mode == 2) { |
| 1296 | /* somewhere new: region centre + slow drift in RA */ |
| 1297 | int r = ui.sky_region % SKY_NREGIONS; |
| 1298 | uint64_t secs = ui.sky_tsc ? (ui.tsc_now - ui.sky_tsc) / (ui.hz ? ui.hz : 1) : 0; |
| 1299 | ra_udeg = sky_region_ra_udeg[r] + (int32_t)(((int64_t)SKY_DRIFT_URA_PER_S * (int64_t)secs) % 360000000); |
| 1300 | dec_udeg = sky_region_dec_udeg[r]; |
| 1301 | ck_sky_view_at(&new_view, ra_udeg, dec_udeg, SKY_FOV_MDEG, 0); |
| 1302 | ck_night_view(&ui.night, &new_view); |
| 1303 | } |
| 1304 | ui.sky_view_tsc = ui.tsc_now; |
| 1305 | mark_all(); |
| 1306 | } |
| 1307 | /* the sky */ |
| 1308 | ck_night_at(&ui.night, t); |
| 1309 | if (t < CK_NIGHT_FADE_END_MS + FRAME_MS * 2) |
| 1310 | mark_all(); |
| 1311 | mark(ui.lens_was); |
| 1312 | mark(ui.night.lens); |
| 1313 | ui.lens_was = ui.night.lens; |
| 1314 | if (ui.night.ripple_w || ui.ripple_was >= 0) { |
| 1315 | int32_t reach = ui.night.ripple_w * 22 / 10 + 2; |
| 1316 | if (ui.ripple_was >= 0) |
| 1317 | mark_ring(ui.ripple_was - reach - 8, ui.ripple_was + reach + 8); |
| 1318 | if (ui.night.ripple_w) |
| 1319 | mark_ring(ui.night.ripple_r - reach - 8, ui.night.ripple_r + reach + 8); |
| 1320 | ui.ripple_was = ui.night.ripple_w ? ui.night.ripple_r : -1; |
| 1321 | } |
| 1322 | ck_rect fb = flash_box(t); |
| 1323 | mark(ui.flash_was); |
| 1324 | mark(fb); |
| 1325 | ui.flash_was = fb; |
| 1326 | /* Every twinkle is evaluated at this frame's time when a strip is |
| 1327 | * drawn, so invalidate every twinkle's footprint at the same time. */ |
| 1328 | for (unsigned i = 0; i < ui.night.ntw; i++) |
| 1329 | mark(ck_night_star_box(&ui.night, i)); |
| 1330 | |
| 1331 | /* the conversation: speaking starts, words reach the mind */ |
| 1332 | if (ui.speak_t && t >= ui.speak_t) { |
| 1333 | ui.now = ui.next; |
| 1334 | ui.now.from = ui.speak_t; |
| 1335 | time_letters(&ui.now, 0, ui.now.from); |
| 1336 | lay_out(&ui.now); |
| 1337 | for (unsigned i = 0; i < LINES_MAX; i++) |
| 1338 | ui.now.x0[i] = ui.now.x[i]; |
| 1339 | ui.speak_t = 0; |
| 1340 | ck_mind_mode(&ui.mind, CK_MIND_SPEAK, t); |
| 1341 | } |
| 1342 | struct words *w = &ui.now; |
| 1343 | if (w->len && t >= w->from && prev < w->done) { |
| 1344 | for (unsigned i = 0; i < w->len; i++) { |
| 1345 | uint32_t at = w->at[i]; |
| 1346 | if (at > prev && at <= t && w->text[i] != ' ' && (i == 0 || w->text[i - 1] == ' ')) |
| 1347 | ck_mind_word(&ui.mind, t), ck_flow_word(&ui.flow, t), ui.word_t = t; |
| 1348 | } |
| 1349 | } |
| 1350 | uint32_t last_letter = w->len ? w->at[w->len - 1] + LETTER_MS : 0; |
| 1351 | int speaking = w->len && t >= w->from && t < last_letter + 300; |
| 1352 | ui.level = speaking ? ck_exp_neg((t - ui.word_t) * 256 / 200) >> 8 : 0; |
| 1353 | ck_mind_voice(&ui.mind, ui.level); |
| 1354 | if (!speaking && !ui.speak_t && !ui.working && (ui.mind.mode == CK_MIND_SPEAK || ui.mind.mode == CK_MIND_THINK) && |
| 1355 | w->len && t >= w->from) { |
| 1356 | ck_mind_mode(&ui.mind, CK_MIND_IDLE, t); |
| 1357 | ui.quiet_t = t; |
| 1358 | } |
| 1359 | if (ui.working && !speaking && !ui.speak_t && ui.mind.mode != CK_MIND_THINK) |
| 1360 | ck_mind_mode(&ui.mind, CK_MIND_THINK, t); /* far off, still at it: he goes on thinking */ |
| 1361 | if (ui.mind.mode != CK_MIND_THINK && ui.mind.mode != CK_MIND_SPEAK) |
| 1362 | ck_mind_mode(&ui.mind, ui.input_len && t - ui.key_t < 4000 ? CK_MIND_LISTEN : CK_MIND_IDLE, t); |
| 1363 | if (w->len && (t < w->done + 40 || words_alpha(w, t) != words_alpha(w, prev))) |
| 1364 | mark(w->box); |
| 1365 | if (ui.words_was.w && (ui.words_was.x != w->box.x || ui.words_was.w != w->box.w || ui.words_was.h != w->box.h)) |
| 1366 | mark(ui.words_was); |
| 1367 | ui.words_was = w->box; |
| 1368 | if (w->len && !words_alpha(w, t) && t > w->from) |
| 1369 | *w = (struct words){0}; |
| 1370 | |
| 1371 | /* the water, and Allen floating on it: he sways a little with the current under him */ |
| 1372 | ck_flow_at(&ui.flow, t); |
| 1373 | int32_t vx, vy; |
| 1374 | ck_flow_velocity(&ui.flow, ui.cx, ui.cy, &vx, &vy); |
| 1375 | ui.mind.cx = ui.cx + ck_clamp(vx * S(10) / 1000, -S(10), S(10)); |
| 1376 | ui.mind.cy = ui.cy + ck_clamp(vy * S(10) / 1000, -S(10), S(10)); |
| 1377 | /* Allen */ |
| 1378 | ck_mind_at(&ui.mind, t); |
| 1379 | mark(ck_mind_dirty(&ui.mind)); |
| 1380 | if (ui.guest_t) { |
| 1381 | ck_rect g = guest_area(); |
| 1382 | mark(g); |
| 1383 | ui.guest_box = g; |
| 1384 | if (t - ui.guest_t > 2400) |
| 1385 | ui.guest_t = 0; |
| 1386 | } |
| 1387 | |
| 1388 | /* the code: in over 0.6 s, out over 0.9 s */ |
| 1389 | ui.code_a = toward(ui.code_a, ui.code_on ? 256 : 0, dt, ui.code_on ? 600 : 900); |
| 1390 | if (ui.code_a != ui.code_was) |
| 1391 | mark(ui.code_box); |
| 1392 | ui.code_was = ui.code_a; |
| 1393 | |
| 1394 | /* the setup wizard's panel: in and out, its lines easing in, the lit choice moving */ |
| 1395 | ui.pan_a = toward(ui.pan_a, ui.pan.on ? 256 : 0, dt, ui.pan.on ? 600 : 900); |
| 1396 | if (ui.pan_a != ui.pan_was) { |
| 1397 | mark(ui.pan_box); |
| 1398 | mark(ui.row_box); |
| 1399 | } |
| 1400 | ui.pan_was = ui.pan_a; |
| 1401 | for (unsigned i = 0; ui.pan_a && i < ui.pan.nline; i++) |
| 1402 | if (t > ui.pl_t[i] && prev < ui.pl_t[i] + 560) { |
| 1403 | mark(ui.pan_box); |
| 1404 | break; |
| 1405 | } |
| 1406 | if (ui.pan_a && t - ui.lit_t < 260) |
| 1407 | mark(ui.row_box); |
| 1408 | |
| 1409 | /* the bar: up once Allen has formed and while in use; down when things rest */ |
| 1410 | int want = ui.on && t >= FORMED_MS && !choices_up() && |
| 1411 | (ui.pan.on ? ui.pan.prompt[0] != 0 : ui.input_len || t - ui.quiet_t < BAR_REST_MS || speaking || ui.speak_t); |
| 1412 | ui.bar_a = toward(ui.bar_a, want ? 256 : 0, dt, want ? (t - ui.key_t < 300 ? 250 : 900) : 1400); |
| 1413 | if (ui.bar_a || ui.bar_was || ui.input_moved) |
| 1414 | mark(ui.bar_box); |
| 1415 | ui.bar_was = ui.bar_a; |
| 1416 | ui.input_moved = 0; |
| 1417 | |
| 1418 | /* the notes: shown a while after a note is saved or handed back */ |
| 1419 | int show = ui.map_t && t - ui.map_t < MAP_SHOW_MS && ui.n; |
| 1420 | ui.map_a = toward(ui.map_a, show ? 256 : 0, dt, show ? 700 : 1500); |
| 1421 | if (ui.map_a || ui.map_was) |
| 1422 | mark(ui.map_box); |
| 1423 | ui.map_was = ui.map_a; |
| 1424 | if (ui.lit >= 0 && ui.map_t && t - ui.map_t < 1200) /* the spark's path, Allen to the star */ |
| 1425 | mark(ck_union(ck_mind_dirty(&ui.mind), ui.map_box)); |
| 1426 | ck_allen_canvas_at(&stage, t, ui.mind.hx, ui.mind.hy, &ui.flow, mark); |
| 1427 | } |
| 1428 | |
| 1429 | int ck_allen_ui_tick(uint64_t now) |
| 1430 | { |
| 1431 | if (!ui.started) |
| 1432 | return 0; |
| 1433 | if (ck_allen_ui_asleep() && !__atomic_load_n(¬e.refresh, __ATOMIC_ACQUIRE)) |
| 1434 | return 0; |
| 1435 | uint32_t t = ms(now); |
| 1436 | if (ui.frames && t - ui.last < FRAME_MS && !__atomic_load_n(¬e.refresh, __ATOMIC_ACQUIRE)) |
| 1437 | return 0; |
| 1438 | uint64_t c0 = ui.clock ? ui.clock() : 0; |
| 1439 | ui.t = t; |
| 1440 | ui.tsc_now = now; /* save TSC for sky update in advance() */ |
| 1441 | advance(t); |
| 1442 | unsigned n = strips(); |
| 1443 | ui.done = 0; |
| 1444 | __atomic_store_n(&ui.ticket, (uint64_t)n << 16, __ATOMIC_RELEASE); |
| 1445 | while (ck_allen_ui_help()) |
| 1446 | ; |
| 1447 | while (__atomic_load_n(&ui.done, __ATOMIC_ACQUIRE) < n) |
| 1448 | relax(); |
| 1449 | if (ui.asleep) |
| 1450 | present_exit(n); |
| 1451 | ui.last = t; |
| 1452 | ui.frames++; |
| 1453 | if (ui.clock) { |
| 1454 | uint64_t c = ui.clock() - c0; |
| 1455 | uint32_t us = (uint32_t)(c * 1000000u / ui.hz); |
| 1456 | ui.total_ticks += us; |
| 1457 | if (us > ui.slowest) |
| 1458 | ui.slowest = us; |
| 1459 | if (ui.asleep && ui.dusk_active && ui.dim < DIM_FULL) { |
| 1460 | ui.dusk_frames++; |
| 1461 | ui.dusk_ticks += us; |
| 1462 | if (us > ui.dusk_slowest) |
| 1463 | ui.dusk_slowest = us; |
| 1464 | } |
| 1465 | } |
| 1466 | if (ui.asleep && ui.dim == DIM_FULL) { |
| 1467 | /* done's acquire above includes every helper's framebuffer writes. |
| 1468 | * Publish only after presentation, and never acknowledge a sleep |
| 1469 | * request superseded while this frame was in flight. */ |
| 1470 | lock(); |
| 1471 | if (note.sleep_gen == ui.seen_sleep && note.sleep) |
| 1472 | __atomic_store_n(&ui.black_presented, 1, __ATOMIC_RELEASE); |
| 1473 | unlock(); |
| 1474 | } |
| 1475 | return 1; |
| 1476 | } |
| 1477 | |
| 1478 | void ck_allen_ui_stats(uint32_t *frames, uint32_t *slowest_us, uint32_t *average_us) |
| 1479 | { |
| 1480 | *frames = ui.frames; |
| 1481 | *slowest_us = ui.slowest; |
| 1482 | *average_us = ui.frames ? ui.total_ticks / ui.frames : 0; |
| 1483 | } |
| 1484 | |
| 1485 | void ck_allen_ui_dusk_stats(uint32_t *frames, uint32_t *slowest_us, uint32_t *average_us) |
| 1486 | { |
| 1487 | *frames = ui.dusk_frames; |
| 1488 | *slowest_us = ui.dusk_slowest; |
| 1489 | *average_us = ui.dusk_frames ? ui.dusk_ticks / ui.dusk_frames : 0; |
| 1490 | } |
| 1491 | |
| 1492 | /* ---- the interface ---- */ |
| 1493 | |
| 1494 | static size_t face_bytes(uint32_t cap) |
| 1495 | { |
| 1496 | size_t s = cap * 256u / 20u; /* as ck_af_face_init */ |
| 1497 | size_t w = (26 * s + 2 * 1024) / 256 + 6, h = (33 * s + 2 * 1024) / 256 + 6; |
| 1498 | return CK_AF_COUNT * w * h; |
| 1499 | } |
| 1500 | |
| 1501 | static uint32_t scale_for(uint32_t w, uint32_t h) |
| 1502 | { |
| 1503 | uint32_t kw = w * 256u / 1920u, kh = h * 256u / 1080u; |
| 1504 | return kw < kh ? kw : kh; |
| 1505 | } |
| 1506 | |
| 1507 | #define CAP_VOICE 19 |
| 1508 | #define CAP_BODY 15 |
| 1509 | #define CAP_SMALL 11 |
| 1510 | |
| 1511 | size_t ck_allen_ui_mem_bytes(uint32_t w, uint32_t h) |
| 1512 | { |
| 1513 | uint32_t k = scale_for(w, h); |
| 1514 | return (size_t)w * h * 4 + ck_night_mem_bytes(w, h, k) + (size_t)w * h * 4 + |
| 1515 | face_bytes(CAP_VOICE * k / 256) + face_bytes(CAP_BODY * k / 256) + face_bytes(CAP_SMALL * k / 256) + 64; |
| 1516 | } |
| 1517 | |
| 1518 | int ck_allen_ui_started(void) { return ui.started; } |
| 1519 | int ck_allen_ui_on(void) { return ui.on; } |
| 1520 | |
| 1521 | int ck_allen_ui_formed(uint64_t now) { return ui.started && ms(now) >= FORMED_MS + 300u; } |
| 1522 | |
| 1523 | int ck_allen_ui_start(volatile uint32_t *fb, uint32_t w, uint32_t h, uint32_t pitch, uint32_t format, void *mem, |
| 1524 | size_t mem_len, uint64_t hz, uint64_t now, uint64_t (*clock)(void)) |
| 1525 | { |
| 1526 | if (!fb || w < 640 || h < 400 || pitch < w || format > 1 || hz == 0 || !mem || |
| 1527 | mem_len < ck_allen_ui_mem_bytes(w, h) || ((w + TW - 1) / TW) * ((h + TH - 1) / TH) > TILES_MAX) |
| 1528 | return -1; |
| 1529 | char hint[sizeof ui.hint]; |
| 1530 | for (unsigned i = 0; i < sizeof hint; i++) |
| 1531 | hint[i] = ui.hint[i]; |
| 1532 | ui = (__typeof__(ui)){0}; |
| 1533 | ck_allen_ui_named(hint[9] ? hint + 9 : "Allen"); /* keeps a name given before the window opened */ |
| 1534 | ui.fb = fb; |
| 1535 | ui.w = (int32_t)w, ui.h = (int32_t)h; |
| 1536 | ui.pitch = pitch, ui.format = format, ui.hz = hz, ui.t0 = now, ui.clock = clock; |
| 1537 | ui.k = scale_for(w, h); |
| 1538 | ui.tx = (int32_t)((w + TW - 1) / TW), ui.ty = (int32_t)((h + TH - 1) / TH); |
| 1539 | uint8_t *p = mem; |
| 1540 | ui.back = (uint32_t *)p; |
| 1541 | p += (size_t)w * h * 4; |
| 1542 | ui.cv = (ck_canvas){ui.back, ui.w, ui.h}; |
| 1543 | ui.cx = ui.w / 2, ui.cy = ui.h * 40 / 100; |
| 1544 | size_t nb = ck_night_mem_bytes(w, h, ui.k); |
| 1545 | if (ck_night_init(&ui.night, w, h, ui.k, ui.cx, ui.cy, p, nb)) |
| 1546 | return -1; |
| 1547 | p += nb; |
| 1548 | ui.keep = p; |
| 1549 | p += (size_t)w * h * 4; |
| 1550 | uint32_t caps[3] = {CAP_VOICE * ui.k / 256, CAP_BODY * ui.k / 256, CAP_SMALL * ui.k / 256}; |
| 1551 | ck_af_face *faces[3] = {&ui.voice, &ui.body, &ui.small}; |
| 1552 | unsigned weights[3] = {1000, 1150, 1250}; |
| 1553 | for (int i = 0; i < 3; i++) { |
| 1554 | size_t n = face_bytes(caps[i]); |
| 1555 | ck_af_face_init(faces[i], caps[i], weights[i], p, n); |
| 1556 | p += n; |
| 1557 | /* every letter now, so drawing never writes to the face (other cores read it) */ |
| 1558 | for (int c = CK_AF_FIRST; c <= CK_AF_LAST; c++) |
| 1559 | (void)ck_af_glyph_get(faces[i], (char)c); |
| 1560 | } |
| 1561 | ck_mind_init(&ui.mind, ui.cx, ui.cy, ui.k, BORN_MS); |
| 1562 | /* layout (design px on 1920 x 1080, centred) */ |
| 1563 | int32_t bw = S(720), bh = S(54); |
| 1564 | ui.bar = (ck_rect){(ui.w - bw) / 2, ui.h - S(150), bw, bh}; |
| 1565 | ui.bar_box = (ck_rect){ui.bar.x - S(20), ui.bar.y - S(20), bw + S(40), bh + S(80)}; |
| 1566 | ui.bar_box.x = ck_min(ui.bar_box.x, ui.w / 2 - S(360)); |
| 1567 | ui.bar_box.w = ck_max(ui.bar_box.w, S(720)); |
| 1568 | ui.mx = ui.w * 84 / 100, ui.my = ui.h * 42 / 100, ui.mr = S(130); |
| 1569 | ui.map_box = (ck_rect){ui.mx - S(190), ui.my - ui.mr - S(64), S(380), 2 * ui.mr + S(150)}; |
| 1570 | ui.pan_box = (ck_rect){ui.w / 2 - S(640), pan_line_y(0) - S(34), S(1280), (int32_t)CK_ALLEN_UI_PANEL_LINES * S(31) + S(30)}; |
| 1571 | ui.row_box = (ck_rect){ui.w / 2 - S(900), ui.bar.y - S(24), S(1800), ui.h - ui.bar.y + S(24)}; |
| 1572 | /* the code: left of Allen, from near the top to above the bar, one width per letter */ |
| 1573 | { |
| 1574 | static const char wide[] = "MWmw0@#%"; |
| 1575 | uint32_t cw = 0; |
| 1576 | for (unsigned i = 0; i + 1 < sizeof wide; i++) |
| 1577 | cw = cw > ck_af_advance(&ui.small, wide[i]) ? cw : ck_af_advance(&ui.small, wide[i]); |
| 1578 | ui.code_cw = (int32_t)cw; |
| 1579 | ui.code_lh = S(21); |
| 1580 | ui.code_x = S(84); |
| 1581 | ui.code_y = S(92); |
| 1582 | ui.code_w = ui.w / 2 - S(300) - ui.code_x; |
| 1583 | ui.code_h = ui.bar.y - S(70) - ui.code_y; |
| 1584 | ui.code_page = ui.code_lh > 0 ? (uint32_t)(ui.code_h / ui.code_lh) : 1; |
| 1585 | int32_t cols = cw ? (int32_t)((int64_t)ui.code_w * 256 / cw) - 5 : 40; |
| 1586 | ui.code_cols = cols > 20 ? (uint32_t)cols : 20; |
| 1587 | ui.code_box = ck_grow((ck_rect){ui.code_x - S(36), ui.code_y - S(56), ui.code_w + S(56), ui.code_h + S(100)}, S(50)); |
| 1588 | } |
| 1589 | ui.lit_was = -1; |
| 1590 | ui.lit = -1; |
| 1591 | ui.ripple_was = -1; |
| 1592 | /* the typed line's ink leaves from just above the bar, so the hint under it stays clear */ |
| 1593 | ck_flow_init(&ui.flow, ui.w, ui.h, ui.cx, ui.cy, ui.mind.R, ui.bar.x + ui.bar.w / 2, ui.bar.y - ui.bar.h / 2, 0); |
| 1594 | if (ck_allen_canvas_init(&stage, ui.w, ui.h, ui.k, ui.cx, ui.cy, &ui.voice, &ui.body, &ui.small)) |
| 1595 | return -1; |
| 1596 | seen_scene = note.scene_gen; |
| 1597 | ui.seen_input = note.input_gen, ui.seen_said = note.said_gen, ui.seen_wake = note.wake_gen; |
| 1598 | ui.seen_map = note.map_gen - 1; /* the map kept from before is taken by the first frame */ |
| 1599 | ui.seen_panel = note.panel_gen - 1; |
| 1600 | ui.started = 1; |
| 1601 | ui.ticket = 0; |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | void ck_allen_ui_refresh(void) |
| 1606 | { |
| 1607 | lock(); |
| 1608 | note.refresh = 1; |
| 1609 | unlock(); |
| 1610 | } |
| 1611 | |
| 1612 | void ck_allen_ui_sleep(int asleep, uint64_t now) |
| 1613 | { |
| 1614 | (void)now; |
| 1615 | lock(); |
| 1616 | __atomic_store_n(&ui.black_presented, 0, __ATOMIC_RELEASE); |
| 1617 | note.sleep = asleep != 0; |
| 1618 | note.sleep_gen++; |
| 1619 | unlock(); |
| 1620 | } |
| 1621 | int ck_allen_ui_asleep(void) { return __atomic_load_n(&ui.black_presented, __ATOMIC_ACQUIRE) != 0; } |
| 1622 | |
| 1623 | void ck_allen_ui_wake(const char *footer, uint64_t now) |
| 1624 | { |
| 1625 | (void)now; |
| 1626 | lock(); |
| 1627 | note.footer = footer; |
| 1628 | note.wake_gen++; |
| 1629 | unlock(); |
| 1630 | } |
| 1631 | |
| 1632 | void ck_allen_ui_input(const char *buf, unsigned len, uint64_t now) |
| 1633 | { |
| 1634 | (void)now; |
| 1635 | if (len > TEXT_MAX - 1) |
| 1636 | len = TEXT_MAX - 1; |
| 1637 | lock(); |
| 1638 | for (unsigned i = 0; i < len; i++) |
| 1639 | note.input[i] = buf[i]; |
| 1640 | note.input_len = len; |
| 1641 | note.input_gen++; |
| 1642 | unlock(); |
| 1643 | } |
| 1644 | |
| 1645 | void ck_allen_ui_said(const char *line, const char *reply, uint64_t now) |
| 1646 | { |
| 1647 | (void)now; |
| 1648 | lock(); |
| 1649 | note.has_line = line != 0; |
| 1650 | note.think = reply == 0; |
| 1651 | note.more = 0; |
| 1652 | copy(note.line, line, TEXT_MAX); |
| 1653 | copy(note.reply, reply ? reply : "", TEXT_MAX); |
| 1654 | note.said_input_gen = note.input_gen; |
| 1655 | note.said_gen++; |
| 1656 | unlock(); |
| 1657 | } |
| 1658 | |
| 1659 | void ck_allen_ui_think(const char *line, uint64_t now) { ck_allen_ui_said(line, 0, now); } |
| 1660 | |
| 1661 | void ck_allen_ui_said_more(const char *reply, uint64_t now) |
| 1662 | { |
| 1663 | (void)now; |
| 1664 | lock(); |
| 1665 | if (note.said_gen == ui.seen_said) { /* the window has the words so far: these go on from them */ |
| 1666 | note.has_line = 0; |
| 1667 | copy(note.line, 0, TEXT_MAX); |
| 1668 | note.more = 1; |
| 1669 | } /* else the last call has not been taken yet: it stays what it was, with these words */ |
| 1670 | note.think = 0; |
| 1671 | copy(note.reply, reply ? reply : "", TEXT_MAX); |
| 1672 | note.said_input_gen = note.input_gen; |
| 1673 | note.said_gen++; |
| 1674 | unlock(); |
| 1675 | } |
| 1676 | |
| 1677 | void ck_allen_ui_map(unsigned n, const uint8_t *ea, const uint8_t *eb, unsigned ne, int lit, const char *label, |
| 1678 | unsigned total, unsigned links) |
| 1679 | { |
| 1680 | lock(); |
| 1681 | note.n = n < CK_ALLEN_UI_MAP_NODES ? n : CK_ALLEN_UI_MAP_NODES; |
| 1682 | note.ne = 0; |
| 1683 | for (unsigned i = 0; i < ne && note.ne < CK_ALLEN_UI_MAP_LINKS; i++) |
| 1684 | if (ea[i] < note.n && eb[i] < note.n && ea[i] != eb[i]) { |
| 1685 | note.ea[note.ne] = ea[i]; |
| 1686 | note.eb[note.ne++] = eb[i]; |
| 1687 | } |
| 1688 | note.lit = lit >= 0 && (unsigned)lit < note.n ? lit : -1; |
| 1689 | copy(note.label, label, TEXT_MAX); |
| 1690 | note.total = total; |
| 1691 | note.links = links; |
| 1692 | note.map_gen++; |
| 1693 | unlock(); |
| 1694 | } |
| 1695 | |
| 1696 | void ck_allen_ui_named(const char *name) |
| 1697 | { |
| 1698 | static const char lead[] = "Speak to "; |
| 1699 | unsigned n = 0; |
| 1700 | for (; lead[n]; n++) |
| 1701 | ui.hint[n] = lead[n]; |
| 1702 | for (unsigned i = 0; name && name[i] && n + 1 < sizeof ui.hint; i++) |
| 1703 | ui.hint[n++] = name[i]; |
| 1704 | ui.hint[n] = 0; |
| 1705 | ui.hint_len = n; |
| 1706 | if (ui.started) |
| 1707 | mark(ui.bar); |
| 1708 | } |
| 1709 | |
| 1710 | void ck_allen_ui_scene(const char *scene, unsigned len, uint64_t now) |
| 1711 | { |
| 1712 | static ck_scene s; /* read outside the lock; one caller at a time, as for the others */ |
| 1713 | (void)now; |
| 1714 | ck_scene_parse(scene, len, &s, 0); |
| 1715 | lock(); |
| 1716 | note.has_line = 0; |
| 1717 | note.more = 0; |
| 1718 | note.think = 0; |
| 1719 | copy(note.reply, ck_scene_say(&s), TEXT_MAX); |
| 1720 | note.said_gen++; |
| 1721 | note.scene = s; |
| 1722 | note.scene_gen++; |
| 1723 | unlock(); |
| 1724 | } |
| 1725 | |
| 1726 | /* ---- sky time and mode ---- */ |
| 1727 | |
| 1728 | void ck_allen_ui_set_unix_time(int64_t unix_s, uint64_t tsc_at_read) |
| 1729 | { |
| 1730 | ui.sky_unix_s = unix_s; |
| 1731 | ui.sky_tsc = tsc_at_read; |
| 1732 | } |
| 1733 | |
| 1734 | void ck_allen_ui_sky_mode(uint8_t mode, uint8_t region, int32_t lat_udeg, int32_t lon_udeg) |
| 1735 | { |
| 1736 | ui.sky_mode = mode; |
| 1737 | ui.sky_region = region; |
| 1738 | ui.sky_lat_udeg = lat_udeg; |
| 1739 | ui.sky_lon_udeg = lon_udeg; |
| 1740 | ui.sky_view_tsc = 0; /* force rebuild on the next tick */ |
| 1741 | } |
| 1742 | |
| 1743 | void ck_allen_ui_panel(const ck_allen_ui_panel_view *p, uint64_t now) |
| 1744 | { |
| 1745 | (void)now; |
| 1746 | lock(); |
| 1747 | struct panel *d = ¬e.panel; |
| 1748 | d->on = p != 0; |
| 1749 | d->nline = d->nchoice = 0; |
| 1750 | d->lit = 0; |
| 1751 | d->hint[0] = d->aside[0] = d->prompt[0] = 0; |
| 1752 | if (p) { |
| 1753 | for (unsigned i = 0; i < p->nline && i < CK_ALLEN_UI_PANEL_LINES; i++) |
| 1754 | copy(d->line[d->nline++], p->line[i], PL_MAX); |
| 1755 | for (unsigned i = 0; i < p->nchoice && i < CK_ALLEN_UI_CHOICES; i++) |
| 1756 | copy(d->choice[d->nchoice++], p->choice[i], PC_MAX); |
| 1757 | d->lit = p->lit >= 0 && (unsigned)p->lit < d->nchoice ? p->lit : 0; |
| 1758 | copy(d->hint, p->hint, PL_MAX); |
| 1759 | copy(d->aside, p->aside, sizeof d->aside); |
| 1760 | copy(d->prompt, p->prompt, sizeof d->prompt); |
| 1761 | } |
| 1762 | note.panel_gen++; |
| 1763 | unlock(); |
| 1764 | } |
| 1765 | |
| 1766 | void ck_allen_ui_code(const char *text, unsigned bytes, const char *lang, uint64_t now) |
| 1767 | { |
| 1768 | (void)now; |
| 1769 | lock(); |
| 1770 | note.code_on = text != 0; |
| 1771 | if (text) { |
| 1772 | unsigned n = bytes < CK_ALLEN_UI_CODE_MAX ? bytes : CK_ALLEN_UI_CODE_MAX; |
| 1773 | for (unsigned i = 0; i < n; i++) { |
| 1774 | char ch = text[i]; |
| 1775 | note_code[i] = ch == '\n' || (ch >= 32 && ch <= 126) ? ch : '?'; |
| 1776 | } |
| 1777 | note_code[n] = 0; |
| 1778 | note.code_len = n; |
| 1779 | note.code_top = note.code_rows = note.code_page = 0; |
| 1780 | copy(note.code_lang, lang ? lang : "", sizeof note.code_lang); |
| 1781 | } |
| 1782 | note.code_gen++; |
| 1783 | unlock(); |
| 1784 | } |
| 1785 | |
| 1786 | int ck_allen_ui_code_scroll(int rows, unsigned *top, unsigned *total, unsigned *page) |
| 1787 | { |
| 1788 | /* the keys move the top row; the frame takes it with its next notes */ |
| 1789 | lock(); |
| 1790 | int on = note.code_on && note.code_page; |
| 1791 | if (on) { |
| 1792 | int32_t pg = (int32_t)note.code_page > 1 ? (int32_t)note.code_page - 1 : 1; |
| 1793 | int32_t d = rows == CK_ALLEN_UI_CODE_PAGE ? pg : rows == -CK_ALLEN_UI_CODE_PAGE ? -pg : rows; |
| 1794 | note.code_top = ck_code_scroll(note.code_top, d, note.code_rows, note.code_page); |
| 1795 | if (top) |
| 1796 | *top = note.code_top; |
| 1797 | if (total) |
| 1798 | *total = note.code_rows; |
| 1799 | if (page) |
| 1800 | *page = note.code_page; |
| 1801 | } |
| 1802 | int r = on ? (int)note.code_top : -1; |
| 1803 | unlock(); |
| 1804 | return r; |
| 1805 | } |
| 1806 | |
| 1807 | int ck_allen_ui_code_on(void) { return note.code_on; } |
| 1808 | |
| 1809 | void ck_allen_ui_working(int on) |
| 1810 | { |
| 1811 | lock(); |
| 1812 | note.working = on; |
| 1813 | unlock(); |
| 1814 | } |