From 4d7402fa52450201dc666e8848aa936855576445 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Wed, 10 Sep 2025 23:05:07 +1000 Subject: [PATCH] add curses.c WINDOW init funcs --- cli/curse.c | 40 +++++++++++++++++++++++++++------------- cli/curse.h | 13 +++++++------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/cli/curse.c b/cli/curse.c index f2eda88..049082b 100644 --- a/cli/curse.c +++ b/cli/curse.c @@ -9,15 +9,15 @@ /* Set ncurses terminal mode (buffering, character processing, * Key->SIG handling, and other termios(3) functionality). */ -inline int termmode(enum crs_mode mode) { +inline int termmode(enum crs_termmode mode) { switch (mode) { - case WMODE_CBREAK: + case TMODE_CBREAK: return cbreak(); - case WMODE_RAW: + case TMODE_RAW: return raw(); - case WMODE_NOCBREAK: + case TMODE_NOCBREAK: return nocbreak(); - case WMODE_NORAW: + case TMODE_NORAW: return noraw(); /* defaulting is not possible. */ } @@ -34,28 +34,42 @@ inline void init_ncurses(void) { start_color(); /* Screen Configuration */ - termmode(WMODE_CBREAK); - noecho(); // manually echo <- getch(3x) + termmode(TMODE_CBREAK); + noecho(); // manually echo from getch(3x) curs_set(0); // hide cursor - - // init_pair(1, COLOR_BLACK, COLOR_CYAN); } -WINDOW *init_window(int x, int y, int width, int height) { - WINDOW *win = newwin(height, width, y, x); - +/* Apply a default IO configuration for an ncurses WINDOW. + */ +static inline void __conf_window(WINDOW *win) { nodelay(win, TRUE); // getch(3x) nonblocking IO keypad(win, TRUE); // allow function keys // intrflush(3x) - flush terminal input buffer on interrupt key intrflush(win, FALSE); +} + +/* Initialise (with default IO configuration) a new ncruses WINDOW. + */ +WINDOW *new_window(int x, int y, int width, int height) { + WINDOW *win = newwin(height, width, y, x); + __conf_window(win); return win; } +/* Initialise (with default IO configuration) the root ncurses WINDOW. + * NOTE: This is typically done via initscr(3x) and called "stdscr". + */ +inline WINDOW *root_window(void) { + WINDOW *rootwin = new_window(0, 0, 0, 0); + __conf_window(rootwin); + return rootwin; +} + int main(int argc, char *argv[]) { init_ncurses(); - WINDOW *stdscr = init_window(0, 0, 0, 0); + WINDOW *stdscr = new_window(0, 0, 0, 0); // Set background wbkgd(stdscr, COLOR_PAIR(1)); diff --git a/cli/curse.h b/cli/curse.h index 67d599f..24bc681 100644 --- a/cli/curse.h +++ b/cli/curse.h @@ -4,19 +4,20 @@ /* libncurses with wide-character support. */ #include -enum crs_mode { +enum crs_termmode { /* tty cbreak mode */ - WMODE_CBREAK, + TMODE_CBREAK, /* tty raw mode */ - WMODE_RAW, + TMODE_RAW, /* tty cooked mode (ISIG & IXON not modified)*/ - WMODE_NOCBREAK, + TMODE_NOCBREAK, /* tty cooked mode (ISIG & IXON set) */ - WMODE_NORAW, + TMODE_NORAW, }; -int termmode(enum crs_mode mode); +int termmode(enum crs_termmode mode); void init_ncurses(void); WINDOW *init_window(int x, int y, int width, int height); +WINDOW *root_window(void); #endif /* DORNE_CURSE_H */