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)
int ui_add_color_rgb (RGB *rgb)
uint ui_add_pair (uint fg, uint bg)
int ui_bkgd (UiSurface *s, uint w, const UiCell *cell)
int ui_bkgdset (UiSurface *s, uint w, const UiCell *cell)
int ui_bkgrnd (UiSurface *s, uint w, const UiCell *cell)
int ui_bkgrndset (UiSurface *s, uint w, const UiCell *cell)
UiSurfaceui_box_surface_new (UiSurface *parent, uint p, uint lines, uint cols, uint y, uint x, char *wtitle)
UiCell ui_cell_from_ucp (const wchar_t *ucp, const uint32_t *fg, const uint32_t *bg)
int ui_chg_color (uint16_t color_idx, uint32_t *color)
int ui_chg_pair (uint pair, uint fg, uint bg)
int ui_clear ()
int ui_color_content (uint color, uint *r, uint *g, uint *b)
int ui_curs_set (int visibility)
int ui_cursor_enable (UiSurface *s, uint w, bool visible)
 Disable (visible = false) or enable (visibile = true) the cursor at its current position on the surface and plane specified.
int ui_cursor_enable_yx (UiSurface *s, uint w, uint y, uint x, bool visible)
 Disable (visible = false) or enable (visibile = true) the cursor at a specified position on the surface and plane specified. Include workaround for NotCurses cursor position bug.
int ui_cursor_move (UiSurface *s, uint w, uint y, uint x)
int ui_doupdate ()
void ui_endwin ()
int ui_erase ()
UiBackend ui_get_backend ()
 Return which backend is powering this runtime.
void ui_get_caps (UiCaps *caps)
 Query the capabilities of the active backend.
uint32_t ui_get_color (uint16_t color_idx)
void ui_get_screen_size (uint *lines, uint *cols)
int ui_getcchar (const UiCell *uc, wchar_t *wstr, attr_t *attrs, uint16_t *pair, void *opts)
int ui_getmaxx (UiSurface *s, uint w)
int ui_getmaxy (UiSurface *s, uint w)
void ui_getmaxyx (UiSurface *s, uint w, uint *lines, uint *cols)
void ui_getyx (UiSurface *s, uint w, uint *lines, uint *cols)
RGB ui_hex_to_rgb (char *s)
int ui_idcok (UiSurface *s, uint w, bool enable)
int ui_idlok (UiSurface *s, uint w, bool enable)
struct UiRuntimeui_init (const UiConfig *cfg)
int ui_init_color (uint color, uint r, uint g, uint b)
int ui_init_pair (uint pair, uint fg, uint bg)
int ui_keypad (UiSurface *s, uint w, bool enable)
SCREEN * ui_ncurses_get_screen ()
PANEL * ui_ncurses_surface_get_panel (const UiSurface *s, uint w)
 Return the raw NCurses PANEL* underlying a surface.
WINDOW * ui_ncurses_surface_get_win (const UiSurface *s, uint w)
 Return the raw NCurses WINDOW* underlying a surface.
int ui_pair_content (uint pair, uint *fg, uint *bg)
int ui_pair_from_hex (const char *fg, const char *bg)
void ui_render ()
int ui_resume ()
int ui_scrollok (UiSurface *s, uint w, bool enable)
int ui_setcchar (cchar_t *wch, const wchar_t *wc, const attr_t attrs, short pair, const void *opts)
int ui_setscrreg (UiSurface *s, uint w, uint top, uint bottom)
void ui_shutdown ()
int ui_surface_addpad (UiSurface *s, uint w, uint view_win, uint lines, uint cols)
int ui_surface_addwin (UiSurface *s, uint w, uint p, uint lines, uint cols, uint y, uint x)
void ui_surface_destroy (UiSurface *s)
int ui_surface_hide (UiSurface *s, uint w)
int ui_surface_move (UiSurface *s, uint w, uint y, uint x)
UiSurfaceui_surface_new (uint w, UiSurface *parent, uint p, uint lines, uint cols, uint y, uint x)
int ui_surface_resize (UiSurface *s, uint w, uint lines, uint cols)
int ui_surface_show (UiSurface *s, uint w)
int ui_suspend ()
int ui_top_panel (UiSurface *s, uint w)
void ui_update_panels ()
int ui_wclear (UiSurface *s, uint w)
int ui_werase (UiSurface *s, uint w)
int ui_wmove (UiSurface *s, uint w, uint y, uint x)
int ui_wnoutrefresh (UiSurface *s, uint w)
int ui_wscrl (UiSurface *s, uint w, uint n)

Variables

wchar_t * border_double = L"═║╠╣╦╩╬╔╗╚╝"
wchar_t * border_heavy = L"━┃┣┫┳┻╋┏┓┗┛"
wchar_t * border_none = L" "
wchar_t * border_rounded = L"─│├┤┬┴┼╭╮╰╯"
wchar_t * border_single = L"─│├┤┬┴┼┌┐└┘"
BorderWide bw
const wchar_t * bw_chk = L"\x2611"
const wchar_t * bw_da = L"\x2193"
const wchar_t * bw_h09 = L"\x23BD"
const wchar_t * bw_la = L"\x2190"
const wchar_t * bw_lan = L"\x276E"
const wchar_t * bw_ra = L"\x2192"
const wchar_t * bw_ran = L"\x276F"
const wchar_t * bw_rbl = L"\x2570"
const wchar_t * bw_rbr = L"\x256f"
const wchar_t * bw_rtl = L"\x256d"
const wchar_t * bw_rtr = L"\x256e"
const wchar_t * bw_sp = L"\x20"
const wchar_t * bw_ua = L"\x2191"
int sfc_ptr = -1
STDRGB std_color []
UiSurfacestdsfc
UiRuntimeui = NULL
uint ui_color_cnt = 0
uint ui_pair_cnt = 0
UiSurfaceui_surface [UI_SFC_MAX]
int win_ptr = -1

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)

