rev |
line source |
nuclear@7
|
1 /*
|
nuclear@7
|
2 DOS interrupt-based keyboard driver.
|
nuclear@7
|
3 Copyright (C) 2013 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@7
|
4
|
nuclear@7
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@7
|
6 it under the terms of the GNU General Public License as published by
|
nuclear@7
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@7
|
8 (at your option) any later version.
|
nuclear@7
|
9
|
nuclear@7
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@7
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@7
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@7
|
13 GNU General Public License for more details.
|
nuclear@7
|
14
|
nuclear@7
|
15 You should have received a copy of the GNU General Public License
|
nuclear@7
|
16 along with the program. If not, see <http://www.gnu.org/licenses/>
|
nuclear@7
|
17 */
|
nuclear@7
|
18 #define KEYB_C_
|
nuclear@7
|
19
|
nuclear@7
|
20 #include <stdio.h>
|
nuclear@7
|
21 #include <stdlib.h>
|
nuclear@7
|
22 #include <string.h>
|
nuclear@7
|
23 #include <conio.h>
|
nuclear@7
|
24 #include <dos.h>
|
nuclear@7
|
25 #include <i86.h>
|
nuclear@7
|
26 #include "keyb.h"
|
nuclear@7
|
27 #include "scancode.h"
|
nuclear@7
|
28
|
nuclear@7
|
29 #define KB_INTR 0x9
|
nuclear@7
|
30 #define KB_PORT 0x60
|
nuclear@7
|
31
|
nuclear@7
|
32 #define PIC1_CMD_PORT 0x20
|
nuclear@7
|
33 #define OCW2_EOI (1 << 5)
|
nuclear@7
|
34
|
nuclear@7
|
35 #define DONE_INIT (prev_handler)
|
nuclear@7
|
36
|
nuclear@7
|
37 static void __interrupt __far kbintr();
|
nuclear@7
|
38
|
nuclear@7
|
39 static void (__interrupt __far *prev_handler)();
|
nuclear@7
|
40
|
nuclear@7
|
41 static int *buffer;
|
nuclear@7
|
42 static int buffer_size, buf_ridx, buf_widx;
|
nuclear@7
|
43 static int last_key;
|
nuclear@7
|
44
|
nuclear@7
|
45 static unsigned int num_pressed;
|
nuclear@7
|
46 static unsigned char keystate[256];
|
nuclear@7
|
47
|
nuclear@7
|
48 #define ADVANCE(x) ((x) = ((x) + 1) % buffer_size)
|
nuclear@7
|
49
|
nuclear@7
|
50 int kb_init(int bufsz)
|
nuclear@7
|
51 {
|
nuclear@7
|
52 if(DONE_INIT) {
|
nuclear@7
|
53 fprintf(stderr, "keyboard driver already initialized!\n");
|
nuclear@7
|
54 return 0;
|
nuclear@7
|
55 }
|
nuclear@7
|
56
|
nuclear@7
|
57 buffer_size = bufsz;
|
nuclear@7
|
58 if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) {
|
nuclear@7
|
59 fprintf(stderr, "failed to allocate input buffer, continuing without\n");
|
nuclear@7
|
60 buffer_size = 0;
|
nuclear@7
|
61 }
|
nuclear@7
|
62 buf_ridx = buf_widx = 0;
|
nuclear@7
|
63 last_key = -1;
|
nuclear@7
|
64
|
nuclear@7
|
65 memset(keystate, 0, sizeof keystate);
|
nuclear@7
|
66 num_pressed = 0;
|
nuclear@7
|
67
|
nuclear@7
|
68 /* set our interrupt handler */
|
nuclear@7
|
69 _disable();
|
nuclear@7
|
70 prev_handler = _dos_getvect(KB_INTR);
|
nuclear@7
|
71 _dos_setvect(KB_INTR, kbintr);
|
nuclear@7
|
72 _enable();
|
nuclear@7
|
73
|
nuclear@7
|
74 return 0;
|
nuclear@7
|
75 }
|
nuclear@7
|
76
|
nuclear@7
|
77 void kb_shutdown(void)
|
nuclear@7
|
78 {
|
nuclear@7
|
79 if(!DONE_INIT) {
|
nuclear@7
|
80 return;
|
nuclear@7
|
81 }
|
nuclear@7
|
82
|
nuclear@7
|
83 /* restore the original interrupt handler */
|
nuclear@7
|
84 _disable();
|
nuclear@7
|
85 _dos_setvect(KB_INTR, prev_handler);
|
nuclear@7
|
86 _enable();
|
nuclear@7
|
87
|
nuclear@7
|
88 free(buffer);
|
nuclear@7
|
89 }
|
nuclear@7
|
90
|
nuclear@7
|
91 int kb_isdown(int key)
|
nuclear@7
|
92 {
|
nuclear@7
|
93 switch(key) {
|
nuclear@7
|
94 case KB_ANY:
|
nuclear@7
|
95 return num_pressed;
|
nuclear@7
|
96
|
nuclear@7
|
97 case KB_ALT:
|
nuclear@7
|
98 return keystate[KB_LALT] + keystate[KB_RALT];
|
nuclear@7
|
99
|
nuclear@7
|
100 case KB_CTRL:
|
nuclear@7
|
101 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
|
nuclear@7
|
102 }
|
nuclear@7
|
103 return keystate[key];
|
nuclear@7
|
104 }
|
nuclear@7
|
105
|
nuclear@7
|
106 void kb_wait(void)
|
nuclear@7
|
107 {
|
nuclear@7
|
108 int key;
|
nuclear@7
|
109 while((key = kb_getkey()) == -1) {
|
nuclear@7
|
110 /* put the processor to sleep while waiting for keypresses, but first
|
nuclear@7
|
111 * make sure interrupts are enabled, or we'll sleep forever
|
nuclear@7
|
112 */
|
nuclear@7
|
113 __asm {
|
nuclear@7
|
114 sti
|
nuclear@7
|
115 hlt
|
nuclear@7
|
116 }
|
nuclear@7
|
117 }
|
nuclear@7
|
118 kb_putback(key);
|
nuclear@7
|
119 }
|
nuclear@7
|
120
|
nuclear@7
|
121 int kb_getkey(void)
|
nuclear@7
|
122 {
|
nuclear@7
|
123 int res;
|
nuclear@7
|
124
|
nuclear@7
|
125 if(buffer) {
|
nuclear@7
|
126 if(buf_ridx == buf_widx) {
|
nuclear@7
|
127 return -1;
|
nuclear@7
|
128 }
|
nuclear@7
|
129 res = buffer[buf_ridx];
|
nuclear@7
|
130 ADVANCE(buf_ridx);
|
nuclear@7
|
131 } else {
|
nuclear@7
|
132 res = last_key;
|
nuclear@7
|
133 last_key = -1;
|
nuclear@7
|
134 }
|
nuclear@7
|
135 return res;
|
nuclear@7
|
136 }
|
nuclear@7
|
137
|
nuclear@7
|
138 void kb_putback(int key)
|
nuclear@7
|
139 {
|
nuclear@7
|
140 if(buffer) {
|
nuclear@7
|
141 /* go back a place */
|
nuclear@7
|
142 if(--buf_ridx < 0) {
|
nuclear@7
|
143 buf_ridx += buffer_size;
|
nuclear@7
|
144 }
|
nuclear@7
|
145
|
nuclear@7
|
146 /* if the write end hasn't caught up with us, go back one place
|
nuclear@7
|
147 * and put it there, otherwise just overwrite the oldest key which
|
nuclear@7
|
148 * is right where we were.
|
nuclear@7
|
149 */
|
nuclear@7
|
150 if(buf_ridx == buf_widx) {
|
nuclear@7
|
151 ADVANCE(buf_ridx);
|
nuclear@7
|
152 }
|
nuclear@7
|
153
|
nuclear@7
|
154 buffer[buf_ridx] = key;
|
nuclear@7
|
155 } else {
|
nuclear@7
|
156 last_key = key;
|
nuclear@7
|
157 }
|
nuclear@7
|
158 }
|
nuclear@7
|
159
|
nuclear@7
|
160 static void __interrupt __far kbintr()
|
nuclear@7
|
161 {
|
nuclear@7
|
162 unsigned char code;
|
nuclear@7
|
163 int key, press;
|
nuclear@7
|
164
|
nuclear@7
|
165 code = inp(KB_PORT);
|
nuclear@7
|
166
|
nuclear@7
|
167 if(code >= 128) {
|
nuclear@7
|
168 press = 0;
|
nuclear@7
|
169 code -= 128;
|
nuclear@7
|
170
|
nuclear@7
|
171 if(num_pressed > 0) {
|
nuclear@7
|
172 num_pressed--;
|
nuclear@7
|
173 }
|
nuclear@7
|
174 } else {
|
nuclear@7
|
175 press = 1;
|
nuclear@7
|
176
|
nuclear@7
|
177 num_pressed++;
|
nuclear@7
|
178 }
|
nuclear@7
|
179
|
nuclear@7
|
180 key = scantbl[code];
|
nuclear@7
|
181
|
nuclear@7
|
182 if(press) {
|
nuclear@7
|
183 /* append to buffer */
|
nuclear@7
|
184 last_key = key;
|
nuclear@7
|
185 if(buffer_size > 0) {
|
nuclear@7
|
186 buffer[buf_widx] = key;
|
nuclear@7
|
187 ADVANCE(buf_widx);
|
nuclear@7
|
188 /* if the write end overtook the read end, advance the read end
|
nuclear@7
|
189 * too, to discard the oldest keypress from the buffer
|
nuclear@7
|
190 */
|
nuclear@7
|
191 if(buf_widx == buf_ridx) {
|
nuclear@7
|
192 ADVANCE(buf_ridx);
|
nuclear@7
|
193 }
|
nuclear@7
|
194 }
|
nuclear@7
|
195 }
|
nuclear@7
|
196
|
nuclear@7
|
197 /* and update keystate table */
|
nuclear@7
|
198 keystate[key] = press;
|
nuclear@7
|
199
|
nuclear@7
|
200 outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */
|
nuclear@7
|
201 }
|