]> 4ch.mooo.com Git - 16.git/blob - src/lib/dos_comm.c
new file: 16.bat
[16.git] / src / lib / dos_comm.c
1 /* Thanks to Alex Russell for example code */
2 /* Thanks to Gary Neal for example code */\r
3 #include "src\lib\dos_comm.h"\r
4 \r
5 // Q code
6 byte kee;\r
7 byte keer[128]; /* key pressed */
8 byte keep[128]; /* key released */
9
10 #ifdef __cplusplus              /* Functions must be declared C style */\r
11 extern "C" {\r
12 #endif\r
13 extern void interrupt (far *oldkb)(void) = NULL; /* BIOS keyboard handler */
14 #ifdef __cplusplus\r
15 }\r
16 #endif\r
17 /*****************NEW KEYBOARD 09h ISR***********************/\r
18 void interrupt newkb(void){\r
19         register char qx;\r
20
21         kee = inp(0x60);        /* Read the keyboard scan code */
22 \r
23         /* Clear keyboard controller on XT machines */\r
24         qx = inp(0x61);\r
25         qx |= 0x82;\r
26         outp(0x61, qx);\r
27         qx &= 0x7F;\r
28         outp(0x61, qx);
29
30         /* Interpret the scan code and set our flags */\r
31         if(kee & 0x80)\r
32                 keep[kee & 0x7F] = 0;\r
33         else\r
34                 keep[kee] = keer[kee] = 1;
35 \r
36         outp(0x20, 0x20);\r
37 }\r
38 \r
39 /* ---------------------- init_keyboard() ---------------- April 17,1993 */\r
40 /* restore the bios keyboard handler */\r
41 /* ---------------------- deinit_keyboard() -------------- April 17,1993 */\r
42 void setkb(int vq){\r
43         if(!vq){ // deinitiation
44                 /* Abort if our function pointer has no valid address */\r
45                 if(oldkb == NULL) return;
46                 /* Set address in our function pointer in interrupt vector table */
47                 _dos_setvect(9, oldkb);
48                 /* Reset our function pointer to contain no valid address */\r
49                 oldkb = NULL;
50         }else if(vq == 1){ // initiation
51                 int i;  /* Index variable */
52                 byte far *lock_key;
53
54                 /* Abort if our function pointer has a valid address. */\r
55                 if(oldkb != NULL) return;\r
56
57                 /* Clear the keyboard buttons state arrays */\r
58                 for(i = 0; i < 128; i++)\r
59                         keep[i] = keer[i] = 0;
60 \r
61                 /* save old BIOS key board handler */\r
62                 oldkb = _dos_getvect(9);\r
63 \r
64                 // turn off num-lock via BIOS\r
65                 lock_key = MK_FP(0x040, 0x017); //wtf is going on here?\r
66                 *lock_key&=(~(16 | 32 | 64));   // toggle off the lock keys\r
67                 oldkb();        // call BIOS keyhandler to change keyboard lights\r
68
69                 /* setup our own handler */\r
70                 _dos_setvect(9, newkb);\r
71         }\r
72 }\r
73 \r
74 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\\r
75  * keyp                                                              *\r
76  *                                                                         *\r
77  * Returns the status of the key requested.                                *\r
78  * The status is 1 if the key is pressed or has been pressed since the     *\r
79  * last call to this function for that particular key.                     *\r
80 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */\r
81 char keyp(byte c){\r
82         register char retVal;\r
83 \r
84         /* Key value in range of keyboard keys available */\r
85         c &= 0x7F;\r
86
87         /* Get the status of the key requested */\r
88         retVal = keep[c] | keer[c];\r
89 \r
90         /* Reset the was pressed status for the requested key */\r
91         keer[c] = 0;
92
93         /* Return the requested key's state */\r
94         return retVal;\r
95 }\r
96
97 // tesuto\r
98 byte scankey(){\r
99         //if(keyp(kee)) printf("%c %03d %03x\n", kee, kee, kee);\r
100         return kee;\r
101 }