Definition at line 490 of file ui_ncurses.c.

490 {
491 update_panels();
492 doupdate();
493 for (int i = SUB_SFC_MAX; i >= 0; i--) {
494 if (s->mpan[i]) {
495 hide_panel(s->mpan[i]);
496 del_panel(s->mpan[i]);
497 s->mpan[i] = NULL;
498 }
499 }
500 for (int i = SUB_SFC_MAX; i >= 0; i--) {
501 if (s->mwin[i]) {
502 delwin(s->mwin[i]);
503 s->mwin[i] = NULL;
504 }
505 }
506 endwin();
507 exit(EXIT_SUCCESS);
508}
PANEL * mpan[SUB_SFC_MAX]
WINDOW * mwin[SUB_SFC_MAX]

References SUB_SFC_MAX.

◆ ui_add_color_rgb()

int ui_add_color_rgb ( RGB * rgb)

Definition at line 138 of file ui_ncurses.c.

138 {
139 uint i;
140 RGB tmp;
141 apply_gamma(rgb);
142 rgb->r = (rgb->r * 1000) / 255;
143 rgb->g = (rgb->g * 1000) / 255;
144 rgb->b = (rgb->b * 1000) / 255;
145 rgb->a = 0;
146 for (i = 0; i < ui_color_cnt && i < UI_COLORS; i++) {
147 extended_color_content(i, &tmp.r, &tmp.g, &tmp.b);
148 if (rgb->r == tmp.r && rgb->g == tmp.g && rgb->b == tmp.b)
149 return i;
150 }
151 if (i < UI_COLORS) {
152 if (i < 16) {
153 std_color[i].r = rgb->r;
154 std_color[i].g = rgb->g;
155 std_color[i].b = rgb->b;
156 }
157 init_extended_color(i, rgb->r, rgb->g, rgb->b);
158 if (ui_color_cnt + 1 < UI_COLORS)
159 ui_color_cnt++;
160 return ui_color_cnt - 1;
161 }
162 return 0;
163}
STDRGB std_color[]
Definition ui_ncurses.c:62
uint ui_color_cnt
Definition ui_ncurses.c:33
#define UI_COLORS
Definition ui_backend.h:39
void apply_gamma(RGB *rgb)
Apply gamma correction to RGB color.
Definition dwin.c:307

◆ ui_add_pair()

uint ui_add_pair ( uint fg,
uint bg )

Definition at line 105 of file ui_ncurses.c.

105 {
106 int rc;
107 uint i;
108 uint pfg, pbg;
109 for (i = 1; i < ui_pair_cnt; i++) {
110 ui_pair_content(i, &pfg, &pbg);
111 if (pfg == fg && pbg == bg)
112 return i;
113 }
114 if (i + 1 >= UI_PAIRS) {
115 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__, __LINE__ - 1);
116 ssnprintf(em1, MAXLEN - 1, "ui_add_pair failed for pair: %d", i);
117 strerror_r(errno, em2, MAXLEN);
118 display_error(em0, em1, em2, nullptr);
119 return (EXIT_FAILURE);
120 }
121 rc = init_extended_pair(i, fg, bg);
122 if (rc == ERR) {
123 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__, __LINE__ - 1);
124 ssnprintf(em1, MAXLEN - 1, "init_extended_pair failed for pair: %d", i);
125 ssnprintf(em2, MAXLEN - 1, "fg: %d, bg: %d, ui_pair_cnt: %d", fg, bg, ui_pair_cnt);
126 display_error(em0, em1, em2, nullptr);
127 return (EXIT_FAILURE);
128 }
129 ui_pair_cnt++;
130 return (int)(ui_pair_cnt - 1);
131}
#define UI_PAIRS
Definition ui_backend.h:40
#define ERR
Definition ui_backend.h:249
uint ui_pair_cnt
Definition ui_ncurses.c:34
int ui_pair_content(uint pair, uint *fg, uint *bg)
Definition ui_ncurses.c:194
#define MAXLEN
Definition curskeys.c:15
char em1[MAXLEN]
Definition dwin.c:143
char em2[MAXLEN]
Definition dwin.c:144
char em0[MAXLEN]
Definition dwin.c:142
int display_error(char *msg0, char *msg1, char *msg2, char *msg3)
Display an error message window or print to stderr.
Definition dwin.c:778
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:413

Referenced by initialize_cells(), parse_ansi(), parse_ansi_str(), ui_cell_from_ucp(), ui_getcchar(), and ui_pair_from_hex().

Here is the caller graph for this function:

◆ ui_bkgd()

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

Definition at line 753 of file ui_ncurses.c.

753 {
754 if (!s)
755 return -1;
756 wbkgrnd(s->mwin[w], cell);
757 return 0;
758}

◆ ui_bkgdset()

int ui_bkgdset ( UiSurface * s,
uint w,
const UiCell * cell )

Definition at line 760 of file ui_ncurses.c.

760 {
761 if (!s)
762 return -1;
763 wbkgrndset(s->mwin[w], cell);
764 return 0;
765}

◆ ui_bkgrnd()

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

Definition at line 767 of file ui_ncurses.c.

767 {
768 if (!s->mwin[w])
769 return -1;
770 wbkgrnd(s->mwin[w], cell);
771 return 0;
772}

