removed clang-format and clang_complete files from the repo
[dosdemo] / src / dos / keyb.c
1 /*
2 DOS interrupt-based keyboard driver.
3 Copyright (C) 2013  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 "inttypes.h"
39 #include "dosutil.h"
40
41 #define KB_INTR         0x9
42 #define KB_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 static int *buffer;
64 static int buffer_size, buf_ridx, buf_widx;
65 static int last_key;
66
67 static unsigned int num_pressed;
68 static unsigned char keystate[256];
69
70 #define ADVANCE(x)      ((x) = ((x) + 1) % buffer_size)
71
72 int kb_init(int bufsz)
73 {
74         if(DONE_INIT) {
75                 fprintf(stderr, "keyboard driver already initialized!\n");
76                 return 0;
77         }
78
79         buffer_size = bufsz;
80         if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) {
81                 fprintf(stderr, "failed to allocate input buffer, continuing without\n");
82                 buffer_size = 0;
83         }
84         buf_ridx = buf_widx = 0;
85         last_key = -1;
86
87         memset(keystate, 0, sizeof keystate);
88         num_pressed = 0;
89
90         /* set our interrupt handler */
91         _disable();
92 #ifdef __WATCOMC__
93         prev_handler = _dos_getvect(KB_INTR);
94         _dos_setvect(KB_INTR, kbintr);
95 #endif
96 #ifdef __DJGPP__
97         _go32_dpmi_get_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
98         intr.pm_offset = (intptr_t)kbintr;
99         intr.pm_selector = _go32_my_cs();
100         _go32_dpmi_allocate_iret_wrapper(&intr);
101         _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &intr);
102 #endif
103         _enable();
104
105         return 0;
106 }
107
108 void kb_shutdown(void)
109 {
110         if(!DONE_INIT) {
111                 return;
112         }
113
114         /* restore the original interrupt handler */
115         _disable();
116 #ifdef __WATCOMC__
117         _dos_setvect(KB_INTR, prev_handler);
118 #endif
119 #ifdef __DJGPP__
120         _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
121         _go32_dpmi_free_iret_wrapper(&intr);
122 #endif
123         _enable();
124
125         free(buffer);
126 }
127
128 int kb_isdown(int key)
129 {
130         switch(key) {
131         case KB_ANY:
132                 return num_pressed;
133
134         case KB_ALT:
135                 return keystate[KB_LALT] + keystate[KB_RALT];
136
137         case KB_CTRL:
138                 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
139         }
140
141         if(isalpha(key)) {
142                 key = tolower(key);
143         }
144         return keystate[key];
145 }
146
147 #ifdef __WATCOMC__
148 void halt(void);
149 #pragma aux halt = \
150         "sti" \
151         "hlt";
152 #endif
153
154 #ifdef __DJGPP__
155 #define halt() asm volatile("sti\n\thlt\n\t")
156 #endif
157
158 void kb_wait(void)
159 {
160         int key;
161         while((key = kb_getkey()) == -1) {
162 #ifdef USE_HLT
163                 /* put the processor to sleep while waiting for keypresses, but first
164                  * make sure interrupts are enabled, or we'll sleep forever
165                  */
166                 halt();
167 #endif
168         }
169         kb_putback(key);
170 }
171
172 int kb_getkey(void)
173 {
174         int res;
175
176         if(buffer) {
177                 if(buf_ridx == buf_widx) {
178                         return -1;
179                 }
180                 res = buffer[buf_ridx];
181                 ADVANCE(buf_ridx);
182         } else {
183                 res = last_key;
184                 last_key = -1;
185         }
186         return res;
187 }
188
189 void kb_putback(int key)
190 {
191         if(buffer) {
192                 /* go back a place */
193                 if(--buf_ridx < 0) {
194                         buf_ridx += buffer_size;
195                 }
196
197                 /* if the write end hasn't caught up with us, go back one place
198                  * and put it there, otherwise just overwrite the oldest key which
199                  * is right where we were.
200                  */
201                 if(buf_ridx == buf_widx) {
202                         ADVANCE(buf_ridx);
203                 }
204
205                 buffer[buf_ridx] = key;
206         } else {
207                 last_key = key;
208         }
209 }
210
211 static void INTERRUPT kbintr()
212 {
213         unsigned char code;
214         int key, c, press;
215         static int ext;
216
217         code = inp(KB_PORT);
218
219         if(code == 0xe0) {
220                 ext = 1;
221                 goto eoi;
222         }
223
224         if(code & 0x80) {
225                 press = 0;
226                 code &= 0x7f;
227
228                 if(num_pressed > 0) {
229                         num_pressed--;
230                 }
231         } else {
232                 press = 1;
233
234                 num_pressed++;
235         }
236
237         if(ext) {
238                 key = scantbl_ext[code];
239                 c = key;
240                 ext = 0;
241         } else {
242                 key = scantbl[code];
243                 c = (keystate[KB_LSHIFT] | keystate[KB_RSHIFT]) ? scantbl_shift[code] : key;
244         }
245
246         if(press) {
247                 /* append to buffer */
248                 last_key = c;
249                 if(buffer_size > 0) {
250                         buffer[buf_widx] = c;
251                         ADVANCE(buf_widx);
252                         /* if the write end overtook the read end, advance the read end
253                          * too, to discard the oldest keypress from the buffer
254                          */
255                         if(buf_widx == buf_ridx) {
256                                 ADVANCE(buf_ridx);
257                         }
258                 }
259         }
260
261         /* and update keystate table */
262         keystate[key] = press;
263
264 eoi:
265         outp(PIC1_CMD_PORT, OCW2_EOI);  /* send end-of-interrupt */
266 }