C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses_internal.h
Go to the documentation of this file.
1#ifndef UI_NCURSES_INTERNAL_H
2#define UI_NCURSES_INTERNAL_H 1
3
4/** @file ui_ncurses_internal.h
5 @ingroup ui_ncurses
6 @brief Internal header for the NCurses UI backend.
7
8 This header defines the concrete (non-opaque) layouts of UiRuntime and
9 UiSurface for the NCurses backend. It must only be included by NCurses
10 backend source files — never by application code.
11*/
12
13#define _XOPEN_SOURCE_EXTENDED 1
14#define _GNU_SOURCE
15#define NCURSES_WIDECHAR 1
16#include <ncursesw/ncurses.h>
17#include <ncursesw/panel.h>
18#include <stdbool.h>
19#include <stdint.h>
20
21#define XLEN 128
22#define MAXLEN 256
23#define MAXSFC 30
24
25#define KEY_F01 KEY_F(1)
26#define KEY_F02 KEY_F(2)
27#define KEY_F03 KEY_F(3)
28#define KEY_F04 KEY_F(4)
29#define KEY_F05 KEY_F(5)
30#define KEY_F06 KEY_F(6)
31#define KEY_F07 KEY_F(7)
32#define KEY_F08 KEY_F(8)
33#define KEY_F09 KEY_F(9)
34#define KEY_F10 KEY_F(10)
35#define KEY_F11 KEY_F(11)
36#define KEY_F12 KEY_F(12)
37
38/** @struct UiRuntime
39 @ingroup ui_ncurses
40 @brief Runtime state for the NCurses UI backend.
41
42 Holds all NCurses session resources so that multiple independent sessions
43 can coexist and the codebase is not dependent on global variables.
44 The legacy globals @c screen and @c tty_fp in @c dwin.c are updated by
45 ui_init() / ui_shutdown() for backward compatibility with code that has
46 not yet been migrated to the UAL API.
47*/
48struct UiRuntime {
49 SCREEN *screen; /**< NCurses SCREEN created by newterm() */
50 FILE *tty_fp; /**< TTY file handle opened by ui_init() */
51 bool mouse_enabled;
52 bool alt_screen;
53 bool cursor_visible;
54 char border_style;
55 uint lines;
56 uint cols;
57 PANEL *panel_main;
58};
59
60#define SFC_MAX 30
61
62enum {
71};
72
73struct UiSurfaceMeta {
74 unsigned int y;
75 unsigned int x;
76 unsigned int lines;
77 unsigned int cols;
78 bool hidden;
79 char name[XLEN];
80 char title[XLEN];
81};
82
83struct UiSurface {
84 union {
85 struct { // DEPRECATED - will be removed
86 PANEL *box_pan;
87 PANEL *win_pan;
88 PANEL *win2_pan;
89 PANEL *lnno_pan;
90 PANEL *cmdln_pan;
91 PANEL *pad_pan;
92 PANEL *plane1_pan;
93 PANEL *plane2_pan;
94 }; // END DEPRECATION
95 struct {
96 PANEL *mpan[SUB_SFC_MAX];
97 };
98 };
99 union {
100 struct { // DEPRECATED - will be removed
101 WINDOW *box;
102 WINDOW *win;
103 WINDOW *win2;
104 WINDOW *lnno;
105 WINDOW *cmdln;
106 WINDOW *pad;
107 WINDOW *plane1;
108 WINDOW *plane2;
109 }; // END DEPRECATION
110 struct {
111 WINDOW *mwin[SUB_SFC_MAX];
112 };
113 };
114 struct UiRuntime *runtime;
115 struct UiSurfaceMeta meta[SUB_SFC_MAX];
116 struct UiSurface *parent;
117 int sfc_idx;
118 int sub_cnt;
119};
120
121typedef struct {
122 int32_t b, g, r, a;
123} RGB;
124
125typedef struct {
126 int fg, bg;
127} UiColorPair;
128
129union UiChannels {
130 struct {
131 union {
132 struct {
133 uint8_t b_b, b_g, b_r, b_a;
134 };
135 uint32_t bargb;
136 };
137 union {
138 struct {
139 uint8_t f_b, f_g, f_r, f_a;
140 };
141 uint32_t fargb;
142 };
143 };
144 uint64_t fb;
145};
146
147struct UiStyle {
148 wchar_t wstr[5];
150 short cp;
151};
152
153#define UI_MASK WA_ATTRIBUTES
154#define UI_NORMAL WA_NORMAL
155#define UI_STANDOUT WA_STANDOUT
156#define UI_UNDERLINE WA_UNDERLINE
157#define UI_REVERSE WA_REVERSE
158#define UI_BLINK WA_BLINK
159#define UI_DIM WA_DIM
160#define UI_BOLD WA_BOLD
161#define UI_ALTCHARSET WA_ALTCHARSET
162#define UI_INVIS WA_INVIS
163#define UI_PROTECT WA_PROTECT
164#define UI_UNDERCURL WA_DIM
165#define UI_STRUCK WA_INVIS
166#define UI_NONE WA_NORMAL
167#define UI_ITALIC WA_ITALIC
168
169/** @struct UiSplitSurface
170 @brief Split surface containing multiple child surfaces.
171 @verbatim
172
173 split_y > 0 && split_x > 0: 4 quadrants
174 0: top-left, 1: top-right, 2: bottom-left, 3: bottom-right
175
176 split_y > 0 && split_x == 0: 2 lines
177 0: top, 1: bottom
178
179 split_y == 0 && split_x > 0: 2 cols
180 0: left, 1: right
181
182 @endverbatim
183 */
184
185/** @struct UiSurface
186 @ingroup ui_ncurses
187 @brief A drawable surface in the NCurses backend.
188 Wraps an NCurses WINDOW and its associated PANEL.
189*/
190/*
191 This configuration is designed to make tiling and splitting surfaces easier
192 to manage. One obvious application is to split the view screen into multiple
193 panes when selecting photos from a gallery. I only added a four quadrants to
194 start with, but it can be expanded to more panes in the future. The split_y and split_x values determine how the surface is divided. If both are greater than
195 zero, the surface is split into four quadrants. If only one of them is greater than zero, the surface is split into two lines or two columns. The mwin array holds the child windows for each quadrant. The parent pointer allows for hierarchical relationships between surfaces, enabling complex layouts. The hidden flag can be used to control the visibility of the surface, and the name and title fields provide identifiers for the surface. The lines and cols fields store the dimensions of the surface, while the x and y fields store its position on the screen.
196*/
197
198// UiSurface is a structure that represents a drawable surface in the NCurses backend.
199// It contains various fields that define the properties and behavior of the
200// surface, including its associated PANELs and WINDOWs, runtime state,
201// position, dimensions, visibility, and identifiers. The structure also
202// supports hierarchical relationships between surfaces, allowing for complex
203// layouts and tiling of multiple panes.
204//
205// The unions create named fields that duplicate the mwin and mpan arrays. This
206// was merely a convenience to facilitate easier porting of legacy code. The
207// SubSurface enum accomplishes the same thing, but the upper-case letters
208// improve comprehennsibility when scan-reading code.
209//
210// Once the named fields have served their purpose they will most likely be
211// removed in the future as they are redundant and can be confusing.
212//
213typedef cchar_t UiCell;
214/* Internal style helpers */
215int ui_ncurses_style_apply(struct UiSurface *s, uint w, const struct UiStyle *style);
220struct UiStyle *ui_style_new(void);
221int ui_pair_from_hex(const char *fg, const char *bg);
222struct UiStyle *ui_style_copy(const struct UiStyle *src);
223int ui_wadd_cell(struct UiSurface *s, uint w, UiCell *cc);
224int ui_mvwadd_cell(struct UiSurface *s, uint w, uint y, uint x, UiCell *cc);
225int ui_wadd_cellstr(struct UiSurface *s, uint w, UiCell *cmplx_buf);
226int ui_wadd_cellnstr(struct UiSurface *s, uint w, UiCell *cmplx_buf, uint n);
227int ui_mvwadd_cellstr(struct UiSurface *s, uint w, uint y, uint x, UiCell *cmplx_buf);
228int ui_mvwadd_cellnstr(struct UiSurface *s, uint w, uint y, uint x, UiCell *cmplx_buf, uint n);
229
230extern struct UiSurface *stdsfc;
231
232#endif
bool f_curses_open
Definition sig.c:33
const wchar_t * bw_sp
Definition ui_ncurses.c:52
void ui_render()
Definition ui_ncurses.c:800
int ui_cursor_move(UiSurface *s, uint w, uint y, uint x)
Definition ui_ncurses.c:727
wchar_t * border_rounded
Definition ui_ncurses.c:41
UiRuntime * ui_init(const UiConfig *config)
Definition ui_ncurses.c:261
void ui_getyx(UiSurface *s, uint w, uint *lines, uint *cols)
Definition ui_ncurses.c:668
int ui_surface_hide(UiSurface *s, uint w)
Definition ui_ncurses.c:614
const wchar_t * bw_ran
Definition ui_ncurses.c:57
int ui_bkgdset(UiSurface *s, uint w, const UiCell *cell)
Definition ui_ncurses.c:760
UiSurface * ui_surface_new(uint w, UiSurface *parent, uint p, uint lines, uint cols, uint y, uint x)
Definition ui_ncurses.c:414
int ui_setscrreg(UiSurface *s, uint w, uint top, uint bottom)
Definition ui_ncurses.c:662
int ui_scrollok(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:626
const wchar_t * bw_rbr
Definition ui_ncurses.c:51
int ui_add_color_rgb(RGB *rgb)
Definition ui_ncurses.c:138
const wchar_t * bw_ua
Definition ui_ncurses.c:55
int ui_wclear(UiSurface *s, uint w)
Definition ui_ncurses.c:574
const wchar_t * bw_chk
Definition ui_ncurses.c:59
BorderWide bw
Definition ui_ncurses.c:46
uint16_t attr_t
Definition ui_backend.h:256
int ui_bkgd(UiSurface *s, uint w, const UiCell *cell)
Definition ui_ncurses.c:753
#define UI_SFC_MAX
Definition ui_backend.h:24
const wchar_t * bw_rtl
Definition ui_ncurses.c:48
const wchar_t * bw_rtr
Definition ui_ncurses.c:49
const wchar_t * bw_lan
Definition ui_ncurses.c:58
#define UI_PAIRS
Definition ui_backend.h:40
int ui_suspend()
Definition ui_ncurses.c:398
void ui_surface_destroy(UiSurface *s)
Definition ui_ncurses.c:377
int ui_surface_move(UiSurface *s, uint w, uint y, uint x)
Definition ui_ncurses.c:542
int ui_idlok(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:644
const wchar_t * bw_ra
Definition ui_ncurses.c:53
void ui_update_panels()
Definition ui_ncurses.c:804
#define ERR
Definition ui_backend.h:249
STDRGB std_color[]
Definition ui_ncurses.c:62
int ui_wmove(UiSurface *s, uint w, uint y, uint x)
Definition ui_ncurses.c:732
UiSurface * ui_surface[UI_SFC_MAX]
Definition ui_ncurses.c:28
const wchar_t * bw_h09
Definition ui_ncurses.c:60
uint32_t ui_get_color(uint16_t color_idx)
Definition ui_ncurses.c:164
int ui_werase(UiSurface *s, uint w)
Definition ui_ncurses.c:560
void ui_get_screen_size(uint *lines, uint *cols)
Definition ui_ncurses.c:692
void fast_exit(UiSurface *s)
Definition ui_ncurses.c:490
int ui_erase()
Definition ui_ncurses.c:590
UiRuntime * ui
RGB ui_hex_to_rgb(char *s)
Definition ui_ncurses.c:174
void ui_shutdown()
Definition ui_ncurses.c:344
int ui_wscrl(UiSurface *s, uint w, int rows)
int win_ptr
Definition ui_ncurses.c:30
UiSurface * stdsfc
Definition ui_ncurses.c:36
int sfc_ptr
Definition nckeys.c:47
UiCell ui_cell_from_ucp(const wchar_t *ucp, const uint32_t *fg, const uint32_t *bg)
Definition ui_ncurses.c:239
int ui_curs_set(int visibility)
Definition ui_ncurses.c:737
int ui_top_panel(UiSurface *s, uint w)
Definition ui_ncurses.c:607
int ui_resume()
Definition ui_ncurses.c:403
uint ui_pair_cnt
Definition ui_ncurses.c:34
int ui_wnoutrefresh(UiSurface *s, uint w)
Definition ui_ncurses.c:811
struct nccell UiCell
Definition ui_backend.h:250
int ui_surface_resize(UiSurface *s, uint w, uint lines, uint cols)
Definition ui_ncurses.c:550
const wchar_t * bw_la
Definition ui_ncurses.c:54
uint ui_color_cnt
Definition ui_ncurses.c:33
void ui_endwin()
Definition ui_ncurses.c:341
wchar_t * border_heavy
Definition ui_ncurses.c:43
int ui_surface_show(UiSurface *s, uint w)
Definition ui_ncurses.c:598
int ui_clear()
Definition ui_ncurses.c:594
UiSurface * ui_box_surface_new(UiSurface *parent, uint p, uint lines, uint cols, uint y, uint x, char *title)
Definition ui_ncurses.c:453
int ui_keypad(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:635
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 surfa...
Definition ui_ncurses.c:713
const wchar_t * bw_da
Definition ui_ncurses.c:56
wchar_t * border_double
Definition ui_ncurses.c:42
int ui_idcok(UiSurface *s, uint w, bool enable)
Definition ui_ncurses.c:653
#define UI_COLORS
Definition ui_backend.h:39
void ui_get_caps(UiCaps *caps)
Query the capabilities of the active backend.
Definition ui_ncurses.c:88
int ui_doupdate()
Definition ui_ncurses.c:807
@ UI_BACKEND_NCURSES
Definition ui_backend.h:156
UiBackend ui_get_backend()
Return which backend is powering this runtime.
Definition ui_ncurses.c:84
int ui_surface_addpad(UiSurface *s, uint w, uint view_win, uint lines, uint cols)
Definition ui_ncurses.c:509
void ui_getmaxyx(UiSurface *s, uint w, uint *lines, uint *cols)
Definition ui_ncurses.c:676
const wchar_t * bw_rbl
Definition ui_ncurses.c:50
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 surfa...
Definition ui_ncurses.c:703
#define ALLWINS
Definition ui_backend.h:92
int ui_surface_addwin(UiSurface *s, uint w, uint p, uint lines, uint cols, uint y, uint x)
Definition ui_ncurses.c:523
int ui_chg_color(uint16_t color_idx, uint32_t *color)
Definition ui_ncurses.c:206
int ui_mvwadd_cell(struct UiSurface *s, uint w, uint y, uint x, UiCell *cc)
cchar_t UiCell
struct UiStyle * ui_style_new(void)
int ui_ncurses_color_pair_from_style(const struct UiStyle *style)
int ui_wadd_cell(struct UiSurface *s, uint w, UiCell *cc)
int ui_mvwadd_cellstr(struct UiSurface *s, uint w, uint y, uint x, UiCell *cmplx_buf)
int ui_ncurses_style_apply(struct UiSurface *s, uint w, const struct UiStyle *style)
UiCell ui_style_to_cch(const struct UiStyle *)
#define XLEN
void ui_style_destroy(struct UiStyle *)
struct UiStyle * ui_style_from_cch(const UiCell *)
int ui_wadd_cellstr(struct UiSurface *s, uint w, UiCell *cmplx_buf)
struct UiStyle * ui_style_copy(const struct UiStyle *src)
int ui_mvwadd_cellnstr(struct UiSurface *s, uint w, uint y, uint x, UiCell *cmplx_buf, uint n)
int ui_wadd_cellnstr(struct UiSurface *s, uint w, UiCell *cmplx_buf, uint n)
#define WA_NORMAL
#define WA_ATTRIBUTES
#define WA_BLINK
#define WA_PROTECT
#define WA_ITALIC
#define WA_DIM
#define WA_INVIS
#define WA_BOLD
#define WA_REVERSE
#define WA_STANDOUT
#define WA_UNDERLINE
#define WA_ALTCHARSET
PANEL * ui_ncurses_surface_get_panel(const UiSurface *s, uint w)
Return the raw NCurses PANEL* underlying a surface.
Definition ui_ncurses.c:833
int ui_chg_pair(uint pair, uint fg, uint bg)
Definition ui_ncurses.c:132
int ui_pair_content(uint pair, uint *fg, uint *bg)
Definition ui_ncurses.c:194
SCREEN * ui_ncurses_get_screen()
Definition ui_ncurses.c:821
int ui_getmaxx(UiSurface *s, uint w)
Definition ui_ncurses.c:686
int ui_bkgrndset(UiSurface *s, uint w, const UiCell *cell)
Definition ui_ncurses.c:774
int ui_init_color(uint color, uint r, uint g, uint b)
Definition ui_ncurses.c:189
int ui_init_pair(uint pair, uint fg, uint bg)
Definition ui_ncurses.c:202
uint ui_add_pair(uint fg, uint bg)
Definition ui_ncurses.c:105
int ui_getcchar(const UiCell *uc, wchar_t *wstr, attr_t *attrs, uint16_t *pair, void *opts)
Definition ui_ncurses.c:784
int ui_color_content(uint color, uint *r, uint *g, uint *b)
Definition ui_ncurses.c:179
int ui_pair_from_hex(const char *fg, const char *bg)
Definition ui_ncurses.c:230
int ui_getmaxy(UiSurface *s, uint w)
Definition ui_ncurses.c:681
int ui_bkgrnd(UiSurface *s, uint w, const UiCell *cell)
Definition ui_ncurses.c:767
wchar_t * border_single
Definition ui_ncurses.c:40
int ui_setcchar(cchar_t *wch, const wchar_t *wc, const attr_t attrs, short pair, const void *opts)
Definition ui_ncurses.c:792
wchar_t * border_none
Definition ui_ncurses.c:44
WINDOW * ui_ncurses_surface_get_win(const UiSurface *s, uint w)
Return the raw NCurses WINDOW* underlying a surface.
Definition ui_ncurses.c:827
#define TRUE
Definition iloan.c:19
#define MAXLEN
Definition curskeys.c:15
UiCell cell_nt
Definition dwin.c:94
char em1[MAXLEN]
Definition dwin.c:143
char em2[MAXLEN]
Definition dwin.c:144
int border_draw(UiSurface *sfc)
Definition dwin.c:581
void apply_gamma(RGB *rgb)
Apply gamma correction to RGB color.
Definition dwin.c:307
char em0[MAXLEN]
Definition dwin.c:142
int border_title(UiSurface *sfc, char *title)
Draw a box with a title around the specified window.
Definition dwin.c:676
int Perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition dwin.c:841
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
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
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
wchar_t str[11]
Definition ui_backend.h:185
uint8_t g
Definition ui_backend.h:261
uint8_t r
Definition ui_backend.h:260
uint8_t b
Definition ui_backend.h:262
Runtime state for the NotCurses backend.
unsigned int cols
unsigned int lines
A drawable surface in the NotCurses backend.
struct UiSurface * parent
struct UiRuntime * runtime
struct UiSurfaceMeta meta[SUB_SFC_MAX]
unsigned int r
Definition nckeys.c:32
unsigned int b
Definition nckeys.c:32
unsigned int g
Definition nckeys.c:32
wchar_t wstr[5]