2 DOS interrupt-based keyboard driver.
3 Copyright (C) 2013 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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/>
44 #define PIC1_CMD_PORT 0x20
45 #define OCW2_EOI (1 << 5)
48 #define INTERRUPT __interrupt __far
50 #define DONE_INIT (prev_handler)
51 static void (INTERRUPT *prev_handler)();
57 #define DONE_INIT prev_intr.pm_offset
58 static _go32_dpmi_seginfo intr, prev_intr;
61 static void INTERRUPT kbintr();
64 static int buffer_size, buf_ridx, buf_widx;
67 static unsigned int num_pressed;
68 static unsigned char keystate[256];
70 #define ADVANCE(x) ((x) = ((x) + 1) % buffer_size)
72 int kb_init(int bufsz)
75 fprintf(stderr, "keyboard driver already initialized!\n");
80 if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) {
81 fprintf(stderr, "failed to allocate input buffer, continuing without\n");
84 buf_ridx = buf_widx = 0;
87 memset(keystate, 0, sizeof keystate);
90 /* set our interrupt handler */
93 prev_handler = _dos_getvect(KB_INTR);
94 _dos_setvect(KB_INTR, kbintr);
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);
108 void kb_shutdown(void)
114 /* restore the original interrupt handler */
117 _dos_setvect(KB_INTR, prev_handler);
120 _go32_dpmi_set_protected_mode_interrupt_vector(KB_INTR, &prev_intr);
121 _go32_dpmi_free_iret_wrapper(&intr);
128 int kb_isdown(int key)
135 return keystate[KB_LALT] + keystate[KB_RALT];
138 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
144 return keystate[key];
155 #define halt() asm volatile("sti\n\thlt\n\t")
161 while((key = kb_getkey()) == -1) {
163 /* put the processor to sleep while waiting for keypresses, but first
164 * make sure interrupts are enabled, or we'll sleep forever
177 if(buf_ridx == buf_widx) {
180 res = buffer[buf_ridx];
189 void kb_putback(int key)
192 /* go back a place */
194 buf_ridx += buffer_size;
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.
201 if(buf_ridx == buf_widx) {
205 buffer[buf_ridx] = key;
211 static void INTERRUPT kbintr()
228 if(num_pressed > 0) {
238 key = scantbl_ext[code];
243 c = (keystate[KB_LSHIFT] | keystate[KB_RSHIFT]) ? scantbl_shift[code] : key;
247 /* append to buffer */
249 if(buffer_size > 0) {
250 buffer[buf_widx] = c;
252 /* if the write end overtook the read end, advance the read end
253 * too, to discard the oldest keypress from the buffer
255 if(buf_widx == buf_ridx) {
261 /* and update keystate table */
262 keystate[key] = press;
265 outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */