Skip to content
Snippets Groups Projects
Commit bec4667e authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Optionally distinguish between timeout and NUL inkey() result

Some terminals can send NUL (ASCII 0), e.g. hitting Ctrl-Space in Apple iTerm. Allow users of inkey() (in C++ or JS) to optionally detect the difference by specifying the K_NUL mode flag. In JS, console.inkey() will return null upon timeout (rather than "") when the K_NUL mode flag is used and return an empty string ("") upon receipt of NUL (ASCII 0).

The default is the previous behavior where a timeout and the receipt of a NUL character appear the same to the caller of inkey().
parent 9439f5b8
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
/* Synchronet single key input function (no wait) */
/* $Id: inkey.cpp,v 1.80 2020/08/04 04:56:37 rswindell Exp $ */
/* Synchronet single key input function */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
......@@ -15,21 +13,9 @@
* See the GNU General Public License for more details: gpl.txt or *
* http://www.fsf.org/copyleft/gpl.html *
* *
* Anonymous FTP access to the most recent released source is available at *
* ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
* *
* Anonymous CVS access to the development source and modification history *
* is available at cvs.synchro.net:/cvsroot/sbbs, example: *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login *
* (just hit return, no password is necessary) *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src *
* *
* For Synchronet coding style and modification guidelines, see *
* http://www.synchro.net/source.html *
* *
* You are encouraged to submit any modifications (preferably in Unix diff *
* format) via e-mail to mods@synchro.net *
* *
* Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/
......@@ -95,18 +81,20 @@ int kbincom(sbbs_t* sbbs, unsigned long timeout)
/****************************************************************************/
/* Returns character if a key has been hit remotely and responds */
/* Called from functions getkey, msgabort and main_sec */
/* May return NOINP on timeout instead of '\0' when K_NUL mode is used. */
/****************************************************************************/
char sbbs_t::inkey(long mode, unsigned long timeout)
int sbbs_t::inkey(long mode, unsigned long timeout)
{
uchar ch=0;
int ch=0;
ch=kbincom(this,timeout);
if(ch==0) {
if(ch == NOINP) {
if(sys_status&SS_SYSPAGE)
sbbs_beep(sbbs_random(800),100);
return(0);
if(mode & K_NUL) // distinguish between timeout and '\0'
return NOINP;
return 0;
}
if(cfg.node_misc&NM_7BITONLY
......
/* js_console.cpp */
/* Synchronet JavaScript "Console" Object */
/* $Id: js_console.cpp,v 1.154 2020/05/24 08:16:52 rswindell Exp $ */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
......@@ -17,21 +13,9 @@
* See the GNU General Public License for more details: gpl.txt or *
* http://www.fsf.org/copyleft/gpl.html *
* *
* Anonymous FTP access to the most recent released source is available at *
* ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
* *
* Anonymous CVS access to the development source and modification history *
* is available at cvs.synchro.net:/cvsroot/sbbs, example: *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login *
* (just hit return, no password is necessary) *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src *
* *
* For Synchronet coding style and modification guidelines, see *
* http://www.synchro.net/source.html *
* *
* You are encouraged to submit any modifications (preferably in Unix diff *
* format) via e-mail to mods@synchro.net *
* *
* Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/
......@@ -458,6 +442,7 @@ js_inkey(JSContext *cx, uintN argc, jsval *arglist)
{
jsval *argv=JS_ARGV(cx, arglist);
char key[2];
int ch;
int32 mode=0;
int32 timeout=0;
sbbs_t* sbbs;
......@@ -467,7 +452,7 @@ js_inkey(JSContext *cx, uintN argc, jsval *arglist)
if((sbbs=(sbbs_t*)js_GetClassPrivate(cx, JS_THIS_OBJECT(cx, arglist), &js_console_class))==NULL)
return(JS_FALSE);
JS_SET_RVAL(cx, arglist, JSVAL_VOID);
JS_SET_RVAL(cx, arglist, JSVAL_NULL);
if(argc) {
if(!JS_ValueToInt32(cx,argv[0],&mode))
......@@ -478,14 +463,15 @@ js_inkey(JSContext *cx, uintN argc, jsval *arglist)
return JS_FALSE;
}
rc=JS_SUSPENDREQUEST(cx);
key[0]=sbbs->inkey(mode,timeout);
ch = sbbs->inkey(mode,timeout);
if(ch != NOINP) {
key[0]=ch;
key[1]=0;
if((js_str = JS_NewStringCopyZ(cx, key))==NULL)
return(JS_FALSE);
JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
}
JS_RESUMEREQUEST(cx, rc);
key[1]=0;
if((js_str = JS_NewStringCopyZ(cx, key))==NULL)
return(JS_FALSE);
JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
return(JS_TRUE);
}
......@@ -2118,7 +2104,7 @@ js_term_updated(JSContext *cx, uintN argc, jsval *arglist)
static jsSyncMethodSpec js_console_functions[] = {
{"inkey", js_inkey, 0, JSTYPE_STRING, JSDOCSTR("[mode=<tt>K_NONE</tt>] [,timeout=<tt>0</tt>]")
,JSDOCSTR("get a single key with optional <i>timeout</i> in milliseconds (defaults to 0, for no wait).<br>"
"Returns an empty string if there is no input (e.g. timeout occurs).<br>"
"Returns an empty string (or <tt>null</tt> when the <tt>K_NUL</tt> mode flag is used) if there is no input (e.g. timeout occurs).<br>"
"See <tt>K_*</tt> in <tt>sbbsdefs.js</tt> for <i>mode</i> flags.")
,311
},
......
......@@ -824,7 +824,7 @@ public:
void mnemonics(const char *str);
/* inkey.cpp */
char inkey(long mode, unsigned long timeout=0);
int inkey(long mode, unsigned long timeout=0);
char handle_ctrlkey(char ch, long mode=0);
// Terminal mouse reporting mode (mouse_mode)
......
......@@ -760,6 +760,7 @@ typedef enum { /* Values for xtrn_t.event */
#define K_ANSI_CPR (1L<<22) /* Expect ANSI Cursor Position Report */
#define K_TRIM (1L<<23) /* Trimmed white-space */
#define K_CTRLKEYS (1L<<24) /* No control-key handling/eating in inkey()*/
#define K_NUL (1L<<25) /* Return NOINP on timeout instead of '\0' */
/* Bits in 'mode' for putmsg and printfile */
#define P_NONE 0 /* No mode flags */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment