C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses.c
Go to the documentation of this file.
1/** @file ui_ncurses.c
2 @ingroup ui_ncurses
3 @brief NCurses UI backend — lifecycle, surface management, and capabilities.
4
5 Implements all UiRuntime and UiSurface operations declared in ui_backend.h
6 using the NCurses / panelw library.
7
8 When compiled as part of the main C-Menu build (UAL_LEGACY_COMPAT defined),
9 the legacy globals @c screen and @c tty_fp from dwin.c are kept in sync so
10 that code not yet migrated to the UAL API continues to work.
11*/
12
13#define _XOPEN_SOURCE_EXTENDED 1
14
15#include "cm.h"
16
18#ifdef UAL_LEGACY_COMPAT
19#include "ui_ncurses_compat.h"
20#endif
21#include <locale.h>
22#include <stdint.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
27/** @defgroup ui_ncurses
28 @ingroup ui_backend
29 @brief NCurses UI backend — lifecycle, surface management, and capabilities.
30
31 Implements all UiRuntime and UiSurface operations declared in ui_backend.h
32 using the NCurses / panelw library.
33
34 When compiled as part of the main C-Menu build (UAL_LEGACY_COMPAT defined),
35 the legacy globals @c screen and @c tty_fp from dwin.c are kept in sync so
36 that code not yet migrated to the UAL API continues to work.
37*/
38
39UiRuntime *ui = NULL;
41
42int sfc_ptr = -1;
43uint ui_color_cnt = 0;
44uint ui_pair_cnt = 0;
45
46UiSurface *stdsfc;
47
48/* -------------------------------------------------------------------------
49 Backend identification and capability query
50 ------------------------------------------------------------------------- */
51/** @brief Get the UI backend type.
52 * @ingroup ui_ncurses
53 * @return The UI backend type, which is always UI_BACKEND_NCURSES for this implementation.
54 */
55UiBackend ui_get_backend() {
56 return UI_BACKEND_NCURSES;
57}
58/** @brief Get the UI backend capabilities.
59 *
60 * @param caps Pointer to a UiCaps struct to be filled with the backend capabilities.
61 */
62void ui_get_caps(UiCaps *caps) {
63 if (!caps)
64 return;
65 memset(caps, 0, sizeof(*caps));
66 if (!ui)
67 return;
68 // Will add code to actually check later. For now, just lie.
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 = false;
76}
77
78/* -------------------------------------------------------------------------
79 Styles
80 ------------------------------------------------------------------------- */
81/** @brief Convert hex color strings to a curses color pair index.
82 *
83 * @param fg Foreground color in hex format (e.g., "#RRGGBB").
84 * @param bg Background color in hex format (e.g., "#RRGGBB").
85 * @return The curses color pair index corresponding to the given foreground and background colors.
86 */
87int ui_pair_from_hex(const char *fg, const char *bg) {
88 RGB rgb;
89 sscanf(fg, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
90 int f_idx = ui_color_from_rgb(&rgb);
91 sscanf(bg, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
92 int b_idx = ui_color_from_rgb(&rgb);
93 return ui_add_pair(f_idx, b_idx);
94}
95/** @brief Convert a Unicode code point to a curses UiCell with specified foreground and background colors.
96 *
97 * @param ucp Pointer to a wide character string containing the Unicode code point.
98 * @param fg Pointer to a 32-bit integer representing the foreground color in hex format (e.g., 0xRRGGBB).
99 * @param bg Pointer to a 32-bit integer representing the background color in hex format (e.g., 0xRRGGBB).
100 * @return A UiCell structure representing the Unicode character with the specified colors.
101 */
102UiCell ui_cell_from_ucp(const wchar_t *ucp, const uint32_t *fg, const uint32_t *bg) {
103 UiCell cc = {0};
104 RGB rgb;
105 rgb.r = (*fg >> 16) & 0xff;
106 rgb.g = (*fg >> 8) & 0xff;
107 rgb.b = *fg & 0xff;
108 int f_idx = ui_color_from_rgb(&rgb);
109 rgb.r = (*bg >> 16) & 0xff;
110 rgb.g = (*bg >> 8) & 0xff;
111 rgb.b = *bg & 0xff;
112 int b_idx = ui_color_from_rgb(&rgb);
113 short cp = ui_add_pair(f_idx, b_idx);
114 wchar_t wstr[2] = {L'\0', L'\0'};
115 wstr[0] = *ucp;
116 wstr[1] = L'\0';
117 setcchar(&cc, wstr, WA_NORMAL, cp, nullptr);
118 return cc;
119}
120
121/* -------------------------------------------------------------------------
122 Lifecycle
123 ------------------------------------------------------------------------- */
124/** @brief Initialize the UI runtime with the given configuration and SIO.
125 *
126 * @param cfg Pointer to a UiConfig structure containing the configuration settings.
127 * @param sio Pointer to an SIO structure for input/output operations.
128 * @return A pointer to the initialized UiRuntime structure, or NULL on failure.
129 */
130struct UiRuntime *ui_init(const UiConfig *cfg, SIO *sio) {
131 setlocale(LC_ALL, "en_US.UTF-8");
132 ui = calloc(1, sizeof(*ui));
133 if (!ui)
134 return NULL;
135 char tty_name[MAXLEN];
136 if (ttyname_r(STDERR_FILENO, tty_name, sizeof(tty_name)) != 0) {
137 free(ui);
138 return NULL;
139 }
140 ui->tty_fp = fopen(tty_name, "r+");
141 if (!ui->tty_fp) {
142 free(ui);
143 return NULL;
144 }
145 ui->tty_fd = fileno(ui->tty_fp);
147
148 ui_log(INFO, _("ui_init: using tty: %s"), tty_name);
149 ui->screen = newterm(NULL, ui->tty_fp, ui->tty_fp);
150 f_ncurses_open = true;
151 if (!ui->screen) {
152 fclose(ui->tty_fp);
153 free(ui);
154 return NULL;
155 }
156 set_term(ui->screen);
157 if (!has_colors() || !can_change_color()) {
159 return NULL;
160 }
161 start_color();
162 use_default_colors();
163 cbreak();
164 noecho();
165 keypad(stdscr, TRUE);
166 if (ui->mouse_enabled)
167 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
168 getmaxyx(stdscr, ui->lines, ui->cols);
169
170 stdsfc = calloc(1, sizeof(*stdsfc));
171 if (!stdsfc)
172 return NULL;
173 stdsfc->runtime = ui;
174 stdsfc->parent = NULL;
175 stdsfc->meta[BOX].lines = (int)ui->lines;
176 stdsfc->meta[BOX].cols = (int)ui->cols;
177 stdsfc->meta[BOX].y = 0;
178 stdsfc->meta[BOX].x = 0;
179 stdsfc->mwin[BOX] = stdscr;
180 stdsfc->mpan[BOX] = new_panel(stdsfc->mwin[BOX]);
181 if (!stdsfc->mpan[BOX]) {
182 free(stdsfc);
183 ui_log(ERROR, _("new_panel failed for stdsfc->mpan[BOX]"));
184 exit(EXIT_FAILURE);
185 }
186 if (cfg->border_style)
187 ui->border_style = cfg->border_style;
188 if (!ui->border_style)
189 ui->border_style = UI_BORDER_ROUNDED;
190 if (cfg) {
191 switch (ui->border_style) {
192 case UI_BORDER_SINGLE:
193 memcpy(bw.str, border_single, sizeof(bw.str));
194 break;
195 case UI_BORDER_DOUBLE:
196 memcpy(bw.str, border_double, sizeof(bw.str));
197 break;
198 case UI_BORDER_ROUNDED:
199 memcpy(bw.str, border_rounded, sizeof(bw.str));
200 break;
201 case UI_BORDER_HEAVY:
202 memcpy(bw.str, border_heavy, sizeof(bw.str));
203 break;
204 case UI_BORDER_NONE:
205 memcpy(bw.str, border_none, sizeof(bw.str));
206 break;
207 default:
208 memcpy(bw.str, border_rounded, sizeof(bw.str));
209 break;
210 }
211 }
213 ui_log(INFO, _("ui_init: stdsfc->mwin[BOX]: %p, stdsfc->mpan[BOX]: %p"), (void *)stdsfc->mwin[BOX], (void *)stdsfc->mpan[BOX]);
214 return ui;
215}
216// -------------------------------------------------------------------------
217// Surface Creation and Destruction
218// -------------------------------------------------------------------------
219/** @brief Create a new UI surface with the specified parameters.
220 *
221 * @param w The index of the window within the surface.
222 * @param parent Pointer to the parent UiSurface, or NULL for a top-level surface.
223 * @param p The index of the parent window to derive from, if applicable.
224 * @param lines The number of lines (rows) for the new surface.
225 * @param cols The number of columns for the new surface.
226 * @param y The y-coordinate (row) for the new surface's position.
227 * @param x The x-coordinate (column) for the new surface's position.
228 * @return A pointer to the newly created UiSurface, or NULL on failure.
229 */
230UiSurface *ui_surface_new(ss_t w, UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x) {
231 if (!ui)
232 return NULL;
233 uint maxy, maxx;
234 ui_get_screen_size(&maxy, &maxx);
235 if (lines > maxy)
236 lines = maxy;
237 if (cols > maxx)
238 cols = maxx;
239 UiSurface *s = calloc(1, sizeof(*s));
240 if (!s) {
241 ui_log(ERROR, _("calloc failed for UiSurface"));
242 return NULL;
243 }
244 for (int i = 0; i < SUB_SFC_MAX; i++) {
245 s->mwin[i] = NULL;
246 s->mpan[i] = NULL;
247 }
248 s->runtime = ui;
249 s->parent = parent;
250 s->meta[w].lines = lines;
251 s->meta[w].cols = cols;
252 s->meta[w].y = y;
253 s->meta[w].x = x;
254 UiLogLevel level = INFO;
255 if (parent && parent->mwin[p]) {
256 s->mwin[w] = derwin(parent->mwin[p], lines, cols, y, x);
257 if (!s->mwin[w]) {
258 free(s);
259 level = ERROR;
260 ui_log(level, "%s = derwin(%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], ss_n[p], lines, cols, y, x);
261 return NULL;
262 }
263 } else {
264 s->mwin[w] = newwin(lines, cols, y, x);
265 if (!s->mwin[w]) {
266 free(s);
267 level = ERROR;
268 ui_log(level, "%s = newwin(lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], lines, cols, y, x);
269 return NULL;
270 }
271 s->mpan[w] = new_panel(s->mwin[w]);
272 if (!s->mpan[w]) {
273 delwin(s->mwin[w]);
274 level = ERROR;
275 ui_log(level, "%s = new_panel(lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], lines, cols, y, x);
276 free(s);
277 return NULL;
278 }
279 }
280 return s;
281}
282/** @brief Create a new UI surface with a border box and optional title.
283 *
284 * @param parent Pointer to the parent UiSurface, or NULL for a top-level surface.
285 * @param p The index of the parent window to derive from, if applicable.
286 * @param lines The number of lines (rows) for the new surface.
287 * @param cols The number of columns for the new surface.
288 * @param y The y-coordinate (row) for the new surface's position.
289 * @param x The x-coordinate (column) for the new surface's position.
290 * @param wtitle The title string to display in the border box, or NULL for no title.
291 * @return A pointer to the newly created UiSurface with a border box, or NULL on failure.
292 */
293UiSurface *ui_surface_box(UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x, const char *wtitle) {
294 if (!ui)
295 return NULL;
296 uint maxy, maxx;
297 ui_get_screen_size(&maxy, &maxx);
298 if (lines > maxy)
299 lines = maxy;
300 if (cols > maxx)
301 cols = maxx;
302 UiSurface *s = calloc(1, sizeof(*s));
303 if (!s) {
304 ui_log(ERROR, _("calloc failed for UiSurface"));
305 return NULL;
306 }
307 ss_t w = BOX;
308 s->runtime = ui;
309 s->parent = parent;
310 s->meta[BOX].y = y;
311 s->meta[BOX].x = x;
312 s->meta[BOX].lines = lines;
313 s->meta[BOX].cols = cols;
314 UiLogLevel level = INFO;
315 if (parent && parent->mwin[p]) {
316 s->mwin[BOX] = derwin(parent->mwin[p], lines, cols, y, x);
317 if (!s->mwin[BOX]) {
318 free(s);
319 level = ERROR;
320 ui_log(level, "%s = derwin(%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], ss_n[p], lines, cols, y, x);
321 return NULL;
322 }
323 } else {
324 s->mwin[BOX] = newwin(lines, cols, y, x);
325 if (!s->mwin[BOX]) {
326 free(s);
327 level = ERROR;
328 ui_log(level, "%s = newwin(lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], lines, cols, y, x);
329 return NULL;
330 }
331 s->mpan[BOX] = new_panel(s->mwin[BOX]);
332 if (!s->mpan[BOX]) {
333 delwin(s->mwin[BOX]);
334 level = ERROR;
335 ui_log(level, "%s = new_panel(lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], lines, cols, y, x);
336 return NULL;
337 }
338 }
339 ui_scrollok(s, BOX, false);
341 ui_border_title(s, BOX, wtitle);
342#ifdef DEBUG_IMMED_UI
343 immedok(s->mwin[BOX], true);
344#endif
345 return s;
346}
347/** @brief Exit the UI application quickly, cleaning up resources.
348 *
349 * @param s Pointer to the UiSurface to be cleaned up before exiting.
350 */
351void fast_exit(UiSurface *s) {
352 update_panels();
353 doupdate();
354 for (int i = SUB_SFC_MAX; i >= 0; i--) {
355 if (s->mpan[i]) {
356 hide_panel(s->mpan[i]);
357 del_panel(s->mpan[i]);
358 s->mpan[i] = NULL;
359 }
360 }
361 for (int i = SUB_SFC_MAX; i >= 0; i--) {
362 if (s->mwin[i]) {
363 delwin(s->mwin[i]);
364 s->mwin[i] = NULL;
365 }
366 }
367 endwin();
368 exit(EXIT_SUCCESS);
369}
370/** @brief Add a new pad to the specified UiSurface.
371 *
372 * @param s Pointer to the UiSurface to which the pad will be added.
373 * @param w The index of the window within the surface where the pad will be created.
374 * @param view_win The index of the window that will be used to view the pad.
375 * @param lines The number of lines (rows) for the new pad.
376 * @param cols The number of columns for the new pad.
377 * @param begy The y-coordinate (row) for the new pad's position (ignored in this implementation).
378 * @param begx The x-coordinate (column) for the new pad's position (ignored in this implementation).
379 * @return 0 on success, or -1 on failure.
380 */
381int ui_surface_addpad(UiSurface *s, ss_t pad, ss_t p, uint lines, uint cols, uint begy, uint begx, bool hidden) {
382 (void)p;
383 (void)hidden;
384 (void)begy;
385 (void)begx;
386 UiLogLevel level = INFO;
387 s->mwin[pad] = newpad(lines, cols);
388 if (s->mwin[pad] == nullptr) {
389 level = ERROR;
390 ui_log(level, "%s = newpad(lines=%d, cols=%d, hidden=%s) failed", ss_n[pad], lines, cols, hidden ? "true" : "false");
391 return -1;
392 }
393 ui_log(level, "%s = newpad(lines=%d, cols=%d) success", ss_n[pad], lines, cols);
394#ifdef ASDFJKL
395 s->mwin[v] = subpad(s->mwin[PAD], lines, cols, 0, 0);
396 if (s->mwin[v] == nullptr) {
397 level = ERROR;
398 ui_log(level, "subpad(%s, %s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], ss_n[v], lines, cols, 0, 0);
399 return -1;
400 }
401 ui_log(level, "subpad(%s, %s, lines=%d, cols=%d) success", ss_n[w],
402 ss_n[v], lines, cols);
403 s->mpan[w] = new_panel(s->mwin[v]);
404 top_panel(s->mpan[w]);
405#endif
406 immedok(s->mwin[pad], true);
407 scrollok(s->mwin[pad], true);
408#ifdef DEBUG_IMMED_UI
409#endif
410 return 0;
411}
412/** @brief Add a new window to the specified UiSurface.
413 *
414 * @param s Pointer to the UiSurface to which the window will be added.
415 * @param w The index of the window within the surface where the new window will be created.
416 * @param p The index of the parent window from which the new window will be derived.
417 * @param lines The number of lines (rows) for the new window.
418 * @param cols The number of columns for the new window.
419 * @param y The y-coordinate (row) for the new window's position relative to the parent window.
420 * @param x The x-coordinate (column) for the new window's position relative to the parent window.
421 * @return 0 on success, or -1 on failure.
422 */
423int ui_surface_addwin(UiSurface *s, ss_t w, ss_t p, uint lines, uint cols, uint y, uint x) {
424 UiLogLevel level = INFO;
425 s->mwin[w] = derwin(s->mwin[p], lines, cols, y, x);
426 if (!s->mwin[w]) {
427 level = ERROR;
428 ui_log(level, "%s = derwin(%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], ss_n[p], lines, cols, y, x);
429 free(s);
430 return -1;
431 }
432 ui_log(level, "%s = derwin(%s, lines=%d, cols=%d, begy=%d, begx=%d) success", ss_n[w], ss_n[p], lines, cols, y, x);
433 s->mpan[w] = new_panel(s->mwin[w]);
434 keypad(s->mwin[w], true);
435 immedok(s->mwin[w], true);
436
437#ifdef DEBUG_IMMED_UI
438#endif
439 return 0;
440}
441/** @brief End the UI application, cleaning up resources and shutting down the UI runtime.
442 */
443void ui_endwin() {
444 endwin();
445 // ui_shutdown();
446}
447/** @brief Shutdown the UI runtime, cleaning up resources and freeing memory.
448 */
450 if (ui == NULL)
451 return;
452 ui_log(INFO, _("ui_shutdown in progress..."));
453 while (sfc_ptr >= 0) {
454 if (ui_surface[sfc_ptr] != NULL) {
456 ui_surface[sfc_ptr] = NULL;
457 }
458 ui_log(INFO, _("surface destroy: %d"), sfc_ptr);
459 sfc_ptr--;
460 }
461 if (stdsfc->mpan[0] != NULL) {
462 ui_log(INFO, _("calling del_panel(stdsfc->mpan[BOX])"));
463 hide_panel(stdsfc->mpan[0]);
464 del_panel(stdsfc->mpan[0]);
465 stdsfc->mpan[0] = NULL;
466 }
467 ui_log(INFO, _("calling endwin()"));
468 endwin();
469 f_ncurses_open = false;
470 if (ui->screen != NULL) {
471 ui_log(INFO, _("calling delscreen(ui->screen)"));
472 delscreen(ui->screen);
473 ui->screen = NULL;
474 }
475 if (ui->tty_fp != NULL) {
476 ui_log(INFO, _("closing tty_fp"));
477 fclose(ui->tty_fp);
478 ui->tty_fp = NULL;
479 }
480 if (stdsfc != NULL) {
481 ui_log(INFO, _("freeing stdsfc"));
482 free(stdsfc);
483 stdsfc = NULL;
484 }
485 if (ui != NULL) {
486 if (ui->sio) {
487 free(ui->sio);
488 ui->sio = nullptr;
489 }
490 ui_log(INFO, _("freeing ui"));
491 free(ui);
492 ui = NULL;
493 }
494}
495/** @brief Destroy a UiSurface, cleaning up its windows and panels.
496 *
497 * @param s Pointer to the UiSurface to be destroyed.
498 */
499void ui_surface_destroy(UiSurface *s) {
500 if (!s)
501 return;
502 for (int i = SUB_SFC_MAX - 1; i >= 0; i--) {
503 if (s->mpan[i] != NULL) {
504 hide_panel(s->mpan[i]);
506 del_panel(s->mpan[i]);
507 s->mpan[i] = NULL;
508 }
509 }
510 for (int i = SUB_SFC_MAX - 1; i >= 0; i--) {
511 if (s->mwin[i] != NULL) {
512 delwin(s->mwin[i]);
513 s->mwin[i] = NULL;
514 }
515 }
516 if (s != NULL) {
517 free(s);
518 s = NULL;
519 }
520}
521/** @brief Suspend the UI, saving the current program state and ending the window session.
522 *
523 * @return 0 on success, or -1 on failure.
524 */
526 def_prog_mode();
527 endwin();
528 return 0;
529}
530/** @brief Resume the UI, restoring the previous program state and updating the display.
531 *
532 * @return 0 on success, or -1 on failure.
533 */
535 reset_prog_mode();
536 update_panels();
537 doupdate();
538 return 0;
539}
540
541// -------------------------------------------------------------------------
542// Surface Management
543// -------------------------------------------------------------------------
544/** @brief Move a UiSurface to a new position on the screen.
545 *
546 * @param s Pointer to the UiSurface to be moved.
547 * @param w The index of the window within the surface to be moved.
548 * @param y The new y-coordinate (row) for the surface's position.
549 * @param x The new x-coordinate (column) for the surface's position.
550 * @return 0 on success, or -1 on failure.
551 */
552int ui_surface_move(UiSurface *s, ss_t w, uint y, uint x) {
553 if (!s)
554 return -1;
555 s->meta[w].y = y;
556 s->meta[w].x = x;
557 return move_panel(s->mpan[w], y, x);
558}
559/** @brief Resize a UiSurface to new dimensions.
560 *
561 * @param s Pointer to the UiSurface to be resized.
562 * @param w The index of the window within the surface to be resized.
563 * @param lines The new number of lines (rows) for the surface.
564 * @param cols The new number of columns for the surface.
565 * @return 0 on success, or -1 on failure.
566 */
567int ui_surface_resize(UiSurface *s, ss_t w, uint lines, uint cols) {
568 if (!s)
569 return -1;
570 s->meta[w].lines = lines;
571 s->meta[w].cols = cols;
572 wresize(s->mwin[w], lines + 2, cols + 2);
573 wresize(s->mwin[w + 1], lines, cols);
574 return 0;
575}
576/** @brief Erase the contents of a UiSurface or all surfaces.
577 *
578 * @param s Pointer to the UiSurface to be erased, or NULL to erase all surfaces.
579 * @param w The index of the window within the surface to be erased, or ALLWINS to erase all windows.
580 * @return 0 on success, or -1 on failure.
581 */
582int ui_werase(UiSurface *s, ss_t w) {
583 if (w == ALLWINS) {
584 for (int i = 1; i < SUB_SFC_MAX; i++) {
585 if (s->mwin[i]) {
586 werase(s->mwin[i]);
587 }
588 }
589 } else {
590 if (s->mwin[w]) {
591 werase(s->mwin[w]);
592 }
593 }
594 return 0;
595}
596/** @brief Clear the contents of a UiSurface or all surfaces.
597 *
598 * @param s Pointer to the UiSurface to be cleared, or NULL to clear all surfaces.
599 * @param w The index of the window within the surface to be cleared, or ALLWINS to clear all windows.
600 * @return 0 on success, or -1 on failure.
601 */
602
603int ui_wclear(UiSurface *s, ss_t w) {
604 if (!s)
605 return -1;
606 if (w == ALLWINS) {
607 for (int i = 1; i < SUB_SFC_MAX; i++) {
608 if (s->mwin[i]) {
609 wclear(s->mwin[i]);
610 }
611 }
612 } else {
613 if (s->mwin[w]) {
614 wclear(s->mwin[w]);
615 }
616 }
617 return 0;
618}
619/** @brief Erase the entire screen.
620 *
621 * @return 0 on success, or -1 on failure.
622 */
623int ui_erase() {
624 erase();
625 return 0;
626}
627/** @brief Clear the entire screen.
628 *
629 * @return 0 on success, or -1 on failure.
630 */
631int ui_clear() {
632 clear();
633 return 0;
634}
635/** @brief Show a UiSurface, making it visible on the screen.
636 *
637 * @param s Pointer to the UiSurface to be shown.
638 * @param w The index of the window within the surface to be shown.
639 * @return 0 on success, or -1 on failure.
640 */
641int ui_surface_show(UiSurface *s, ss_t w) {
642 if (!s)
643 return -1;
644 show_panel(s->mpan[w]);
645 s->meta[w].hidden = false;
646 return 0;
647 return 0;
648}
649/** @brief Hide a UiSurface, making it invisible on the screen.
650 *
651 * @param s Pointer to the UiSurface to be hidden.
652 * @param w The index of the window within the surface to be hidden.
653 * @return 0 on success, or -1 on failure.
654 */
655int ui_top_surface(UiSurface *s, ss_t w) {
656 if (!s)
657 return -1;
658 top_panel(s->mpan[w]);
659 return 0;
660}
661/** @brief Hide a UiSurface, making it invisible on the screen.
662 *
663 * @param s Pointer to the UiSurface to be hidden.
664 * @param w The index of the window within the surface to be hidden.
665 * @return 0 on success, or -1 on failure.
666 */
667int ui_surface_hide(UiSurface *s, ss_t w) {
668 if (!s)
669 return -1;
670 hide_panel(s->mpan[w]);
671 s->meta[w].hidden = true;
672 return 0;
673}
674// -------------------------------------------------------------------------
675// Screen Navigation
676// -------------------------------------------------------------------------
677/** @brief Move the cursor to a specified position within a UiSurface.
678 *
679 * @param s Pointer to the UiSurface where the cursor will be moved.
680 * @param w The index of the window within the surface where the cursor will be moved.
681 * @param y The new y-coordinate (row) for the cursor's position.
682 * @param x The new x-coordinate (column) for the cursor's position.
683 * @return 0 on success, or -1 on failure.
684 */
685int ui_wmove(UiSurface *s, ss_t w, uint y, uint x) {
686 if (!s || !s->mwin[w])
687 return -1;
688 return wmove(s->mwin[w], y, x);
689}
690/** @brief Move the cursor to a specified position within a UiSurface.
691 *
692 * @param s Pointer to the UiSurface where the cursor will be moved.
693 * @param w The index of the window within the surface where the cursor will be moved.
694 * @param y The new y-coordinate (row) for the cursor's position.
695 * @param x The new x-coordinate (column) for the cursor's position.
696 * @return 0 on success, or -1 on failure.
697 */
698int ui_cursor_move(UiSurface *s, ss_t w, uint y, uint x) {
699 if (!s || !s->mwin[w])
700 return -1;
701 return wmove(s->mwin[w], y, x);
702}
703// ui_cursor_yx(*int y, int *x);
704//
705// ui_abs_yx(UiSurface *s, ss_t w, int *y, int *x);
706//
707void ui_getyx(UiSurface *s, ss_t w, uint *lines, uint *cols) {
708 if (!s->mwin[w])
709 return;
710 int _lines, _cols;
711 getyx(s->mwin[w], _lines, _cols);
712 *lines = (uint)(_lines);
713 *cols = (uint)(_cols);
714}
715/** @brief Get the maximum dimensions of a UiSurface.
716 *
717 * @param s Pointer to the UiSurface for which to retrieve the maximum dimensions.
718 * @param w The index of the window within the surface for which to retrieve the maximum dimensions.
719 * @param lines Pointer to a uint variable where the maximum number of lines (rows) will be stored.
720 * @param cols Pointer to a uint variable where the maximum number of columns will be stored.
721 */
722void ui_getmaxyx(UiSurface *s, ss_t w, uint *lines, uint *cols) {
723 if (!s->mwin[w])
724 return;
725 getmaxyx(s->mwin[w], *lines, *cols);
726}
727/** @brief Get the maximum number of lines (rows) of a UiSurface.
728 *
729 * @param s Pointer to the UiSurface for which to retrieve the maximum number of lines.
730 * @param w The index of the window within the surface for which to retrieve the maximum number of lines.
731 * @return The maximum number of lines (rows) of the specified UiSurface, or -1 on failure.
732 */
733int ui_getmaxy(UiSurface *s, ss_t w) {
734 if (!s->mwin[w])
735 return -1;
736 return (int)(getmaxy(s->mwin[w]));
737}
738/** @brief Get the maximum number of columns of a UiSurface.
739 *
740 * @param s Pointer to the UiSurface for which to retrieve the maximum number of columns.
741 * @param w The index of the window within the surface for which to retrieve the maximum number of columns.
742 * @return The maximum number of columns of the specified UiSurface, or -1 on failure.
743 */
744int ui_getmaxx(UiSurface *s, ss_t w) {
745 if (!s->mwin[w])
746 return -1;
747 return (int)(getmaxx(s->mwin[w]));
748}
749/** @brief Get the current screen size (number of lines and columns).
750 *
751 * @param lines Pointer to a uint variable where the number of lines (rows) will be stored.
752 * @param cols Pointer to a uint variable where the number of columns will be stored.
753 */
754void ui_get_screen_size(uint *lines, uint *cols) {
755 if (!ui)
756 return;
757 getmaxyx(stdscr, ui->lines, ui->cols);
758 if (lines)
759 *lines = ui->lines;
760 if (cols)
761 *cols = ui->cols;
762}
763/** @brief Scroll the contents of a UiSurface by a specified number of lines.
764 *
765 * @param s Pointer to the UiSurface to be scrolled.
766 * @param w The index of the window within the surface to be scrolled.
767 * @param n The number of lines to scroll. Positive values scroll down, negative values scroll up.
768 * @return 0 on success, or -1 on failure.
769 */
770int ui_wscrl(UiSurface *s, ss_t w, int n) {
771 if (!s->mwin[w])
772 return -1;
773 wscrl(s->mwin[w], n);
774 return 0;
775}
776
777// -------------------------------------------------------------------------
778// Configuration Control
779// -------------------------------------------------------------------------
780int ui_immedok(UiSurface *s, ss_t w, bool enable) {
781 if (!s->mwin[w])
782 return -1;
783 if (enable)
784 immedok(s->mwin[w], true);
785 else
786 immedok(s->mwin[w], false);
787 return 0;
788}
789/** @brief Enable or disable scrolling for a UiSurface.
790 *
791 * @param s Pointer to the UiSurface for which to enable or disable scrolling.
792 * @param w The index of the window within the surface for which to enable or disable scrolling.
793 * @param enable A boolean value indicating whether to enable (true) or disable (false) scrolling.
794 * @return 0 on success, or -1 on failure.
795 */
796int ui_scrollok(UiSurface *s, ss_t w, bool enable) {
797 if (!s->mwin[w])
798 return -1;
799 if (enable)
800 scrollok(s->mwin[w], true);
801 else
802 scrollok(s->mwin[w], false);
803 return 0;
804}
805/** @brief Enable or disable the keypad for a UiSurface.
806 *
807 * @param s Pointer to the UiSurface for which to enable or disable the keypad.
808 * @param w The index of the window within the surface for which to enable or disable the keypad.
809 * @param enable A boolean value indicating whether to enable (true) or disable (false) the keypad.
810 * @return 0 on success, or -1 on failure.
811 */
812int ui_keypad(UiSurface *s, ss_t w, bool enable) {
813 if (!s->mwin[w])
814 return -1;
815 if (enable)
816 keypad(s->mwin[w], true);
817 else
818 keypad(s->mwin[w], false);
819 return 0;
820}
821/** @brief Enable or disable the use of the insert/delete line capabilities for a UiSurface.
822 *
823 * @param s Pointer to the UiSurface for which to enable or disable the insert/delete line capabilities.
824 * @param w The index of the window within the surface for which to enable or disable the insert/delete line capabilities.
825 * @param enable A boolean value indicating whether to enable (true) or disable (false) the insert/delete line capabilities.
826 * @return 0 on success, or -1 on failure.
827 */
828int ui_idlok(UiSurface *s, ss_t w, bool enable) {
829 if (!s->mwin[w])
830 return -1;
831 if (enable)
832 idlok(s->mwin[w], true);
833 else
834 idlok(s->mwin[w], false);
835 return 0;
836}
837/** @brief Enable or disable the use of the insert/delete character capabilities for a UiSurface.
838 *
839 * @param s Pointer to the UiSurface for which to enable or disable the insert/delete character capabilities.
840 * @param w The index of the window within the surface for which to enable or disable the insert/delete character capabilities.
841 * @param enable A boolean value indicating whether to enable (true) or disable (false) the insert/delete character capabilities.
842 * @return 0 on success, or -1 on failure.
843 */
844int ui_idcok(UiSurface *s, ss_t w, bool enable) {
845 if (!s->mwin[w])
846 return -1;
847 if (enable)
848 idcok(s->mwin[w], true);
849 else
850 idcok(s->mwin[w], false);
851 return 0;
852}
853/** @brief Set the scrolling region for a UiSurface.
854 *
855 * @param s Pointer to the UiSurface for which to set the scrolling region.
856 * @param w The index of the window within the surface for which to set the scrolling region.
857 * @param top The top line of the scrolling region (0-based).
858 * @param bottom The bottom line of the scrolling region (0-based).
859 * @return 0 on success, or -1 on failure.
860 */
861int ui_setscrreg(UiSurface *s, ss_t w, uint top, uint bottom) {
862 if (!s->mwin[w])
863 return -1;
864 wsetscrreg(s->mwin[w], top, bottom);
865 return 0;
866}
868 def_prog_mode();
869}
870// -------------------------------------------------------------------------
871// Cursor Control
872// -------------------------------------------------------------------------
873/** @brief Set the visibility of the cursor.
874 *
875 * @param visibility An integer value indicating the desired cursor visibility:
876 * 0 - Invisible
877 * 1 - Visible
878 * 2 - Very visible (e.g., block cursor)
879 * @return 0 on success, or -1 on failure.
880 */
881int ui_curs_set(int visibility) {
882 curs_set(visibility);
883 return 0;
884}
885// included for symetry with notcurses, which needs it to compensate
886// for cursor positioning bug
887/** @brief Enable or disable the cursor at a specific position within a UiSurface.
888 *
889 * @param s Pointer to the UiSurface where the cursor will be enabled or disabled.
890 * @param w The index of the window within the surface where the cursor will be enabled or disabled.
891 * @param y The y-coordinate (row) for the cursor's position.
892 * @param x The x-coordinate (column) for the cursor's position.
893 * @param visible A boolean value indicating whether to make the cursor visible (true) or invisible (false).
894 * @return 0 on success, or -1 on failure.
895 */
896int ui_cursor_enable_yx(UiSurface *s, ss_t w, uint y, uint x, bool visible) {
897 if (!ui)
898 return -1;
899 wmove(s->mwin[w], y, x);
900 ui->cursor_visible = visible;
901 curs_set(visible ? 2 : 0);
902 return 0;
903}
904/** @brief Enable or disable the cursor for a UiSurface.
905 *
906 * @param s Pointer to the UiSurface where the cursor will be enabled or disabled.
907 * @param w The index of the window within the surface where the cursor will be enabled or disabled.
908 * @param visible A boolean value indicating whether to make the cursor visible (true) or invisible (false).
909 * @return 0 on success, or -1 on failure.
910 */
911int ui_cursor_enable(UiSurface *s, ss_t w, bool visible) {
912 (void)s;
913 (void)w;
914 if (!ui)
915 return -1;
916 ui->cursor_visible = visible;
917 curs_set(visible ? 1 : 0);
918 return 0;
919}
920/* -------------------------------------------------------------------------
921 Rendering
922 ------------------------------------------------------------------------- */
923int ui_prefresh(UiSurface *s, ss_t w, int pminrow, int pmincol, int sminrow, int smincol, int smaxrow, int smaxcol) {
924 if (!s)
925 return -1;
926 return prefresh(s->mwin[w], pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol);
927}
928
929/** @brief Render the UI by updating panels and refreshing the display.
930 */
931void ui_render() {
932 update_panels();
933 doupdate();
934}
935/** @brief Update the panels in the UI.
936 */
938 update_panels();
939}
940/** @brief Refresh the display by updating the screen.
941 */
943 doupdate();
944 return 0;
945}
946/** -------------------------------------------------------------------------
947 Refreshing
948 ------------------------------------------------------------------------- */
949/** @brief Refresh a specific window within a UiSurface.
950 *
951 * @param s Pointer to the UiSurface containing the window to be refreshed.
952 * @param w The index of the window within the surface to be refreshed.
953 * @return 0 on success, or -1 on failure.
954 */
955int ui_wnoutrefresh(UiSurface *s, ss_t w) {
956 if (!s)
957 return -1;
958 wnoutrefresh(s->mwin[w]);
959 return 0;
960}
961
962// -------------------------------------------------------------------------
963// Background
964// -------------------------------------------------------------------------
965
966// for the entire window
967/** @brief Set the background of a specific window within a UiSurface.
968 *
969 * @param s Pointer to the UiSurface containing the window.
970 * @param w The index of the window within the surface for which to set the background.
971 * @param cell Pointer to a UiCell structure representing the desired background attributes.
972 * @return 0 on success, or -1 on failure.
973 */
974int ui_bkgd(UiSurface *s, ss_t w, const UiCell *cell) {
975 if (!s)
976 return -1;
977 wbkgrnd(s->mwin[w], cell);
978 return 0;
979}
980// for new content to be written to the window
981/** @brief Set the background for new content to be written to a specific window within a UiSurface.
982 *
983 * @param s Pointer to the UiSurface containing the window.
984 * @param w The index of the window within the surface for which to set the background for new content.
985 * @param cell Pointer to a UiCell structure representing the desired background attributes for new content.
986 * @return 0 on success, or -1 on failure.
987 */
988int ui_bkgdset(UiSurface *s, ss_t w, const UiCell *cell) {
989 if (!s)
990 return -1;
991 wbkgrndset(s->mwin[w], cell);
992 return 0;
993}
994// for the entire window
995/** @brief Set the background of a specific window within a UiSurface.
996 *
997 * @param s Pointer to the UiSurface containing the window.
998 * @param w The index of the window within the surface for which to set the background.
999 * @param cell Pointer to a UiCell structure representing the desired background attributes.
1000 * @return 0 on success, or -1 on failure.
1001 */
1002int ui_bkgrnd(UiSurface *s, ss_t w, const UiCell *cell) {
1003 if (!s->mwin[w])
1004 return -1;
1005 wbkgrnd(s->mwin[w], cell);
1006 return 0;
1007}
1008// for new content to be written to the window
1009/** @brief Set the background for new content to be written to a specific window within a UiSurface.
1010 *
1011 * @param s Pointer to the UiSurface containing the window.
1012 * @param w The index of the window within the surface for which to set the background for new content.
1013 * @param cell Pointer to a UiCell structure representing the desired background attributes for new content.
1014 * @return 0 on success, or -1 on failure.
1015 */
1016int ui_bkgrndset(UiSurface *s, ss_t w, const UiCell *cell) {
1017 if (!s->mwin[w])
1018 return -1;
1019 wbkgrndset(s->mwin[w], cell);
1020 return 0;
1021}
1022/* -------------------------------------------------------------------------
1023 Cell Manipulation
1024 ------------------------------------------------------------------------- */
1025/** @brief Get the character and attributes from a UiCell.
1026 *
1027 * @param cell Pointer to the UiCell from which to retrieve the character and attributes.
1028 * @param wstr Pointer to a wchar_t array where the retrieved character will be stored.
1029 * @param attrs Pointer to an attr_t variable where the retrieved attributes will be stored.
1030 * @param pair Pointer to a short variable where the retrieved color pair will be stored.
1031 * @param opts Pointer to additional options (not used in this implementation).
1032 * @return 0 on success, or -1 on failure.
1033 */
1034int ui_getcchar(const UiCell *cell, wchar_t *wstr, attr_t *attrs, short *pair, const void *opts) {
1035 (void)opts;
1036 getcchar(cell, wstr, attrs, pair, nullptr);
1037 return 0;
1038}
1039/** @brief Set the character and attributes of a UiCell.
1040 *
1041 * @param cell Pointer to the UiCell to be modified.
1042 * @param wstr Pointer to a wchar_t array containing the character to be set.
1043 * @param attrs The attributes to be applied to the cell.
1044 * @param pair The color pair to be applied to the cell.
1045 * @param opts Pointer to additional options (not used in this implementation).
1046 * @return 0 on success, or -1 on failure.
1047 */
1048int ui_setcchar(UiCell *cell, const wchar_t *wstr, const attr_t attrs, short pair, const void *opts) {
1049 (void)opts;
1050 return setcchar(cell, wstr, attrs, pair, NULL);
1051}
1052/* -------------------------------------------------------------------------
1053 Colors, Color Pairs
1054 ------------------------------------------------------------------------- */
1055/** @brief Add a new color pair to the UI, or retrieve the index of an existing pair.
1056 *
1057 * @param fg The foreground color index for the new color pair.
1058 * @param bg The background color index for the new color pair.
1059 * @return The index of the new or existing color pair, or EXIT_FAILURE on failure.
1060 */
1061uint ui_add_pair(uint fg, uint bg) {
1062 int rc;
1063 uint i;
1064 uint pfg, pbg;
1065 for (i = 1; i < ui_pair_cnt; i++) {
1066 ui_pair_content(i, &pfg, &pbg);
1067 if (pfg == fg && pbg == bg)
1068 return i;
1069 }
1070 if (i + 1 >= UI_PAIRS) {
1071 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
1072 ssnprintf(em1, MAXLEN - 1, _("ui_add_pair failed for pair: %d"), i);
1073 strerror_r(errno, em2, MAXLEN);
1075 return (EXIT_FAILURE);
1076 }
1077 rc = init_extended_pair(i, fg, bg);
1078 if (rc == ERR) {
1079 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
1080 ssnprintf(em1, MAXLEN - 1, _("init_extended_pair failed for pair: %d"), i);
1081 ssnprintf(em2, MAXLEN - 1, _("fg: %d, bg: %d, ui_pair_cnt: %d"), fg, bg, ui_pair_cnt);
1083 return (EXIT_FAILURE);
1084 }
1085 ui_pair_cnt++;
1086 return (int)(ui_pair_cnt - 1);
1087}
1088/** @brief Change the foreground and background colors of an existing color pair.
1089 *
1090 * @param pair The index of the color pair to be modified.
1091 * @param fg The new foreground color index for the color pair.
1092 * @param bg The new background color index for the color pair.
1093 * @return 0 on success, or -1 on failure.
1094 */
1095int ui_chg_pair(uint pair, uint fg, uint bg) {
1096 if (pair + 1 >= UI_PAIRS)
1097 return -1;
1098 init_extended_pair(pair, fg, bg);
1099 return 0;
1100}
1101
1102/** was rgb_to_curses_clr
1103 * @brief Convert RGB to curses color index, adding new color if necessary.
1104 * @param rgb Pointer to RGB struct with r, g, b values (0-255).
1105 * @return Color index in curses color table, or 0 on failure.
1106 */
1107int ui_color_from_rgb(RGB *rgb) {
1108 if (rgb->r == rgb->g && rgb->g == rgb->b && rgb->b == 0)
1109 return 0;
1110 // #ifdef DEBUG_COLOR
1111 if (rgb->r > 255 || rgb->g > 255 || rgb->b > 255) {
1112 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
1113 ui_log(ERROR, "%s", em0);
1114 ssnprintf(em1, MAXLEN - 1, _("ui_color_from_rgb failed for RGB: %d,%d,%d"), rgb->r, rgb->g, rgb->b);
1115 ui_log(ERROR, "%s", em0);
1116 // return (EXIT_FAILURE);
1117 }
1118 // #endif
1119 uint i;
1120 RGB tmp;
1122 rgb->r = (rgb->r * 1000) / 255;
1123 rgb->g = (rgb->g * 1000) / 255;
1124 rgb->b = (rgb->b * 1000) / 255;
1125 for (i = 0; i < ui_color_cnt && i < UI_COLORS; i++) {
1126 extended_color_content(i, &tmp.r, &tmp.g, &tmp.b);
1127 if (rgb->r == tmp.r && rgb->g == tmp.g && rgb->b == tmp.b)
1128 return i;
1129 }
1130 if (i < UI_COLORS) {
1131 if (i < 16) {
1132 std_color[i].r = rgb->r;
1133 std_color[i].g = rgb->g;
1134 std_color[i].b = rgb->b;
1135 }
1136 init_extended_color(i, rgb->r, rgb->g, rgb->b);
1137 if (ui_color_cnt + 1 < UI_COLORS)
1138 ui_color_cnt++;
1139 return ui_color_cnt - 1;
1140 }
1141 return 0;
1142}
1143/** @brief Get the RGB color value for a given color index.
1144 *
1145 * @param color_idx The index of the color in the curses color table.
1146 * @return The RGB color value as a 32-bit unsigned integer (0xRRGGBB), or -1 on failure.
1147 */
1148uint32_t ui_get_color(uint16_t color_idx) {
1149 if (color_idx + 1 >= UI_COLORS)
1150 return -1;
1151 RGB rgb;
1152 extended_color_content(color_idx, &rgb.r, &rgb.g, &rgb.b);
1153 rgb.r = (rgb.r * 255) / 1000;
1154 rgb.g = (rgb.g * 255) / 1000;
1155 rgb.b = (rgb.b * 255) / 1000;
1156 return 0;
1157}
1158RGB ui_hex_to_rgb(char *s) {
1159 RGB rgb;
1160 sscanf(s, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
1161 return rgb;
1162}
1163int ui_color_content(uint color, uint *r, uint *g, uint *b) {
1164 int _color = (int)color;
1165 int _r, _g, _b;
1166 extended_color_content(_color, &_r, &_g, &_b);
1167 *r = (uint)_r;
1168 *g = (uint)_g;
1169 *b = (uint)_b;
1170 return 0;
1171}
1172/** @brief Initialize a color in the curses color table with specified RGB values.
1173 *
1174 * @param color The index of the color to be initialized.
1175 * @param r The red component of the color (0-255).
1176 * @param g The green component of the color (0-255).
1177 * @param b The blue component of the color (0-255).
1178 * @return 0 on success, or -1 on failure.
1179 */
1180int ui_init_color(uint color, uint r, uint g, uint b) {
1181 init_extended_color(color, r, g, b);
1182 return 0;
1183}
1184/** @brief Get the foreground and background colors of a specified color pair.
1185 *
1186 * @param pair The index of the color pair to be queried.
1187 * @param fg Pointer to a uint variable where the foreground color index will be stored.
1188 * @param bg Pointer to a uint variable where the background color index will be stored.
1189 * @return 0 on success, or -1 on failure.
1190 */
1191int ui_pair_content(uint pair, uint *fg, uint *bg) {
1192 int _pair = (int)pair;
1193 int _fg, _bg;
1194 extended_pair_content(_pair, &_fg, &_bg);
1195 *fg = (uint)_fg;
1196 *bg = (uint)_bg;
1197 return 0;
1198}
1199/** @brief Initialize a color pair in the curses color table with specified foreground and background colors.
1200 *
1201 * @param pair The index of the color pair to be initialized.
1202 * @param fg The foreground color index for the color pair.
1203 * @param bg The background color index for the color pair.
1204 * @return 0 on success, or -1 on failure.
1205 */
1206int ui_init_pair(uint pair, uint fg, uint bg) {
1207 init_extended_pair(pair, fg, bg);
1208 return 0;
1209}
1210/** @brief Change the RGB color value for a given color index.
1211 *
1212 * @param color_idx The index of the color in the curses color table to be changed.
1213 * @param color Pointer to a 32-bit unsigned integer representing the new RGB color value (0xRRGGBB).
1214 * @return 0 on success, or -1 on failure.
1215 */
1216int ui_chg_color(uint16_t color_idx, uint32_t *color) {
1217 if (color_idx + 1 >= UI_COLORS)
1218 return -1;
1219 RGB rgb;
1220 rgb.r = (*color >> 16) & 0xff;
1221 rgb.g = (*color >> 8) & 0xff;
1222 rgb.b = *color & 0xff;
1223 ui_apply_gamma(&rgb);
1224 if (color_idx < 16) {
1225 std_color[color_idx].r = rgb.r;
1226 std_color[color_idx].g = rgb.g;
1227 std_color[color_idx].b = rgb.b;
1228 }
1229 rgb.r = (rgb.r * 1000) / 255;
1230 rgb.g = (rgb.g * 1000) / 255;
1231 rgb.b = (rgb.b * 1000) / 255;
1232 init_extended_color(color_idx, rgb.r, rgb.g, rgb.b);
1233 return 0;
1234}
1235/* -------------------------------------------------------------------------
1236 Non-portable escape-hatch getters (see ui_ncurses_compat.h)
1237 ------------------------------------------------------------------------- */
1238/** @brief Get the current SCREEN pointer used by the UI.
1239 *
1240 * @return The SCREEN pointer if the UI is initialized, or NULL if the UI is not initialized.
1241 */
1243 if (!ui)
1244 return NULL;
1245 return ui->screen;
1246}
1247/** @brief Get the current FILE pointer for the TTY used by the UI.
1248 *
1249 * @return The FILE pointer for the TTY if the UI is initialized, or NULL if the UI is not initialized.
1250 */
1251WINDOW *ui_ncurses_surface_get_win(const UiSurface *s, ss_t w) {
1252 if (!s)
1253 return NULL;
1254 return s->mwin[w];
1255}
1256/** @brief Get the current PANEL pointer for a specific window within a UiSurface.
1257 *
1258 * @param s Pointer to the UiSurface containing the window.
1259 * @param w The index of the window within the surface for which to retrieve the PANEL pointer.
1260 * @return The PANEL pointer for the specified window, or NULL if the UiSurface is NULL.
1261 */
1262PANEL *ui_ncurses_surface_get_panel(const UiSurface *s, ss_t w) {
1263 if (!s)
1264 return NULL;
1265 return s->mpan[w];
1266}
1267// -------------------------------------------------------------------------
1268// Multibyte conversion
1269// -------------------------------------------------------------------------
1270uint ui__mbstr_to_cellstr(UiCell *cmplx_buf, const char *str, const UiCell *cell_base, uint *p, const uint atmost) {
1271 attr_t attrs;
1272 short cp;
1273 uint p1 = 0;
1274 uint *pos = &p1;
1275 if (p)
1276 pos = p;
1277 else
1278 pos = &p1;
1279 uint i = 0, len = 0;
1280 const char *s;
1281 UiCell cc = {};
1282 wchar_t wstr[5];
1283 getcchar(cell_base, &wstr[0], &attrs, &cp, nullptr);
1284 mbstate_t mbstate;
1285 memset(&mbstate, 0, sizeof(mbstate));
1286 if (pos && *pos >= atmost - 1)
1287 return 0;
1288 while (str[i] != '\0') {
1289 s = &str[i];
1290 len = mbrtowc(wstr, s, MB_CUR_MAX, &mbstate);
1291 if (len <= 0) {
1292 wstr[0] = L'?';
1293 wstr[1] = L'\0';
1294 len = 1;
1295 }
1296 wstr[1] = L'\0';
1297 if (*pos > atmost)
1298 break;
1299 if (setcchar(&cc, wstr, attrs, cp, nullptr) != ERR) {
1300 if (len > 0 && (*pos + len) < atmost)
1301 cmplx_buf[(*pos)++] = cc;
1302 }
1303 i += len;
1304 }
1305 wstr[0] = L'\0';
1306 wstr[1] = L'\0';
1307 setcchar(&cc, wstr, attrs, cp, nullptr);
1308 cmplx_buf[*pos] = cc;
1309 return *pos;
1310}
1313 return;
1315 f_ncurses_open = false;
1318 return;
1319}
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_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_immedok(UiSurface *s, ss_t w, bool enable)
Definition ui_ncurses.c:780
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
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
int ui_wnoutrefresh(UiSurface *s, ss_t w)
Refresh a specific window within a UiSurface.
Definition ui_ncurses.c:955
#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_doupdate()
Refresh the display by updating the screen.
Definition ui_ncurses.c:942
int ui_werase(UiSurface *s, ss_t w)
Erase the contents of a UiSurface or all surfaces.
Definition ui_ncurses.c:582
@ UI_BACKEND_NCURSES
Definition ui_backend.h:493
#define ALLWINS
Definition ui_backend.h:63
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
const wchar_t * border_none
Definition ui_common.c:89
int ui_border_draw(UiSurface *sfc, ss_t w)
Definition ui_common.c:523
bool f_ncurses_open
Definition ui_common.c:50
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
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_setcchar(UiCell *cell, const wchar_t *wstr, const attr_t attrs, short pair, const void *opts)
Set the character and attributes of a UiCell.
uint ui__mbstr_to_cellstr(UiCell *cmplx_buf, const char *str, const UiCell *cell_base, uint *p, const uint atmost)
int ui_chg_pair(uint pair, uint fg, uint bg)
Change the foreground and background colors of an existing color pair.
int ui_pair_content(uint pair, uint *fg, uint *bg)
Get the foreground and background colors of a specified color pair.
SCREEN * ui_ncurses_get_screen()
Get the current SCREEN pointer used by the UI.
int ui_getcchar(const UiCell *cell, wchar_t *wstr, attr_t *attrs, short *pair, const void *opts)
Get the character and attributes from a UiCell.
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
uint32_t ui_get_color(uint16_t color_idx)
Get the RGB color value for a given color index.
int ui_color_content(uint color, uint *r, uint *g, uint *b)
void fast_exit(UiSurface *s)
Exit the UI application quickly, cleaning up resources.
Definition ui_ncurses.c:351
UiRuntime * ui
Definition ui_ncurses.c:39
RGB ui_hex_to_rgb(char *s)
Convert a hexadecimal color string to an RGB structure.
int ui_pair_from_hex(const char *fg, const char *bg)
Convert hex color strings to a curses color pair index.
Definition ui_ncurses.c:87
WINDOW * ui_ncurses_surface_get_win(const UiSurface *s, ss_t w)
Get the current FILE pointer for the TTY used by the UI.
void ui_destroy_curses()
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
PANEL * ui_ncurses_surface_get_panel(const UiSurface *s, ss_t w)
Get the current PANEL pointer for a specific window within a UiSurface.
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 TRUE
Definition iloan.c:31
#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 ssnprintf(char *, size_t, const char *,...)
Custom safe string formatting utility (Variadic version).
Definition futil.c:398
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:57
void sig_dfl_mode()
Set signal handlers to default behavior.
Definition sig.c:41
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
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