C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses.c File Reference

NCurses UI backend — lifecycle, surface management, and capabilities. More...

#include "cm.h"
#include "ui_ncurses_internal.h"
#include <locale.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Include dependency graph for ui_ncurses.c:

Go to the source code of this file.

Macros

#define _XOPEN_SOURCE_EXTENDED   1

Functions

void fast_exit (UiSurface *s)
 Exit the UI application quickly, cleaning up resources.
uint ui__mbstr_to_cellstr (UiCell *cmplx_buf, const char *str, const UiCell *cell_base, uint *p, const uint atmost)
uint ui_add_pair (uint fg, uint bg)
 Add a new color pair to the UI, or retrieve the index of an existing pair.
int ui_bkgd (UiSurface *s, ss_t w, const UiCell *cell)
 Set the background of a specific window within a UiSurface.
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.
int ui_bkgrnd (UiSurface *s, ss_t w, const UiCell *cell)
 Set the background of a specific window within a UiSurface.
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.
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.
int ui_chg_color (uint16_t color_idx, uint32_t *color)
 Change the RGB color value for a given color index.
int ui_chg_pair (uint pair, uint fg, uint bg)
 Change the foreground and background colors of an existing color pair.
int ui_clear ()
 Clear the entire screen.
int ui_color_content (uint color, uint *r, uint *g, uint *b)
int ui_color_from_rgb (RGB *rgb)
 Convert RGB to curses color index, adding new color if necessary.
int ui_curs_set (int visibility)
 Set the visibility of the cursor.
int ui_cursor_enable (UiSurface *s, ss_t w, bool visible)
 Enable or disable the cursor for a UiSurface.
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.
int ui_cursor_move (UiSurface *s, ss_t w, uint y, uint x)
 Move the cursor to a specified position within a UiSurface.
void ui_def_prog_mode ()
void ui_destroy_curses ()
int ui_doupdate ()
 Refresh the display by updating the screen.
void ui_endwin ()
 End the UI application, cleaning up resources and shutting down the UI runtime.
int ui_erase ()
 Erase the entire screen.
UiBackend ui_get_backend ()
 Get the UI backend type.
void ui_get_caps (UiCaps *caps)
 Get the UI backend capabilities.
uint32_t ui_get_color (uint16_t color_idx)
 Get the RGB color value for a given color index.
void ui_get_screen_size (uint *lines, uint *cols)
 Get the current screen size (number of lines and columns).
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_getmaxx (UiSurface *s, ss_t w)
 Get the maximum number of columns of a UiSurface.
int ui_getmaxy (UiSurface *s, ss_t w)
 Get the maximum number of lines (rows) of a UiSurface.
void ui_getmaxyx (UiSurface *s, ss_t w, uint *lines, uint *cols)
 Get the maximum dimensions of a UiSurface.
void ui_getyx (UiSurface *s, ss_t w, uint *lines, uint *cols)
 Get the current cursor position within a window of a UI surface.
RGB ui_hex_to_rgb (char *s)
 Convert a hexadecimal color string to an RGB structure.
int ui_idcok (UiSurface *s, ss_t w, bool enable)
 Enable or disable the use of the insert/delete character capabilities for a UiSurface.
int ui_idlok (UiSurface *s, ss_t w, bool enable)
 Enable or disable the use of the insert/delete line capabilities for a UiSurface.
int ui_immedok (UiSurface *s, ss_t w, bool enable)
struct UiRuntime * ui_init (const UiConfig *cfg, SIO *sio)
 Initialize the UI runtime with the given configuration and SIO.
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.
int ui_keypad (UiSurface *s, ss_t w, bool enable)
 Enable or disable the keypad for a UiSurface.
SCREEN * ui_ncurses_get_screen ()
 Get the current SCREEN pointer used by the UI.
PANEL * ui_ncurses_surface_get_panel (const UiSurface *s, ss_t w)
 Get the current PANEL pointer for a specific window within a UiSurface.
WINDOW * ui_ncurses_surface_get_win (const UiSurface *s, ss_t w)
 Get the current FILE pointer for the TTY used by the UI.
int ui_pair_content (uint pair, uint *fg, uint *bg)
 Get the foreground and background colors of a specified color pair.
int ui_pair_from_hex (const char *fg, const char *bg)
 Convert hex color strings to a curses color pair index.
int ui_prefresh (UiSurface *s, ss_t w, int pminrow, int pmincol, int sminrow, int smincol, int smaxrow, int smaxcol)
void ui_render ()
 Render the UI by updating panels and refreshing the display.
int ui_resume ()
 Resume the UI, restoring the previous program state and updating the display.
int ui_scrollok (UiSurface *s, ss_t w, bool enable)
 Enable or disable scrolling for a UiSurface.
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.
int ui_setscrreg (UiSurface *s, ss_t w, uint top, uint bottom)
 Set the scrolling region for a UiSurface.
void ui_shutdown ()
 Shutdown the UI runtime, cleaning up resources and freeing memory.
int ui_surface_addpad (UiSurface *s, ss_t pad, ss_t p, uint lines, uint cols, uint begy, uint begx, bool hidden)
 Add a new pad to the specified UiSurface.
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.
UiSurface * ui_surface_box (UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x, const char *wtitle)
 Create a new UI surface with a border box and optional title.
void ui_surface_destroy (UiSurface *s)
 Destroy a UiSurface, cleaning up its windows and panels.
int ui_surface_hide (UiSurface *s, ss_t w)
 Hide a UiSurface, making it invisible on the screen.
int ui_surface_move (UiSurface *s, ss_t w, uint y, uint x)
 Move a UiSurface to a new position on the screen.
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.
int ui_surface_resize (UiSurface *s, ss_t w, uint lines, uint cols)
 Resize a UiSurface to new dimensions.
int ui_surface_show (UiSurface *s, ss_t w)
 Show a UiSurface, making it visible on the screen.
int ui_suspend ()
 Suspend the UI, saving the current program state and ending the window session.
int ui_top_surface (UiSurface *s, ss_t w)
 Hide a UiSurface, making it invisible on the screen.
void ui_update_panels ()
 Update the panels in the UI.
int ui_wclear (UiSurface *s, ss_t w)
 Clear the contents of a UiSurface or all surfaces.
int ui_werase (UiSurface *s, ss_t w)
 Erase the contents of a UiSurface or all surfaces.
int ui_wmove (UiSurface *s, ss_t w, uint y, uint x)
 Move the cursor to a specified position within a UiSurface.
int ui_wnoutrefresh (UiSurface *s, ss_t w)
 Refresh a specific window within a UiSurface.
int ui_wscrl (UiSurface *s, ss_t w, int n)
 Scroll the contents of a UiSurface by a specified number of lines.

Variables

int sfc_ptr = -1
UiSurface * stdsfc
UiRuntime * ui = NULL
uint ui_color_cnt = 0
uint ui_pair_cnt = 0
UiSurface * ui_surface [UI_SFC_MAX]

Detailed Description

NCurses UI backend — lifecycle, surface management, and capabilities.

Implements all UiRuntime and UiSurface operations declared in ui_backend.h using the NCurses / panelw library.

When compiled as part of the main C-Menu build (UAL_LEGACY_COMPAT defined), the legacy globals screen and tty_fp from dwin.c are kept in sync so that code not yet migrated to the UAL API continues to work.

Definition in file ui_ncurses.c.

Macro Definition Documentation

◆ _XOPEN_SOURCE_EXTENDED

#define _XOPEN_SOURCE_EXTENDED   1

Definition at line 13 of file ui_ncurses.c.

Function Documentation

◆ fast_exit()

void fast_exit ( UiSurface * s)

Exit the UI application quickly, cleaning up resources.

Parameters
sPointer to the UiSurface to be cleaned up before exiting.

Definition at line 351 of file ui_ncurses.c.

351 {
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}
@ SUB_SFC_MAX
Definition ui_backend.h:86

References SUB_SFC_MAX.

◆ ui__mbstr_to_cellstr()

uint ui__mbstr_to_cellstr ( UiCell * cmplx_buf,
const char * str,
const UiCell * cell_base,
uint * p,
const uint atmost )

Definition at line 1270 of file ui_ncurses.c.

1270 {
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}
uint16_t attr_t
Definition ui_backend.h:458
#define ERR
Definition ui_backend.h:468

◆ ui_add_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.

Add a new color pair to the UI color pair palette.

Parameters
fgThe foreground color index for the new color pair.
bgThe background color index for the new color pair.
Returns
The index of the new or existing color pair, or EXIT_FAILURE on failure.
Parameters
fgThe foreground color index for the new color pair.
bgThe background color index for the new color pair.
Returns
The index of the newly added color pair, or EXIT_FAILURE if the maximum number of pairs is exceeded.

This function adds a new color pair to the UI color pair palette. It checks if the specified foreground and background colors already exist in the palette. If they do, it returns the existing index. If not, it adds the new color pair and returns its index. If the maximum number of pairs is exceeded, it displays an error message and returns EXIT_FAILURE.

Definition at line 1061 of file ui_ncurses.c.

1061 {
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);
1074 ui_display_error(em0, em1, em2, nullptr);
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);
1082 ui_display_error(em0, em1, em2, nullptr);
1083 return (EXIT_FAILURE);
1084 }
1085 ui_pair_cnt++;
1086 return (int)(ui_pair_cnt - 1);
1087}
#define UI_PAIRS
Definition ui_backend.h:62
int ui_pair_content(uint pair, uint *fg, uint *bg)
Get the foreground and background colors of a specified color pair.
uint ui_pair_cnt
Definition ui_ncurses.c:44
#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
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

References em0, em1, em2, ssnprintf(), ui_display_error(), ui_pair_cnt, and ui_pair_content().

Referenced by parse_ansi_str(), ui_cell_from_ucp(), ui_get_nccell(), ui_initialize_sio(), ui_pair_from_hex(), and ui_parse_ansi().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ui_bkgd()

int ui_bkgd ( UiSurface * s,
ss_t w,
const UiCell * cell )

Set the background of a specific window within a UiSurface.

Parameters
sPointer to the UiSurface containing the window.
wThe index of the window within the surface for which to set the background.
cellPointer to a UiCell structure representing the desired background attributes.
Returns
0 on success, or -1 on failure.

Definition at line 974 of file ui_ncurses.c.

974 {
975 if (!s)
976 return -1;
977 wbkgrnd(s->mwin[w], cell);
978 return 0;
979}

◆ ui_bkgdset()

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.

Parameters
sPointer to the UiSurface containing the window.
wThe index of the window within the surface for which to set the background for new content.
cellPointer to a UiCell structure representing the desired background attributes for new content.
Returns
0 on success, or -1 on failure.

Definition at line 988 of file ui_ncurses.c.

988 {
989 if (!s)
990 return -1;
991 wbkgrndset(s->mwin[w], cell);
992 return 0;
993}

◆ ui_bkgrnd()

int ui_bkgrnd ( UiSurface * s,
ss_t w,
const UiCell * cell )

Set the background of a specific window within a UiSurface.

Parameters
sPointer to the UiSurface containing the window.
wThe index of the window within the surface for which to set the background.
cellPointer to a UiCell structure representing the desired background attributes.
Returns
0 on success, or -1 on failure.

Definition at line 1002 of file ui_ncurses.c.

