diff --git a/cli/_pty.c b/cli/_pty.c index 60509e8..0fdd9d2 100644 --- a/cli/_pty.c +++ b/cli/_pty.c @@ -55,7 +55,7 @@ int mkpty(int *fdmx, int *fds) { goto fail; } - // Propagate file descriptors via parameters + /* Propagate file descriptors via parameters */ *fdmx = _fdmx; *fds = _fds; return EXIT_SUCCESS; diff --git a/cli/curse.c b/cli/curse.c new file mode 100644 index 0000000..bc5a1c9 --- /dev/null +++ b/cli/curse.c @@ -0,0 +1,77 @@ +#include +#include + +#include "curse.h" + +#define BASE_DELAY 1000000 +#define CRS_LOCALE "en_US.UTF-8" + +/* Set ncurses terminal mode (buffering, character processing, + * Key->SIG handling, and other termios(3) functionality). + */ +inline int termmode(enum crs_mode mode) { + switch (mode) { + case WMODE_CBREAK: + return cbreak(); + case WMODE_RAW: + return raw(); + case WMODE_NOCBREAK: + return nocbreak(); + case WMODE_NORAW: + return noraw(); + /* defaulting is not possible. */ + } +} + +/* Initialise ncurses for our usecase. + * WARNING: This function should only be called once. + */ +inline void init_ncurses(void) { + // ncurses expects a locale for consistent behaviour + setlocale(LC_ALL, CRS_LOCALE); + + /* NCurses Init */ + *stdscr = initscr(); + start_color(); + + /* Screen Configuration */ + termmode(WMODE_CBREAK); + noecho(); // manually echo <- getch(3x) + nodelay(*stdscr, TRUE); // getch(3x) nonblocking IO + keypad(*stdscr, TRUE); // allow function keys + // intrflush(3x) - flush terminal input buffer on interrupt key + intrflush(*stdscr, FALSE); + + curs_set(0); // hide cursor + + // init_pair(1, COLOR_BLACK, COLOR_CYAN); +} + +int main(int argc, char *argv[]) { + WINDOW *stdscr; + crs_init(&stdscr); + + // Set background + wbkgd(stdscr, COLOR_PAIR(1)); + + attron(COLOR_PAIR(1)); + attron(A_BOLD); + int counter = 0; + while (1) { + mvprintw(0, 0, "COUNTER %d", counter++); + refresh(); + + switch (getch()) { + case 'q': + goto end; + default: + break; + } + + usleep(BASE_DELAY); + } + +end: + endwin(); + return 0; +} diff --git a/cli/curse.h b/cli/curse.h new file mode 100644 index 0000000..8234a9a --- /dev/null +++ b/cli/curse.h @@ -0,0 +1,21 @@ +#ifndef DORNE_CURSE_H +#define DORNE_CURSE_H + +/* libncurses with wide-character support. */ +#include + +enum crs_mode { + /* tty cbreak mode */ + WMODE_CBREAK, + /* tty raw mode */ + WMODE_RAW, + /* tty cooked mode (ISIG & IXON not modified)*/ + WMODE_NOCBREAK, + /* tty cooked mode (ISIG & IXON set) */ + WMODE_NORAW, +}; + +int termmode(enum crs_mode mode); +void init_ncurses(void); + +#endif /* DORNE_CURSE_H */