C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_common.c
Go to the documentation of this file.
1#include "cm.h"
2#include "ui_backend.h"
3#include <fcntl.h>
4#include <iso646.h>
5#include <math.h>
6#ifdef UAL_UI
7#include "ui_ncurses_internal.h"
8#include <ncursesw/ncurses.h>
9#include <ncursesw/panel.h>
10#endif
11#ifdef NOTCURSES_UI
12#include "ui_notcurses_internal.h"
13#include <notcurses/notcurses.h>
14#endif
15#include <poll.h>
16#include <stdbool.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/ioctl.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <termios.h>
23#include <time.h>
24#include <unistd.h>
25#include <wchar.h>
26
27BorderWide bw;
28
32unsigned int cmd_key;
33bool f_sigwench = false;
34uint16_t win_attr;
35uint16_t box_attr;
37uint m_cols;
38uint m_begy = -1;
39uint m_begx = -1;
45char fn[MAXLEN];
46char em0[MAXLEN];
47char em1[MAXLEN];
48char em2[MAXLEN];
49char em3[MAXLEN];
50bool f_ncurses_open = false;
51bool f_notcurses_open = false;
52
53UiLogContext ui_log_ctx = {
54 .fd = -1,
55 .file_spec = "\0",
56 .min_level = INFO,
57 .localtime = false,
58 .timestamp = "\0",
59 .color = {
60 [FATAL] = "\033[1;31m", // Bold Red
61 [ERROR] = "\033[0;31m", // Regular Red
62 [WARN] = "\033[0;33m", // Yellow
63 [INFO] = "\033[0;32m", // Green
64 [VERBOSE] = "\033[0;36m", // Cyan
65 [DEBUG] = "\033[0;34m", // Blue
66 [SILENT] = _("SILENT"),
67 },
68 .level = {[FATAL] = "FATAL", [ERROR] = "ERROR", [WARN] = "WARN", [INFO] = "INFO", [VERBOSE] = "VERBOSE", [DEBUG] = "DEBUG", [SILENT] = "SILENT"}};
69
70// NOTE: It should be safe to use ui_ctx as a global variable, as it is only
71// used for input handling and does not contain any state that would be modified
72// by multiple threads. However, if you plan to use this in a multi-threaded
73// environment, consider using thread-local storage or mutexes to protect access
74// to this structure.
75
76ui_context_t ui_ctx;
77
79double GRAY_GAMMA = 1.2; /**< Gamma correction. Set in .minitrc */
80double RED_GAMMA = 1.2;
81double GREEN_GAMMA = 1.2;
82double BLUE_GAMMA = 1.2;
83
84/** BOX WIDE UNICODE CODEPOINTS */
85const wchar_t *border_single = L"─│├┤┬┴┼┌┐└┘";
86const wchar_t *border_double = L"═║╠╣╦╩╬╔╗╚╝";
87const wchar_t *border_rounded = L"─│├┤┬┴┼╭╮╰╯";
88const wchar_t *border_heavy = L"━┃┣┫┳┻╋┏┓┗┛";
89const wchar_t *border_none = L" ";
90
91/** MISCELANEOUS UNICODE CODEPOINTS */
92const wchar_t *bw_rtl = L"\u256d"; /**< rounded top left */
93const wchar_t *bw_rtr = L"\u256e"; /**< rounded top right */
94const wchar_t *bw_rbl = L"\u2570"; /**< rounded bottom left */
95const wchar_t *bw_rbr = L"\u256f"; /**< rounded bottom right */
96const wchar_t *bw_sp = L"\x20"; /**< space */
97const wchar_t *bw_ra = L"\u2192"; /**< large right arrow */
98const wchar_t *bw_la = L"\u2190"; /**< large left arrow */
99const wchar_t *bw_ua = L"\u2191"; /**< large up arrow */
100const wchar_t *bw_da = L"\u2193"; /**< large down arrow */
101const wchar_t *bw_ran = L"\u276F"; /**< right_angle */
102const wchar_t *bw_lan = L"\u276E"; /**< left_angle */
103const wchar_t *bw_chk = L"\u2611"; /**< left_angle */
104const wchar_t *bw_h09 = L"\u23BD"; /**< horizontal line 9 */
105
106/** colors_text
107 @brief Color names for .minitrc overrides
108 @details These names are used in .minitrc to specify color overrides The
109 order of these names corresponds to the ColorsEnum values */
110char const colors_text[][10] = {
111 _("black"), _("red"), _("green"), _("yellow"), _("blue"), _("magenta"), _("cyan"),
112 _("white"), _("orange"), _("bg"), _("abg"), _("bblack"), _("bred"), _("bgreen"),
113 _("byellow"), _("bblue"), _("bcyan"), _("bmagenta"), _("bwhite"), _("borange"), ""};
114
115// -----------------------------------------------------------------------
116// Standard UiCells
117// -----------------------------------------------------------------------
118
123UiCell cell_nt;
127UiCell cell_box;
128UiCell cell_ind;
131UiCell cell_ln;
132UiCell cell_ran;
133UiCell cell_chk;
134UiCell cell_ls;
135UiCell cell_rs;
136UiCell cell_ts;
137UiCell cell_bs;
138UiCell cell_tl;
139UiCell cell_tr;
140UiCell cell_ho;
141UiCell cell_ve;
142UiCell cell_bl;
143UiCell cell_br;
144UiCell cell_lt;
145UiCell cell_rt;
146UiCell cell_tt;
147UiCell cell_bt;
148UiCell cell_cr;
149UiCell cell_sp;
158
159// -----------------------------------------------------------------------
160// Standard Color Pairs
161// -----------------------------------------------------------------------
162
163ushort cp_box; /**< border */
164ushort cp_ind; /**< indicator character */
165ushort cp_cmdln; /**< command line */
166ushort cp_title; /**< border title */
167ushort cp_nt; /**< normal text */
168ushort cp_nt_rev; /**< normal text reverse */
169ushort cp_nt_hl; /**< normal text highlight */
170ushort cp_nt_hl_rev; /**< normal text highlight reverse */
171ushort cp_ln; /**< line numbers */
172ushort cp_default; /**< default color pair */
173ushort cp_fill_char; /**< field fill characters */
174ushort cp_brackets; /**< field enclosure brackets */
175ushort cp_black; /**< black */
176ushort cp_red; /**< red */
177ushort cp_green; /**< green */
178ushort cp_yellow; /**< yellow */
179ushort cp_blue; /**< blue */
180ushort cp_cyan; /**< cyan */
181ushort cp_magenta; /**< magenta */
182ushort cp_white; /**< white */
183
184// -----------------------------------------------------------------------
185// Standard VGA Colors
186// -----------------------------------------------------------------------
187
188STDRGB std_color[] = {
189 {0, 0, 0},
190 {128, 0, 0},
191 {0, 128, 0},
192 {128, 128, 0},
193 {0, 0, 128},
194 {128, 0, 128},
195 {0, 128, 128},
196 {192, 192, 192},
197 {128, 128, 128},
198 {255, 0, 0},
199 {0, 255, 0},
200 {255, 255, 0},
201 {0, 0, 255},
202 {255, 0, 255},
203 {0, 255, 255},
204 {255, 255, 255}};
205
206bool ui_init_clr_palette(SIO *sio);
207//-------------------------------------------------------------------------
208// Initialization
209// -------------------------------------------------------------------------
210
211void ui_initialize_sio(SIO *sio) {
212 /** gamma correction values */
213 /** These are read from ~/.minitrc */
214 /** used when initializing colors */
215 ui->sio = sio;
221 //
222 // Initialize Standardized color pairs
223 //
236
245 //
246 // Initialize Standardized UiCells
247 //
250 mbstate_t mbstate;
251 memset(&mbstate, 0, sizeof(mbstate));
252 wchar_t brktl = sio->brackets[0];
254 wchar_t brktr = sio->brackets[1];
287}
288
289bool ui_init_clr_palette(SIO *sio) {
329 return true;
330}
331// -----------------------------------------------------------------------------
332// Multibyte, Wide Character (wchar_t), and Complex Character (cchar_t) Support
333// -----------------------------------------------------------------------------
334
335/** mbc_to_wc
336 @brief Convert a multibyte character to a wide character
337 @ingroup window_support
338 @param wc Output array for wide characters (size 2)
339 @param mbc Input multibyte character
340 @details This function converts a single multibyte character to a wide
341 character. It uses the mbrtowc function to perform the conversion and handles
342 any errors by setting the output to a question mark ('?') if the conversion
343 fails. The output array is null-terminated. */
344void ui_mbc_to_wc(wchar_t wc[2], const char mbc) {
345 wc[0] = wc[1] = L'\0';
346 mbstate_t state = {};
347 size_t len = mbrtowc(wc, &mbc, MB_CUR_MAX, &state);
348 if (len <= 0) {
349 wc[0] = L'?';
350 wc[1] = L'\0';
351 len = 1;
352 }
353}
354/** ui_mbstr_to_wcstr
355 @brief Convert a multibyte string to a wide character string
356 @ingroup window_support
357 @param mb_str Input multibyte string
358 @return Pointer to newly allocated wide character string, or nullptr on error
359 @details This function converts a null-terminated multibyte string to a wide
360 character string. It first calculates the required length for the wide
361 character string, allocates memory for it, and then performs the conversion.
362 The caller is responsible for freeing the allocated memory. If the conversion
363 fails, it returns nullptr. */
364wchar_t *ui_mbstr_to_wcstr(const char *mb_str) {
365 const char *src_ptr = mb_str;
366 mbstate_t state = {};
367 size_t wc_count = mbsrtowcs(NULL, &src_ptr, 0, &state);
368 if (wc_count == (size_t)-1)
369 return nullptr;
370 src_ptr = mb_str;
371 wmemset((wchar_t *)&state, 0, sizeof(state) / sizeof(wchar_t));
372 wchar_t *wc_str = malloc((wc_count + 1) * sizeof(wchar_t));
373 if (!wc_str)
374 return nullptr;
375 mbsrtowcs(wc_str, &src_ptr, wc_count + 1, &state);
376 return wc_str;
377}
378/** ui_mbstr_to_cellstr
379 @brief Convert multibyte string to complex character array
380 @ingroup UiChyron
381 @param cmplx_buf Output buffer for complex characters
382 @param str Input multibyte string
383 @param cell_base Base cell for attributes and color pair
384 @param p Pointer to current position in the output buffer, updated as
385 characters are added
386 @param atmost Maximum length of the output buffer
387 @return Number of bytes processed from the input string
388 @details This function converts a multibyte string to an array of complex
389 characters (cchar_t) that can be used with NCurses functions. It handles
390 multibyte characters and applies the attributes and color pair from the base
391 cell to each character. The p parameter is updated to reflect the current
392 position in the output buffer, and the function ensures that it does not exceed
393 the maximum length specified by atmost.
394*/
395/** ui_abend
396 @brief Handle abnormal termination of the program
397 @ingroup window_support
398 @param ec Exit code to return to the operating system
399 @param s Message describing the reason for termination
400 @details This function is called when the program encounters a critical error
401 that requires it to terminate. It performs necessary cleanup, restores terminal
402 settings, and outputs an error message to stderr before exiting with the
403 specified exit code. */
404void ui_abend(int ec, char *s) {
408 fprintf(stderr, _("\n\nABEND: %s (code: %d)\n"), s, ec);
409 exit(EXIT_FAILURE);
410}
411// -----------------------------------------------------------------------------
412// High Level Surface Instantiation
413// -----------------------------------------------------------------------------
414/** ui_tracked_sfc_box_com
415 @brief Create a new surface with a box window
416 @ingroup window_support
417 @param wlines Number of lines for the window
418 @param wcols Number of columns for the window
419 @param wbegy Starting Y position for the window
420 @param wbegx Starting X position for the window
421 @param wtitle Title for the window
422 @return 0 on success, -1 on error
423 @details This function creates a new surface and adds a box window to it.
424 It checks if the maximum number of surfaces has been exceeded and adjusts the
425 window size based on the screen size. The new surface is stored in the global
426 ui_surface array, and the surface pointer (sfc_ptr) is incremented. The window
427 is then rendered on the screen.
428 */
429int ui_tracked_sfc_box(uint wlines, uint wcols, uint wbegy, uint wbegx, const char *wtitle) {
430 if (sfc_ptr >= SFC_MAX) {
431 ui_log(ERROR, _("Maximum number of surfaces (%d) exceeded"), SFC_MAX);
432 exit(EXIT_FAILURE);
433 }
434 uint maxy, maxx;
435 ui_get_screen_size(&maxy, &maxx);
436 wlines = min(wlines, maxy);
437 wcols = min(wcols, maxx);
438 sfc_ptr++;
439 // -------------------> UI_win_box <-------------------
440 ui_surface[sfc_ptr] = ui_surface_box(nullptr, 0, wlines + 2, wcols + 2, wbegy, wbegx, wtitle);
441 UiSurface *sfc = ui_surface[sfc_ptr];
442 ui_surface_addwin(sfc, WIN, BOX, wlines, wcols, 1, 1);
444 return 0;
445}
446// -----------------------------------------------------------------------------
447// box_split_new
448// -----------------------------------------------------------------------------
449/** surface_split_box_win_new
450 @brief Create a new surface with a split box window
451 @ingroup window_support
452 @param wlines Number of lines for the main window
453 @param wcols Number of columns for the main window
454 @param split_y Y position for the split line
455 @param split_x X position for the split line (not implemented yet)
456 @param wbegy Starting Y position for the window
457 @param wbegx Starting X position for the window
458 @param wtitle Title for the window
459 @return 0 on success, -1 on error
460 @details This function creates a new surface and adds a split box window to it.
461 It checks if the maximum number of surfaces has been exceeded and adjusts the
462 window size based on the screen size. The new surface is stored in the global
463 ui_surface array, and the surface pointer (sfc_ptr) is incremented. The main
464 window and the split window are then rendered on the screen, with a separator
465 line drawn at the specified Y position.
466 */
467int ui_tracked_sfc_split_box(uint wlines, uint wcols, uint split_y, uint split_x, uint wbegy, uint wbegx, const char *wtitle) {
468 if (sfc_ptr >= SFC_MAX) {
469 ui_log(ERROR, _("Maximum number of surfaces (%d) exceeded"), SFC_MAX);
470 exit(EXIT_FAILURE);
471 }
472 if (wlines == 0 || wcols == 0) {
473 ui_log(ERROR, _("Invalid window size: lines=%d, cols=%d"), wlines, wcols);
474 return -1;
475 }
476 uint maxy, maxx;
477 ui_get_screen_size(&maxy, &maxx);
478 if (wbegy > maxy || wbegx > maxx) {
479 ui_log(ERROR, _("Invalid window position: wbegy=%d, wbegx=%d, wlines=%d, wcols=%d"), wbegy, wbegx, wlines, wcols);
480 return -1;
481 }
482 wlines = min(wlines, maxy);
483 wcols = min(wcols, maxx);
484 split_x = min(split_x, maxx); // not implemented yet
485 uint split_wlines = min(wlines + split_y + 1, maxy);
486 wcols = min(wcols, maxx);
487 sfc_ptr++;
488 // -------------------> surface_new <-------------------
489 ui_surface[sfc_ptr] = ui_surface_box(nullptr, 0, split_wlines + 2, wcols + 2, wbegy, wbegx, wtitle);
490 UiSurface *sfc = ui_surface[sfc_ptr];
491
492 ui_surface_addwin(sfc, WIN, BOX, wlines, wcols, 1, 1);
493 ui_bkgd(sfc, WIN, &cell_nt);
494 ui_werase(sfc, WIN);
495 ui_border_ysplit(sfc, BOX, wlines + 1);
496 ui_surface_addwin(sfc, WIN2, BOX, 2, wcols, wlines + 2, 1);
497 ui_bkgd(sfc, WIN2, &cell_nt);
498 ui_werase(sfc, WIN2);
500#ifdef DEBUG_IMMED_UI
501 ui_render();
502#endif
503 return 0;
504}
505// -----------------------------------------------------------------------------
506// ui_cm_surface_destroy
507// -----------------------------------------------------------------------------
508/** ui_cm_destroy_surface
509 @brief Destroy the most recently created surface
510 @ingroup window_support
511 @return 0 on success, -1 if no surfaces exist
512 @details This function destroys the most recently created surface and decrements the surface pointer. It should be called when a surface is no longer needed to free up resources. If there are no surfaces to destroy, it returns -1.
513 @note The difference between this function and ui_surface_destroy() is that this function destroys the surface pointed to by the surface pointer (sfc_ptr) and decrements the surface pointer after destroying the surface.
514 */
515int ui_cm_surface_destroy(UiSurface *sfc) {
517 sfc_ptr--;
518 return 0;
519}
520// -----------------------------------------------------------------------------
521// ui_border_draw
522// -----------------------------------------------------------------------------
523int ui_border_draw(UiSurface *sfc, ss_t w) {
524 uint maxy = ui_getmaxy(sfc, w);
525 uint maxx = ui_getmaxx(sfc, w);
526 uint y = 0;
527 uint x = 0;
528 ui_mvwadd_wchnstr(sfc, w, y, x++, &cell_tl, 1);
529 for (x = 1; x < maxx - 1; x++)
531 ui_mvwadd_wchnstr(sfc, w, y, maxx - 1, &cell_tr, 1);
532 for (y = 1; y < maxy - 1; y++) {
534 ui_mvwadd_wchnstr(sfc, w, y, maxx - 1, &cell_ve, 1);
535 }
537 for (x = 1; x < maxx - 1; x++)
539 ui_mvwadd_wchnstr(sfc, w, y, maxx - 1, &cell_br, 1);
540#ifdef DEBUG_IMMED_UI
541 ui_render();
542#endif
543 return 0;
544}
545/** ui_border-ysplit
546 @brief Draw a box with a separator line around the specified window
547 @ingroup window_support
548 @param box Pointer to the window to draw the box around
549 @param y Line number where the separator line should be drawn
550 @details This function draws a box around the specified window, similar to
551 border_draw(), but it also includes a horizontal separator line that divides the box
552 into two sections. The separator line is drawn at a fixed position (line 00,
553 page 00) and extends across the width of the box. Use this function when you
554 want to visually separate two sections within a window, such as for a header
555 and content area. */
556int ui_border_ysplit(UiSurface *sfc, ss_t w, uint y) {
557 uint maxx = ui_getmaxx(sfc, w);
559 for (uint x = 1; x < maxx - 1; x++)
561 ui_mvwadd_wchnstr(sfc, w, y, maxx - 1, &cell_rt, 1);
562#ifdef DEBUG_IMMED_UI
563 ui_render();
564#endif
565 return 0;
566}
567/** ui_border_ysplit_text
568 @brief Draw a box with a separator line and text around the specified window
569 @ingroup window_support
570 @param box Pointer to the window to draw the box around
571 @param text Text to display in the middle of the separator line
572 @param separator_line Line number where the separator line should be drawn
573 @details This function draws a box around the specified window, similar to
574 border_draw(), but it also includes a horizontal separator line that divides the box
575 into two sections. The separator line is drawn at the specified line number and
576 extends across the width of the box, with the provided text displayed in the
577 middle of the line. Use this function when you want to visually separate two
578 sections within a window and label the separator with descriptive text. */
579int ui_border_ysplit_text(UiSurface *sfc, char *text_in, uint separator_line) {
580 char text[MAXLEN];
581 uint maxx = ui_getmaxx(sfc, BOX);
582 uint l;
583 uint y = separator_line;
584 uint x = 0;
585 // Draw the horizontal line with text in the middle, so we start by drawing
586 // the left edge, then the text, then the right edge, and finally fill in the
587 // horizontal line on either side of the text.
588 ui_mvwadd_wchnstr(sfc, BOX, y, x++, &cell_lt, 1);
589 ui_mvwadd_wchnstr(sfc, BOX, y, x++, &cell_ho, 1);
590 ui_mvwadd_wchnstr(sfc, BOX, y, x++, &cell_rt, 1);
591 ui_mvwadd_wchnstr(sfc, BOX, y, x++, &cell_sp, 1);
592 strnz__cpy(text, text_in, maxx - 7);
593 wchar_t *text_wc;
594 text_wc = ui_mbstr_to_wcstr(text);
595 l = wcswidth(text_wc, wcslen(text_wc));
597 ui_mvwaddnwstr(sfc, BOX, y, x, text_wc, l);
598 x += l;
599 l = min(l, maxx - 7);
600 free(text_wc);
601 ui_mvwadd_wchnstr(sfc, BOX, y, x++, &cell_sp, 1);
602 ui_mvwadd_wchnstr(sfc, BOX, y, x++, &cell_lt, 1);
603 while (x < maxx - 1)
604 ui_mvwadd_wchnstr(sfc, BOX, y, x++, &cell_ho, 1);
605 ui_mvwadd_wchnstr(sfc, BOX, y, x++, &cell_rt, 1);
606#ifdef DEBUG_IMMED_UI
607 ui_render();
608#endif
609 return 0;
610}
611/** border_title
612 @brief Draw a box with a title around the specified window
613 @ingroup window_support
614 @param box Pointer to the window to draw the box around
615 @param title Title text to display at the top of the box
616 @details This function draws a box around the specified window, similar to
617 border_draw(), but it also includes a title at the top of the box. The title
618 is displayed in the center of the top edge of the box, and the horizontal line
619 is drawn on either side of the title. Use this function when you want to
620 visually label a window with a title. */
621int ui_border_title(UiSurface *sfc, ss_t w, const char *title) {
622 if (!title || !*title)
623 return 0;
624 uint y = 0;
625 uint x = 0;
626 uint l;
627 uint maxx = ui_getmaxx(sfc, w);
628 ui_mvwadd_wchnstr(sfc, w, y, x++, &cell_tl, 1);
629 ui_mvwadd_wchnstr(sfc, w, y, x++, &cell_rt, 1);
630 ui_mvwadd_wchnstr(sfc, w, y, x++, &cell_sp, 1);
631 wchar_t *title_wc;
632 title_wc = ui_mbstr_to_wcstr(title);
633 l = wcswidth(title_wc, wcslen(title_wc));
634 l = min(l, maxx - 7);
636 ui_mvwaddnwstr(sfc, w, y, x, title_wc, l);
638 x += l;
639 free(title_wc);
640 ui_mvwadd_wchnstr(sfc, w, y, x++, &cell_sp, 1);
641 ui_mvwadd_wchnstr(sfc, w, y, x++, &cell_lt, 1);
642 while (x < maxx - 1) {
643 ui_mvwadd_wchnstr(sfc, w, y, x++, &cell_ho, 1);
644 }
645#ifdef DEBUG_IMMED_UI
646 ui_render();
647#endif
648 return 0;
649}
650// -----------------------------------------------------------------------
651// Chyron API
652// -----------------------------------------------------------------------
653/** new_chyron
654 @brief Create new chyron structure
655 @ingroup UiChyron
656 @return pointer to new chyron structure
657 @details This function allocates memory for a new chyron structure and its
658 associated keys. It initializes the structure to zero and returns a pointer
659 to the newly created chyron. If memory allocation fails, it calls ui_abend() to
660 terminate the program with an error message.
661 */
662UiChyron *ui_new_chyron(UiSurface *sfc, ss_t w) {
663 UiChyron *chyron = (UiChyron *)calloc(1, sizeof(UiChyron));
664 if (!chyron) {
665 ui_abend(-1, _("calloc chyron failed"));
666 return nullptr;
667 }
668 chyron->sfc = sfc;
669 chyron->w = w;
670 for (int i = 0; i < CHYRON_KEYS; i++) {
671 chyron->key[i] = (UiChyronKey *)calloc(1, sizeof(UiChyronKey));
672 if (!chyron->key[i]) {
673 ui_abend(-1, _("calloc chyron->key[i] failed"));
674 return nullptr;
675 }
676 }
677 return chyron;
678}
679/** assign_chyron_win
680 @brief Assign surface and window to chyron structure
681 @ingroup UiChyron
682 @param chyron pointer to UiChyron structure
683 @param sfc pointer to UiSurface structure
684 @param win window index (WIN or BOX)
685 @param y line number for chyron display
686 @return 0 on success, -1 on error
687 @details This function assigns the specified surface and window to the chyron
688 structure. It also sets the line number for displaying the chyron. If the
689 input string y is "-", it sets the line number to the last line of the
690 surface. Otherwise, it converts the string y to an integer and sets it as the
691 line number, ensuring it does not exceed the maximum line number of the
692 surface.
693 @note Drop-down menus from the chyron will be added in the future
694 */
695int ui_assign_chyron_win(UiChyron *chyron, UiSurface *sfc, ss_t w, char *y) {
696 bool a_toi_error = false;
697 if (!sfc)
698 return -1;
699 chyron->sfc = sfc;
700 chyron->w = w;
701 if (*y == '-')
702 chyron->y = ui_getmaxy(sfc, w) - 1;
703 else {
704 chyron->y = a_toi(y, &a_toi_error);
705 if (a_toi_error)
706 return -1;
707 chyron->y = min(chyron->y, ui_getmaxy(sfc, w) - 1);
708 }
709 return 0;
710}
711/** destroy_chyron
712 @brief Destroy UiChyron structure
713 @ingroup UiChyron
714 @param chyron pointer to UiChyron structure
715 @return nullptr
716 */
717UiChyron *ui_destroy_chyron(UiChyron *chyron) {
718 uint i;
719
720 if (!chyron)
721 return nullptr;
722 for (i = 0; i < CHYRON_KEYS; i++) {
723 if (chyron->key[i]) {
724 free(chyron->key[i]);
725 chyron->key[i] = nullptr;
726 }
727 }
728 free(chyron);
729 chyron = nullptr;
730 return chyron;
731}
732/** is_set_chyron_key
733 @brief Check if function key label is set
734 @ingroup UiChyron
735 @param chyron structure
736 @param k Function key index (0-19)
737 @return true if set, false if not set */
738bool ui_is_set_chyron_key(UiChyron *chyron, uint k) {
739 if (chyron->key[k]->text[0] != '\0')
740 return true;
741 else
742 return false;
743}
744/** set_chyron_key
745 @brief Set chyron key with color pair (cp)
746 @ingroup UiChyron
747 @param chyron structure
748 @param k chyron key index (0-19)
749 @param s chyron key label
750 @param kc chyron key code
751 @param cell_base color pair for chyron key label
752 @details This function is like set_chyron_key, except it includes a color
753 pair numbers */
754void ui_set_chyron_key_cb(UiChyron *chyron, uint k, char *s, uint kc, UiCell cell_base) {
755 if (*s != '\0')
756 ssnprintf(chyron->key[k]->text, CHYRON_KEY_MAXLEN - 1, "%s", s);
757 else
758 chyron->key[k]->text[0] = '\0';
759 chyron->key[k]->keycode = kc;
760 chyron->key[k]->active = true;
761 chyron->key[k]->cell_base = cell_base;
762}
763/** set_chyron_key
764 @brief Set chyron key with default color pair (cp_nt_rev)
765 @ingroup UiChyron
766 @param chyron structure
767 @param k chyron key index (0-19)
768 @param s chyron key label
769 @param kc chyron key code
770 @details This function sets the label and keycode for a function key in the
771 chyron structure. It uses the default color pair cp_nt_rev for the key
772 label. If the input string s is not empty, it copies the string into the
773 chyron key's text field. If the input string is empty, it sets the first
774 character of the text field to '\0' to indicate that the key is not set.
775 The keycode is stored in the chyron key's keycode field, and the color pair
776 index is set to cp_nt_rev.
777 */
778void ui_set_chyron_key(UiChyron *chyron, uint k, char *s, uint kc) {
779 if (*s != '\0')
780 ssnprintf(chyron->key[k]->text, CHYRON_KEY_MAXLEN - 1, "%s", s);
781 else
782 chyron->key[k]->text[0] = '\0';
783 chyron->key[k]->keycode = kc;
784 chyron->key[k]->active = true;
785 chyron->key[k]->cell_base = cell_nt_rev;
786}
787/** unset_chyron_key
788 @brief Unset chyron key
789 @ingroup UiChyron
790 @param chyron structure
791 @param k chyron_key index
792*/
793void ui_unset_chyron_key(UiChyron *chyron, uint k) {
794 chyron->key[k]->text[0] = '\0';
795}
796/** activate_chyron_key
797 @brief Activate chyron key
798 @ingroup UiChyron
799 @param chyron structure
800 @param k chyron_key index
801*/
802void ui_activate_chyron_key(UiChyron *chyron, uint k) {
803 chyron->key[k]->active = true;
804}
805/** deactivate_chyron_key
806 @brief Deactivate chyron key
807 @ingroup UiChyron
808 @param chyron structure
809 @param k chyron_key index
810*/
811void ui_activate_all_chyron_keys(UiChyron *chyron) {
812 for (int k = 0; k < CHYRON_KEYS; k++)
813 chyron->key[k]->active = true;
814}
815/** deactivate_chyron_key
816 @brief Deactivate chyron key
817 @ingroup UiChyron
818 @param chyron structure
819 @param k chyron_key index
820*/
821void ui_deactivate_chyron_key(UiChyron *chyron, uint k) {
822 chyron->key[k]->active = false;
823}
824/** deactivate_all_chyron_keys
825 @brief Deactivate all chyron keys
826 @ingroup UiChyron
827 @param chyron structure
828*/
829void ui_deactivate_all_chyron_keys(UiChyron *chyron) {
830 for (int k = 0; k < CHYRON_KEYS; k++)
831 chyron->key[k]->active = false;
832}
833/** compile_chyron
834 @brief construct the chyron string from the chyron structure
835 @ingroup UiChyron
836 @param chyron
837 @details The chyron string is constructed by concatenating the labels of the
838 set keys, separated by _(" | "). The end_pos values for each key are set to
839 determine the zones for mouse clicks. When a mouse click occurs, the
840 get_chyron_key function uses the end_pos values to determine which key was
841 clicked based on the X position of the click.
842*/
843void ui_compile_chyron(UiChyron *chyron) {
844 uint end_pos = 0;
845 uint k = 0;
846 uint pos = 0;
847 UiCell cell_base = cell_nt_rev;
848 UiCell *cx;
849 char tmp_str[MAXLEN];
850 while (k < CHYRON_KEYS) {
851 if (chyron->key[k]->text[0] == '\0' || !chyron->key[k]->active) {
852 k++;
853 continue;
854 }
855 cell_base = chyron->key[k]->cell_base;
856 if (end_pos == 0) {
857 cx = chyron->cmplx_buf;
858 ui_mbstr_to_cellstr(chyron->sfc, chyron->w,
859 cx,
860 " ",
861 &cell_base,
862 &pos,
863 MAXLEN - 1);
864 } else {
865 ui_mbstr_to_cellstr(chyron->sfc, chyron->w,
866 chyron->cmplx_buf,
867 "|",
868 &cell_base,
869 &pos,
870 MAXLEN - 1);
871 }
872 cx = chyron->cmplx_buf;
873 ui_mbstr_to_cellstr(chyron->sfc, chyron->w, cx, chyron->key[k]->text, &cell_base, &pos, MAXLEN - 1);
874 end_pos = pos;
875 chyron->l = end_pos;
876 chyron->key[k]->end_pos = end_pos;
877 ssnprintf(tmp_str, MAXLEN - 1, _("k=%d, text=%s, end_pos=%d"), k,
878 chyron->key[k]->text, chyron->key[k]->end_pos);
879 k++;
880 }
881 ui_mbstr_to_cellstr(chyron->sfc, chyron->w, chyron->cmplx_buf, " ", &cell_base, &pos, MAXLEN - 1);
882 chyron->l = end_pos;
883}
884/** display_chyron
885 * @brief Display chyron on the specified line and column of the surface
886 @ingroup UiChyron
887 @param sfc Pointer to the UiSurface structure
888 @param w Window index (WIN or BOX)
889 @param chyron Pointer to the UiChyron structure
890 @param line Line number where the chyron should be displayed
891 @param col Column number where the chyron should start
892 @details This function displays the compiled chyron string on the specified
893 line and column of the given surface. It first moves the cursor to the
894 specified position, clears to the end of the line, and then writes the chyron
895 string. Finally, it moves the cursor back to the specified column for user
896 input.
897 */
898void ui_display_chyron(UiSurface *sfc, ss_t w, UiChyron *chyron, uint line, uint col) {
899 chyron->y = line;
900 ui_wmove(sfc, w, line, 0);
901 ui_wclrtoeol(sfc, w);
902 ui_wmove(sfc, w, line, 0);
903 ui_mvwadd_wchstr(sfc, w, line, 0, chyron->cmplx_buf);
904 ui_wmove(sfc, w, line, col);
905 return;
906}
907/** get_chyron_key
908 @brief Get keycode from chyron
909 @ingroup UiChyron
910 @param chyron structure
911 @param x Mouse X position
912 @return Keycode
913 @details This function uses the end_pos values set in compile_chyron
914 to determine which key was clicked.
915 The chyron functions provide xwgetch() with a mechanism to translate
916 mouse click positions into key codes based on the labels set in the chyron
917 structure. When a mouse click occurs, xwgetch() can call get_chyron_key()
918 with the X position of the click to determine which function key was clicked,
919 allowing for dynamic and customizable function key behavior in the chyron
920 area of the interface.
921*/
922int ui_get_chyron_key(UiChyron *chyron, uint x) {
923 uint i = 0;
924 uint k = -1;
925 while (i < CHYRON_KEYS - 1) {
926 if (chyron->key[i]->text[0] != '\0' && chyron->key[i]->active)
927 if (chyron->key[i]->end_pos >= x) {
928 k = i;
929 break;
930 }
931 i++;
932 }
933 return chyron->key[k]->keycode;
934}
935// -----------------------------------------------------------------------
936// Color format translations
937// -----------------------------------------------------------------------
938/** ui_rgb_to_xterm256_idx
939 @brief Convert RGB color to XTerm 256 color index
940 @ingroup color_management
941 @param rgb RGB color
942 @return XTerm 256 color index
943 @details This function converts an RGB color to the nearest XTerm 256 color
944 index. It first checks if the color is a shade of gray, and if so, it uses
945 the gray ramp. Otherwise, it calculates the nearest color in the 6x6x6 color
946 cube. */
948 if (rgb->r == rgb->g && rgb->g == rgb->b) {
949 if (rgb->r < 8)
950 return 16;
951 if (rgb->r > 248)
952 return 231;
953 return ((rgb->r - 8) / 10) + 231;
954 } else {
955 uint r_index = (rgb->r < 45) ? 0 : (rgb->r - 60) / 40 + 1;
956 uint g_index = (rgb->g < 45) ? 0 : (rgb->g - 60) / 40 + 1;
957 uint b_index = (rgb->b < 45) ? 0 : (rgb->b - 60) / 40 + 1;
958 return 16 + (36 * r_index) + (6 * g_index) + b_index;
959 }
960}
961/** ui_xterm256_idx_to_rgb
962 @brief Convert XTerm 256 color index to RGB
963 @ingroup color_management
964 @param idx XTerm 256 color index
965 @return RGB color
966 @details This function converts an XTerm 256 color index to an RGB color. It
967 first checks if the index is in the standard 16 colors, then checks if it's
968 in the 6x6x6 color cube, and finally checks if it's in the gray ramp. */
970 /** Convert XTerm 256 color index to RGB
971 @param idx - XTerm 256 color index
972 @return RGB struct */
973 RGB rgb;
974 if (idx > 255)
975 idx = 255;
976 rgb.r = rgb.g = rgb.b = 0;
977 if (idx < 16) {
978 rgb.r = std_color[idx].r;
979 rgb.g = std_color[idx].g;
980 rgb.b = std_color[idx].b;
981 } else if (idx >= 16 && idx <= 231) {
982 idx -= 16;
983 rgb.r = (idx / 36) % 6 * 51;
984 rgb.g = (idx / 6) % 6 * 51;
985 rgb.b = (idx % 6) * 51;
986 } else if (idx >= 232 && idx <= 255) {
987 uint gray = (idx - 232) * 11;
988 rgb.r = rgb.g = rgb.b = gray;
989 }
990 return rgb;
991}
992
993/** ui_apply_gamma
994 @brief Apply gamma correction to RGB color
995 @ingroup color_management
996 @param rgb Pointer to RGB color
997 @details This function modifies the RGB color in place. It applies gamma
998 correction to the RGB color based on the gamma values set in the SIO struct.
999 If the color is a shade of gray, it applies the gray gamma correction.
1000 Otherwise, it applies the individual red, green, and blue gamma corrections.
1001 */
1002void ui_apply_gamma(RGB *rgb) {
1003 if (rgb->r == rgb->g && rgb->r == rgb->b) {
1004 if (GRAY_GAMMA > 0.0f && GRAY_GAMMA != 1.0f) {
1005 rgb->r = (int)(pow((rgb->r / 255.0f), 1.0f / GRAY_GAMMA) * 255.0f);
1006 rgb->g = rgb->r;
1007 rgb->b = rgb->r;
1008 }
1009 return;
1010 }
1011 if (rgb->r != 0 && RED_GAMMA > 0.0f && RED_GAMMA != 1.0f)
1012 rgb->r = (int)(pow((rgb->r / 255.0f), 1.0f / RED_GAMMA) * 255.0f);
1013 if (rgb->g != 0 && GREEN_GAMMA > 0.0f && GREEN_GAMMA != 1.0f)
1014 rgb->g = (int)(pow((rgb->g / 255.0f), 1.0f / GREEN_GAMMA) * 255.0f);
1015 if (rgb->b != 0 && BLUE_GAMMA > 0.0f && BLUE_GAMMA != 1.0f)
1016 rgb->b = (int)(pow((rgb->b / 255.0f), 1.0f / BLUE_GAMMA) * 255.0f);
1017}
1018// -----------------------------------------------------------------------
1019// Error Handling
1020// -----------------------------------------------------------------------
1021/**
1022 * @defgroup error_handling Error Handling
1023 @brief Display Error messages
1024 */
1025/** ui_answer_yn
1026 @brief Accept a single letter answer
1027 @ingroup error_handling
1028 @param msg0 First error message line
1029 @param msg1 Second error message line
1030 @param msg2 Third error message line
1031 @param msg3 Fourth error message line
1032 @return Key code of user command */
1033int ui_answer_yn(char *msg0, char *msg1, char *msg2, char *msg3) {
1034 char title[MAXLEN];
1035 uint line, pos, msg_l, msg0_l, msg1_l, msg2_l, msg3_l;
1036
1038 fprintf(stderr, _("\n\n%s\n%s\n%s\n%s\n\n"), msg0, msg1, msg2, msg3);
1039 return 1;
1040 }
1041 uint maxy, maxx;
1042 ui_get_screen_size(&maxy, &maxx);
1043 msg0_l = strlen(msg0);
1044 msg1_l = strlen(msg1);
1045 msg2_l = strlen(msg2);
1046 msg3_l = strlen(msg3);
1047 msg_l = max(msg0_l, msg1_l);
1048 msg_l = max(msg_l, msg2_l);
1049 msg_l = max(msg_l, msg3_l);
1050 msg_l = max(msg_l, 50);
1051 msg_l = min(msg_l, maxx - 4);
1052 pos = ((maxx - msg_l) - 4) / 2;
1053 line = (maxy - 6) / 2;
1054 strnz__cpy(title, _("Notification"), MAXLEN - 1);
1055 if (ui_tracked_sfc_box(5, msg_l, line, pos, title)) {
1056 ssnprintf(title, MAXLEN - 1, _("ui_tracked_sfc_box(%d, %d, %d, %d, %s) failed"), 5,
1057 msg_l + 2, line, pos, title);
1058 ui_abend(-1, title);
1059 }
1060 UiSurface *sfc = ui_surface[sfc_ptr];
1061 UiChyron *chyron = ui_new_chyron(sfc, WIN);
1062 ui_set_chyron_key(chyron, 1, _("F1 Help"), UIKEY_F01);
1063 ui_set_chyron_key(chyron, 2, _("N - No"), 'n');
1064 ui_set_chyron_key(chyron, 3, _("Y - Yes"), 'y');
1065 ui_compile_chyron(chyron);
1066 UiEvent event;
1067 ui_draw_text(sfc, WIN, 0, 1, msg0);
1068 ui_draw_text(sfc, WIN, 1, 1, msg1);
1069 ui_draw_text(sfc, WIN, 2, 1, msg2);
1070 ui_draw_text(sfc, WIN, 3, 1, msg3);
1071 ui_display_chyron(sfc, WIN, chyron, 4, chyron->l + 1);
1072 do {
1074 event.y = event.x = -1;
1075 cmd_key = ui_get_event(sfc, WIN, chyron, &event, -1);
1076 if (cmd_key == UIKEY_F01 || cmd_key == 'N' || cmd_key == 'n' || cmd_key == 'Y' || cmd_key == 'y')
1077 break;
1078 } while (1);
1080 ui_destroy_chyron(chyron);
1081 return (cmd_key);
1082}
1083// -----------------------------------------------------------------------
1084// Multiplexed Input
1085// -----------------------------------------------------------------------
1086/** ui_timeout_prompt
1087 @brief Display a timeout prompt window or print to stderr
1088 @ingroup error_handling
1089 @param msg0 First message line
1090 @param msg1 Second message line
1091 @param timeout_ms Timeout in milliseconds
1092 @param timeout_secs Timeout in seconds to add if F10 is pressed
1093 @return Key code of user command */
1094int ui_timeout_prompt(char *msg0, char *msg1, uint timeout_ms, uint timeout_secs) {
1095 char title[MAXLEN];
1096 uint line, pos, msg_l, msg0_l, msg1_l;
1098 fprintf(stderr, "\n\n%s\n", msg0);
1099 fprintf(stderr, "%s\n", msg1);
1100 return 1;
1101 }
1102 uint maxy, maxx;
1103 ui_get_screen_size(&maxy, &maxx);
1104 msg0_l = strlen(msg0);
1105 msg1_l = strlen(msg1);
1106 msg_l = max(msg0_l, msg1_l);
1107 msg_l = max(msg_l, 50);
1108 msg_l = min(msg_l, maxx - 4);
1109
1110 pos = ((maxx - msg_l) - 4) / 2;
1111 line = (maxy - 6) / 2;
1112 strnz__cpy(title, _("Notification"), MAXLEN - 1);
1113 if (ui_tracked_sfc_box(5, msg_l, line, pos, title)) {
1114 ssnprintf(title, MAXLEN - 1, _("ui_tracked_sfc_box(%d, %d, %d, %d, %s) failed"), 5,
1115 msg_l + 2, line, pos, title);
1116 ui_abend(-1, title);
1117 }
1118 UiSurface *sfc = ui_surface[sfc_ptr];
1119 UiChyron *chyron = ui_new_chyron(sfc, WIN);
1120 ui_set_chyron_key(chyron, 1, _("F1 Help"), UIKEY_F01);
1121 ui_set_chyron_key(chyron, 9, _("F9 Cancel"), UIKEY_F09);
1122 ui_set_chyron_key(chyron, 10, _("F10 Continue"), UIKEY_F10);
1123 ui_compile_chyron(chyron);
1124 UiEvent event;
1125 ui_draw_text(sfc, WIN, 0, 1, msg0);
1126 ui_draw_text(sfc, WIN, 1, 1, msg1);
1127 ssnprintf(em3, MAXLEN - 1, _("Timeout in %d seconds"), timeout_ms / 1000);
1128 ui_draw_text(sfc, WIN, 3, 1, em3);
1129 ui_display_chyron(sfc, WIN, chyron, 4, chyron->l + 1);
1130 do {
1131 event.y = event.x = -1;
1132 cmd_key = ui_get_event(sfc, WIN, chyron, &event, 1000);
1133 if (cmd_key == UIKEY_F09 || cmd_key == 'q' || cmd_key == 'Q' || cmd_key == UIKEY_STREAM_DATA)
1134 break;
1135 if (cmd_key == UIKEY_F10)
1136 timeout_ms += timeout_secs * 1000;
1137 else
1138 timeout_ms -= 1000;
1139 ui_update_timeout_prompt(sfc, WIN, timeout_ms);
1140 } while (timeout_ms > 0);
1142 ui_destroy_chyron(chyron);
1144 return cmd_key;
1145}
1146/** ui_update_timeout_prompt
1147 @brief Update the timeout prompt with the remaining time
1148 @ingroup error_handling
1149 @param sfc Pointer to the UiSurface structure
1150 @param w Window index (WIN or BOX)
1151 @param timeout_ms Remaining timeout in milliseconds
1152 @return 0 on success, 1 if not in a curses environment
1153 @details This function updates the timeout prompt displayed on the specified
1154 surface and window with the remaining time in seconds. If not in a curses
1155 environment, it prints the timeout message to stderr.
1156 */
1157int ui_update_timeout_prompt(UiSurface *sfc, ss_t w, uint timeout_ms) {
1159 fprintf(stderr, _("Timeout in %d seconds\n"), timeout_ms / 1000);
1160 return 1;
1161 }
1162 ssnprintf(em3, MAXLEN - 1, _("%d seconds"), timeout_ms / 1000);
1163 ui_draw_text(sfc, w, 3, 12, em3);
1164 ui_wclrtoeol(sfc, w);
1166 return 0;
1167}
1168/** ui_register_read_fd
1169 @brief Register an external file descriptor for polling
1170 @ingroup error_handling
1171 @param fd File descriptor to register
1172 @param token_id Token ID associated with the file descriptor
1173 @details This function registers an external file descriptor for polling. It
1174 adds the file descriptor and its associated token ID to the list of registered
1175 external file descriptors. If the maximum number of registered file descriptors
1176 is reached, it does not register the new file descriptor.
1177 */
1178void ui_register_read_fd(int fd, int token_id) {
1179 char tmp_str[MAXLEN];
1181 ui_ctx.ext_fds[ui_ctx.ext_nfds] = (ext_fd_reg_t){fd, token_id};
1183 ssnprintf(tmp_str, MAXLEN - 1, "ui_ctx.ext_nfds = %ld", ui_ctx.ext_nfds);
1184 }
1185}
1186/** ui_unregister_read_fd
1187 @brief Unregister an external file descriptor from polling
1188 @ingroup error_handling
1189 @param fd File descriptor to unregister
1190 @details This function unregisters an external file descriptor from polling.
1191 It removes the file descriptor and its associated token ID from the list of
1192 registered external file descriptors. If the file descriptor is not found in
1193 the list, it does nothing.
1194 */
1196 for (nfds_t i = 0; i < ui_ctx.ext_nfds; i++) {
1197 if (ui_ctx.ext_fds[i].fd == fd) {
1200 break;
1201 }
1202 }
1203}
1204/** ui_poll_reg_read_fd
1205 @brief Poll for input on registered external files
1206 @ingroup error_handling
1207 @param timeout_ms Timeout in milliseconds
1208 @return Token ID of the external file that has input, or 0 if no input
1209 @details This function polls for input on registered external files. It uses
1210 the poll() system call to wait for input on the file descriptors of the
1211 registered external files. If input is available, it returns the token ID of
1212 the external file that has input. If no input is available within the
1213 specified timeout, it returns 0. If an error occurs during polling, it
1214 returns -1.
1215 */
1216int ui_poll_reg_read_fd(int timeout_ms) {
1217 if (timeout_ms <= 0)
1218 return 0;
1219 // ------------------------------------------------------------
1220 // Poll for input on registered external files
1221 // ------------------------------------------------------------
1222 int token_id = 0;
1223 int reg_idx;
1224 nfds_t nfds = 0;
1225 nfds_t ext_start_idx = ui_ctx.ext_nfds;
1226 struct pollfd fds[1 + MAX_EXT_FDS];
1227 memset(fds, 0, sizeof(fds));
1228 for (nfds_t i = 0; i < ui_ctx.ext_nfds; i++) {
1229 fds[nfds].fd = ui_ctx.ext_fds[i].fd;
1230 fds[nfds].events = POLLIN; // also POLLERR | POLLHUP | POLLNVAL
1231 nfds++;
1232 }
1233 int poll_rc = poll(fds, nfds, timeout_ms);
1234 if (poll_rc < 0) {
1235 if (errno == EINTR)
1236 return 0;
1237 return -1;
1238 }
1239 if (poll_rc == 0)
1240 return 0;
1241 for (nfds_t i = ext_start_idx; i < nfds; i++) {
1242 if (fds[i].revents & (POLLIN | POLLHUP | POLLERR)) {
1243 reg_idx = i - ext_start_idx;
1244 token_id = ui_ctx.ext_fds[reg_idx].token_id;
1245 return token_id;
1246 }
1247 }
1248 return 0;
1249}
1250// -----------------------------------------------------------------------
1251// Error handling
1252// -----------------------------------------------------------------------
1253/** ui_display_error
1254 @brief Display an error message window or print to stderr
1255 @ingroup error_handling
1256 @param msg0 First error message line
1257 @param msg1 Second error message line
1258 @param msg2 Third error message line
1259 @param msg3 Fourth error message line
1260 @return Key code of user command */
1261int ui_display_error(char *msg0, char *msg1, char *msg2, char *msg3) {
1262 char title[MAXLEN];
1263 uint line = 0, pos = 0, msg_l = 0, msg0_l = 0, msg1_l = 0, msg2_l = 0, msg3_l = 0;
1265 fprintf(stderr, "\n\n%s\n", msg0);
1266 fprintf(stderr, "%s\n", msg1);
1267 fprintf(stderr, "%s\n", msg2);
1268 fprintf(stderr, "%s\n\n", msg3);
1269 return 1;
1270 }
1271 uint maxy, maxx;
1272 ui_get_screen_size(&maxy, &maxx);
1273 if (msg0 != nullptr)
1274 msg0_l = strlen(msg0);
1275 if (msg1 != nullptr)
1276 msg1_l = strlen(msg1);
1277 if (msg2 != nullptr)
1278 msg2_l = strlen(msg2);
1279 if (msg3 != nullptr)
1280 msg3_l = strlen(msg3);
1281 msg_l = max(msg0_l, msg1_l);
1282 msg_l = max(msg_l, msg2_l);
1283 msg_l = max(msg_l, msg3_l);
1284 msg_l = max(msg_l, 50);
1285 msg_l = min(msg_l, maxx - 4);
1286
1287 pos = ((maxx - msg_l) - 4) / 2;
1288 line = (maxy - 6) / 2;
1289 strnz__cpy(title, _("Notification"), MAXLEN - 1);
1290 if (ui_tracked_sfc_box(5, msg_l, line, pos, title)) {
1291 ssnprintf(title, MAXLEN - 1, _("ui_tracked_sfc_box(%d, %d, %d, %d, %s) failed"), 5,
1292 msg_l + 2, line, pos, title);
1293 ui_abend(-1, title);
1294 }
1295 UiSurface *sfc = ui_surface[sfc_ptr];
1296 UiChyron *chyron = ui_new_chyron(sfc, WIN);
1297 ui_set_chyron_key(chyron, 1, _("F1 Help"), UIKEY_F01);
1298 ui_set_chyron_key(chyron, 9, _("F9 Cancel"), UIKEY_F09);
1299 ui_set_chyron_key(chyron, 10, _("F10 Continue"), UIKEY_F10);
1300 ui_compile_chyron(chyron);
1301 UiEvent event;
1302 ui_werase(sfc, WIN);
1303 ui_draw_text(sfc, WIN, 0, 1, msg0);
1304 ui_draw_text(sfc, WIN, 1, 1, msg1);
1305 ui_draw_text(sfc, WIN, 2, 1, msg2);
1306 ui_draw_text(sfc, WIN, 3, 1, msg3);
1307 ui_display_chyron(sfc, WIN, chyron, 4, chyron->l + 1);
1308 do {
1309 event.y = event.x = -1;
1310 cmd_key = ui_get_event(sfc, WIN, chyron, &event, -1);
1311 if (cmd_key == UIKEY_F09 || cmd_key == UIKEY_F10 || cmd_key == 'q' || cmd_key == 'Q')
1312 break;
1313 } while (1);
1315 ui_destroy_chyron(chyron);
1316 return (cmd_key);
1317}
1318/** ui_perror
1319 @brief Display a simple error message window or print to stderr
1320 @ingroup error_handling
1321 @param emsg_str Error message string
1322 @return Key code of user command */
1323int ui_perror(char *emsg_str) {
1324 char emsg[MAXLEN];
1325 unsigned in_key;
1326 uint line, pos, cols;
1327 char title[MAXLEN];
1328 char tmp_str[MAXLEN];
1329 bool f_xwgetch = true;
1330 if (emsg_str[0] == '␛' && emsg_str[1] == 'w') {
1331 emsg_str += 2;
1332 f_xwgetch = false;
1333 }
1334 strnz__cpy(emsg, emsg_str, 79);
1336 fprintf(stderr, "\n%s\n", emsg);
1337 return 1;
1338 }
1339 uint maxy, maxx;
1340 ui_get_screen_size(&maxy, &maxx);
1341 cols = strnz(emsg, maxx - 4);
1342 cols = max(cols, 50);
1343 pos = (maxx - cols - 4) / 2;
1344 line = (maxy - 4) / 2;
1345 strnz__cpy(title, _("Notification"), MAXLEN - 1);
1346 if (ui_tracked_sfc_box(2, cols, line, pos, title)) {
1347 ssnprintf(tmp_str, MAXLEN - 1, _("ui_tracked_sfc_box(%d, %d, %d, %d, %s) failed"),
1348 4, line, line, pos, title);
1349 ui_log(ERROR, "%s", tmp_str);
1350 exit(EXIT_FAILURE);
1351 }
1352 UiSurface *sfc = ui_surface[sfc_ptr];
1353 UiChyron *chyron = ui_new_chyron(sfc, WIN);
1354 ui_set_chyron_key(chyron, 1, _("F1 Help"), UIKEY_F01);
1355 ui_set_chyron_key(chyron, 9, _("F9 Cancel"), UIKEY_F09);
1356 ui_set_chyron_key(chyron, 10, _("F10 Continue"), UIKEY_F10);
1357 ui_compile_chyron(chyron);
1358 UiEvent event;
1359 ui_bkgd(sfc, WIN, &cell_nt);
1360 ui_werase(sfc, WIN);
1361 ui_draw_text(sfc, WIN, 0, 1, emsg_str);
1362 ui_display_chyron(sfc, WIN, chyron, 1, chyron->l + 1);
1363 if (f_xwgetch) {
1364 event.y = event.x = -1;
1365 in_key = ui_get_event(sfc, WIN, chyron, &event, -1);
1366 } else {
1367 in_key = UIKEY_F10;
1368 }
1369 ui_destroy_chyron(chyron);
1371 return (in_key);
1372}
1373/** ui_action_disposition
1374 @brief Display a simple action disposition message window or print to stderr
1375 @ingroup error_handling
1376 @param title Window title
1377 @param action_str Action description string
1378 @return true if successful */
1379bool ui_action_disposition(char *title, char *action_str) {
1380 uint len;
1381 uint line, col;
1382
1384 fprintf(stderr, "\n%s\n", title);
1385 fprintf(stderr, "%s\n", action_str);
1386 return true;
1387 }
1388 len = max(strlen(title), strlen(action_str));
1389 uint maxy, maxx;
1390 ui_get_screen_size(&maxy, &maxx);
1391 col = (maxx - len - 4) / 2;
1392 line = (maxy - 4) / 2;
1393 if (ui_tracked_sfc_box(2, len, line, col, title)) {
1394 ssnprintf(em0, MAXLEN - 1, _("ui_tracked_sfc_box(%d, %d, %d, %d, %s) failed"), 4,
1395 line, line, col, title);
1397 }
1398 UiSurface *sfc = ui_surface[sfc_ptr];
1399 UiChyron *chyron = ui_new_chyron(sfc, WIN);
1400 ui_set_chyron_key(chyron, 10, _("F10 Continue"), UIKEY_F10);
1401 ui_compile_chyron(chyron);
1402 UiEvent event;
1403 ui_draw_text(sfc, WIN, 0, 1, action_str);
1404 ui_display_chyron(sfc, WIN, chyron, 1, 0);
1405 event.y = event.x = -1;
1406 cmd_key = ui_get_event(sfc, WIN, chyron, &event, -1);
1408 ui_destroy_chyron(chyron);
1409 return true;
1410}
1411// -----------------------------------------------------------------------
1412// Logging
1413// -----------------------------------------------------------------------
1414/** ui_iso8601_timestamp
1415 @brief Generate an ISO 8601 timestamp string
1416 @ingroup logging
1417 @param buf Buffer to store the timestamp string
1418 @param n Size of the buffer
1419 @param local If true, use local time; if false, use UTC
1420 @return Pointer to the buffer containing the timestamp string */
1421char *ui_iso8601_timestamp(char *buf, size_t n, bool local) {
1422 if (buf == NULL)
1423 return NULL;
1424 time_t t = time(NULL);
1425 struct tm *tp = local ? localtime(&t) : gmtime(&t);
1426 if (local) {
1427 strftime(buf, n, _("%Y-%m-%dT%H:%M:%S%z"), tp);
1428 } else {
1429 strftime(buf, n, _("%Y-%m-%dT%H:%M:%SZ"), tp);
1430 }
1431 return buf;
1432}
1433#define AS_STRING(NAME) #NAME,
1434const char *subsfc_s[] = {
1435 SUB_SURFACE_LIST(AS_STRING)};
1436const char *ui_sub_surface_str(ss_t w) {
1437 if (w < BOX || w >= SUB_SFC_MAX)
1438 return _("unknown");
1439 return subsfc_s[w];
1440}
1441
1442/** ui_open_log
1443 @brief Open the log file for writing
1444 @ingroup logging
1445 @return FILE pointer to the opened log file */
1447 if (ui_log_ctx.fd == -1) {
1448 char *user = getenv("USER");
1449 time_t t = time(NULL);
1450 struct tm tp;
1451 localtime_r(&t, &tp);
1452 char time_s[16];
1453 strftime(time_s, 100, "%Y%m%d%H%M%S", &tp);
1454 ssnprintf(ui_log_ctx.file_spec, MAXLEN - 1, "/tmp/%s_ui_%s.log", user, time_s);
1455 ui_log_ctx.fd = open(ui_log_ctx.file_spec, O_WRONLY | O_CREAT | O_APPEND, 0644);
1456 if (ui_log_ctx.fd == -1) {
1457 ssnprintf(em0, MAXLEN - 1, _("Failed to open log file: %s"), ui_log_ctx.file_spec);
1458 ssnprintf(em1, MAXLEN - 1, _("Error: %s"), strerror(errno));
1459 ssnprintf(em2, MAXLEN - 1, "%s: line: %d", __FILE__, __LINE__ - 4);
1460 ssnprintf(em3, MAXLEN - 1, _("remedy: delete %s and try again"), ui_log_ctx.file_spec);
1461 fprintf(stderr, "%s\n%s\n%s\n%s\n", em0, em1, em2, em3);
1462 exit(EXIT_FAILURE);
1463 }
1464 }
1465 char ttyname[MAXLEN];
1466 char cmenu_user[MAXLEN];
1467 char *p = getenv(_("USER"));
1468 strnz__cpy(cmenu_user, p, MAXLEN - 1);
1469 if (ttyname_r(STDERR_FILENO, ttyname, sizeof(ttyname)) == 0)
1470 strnz__cpy(em0, ttyname, MAXLEN - 1);
1471 ssnprintf(em0, MAXLEN - 1, _("Ui_Log started by user '%s' on terminal '%s'"), cmenu_user, ttyname);
1472 ui_log(INFO, "%s:", em0);
1473 return ui_log_ctx.fd;
1474}
1475
1476void ui_log_write(const UiLogLevel level, const char *file, const char *func, const int line, const char *fmt, ...) {
1477 UiLogLevel safe_level = (level >= LOG_LEVEL_COUNT) ? INFO : level;
1478 struct stat st;
1479 if (ui_log_ctx.fd == -1 || (stat(ui_log_ctx.file_spec, &st) == 0 && st.st_size >= MAX_LOG_SIZE)) {
1480 if (ui_log_ctx.fd != -1) {
1481 close(ui_log_ctx.fd);
1482 ui_log_ctx.fd = -1;
1483 }
1484 char old_archive[MAXLEN];
1486 strnz__cat(old_archive, ".closed", MAXLEN - 1);
1487 rename(ui_log_ctx.file_spec, old_archive);
1489 }
1490 char log_buffer[MAX_LOG_REC_LEN];
1491 int offset = 0;
1492 int written = snprintf(log_buffer + offset, sizeof(log_buffer) - offset, "[%s]%s[%s]%s<%s:%s:%d> ",
1494 ui_log_ctx.color[safe_level],
1495 ui_log_ctx.level[safe_level],
1496 ANSI_RESET,
1497 file, func, line);
1498 if (written > 0)
1499 offset += written;
1500 va_list args;
1501 va_start(args, fmt);
1502 written = vsnprintf(log_buffer + offset, sizeof(log_buffer) - offset, fmt, args);
1503 va_end(args);
1504 if (written > 0)
1505 offset += written;
1506 if (offset < (int)sizeof(log_buffer) - 2) {
1507 log_buffer[offset++] = '\n';
1508 log_buffer[offset] = '\0';
1509 } else {
1510 log_buffer[sizeof(log_buffer) - 2] = '\n';
1511 log_buffer[sizeof(log_buffer) - 1] = '\0';
1512 offset = sizeof(log_buffer) - 1;
1513 }
1514 // Atomic if log_buffer <= 4096 bytes
1515 if (write(ui_log_ctx.fd, log_buffer, offset) == -1) {
1516 perror("Failed to write atomic log record");
1517 }
1518}
1519// -------------------------------------------------------------------------
1520// Formatting
1521// -------------------------------------------------------------------------
1522/** parse_ansi
1523 @brief Parse ANSI escape sequences for color and attributes
1524 @ingroup formatting
1525 @param ansi_str ANSI escape sequence string
1526 @param attr Pointer to attribute variable to be updated
1527 @param cpx Pointer to color pair index to be updated
1528 @details This function parses an ANSI escape sequence string (e.g., _("\033[31;1m"))
1529 and updates the provided attribute and color pair index accordingly. It handles
1530 standard colors, extended colors, and text attributes like bold, underline, etc.
1531 */
1532void ui_parse_ansi(char *ansi_str, attr_t *attrs, uint *cpx) {
1533 char *tok;
1534 char t0, t1;
1535 char tstr[3];
1536 uint len, x_idx;
1537 uint fg, bg;
1538 uint fg_clr, bg_clr;
1539 char *ansi_p = ansi_str + 2;
1540 ui_pair_content(*cpx, &fg_clr, &bg_clr);
1541 bool f_rgb = false;
1542 fg = fg_clr;
1543 bg = bg_clr;
1544 RGB rgb;
1545 tok = strtok((char *)ansi_p, ";m");
1546 bool a_toi_error = false;
1547 while (1) {
1548 if (tok == nullptr || *tok == '\0')
1549 break;
1550 len = strlen(tok);
1551 if (len == 2) {
1552 t0 = tok[0];
1553 t1 = tok[1];
1554 if (t0 == '3' || t0 == '4') {
1555 if (t1 == '8') {
1556 tok = strtok(nullptr, ";m");
1557 if (tok != nullptr) {
1558 if (*tok == '5') {
1559 tok = strtok(nullptr, ";m");
1560 if (tok != nullptr) {
1561 x_idx = a_toi(tok, &a_toi_error);
1562 rgb = ui_xterm256_idx_to_rgb(x_idx);
1563 f_rgb = true;
1564 }
1565 } else if (*tok == '2') {
1566 tok = strtok(nullptr, ";m");
1567 if (tok != nullptr) {
1568 rgb.r = a_toi(tok, &a_toi_error);
1569 tok = strtok(nullptr, ";m");
1570 if (tok != nullptr) {
1571 rgb.g = a_toi(tok, &a_toi_error);
1572 tok = strtok(nullptr, ";m");
1573 if (tok != nullptr) {
1574 rgb.b = a_toi(tok, &a_toi_error);
1575 f_rgb = true;
1576 }
1577 }
1578 }
1579 }
1580 }
1581 if (f_rgb) {
1582 if (t0 == '3')
1583 fg_clr = ui_color_from_rgb(&rgb);
1584 else if (t0 == '4')
1585 bg_clr = ui_color_from_rgb(&rgb);
1586 }
1587 } else if (t1 == '9') {
1588 if (t0 == '3')
1589 fg_clr = CLR_NT_FG;
1590 else if (t0 == '4')
1591 bg_clr = CLR_NT_BG;
1592 } else if (t1 >= '0' && t1 <= '7') {
1593 if (t0 == '3') {
1594 tstr[0] = t1;
1595 tstr[1] = '\0';
1596 x_idx = a_toi(tstr, &a_toi_error);
1597 rgb = ui_xterm256_idx_to_rgb(x_idx);
1598 fg_clr = ui_color_from_rgb(&rgb);
1599 } else if (t0 == '4') {
1600 tstr[0] = t1;
1601 tstr[1] = '\0';
1602 x_idx = a_toi(tstr, &a_toi_error);
1603 rgb = ui_xterm256_idx_to_rgb(x_idx);
1604 bg_clr = ui_color_from_rgb(&rgb);
1605 }
1606 }
1607 } else if (t0 == '0') {
1608 *tok = t1;
1609 len = 1;
1610 }
1611 }
1612 if (len == 1) {
1613 if (*tok == '0') {
1614 *attrs = WA_NORMAL;
1615 fg_clr = CLR_NT_FG;
1616 bg_clr = CLR_NT_BG;
1617 } else {
1618 switch (a_toi(tok, &a_toi_error)) {
1619 case 1:
1620 *attrs |= WA_BOLD;
1621 break;
1622 case 2:
1623 *attrs |= WA_DIM;
1624 break;
1625 case 3:
1626 *attrs |= WA_ITALIC;
1627 break;
1628 case 4:
1629 *attrs |= WA_UNDERLINE;
1630 break;
1631 case 5:
1632 *attrs |= WA_BLINK;
1633 break;
1634 case 7:
1635 *attrs |= WA_REVERSE;
1636 break;
1637 case 8:
1638 *attrs |= WA_INVIS;
1639 break;
1640 default:
1641 break;
1642 }
1643 }
1644 } else if (len == 0) {
1645 *attrs = WA_NORMAL;
1646 fg_clr = CLR_NT_FG;
1647 bg_clr = CLR_NT_BG;
1648 }
1649 tok = strtok(nullptr, ";m");
1650 }
1651 if (!a_toi_error && (fg_clr != fg || bg_clr != bg))
1652 *cpx = ui_add_pair(fg_clr, bg_clr);
1653 return;
1654}
int stdout_fd
Definition ui_common.c:78
char fn[MAXLEN]
Definition ui_common.c:45
uint src_line
Definition ui_common.c:43
int exit_code
Definition ui_common.c:31
uint16_t win_attr
Definition ui_common.c:34
int stdin_fd
Definition ui_common.c:78
char * src_name
Definition ui_common.c:44
int stderr_fd
Definition lf.c:62
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
#define bw_ho
Definition ui_backend.h:627
#define SUB_SURFACE_LIST(X)
Definition ui_backend.h:74
void ui_render()
Render the UI by updating panels and refreshing the display.
Definition ui_ncurses.c:931
int ui_draw_text(UiSurface *s, ss_t w, uint y, uint x, const char *text)
on specified window, move to y, x, add string
@ LOG_LEVEL_COUNT
Definition ui_backend.h:232
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
#define MAX_EXT_FDS
Definition ui_backend.h:157
#define bw_tr
Definition ui_backend.h:635
#define CHYRON_KEYS
Definition ui_backend.h:92
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 bw_lt
Definition ui_backend.h:629
void ui_surface_destroy(UiSurface *s)
Destroy a UiSurface, cleaning up its windows and panels.
Definition ui_ncurses.c:499
@ SUB_SFC_MAX
Definition ui_backend.h:86
#define bw_br
Definition ui_backend.h:637
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
int ui_getmaxy(UiSurface *s, ss_t w)
Get the maximum number of lines (rows) of a UiSurface.
Definition ui_ncurses.c:733
#define bw_bl
Definition ui_backend.h:636
#define CHYRON_KEY_MAXLEN
Definition ui_backend.h:91
void ui_get_screen_size(uint *lines, uint *cols)
Get the current screen size (number of lines and columns).
Definition ui_ncurses.c:754
void ui_shutdown()
Shutdown the UI runtime, cleaning up resources and freeing memory.
Definition ui_ncurses.c:449
@ CLR_CMDLN_BG
Definition ui_backend.h:582
@ CLR_FILL_CHAR_BG
Definition ui_backend.h:578
@ CLR_BOX_BG
Definition ui_backend.h:572
@ CLR_FILL_CHAR_FG
Definition ui_backend.h:577
@ CLR_FG
Definition ui_backend.h:569
@ CLR_RED
Definition ui_backend.h:553
@ CLR_NT_FG
Definition ui_backend.h:583
@ CLR_BRACKETS_FG
Definition ui_backend.h:575
@ CLR_YELLOW
Definition ui_backend.h:555
@ CLR_IND_FG
Definition ui_backend.h:573
@ CLR_BCYAN
Definition ui_backend.h:566
@ CLR_TITLE_FG
Definition ui_backend.h:591
@ CLR_WHITE
Definition ui_backend.h:559
@ CLR_MAGENTA
Definition ui_backend.h:557
@ CLR_BRACKETS_BG
Definition ui_backend.h:576
@ CLR_NT_REV_BG
Definition ui_backend.h:586
@ CLR_BLACK
Definition ui_backend.h:552
@ CLR_BWHITE
Definition ui_backend.h:567
@ CLR_LN_BG
Definition ui_backend.h:580
@ CLR_NT_HL_REV_BG
Definition ui_backend.h:590
@ CLR_BBLACK
Definition ui_backend.h:560
@ CLR_LN_FG
Definition ui_backend.h:579
@ CLR_NT_HL_FG
Definition ui_backend.h:587
@ CLR_BBLUE
Definition ui_backend.h:564
@ CLR_BGREEN
Definition ui_backend.h:562
@ CLR_BYELLOW
Definition ui_backend.h:563
@ CLR_BMAGENTA
Definition ui_backend.h:565
@ CLR_BORANGE
Definition ui_backend.h:568
@ CLR_NT_HL_BG
Definition ui_backend.h:588
@ CLR_NT_HL_REV_FG
Definition ui_backend.h:589
@ CLR_BG
Definition ui_backend.h:570
@ CLR_BOX_FG
Definition ui_backend.h:571
@ CLR_NT_BG
Definition ui_backend.h:584
@ CLR_TITLE_BG
Definition ui_backend.h:592
@ CLR_BRED
Definition ui_backend.h:561
@ CLR_NCOLORS
Definition ui_backend.h:593
@ CLR_BLUE
Definition ui_backend.h:556
@ CLR_IND_BG
Definition ui_backend.h:574
@ CLR_GREEN
Definition ui_backend.h:554
@ CLR_CMDLN_FG
Definition ui_backend.h:581
@ CLR_NT_REV_FG
Definition ui_backend.h:585
@ CLR_CYAN
Definition ui_backend.h:558
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
#define bw_tt
Definition ui_backend.h:631
#define bw_cr
Definition ui_backend.h:633
#define bw_bt
Definition ui_backend.h:632
int ui_getmaxx(UiSurface *s, ss_t w)
Get the maximum number of columns of a UiSurface.
Definition ui_ncurses.c:744
#define bw_ve
Definition ui_backend.h:628
int ui_wclrtoeol(UiSurface *s, ss_t w)
Clear to the end of the line in a surface window.
#define bw_rt
Definition ui_backend.h:630
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
#define SFC_MAX
Definition ui_backend.h:65
#define bw_tl
Definition ui_backend.h:634
#define UIKEY_STREAM_DATA
#define UIKEY_F09
#define UIKEY_F01
#define UIKEY_F10
#define WA_NORMAL
#define WA_BLINK
#define WA_ITALIC
#define WA_DIM
#define WA_INVIS
#define WA_BOLD
#define WA_REVERSE
#define WA_UNDERLINE
int ui_get_event(UiSurface *s, ss_t w, UiChyron *chyron, UiEvent *ev, int timeout_ms)
Read input events.
const wchar_t * border_none
Definition ui_common.c:89
uint16_t box_attr
Definition ui_common.c:35
const wchar_t * bw_sp
Definition ui_common.c:96
unsigned int cmd_key
Definition ui_common.c:32
UiCell cell_sp
Definition ui_common.c:149
void ui_unset_chyron_key(UiChyron *chyron, uint k)
Unset chyron key.
Definition ui_common.c:793
uint m_begx
Definition ui_common.c:39
void ui_set_chyron_key(UiChyron *chyron, uint k, char *s, uint kc)
Set chyron key with default color pair (cp_nt_rev).
Definition ui_common.c:778
int ui_border_draw(UiSurface *sfc, ss_t w)
Definition ui_common.c:523
UiCell cell_blue_bg
Definition ui_common.c:154
UiCell cell_ve
Definition ui_common.c:141
ushort cp_nt
Definition ui_common.c:167
void ui_deactivate_all_chyron_keys(UiChyron *chyron)
Deactivate all chyron keys.
Definition ui_common.c:829
UiCell cell_tr
Definition ui_common.c:139
UiCell cell_nt
Definition ui_common.c:123
ushort cp_magenta
Definition ui_common.c:181
char const colors_text[][10]
Color names for .minitrc overrides.
Definition ui_common.c:110
int ui_tracked_sfc_split_box(uint wlines, uint wcols, uint split_y, uint split_x, uint wbegy, uint wbegx, const char *wtitle)
Create a new surface with a split box window.
Definition ui_common.c:467
const wchar_t * bw_ran
Definition ui_common.c:101
UiCell cell_ls
Definition ui_common.c:134
UiCell cell_nt_hl_rev
Definition ui_common.c:126
bool f_ncurses_open
Definition ui_common.c:50
UiCell cell_cyan_bg
Definition ui_common.c:155
UiCell cell_bt
Definition ui_common.c:147
UiCell cell_br
Definition ui_common.c:143
bool f_sigwench
Definition ui_common.c:33
const wchar_t * bw_rbr
Definition ui_common.c:95
ushort cp_green
Definition ui_common.c:177
UiCell cell_magenta_bg
Definition ui_common.c:156
const wchar_t * bw_ua
Definition ui_common.c:99
UiCell cell_tl
Definition ui_common.c:138
ushort cp_red
Definition ui_common.c:176
ushort cp_white
Definition ui_common.c:182
wchar_t * ui_mbstr_to_wcstr(const char *mb_str)
Convert a multibyte string to a wide character string.
Definition ui_common.c:364
ushort cp_box
Definition ui_common.c:163
UiChyron * ui_new_chyron(UiSurface *sfc, ss_t w)
Create new chyron structure.
Definition ui_common.c:662
ushort cp_blue
Definition ui_common.c:179
UiCell cell_ln
Definition ui_common.c:131
ui_context_t ui_ctx
Definition ui_common.c:76
uint m_lines
Definition ui_common.c:36
const char * ui_sub_surface_str(ss_t w)
Definition ui_common.c:1436
const wchar_t * bw_chk
Definition ui_common.c:103
UiLogContext ui_log_ctx
Definition ui_common.c:53
BorderWide bw
Definition ui_common.c:27
char * ui_iso8601_timestamp(char *buf, size_t n, bool local)
Generate an ISO 8601 timestamp string.
Definition ui_common.c:1421
ushort cp_black
Definition ui_common.c:175
UiCell cell_ts
Definition ui_common.c:136
RGB ui_xterm256_idx_to_rgb(uint idx)
Convert XTerm 256 color index to RGB.
Definition ui_common.c:969
double BLUE_GAMMA
Definition ui_common.c:82
void ui_activate_chyron_key(UiChyron *chyron, uint k)
Activate chyron key.
Definition ui_common.c:802
const wchar_t * bw_rtl
Definition ui_common.c:92
ushort cp_cyan
Definition ui_common.c:180
ushort cp_nt_hl
Definition ui_common.c:169
int ui_cm_surface_destroy(UiSurface *sfc)
Destroy the most recently created surface.
Definition ui_common.c:515
UiCell cell_yellow_bg
Definition ui_common.c:153
const wchar_t * bw_rtr
Definition ui_common.c:93
const wchar_t * bw_lan
Definition ui_common.c:102
ushort cp_cmdln
Definition ui_common.c:165
ushort cp_fill_char
Definition ui_common.c:173
UiCell cell_tt
Definition ui_common.c:146
void ui_display_chyron(UiSurface *sfc, ss_t w, UiChyron *chyron, uint line, uint col)
Display chyron on the specified line and column of the surface.
Definition ui_common.c:898
UiCell cell_bs
Definition ui_common.c:137
ushort cp_brackets
Definition ui_common.c:174
const wchar_t * bw_ra
Definition ui_common.c:97
ushort cp_yellow
Definition ui_common.c:178
STDRGB std_color[]
Definition ui_common.c:188
UiCell cell_ho
Definition ui_common.c:140
const wchar_t * border_double
Definition ui_common.c:86
uint mouse_support
Definition ui_common.c:40
UiCell cell_red_bg
Definition ui_common.c:151
UiCell cell_rs
Definition ui_common.c:135
void ui_set_chyron_key_cb(UiChyron *chyron, uint k, char *s, uint kc, UiCell cell_base)
Set chyron key with color pair (cp).
Definition ui_common.c:754
int ui_assign_chyron_win(UiChyron *chyron, UiSurface *sfc, ss_t w, char *y)
Assign surface and window to chyron structure.
Definition ui_common.c:695
const wchar_t * bw_h09
Definition ui_common.c:104
bool ui_init_clr_palette(SIO *sio)
Definition ui_common.c:289
UiCell cell_default
Definition ui_common.c:119
uint m_cols
Definition ui_common.c:37
bool ui_is_set_chyron_key(UiChyron *chyron, uint k)
Check if function key label is set.
Definition ui_common.c:738
int click_x
Definition ui_common.c:30
int ui_border_ysplit_text(UiSurface *sfc, char *text_in, uint separator_line)
Draw a box with a separator line and text around the specified window.
Definition ui_common.c:579
const wchar_t * border_single
Definition ui_common.c:85
ushort cp_nt_hl_rev
Definition ui_common.c:170
ushort cp_default
Definition ui_common.c:172
UiCell cell_nt_hl
Definition ui_common.c:125
ushort cp_nt_rev
Definition ui_common.c:168
ushort cp_ln
Definition ui_common.c:171
ushort cp_title
Definition ui_common.c:166
void ui_parse_ansi(char *ansi_str, attr_t *attrs, uint *cpx)
Parse ANSI escape sequences for color and attributes.
Definition ui_common.c:1532
void ui_abend(int ec, char *s)
Convert multibyte string to complex character array.
Definition ui_common.c:404
UiCell cell_lt
Definition ui_common.c:144
const char * subsfc_s[]
Definition ui_common.c:1434
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
int ui_border_ysplit(UiSurface *sfc, ss_t w, uint y)
Draw a box with a separator line around the specified window.
Definition ui_common.c:556
UiCell cell_brktl
Definition ui_common.c:121
UiCell cell_cr
Definition ui_common.c:148
UiCell cell_title
Definition ui_common.c:130
int click_y
Definition ui_common.c:29
const wchar_t * bw_la
Definition ui_common.c:98
UiCell cell_ind
Definition ui_common.c:128
int ui_tracked_sfc_box(uint wlines, uint wcols, uint wbegy, uint wbegx, const char *wtitle)
Create a new surface with a box window.
Definition ui_common.c:429
UiChyron * ui_destroy_chyron(UiChyron *chyron)
Destroy UiChyron structure.
Definition ui_common.c:717
uint m_begy
Definition ui_common.c:38
int ui_get_chyron_key(UiChyron *chyron, uint x)
Get keycode from chyron.
Definition ui_common.c:922
double GRAY_GAMMA
Definition ui_common.c:79
void ui_mbc_to_wc(wchar_t wc[2], const char mbc)
Convert a multibyte character to a wide character.
Definition ui_common.c:344
UiCell cell_ran
Definition ui_common.c:132
double RED_GAMMA
Definition ui_common.c:80
UiCell cell_black_bg
Definition ui_common.c:150
void ui_deactivate_chyron_key(UiChyron *chyron, uint k)
Deactivate chyron key.
Definition ui_common.c:821
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
UiCell cell_white_bg
Definition ui_common.c:157
void ui_apply_gamma(RGB *rgb)
Apply gamma correction to RGB color.
Definition ui_common.c:1002
UiCell cell_cmdln
Definition ui_common.c:129
void ui_activate_all_chyron_keys(UiChyron *chyron)
Deactivate chyron key.
Definition ui_common.c:811
const wchar_t * bw_da
Definition ui_common.c:100
UiCell cell_brktr
Definition ui_common.c:122
ushort cp_ind
Definition ui_common.c:164
UiCell cell_green_bg
Definition ui_common.c:152
uint ui_rgb_to_xterm256_idx(RGB *rgb)
Convert RGB color to XTerm 256 color index.
Definition ui_common.c:947
double GREEN_GAMMA
Definition ui_common.c:81
void ui_compile_chyron(UiChyron *chyron)
construct the chyron string from the chyron structure
Definition ui_common.c:843
UiCell cell_fill_char
Definition ui_common.c:120
UiCell cell_box
Definition ui_common.c:127
UiCell cell_rt
Definition ui_common.c:145
int ui_open_log()
Open the log file for writing.
Definition ui_common.c:1446
void ui_log_write(const UiLogLevel level, const char *file, const char *func, const int line, const char *fmt,...)
Definition ui_common.c:1476
const wchar_t * bw_rbl
Definition ui_common.c:94
UiCell cell_bl
Definition ui_common.c:142
UiCell cell_chk
Definition ui_common.c:133
UiCell cell_nt_rev
Definition ui_common.c:124
int ui_pair_content(uint pair, uint *fg, uint *bg)
Get the foreground and background colors of a specified color pair.
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
UiRuntime * ui
Definition ui_ncurses.c:39
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_color_cnt
Definition ui_ncurses.c:43
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.
int ui_mvwadd_wchnstr(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cmplx_buf, uint n)
Draw a complex character string at a specific position in a surface window, with a maximum length.
int ui_mvwaddnwstr(UiSurface *s, ss_t w, uint y, uint x, const wchar_t *wstr, int m)
Draw a wide-character string at a specific position in a surface window, with a maximum length.
int ui_mvwadd_wchstr(UiSurface *s, ss_t w, uint y, uint x, const UiCell *cmplx_buf)
Draw a complex character string at a specific position in a surface window.
#define MAX_LOG_REC_LEN
Definition lf.h:50
#define MAX_LOG_SIZE
Definition lf.h:51
#define min(x, y)
Definition lf.h:158
#define MAXLEN
Definition lf.h:38
#define ANSI_RESET
Definition lf.h:86
#define max(a, b)
Definition lf.h:152
int pipe_in
Definition lf.c:62
char em1[MAXLEN]
Definition lf.c:43
int tty_fd
Definition lf.c:62
char em2[MAXLEN]
Definition lf.c:44
char em3[MAXLEN]
Definition lf.c:45
char em0[MAXLEN]
Definition lf.c:42
int pipe_out
Definition lf.c:62
#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 strnz(char *, size_t)
terminates string at New Line, Carriage Return, or max_len
Definition futil.c:621
size_t ssnprintf(char *, size_t, const char *,...)
Custom safe string formatting utility (Variadic version).
Definition futil.c:398
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition lf.c:2330
int a_toi(char *, bool *)
a safer alternative to atoi() for converting ASCII strings to integers.
Definition futil.c:806
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_perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition ui_common.c:1323
void ui_unregister_read_fd(int fd)
Unregister an external file descriptor from polling.
Definition ui_common.c:1195
void ui_register_read_fd(int fd, int token_id)
Register an external file descriptor for polling.
Definition ui_common.c:1178
int ui_timeout_prompt(char *msg0, char *msg1, uint timeout_ms, uint timeout_secs)
Display a timeout prompt window or print to stderr.
Definition ui_common.c:1094
int ui_update_timeout_prompt(UiSurface *sfc, ss_t w, uint timeout_ms)
Update the timeout prompt with the remaining time.
Definition ui_common.c:1157
int ui_poll_reg_read_fd(int timeout_ms)
Poll for input on registered external files.
Definition ui_common.c:1216
bool ui_action_disposition(char *title, char *action_str)
Display a simple action disposition message window or print to stderr.
Definition ui_common.c:1379
int ui_answer_yn(char *msg0, char *msg1, char *msg2, char *msg3)
Accept a single letter answer.
Definition ui_common.c:1033
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
nfds_t ext_nfds
Definition ui_backend.h:170
ext_fd_reg_t ext_fds[MAX_EXT_FDS]
Definition ui_backend.h:169
char * color[LOG_LEVEL_COUNT]
Definition ui_backend.h:240
char file_spec[MAXLEN]
Definition ui_backend.h:236
char timestamp[32]
Definition ui_backend.h:239
UiLogLevel min_level
Definition ui_backend.h:237
char * level[LOG_LEVEL_COUNT]
Definition ui_backend.h:241
uint32_t ran_bg
Definition ui_backend.h:794
double green_gamma
Definition ui_backend.h:747
uint32_t ln_bg
Definition ui_backend.h:780
double blue_gamma
Definition ui_backend.h:748
char brackets[3]
Definition ui_backend.h:807
uint32_t magenta
Definition ui_backend.h:755
uint32_t cmdln_bg
Definition ui_backend.h:782
uint32_t cmdln_fg
Definition ui_backend.h:781
uint32_t nt_hl_rev_fg
Definition ui_backend.h:789
uint32_t ran_fg
Definition ui_backend.h:793
uint32_t bgreen
Definition ui_backend.h:761
double red_gamma
Definition ui_backend.h:746
uint32_t green
Definition ui_backend.h:752
uint32_t nt_fg
Definition ui_backend.h:783
uint32_t yellow
Definition ui_backend.h:753
uint32_t fill_char_fg
Definition ui_backend.h:777
uint32_t box_fg
Definition ui_backend.h:771
uint32_t ln_fg
Definition ui_backend.h:779
uint32_t ind_bg
Definition ui_backend.h:774
uint32_t bmagenta
Definition ui_backend.h:764
uint32_t ind_fg
Definition ui_backend.h:773
uint32_t byellow
Definition ui_backend.h:762
uint32_t bblue
Definition ui_backend.h:763
uint32_t borange
Definition ui_backend.h:767
uint32_t title_bg
Definition ui_backend.h:792
uint32_t fg
Definition ui_backend.h:769
uint32_t cyan
Definition ui_backend.h:756
uint32_t bblack
Definition ui_backend.h:759
uint32_t red
Definition ui_backend.h:751
uint32_t nt_hl_rev_bg
Definition ui_backend.h:790
uint32_t title_fg
Definition ui_backend.h:791
uint32_t bred
Definition ui_backend.h:760
uint32_t fill_char_bg
Definition ui_backend.h:778
uint32_t black
Definition ui_backend.h:750
uint32_t nt_rev_bg
Definition ui_backend.h:786
uint32_t brackets_bg
Definition ui_backend.h:776
uint32_t bcyan
Definition ui_backend.h:765
uint32_t nt_bg
Definition ui_backend.h:784
uint32_t blue
Definition ui_backend.h:754
uint32_t brackets_fg
Definition ui_backend.h:775
uint32_t white
Definition ui_backend.h:757
uint32_t box_bg
Definition ui_backend.h:772
uint32_t nt_hl_fg
Definition ui_backend.h:787
uint32_t bwhite
Definition ui_backend.h:766
uint32_t nt_rev_fg
Definition ui_backend.h:785
double gray_gamma
Definition ui_backend.h:749
uint32_t nt_hl_bg
Definition ui_backend.h:788
uint32_t bg
Definition ui_backend.h:770