]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_in.c
meh ca needs huge amounts of work and i should remember what needs to be done soon w
[16.git] / src / lib / 16_in.c
1 /* Catacomb Apocalypse Source Code\r
2  * Copyright (C) 1993-2014 Flat Rock Software\r
3  *\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
8  *\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
13  *\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
17  */\r
18 \r
19 //\r
20 //      ID Engine\r
21 //      ID_IN.c - Input Manager\r
22 //      v1.0d1w\r
23 //      By Jason Blochowiak\r
24 //      Open Watcom port by sparky4\r
25 //\r
26 \r
27 //\r
28 //      This module handles dealing with the various input devices\r
29 //\r
30 //      Depends on: Memory Mgr (for demo recording), Sound Mgr (for timing stuff),\r
31 //                              User Mgr (for command line parms)\r
32 //\r
33 //      Globals:\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
37 //\r
38 \r
39 #include "src/lib/16_in.h"\r
40 \r
41 static word far* clockw= (word far*) 0x046C; /* 18.2hz clock */\r
42 \r
43 /*\r
44 =============================================================================\r
45 \r
46                                         GLOBAL VARIABLES\r
47 \r
48 =============================================================================\r
49 */\r
50 struct inconfig\r
51 {\r
52         boolean         MousePresent;\r
53         boolean         JoysPresent[MaxJoys];\r
54         boolean         Keyboard[NumCodes];\r
55         boolean         Paused;\r
56         char            LastASCII;\r
57         ScanCode        LastScan;\r
58 \r
59         KeyboardDef     KbdDefs[MaxKbds];\r
60         JoystickDef     JoyDefs[MaxJoys];\r
61 } inpu;\r
62 \r
63 //extern inconfig inpu;\r
64 //inpu.KbdDefs = {0x1d,0x38,/*0x47,*/0x48,/*0x49,*/0x4b,0x4d,/*0x4f,*/0x50/*,0x51*/};\r
65 \r
66 /*\r
67 =============================================================================\r
68 \r
69                                         LOCAL VARIABLES\r
70 \r
71 =============================================================================\r
72 */\r
73 \r
74 #ifdef __cplusplus              /* Function must be declared C style */\r
75 extern "C" {\r
76 #endif\r
77 \r
78 static struct instat {\r
79         boolean         IN_Started;\r
80         boolean         CapsLock;\r
81         ScanCode        CurCode,LastCode;\r
82 } inst;\r
83 \r
84 static  void                    (*INL_KeyHook)(void);\r
85 static  void interrupt  (*OldKeyVect)(void);\r
86 static  char                    *ParmStringsIN[] = {"nojoys","nomouse",nil};\r
87 \r
88 static  byte        far ASCIINames[] =          // Unshifted ASCII for scan codes\r
89                                         {\r
90 //       0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F\r
91         0  ,27 ,'1','2','3','4','5','6','7','8','9','0','-','=',8  ,9  ,        // 0\r
92         'q','w','e','r','t','y','u','i','o','p','[',']',13 ,0  ,'a','s',        // 1\r
93         'd','f','g','h','j','k','l',';',39 ,'`',0  ,92 ,'z','x','c','v',        // 2\r
94         'b','n','m',',','.','/',0  ,'*',0  ,' ',0  ,0  ,0  ,0  ,0  ,0  ,        // 3\r
95         0  ,0  ,0  ,0  ,0  ,0  ,0  ,'7','8','9','-','4','5','6','+','1',        // 4\r
96         '2','3','0',127,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 5\r
97         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 6\r
98         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0           // 7\r
99                                         },\r
100                                         far ShiftNames[] =              // Shifted ASCII for scan codes\r
101                                         {\r
102 //       0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F\r
103         0  ,27 ,'!','@','#','$','%','^','&','*','(',')','_','+',8  ,9  ,        // 0\r
104         'Q','W','E','R','T','Y','U','I','O','P','{','}',13 ,0  ,'A','S',        // 1\r
105         'D','F','G','H','J','K','L',':',34 ,'~',0  ,'|','Z','X','C','V',        // 2\r
106         'B','N','M','<','>','?',0  ,'*',0  ,' ',0  ,0  ,0  ,0  ,0  ,0  ,        // 3\r
107         0  ,0  ,0  ,0  ,0  ,0  ,0  ,'7','8','9','-','4','5','6','+','1',        // 4\r
108         '2','3','0',127,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 5\r
109         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 6\r
110         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0           // 7\r
111                                         },\r
112                                         far SpecialNames[] =    // ASCII for 0xe0 prefixed codes\r
113                                         {\r
114 //       0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F\r
115         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 0\r
116         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,13 ,0  ,0  ,0  ,        // 1\r
117         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 2\r
118         0  ,0  ,0  ,0  ,0  ,'/',0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 3\r
119         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 4\r
120         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 5\r
121         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,        // 6\r
122         0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0           // 7\r
123                                         },\r
124                                         *ScanNames[] =          // Scan code names with single chars\r
125                                         {\r
126         "?","?","1","2","3","4","5","6","7","8","9","0","-","+","?","?",\r
127         "Q","W","E","R","T","Y","U","I","O","P","[","]","|","?","A","S",\r
128         "D","F","G","H","J","K","L",";","\"","?","?","?","Z","X","C","V",\r
129         "B","N","M",",",".","/","?","?","?","?","?","?","?","?","?","?",\r
130         "?","?","?","?","?","?","?","?","\xf","?","-","\x15","5","\x11","+","?",\r
131         "\x13","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",\r
132         "?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",\r
133         "?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"\r
134                                         },      // DEBUG - consolidate these\r
135                                         far ExtScanCodes[] =    // Scan codes with >1 char names\r
136                                         {\r
137         1,0xe,0xf,0x1d,0x2a,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,\r
138         0x3f,0x40,0x41,0x42,0x43,0x44,0x57,0x59,0x46,0x1c,0x36,\r
139         0x37,0x38,0x47,0x49,0x4f,0x51,0x52,0x53,0x45,0x48,\r
140         0x50,0x4b,0x4d,0x00\r
141                                         },\r
142                                         *ExtScanNames[] =       // Names corresponding to ExtScanCodes\r
143                                         {\r
144         "Esc","BkSp","Tab","Ctrl","LShft","Space","CapsLk","F1","F2","F3","F4",\r
145         "F5","F6","F7","F8","F9","F10","F11","F12","ScrlLk","Enter","RShft",\r
146         "PrtSc","Alt","Home","PgUp","End","PgDn","Ins","Del","NumLk","Up",\r
147         "Down","Left","Right",""\r
148                                         };\r
149 \r
150 static  Direction       DirTable[] =            // Quick lookup for total direction\r
151                                         {\r
152                                                 //dir_Nortinest,\r
153                                                 dir_North,\r
154                                                 dir_West,\r
155                                                 dir_None,\r
156                                                 dir_East,\r
157                                                 dir_South\r
158                                                 //dir_NorthEast,\r
159                                                 //dir_Soutinest,\r
160                                                 //,dir_SouthEast\r
161                                         };\r
162 #ifdef __cplusplus\r
163 }\r
164 #endif\r
165 \r
166 //      Internal routines\r
167 \r
168 ///////////////////////////////////////////////////////////////////////////\r
169 //\r
170 //      INL_KeyService() - Handles a keyboard interrupt (key up/down)\r
171 //\r
172 ///////////////////////////////////////////////////////////////////////////\r
173 void interrupt\r
174 INL_KeyService()\r
175 {\r
176 static  boolean special;\r
177                 byte    k,c;\r
178                 register byte temp;\r
179 \r
180         k = inp(0x60);  // Get the scan code\r
181 \r
182         // Tell the XT keyboard controller to clear the key\r
183         outp(0x61,(temp = inp(0x61)) | 0x80);\r
184         outp(0x61,temp);\r
185 \r
186         if (k == 0xe0)          // Special key prefix\r
187                 special = true;\r
188         else if (k == 0xe1)     // Handle Pause key\r
189                 inpu.Paused = true;\r
190         else\r
191         {\r
192                 if (k & 0x80)   // Break code\r
193                 {\r
194                         k &= 0x7f;\r
195 \r
196 // DEBUG - handle special keys: ctl-alt-delete, print scrn\r
197 \r
198                         inpu.Keyboard[k] = false;\r
199                 }\r
200                 else                    // Make code\r
201                 {\r
202                         inst.LastCode = inst.CurCode;\r
203                         inst.CurCode = inpu.LastScan = k;\r
204                         inpu.Keyboard[k] = true;\r
205 \r
206                         if (special)\r
207                                 c = SpecialNames[k];\r
208                         else\r
209                         {\r
210                                 if (k == sc_CapsLock)\r
211                                 {\r
212                                         inst.CapsLock ^= true;\r
213                                         // DEBUG - make caps lock light work\r
214                                 }\r
215 \r
216                                 if (inpu.Keyboard[sc_LShift] || inpu.Keyboard[sc_RShift])       // If shifted\r
217                                 {\r
218                                         c = ShiftNames[k];\r
219                                         if ((c >= 'A') && (c <= 'Z') && inst.CapsLock)\r
220                                                 c += 'a' - 'A';\r
221                                 }\r
222                                 else\r
223                                 {\r
224                                         c = ASCIINames[k];\r
225                                         if ((c >= 'a') && (c <= 'z') && inst.CapsLock)\r
226                                                 c -= 'a' - 'A';\r
227                                 }\r
228                         }\r
229                         if (c)\r
230                                 inpu.LastASCII = c;\r
231                 }\r
232 \r
233                 special = false;\r
234         }\r
235 \r
236         if (INL_KeyHook && !special)\r
237                 INL_KeyHook();\r
238 #ifdef __DEBUG_InputMgr__\r
239         if(dbg_testkeyin > 0) printf("%c        %u      [0x%x %u]       %u\n", c, c, k, k, inpu.Keyboard[k]);\r
240 #endif\r
241         outp(0x20,0x20);\r
242 }\r
243 \r
244 void\r
245 Mouse(int x)\r
246 {\r
247         //union REGS CPURegs;\r
248         //x = CPURegs.x.ax;\r
249         __asm {\r
250                 mov     ax,x\r
251                 int     MouseInt\r
252         }\r
253         //int86(MouseInt,&CPURegs,&CPURegs);\r
254 }\r
255 \r
256 ///////////////////////////////////////////////////////////////////////////\r
257 //\r
258 //      INL_GetMouseDelta() - Gets the amount that the mouse has moved from the\r
259 //              mouse driver\r
260 //\r
261 ///////////////////////////////////////////////////////////////////////////\r
262 static void\r
263 INL_GetMouseDelta(int *x,int *y)\r
264 {\r
265         union REGS CPURegs;\r
266         Mouse(MDelta);\r
267         *x = CPURegs.x.cx;\r
268         *y = CPURegs.x.dx;\r
269 }\r
270 \r
271 ///////////////////////////////////////////////////////////////////////////\r
272 //\r
273 //      INL_GetMouseButtons() - Gets the status of the mouse buttons from the\r
274 //              mouse driver\r
275 //\r
276 ///////////////////////////////////////////////////////////////////////////\r
277 static word\r
278 INL_GetMouseButtons(void)\r
279 {\r
280         union REGS CPURegs;\r
281         word    buttons;\r
282 \r
283         Mouse(MButtons);\r
284         buttons = CPURegs.x.bx;\r
285         return(buttons);\r
286 }\r
287 \r
288 ///////////////////////////////////////////////////////////////////////////\r
289 //\r
290 //      IN_GetJoyAbs() - Reads the absolute position of the specified joystick\r
291 //\r
292 ///////////////////////////////////////////////////////////////////////////\r
293 void\r
294 IN_GetJoyAbs(word joy,word *xp,word *yp)\r
295 {\r
296         byte    xb,yb,\r
297                         xs,ys;\r
298         word    x,y;\r
299 \r
300         x = y = 0;\r
301         xs = joy? 2 : 0;                // Find shift value for x axis\r
302         xb = 1 << xs;                   // Use shift value to get x bit mask\r
303         ys = joy? 3 : 1;                // Do the same for y axis\r
304         yb = 1 << ys;\r
305 \r
306 // Read the absolute joystick values\r
307         __asm {\r
308                 pushf                           // Save some registers\r
309                 push    si\r
310                 push    di\r
311                 cli                                     // Make sure an interrupt doesn't screw the timings\r
312 \r
313 \r
314                 mov             dx,0x201\r
315                 in              al,dx\r
316                 out             dx,al           // Clear the resistors\r
317 \r
318                 mov             ah,[xb]         // Get masks into registers\r
319                 mov             ch,[yb]\r
320 \r
321                 xor             si,si           // Clear count registers\r
322                 xor             di,di\r
323                 xor             bh,bh           // Clear high byte of bx for later\r
324 \r
325                 push    bp                      // Don't mess up stack frame\r
326                 mov             bp,MaxJoyValue\r
327 #ifdef __BORLANDC__\r
328         }\r
329 #endif\r
330 loo:\r
331 #ifdef __BORLANDC__\r
332         __asm {\r
333 #endif\r
334                 in              al,dx           // Get bits indicating whether all are finished\r
335 \r
336                 dec             bp                      // Check bounding register\r
337                 jz              done            // We have a silly value - abort\r
338 \r
339                 mov             bl,al           // Duplicate the bits\r
340                 and             bl,ah           // Mask off useless bits (in [xb])\r
341                 add             si,bx           // Possibly increment count register\r
342                 mov             cl,bl           // Save for testing later\r
343 \r
344                 mov             bl,al\r
345                 and             bl,ch           // [yb]\r
346                 add             di,bx\r
347 \r
348                 add             cl,bl\r
349                 jnz             loo             // If both bits were 0, drop out\r
350 #ifdef __BORLANDC__\r
351         }\r
352 #endif\r
353 done:\r
354 #ifdef __BORLANDC__\r
355         __asm {\r
356 #endif\r
357                 pop             bp\r
358 \r
359                 mov             cl,[xs]         // Get the number of bits to shift\r
360                 shr             si,cl           //  and shift the count that many times\r
361 \r
362                 mov             cl,[ys]\r
363                 shr             di,cl\r
364 \r
365                 mov             [x],si          // Store the values into the variables\r
366                 mov             [y],di\r
367 \r
368                 pop             di\r
369                 pop             si\r
370                 popf                            // Restore the registers\r
371         }\r
372 \r
373         *xp = x;\r
374         *yp = y;\r
375 }\r
376 \r
377 ///////////////////////////////////////////////////////////////////////////\r
378 //\r
379 //      INL_GetJoyDelta() - Returns the relative movement of the specified\r
380 //              joystick (from +/-127, scaled adaptively)\r
381 //\r
382 ///////////////////////////////////////////////////////////////////////////\r
383 static void\r
384 INL_GetJoyDelta(word joy,int *dx,int *dy,boolean adaptive)\r
385 {\r
386         word            x,y;\r
387         word    time;\r
388         word TimeCount = *clockw;\r
389         JoystickDef     *def;\r
390 static  word    lasttime;\r
391 \r
392         IN_GetJoyAbs(joy,&x,&y);\r
393         def = inpu.JoyDefs + joy;\r
394 \r
395         if (x < def->threshMinX)\r
396         {\r
397                 if (x < def->joyMinX)\r
398                         x = def->joyMinX;\r
399 \r
400                 x = -(x - def->threshMinX);\r
401                 x *= def->joyMultXL;\r
402                 x >>= JoyScaleShift;\r
403                 *dx = (x > 127)? -127 : -x;\r
404         }\r
405         else if (x > def->threshMaxX)\r
406         {\r
407                 if (x > def->joyMaxX)\r
408                         x = def->joyMaxX;\r
409 \r
410                 x = x - def->threshMaxX;\r
411                 x *= def->joyMultXH;\r
412                 x >>= JoyScaleShift;\r
413                 *dx = (x > 127)? 127 : x;\r
414         }\r
415         else\r
416                 *dx = 0;\r
417 \r
418         if (y < def->threshMinY)\r
419         {\r
420                 if (y < def->joyMinY)\r
421                         y = def->joyMinY;\r
422 \r
423                 y = -(y - def->threshMinY);\r
424                 y *= def->joyMultYL;\r
425                 y >>= JoyScaleShift;\r
426                 *dy = (y > 127)? -127 : -y;\r
427         }\r
428         else if (y > def->threshMaxY)\r
429         {\r
430                 if (y > def->joyMaxY)\r
431                         y = def->joyMaxY;\r
432 \r
433                 y = y - def->threshMaxY;\r
434                 y *= def->joyMultYH;\r
435                 y >>= JoyScaleShift;\r
436                 *dy = (y > 127)? 127 : y;\r
437         }\r
438         else\r
439                 *dy = 0;\r
440 \r
441         if (adaptive)\r
442         {\r
443                 time = (TimeCount - lasttime) / 2;\r
444                 if (time)\r
445                 {\r
446                         if (time > 8)\r
447                                 time = 8;\r
448                         *dx *= time;\r
449                         *dy *= time;\r
450                 }\r
451         }\r
452         lasttime = TimeCount;\r
453 }\r
454 \r
455 ///////////////////////////////////////////////////////////////////////////\r
456 //\r
457 //      INL_GetJoyButtons() - Returns the button status of the specified\r
458 //              joystick\r
459 //\r
460 ///////////////////////////////////////////////////////////////////////////\r
461 static word\r
462 INL_GetJoyButtons(word joy)\r
463 {\r
464 register        word    result;\r
465 \r
466         result = inp(0x201);    // Get all the joystick buttons\r
467         result >>= joy? 6 : 4;  // Shift into bits 0-1\r
468         result &= 3;                            // Mask off the useless bits\r
469         result ^= 3;\r
470         return(result);\r
471 }\r
472 \r
473 ///////////////////////////////////////////////////////////////////////////\r
474 //\r
475 //      IN_GetJoyButtonsDB() - Returns the de-bounced button status of the\r
476 //              specified joystick\r
477 //\r
478 ///////////////////////////////////////////////////////////////////////////\r
479 word\r
480 IN_GetJoyButtonsDB(word joy)\r
481 {\r
482         word TimeCount = *clockw;\r
483         word    lasttime;\r
484         word            result1,result2;\r
485 \r
486         do\r
487         {\r
488                 result1 = INL_GetJoyButtons(joy);\r
489                 lasttime = TimeCount;\r
490                 while(TimeCount == lasttime)\r
491                 result2 = INL_GetJoyButtons(joy);\r
492         } while(result1 != result2);\r
493         return(result1);\r
494 }\r
495 \r
496 ///////////////////////////////////////////////////////////////////////////\r
497 //\r
498 //      INL_StartKbd() - Sets up my keyboard stuff for use\r
499 //\r
500 ///////////////////////////////////////////////////////////////////////////\r
501 static void\r
502 INL_StartKbd()\r
503 {\r
504         byte far *lock_key;\r
505         INL_KeyHook = 0;        // Clear key hook\r
506 \r
507         IN_ClearKeysDown();\r
508 \r
509         OldKeyVect = _dos_getvect(KeyInt);\r
510 \r
511         // turn off num-lock via BIOS\r
512         lock_key = MK_FP(0x040, 0x017); // Pointing to the address of the bios shift state keys\r
513         *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
514         OldKeyVect();   // call BIOS keyhandler to change keyboard lights\r
515         _dos_setvect(KeyInt,INL_KeyService);\r
516 }\r
517 \r
518 ///////////////////////////////////////////////////////////////////////////\r
519 //\r
520 //      INL_ShutKbd() - Restores keyboard control to the BIOS\r
521 //\r
522 ///////////////////////////////////////////////////////////////////////////\r
523 static void\r
524 INL_ShutKbd(void)\r
525 {\r
526         pokeb(0x40,0x17,peekb(0x40,0x17) & 0xfaf0);     // Clear ctrl/alt/shift flags\r
527 \r
528         _dos_setvect(KeyInt,OldKeyVect);\r
529 }\r
530 \r
531 ///////////////////////////////////////////////////////////////////////////\r
532 //\r
533 //      INL_StartMouse() - Detects and sets up the mouse\r
534 //\r
535 ///////////////////////////////////////////////////////////////////////////\r
536 static boolean\r
537 INL_StartMouse(void)\r
538 {\r
539         union REGS CPURegs;\r
540         if(_dos_getvect(MouseInt))\r
541         {\r
542                 Mouse(MReset);\r
543                 if(CPURegs.x.ax == 0xffff)\r
544                         return(true);\r
545         }\r
546         return(false);\r
547 }\r
548 \r
549 ///////////////////////////////////////////////////////////////////////////\r
550 //\r
551 //      INL_ShutMouse() - Cleans up after the mouse\r
552 //\r
553 ///////////////////////////////////////////////////////////////////////////\r
554 static void\r
555 INL_ShutMouse(void)\r
556 {\r
557 }\r
558 \r
559 //\r
560 //      INL_SetJoyScale() - Sets up scaling values for the specified joystick\r
561 //\r
562 static void\r
563 INL_SetJoyScale(word joy)\r
564 {\r
565         JoystickDef     *def;\r
566 \r
567         def = &(inpu.JoyDefs[joy]);\r
568         def->joyMultXL = JoyScaleMax / (def->threshMinX - def->joyMinX);\r
569         def->joyMultXH = JoyScaleMax / (def->joyMaxX - def->threshMaxX);\r
570         def->joyMultYL = JoyScaleMax / (def->threshMinY - def->joyMinY);\r
571         def->joyMultYH = JoyScaleMax / (def->joyMaxY - def->threshMaxY);\r
572 }\r
573 \r
574 ///////////////////////////////////////////////////////////////////////////\r
575 //\r
576 //      IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale()\r
577 //              to set up scaling values\r
578 //\r
579 ///////////////////////////////////////////////////////////////////////////\r
580 void\r
581 IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy)\r
582 {\r
583         word            d,r;\r
584         JoystickDef     *def;\r
585 \r
586         def = &(inpu.JoyDefs[joy]);\r
587 \r
588         def->joyMinX = minx;\r
589         def->joyMaxX = maxx;\r
590         r = maxx - minx;\r
591         d = r / 3;\r
592         def->threshMinX = ((r / 2) - d) + minx;\r
593         def->threshMaxX = ((r / 2) + d) + minx;\r
594 \r
595         def->joyMinY = miny;\r
596         def->joyMaxY = maxy;\r
597         r = maxy - miny;\r
598         d = r / 3;\r
599         def->threshMinY = ((r / 2) - d) + miny;\r
600         def->threshMaxY = ((r / 2) + d) + miny;\r
601 \r
602         INL_SetJoyScale(joy);\r
603 }\r
604 \r
605 ///////////////////////////////////////////////////////////////////////////\r
606 //\r
607 //      INL_StartJoy() - Detects & auto-configures the specified joystick\r
608 //                                      The auto-config assumes the joystick is centered\r
609 //\r
610 ///////////////////////////////////////////////////////////////////////////\r
611 static boolean\r
612 INL_StartJoy(word joy)\r
613 {\r
614         word            x,y;\r
615 \r
616         IN_GetJoyAbs(joy,&x,&y);\r
617 \r
618         if\r
619         (\r
620                 ((x == 0) || (x > MaxJoyValue - 10))\r
621         ||      ((y == 0) || (y > MaxJoyValue - 10))\r
622         )\r
623                 return(false);\r
624         else\r
625         {\r
626                 IN_SetupJoy(joy,0,x * 2,0,y * 2);\r
627                 return(true);\r
628         }\r
629 }\r
630 \r
631 ///////////////////////////////////////////////////////////////////////////\r
632 //\r
633 //      INL_ShutJoy() - Cleans up the joystick stuff\r
634 //\r
635 ///////////////////////////////////////////////////////////////////////////\r
636 static void\r
637 INL_ShutJoy(word joy)\r
638 {\r
639         inpu.JoysPresent[joy] = false;\r
640 }\r
641 \r
642 //      Public routines\r
643 \r
644 ///////////////////////////////////////////////////////////////////////////\r
645 //\r
646 //      IN_Startup() - Starts up the Input Mgr\r
647 //\r
648 ///////////////////////////////////////////////////////////////////////////\r
649 void\r
650 IN_Startup()\r
651 {\r
652         boolean checkjoys,checkmouse;\r
653         word    i;\r
654 \r
655         if (inst.IN_Started)\r
656                 return;\r
657 \r
658         checkjoys = true;\r
659         checkmouse = true;\r
660         for (i = 1;i < _argc;i++)\r
661         {\r
662                 switch (US_CheckParm(_argv[i],ParmStringsIN))\r
663                 {\r
664                 case 0:\r
665                         checkjoys = false;\r
666                         break;\r
667                 case 1:\r
668                         checkmouse = false;\r
669                         break;\r
670                 }\r
671         }\r
672 \r
673         INL_StartKbd();\r
674         inpu.MousePresent = checkmouse? INL_StartMouse() : false;\r
675 \r
676         for (i = 0;i < MaxJoys;i++)\r
677                 inpu.JoysPresent[i] = checkjoys? INL_StartJoy(i) : false;\r
678 \r
679         inst.IN_Started = true;\r
680 \r
681 }\r
682 \r
683 ///////////////////////////////////////////////////////////////////////////\r
684 //\r
685 //      IN_Default() - Sets up default conditions for the Input Mgr\r
686 //\r
687 ///////////////////////////////////////////////////////////////////////////\r
688 void\r
689 IN_Default(boolean gotit,player_t *player,ControlType nt)\r
690 {\r
691         int i;\r
692         if\r
693         (\r
694                 (!gotit)\r
695         ||      ((nt == ctrl_Joystick1) && !inpu.JoysPresent[0])\r
696         ||      ((nt == ctrl_Joystick2) && !inpu.JoysPresent[1])\r
697         ||      ((nt == ctrl_Mouse) && !inpu.MousePresent)\r
698         )\r
699                 nt = ctrl_Keyboard1;\r
700         inpu.KbdDefs[0].button0 = 0x1c;\r
701         inpu.KbdDefs[0].button1 = 0x38;\r
702         //in.KbdDefs[0].upleft = 0x47;\r
703         inpu.KbdDefs[0].up = 0x48;\r
704         //in.KbdDefs[0].upright = 0x49;\r
705         inpu.KbdDefs[0].left = 0x4b;\r
706         inpu.KbdDefs[0].right = 0x4d;\r
707         //in.KbdDefs[0].downleft = 0x4f;\r
708         inpu.KbdDefs[0].down = 0x50;\r
709         //in.KbdDefs[0].downright = 0x51;\r
710         IN_SetControlType(0,player,nt);\r
711         for(i=0; i>MaxPlayers;i++)\r
712                 player[i].enti.d =2;\r
713 }\r
714 \r
715 ///////////////////////////////////////////////////////////////////////////\r
716 //\r
717 //      IN_Shutdown() - Shuts down the Input Mgr\r
718 //\r
719 ///////////////////////////////////////////////////////////////////////////\r
720 void\r
721 IN_Shutdown()\r
722 {\r
723         word    i;\r
724 \r
725         if (!inst.IN_Started)\r
726                 return;\r
727 \r
728         INL_ShutMouse();\r
729         for (i = 0;i < MaxJoys;i++)\r
730                 INL_ShutJoy(i);\r
731         INL_ShutKbd();\r
732 \r
733         inst.IN_Started = false;\r
734 }\r
735 \r
736 ///////////////////////////////////////////////////////////////////////////\r
737 //\r
738 //      IN_SetKeyHook() - Sets the routine that gets called by INL_KeyService()\r
739 //                      everytime a real make/break code gets hit\r
740 //\r
741 ///////////////////////////////////////////////////////////////////////////\r
742 void\r
743 IN_SetKeyHook(void (*hook)())\r
744 {\r
745         INL_KeyHook = hook;\r
746 }\r
747 \r
748 ///////////////////////////////////////////////////////////////////////////\r
749 //\r
750 //      IN_ClearKeyDown() - Clears the keyboard array\r
751 //\r
752 ///////////////////////////////////////////////////////////////////////////\r
753 void\r
754 IN_ClearKeysDown()\r
755 {\r
756         //int   i;\r
757 \r
758         inpu.LastScan = sc_None;\r
759         inpu.LastASCII = key_None;\r
760         memset (inpu.Keyboard,0,sizeof(inpu.Keyboard));\r
761 }\r
762 \r
763 ///////////////////////////////////////////////////////////////////////////\r
764 //\r
765 //      INL_AdjustCursor() - Internal routine of common code from IN_ReadCursor()\r
766 //\r
767 ///////////////////////////////////////////////////////////////////////////\r
768 static void\r
769 INL_AdjustCursor(CursorInfo *info,word buttons,int dx,int dy)\r
770 {\r
771         if (buttons & (1 << 0))\r
772                 info->button0 = true;\r
773         if (buttons & (1 << 1))\r
774                 info->button1 = true;\r
775 \r
776         info->x += dx;\r
777         info->y += dy;\r
778 }\r
779 \r
780 ///////////////////////////////////////////////////////////////////////////\r
781 //\r
782 //      IN_ReadCursor() - Reads the input devices and fills in the cursor info\r
783 //              struct\r
784 //\r
785 ///////////////////////////////////////////////////////////////////////////\r
786 void\r
787 IN_ReadCursor(CursorInfo *info)\r
788 {\r
789         word    i,\r
790                         buttons;\r
791         int             dx,dy;\r
792 \r
793         info->x = info->y = 0;\r
794         info->button0 = info->button1 = false;\r
795 \r
796         if (inpu.MousePresent)\r
797         {\r
798                 buttons = INL_GetMouseButtons();\r
799                 INL_GetMouseDelta(&dx,&dy);\r
800                 INL_AdjustCursor(info,buttons,dx,dy);\r
801         }\r
802 \r
803         for (i = 0;i < MaxJoys;i++)\r
804         {\r
805                 if (!inpu.JoysPresent[i])\r
806                         continue;\r
807 \r
808                 buttons = INL_GetJoyButtons(i);\r
809                 INL_GetJoyDelta(i,&dx,&dy,true);\r
810                 dx /= 64;\r
811                 dy /= 64;\r
812                 INL_AdjustCursor(info,buttons,dx,dy);\r
813         }\r
814 }\r
815 \r
816 ///////////////////////////////////////////////////////////////////////////\r
817 //\r
818 //      IN_ReadControl() - Reads the device associated with the specified\r
819 //              player and fills in the control info struct\r
820 //\r
821 ///////////////////////////////////////////////////////////////////////////\r
822 void near\r
823 IN_ReadControl(word pn, player_t *player)\r
824 {\r
825                         boolean         realdelta;\r
826 #if DEMO0\r
827                         byte            dbyte;\r
828 #endif\r
829                         word            buttons;\r
830                         int                     dx,dy;\r
831                         Motion          mx,my;\r
832                         ControlType     type;\r
833                         sword conpee;\r
834                         byte dir=DirTable[2];\r
835 register        KeyboardDef     *def;\r
836 \r
837         dx = dy = 0;\r
838         mx = my = motion_None;\r
839         buttons = 0;\r
840 \r
841 #if DEMO0\r
842         if (DemoMode == demo_Playback)\r
843         {\r
844                 dbyte = DemoBuffer[DemoOffset + 1];\r
845                 my = (dbyte & 3) - 1;\r
846                 mx = ((dbyte >> 2) & 3) - 1;\r
847                 buttons = (dbyte >> 4) & 3;\r
848 \r
849                 if (!(--DemoBuffer[DemoOffset]))\r
850                 {\r
851                         DemoOffset += 2;\r
852                         if (DemoOffset >= DemoSize)\r
853                                 DemoMode = demo_PlayDone;\r
854                 }\r
855 \r
856                 realdelta = false;\r
857         }\r
858         else if (DemoMode == demo_PlayDone)\r
859                 Quit ("Demo playback exceeded");\r
860         else\r
861         {\r
862 #endif\r
863                 switch (type = player[pn].Controls)\r
864                 {\r
865                 case ctrl_Keyboard1:\r
866                 case ctrl_Keyboard2:\r
867                         def = &(inpu.KbdDefs[type - ctrl_Keyboard]);\r
868 \r
869 /*                      if (Keyboard[def->upleft])\r
870                                 mx = motion_Left,my = motion_Up;\r
871                         else if (Keyboard[def->upright])\r
872                                 mx = motion_Right,my = motion_Up;\r
873                         else if (Keyboard[def->downleft])\r
874                                 mx = motion_Left,my = motion_Down;\r
875                         else if (Keyboard[def->downright])\r
876                                 mx = motion_Right,my = motion_Down;*/\r
877 //TODO: make this into a function that the joystick AND keyboard can use wwww\r
878                         if(DIRECTIONIFELSE)//(player[pn].info.dir == 2)\r
879                         {\r
880                         if(!inpu.Keyboard[def->left] && !inpu.Keyboard[def->right]){\r
881                                 if((inpu.Keyboard[def->up] && !inpu.Keyboard[def->down]))\r
882                                         my = motion_Up;\r
883                                 if((inpu.Keyboard[def->down] && !inpu.Keyboard[def->up]))\r
884                                         my = motion_Down;\r
885                         }else if(!inpu.Keyboard[def->up] && !inpu.Keyboard[def->down]){\r
886                                 if((inpu.Keyboard[def->left] && !inpu.Keyboard[def->right]))\r
887                                         mx = motion_Left;\r
888                                 if((inpu.Keyboard[def->right] && !inpu.Keyboard[def->left]))\r
889                                         mx = motion_Right;\r
890                         }else{  //2 keys pressed\r
891                                         switch (player[pn].pdir)\r
892                                         {\r
893                                                 case 0:\r
894                                                 case 4:\r
895                                                         if((inpu.Keyboard[def->left] && !inpu.Keyboard[def->right])){ dir = DirTable[1]; }//mx = motion_Left; }\r
896                                                         else if((inpu.Keyboard[def->right] && !inpu.Keyboard[def->left])){ dir = DirTable[3]; }//mx = motion_Right; }\r
897                                                 break;\r
898                                                 case 1:\r
899                                                 case 3:\r
900                                                         if((inpu.Keyboard[def->up] && !inpu.Keyboard[def->down])){ dir = DirTable[0]; }//my = motion_Up; }\r
901                                                         else if((inpu.Keyboard[def->down] && !inpu.Keyboard[def->up])){ dir = DirTable[4]; }//my = motion_Down; }\r
902                                                 break;\r
903                                                 default:\r
904                                                 break;\r
905                                         }\r
906 #ifdef __DEBUG_InputMgr__\r
907                                         //if(dbg_testcontrolnoisy > 0){ printf("dir=%c ", dirchar(dir)); printf("pdir=%c        ", dirchar(player[pn].pdir)); }\r
908 #endif\r
909                                 }\r
910                         }\r
911                         //input from player\r
912                         if (inpu.Keyboard[def->button0])\r
913                                 buttons += 1 << 0;\r
914                         if (inpu.Keyboard[def->button1])\r
915                                 buttons += 1 << 1;\r
916                         realdelta = false;\r
917                         break;\r
918                 case ctrl_Joystick1:\r
919                 case ctrl_Joystick2:\r
920                         INL_GetJoyDelta(type - ctrl_Joystick,&dx,&dy,false);\r
921                         buttons = INL_GetJoyButtons(type - ctrl_Joystick);\r
922                         realdelta = true;\r
923                         break;\r
924                 case ctrl_Mouse:\r
925                         INL_GetMouseDelta(&dx,&dy);\r
926                         buttons = INL_GetMouseButtons();\r
927                         realdelta = true;\r
928                         break;\r
929                 }\r
930 #ifdef DEMO0\r
931         }\r
932 #endif\r
933 \r
934         if (realdelta)\r
935         {\r
936                 mx = (dx < 0)? motion_Left : ((dx > 0)? motion_Right : motion_None);\r
937                 my = (dy < 0)? motion_Up : ((dy > 0)? motion_Down : motion_None);\r
938         }\r
939         else\r
940         {\r
941                 dx = mx;// * 127;\r
942                 dy = my;// * 127;\r
943         }\r
944 \r
945         player[pn].info.x = dx;\r
946         player[pn].info.xaxis = mx;\r
947         player[pn].info.y = dy;\r
948         player[pn].info.yaxis = my;\r
949         player[pn].info.button0 = buttons & (1 << 0);\r
950         player[pn].info.button1 = buttons & (1 << 1);\r
951         player[pn].info.button2 = buttons & (1 << 2);\r
952         player[pn].info.button3 = buttons & (1 << 3);\r
953 //      player[pn].info.dir = DirTable[((my + 1) * 3) + (mx + 1)];\r
954         conpee=(((my + 1) * 2) + (mx + 1))-1;\r
955         player[pn].info.dir = DirTable[conpee];\r
956 \r
957         if(DirTable[conpee]!=2) player[pn].pdir=DirTable[conpee];\r
958         if(player[pn].enti.q==1 &&( dir!=2 || (mx!=motion_None || my!=motion_None)))\r
959         {\r
960                 if(dir==2) player[pn].enti.d = player[pn].info.dir;\r
961                 else player[pn].enti.d = DirTable[dir];\r
962         }\r
963 \r
964 #if DEMO0\r
965         if (DemoMode == demo_Record)\r
966         {\r
967                 // Pack the control info into a byte\r
968                 dbyte = (buttons << 4) | ((mx + 1) << 2) | (my + 1);\r
969 \r
970                 if\r
971                 (\r
972                         (DemoBuffer[DemoOffset + 1] == dbyte)\r
973                 &&      (DemoBuffer[DemoOffset] < 255)\r
974                 )\r
975                         (DemoBuffer[DemoOffset])++;\r
976                 else\r
977                 {\r
978                         if (DemoOffset || DemoBuffer[DemoOffset])\r
979                                 DemoOffset += 2;\r
980 \r
981                         if (DemoOffset >= DemoSize)\r
982                                 Quit ("Demo buffer overflow");\r
983 \r
984                         DemoBuffer[DemoOffset] = 1;\r
985                         DemoBuffer[DemoOffset + 1] = dbyte;\r
986                 }\r
987         }\r
988 #endif\r
989 #ifdef __DEBUG_InputMgr__\r
990 if(dbg_testcontrolnoisy > 0)\r
991 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
992 {\r
993         //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
994         //printf("q=%d ", player[pn].enti.q);\r
995         //printf("cpee=%c ", dirchar(conpee));\r
996         printf("pdir=%c d=%c dir=%c ", dirchar(player[pn].pdir), dirchar(player[pn].enti.d), dirchar(player[pn].info.dir));\r
997         /*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
998         //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
999         printf("\n");\r
1000 }\r
1001 #endif\r
1002 }\r
1003 \r
1004 ///////////////////////////////////////////////////////////////////////////\r
1005 //\r
1006 //      IN_SetControlType() - Sets the control type to be used by the specified\r
1007 //              player\r
1008 //\r
1009 ///////////////////////////////////////////////////////////////////////////\r
1010 void\r
1011 IN_SetControlType(word pn,player_t *player,ControlType type)\r
1012 {\r
1013         // DEBUG - check that requested type is present?\r
1014         player[pn].Controls = type;\r
1015 }\r
1016 \r
1017 #if DEMO0\r
1018 ///////////////////////////////////////////////////////////////////////////\r
1019 //\r
1020 //      IN_StartDemoRecord() - Starts the demo recording, using a buffer the\r
1021 //              size passed. Returns if the buffer allocation was successful\r
1022 //\r
1023 ///////////////////////////////////////////////////////////////////////////\r
1024 boolean\r
1025 IN_StartDemoRecord(word bufsize)\r
1026 {\r
1027         if (!bufsize)\r
1028                 return(false);\r
1029 \r
1030         MM_GetPtr((memptr *)&DemoBuffer,bufsize);\r
1031         DemoMode = demo_Record;\r
1032         DemoSize = bufsize & ~1;\r
1033         DemoOffset = 0;\r
1034         DemoBuffer[0] = DemoBuffer[1] = 0;\r
1035 \r
1036         return(true);\r
1037 }\r
1038 \r
1039 ///////////////////////////////////////////////////////////////////////////\r
1040 //\r
1041 //      IN_StartDemoPlayback() - Plays back the demo pointed to of the given size\r
1042 //\r
1043 ///////////////////////////////////////////////////////////////////////////\r
1044 void\r
1045 IN_StartDemoPlayback(byte /*__segment*/ *buffer,word bufsize)\r
1046 {\r
1047         DemoBuffer = buffer;\r
1048         DemoMode = demo_Playback;\r
1049         DemoSize = bufsize & ~1;\r
1050         DemoOffset = 0;\r
1051 }\r
1052 \r
1053 ///////////////////////////////////////////////////////////////////////////\r
1054 //\r
1055 //      IN_StopDemo() - Turns off demo mode\r
1056 //\r
1057 ///////////////////////////////////////////////////////////////////////////\r
1058 void\r
1059 IN_StopDemo(void)\r
1060 {\r
1061         if ((DemoMode == demo_Record) && DemoOffset)\r
1062                 DemoOffset += 2;\r
1063 \r
1064         DemoMode = demo_Off;\r
1065 }\r
1066 \r
1067 ///////////////////////////////////////////////////////////////////////////\r
1068 //\r
1069 //      IN_FreeDemoBuffer() - Frees the demo buffer, if it's been allocated\r
1070 //\r
1071 ///////////////////////////////////////////////////////////////////////////\r
1072 void\r
1073 IN_FreeDemoBuffer(void)\r
1074 {\r
1075         if (DemoBuffer)\r
1076                 MM_FreePtr((memptr *)&DemoBuffer);\r
1077 }\r
1078 #endif\r
1079 \r
1080 \r
1081 ///////////////////////////////////////////////////////////////////////////\r
1082 //\r
1083 //      IN_GetScanName() - Returns a string containing the name of the\r
1084 //              specified scan code\r
1085 //\r
1086 ///////////////////////////////////////////////////////////////////////////\r
1087 byte *\r
1088 IN_GetScanName(ScanCode scan)\r
1089 {\r
1090         byte            **p;\r
1091         ScanCode        far *s;\r
1092 \r
1093         for (s = ExtScanCodes,p = ExtScanNames;*s;p++,s++)\r
1094                 if (*s == scan)\r
1095                         return(*p);\r
1096 \r
1097         return(ScanNames[scan]);\r
1098 }\r
1099 \r
1100 ///////////////////////////////////////////////////////////////////////////\r
1101 //\r
1102 //      IN_WaitForKey() - Waits for a scan code, then clears LastScan and\r
1103 //              returns the scan code\r
1104 //\r
1105 ///////////////////////////////////////////////////////////////////////////\r
1106 ScanCode\r
1107 IN_WaitForKey()\r
1108 {\r
1109         ScanCode        result;\r
1110 \r
1111         while (!(result = inpu.LastScan))\r
1112                 ;\r
1113         inpu.LastScan = 0;\r
1114         return(result);\r
1115 }\r
1116 \r
1117 ///////////////////////////////////////////////////////////////////////////\r
1118 //\r
1119 //      IN_WaitForASCII() - Waits for an ASCII char, then clears LastASCII and\r
1120 //              returns the ASCII value\r
1121 //\r
1122 ///////////////////////////////////////////////////////////////////////////\r
1123 char\r
1124 IN_WaitForASCII()\r
1125 {\r
1126         char            result;\r
1127 \r
1128         while (!(result = inpu.LastASCII))\r
1129                 ;\r
1130         inpu.LastASCII = '\0';\r
1131         return(result);\r
1132 }\r
1133 \r
1134 ///////////////////////////////////////////////////////////////////////////\r
1135 //\r
1136 //      IN_AckBack() - Waits for either an ASCII keypress or a button press\r
1137 //\r
1138 ///////////////////////////////////////////////////////////////////////////\r
1139 void\r
1140 IN_AckBack()\r
1141 {\r
1142         word    i;\r
1143 \r
1144         while (!inpu.LastScan)\r
1145         {\r
1146                 if (inpu.MousePresent)\r
1147                 {\r
1148                         if (INL_GetMouseButtons())\r
1149                         {\r
1150                                 while (INL_GetMouseButtons())\r
1151                                         ;\r
1152                                 return;\r
1153                         }\r
1154                 }\r
1155 \r
1156                 for (i = 0;i < MaxJoys;i++)\r
1157                 {\r
1158                         if (inpu.JoysPresent[i])\r
1159                         {\r
1160                                 if (IN_GetJoyButtonsDB(i))\r
1161                                 {\r
1162                                         while (IN_GetJoyButtonsDB(i))\r
1163                                                 ;\r
1164                                         return;\r
1165                                 }\r
1166                         }\r
1167                 }\r
1168         }\r
1169 \r
1170         IN_ClearKey(inpu.LastScan);\r
1171         inpu.LastScan = sc_None;\r
1172 }\r
1173 \r
1174 ///////////////////////////////////////////////////////////////////////////\r
1175 //\r
1176 //      IN_Ack() - Clears user input & then calls IN_AckBack()\r
1177 //\r
1178 ///////////////////////////////////////////////////////////////////////////\r
1179 void\r
1180 IN_Ack()\r
1181 {\r
1182         word    i;\r
1183 \r
1184         if (!inst.IN_Started)\r
1185                 return;\r
1186 \r
1187         IN_ClearKey(inpu.LastScan);\r
1188         inpu.LastScan = sc_None;\r
1189 \r
1190         if (inpu.MousePresent)\r
1191                 while (INL_GetMouseButtons())\r
1192                                         ;\r
1193         for (i = 0;i < MaxJoys;i++)\r
1194                 if (inpu.JoysPresent[i])\r
1195                         while (IN_GetJoyButtonsDB(i))\r
1196                                 ;\r
1197 \r
1198         IN_AckBack();\r
1199 }\r
1200 \r
1201 ///////////////////////////////////////////////////////////////////////////\r
1202 //\r
1203 //      IN_IsUserInput() - Returns true if a key has been pressed or a button\r
1204 //              is down\r
1205 //\r
1206 ///////////////////////////////////////////////////////////////////////////\r
1207 boolean\r
1208 IN_IsUserInput()\r
1209 {\r
1210         boolean result;\r
1211         word    i;\r
1212 \r
1213         result = inpu.LastScan;\r
1214 \r
1215         if (inpu.MousePresent)\r
1216                 if (INL_GetMouseButtons())\r
1217                         result = true;\r
1218 \r
1219         for (i = 0;i < MaxJoys;i++)\r
1220                 if (inpu.JoysPresent[i])\r
1221                         if (INL_GetJoyButtons(i))\r
1222                                 result = true;\r
1223 \r
1224         return(result);\r
1225 }\r
1226 \r
1227 ///////////////////////////////////////////////////////////////////////////\r
1228 //\r
1229 //      IN_UserInput() - Waits for the specified delay time (in ticks) or the\r
1230 //              user pressing a key or a mouse button. If the clear flag is set, it\r
1231 //              then either clears the key or waits for the user to let the mouse\r
1232 //              button up.\r
1233 //\r
1234 ///////////////////////////////////////////////////////////////////////////\r
1235 boolean\r
1236 IN_UserInput(dword delay,boolean clear)\r
1237 {\r
1238         word TimeCount = *clockw;\r
1239         word    lasttime;\r
1240 \r
1241         lasttime = TimeCount;\r
1242         do\r
1243         {\r
1244                 if (IN_IsUserInput())\r
1245                 {\r
1246                         if (clear)\r
1247                                 IN_AckBack();\r
1248                         return(true);\r
1249                 }\r
1250         } while (TimeCount - lasttime < delay);\r
1251         return(false);\r
1252 }\r
1253 \r
1254 //===========================================================================\r
1255 \r
1256 /*\r
1257 ===================\r
1258 =\r
1259 = IN_MouseButtons\r
1260 =\r
1261 ===================\r
1262 */\r
1263 \r
1264 byte    IN_MouseButtons (void)\r
1265 {\r
1266         union REGS CPURegs;\r
1267         if (inpu.MousePresent)\r
1268         {\r
1269                 Mouse(MButtons);\r
1270                 return CPURegs.x.bx;\r
1271         }\r
1272         else\r
1273                 return 0;\r
1274 }\r
1275 \r
1276 \r
1277 /*\r
1278 ===================\r
1279 =\r
1280 = IN_JoyButtons\r
1281 =\r
1282 ===================\r
1283 */\r
1284 \r
1285 byte    IN_JoyButtons (void)\r
1286 {\r
1287         byte joybits;\r
1288 \r
1289         joybits = inp(0x201);   // Get all the joystick buttons\r
1290         joybits >>= 4;                          // only the high bits are useful\r
1291         joybits ^= 15;                          // return with 1=pressed\r
1292 \r
1293         return joybits;\r
1294 }\r
1295 \r
1296 boolean IN_KeyDown(byte code)\r
1297 {\r
1298 #ifdef __DEBUG_InputMgr__\r
1299         if(!dbg_nointest)\r
1300 #endif\r
1301         return inpu.Keyboard[code];\r
1302 #ifdef __DEBUG_InputMgr__\r
1303         else\r
1304                 if(dbg_nointest && kbhit())\r
1305                         return 1;\r
1306                 else\r
1307                         return 0;\r
1308 #endif\r
1309 }\r
1310 \r
1311 void IN_ClearKey(byte code)\r
1312 {\r
1313         inpu.Keyboard[code] = false;\r
1314         if(code == inpu.LastScan)\r
1315                 inpu.LastScan = sc_None;\r
1316         }\r
1317 \r
1318 boolean IN_qb(byte kee)\r
1319 {\r
1320 #ifdef __DEBUG_InputMgr__\r
1321         if(dbg_testkeyin) printf("%u\n", inpu.Keyboard[kee]);\r
1322 #endif\r
1323         if(inpu.Keyboard[kee]==true) return 1;\r
1324         else return 0;\r
1325 }\r
1326 \r
1327 ScanCode IN_GetLastScan()\r
1328 {\r
1329         return inpu.LastScan;\r
1330 }\r
1331 \r
1332 ScanCode IN_GetCurCode()\r
1333 {\r
1334         return inst.CurCode;\r
1335 }\r