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.
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.
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 w, uint view_win, uint lines, uint cols, uint begy, uint begx)
 Add a new pad to the specified UiSurface.
int ui_surface_addwin (UiSurface *s, ss_t w, uint p, uint lines, uint cols, uint y, uint x)
 Add a new window to the specified UiSurface.
UiSurface * ui_surface_box (UiSurface *parent, uint 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, uint 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 339 of file ui_ncurses.c.

339 {
340 update_panels();
341 doupdate();
342 for (int i = SUB_SFC_MAX; i >= 0; i--) {
343 if (s->mpan[i]) {
344 hide_panel(s->mpan[i]);
345 del_panel(s->mpan[i]);
346 s->mpan[i] = NULL;
347 }
348 }
349 for (int i = SUB_SFC_MAX; i >= 0; i--) {
350 if (s->mwin[i]) {
351 delwin(s->mwin[i]);
352 s->mwin[i] = NULL;
353 }
354 }
355 endwin();
356 exit(EXIT_SUCCESS);
357}
@ SUB_SFC_MAX
Definition ui_backend.h:47

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 1223 of file ui_ncurses.c.

1223 {
1224 attr_t attrs;
1225 short cp;
1226 uint p1 = 0;
1227 uint *pos = &p1;
1228 if (p)
1229 pos = p;
1230 else
1231 pos = &p1;
1232 uint i = 0, len = 0;
1233 const char *s;
1234 UiCell cc = {};
1235 wchar_t wstr[5];
1236 getcchar(cell_base, &wstr[0], &attrs, &cp, nullptr);
1237 mbstate_t mbstate;
1238 memset(&mbstate, 0, sizeof(mbstate));
1239 if (pos && *pos >= atmost - 1)
1240 return 0;
1241 while (str[i] != '\0') {
1242 s = &str[i];
1243 len = mbrtowc(wstr, s, MB_CUR_MAX, &mbstate);
1244 if (len <= 0) {
1245 wstr[0] = L'?';
1246 wstr[1] = L'\0';
1247 len = 1;
1248 }
1249 wstr[1] = L'\0';
1250 if (*pos > atmost)
1251 break;
1252 if (setcchar(&cc, wstr, attrs, cp, nullptr) != ERR) {
1253 if (len > 0 && (*pos + len) < atmost)
1254 cmplx_buf[(*pos)++] = cc;
1255 }
1256 i += len;
1257 }
1258 wstr[0] = L'\0';
1259 wstr[1] = L'\0';
1260 setcchar(&cc, wstr, attrs, cp, nullptr);
1261 cmplx_buf[*pos] = cc;
1262 return *pos;
1263}
uint16_t attr_t
Definition ui_backend.h:417
#define ERR
Definition ui_backend.h:427

◆ 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 1014 of file ui_ncurses.c.

1014 {
1015 int rc;
1016 uint i;
1017 uint pfg, pbg;
1018 for (i = 1; i < ui_pair_cnt; i++) {
1019 ui_pair_content(i, &pfg, &pbg);
1020 if (pfg == fg && pbg == bg)
1021 return i;
1022 }
1023 if (i + 1 >= UI_PAIRS) {
1024 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
1025 ssnprintf(em1, MAXLEN - 1, _("ui_add_pair failed for pair: %d"), i);
1026 strerror_r(errno, em2, MAXLEN);
1027 ui_display_error(em0, em1, em2, nullptr);
1028 return (EXIT_FAILURE);
1029 }
1030 rc = init_extended_pair(i, fg, bg);
1031 if (rc == ERR) {
1032 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
1033 ssnprintf(em1, MAXLEN - 1, _("init_extended_pair failed for pair: %d"), i);
1034 ssnprintf(em2, MAXLEN - 1, _("fg: %d, bg: %d, ui_pair_cnt: %d"), fg, bg, ui_pair_cnt);
1035 ui_display_error(em0, em1, em2, nullptr);
1036 return (EXIT_FAILURE);
1037 }
1038 ui_pair_cnt++;
1039 return (int)(ui_pair_cnt - 1);
1040}
char em1[MAXLEN]
Definition ui_common.c:47
char em2[MAXLEN]
Definition ui_common.c:48
#define UI_PAIRS
Definition ui_backend.h:194
char em0[MAXLEN]
Definition ui_common.c:46
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 curskeys.c:15
#define _(String)
Definition amort.c:23
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:390
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:1200

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 927 of file ui_ncurses.c.

927 {
928 if (!s)
929 return -1;
930 wbkgrnd(s->mwin[w], cell);
931 return 0;
932}

◆ 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 941 of file ui_ncurses.c.

941 {
942 if (!s)
943 return -1;
944 wbkgrndset(s->mwin[w], cell);
945 return 0;
946}

◆ 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 955 of file ui_ncurses.c.

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

◆ 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 969 of file ui_ncurses.c.

969 {
970 if (!s->mwin[w])
971 return -1;
972 wbkgrndset(s->mwin[w], cell);
973 return 0;
974}

◆ 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 101 of file ui_ncurses.c.

101 {
102 UiCell cc = {0};
103 RGB rgb;
104 rgb.r = (*fg >> 16) & 0xff;
105 rgb.g = (*fg >> 8) & 0xff;
106 rgb.b = *fg & 0xff;
107 int f_idx = ui_color_from_rgb(&rgb);
108 rgb.r = (*bg >> 16) & 0xff;
109 rgb.g = (*bg >> 8) & 0xff;
110 rgb.b = *bg & 0xff;
111 int b_idx = ui_color_from_rgb(&rgb);
112 short cp = ui_add_pair(f_idx, b_idx);
113 wchar_t wstr[2] = {L'\0', L'\0'};
114 wstr[0] = *ucp;
115 wstr[1] = L'\0';
116 setcchar(&cc, wstr, WA_NORMAL, cp, nullptr);
117 return cc;
118}
#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:308
uint8_t r
Definition ui_backend.h:308
uint8_t g
Definition ui_backend.h:308

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 1169 of file ui_ncurses.c.

1169 {
1170 if (color_idx + 1 >= UI_COLORS)
1171 return -1;
1172 RGB rgb;
1173 rgb.r = (*color >> 16) & 0xff;
1174 rgb.g = (*color >> 8) & 0xff;
1175 rgb.b = *color & 0xff;
1176 ui_apply_gamma(&rgb);
1177 if (color_idx < 16) {
1178 std_color[color_idx].r = rgb.r;
1179 std_color[color_idx].g = rgb.g;
1180 std_color[color_idx].b = rgb.b;
1181 }
1182 rgb.r = (rgb.r * 1000) / 255;
1183 rgb.g = (rgb.g * 1000) / 255;
1184 rgb.b = (rgb.b * 1000) / 255;
1185 init_extended_color(color_idx, rgb.r, rgb.g, rgb.b);
1186 return 0;
1187}
#define UI_COLORS
Definition ui_backend.h:193
STDRGB std_color[]
Definition ui_common.c:159
void ui_apply_gamma(RGB *rgb)
Apply gamma correction to RGB color.
Definition ui_common.c:941

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 1048 of file ui_ncurses.c.

1048 {
1049 if (pair + 1 >= UI_PAIRS)
1050 return -1;
1051 init_extended_pair(pair, fg, bg);
1052 return 0;
1053}

◆ ui_clear()

int ui_clear ( )

Clear the entire screen.

Returns
0 on success, or -1 on failure.

Definition at line 599 of file ui_ncurses.c.

599 {
600 clear();
601 return 0;
602}

◆ ui_color_content()

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

Definition at line 1116 of file ui_ncurses.c.

1116 {
1117 int _color = (int)color;
1118 int _r, _g, _b;
1119 extended_color_content(_color, &_r, &_g, &_b);
1120 *r = (uint)_r;
1121 *g = (uint)_g;
1122 *b = (uint)_b;
1123 return 0;
1124}

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 1060 of file ui_ncurses.c.

1060 {
1061 if (rgb->r == rgb->g && rgb->g == rgb->b && rgb->b == 0)
1062 return 0;
1063 // #ifdef DEBUG_COLOR
1064 if (rgb->r > 255 || rgb->g > 255 || rgb->b > 255) {
1065 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
1066 ui_log(ERROR, "%s", em0);
1067 ssnprintf(em1, MAXLEN - 1, _("ui_color_from_rgb failed for RGB: %d,%d,%d"), rgb->r, rgb->g, rgb->b);
1068 ui_log(ERROR, "%s", em0);
1069 // return (EXIT_FAILURE);
1070 }
1071 // #endif
1072 uint i;
1073 RGB tmp;
1074 ui_apply_gamma(rgb);
1075 rgb->r = (rgb->r * 1000) / 255;
1076 rgb->g = (rgb->g * 1000) / 255;
1077 rgb->b = (rgb->b * 1000) / 255;
1078 for (i = 0; i < ui_color_cnt && i < UI_COLORS; i++) {
1079 extended_color_content(i, &tmp.r, &tmp.g, &tmp.b);
1080 if (rgb->r == tmp.r && rgb->g == tmp.g && rgb->b == tmp.b)
1081 return i;
1082 }
1083 if (i < UI_COLORS) {
1084 if (i < 16) {
1085 std_color[i].r = rgb->r;
1086 std_color[i].g = rgb->g;
1087 std_color[i].b = rgb->b;
1088 }
1089 init_extended_color(i, rgb->r, rgb->g, rgb->b);
1090 if (ui_color_cnt + 1 < UI_COLORS)
1091 ui_color_cnt++;
1092 return ui_color_cnt - 1;
1093 }
1094 return 0;
1095}
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 840 of file ui_ncurses.c.

840 {
841 curs_set(visibility);
842 return 0;
843}

◆ 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 870 of file ui_ncurses.c.

870 {
871 (void)s;
872 (void)w;
873 if (!ui)
874 return -1;
875 ui->cursor_visible = visible;
876 curs_set(visible ? 1 : 0);
877 return 0;
878}
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 855 of file ui_ncurses.c.

855 {
856 if (!ui)
857 return -1;
858 wmove(s->mwin[w], y, x);
859 ui->cursor_visible = visible;
860 curs_set(visible ? 2 : 0);
861 return 0;
862}

◆ 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 666 of file ui_ncurses.c.

666 {
667 if (!s || !s->mwin[w])
668 return -1;
669 return wmove(s->mwin[w], y, x);
670}

◆ ui_def_prog_mode()

void ui_def_prog_mode ( )

Definition at line 826 of file ui_ncurses.c.

826 {
827 def_prog_mode();
828}

Referenced by fork_exec().

Here is the caller graph for this function:

◆ ui_destroy_curses()

void ui_destroy_curses ( )

Definition at line 1264 of file ui_ncurses.c.

1264 {
1266 return;
1267 ui_shutdown();
1268 f_ncurses_open = false;
1270 sig_dfl_mode();
1271 return;
1272}
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:418
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 895 of file ui_ncurses.c.

895 {
896 doupdate();
897 return 0;
898}

◆ 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 412 of file ui_ncurses.c.

412 {
413 endwin();
414 // ui_shutdown();
415}

◆ ui_erase()

int ui_erase ( )

Erase the entire screen.

Returns
0 on success, or -1 on failure.

Definition at line 591 of file ui_ncurses.c.

591 {
592 erase();
593 return 0;
594}

◆ 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 = 0;
75}
bool palette256
Definition ui_backend.h:458
bool mouse
Definition ui_backend.h:459
int color_pairs
Definition ui_backend.h:462
bool truecolor
Definition ui_backend.h:457
bool unicode
Definition ui_backend.h:460
bool resize
Definition ui_backend.h:461

◆ 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 1101 of file ui_ncurses.c.

1101 {
1102 if (color_idx + 1 >= UI_COLORS)
1103 return -1;
1104 RGB rgb;
1105 extended_color_content(color_idx, &rgb.r, &rgb.g, &rgb.b);
1106 rgb.r = (rgb.r * 255) / 1000;
1107 rgb.g = (rgb.g * 255) / 1000;
1108 rgb.b = (rgb.b * 255) / 1000;
1109 return 0;
1110}

◆ 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 722 of file ui_ncurses.c.

722 {
723 if (!ui)
724 return;
725 getmaxyx(stdscr, ui->lines, ui->cols);
726 if (lines)
727 *lines = ui->lines;
728 if (cols)
729 *cols = ui->cols;
730}

◆ 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 987 of file ui_ncurses.c.

987 {
988 (void)opts;
989 getcchar(cell, wstr, attrs, pair, nullptr);
990 return 0;
991}

◆ 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 712 of file ui_ncurses.c.

712 {
713 if (!s->mwin[w])
714 return -1;
715 return (int)(getmaxx(s->mwin[w]));
716}

◆ 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 701 of file ui_ncurses.c.

701 {
702 if (!s->mwin[w])
703 return -1;
704 return (int)(getmaxy(s->mwin[w]));
705}

