-
Notifications
You must be signed in to change notification settings - Fork 1
/
i_ibm.c
1502 lines (1233 loc) · 29.3 KB
/
i_ibm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (C) 1993-1996 Id Software, Inc.
// Copyright (C) 2023 Frenkel Smeijers
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// I_IBM.C
#include <conio.h>
#include <dos.h>
#include <stdarg.h>
#include "doomdef.h"
#include "r_local.h"
#define DPMI_INT 0x31
//#define NOTIMER
void I_StartupSound (void);
void I_ShutdownSound (void);
void I_ShutdownTimer (void);
static void DPMIInt (int32_t i);
static void I_ReadMouse (void);
static void I_InitDiskFlash (void);
extern int32_t usemouse, usejoystick;
/*
=============================================================================
CONSTANTS
=============================================================================
*/
#define SC_INDEX 0x3c4
#define SC_RESET 0
#define SC_CLOCK 1
#define SC_MAPMASK 2
#define SC_CHARMAP 3
#define SC_MEMMODE 4
#define CRTC_INDEX 0x3d4
#define CRTC_H_TOTAL 0
#define CRTC_H_DISPEND 1
#define CRTC_H_BLANK 2
#define CRTC_H_ENDBLANK 3
#define CRTC_H_RETRACE 4
#define CRTC_H_ENDRETRACE 5
#define CRTC_V_TOTAL 6
#define CRTC_OVERFLOW 7
#define CRTC_ROWSCAN 8
#define CRTC_MAXSCANLINE 9
#define CRTC_CURSORSTART 10
#define CRTC_CURSOREND 11
#define CRTC_STARTHIGH 12
#define CRTC_STARTLOW 13
#define CRTC_CURSORHIGH 14
#define CRTC_CURSORLOW 15
#define CRTC_V_RETRACE 16
#define CRTC_V_ENDRETRACE 17
#define CRTC_V_DISPEND 18
#define CRTC_OFFSET 19
#define CRTC_UNDERLINE 20
#define CRTC_V_BLANK 21
#define CRTC_V_ENDBLANK 22
#define CRTC_MODE 23
#define CRTC_LINECOMPARE 24
#define GC_INDEX 0x3ce
#define GC_SETRESET 0
#define GC_ENABLESETRESET 1
#define GC_COLORCOMPARE 2
#define GC_DATAROTATE 3
#define GC_READMAP 4
#define GC_MODE 5
#define GC_MISCELLANEOUS 6
#define GC_COLORDONTCARE 7
#define GC_BITMASK 8
#define STATUS_REGISTER_1 0x3da
#define PEL_WRITE_ADR 0x3c8
#define PEL_DATA 0x3c9
static boolean grmode;
//==================================================
//
// joystick vars
//
//==================================================
static boolean joystickpresent;
static uint32_t joystickx, joysticky;
static boolean I_ReadJoystick (void) // returns false if not connected
{
//TODO implement joystick support
return false;
}
//==================================================
#define KEYBOARDINT 9
static boolean mousepresent;
//===============================
static volatile int32_t ticcount;
// REGS stuff used for int calls
static union REGS regs;
static boolean novideo; // if true, stay in text mode for debugging
#define KBDQUESIZE 32
static byte keyboardque[KBDQUESIZE];
static int32_t kbdtail, kbdhead;
#define KEY_LSHIFT 0xfe
#define KEY_INS (0x80+0x52)
#define KEY_DEL (0x80+0x53)
#define KEY_PGUP (0x80+0x49)
#define KEY_PGDN (0x80+0x51)
#define KEY_HOME (0x80+0x47)
#define KEY_END (0x80+0x4f)
#define SC_RSHIFT 0x36
#define SC_LSHIFT 0x2a
byte scantokey[128] =
{
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
0 , 27, '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', KEY_BACKSPACE, 9, // 0
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
'o', 'p', '[', ']', 13 , KEY_RCTRL,'a', 's', // 1
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
39 , '`', KEY_LSHIFT,92, 'z', 'x', 'c', 'v', // 2
'b', 'n', 'm', ',', '.', '/', KEY_RSHIFT,'*',
KEY_RALT,' ', 0 , KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, // 3
KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10,0 , 0 , KEY_HOME,
KEY_UPARROW,KEY_PGUP,'-',KEY_LEFTARROW,'5',KEY_RIGHTARROW,'+',KEY_END, //4
KEY_DOWNARROW,KEY_PGDN,KEY_INS,KEY_DEL,0,0, 0, KEY_F11,
KEY_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
};
//==========================================================================
/*
===================
=
= I_BaseTiccmd
=
===================
*/
typedef struct
{
int32_t f_0; // interrupt
ticcmd_t f_4; // cmd
} doomcontrol_t;
static doomcontrol_t *doomcon;
static ticcmd_t emptycmd;
ticcmd_t *I_BaseTiccmd (void)
{
if (!doomcon)
return &emptycmd;
DPMIInt (doomcon->f_0);
return &doomcon->f_4;
}
/*
===================
=
= I_GetTime
=
= Returns time in 1/35th second tics.
=
===================
*/
#if defined NOTIMER
#include <time.h>
static uint32_t basetime;
void I_InitBaseTime(void)
{
basetime = clock();
}
int32_t I_GetTime (void)
{
uint32_t ticks = clock();
ticks -= basetime;
return (ticks * TICRATE) / CLOCKS_PER_SEC;
}
#else
int32_t I_GetTime (void)
{
return ticcount;
}
#endif
/*
===================
=
= I_WaitVBL
=
===================
*/
void I_WaitVBL (int32_t vbls)
{
int32_t stat;
if (novideo)
return;
while (vbls--)
{
do
{
stat = inp (STATUS_REGISTER_1);
if (stat&8)
break;
} while(1);
do
{
stat = inp (STATUS_REGISTER_1);
if ((stat&8) == 0)
break;
} while(1);
}
}
/*
===================
=
= I_SetPalette
=
= Palette source must use 8 bit RGB elements.
=
===================
*/
void I_SetPalette (byte *palette)
{
int32_t i;
if (novideo)
return;
I_WaitVBL (1);
outp (PEL_WRITE_ADR, 0);
for (i = 0; i < 768; i++)
outp (PEL_DATA, (gammatable[usegamma][*palette++])>>2);
}
/*
============================================================================
GRAPHICS MODE
============================================================================
*/
static byte *screen, *currentscreen;
byte *destscreen;
byte *destview __attribute__ ((externally_visible));
/*
===================
=
= I_UpdateBox
=
===================
*/
static void I_UpdateBox (int32_t x, int32_t y, int32_t width, int32_t height)
{
int32_t ofs;
byte *source;
int16_t *dest;
int32_t p,x1, x2;
int32_t srcdelta, destdelta;
int32_t wwide;
#ifdef RANGECHECK
if (x < 0 || y < 0 || width <= 0 || height <= 0
|| x + width > SCREENWIDTH || y + height > SCREENHEIGHT)
{
I_Error("Bad I_UpdateBox (%i, %i, %i, %i)", x, y, width, height);
}
#endif
x1 = x>>3;
x2 = (x+width)>>3;
wwide = x2-x1+1;
ofs = y*SCREENWIDTH+(x1<<3);
srcdelta = SCREENWIDTH - (wwide<<3);
destdelta = PLANEWIDTH/2 - wwide;
outp (SC_INDEX, SC_MAPMASK);
for (p = 0 ; p < 4 ; p++)
{
outp (SC_INDEX+1, 1<<p);
source = screens[0] + ofs + p;
dest = (int16_t *)(destscreen + (ofs>>2));
for (y=0 ; y<height ; y++)
{
for (x=wwide ; x ; x--)
{
*dest++ = *source + (source[4]<<8);
source += 8;
}
source+=srcdelta;
dest+=destdelta;
}
}
}
/*
===================
=
= I_UpdateNoBlit
=
===================
*/
void I_UpdateNoBlit(void)
{
static int32_t oldupdatebox[4];
static int32_t voldupdatebox[4];
int32_t updatebox[4];
currentscreen = destscreen;
updatebox[BOXTOP] = (dirtybox[BOXTOP] > voldupdatebox[BOXTOP]) ?
dirtybox[BOXTOP] : voldupdatebox[BOXTOP];
updatebox[BOXRIGHT] = (dirtybox[BOXRIGHT] > voldupdatebox[BOXRIGHT]) ?
dirtybox[BOXRIGHT] : voldupdatebox[BOXRIGHT];
updatebox[BOXBOTTOM] = (dirtybox[BOXBOTTOM] < voldupdatebox[BOXBOTTOM]) ?
dirtybox[BOXBOTTOM] : voldupdatebox[BOXBOTTOM];
updatebox[BOXLEFT] = (dirtybox[BOXLEFT] < voldupdatebox[BOXLEFT]) ?
dirtybox[BOXLEFT] : voldupdatebox[BOXLEFT];
updatebox[BOXTOP] = (updatebox[BOXTOP] > oldupdatebox[BOXTOP]) ?
updatebox[BOXTOP] : oldupdatebox[BOXTOP];
updatebox[BOXRIGHT] = (updatebox[BOXRIGHT] > oldupdatebox[BOXRIGHT]) ?
updatebox[BOXRIGHT] : oldupdatebox[BOXRIGHT];
updatebox[BOXBOTTOM] = (updatebox[BOXBOTTOM] < oldupdatebox[BOXBOTTOM]) ?
updatebox[BOXBOTTOM] : oldupdatebox[BOXBOTTOM];
updatebox[BOXLEFT] = (updatebox[BOXLEFT] < oldupdatebox[BOXLEFT]) ?
updatebox[BOXLEFT] : oldupdatebox[BOXLEFT];
memcpy (voldupdatebox, oldupdatebox, sizeof(oldupdatebox));
memcpy (oldupdatebox, dirtybox, sizeof(dirtybox));
if (updatebox[BOXTOP] >= updatebox[BOXBOTTOM])
I_UpdateBox (updatebox[BOXLEFT], updatebox[BOXBOTTOM],
updatebox[BOXRIGHT] - updatebox[BOXLEFT] + 1,
updatebox[BOXTOP] - updatebox[BOXBOTTOM] + 1);
M_ClearBox (dirtybox);
}
/*
===================
=
= I_FinishUpdate
=
===================
*/
void I_FinishUpdate (void)
{
static int32_t lasttic;
int32_t tics, i;
// draws little dots on the bottom of the screen
if (devparm)
{
tics = I_GetTime() - lasttic;
lasttic = I_GetTime();
if (tics > 20) tics = 20;
outpw (SC_INDEX, SC_MAPMASK | (1 << 8));
for (i=0 ; i<tics ; i++)
destscreen[ (SCREENHEIGHT-1)*PLANEWIDTH + i] = 0xff;
for ( ; i<20 ; i++)
destscreen[ (SCREENHEIGHT-1)*PLANEWIDTH + i] = 0x0;
}
// page flip
outpw (CRTC_INDEX, CRTC_STARTHIGH+((int32_t)destscreen&0xff00));
destscreen += 0x4000;
if ( (int32_t)destscreen == (int32_t)(0xac000 + __djgpp_conventional_base))
destscreen = (byte *)(0xa0000 + __djgpp_conventional_base);
}
/*
===================
=
= I_InitGraphics
=
===================
*/
void I_InitGraphics (void)
{
if (novideo)
return;
grmode = true;
regs.h.ah = 0;
regs.h.al = 0x13;
int386 (0x10, ®s, ®s);
screen = currentscreen = (byte *)(0xa0000 + __djgpp_conventional_base);
destscreen = (byte *)(0xa4000 + __djgpp_conventional_base);
outp (SC_INDEX,SC_MEMMODE);
outp (SC_INDEX+1,(inp(SC_INDEX+1)&~8)|4);
outp (GC_INDEX,GC_MODE);
outp (GC_INDEX+1,inp(GC_INDEX+1)&~0x13);
outp (GC_INDEX,GC_MISCELLANEOUS);
outp (GC_INDEX+1,inp(GC_INDEX+1)&~2);
outpw (SC_INDEX,SC_MAPMASK|(15<<8));
memset (screen, 0, 65536);
outp (CRTC_INDEX,CRTC_UNDERLINE);
outp (CRTC_INDEX+1,inp(CRTC_INDEX+1)&~0x40);
outp (CRTC_INDEX,CRTC_MODE);
outp (CRTC_INDEX+1,inp(CRTC_INDEX+1)|0x40);
outp (GC_INDEX,GC_READMAP);
I_SetPalette (W_CacheLumpName("PLAYPAL", PU_CACHE));
I_InitDiskFlash ();
}
/*
===================
=
= I_ShutdownGraphics
=
===================
*/
static void I_ShutdownGraphics (void)
{
if (*(byte *)(0x449 + __djgpp_conventional_base) == 0x13) // don't reset mode if it didn't get set
{
regs.h.ah = 0;
regs.h.al = 3;
int386 (0x10, ®s, ®s); // back to text mode
}
}
/*
===================
=
= I_ReadScreen
=
= Reads the screen currently displayed into a linear buffer.
=
===================
*/
void I_ReadScreen (byte *scr)
{
int32_t p, i;
outp (GC_INDEX,GC_READMAP);
for (p = 0; p < 4; p++)
{
outp (GC_INDEX+1,p);
for (i = 0; i < SCREENWIDTH*SCREENHEIGHT/4; i++)
scr[i*4+p] = currentscreen[i];
}
}
//===========================================================================
/*
===================
=
= I_StartTic
=
// called by D_DoomLoop
// called before processing each tic in a frame
// can call D_PostEvent
// asyncronous interrupt functions should maintain private ques that are
// read by the syncronous functions to be converted into events
===================
*/
/*
OLD STARTTIC STUFF
void I_StartTic (void)
{
int32_t k;
event_t ev;
I_ReadMouse ();
//
// keyboard events
//
while (kbdtail < kbdhead)
{
k = keyboardque[kbdtail&(KBDQUESIZE-1)];
// if (k==14)
// I_Error ("exited");
kbdtail++;
// extended keyboard shift key bullshit
if ( (k&0x7f)==KEY_RSHIFT )
{
if ( keyboardque[(kbdtail-2)&(KBDQUESIZE-1)]==0xe0 )
continue;
k &= 0x80;
k |= KEY_RSHIFT;
}
if (k==0xe0)
continue; // special / pause keys
if (keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0xe1)
continue; // pause key bullshit
if (k==0xc5 && keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0x9d)
{
ev.type = ev_keydown;
ev.data1 = KEY_PAUSE;
D_PostEvent (&ev);
continue;
}
if (k&0x80)
ev.type = ev_keyup;
else
ev.type = ev_keydown;
k &= 0x7f;
ev.data1 = k;
//ev.data1 = scantokey[k];
D_PostEvent (&ev);
}
}
*/
#define SC_UPARROW 0x48
#define SC_DOWNARROW 0x50
#define SC_LEFTARROW 0x4b
#define SC_RIGHTARROW 0x4d
void I_StartTic (void)
{
int32_t k;
event_t ev;
I_ReadMouse ();
//
// keyboard events
//
while (kbdtail < kbdhead)
{
k = keyboardque[kbdtail&(KBDQUESIZE-1)];
kbdtail++;
// extended keyboard shift key bullshit
if ( (k&0x7f)==SC_LSHIFT || (k&0x7f)==SC_RSHIFT )
{
if ( keyboardque[(kbdtail-2)&(KBDQUESIZE-1)]==0xe0 )
continue;
k &= 0x80;
k |= SC_RSHIFT;
}
if (k==0xe0)
continue; // special / pause keys
if (keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0xe1)
continue; // pause key bullshit
if (k==0xc5 && keyboardque[(kbdtail-2)&(KBDQUESIZE-1)] == 0x9d)
{
ev.type = ev_keydown;
ev.data1 = KEY_PAUSE;
D_PostEvent (&ev);
continue;
}
if (k&0x80)
ev.type = ev_keyup;
else
ev.type = ev_keydown;
k &= 0x7f;
switch (k)
{
case SC_UPARROW:
ev.data1 = KEY_UPARROW;
break;
case SC_DOWNARROW:
ev.data1 = KEY_DOWNARROW;
break;
case SC_LEFTARROW:
ev.data1 = KEY_LEFTARROW;
break;
case SC_RIGHTARROW:
ev.data1 = KEY_RIGHTARROW;
break;
default:
ev.data1 = scantokey[k];
break;
}
D_PostEvent (&ev);
}
}
/*
============================================================================
TIMER INTERRUPT
============================================================================
*/
/*
================
=
= I_TimerISR
=
================
*/
void I_TimerISR (void)
{
ticcount++;
}
/*
============================================================================
KEYBOARD
============================================================================
*/
static byte lastpress;
/*
================
=
= I_KeyboardISR
=
================
*/
#if defined __DMC__
static int I_KeyboardISR (struct INT_DATA *pd)
#else
static void _interrupt I_KeyboardISR (void)
#endif
{
// Get the scan code
keyboardque[kbdhead&(KBDQUESIZE-1)] = lastpress = inp(0x60);
kbdhead++;
// acknowledge the interrupt
outp(0x20,0x20);
#if defined __DMC__
return 1;
#endif
}
/*
===============
=
= I_StartupKeyboard
=
===============
*/
static boolean isKeyboardIsrSet = false;
#if defined __DJGPP__ || defined __CCDL__
static _go32_dpmi_seginfo oldkeyboardisr, newkeyboardisr;
#elif defined __WATCOMC__
static void (_interrupt _far *oldkeyboardisr) (void) = NULL;
#endif
static void I_StartupKeyboard (void)
{
replaceInterrupt(oldkeyboardisr, newkeyboardisr, KEYBOARDINT, I_KeyboardISR);
isKeyboardIsrSet = true;
}
static void I_ShutdownKeyboard (void)
{
if (isKeyboardIsrSet)
{
restoreInterrupt(KEYBOARDINT, oldkeyboardisr, newkeyboardisr);
}
*(int16_t *)(0x41c + __djgpp_conventional_base) = *(int16_t *)(0x41a + __djgpp_conventional_base); // clear bios key buffer
}
/*
============================================================================
MOUSE
============================================================================
*/
static boolean I_ResetMouse (void)
{
regs.h.ah = 0; // reset
regs.h.al = 0;
int386 (0x33, ®s, ®s);
return regs.h.al != 0;
}
/*
================
=
= StartupMouse
=
================
*/
static void I_StartupMouse (void)
{
//
// General mouse detection
//
mousepresent = false;
if ( M_CheckParm ("-nomouse") || !usemouse )
return;
if (I_ResetMouse ())
{
mousepresent = true;
printf("Mouse: detected\n");
} else
printf("Mouse: not present\n");
}
/*
================
=
= ShutdownMouse
=
================
*/
static void I_ShutdownMouse (void)
{
if (!mousepresent)
return;
I_ResetMouse ();
}
/*
================
=
= I_ReadMouse
=
================
*/
static void I_ReadMouse (void)
{
event_t ev;
//
// mouse events
//
if (!mousepresent)
return;
ev.type = ev_mouse;
regs.h.ah = 0;
regs.h.al = 3; // read buttons / position
int386 (0x33, ®s, ®s);
ev.data1 = regs.h.bl;
regs.h.ah = 0;
regs.h.al = 11; // read counters
int386 (0x33, ®s, ®s);
#if defined __CCDL__ || defined __WATCOMC__
ev.data2 = (int16_t)regs.w.cx;
ev.data3 = -(int16_t)regs.w.dx;
#else
ev.data2 = (int16_t)regs.x.cx;
ev.data3 = -(int16_t)regs.x.dx;
#endif
D_PostEvent (&ev);
}
/*
============================================================================
JOYSTICK
============================================================================
*/
static int32_t joyxl, joyxh, joyyl, joyyh;
static boolean WaitJoyButton (void)
{
int32_t oldbuttons, buttons;
oldbuttons = 0;
do
{
I_WaitVBL (1);
buttons = ((inp(0x201) >> 4)&1)^1;
if (buttons != oldbuttons)
{
oldbuttons = buttons;
continue;
}
if ( (lastpress& 0x7f) == 1 )
{
joystickpresent = false;
return false;
}
} while ( !buttons);
do
{
I_WaitVBL (1);
buttons = ((inp(0x201) >> 4)&1)^1;
if (buttons != oldbuttons)
{
oldbuttons = buttons;
continue;
}
if ( (lastpress& 0x7f) == 1 )
{
joystickpresent = false;
return false;
}
} while ( buttons);
return true;
}
/*
===============
=
= I_StartupJoystick
=
===============
*/
static void I_StartupJoystick (void)
{
int32_t centerx, centery;
joystickpresent = 0;
if ( M_CheckParm ("-nojoy") || !usejoystick )
return;
if (!I_ReadJoystick ())
{
joystickpresent = false;
printf ("joystick not found\n");
return;
}
printf ("joystick found\n");
joystickpresent = true;
printf ("CENTER the joystick and press button 1:");
if (!WaitJoyButton ())
return;
I_ReadJoystick ();
centerx = joystickx;
centery = joysticky;
printf ("\nPush the joystick to the UPPER LEFT corner and press button 1:");
if (!WaitJoyButton ())
return;
I_ReadJoystick ();
joyxl = (centerx + joystickx)/2;
joyyl = (centerx + joysticky)/2;
printf ("\nPush the joystick to the LOWER RIGHT corner and press button 1:");
if (!WaitJoyButton ())
return;
I_ReadJoystick ();
joyxh = (centerx + joystickx)/2;
joyyh = (centery + joysticky)/2;
printf ("\n");
}
/*
===============
=
= I_StartFrame
=
===============
*/
void I_StartFrame (void)
{
event_t ev;
//
// joystick events
//
if (!joystickpresent)
return;
I_ReadJoystick ();
ev.type = ev_joystick;
ev.data1 = ((inp(0x201) >> 4)&15)^15;
if (joystickx < joyxl)
ev.data2 = -1;
else if (joystickx > joyxh)
ev.data2 = 1;
else
ev.data2 = 0;
if (joysticky < joyyl)
ev.data3 = -1;
else if (joysticky > joyyh)
ev.data3 = 1;
else
ev.data3 = 0;
D_PostEvent (&ev);
}
/*
============================================================================
DPMI STUFF
============================================================================
*/