98a1f3da20ee0a7cb3c16f63f90fa63a6c256e92
[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 <conio.h>
24 #include <dos.h>
25
26 #ifdef __WATCOMC__
27 #include <i86.h>
28 #endif
29 #ifdef __DJGPP__
30 #include <stdint.h>
31 #include <dpmi.h>
32 #include <go32.h>
33 #include <pc.h>
34 #endif
35
36 #include "keyb.h"
37 #include "scancode.h"
38
39 #define KB_INTR         0x9
40 #define KB_PORT         0x60
41
42 #define PIC1_CMD_PORT   0x20
43 #define OCW2_EOI                (1 << 5)
44
45 #ifdef __WATCOMC__
46 #define INTERRUPT __interrupt __far
47
48 #define DONE_INIT       (prev_handler)
49 static void (INTERRUPT *prev_handler)();
50 #endif
51
52 #ifdef __DJGPP__
53 #define INTERRUPT
54
55 #define DONE_INIT prev_intr.pm_offset
56 static _go32_dpmi_seginfo intr, prev_intr;
57
58 #define outp(p, v)      outportb(p, v)
59 #define inp(p)  inportb(p)
60 #endif
61
62 static void INTERRUPT kbintr();
63
64 static int *buffer;
65 static int buffer_size, buf_ridx, buf_widx;
66 static int last_key;
67
68 static unsigned int num_pressed;
69 static unsigned char keystate[256];
70
71 #define ADVANCE(x)      ((x) = ((x) + 1) % buffer_size)
72
73 int kb_init(int bufsz)
74 {
75         if(DONE_INIT) {
76                 fprintf(stderr, "keyboard driver already initialized!\n");
77                 return 0;
78         }
79
80         buffer_size = bufsz;
81         if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) {
82                 fprintf(stderr, "failed to allocate input buffer, continuing without\n");
83                 buffer_size = 0;
84         }
85         buf_ridx = buf_widx = 0;
86         last_key = -1;
87
88         memset(keystate, 0, sizeof keystate);
89         num_pressed = 0;
90
91         /* set our interrupt handler */
92         _disable();
93 #ifdef __WATCOMC__
94         prev_handler = _dos_getvect(KB_INTR);
95         _dos_setvect(KB_INTR, kbintr);
96 #endif
97 #ifdef __DJGPP__
98         _go32_dpmi_get_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
99         intr.pm_offset = (intptr_t)kbintr;
100         intr.pm_selector = _go32_my_cs();
101         _go32_dpmi_allocate_iret_wrapper(&intr);
102         _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &intr);
103 #endif
104         _enable();
105
106         return 0;
107 }
108
109 void kb_shutdown(void)
110 {
111         if(!DONE_INIT) {
112                 return;
113         }
114
115         /* restore the original interrupt handler */
116         _disable();
117 #ifdef __WATCOMC__
118         _dos_setvect(KB_INTR, prev_handler);
119 #endif
120 #ifdef __DJGPP__
121         _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
122         _go32_dpmi_free_iret_wrapper(&intr);
123 #endif
124         _enable();
125
126         free(buffer);
127 }
128
129 int kb_isdown(int key)
130 {
131         switch(key) {
132         case KB_ANY:
133                 return num_pressed;
134
135         case KB_ALT:
136                 return keystate[KB_LALT] + keystate[KB_RALT];
137
138         case KB_CTRL:
139                 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
140         }
141         return keystate[key];
142 }
143
144 #ifdef __WATCOMC__
145 void halt(void);
146 #pragma aux halt = \
147         "sti" \
148         "hlt";
149 #endif
150
151 #ifdef __DJGPP__
152 #define halt() asm volatile("sti\n\thlt\n\t")
153 #endif
154
155 void kb_wait(void)
156 {
157         int key;
158         while((key = kb_getkey()) == -1) {
159                 /* put the processor to sleep while waiting for keypresses, but first
160                  * make sure interrupts are enabled, or we'll sleep forever
161                  */
162                 halt();
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 += buffer_size;
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, press;
210
211         code = inp(KB_PORT);
212
213         if(code >= 128) {
214                 press = 0;
215                 code -= 128;
216
217                 if(num_pressed > 0) {
218                         num_pressed--;
219                 }
220         } else {
221                 press = 1;
222
223                 num_pressed++;
224         }
225
226         key = scantbl[code];
227
228         if(press) {
229                 /* append to buffer */
230                 last_key = key;
231                 if(buffer_size > 0) {
232                         buffer[buf_widx] = key;
233                         ADVANCE(buf_widx);
234                         /* if the write end overtook the read end, advance the read end
235                          * too, to discard the oldest keypress from the buffer
236                          */
237                         if(buf_widx == buf_ridx) {
238                                 ADVANCE(buf_ridx);
239                         }
240                 }
241         }
242
243         /* and update keystate table */
244         keystate[key] = press;
245
246         outp(PIC1_CMD_PORT, OCW2_EOI);  /* send end-of-interrupt */
247 }