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