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