1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2017 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
24 // ID_IN.c - Input Manager
\r
26 // By Jason Blochowiak
\r
27 // Open Watcom port by sparky4
\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 "src/lib/16_in.h"
\r
45 static word far* clockw= (word far*) 0x046C; /* 18.2hz clock */
\r
48 =============================================================================
\r
52 =============================================================================
\r
56 boolean MousePresent;
\r
57 boolean JoysPresent[MaxJoys];
\r
58 boolean Keyboard[NumCodes];
\r
63 KeyboardDef KbdDefs[MaxKbds];
\r
64 JoystickDef JoyDefs[MaxJoys];
\r
67 //gvar->in.KbdDefs = {0x1d,0x38,/*0x47,*/0x48,/*0x49,*/0x4b,0x4d,/*0x4f,*/0x50/*,0x51*/};
\r
70 =============================================================================
\r
74 =============================================================================
\r
77 #ifdef __cplusplus /* Function must be declared C style */
\r
81 /*static struct instat {
\r
83 ScanCode CurCode,LastCode;
\r
85 boolean Keyboard[NumCodes];
\r
91 //the def stuff is need fix warnings
\r
97 static byte far ASCIINames[] = // Unshifted ASCII for scan codes
\r
99 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
100 0 ,27 ,'1','2','3','4','5','6','7','8','9','0','-','=',8 ,9 , // 0
\r
101 'q','w','e','r','t','y','u','i','o','p','[',']',13 ,0 ,'a','s', // 1
\r
102 'd','f','g','h','j','k','l',';',39 ,'`',0 ,92 ,'z','x','c','v', // 2
\r
103 'b','n','m',',','.','/',0 ,'*',0 ,' ',0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
104 0 ,0 ,0 ,0 ,0 ,0 ,0 ,'7','8','9','-','4','5','6','+','1', // 4
\r
105 '2','3','0',127,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
106 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
107 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
109 far ShiftNames[] = // Shifted ASCII for scan codes
\r
111 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
112 0 ,27 ,'!','@','#','$','%','^','&','*','(',')','_','+',8 ,9 , // 0
\r
113 'Q','W','E','R','T','Y','U','I','O','P','{','}',13 ,0 ,'A','S', // 1
\r
114 'D','F','G','H','J','K','L',':',34 ,'~',0 ,'|','Z','X','C','V', // 2
\r
115 'B','N','M','<','>','?',0 ,'*',0 ,' ',0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
116 0 ,0 ,0 ,0 ,0 ,0 ,0 ,'7','8','9','-','4','5','6','+','1', // 4
\r
117 '2','3','0',127,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
118 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
119 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
121 far SpecialNames[] = // ASCII for 0xe0 prefixed codes
\r
123 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
\r
124 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 0
\r
125 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,13 ,0 ,0 ,0 , // 1
\r
126 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 2
\r
127 0 ,0 ,0 ,0 ,0 ,'/',0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 3
\r
128 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 4
\r
129 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
\r
130 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
\r
131 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
\r
133 *ScanNames[] = // Scan code names with single chars
\r
135 "?","?","1","2","3","4","5","6","7","8","9","0","-","+","?","?",
\r
136 "Q","W","E","R","T","Y","U","I","O","P","[","]","|","?","A","S",
\r
137 "D","F","G","H","J","K","L",";","\"","?","?","?","Z","X","C","V",
\r
138 "B","N","M",",",".","/","?","?","?","?","?","?","?","?","?","?",
\r
139 "?","?","?","?","?","?","?","?","\xf","?","-","\x15","5","\x11","+","?",
\r
140 "\x13","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",
\r
141 "?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",
\r
142 "?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"
\r
143 }, // DEBUG - consolidate these
\r
144 far ExtScanCodes[] = // Scan codes with >1 char names
\r
146 1,0xe,0xf,0x1d,0x2a,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,
\r
147 0x3f,0x40,0x41,0x42,0x43,0x44,0x57,0x59,0x46,0x1c,0x36,
\r
148 0x37,0x38,0x47,0x49,0x4f,0x51,0x52,0x53,0x45,0x48,
\r
149 0x50,0x4b,0x4d,0x00
\r
151 *ExtScanNames[] = // Names corresponding to ExtScanCodes
\r
153 "Esc","BkSp","Tab","Ctrl","LShft","Space","CapsLk","F1","F2","F3","F4",
\r
154 "F5","F6","F7","F8","F9","F10","F11","F12","ScrlLk","Enter","RShft",
\r
155 "PrtSc","Alt","Home","PgUp","End","PgDn","Ins","Del","NumLk","Up",
\r
156 "Down","Left","Right",""
\r
159 static Direction DirTable[] = // Quick lookup for total direction
\r
161 /*dir_NorthWest, */dir_North,/* dir_NorthEast,*/
\r
162 dir_West, dir_None, dir_East,
\r
163 /*dir_SouthWest, */dir_South/*, dir_SouthEast*/
\r
169 static void (*INL_KeyHook)(void);
\r
170 static void interrupt (*OldKeyVect)(void);
\r
171 static char *ParmStringsIN[] = {"nojoys","nomouse",nil};
\r
172 static void INL_StartKbd(void);
\r
174 // Internal routines
\r
176 ///////////////////////////////////////////////////////////////////////////
\r
178 // INL_KeyService() - Handles a keyboard interrupt (key up/down)
\r
180 ///////////////////////////////////////////////////////////////////////////
\r
181 /*static */void interrupt
\r
182 INL_KeyService(void)
\r
184 static boolean special;
\r
188 k = inportb(0x60); // Get the scan code
\r
190 // Tell the XT keyboard controller to clear the key
\r
191 outportb(0x61,(temp = inportb(0x61)) | 0x80);
\r
192 outportb(0x61,temp);
\r
194 if (k == 0xe0) // Special key prefix
\r
196 else if (k == 0xe1) // Handle Pause key
\r
197 inst.Paused = true;
\r
200 if (k & 0x80) // Break code
\r
204 // DEBUG - handle special keys: ctl-alt-delete, print scrn
\r
206 inst.Keyboard[k] = false;
\r
210 inst.LastCode = inst.CurCode;
\r
211 inst.CurCode = inst.LastScan = k;
\r
212 inst.Keyboard[k] = true;
\r
215 c = SpecialNames[k];
\r
218 if (k == sc_CapsLock)
\r
220 inst.CapsLock ^= true;
\r
221 // DEBUG - make caps lock light work
\r
224 if (inst.Keyboard[sc_LShift] || inst.Keyboard[sc_RShift]) // If shifted
\r
227 if ((c >= 'A') && (c <= 'Z') && inst.CapsLock)
\r
233 if ((c >= 'a') && (c <= 'z') && inst.CapsLock)
\r
238 inst.LastASCII = c;
\r
244 if (INL_KeyHook && !special)
\r
246 #ifdef __DEBUG_InputMgr__
\r
247 if(dbg_testkeyin > 0) printf("%c %u [0x%x %u] %u\n", c, c, k, k, inst.Keyboard[k]);
\r
249 outportb(0x20,0x20);
\r
252 void INL_Mouse(int x)
\r
254 union REGS CPURegs;
\r
260 int86(MouseInt,&CPURegs,&CPURegs);
\r
263 ///////////////////////////////////////////////////////////////////////////
\r
265 // INL_GetMouseDelta() - Gets the amount that the mouse has moved from the
\r
268 ///////////////////////////////////////////////////////////////////////////
\r
270 INL_GetMouseDelta(int *x,int *y)
\r
272 union REGS CPURegs;
\r
276 #ifdef __DEBUG_InputMgr__
\r
277 if(dbg_joymousedelta)
\r
279 printf("mousedelta=[%dx%d]\n", *x, *y);
\r
284 ///////////////////////////////////////////////////////////////////////////
\r
286 // INL_GetMouseButtons() - Gets the status of the mouse buttons from the
\r
289 ///////////////////////////////////////////////////////////////////////////
\r
291 INL_GetMouseButtons(void)
\r
293 union REGS CPURegs;
\r
297 buttons = CPURegs.x.bx;
\r
301 ///////////////////////////////////////////////////////////////////////////
\r
303 // IN_GetJoyAbs() - Reads the absolute position of the specified joystick
\r
305 ///////////////////////////////////////////////////////////////////////////
\r
307 IN_GetJoyAbs(word joy,word *xp,word *yp)
\r
314 xs = joy? 2 : 0; // Find shift value for x axis
\r
315 xb = 1 << xs; // Use shift value to get x bit mask
\r
316 ys = joy? 3 : 1; // Do the same for y axis
\r
319 // Read the absolute joystick values
\r
321 pushf // Save some registers
\r
324 cli // Make sure an interrupt doesn't screw the timings
\r
329 out dx,al // Clear the resistors
\r
331 mov ah,[xb] // Get masks into registers
\r
334 xor si,si // Clear count registers
\r
336 xor bh,bh // Clear high byte of bx for later
\r
338 push bp // Don't mess up stack frame
\r
340 #ifdef __BORLANDC__
\r
344 #ifdef __BORLANDC__
\r
347 in al,dx // Get bits indicating whether all are finished
\r
349 dec bp // Check bounding register
\r
350 jz done // We have a silly value - abort
\r
352 mov bl,al // Duplicate the bits
\r
353 and bl,ah // Mask off useless bits (in [xb])
\r
354 add si,bx // Possibly increment count register
\r
355 mov cl,bl // Save for testing later
\r
362 jnz loo // If both bits were 0, drop out
\r
363 #ifdef __BORLANDC__
\r
367 #ifdef __BORLANDC__
\r
372 mov cl,[xs] // Get the number of bits to shift
\r
373 shr si,cl // and shift the count that many times
\r
378 mov [x],si // Store the values into the variables
\r
383 popf // Restore the registers
\r
390 ///////////////////////////////////////////////////////////////////////////
\r
392 // INL_GetJoyDelta() - Returns the relative movement of the specified
\r
393 // joystick (from +/-127)
\r
395 ///////////////////////////////////////////////////////////////////////////
\r
396 void INL_GetJoyDelta(word joy,int *dx,int *dy/*,boolean adaptive*/, global_game_variables_t *gvar)
\r
399 word TimeCount = *clockw;
\r
401 static word lasttime;
\r
403 IN_GetJoyAbs(joy,&x,&y);
\r
404 def = gvar->in.JoyDefs + joy;
\r
406 if (x < def->threshMinX)
\r
408 if (x < def->joyMinX)
\r
411 x = -(x - def->threshMinX);
\r
412 x *= def->joyMultXL;
\r
413 x >>= JoyScaleShift;
\r
414 *dx = (x > 127)? -127 : -x;
\r
416 else if (x > def->threshMaxX)
\r
418 if (x > def->joyMaxX)
\r
421 x = x - def->threshMaxX;
\r
422 x *= def->joyMultXH;
\r
423 x >>= JoyScaleShift;
\r
424 *dx = (x > 127)? 127 : x;
\r
429 if (y < def->threshMinY)
\r
431 if (y < def->joyMinY)
\r
434 y = -(y - def->threshMinY);
\r
435 y *= def->joyMultYL;
\r
436 y >>= JoyScaleShift;
\r
437 *dy = (y > 127)? -127 : -y;
\r
439 else if (y > def->threshMaxY)
\r
441 if (y > def->joyMaxY)
\r
444 y = y - def->threshMaxY;
\r
445 y *= def->joyMultYH;
\r
446 y >>= JoyScaleShift;
\r
447 *dy = (y > 127)? 127 : y;
\r
453 // time = (TimeCount - lasttime) / 2;
\r
462 lasttime = TimeCount;
\r
465 ///////////////////////////////////////////////////////////////////////////
\r
467 // INL_GetJoyButtons() - Returns the button status of the specified
\r
470 ///////////////////////////////////////////////////////////////////////////
\r
472 INL_GetJoyButtons(word joy)
\r
474 register word result;
\r
476 result = inportb(0x201); // Get all the joystick buttons
\r
477 result >>= joy? 6 : 4; // Shift into bits 0-1
\r
478 result &= 3; // Mask off the useless bits
\r
483 ///////////////////////////////////////////////////////////////////////////
\r
485 // IN_GetJoyButtonsDB() - Returns the de-bounced button status of the
\r
486 // specified joystick
\r
488 ///////////////////////////////////////////////////////////////////////////
\r
490 IN_GetJoyButtonsDB(word joy)
\r
492 word TimeCount = *clockw;
\r
494 word result1,result2;
\r
498 result1 = INL_GetJoyButtons(joy);
\r
499 lasttime = TimeCount;
\r
500 while(TimeCount == lasttime)
\r
502 result2 = INL_GetJoyButtons(joy);
\r
503 } while(result1 != result2);
\r
507 ///////////////////////////////////////////////////////////////////////////
\r
509 // INL_StartKbd() - Sets up my keyboard stuff for use
\r
511 ///////////////////////////////////////////////////////////////////////////
\r
515 INL_KeyHook = NULL; // Clear key hook
\r
517 IN_ClearKeysDown();
\r
519 OldKeyVect = getvect(KeyInt);//IN_KbdLED();
\r
520 setvect(KeyInt,INL_KeyService);
\r
523 ///////////////////////////////////////////////////////////////////////////
\r
525 // INL_ShutKbd() - Restores keyboard control to the BIOS
\r
527 ///////////////////////////////////////////////////////////////////////////
\r
531 pokeb(0x40,0x17,peekb(0x40,0x17) & 0xfaf0); // Clear ctrl/alt/shift flags
\r
533 setvect(KeyInt,OldKeyVect);
\r
536 ///////////////////////////////////////////////////////////////////////////
\r
538 // INL_StartMouse() - Detects and sets up the mouse
\r
540 ///////////////////////////////////////////////////////////////////////////
\r
542 INL_StartMouse(void)
\r
544 union REGS CPURegs;
\r
545 if (getvect(MouseInt))
\r
548 if (/*_AX*/CPURegs.x.ax == 0xffff)
\r
556 if ((vector=MK_FP(peek(0,0x33*4+2),peek(0,0x33*4)))==NULL)
\r
559 if (*vector == 207)
\r
567 ///////////////////////////////////////////////////////////////////////////
\r
569 // INL_ShutMouse() - Cleans up after the mouse
\r
571 ///////////////////////////////////////////////////////////////////////////
\r
573 INL_ShutMouse(void)
\r
578 // INL_SetJoyScale() - Sets up scaling values for the specified joystick
\r
581 INL_SetJoyScale(word joy, global_game_variables_t *gvar)
\r
585 def = &(gvar->in.JoyDefs[joy]);
\r
586 def->joyMultXL = JoyScaleMax / (def->threshMinX - def->joyMinX);
\r
587 def->joyMultXH = JoyScaleMax / (def->joyMaxX - def->threshMaxX);
\r
588 def->joyMultYL = JoyScaleMax / (def->threshMinY - def->joyMinY);
\r
589 def->joyMultYH = JoyScaleMax / (def->joyMaxY - def->threshMaxY);
\r
592 ///////////////////////////////////////////////////////////////////////////
\r
594 // IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale()
\r
595 // to set up scaling values
\r
597 ///////////////////////////////////////////////////////////////////////////
\r
599 IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy, global_game_variables_t *gvar)
\r
604 def = &(gvar->in.JoyDefs[joy]);
\r
606 def->joyMinX = minx;
\r
607 def->joyMaxX = maxx;
\r
610 def->threshMinX = ((r / 2) - d) + minx;
\r
611 def->threshMaxX = ((r / 2) + d) + minx;
\r
613 def->joyMinY = miny;
\r
614 def->joyMaxY = maxy;
\r
617 def->threshMinY = ((r / 2) - d) + miny;
\r
618 def->threshMaxY = ((r / 2) + d) + miny;
\r
620 INL_SetJoyScale(joy, gvar);
\r
623 ///////////////////////////////////////////////////////////////////////////
\r
625 // INL_StartJoy() - Detects & auto-configures the specified joystick
\r
626 // The auto-config assumes the joystick is centered
\r
628 ///////////////////////////////////////////////////////////////////////////
\r
630 INL_StartJoy(word joy, global_game_variables_t *gvar)
\r
634 IN_GetJoyAbs(joy,&x,&y);
\r
638 ((x == 0) || (x > MaxJoyValue - 10))
\r
639 || ((y == 0) || (y > MaxJoyValue - 10))
\r
644 IN_SetupJoy(joy,0,x * 2,0,y * 2, gvar);
\r
649 ///////////////////////////////////////////////////////////////////////////
\r
651 // INL_ShutJoy() - Cleans up the joystick stuff
\r
653 ///////////////////////////////////////////////////////////////////////////
\r
655 INL_ShutJoy(word joy, global_game_variables_t *gvar)
\r
657 gvar->in.JoysPresent[joy] = false;
\r
662 ///////////////////////////////////////////////////////////////////////////
\r
664 // IN_Startup() - Starts up the Input Mgr
\r
666 ///////////////////////////////////////////////////////////////////////////
\r
668 IN_Startup(global_game_variables_t *gvar)
\r
670 boolean checkjoys,checkmouse;
\r
673 if (gvar->in.IN_Started)
\r
678 for (i = 1;i < _argc;i++)
\r
680 switch (US_CheckParm(_argv[i],ParmStringsIN))
\r
686 checkmouse = false;
\r
692 gvar->in.MousePresent = checkmouse? INL_StartMouse() : false;
\r
694 for (i = 0;i < MaxJoys;i++)
\r
695 gvar->in.JoysPresent[i] = checkjoys? INL_StartJoy(i, gvar) : false;
\r
697 gvar->in.inst = &inst;
\r
699 gvar->in.IN_Started = true;
\r
703 ///////////////////////////////////////////////////////////////////////////
\r
705 // IN_Default() - Sets up default conditions for the Input Mgr
\r
707 ///////////////////////////////////////////////////////////////////////////
\r
709 IN_Default(boolean gotit,player_t *player,ControlType nt, global_game_variables_t *gvar)
\r
715 || ((nt == ctrl_Joystick1) && !gvar->in.JoysPresent[0])
\r
716 || ((nt == ctrl_Joystick2) && !gvar->in.JoysPresent[1])
\r
717 || ((nt == ctrl_Mouse) && !gvar->in.MousePresent)
\r
719 nt = ctrl_Keyboard1;
\r
720 gvar->in.KbdDefs[0].button0 = 0x1c;
\r
721 gvar->in.KbdDefs[0].button1 = 0x38;
\r
722 //in.KbdDefs[0].upleft = 0x47;
\r
723 gvar->in.KbdDefs[0].up = 0x48;
\r
724 //in.KbdDefs[0].upright = 0x49;
\r
725 gvar->in.KbdDefs[0].left = 0x4b;
\r
726 gvar->in.KbdDefs[0].right = 0x4d;
\r
727 //in.KbdDefs[0].downleft = 0x4f;
\r
728 gvar->in.KbdDefs[0].down = 0x50;
\r
729 //in.KbdDefs[0].downright = 0x51;
\r
730 IN_SetControlType(player,nt);
\r
731 for(i=0; i>MaxPlayers;i++)
\r
732 player[i].enti.d =2;
\r
735 ///////////////////////////////////////////////////////////////////////////
\r
737 // IN_Shutdown() - Shuts down the Input Mgr
\r
739 ///////////////////////////////////////////////////////////////////////////
\r
741 IN_Shutdown(global_game_variables_t *gvar)
\r
745 if (!gvar->in.IN_Started)
\r
749 for (i = 0;i < MaxJoys;i++)
\r
750 INL_ShutJoy(i, gvar);
\r
753 gvar->in.IN_Started = false;
\r
756 ///////////////////////////////////////////////////////////////////////////
\r
758 // IN_SetKeyHook() - Sets the routine that gets called by INL_KeyService()
\r
759 // everytime a real make/break code gets hit
\r
761 ///////////////////////////////////////////////////////////////////////////
\r
763 IN_SetKeyHook(void (*hook)())
\r
765 INL_KeyHook = hook;
\r
768 ///////////////////////////////////////////////////////////////////////////
\r
770 // IN_ClearKeysDown() - Clears the keyboard array
\r
772 ///////////////////////////////////////////////////////////////////////////
\r
774 IN_ClearKeysDown(void)
\r
778 inst.LastScan = sc_None;
\r
779 inst.LastASCII = key_None;
\r
780 memset (inst.Keyboard,0,sizeof(inst.Keyboard));
\r
783 ///////////////////////////////////////////////////////////////////////////
\r
785 // INL_AdjustCursor() - Internal routine of common code from IN_ReadCursor()
\r
787 ///////////////////////////////////////////////////////////////////////////
\r
789 INL_AdjustCursor(CursorInfo *info,word buttons,int dx,int dy)
\r
791 if (buttons & (1 << 0))
\r
792 info->button0 = true;
\r
793 if (buttons & (1 << 1))
\r
794 info->button1 = true;
\r
800 ///////////////////////////////////////////////////////////////////////////
\r
802 // IN_ReadCursor() - Reads the input devices and fills in the cursor info
\r
805 ///////////////////////////////////////////////////////////////////////////
\r
807 IN_ReadCursor(CursorInfo *info, global_game_variables_t *gvar)
\r
813 info->x = info->y = 0;
\r
814 info->button0 = info->button1 = false;
\r
816 if (gvar->in.MousePresent)
\r
818 buttons = INL_GetMouseButtons();
\r
819 INL_GetMouseDelta(&dx,&dy);
\r
820 INL_AdjustCursor(info,buttons,dx,dy);
\r
823 for (i = 0;i < MaxJoys;i++)
\r
825 if (!gvar->in.JoysPresent[i])
\r
828 buttons = INL_GetJoyButtons(i);
\r
829 INL_GetJoyDelta(i,&dx,&dy/*,true*/, gvar);
\r
832 INL_AdjustCursor(info,buttons,dx,dy);
\r
836 //if else for gfxtesting and direction
\r
837 #define DIRECTIONIFELSE (player->info.dir == 2)
\r
838 //#define NDIRECTIONIFELSE (player->info.dir != 2)
\r
840 ///////////////////////////////////////////////////////////////////////////
\r
842 // IN_ReadControl() - Reads the device associated with the specified
\r
843 // player and fills in the control info struct
\r
845 ///////////////////////////////////////////////////////////////////////////
\r
847 IN_ReadControl(player_t *player, global_game_variables_t *gvar)
\r
858 byte dir=DirTable[2];
\r
859 register KeyboardDef *def;
\r
860 //#ifdef __DEBUG_InputMgr__
\r
861 //static int old_dx,old_dy;
\r
865 mx = my = motion_None;
\r
869 if (DemoMode == demo_Playback)
\r
871 dbyte = DemoBuffer[DemoOffset + 1];
\r
872 my = (dbyte & 3) - 1;
\r
873 mx = ((dbyte >> 2) & 3) - 1;
\r
874 buttons = (dbyte >> 4) & 3;
\r
876 if (!(--DemoBuffer[DemoOffset]))
\r
879 if (DemoOffset >= DemoSize)
\r
880 DemoMode = demo_PlayDone;
\r
885 else if (DemoMode == demo_PlayDone)
\r
886 Quit ("Demo playback exceeded");
\r
890 switch (type = player->Controls)
\r
892 case ctrl_Keyboard1:
\r
893 case ctrl_Keyboard2:
\r
894 def = &(gvar->in.KbdDefs[type - ctrl_Keyboard]);
\r
896 /* if (Keyboard[def->upleft])
\r
897 mx = motion_Left,my = motion_Up;
\r
898 else if (Keyboard[def->upright])
\r
899 mx = motion_Right,my = motion_Up;
\r
900 else if (Keyboard[def->downleft])
\r
901 mx = motion_Left,my = motion_Down;
\r
902 else if (Keyboard[def->downright])
\r
903 mx = motion_Right,my = motion_Down;*/
\r
904 //TODO: make this into a function that the joystick AND keyboard can use wwww
\r
905 if(DIRECTIONIFELSE)//(player->info.dir == 2)
\r
907 if(!inst.Keyboard[def->left] && !inst.Keyboard[def->right]){
\r
908 if((inst.Keyboard[def->up] && !inst.Keyboard[def->down]))
\r
910 if((inst.Keyboard[def->down] && !inst.Keyboard[def->up]))
\r
912 }else if(!inst.Keyboard[def->up] && !inst.Keyboard[def->down]){
\r
913 if((inst.Keyboard[def->left] && !inst.Keyboard[def->right]))
\r
915 if((inst.Keyboard[def->right] && !inst.Keyboard[def->left]))
\r
917 }else{ //2 keys pressed
\r
918 switch (player->pdir)
\r
922 if((inst.Keyboard[def->left] && !inst.Keyboard[def->right])){ dir = DirTable[1]; }//mx = motion_Left; }
\r
923 else if((inst.Keyboard[def->right] && !inst.Keyboard[def->left])){ dir = DirTable[3]; }//mx = motion_Right; }
\r
927 if((inst.Keyboard[def->up] && !inst.Keyboard[def->down])){ dir = DirTable[0]; }//my = motion_Up; }
\r
928 else if((inst.Keyboard[def->down] && !inst.Keyboard[def->up])){ dir = DirTable[4]; }//my = motion_Down; }
\r
933 #ifdef __DEBUG_InputMgr__
\r
934 //if(dbg_testcontrolnoisy > 0){ printf("dir=%c ", dirchar(dir)); printf("pdir=%c ", dirchar(player->pdir)); }
\r
938 //input from player
\r
939 if (inst.Keyboard[def->button0])
\r
941 if (inst.Keyboard[def->button1])
\r
945 case ctrl_Joystick1:
\r
946 case ctrl_Joystick2:
\r
947 INL_GetJoyDelta(type - ctrl_Joystick,&dx,&dy/*,false*/, gvar);
\r
948 buttons = INL_GetJoyButtons(type - ctrl_Joystick);
\r
952 INL_GetMouseDelta(&dx,&dy);
\r
953 buttons = INL_GetMouseButtons();
\r
963 mx = (dx < 0)? motion_Left : ((dx > 0)? motion_Right : motion_None);
\r
964 my = (dy < 0)? motion_Up : ((dy > 0)? motion_Down : motion_None);
\r
972 player->info.x = dx;
\r
973 player->info.xaxis = mx;
\r
974 player->info.y = dy;
\r
975 player->info.yaxis = my;
\r
976 player->info.button0 = buttons & (1 << 0);
\r
977 player->info.button1 = buttons & (1 << 1);
\r
978 player->info.button2 = buttons & (1 << 2);
\r
979 player->info.button3 = buttons & (1 << 3);
\r
980 // player->info.dir = DirTable[((my + 1) * 3) + (mx + 1)];
\r
981 conpee=(((my + 1) * 2) + (mx + 1))-1;
\r
982 player->info.dir = DirTable[conpee];
\r
984 if(DirTable[conpee]!=2) player->pdir=DirTable[conpee];
\r
985 if(player->enti.q==1 &&( dir!=2 || (mx!=motion_None || my!=motion_None)))
\r
987 if(dir==2) player->enti.d = player->info.dir;
\r
988 else player->enti.d = DirTable[dir];
\r
992 if (DemoMode == demo_Record)
\r
994 // Pack the control info into a byte
\r
995 dbyte = (buttons << 4) | ((mx + 1) << 2) | (my + 1);
\r
999 (DemoBuffer[DemoOffset + 1] == dbyte)
\r
1000 && (DemoBuffer[DemoOffset] < 255)
\r
1002 (DemoBuffer[DemoOffset])++;
\r
1005 if (DemoOffset || DemoBuffer[DemoOffset])
\r
1008 if (DemoOffset >= DemoSize)
\r
1009 Quit ("Demo buffer overflow");
\r
1011 DemoBuffer[DemoOffset] = 1;
\r
1012 DemoBuffer[DemoOffset + 1] = dbyte;
\r
1016 #ifdef __DEBUG_InputMgr__
\r
1017 /*if(dbg_joymousedelta)
\r
1019 if(dx!=old_dx || dy!=old_dy) printf("dx,dy [%d,%d] %d,%d\n", dx, dy, mx, my);
\r
1020 if(dx!=old_dx) old_dx=dx;
\r
1021 if(dy!=old_dy) old_dy=dy;
\r
1024 if(dbg_testcontrolnoisy > 0)
\r
1025 if(player->info.dir!=2/*(inst.Keyboard[def->up] || inst.Keyboard[def->down] || inst.Keyboard[def->left] || inst.Keyboard[def->right])*/ || player->enti.q>1)
\r
1027 //printf("b1=%u b2=%u b3=%u b4=%u ", player->info.button0, player->info.button1, player->info.button2, player->info.button3);
\r
1028 //printf("q=%d ", player->enti.q);
\r
1029 //printf("cpee=%c ", dirchar(conpee));
\r
1030 printf("pdir=%c d=%c dir=%c ", dirchar(player->pdir), dirchar(player->enti.d), dirchar(player->info.dir));
\r
1031 /*if(realdelta) */printf("dx=%d dy=%d mx=%d my=%d", player->info.x, player->info.y, player->info.xaxis, player->info.yaxis);
\r
1032 //else if(!realdelta) printf("%c%d %c%d %c%d %c%d", dirchar(0), inst.Keyboard[def->up], dirchar(4), inst.Keyboard[def->down], dirchar(1), inst.Keyboard[def->left], dirchar(3), inst.Keyboard[def->right]);
\r
1038 ///////////////////////////////////////////////////////////////////////////
\r
1040 // IN_SetControlType() - Sets the control type to be used by the specified
\r
1043 ///////////////////////////////////////////////////////////////////////////
\r
1045 IN_SetControlType(player_t *player,ControlType type)
\r
1047 // DEBUG - check that requested type is present?
\r
1048 player->Controls = type;
\r
1052 ///////////////////////////////////////////////////////////////////////////
\r
1054 // IN_StartDemoRecord() - Starts the demo recording, using a buffer the
\r
1055 // size passed. Returns if the buffer allocation was successful
\r
1057 ///////////////////////////////////////////////////////////////////////////
\r
1059 IN_StartDemoRecord(word bufsize)
\r
1064 MM_GetPtr((memptr *)&DemoBuffer,bufsize);
\r
1065 DemoMode = demo_Record;
\r
1066 DemoSize = bufsize & ~1;
\r
1068 DemoBuffer[0] = DemoBuffer[1] = 0;
\r
1073 ///////////////////////////////////////////////////////////////////////////
\r
1075 // IN_StartDemoPlayback() - Plays back the demo pointed to of the given size
\r
1077 ///////////////////////////////////////////////////////////////////////////
\r
1079 IN_StartDemoPlayback(byte /*__segment*/ *buffer,word bufsize)
\r
1081 DemoBuffer = buffer;
\r
1082 DemoMode = demo_Playback;
\r
1083 DemoSize = bufsize & ~1;
\r
1087 ///////////////////////////////////////////////////////////////////////////
\r
1089 // IN_StopDemo() - Turns off demo mode
\r
1091 ///////////////////////////////////////////////////////////////////////////
\r
1095 if ((DemoMode == demo_Record) && DemoOffset)
\r
1098 DemoMode = demo_Off;
\r
1101 ///////////////////////////////////////////////////////////////////////////
\r
1103 // IN_FreeDemoBuffer() - Frees the demo buffer, if it's been allocated
\r
1105 ///////////////////////////////////////////////////////////////////////////
\r
1107 IN_FreeDemoBuffer(void)
\r
1110 MM_FreePtr((memptr *)&DemoBuffer);
\r
1115 ///////////////////////////////////////////////////////////////////////////
\r
1117 // IN_GetScanName() - Returns a string containing the name of the
\r
1118 // specified scan code
\r
1120 ///////////////////////////////////////////////////////////////////////////
\r
1122 IN_GetScanName(ScanCode scan)
\r
1127 for (s = ExtScanCodes,p = ExtScanNames;*s;p++,s++)
\r
1131 return(ScanNames[scan]);
\r
1134 ///////////////////////////////////////////////////////////////////////////
\r
1136 // IN_WaitForKey() - Waits for a scan code, then clears LastScan and
\r
1137 // returns the scan code
\r
1139 ///////////////////////////////////////////////////////////////////////////
\r
1141 IN_WaitForKey(void)
\r
1145 while (!(result = inst.LastScan))
\r
1147 inst.LastScan = 0;
\r
1151 ///////////////////////////////////////////////////////////////////////////
\r
1153 // IN_WaitForASCII() - Waits for an ASCII char, then clears LastASCII and
\r
1154 // returns the ASCII value
\r
1156 ///////////////////////////////////////////////////////////////////////////
\r
1158 IN_WaitForASCII(void)
\r
1162 while (!(result = inst.LastASCII))
\r
1164 inst.LastASCII = '\0';
\r
1168 ///////////////////////////////////////////////////////////////////////////
\r
1170 // IN_Ack() - waits for a button or key press. If a button is down, upon
\r
1171 // calling, it must be released for it to be recognized
\r
1173 ///////////////////////////////////////////////////////////////////////////
\r
1175 boolean btnstate[8];
\r
1177 void IN_StartAck(global_game_variables_t *gvar)
\r
1179 unsigned i,buttons;
\r
1182 // get initial state of everything
\r
1184 IN_ClearKeysDown();
\r
1185 memset (btnstate,0,sizeof(btnstate));
\r
1187 buttons = IN_JoyButtons () << 4;
\r
1188 if (gvar->in.MousePresent)
\r
1189 buttons |= IN_MouseButtons (gvar);
\r
1191 for (i=0;i<8;i++,buttons>>=1)
\r
1193 btnstate[i] = true;
\r
1197 boolean IN_CheckAck (global_game_variables_t *gvar)
\r
1199 unsigned i,buttons;
\r
1202 // see if something has been pressed
\r
1204 if(!gvar->in.IN_Started)
\r
1208 if (inst.LastScan)
\r
1212 buttons = IN_JoyButtons () << 4;
\r
1213 if (gvar->in.MousePresent)
\r
1214 buttons |= IN_MouseButtons (gvar);
\r
1216 for (i=0;i<8;i++,buttons>>=1)
\r
1223 btnstate[i]=false;
\r
1229 void IN_Ack (global_game_variables_t *gvar)
\r
1231 IN_StartAck (gvar);
\r
1233 while (!IN_CheckAck (gvar))
\r
1237 ///////////////////////////////////////////////////////////////////////////
\r
1239 // IN_IsUserInput() - Returns true if a key has been pressed or a button
\r
1242 ///////////////////////////////////////////////////////////////////////////
\r
1244 IN_IsUserInput(global_game_variables_t *gvar)
\r
1249 result = inst.LastScan;
\r
1251 if (gvar->in.MousePresent)
\r
1252 if (INL_GetMouseButtons())
\r
1255 for (i = 0;i < MaxJoys;i++)
\r
1256 if (gvar->in.JoysPresent[i])
\r
1257 if (INL_GetJoyButtons(i))
\r
1263 ///////////////////////////////////////////////////////////////////////////
\r
1265 // IN_UserInput() - Waits for the specified delay time (in ticks) or the
\r
1266 // user pressing a key or a mouse button. If the clear flag is set, it
\r
1267 // then either clears the key or waits for the user to let the mouse
\r
1270 ///////////////////////////////////////////////////////////////////////////
\r
1271 boolean IN_UserInput(word delay, global_game_variables_t *gvar)
\r
1273 word TimeCount = *clockw;
\r
1276 lasttime = TimeCount;
\r
1277 IN_StartAck (gvar);
\r
1280 if (IN_CheckAck(gvar))
\r
1282 } while (TimeCount - lasttime < delay);
\r
1286 //===========================================================================
\r
1289 ===================
\r
1293 ===================
\r
1296 byte IN_MouseButtons (global_game_variables_t *gvar)
\r
1298 union REGS CPURegs;
\r
1299 if (gvar->in.MousePresent)
\r
1302 return CPURegs.x.bx;
\r
1310 ===================
\r
1314 ===================
\r
1317 byte IN_JoyButtons (void)
\r
1321 joybits = inportb(0x201); // Get all the joystick buttons
\r
1322 joybits >>= 4; // only the high bits are useful
\r
1323 joybits ^= 15; // return with 1=pressed
\r
1328 boolean IN_KeyDown(byte code)
\r
1330 #ifdef __DEBUG_InputMgr__
\r
1331 // if(inst.Keyboard[code])
\r
1332 // printf("IN_KeyDown(%c): %u\n", code, inst.Keyboard[code]);
\r
1335 return inst.Keyboard[code];
\r
1336 #ifdef __DEBUG_InputMgr__
\r
1338 if(dbg_nointest && kbhit())
\r
1345 void IN_ClearKey(byte code)
\r
1347 inst.Keyboard[code] = false;
\r
1348 if(code == inst.LastScan)
\r
1349 inst.LastScan = sc_None;
\r
1352 boolean IN_qb(byte kee)
\r
1354 #ifdef __DEBUG_InputMgr__
\r
1355 if(dbg_testkeyin) if(inst.Keyboard[kee]) printf("IN_qb(): %u\n", inst.Keyboard[kee]);
\r
1357 if(inst.Keyboard[kee]==true) return 1;
\r
1361 ScanCode IN_GetLastScan()
\r
1363 return inst.LastScan;
\r
1366 ScanCode IN_GetCurCode()
\r
1368 return inst.CurCode;
\r
1373 byte far *lock_key;
\r
1375 // turn off num-lock via BIOS
\r
1376 lock_key = MK_FP(0x040, 0x017); // Pointing to the address of the bios shift state keys
\r
1377 *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
1378 OldKeyVect(); // call BIOS keyhandler to change keyboard lights
\r