1002 {
1003 if (!s->mwin[w])
1004 return -1;
1005 wbkgrnd(s->mwin[w], cell);
1006 return 0;
1007}

◆ ui_bkgrndset()

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.

Parameters
sPointer to the UiSurface containing the window.
wThe index of the window within the surface for which to set the background for new content.
cellPointer to a UiCell structure representing the desired background attributes for new content.
Returns
0 on success, or -1 on failure.

Definition at line 1016 of file ui_ncurses.c.

1016 {
1017 if (!s->mwin[w])
1018 return -1;
1019 wbkgrndset(s->mwin[w], cell);
1020 return 0;
1021}

◆ ui_cell_from_ucp()

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.

Create a UiCell from a Unicode codepoint and specified foreground and background colors.

Parameters
ucpPointer to a wide character string containing the Unicode code point.
fgPointer to a 32-bit integer representing the foreground color in hex format (e.g., 0xRRGGBB).
bgPointer to a 32-bit integer representing the background color in hex format (e.g., 0xRRGGBB).
Returns
A UiCell structure representing the Unicode character with the specified colors.
Parameters
ucpPointer to a wide character string representing the Unicode codepoint.
fgPointer to a 32-bit unsigned integer representing the foreground color.
bgPointer to a 32-bit unsigned integer representing the background color.
Returns
A UiCell structure representing the created cell.

This function creates a UiCell from a Unicode codepoint and specified foreground and background colors. It initializes the cell, sets its width based on the Unicode codepoint, converts the codepoint to UTF-8, and sets the styles and channels for the cell. The resulting UiCell can be used for rendering text in a UI surface.

Definition at line 102 of file ui_ncurses.c.

102 {
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}
#define WA_NORMAL
uint ui_add_pair(uint fg, uint bg)
Add a new color pair to the UI, or retrieve the index of an existing pair.
int ui_color_from_rgb(RGB *rgb)
Convert RGB to curses color index, adding new color if necessary.
uint8_t b
Definition ui_backend.h:349
uint8_t r
Definition ui_backend.h:349
uint8_t g
Definition ui_backend.h:349

References ui_add_pair(), and ui_color_from_rgb().

Referenced by ui_initialize_sio().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ui_chg_color()

int ui_chg_color ( uint16_t color_idx,
uint32_t * color )

Change the RGB color value for a given color index.

Change the color at a specified index in the UI color palette.

Parameters
color_idxThe index of the color in the curses color table to be changed.
colorPointer to a 32-bit unsigned integer representing the new RGB color value (0xRRGGBB).
Returns
0 on success, or -1 on failure.
Parameters
color_idxThe index of the color to change in the UI color palette.
colorPointer to a 32-bit unsigned integer representing the new color value.
Returns
0 on success, -1 on error (if the color index is out of bounds).

This function changes the color at a specified index in the UI color palette. It applies gamma correction to the new color value and updates both the standard and UI color arrays with the new RGB components. If the color index is out of bounds, it returns -1.

Definition at line 1216 of file ui_ncurses.c.

1216 {
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}
#define UI_COLORS
Definition ui_backend.h:61
STDRGB std_color[]
Definition ui_common.c:188
void ui_apply_gamma(RGB *rgb)
Apply gamma correction to RGB color.
Definition ui_common.c:1002

References std_color, and ui_apply_gamma().

Referenced by ui_init_clr_palette().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ui_chg_pair()

int ui_chg_pair ( uint pair,
uint fg,
uint bg )

Change the foreground and background colors of an existing color pair.

Parameters
pairThe index of the color pair to be modified.
fgThe new foreground color index for the color pair.
bgThe new background color index for the color pair.
Returns
0 on success, or -1 on failure.

Definition at line 1095 of file ui_ncurses.c.

1095 {
1096 if (pair + 1 >= UI_PAIRS)
1097 return -1;
1098 init_extended_pair(pair, fg, bg);
1099 return 0;
1100}

◆ ui_clear()

int ui_clear ( )

Clear the entire screen.

Returns
0 on success, or -1 on failure.

Definition at line 631 of file ui_ncurses.c.

631 {
632 clear();
633 return 0;
634}

◆ ui_color_content()

int ui_color_content ( uint color,
uint * r,
uint * g,
uint * b )

Definition at line 1163 of file ui_ncurses.c.

1163 {
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}

Referenced by ui_color_from_rgb(), ui_get_channels_from_pair(), and ui_set_nccell().

Here is the caller graph for this function:

◆ ui_color_from_rgb()

uint ui_color_from_rgb ( RGB * rgb)

Convert RGB to curses color index, adding new color if necessary.

Convert an RGB color to a color index in the UI color palette.

was rgb_to_curses_clr

Parameters
rgbPointer to RGB struct with r, g, b values (0-255).
Returns
Color index in curses color table, or 0 on failure.
Parameters
rgbPointer to an RGB structure representing the color to convert.
Returns
The color index corresponding to the RGB color, or -1 if the color cannot be added.

This function converts an RGB color to a color index in the UI color palette. It applies gamma correction to the RGB values and checks if the color already exists in the palette. If it does, it returns the existing index. If not, it adds the new color to the palette and returns its index. If the palette is full, it returns -1.

Definition at line 1107 of file ui_ncurses.c.

1107 {
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;
1121 ui_apply_gamma(rgb);
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}
uint ui_color_cnt
Definition ui_ncurses.c:43

References em0, em1, ssnprintf(), std_color, ui_apply_gamma(), and ui_color_cnt.

Referenced by parse_ansi_str(), ui_cell_from_ucp(), ui_get_nccell(), ui_pair_from_hex(), and ui_parse_ansi().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ui_curs_set()

int ui_curs_set ( int visibility)

Set the visibility of the cursor.

