#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_termmode mode) { switch (mode) { case TMODE_CBREAK: return cbreak(); case TMODE_RAW: return raw(); case TMODE_NOCBREAK: return nocbreak(); case TMODE_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 */ start_color(); /* Screen Configuration */ termmode(TMODE_CBREAK); noecho(); // manually echo from getch(3x) curs_set(0); // hide cursor } /* 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 = new_window(0, 0, 0, 0); // 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; }