1 /* Catacomb Apocalypse Source Code
\r
2 * Copyright (C) 1993-2014 Flat Rock Software
\r
4 * This program is free software; you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation; either version 2 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License along
\r
15 * with this program; if not, write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
21 // ID_IN.c - Input Manager
\r
23 // By Jason Blochowiak
\r
24 // Open Watcom port by sparky4
\r
28 // This module handles dealing with the various input devices
\r
30 // Depends on: Memory Mgr (for demo recording), Sound Mgr (for timing stuff),
\r
31 // User Mgr (for command line parms)
\r
34 // LastScan - The keyboard scan code of the last key pressed
\r
35 // LastASCII - The ASCII value of the last key pressed
\r
36 // DEBUG - there are more globals
\r
39 #include "src/lib/16_in.h"
\r
42 static word far* clockw= (word far*) 0x046C; /* 18.2hz clock */
\r
45 =============================================================================
\r
49 =============================================================================
\r
53 boolean MousePresent;
\r
54 boolean JoysPresent[MaxJoys];
\r
55 boolean Keyboard[NumCodes];
\r
60 KeyboardDef KbdDefs[MaxKbds];
\r
61 JoystickDef JoyDefs[MaxJoys];
\r
64 //extern inconfig inpu;
\r
65 //inpu.KbdDefs = {0x1d,0x38,/*0x47,*/0x48,/*0x49,*/0x4b,0x4d,/*0x4f,*/0x50/*,0x51*/};
\r
68 =============================================================================
\r
72 =============================================================================
\r
75 #ifdef __cplusplus /* Function must be declared C style */
\r
79 static struct instat {
\r
82 ScanCode CurCode,LastCode;
\r
85 static void (*INL_KeyHook)(void);
\r
86 static void interrupt (*OldKeyVect)(void);
\r
87 static char *ParmStringsIN[] = {"nojoys","nomouse",nil};
\r
89 static byte far ASCIINames[] = // Unshifted ASCII for scan codes
\r
91 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
92 0 ,27 ,'1','2','3','4','5','6','7','8','9','0','-','=',8 ,9 , // 0
\r
93 'q','w','e','r','t','y','u','i','o','p','[',']',13 ,0 ,'a','s', // 1
\r
94 'd','f','g','h','j','k','l',';',39 ,'`',0 ,92 ,'z','x','c','v', // 2
\r
95 'b','n','m',',','.','/',0 ,'*',0 ,' ',0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
96 0 ,0 ,0 ,0 ,0 ,0 ,0 ,'7','8','9','-','4','5','6','+','1', // 4
\r
97 '2','3','0',127,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
98 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
99 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
101 far ShiftNames[] = // Shifted ASCII for scan codes
\r
103 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
104 0 ,27 ,'!','@','#','$','%','^','&','*','(',')','_','+',8 ,9 , // 0
\r
105 'Q','W','E','R','T','Y','U','I','O','P','{','}',13 ,0 ,'A','S', // 1
\r
106 'D','F','G','H','J','K','L',':',34 ,'~',0 ,'|','Z','X','C','V', // 2
\r
107 'B','N','M','<','>','?',0 ,'*',0 ,' ',0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
108 0 ,0 ,0 ,0 ,0 ,0 ,0 ,'7','8','9','-','4','5','6','+','1', // 4
\r
109 '2','3','0',127,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
110 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
111 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
113 far SpecialNames[] = // ASCII for 0xe0 prefixed codes
\r
115 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
116 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 0
\r
117 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,13 ,0 ,0 ,0 , // 1
\r
118 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 2
\r
119 0 ,0 ,0 ,0 ,0 ,'/',0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
120 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 4
\r
121 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
122 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
123 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
125 *ScanNames[] = // Scan code names with single chars
\r
127 "?","?","1","2","3","4","5","6","7","8","9","0","-","+","?","?",
\r
128 "Q","W","E","R","T","Y","U","I","O","P","[","]","|","?","A","S",
\r
129 "D","F","G","H","J","K","L",";","\"","?","?","?","Z","X","C","V",
\r
130 "B","N","M",",",".","/","?","?","?","?","?","?","?","?","?","?",
\r
131 "?","?","?","?","?","?","?","?","\xf","?","-","\x15","5","\x11","+","?",
\r
132 "\x13","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",
\r
133 "?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",
\r
134 "?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"
\r
135 }, // DEBUG - consolidate these
\r
136 far ExtScanCodes[] = // Scan codes with >1 char names
\r
138 1,0xe,0xf,0x1d,0x2a,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,
\r
139 0x3f,0x40,0x41,0x42,0x43,0x44,0x57,0x59,0x46,0x1c,0x36,
\r
140 0x37,0x38,0x47,0x49,0x4f,0x51,0x52,0x53,0x45,0x48,
\r
141 0x50,0x4b,0x4d,0x00
\r
143 *ExtScanNames[] = // Names corresponding to ExtScanCodes
\r
145 "Esc","BkSp","Tab","Ctrl","LShft","Space","CapsLk","F1","F2","F3","F4",
\r
146 "F5","F6","F7","F8","F9","F10","F11","F12","ScrlLk","Enter","RShft",
\r
147 "PrtSc","Alt","Home","PgUp","End","PgDn","Ins","Del","NumLk","Up",
\r
148 "Down","Left","Right",""
\r
151 static Direction DirTable[] = // Quick lookup for total direction
\r
167 // Internal routines
\r
169 ///////////////////////////////////////////////////////////////////////////
\r
171 // INL_KeyService() - Handles a keyboard interrupt (key up/down)
\r
173 ///////////////////////////////////////////////////////////////////////////
\r
177 static boolean special;
\r
179 register byte temp;
\r
181 k = inp(0x60); // Get the scan code
\r
183 // Tell the XT keyboard controller to clear the key
\r
184 outp(0x61,(temp = inp(0x61)) | 0x80);
\r
187 if (k == 0xe0) // Special key prefix
\r
189 else if (k == 0xe1) // Handle Pause key
\r
190 inpu.Paused = true;
\r
193 if (k & 0x80) // Break code
\r
197 // DEBUG - handle special keys: ctl-alt-delete, print scrn
\r
199 inpu.Keyboard[k] = false;
\r
203 inst.LastCode = inst.CurCode;
\r
204 inst.CurCode = inpu.LastScan = k;
\r
205 inpu.Keyboard[k] = true;
\r
208 c = SpecialNames[k];
\r
211 if (k == sc_CapsLock)
\r
213 inst.CapsLock ^= true;
\r
214 // DEBUG - make caps lock light work
\r
217 if (inpu.Keyboard[sc_LShift] || inpu.Keyboard[sc_RShift]) // If shifted
\r
220 if ((c >= 'A') && (c <= 'Z') && inst.CapsLock)
\r
226 if ((c >= 'a') && (c <= 'z') && inst.CapsLock)
\r
231 inpu.LastASCII = c;
\r
237 if (INL_KeyHook && !special)
\r
239 #ifdef __DEBUG_InputMgr__
\r
240 if(dbg_testkeyin > 0) printf("%c %u [0x%x %u] %u\n", c, c, k, k, inpu.Keyboard[k]);
\r
248 //union REGS CPURegs;
\r
249 //x = CPURegs.x.ax;
\r
254 //int86(MouseInt,&CPURegs,&CPURegs);
\r
257 ///////////////////////////////////////////////////////////////////////////
\r
259 // INL_GetMouseDelta() - Gets the amount that the mouse has moved from the
\r
262 ///////////////////////////////////////////////////////////////////////////
\r
264 INL_GetMouseDelta(int *x,int *y)
\r
266 union REGS CPURegs;
\r
272 ///////////////////////////////////////////////////////////////////////////
\r
274 // INL_GetMouseButtons() - Gets the status of the mouse buttons from the
\r
277 ///////////////////////////////////////////////////////////////////////////
\r
279 INL_GetMouseButtons(void)
\r
281 union REGS CPURegs;
\r
285 buttons = CPURegs.x.bx;
\r
289 ///////////////////////////////////////////////////////////////////////////
\r
291 // IN_GetJoyAbs() - Reads the absolute position of the specified joystick
\r
293 ///////////////////////////////////////////////////////////////////////////
\r
295 IN_GetJoyAbs(word joy,word *xp,word *yp)
\r
302 xs = joy? 2 : 0; // Find shift value for x axis
\r
303 xb = 1 << xs; // Use shift value to get x bit mask
\r
304 ys = joy? 3 : 1; // Do the same for y axis
\r
307 // Read the absolute joystick values
\r
309 pushf // Save some registers
\r
312 cli // Make sure an interrupt doesn't screw the timings
\r
317 out dx,al // Clear the resistors
\r
319 mov ah,[xb] // Get masks into registers
\r
322 xor si,si // Clear count registers
\r
324 xor bh,bh // Clear high byte of bx for later
\r
326 push bp // Don't mess up stack frame
\r
328 #ifdef __BORLANDC__
\r
332 #ifdef __BORLANDC__
\r
335 in al,dx // Get bits indicating whether all are finished
\r
337 dec bp // Check bounding register
\r
338 jz done // We have a silly value - abort
\r
340 mov bl,al // Duplicate the bits
\r
341 and bl,ah // Mask off useless bits (in [xb])
\r
342 add si,bx // Possibly increment count register
\r
343 mov cl,bl // Save for testing later
\r
350 jnz loo // If both bits were 0, drop out
\r
351 #ifdef __BORLANDC__
\r
355 #ifdef __BORLANDC__
\r
360 mov cl,[xs] // Get the number of bits to shift
\r
361 shr si,cl // and shift the count that many times
\r
366 mov [x],si // Store the values into the variables
\r
371 popf // Restore the registers
\r
378 ///////////////////////////////////////////////////////////////////////////
\r
380 // INL_GetJoyDelta() - Returns the relative movement of the specified
\r
381 // joystick (from +/-127, scaled adaptively)
\r
383 ///////////////////////////////////////////////////////////////////////////
\r
385 INL_GetJoyDelta(word joy,int *dx,int *dy,boolean adaptive)
\r
389 word TimeCount = *clockw;
\r
391 static word lasttime;
\r
393 IN_GetJoyAbs(joy,&x,&y);
\r
394 def = inpu.JoyDefs + joy;
\r
396 if (x < def->threshMinX)
\r
398 if (x < def->joyMinX)
\r
401 x = -(x - def->threshMinX);
\r
402 x *= def->joyMultXL;
\r
403 x >>= JoyScaleShift;
\r
404 *dx = (x > 127)? -127 : -x;
\r
406 else if (x > def->threshMaxX)
\r
408 if (x > def->joyMaxX)
\r
411 x = x - def->threshMaxX;
\r
412 x *= def->joyMultXH;
\r
413 x >>= JoyScaleShift;
\r
414 *dx = (x > 127)? 127 : x;
\r
419 if (y < def->threshMinY)
\r
421 if (y < def->joyMinY)
\r
424 y = -(y - def->threshMinY);
\r
425 y *= def->joyMultYL;
\r
426 y >>= JoyScaleShift;
\r
427 *dy = (y > 127)? -127 : -y;
\r
429 else if (y > def->threshMaxY)
\r
431 if (y > def->joyMaxY)
\r
434 y = y - def->threshMaxY;
\r
435 y *= def->joyMultYH;
\r
436 y >>= JoyScaleShift;
\r
437 *dy = (y > 127)? 127 : y;
\r
444 time = (TimeCount - lasttime) / 2;
\r
453 lasttime = TimeCount;
\r
456 ///////////////////////////////////////////////////////////////////////////
\r
458 // INL_GetJoyButtons() - Returns the button status of the specified
\r
461 ///////////////////////////////////////////////////////////////////////////
\r
463 INL_GetJoyButtons(word joy)
\r
465 register word result;
\r
467 result = inp(0x201); // Get all the joystick buttons
\r
468 result >>= joy? 6 : 4; // Shift into bits 0-1
\r
469 result &= 3; // Mask off the useless bits
\r
474 ///////////////////////////////////////////////////////////////////////////
\r
476 // IN_GetJoyButtonsDB() - Returns the de-bounced button status of the
\r
477 // specified joystick
\r
479 ///////////////////////////////////////////////////////////////////////////
\r
481 IN_GetJoyButtonsDB(word joy)
\r
483 word TimeCount = *clockw;
\r
485 word result1,result2;
\r
489 result1 = INL_GetJoyButtons(joy);
\r
490 lasttime = TimeCount;
\r
491 while(TimeCount == lasttime)
\r
492 result2 = INL_GetJoyButtons(joy);
\r
493 } while(result1 != result2);
\r
497 ///////////////////////////////////////////////////////////////////////////
\r
499 // INL_StartKbd() - Sets up my keyboard stuff for use
\r
501 ///////////////////////////////////////////////////////////////////////////
\r
505 byte far *lock_key;
\r
506 INL_KeyHook = 0; // Clear key hook
\r
508 IN_ClearKeysDown();
\r
510 OldKeyVect = _dos_getvect(KeyInt);
\r
512 // turn off num-lock via BIOS
\r
513 lock_key = MK_FP(0x040, 0x017); // Pointing to the address of the bios shift state keys
\r
514 *lock_key&=(~(16 | 32 | 64)); // toggle off the locks by changing the values of the 4th, 5th, and 6th bits of the address byte of 0040:0017
\r
515 OldKeyVect(); // call BIOS keyhandler to change keyboard lights
\r
516 _dos_setvect(KeyInt,INL_KeyService);
\r
519 ///////////////////////////////////////////////////////////////////////////
\r
521 // INL_ShutKbd() - Restores keyboard control to the BIOS
\r
523 ///////////////////////////////////////////////////////////////////////////
\r
527 pokeb(0x40,0x17,peekb(0x40,0x17) & 0xfaf0); // Clear ctrl/alt/shift flags
\r
529 _dos_setvect(KeyInt,OldKeyVect);
\r
532 ///////////////////////////////////////////////////////////////////////////
\r
534 // INL_StartMouse() - Detects and sets up the mouse
\r
536 ///////////////////////////////////////////////////////////////////////////
\r
538 INL_StartMouse(void)
\r
540 union REGS CPURegs;
\r
541 if(_dos_getvect(MouseInt))
\r
544 if(CPURegs.x.ax == 0xffff)
\r
550 ///////////////////////////////////////////////////////////////////////////
\r
552 // INL_ShutMouse() - Cleans up after the mouse
\r
554 ///////////////////////////////////////////////////////////////////////////
\r
556 INL_ShutMouse(void)
\r
561 // INL_SetJoyScale() - Sets up scaling values for the specified joystick
\r
564 INL_SetJoyScale(word joy)
\r
568 def = &(inpu.JoyDefs[joy]);
\r
569 def->joyMultXL = JoyScaleMax / (def->threshMinX - def->joyMinX);
\r
570 def->joyMultXH = JoyScaleMax / (def->joyMaxX - def->threshMaxX);
\r
571 def->joyMultYL = JoyScaleMax / (def->threshMinY - def->joyMinY);
\r
572 def->joyMultYH = JoyScaleMax / (def->joyMaxY - def->threshMaxY);
\r
575 ///////////////////////////////////////////////////////////////////////////
\r
577 // IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale()
\r
578 // to set up scaling values
\r
580 ///////////////////////////////////////////////////////////////////////////
\r
582 IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy)
\r
587 def = &(inpu.JoyDefs[joy]);
\r
589 def->joyMinX = minx;
\r
590 def->joyMaxX = maxx;
\r
593 def->threshMinX = ((r / 2) - d) + minx;
\r
594 def->threshMaxX = ((r / 2) + d) + minx;
\r
596 def->joyMinY = miny;
\r
597 def->joyMaxY = maxy;
\r
600 def->threshMinY = ((r / 2) - d) + miny;
\r
601 def->threshMaxY = ((r / 2) + d) + miny;
\r
603 INL_SetJoyScale(joy);
\r
606 ///////////////////////////////////////////////////////////////////////////
\r
608 // INL_StartJoy() - Detects & auto-configures the specified joystick
\r
609 // The auto-config assumes the joystick is centered
\r
611 ///////////////////////////////////////////////////////////////////////////
\r
613 INL_StartJoy(word joy)
\r
617 IN_GetJoyAbs(joy,&x,&y);
\r
621 ((x == 0) || (x > MaxJoyValue - 10))
\r
622 || ((y == 0) || (y > MaxJoyValue - 10))
\r
627 IN_SetupJoy(joy,0,x * 2,0,y * 2);
\r
632 ///////////////////////////////////////////////////////////////////////////
\r
634 // INL_ShutJoy() - Cleans up the joystick stuff
\r
636 ///////////////////////////////////////////////////////////////////////////
\r
638 INL_ShutJoy(word joy)
\r
640 inpu.JoysPresent[joy] = false;
\r
645 ///////////////////////////////////////////////////////////////////////////
\r
647 // IN_Startup() - Starts up the Input Mgr
\r
649 ///////////////////////////////////////////////////////////////////////////
\r
653 boolean checkjoys,checkmouse;
\r
656 if (inst.IN_Started)
\r
661 for (i = 1;i < _argc;i++)
\r
663 switch (US_CheckParm(_argv[i],ParmStringsIN))
\r
669 checkmouse = false;
\r
675 inpu.MousePresent = checkmouse? INL_StartMouse() : false;
\r
677 for (i = 0;i < MaxJoys;i++)
\r
678 inpu.JoysPresent[i] = checkjoys? INL_StartJoy(i) : false;
\r
680 inst.IN_Started = true;
\r
684 ///////////////////////////////////////////////////////////////////////////
\r
686 // IN_Default() - Sets up default conditions for the Input Mgr
\r
688 ///////////////////////////////////////////////////////////////////////////
\r
690 IN_Default(boolean gotit,player_t *player,ControlType nt)
\r
696 || ((nt == ctrl_Joystick1) && !inpu.JoysPresent[0])
\r
697 || ((nt == ctrl_Joystick2) && !inpu.JoysPresent[1])
\r
698 || ((nt == ctrl_Mouse) && !inpu.MousePresent)
\r
700 nt = ctrl_Keyboard1;
\r
701 inpu.KbdDefs[0].button0 = 0x1c;
\r
702 inpu.KbdDefs[0].button1 = 0x38;
\r
703 //in.KbdDefs[0].upleft = 0x47;
\r
704 inpu.KbdDefs[0].up = 0x48;
\r
705 //in.KbdDefs[0].upright = 0x49;
\r
706 inpu.KbdDefs[0].left = 0x4b;
\r
707 inpu.KbdDefs[0].right = 0x4d;
\r
708 //in.KbdDefs[0].downleft = 0x4f;
\r
709 inpu.KbdDefs[0].down = 0x50;
\r
710 //in.KbdDefs[0].downright = 0x51;
\r
711 IN_SetControlType(0,player,nt);
\r
712 for(i=0; i>MaxPlayers;i++)
\r
713 player[i].enti.d =2;
\r
716 ///////////////////////////////////////////////////////////////////////////
\r
718 // IN_Shutdown() - Shuts down the Input Mgr
\r
720 ///////////////////////////////////////////////////////////////////////////
\r
726 if (!inst.IN_Started)
\r
730 for (i = 0;i < MaxJoys;i++)
\r
734 inst.IN_Started = false;
\r
737 ///////////////////////////////////////////////////////////////////////////
\r
739 // IN_SetKeyHook() - Sets the routine that gets called by INL_KeyService()
\r
740 // everytime a real make/break code gets hit
\r
742 ///////////////////////////////////////////////////////////////////////////
\r
744 IN_SetKeyHook(void (*hook)())
\r
746 INL_KeyHook = hook;
\r
749 ///////////////////////////////////////////////////////////////////////////
\r
751 // IN_ClearKeyDown() - Clears the keyboard array
\r
753 ///////////////////////////////////////////////////////////////////////////
\r
759 inpu.LastScan = sc_None;
\r
760 inpu.LastASCII = key_None;
\r
761 memset (inpu.Keyboard,0,sizeof(inpu.Keyboard));
\r
764 ///////////////////////////////////////////////////////////////////////////
\r
766 // INL_AdjustCursor() - Internal routine of common code from IN_ReadCursor()
\r
768 ///////////////////////////////////////////////////////////////////////////
\r
770 INL_AdjustCursor(CursorInfo *info,word buttons,int dx,int dy)
\r
772 if (buttons & (1 << 0))
\r
773 info->button0 = true;
\r
774 if (buttons & (1 << 1))
\r
775 info->button1 = true;
\r
781 ///////////////////////////////////////////////////////////////////////////
\r
783 // IN_ReadCursor() - Reads the input devices and fills in the cursor info
\r
786 ///////////////////////////////////////////////////////////////////////////
\r
788 IN_ReadCursor(CursorInfo *info)
\r
794 info->x = info->y = 0;
\r
795 info->button0 = info->button1 = false;
\r
797 if (inpu.MousePresent)
\r
799 buttons = INL_GetMouseButtons();
\r
800 INL_GetMouseDelta(&dx,&dy);
\r
801 INL_AdjustCursor(info,buttons,dx,dy);
\r
804 for (i = 0;i < MaxJoys;i++)
\r
806 if (!inpu.JoysPresent[i])
\r
809 buttons = INL_GetJoyButtons(i);
\r
810 INL_GetJoyDelta(i,&dx,&dy,true);
\r
813 INL_AdjustCursor(info,buttons,dx,dy);
\r
817 ///////////////////////////////////////////////////////////////////////////
\r
819 // IN_ReadControl() - Reads the device associated with the specified
\r
820 // player and fills in the control info struct
\r
822 ///////////////////////////////////////////////////////////////////////////
\r
824 IN_ReadControl(word pn, player_t *player)
\r
835 byte dir=DirTable[2];
\r
836 register KeyboardDef *def;
\r
839 mx = my = motion_None;
\r
843 if (DemoMode == demo_Playback)
\r
845 dbyte = DemoBuffer[DemoOffset + 1];
\r
846 my = (dbyte & 3) - 1;
\r
847 mx = ((dbyte >> 2) & 3) - 1;
\r
848 buttons = (dbyte >> 4) & 3;
\r
850 if (!(--DemoBuffer[DemoOffset]))
\r
853 if (DemoOffset >= DemoSize)
\r
854 DemoMode = demo_PlayDone;
\r
859 else if (DemoMode == demo_PlayDone)
\r
860 Quit ("Demo playback exceeded");
\r
864 switch (type = player[pn].Controls)
\r
866 case ctrl_Keyboard1:
\r
867 case ctrl_Keyboard2:
\r
868 def = &(inpu.KbdDefs[type - ctrl_Keyboard]);
\r
870 /* if (Keyboard[def->upleft])
\r
871 mx = motion_Left,my = motion_Up;
\r
872 else if (Keyboard[def->upright])
\r
873 mx = motion_Right,my = motion_Up;
\r
874 else if (Keyboard[def->downleft])
\r
875 mx = motion_Left,my = motion_Down;
\r
876 else if (Keyboard[def->downright])
\r
877 mx = motion_Right,my = motion_Down;*/
\r
878 //TODO: make this into a function that the joystick AND keyboard can use wwww
\r
879 if(DIRECTIONIFELSE)//(player[pn].info.dir == 2)
\r
881 if(!inpu.Keyboard[def->left] && !inpu.Keyboard[def->right]){
\r
882 if((inpu.Keyboard[def->up] && !inpu.Keyboard[def->down]))
\r
884 if((inpu.Keyboard[def->down] && !inpu.Keyboard[def->up]))
\r
886 }else if(!inpu.Keyboard[def->up] && !inpu.Keyboard[def->down]){
\r
887 if((inpu.Keyboard[def->left] && !inpu.Keyboard[def->right]))
\r
889 if((inpu.Keyboard[def->right] && !inpu.Keyboard[def->left]))
\r
891 }else{ //2 keys pressed
\r
892 switch (player[pn].pdir)
\r
896 if((inpu.Keyboard[def->left] && !inpu.Keyboard[def->right])){ dir = DirTable[1]; }//mx = motion_Left; }
\r
897 else if((inpu.Keyboard[def->right] && !inpu.Keyboard[def->left])){ dir = DirTable[3]; }//mx = motion_Right; }
\r
901 if((inpu.Keyboard[def->up] && !inpu.Keyboard[def->down])){ dir = DirTable[0]; }//my = motion_Up; }
\r
902 else if((inpu.Keyboard[def->down] && !inpu.Keyboard[def->up])){ dir = DirTable[4]; }//my = motion_Down; }
\r
907 #ifdef __DEBUG_InputMgr__
\r
908 //if(dbg_testcontrolnoisy > 0){ printf("dir=%c ", dirchar(dir)); printf("pdir=%c ", dirchar(player[pn].pdir)); }
\r
912 //input from player
\r
913 if (inpu.Keyboard[def->button0])
\r
915 if (inpu.Keyboard[def->button1])
\r
919 case ctrl_Joystick1:
\r
920 case ctrl_Joystick2:
\r
921 INL_GetJoyDelta(type - ctrl_Joystick,&dx,&dy,false);
\r
922 buttons = INL_GetJoyButtons(type - ctrl_Joystick);
\r
926 INL_GetMouseDelta(&dx,&dy);
\r
927 buttons = INL_GetMouseButtons();
\r
937 mx = (dx < 0)? motion_Left : ((dx > 0)? motion_Right : motion_None);
\r
938 my = (dy < 0)? motion_Up : ((dy > 0)? motion_Down : motion_None);
\r
946 player[pn].info.x = dx;
\r
947 player[pn].info.xaxis = mx;
\r
948 player[pn].info.y = dy;
\r
949 player[pn].info.yaxis = my;
\r
950 player[pn].info.button0 = buttons & (1 << 0);
\r
951 player[pn].info.button1 = buttons & (1 << 1);
\r
952 player[pn].info.button2 = buttons & (1 << 2);
\r
953 player[pn].info.button3 = buttons & (1 << 3);
\r
954 // player[pn].info.dir = DirTable[((my + 1) * 3) + (mx + 1)];
\r
955 conpee=(((my + 1) * 2) + (mx + 1))-1;
\r
956 player[pn].info.dir = DirTable[conpee];
\r
958 if(DirTable[conpee]!=2) player[pn].pdir=DirTable[conpee];
\r
959 if(player[pn].enti.q==1 &&( dir!=2 || (mx!=motion_None || my!=motion_None)))
\r
961 if(dir==2) player[pn].enti.d = player[pn].info.dir;
\r
962 else player[pn].enti.d = DirTable[dir];
\r
966 if (DemoMode == demo_Record)
\r
968 // Pack the control info into a byte
\r
969 dbyte = (buttons << 4) | ((mx + 1) << 2) | (my + 1);
\r
973 (DemoBuffer[DemoOffset + 1] == dbyte)
\r
974 && (DemoBuffer[DemoOffset] < 255)
\r
976 (DemoBuffer[DemoOffset])++;
\r
979 if (DemoOffset || DemoBuffer[DemoOffset])
\r
982 if (DemoOffset >= DemoSize)
\r
983 Quit ("Demo buffer overflow");
\r
985 DemoBuffer[DemoOffset] = 1;
\r
986 DemoBuffer[DemoOffset + 1] = dbyte;
\r
990 #ifdef __DEBUG_InputMgr__
\r
991 if(dbg_testcontrolnoisy > 0)
\r
992 if(player[pn].info.dir!=2/*(inpu.Keyboard[def->up] || inpu.Keyboard[def->down] || inpu.Keyboard[def->left] || inpu.Keyboard[def->right])*/ || player[pn].enti.q>1)
\r
994 //printf("b1=%u b2=%u b3=%u b4=%u ", player[pn].info.button0, player[pn].info.button1, player[pn].info.button2, player[pn].info.button3);
\r
995 //printf("q=%d ", player[pn].enti.q);
\r
996 //printf("cpee=%c ", dirchar(conpee));
\r
997 printf("pdir=%c d=%c dir=%c ", dirchar(player[pn].pdir), dirchar(player[pn].enti.d), dirchar(player[pn].info.dir));
\r
998 /*if(realdelta) */printf("dx=%d dy=%d mx=%d my=%d", player[pn].info.x, player[pn].info.y, player[pn].info.xaxis, player[pn].info.yaxis);
\r
999 //else if(!realdelta) printf("%c%d %c%d %c%d %c%d", dirchar(0), inpu.Keyboard[def->up], dirchar(4), inpu.Keyboard[def->down], dirchar(1), inpu.Keyboard[def->left], dirchar(3), inpu.Keyboard[def->right]);
\r
1005 ///////////////////////////////////////////////////////////////////////////
\r
1007 // IN_SetControlType() - Sets the control type to be used by the specified
\r
1010 ///////////////////////////////////////////////////////////////////////////
\r
1012 IN_SetControlType(word pn,player_t *player,ControlType type)
\r
1014 // DEBUG - check that requested type is present?
\r
1015 player[pn].Controls = type;
\r
1019 ///////////////////////////////////////////////////////////////////////////
\r
1021 // IN_StartDemoRecord() - Starts the demo recording, using a buffer the
\r
1022 // size passed. Returns if the buffer allocation was successful
\r
1024 ///////////////////////////////////////////////////////////////////////////
\r
1026 IN_StartDemoRecord(word bufsize)
\r
1031 MM_GetPtr((memptr *)&DemoBuffer,bufsize);
\r
1032 DemoMode = demo_Record;
\r
1033 DemoSize = bufsize & ~1;
\r
1035 DemoBuffer[0] = DemoBuffer[1] = 0;
\r
1040 ///////////////////////////////////////////////////////////////////////////
\r
1042 // IN_StartDemoPlayback() - Plays back the demo pointed to of the given size
\r
1044 ///////////////////////////////////////////////////////////////////////////
\r
1046 IN_StartDemoPlayback(byte /*__segment*/ *buffer,word bufsize)
\r
1048 DemoBuffer = buffer;
\r
1049 DemoMode = demo_Playback;
\r
1050 DemoSize = bufsize & ~1;
\r
1054 ///////////////////////////////////////////////////////////////////////////
\r
1056 // IN_StopDemo() - Turns off demo mode
\r
1058 ///////////////////////////////////////////////////////////////////////////
\r
1062 if ((DemoMode == demo_Record) && DemoOffset)
\r
1065 DemoMode = demo_Off;
\r
1068 ///////////////////////////////////////////////////////////////////////////
\r
1070 // IN_FreeDemoBuffer() - Frees the demo buffer, if it's been allocated
\r
1072 ///////////////////////////////////////////////////////////////////////////
\r
1074 IN_FreeDemoBuffer(void)
\r
1077 MM_FreePtr((memptr *)&DemoBuffer);
\r
1082 ///////////////////////////////////////////////////////////////////////////
\r
1084 // IN_GetScanName() - Returns a string containing the name of the
\r
1085 // specified scan code
\r
1087 ///////////////////////////////////////////////////////////////////////////
\r
1089 IN_GetScanName(ScanCode scan)
\r
1094 for (s = ExtScanCodes,p = ExtScanNames;*s;p++,s++)
\r
1098 return(ScanNames[scan]);
\r
1101 ///////////////////////////////////////////////////////////////////////////
\r
1103 // IN_WaitForKey() - Waits for a scan code, then clears LastScan and
\r
1104 // returns the scan code
\r
1106 ///////////////////////////////////////////////////////////////////////////
\r
1112 while (!(result = inpu.LastScan))
\r
1114 inpu.LastScan = 0;
\r
1118 ///////////////////////////////////////////////////////////////////////////
\r
1120 // IN_WaitForASCII() - Waits for an ASCII char, then clears LastASCII and
\r
1121 // returns the ASCII value
\r
1123 ///////////////////////////////////////////////////////////////////////////
\r
1129 while (!(result = inpu.LastASCII))
\r
1131 inpu.LastASCII = '\0';
\r
1135 ///////////////////////////////////////////////////////////////////////////
\r
1137 // IN_AckBack() - Waits for either an ASCII keypress or a button press
\r
1139 ///////////////////////////////////////////////////////////////////////////
\r
1145 while (!inpu.LastScan)
\r
1147 if (inpu.MousePresent)
\r
1149 if (INL_GetMouseButtons())
\r
1151 while (INL_GetMouseButtons())
\r
1157 for (i = 0;i < MaxJoys;i++)
\r
1159 if (inpu.JoysPresent[i])
\r
1161 if (IN_GetJoyButtonsDB(i))
\r
1163 while (IN_GetJoyButtonsDB(i))
\r
1171 IN_ClearKey(inpu.LastScan);
\r
1172 inpu.LastScan = sc_None;
\r
1175 ///////////////////////////////////////////////////////////////////////////
\r
1177 // IN_Ack() - Clears user input & then calls IN_AckBack()
\r
1179 ///////////////////////////////////////////////////////////////////////////
\r
1185 if (!inst.IN_Started)
\r
1188 IN_ClearKey(inpu.LastScan);
\r
1189 inpu.LastScan = sc_None;
\r
1191 if (inpu.MousePresent)
\r
1192 while (INL_GetMouseButtons())
\r
1194 for (i = 0;i < MaxJoys;i++)
\r
1195 if (inpu.JoysPresent[i])
\r
1196 while (IN_GetJoyButtonsDB(i))
\r
1202 ///////////////////////////////////////////////////////////////////////////
\r
1204 // IN_IsUserInput() - Returns true if a key has been pressed or a button
\r
1207 ///////////////////////////////////////////////////////////////////////////
\r
1214 result = inpu.LastScan;
\r
1216 if (inpu.MousePresent)
\r
1217 if (INL_GetMouseButtons())
\r
1220 for (i = 0;i < MaxJoys;i++)
\r
1221 if (inpu.JoysPresent[i])
\r
1222 if (INL_GetJoyButtons(i))
\r
1228 ///////////////////////////////////////////////////////////////////////////
\r
1230 // IN_UserInput() - Waits for the specified delay time (in ticks) or the
\r
1231 // user pressing a key or a mouse button. If the clear flag is set, it
\r
1232 // then either clears the key or waits for the user to let the mouse
\r
1235 ///////////////////////////////////////////////////////////////////////////
\r
1237 IN_UserInput(dword delay,boolean clear)
\r
1239 word TimeCount = *clockw;
\r
1242 lasttime = TimeCount;
\r
1245 if (IN_IsUserInput())
\r
1251 } while (TimeCount - lasttime < delay);
\r
1255 //===========================================================================
\r
1258 ===================
\r
1262 ===================
\r
1265 byte IN_MouseButtons (void)
\r
1267 union REGS CPURegs;
\r
1268 if (inpu.MousePresent)
\r
1271 return CPURegs.x.bx;
\r
1279 ===================
\r
1283 ===================
\r
1286 byte IN_JoyButtons (void)
\r
1290 joybits = inp(0x201); // Get all the joystick buttons
\r
1291 joybits >>= 4; // only the high bits are useful
\r
1292 joybits ^= 15; // return with 1=pressed
\r
1297 boolean IN_KeyDown(byte code)
\r
1299 #ifdef __DEBUG_InputMgr__
\r
1302 return inpu.Keyboard[code];
\r
1303 #ifdef __DEBUG_InputMgr__
\r
1305 if(dbg_nointest && kbhit())
\r
1312 void IN_ClearKey(byte code)
\r
1314 inpu.Keyboard[code] = false;
\r
1315 if(code == inpu.LastScan)
\r
1316 inpu.LastScan = sc_None;
\r
1319 boolean IN_qb(byte kee)
\r
1321 #ifdef __DEBUG_InputMgr__
\r
1322 if(dbg_testkeyin) printf("%u\n", inpu.Keyboard[kee]);
\r
1324 if(inpu.Keyboard[kee]==true) return 1;
\r
1328 ScanCode IN_GetLastScan()
\r
1330 return inpu.LastScan;
\r
1333 ScanCode IN_GetCurCode()
\r
1335 return inst.CurCode;
\r