Parameters
visibilityAn integer value indicating the desired cursor visibility: 0 - Invisible 1 - Visible 2 - Very visible (e.g., block cursor)
Returns
0 on success, or -1 on failure.

Definition at line 881 of file ui_ncurses.c.

881 {
882 curs_set(visibility);
883 return 0;
884}

◆ ui_cursor_enable()

int ui_cursor_enable ( UiSurface * s,
ss_t w,
bool visible )

Enable or disable the cursor for a UiSurface.

Parameters
sPointer to the UiSurface where the cursor will be enabled or disabled.
wThe index of the window within the surface where the cursor will be enabled or disabled.
visibleA boolean value indicating whether to make the cursor visible (true) or invisible (false).
Returns
0 on success, or -1 on failure.

Definition at line 911 of file ui_ncurses.c.

911 {
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}
UiRuntime * ui
Definition ui_ncurses.c:39

◆ ui_cursor_enable_yx()

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.

Parameters
sPointer to the UiSurface where the cursor will be enabled or disabled.
wThe index of the window within the surface where the cursor will be enabled or disabled.
yThe y-coordinate (row) for the cursor's position.
xThe x-coordinate (column) for the cursor's position.
visibleA boolean value indicating whether to make the cursor visible (true) or invisible (false).
Returns
0 on success, or -1 on failure.

Definition at line 896 of file ui_ncurses.c.

896 {
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}

◆ ui_cursor_move()

int ui_cursor_move ( UiSurface * s,
ss_t w,
uint y,
uint x )

Move the cursor to a specified position within a UiSurface.

Parameters
sPointer to the UiSurface where the cursor will be moved.
wThe index of the window within the surface where the cursor will be moved.
yThe new y-coordinate (row) for the cursor's position.
xThe new x-coordinate (column) for the cursor's position.
Returns
0 on success, or -1 on failure.

Definition at line 698 of file ui_ncurses.c.

698 {
699 if (!s || !s->mwin[w])
700 return -1;
701 return wmove(s->mwin[w], y, x);
702}

◆ ui_def_prog_mode()

void ui_def_prog_mode ( )

Definition at line 867 of file ui_ncurses.c.

867 {
868 def_prog_mode();
869}

Referenced by fork_exec().

Here is the caller graph for this function:

◆ ui_destroy_curses()

void ui_destroy_curses ( )

Definition at line 1311 of file ui_ncurses.c.

1311 {
1313 return;
1314 ui_shutdown();
1315 f_ncurses_open = false;
1317 sig_dfl_mode();
1318 return;
1319}
bool f_ncurses_open
Definition ui_common.c:50
bool f_notcurses_open
Definition ui_common.c:51
void ui_shutdown()
Shutdown the UI runtime, cleaning up resources and freeing memory.
Definition ui_ncurses.c:449
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

References f_ncurses_open, f_notcurses_open, restore_shell_tioctl(), sig_dfl_mode(), and ui_shutdown().

Here is the call graph for this function:

◆ ui_doupdate()

int ui_doupdate ( )

Refresh the display by updating the screen.

Definition at line 942 of file ui_ncurses.c.

942 {
943 doupdate();
944 return 0;
945}

◆ ui_endwin()

void ui_endwin ( )

End the UI application, cleaning up resources and shutting down the UI runtime.

Destroy a UI surface and free its resources.

Parameters
sPointer to the UiSurface to be destroyed.

This function destroys a UI surface by freeing its associated ncplanes and metadata. It also frees the memory allocated for the UiSurface structure. If the provided pointer is NULL, the function does nothing.

Definition at line 443 of file ui_ncurses.c.

443 {
444 endwin();
445 // ui_shutdown();
446}

◆ ui_erase()

int ui_erase ( )

Erase the entire screen.

Returns
0 on success, or -1 on failure.

Definition at line 623 of file ui_ncurses.c.

623 {
624 erase();
625 return 0;
626}

◆ ui_get_caps()

void ui_get_caps ( UiCaps * caps)

Get the UI backend capabilities.

Parameters
capsPointer to a UiCaps struct to be filled with the backend capabilities.

Definition at line 62 of file ui_ncurses.c.

62 {
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;
74 caps->color_pairs = UI_PAIRS;
75 caps->pad_relative_yx = false;
76}
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

◆ ui_get_color()

uint32_t ui_get_color ( uint16_t color_idx)

Get the RGB color value for a given color index.

Parameters
color_idxThe index of the color in the curses color table.
Returns
The RGB color value as a 32-bit unsigned integer (0xRRGGBB), or -1 on failure.

Definition at line 1148 of file ui_ncurses.c.

1148 {
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}

◆ ui_get_screen_size()

void ui_get_screen_size ( uint * lines,
uint * cols )

Get the current screen size (number of lines and columns).

Parameters
linesPointer to a uint variable where the number of lines (rows) will be stored.
colsPointer to a uint variable where the number of columns will be stored.

Definition at line 754 of file ui_ncurses.c.

754 {
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}

◆ ui_getcchar()

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.

Parameters
cellPointer to the UiCell from which to retrieve the character and attributes.
wstrPointer to a wchar_t array where the retrieved character will be stored.
attrsPointer to an attr_t variable where the retrieved attributes will be stored.
pairPointer to a short variable where the retrieved color pair will be stored.
optsPointer to additional options (not used in this implementation).
Returns
0 on success, or -1 on failure.

Definition at line 1034 of file ui_ncurses.c.

1034 {
1035 (void)opts;
1036 getcchar(cell, wstr, attrs, pair, nullptr);
1037 return 0;
1038}

◆ ui_getmaxx()

int ui_getmaxx ( UiSurface * s,
ss_t w )

Get the maximum number of columns of a UiSurface.

