rayzor

annotate src/keyb.c @ 14:a9a948809c6f

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