From ad4ed4d744495e552e691f79c09046e3b6246761 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Mon, 4 Jul 2011 22:02:01 +1000 Subject: [PATCH] Add input routines gettxchar() and getanswer(); add various macros In particular, add macros for KEY_TAB, KEY_RETURN and KEY_ESC, and for CURS_ON and CURS_OFF (instead of using CURS_INVISIBLE and CURS_VISIBLE directly: this allows redefining CURS_ON to be CURS_NORMAL). Add more colour pairs. --- src/intf.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++----- src/intf.h | 16 ++++++++++ 2 files changed, 100 insertions(+), 7 deletions(-) diff --git a/src/intf.c b/src/intf.c index 775d2a4..1f2f5cb 100644 --- a/src/intf.c +++ b/src/intf.c @@ -66,7 +66,6 @@ txwin_t *firstwin = NULL; // First (bottom-most) txwin structure * Basic text input/output function definitions * ************************************************************************/ - /*----------------------------------------------------------------------- Function: init_screen - Initialise the screen (terminal) Arguments: (none) @@ -90,17 +89,19 @@ void init_screen (void) firstwin = NULL; noecho(); - curs_set(CURS_INVISIBLE); + curs_set(CURS_OFF); raw(); if (has_colors()) { start_color(); - init_pair(WHITE_ON_BLACK, COLOR_WHITE, COLOR_BLACK); - init_pair(WHITE_ON_BLUE, COLOR_WHITE, COLOR_BLUE); - init_pair(YELLOW_ON_CYAN, COLOR_YELLOW, COLOR_CYAN); - init_pair(WHITE_ON_RED, COLOR_WHITE, COLOR_RED); - init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE); + init_pair(WHITE_ON_BLACK, COLOR_WHITE, COLOR_BLACK); + init_pair(WHITE_ON_BLUE, COLOR_WHITE, COLOR_BLUE); + init_pair(WHITE_ON_RED, COLOR_WHITE, COLOR_RED); + init_pair(YELLOW_ON_BLACK, COLOR_YELLOW, COLOR_BLACK); + init_pair(YELLOW_ON_BLUE, COLOR_YELLOW, COLOR_BLUE); + init_pair(YELLOW_ON_CYAN, COLOR_YELLOW, COLOR_CYAN); + init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE); bkgd(COLOR_PAIR(WHITE_ON_BLACK)); } @@ -136,6 +137,10 @@ void end_screen (void) } +/************************************************************************ +* Simplified panel-like window functions * +************************************************************************/ + /*----------------------------------------------------------------------- Function: newtxwin - Create a new window, inserted into window stack Arguments: nlines - Number of lines in new window @@ -277,6 +282,10 @@ int txrefresh (void) } +/************************************************************************ +* Output routines * +************************************************************************/ + /*----------------------------------------------------------------------- Function: center - Centre a string on the current line Arguments: win - Window to use @@ -370,3 +379,71 @@ int attrpr (WINDOW *win, int attr_start, int attr_end, const char *format, ...) return ret; } + + +/************************************************************************ +* Input routines * +************************************************************************/ + +/*----------------------------------------------------------------------- + Function: gettxchar - Read a keyboard character + Arguments: win - Window to use + Returns: int - Keyboard character + + This function reads a single character from the keyboard. The key is + NOT echoed to the screen and the cursor visibility is NOT affected. +*/ + +int gettxchar (WINDOW *win) +{ + keypad(win, true); + meta(win, true); + wtimeout(win, -1); + + return wgetch(win); +} + + +/*----------------------------------------------------------------------- + Function: getanswer - Read a Yes/No answer and return true/false + Arguments: win - Window to use + Returns: bool - true if Yes ("Y") was selected, else false + + This function waits for either "Y" or "N" to be pressed on the + keyboard. If "Y" was pressed, "Yes." is printed and true is returned. + If "N" was pressed, "No." is printed and false is returned. Note that + the cursor becomes invisible after this function. +*/ + +bool getanswer (WINDOW *win) +{ + int key; + bool ok; + + + keypad(win, true); + meta(win, true); + wtimeout(win, -1); + + curs_set(CURS_ON); + + do { + key = toupper(wgetch(win)); + ok = ((key == 'Y') || (key == 'N')); + + if (! ok) { + beep(); + } + } while (! ok); + + curs_set(CURS_OFF); + + if (key == 'Y') { + wprintw(win, "Yes."); + } else { + wprintw(win, "No."); + } + + wrefresh(win); + return (key == 'Y'); +} diff --git a/src/intf.h b/src/intf.h index d66f844..b1f7b93 100644 --- a/src/intf.h +++ b/src/intf.h @@ -46,6 +46,15 @@ typedef enum curs_type { CURS_VISIBLE = 2 } curs_type_t; +#define CURS_OFF (CURS_INVISIBLE) +#define CURS_ON (CURS_VISIBLE) + + +// Keycodes +#define KEY_TAB (011) +#define KEY_RETURN (012) +#define KEY_ESC (033) + /* This version of Star Traders only utilises WIN_COLS x WIN_LINES of a @@ -74,6 +83,8 @@ enum color_pairs { WHITE_ON_BLACK, WHITE_ON_BLUE, WHITE_ON_RED, + YELLOW_ON_BLACK, + YELLOW_ON_BLUE, YELLOW_ON_CYAN, BLACK_ON_WHITE, }; @@ -110,5 +121,10 @@ extern int attrpr (WINDOW *win, int attr_start, int attr_end, const char *format, ...) __attribute__((format (printf, 4, 5))); +// Input routines + +extern int gettxchar (WINDOW *win); +extern bool getanswer (WINDOW *win); + #endif /* included_INTF_H */