started backporting the eradicate code
[dosdemo] / src / dos / keyb.c
index 98a1f3d..ac200b3 100644 (file)
@@ -20,6 +20,7 @@ along with the program. If not, see <http://www.gnu.org/licenses/>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #include <conio.h>
 #include <dos.h>
 
@@ -27,7 +28,6 @@ along with the program. If not, see <http://www.gnu.org/licenses/>
 #include <i86.h>
 #endif
 #ifdef __DJGPP__
-#include <stdint.h>
 #include <dpmi.h>
 #include <go32.h>
 #include <pc.h>
@@ -35,6 +35,7 @@ along with the program. If not, see <http://www.gnu.org/licenses/>
 
 #include "keyb.h"
 #include "scancode.h"
+#include "inttypes.h"
 
 #define KB_INTR                0x9
 #define KB_PORT                0x60
@@ -138,6 +139,10 @@ int kb_isdown(int key)
        case KB_CTRL:
                return keystate[KB_LCTRL] + keystate[KB_RCTRL];
        }
+
+       if(isalpha(key)) {
+               key = tolower(key);
+       }
        return keystate[key];
 }
 
@@ -156,10 +161,12 @@ void kb_wait(void)
 {
        int key;
        while((key = kb_getkey()) == -1) {
+#ifdef USE_HLT
                /* put the processor to sleep while waiting for keypresses, but first
                 * make sure interrupts are enabled, or we'll sleep forever
                 */
                halt();
+#endif
        }
        kb_putback(key);
 }
@@ -206,13 +213,19 @@ void kb_putback(int key)
 static void INTERRUPT kbintr()
 {
        unsigned char code;
-       int key, press;
+       int key, c, press;
+       static int ext;
 
        code = inp(KB_PORT);
 
-       if(code >= 128) {
+       if(code == 0xe0) {
+               ext = 1;
+               goto eoi;
+       }
+
+       if(code & 0x80) {
                press = 0;
-               code -= 128;
+               code &= 0x7f;
 
                if(num_pressed > 0) {
                        num_pressed--;
@@ -223,13 +236,20 @@ static void INTERRUPT kbintr()
                num_pressed++;
        }
 
-       key = scantbl[code];
+       if(ext) {
+               key = scantbl_ext[code];
+               c = key;
+               ext = 0;
+       } else {
+               key = scantbl[code];
+               c = (keystate[KB_LSHIFT] | keystate[KB_RSHIFT]) ? scantbl_shift[code] : key;
+       }
 
        if(press) {
                /* append to buffer */
-               last_key = key;
+               last_key = c;
                if(buffer_size > 0) {
-                       buffer[buf_widx] = key;
+                       buffer[buf_widx] = c;
                        ADVANCE(buf_widx);
                        /* if the write end overtook the read end, advance the read end
                         * too, to discard the oldest keypress from the buffer
@@ -243,5 +263,6 @@ static void INTERRUPT kbintr()
        /* and update keystate table */
        keystate[key] = press;
 
+eoi:
        outp(PIC1_CMD_PORT, OCW2_EOI);  /* send end-of-interrupt */
 }