Parameters
sPointer to the UiSurface for which to retrieve the maximum number of columns.
wThe index of the window within the surface for which to retrieve the maximum number of columns.
Returns
The maximum number of columns of the specified UiSurface, or -1 on failure.

Definition at line 744 of file ui_ncurses.c.

744 {
745 if (!s->mwin[w])
746 return -1;
747 return (int)(getmaxx(s->mwin[w]));
748}

◆ ui_getmaxy()

int ui_getmaxy ( UiSurface * s,
ss_t w )

Get the maximum number of lines (rows) of a UiSurface.

Parameters
sPointer to the UiSurface for which to retrieve the maximum number of lines.
wThe index of the window within the surface for which to retrieve the maximum number of lines.
Returns
The maximum number of lines (rows) of the specified UiSurface, or -1 on failure.

Definition at line 733 of file ui_ncurses.c.

733 {
734 if (!s->mwin[w])
735 return -1;
736 return (int)(getmaxy(s->mwin[w]));
737}

◆ ui_getmaxyx()

void ui_getmaxyx ( UiSurface * s,
ss_t w,
uint * lines,
uint * cols )

Get the maximum dimensions of a UiSurface.

Parameters
sPointer to the UiSurface for which to retrieve the maximum dimensions.
wThe index of the window within the surface for which to retrieve the maximum dimensions.
linesPointer to a uint variable where the maximum number of lines (rows) will be stored.
colsPointer to a uint variable where the maximum number of columns will be stored.

Definition at line 722 of file ui_ncurses.c.

722 {
723 if (!s->mwin[w])
724 return;
725 getmaxyx(s->mwin[w], *lines, *cols);
726}

◆ ui_getyx()

void ui_getyx ( UiSurface * s,
ss_t w,
uint * lines,
uint * cols )

Get the current cursor position within a window of a UI surface.

Parameters
sPointer to the UiSurface containing the window.
wThe index of the window within the surface.
yPointer to an unsigned integer to store the y-coordinate of the cursor.
xPointer to an unsigned integer to store the x-coordinate of the cursor.

This function retrieves the current cursor position within a window of a UI surface. If the surface or window is not initialized, it does nothing.

Definition at line 707 of file ui_ncurses.c.

707 {
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}

◆ ui_hex_to_rgb()

RGB ui_hex_to_rgb ( char * s)

Convert a hexadecimal color string to an RGB structure.

Parameters
sA string representing the color in hexadecimal format (e.g., "#RRGGBB").
Returns
An RGB structure containing the red, green, and blue components of the color.

This function converts a hexadecimal color string to an RGB structure. It uses sscanf to parse the hex string and extract the red, green, and blue components. The resulting RGB structure is returned.

Definition at line 1158 of file ui_ncurses.c.

1158 {
1159 RGB rgb;
1160 sscanf(s, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
1161 return rgb;
1162}

Referenced by ui_init_color_hex().

Here is the caller graph for this function:

◆ ui_idcok()

int ui_idcok ( UiSurface * s,
ss_t w,
bool enable )

Enable or disable the use of the insert/delete character capabilities for a UiSurface.

Parameters
sPointer to the UiSurface for which to enable or disable the insert/delete character capabilities.
wThe index of the window within the surface for which to enable or disable the insert/delete character capabilities.
enableA boolean value indicating whether to enable (true) or disable (false) the insert/delete character capabilities.
Returns
0 on success, or -1 on failure.

Definition at line 844 of file ui_ncurses.c.

844 {
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}

◆ ui_idlok()

int ui_idlok ( UiSurface * s,
ss_t w,
bool enable )

Enable or disable the use of the insert/delete line capabilities for a UiSurface.

Parameters
sPointer to the UiSurface for which to enable or disable the insert/delete line capabilities.
wThe index of the window within the surface for which to enable or disable the insert/delete line capabilities.
enableA boolean value indicating whether to enable (true) or disable (false) the insert/delete line capabilities.
Returns
0 on success, or -1 on failure.

Definition at line 828 of file ui_ncurses.c.

828 {
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}

◆ ui_immedok()

int ui_immedok ( UiSurface * s,
ss_t w,
bool enable )

Definition at line 780 of file ui_ncurses.c.

780 {
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}

◆ ui_init()

struct UiRuntime * ui_init ( const UiConfig * cfg,
SIO * sio )

Initialize the UI runtime with the given configuration and SIO.

Parameters
cfgPointer to a UiConfig structure containing the configuration settings.
sioPointer to an SIO structure for input/output operations.
Returns
A pointer to the initialized UiRuntime structure, or NULL on failure.

Definition at line 130 of file ui_ncurses.c.

130 {
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);
146 ui_log_ctx.min_level = cfg->log_level;
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()) {
158 ui_shutdown();
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}
UiSurface * stdsfc
Definition ui_ncurses.c:46
const wchar_t * border_none
Definition ui_common.c:89
UiLogContext ui_log_ctx
Definition ui_common.c:53
BorderWide bw
Definition ui_common.c:27
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
const wchar_t * border_heavy
Definition ui_common.c:88
const wchar_t * border_rounded
Definition ui_common.c:87
#define TRUE
Definition iloan.c:31
UiBorderStyle border_style
Definition ui_backend.h:486
UiLogLevel log_level
Definition ui_backend.h:488

◆ ui_init_color()

int ui_init_color ( uint color,
uint r,
uint g,
uint b )

Initialize a color in the curses color table with specified RGB values.

Parameters
colorThe index of the color to be initialized.
rThe red component of the color (0-255).
gThe green component of the color (0-255).
bThe blue component of the color (0-255).
Returns
0 on success, or -1 on failure.

Definition at line 1180 of file ui_ncurses.c.

1180 {
1181 init_extended_color(color, r, g, b);
1182 return 0;
1183}

