2
3
4
5
6
7
8
9
10
11
13#define _XOPEN_SOURCE_EXTENDED 1
18#ifdef UAL_LEGACY_COMPAT
19#include "ui_ncurses_compat.h"
28
29
30
31
32
33
34
35
36
37
49
50
52
53
54
59
60
61
65 memset(caps, 0,
sizeof(*caps));
79
80
82
83
84
85
86
89 sscanf(fg,
"#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
91 sscanf(bg,
"#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
96
97
98
99
100
101
105 rgb.r = (*fg >> 16) & 0xff;
106 rgb.g = (*fg >> 8) & 0xff;
109 rgb.r = (*bg >> 16) & 0xff;
110 rgb.g = (*bg >> 8) & 0xff;
114 wchar_t wstr[2] = {L'\0', L'\0'};
117 setcchar(&cc, wstr,
WA_NORMAL, cp,
nullptr);
122
123
125
126
127
128
129
130struct UiRuntime *
ui_init(
const UiConfig *cfg, SIO *sio) {
131 setlocale(LC_ALL,
"en_US.UTF-8");
132 ui = calloc(1,
sizeof(*
ui));
136 if (ttyname_r(STDERR_FILENO, tty_name,
sizeof(tty_name)) != 0) {
140 ui->tty_fp = fopen(tty_name,
"r+");
145 ui->tty_fd = fileno(
ui->tty_fp);
148 ui_log(INFO,
_(
"ui_init: using tty: %s"), tty_name);
149 ui->screen = newterm(NULL,
ui->tty_fp,
ui->tty_fp);
156 set_term(
ui->screen);
157 if (!has_colors() || !can_change_color()) {
162 use_default_colors();
165 keypad(stdscr,
TRUE);
166 if (
ui->mouse_enabled)
167 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
168 getmaxyx(stdscr,
ui->lines,
ui->cols);
175 stdsfc->meta[BOX].lines = (
int)
ui->lines;
176 stdsfc->meta[BOX].cols = (
int)
ui->cols;
179 stdsfc->mwin[BOX] = stdscr;
183 ui_log(ERROR,
_(
"new_panel failed for stdsfc->mpan[BOX]"));
188 if (!
ui->border_style)
189 ui->border_style = UI_BORDER_ROUNDED;
191 switch (
ui->border_style) {
192 case UI_BORDER_SINGLE:
195 case UI_BORDER_DOUBLE:
198 case UI_BORDER_ROUNDED:
201 case UI_BORDER_HEAVY:
213 ui_log(INFO,
_(
"ui_init: stdsfc->mwin[BOX]: %p, stdsfc->mpan[BOX]: %p"), (
void *)
stdsfc->mwin[BOX]
, (
void *)
stdsfc->mpan[BOX]);
220
221
222
223
224
225
226
227
228
229
230UiSurface *
ui_surface_new(ss_t w, UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x) {
239 UiSurface *s = calloc(1,
sizeof(*s));
241 ui_log(ERROR,
_(
"calloc failed for UiSurface"));
250 s->meta[w].lines = lines;
251 s->meta[w].cols = cols;
254 UiLogLevel level = INFO;
255 if (parent && parent->mwin[p]) {
256 s->mwin[w] = derwin(parent->mwin[p], lines, cols, y, x);
260 ui_log(level,
"%s = derwin(%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w]
, ss_n[p]
, lines
, cols
, y
, x);
264 s->mwin[w] = newwin(lines, cols, y, x);
268 ui_log(level,
"%s = newwin(lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w]
, lines
, cols
, y
, x);
271 s->mpan[w] = new_panel(s->mwin[w]);
275 ui_log(level,
"%s = new_panel(lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w]
, lines
, cols
, y
, x);
283
284
285
286
287
288
289
290
291
292
293UiSurface *
ui_surface_box(UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x,
const char *wtitle) {
302 UiSurface *s = calloc(1,
sizeof(*s));
304 ui_log(ERROR,
_(
"calloc failed for UiSurface"));
312 s->meta[BOX].lines = lines;
313 s->meta[BOX].cols = cols;
314 UiLogLevel level = INFO;
315 if (parent && parent->mwin[p]) {
316 s->mwin[BOX] = derwin(parent->mwin[p], lines, cols, y, x);
320 ui_log(level,
"%s = derwin(%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w]
, ss_n[p]
, lines
, cols
, y
, x);
324 s->mwin[BOX] = newwin(lines, cols, y, x);
328 ui_log(level,
"%s = newwin(lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w]
, lines
, cols
, y
, x);
331 s->mpan[BOX] = new_panel(s->mwin[BOX]);
333 delwin(s->mwin[BOX]);
335 ui_log(level,
"%s = new_panel(lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w]
, lines
, cols
, y
, x);
343 immedok(s->mwin[BOX],
true);
348
349
350
356 hide_panel(s->mpan[i]);
357 del_panel(s->mpan[i]);
371
372
373
374
375
376
377
378
379
380
381int ui_surface_addpad(UiSurface *s, ss_t pad, ss_t p, uint lines, uint cols, uint begy, uint begx,
bool hidden) {
386 UiLogLevel level = INFO;
387 s->mwin[pad] = newpad(lines, cols);
388 if (s->mwin[pad] ==
nullptr) {
390 ui_log(level,
"%s = newpad(lines=%d, cols=%d, hidden=%s) failed", ss_n[pad]
, lines
, cols
, hidden ?
"true" :
"false");
393 ui_log(level,
"%s = newpad(lines=%d, cols=%d) success", ss_n[pad]
, lines
, cols);
395 s->mwin[v] = subpad(s->mwin[PAD], lines, cols, 0, 0);
396 if (s->mwin[v] ==
nullptr) {
398 ui_log(level,
"subpad(%s, %s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w], ss_n[v], lines, cols, 0, 0);
401 ui_log(level,
"subpad(%s, %s, lines=%d, cols=%d) success", ss_n[w],
402 ss_n[v], lines, cols);
403 s->mpan[w] = new_panel(s->mwin[v]);
404 top_panel(s->mpan[w]);
406 immedok(s->mwin[pad],
true);
407 scrollok(s->mwin[pad],
true);
413
414
415
416
417
418
419
420
421
422
424 UiLogLevel level = INFO;
425 s->mwin[w] = derwin(s->mwin[p], lines, cols, y, x);
428 ui_log(level,
"%s = derwin(%s, lines=%d, cols=%d, begy=%d, begx=%d) failed", ss_n[w]
, ss_n[p]
, lines
, cols
, y
, x);
432 ui_log(level,
"%s = derwin(%s, lines=%d, cols=%d, begy=%d, begx=%d) success", ss_n[w]
, ss_n[p]
, lines
, cols
, y
, x);
433 s->mpan[w] = new_panel(s->mwin[w]);
434 keypad(s->mwin[w],
true);
435 immedok(s->mwin[w],
true);
442
448
452 ui_log(INFO,
_(
"ui_shutdown in progress..."));
458 ui_log(INFO,
_(
"surface destroy: %d"),
sfc_ptr);
461 if (
stdsfc->mpan[0] != NULL) {
462 ui_log(INFO,
_(
"calling del_panel(stdsfc->mpan[BOX])"));
463 hide_panel(
stdsfc->mpan[0]);
464 del_panel(
stdsfc->mpan[0]);
467 ui_log(INFO,
_(
"calling endwin()"));
470 if (
ui->screen != NULL) {
471 ui_log(INFO,
_(
"calling delscreen(ui->screen)"));
472 delscreen(
ui->screen);
475 if (
ui->tty_fp != NULL) {
476 ui_log(INFO,
_(
"closing tty_fp"));
481 ui_log(INFO,
_(
"freeing stdsfc"));
490 ui_log(INFO,
_(
"freeing ui"));
496
497
498
503 if (s->mpan[i] != NULL) {
504 hide_panel(s->mpan[i]);
506 del_panel(s->mpan[i]);
511 if (s->mwin[i] != NULL) {
522
523
524
531
532
533
545
546
547
548
549
550
551
557 return move_panel(s->mpan[w], y, x);
560
561
562
563
564
565
566
570 s->meta[w].lines = lines;
571 s->meta[w].cols = cols;
572 wresize(s->mwin[w], lines + 2, cols + 2);
573 wresize(s->mwin[w + 1], lines, cols);
577
578
579
580
581
597
598
599
600
601
620
621
622
628
629
630
636
637
638
639
640
644 show_panel(s->mpan[w]);
645 s->meta[w].hidden =
false;
650
651
652
653
654
658 top_panel(s->mpan[w]);
662
663
664
665
666
670 hide_panel(s->mpan[w]);
671 s->meta[w].hidden =
true;
678
679
680
681
682
683
684
685int ui_wmove(UiSurface *s, ss_t w, uint y, uint x) {
686 if (!s || !s->mwin[w])
688 return wmove(s->mwin[w], y, x);
691
692
693
694
695
696
697
699 if (!s || !s->mwin[w])
701 return wmove(s->mwin[w], y, x);
707void ui_getyx(UiSurface *s, ss_t w, uint *lines, uint *cols) {
711 getyx(s->mwin[w], _lines, _cols);
712 *lines = (uint)(_lines);
713 *cols = (uint)(_cols);
716
717
718
719
720
721
725 getmaxyx(s->mwin[w], *lines, *cols);
728
729
730
731
732
736 return (
int)(getmaxy(s->mwin[w]));
739
740
741
742
743
747 return (
int)(getmaxx(s->mwin[w]));
750
751
752
753
757 getmaxyx(stdscr,
ui->lines,
ui->cols);
764
765
766
767
768
769
773 wscrl(s->mwin[w], n);
784 immedok(s->mwin[w],
true);
786 immedok(s->mwin[w],
false);
790
791
792
793
794
795
800 scrollok(s->mwin[w],
true);
802 scrollok(s->mwin[w],
false);
806
807
808
809
810
811
816 keypad(s->mwin[w],
true);
818 keypad(s->mwin[w],
false);
822
823
824
825
826
827
832 idlok(s->mwin[w],
true);
834 idlok(s->mwin[w],
false);
838
839
840
841
842
843
848 idcok(s->mwin[w],
true);
850 idcok(s->mwin[w],
false);
854
855
856
857
858
859
860
864 wsetscrreg(s->mwin[w], top, bottom);
874
875
876
877
878
879
880
882 curs_set(visibility);
888
889
890
891
892
893
894
895
899 wmove(s->mwin[w], y, x);
900 ui->cursor_visible = visible;
901 curs_set(visible ? 2 : 0);
905
906
907
908
909
910
916 ui->cursor_visible = visible;
917 curs_set(visible ? 1 : 0);
921
922
923int ui_prefresh(UiSurface *s, ss_t w,
int pminrow,
int pmincol,
int sminrow,
int smincol,
int smaxrow,
int smaxcol) {
926 return prefresh(s->mwin[w], pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol);
930
936
941
947
948
950
951
952
953
954
958 wnoutrefresh(s->mwin[w]);
968
969
970
971
972
973
974int ui_bkgd(UiSurface *s, ss_t w,
const UiCell *cell) {
977 wbkgrnd(s->mwin[w], cell);
982
983
984
985
986
987
991 wbkgrndset(s->mwin[w], cell);
996
997
998
999
1000
1001
1005 wbkgrnd(s->mwin[w], cell);
1010
1011
1012
1013
1014
1015
1019 wbkgrndset(s->mwin[w], cell);
1023
1024
1026
1027
1028
1029
1030
1031
1032
1033
1034int ui_getcchar(
const UiCell *cell, wchar_t *wstr, attr_t *attrs,
short *pair,
const void *opts) {
1036 getcchar(cell, wstr, attrs, pair,
nullptr);
1040
1041
1042
1043
1044
1045
1046
1047
1048int ui_setcchar(UiCell *cell,
const wchar_t *wstr,
const attr_t attrs,
short pair,
const void *opts) {
1050 return setcchar(cell, wstr, attrs, pair, NULL);
1053
1054
1056
1057
1058
1059
1060
1067 if (pfg == fg && pbg == bg)
1075 return (EXIT_FAILURE);
1077 rc = init_extended_pair(i, fg, bg);
1083 return (EXIT_FAILURE);
1089
1090
1091
1092
1093
1094
1098 init_extended_pair(pair, fg, bg);
1103
1104
1105
1106
1108 if (rgb->r == rgb->g && rgb->g == rgb->b && rgb->b == 0)
1111 if (rgb->r > 255 || rgb->g > 255 || rgb->b > 255) {
1113 ui_log(ERROR,
"%s",
em0);
1115 ui_log(ERROR,
"%s",
em0);
1122 rgb->r = (rgb->r * 1000) / 255;
1123 rgb->g = (rgb->g * 1000) / 255;
1124 rgb->b = (rgb->b * 1000) / 255;
1126 extended_color_content(i, &tmp.r, &tmp.g, &tmp.b);
1127 if (rgb->r == tmp.r && rgb->g == tmp.g && rgb->b == tmp.b)
1136 init_extended_color(i, rgb->r, rgb->g, rgb->b);
1144
1145
1146
1147
1152 extended_color_content(color_idx, &rgb.r, &rgb.g, &rgb.b);
1153 rgb.r = (rgb.r * 255) / 1000;
1154 rgb.g = (rgb.g * 255) / 1000;
1155 rgb.b = (rgb.b * 255) / 1000;
1160 sscanf(s,
"#%02x%02x%02x", &rgb.r, &rgb.g, &rgb.b);
1164 int _color = (
int)color;
1166 extended_color_content(_color, &_r, &_g, &_b);
1173
1174
1175
1176
1177
1178
1179
1181 init_extended_color(color, r, g, b);
1185
1186
1187
1188
1189
1190
1192 int _pair = (
int)pair;
1194 extended_pair_content(_pair, &_fg, &_bg);
1200
1201
1202
1203
1204
1205
1207 init_extended_pair(pair, fg, bg);
1211
1212
1213
1214
1215
1220 rgb.r = (*color >> 16) & 0xff;
1221 rgb.g = (*color >> 8) & 0xff;
1222 rgb.b = *color & 0xff;
1224 if (color_idx < 16) {
1229 rgb.r = (rgb.r * 1000) / 255;
1230 rgb.g = (rgb.g * 1000) / 255;
1231 rgb.b = (rgb.b * 1000) / 255;
1232 init_extended_color(color_idx, rgb.r, rgb.g, rgb.b);
1236
1237
1239
1240
1241
1248
1249
1250
1257
1258
1259
1260
1261
1270uint
ui__mbstr_to_cellstr(UiCell *cmplx_buf,
const char *str,
const UiCell *cell_base, uint *p,
const uint atmost) {
1279 uint i = 0, len = 0;
1283 getcchar(cell_base, &wstr[0], &attrs, &cp,
nullptr);
1285 memset(&mbstate, 0,
sizeof(mbstate));
1286 if (pos && *pos >= atmost - 1)
1288 while (str[i] !=
'\0') {
1290 len = mbrtowc(wstr, s, MB_CUR_MAX, &mbstate);
1299 if (setcchar(&cc, wstr, attrs, cp,
nullptr) !=
ERR) {
1300 if (len > 0 && (*pos + len) < atmost)
1301 cmplx_buf[(*pos)++] = cc;
1307 setcchar(&cc, wstr, attrs, cp,
nullptr);
1308 cmplx_buf[*pos] = cc;
UiSurface * ui_surface_box(UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x, const char *title)
Create a new UI surface with a border box and optional title.
int ui_surface_resize(UiSurface *s, ss_t w, uint lines, uint cols)
Resize a UiSurface to new dimensions.
UiRuntime * ui_init(const UiConfig *config, SIO *sio)
Initialize the UI runtime with the given configuration and SIO.
void ui_render()
Render the UI by updating panels and refreshing the display.
void ui_getyx(UiSurface *s, ss_t w, uint *lines, uint *cols)
Get the current cursor position within a window of a UI surface.
UiSurface * ui_surface_new(ss_t w, UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x)
Create a new UI surface with the specified parameters.
int ui_setscrreg(UiSurface *s, ss_t w, uint top, uint bottom)
Set the scrolling region for a UiSurface.
int ui_wmove(UiSurface *s, ss_t w, uint y, uint x)
Move the cursor to a specified position within a UiSurface.
void ui_getmaxyx(UiSurface *s, ss_t w, uint *lines, uint *cols)
Get the maximum dimensions of 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_wscrl(UiSurface *s, ss_t w, int rows)
Scroll the contents of a UiSurface by a specified number of lines.
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_immedok(UiSurface *s, ss_t w, bool enable)
int ui_cursor_move(UiSurface *s, ss_t w, uint y, uint x)
Move the cursor to a specified position within a UiSurface.
int ui_surface_addwin(UiSurface *s, ss_t w, ss_t p, uint lines, uint cols, uint y, uint x)
Add a new window to the specified UiSurface.
int ui_suspend()
Suspend the UI, saving the current program state and ending the window session.
void ui_surface_destroy(UiSurface *s)
Destroy a UiSurface, cleaning up its windows and panels.
int ui_surface_addpad(UiSurface *s, ss_t w, ss_t view_win, uint lines, uint cols, uint y, uint x, bool hidden)
Add a new pad to the specified UiSurface.
void ui_update_panels()
Update the panels in the UI.
int ui_bkgd(UiSurface *s, ss_t w, const UiCell *cell)
Set the background of a specific window within a UiSurface.
int ui_idlok(UiSurface *s, ss_t w, bool enable)
Enable or disable the use of the insert/delete line capabilities for a UiSurface.
int ui_surface_move(UiSurface *s, ss_t w, uint y, uint x)
Move a UiSurface to a new position on the screen.
int ui_prefresh(UiSurface *s, ss_t w, int pminrow, int pmincol, int sminrow, int smincol, int smaxrow, int smaxcol)
int ui_getmaxy(UiSurface *s, ss_t w)
Get the maximum number of lines (rows) of a UiSurface.
void ui_get_screen_size(uint *lines, uint *cols)
Get the current screen size (number of lines and columns).
int ui_surface_show(UiSurface *s, ss_t w)
Show a UiSurface, making it visible on the screen.
int ui_erase()
Erase the entire screen.
void ui_shutdown()
Shutdown the UI runtime, cleaning up resources and freeing memory.
int ui_curs_set(int visibility)
Set the visibility of the cursor.
int ui_resume()
Resume the UI, restoring the previous program state and updating the display.
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.
int ui_top_surface(UiSurface *s, ss_t w)
Hide a UiSurface, making it invisible on the screen.
int ui_wclear(UiSurface *s, ss_t w)
Clear the contents of a UiSurface or all surfaces.
void ui_endwin()
End the UI application, cleaning up resources and shutting down the UI runtime.
int ui_cursor_enable(UiSurface *s, ss_t w, bool visible)
Enable or disable the cursor for a UiSurface.
int ui_clear()
Clear the entire screen.
int ui_getmaxx(UiSurface *s, ss_t w)
Get the maximum number of columns of a UiSurface.
int ui_surface_hide(UiSurface *s, ss_t w)
Hide a UiSurface, making it invisible on the screen.
int ui_bkgrnd(UiSurface *s, ss_t w, const UiCell *cell)
Set the background of a specific window within a UiSurface.
int ui_scrollok(UiSurface *s, ss_t w, bool enable)
Enable or disable scrolling for a UiSurface.
int ui_wnoutrefresh(UiSurface *s, ss_t w)
Refresh a specific window within a UiSurface.
void ui_get_caps(UiCaps *caps)
Get the UI backend capabilities.
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_doupdate()
Refresh the display by updating the screen.
int ui_werase(UiSurface *s, ss_t w)
Erase the contents of a UiSurface or all surfaces.
int ui_keypad(UiSurface *s, ss_t w, bool enable)
Enable or disable the keypad for a UiSurface.
const wchar_t * border_none
int ui_border_draw(UiSurface *sfc, ss_t w)
const wchar_t * border_double
const wchar_t * border_single
void ui_initialize_sio(SIO *sio)
int ui_border_title(UiSurface *sfc, ss_t w, const char *title)
Draw a box with a title around the specified window.
const wchar_t * border_heavy
const wchar_t * border_rounded
void ui_apply_gamma(RGB *rgb)
Apply gamma correction to RGB color.
void ui_log_write(const UiLogLevel level, const char *file, const char *func, const int line, const char *fmt,...)
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.
uint ui__mbstr_to_cellstr(UiCell *cmplx_buf, const char *str, const UiCell *cell_base, uint *p, const uint atmost)
int ui_chg_pair(uint pair, uint fg, uint bg)
Change the foreground and background colors of an existing color pair.
int ui_pair_content(uint pair, uint *fg, uint *bg)
Get the foreground and background colors of a specified color pair.
SCREEN * ui_ncurses_get_screen()
Get the current SCREEN pointer used by the UI.
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_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.
uint ui_add_pair(uint fg, uint bg)
Add a new color pair to the UI, or retrieve the index of an existing pair.
UiSurface * ui_surface[UI_SFC_MAX]
uint32_t ui_get_color(uint16_t color_idx)
Get the RGB color value for a given color index.
int ui_color_content(uint color, uint *r, uint *g, uint *b)
void fast_exit(UiSurface *s)
Exit the UI application quickly, cleaning up resources.
RGB ui_hex_to_rgb(char *s)
Convert a hexadecimal color string to an RGB structure.
int ui_pair_from_hex(const char *fg, const char *bg)
Convert hex color strings to a curses color pair index.
WINDOW * ui_ncurses_surface_get_win(const UiSurface *s, ss_t w)
Get the current FILE pointer for the TTY used by the UI.
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.
PANEL * ui_ncurses_surface_get_panel(const UiSurface *s, ss_t w)
Get the current PANEL pointer for a specific window within a UiSurface.
int ui_color_from_rgb(RGB *rgb)
Convert RGB to curses color index, adding new color if necessary.
int ui_chg_color(uint16_t color_idx, uint32_t *color)
Change the RGB color value for a given color index.
size_t ssnprintf(char *, size_t, const char *,...)
Custom safe string formatting utility (Variadic version).
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
void sig_dfl_mode()
Set signal handlers to default behavior.
int ui_display_error(char *msg0, char *msg1, char *msg2, char *msg3)
Display an error message window or print to stderr.
UiBackend ui_get_backend()
Get the UI backend type.
UiBorderStyle border_style