From bc65112ce07c6e2056ed28b5e93954679fb36acd Mon Sep 17 00:00:00 2001 From: Bart House Date: Tue, 9 Jul 2019 22:30:34 -0700 Subject: [PATCH] Added experimental feature NEW_KEYBOARD_HIT. --- include/config.h | 8 +++- include/extern.h | 15 +++++++ include/ntconf.h | 3 ++ include/unixconf.h | 2 + include/winprocs.h | 12 ++++++ include/wintty.h | 4 ++ src/allmain.c | 4 ++ src/windows.c | 14 +++++++ sys/winnt/nttty.c | 10 +++++ sys/winnt/winnt.c | 7 +++- win/share/safeproc.c | 11 +++++ win/tty/wintty.c | 97 ++++++++++++++++++++++++++++++++++++++++++++ win/win32/mswproc.c | 12 ++++++ win/win32/winMS.h | 3 ++ 14 files changed, 199 insertions(+), 3 deletions(-) diff --git a/include/config.h b/include/config.h index d12576f9d..f054de5c4 100644 --- a/include/config.h +++ b/include/config.h @@ -564,9 +564,13 @@ typedef unsigned char uchar; %N first character of player name 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 */ diff --git a/include/extern.h b/include/extern.h index 90548cf37..0761d224e 100644 --- a/include/extern.h +++ b/include/extern.h @@ -1147,6 +1147,9 @@ E void NDECL(gettty); E void NDECL(setftty); E void FDECL(settty, (const char *)); E int NDECL(tgetch); +#ifdef NEW_KEYBOARD_HIT +E boolean NDECL(tkbhit); +#endif E void FDECL(cmov, (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 E int NDECL(tgetch); +#ifdef NEW_KEYBOARD_HIT +E boolean NDECL(tkbhit); +#endif #endif #ifndef TOS E char NDECL(switchar); @@ -1582,7 +1588,9 @@ E void FDECL(gotoxy, (int, int)); #endif #ifdef TOS E int FDECL(_copyfile, (char *, char *)); +#ifndef NEW_KEYBOARD_HIT E int NDECL(kbhit); +#endif E void NDECL(set_colors); E void NDECL(restore_colors); #ifdef SUSPEND @@ -1592,7 +1600,9 @@ E int NDECL(dosuspend); #ifdef WIN32 E char *FDECL(get_username, (int *)); E void FDECL(nt_regularize, (char *)); +#ifndef NEW_KEYBOARD_HIT E int NDECL((*nt_kbhit)); +#endif E void FDECL(Delay, (int)); #endif /* WIN32 */ #endif /* MICRO || WIN32 */ @@ -1660,10 +1670,15 @@ E void FDECL(regex_free, (struct nhregex *)); #ifdef WIN32 E void NDECL(get_scr_size); +#ifndef NEW_KEYBOARD_HIT E int NDECL(nttty_kbhit); +#endif E void FDECL(nttty_open, (int)); E void NDECL(nttty_rubout); E int NDECL(tgetch); +#ifdef NEW_KEYBOARD_HIT +E boolean NDECL(tkbhit); +#endif E int FDECL(ntposkey, (int *, int *, int *)); E void FDECL(set_output_mode, (int)); E void NDECL(synch_cursor); diff --git a/include/ntconf.h b/include/ntconf.h index eb106090f..994615c2d 100644 --- a/include/ntconf.h +++ b/include/ntconf.h @@ -252,8 +252,11 @@ int _RTLENTRY _EXPFUNC read(int __handle, void _FAR *__buf, unsigned __len); #ifndef CURSES_GRAPHICS #include /* conflicting definitions with curses.h */ #endif + +#ifndef NEW_KEYBOARD_HIT #undef kbhit /* Use our special NT kbhit */ #define kbhit (*nt_kbhit) +#endif #ifdef LAN_FEATURES #define MAX_LAN_USERNAME 20 diff --git a/include/unixconf.h b/include/unixconf.h index c126d6839..f9ac16f0c 100644 --- a/include/unixconf.h +++ b/include/unixconf.h @@ -311,7 +311,9 @@ #define HLOCK "perm" /* an empty file used for locking purposes */ +#ifndef NEW_KEYBOARD_HIT #define tgetch getchar +#endif #ifndef NOSHELL #define SHELL /* do not delete the '!' command */ diff --git a/include/winprocs.h b/include/winprocs.h index 36962750e..d1cea9a3c 100644 --- a/include/winprocs.h +++ b/include/winprocs.h @@ -79,6 +79,9 @@ struct window_procs { (int, const char *, const char *, BOOLEAN_P)); void FDECL((*win_status_update), (int, genericptr_t, int, int, int, unsigned long *)); boolean NDECL((*win_can_suspend)); +#ifdef NEW_KEYBOARD_HIT + boolean NDECL((*win_keyboard_hit)); +#endif }; extern @@ -160,6 +163,9 @@ extern */ #define status_enablefield (*windowprocs.win_status_enablefield) #define status_update (*windowprocs.win_status_update) +#ifdef NEW_KEYBOARD_HIT +#define keyboard_hit (*windowprocs.win_keyboard_hit) +#endif /* * WINCAP @@ -379,6 +385,9 @@ struct chain_procs { (CARGS, int, const char *, const char *, BOOLEAN_P)); void FDECL((*win_status_update), (CARGS, int, genericptr_t, int, int, int, unsigned long *)); boolean FDECL((*win_can_suspend), (CARGS)); +#ifdef NEW_KEYBOARD_HIT + boolean FDECL((*keyboard_hit), (CARGS)); +#endif }; #endif /* WINCHAIN */ @@ -449,6 +458,9 @@ extern void FDECL(safe_status_enablefield, (int, const char *, const char *, BOOLEAN_P)); extern void FDECL(safe_status_update, (int, genericptr_t, int, int, int, unsigned long *)); 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_bold, (const char *)); extern void NDECL(stdio_wait_synch); diff --git a/include/wintty.h b/include/wintty.h index 4863d6e10..6994fc93f 100644 --- a/include/wintty.h +++ b/include/wintty.h @@ -247,6 +247,10 @@ E void FDECL(genl_outrip, (winid, int, time_t)); E char *FDECL(tty_getmsghistory, (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 MAC #ifdef putchar diff --git a/src/allmain.c b/src/allmain.c index b0bb0f3a2..1ce8cad08 100644 --- a/src/allmain.c +++ b/src/allmain.c @@ -374,7 +374,11 @@ boolean resuming; if (multi >= 0 && occupation) { #if defined(MICRO) || defined(WIN32) abort_lev = 0; +#ifdef NEW_KEYBOARD_HIT + if (keyboard_hit()) { +#else if (kbhit()) { +#endif if ((ch = pgetchar()) == ABORT) abort_lev++; else diff --git a/src/windows.c b/src/windows.c index ad7b91b0d..0d2ce514d 100644 --- a/src/windows.c +++ b/src/windows.c @@ -523,6 +523,9 @@ static void NDECL(hup_void_ndecl); static void FDECL(hup_void_fdecl_int, (int)); static void FDECL(hup_void_fdecl_winid, (winid)); 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 = { "hup", 0L, 0L, hup_init_nhwindows, @@ -570,6 +573,9 @@ static struct window_procs hup_procs = { hup_void_ndecl, /* status_finish */ genl_status_enablefield, hup_status_update, genl_can_suspend_no, +#ifdef NEW_KEYBOARD_HIT + hup_keyboard_hit +#endif }; static void FDECL((*previnterface_exit_nhwindows), (const char *)) = 0; @@ -624,6 +630,14 @@ hup_nhgetch(VOID_ARGS) return '\033'; /* ESC */ } +#ifdef NEW_KEYBOARD_HIT +static boolean +hup_keyboard_hit(VOID_ARGS) +{ + return FALSE; +} +#endif + /*ARGSUSED*/ static char hup_yn_function(prompt, resp, deflt) diff --git a/sys/winnt/nttty.c b/sys/winnt/nttty.c index 3233dc9cb..a8000bc05 100644 --- a/sys/winnt/nttty.c +++ b/sys/winnt/nttty.c @@ -398,10 +398,12 @@ int mode; // unused { DWORD cmode; +#ifndef NEW_KEYBOARD_HIT /* Initialize the function pointer that points to * the kbhit() equivalent, in this TTY case nttty_kbhit() */ nt_kbhit = nttty_kbhit; +#endif if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE)) { /* Unable to set control handler */ @@ -436,11 +438,19 @@ int portdebug; return ch; } +#ifndef NEW_KEYBOARD_HIT int nttty_kbhit() { return keyboard_handler.pNHkbhit(console.hConIn, &ir); } +#else +boolean +tkbhit() +{ + return keyboard_handler.pNHkbhit(console.hConIn, &ir) != 0; +} +#endif int tgetch() diff --git a/sys/winnt/winnt.c b/sys/winnt/winnt.c index f6a9aa791..e9041262d 100644 --- a/sys/winnt/winnt.c +++ b/sys/winnt/winnt.c @@ -56,6 +56,7 @@ extern void NDECL(backsp); int NDECL(windows_console_custom_nhgetch); extern void NDECL(safe_routines); +#ifndef NEW_KEYBOARD_HIT /* The function pointer nt_kbhit contains a kbhit() equivalent * which varies depending on which window port is active. * 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 (*nt_kbhit)() = def_kbhit; +#endif char switchar() @@ -158,11 +160,13 @@ max_filename() return 0; } +#ifndef NEW_KEYBOARD_HIT int def_kbhit() { return 0; } +#endif /* * Strip out troublesome file system characters. @@ -510,7 +514,9 @@ int code; exit(code); } +#ifndef NEW_KEYBOARD_HIT #undef kbhit +#endif #include int @@ -719,7 +725,6 @@ sys_random_seed(VOID_ARGS) } return ourseed; } - #endif /* WIN32 */ /*winnt.c*/ diff --git a/win/share/safeproc.c b/win/share/safeproc.c index ab5cb1c5a..9f3e0ce2f 100644 --- a/win/share/safeproc.c +++ b/win/share/safeproc.c @@ -101,6 +101,9 @@ struct window_procs safe_procs = { safe_status_update, #endif safe_can_suspend, +#ifdef NEW_KEYBOARD_HIT + safe_keyboard_hit +#endif }; struct window_procs * @@ -386,6 +389,14 @@ safe_can_suspend() return FALSE; } +#ifdef NEW_KEYBOARD_HIT +boolean +safe_keyboard_hit() +{ + return FALSE; +} +#endif + void safe_nhbell() { diff --git a/win/tty/wintty.c b/win/tty/wintty.c index ac0d61880..1411839da 100644 --- a/win/tty/wintty.c +++ b/win/tty/wintty.c @@ -36,6 +36,12 @@ extern void msmsg(const char *, ...); #endif #endif +#ifdef NEW_KEYBOARD_HIT +#if defined(UNIX) +#include +#endif +#endif + #ifdef TTY_TILES_ESCCODES extern short glyph2tile[]; #define TILE_ANSI_COMMAND 'z' @@ -133,6 +139,9 @@ struct window_procs tty_procs = { genl_status_update, #endif genl_can_suspend_yes, +#ifdef NEW_KEYBOARD_HIT + tty_keyboard_hit +#endif }; static int maxwin = 0; /* number of windows in use */ @@ -3479,6 +3488,7 @@ const char *str; #endif } +#ifndef NEW_KEYBOARD_HIT int tty_nhgetch() { @@ -3533,6 +3543,93 @@ tty_nhgetch() #endif /* TTY_TILES_ESCCODES */ 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 diff --git a/win/win32/mswproc.c b/win/win32/mswproc.c index b637c463f..a5aa02bb4 100644 --- a/win/win32/mswproc.c +++ b/win/win32/mswproc.c @@ -116,6 +116,9 @@ struct window_procs mswin_procs = { mswin_status_init, mswin_status_finish, mswin_status_enablefield, mswin_status_update, genl_can_suspend_yes, +#ifdef NEW_KEYBOARD_HIT + mswin_keyboard_hit +#endif }; /* @@ -1402,6 +1405,15 @@ mswin_nhgetch() 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) -- Returns a single character input from the user or a diff --git a/win/win32/winMS.h b/win/win32/winMS.h index c5ee77368..c649917dc 100644 --- a/win/win32/winMS.h +++ b/win/win32/winMS.h @@ -167,6 +167,9 @@ void mswin_raw_print(const char *str); void mswin_raw_print_bold(const char *str); void mswin_raw_print_flush(); int mswin_nhgetch(void); +#ifdef NEW_KEYBOARD_HIT +boolean mswin_keyboard_hit(void); +#endif int mswin_nh_poskey(int *x, int *y, int *mod); void mswin_nhbell(void); int mswin_doprev_message(void);