added copyright headers to new files
[bootcensus] / src / keyb.c
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018  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 this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include "keyb.h"
19 #include "intr.h"
20 #include "asmops.h"
21
22 #define KB_IRQ  1
23 #define KB_PORT         0x60
24
25 /* table with rough translations from set 1 scancodes to ASCII-ish */
26 static int scantbl[] = {
27         0, KB_ESC, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',            /* 0 - e */
28         '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',                 /* f - 1c */
29         KB_LCTRL, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',                          /* 1d - 29 */
30         KB_LSHIFT, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', KB_RSHIFT,                   /* 2a - 36 */
31         KB_NUM_MUL, KB_LALT, ' ', KB_CAPSLK, KB_F1, KB_F2, KB_F3, KB_F4, KB_F5, KB_F6, KB_F7, KB_F8, KB_F9, KB_F10,                     /* 37 - 44 */
32         KB_NUMLK, KB_SCRLK, KB_NUM_7, KB_NUM_8, KB_NUM_9, KB_NUM_MINUS, KB_NUM_4, KB_NUM_5, KB_NUM_6, KB_NUM_PLUS,      /* 45 - 4e */
33         KB_NUM_1, KB_NUM_2, KB_NUM_3, KB_NUM_0, KB_NUM_DOT, KB_SYSRQ, 0, 0, KB_F11, KB_F12,                                             /* 4d - 58 */
34         0, 0, 0, 0, 0, 0, 0,                                                                                                                    /* 59 - 5f */
35         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                                                                 /* 60 - 6f */
36         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0                                                                  /* 70 - 7f */
37 };
38
39 static void kbintr();
40
41 #define BUFSZ   64
42 #define ADVANCE(x)      ((x) = ((x) + 1) & (BUFSZ - 1))
43
44 static int buffer[BUFSZ];
45 static int buf_ridx, buf_widx;
46
47 static unsigned int num_pressed;
48 static unsigned char keystate[256];
49
50 void kb_init(void)
51 {
52         interrupt(IRQ_TO_INTR(KB_IRQ), kbintr);
53 }
54
55 int kb_isdown(int key)
56 {
57         switch(key) {
58         case KB_ANY:
59                 return num_pressed;
60
61         case KB_ALT:
62                 return keystate[KB_LALT] + keystate[KB_RALT];
63
64         case KB_CTRL:
65                 return keystate[KB_LCTRL] + keystate[KB_RCTRL];
66         }
67         return keystate[key];
68 }
69
70 void kb_wait(void)
71 {
72         int key;
73         while((key = kb_getkey()) == -1) {
74                 /* put the processor to sleep while waiting for keypresses, but first
75                  * make sure interrupts are enabled, or we'll sleep forever
76                  */
77                 enable_intr();
78                 halt_cpu();
79         }
80         kb_putback(key);
81 }
82
83 int kb_getkey(void)
84 {
85         int res;
86
87         if(buf_ridx == buf_widx) {
88                 return -1;
89         }
90         res = buffer[buf_ridx];
91         ADVANCE(buf_ridx);
92         return res;
93 }
94
95 void kb_putback(int key)
96 {
97         /* go back a place */
98         if(--buf_ridx < 0) {
99                 buf_ridx += BUFSZ;
100         }
101
102         /* if the write end hasn't caught up with us, go back one place
103          * and put it there, otherwise just overwrite the oldest key which
104          * is right where we were.
105          */
106         if(buf_ridx == buf_widx) {
107                 ADVANCE(buf_ridx);
108         }
109
110         buffer[buf_ridx] = key;
111 }
112
113 static void kbintr()
114 {
115         unsigned char code;
116         int key, press;
117
118         code = inb(KB_PORT);
119
120         if(code >= 128) {
121                 press = 0;
122                 code -= 128;
123
124                 if(num_pressed > 0) {
125                         num_pressed--;
126                 }
127         } else {
128                 press = 1;
129
130                 num_pressed++;
131         }
132
133         key = scantbl[code];
134
135         if(press) {
136                 /* append to buffer */
137                 buffer[buf_widx] = key;
138                 ADVANCE(buf_widx);
139                 /* if the write end overtook the read end, advance the read end
140                  * too, to discard the oldest keypress from the buffer
141                  */
142                 if(buf_widx == buf_ridx) {
143                         ADVANCE(buf_ridx);
144                 }
145         }
146
147         /* and update keystate table */
148         keystate[key] = press;
149 }