Added experimental feature NEW_KEYBOARD_HIT.

This commit is contained in:
Bart House
2019-07-09 22:30:34 -07:00
parent 0e8163e341
commit bc65112ce0
14 changed files with 199 additions and 3 deletions
+6 -2
View File
@@ -564,9 +564,13 @@ typedef unsigned char uchar;
%N first character of player name %N first character of player name
DUMPLOG_FILE is not used if SYSCF is defined DUMPLOG_FILE is not used if SYSCF is defined
*/ */
#endif #endif /* DUMPLOG_FILE */
#endif #endif /* DUMPLOG */
/* NEW_KEYBOARD_HIT adds new window proc to return whether keyboard has been hit
and character input is available */
/* #define NEW_KEYBOARD_HIT */
#define USE_ISAAC64 /* Use cross-plattform, bundled RNG */ #define USE_ISAAC64 /* Use cross-plattform, bundled RNG */
+15
View File
@@ -1147,6 +1147,9 @@ E void NDECL(gettty);
E void NDECL(setftty); E void NDECL(setftty);
E void FDECL(settty, (const char *)); E void FDECL(settty, (const char *));
E int NDECL(tgetch); E int NDECL(tgetch);
#ifdef NEW_KEYBOARD_HIT
E boolean NDECL(tkbhit);
#endif
E void FDECL(cmov, (int x, int y)); E void FDECL(cmov, (int x, int y));
E void FDECL(nocmov, (int x, int y)); E void FDECL(nocmov, (int x, int y));
@@ -1552,6 +1555,9 @@ E void FDECL(mplayer_talk, (struct monst *));
#ifndef WIN32 #ifndef WIN32
E int NDECL(tgetch); E int NDECL(tgetch);
#ifdef NEW_KEYBOARD_HIT
E boolean NDECL(tkbhit);
#endif
#endif #endif
#ifndef TOS #ifndef TOS
E char NDECL(switchar); E char NDECL(switchar);
@@ -1582,7 +1588,9 @@ E void FDECL(gotoxy, (int, int));
#endif #endif
#ifdef TOS #ifdef TOS
E int FDECL(_copyfile, (char *, char *)); E int FDECL(_copyfile, (char *, char *));
#ifndef NEW_KEYBOARD_HIT
E int NDECL(kbhit); E int NDECL(kbhit);
#endif
E void NDECL(set_colors); E void NDECL(set_colors);
E void NDECL(restore_colors); E void NDECL(restore_colors);
#ifdef SUSPEND #ifdef SUSPEND
@@ -1592,7 +1600,9 @@ E int NDECL(dosuspend);
#ifdef WIN32 #ifdef WIN32
E char *FDECL(get_username, (int *)); E char *FDECL(get_username, (int *));
E void FDECL(nt_regularize, (char *)); E void FDECL(nt_regularize, (char *));
#ifndef NEW_KEYBOARD_HIT
E int NDECL((*nt_kbhit)); E int NDECL((*nt_kbhit));
#endif
E void FDECL(Delay, (int)); E void FDECL(Delay, (int));
#endif /* WIN32 */ #endif /* WIN32 */
#endif /* MICRO || WIN32 */ #endif /* MICRO || WIN32 */
@@ -1660,10 +1670,15 @@ E void FDECL(regex_free, (struct nhregex *));
#ifdef WIN32 #ifdef WIN32
E void NDECL(get_scr_size); E void NDECL(get_scr_size);
#ifndef NEW_KEYBOARD_HIT
E int NDECL(nttty_kbhit); E int NDECL(nttty_kbhit);
#endif
E void FDECL(nttty_open, (int)); E void FDECL(nttty_open, (int));
E void NDECL(nttty_rubout); E void NDECL(nttty_rubout);
E int NDECL(tgetch); E int NDECL(tgetch);
#ifdef NEW_KEYBOARD_HIT
E boolean NDECL(tkbhit);
#endif
E int FDECL(ntposkey, (int *, int *, int *)); E int FDECL(ntposkey, (int *, int *, int *));
E void FDECL(set_output_mode, (int)); E void FDECL(set_output_mode, (int));
E void NDECL(synch_cursor); E void NDECL(synch_cursor);
+3
View File
@@ -252,8 +252,11 @@ int _RTLENTRY _EXPFUNC read(int __handle, void _FAR *__buf, unsigned __len);
#ifndef CURSES_GRAPHICS #ifndef CURSES_GRAPHICS
#include <conio.h> /* conflicting definitions with curses.h */ #include <conio.h> /* conflicting definitions with curses.h */
#endif #endif
#ifndef NEW_KEYBOARD_HIT
#undef kbhit /* Use our special NT kbhit */ #undef kbhit /* Use our special NT kbhit */
#define kbhit (*nt_kbhit) #define kbhit (*nt_kbhit)
#endif
#ifdef LAN_FEATURES #ifdef LAN_FEATURES
#define MAX_LAN_USERNAME 20 #define MAX_LAN_USERNAME 20
+2
View File
@@ -311,7 +311,9 @@
#define HLOCK "perm" /* an empty file used for locking purposes */ #define HLOCK "perm" /* an empty file used for locking purposes */
#ifndef NEW_KEYBOARD_HIT
#define tgetch getchar #define tgetch getchar
#endif
#ifndef NOSHELL #ifndef NOSHELL
#define SHELL /* do not delete the '!' command */ #define SHELL /* do not delete the '!' command */
+12
View File
@@ -79,6 +79,9 @@ struct window_procs {
(int, const char *, const char *, BOOLEAN_P)); (int, const char *, const char *, BOOLEAN_P));
void FDECL((*win_status_update), (int, genericptr_t, int, int, int, unsigned long *)); void FDECL((*win_status_update), (int, genericptr_t, int, int, int, unsigned long *));
boolean NDECL((*win_can_suspend)); boolean NDECL((*win_can_suspend));
#ifdef NEW_KEYBOARD_HIT
boolean NDECL((*win_keyboard_hit));
#endif
}; };
extern extern
@@ -160,6 +163,9 @@ extern
*/ */
#define status_enablefield (*windowprocs.win_status_enablefield) #define status_enablefield (*windowprocs.win_status_enablefield)
#define status_update (*windowprocs.win_status_update) #define status_update (*windowprocs.win_status_update)
#ifdef NEW_KEYBOARD_HIT
#define keyboard_hit (*windowprocs.win_keyboard_hit)
#endif
/* /*
* WINCAP * WINCAP
@@ -379,6 +385,9 @@ struct chain_procs {
(CARGS, int, const char *, const char *, BOOLEAN_P)); (CARGS, int, const char *, const char *, BOOLEAN_P));
void FDECL((*win_status_update), (CARGS, int, genericptr_t, int, int, int, unsigned long *)); void FDECL((*win_status_update), (CARGS, int, genericptr_t, int, int, int, unsigned long *));
boolean FDECL((*win_can_suspend), (CARGS)); boolean FDECL((*win_can_suspend), (CARGS));
#ifdef NEW_KEYBOARD_HIT
boolean FDECL((*keyboard_hit), (CARGS));
#endif
}; };
#endif /* WINCHAIN */ #endif /* WINCHAIN */
@@ -449,6 +458,9 @@ extern void FDECL(safe_status_enablefield,
(int, const char *, const char *, BOOLEAN_P)); (int, const char *, const char *, BOOLEAN_P));
extern void FDECL(safe_status_update, (int, genericptr_t, int, int, int, unsigned long *)); extern void FDECL(safe_status_update, (int, genericptr_t, int, int, int, unsigned long *));
extern boolean NDECL(safe_can_suspend); extern boolean NDECL(safe_can_suspend);
#ifdef NEW_KEYBOARD_HIT
extern boolean NDECL(safe_keyboard_hit);
#endif
extern void FDECL(stdio_raw_print, (const char *)); extern void FDECL(stdio_raw_print, (const char *));
extern void FDECL(stdio_raw_print_bold, (const char *)); extern void FDECL(stdio_raw_print_bold, (const char *));
extern void NDECL(stdio_wait_synch); extern void NDECL(stdio_wait_synch);
+4
View File
@@ -247,6 +247,10 @@ E void FDECL(genl_outrip, (winid, int, time_t));
E char *FDECL(tty_getmsghistory, (BOOLEAN_P)); E char *FDECL(tty_getmsghistory, (BOOLEAN_P));
E void FDECL(tty_putmsghistory, (const char *, BOOLEAN_P)); E void FDECL(tty_putmsghistory, (const char *, BOOLEAN_P));
#ifdef NEW_KEYBOARD_HIT
E boolean NDECL(tty_keyboard_hit);
#endif
#ifdef NO_TERMS #ifdef NO_TERMS
#ifdef MAC #ifdef MAC
#ifdef putchar #ifdef putchar
+4
View File
@@ -374,7 +374,11 @@ boolean resuming;
if (multi >= 0 && occupation) { if (multi >= 0 && occupation) {
#if defined(MICRO) || defined(WIN32) #if defined(MICRO) || defined(WIN32)
abort_lev = 0; abort_lev = 0;
#ifdef NEW_KEYBOARD_HIT
if (keyboard_hit()) {
#else
if (kbhit()) { if (kbhit()) {
#endif
if ((ch = pgetchar()) == ABORT) if ((ch = pgetchar()) == ABORT)
abort_lev++; abort_lev++;
else else
+14
View File
@@ -523,6 +523,9 @@ static void NDECL(hup_void_ndecl);
static void FDECL(hup_void_fdecl_int, (int)); static void FDECL(hup_void_fdecl_int, (int));
static void FDECL(hup_void_fdecl_winid, (winid)); static void FDECL(hup_void_fdecl_winid, (winid));
static void FDECL(hup_void_fdecl_constchar_p, (const char *)); static void FDECL(hup_void_fdecl_constchar_p, (const char *));
#ifdef NEW_KEYBOARD_HIT
static boolean NDECL(hup_keyboard_hit);
#endif
static struct window_procs hup_procs = { static struct window_procs hup_procs = {
"hup", 0L, 0L, hup_init_nhwindows, "hup", 0L, 0L, hup_init_nhwindows,
@@ -570,6 +573,9 @@ static struct window_procs hup_procs = {
hup_void_ndecl, /* status_finish */ hup_void_ndecl, /* status_finish */
genl_status_enablefield, hup_status_update, genl_status_enablefield, hup_status_update,
genl_can_suspend_no, genl_can_suspend_no,
#ifdef NEW_KEYBOARD_HIT
hup_keyboard_hit
#endif
}; };
static void FDECL((*previnterface_exit_nhwindows), (const char *)) = 0; static void FDECL((*previnterface_exit_nhwindows), (const char *)) = 0;
@@ -624,6 +630,14 @@ hup_nhgetch(VOID_ARGS)
return '\033'; /* ESC */ return '\033'; /* ESC */
} }
#ifdef NEW_KEYBOARD_HIT
static boolean
hup_keyboard_hit(VOID_ARGS)
{
return FALSE;
}
#endif
/*ARGSUSED*/ /*ARGSUSED*/
static char static char
hup_yn_function(prompt, resp, deflt) hup_yn_function(prompt, resp, deflt)
+10
View File
@@ -398,10 +398,12 @@ int mode; // unused
{ {
DWORD cmode; DWORD cmode;
#ifndef NEW_KEYBOARD_HIT
/* Initialize the function pointer that points to /* Initialize the function pointer that points to
* the kbhit() equivalent, in this TTY case nttty_kbhit() * the kbhit() equivalent, in this TTY case nttty_kbhit()
*/ */
nt_kbhit = nttty_kbhit; nt_kbhit = nttty_kbhit;
#endif
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE)) { if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE)) {
/* Unable to set control handler */ /* Unable to set control handler */
@@ -436,11 +438,19 @@ int portdebug;
return ch; return ch;
} }
#ifndef NEW_KEYBOARD_HIT
int int
nttty_kbhit() nttty_kbhit()
{ {
return keyboard_handler.pNHkbhit(console.hConIn, &ir); return keyboard_handler.pNHkbhit(console.hConIn, &ir);
} }
#else
boolean
tkbhit()
{
return keyboard_handler.pNHkbhit(console.hConIn, &ir) != 0;
}
#endif
int int
tgetch() tgetch()
+6 -1
View File
@@ -56,6 +56,7 @@ extern void NDECL(backsp);
int NDECL(windows_console_custom_nhgetch); int NDECL(windows_console_custom_nhgetch);
extern void NDECL(safe_routines); extern void NDECL(safe_routines);
#ifndef NEW_KEYBOARD_HIT
/* The function pointer nt_kbhit contains a kbhit() equivalent /* The function pointer nt_kbhit contains a kbhit() equivalent
* which varies depending on which window port is active. * which varies depending on which window port is active.
* For the tty port it is tty_kbhit() [from nttty.c] * For the tty port it is tty_kbhit() [from nttty.c]
@@ -65,6 +66,7 @@ extern void NDECL(safe_routines);
int def_kbhit(void); int def_kbhit(void);
int (*nt_kbhit)() = def_kbhit; int (*nt_kbhit)() = def_kbhit;
#endif
char char
switchar() switchar()
@@ -158,11 +160,13 @@ max_filename()
return 0; return 0;
} }
#ifndef NEW_KEYBOARD_HIT
int int
def_kbhit() def_kbhit()
{ {
return 0; return 0;
} }
#endif
/* /*
* Strip out troublesome file system characters. * Strip out troublesome file system characters.
@@ -510,7 +514,9 @@ int code;
exit(code); exit(code);
} }
#ifndef NEW_KEYBOARD_HIT
#undef kbhit #undef kbhit
#endif
#include <conio.h> #include <conio.h>
int int
@@ -719,7 +725,6 @@ sys_random_seed(VOID_ARGS)
} }
return ourseed; return ourseed;
} }
#endif /* WIN32 */ #endif /* WIN32 */
/*winnt.c*/ /*winnt.c*/
+11
View File
@@ -101,6 +101,9 @@ struct window_procs safe_procs = {
safe_status_update, safe_status_update,
#endif #endif
safe_can_suspend, safe_can_suspend,
#ifdef NEW_KEYBOARD_HIT
safe_keyboard_hit
#endif
}; };
struct window_procs * struct window_procs *
@@ -386,6 +389,14 @@ safe_can_suspend()
return FALSE; return FALSE;
} }
#ifdef NEW_KEYBOARD_HIT
boolean
safe_keyboard_hit()
{
return FALSE;
}
#endif
void void
safe_nhbell() safe_nhbell()
{ {
+97
View File
@@ -36,6 +36,12 @@ extern void msmsg(const char *, ...);
#endif #endif
#endif #endif
#ifdef NEW_KEYBOARD_HIT
#if defined(UNIX)
#include <fcntl.h>
#endif
#endif
#ifdef TTY_TILES_ESCCODES #ifdef TTY_TILES_ESCCODES
extern short glyph2tile[]; extern short glyph2tile[];
#define TILE_ANSI_COMMAND 'z' #define TILE_ANSI_COMMAND 'z'
@@ -133,6 +139,9 @@ struct window_procs tty_procs = {
genl_status_update, genl_status_update,
#endif #endif
genl_can_suspend_yes, genl_can_suspend_yes,
#ifdef NEW_KEYBOARD_HIT
tty_keyboard_hit
#endif
}; };
static int maxwin = 0; /* number of windows in use */ static int maxwin = 0; /* number of windows in use */
@@ -3479,6 +3488,7 @@ const char *str;
#endif #endif
} }
#ifndef NEW_KEYBOARD_HIT
int int
tty_nhgetch() tty_nhgetch()
{ {
@@ -3533,6 +3543,93 @@ tty_nhgetch()
#endif /* TTY_TILES_ESCCODES */ #endif /* TTY_TILES_ESCCODES */
return i; return i;
} }
#else /* NEW_KEYBOARD_HIT */
#ifdef UNIX
static boolean stdin_non_blocking = FALSE;
int
tgetch()
{
static volatile int nesting = 0;
int i;
char nestbuf;
if (stdin_non_blocking) {
fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK);
stdin_non_blocking = FALSE;
}
/* kludge alert: Some Unix variants return funny values if getc()
* is called, interrupted, and then called again. There
* is non-reentrant code in the internal _filbuf() routine, called by
* getc().
*/
i = (++nesting == 1)
? getchar()
: (read(fileno(stdin), (genericptr_t) &nestbuf, 1) == 1)
? (int) nestbuf : EOF;
--nesting;
return i;
}
boolean
tkbhit()
{
int i;
if (!stdin_non_blocking) {
fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
stdin_non_blocking = TRUE;
}
i = getchar();
if (i != EOF) ungetc(i, stdin);
return (i != EOF);
}
#endif
int
tty_nhgetch()
{
int i;
HUPSKIP_RESULT('\033');
print_vt_code1(AVTC_INLINE_SYNC);
(void) fflush(stdout);
/* Note: if raw_print() and wait_synch() get called to report terminal
* initialization problems, then wins[] and ttyDisplay might not be
* available yet. Such problems will probably be fatal before we get
* here, but validate those pointers just in case...
*/
if (WIN_MESSAGE != WIN_ERR && wins[WIN_MESSAGE])
wins[WIN_MESSAGE]->flags &= ~WIN_STOP;
if (iflags.debug_fuzzer) {
i = randomkey();
} else {
i = tgetch();
}
if (!i)
i = '\033'; /* map NUL to ESC since nethack doesn't expect NUL */
else if (i == EOF)
i = '\033'; /* same for EOF */
if (ttyDisplay && ttyDisplay->toplin == 1)
ttyDisplay->toplin = 2;
#ifdef TTY_TILES_ESCCODES
{
/* hack to force output of the window select code */
int tmp = vt_tile_current_window;
vt_tile_current_window++;
print_vt_code2(AVTC_SELECT_WINDOW, tmp);
}
#endif /* TTY_TILES_ESCCODES */
return i;
}
boolean tty_keyboard_hit()
{
/* tgetch provider needs to also provide tkbhit() */
return tkbhit();
}
#endif /* NEW_KEYBOARD_HIT */
/* /*
* return a key, or 0, in which case a mouse button was pressed * return a key, or 0, in which case a mouse button was pressed
+12
View File
@@ -116,6 +116,9 @@ struct window_procs mswin_procs = {
mswin_status_init, mswin_status_finish, mswin_status_enablefield, mswin_status_init, mswin_status_finish, mswin_status_enablefield,
mswin_status_update, mswin_status_update,
genl_can_suspend_yes, genl_can_suspend_yes,
#ifdef NEW_KEYBOARD_HIT
mswin_keyboard_hit
#endif
}; };
/* /*
@@ -1402,6 +1405,15 @@ mswin_nhgetch()
return (key); return (key);
} }
#ifdef NEW_KEYBOARD_HIT
/* boolean keyboard_hit() -- returns TRUE if input is available */
boolean
mswin_keyboard_hit()
{
return mswin_have_input() != 0;
}
#endif
/* /*
int nh_poskey(int *x, int *y, int *mod) int nh_poskey(int *x, int *y, int *mod)
-- Returns a single character input from the user or a -- Returns a single character input from the user or a
+3
View File
@@ -167,6 +167,9 @@ void mswin_raw_print(const char *str);
void mswin_raw_print_bold(const char *str); void mswin_raw_print_bold(const char *str);
void mswin_raw_print_flush(); void mswin_raw_print_flush();
int mswin_nhgetch(void); int mswin_nhgetch(void);
#ifdef NEW_KEYBOARD_HIT
boolean mswin_keyboard_hit(void);
#endif
int mswin_nh_poskey(int *x, int *y, int *mod); int mswin_nh_poskey(int *x, int *y, int *mod);
void mswin_nhbell(void); void mswin_nhbell(void);
int mswin_doprev_message(void); int mswin_doprev_message(void);