C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_notcurses.c
Go to the documentation of this file.
1/** @file ui_notcurses.c
2 @ingroup ui_notcurses
3 @brief NotCurses UI backend — lifecycle, surface management, and capabilities.
4
5 Implements all UiRuntime and UiSurface operations declared in ui_backend.h
6 using the NotCurses library.
7*/
8
9#include <iso646.h>
10#define _XOPEN_SOURCE_EXTENDED 1
11
12#include "cm.h"
13#include "ui_backend.h"
16#include <errno.h>
17#include <inttypes.h>
18#include <locale.h>
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
24UiRuntime *ui = NULL;
25uint ui_color_cnt = 0;
26uint ui_pair_cnt = 0;
29UiSurface *stdsfc;
30uint LINES, COLS;
31UiRuntime *ui;
32UiConfig *ui_config;
34UiCell bkgd_cell;
35
36int sfc_ptr = -1;
38
39void ui_plane_get_abs_yx(UiSurface *s, ss_t w, int *absy, int *absx);
40
41/* -------------------------------------------------------------------------
42 Backend identification and capability query
43 ------------------------------------------------------------------------- */
44/** @brief Get the current UI backend in use.
45 * @return The current UI backend as an enum value of type UiBackend.
46 *
47 * This function returns the current UI backend being used by the application.
48 * In this implementation, it always returns UI_BACKEND_NOTCURSES, indicating
49 * that the NotCurses library is being used as the UI backend.
50 */
51UiBackend ui_get_backend() {
53}
54/** @brief Get the capabilities of the current UI backend.
55 * @param caps Pointer to a UiCaps structure to be filled with the capabilities.
56 *
57 * This function populates the provided UiCaps structure with information about
58 * the capabilities of the current UI backend. It sets various fields in the
59 * structure, such as truecolor support, palette size, mouse support, Unicode
60 * support, and resize capability. If the provided pointer is NULL or if the
61 * UI runtime is not initialized, the function does nothing.
62 */
63void ui_get_caps(UiCaps *caps) {
64 if (!caps)
65 return;
66 memset(caps, 0, sizeof(*caps));
67 if (!ui)
68 return;
69 caps->truecolor = true;
70 caps->palette256 = true;
71 caps->mouse = ui->mouse_enabled;
72 caps->unicode = true;
73 caps->resize = true;
75 caps->pad_relative_yx = true;
76}
77/* -------------------------------------------------------------------------
78 Lifecycle
79 ------------------------------------------------------------------------- */
80/** @brief Initialize the UI runtime with the specified configuration and SIO.
81 * @param cfg Pointer to a UiConfig structure containing configuration options.
82 * @param sio Pointer to a SIO structure for input/output operations.
83 * @return Pointer to the initialized UiRuntime structure, or NULL on failure.
84 *
85 * This function initializes the UI runtime using the NotCurses library. It sets
86 * up the terminal, creates the standard plane, and initializes various UI
87 * components based on the provided configuration. If any step fails, it cleans
88 * up and returns NULL.
89 */
90UiRuntime *ui_init(const UiConfig *cfg, SIO *sio) {
91 setlocale(LC_ALL, "en_US.UTF-8");
92 ui = calloc(1, sizeof(*ui));
93 if (!ui)
94 return NULL;
95 char tty_name[MAXLEN];
96 if (ttyname_r(STDIN_FILENO, tty_name, sizeof(tty_name)) != 0) {
97 free(ui);
98 ui = NULL;
99 return NULL;
100 }
101 ui->tty_fp = fopen(tty_name, "r+");
102 if (ui->tty_fp == NULL) {
103 ui = NULL;
104 free(ui);
105 return NULL;
106 }
108 NotCursesOptions nc_opts = {
109 .flags = NCOPTION_SUPPRESS_BANNERS |
110 NCOPTION_NO_QUIT_SIGHANDLERS,
111 };
112 ui->nc = notcurses_init(&nc_opts, ui->tty_fp);
113 if (ui->nc == NULL) {
114 free(ui);
115 ui = NULL;
116 return NULL;
117 }
118 f_notcurses_open = true;
119 stdplane = notcurses_stdplane(ui->nc);
120 notcurses_render(ui->nc);
121
122 if (ui->nc == NULL) {
123 fclose(ui->tty_fp);
124 ui->tty_fp = NULL;
125 free(ui);
126 ui = NULL;
127 return NULL;
128 }
129 notcurses_mice_enable(ui->nc, NCMICE_BUTTON_EVENT);
130 notcurses_cursor_disable(ui->nc);
131 notcurses_stddim_yx(ui->nc, &ui->lines, &ui->cols);
132 stdsfc = calloc(1, sizeof(*stdsfc));
133 if (!stdsfc)
134 return NULL;
135 for (int i = 0; i < SUB_SFC_MAX; i++) {
136 stdsfc->mplane[i] = NULL;
137 stdsfc->meta[i].lines = 0;
138 stdsfc->meta[i].cols = 0;
139 stdsfc->meta[i].y = 0;
140 stdsfc->meta[i].x = 0;
141 stdsfc->meta[i].hidden = false;
142 }
143 stdsfc->runtime = ui;
144 stdsfc->parent = NULL;
145 stdsfc->meta[BOX].lines = ui->lines;
146 stdsfc->meta[BOX].cols = ui->cols;
147 stdsfc->meta[BOX].y = 0;
148 stdsfc->meta[BOX].x = 0;
149 stdsfc->mplane[BOX] = stdplane;
150 if (!stdsfc->mplane[BOX]) {
151 notcurses_stop(ui->nc);
152 f_notcurses_open = false;
153 return NULL;
154 }
155 sfc_ptr = -1;
156 LINES = ui->lines;
157 COLS = ui->cols;
158 ui_pair = calloc(UI_PAIRS, sizeof(UiPair));
159 if (!ui_pair) {
160 free(ui_pair);
161 notcurses_stop(ui->nc);
162 f_notcurses_open = false;
163 free(ui);
164 return NULL;
165 }
166 ui_pair_cnt = 0;
167 ui_color = calloc(UI_COLORS, sizeof(UiColor));
168 if (!ui_color) {
169 free(ui_pair);
170 notcurses_stop(ui->nc);
171 f_notcurses_open = false;
172 free(ui);
173 return NULL;
174 }
175 ui_color_cnt = 0;
176 if (cfg->border_style)
177 ui->border_style = cfg->border_style;
178 if (!ui->border_style)
179 ui->border_style = UI_BORDER_ROUNDED;
180 if (cfg) {
181 switch (ui->border_style) {
182 case UI_BORDER_SINGLE:
183 memcpy(bw.str, border_single, sizeof(bw.str));
184 break;
185 case UI_BORDER_DOUBLE:
186 memcpy(bw.str, border_double, sizeof(bw.str));
187 break;
188 case UI_BORDER_ROUNDED:
189 memcpy(bw.str, border_rounded, sizeof(bw.str));
190 break;
191 case UI_BORDER_HEAVY:
192 memcpy(bw.str, border_heavy, sizeof(bw.str));
193 break;
194 case UI_BORDER_NONE:
195 memcpy(bw.str, border_none, sizeof(bw.str));
196 break;
197 default:
198 memcpy(bw.str, border_rounded, sizeof(bw.str));
199 break;
200 }
201 }
202 ui->tty_fd = notcurses_inputready_fd(ui->nc);
204 stdsfc = calloc(1, sizeof(*stdsfc));
205 return ui;
206}
207
208/* -------------------------------------------------------------------------
209 Surface Creation and Destruction
210 ------------------------------------------------------------------------- */
211/** @brief Create a new UI surface with the specified parameters.
212 * @param w The index of the surface to create.
213 * @param parent Pointer to the parent UiSurface, or NULL for no parent.
214 * @param p The index of the parent plane to attach to.
215 * @param lines The number of lines (rows) for the new surface.
216 * @param cols The number of columns for the new surface.
217 * @param y The y-coordinate for the new surface's position.
218 * @param x The x-coordinate for the new surface's position.
219 * @return Pointer to the newly created UiSurface, or NULL on failure.
220 *
221 * This function creates a new UI surface and initializes its properties based
222 * on the provided parameters. It allocates memory for the surface, sets up its
223 * metadata, and creates a new ncplane for rendering. If any step fails, it
224 * cleans up and returns NULL.
225 */
226UiSurface *ui_surface_new(ss_t w, UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x) {
227 if (!ui)
228 return NULL;
229 UiSurface *s = calloc(1, sizeof(*s));
230 if (!s) {
231 ui_log(ERROR, "Failed to allocate memory for UiSurface\n");
232 return NULL;
233 }
234 ui_log(INFO, _("Memory allocated for new surface"));
235 s->runtime = ui;
236 s->parent = parent;
237 UiLogLevel level = INFO;
238 ncplane_options plane_opts = {
239 .y = y,
240 .x = x,
241 .rows = lines,
242 .cols = cols};
243 s->meta[w].y = y;
244 s->meta[w].x = x;
245 s->meta[w].lines = lines;
246 s->meta[w].cols = cols;
247 s->meta[w].hidden = false;
248 if (parent && parent->mplane[p]) {
249 s->mplane[w] = ncplane_create(parent->mplane[p], &plane_opts);
250 if (!s->mplane[w]) {
251 level = ERROR;
252 ui_log(level, "%s = ncplane_create(parent=%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], ss_n[p], lines, cols, y, x);
253 notcurses_stop(ui->nc);
254 f_notcurses_open = false;
255 return NULL;
256 }
257 ui_log(level, "%s = ncplane_create(parent=%s, lines=%d, cols=%d, begy=%d, begx=%d) success", ss_n[w], ss_n[p], lines, cols, y, x);
258 ncplane_set_base(s->mplane[PAD], " ", 0, cell_nt.channels);
259 ncplane_set_channels(s->mplane[PAD], cell_nt.channels);
260 if (!s->mplane[w]) {
261 return NULL;
262 }
263 } else {
264 stdplane = notcurses_stdplane(ui->nc);
265 s->mplane[w] = ncplane_create(stdplane, &plane_opts);
266 if (!s->mplane[w]) {
267 level = ERROR;
268 ui_log(level, "%s = ncplane_create(parent=%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], "stdplane", lines, cols, y, x);
269 return NULL;
270 }
271 ui_log(level, "%s = ncplane_create(parent=%s, lines=%d, cols=%d, begy=%d, begx=%d) success", ss_n[w], ss_n[p], lines, cols, y, x);
272 }
273 if (!s->mplane[w]) {
274 notcurses_stop(ui->nc);
275 f_notcurses_open = false;
276 return NULL;
277 }
278 return s;
279}
280/** @brief Create a new UI surface with a border box and optional title.
281 * @param parent Pointer to the parent UiSurface, or NULL for no parent.
282 * @param p The index of the parent plane to attach to.
283 * @param lines The number of lines (rows) for the new surface.
284 * @param cols The number of columns for the new surface.
285 * @param y The y-coordinate for the new surface's position.
286 * @param x The x-coordinate for the new surface's position.
287 * @param wtitle The title to display on the border box, or NULL for no title.
288 * @return Pointer to the newly created UiSurface, or NULL on failure.
289 *
290 * This function creates a new UI surface with a border box and an optional
291 * title. It allocates memory for the surface, sets up its metadata, and creates
292 * a new ncplane for rendering. If any step fails, it cleans up and returns NULL.
293 */
294UiSurface *ui_surface_box(UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x, const char *wtitle) {
295 if (!ui)
296 return NULL;
297 UiSurface *s = calloc(1, sizeof(*s));
298 if (!s)
299 return NULL;
300 s->runtime = ui;
301 s->parent = parent;
302 UiLogLevel level = INFO;
303 ncplane_options plane_opts = {
304 .y = y,
305 .x = x,
306 .rows = lines,
307 .cols = cols,
308 .name = NULL};
309 s->meta[BOX].y = y;
310 s->meta[BOX].x = x;
311 s->meta[BOX].lines = lines + 2;
312 s->meta[BOX].cols = cols + 2;
313 s->meta[BOX].hidden = false;
314 strnz__cpy(s->meta[BOX].title, wtitle, MAXLEN - 1);
315 strnz__cpy(s->meta[BOX].name, "BOX", XLEN - 1);
316 ss_t w = BOX;
317 if (parent && parent->mplane[p]) {
318 s->mplane[w] = ncplane_create(parent->mplane[p], &plane_opts);
319 } else {
320 stdplane = notcurses_stdplane(ui->nc);
321 s->mplane[w] = ncplane_create(stdplane, &plane_opts);
322 }
323 if (!s->mplane[w]) {
324 level = ERROR;
325 ui_log(level, "ss_n[%s] = ncplane_create(%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], ss_n[p], lines, cols, y, x);
326 free(s);
327 return NULL;
328 }
329 ui_log(level, "ss_n[%s] = ncplane_create(%s, lines=%d, cols=%d, begy=%d, begx=%d) success", ss_n[w], ss_n[p], lines, cols, y, x);
330 ncplane_set_channels(s->mplane[w], cell_box.channels);
332 ui_border_title(s, w, wtitle);
334 return s;
335}
336/** @brief Add padding to an existing UI surface.
337 * @param s Pointer to the UiSurface to which padding will be added.
338 * @param w The index of the new padded surface.
339 * @param p The index of the parent plane to attach to.
340 * @param lines The number of lines (rows) for the padded surface.
341 * @param cols The number of columns for the padded surface.
342 * @param y The y-coordinate for the padded surface's position.
343 * @param x The x-coordinate for the padded surface's position.
344 * @return 0 on success, -1 on failure.
345 *
346 * This function adds padding to an existing UI surface by creating a new
347 * ncplane with the specified dimensions and position. It updates the metadata
348 * for the new padded surface and sets its background and scrolling properties.
349 * If any step fails, it cleans up and returns -1.
350 */
351int ui_surface_addpad(UiSurface *s, ss_t pad, ss_t win, uint lines, uint cols, uint y, uint x, bool hidden) {
352 uint plines, pcols;
353 ui_surface_addwin(s, VIEWPORT, win, lines, s->meta[win].cols - x, y, x);
354 int offset = 0;
355 ui_surface_addwin(s, pad, VIEWPORT, lines, cols, 0, 0);
356 ncplane_dim_yx(stdplane, &plines, &pcols);
357 if (hidden) {
358 s->meta[pad].hidden = true;
359 int absy, absx;
360 ui_plane_get_abs_yx(s, VIEWPORT, &absy, &absx);
361 offset = absy + s->meta[VIEWPORT].lines + 1;
362 ncplane_move_yx(s->mplane[pad], -offset, 0);
363 }
364 ui_log(INFO, "ncplane_move(%s, begy=%d, begx=%d) success", ss_n[pad], -offset - 1, 0);
365 ncplane_move_above(s->mplane[VIEWPORT], s->mplane[win]);
366 if (!hidden) {
367 ncplane_move_above(s->mplane[pad], s->mplane[VIEWPORT]);
368 ui_log(INFO, "ncplane_move_above(s->mplane[%s], s->mplane[%s]) success", ss_n[pad], ss_n[VIEWPORT]);
369 // PAD sits right on top of its background
370 ncplane_move_above(s->mplane[LNNO], s->mplane[pad]);
371 // Line numbers sit above PAD, clipping it on the left
372 ncplane_move_above(s->mplane[CMDLN], s->mplane[pad]);
373 // Command line sits above PAD, clipping it on the bottom
374 if (s->mplane[BOX])
375 ncplane_move_above(s->mplane[BOX], s->mplane[pad]);
376 ui_scrollok(s, pad, true);
377 }
378 return 0;
379}
380/** @brief Add a new window to an existing UI surface.
381 * @param s Pointer to the UiSurface to which the window will be added.
382 * @param w The index of the new window.
383 * @param p The index of the parent plane to attach to.
384 * @param lines The number of lines (rows) for the new window.
385 * @param cols The number of columns for the new window.
386 * @param y The y-coordinate for the new window's position.
387 * @param x The x-coordinate for the new window's position.
388 * @return 0 on success, -1 on failure.
389 *
390 * This function adds a new window to an existing UI surface by creating a new
391 * ncplane with the specified dimensions and position. It updates the metadata
392 * for the new window and sets its background properties. If any step fails, it
393 * cleans up and returns -1.
394 */
395int ui_surface_addwin(UiSurface *s, ss_t w, ss_t p, uint lines, uint cols, uint y, uint x) {
396 ncplane_options plane_opts = {
397 .y = y,
398 .x = x,
399 .rows = lines,
400 .cols = cols,
401 .name = NULL};
402 s->meta[w].y = y;
403 s->meta[w].x = x;
404 s->meta[w].lines = lines;
405 s->meta[w].cols = cols;
406 s->meta[w].hidden = false;
407 s->mplane[w] = ncplane_create(s->mplane[p], &plane_opts);
408 UiLogLevel level = INFO;
409 if (!s->mplane[w]) {
410 ui_log(level, "%s = ncplane_create(parent=%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], ss_n[p], lines, cols, y, x);
411 notcurses_stop(ui->nc);
412 f_notcurses_open = false;
413 return -1;
414 }
415 ui_log(level, "%s = ncplane_create(parent=%s, lines=%d, cols=%d, begy=%d, begx=%d) success", ss_n[w], ss_n[p], lines, cols, y, x);
416 return 0;
417}
418/** @brief Destroy a UI surface and free its resources.
419 * @param s Pointer to the UiSurface to be destroyed.
420 *
421 * This function destroys a UI surface by freeing its associated ncplanes and
422 * metadata. It also frees the memory allocated for the UiSurface structure.
423 * If the provided pointer is NULL, the function does nothing.
424 */
425void ui_endwin() {
427}
428/** @brief Shutdown the UI runtime and free all associated resources.
429 *
430 * This function shuts down the UI runtime by destroying all surfaces, freeing
431 * memory, and stopping the NotCurses library. It also closes the terminal file
432 * pointer if it was opened. If the UI runtime is not initialized, the function
433 * does nothing.
434 */
436 if (ui == NULL)
437 return;
438 while (sfc_ptr >= 0) {
439 if (ui_surface[sfc_ptr]) {
440 ui_log(INFO, _("Destroying surface: %d"), sfc_ptr);
442 ui_surface[sfc_ptr] = NULL;
443 }
444 sfc_ptr--;
445 }
446 if (stdsfc->mplane[BOX]) {
447 ui_log(INFO, _("Destroying mplane[%d]"), BOX);
448 ncplane_erase(stdsfc->mplane[BOX]);
449 ncplane_destroy(stdsfc->mplane[BOX]);
450 stdsfc->mplane[BOX] = NULL;
451 }
452 if (ui->tty_fp) {
453 // fclose(ui->tty_fp);
454 // ui->tty_fp = NULL;
455 }
456 if (stdsfc != NULL) {
457 ui_log(INFO, _("Destroying stdsfc"));
458 free(stdsfc);
459 stdsfc = NULL;
460 }
461 ui_log(INFO, _("Calling notcurses_stop"));
462 notcurses_stop(ui->nc);
463 f_notcurses_open = false;
464 if (ui != NULL) {
465 if (ui_pair != NULL) {
466 ui_log(INFO, _("Destroying ui_pair"));
467 free(ui_pair);
468 ui_pair = NULL;
469 }
470 if (ui_color != NULL) {
471 ui_log(INFO, _("Destroying ui_color"));
472 free(ui_color);
473 ui_color = NULL;
474 }
475 if (ui->sio) {
476 ui_log(INFO, _("Destroying ui->sio"));
477 free(ui->sio);
478 ui->sio = nullptr;
479 }
480 if (ui != NULL) {
481 ui_log(INFO, _("Destroying ui"));
482 free(ui);
483 ui = NULL;
484 }
485 }
486}
487/** @brief Destroy a UI surface and free its resources.
488 * @param s Pointer to the UiSurface to be destroyed.
489 *
490 * This function destroys a UI surface by freeing its associated ncplanes and
491 * metadata. It also frees the memory allocated for the UiSurface structure.
492 * If the provided pointer is NULL, the function does nothing.
493 */
494void ui_surface_destroy(UiSurface *s) {
495 if (!s)
496 return;
497 ss_t w = SUB_SFC_MAX - 1;
498 while (1) {
499 if (s->mplane[w] != NULL) {
500 ui_log(INFO, _("Destroying mplane[%d]"), w);
501 ncplane_erase(s->mplane[w]);
502 ncplane_destroy(s->mplane[w]);
503 s->mplane[w] = NULL;
504 }
505 if (w == BOX)
506 break;
507 w--;
508 };
509 if (s != NULL) {
510 ui_log(INFO, _("Destroying surface: s"));
511 free(s);
512 s = NULL;
513 }
514}
515/* -------------------------------------------------------------------------
516 Surface Navigation and Management
517 ------------------------------------------------------------------------- */
518/** @brief Move a UI surface to a new position on the screen.
519 * @param s Pointer to the UiSurface to be moved.
520 * @param w The index of the window within the surface to move.
521 * @param y The new y-coordinate for the surface's position.
522 * @param x The new x-coordinate for the surface's position.
523 * @return 0 on success, -1 on failure.
524 *
525 * This function moves a UI surface to a new position on the screen by updating
526 * its metadata and moving the associated ncplane. If the surface is hidden, it
527 * only updates the metadata without moving the ncplane. If any step fails, it
528 * returns -1.
529 */
530int ui_surface_move(UiSurface *s, ss_t w, uint y, uint x) {
531 if (!s)
532 return -1;
533 s->meta[w].y = y;
534 s->meta[w].x = x;
535 if (!s->meta[w].hidden)
536 return ncplane_move_yx(s->mplane[w], y, x) == 0 ? 0 : -1;
537 return 0;
538}
539/** @brief Resize a UI surface to new dimensions.
540 * @param s Pointer to the UiSurface to be resized.
541 * @param w The index of the window within the surface to resize.
542 * @param lines The new number of lines (rows) for the surface.
543 * @param cols The new number of columns for the surface.
544 * @return 0 on success, -1 on failure.
545 *
546 * This function resizes a UI surface to new dimensions by updating its metadata
547 * and resizing the associated ncplane. If any step fails, it returns -1.
548 */
549int ui_surface_resize(UiSurface *s, ss_t w, uint lines, uint cols) {
550 if (!s)
551 return -1;
552 return ncplane_resize_simple(s->mplane[w], (unsigned int)lines,
553 (unsigned int)cols) == 0
554 ? 0
555 : -1;
556}
557/** @brief Clear the contents of the standard plane.
558 * @return 0 on success, -1 on failure.
559 *
560 * This function clears the contents of the standard plane by erasing its
561 * contents. If the standard plane is not initialized, it returns -1.
562 */
563int ui_clear() {
564 if (!stdplane)
565 return -1;
566 ncplane_erase_region(stdplane, 0, 0, 0, 0);
567 return 0;
568}
569/** @brief Clear the contents of the specified window within a UI surface.
570 * @param s Pointer to the UiSurface containing the window to clear.
571 * @param w The index of the window to clear.
572 * @return 0 on success, -1 on failure.
573 *
574 * This function clears the contents of the specified window within a UI surface
575 * by erasing its contents. If the surface or window is not initialized, it
576 * returns -1.
577 */
578int ui_erase() {
579 if (!stdplane)
580 return -1;
581 ncplane_erase_region(stdplane, 0, 0, 0, 0);
582 return 0;
583}
584/** @brief Clear the contents of the specified window within a UI surface.
585 * @param s Pointer to the UiSurface containing the window to clear.
586 * @param w The index of the window to clear.
587 * @return 0 on success, -1 on failure.
588 *
589 * This function clears the contents of the specified window within a UI surface
590 * by erasing its contents. If the surface or window is not initialized, it
591 * returns -1.
592 */
593int ui_werase(UiSurface *s, ss_t w) {
594 if (!s)
595 return -1;
596 ncplane_erase_region(s->mplane[w], 0, 0, 0, 0);
597 return 0;
598}
599/** @brief Clear the contents of the specified window within a UI surface.
600 * @param s Pointer to the UiSurface containing the window to clear.
601 * @param w The index of the window to clear.
602 * @return 0 on success, -1 on failure.
603 *
604 * This function clears the contents of the specified window within a UI surface
605 * by erasing its contents. If the surface or window is not initialized, it
606 * returns -1.
607 */
608int ui_wclear(UiSurface *s, ss_t w) {
609 if (!s)
610 return -1;
611 ncplane_erase_region(s->mplane[w], 0, 0, 0, 0);
612 return 0;
613}
614/** @brief Move the specified window within a UI surface to the top of the z-order.
615 * @param s Pointer to the UiSurface containing the window to move.
616 * @param w The index of the window to move to the top.
617 * @return 0 on success, -1 on failure.
618 *
619 * This function moves the specified window within a UI surface to the top of
620 * the z-order, making it the foremost window. If the surface or window is not
621 * initialized, it returns -1.
622 */
623int ui_top_surface(UiSurface *s, ss_t w) {
624 if (!s)
625 return -1;
626 ncplane_move_top(s->mplane[w]);
627 return 0;
628}
629/** @brief Show the specified window within a UI surface.
630 * @param s Pointer to the UiSurface containing the window to show.
631 * @param w The index of the window to show.
632 * @return 0 on success, -1 on failure.
633 *
634 * This function shows the specified window within a UI surface by updating its
635 * metadata and moving it to its designated position. If the surface or window
636 * is not initialized, it returns -1.
637 */
638int ui_surface_show(UiSurface *s, ss_t w) {
639 if (!s)
640 return -1;
641 if (s->meta[w].hidden) {
642 s->meta[w].hidden = false;
643 ncplane_move_yx(s->mplane[w], s->meta[w].y, s->meta[w].x);
644 }
645 return 0;
646}
647/** @brief Hide the specified window within a UI surface.
648 * @param s Pointer to the UiSurface containing the window to hide.
649 * @param w The index of the window to hide.
650 * @return 0 on success, -1 on failure.
651 *
652 * This function hides the specified window within a UI surface by updating its
653 * metadata and moving it far off-screen. If the surface or window is not
654 * initialized, it returns -1.
655 */
656int ui_surface_hide(UiSurface *s, ss_t w) {
657 if (!s)
658 return -1;
659 if (!s->meta[w].hidden) {
660 s->meta[w].hidden = true;
661 /* Move far off-screen so the plane does not obscure anything. */
662 ncplane_move_yx(s->mplane[w], -s->meta[w].lines - 1, 0);
663 }
664 return 0;
665}
666// -------------------------------------------------------------------------
667// Screen Navigation
668// -------------------------------------------------------------------------
669/** @brief Move the cursor to the specified position within a window of a UI surface.
670 * @param s Pointer to the UiSurface containing the window.
671 * @param w The index of the window within the surface.
672 * @param y The y-coordinate for the new cursor position.
673 * @param x The x-coordinate for the new cursor position.
674 * @return 0 on success, -1 on failure.
675 *
676 * This function moves the cursor to the specified position within a window of
677 * a UI surface. If the surface or window is not initialized, it returns -1.
678 */
679int ui_wmove(UiSurface *s, ss_t w, uint y, uint x) {
680 if (!s)
681 return -1;
682 if (ncplane_cursor_move_yx(s->mplane[w], y, x) != 0)
683 return -1;
684 return 0;
685}
686/** @brief Move the cursor to the specified position within a window of a UI surface.
687 * @param s Pointer to the UiSurface containing the window.
688 * @param w The index of the window within the surface.
689 * @param y The y-coordinate for the new cursor position.
690 * @param x The x-coordinate for the new cursor position.
691 * @return 0 on success, -1 on failure.
692 *
693 * This function moves the cursor to the specified position within a window of
694 * a UI surface. If the surface or window is not initialized, it returns -1.
695 */
696int ui_cursor_move(UiSurface *s, ss_t w, uint y, uint x) {
697 if (!s)
698 return -1;
699 if (ncplane_cursor_move_yx(s->mplane[w], y, x) != 0)
700 return -1;
701 return 0;
702}
703/** @brief Get the current cursor position within a window of a UI surface.
704 * @param s Pointer to the UiSurface containing the window.
705 * @param w The index of the window within the surface.
706 * @param y Pointer to an unsigned integer to store the y-coordinate of the cursor.
707 * @param x Pointer to an unsigned integer to store the x-coordinate of the cursor.
708 *
709 * This function retrieves the current cursor position within a window of a UI
710 * surface. If the surface or window is not initialized, it does nothing.
711 */
712void ui_cursor_yx(int *y, int *x) {
713 notcurses_cursor_yx(ui->nc, y, x);
714}
715/** @brief Get the absolute cursor position within a window of a UI surface.
716 * @param s Pointer to the UiSurface containing the window.
717 * @param w The index of the window within the surface.
718 * @param y Pointer to an integer to store the absolute y-coordinate of the cursor.
719 * @param x Pointer to an integer to store the absolute x-coordinate of the cursor.
720 *
721 * This function retrieves the absolute cursor position within a window of a UI
722 * surface. If the surface or window is not initialized, it does nothing.
723 */
724void ui_abs_yx(UiSurface *s, ss_t w, int *y, int *x) {
725 if (!s)
726 return;
727 ncplane_abs_yx(s->mplane[w], y, x);
728}
729void ui_plane_get_abs_yx(UiSurface *s, ss_t w, int *absy, int *absx) {
730 ncplane_translate(s->mplane[w], NULL, absy, absx);
731 return;
732}
733/** @brief Get the current cursor position within a window of a UI surface.
734 * @param s Pointer to the UiSurface containing the window.
735 * @param w The index of the window within the surface.
736 * @param y Pointer to an unsigned integer to store the y-coordinate of the cursor.
737 * @param x Pointer to an unsigned integer to store the x-coordinate of the cursor.
738 *
739 * This function retrieves the current cursor position within a window of a UI
740 * surface. If the surface or window is not initialized, it does nothing.
741 */
742void ui_getyx(UiSurface *s, ss_t w, uint *y, uint *x) {
743 if (!s)
744 return;
745 ncplane_cursor_yx(s->mplane[w], y, x);
746}
747/** @brief Get the maximum dimensions of a window within a UI surface.
748 * @param s Pointer to the UiSurface containing the window.
749 * @param w The index of the window within the surface.
750 * @param y Pointer to an unsigned integer to store the maximum number of lines (rows).
751 * @param x Pointer to an unsigned integer to store the maximum number of columns.
752 *
753 * This function retrieves the maximum dimensions of a window within a UI
754 * surface. If the surface or window is not initialized, it does nothing.
755 */
756void ui_getmaxyx(UiSurface *s, ss_t w, uint *y, uint *x) {
757 if (!s)
758 return;
759 ncplane_dim_yx(s->mplane[w], y, x);
760}
761/** @brief Get the maximum number of lines (rows) of a window within a UI surface.
762 * @param s Pointer to the UiSurface containing the window.
763 * @param w The index of the window within the surface.
764 * @return The maximum number of lines (rows) of the window, or -1 on failure.
765 *
766 * This function retrieves the maximum number of lines (rows) of a window
767 * within a UI surface. If the surface or window is not initialized, it returns
768 * -1.
769 */
770int ui_getmaxy(UiSurface *s, ss_t w) {
771 if (!s)
772 return -1;
773 uint y, x;
774 ncplane_dim_yx(s->mplane[w], &y, &x);
775 return y;
776}
777/** @brief Get the maximum number of columns of a window within a UI surface.
778 * @param s Pointer to the UiSurface containing the window.
779 * @param w The index of the window within the surface.
780 * @return The maximum number of columns of the window, or -1 on failure.
781 *
782 * This function retrieves the maximum number of columns of a window within a
783 * UI surface. If the surface or window is not initialized, it returns -1.
784 */
785int ui_getmaxx(UiSurface *s, ss_t w) {
786 if (!s)
787 return -1;
788 uint y, x;
789 ncplane_dim_yx(s->mplane[w], &y, &x);
790 return x;
791}
792/** @brief Scroll the contents of the specified window up or down by the specified
793 * number of lines.
794 * @param s The surface containing the window to scroll.
795 * @param w The index of the window to scroll.
796 * @param r The number of lines to scroll. Positive values scroll up, negative
797 * values scroll down.
798 * @return 0 on success, -1 on error.
799 *
800 * If r is positive, the contents of the plane are moved up, leaving blank lines
801 * at the bottom. In the view application, the view scope is moved toward end of
802 * file (eof). - scope_toward_eof()
803 *
804 * @note ui_wscrl(-n) - If r is negative, the contents of the plane are moved down,
805 * leaving blank lines at the top. In the view application, the view scope is
806 * moved toward beginning of file (bof). - scope_toward_bof()
807 */
808int ui_wscrl(UiSurface *s, ss_t w, int r) {
809 uint rows, cols;
810 ncplane_dim_yx(s->mplane[w], &rows, &cols);
811
812 if (r > 0) {
813 // Scroll up (toward EOF): notcurses has this natively
814 ncplane_scrollup(s->mplane[w], r);
815 return 0;
816 }
817
818 // Scroll down (toward BOF): shift content down by |r| rows
819 r = -r;
820 if ((uint)r >= rows) {
821 ncplane_erase(s->mplane[w]);
822 return 0;
823 }
824
825 // Copy rows bottom-up to avoid overwriting source before reading
826 for (int src_row = (int)(rows - r) - 1; src_row >= 0; src_row--) {
827 int dst_row = src_row + r;
828 for (uint col = 0; col < cols; col++) {
829 nccell c = NCCELL_TRIVIAL_INITIALIZER;
830 ncplane_at_yx_cell(s->mplane[w], src_row, col, &c);
831 ncplane_putc_yx(s->mplane[w], dst_row, col, &c);
832 nccell_release(s->mplane[w], &c);
833 }
834 }
835 // Blank the top r rows
836 for (int row = 0; row < r; row++) {
837 for (uint col = 0; col < cols; col++) {
838 ncplane_putchar_yx(s->mplane[w], row, col, ' ');
839 }
840 }
841 return 0;
842}
843
844/* -------------------------------------------------------------------------
845 Configuration Control
846 ------------------------------------------------------------------------- */
847/** @brief Enable or disable scrolling for the specified window within a UI surface.
848 * @param s Pointer to the UiSurface containing the window.
849 * @param w The index of the window within the surface.
850 * @param enable True to enable scrolling, false to disable scrolling.
851 * @return 0 on success, -1 on failure.
852 *
853 * This function enables or disables scrolling for the specified window within
854 * a UI surface. If the surface or window is not initialized, it returns -1.
855 */
856int ui_scrollok(UiSurface *s, ss_t w, bool enable) {
857 if (!s)
858 return -1;
859 ncplane_set_scrolling(s->mplane[w], enable);
860 return 0;
861}
862/** @brief Enable or disable the keypad for the specified window within a UI surface.
863 * @param s Pointer to the UiSurface containing the window.
864 * @param w The index of the window within the surface.
865 * @param enable True to enable the keypad, false to disable the keypad.
866 * @return 0 on success, -1 on failure.
867 *
868 * This function does nothing.
869 */
870int ui_idcok(UiSurface *s, ss_t w, bool enable) {
871 (void)s;
872 (void)w;
873 (void)enable;
874 return 0;
875}
876/** @brief Enable or disable the use of the keypad for the specified window within a UI surface.
877 * @param s Pointer to the UiSurface containing the window.
878 * @param w The index of the window within the surface.
879 * @param enable True to enable the use of the keypad, false to disable it.
880 * @return 0 on success, -1 on failure.
881 *
882 * This function does nothing.
883 */
884int ui_idlok(UiSurface *s, ss_t w, bool enable) {
885 (void)s;
886 (void)w;
887 (void)enable;
888 return 0;
889}
890/** @brief Set the scrolling region for the specified window within a UI surface.
891 * @param s Pointer to the UiSurface containing the window.
892 * @param w The index of the window within the surface.
893 * @param top The top line of the scrolling region.
894 * @param bottom The bottom line of the scrolling region.
895 * @return 0 on success, -1 on failure.
896 *
897 * This function does nothing.
898 */
899int ui_setscrreg(UiSurface *s, ss_t w, uint top, uint bottom) {
900 (void)s;
901 (void)w;
902 (void)top;
903 (void)bottom;
904 return 0;
905}
906/** @brief Enable or disable the keypad for the specified window within a UI surface.
907 * @param s Pointer to the UiSurface containing the window.
908 * @param w The index of the window within the surface.
909 * @param enable True to enable the keypad, false to disable it.
910 * @return 0 on success, -1 on failure.
911 *
912 * This function does nothing.
913 */
914int ui_keypad(UiSurface *s, ss_t w, bool enable) {
915 (void)s;
916 (void)w;
917 (void)enable;
918 return 0;
919}
920/* -------------------------------------------------------------------------
921 Screen management functions
922 ------------------------------------------------------------------------- */
923/** @brief Get the current screen size in lines and columns.
924 * @param lines Pointer to an unsigned integer to store the number of lines (rows).
925 * @param cols Pointer to an unsigned integer to store the number of columns.
926 *
927 * This function retrieves the current screen size in lines and columns.
928 */
929void ui_get_screen_size(uint *lines, uint *cols) {
930 if (!ui)
931 return;
932 unsigned int r = 0, c = 0;
933 notcurses_stddim_yx(ui->nc, &r, &c);
934 ui->lines = (int)r;
935 ui->cols = (int)c;
936 if (lines)
937 *lines = ui->lines;
938 if (cols)
939 *cols = ui->cols;
940 ui_log(INFO, "ui_get_screen_size(lines=%u, cols=%u)", *lines, *cols);
941}
942/** @brief Update the panels and render the UI.
943 *
944 * This function updates the panels and renders the UI.
945 */
947 if (!ui)
948 return;
949 notcurses_render(ui->nc);
950 ui_log(INFO, "notcurses_render(ui->nc)");
951}
952
953int ui_plane_cpy(NcPlane *dst, NcPlane *src, int dst_y, int dst_x, int src_y, int src_x, int ylen, int xlen) {
954 if (!dst || !src)
955 return -1;
956 for (int y = 0; y < ylen; y++) {
957 for (int x = 0; x < xlen; x++) {
958 nccell c = NCCELL_TRIVIAL_INITIALIZER;
959 ncplane_at_yx_cell(src, src_y + y, src_x + x, &c);
960 ncplane_putc_yx(dst, dst_y + y, dst_x + x, &c);
961 nccell_release(src, &c);
962 }
963 }
964 return 0;
965}
966
967/** @brief Render the UI.
968 *
969 * This function renders the UI.
970 */
971void ui_render() {
972 if (!ui)
973 return;
974 notcurses_render(ui->nc);
975 ui_log(INFO, "notcurses_render(ui->nc)");
976}
977int ui_prefresh(UiSurface *s, ss_t w, int pminrow, int pmincol, int sminrow, int smincol, int smaxrow, int smaxcol) {
978 if (!ui)
979 return -1;
980 if (s->meta[PAD].hidden == false)
981 ncplane_move_yx(s->mplane[w], -pminrow, -pmincol);
982 else {
983 int dst_y = sminrow;
984 int dst_x = smincol;
985 int src_y = pminrow;
986 int src_x = pmincol;
987 int ylen = smaxrow - sminrow;
988 int xlen = smaxcol - smincol;
989 ui_plane_cpy(s->mplane[VIEWPORT], s->mplane[PAD], dst_y, dst_x, src_y, src_x, ylen, xlen);
990 ui_log(INFO, "ui_plane_cpy(dst: %s, src: %s, dst_y=%d, dst_x=%d, src_y=%d, src_x=%d, ylen=%d, xlen=%d success)", ss_n[VIEWPORT], ss_n[PAD], pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol);
991 }
992 notcurses_render(ui->nc);
993 ui_log(INFO, "notcurses_render(ui->nc)");
994 return 0;
995}
996/** @brief Suspend the UI and leave the alternate screen.
997 *
998 * This function suspends the UI and leaves the alternate screen.
999 * It is typically used when the application needs to temporarily exit
1000 * the UI mode, such as when executing a shell command or displaying
1001 * a message outside of the UI context.
1002 *
1003 * @return 0 on success, -1 on failure.
1004 */
1006 if (!ui)
1007 return -1;
1008 notcurses_leave_alternate_screen(ui->nc);
1009 return 0;
1010}
1011/** @brief Resume the UI and enter the alternate screen.
1012 *
1013 * This function resumes the UI and enters the alternate screen.
1014 * It is typically used after a suspension of the UI, allowing
1015 * the application to return to its previous state and continue
1016 * rendering the UI.
1017 *
1018 * @return 0 on success, -1 on failure.
1019 */
1021 if (!ui)
1022 return -1;
1023 notcurses_enter_alternate_screen(ui->nc);
1024 notcurses_render(ui->nc);
1025 return 0;
1026}
1028 return;
1029}
1030// -------------------------------------------------------------------------
1031// Cursor Control
1032// -------------------------------------------------------------------------
1033/** @brief Enable or disable the cursor visibility.
1034 * @param visible True to enable the cursor, false to disable it.
1035 * @return 0 on success, -1 on failure.
1036 *
1037 * This function enables or disables the cursor visibility. If the UI is not
1038 * initialized, it returns -1. When enabling the cursor, it retrieves the
1039 * current cursor position and enables the cursor at that position.
1040 */
1041int ui_curs_set(int visible) {
1042 if (!ui)
1043 return -1;
1044 if (visible == 0) {
1045 ui->cursor_visible = false;
1046 notcurses_cursor_disable(ui->nc);
1047 } else {
1048 ui->cursor_visible = true;
1049 int y, x;
1050 notcurses_cursor_yx(ui->nc, &y, &x);
1051 int rc = notcurses_cursor_enable(ui->nc, y, x);
1052 return rc;
1053 }
1054 return 0;
1055}
1056/** @brief Enable or disable the cursor at a specific position on the surface and plane specified.
1057 * @param s Pointer to the UiSurface containing the window.
1058 * @param w The index of the window within the surface.
1059 * @param y The y-coordinate for the cursor position.
1060 * @param x The x-coordinate for the cursor position.
1061 * @param visible True to enable the cursor, false to disable it.
1062 * @return 0 on success, -1 on failure.
1063 *
1064 * This function enables or disables the cursor at a specific position on the
1065 * surface and plane specified. If the UI is not initialized, it returns -1.
1066 */
1067int ui_cursor_enable_yx(UiSurface *s, ss_t w, uint y, uint x, bool visible) {
1068 if (!s)
1069 return -1;
1070 if (!visible) {
1071 ui->cursor_visible = false;
1072 notcurses_cursor_disable(ui->nc);
1073 } else {
1074 int yy, xx;
1075 ncplane_abs_yx(s->mplane[w], &yy, &xx);
1076 y += yy;
1077 x += xx;
1078 ui->cursor_visible = true;
1079 int rc = notcurses_cursor_enable(ui->nc, y, x);
1080 return rc;
1081 }
1082 return 0;
1083}
1084/** @brief Enable or disable the cursor at the current position on the surface and plane specified.
1085 * @param s Pointer to the UiSurface containing the window.
1086 * @param w The index of the window within the surface.
1087 * @param visible True to enable the cursor, false to disable it.
1088 * @return 0 on success, -1 on failure.
1089 *
1090 * This function enables or disables the cursor at the current position on the
1091 * surface and plane specified. If the UI is not initialized, it returns -1.
1092 */
1093int ui_cursor_enable(UiSurface *s, ss_t w, bool visible) {
1094 if (!s)
1095 return -1;
1096 if (!visible) {
1097 ui->cursor_visible = false;
1098 notcurses_cursor_disable(ui->nc);
1099 } else {
1100 uint y, x;
1101 ncplane_cursor_yx(s->mplane[w], &y, &x);
1102 int yy, xx;
1103 ncplane_abs_yx(s->mplane[w], &yy, &xx);
1104 y += yy;
1105 x += xx;
1106 ui->cursor_visible = true;
1107 return notcurses_cursor_enable(ui->nc, y, x) == 0 ? 0 : -1;
1108 }
1109 return 0;
1110}
1111/* -------------------------------------------------------------------------
1112 background
1113 ------------------------------------------------------------------------- */
1114/** @brief Set the background cell for the specified window within a UI surface.
1115 * @param s Pointer to the UiSurface containing the window.
1116 * @param w The index of the window within the surface.
1117 * @param cell Pointer to the UiCell representing the background cell.
1118 * @return 0 on success, -1 on failure.
1119 *
1120 * This function sets the background cell for the specified window within a UI
1121 * surface. It updates the styles and channels of the ncplane associated with
1122 * the window and stores the background cell in the metadata. If the surface is
1123 * not initialized, it returns -1.
1124 */
1125int ui_bkgd(UiSurface *s, ss_t w, const UiCell *cell) {
1126 if (!s)
1127 return -1;
1128 ncplane_set_styles(s->mplane[w], cell->stylemask);
1129 ncplane_set_channels(s->mplane[w], cell->channels);
1130 ncplane_set_base(s->mplane[w], " ",
1131 cell->stylemask, cell->channels);
1132 s->meta[w].bkgd_cell = *cell;
1133 return 0;
1134}
1135/** @brief Set the background cell for the specified window within a UI surface without changing the base character.
1136 * @param s Pointer to the UiSurface containing the window.
1137 * @param w The index of the window within the surface.
1138 * @param cell Pointer to the UiCell representing the background cell.
1139 * @return 0 on success, -1 on failure.
1140 *
1141 * This function sets the background cell for the specified window within a UI
1142 * surface without changing the base character. It updates the styles and
1143 * channels of the ncplane associated with the window and stores the background
1144 * cell in the metadata. If the surface is not initialized, it returns -1.
1145 */
1146int ui_bkgdset(UiSurface *s, ss_t w, const UiCell *cell) {
1147 if (!s)
1148 return -1;
1149 ncplane_set_styles(s->mplane[w], cell->stylemask);
1150 ncplane_set_channels(s->mplane[w], cell->channels);
1151 s->meta[w].bkgd_cell = *cell;
1152 // ncplane_set_base(s->mplane[w], " ",
1153 // cell->stylemask, cell->channels);
1154 return 0;
1155}
1156/** @brief Set the background cell for the specified window within a UI surface without changing the base character.
1157 * @param s Pointer to the UiSurface containing the window.
1158 * @param w The index of the window within the surface.
1159 * @param cell Pointer to the UiCell representing the background cell.
1160 * @return 0 on success, -1 on failure.
1161 *
1162 * This function sets the background cell for the specified window within a UI
1163 * surface without changing the base character. It updates the styles and
1164 * channels of the ncplane associated with the window and stores the background
1165 * cell in the metadata. If the surface is not initialized, it returns -1.
1166 */
1167int ui_bkgrnd(UiSurface *s, ss_t w, const UiCell *cell) {
1168 if (!s)
1169 return -1;
1170 ncplane_set_styles(s->mplane[w], cell->stylemask);
1171 ncplane_set_channels(s->mplane[w], cell->channels);
1172 ncplane_set_base(s->mplane[w], " ",
1173 cell->stylemask, cell->channels);
1174 return 0;
1175}
1176/** @brief Set the background cell for the specified window within a UI surface without changing the base character.
1177 * @param s Pointer to the UiSurface containing the window.
1178 * @param w The index of the window within the surface.
1179 * @param cell Pointer to the UiCell representing the background cell.
1180 * @return 0 on success, -1 on failure.
1181 *
1182 * This function sets the background cell for the specified window within a UI
1183 * surface without changing the base character. It updates the styles and
1184 * channels of the ncplane associated with the window and stores the background
1185 * cell in the metadata. If the surface is not initialized, it returns -1.
1186 */
1187int ui_bkgrndset(UiSurface *s, ss_t w, const UiCell *cell) {
1188 if (!s)
1189 return -1;
1190 ncplane_set_styles(s->mplane[w], cell->stylemask);
1191 ncplane_set_channels(s->mplane[w], cell->channels);
1192 ncplane_set_base(s->mplane[w], " ",
1193 cell->stylemask, cell->channels);
1194 return 0;
1195}
1196/* -------------------------------------------------------------------------
1197 Cell Manipulation
1198 ------------------------------------------------------------------------- */
1199/** @brief Get the properties of a cell at the current cursor position in a window of a UI surface.
1200 * @param sfc Pointer to the UiSurface containing the window.
1201 * @param w The index of the window within the surface.
1202 * @param cell Pointer to a UiCell structure to store the cell properties.
1203 * @param wstr Pointer to a wide character string to store the cell's character(s).
1204 * @param style Pointer to a UiStyle variable to store the cell's style attributes.
1205 * @param pair Pointer to a short variable to store the color pair index of the cell.
1206 * @return 0 on success, -1 on failure.
1207 *
1208 * This function retrieves the properties of a cell at the current cursor position
1209 * in a window of a UI surface. It fills in the provided UiCell structure with
1210 * the cell's properties, including its character(s), style attributes, and
1211 * color pair index. If any of the input pointers are NULL, it returns -1.
1212 */
1214 UiSurface *sfc,
1215 ss_t w,
1216 const UiCell *cell,
1217 wchar_t *wstr,
1218 UiStyle *style,
1219 short *pair) {
1220 (void)sfc;
1221 (void)w;
1222 if (!cell || !wstr || !style || !pair)
1223 return -1;
1224 GCluster gc;
1225 gc.u32 = cell->gcluster;
1226 *wstr = gc.w32;
1227 *style = cell->stylemask;
1228 // Convert the channels to a color pair index
1229 UiChannels channels;
1230 channels.fb = cell->channels;
1231 RGB rgb;
1232 rgb.r = channels.f_r;
1233 rgb.g = channels.f_g;
1234 rgb.b = channels.f_b;
1235 // If the color index does not exist, create a new one
1236 int fg = ui_color_from_rgb(&rgb);
1237 rgb.r = channels.b_r;
1238 rgb.g = channels.b_g;
1239 rgb.b = channels.b_b;
1240 // If the color index does not exist, create a new one
1241 int bg = ui_color_from_rgb(&rgb);
1242 // If the color pair does not exist, create a new one
1243 *pair = ui_add_pair(fg, bg);
1244 return 0;
1245}
1246/** @brief Set the properties of a cell at the current cursor position in a window of a UI surface.
1247 * @param sfc Pointer to the UiSurface containing the window.
1248 * @param w The index of the window within the surface.
1249 * @param cell Pointer to a UiCell structure representing the cell properties to set.
1250 * @param wstr Pointer to a wide character string representing the character(s) to set in the cell.
1251 * @param style The style attributes to set for the cell.
1252 * @param pair The color pair index to set for the cell.
1253 * @return 0 on success, -1 on failure.
1254 *
1255 * This function sets the properties of a cell at the current cursor position
1256 * in a window of a UI surface. It updates the specified UiCell structure with
1257 * the provided character(s), style attributes, and color pair index. If any
1258 * of the input pointers are NULL, it returns -1.
1259 */
1261 UiSurface *sfc,
1262 ss_t w,
1263 UiCell *cell,
1264 const wchar_t *wstr,
1265 const UiStyle style,
1266 short pair) {
1267 if (!cell)
1268 return -1;
1269 uint32_t fg, bg;
1270 nccell_init(cell);
1271 GCluster gc;
1272 unicode_to_utf8_gcluster(*wstr, &gc);
1273 nccell_set_styles(cell, style);
1274 UiChannels *chan = (UiChannels *)&cell->channels;
1275 ui_get_pair(pair, &fg, &bg);
1276 ui_color_content(fg, &chan->f_r, &chan->f_g, &chan->f_b);
1277 chan->f_a = 0x40;
1278 ui_color_content(bg, &chan->b_r, &chan->b_g, &chan->b_b);
1279 chan->b_a = 0x40;
1280 nccell_load_egc32(sfc->mplane[w], cell, gc.u32);
1281 return 0;
1282}
1283/** @brief Create a UiCell from a Unicode codepoint and specified foreground and background colors.
1284 * @param ucp Pointer to a wide character string representing the Unicode codepoint.
1285 * @param fg Pointer to a 32-bit unsigned integer representing the foreground color.
1286 * @param bg Pointer to a 32-bit unsigned integer representing the background color.
1287 * @return A UiCell structure representing the created cell.
1288 *
1289 * This function creates a UiCell from a Unicode codepoint and specified foreground
1290 * and background colors. It initializes the cell, sets its width based on the
1291 * Unicode codepoint, converts the codepoint to UTF-8, and sets the styles and
1292 * channels for the cell. The resulting UiCell can be used for rendering text
1293 * in a UI surface.
1294 */
1295UiCell ui_cell_from_ucp(const wchar_t *ucp, const uint32_t *fg, const uint32_t *bg) {
1296 nccell cell;
1297 nccell_init(&cell);
1298 GCluster *gc = (GCluster *)&cell.gcluster;
1299 wchar_t wstr[2] = {ucp[0], L'\0'};
1300 // wcwidth expects Unicode codepoints, not UTF-8 encoded bytes
1301 cell.width = wcwidth(wstr[0]);
1302 // unicode_to_utf8_gcluster converts Unicode codepoints to UTF-8
1303 unicode_to_utf8_gcluster(wstr[0], gc);
1304 nccell_set_styles(&cell, WA_NORMAL);
1305 UiChannels *chan = (UiChannels *)&cell.channels;
1306 chan->bargb = *bg;
1307 chan->fargb = *fg;
1308 chan->b_a = chan->f_a = 0x40;
1309 return cell;
1310}
1311/* -------------------------------------------------------------------------
1312 Colors, Color Pairs
1313 ------------------------------------------------------------------------- */
1314/** @brief Convert an RGB color to a color index in the UI color palette.
1315 * @param rgb Pointer to an RGB structure representing the color to convert.
1316 * @return The color index corresponding to the RGB color, or -1 if the color cannot be added.
1317 *
1318 * This function converts an RGB color to a color index in the UI color palette.
1319 * It applies gamma correction to the RGB values and checks if the color already
1320 * exists in the palette. If it does, it returns the existing index. If not, it
1321 * adds the new color to the palette and returns its index. If the palette is full,
1322 * it returns -1.
1323 */
1324uint ui_color_from_rgb(RGB *rgb) {
1325 uint i;
1326 RGB tmp;
1328 for (i = 0; i < ui_color_cnt && i < UI_COLORS; i++) {
1329 ui_color_content(i, &tmp.r, &tmp.g, &tmp.b);
1330 if (rgb->r == tmp.r && rgb->g == tmp.g && rgb->b == tmp.b)
1331 return i;
1332 }
1333 if (i < UI_COLORS) {
1334 if (i < 16) {
1335 std_color[i].r = rgb->r;
1336 std_color[i].g = rgb->g;
1337 std_color[i].b = rgb->b;
1338 }
1339 ui_init_color(i, rgb->r, rgb->g, rgb->b);
1340 if (ui_color_cnt + 1 < UI_COLORS)
1341 ui_color_cnt++;
1342 return ui_color_cnt - 1;
1343 }
1344 return -1;
1345}
1346/* ------------------------------------------------------------------------- */
1347/** @brief Add a new color pair to the UI color pair palette.
1348 * @param fg The foreground color index for the new color pair.
1349 * @param bg The background color index for the new color pair.
1350 * @return The index of the newly added color pair, or EXIT_FAILURE if the maximum number of pairs is exceeded.
1351 *
1352 * This function adds a new color pair to the UI color pair palette. It checks
1353 * if the specified foreground and background colors already exist in the palette.
1354 * If they do, it returns the existing index. If not, it adds the new color pair
1355 * and returns its index. If the maximum number of pairs is exceeded, it displays
1356 * an error message and returns EXIT_FAILURE.
1357 */
1358uint ui_add_pair(uint fg, uint bg) {
1359 uint16_t i;
1360 for (i = 1; i < ui_pair_cnt; i++) {
1361 if (ui_pair[i].fg == fg && ui_pair[i].bg == bg)
1362 return i;
1363 }
1364 if (i + 1 >= UI_PAIRS) {
1365 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
1366 ssnprintf(em1, MAXLEN - 1, _("NotCurses COLOR_PAIRS (%d) exceeded (%d)"),
1367 UI_PAIRS, i);
1368 strerror_r(errno, em2, MAXLEN);
1370 return (EXIT_FAILURE);
1371 }
1372 if (i < UI_PAIRS) {
1373 ui_pair[i].fg = fg;
1374 ui_pair[i].bg = bg;
1375 ui_pair_cnt++;
1376 }
1377 return ui_pair_cnt - 1;
1378}
1379/** @brief Get the foreground and background color indices for a given color pair index.
1380 * @param pair The color pair index.
1381 * @param fg Pointer to store the foreground color index.
1382 * @param bg Pointer to store the background color index.
1383 * @return 0 on success, -1 on error (if the pair index is out of bounds).
1384 */
1385int ui_get_pair(uint16_t pair, uint *fg, uint *bg) {
1386 *fg = ui_pair[pair].fg;
1387 *bg = ui_pair[pair].bg;
1388 return 0;
1389}
1390/** @brief Get the combined foreground and background color channels for a given color pair index.
1391 * @param pair The color pair index.
1392 * @return A 64-bit unsigned integer representing the combined foreground and background color channels.
1393 *
1394 * This function retrieves the combined foreground and background color channels
1395 * for a given color pair index. It uses the ui_get_pair function to obtain the
1396 * foreground and background color indices, then retrieves their RGB values and
1397 * constructs a UiChannels structure. The resulting combined channels are returned
1398 * as a 64-bit unsigned integer.
1399 */
1400uint64_t ui_get_channels_from_pair(uint16_t pair) {
1401 uint fg, bg;
1402 // ui_channels is a struct that holds the foreground and background color
1403 // channels
1404 // use channels.fb to get the combined foreground and background color channels
1405 UiChannels ui_channels = {};
1406 ui_get_pair(pair, &fg, &bg);
1407 ui_color_content(fg, &ui_channels.f_r, &ui_channels.f_g, &ui_channels.f_b);
1408 ui_channels.f_a = 0x40;
1409 ui_channels.f_r = ui_color[fg].r;
1410 ui_channels.f_g = ui_color[fg].g;
1411 ui_channels.f_b = ui_color[fg].b;
1412 bg = ui_pair[pair].bg;
1413 ui_color_content(bg, &ui_channels.b_r, &ui_channels.b_g, &ui_channels.b_b);
1414 ui_channels.b_a = 0x40;
1415 ui_channels.b_r = ui_color[bg].r;
1416 ui_channels.b_g = ui_color[bg].g;
1417 ui_channels.b_b = ui_color[bg].b;
1418 return ui_channels.fb;
1419}
1420/** @brief Initialize a color from a hexadecimal string representation.
1421 * @param s A string representing the color in hexadecimal format (e.g., "#RRGGBB").
1422 * @return The index of the initialized color in the UI color palette, or 0 if the color cannot be added.
1423 *
1424 * This function initializes a color from a hexadecimal string representation.
1425 * It converts the hex string to an RGB structure, applies gamma correction,
1426 * and checks if the color already exists in the UI color palette. If it does,
1427 * it returns the existing index. If not, it adds the new color to the palette
1428 * and returns its index. If the palette is full, it returns 0.
1429 */
1430uint ui_init_color_hex(char *s) {
1431 RGB rgb;
1432 rgb = ui_hex_to_rgb(s);
1433 ui_apply_gamma(&rgb);
1434 uint i;
1435 for (i = 0; i < ui_color_cnt && i < UI_COLORS; i++) {
1436 if (rgb.r == ui_color[i].r && rgb.g == ui_color[i].g && rgb.b == ui_color[i].b)
1437 return i;
1438 }
1439 if (i < UI_COLORS) {
1440 if (i < 16) {
1441 std_color[i].r = rgb.r;
1442 std_color[i].g = rgb.g;
1443 std_color[i].b = rgb.b;
1444 }
1445 ui_color[i].r = rgb.r;
1446 ui_color[i].g = rgb.g;
1447 ui_color[i].b = rgb.b;
1448 if (ui_color_cnt + 1 < UI_COLORS)
1449 ui_color_cnt++;
1450 return ui_color_cnt - 1;
1451 }
1452 return 0;
1453}
1454/** @brief Convert a hexadecimal color string to an RGB structure.
1455 * @param s A string representing the color in hexadecimal format (e.g., "#RRGGBB").
1456 * @return An RGB structure containing the red, green, and blue components of the color.
1457 *
1458 * This function converts a hexadecimal color string to an RGB structure. It
1459 * uses sscanf to parse the hex string and extract the red, green, and blue
1460 * components. The resulting RGB structure is returned.
1461 */
1462RGB ui_hex_to_rgb(char *s) {
1463 RGB rgb;
1464 sscanf(s, "#%02hhX%02hhX%02hhX", &rgb.r, &rgb.g, &rgb.b);
1465 return rgb;
1466}
1467/** @brief Get the RGB components of a color from the UI color palette.
1468 * @param color The index of the color in the UI color palette.
1469 * @param r Pointer to store the red component of the color.
1470 * @param g Pointer to store the green component of the color.
1471 * @param b Pointer to store the blue component of the color.
1472 * @return 0 on success, -1 on error (if the color index is out of bounds).
1473 *
1474 * This function retrieves the RGB components of a color from the UI color
1475 * palette. It checks if the specified color index is valid and then fills
1476 * in the provided pointers with the corresponding RGB values. If the color
1477 * index is out of bounds, it returns -1.
1478 */
1479int ui_color_content(uint16_t color, uint8_t *r, uint8_t *g, uint8_t *b) {
1480 if (color + 1 >= UI_COLORS)
1481 return -1;
1482 *r = ui_color[color].r;
1483 *g = ui_color[color].g;
1484 *b = ui_color[color].b;
1485 return 0;
1486}
1487/** @brief Initialize a color in the UI color palette with specified RGB components.
1488 * @param color The index of the color to initialize in the UI color palette.
1489 * @param r The red component of the color (0-255).
1490 * @param g The green component of the color (0-255).
1491 * @param b The blue component of the color (0-255).
1492 * @return 0 on success, -1 on error (if the color index is out of bounds).
1493 *
1494 * This function initializes a color in the UI color palette with specified
1495 * RGB components. It checks if the specified color index is valid and then
1496 * sets the corresponding RGB values in the ui_color array. If the color index
1497 * is out of bounds, it returns -1.
1498 */
1499int ui_init_color(uint16_t color, uint8_t r, uint8_t g, uint8_t b) {
1500 if (color + 1 >= UI_COLORS)
1501 return -1;
1502 ui_color[color].r = r;
1503 ui_color[color].g = g;
1504 ui_color[color].b = b;
1505 return 0;
1506}
1507/** @brief Get the foreground and background color indices for a given color pair index.
1508 * @param pair The color pair index.
1509 * @param fg Pointer to store the foreground color index.
1510 * @param bg Pointer to store the background color index.
1511 * @return 0 on success, -1 on error (if the pair index is out of bounds).
1512 *
1513 * This function retrieves the foreground and background color indices for a
1514 * given color pair index. It checks if the specified pair index is valid and
1515 * then fills in the provided pointers with the corresponding foreground and
1516 * background color indices. If the pair index is out of bounds, it returns -1.
1517 */
1518int ui_pair_content(uint16_t pair, uint *fg, uint *bg) {
1519 if (pair + 1 >= UI_PAIRS)
1520 return -1;
1521 *fg = ui_pair[pair].fg;
1522 *bg = ui_pair[pair].bg;
1523 return 0;
1524}
1525/** @brief Initialize a color pair in the UI color pair palette with specified foreground and background colors.
1526 * @param pair The index of the color pair to initialize in the UI color pair palette.
1527 * @param fg The foreground color index for the color pair.
1528 * @param bg The background color index for the color pair.
1529 * @return 0 on success, -1 on error (if the pair index is out of bounds).
1530 *
1531 * This function initializes a color pair in the UI color pair palette with
1532 * specified foreground and background colors. It checks if the specified
1533 * pair index is valid and then sets the corresponding foreground and background
1534 * color indices in the ui_pair array. If the pair index is out of bounds,
1535 * it returns -1.
1536 */
1537int ui_init_pair(uint16_t pair, uint fg, uint bg) {
1538 if (pair + 1 >= UI_PAIRS)
1539 return -1;
1540 ui_pair[pair].fg = fg;
1541 ui_pair[pair].bg = bg;
1542 return 0;
1543}
1544/** @brief Change the color at a specified index in the UI color palette.
1545 * @param color_idx The index of the color to change in the UI color palette.
1546 * @param color Pointer to a 32-bit unsigned integer representing the new color value.
1547 * @return 0 on success, -1 on error (if the color index is out of bounds).
1548 *
1549 * This function changes the color at a specified index in the UI color palette.
1550 * It applies gamma correction to the new color value and updates both the
1551 * standard and UI color arrays with the new RGB components. If the color index
1552 * is out of bounds, it returns -1.
1553 */
1554int ui_chg_color(uint16_t color_idx, uint32_t *color) {
1555 RGB rgb;
1556 rgb.color = *color;
1557 if (color_idx + 1 >= UI_COLORS)
1558 return -1;
1559 ui_apply_gamma(&rgb);
1560 if (color_idx < 16) {
1561 std_color[color_idx].r = rgb.r;
1562 std_color[color_idx].g = rgb.g;
1563 std_color[color_idx].b = rgb.b;
1564 }
1565 ui_color[color_idx].r = rgb.r;
1566 ui_color[color_idx].g = rgb.g;
1567 ui_color[color_idx].b = rgb.b;
1568 return 0;
1569}
1570/* -------------------------------------------------------------------------
1571 Non-portable escape-hatch getters (see ui_notcurses_compat.h)
1572 ------------------------------------------------------------------------- */
1573/** @brief Get the pointer to the Notcurses context associated with the UI.
1574 * @return A pointer to the Notcurses context, or NULL if the UI is not initialized.
1575 *
1576 * This function retrieves the pointer to the Notcurses context associated
1577 * with the UI. If the UI is not initialized, it returns NULL.
1578 */
1579struct notcurses *ui_notcurses_get_nc() {
1580 if (!ui)
1581 return NULL;
1582 return ui->nc;
1583}
1584/** @brief Get the pointer to the NcPlane associated with a specific window in a UI surface.
1585 * @param s Pointer to the UiSurface containing the window.
1586 * @param w The index of the window within the surface.
1587 * @return A pointer to the NcPlane associated with the specified window, or NULL if the surface is not initialized.
1588 *
1589 * This function retrieves the pointer to the NcPlane associated with a specific
1590 * window in a UI surface. If the surface is not initialized, it returns NULL.
1591 */
1592NcPlane *ui_notcurses_surface_get_plane(const UiSurface *s, ss_t w) {
1593 (void)w;
1594 if (!s)
1595 return NULL;
1596 return s->mplane[w];
1597}
1598// -------------------------------------------------------------------------
1599// Multibyte conversion
1600// -------------------------------------------------------------------------
1601/** ui__mbstr_to_cellstr
1602 @brief Convert multibyte string to complex character array (Notcurses version)
1603 @ingroup UiChyron
1604 @param cmplx_buf Output buffer for complex characters
1605 @param str Input multibyte string
1606 @param cell_base Base cell for attributes and color pair
1607 @param p Pointer to current position in the output buffer, updated as
1608 characters are added
1609 @param atmost Maximum length of the output buffer
1610 @return Number of bytes processed from the input string
1611 @details This function is a specialized version of ui_mbstr_to_cellstr for use
1612 with NCurses. It converts a multibyte string to an array of complex characters
1613 (cchar_t) that can be used with NCurses functions. It handles multibyte
1614 characters and applies the attributes and color pair from the base cell to each
1615 character. The p parameter is updated to reflect the current position in the
1616 output buffer, and the function ensures that it does not exceed the maximum
1617 length specified by atmost. */
1618uint ui_mbstr_to_cellstr(UiSurface *sfc, ss_t w, UiCell *cmplx_buf, const char *str, const UiCell *cell_base, uint *p, const uint atmost) {
1619 short cp;
1620 uint p1 = 0;
1621 uint *pos = &p1;
1622 if (p)
1623 pos = p;
1624 uint i = 0, len = 0;
1625 const char *s;
1626 attr_t style;
1627 UiCell cc;
1628 wchar_t wstr[5];
1629 ui_get_nccell(sfc, w, cell_base, &wstr[0], &style, &cp);
1630 mbstate_t mbstate;
1631 memset(&mbstate, 0, sizeof(mbstate));
1632 if (pos && *pos >= atmost - 1)
1633 return 0;
1634 while (str[i] != '\0') {
1635 s = &str[i];
1636 len = mbrtowc(wstr, s, MB_CUR_MAX, &mbstate);
1637 if (len <= 0) {
1638 wstr[0] = L'?';
1639 wstr[1] = L'\0';
1640 len = 1;
1641 }
1642 wstr[1] = L'\0';
1643 if (*pos > atmost)
1644 break;
1645 if (ui_set_nccell(sfc, w, &cc, wstr, style, cp) != ERR) {
1646 if (len > 0 && (*pos + len) < atmost)
1647 cmplx_buf[(*pos)++] = cc;
1648 }
1649 i += len;
1650 }
1651 wstr[0] = L'\0';
1652 wstr[1] = L'\0';
1653 ui_set_nccell(sfc, w, &cc, wstr, style, cp);
1654 cmplx_buf[*pos] = cc;
1655 return *pos;
1656}
UiSurface * ui_surface_box(UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x, const char *title)
Create a new UI surface with a border box and optional title.
Definition ui_ncurses.c:293
int ui_surface_resize(UiSurface *s, ss_t w, uint lines, uint cols)
Resize a UiSurface to new dimensions.
Definition ui_ncurses.c:567
UiRuntime * ui_init(const UiConfig *config, SIO *sio)
Initialize the UI runtime with the given configuration and SIO.
Definition ui_ncurses.c:130
void ui_render()
Render the UI by updating panels and refreshing the display.
Definition ui_ncurses.c:931
void ui_getyx(UiSurface *s, ss_t w, uint *lines, uint *cols)
Get the current cursor position within a window of a UI surface.
Definition ui_ncurses.c:707
UiSurface * ui_surface_new(ss_t w, UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x)
Create a new UI surface with the specified parameters.
Definition ui_ncurses.c:230
int ui_setscrreg(UiSurface *s, ss_t w, uint top, uint bottom)
Set the scrolling region for a UiSurface.
Definition ui_ncurses.c:861
int ui_plane_cpy(NcPlane *dst, NcPlane *src, int dst_y, int dst_x, int src_y, int src_x, int ylen, int xlen)
int ui_wmove(UiSurface *s, ss_t w, uint y, uint x)
Move the cursor to a specified position within a UiSurface.
Definition ui_ncurses.c:685
void ui_getmaxyx(UiSurface *s, ss_t w, uint *lines, uint *cols)
Get the maximum dimensions of a UiSurface.
Definition ui_ncurses.c:722
int ui_cursor_enable_yx(UiSurface *s, ss_t w, uint y, uint x, bool visible)
Enable or disable the cursor at a specific position within a UiSurface.
Definition ui_ncurses.c:896
int ui_wscrl(UiSurface *s, ss_t w, int rows)
Scroll the contents of a UiSurface by a specified number of lines.
Definition ui_ncurses.c:770
int ui_idcok(UiSurface *s, ss_t w, bool enable)
Enable or disable the use of the insert/delete character capabilities for a UiSurface.
Definition ui_ncurses.c:844
int ui_cursor_move(UiSurface *s, ss_t w, uint y, uint x)
Move the cursor to a specified position within a UiSurface.
Definition ui_ncurses.c:698
#define UI_SFC_MAX
Definition ui_backend.h:64
int ui_surface_addwin(UiSurface *s, ss_t w, ss_t p, uint lines, uint cols, uint y, uint x)
Add a new window to the specified UiSurface.
Definition ui_ncurses.c:423
#define UI_PAIRS
Definition ui_backend.h:62
int ui_suspend()
Suspend the UI, saving the current program state and ending the window session.
Definition ui_ncurses.c:525
void ui_surface_destroy(UiSurface *s)
Destroy a UiSurface, cleaning up its windows and panels.
Definition ui_ncurses.c:499
int ui_surface_addpad(UiSurface *s, ss_t w, ss_t view_win, uint lines, uint cols, uint y, uint x, bool hidden)
Add a new pad to the specified UiSurface.
Definition ui_ncurses.c:381
@ SUB_SFC_MAX
Definition ui_backend.h:86
void ui_update_panels()
Update the panels in the UI.
Definition ui_ncurses.c:937
int ui_bkgd(UiSurface *s, ss_t w, const UiCell *cell)
Set the background of a specific window within a UiSurface.
Definition ui_ncurses.c:974
#define ERR
Definition ui_backend.h:468
int ui_idlok(UiSurface *s, ss_t w, bool enable)
Enable or disable the use of the insert/delete line capabilities for a UiSurface.
Definition ui_ncurses.c:828
int ui_surface_move(UiSurface *s, ss_t w, uint y, uint x)
Move a UiSurface to a new position on the screen.
Definition ui_ncurses.c:552
int ui_prefresh(UiSurface *s, ss_t w, int pminrow, int pmincol, int sminrow, int smincol, int smaxrow, int smaxcol)
Definition ui_ncurses.c:923
int ui_getmaxy(UiSurface *s, ss_t w)
Get the maximum number of lines (rows) of a UiSurface.
Definition ui_ncurses.c:733
void ui_get_screen_size(uint *lines, uint *cols)
Get the current screen size (number of lines and columns).
Definition ui_ncurses.c:754
int ui_surface_show(UiSurface *s, ss_t w)
Show a UiSurface, making it visible on the screen.
Definition ui_ncurses.c:641
int ui_erase()
Erase the entire screen.
Definition ui_ncurses.c:623
void ui_shutdown()
Shutdown the UI runtime, cleaning up resources and freeing memory.
Definition ui_ncurses.c:449
UiSurface * stdsfc
Definition ui_ncurses.c:46
int sfc_ptr
Definition ui_ncurses.c:42
int ui_curs_set(int visibility)
Set the visibility of the cursor.
Definition ui_ncurses.c:881
int ui_resume()
Resume the UI, restoring the previous program state and updating the display.
Definition ui_ncurses.c:534
struct ncplane NcPlane
Definition ui_backend.h:189
int ui_bkgrndset(UiSurface *s, ss_t w, const UiCell *cell)
Set the background for new content to be written to a specific window within a UiSurface.
int ui_top_surface(UiSurface *s, ss_t w)
Hide a UiSurface, making it invisible on the screen.
Definition ui_ncurses.c:655
int ui_wclear(UiSurface *s, ss_t w)
Clear the contents of a UiSurface or all surfaces.
Definition ui_ncurses.c:603
void ui_endwin()
End the UI application, cleaning up resources and shutting down the UI runtime.
Definition ui_ncurses.c:443
int ui_cursor_enable(UiSurface *s, ss_t w, bool visible)
Enable or disable the cursor for a UiSurface.
Definition ui_ncurses.c:911
int ui_clear()
Clear the entire screen.
Definition ui_ncurses.c:631
int ui_getmaxx(UiSurface *s, ss_t w)
Get the maximum number of columns of a UiSurface.
Definition ui_ncurses.c:744
int ui_surface_hide(UiSurface *s, ss_t w)
Hide a UiSurface, making it invisible on the screen.
Definition ui_ncurses.c:667
int ui_bkgrnd(UiSurface *s, ss_t w, const UiCell *cell)
Set the background of a specific window within a UiSurface.
int ui_scrollok(UiSurface *s, ss_t w, bool enable)
Enable or disable scrolling for a UiSurface.
Definition ui_ncurses.c:796
uint16_t UiStyle
Definition ui_backend.h:184
#define UI_COLORS
Definition ui_backend.h:61
void ui_get_caps(UiCaps *caps)
Get the UI backend capabilities.
Definition ui_ncurses.c:62
int ui_bkgdset(UiSurface *s, ss_t w, const UiCell *cell)
Set the background for new content to be written to a specific window within a UiSurface.
Definition ui_ncurses.c:988
int ui_werase(UiSurface *s, ss_t w)
Erase the contents of a UiSurface or all surfaces.
Definition ui_ncurses.c:582
@ UI_BACKEND_NOTCURSES
Definition ui_backend.h:494
int ui_keypad(UiSurface *s, ss_t w, bool enable)
Enable or disable the keypad for a UiSurface.
Definition ui_ncurses.c:812
#define WA_NORMAL
#define XLEN
int ui_get_pair(uint16_t pair, uint *fg, uint *bg)
Get the foreground and background color indices for a given color pair index.
void ui_abs_yx(UiSurface *s, ss_t w, int *y, int *x)
Get the absolute cursor position within a window of a UI surface.
UiPair * ui_pair
UiConfig * ui_config
UiCell bkgd_cell
uint ui_init_color_hex(char *s)
Initialize a color from a hexadecimal string representation.
UiColor * ui_color
struct notcurses * ui_notcurses_get_nc()
Get the pointer to the Notcurses context associated with the UI.
NcPlane * stdplane
int ui_get_nccell(UiSurface *sfc, ss_t w, const UiCell *cell, wchar_t *wstr, UiStyle *style, short *pair)
Get the properties of a cell at the current cursor position in a window of a UI surface.
void ui_plane_get_abs_yx(UiSurface *s, ss_t w, int *absy, int *absx)
uint64_t ui_get_channels_from_pair(uint16_t pair)
Get the combined foreground and background color channels for a given color pair index.
int ui_set_nccell(UiSurface *sfc, ss_t w, UiCell *cell, const wchar_t *wstr, const UiStyle style, short pair)
Set the properties of a cell at the current cursor position in a window of a UI surface.
void ui_cursor_yx(int *y, int *x)
Get the current cursor position within a window of a UI surface.
NcPlane * ui_notcurses_surface_get_plane(const UiSurface *s, ss_t w)
Get the pointer to the NcPlane associated with a specific window in a UI surface.
const wchar_t * border_none
Definition ui_common.c:89
int ui_border_draw(UiSurface *sfc, ss_t w)
Definition ui_common.c:523
UiCell cell_nt
Definition ui_common.c:123
UiLogContext ui_log_ctx
Definition ui_common.c:53
BorderWide bw
Definition ui_common.c:27
STDRGB std_color[]
Definition ui_common.c:188
const wchar_t * border_double
Definition ui_common.c:86
const wchar_t * border_single
Definition ui_common.c:85
void ui_initialize_sio(SIO *sio)
Definition ui_common.c:211
int ui_border_title(UiSurface *sfc, ss_t w, const char *title)
Draw a box with a title around the specified window.
Definition ui_common.c:621
const wchar_t * border_heavy
Definition ui_common.c:88
bool f_notcurses_open
Definition ui_common.c:51
const wchar_t * border_rounded
Definition ui_common.c:87
void ui_apply_gamma(RGB *rgb)
Apply gamma correction to RGB color.
Definition ui_common.c:1002
UiCell cell_box
Definition ui_common.c:127
void ui_log_write(const UiLogLevel level, const char *file, const char *func, const int line, const char *fmt,...)
Definition ui_common.c:1476
int ui_pair_content(uint pair, uint *fg, uint *bg)
Get the foreground and background colors of a specified color pair.
int ui_init_color(uint color, uint r, uint g, uint b)
Initialize a color in the curses color table with specified RGB values.
int ui_init_pair(uint pair, uint fg, uint bg)
Initialize a color pair in the curses color table with specified foreground and background colors.
uint ui_add_pair(uint fg, uint bg)
Add a new color pair to the UI, or retrieve the index of an existing pair.
UiSurface * ui_surface[UI_SFC_MAX]
Definition ui_ncurses.c:40
int ui_color_content(uint color, uint *r, uint *g, uint *b)
UiRuntime * ui
Definition ui_ncurses.c:39
RGB ui_hex_to_rgb(char *s)
Convert a hexadecimal color string to an RGB structure.
UiCell ui_cell_from_ucp(const wchar_t *ucp, const uint32_t *fg, const uint32_t *bg)
Convert a Unicode code point to a curses UiCell with specified foreground and background colors.
Definition ui_ncurses.c:102
uint ui_pair_cnt
Definition ui_ncurses.c:44
uint ui_color_cnt
Definition ui_ncurses.c:43
void ui_def_prog_mode()
Definition ui_ncurses.c:867
int ui_color_from_rgb(RGB *rgb)
Convert RGB to curses color index, adding new color if necessary.
int ui_chg_color(uint16_t color_idx, uint32_t *color)
Change the RGB color value for a given color index.
#define MAXLEN
Definition lf.h:38
char em1[MAXLEN]
Definition lf.c:43
char em2[MAXLEN]
Definition lf.c:44
char em0[MAXLEN]
Definition lf.c:42
#define _(String)
Definition amort.c:23
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition lf.c:2299
size_t ssnprintf(char *, size_t, const char *,...)
Custom safe string formatting utility (Variadic version).
Definition futil.c:398
int ui_display_error(char *msg0, char *msg1, char *msg2, char *msg3)
Display an error message window or print to stderr.
Definition ui_common.c:1261
UiBackend ui_get_backend()
Get the UI backend type.
Definition ui_ncurses.c:55
wchar_t w32
Definition ui_backend.h:148
uint32_t u32
Definition ui_backend.h:147
UiLogLevel min_level
Definition ui_backend.h:237
UiBorderStyle border_style
Definition ui_backend.h:486
UiLogLevel log_level
Definition ui_backend.h:488
bool palette256
Definition ui_backend.h:499
bool mouse
Definition ui_backend.h:500
bool pad_relative_yx
Definition ui_backend.h:504
int color_pairs
Definition ui_backend.h:503
bool truecolor
Definition ui_backend.h:498
bool unicode
Definition ui_backend.h:501
bool resize
Definition ui_backend.h:502
wchar_t str[11]
Definition ui_backend.h:609