backported build fixes and warnings cleanup from dos
[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                 /* put the processor to sleep while waiting for keypresses, but first
165                  * make sure interrupts are enabled, or we'll sleep forever
166                  */
167                 halt();
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
216         code = inp(KB_PORT);
217
218         if(code >= 128) {
219                 press = 0;
220                 code -= 128;
221
222                 if(num_pressed > 0) {
223                         num_pressed--;
224                 }
225         } else {
226                 press = 1;
227
228                 num_pressed++;
229         }
230
231         key = scantbl[code];
232         c = (keystate[KB_LSHIFT] | keystate[KB_RSHIFT]) ? scantbl_shift[code] : key;
233
234         if(press) {
235                 /* append to buffer */
236                 last_key = c;
237                 if(buffer_size > 0) {
238                         buffer[buf_widx] = c;
239                         ADVANCE(buf_widx);
240                         /* if the write end overtook the read end, advance the read end
241                          * too, to discard the oldest keypress from the buffer
242                          */
243                         if(buf_widx == buf_ridx) {
244                                 ADVANCE(buf_ridx);
245                         }
246                 }
247         }
248
249         /* and update keystate table */
250         keystate[key] = press;
251
252         outp(PIC1_CMD_PORT, OCW2_EOI);  /* send end-of-interrupt */
253 }