Set TFS_DIR by default when the TFS_DIR env is not set
[thunix.git] / kernel / kb.c
blobd5844ee98e5d1fd1f003b6de2ed896fa1f753713
1 /* OK!
2 * this is the program that i don't wanna write most.
3 * there are even some mistakes, such as when CAPS_LOCK
4 * is on, i just put it in the SHIFT case, so mistakes
5 * happens, saying i press the 1 he will put the '!',
6 * yeah, that's realy and simply equal to the SHIFT case.
7 *
8 * what it can't do also is it can't handle when the CAPS
9 * Lock and SHIFT key press both present. And we only can ues
10 * the PAD key by CAPS_LOCK or by SHIFT key. Really ugly. but
11 * i don't wanna do any work on it.
13 * Maybe i will correct it in furture when i'm free and
14 * with the passion that swearing to correct it or even re-
15 * write it.
17 * What an ugly work !
19 * Aleaxander, 2007-2008
20 * Aleaxander@gmail.com
24 * OK again!
25 * Altough it makes mistakes, but i am happy that it can handle
26 * the regular things like move the cursor left,right..., it also
27 * can move the start of line (HOME) or the end of a line(END).
28 * and what the most important is you can move it anywhere you like.
29 * God bless you. :)
32 #include <asm/io.h>
33 #include <asm/system.h>
34 #include <keyboard.h>
35 #include <console.h>
36 #include <string.h>
38 static unsigned char scancode;
39 //static unsigned char brkflag;
40 static unsigned char mode = 0;
41 static unsigned char e0 = 2; /* num lock is opened by default */
42 static unsigned char key;
43 static unsigned char leds;
44 //static unsigned char shift;
46 static void kb_wait(void);
47 extern void con_write(char *, int);
49 #define NO 0x0
51 static unsigned char normal_map[256] =
53 NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00
54 '7', '8', '9', '0', '-', '=', '\b', '\t',
55 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 0x10
56 'o', 'p', '[', ']', '\n', NO, 'a', 's',
57 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 0x20
58 '\'', '`', NO, '\\', 'z', 'x', 'c', 'v',
59 'b', 'n', 'm', ',', '.', '/', NO, '*', // 0x30
60 NO, ' ', NO, NO, NO, NO, NO, NO,
61 NO, NO, NO, NO, NO, NO, NO, HOME, // 0x40
62 UP, PGUP, '-', LF, '5', RT, '+', END,
63 DN, PGDN, INS, DEL, NO, NO, NO, NO, // 0x50
64 [0x97] KEY_HOME,
65 [0x9C] '\n', // KP_Enter
66 [0xB5] '/', // KP_Div
67 [0xC8] KEY_UP,
68 [0xC9] KEY_PGUP,
69 [0xCB] KEY_LF,
70 [0xCD] KEY_RT,
71 [0xCF] KEY_END,
72 [0xD0] KEY_DN,
73 [0xD1] KEY_PGDN,
74 [0xD2] KEY_INS,
75 [0xD3] KEY_DEL
78 static unsigned char shift_map[256] = {
79 NO, 033, '!', '@', '#', '$', '%', '^', // 0x00
80 '&', '*', '(', ')', '_', '+', '\b', '\t',
81 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 0x10
82 'O', 'P', '{', '}', '\n', NO, 'A', 'S',
83 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 0x20
84 '"', '~', NO, '|', 'Z', 'X', 'C', 'V',
85 'B', 'N', 'M', '<', '>', '?', NO, '*', // 0x30
86 NO, ' ', NO, NO, NO, NO, NO, NO,
87 NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
88 '8', '9', '-', '4', '5', '6', '+', '1',
89 '2', '3', '0', '.', NO, NO, NO, NO, // 0x50
90 [0x97] KEY_HOME,
91 [0x9C] '\n', // KP_Enter
92 [0xB5] '/', // KP_Div
93 [0xC8] KEY_UP,
94 [0xC9] KEY_PGUP,
95 [0xCB] KEY_LF,
96 [0xCD] KEY_RT,
97 [0xCF] KEY_END,
98 [0xD0] KEY_DN,
99 [0xD1] KEY_PGDN,
100 [0xD2] KEY_INS,
101 [0xD3] KEY_DEL
104 #define C(x) (x - '@')
106 static unsigned char ctl_map[256] = {
107 NO, NO, NO, NO, NO, NO, NO, NO,
108 NO, NO, NO, NO, NO, NO, NO, NO,
109 C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
110 C('O'), C('P'), NO, NO, '\r', NO, C('A'), C('S'),
111 C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), NO,
112 NO, NO, NO, C('\\'), C('Z'), C('X'), C('C'), C('V'),
113 C('B'), C('N'), C('M'), NO, NO, C('/'), NO, NO,
114 [0x97] KEY_HOME,
115 [0xB5] C('/'), // KP_Div
116 [0xC8] KEY_UP,
117 [0xC9] KEY_PGUP,
118 [0xCB] KEY_LF,
119 [0xCD] KEY_RT,
120 [0xCF] KEY_END,
121 [0xD0] KEY_DN,
122 [0xD1] KEY_PGDN,
123 [0xD2] KEY_INS,
124 [0xD3] KEY_DEL
128 extern void parse_command (char *);
129 static int index = 0;
130 char command_buffer[512] = {'\0',};
133 /* printable char */
134 static void pln(void)
136 unsigned char *map;
139 if (scancode & 0x80)
140 return;
143 if ((mode & (RSHIFT | LSHIFT)) || (leds & CAPS_LOCK))
144 map = shift_map;
145 else
146 map = normal_map;
147 key = *(map + scancode);
149 /* I have no meaning of the following code, so just ignore it */
150 #if 0
151 if (mode & (LCTRL | RCTRL | CAPS_STATE))
152 if (key >= 'a' && key <= '}')
153 key -= 32;
154 if (mode & (LCTRL | RCTRL))
155 if (key >= 64 && key <= 64 + 26)
156 key -= 32;
157 #endif
159 /* shell part */
161 if (key == '\n') {
162 command_buffer[index] = '\0';
163 goto skip;
166 if (key == '\b') {
167 if (index) {
168 index --;
169 goto skip;
172 return;
175 command_buffer[index] = (char)key;
176 index ++;
178 skip:
179 con_write((char *)&key,1);
181 if (key == '\n') {
183 if ( index == 0)
184 goto next;
186 parse_command (command_buffer);
187 memset(command_buffer, '\0', index);
188 index = 0;
189 next:
190 puts("thunix $ ");
195 static void unp(void)
197 /* Just do nothing */
200 static void ctl(void)
202 unsigned char temp;
203 temp = LCTRL;
205 if (e0 & E0)
206 temp <<= 1;
208 mode |= temp;
211 static void alt(void)
213 unsigned char temp;
214 temp = LALT;
216 if (e0 & E0)
217 temp <<= 1;
219 mode |= temp;
222 static void sft(void)
224 mode |= LSHIFT;
225 if (scancode == 0x36)
226 mode |= LSHIFT * 2;
229 static void unshift(void)
231 if (scancode == 0xAA)
232 mode &= ~LSHIFT;
233 else if (scancode == 0xB6)
234 mode &= ~RSHIFT;
237 static void cur(void)
239 /*Not implemented yet*/
242 static void set_leds(void)
244 kb_wait();
245 outb(0xed, 0x60); /* set leds command */
246 kb_wait();
247 outb(leds, 0x60); /* send param */
250 static void sroll(void)
252 leds ^= SCROLL_LOCK;
253 set_leds();
256 static void num_lock(void)
258 leds ^= NUM_LOCK;
259 set_leds();
262 static void cap(void)
264 leds ^= CAPS_LOCK;
265 set_leds();
268 static void kb_wait(void)
270 while (inb(0x64) & 0x02)
274 static void fun(void)
276 /* Not implement */
280 * key make break
281 * home 0x47 0xc7
282 * up 0x48 0xc8
283 * pgup 0x49 0xc9
284 * - 0x4A 0xca
285 * left 0x4B 0xcb
286 * centr 0x4C 0xcc
287 * right 0x4D 0xcd
288 * + 0x4E 0xce
289 * end 0x4F 0xcf
290 * down 0x50 0xd0
291 * pgdn 0x51 0xd1
292 * ins 0x52 0xd2
293 * del 0x53 0xd3
296 void (*kfun_table[0x80])(void) = {
297 /* 0/8 1/9 2/a 3/b 4/c 5/d 6/e 7/f */
298 /*00*/unp, unp, pln, pln, pln, pln, pln, pln,
299 /* */pln, pln, pln, pln, pln, pln, pln, pln,
300 /*10*/pln, pln, pln, pln, pln, pln, pln, pln,
301 /* */pln, pln, pln, pln, pln, ctl, pln, pln,
302 /*20*/pln, pln, pln, pln, pln, pln, pln, pln,
303 /* */pln, pln, sft, pln, pln, pln, pln, pln,
304 /*30*/pln, pln, pln, pln, pln, pln, sft, pln,
305 /* */alt, pln, cap, fun, fun, fun, fun, fun,
306 /*40*/fun, fun, fun, fun, fun, unp, unp, pln,
307 /* */pln, pln, pln, unp, pln, pln, pln, pln,
308 /*50*/pln, pln, pln, pln, unp, unp, unp, fun,
309 /* */fun, unp, unp, unp, unp, unp, unp, unp,
310 /*60*/unp, unp, unp, unp, unp, unp, unp, unp,
311 /* */unp, unp, unp, unp, unp, unp, unp, unp,
312 /*70*/unp, unp, unp, unp, unp, unp, unp, unp,
313 /* */unp, unp, unp, unp, unp, unp, unp, unp,
318 /* looks like not works */
319 void wait_for_keypress()
321 char code;
323 code = inb(0x60);
325 while ( (code == '\0') || (code & 0x80) )
329 void keyboard_interrupt ()
331 int com;
334 com = 0;
336 scancode = inb(0x60);
340 if (scancode == 0xAA || scancode == 0xB6) {
341 unshift();
342 goto end;
345 if (scancode == 0x2A || scancode == 0x36) {
346 sft();
347 goto end;
350 if (scancode == 0x3A || scancode == 0xB6) {
351 cap();
352 goto end;
356 * I don't know why i need hanle the left move here,
357 * may just because i don't know why the origin handle
358 * can't work! what a sham day
360 * And what make happy is that it works now! greate work.
362 if (scancode == 0x4b) {
363 scancode = LF;
364 con_write((char *)&scancode, 1);
365 goto end;
369 /* we haven't implenmented the ESC codes now */
370 #if 0
371 if (scancode == 0xe0) {
372 e0 = 1;
373 goto end;
375 /* we must reset the e0 later */
376 if (e0 == 1 && scancode & 0x80) {
377 e0 = 1;
378 goto end;
380 #endif
381 if (scancode & 0x80)
382 goto end;
383 else
384 (*kfun_table[scancode&0x7f])();
385 /* key stroke has been handled */
387 end:
388 outb((com=inb(0x61))|0x80, 0x61); /* disable it */
389 outb(com&0x7f, 0x61); /* enable it again */
390 outb(0x20, 0x20); /* EOI */
394 void keyboard_init ()
396 set_trap_gate(0x21,keyboard_interrupt);
397 outb(inb(0x21)&0xfd, 0x21);