From 6f84fb8786a2f94d772e3f95a10e211227885ed2 Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Thu, 5 Feb 2009 22:12:05 +0000
Subject: [PATCH] Support 0xe0 (enhanced keyboard) doorway codes on input. Add
 support for the abort key.

---
 src/conio/ansi_cio.c | 34 ++++++++++++++++++++++++++++++++--
 src/conio/ciolib.c   | 21 ++++++++++++++-------
 2 files changed, 46 insertions(+), 9 deletions(-)

diff --git a/src/conio/ansi_cio.c b/src/conio/ansi_cio.c
index 85ad1dfe10..670ed89e07 100644
--- a/src/conio/ansi_cio.c
+++ b/src/conio/ansi_cio.c
@@ -664,7 +664,37 @@ static void ansi_keyparse(void *par)
 			sem_wait(&got_key);
 
 		ch=ansi_raw_inch;
-		if(gotnull) {
+		if(ch==-2) {
+			ansi_inch=0x0100;
+			sem_post(&got_input);
+			/* Two-byte code, need to post twice times and wait for one to
+			   be received */
+			sem_wait(&used_input);
+			sem_wait(&goahead);
+			sem_post(&got_input);
+			sem_wait(&used_input);
+		}
+		if(gotnull==2) {
+			// 0xe0 enhanced keyboard key... translate to 0x00 key for now.
+
+			ansi_inch=ch<<8;	// (ch<<8)|0xe0;
+			sem_post(&got_input);
+			/* Two-byte code, need to post twice times and wait for one to
+			   be received */
+			sem_wait(&used_input);
+			sem_wait(&goahead);
+			sem_post(&got_input);
+			sem_wait(&used_input);
+			gotnull=0;
+			continue;
+		}
+		if(gotnull==1) {
+			if(ch==0xe0) {
+				gotnull=2;
+				// Need another key... keep looping.
+				sem_post(&goahead);
+				continue;
+			}
 			ansi_inch=ch<<8;
 			sem_post(&got_input);
 			/* Two-byte code, need to post twice and wait for one to
@@ -781,7 +811,7 @@ static void ansi_keythread(void *params)
 		sem_getvalue(&got_key,&sval);
 		if(!sval) {
 			ansi_raw_inch=ciolib_ansi_readbyte_cb();
-			if(ansi_raw_inch >= 0)
+			if(ansi_raw_inch >= 0 || ansi_raw_inch==-2)
 				sem_post(&got_key);
 			else
 				SLEEP(1);
diff --git a/src/conio/ciolib.c b/src/conio/ciolib.c
index c3b15ab514..5acb58dbb4 100644
--- a/src/conio/ciolib.c
+++ b/src/conio/ciolib.c
@@ -404,12 +404,13 @@ CIOLIBEXPORT int CIOLIBCALL ciolib_getche(void)
 	else {
 		while(1) {
 			ch=ciolib_getch();
-			if(ch) {
+			if(ch != 0 && ch != 0xe0) {
 				ciolib_putch(ch);
 				return(ch);
 			}
-			/* Eat extended chars */
-			ciolib_getch();
+			/* Eat extended chars - except ESC which is an abort */
+			if(ciolib_getch()==1)
+				return(EOF);
 		}
 	}
 }
@@ -466,8 +467,10 @@ CIOLIBEXPORT char * CIOLIBCALL ciolib_cgets(char *str)
 	maxlen=*(unsigned char *)str;
 	while((ch=ciolib_getch())!='\n' && ch !='\r') {
 		switch(ch) {
-			case 0:	/* Skip extended keys */
-				ciolib_getche();
+			case 0:		/* Skip extended keys */
+			case 0xe0:	/* Skip extended keys */
+				if(ciolib_getche()==1)
+					goto early_return;
 				break;
 			case '\r':	/* Skip \r (ToDo: Should this be treated as a \n? */
 				break;
@@ -492,6 +495,7 @@ CIOLIBEXPORT char * CIOLIBCALL ciolib_cgets(char *str)
 				break;
 		}
 	}
+early_return;
 	str[len+2]=0;
 	*((unsigned char *)(str+1))=(unsigned char)len;
 	ciolib_putch('\r');
@@ -563,8 +567,10 @@ CIOLIBEXPORT char * CIOLIBCALL ciolib_getpass(const char *prompt)
 	ciolib_cputs((char *)prompt);
 	while((ch=ciolib_getch())!='\n') {
 		switch(ch) {
-			case 0:	/* Skip extended keys */
-				ciolib_getch();
+			case 0:		/* Skip extended keys */
+			case 0xe0:	/* Skip extended keys */
+				if(ciolib_getch()==1)
+					goto early_return;
 				break;
 			case '\r':	/* Skip \r (ToDo: Should this be treeated as a \n? */
 				break;
@@ -583,6 +589,7 @@ CIOLIBEXPORT char * CIOLIBCALL ciolib_getpass(const char *prompt)
 				break;
 		}
 	}
+early_return:
 	pass[len]=0;
 	return(pass);
 }
-- 
GitLab