]> 4ch.mooo.com Git - 16.git/blobdiff - 16/sod8086/id_in.c
got 8086 port of wolf3d to work and sod to work
[16.git] / 16 / sod8086 / id_in.c
diff --git a/16/sod8086/id_in.c b/16/sod8086/id_in.c
new file mode 100755 (executable)
index 0000000..3f7c89d
--- /dev/null
@@ -0,0 +1,990 @@
+//\r
+//     ID Engine\r
+//     ID_IN.c - Input Manager\r
+//     v1.0d1\r
+//     By Jason Blochowiak\r
+//\r
+\r
+//\r
+//     This module handles dealing with the various input devices\r
+//\r
+//     Depends on: Memory Mgr (for demo recording), Sound Mgr (for timing stuff),\r
+//                             User Mgr (for command line parms)\r
+//\r
+//     Globals:\r
+//             LastScan - The keyboard scan code of the last key pressed\r
+//             LastASCII - The ASCII value of the last key pressed\r
+//     DEBUG - there are more globals\r
+//\r
+\r
+#include "ID_HEADS.H"\r
+#pragma        hdrstop\r
+\r
+#define        KeyInt          9       // The keyboard ISR number\r
+\r
+//\r
+// mouse constants\r
+//\r
+#define        MReset          0\r
+#define        MButtons        3\r
+#define        MDelta          11\r
+\r
+#define        MouseInt        0x33\r
+#define        Mouse(x)        _AX = x,geninterrupt(MouseInt)\r
+\r
+//\r
+// joystick constants\r
+//\r
+#define        JoyScaleMax             32768\r
+#define        JoyScaleShift   8\r
+#define        MaxJoyValue             5000\r
+\r
+/*\r
+=============================================================================\r
+\r
+                                       GLOBAL VARIABLES\r
+\r
+=============================================================================\r
+*/\r
+\r
+//\r
+// configuration variables\r
+//\r
+boolean                        MousePresent;\r
+boolean                        JoysPresent[MaxJoys];\r
+boolean                        JoyPadPresent;\r
+\r
+\r
+//     Global variables\r
+               boolean         Keyboard[NumCodes];\r
+               boolean         Paused;\r
+               char            LastASCII;\r
+               ScanCode        LastScan;\r
+\r
+               KeyboardDef     KbdDefs = {0x1d,0x38,0x47,0x48,0x49,0x4b,0x4d,0x4f,0x50,0x51};\r
+               JoystickDef     JoyDefs[MaxJoys];\r
+               ControlType     Controls[MaxPlayers];\r
+\r
+               longword        MouseDownCount;\r
+\r
+               Demo            DemoMode = demo_Off;\r
+               byte _seg       *DemoBuffer;\r
+               word            DemoOffset,DemoSize;\r
+\r
+/*\r
+=============================================================================\r
+\r
+                                       LOCAL VARIABLES\r
+\r
+=============================================================================\r
+*/\r
+static byte        far ASCIINames[] =          // Unshifted ASCII for scan codes\r
+                                       {\r
+//      0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F\r
+       0  ,27 ,'1','2','3','4','5','6','7','8','9','0','-','=',8  ,9  ,        // 0\r
+       'q','w','e','r','t','y','u','i','o','p','[',']',13 ,0  ,'a','s',        // 1\r
+       'd','f','g','h','j','k','l',';',39 ,'`',0  ,92 ,'z','x','c','v',        // 2\r
+       'b','n','m',',','.','/',0  ,'*',0  ,' ',0  ,0  ,0  ,0  ,0  ,0  ,        // 3\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,'7','8','9','-','4','5','6','+','1',        // 4\r
+       '2','3','0',127,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 5\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 6\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0           // 7\r
+                                       },\r
+                                       far ShiftNames[] =              // Shifted ASCII for scan codes\r
+                                       {\r
+//      0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F\r
+       0  ,27 ,'!','@','#','$','%','^','&','*','(',')','_','+',8  ,9  ,        // 0\r
+       'Q','W','E','R','T','Y','U','I','O','P','{','}',13 ,0  ,'A','S',        // 1\r
+       'D','F','G','H','J','K','L',':',34 ,'~',0  ,'|','Z','X','C','V',        // 2\r
+       'B','N','M','<','>','?',0  ,'*',0  ,' ',0  ,0  ,0  ,0  ,0  ,0  ,        // 3\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,'7','8','9','-','4','5','6','+','1',        // 4\r
+       '2','3','0',127,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 5\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 6\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0           // 7\r
+                                       },\r
+                                       far SpecialNames[] =    // ASCII for 0xe0 prefixed codes\r
+                                       {\r
+//      0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 0\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,13 ,0  ,0  ,0  ,        // 1\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 2\r
+       0  ,0  ,0  ,0  ,0  ,'/',0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 3\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 4\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 5\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 6\r
+       0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0           // 7\r
+                                       };\r
+\r
+\r
+static boolean         IN_Started;\r
+static boolean         CapsLock;\r
+static ScanCode        CurCode,LastCode;\r
+\r
+static Direction       DirTable[] =            // Quick lookup for total direction\r
+                                       {\r
+                                               dir_NorthWest,  dir_North,      dir_NorthEast,\r
+                                               dir_West,               dir_None,       dir_East,\r
+                                               dir_SouthWest,  dir_South,      dir_SouthEast\r
+                                       };\r
+\r
+static void                    (*INL_KeyHook)(void);\r
+static void interrupt  (*OldKeyVect)(void);\r
+\r
+static char                    *ParmStrings[] = {"nojoys","nomouse",nil};\r
+\r
+//     Internal routines\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_KeyService() - Handles a keyboard interrupt (key up/down)\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static void interrupt\r
+INL_KeyService(void)\r
+{\r
+static boolean special;\r
+               byte    k,c,\r
+                               temp;\r
+               int             i;\r
+\r
+       k = inportb(0x60);      // Get the scan code\r
+\r
+       // Tell the XT keyboard controller to clear the key\r
+       outportb(0x61,(temp = inportb(0x61)) | 0x80);\r
+       outportb(0x61,temp);\r
+\r
+       if (k == 0xe0)          // Special key prefix\r
+               special = true;\r
+       else if (k == 0xe1)     // Handle Pause key\r
+               Paused = true;\r
+       else\r
+       {\r
+               if (k & 0x80)   // Break code\r
+               {\r
+                       k &= 0x7f;\r
+\r
+// DEBUG - handle special keys: ctl-alt-delete, print scrn\r
+\r
+                       Keyboard[k] = false;\r
+               }\r
+               else                    // Make code\r
+               {\r
+                       LastCode = CurCode;\r
+                       CurCode = LastScan = k;\r
+                       Keyboard[k] = true;\r
+\r
+                       if (special)\r
+                               c = SpecialNames[k];\r
+                       else\r
+                       {\r
+                               if (k == sc_CapsLock)\r
+                               {\r
+                                       CapsLock ^= true;\r
+                                       // DEBUG - make caps lock light work\r
+                               }\r
+\r
+                               if (Keyboard[sc_LShift] || Keyboard[sc_RShift]) // If shifted\r
+                               {\r
+                                       c = ShiftNames[k];\r
+                                       if ((c >= 'A') && (c <= 'Z') && CapsLock)\r
+                                               c += 'a' - 'A';\r
+                               }\r
+                               else\r
+                               {\r
+                                       c = ASCIINames[k];\r
+                                       if ((c >= 'a') && (c <= 'z') && CapsLock)\r
+                                               c -= 'a' - 'A';\r
+                               }\r
+                       }\r
+                       if (c)\r
+                               LastASCII = c;\r
+               }\r
+\r
+               special = false;\r
+       }\r
+\r
+       if (INL_KeyHook && !special)\r
+               INL_KeyHook();\r
+       outportb(0x20,0x20);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_GetMouseDelta() - Gets the amount that the mouse has moved from the\r
+//             mouse driver\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static void\r
+INL_GetMouseDelta(int *x,int *y)\r
+{\r
+       Mouse(MDelta);\r
+       *x = _CX;\r
+       *y = _DX;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_GetMouseButtons() - Gets the status of the mouse buttons from the\r
+//             mouse driver\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static word\r
+INL_GetMouseButtons(void)\r
+{\r
+       word    buttons;\r
+\r
+       Mouse(MButtons);\r
+       buttons = _BX;\r
+       return(buttons);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_GetJoyAbs() - Reads the absolute position of the specified joystick\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_GetJoyAbs(word joy,word *xp,word *yp)\r
+{\r
+       byte    xb,yb,\r
+                       xs,ys;\r
+       word    x,y;\r
+\r
+       x = y = 0;\r
+       xs = joy? 2 : 0;                // Find shift value for x axis\r
+       xb = 1 << xs;                   // Use shift value to get x bit mask\r
+       ys = joy? 3 : 1;                // Do the same for y axis\r
+       yb = 1 << ys;\r
+\r
+// Read the absolute joystick values\r
+asm            pushf                           // Save some registers\r
+asm            push    si\r
+asm            push    di\r
+asm            cli                                     // Make sure an interrupt doesn't screw the timings\r
+\r
+\r
+asm            mov             dx,0x201\r
+asm            in              al,dx\r
+asm            out             dx,al           // Clear the resistors\r
+\r
+asm            mov             ah,[xb]         // Get masks into registers\r
+asm            mov             ch,[yb]\r
+\r
+asm            xor             si,si           // Clear count registers\r
+asm            xor             di,di\r
+asm            xor             bh,bh           // Clear high byte of bx for later\r
+\r
+asm            push    bp                      // Don't mess up stack frame\r
+asm            mov             bp,MaxJoyValue\r
+\r
+loop:\r
+asm            in              al,dx           // Get bits indicating whether all are finished\r
+\r
+asm            dec             bp                      // Check bounding register\r
+asm            jz              done            // We have a silly value - abort\r
+\r
+asm            mov             bl,al           // Duplicate the bits\r
+asm            and             bl,ah           // Mask off useless bits (in [xb])\r
+asm            add             si,bx           // Possibly increment count register\r
+asm            mov             cl,bl           // Save for testing later\r
+\r
+asm            mov             bl,al\r
+asm            and             bl,ch           // [yb]\r
+asm            add             di,bx\r
+\r
+asm            add             cl,bl\r
+asm            jnz             loop            // If both bits were 0, drop out\r
+\r
+done:\r
+asm     pop            bp\r
+\r
+asm            mov             cl,[xs]         // Get the number of bits to shift\r
+asm            shr             si,cl           //  and shift the count that many times\r
+\r
+asm            mov             cl,[ys]\r
+asm            shr             di,cl\r
+\r
+asm            mov             [x],si          // Store the values into the variables\r
+asm            mov             [y],di\r
+\r
+asm            pop             di\r
+asm            pop             si\r
+asm            popf                            // Restore the registers\r
+\r
+       *xp = x;\r
+       *yp = y;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_GetJoyDelta() - Returns the relative movement of the specified\r
+//             joystick (from +/-127)\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void INL_GetJoyDelta(word joy,int *dx,int *dy)\r
+{\r
+       word            x,y;\r
+       longword        time;\r
+       JoystickDef     *def;\r
+static longword        lasttime;\r
+\r
+       IN_GetJoyAbs(joy,&x,&y);\r
+       def = JoyDefs + joy;\r
+\r
+       if (x < def->threshMinX)\r
+       {\r
+               if (x < def->joyMinX)\r
+                       x = def->joyMinX;\r
+\r
+               x = -(x - def->threshMinX);\r
+               x *= def->joyMultXL;\r
+               x >>= JoyScaleShift;\r
+               *dx = (x > 127)? -127 : -x;\r
+       }\r
+       else if (x > def->threshMaxX)\r
+       {\r
+               if (x > def->joyMaxX)\r
+                       x = def->joyMaxX;\r
+\r
+               x = x - def->threshMaxX;\r
+               x *= def->joyMultXH;\r
+               x >>= JoyScaleShift;\r
+               *dx = (x > 127)? 127 : x;\r
+       }\r
+       else\r
+               *dx = 0;\r
+\r
+       if (y < def->threshMinY)\r
+       {\r
+               if (y < def->joyMinY)\r
+                       y = def->joyMinY;\r
+\r
+               y = -(y - def->threshMinY);\r
+               y *= def->joyMultYL;\r
+               y >>= JoyScaleShift;\r
+               *dy = (y > 127)? -127 : -y;\r
+       }\r
+       else if (y > def->threshMaxY)\r
+       {\r
+               if (y > def->joyMaxY)\r
+                       y = def->joyMaxY;\r
+\r
+               y = y - def->threshMaxY;\r
+               y *= def->joyMultYH;\r
+               y >>= JoyScaleShift;\r
+               *dy = (y > 127)? 127 : y;\r
+       }\r
+       else\r
+               *dy = 0;\r
+\r
+       lasttime = TimeCount;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_GetJoyButtons() - Returns the button status of the specified\r
+//             joystick\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static word\r
+INL_GetJoyButtons(word joy)\r
+{\r
+register       word    result;\r
+\r
+       result = inportb(0x201);        // Get all the joystick buttons\r
+       result >>= joy? 6 : 4;  // Shift into bits 0-1\r
+       result &= 3;                            // Mask off the useless bits\r
+       result ^= 3;\r
+       return(result);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_GetJoyButtonsDB() - Returns the de-bounced button status of the\r
+//             specified joystick\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+word\r
+IN_GetJoyButtonsDB(word joy)\r
+{\r
+       longword        lasttime;\r
+       word            result1,result2;\r
+\r
+       do\r
+       {\r
+               result1 = INL_GetJoyButtons(joy);\r
+               lasttime = TimeCount;\r
+               while (TimeCount == lasttime)\r
+                       ;\r
+               result2 = INL_GetJoyButtons(joy);\r
+       } while (result1 != result2);\r
+       return(result1);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_StartKbd() - Sets up my keyboard stuff for use\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static void\r
+INL_StartKbd(void)\r
+{\r
+       INL_KeyHook = NULL;                     // no key hook routine\r
+\r
+       IN_ClearKeysDown();\r
+\r
+       OldKeyVect = getvect(KeyInt);\r
+       setvect(KeyInt,INL_KeyService);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_ShutKbd() - Restores keyboard control to the BIOS\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static void\r
+INL_ShutKbd(void)\r
+{\r
+       poke(0x40,0x17,peek(0x40,0x17) & 0xfaf0);       // Clear ctrl/alt/shift flags\r
+\r
+       setvect(KeyInt,OldKeyVect);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_StartMouse() - Detects and sets up the mouse\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static boolean\r
+INL_StartMouse(void)\r
+{\r
+#if 0\r
+       if (getvect(MouseInt))\r
+       {\r
+               Mouse(MReset);\r
+               if (_AX == 0xffff)\r
+                       return(true);\r
+       }\r
+       return(false);\r
+#endif\r
+ union REGS regs;\r
+ unsigned char far *vector;\r
+\r
+\r
+ if ((vector=MK_FP(peek(0,0x33*4+2),peek(0,0x33*4)))==NULL)\r
+   return false;\r
+\r
+ if (*vector == 207)\r
+   return false;\r
+\r
+ Mouse(MReset);\r
+ return true;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_ShutMouse() - Cleans up after the mouse\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static void\r
+INL_ShutMouse(void)\r
+{\r
+}\r
+\r
+//\r
+//     INL_SetJoyScale() - Sets up scaling values for the specified joystick\r
+//\r
+static void\r
+INL_SetJoyScale(word joy)\r
+{\r
+       JoystickDef     *def;\r
+\r
+       def = &JoyDefs[joy];\r
+       def->joyMultXL = JoyScaleMax / (def->threshMinX - def->joyMinX);\r
+       def->joyMultXH = JoyScaleMax / (def->joyMaxX - def->threshMaxX);\r
+       def->joyMultYL = JoyScaleMax / (def->threshMinY - def->joyMinY);\r
+       def->joyMultYH = JoyScaleMax / (def->joyMaxY - def->threshMaxY);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale()\r
+//             to set up scaling values\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy)\r
+{\r
+       word            d,r;\r
+       JoystickDef     *def;\r
+\r
+       def = &JoyDefs[joy];\r
+\r
+       def->joyMinX = minx;\r
+       def->joyMaxX = maxx;\r
+       r = maxx - minx;\r
+       d = r / 3;\r
+       def->threshMinX = ((r / 2) - d) + minx;\r
+       def->threshMaxX = ((r / 2) + d) + minx;\r
+\r
+       def->joyMinY = miny;\r
+       def->joyMaxY = maxy;\r
+       r = maxy - miny;\r
+       d = r / 3;\r
+       def->threshMinY = ((r / 2) - d) + miny;\r
+       def->threshMaxY = ((r / 2) + d) + miny;\r
+\r
+       INL_SetJoyScale(joy);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_StartJoy() - Detects & auto-configures the specified joystick\r
+//                                     The auto-config assumes the joystick is centered\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static boolean\r
+INL_StartJoy(word joy)\r
+{\r
+       word            x,y;\r
+\r
+       IN_GetJoyAbs(joy,&x,&y);\r
+\r
+       if\r
+       (\r
+               ((x == 0) || (x > MaxJoyValue - 10))\r
+       ||      ((y == 0) || (y > MaxJoyValue - 10))\r
+       )\r
+               return(false);\r
+       else\r
+       {\r
+               IN_SetupJoy(joy,0,x * 2,0,y * 2);\r
+               return(true);\r
+       }\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     INL_ShutJoy() - Cleans up the joystick stuff\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+static void\r
+INL_ShutJoy(word joy)\r
+{\r
+       JoysPresent[joy] = false;\r
+}\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_Startup() - Starts up the Input Mgr\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_Startup(void)\r
+{\r
+       boolean checkjoys,checkmouse;\r
+       word    i;\r
+\r
+       if (IN_Started)\r
+               return;\r
+\r
+       checkjoys = true;\r
+       checkmouse = true;\r
+       for (i = 1;i < _argc;i++)\r
+       {\r
+               switch (US_CheckParm(_argv[i],ParmStrings))\r
+               {\r
+               case 0:\r
+                       checkjoys = false;\r
+                       break;\r
+               case 1:\r
+                       checkmouse = false;\r
+                       break;\r
+               }\r
+       }\r
+\r
+       INL_StartKbd();\r
+       MousePresent = checkmouse? INL_StartMouse() : false;\r
+\r
+       for (i = 0;i < MaxJoys;i++)\r
+               JoysPresent[i] = checkjoys? INL_StartJoy(i) : false;\r
+\r
+       IN_Started = true;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_Default() - Sets up default conditions for the Input Mgr\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_Default(boolean gotit,ControlType in)\r
+{\r
+       if\r
+       (\r
+               (!gotit)\r
+       ||      ((in == ctrl_Joystick1) && !JoysPresent[0])\r
+       ||      ((in == ctrl_Joystick2) && !JoysPresent[1])\r
+       ||      ((in == ctrl_Mouse) && !MousePresent)\r
+       )\r
+               in = ctrl_Keyboard1;\r
+       IN_SetControlType(0,in);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_Shutdown() - Shuts down the Input Mgr\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_Shutdown(void)\r
+{\r
+       word    i;\r
+\r
+       if (!IN_Started)\r
+               return;\r
+\r
+       INL_ShutMouse();\r
+       for (i = 0;i < MaxJoys;i++)\r
+               INL_ShutJoy(i);\r
+       INL_ShutKbd();\r
+\r
+       IN_Started = false;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_SetKeyHook() - Sets the routine that gets called by INL_KeyService()\r
+//                     everytime a real make/break code gets hit\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_SetKeyHook(void (*hook)())\r
+{\r
+       INL_KeyHook = hook;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_ClearKeysDown() - Clears the keyboard array\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_ClearKeysDown(void)\r
+{\r
+       int     i;\r
+\r
+       LastScan = sc_None;\r
+       LastASCII = key_None;\r
+       memset (Keyboard,0,sizeof(Keyboard));\r
+}\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_ReadControl() - Reads the device associated with the specified\r
+//             player and fills in the control info struct\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_ReadControl(int player,ControlInfo *info)\r
+{\r
+                       boolean         realdelta;\r
+                       byte            dbyte;\r
+                       word            buttons;\r
+                       int                     dx,dy;\r
+                       Motion          mx,my;\r
+                       ControlType     type;\r
+register       KeyboardDef     *def;\r
+\r
+       dx = dy = 0;\r
+       mx = my = motion_None;\r
+       buttons = 0;\r
+\r
+       if (DemoMode == demo_Playback)\r
+       {\r
+               dbyte = DemoBuffer[DemoOffset + 1];\r
+               my = (dbyte & 3) - 1;\r
+               mx = ((dbyte >> 2) & 3) - 1;\r
+               buttons = (dbyte >> 4) & 3;\r
+\r
+               if (!(--DemoBuffer[DemoOffset]))\r
+               {\r
+                       DemoOffset += 2;\r
+                       if (DemoOffset >= DemoSize)\r
+                               DemoMode = demo_PlayDone;\r
+               }\r
+\r
+               realdelta = false;\r
+       }\r
+       else if (DemoMode == demo_PlayDone)\r
+               Quit("Demo playback exceeded");\r
+       else\r
+       {\r
+               switch (type = Controls[player])\r
+               {\r
+               case ctrl_Keyboard:\r
+                       def = &KbdDefs;\r
+\r
+                       if (Keyboard[def->upleft])\r
+                               mx = motion_Left,my = motion_Up;\r
+                       else if (Keyboard[def->upright])\r
+                               mx = motion_Right,my = motion_Up;\r
+                       else if (Keyboard[def->downleft])\r
+                               mx = motion_Left,my = motion_Down;\r
+                       else if (Keyboard[def->downright])\r
+                               mx = motion_Right,my = motion_Down;\r
+\r
+                       if (Keyboard[def->up])\r
+                               my = motion_Up;\r
+                       else if (Keyboard[def->down])\r
+                               my = motion_Down;\r
+\r
+                       if (Keyboard[def->left])\r
+                               mx = motion_Left;\r
+                       else if (Keyboard[def->right])\r
+                               mx = motion_Right;\r
+\r
+                       if (Keyboard[def->button0])\r
+                               buttons += 1 << 0;\r
+                       if (Keyboard[def->button1])\r
+                               buttons += 1 << 1;\r
+                       realdelta = false;\r
+                       break;\r
+               case ctrl_Joystick1:\r
+               case ctrl_Joystick2:\r
+                       INL_GetJoyDelta(type - ctrl_Joystick,&dx,&dy);\r
+                       buttons = INL_GetJoyButtons(type - ctrl_Joystick);\r
+                       realdelta = true;\r
+                       break;\r
+               case ctrl_Mouse:\r
+                       INL_GetMouseDelta(&dx,&dy);\r
+                       buttons = INL_GetMouseButtons();\r
+                       realdelta = true;\r
+                       break;\r
+               }\r
+       }\r
+\r
+       if (realdelta)\r
+       {\r
+               mx = (dx < 0)? motion_Left : ((dx > 0)? motion_Right : motion_None);\r
+               my = (dy < 0)? motion_Up : ((dy > 0)? motion_Down : motion_None);\r
+       }\r
+       else\r
+       {\r
+               dx = mx * 127;\r
+               dy = my * 127;\r
+       }\r
+\r
+       info->x = dx;\r
+       info->xaxis = mx;\r
+       info->y = dy;\r
+       info->yaxis = my;\r
+       info->button0 = buttons & (1 << 0);\r
+       info->button1 = buttons & (1 << 1);\r
+       info->button2 = buttons & (1 << 2);\r
+       info->button3 = buttons & (1 << 3);\r
+       info->dir = DirTable[((my + 1) * 3) + (mx + 1)];\r
+\r
+       if (DemoMode == demo_Record)\r
+       {\r
+               // Pack the control info into a byte\r
+               dbyte = (buttons << 4) | ((mx + 1) << 2) | (my + 1);\r
+\r
+               if\r
+               (\r
+                       (DemoBuffer[DemoOffset + 1] == dbyte)\r
+               &&      (DemoBuffer[DemoOffset] < 255)\r
+               )\r
+                       (DemoBuffer[DemoOffset])++;\r
+               else\r
+               {\r
+                       if (DemoOffset || DemoBuffer[DemoOffset])\r
+                               DemoOffset += 2;\r
+\r
+                       if (DemoOffset >= DemoSize)\r
+                               Quit("Demo buffer overflow");\r
+\r
+                       DemoBuffer[DemoOffset] = 1;\r
+                       DemoBuffer[DemoOffset + 1] = dbyte;\r
+               }\r
+       }\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_SetControlType() - Sets the control type to be used by the specified\r
+//             player\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+void\r
+IN_SetControlType(int player,ControlType type)\r
+{\r
+       // DEBUG - check that requested type is present?\r
+       Controls[player] = type;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_WaitForKey() - Waits for a scan code, then clears LastScan and\r
+//             returns the scan code\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+ScanCode\r
+IN_WaitForKey(void)\r
+{\r
+       ScanCode        result;\r
+\r
+       while (!(result = LastScan))\r
+               ;\r
+       LastScan = 0;\r
+       return(result);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_WaitForASCII() - Waits for an ASCII char, then clears LastASCII and\r
+//             returns the ASCII value\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+char\r
+IN_WaitForASCII(void)\r
+{\r
+       char            result;\r
+\r
+       while (!(result = LastASCII))\r
+               ;\r
+       LastASCII = '\0';\r
+       return(result);\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_Ack() - waits for a button or key press.  If a button is down, upon\r
+// calling, it must be released for it to be recognized\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+\r
+boolean        btnstate[8];\r
+\r
+void IN_StartAck(void)\r
+{\r
+       unsigned        i,buttons;\r
+\r
+//\r
+// get initial state of everything\r
+//\r
+       IN_ClearKeysDown();\r
+       memset (btnstate,0,sizeof(btnstate));\r
+\r
+       buttons = IN_JoyButtons () << 4;\r
+       if (MousePresent)\r
+               buttons |= IN_MouseButtons ();\r
+\r
+       for (i=0;i<8;i++,buttons>>=1)\r
+               if (buttons&1)\r
+                       btnstate[i] = true;\r
+}\r
+\r
+\r
+boolean IN_CheckAck (void)\r
+{\r
+       unsigned        i,buttons;\r
+\r
+//\r
+// see if something has been pressed\r
+//\r
+       if (LastScan)\r
+               return true;\r
+\r
+       buttons = IN_JoyButtons () << 4;\r
+       if (MousePresent)\r
+               buttons |= IN_MouseButtons ();\r
+\r
+       for (i=0;i<8;i++,buttons>>=1)\r
+               if ( buttons&1 )\r
+               {\r
+                       if (!btnstate[i])\r
+                               return true;\r
+               }\r
+               else\r
+                       btnstate[i]=false;\r
+\r
+       return false;\r
+}\r
+\r
+\r
+void IN_Ack (void)\r
+{\r
+       IN_StartAck ();\r
+\r
+       while (!IN_CheckAck ())\r
+       ;\r
+}\r
+\r
+\r
+///////////////////////////////////////////////////////////////////////////\r
+//\r
+//     IN_UserInput() - Waits for the specified delay time (in ticks) or the\r
+//             user pressing a key or a mouse button. If the clear flag is set, it\r
+//             then either clears the key or waits for the user to let the mouse\r
+//             button up.\r
+//\r
+///////////////////////////////////////////////////////////////////////////\r
+boolean IN_UserInput(longword delay)\r
+{\r
+       longword        lasttime;\r
+\r
+       lasttime = TimeCount;\r
+       IN_StartAck ();\r
+       do\r
+       {\r
+               if (IN_CheckAck())\r
+                       return true;\r
+       } while (TimeCount - lasttime < delay);\r
+       return(false);\r
+}\r
+\r
+//===========================================================================\r
+\r
+/*\r
+===================\r
+=\r
+= IN_MouseButtons\r
+=\r
+===================\r
+*/\r
+\r
+byte   IN_MouseButtons (void)\r
+{\r
+       if (MousePresent)\r
+       {\r
+               Mouse(MButtons);\r
+               return _BX;\r
+       }\r
+       else\r
+               return 0;\r
+}\r
+\r
+\r
+/*\r
+===================\r
+=\r
+= IN_JoyButtons\r
+=\r
+===================\r
+*/\r
+\r
+byte   IN_JoyButtons (void)\r
+{\r
+       unsigned joybits;\r
+\r
+       joybits = inportb(0x201);       // Get all the joystick buttons\r
+       joybits >>= 4;                          // only the high bits are useful\r
+       joybits ^= 15;                          // return with 1=pressed\r
+\r
+       return joybits;\r
+}\r
+\r
+\r