1 /* Reconstructed Commander Keen 4-6 Source Code
\r
2 * Copyright (C) 2021 K1n9_Duk3
\r
4 * This file is primarily based on:
\r
5 * Catacomb 3-D Source Code
\r
6 * Copyright (C) 1993-2014 Flat Rock Software
\r
8 * This program is free software; you can redistribute it and/or modify
\r
9 * it under the terms of the GNU General Public License as published by
\r
10 * the Free Software Foundation; either version 2 of the License, or
\r
11 * (at your option) any later version.
\r
13 * This program is distributed in the hope that it will be useful,
\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 * GNU General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License along
\r
19 * with this program; if not, write to the Free Software Foundation, Inc.,
\r
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
25 // ID_IN.c - Input Manager
\r
27 // By Jason Blochowiak
\r
31 // This module handles dealing with the various input devices
\r
33 // Depends on: Memory Mgr (for demo recording), Sound Mgr (for timing stuff),
\r
34 // User Mgr (for command line parms)
\r
37 // LastScan - The keyboard scan code of the last key pressed
\r
38 // LastASCII - The ASCII value of the last key pressed
\r
39 // DEBUG - there are more globals
\r
42 #include "ID_HEADS.H"
\r
45 #define KeyInt 9 // The keyboard ISR number
\r
47 // Stuff for the joystick
\r
48 #define JoyScaleMax 32768
\r
49 #define JoyScaleShift 8
\r
50 #define MaxJoyValue 5000
\r
53 boolean Keyboard[NumCodes],
\r
54 JoysPresent[MaxJoys],
\r
59 KeyboardDef KbdDefs[MaxKbds] = {{0x1d,0x38,0x47,0x48,0x49,0x4b,0x4d,0x4f,0x50,0x51}};
\r
60 JoystickDef JoyDefs[MaxJoys];
\r
61 ControlType Controls[MaxPlayers];
\r
62 boolean GravisGamepad;
\r
63 word GravisAction[4];
\r
67 long MouseDownCount;
\r
68 boolean LatchedButton0[MaxPlayers];
\r
69 boolean LatchedButton1[MaxPlayers];
\r
71 Demo DemoMode = demo_Off;
\r
72 byte _seg *DemoBuffer;
\r
73 word DemoOffset,DemoSize;
\r
75 // Internal variables
\r
76 static boolean IN_Started;
\r
77 static boolean CapsLock;
\r
78 static ScanCode CurCode,LastCode;
\r
79 static byte far ASCIINames[] = // Unshifted ASCII for scan codes
\r
81 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
82 0 ,27 ,'1','2','3','4','5','6','7','8','9','0','-','=',8 ,9 , // 0
\r
83 'q','w','e','r','t','y','u','i','o','p','[',']',13 ,0 ,'a','s', // 1
\r
84 'd','f','g','h','j','k','l',';',39 ,'`',0 ,92 ,'z','x','c','v', // 2
\r
85 'b','n','m',',','.','/',0 ,'*',0 ,' ',0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
86 0 ,0 ,0 ,0 ,0 ,0 ,0 ,'7','8','9','-','4','5','6','+','1', // 4
\r
87 '2','3','0',127,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
88 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
89 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
91 far ShiftNames[] = // Shifted ASCII for scan codes
\r
93 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
94 0 ,27 ,'!','@','#','$','%','^','&','*','(',')','_','+',8 ,9 , // 0
\r
95 'Q','W','E','R','T','Y','U','I','O','P','{','}',13 ,0 ,'A','S', // 1
\r
96 'D','F','G','H','J','K','L',':',34 ,'~',0 ,'|','Z','X','C','V', // 2
\r
97 'B','N','M','<','>','?',0 ,'*',0 ,' ',0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
98 0 ,0 ,0 ,0 ,0 ,0 ,0 ,'7','8','9','-','4','5','6','+','1', // 4
\r
99 '2','3','0',127,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
100 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
101 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
103 far SpecialNames[] = // ASCII for 0xe0 prefixed codes
\r
105 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
106 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 0
\r
107 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,13 ,0 ,0 ,0 , // 1
\r
108 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 2
\r
109 0 ,0 ,0 ,0 ,0 ,'/',0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
110 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 4
\r
111 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
112 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
113 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
116 *ScanNames[] = // Scan code names with single chars
\r
118 "?","?","1","2","3","4","5","6","7","8","9","0","-","+","?","?",
\r
119 "Q","W","E","R","T","Y","U","I","O","P","[","]","|","?","A","S",
\r
120 "D","F","G","H","J","K","L",";","\"","?","?","?","Z","X","C","V",
\r
121 "B","N","M",",",".","/","?","?","?","?","?","?","?","?","?","?",
\r
122 "?","?","?","?","?","?","?","?","\xf","?","-","\x15","5","\x11","+","?",
\r
123 "\x13","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",
\r
124 "?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",
\r
125 "?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"
\r
126 }, // DEBUG - consolidate these
\r
127 far ExtScanCodes[] = // Scan codes with >1 char names
\r
129 1,0xe,0xf,0x1d,0x2a,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,
\r
130 0x3f,0x40,0x41,0x42,0x43,0x44,0x57,0x59,0x46,0x1c,0x36,
\r
131 0x37,0x38,0x47,0x49,0x4f,0x51,0x52,0x53,0x45,0x48,
\r
132 0x50,0x4b,0x4d,0x00
\r
134 *ExtScanNames[] = // Names corresponding to ExtScanCodes
\r
136 "Esc","BkSp","Tab","Ctrl","LShft","Space","CapsLk","F1","F2","F3","F4",
\r
137 "F5","F6","F7","F8","F9","F10","F11","F12","ScrlLk","Enter","RShft",
\r
138 "PrtSc","Alt","Home","PgUp","End","PgDn","Ins","Del","NumLk","Up",
\r
139 "Down","Left","Right",""
\r
141 static Direction DirTable[] = // Quick lookup for total direction
\r
143 dir_NorthWest, dir_North, dir_NorthEast,
\r
144 dir_West, dir_None, dir_East,
\r
145 dir_SouthWest, dir_South, dir_SouthEast
\r
148 static void (*INL_KeyHook)(void);
\r
149 static void interrupt (*OldKeyVect)(void);
\r
151 static char *ParmStrings[] = {"nojoys","nomouse",nil};
\r
153 // Internal routines
\r
155 ///////////////////////////////////////////////////////////////////////////
\r
157 // INL_KeyService() - Handles a keyboard interrupt (key up/down)
\r
159 ///////////////////////////////////////////////////////////////////////////
\r
160 static void interrupt
\r
161 INL_KeyService(void)
\r
163 static boolean special;
\r
168 k = inportb(0x60); // Get the scan code
\r
170 // Tell the XT keyboard controller to clear the key
\r
171 outportb(0x61,(temp = inportb(0x61)) | 0x80);
\r
172 outportb(0x61,temp);
\r
174 if (k == 0xe0) // Special key prefix
\r
176 else if (k == 0xe1) // Handle Pause key
\r
180 if (k & 0x80) // Break code
\r
184 // DEBUG - handle special keys: ctl-alt-delete, print scrn
\r
186 Keyboard[k] = false;
\r
190 LastCode = CurCode;
\r
191 CurCode = LastScan = k;
\r
192 Keyboard[k] = true;
\r
196 for (player = 0; player < MaxPlayers; player++)
\r
198 if (Controls[player] == ctrl_Keyboard1)
\r
200 if (CurCode == KbdDefs[0].button0)
\r
201 LatchedButton0[player] = true;
\r
202 else if (CurCode == KbdDefs[0].button1)
\r
203 LatchedButton1[player] = true;
\r
205 else if (Controls[player] == ctrl_Keyboard1) // BUG? should probably check for ctrl_Keyboard2 here...
\r
207 if (CurCode == KbdDefs[1].button0)
\r
208 LatchedButton0[player] = true;
\r
209 else if (CurCode == KbdDefs[1].button1)
\r
210 LatchedButton1[player] = true;
\r
216 c = SpecialNames[k];
\r
219 if (k == sc_CapsLock)
\r
222 // DEBUG - make caps lock light work
\r
225 if (Keyboard[sc_LShift] || Keyboard[sc_RShift]) // If shifted
\r
228 if ((c >= 'A') && (c <= 'Z') && CapsLock)
\r
234 if ((c >= 'a') && (c <= 'z') && CapsLock)
\r
245 if (INL_KeyHook && !special)
\r
247 outportb(0x20,0x20);
\r
250 ///////////////////////////////////////////////////////////////////////////
\r
252 // INL_GetMouseDelta() - Gets the amount that the mouse has moved from the
\r
255 ///////////////////////////////////////////////////////////////////////////
\r
257 INL_GetMouseDelta(int *x,int *y)
\r
264 ///////////////////////////////////////////////////////////////////////////
\r
266 // INL_GetMouseButtons() - Gets the status of the mouse buttons from the
\r
269 ///////////////////////////////////////////////////////////////////////////
\r
271 INL_GetMouseButtons(void)
\r
280 ///////////////////////////////////////////////////////////////////////////
\r
282 // IN_GetJoyAbs() - Reads the absolute position of the specified joystick
\r
284 ///////////////////////////////////////////////////////////////////////////
\r
286 IN_GetJoyAbs(word joy,word *xp,word *yp)
\r
293 xs = joy? 2 : 0; // Find shift value for x axis
\r
294 xb = 1 << xs; // Use shift value to get x bit mask
\r
295 ys = joy? 3 : 1; // Do the same for y axis
\r
298 // Read the absolute joystick values
\r
299 asm pushf // Save some registers
\r
302 asm cli // Make sure an interrupt doesn't screw the timings
\r
307 asm out dx,al // Clear the resistors
\r
309 asm mov ah,[xb] // Get masks into registers
\r
312 asm xor si,si // Clear count registers
\r
314 asm xor bh,bh // Clear high byte of bx for later
\r
316 asm push bp // Don't mess up stack frame
\r
317 asm mov bp,MaxJoyValue
\r
320 asm in al,dx // Get bits indicating whether all are finished
\r
322 asm dec bp // Check bounding register
\r
323 asm jz done // We have a silly value - abort
\r
325 asm mov bl,al // Duplicate the bits
\r
326 asm and bl,ah // Mask off useless bits (in [xb])
\r
327 asm add si,bx // Possibly increment count register
\r
328 asm mov cl,bl // Save for testing later
\r
331 asm and bl,ch // [yb]
\r
335 asm jnz loop // If both bits were 0, drop out
\r
340 asm mov cl,[xs] // Get the number of bits to shift
\r
341 asm shr si,cl // and shift the count that many times
\r
346 asm mov [x],si // Store the values into the variables
\r
351 asm popf // Restore the registers
\r
357 ///////////////////////////////////////////////////////////////////////////
\r
359 // INL_GetJoyDelta() - Returns the relative movement of the specified
\r
360 // joystick (from +/-127, scaled adaptively)
\r
362 ///////////////////////////////////////////////////////////////////////////
\r
364 INL_GetJoyDelta(word joy,int *dx,int *dy,boolean adaptive)
\r
369 static longword lasttime;
\r
371 IN_GetJoyAbs(joy,&x,&y);
\r
372 def = JoyDefs + joy;
\r
374 if (x < def->threshMinX)
\r
376 if (x < def->joyMinX)
\r
379 x = -(x - def->threshMinX);
\r
380 x *= def->joyMultXL;
\r
381 x >>= JoyScaleShift;
\r
382 *dx = (x > 127)? -127 : -x;
\r
384 else if (x > def->threshMaxX)
\r
386 if (x > def->joyMaxX)
\r
389 x = x - def->threshMaxX;
\r
390 x *= def->joyMultXH;
\r
391 x >>= JoyScaleShift;
\r
392 *dx = (x > 127)? 127 : x;
\r
397 if (y < def->threshMinY)
\r
399 if (y < def->joyMinY)
\r
402 y = -(y - def->threshMinY);
\r
403 y *= def->joyMultYL;
\r
404 y >>= JoyScaleShift;
\r
405 *dy = (y > 127)? -127 : -y;
\r
407 else if (y > def->threshMaxY)
\r
409 if (y > def->joyMaxY)
\r
412 y = y - def->threshMaxY;
\r
413 y *= def->joyMultYH;
\r
414 y >>= JoyScaleShift;
\r
415 *dy = (y > 127)? 127 : y;
\r
422 time = (TimeCount - lasttime) / 2;
\r
431 lasttime = TimeCount;
\r
434 ///////////////////////////////////////////////////////////////////////////
\r
436 // INL_GetJoyButtons() - Returns the button status of the specified
\r
439 ///////////////////////////////////////////////////////////////////////////
\r
441 INL_GetJoyButtons(word joy)
\r
443 register word result;
\r
445 result = inportb(0x201); // Get all the joystick buttons
\r
448 // all 4 buttons (for Gravis Gamepad option)
\r
455 result >>= joy? 6 : 4; // Shift into bits 0-1
\r
456 result &= 3; // Mask off the useless bits
\r
462 ///////////////////////////////////////////////////////////////////////////
\r
464 // IN_GetJoyButtonsDB() - Returns the de-bounced button status of the
\r
465 // specified joystick
\r
467 ///////////////////////////////////////////////////////////////////////////
\r
469 IN_GetJoyButtonsDB(word joy)
\r
472 word result1,result2;
\r
476 result1 = INL_GetJoyButtons(joy);
\r
477 endtime = TimeCount + 2;
\r
478 while (TimeCount <= endtime)
\r
480 result2 = INL_GetJoyButtons(joy);
\r
481 } while (result1 != result2);
\r
485 ///////////////////////////////////////////////////////////////////////////
\r
487 // INL_StartKbd() - Sets up my keyboard stuff for use
\r
489 ///////////////////////////////////////////////////////////////////////////
\r
493 INL_KeyHook = 0; // Clear key hook
\r
495 IN_ClearKeysDown();
\r
497 OldKeyVect = getvect(KeyInt);
\r
498 setvect(KeyInt,INL_KeyService);
\r
501 ///////////////////////////////////////////////////////////////////////////
\r
503 // INL_ShutKbd() - Restores keyboard control to the BIOS
\r
505 ///////////////////////////////////////////////////////////////////////////
\r
509 poke(0x40,0x17,peek(0x40,0x17) & 0xfaf0); // Clear ctrl/alt/shift flags
\r
511 setvect(KeyInt,OldKeyVect);
\r
514 ///////////////////////////////////////////////////////////////////////////
\r
516 // INL_StartMouse() - Detects and sets up the mouse
\r
518 ///////////////////////////////////////////////////////////////////////////
\r
520 INL_StartMouse(void)
\r
522 if (getvect(MouseInt))
\r
531 ///////////////////////////////////////////////////////////////////////////
\r
533 // INL_ShutMouse() - Cleans up after the mouse
\r
535 ///////////////////////////////////////////////////////////////////////////
\r
537 INL_ShutMouse(void)
\r
542 // INL_SetJoyScale() - Sets up scaling values for the specified joystick
\r
545 INL_SetJoyScale(word joy)
\r
549 def = &JoyDefs[joy];
\r
550 def->joyMultXL = JoyScaleMax / (def->threshMinX - def->joyMinX);
\r
551 def->joyMultXH = JoyScaleMax / (def->joyMaxX - def->threshMaxX);
\r
552 def->joyMultYL = JoyScaleMax / (def->threshMinY - def->joyMinY);
\r
553 def->joyMultYH = JoyScaleMax / (def->joyMaxY - def->threshMaxY);
\r
556 ///////////////////////////////////////////////////////////////////////////
\r
558 // IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale()
\r
559 // to set up scaling values
\r
561 ///////////////////////////////////////////////////////////////////////////
\r
563 IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy)
\r
568 def = &JoyDefs[joy];
\r
570 def->joyMinX = minx;
\r
571 def->joyMaxX = maxx;
\r
574 def->threshMinX = ((r / 2) - d) + minx;
\r
575 def->threshMaxX = ((r / 2) + d) + minx;
\r
577 def->joyMinY = miny;
\r
578 def->joyMaxY = maxy;
\r
581 def->threshMinY = ((r / 2) - d) + miny;
\r
582 def->threshMaxY = ((r / 2) + d) + miny;
\r
584 INL_SetJoyScale(joy);
\r
587 ///////////////////////////////////////////////////////////////////////////
\r
589 // INL_StartJoy() - Detects & auto-configures the specified joystick
\r
590 // The auto-config assumes the joystick is centered
\r
592 ///////////////////////////////////////////////////////////////////////////
\r
594 INL_StartJoy(word joy)
\r
598 IN_GetJoyAbs(joy,&x,&y);
\r
602 ((x == 0) || (x > MaxJoyValue - 10))
\r
603 || ((y == 0) || (y > MaxJoyValue - 10))
\r
608 IN_SetupJoy(joy,0,x * 2,0,y * 2);
\r
613 ///////////////////////////////////////////////////////////////////////////
\r
615 // INL_ShutJoy() - Cleans up the joystick stuff
\r
617 ///////////////////////////////////////////////////////////////////////////
\r
619 INL_ShutJoy(word joy)
\r
621 JoysPresent[joy] = false;
\r
626 ///////////////////////////////////////////////////////////////////////////
\r
628 // IN_ClearButtonLatch() - Clears the button latch stuff
\r
630 ///////////////////////////////////////////////////////////////////////////
\r
632 IN_ClearButtonLatch(void)
\r
639 MouseDownCount = 0;
\r
641 for (player = 0; player < MaxPlayers; player++)
\r
643 LatchedButton0[player] = LatchedButton1[player] = 0;
\r
649 ///////////////////////////////////////////////////////////////////////////
\r
651 // LatchSndHook() - Hook routine for joystick button latch
\r
653 ///////////////////////////////////////////////////////////////////////////
\r
661 for (player = 0; player < MaxPlayers; player++)
\r
663 ctrl = Controls[player];
\r
665 if (ctrl == ctrl_Joystick1 || ctrl == ctrl_Joystick2)
\r
667 buttons = INL_GetJoyButtons(ctrl - ctrl_Joystick1);
\r
670 LatchedButton0[player] = true;
\r
672 LatchedButton1[player] = true;
\r
677 ///////////////////////////////////////////////////////////////////////////
\r
679 // IN_LatchButtons() - Enables or disables button latch
\r
681 ///////////////////////////////////////////////////////////////////////////
\r
682 void IN_LatchButtons(boolean enabled)
\r
687 IN_ClearButtonLatch();
\r
691 SD_SetUserHook(Latch ? LatchSndHook : NULL);
\r
694 ///////////////////////////////////////////////////////////////////////////
\r
696 // IN_Startup() - Starts up the Input Mgr
\r
698 ///////////////////////////////////////////////////////////////////////////
\r
702 boolean checkjoys,checkmouse;
\r
710 for (i = 1;i < _argc;i++)
\r
712 switch (US_CheckParm(_argv[i],ParmStrings))
\r
718 checkmouse = false;
\r
724 MousePresent = checkmouse? INL_StartMouse() : false;
\r
726 for (i = 0;i < MaxJoys;i++)
\r
727 JoysPresent[i] = checkjoys? INL_StartJoy(i) : false;
\r
732 ///////////////////////////////////////////////////////////////////////////
\r
734 // IN_Default() - Sets up default conditions for the Input Mgr
\r
736 ///////////////////////////////////////////////////////////////////////////
\r
738 IN_Default(boolean gotit,ControlType in)
\r
743 || ((in == ctrl_Joystick1) && !JoysPresent[0])
\r
744 || ((in == ctrl_Joystick2) && !JoysPresent[1])
\r
745 || ((in == ctrl_Mouse) && !MousePresent)
\r
747 in = ctrl_Keyboard1;
\r
748 IN_SetControlType(0,in);
\r
751 ///////////////////////////////////////////////////////////////////////////
\r
753 // IN_Shutdown() - Shuts down the Input Mgr
\r
755 ///////////////////////////////////////////////////////////////////////////
\r
765 for (i = 0;i < MaxJoys;i++)
\r
769 IN_Started = false;
\r
772 ///////////////////////////////////////////////////////////////////////////
\r
774 // IN_SetKeyHook() - Sets the routine that gets called by INL_KeyService()
\r
775 // everytime a real make/break code gets hit
\r
777 ///////////////////////////////////////////////////////////////////////////
\r
779 IN_SetKeyHook(void (*hook)())
\r
781 // BUG: interrupts should be disabled while setting INL_KeyHook!
\r
782 INL_KeyHook = hook;
\r
785 ///////////////////////////////////////////////////////////////////////////
\r
787 // IN_ClearKeyDown() - Clears the keyboard array
\r
789 ///////////////////////////////////////////////////////////////////////////
\r
791 IN_ClearKeysDown(void)
\r
795 LastScan = sc_None;
\r
796 LastASCII = key_None;
\r
797 for (i = 0;i < NumCodes;i++)
\r
798 Keyboard[i] = false;
\r
801 ///////////////////////////////////////////////////////////////////////////
\r
803 // INL_AdjustCursor() - Internal routine of common code from IN_ReadCursor()
\r
805 ///////////////////////////////////////////////////////////////////////////
\r
807 INL_AdjustCursor(CursorInfo *info,word buttons,int dx,int dy)
\r
809 if (buttons & (1 << 0))
\r
810 info->button0 = true;
\r
811 if (buttons & (1 << 1))
\r
812 info->button1 = true;
\r
818 ///////////////////////////////////////////////////////////////////////////
\r
820 // IN_ReadCursor() - Reads the input devices and fills in the cursor info
\r
823 ///////////////////////////////////////////////////////////////////////////
\r
825 IN_ReadCursor(CursorInfo *info)
\r
832 info->x = info->y = 0;
\r
833 info->button0 = info->button1 = false;
\r
837 buttons = INL_GetMouseButtons();
\r
838 INL_GetMouseDelta(&dx,&dy);
\r
839 INL_AdjustCursor(info,buttons,dx,dy);
\r
842 for (i = 0;i < MaxJoys;i++)
\r
844 if (!JoysPresent[i])
\r
847 for (player = 0;player < MaxPlayers; player++)
\r
849 if (Controls[player] == ctrl_Joystick1+i)
\r
855 buttons = INL_GetJoyButtons(i);
\r
856 INL_GetJoyDelta(i,&dx,&dy,true);
\r
859 INL_AdjustCursor(info,buttons,dx,dy);
\r
863 ///////////////////////////////////////////////////////////////////////////
\r
865 // IN_ReadControl() - Reads the device associated with the specified
\r
866 // player and fills in the control info struct
\r
868 ///////////////////////////////////////////////////////////////////////////
\r
870 IN_ReadControl(int player,ControlInfo *info)
\r
879 register KeyboardDef *def;
\r
882 mx = my = motion_None;
\r
885 if (DemoMode == demo_Playback)
\r
887 dbyte = DemoBuffer[DemoOffset + 1];
\r
888 my = (dbyte & 3) - 1;
\r
889 mx = ((dbyte >> 2) & 3) - 1;
\r
890 buttons = (dbyte >> 4) & 3;
\r
892 if (!(--DemoBuffer[DemoOffset]))
\r
895 if (DemoOffset >= DemoSize)
\r
896 DemoMode = demo_PlayDone;
\r
901 else if (DemoMode == demo_PlayDone)
\r
902 Quit("Demo playback exceeded");
\r
905 switch (type = Controls[player])
\r
907 case ctrl_Keyboard1:
\r
908 case ctrl_Keyboard2:
\r
909 def = &KbdDefs[type - ctrl_Keyboard];
\r
911 if (Keyboard[def->upleft])
\r
912 mx = motion_Left,my = motion_Up;
\r
913 else if (Keyboard[def->upright])
\r
914 mx = motion_Right,my = motion_Up;
\r
915 else if (Keyboard[def->downleft])
\r
916 mx = motion_Left,my = motion_Down;
\r
917 else if (Keyboard[def->downright])
\r
918 mx = motion_Right,my = motion_Down;
\r
920 if (Keyboard[def->up])
\r
922 else if (Keyboard[def->down])
\r
925 if (Keyboard[def->left])
\r
927 else if (Keyboard[def->right])
\r
930 if (Keyboard[def->button0])
\r
932 if (Keyboard[def->button1])
\r
936 case ctrl_Joystick1:
\r
937 case ctrl_Joystick2:
\r
938 INL_GetJoyDelta(type - ctrl_Joystick,&dx,&dy,false);
\r
941 buttons = INL_GetJoyButtons(2);
\r
942 for (i=0; i<4; i++)
\r
944 GravisAction[i] = buttons & (1 << GravisMap[i]);
\r
947 buttons = INL_GetJoyButtons(type - ctrl_Joystick);
\r
951 INL_GetMouseDelta(&dx,&dy);
\r
952 buttons = INL_GetMouseButtons();
\r
960 mx = (dx < 0)? motion_Left : ((dx > 0)? motion_Right : motion_None);
\r
961 my = (dy < 0)? motion_Up : ((dy > 0)? motion_Down : motion_None);
\r
973 info->button0 = buttons & (1 << 0);
\r
974 info->button1 = buttons & (1 << 1);
\r
975 info->dir = DirTable[((my + 1) * 3) + (mx + 1)];
\r
977 if (DemoMode == demo_Record)
\r
979 // Pack the control info into a byte
\r
980 dbyte = (buttons << 4) | ((mx + 1) << 2) | (my + 1);
\r
984 (DemoBuffer[DemoOffset + 1] == dbyte)
\r
985 && (DemoBuffer[DemoOffset] < 255)
\r
987 (DemoBuffer[DemoOffset])++;
\r
990 if (DemoOffset || DemoBuffer[DemoOffset])
\r
993 if (DemoOffset >= DemoSize)
\r
994 Quit("Demo buffer overflow");
\r
996 DemoBuffer[DemoOffset] = 1;
\r
997 DemoBuffer[DemoOffset + 1] = dbyte;
\r
1002 ///////////////////////////////////////////////////////////////////////////
\r
1004 // IN_SetControlType() - Sets the control type to be used by the specified
\r
1007 ///////////////////////////////////////////////////////////////////////////
\r
1009 IN_SetControlType(int player,ControlType type)
\r
1011 // DEBUG - check that requested type is present?
\r
1012 Controls[player] = type;
\r
1015 ///////////////////////////////////////////////////////////////////////////
\r
1017 // IN_StartDemoRecord() - Starts the demo recording, using a buffer the
\r
1018 // size passed. Returns if the buffer allocation was successful
\r
1020 ///////////////////////////////////////////////////////////////////////////
\r
1022 IN_StartDemoRecord(word bufsize)
\r
1027 MM_GetPtr((memptr *)&DemoBuffer,bufsize);
\r
1028 DemoMode = demo_Record;
\r
1029 DemoSize = bufsize & ~1;
\r
1031 DemoBuffer[0] = DemoBuffer[1] = 0;
\r
1036 ///////////////////////////////////////////////////////////////////////////
\r
1038 // IN_StartDemoPlayback() - Plays back the demo pointed to of the given size
\r
1040 ///////////////////////////////////////////////////////////////////////////
\r
1042 IN_StartDemoPlayback(byte _seg *buffer,word bufsize)
\r
1044 DemoBuffer = buffer;
\r
1045 DemoMode = demo_Playback;
\r
1046 DemoSize = bufsize & ~1;
\r
1050 ///////////////////////////////////////////////////////////////////////////
\r
1052 // IN_StopDemo() - Turns off demo mode
\r
1054 ///////////////////////////////////////////////////////////////////////////
\r
1058 if ((DemoMode == demo_Record) && DemoOffset)
\r
1061 DemoMode = demo_Off;
\r
1064 ///////////////////////////////////////////////////////////////////////////
\r
1066 // IN_FreeDemoBuffer() - Frees the demo buffer, if it's been allocated
\r
1068 ///////////////////////////////////////////////////////////////////////////
\r
1070 IN_FreeDemoBuffer(void)
\r
1073 MM_FreePtr((memptr *)&DemoBuffer);
\r
1076 ///////////////////////////////////////////////////////////////////////////
\r
1078 // IN_GetScanName() - Returns a string containing the name of the
\r
1079 // specified scan code
\r
1081 ///////////////////////////////////////////////////////////////////////////
\r
1083 IN_GetScanName(ScanCode scan)
\r
1088 for (s = ExtScanCodes,p = ExtScanNames;*s;p++,s++)
\r
1092 return(ScanNames[scan]);
\r
1095 ///////////////////////////////////////////////////////////////////////////
\r
1097 // IN_WaitForKey() - Waits for a scan code, then clears LastScan and
\r
1098 // returns the scan code
\r
1100 ///////////////////////////////////////////////////////////////////////////
\r
1102 IN_WaitForKey(void)
\r
1106 while (!(result = LastScan))
\r
1112 ///////////////////////////////////////////////////////////////////////////
\r
1114 // IN_WaitForASCII() - Waits for an ASCII char, then clears LastASCII and
\r
1115 // returns the ASCII value
\r
1117 ///////////////////////////////////////////////////////////////////////////
\r
1119 IN_WaitForASCII(void)
\r
1123 while (!(result = LastASCII))
\r
1129 ///////////////////////////////////////////////////////////////////////////
\r
1131 // IN_AckBack() - Waits for either an ASCII keypress or a button press
\r
1133 ///////////////////////////////////////////////////////////////////////////
\r
1143 if (INL_GetMouseButtons())
\r
1145 while (INL_GetMouseButtons())
\r
1151 for (i = 0;i < MaxJoys;i++)
\r
1153 if (JoysPresent[i] || GravisGamepad)
\r
1155 if (IN_GetJoyButtonsDB(i))
\r
1157 while (IN_GetJoyButtonsDB(i))
\r
1165 IN_ClearKey(LastScan);
\r
1166 LastScan = sc_None;
\r
1169 ///////////////////////////////////////////////////////////////////////////
\r
1171 // IN_Ack() - Clears user input & then calls IN_AckBack()
\r
1173 ///////////////////////////////////////////////////////////////////////////
\r
1179 IN_ClearKey(LastScan);
\r
1180 LastScan = sc_None;
\r
1183 while (INL_GetMouseButtons())
\r
1185 for (i = 0;i < MaxJoys;i++)
\r
1186 if (JoysPresent[i] || GravisGamepad)
\r
1187 while (IN_GetJoyButtonsDB(i))
\r
1193 ///////////////////////////////////////////////////////////////////////////
\r
1195 // IN_IsUserInput() - Returns true if a key has been pressed or a button
\r
1198 ///////////////////////////////////////////////////////////////////////////
\r
1200 IN_IsUserInput(void)
\r
1205 result = LastScan;
\r
1208 if (INL_GetMouseButtons())
\r
1211 for (i = 0;i < MaxJoys;i++)
\r
1212 if (JoysPresent[i] || GravisGamepad)
\r
1213 if (INL_GetJoyButtons(i))
\r
1219 ///////////////////////////////////////////////////////////////////////////
\r
1221 // IN_UserInput() - Waits for the specified delay time (in ticks) or the
\r
1222 // user pressing a key or a mouse button. If the clear flag is set, it
\r
1223 // then either clears the key or waits for the user to let the mouse
\r
1226 ///////////////////////////////////////////////////////////////////////////
\r
1228 IN_UserInput(longword delay,boolean clear)
\r
1230 longword lasttime;
\r
1232 lasttime = TimeCount;
\r
1235 if (IN_IsUserInput())
\r
1241 } while (TimeCount - lasttime < delay);
\r