deepstone

annotate src/keyb.c @ 36:e234f2a4b6fa

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