Referenced by ui_color_from_rgb().

Here is the caller graph for this function:

◆ ui_init_pair()

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.

Parameters
pairThe index of the color pair to be initialized.
fgThe foreground color index for the color pair.
bgThe background color index for the color pair.
Returns
0 on success, or -1 on failure.

Definition at line 1206 of file ui_ncurses.c.

1206 {
1207 init_extended_pair(pair, fg, bg);
1208 return 0;
1209}

◆ ui_keypad()

int ui_keypad ( UiSurface * s,
ss_t w,
bool enable )

Enable or disable the keypad for a UiSurface.

Parameters
sPointer to the UiSurface for which to enable or disable the keypad.
wThe index of the window within the surface for which to enable or disable the keypad.
enableA boolean value indicating whether to enable (true) or disable (false) the keypad.
Returns
0 on success, or -1 on failure.

Definition at line 812 of file ui_ncurses.c.

812 {
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}

◆ ui_ncurses_get_screen()

SCREEN * ui_ncurses_get_screen ( )

Get the current SCREEN pointer used by the UI.

Returns
The SCREEN pointer if the UI is initialized, or NULL if the UI is not initialized.

Definition at line 1242 of file ui_ncurses.c.

1242 {
1243 if (!ui)
1244 return NULL;
1245 return ui->screen;
1246}

References ui.

◆ ui_ncurses_surface_get_panel()

PANEL * ui_ncurses_surface_get_panel ( const UiSurface * s,
ss_t w )

Get the current PANEL pointer for a specific window within a UiSurface.

Return the raw NCurses PANEL* underlying a surface.

Parameters
sPointer to the UiSurface containing the window.
wThe index of the window within the surface for which to retrieve the PANEL pointer.
Returns
The PANEL pointer for the specified window, or NULL if the UiSurface is NULL.

Definition at line 1262 of file ui_ncurses.c.

1262 {
1263 if (!s)
1264 return NULL;
1265 return s->mpan[w];
1266}

◆ ui_ncurses_surface_get_win()

WINDOW * ui_ncurses_surface_get_win ( const UiSurface * s,
ss_t w )

Get the current FILE pointer for the TTY used by the UI.

Return the raw NCurses WINDOW* underlying a surface.

Returns
The FILE pointer for the TTY if the UI is initialized, or NULL if the UI is not initialized.

Definition at line 1251 of file ui_ncurses.c.

1251 {
1252 if (!s)
1253 return NULL;
1254 return s->mwin[w];
1255}

◆ ui_pair_content()

int ui_pair_content ( uint pair,
uint * fg,
uint * bg )

Get the foreground and background colors of a specified color pair.

Parameters
pairThe index of the color pair to be queried.
fgPointer to a uint variable where the foreground color index will be stored.
bgPointer to a uint variable where the background color index will be stored.
Returns
0 on success, or -1 on failure.

Definition at line 1191 of file ui_ncurses.c.

1191 {
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}

Referenced by parse_ansi_str(), ui_add_pair(), and ui_parse_ansi().

Here is the caller graph for this function:

◆ ui_pair_from_hex()

int ui_pair_from_hex ( const char * fg,
const char * bg )

Convert hex color strings to a curses color pair index.

Parameters
fgForeground color in hex format (e.g., "#RRGGBB").
bgBackground color in hex format (e.g., "#RRGGBB").
Returns
The curses color pair index corresponding to the given foreground and background colors.

Definition at line 87 of file ui_ncurses.c.

87 {
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}

References ui_add_pair(), and ui_color_from_rgb().

Here is the call graph for this function:

◆ ui_prefresh()

int ui_prefresh ( UiSurface * s,
ss_t w,
int pminrow,
int pmincol,
int sminrow,
int smincol,
int smaxrow,
int smaxcol )

Definition at line 923 of file ui_ncurses.c.

923 {
924 if (!s)
925 return -1;
926 return prefresh(s->mwin[w], pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol);
927}

◆ ui_render()

void ui_render ( )

Render the UI by updating panels and refreshing the display.

Render the UI.

This function renders the UI.

Definition at line 931 of file ui_ncurses.c.

931 {
932 update_panels();
933 doupdate();
934}

◆ ui_resume()

int ui_resume ( )

Resume the UI, restoring the previous program state and updating the display.

Returns
0 on success, or -1 on failure.

Definition at line 534 of file ui_ncurses.c.

534 {
535 reset_prog_mode();
536 update_panels();
537 doupdate();
538 return 0;
539}

◆ ui_scrollok()

int ui_scrollok ( UiSurface * s,
ss_t w,
bool enable )

Enable or disable scrolling for a UiSurface.

Parameters
sPointer to the UiSurface for which to enable or disable scrolling.
wThe index of the window within the surface for which to enable or disable scrolling.
enableA boolean value indicating whether to enable (true) or disable (false) scrolling.
Returns
0 on success, or -1 on failure.

Definition at line 796 of file ui_ncurses.c.

796 {
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}

◆ ui_setcchar()

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.

Parameters
cellPointer to the UiCell to be modified.
wstrPointer to a wchar_t array containing the character to be set.
attrsThe attributes to be applied to the cell.
pairThe color pair to be applied to the cell.
optsPointer to additional options (not used in this implementation).
Returns
0 on success, or -1 on failure.

Definition at line 1048 of file ui_ncurses.c.

1048 {
1049 (void)opts;
1050 return setcchar(cell, wstr, attrs, pair, NULL);
1051}

◆ ui_setscrreg()

int ui_setscrreg ( UiSurface * s,
ss_t w,
uint top,
uint bottom )

Set the scrolling region for a UiSurface.

