stop appending repeat keys to the input buffer
[retroray] / src / dos / keyb.c
1 /*
2 DOS interrupt-based keyboard driver.
3 Copyright (C) 2013-2023  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License  for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with the program. If not, see <http://www.gnu.org/licenses/>
17 */
18 #define KEYB_C_
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <conio.h>
25 #include <dos.h>
26
27 #ifdef __WATCOMC__
28 #include <i86.h>
29 #endif
30 #ifdef __DJGPP__
31 #include <dpmi.h>
32 #include <go32.h>
33 #include <pc.h>
34 #endif
35
36 #include "keyb.h"
37 #include "scancode.h"
38 #include "sizeint.h"
39 #include "dosutil.h"
40
41 #define KEY_INTR                0x9
42 #define KEY_PORT                0x60
43
44 #define PIC1_CMD_PORT   0x20
45 #define OCW2_EOI                (1 << 5)
46
47 #ifdef __WATCOMC__
48 #define INTERRUPT __interrupt __far
49
50 #define DONE_INIT       (prev_handler)
51 static void (INTERRUPT *prev_handler)();
52 #endif
53
54 #ifdef __DJGPP__
55 #define INTERRUPT
56
57 #define DONE_INIT prev_intr.pm_offset
58 static _go32_dpmi_seginfo intr, prev_intr;
59 #endif
60
61 static void INTERRUPT kbintr();
62
63 #define BUFSIZE         64
64 static int buffer[BUFSIZE];
65 static int buf_ridx, buf_widx;
66 static int last_key;
67
68 static unsigned int num_pressed;
69 static unsigned char keystate[512];
70
71 #define ADVANCE(x)      ((x) = ((x) + 1) & (BUFSIZE - 1))
72
73 void kb_init(void)
74 {
75         if(DONE_INIT) {
76                 errormsg("keyboard driver already initialized!\n");
77                 return;
78         }
79
80         buf_ridx = buf_widx = 0;
81         last_key = -1;
82
83         memset(keystate, 0, sizeof keystate);
84         num_pressed = 0;
85
86         /* set our interrupt handler */
87         _disable();
88 #ifdef __WATCOMC__
89         prev_handler = _dos_getvect(KEY_INTR);
90         _dos_setvect(KEY_INTR, kbintr);
91 #endif
92 #ifdef __DJGPP__
93         _go32_dpmi_get_protected_mode_interrupt_vector(KEY_INTR, &prev_intr);
94         intr.pm_offset = (intptr_t)kbintr;
95         intr.pm_selector = _go32_my_cs();
96         _go32_dpmi_allocate_iret_wrapper(&intr);
97         _go32_dpmi_set_protected_mode_interrupt_vector(KEY_INTR, &intr);
98 #endif
99         _enable();
100 }
101
102 void kb_shutdown(void)
103 {
104         if(!DONE_INIT) {
105                 return;
106         }
107
108         /* restore the original interrupt handler */
109         _disable();
110 #ifdef __WATCOMC__
111         _dos_setvect(KEY_INTR, prev_handler);
112 #endif
113 #ifdef __DJGPP__
114         _go32_dpmi_set_protected_mode_interrupt_vector(KEY_INTR, &prev_intr);
115         _go32_dpmi_free_iret_wrapper(&intr);
116 #endif
117         _enable();
118 }
119
120 int kb_isdown(int key)
121 {
122         switch(key) {
123         case KEY_ANY:
124                 return num_pressed;
125
126         case KEY_ALT:
127                 return keystate[KEY_LALT] + keystate[KEY_RALT];
128
129         case KEY_CTRL:
130                 return keystate[KEY_LCTRL] + keystate[KEY_RCTRL];
131
132         case KEY_SHIFT:
133                 return keystate[KEY_LSHIFT] + keystate[KEY_RSHIFT];
134         }
135
136         if(isalpha(key)) {
137                 key = tolower(key);
138         }
139         return keystate[key];
140 }
141
142 #ifdef __WATCOMC__
143 void halt(void);
144 #pragma aux halt = \
145         "sti" \
146         "hlt";
147 #endif
148
149 #ifdef __DJGPP__
150 #define halt() asm volatile("sti\n\thlt\n\t")
151 #endif
152
153 void kb_wait(void)
154 {
155         int key;
156         while((key = kb_getkey()) == -1) {
157 #ifdef USE_HLT
158                 /* put the processor to sleep while waiting for keypresses, but first
159                  * make sure interrupts are enabled, or we'll sleep forever
160                  */
161                 halt();
162 #endif
163         }
164         kb_putback(key);
165 }
166
167 int kb_getkey(void)
168 {
169         int res;
170
171         if(buffer) {
172                 if(buf_ridx == buf_widx) {
173                         return -1;
174                 }
175                 res = buffer[buf_ridx];
176                 ADVANCE(buf_ridx);
177         } else {
178                 res = last_key;
179                 last_key = -1;
180         }
181         return res;
182 }
183
184 void kb_putback(int key)
185 {
186         if(buffer) {
187                 /* go back a place */
188                 if(--buf_ridx < 0) {
189                         buf_ridx += BUFSIZE;
190                 }
191
192                 /* if the write end hasn't caught up with us, go back one place
193                  * and put it there, otherwise just overwrite the oldest key which
194                  * is right where we were.
195                  */
196                 if(buf_ridx == buf_widx) {
197                         ADVANCE(buf_ridx);
198                 }
199
200                 buffer[buf_ridx] = key;
201         } else {
202                 last_key = key;
203         }
204 }
205
206 static void INTERRUPT kbintr()
207 {
208         unsigned char code;
209         int key, c, press;
210         static int ext;
211
212         code = inp(KEY_PORT);
213
214         if(code == 0xe0) {
215                 ext = 1;
216                 goto eoi;
217         }
218
219         if(code & 0x80) {
220                 press = 0;
221                 code &= 0x7f;
222
223                 if(num_pressed > 0) {
224                         num_pressed--;
225                 }
226         } else {
227                 press = 1;
228
229                 num_pressed++;
230         }
231
232         if(ext) {
233                 key = scantbl_ext[code];
234                 c = key;
235                 ext = 0;
236         } else {
237                 key = scantbl[code];
238                 c = (keystate[KEY_LSHIFT] | keystate[KEY_RSHIFT]) ? scantbl_shift[code] : key;
239         }
240
241         if(press && !keystate[key]) {
242                 /* append to buffer */
243                 last_key = c;
244                 buffer[buf_widx] = c;
245                 ADVANCE(buf_widx);
246                 /* if the write end overtook the read end, advance the read end
247                  * too, to discard the oldest keypress from the buffer
248                  */
249                 if(buf_widx == buf_ridx) {
250                         ADVANCE(buf_ridx);
251                 }
252         }
253
254         /* and update keystate table */
255         keystate[key] = press;
256
257 eoi:
258         outp(PIC1_CMD_PORT, OCW2_EOI);  /* send end-of-interrupt */
259 }