◆ ui_bkgrndset()

int ui_bkgrndset ( UiSurface * s,
uint w,
const UiCell * cell )

Definition at line 774 of file ui_ncurses.c.

774 {
775 if (!s->mwin[w])
776 return -1;
777 wbkgrndset(s->mwin[w], cell);
778 return 0;
779}

◆ ui_box_surface_new()

UiSurface * ui_box_surface_new ( UiSurface * parent,
uint p,
uint lines,
uint cols,
uint y,
uint x,
char * wtitle )

Definition at line 453 of file ui_ncurses.c.

453 {
454 UiSurface *s = calloc(1, sizeof(*s));
455 if (!s)
456 return NULL;
457 s->runtime = ui;
458 s->parent = parent;
459 s->meta[BOX].y = y;
460 s->meta[BOX].x = x;
461 s->meta[BOX].lines = lines;
462 s->meta[BOX].cols = cols;
463 if (parent && parent->mwin[p]) {
464 s->mwin[BOX] = derwin(parent->mwin[p], lines + 2, cols + 2, y, x);
465 if (!s->mwin[BOX]) {
466 free(s);
467 return NULL;
468 }
469 } else {
470 s->mwin[BOX] = newwin(lines + 2, cols + 2, y, x);
471 if (!s->mwin[BOX]) {
472 free(s);
473 return NULL;
474 }
475 s->mpan[BOX] = new_panel(s->mwin[BOX]);
476 if (!s->mpan[BOX]) {
477 delwin(s->mwin[BOX]);
478 return NULL;
479 }
480 }
481 ui_scrollok(s, BOX, false);
482 border_draw(s);
483 border_title(s, wtitle);
484#ifdef DEBUG_UI
485 immedok(s->mwin[BOX], true);
486#endif
487 return s;
488}
UiRuntime * ui
int ui_scrollok(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:626
int border_draw(UiSurface *sfc)
Definition dwin.c:581
int border_title(UiSurface *sfc, char *title)
Draw a box with a title around the specified window.
Definition dwin.c:676
A drawable surface in the NotCurses backend.
struct UiSurface * parent
struct UiRuntime * runtime
struct UiSurfaceMeta meta[SUB_SFC_MAX]

◆ ui_cell_from_ucp()

UiCell ui_cell_from_ucp ( const wchar_t * ucp,
const uint32_t * fg,
const uint32_t * bg )

Definition at line 239 of file ui_ncurses.c.

239 {
240 UiCell cc = {0};
241 RGB rgb;
242 rgb.r = (*fg >> 16) & 0xff;
243 rgb.g = (*fg >> 8) & 0xff;
244 rgb.b = *fg & 0xff;
245 int f_idx = ui_add_color_rgb(&rgb);
246 rgb.r = (*bg >> 16) & 0xff;
247 rgb.g = (*bg >> 8) & 0xff;
248 rgb.b = *bg & 0xff;
249 int b_idx = ui_add_color_rgb(&rgb);
250 short cp = ui_add_pair(f_idx, b_idx);
251 wchar_t wstr[2] = {L'\0', L'\0'};
252 wstr[0] = *ucp;
253 wstr[1] = L'\0';
254 setcchar(&cc, wstr, WA_NORMAL, cp, nullptr);
255 return cc;
256}
#define WA_NORMAL
int ui_add_color_rgb(RGB *rgb)
Definition ui_ncurses.c:138
uint ui_add_pair(uint fg, uint bg)
Definition ui_ncurses.c:105

◆ ui_chg_color()

int ui_chg_color ( uint16_t color_idx,
uint32_t * color )

Definition at line 206 of file ui_ncurses.c.

206 {
207 if (color_idx + 1 >= UI_COLORS)
208 return -1;
209 RGB rgb;
210 rgb.r = (*color >> 16) & 0xff;
211 rgb.g = (*color >> 8) & 0xff;
212 rgb.b = *color & 0xff;
213 apply_gamma(&rgb);
214 rgb.r = (rgb.r * 1000) / 255;
215 rgb.g = (rgb.g * 1000) / 255;
216 rgb.b = (rgb.b * 1000) / 255;
217 if (color_idx < 16) {
218 std_color[color_idx].r = rgb.r;
219 std_color[color_idx].g = rgb.g;
220 std_color[color_idx].b = rgb.b;
221 }
222 init_extended_color(color_idx, rgb.r, rgb.g, rgb.b);
223 return 0;
224}

◆ ui_chg_pair()

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

Definition at line 132 of file ui_ncurses.c.

132 {
133 if (pair + 1 >= UI_PAIRS)
134 return -1;
135 init_extended_pair(pair, fg, bg);
136 return 0;
137}

◆ ui_clear()

int ui_clear ( )

Definition at line 594 of file ui_ncurses.c.

594 {
595 clear();
596 return 0;
597}

◆ ui_color_content()

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

Definition at line 179 of file ui_ncurses.c.

179 {
180 int _color = (int)color;
181 int _r, _g, _b;
182 extended_color_content(_color, &_r, &_g, &_b);
183 *r = (uint)_r;
184 *g = (uint)_g;
185 *b = (uint)_b;
186 return 0;
187}

Referenced by ui_add_color_rgb(), ui_get_channels_from_pair(), and ui_setcchar().

Here is the caller graph for this function:

◆ ui_curs_set()

int ui_curs_set ( int visibility)

Definition at line 737 of file ui_ncurses.c.

737 {
738 curs_set(visibility);
739 return 0;
740}

◆ ui_cursor_enable()

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

Disable (visible = false) or enable (visibile = true) the cursor at its current position on the surface and plane specified.

Definition at line 713 of file ui_ncurses.c.

713 {
714 (void)s;
715 (void)w;
716 if (!ui)
717 return -1;
718 ui->cursor_visible = visible;
719 curs_set(visible ? 1 : 0);
720 return 0;
721}

◆ ui_cursor_enable_yx()

int ui_cursor_enable_yx ( UiSurface * s,
uint w,
uint y,
uint x,
bool visible )

Disable (visible = false) or enable (visibile = true) the cursor at a specified position on the surface and plane specified. Include workaround for NotCurses cursor position bug.

Definition at line 703 of file ui_ncurses.c.

703 {
704 (void)s;
705 (void)w;
706 if (!ui)
707 return -1;
708 wmove(s->mwin[w], y, x);
709 ui->cursor_visible = visible;
710 curs_set(visible ? 1 : 0);
711 return 0;
712}

◆ ui_cursor_move()

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

Definition at line 727 of file ui_ncurses.c.

727 {
728 if (!s || !s->mwin[w])
729 return -1;
730 return wmove(s->mwin[w], y, x);
731}

◆ ui_doupdate()

int ui_doupdate ( )

Definition at line 807 of file ui_ncurses.c.

807 {
808 doupdate();
809 return 0;
810}

◆ ui_endwin()

void ui_endwin ( )

Definition at line 341 of file ui_ncurses.c.

341 {
342 ui_shutdown();
343}
void ui_shutdown()
Definition ui_ncurses.c:344

◆ ui_erase()

int ui_erase ( )

Definition at line 590 of file ui_ncurses.c.

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

◆ ui_get_backend()

UiBackend ui_get_backend ( )

Return which backend is powering this runtime.

Parameters
uiThe UiRuntime returned by ui_init().
Returns
The UiBackend enum value for the compiled-in backend.

Definition at line 84 of file ui_ncurses.c.

84 {
85 return UI_BACKEND_NCURSES;
86}
@ UI_BACKEND_NCURSES
Definition ui_backend.h:156

◆ ui_get_caps()

void ui_get_caps ( UiCaps * caps)

Query the capabilities of the active backend.

Parameters
uiThe UiRuntime returned by ui_init().
capsOutput structure filled with capability flags.

Definition at line 88 of file ui_ncurses.c.

88 {
89 if (!caps)
90 return;
91 memset(caps, 0, sizeof(*caps));
92 if (!ui)
93 return;
94 // Will add code to actually check later. For now, just lie.
95 caps->truecolor = true;
96 caps->palette256 = true;
97 caps->mouse = ui->mouse_enabled;
98 caps->unicode = true;
99 caps->resize = true;
100 caps->color_pairs = 0;
101}
bool palette256
Definition ui_backend.h:170
bool mouse
Definition ui_backend.h:171
int color_pairs
Definition ui_backend.h:174
bool truecolor
Definition ui_backend.h:169
bool unicode
Definition ui_backend.h:172
bool resize
Definition ui_backend.h:173

◆ ui_get_color()

uint32_t ui_get_color ( uint16_t color_idx)

Definition at line 164 of file ui_ncurses.c.

164 {
165 if (color_idx + 1 >= UI_COLORS)
166 return -1;
167 RGB rgb;
168 extended_color_content(color_idx, &rgb.r, &rgb.g, &rgb.b);
169 rgb.r = (rgb.r * 255) / 1000;
170 rgb.g = (rgb.g * 255) / 1000;
171 rgb.b = (rgb.b * 255) / 1000;
172 return 0;
173}

◆ ui_get_screen_size()

void ui_get_screen_size ( uint * lines,
uint * cols )

Definition at line 692 of file ui_ncurses.c.

692 {
693 if (!ui)
694 return;
695 getmaxyx(stdscr, ui->lines, ui->cols);
696 if (lines)
697 *lines = ui->lines;
698 if (cols)
699 *cols = ui->cols;
700}

◆ ui_getcchar()

int ui_getcchar ( const UiCell * uc,
wchar_t * wstr,
attr_t * attrs,
uint16_t * pair,
void * opts )

Definition at line 784 of file ui_ncurses.c.

784 {
785 short p;
786 getcchar(uc, wstr, attrs, &p, opts);
787 pair = (uint16_t *)&p;
788 p = (short)(*pair); // Tell compiler to forget about it!
789 return 0;
790}

Referenced by fmt_line(), log_cc_buf(), and mbstr_to_cellstr().

Here is the caller graph for this function:

◆ ui_getmaxx()

int ui_getmaxx ( UiSurface * s,
uint w )

Definition at line 686 of file ui_ncurses.c.

686 {
687
688 if (!s->mwin[w])
689 return -1;
690 return (int)(getmaxx(s->mwin[w]));
691}

Referenced by border_draw(), border_title(), border_ysplit(), and border_ysplit_text().

Here is the caller graph for this function:

◆ ui_getmaxy()

int ui_getmaxy ( UiSurface * s,
uint w )

Definition at line 681 of file ui_ncurses.c.

681 {
682 if (!s->mwin[w])
683 return -1;
684 return (int)(getmaxy(s->mwin[w]));
685}

Referenced by assign_chyron_win(), and border_draw().

Here is the caller graph for this function:

◆ ui_getmaxyx()

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

Definition at line 676 of file ui_ncurses.c.

676 {
677 if (!s->mwin[w])
678 return;
679 getmaxyx(s->mwin[w], *lines, *cols);
680}

◆ ui_getyx()

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

Definition at line 668 of file ui_ncurses.c.

668 {
669 if (!s->mwin[w])
670 return;
671 int _lines, _cols;
672 getyx(s->mwin[w], _lines, _cols);
673 *lines = (uint)(_lines);
674 *cols = (uint)(_cols);
675}

◆ ui_hex_to_rgb()

RGB ui_hex_to_rgb ( char * s)

Definition at line 174 of file ui_ncurses.c.

174 {
175 RGB rgb;
176 sscanf(s, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
177 return rgb;
178}

◆ ui_idcok()

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

Definition at line 653 of file ui_ncurses.c.

653 {
654 if (!s->mwin[w])
655 return -1;
656 if (enable)
657 idcok(s->mwin[w], true);
658 else
659 idcok(s->mwin[w], false);
660 return 0;
661}

◆ ui_idlok()

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

Definition at line 644 of file ui_ncurses.c.

644 {
645 if (!s->mwin[w])
646 return -1;
647 if (enable)
648 idlok(s->mwin[w], true);
649 else
650 idlok(s->mwin[w], false);
651 return 0;
652}

◆ ui_init()

struct UiRuntime * ui_init ( const UiConfig * cfg)

Definition at line 261 of file ui_ncurses.c.

261 {
262 setlocale(LC_ALL, "");
263 ui = calloc(1, sizeof(*ui));
264 if (!ui)
265 return NULL;
266 char tty_name[MAXLEN];
267 if (cfg && cfg->tty_path) {
268 strncpy(tty_name, cfg->tty_path, sizeof(tty_name) - 1);
269 tty_name[sizeof(tty_name) - 1] = '\0';
270 } else {
271 if (ttyname_r(STDERR_FILENO, tty_name, sizeof(tty_name)) != 0) {
272 free(ui);
273 return NULL;
274 }
275 }
276 ui->tty_fp = fopen(tty_name, "r+");
277 if (!ui->tty_fp) {
278 free(ui);
279 return NULL;
280 }
281 ui->screen = newterm(NULL, ui->tty_fp, ui->tty_fp);
282 if (!ui->screen) {
283 fclose(ui->tty_fp);
284 free(ui);
285 return NULL;
286 }
287 set_term(ui->screen);
288 f_curses_open = true;
289 if (!has_colors() || !can_change_color()) {
290 ui_shutdown();
291 return NULL;
292 }
293 start_color();
294 use_default_colors();
295 cbreak();
296 noecho();
297 keypad(stdscr, TRUE);
298 if (cfg) {
299 ui->mouse_enabled = cfg->enable_mouse;
300 ui->alt_screen = cfg->enable_alt_screen;
301 ui->cursor_visible = cfg->cursor_visible;
302 ui->border_style = cfg->border_style;
303 } else {
304 ui->cursor_visible = false;
305 ui->alt_screen = false;
306 }
307 if (ui->mouse_enabled)
308 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
309 getmaxyx(stdscr, ui->lines, ui->cols);
310 if (cfg) {
311 if (ui->border_style == 'r' || ui->border_style == 'R') {
312 memcpy(bw.str, border_rounded, sizeof(bw.str));
313 } else if (ui->border_style == 's' || ui->border_style == 'S') {
314 memcpy(bw.str, border_single, sizeof(bw.str));
315 } else if (ui->border_style == 'd' || ui->border_style == 'D') {
316 memcpy(bw.str, border_double, sizeof(bw.str));
317 } else if (ui->border_style == 'h' || ui->border_style == 'H') {
318 memcpy(bw.str, border_heavy, sizeof(bw.str));
319 } else if (ui->border_style == 'n' || ui->border_style == 'N') {
320 memcpy(bw.str, border_none, sizeof(bw.str));
321 }
322 }
323 stdsfc = calloc(1, sizeof(*stdsfc));
324 if (!stdsfc)
325 return NULL;
326 stdsfc->runtime = ui;
327 stdsfc->parent = NULL;
328 stdsfc->meta[BOX].lines = (int)ui->lines;
329 stdsfc->meta[BOX].cols = (int)ui->cols;
330 stdsfc->meta[BOX].y = 0;
331 stdsfc->meta[BOX].x = 0;
332 stdsfc->mwin[BOX] = stdscr;
333 stdsfc->mpan[BOX] = new_panel(stdsfc->mwin[BOX]);
334 if (!stdsfc->mpan[BOX]) {
335 free(stdsfc);
336 Perror("new_panel failed for stdsfc->mpan[BOX]");
337 exit(EXIT_FAILURE);
338 }
339 return ui;
340}
bool f_curses_open
Definition sig.c:33
wchar_t * border_rounded
Definition ui_ncurses.c:41
BorderWide bw
Definition ui_ncurses.c:46
UiSurface * stdsfc
Definition ui_ncurses.c:36
wchar_t * border_heavy
Definition ui_ncurses.c:43
wchar_t * border_double
Definition ui_ncurses.c:42
wchar_t * border_single
Definition ui_ncurses.c:40
wchar_t * border_none
Definition ui_ncurses.c:44
#define TRUE
Definition iloan.c:19
int Perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition dwin.c:841
bool cursor_visible
Definition ui_backend.h:145
const char * tty_path
Definition ui_backend.h:147
bool enable_mouse
Definition ui_backend.h:143
char border_style
Definition ui_backend.h:146
bool enable_alt_screen
Definition ui_backend.h:144

◆ ui_init_color()

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

Definition at line 189 of file ui_ncurses.c.

189 {
190 init_extended_color(color, r, g, b);
191 return 0;
192}

Referenced by ui_add_color_rgb().

Here is the caller graph for this function:

◆ ui_init_pair()

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

Definition at line 202 of file ui_ncurses.c.

202 {
203 init_extended_pair(pair, fg, bg);
204 return 0;
205}

◆ ui_keypad()

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

Definition at line 635 of file ui_ncurses.c.

635 {
636 if (!s->mwin[w])
637 return -1;
638 if (enable)
639 keypad(s->mwin[w], true);
640 else
641 keypad(s->mwin[w], false);
642 return 0;
643}

◆ ui_ncurses_get_screen()

SCREEN * ui_ncurses_get_screen ( )

Definition at line 821 of file ui_ncurses.c.

821 {
822 if (!ui)
823 return NULL;
824 return ui->screen;
825}

References ui.

◆ ui_ncurses_surface_get_panel()

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

Return the raw NCurses PANEL* underlying a surface.

Parameters
sThe UiSurface to inspect.
wUiSurface window
Returns
The PANEL pointer, or NULL if s is NULL.

Definition at line 833 of file ui_ncurses.c.

833 {
834 if (!s)
835 return NULL;
836 return s->mpan[w];
837}

◆ ui_ncurses_surface_get_win()

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

Return the raw NCurses WINDOW* underlying a surface.

Parameters
sThe UiSurface to inspect.
Returns
The WINDOW pointer, or NULL if s is NULL.

Definition at line 827 of file ui_ncurses.c.

827 {
828 if (!s)
829 return NULL;
830 return s->mwin[w];
831}

◆ ui_pair_content()

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

Definition at line 194 of file ui_ncurses.c.

194 {
195 int _pair = (int)pair;
196 int _fg, _bg;
197 extended_pair_content(_pair, &_fg, &_bg);
198 *fg = (uint)_fg;
199 *bg = (uint)_bg;
200 return 0;
201}

Referenced by parse_ansi_str(), and ui_add_pair().

Here is the caller graph for this function:

◆ ui_pair_from_hex()

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

Definition at line 230 of file ui_ncurses.c.

230 {
231 RGB rgb;
232 sscanf(fg, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
233 int f_idx = ui_add_color_rgb(&rgb);
234 sscanf(bg, "#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
235 int b_idx = ui_add_color_rgb(&rgb);
236 return ui_add_pair(f_idx, b_idx);
237}

References ui_add_color_rgb(), and ui_add_pair().

Here is the call graph for this function:

◆ ui_render()

void ui_render ( )

Definition at line 800 of file ui_ncurses.c.

800 {
801 update_panels();
802 doupdate();
803}

◆ ui_resume()

int ui_resume ( )

Definition at line 403 of file ui_ncurses.c.

403 {
404 reset_prog_mode();
405 update_panels();
406 doupdate();
407 return 0;
408}

◆ ui_scrollok()

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

Definition at line 626 of file ui_ncurses.c.

626 {
627 if (!s->mwin[w])
628 return -1;
629 if (enable)
630 scrollok(s->mwin[w], true);
631 else
632 scrollok(s->mwin[w], false);
633 return 0;
634}

◆ ui_setcchar()

int ui_setcchar ( cchar_t * wch,
const wchar_t * wc,
const attr_t attrs,
short pair,
const void * opts )

Definition at line 792 of file ui_ncurses.c.

792 {
793 (void)opts;
794 return setcchar(wch, wc, attrs, pair, NULL);
795}

Referenced by fmt_line(), and mbstr_to_cellstr().

Here is the caller graph for this function:

◆ ui_setscrreg()

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

Definition at line 662 of file ui_ncurses.c.

662 {
663 if (!s->mwin[w])
664 return -1;
665 wsetscrreg(s->mwin[w], top, bottom);
666 return 0;
667}

◆ ui_shutdown()

void ui_shutdown ( )

Definition at line 344 of file ui_ncurses.c.

344 {
345 if (ui == NULL)
346 return;
347 for (int i = sfc_ptr; i >= 0; i--) {
348 if (ui_surface[i] != NULL) {
350 ui_surface[i] = NULL;
351 }
352 }
353 if (stdsfc->mpan[0] != NULL) {
354 hide_panel(stdsfc->mpan[0]);
355 del_panel(stdsfc->mpan[0]);
356 stdsfc->mpan[0] = NULL;
357 }
358 f_curses_open = false;
359 endwin();
360 if (ui->screen != NULL) {
361 delscreen(ui->screen);
362 ui->screen = NULL;
363 }
364 if (ui->tty_fp != NULL) {
365 fclose(ui->tty_fp);
366 ui->tty_fp = NULL;
367 }
368 if (stdsfc != NULL) {
369 free(stdsfc);
370 stdsfc = NULL;
371 }
372 if (ui != NULL) {
373 free(ui);
374 ui = NULL;
375 }
376}
UiSurface * ui_surface[UI_SFC_MAX]
Definition ui_ncurses.c:28
int sfc_ptr
Definition nckeys.c:47
void ui_surface_destroy(UiSurface *s)
Definition ui_ncurses.c:377

◆ ui_surface_addpad()

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

Definition at line 509 of file ui_ncurses.c.

509 {
510 s->mwin[w] = newpad(lines, cols);
511 if (s->mwin[w] == nullptr)
512 return -1;
513 s->mwin[view_win] = subpad(s->mwin[PAD], lines, cols, 0, 0);
514 if (s->mwin[view_win] == nullptr)
515 return -1;
516 s->mpan[w] = new_panel(s->mwin[view_win]);
517 immedok(s->mwin[w], true);
518#ifdef DEBUG_UI
519#endif
520 return 0;
521}

◆ ui_surface_addwin()

int ui_surface_addwin ( UiSurface * s,
uint w,
uint p,
uint lines,
uint cols,
uint y,
uint x )

Definition at line 523 of file ui_ncurses.c.

523 {
524 s->mwin[w] = derwin(s->mwin[p], lines, cols, y, x);
525 if (!s->mwin[w]) {
526 free(s);
527 return -1;
528 }
529 s->mpan[w] = new_panel(s->mwin[w]);
530 keypad(s->mwin[w], true);
531 ui_bkgd(s, w, &cell_nt);
532 ui_bkgdset(s, w, &cell_nt);
533 immedok(s->mwin[w], true);
534#ifdef DEBUG_UI
535#endif
536 return 0;
537}
int ui_bkgdset(UiSurface *s, uint w, const UiCell *cell)
Definition ui_ncurses.c:760
int ui_bkgd(UiSurface *s, uint w, const UiCell *cell)
Definition ui_ncurses.c:753
UiCell cell_nt
Definition dwin.c:94

◆ ui_surface_destroy()

void ui_surface_destroy ( UiSurface * s)

Definition at line 377 of file ui_ncurses.c.

377 {
378 if (!s)
379 return;
380 for (int i = SUB_SFC_MAX; i >= 0; i--) {
381 if (s->mpan[i] != NULL) {
382 hide_panel(s->mpan[i]);
383 del_panel(s->mpan[i]);
384 s->mpan[i] = NULL;
385 }
386 }
387 for (int i = SUB_SFC_MAX; i >= 0; i--) {
388 if (s->mwin[i] != NULL) {
389 delwin(s->mwin[i]);
390 s->mwin[i] = NULL;
391 }
392 }
393 if (s != NULL) {
394 free(s);
395 s = NULL;
396 }
397}

◆ ui_surface_hide()

int ui_surface_hide ( UiSurface * s,
uint w )

Definition at line 614 of file ui_ncurses.c.

614 {
615 if (!s)
616 return -1;
617 hide_panel(s->mpan[w]);
618 s->meta[w].hidden = true;
619 return 0;
620}

◆ ui_surface_move()

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

Definition at line 542 of file ui_ncurses.c.

542 {
543 if (!s)
544 return -1;
545 s->meta[w].y = y;
546 s->meta[w].x = x;
547 return move_panel(s->mpan[w], y, x);
548}

◆ ui_surface_new()

UiSurface * ui_surface_new ( uint w,
UiSurface * parent,
uint p,
uint lines,
uint cols,
uint y,
uint x )

Definition at line 414 of file ui_ncurses.c.

414 {
415 if (!ui)
416 return NULL;
417 UiSurface *s = calloc(1, sizeof(*s));
418 if (!s)
419 return NULL;
420 for (int i = 0; i < SUB_SFC_MAX; i++) {
421 s->mwin[i] = NULL;
422 s->mpan[i] = NULL;
423 }
424 s->runtime = ui;
425 s->parent = parent;
426 s->meta[w].lines = lines;
427 s->meta[w].cols = cols;
428 s->meta[w].y = y;
429 s->meta[w].x = x;
430
431 if (parent && parent->mwin[p]) {
432 s->mwin[w] = derwin(parent->mwin[p], lines, cols, y, x);
433 if (!s->mwin[w]) {
434 free(s);
435 return NULL;
436 }
437 } else {
438 s->mwin[w] = newwin(lines, cols, y, x);
439 if (!s->mwin[w]) {
440 free(s);
441 return NULL;
442 }
443 s->mpan[w] = new_panel(s->mwin[w]);
444 if (!s->mpan[w]) {
445 delwin(s->mwin[w]);
446 free(s);
447 return NULL;
448 }
449 }
450 return s;
451}

◆ ui_surface_resize()

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

Definition at line 550 of file ui_ncurses.c.

550 {
551 if (!s)
552 return -1;
553 s->meta[w].lines = lines;
554 s->meta[w].cols = cols;
555 wresize(s->mwin[w], lines + 2, cols + 2);
556 wresize(s->mwin[w + 1], lines, cols);
557 return 0;
558}

◆ ui_surface_show()

int ui_surface_show ( UiSurface * s,
uint w )

Definition at line 598 of file ui_ncurses.c.

598 {
599 if (!s)
600 return -1;
601 show_panel(s->mpan[w]);
602 s->meta[w].hidden = false;
603 return 0;
604 return 0;
605}

◆ ui_suspend()

int ui_suspend ( )

Definition at line 398 of file ui_ncurses.c.

398 {
399 def_prog_mode();
400 endwin();
401 return 0;
402}

◆ ui_top_panel()

int ui_top_panel ( UiSurface * s,
uint w )

Definition at line 607 of file ui_ncurses.c.

607 {
608 if (!s)
609 return -1;
610 top_panel(s->mpan[w]);
611 return 0;
612}

◆ ui_update_panels()

void ui_update_panels ( )

Definition at line 804 of file ui_ncurses.c.

804 {
805 update_panels();
806}

◆ ui_wclear()

int ui_wclear ( UiSurface * s,
uint w )

Definition at line 574 of file ui_ncurses.c.

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

◆ ui_werase()

int ui_werase ( UiSurface * s,
uint w )

Definition at line 560 of file ui_ncurses.c.

560 {
561 if (w == ALLWINS) {
562 for (int i = 1; i < SUB_SFC_MAX; i++) {
563 if (s->mwin[i]) {
564 werase(s->mwin[i]);
565 }
566 }
567 } else {
568 if (s->mwin[w]) {
569 werase(s->mwin[w]);
570 }
571 }
572 return 0;
573}

◆ ui_wmove()

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

Definition at line 732 of file ui_ncurses.c.

732 {
733 if (!s || !s->mwin[w])
734 return -1;
735 return wmove(s->mwin[w], y, x);
736}

◆ ui_wnoutrefresh()

int ui_wnoutrefresh ( UiSurface * s,
uint w )

Definition at line 811 of file ui_ncurses.c.

811 {
812 if (!s)
813 return -1;
814 wnoutrefresh(s->mwin[w]);
815 return 0;
816}

◆ ui_wscrl()

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

Definition at line 741 of file ui_ncurses.c.

741 {
742 if (!s->mwin[w])
743 return -1;
744 wscrl(s->mwin[w], n);
745 ui_render();
746 return 0;
747}
void ui_render()
Definition ui_ncurses.c:800

References ui_render().

Here is the call graph for this function:

Variable Documentation

◆ border_double

wchar_t* border_double = L"═║╠╣╦╩╬╔╗╚╝"

Definition at line 42 of file ui_ncurses.c.

◆ border_heavy

wchar_t* border_heavy = L"━┃┣┫┳┻╋┏┓┗┛"

Definition at line 43 of file ui_ncurses.c.

◆ border_none

wchar_t * border_none = L" "

Definition at line 44 of file ui_ncurses.c.

Referenced by ui_init(), and ui_init().

◆ border_rounded

wchar_t* border_rounded = L"─│├┤┬┴┼╭╮╰╯"

BOX WIDE UNICODE CODEPOINTS

Definition at line 41 of file ui_ncurses.c.

◆ border_single

wchar_t * border_single = L"─│├┤┬┴┼┌┐└┘"

BOX WIDE UNICODE CODEPOINTS

Definition at line 40 of file ui_ncurses.c.

Referenced by ui_init(), and ui_init().

◆ bw

Definition at line 46 of file ui_ncurses.c.

◆ bw_chk

const wchar_t* bw_chk = L"\x2611"

left_angle

Definition at line 59 of file ui_ncurses.c.

◆ bw_da

const wchar_t* bw_da = L"\x2193"

large down arrow

Definition at line 56 of file ui_ncurses.c.

◆ bw_h09

const wchar_t* bw_h09 = L"\x23BD"

horizontal line 9

Definition at line 60 of file ui_ncurses.c.

◆ bw_la

const wchar_t* bw_la = L"\x2190"

large left arrow

Definition at line 54 of file ui_ncurses.c.

◆ bw_lan

const wchar_t* bw_lan = L"\x276E"

left_angle

Definition at line 58 of file ui_ncurses.c.

◆ bw_ra

const wchar_t* bw_ra = L"\x2192"

large right arrow

Definition at line 53 of file ui_ncurses.c.

◆ bw_ran

const wchar_t* bw_ran = L"\x276F"

right_angle

Definition at line 57 of file ui_ncurses.c.

◆ bw_rbl

const wchar_t* bw_rbl = L"\x2570"

rounded bottom left

Definition at line 50 of file ui_ncurses.c.

◆ bw_rbr

const wchar_t* bw_rbr = L"\x256f"

rounded bottom right

Definition at line 51 of file ui_ncurses.c.

◆ bw_rtl

const wchar_t* bw_rtl = L"\x256d"

rounded top left

Definition at line 48 of file ui_ncurses.c.

◆ bw_rtr

const wchar_t* bw_rtr = L"\x256e"

rounded top right

Definition at line 49 of file ui_ncurses.c.

◆ bw_sp

const wchar_t* bw_sp = L"\x20"

space

Definition at line 52 of file ui_ncurses.c.

◆ bw_ua

const wchar_t* bw_ua = L"\x2191"

large up arrow

Definition at line 55 of file ui_ncurses.c.

◆ sfc_ptr

int sfc_ptr = -1

Definition at line 31 of file ui_ncurses.c.

◆ std_color

STDRGB std_color[]
Initial value:
= {
{0, 0, 0},
{128, 0, 0},
{0, 128, 0},
{128, 128, 0},
{0, 0, 128},
{128, 0, 128},
{0, 128, 128},
{192, 192, 192},
{128, 128, 128},
{255, 0, 0},
{0, 255, 0},
{255, 255, 0},
{0, 0, 255},
{255, 0, 255},
{0, 255, 255},
{255, 255, 255}}

Definition at line 62 of file ui_ncurses.c.

62 {
63 {0, 0, 0},
64 {128, 0, 0},
65 {0, 128, 0},
66 {128, 128, 0},
67 {0, 0, 128},
68 {128, 0, 128},
69 {0, 128, 128},
70 {192, 192, 192},
71 {128, 128, 128},
72 {255, 0, 0},
73 {0, 255, 0},
74 {255, 255, 0},
75 {0, 0, 255},
76 {255, 0, 255},
77 {0, 255, 255},
78 {255, 255, 255}};

◆ stdsfc

UiSurface* stdsfc

Definition at line 36 of file ui_ncurses.c.

◆ ui

UiRuntime* ui = NULL

Definition at line 27 of file ui_ncurses.c.

◆ ui_color_cnt

uint ui_color_cnt = 0

Definition at line 33 of file ui_ncurses.c.

◆ ui_pair_cnt

uint ui_pair_cnt = 0

Definition at line 34 of file ui_ncurses.c.

◆ ui_surface

UiSurface* ui_surface[UI_SFC_MAX]

Definition at line 28 of file ui_ncurses.c.

◆ win_ptr

int win_ptr = -1

Definition at line 30 of file ui_ncurses.c.