Parameters
sPointer to the UiSurface for which to set the scrolling region.
wThe index of the window within the surface for which to set the scrolling region.
topThe top line of the scrolling region (0-based).
bottomThe bottom line of the scrolling region (0-based).
Returns
0 on success, or -1 on failure.

Definition at line 861 of file ui_ncurses.c.

861 {
862 if (!s->mwin[w])
863 return -1;
864 wsetscrreg(s->mwin[w], top, bottom);
865 return 0;
866}

◆ ui_shutdown()

void ui_shutdown ( )

Shutdown the UI runtime, cleaning up resources and freeing memory.

Shutdown the UI runtime and free all associated resources.

This function shuts down the UI runtime by destroying all surfaces, freeing memory, and stopping the NotCurses library. It also closes the terminal file pointer if it was opened. If the UI runtime is not initialized, the function does nothing.

Definition at line 449 of file ui_ncurses.c.

449 {
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}
int sfc_ptr
Definition ui_ncurses.c:42
void ui_surface_destroy(UiSurface *s)
Destroy a UiSurface, cleaning up its windows and panels.
Definition ui_ncurses.c:499
UiSurface * ui_surface[UI_SFC_MAX]
Definition ui_ncurses.c:40

◆ ui_surface_addpad()

int ui_surface_addpad ( UiSurface * s,
ss_t pad,
ss_t p,
uint lines,
uint cols,
uint begy,
uint begx,
bool hidden )

Add a new pad to the specified UiSurface.

Parameters
sPointer to the UiSurface to which the pad will be added.
wThe index of the window within the surface where the pad will be created.
view_winThe index of the window that will be used to view the pad.
linesThe number of lines (rows) for the new pad.
colsThe number of columns for the new pad.
begyThe y-coordinate (row) for the new pad's position (ignored in this implementation).
begxThe x-coordinate (column) for the new pad's position (ignored in this implementation).
Returns
0 on success, or -1 on failure.

Definition at line 381 of file ui_ncurses.c.

381 {
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}
UiLogLevel
Definition ui_backend.h:230

◆ ui_surface_addwin()

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.

Parameters
sPointer to the UiSurface to which the window will be added.
wThe index of the window within the surface where the new window will be created.
pThe index of the parent window from which the new window will be derived.
linesThe number of lines (rows) for the new window.
colsThe number of columns for the new window.
yThe y-coordinate (row) for the new window's position relative to the parent window.
xThe x-coordinate (column) for the new window's position relative to the parent window.
Returns
0 on success, or -1 on failure.

Definition at line 423 of file ui_ncurses.c.

423 {
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}

◆ ui_surface_box()

UiSurface * ui_surface_box ( UiSurface * parent,
ss_t p,
uint lines,
uint cols,
uint y,
uint x,
const char * wtitle )

Create a new UI surface with a border box and optional title.

Parameters
parentPointer to the parent UiSurface, or NULL for a top-level surface.
pThe index of the parent window to derive from, if applicable.
linesThe number of lines (rows) for the new surface.
colsThe number of columns for the new surface.
yThe y-coordinate (row) for the new surface's position.
xThe x-coordinate (column) for the new surface's position.
wtitleThe title string to display in the border box, or NULL for no title.
Returns
A pointer to the newly created UiSurface with a border box, or NULL on failure.

Definition at line 293 of file ui_ncurses.c.

293 {
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);
340 ui_border_draw(s, BOX);
341 ui_border_title(s, BOX, wtitle);
342#ifdef DEBUG_IMMED_UI
343 immedok(s->mwin[BOX], true);
344#endif
345 return s;
346}
ss_t
Definition ui_backend.h:84
int ui_border_draw(UiSurface *sfc, ss_t w)
Definition ui_common.c:523
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
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_scrollok(UiSurface *s, ss_t w, bool enable)
Enable or disable scrolling for a UiSurface.
Definition ui_ncurses.c:796
unsigned int y
Definition ui_backend.h:369
unsigned int lines
Definition ui_backend.h:371
unsigned int cols
Definition ui_backend.h:372
unsigned int x
Definition ui_backend.h:370
struct UiSurface * parent
Definition ui_backend.h:397
struct UiSurfaceMeta meta[SUB_SFC_MAX]
Definition ui_backend.h:395
struct UiRuntime * runtime
Definition ui_backend.h:396

◆ ui_surface_destroy()

void ui_surface_destroy ( UiSurface * s)

Destroy a UiSurface, cleaning up its windows and panels.

Parameters
sPointer to the UiSurface to be destroyed.

Definition at line 499 of file ui_ncurses.c.

499 {
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]);
505 ui_render();
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}
void ui_render()
Render the UI by updating panels and refreshing the display.
Definition ui_ncurses.c:931

◆ ui_surface_hide()

int ui_surface_hide ( UiSurface * s,
ss_t w )

Hide a UiSurface, making it invisible on the screen.

Parameters
sPointer to the UiSurface to be hidden.
wThe index of the window within the surface to be hidden.
Returns
0 on success, or -1 on failure.

Definition at line 667 of file ui_ncurses.c.

667 {
668 if (!s)
669 return -1;
670 hide_panel(s->mpan[w]);
671 s->meta[w].hidden = true;
672 return 0;
673}

◆ ui_surface_move()

int ui_surface_move ( UiSurface * s,
ss_t w,
uint y,
uint x )

Move a UiSurface to a new position on the screen.

Parameters
sPointer to the UiSurface to be moved.
wThe index of the window within the surface to be moved.
yThe new y-coordinate (row) for the surface's position.
xThe new x-coordinate (column) for the surface's position.
Returns
0 on success, or -1 on failure.

Definition at line 552 of file ui_ncurses.c.

552 {
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}

◆ ui_surface_new()

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.

