kernel/drivers/tty.c (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include "console.h"
#include "file.h"
#include "drivers/tty.h"
#include "drivers/console.h"
#include "drivers/keyboard.h"
file_operations_t tty_fops = {
tty_open, /* open */
tty_openat, /* openat */
tty_read, /* read */
tty_write, /* write */
NULL, /* dir_next */
NULL, /* lseek */
NULL /* tell */
};
int tty_open(const char *pathname, int flags) {
(void)pathname;
(void)flags;
return 0;
}
int tty_openat(file_t *file, file_t *handle, const char *fname, int flags) {
(void)file;
(void)handle;
(void)fname;
(void)flags;
return 0;
}
ssize_t tty_read(file_t *file, char *buffer, size_t size) {
ssize_t i = 0;
static int state = 0;
static char next[2];
(void)file;
while (i < size) {
if (state > 0) {
buffer[i++] = next[--state];
continue;
}
char table[] = {
'^', 0x1B, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\\', '`', 0x08,
'\t', 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', '?', '+', '\n',
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '?', '?', '#',
0, '<', 'y', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '-', 0, 0, 0, ' ',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'A' };
int scancode = std_kb->read();
if (scancode < 0) {
break;
} else if (scancode <= 60) {
char c = scancode <= 60 ? table[scancode] : 0;
if (c == 0) { continue; }
buffer[i++] = c;
} else if (scancode == 0xE0) {
scancode = std_kb->read();
if (scancode > 0x48 || table[scancode] == 0) { continue; }
buffer[i++] = '^';
next[1] = '[';
next[0] = table[scancode];
state = 2;
}
}
return i;
}
ssize_t tty_write(file_t *file, const char *buffer, size_t size) {
(void)file;
vga_con.write_n(buffer, size);
return size;
}