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