/* This file is term.c for i5.nyu.edu. It's written in C. If you comment in the calls to keypad, then getch will return KEY_LEFT, etc., for the arrow keys. In telnet, you have to select Terminal, Preferences, VT100 Arrows. */ #include #include #include #include /* header files for fcntl */ #include #include #include "term.h" void term_construct(void) { initscr(); /* inintscr or newterm must be the first curses call */ cbreak(); /* cbreak mode: don't wait for RETURN */ noecho(); /* don't display typed characters on screen */ nodelay(stdscr, TRUE); /* tell getch to always return immediately: don't wait for user to press key */ /* keypad(stdscr, TRUE); treat arrow key as a single char */ curs_set(0); /* make cursor invisible */ refresh(); } void term_destruct(void) /* Undo everything that term_construct did. */ { int flags; curs_set(1); /* make cursor visible again */ /* keypad(stdscr, FALSE); */ nodelay(stdscr, FALSE); echo(); nocbreak(); endwin(); /* must be the last curses call */ /* Because of a bug in curses, we have to turn off the O_NDELAY bit ourselves. */ flags = fcntl(0, F_GETFL, 0); if (flags < 0) { fprintf(stderr, "fcntl(F_GETFL) failed\n"); } flags &= ~O_NDELAY; if (fcntl(0, F_SETFL, flags) == -1) { fprintf(stderr, "fcntl(F_SETFL, %d) failed\n", flags); } } unsigned term_xmax(void) {return stdscr->_maxx - stdscr->_begx;} unsigned term_ymax(void) {return stdscr->_maxy - stdscr->_begy;} char term_get(unsigned x, unsigned y) { refresh(); return A_CHARTEXT & mvinch(y + stdscr->_begy, x + stdscr->_begx); } void term_put(unsigned x, unsigned y, char c) { mvprintw(y + stdscr->_begy, x + stdscr->_begx, "%c", c); refresh(); } void term_puts(unsigned x, unsigned y, const char *s) { mvprintw(y + stdscr->_begy, x + stdscr->_begx, "%s", s); refresh(); } /* Return the key the user pressed. If no key was pressed, return '\0' immediately. (Because of the call to nodelay in term_construct, getch will return ERR immediately instead of waiting if the user hasn't pressed a key.) */ char term_key(void) { const int c = getch(); return c == ERR ? '\0' : c; } /* Wait a fraction of a second. 1000 milliseconds == 1 second. */ void term_wait(int milliseconds) { napms(milliseconds); } void term_beep(void) { beep(); refresh(); }