Parameters
wThe index of the window within the surface.
parentPointer to the parent UiSurface, or NULL for a top-level surface.
pThe index of the parent window to derive from, if applicable.
linesThe number of lines (rows) for the new surface.
colsThe number of columns for the new surface.
yThe y-coordinate (row) for the new surface's position.
xThe x-coordinate (column) for the new surface's position.
Returns
A pointer to the newly created UiSurface, or NULL on failure.

Definition at line 230 of file ui_ncurses.c.

230 {
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}

◆ ui_surface_resize()

int ui_surface_resize ( UiSurface * s,
ss_t w,
uint lines,
uint cols )

Resize a UiSurface to new dimensions.

Parameters
sPointer to the UiSurface to be resized.
wThe index of the window within the surface to be resized.
linesThe new number of lines (rows) for the surface.
colsThe new number of columns for the surface.
Returns
0 on success, or -1 on failure.

Definition at line 567 of file ui_ncurses.c.

567 {
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}

◆ ui_surface_show()

int ui_surface_show ( UiSurface * s,
ss_t w )

Show a UiSurface, making it visible on the screen.

Parameters
sPointer to the UiSurface to be shown.
wThe index of the window within the surface to be shown.
Returns
0 on success, or -1 on failure.

Definition at line 641 of file ui_ncurses.c.

641 {
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}

◆ ui_suspend()

int ui_suspend ( )

Suspend the UI, saving the current program state and ending the window session.

Returns
0 on success, or -1 on failure.

Definition at line 525 of file ui_ncurses.c.

525 {
526 def_prog_mode();
527 endwin();
528 return 0;
529}

◆ ui_top_surface()

int ui_top_surface ( UiSurface * s,
ss_t w )

Hide a UiSurface, making it invisible on the screen.

Parameters
sPointer to the UiSurface to be hidden.
wThe index of the window within the surface to be hidden.
Returns
0 on success, or -1 on failure.

Definition at line 655 of file ui_ncurses.c.

655 {
656 if (!s)
657 return -1;
658 top_panel(s->mpan[w]);
659 return 0;
660}

◆ ui_update_panels()

void ui_update_panels ( )

Update the panels in the UI.

Update the panels and render the UI.

This function updates the panels and renders the UI.

Definition at line 937 of file ui_ncurses.c.

937 {
938 update_panels();
939}

◆ ui_wclear()

int ui_wclear ( UiSurface * s,
ss_t w )

Clear the contents of a UiSurface or all surfaces.

Parameters
sPointer to the UiSurface to be cleared, or NULL to clear all surfaces.
wThe index of the window within the surface to be cleared, or ALLWINS to clear all windows.
Returns
0 on success, or -1 on failure.

Definition at line 603 of file ui_ncurses.c.

603 {
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}
#define ALLWINS
Definition ui_backend.h:63

◆ ui_werase()

int ui_werase ( UiSurface * s,
ss_t w )

Erase the contents of a UiSurface or all surfaces.

Parameters
sPointer to the UiSurface to be erased, or NULL to erase all surfaces.
wThe index of the window within the surface to be erased, or ALLWINS to erase all windows.
Returns
0 on success, or -1 on failure.

Definition at line 582 of file ui_ncurses.c.

582 {
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}

◆ ui_wmove()

int ui_wmove ( UiSurface * s,
ss_t w,
uint y,
uint x )

Move the cursor to a specified position within a UiSurface.

Parameters
sPointer to the UiSurface where the cursor will be moved.
wThe index of the window within the surface where the cursor will be moved.
yThe new y-coordinate (row) for the cursor's position.
xThe new x-coordinate (column) for the cursor's position.
Returns
0 on success, or -1 on failure.

Definition at line 685 of file ui_ncurses.c.

685 {
686 if (!s || !s->mwin[w])
687 return -1;
688 return wmove(s->mwin[w], y, x);
689}

◆ ui_wnoutrefresh()

int ui_wnoutrefresh ( UiSurface * s,
ss_t w )

Refresh a specific window within a UiSurface.


Refreshing

Parameters
sPointer to the UiSurface containing the window to be refreshed.
wThe index of the window within the surface to be refreshed.
Returns
0 on success, or -1 on failure.

Refreshing

Parameters
sPointer to the UiSurface containing the window to be refreshed.
wThe index of the window within the surface to be refreshed.
Returns
0 on success, or -1 on failure.

Definition at line 955 of file ui_ncurses.c.

955 {
956 if (!s)
957 return -1;
958 wnoutrefresh(s->mwin[w]);
959 return 0;
960}

◆ ui_wscrl()

int ui_wscrl ( UiSurface * s,
ss_t w,
int n )

Scroll the contents of a UiSurface by a specified number of lines.

Parameters
sPointer to the UiSurface to be scrolled.
wThe index of the window within the surface to be scrolled.
nThe number of lines to scroll. Positive values scroll down, negative values scroll up.
Returns
0 on success, or -1 on failure.

Definition at line 770 of file ui_ncurses.c.

770 {
771 if (!s->mwin[w])
772 return -1;
773 wscrl(s->mwin[w], n);
774 return 0;
775}

Variable Documentation

◆ sfc_ptr

int sfc_ptr = -1

Definition at line 42 of file ui_ncurses.c.

◆ stdsfc

UiSurface* stdsfc

Definition at line 46 of file ui_ncurses.c.

◆ ui

◆ ui_color_cnt

uint ui_color_cnt = 0

Definition at line 43 of file ui_ncurses.c.

Referenced by ui_color_from_rgb(), ui_init(), ui_init_clr_palette(), and ui_init_color_hex().

◆ ui_pair_cnt

uint ui_pair_cnt = 0

Definition at line 44 of file ui_ncurses.c.

Referenced by ui_add_pair(), and ui_init().

◆ ui_surface