◆ 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 690 of file ui_ncurses.c.

690 {
691 if (!s->mwin[w])
692 return;
693 getmaxyx(s->mwin[w], *lines, *cols);
694}

◆ 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 675 of file ui_ncurses.c.

675 {
676 if (!s->mwin[w])
677 return;
678 int _lines, _cols;
679 getyx(s->mwin[w], _lines, _cols);
680 *lines = (uint)(_lines);
681 *cols = (uint)(_cols);
682}

◆ 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 1111 of file ui_ncurses.c.

1111 {
1112 RGB rgb;
1113 sscanf(s, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
1114 return rgb;
1115}

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 803 of file ui_ncurses.c.

803 {
804 if (!s->mwin[w])
805 return -1;
806 if (enable)
807 idcok(s->mwin[w], true);
808 else
809 idcok(s->mwin[w], false);
810 return 0;
811}

◆ 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 787 of file ui_ncurses.c.

787 {
788 if (!s->mwin[w])
789 return -1;
790 if (enable)
791 idlok(s->mwin[w], true);
792 else
793 idlok(s->mwin[w], false);
794 return 0;
795}

◆ 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 129 of file ui_ncurses.c.

129 {
130 setlocale(LC_ALL, "en_US.UTF-8");
131 ui = calloc(1, sizeof(*ui));
132 if (!ui)
133 return NULL;
134 char tty_name[MAXLEN];
135 if (ttyname_r(STDERR_FILENO, tty_name, sizeof(tty_name)) != 0) {
136 free(ui);
137 return NULL;
138 }
139 ui->tty_fp = fopen(tty_name, "r+");
140 if (!ui->tty_fp) {
141 free(ui);
142 return NULL;
143 }
144 ui->tty_fd = fileno(ui->tty_fp);
146
147 ui_log(INFO, _("ui_init: using tty: %s"), tty_name);
148 ui->screen = newterm(NULL, ui->tty_fp, ui->tty_fp);
149 f_ncurses_open = true;
150 if (!ui->screen) {
151 fclose(ui->tty_fp);
152 free(ui);
153 return NULL;
154 }
155 set_term(ui->screen);
156 if (!has_colors() || !can_change_color()) {
157 ui_shutdown();
158 return NULL;
159 }
160 start_color();
161 use_default_colors();
162 cbreak();
163 noecho();
164 keypad(stdscr, TRUE);
165 if (ui->mouse_enabled)
166 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
167 getmaxyx(stdscr, ui->lines, ui->cols);
168
169 stdsfc = calloc(1, sizeof(*stdsfc));
170 if (!stdsfc)
171 return NULL;
172 stdsfc->runtime = ui;
173 stdsfc->parent = NULL;
174 stdsfc->meta[BOX].lines = (int)ui->lines;
175 stdsfc->meta[BOX].cols = (int)ui->cols;
176 stdsfc->meta[BOX].y = 0;
177 stdsfc->meta[BOX].x = 0;
178 stdsfc->mwin[BOX] = stdscr;
179 stdsfc->mpan[BOX] = new_panel(stdsfc->mwin[BOX]);
180 if (!stdsfc->mpan[BOX]) {
181 free(stdsfc);
182 ui_log(ERROR, _("new_panel failed for stdsfc->mpan[BOX]"));
183 exit(EXIT_FAILURE);
184 }
185 if (cfg->border_style)
186 ui->border_style = cfg->border_style;
187 if (!ui->border_style)
188 ui->border_style = UI_BORDER_ROUNDED;
189 if (cfg) {
190 switch (ui->border_style) {
191 case UI_BORDER_SINGLE:
192 memcpy(bw.str, border_single, sizeof(bw.str));
193 break;
194 case UI_BORDER_DOUBLE:
195 memcpy(bw.str, border_double, sizeof(bw.str));
196 break;
197 case UI_BORDER_ROUNDED:
198 memcpy(bw.str, border_rounded, sizeof(bw.str));
199 break;
200 case UI_BORDER_HEAVY:
201 memcpy(bw.str, border_heavy, sizeof(bw.str));
202 break;
203 case UI_BORDER_NONE:
204 memcpy(bw.str, border_none, sizeof(bw.str));
205 break;
206 default:
207 memcpy(bw.str, border_rounded, sizeof(bw.str));
208 break;
209 }
210 }
212 ui_log(INFO, _("ui_init: stdsfc->mwin[BOX]: %p, stdsfc->mpan[BOX]: %p"), (void *)stdsfc->mwin[BOX], (void *)stdsfc->mpan[BOX]);
213 return ui;
214}
const wchar_t * border_none
Definition ui_common.c:72
BorderWide bw
Definition ui_common.c:27
const wchar_t * border_double
Definition ui_common.c:69
const wchar_t * border_single
Definition ui_common.c:68
void ui_initialize_sio(SIO *sio)
Definition ui_common.c:182
UiSurface * stdsfc
Definition ui_ncurses.c:46
const wchar_t * border_heavy
Definition ui_common.c:71
const wchar_t * border_rounded
Definition ui_common.c:70
UiLogLevel ui_min_log_level
Definition ui_common.c:1390
#define TRUE
Definition iloan.c:31
UiBorderStyle border_style
Definition ui_backend.h:445
UiLogLevel log_level
Definition ui_backend.h:447

◆ 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 1133 of file ui_ncurses.c.

1133 {
1134 init_extended_color(color, r, g, b);
1135 return 0;
1136}

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 1159 of file ui_ncurses.c.

1159 {
1160 init_extended_pair(pair, fg, bg);
1161 return 0;
1162}

◆ 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 771 of file ui_ncurses.c.

771 {
772 if (!s->mwin[w])
773 return -1;
774 if (enable)
775 keypad(s->mwin[w], true);
776 else
777 keypad(s->mwin[w], false);
778 return 0;
779}

◆ 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 1195 of file ui_ncurses.c.

1195 {
1196 if (!ui)
1197 return NULL;
1198 return ui->screen;
1199}

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 1215 of file ui_ncurses.c.

1215 {
1216 if (!s)
1217 return NULL;
1218 return s->mpan[w];
1219}

◆ 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 1204 of file ui_ncurses.c.

1204 {
1205 if (!s)
1206 return NULL;
1207 return s->mwin[w];
1208}

◆ 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 1144 of file ui_ncurses.c.

1144 {
1145 int _pair = (int)pair;
1146 int _fg, _bg;
1147 extended_pair_content(_pair, &_fg, &_bg);
1148 *fg = (uint)_fg;
1149 *bg = (uint)_bg;
1150 return 0;
1151}

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 86 of file ui_ncurses.c.

86 {
87 RGB rgb;
88 sscanf(fg, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
89 int f_idx = ui_color_from_rgb(&rgb);
90 sscanf(bg, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
91 int b_idx = ui_color_from_rgb(&rgb);
92 return ui_add_pair(f_idx, b_idx);
93}

References ui_add_pair(), and ui_color_from_rgb().

Here is the call graph for this function:

◆ 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 884 of file ui_ncurses.c.

884 {
885 update_panels();
886 doupdate();
887}

◆ 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 503 of file ui_ncurses.c.

503 {
504 reset_prog_mode();
505 update_panels();
506 doupdate();
507 return 0;
508}

◆ 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 755 of file ui_ncurses.c.

755 {
756 if (!s->mwin[w])
757 return -1;
758 if (enable)
759 scrollok(s->mwin[w], true);
760 else
761 scrollok(s->mwin[w], false);
762 return 0;
763}

◆ 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 1001 of file ui_ncurses.c.

1001 {
1002 (void)opts;
1003 return setcchar(cell, wstr, attrs, pair, NULL);
1004}

◆ 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 820 of file ui_ncurses.c.

820 {
821 if (!s->mwin[w])
822 return -1;
823 wsetscrreg(s->mwin[w], top, bottom);
824 return 0;
825}

◆ 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 418 of file ui_ncurses.c.

418 {
419 if (ui == NULL)
420 return;
421 ui_log(INFO, _("ui_shutdown in progress..."));
422 while (sfc_ptr >= 0) {
423 if (ui_surface[sfc_ptr] != NULL) {
425 ui_surface[sfc_ptr] = NULL;
426 }
427 ui_log(INFO, _("surface destroy: %d"), sfc_ptr);
428 sfc_ptr--;
429 }
430 if (stdsfc->mpan[0] != NULL) {
431 ui_log(INFO, _("calling del_panel(stdsfc->mpan[BOX])"));
432 hide_panel(stdsfc->mpan[0]);
433 del_panel(stdsfc->mpan[0]);
434 stdsfc->mpan[0] = NULL;
435 }
436 ui_log(INFO, _("calling endwin()"));
437 endwin();
438 f_ncurses_open = false;
439 if (ui->screen != NULL) {
440 ui_log(INFO, _("calling delscreen(ui->screen)"));
441 delscreen(ui->screen);
442 ui->screen = NULL;
443 }
444 if (ui->tty_fp != NULL) {
445 ui_log(INFO, _("closing tty_fp"));
446 fclose(ui->tty_fp);
447 ui->tty_fp = NULL;
448 }
449 if (stdsfc != NULL) {
450 ui_log(INFO, _("freeing stdsfc"));
451 free(stdsfc);
452 stdsfc = NULL;
453 }
454 if (ui != NULL) {
455 if (ui->sio) {
456 free(ui->sio);
457 ui->sio = nullptr;
458 }
459 ui_log(INFO, _("freeing ui"));
460 free(ui);
461 ui = NULL;
462 }
463}
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:468
UiSurface * ui_surface[UI_SFC_MAX]
Definition ui_ncurses.c:40

◆ ui_surface_addpad()

int ui_surface_addpad ( UiSurface * s,
ss_t w,
uint view_win,
uint lines,
uint cols,
uint begy,
uint begx )

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 369 of file ui_ncurses.c.

369 {
370 (void)begy;
371 (void)begx;
372 s->mwin[w] = newpad(lines, cols);
373 if (s->mwin[w] == nullptr)
374 return -1;
375 s->mwin[view_win] = subpad(s->mwin[PAD], lines, cols, 0, 0);
376 if (s->mwin[view_win] == nullptr)
377 return -1;
378 s->mpan[w] = new_panel(s->mwin[view_win]);
379 immedok(s->mwin[w], true);
380#ifdef DEBUG_UI
381#endif
382 return 0;
383}

◆ ui_surface_addwin()

int ui_surface_addwin ( UiSurface * s,
ss_t w,
uint 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 395 of file ui_ncurses.c.

395 {
396 s->mwin[w] = derwin(s->mwin[p], lines, cols, y, x);
397 if (!s->mwin[w]) {
398 free(s);
399 return -1;
400 }
401 s->mpan[w] = new_panel(s->mwin[w]);
402 keypad(s->mwin[w], true);
403 ui_bkgd(s, w, &cell_nt);
404 ui_bkgdset(s, w, &cell_nt);
405 immedok(s->mwin[w], true);
406#ifdef DEBUG_UI
407#endif
408 return 0;
409}
UiCell cell_nt
Definition ui_common.c:106
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:927
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:941

◆ ui_surface_box()

UiSurface * ui_surface_box ( UiSurface * parent,
uint 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 288 of file ui_ncurses.c.

288 {
289 if (!ui)
290 return NULL;
291 uint maxy, maxx;
292 ui_get_screen_size(&maxy, &maxx);
293 if (lines > maxy || cols > maxx) {
294 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
295 ssnprintf(em1, MAXLEN - 1, _("ui_surface_box failed for lines: %d, cols: %d"), lines, cols);
296 ssnprintf(em2, MAXLEN - 1, _("maxy: %d, maxx: %d"), maxy, maxx);
297 ui_display_error(em0, em1, em2, nullptr);
298 return NULL;
299 }
300 UiSurface *s = calloc(1, sizeof(*s));
301 if (!s)
302 return NULL;
303 s->runtime = ui;
304 s->parent = parent;
305 s->meta[BOX].y = y;
306 s->meta[BOX].x = x;
307 s->meta[BOX].lines = lines;
308 s->meta[BOX].cols = cols;
309 if (parent && parent->mwin[p]) {
310 s->mwin[BOX] = derwin(parent->mwin[p], lines, cols, y, x);
311 if (!s->mwin[BOX]) {
312 free(s);
313 return NULL;
314 }
315 } else {
316 s->mwin[BOX] = newwin(lines, cols, y, x);
317 if (!s->mwin[BOX]) {
318 free(s);
319 return NULL;
320 }
321 s->mpan[BOX] = new_panel(s->mwin[BOX]);
322 if (!s->mpan[BOX]) {
323 delwin(s->mwin[BOX]);
324 return NULL;
325 }
326 }
327 ui_scrollok(s, BOX, false);
329 ui_border_title(s, wtitle);
330#ifdef DEBUG_UI
331 immedok(s->mwin[BOX], true);
332#endif
333 return s;
334}
int ui_border_draw(UiSurface *sfc)
Definition ui_common.c:474
int ui_border_title(UiSurface *sfc, const char *title)
Draw a box with a title around the specified window.
Definition ui_common.c:563
void ui_get_screen_size(uint *lines, uint *cols)
Get the current screen size (number of lines and columns).
Definition ui_ncurses.c:722
int ui_scrollok(UiSurface *s, ss_t w, bool enable)
Enable or disable scrolling for a UiSurface.
Definition ui_ncurses.c:755
unsigned int y
Definition ui_backend.h:328
unsigned int lines
Definition ui_backend.h:330
unsigned int cols
Definition ui_backend.h:331
unsigned int x
Definition ui_backend.h:329
struct UiSurface * parent
Definition ui_backend.h:356
struct UiSurfaceMeta meta[SUB_SFC_MAX]
Definition ui_backend.h:354
struct UiRuntime * runtime
Definition ui_backend.h:355

◆ 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 468 of file ui_ncurses.c.

468 {
469 if (!s)
470 return;
471 for (int i = SUB_SFC_MAX; i >= 0; i--) {
472 if (s->mpan[i] != NULL) {
473 hide_panel(s->mpan[i]);
474 ui_render();
475 del_panel(s->mpan[i]);
476 s->mpan[i] = NULL;
477 }
478 }
479 for (int i = SUB_SFC_MAX; i >= 0; i--) {
480 if (s->mwin[i] != NULL) {
481 delwin(s->mwin[i]);
482 s->mwin[i] = NULL;
483 }
484 }
485 if (s != NULL) {
486 free(s);
487 s = NULL;
488 }
489}
void ui_render()
Render the UI by updating panels and refreshing the display.
Definition ui_ncurses.c:884

◆ 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 635 of file ui_ncurses.c.

635 {
636 if (!s)
637 return -1;
638 hide_panel(s->mpan[w]);
639 s->meta[w].hidden = true;
640 return 0;
641}

◆ 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 521 of file ui_ncurses.c.

521 {
522 if (!s)
523 return -1;
524 s->meta[w].y = y;
525 s->meta[w].x = x;
526 return move_panel(s->mpan[w], y, x);
527}

◆ ui_surface_new()

UiSurface * ui_surface_new ( ss_t w,
UiSurface * parent,
uint 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 229 of file ui_ncurses.c.

229 {
230 if (!ui)
231 return NULL;
232 uint maxy, maxx;
233 ui_get_screen_size(&maxy, &maxx);
234 if (lines > maxy || cols > maxx) {
235 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 1);
236 ssnprintf(em1, MAXLEN - 1, _("ui_surface_new failed for lines: %d, cols: %d"), lines, cols);
237 ssnprintf(em2, MAXLEN - 1, _("maxy: %d, maxx: %d"), maxy, maxx);
238 ui_display_error(em0, em1, em2, nullptr);
239 return NULL;
240 }
241
242 UiSurface *s = calloc(1, sizeof(*s));
243 if (!s)
244 return NULL;
245 for (int i = 0; i < SUB_SFC_MAX; i++) {
246 s->mwin[i] = NULL;
247 s->mpan[i] = NULL;
248 }
249 s->runtime = ui;
250 s->parent = parent;
251 s->meta[w].lines = lines;
252 s->meta[w].cols = cols;
253 s->meta[w].y = y;
254 s->meta[w].x = x;
255
256 if (parent && parent->mwin[p]) {
257 s->mwin[w] = derwin(parent->mwin[p], lines, cols, y, x);
258 if (!s->mwin[w]) {
259 free(s);
260 return NULL;
261 }
262 } else {
263 s->mwin[w] = newwin(lines, cols, y, x);
264 if (!s->mwin[w]) {
265 free(s);
266 return NULL;
267 }
268 s->mpan[w] = new_panel(s->mwin[w]);
269 if (!s->mpan[w]) {
270 delwin(s->mwin[w]);
271 free(s);
272 return NULL;
273 }
274 }
275 return s;
276}

◆ 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 536 of file ui_ncurses.c.

536 {
537 if (!s)
538 return -1;
539 s->meta[w].lines = lines;
540 s->meta[w].cols = cols;
541 wresize(s->mwin[w], lines + 2, cols + 2);
542 wresize(s->mwin[w + 1], lines, cols);
543 return 0;
544}

◆ 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 609 of file ui_ncurses.c.

609 {
610 if (!s)
611 return -1;
612 show_panel(s->mpan[w]);
613 s->meta[w].hidden = false;
614 return 0;
615 return 0;
616}

◆ 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 494 of file ui_ncurses.c.

494 {
495 def_prog_mode();
496 endwin();
497 return 0;
498}

◆ 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 623 of file ui_ncurses.c.

623 {
624 if (!s)
625 return -1;
626 top_panel(s->mpan[w]);
627 return 0;
628}

◆ 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 890 of file ui_ncurses.c.

890 {
891 update_panels();
892}

◆ 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 571 of file ui_ncurses.c.

571 {
572 if (!s)
573 return -1;
574 if (w == ALLWINS) {
575 for (int i = 1; i < SUB_SFC_MAX; i++) {
576 if (s->mwin[i]) {
577 wclear(s->mwin[i]);
578 }
579 }
580 } else {
581 if (s->mwin[w]) {
582 wclear(s->mwin[w]);
583 }
584 }
585 return 0;
586}
#define ALLWINS
Definition ui_backend.h:195

◆ 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 551 of file ui_ncurses.c.

551 {
552 if (w == ALLWINS) {
553 for (int i = 1; i < SUB_SFC_MAX; i++) {
554 if (s->mwin[i]) {
555 werase(s->mwin[i]);
556 }
557 }
558 } else {
559 if (s->mwin[w]) {
560 werase(s->mwin[w]);
561 }
562 }
563 return 0;
564}

◆ 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 653 of file ui_ncurses.c.

653 {
654 if (!s || !s->mwin[w])
655 return -1;
656 return wmove(s->mwin[w], y, x);
657}

◆ 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 908 of file ui_ncurses.c.

908 {
909 if (!s)
910 return -1;
911 wnoutrefresh(s->mwin[w]);
912 return 0;
913}

◆ 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 738 of file ui_ncurses.c.

738 {
739 if (!s->mwin[w])
740 return -1;
741 wscrl(s->mwin[w], n);
742 return 0;
743}

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