add curses.c WINDOW init funcs

This commit is contained in:
Emile Clark-Boman 2025-09-10 23:05:07 +10:00
parent a55c25d319
commit 4d7402fa52
2 changed files with 34 additions and 19 deletions

View file

@ -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));

View file

@ -4,19 +4,20 @@
/* libncurses with wide-character support. */
#include <ncursesw/ncurses.h>
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 */