better api 1(ioctl still no)

This commit is contained in:
catfood 2023-01-07 23:15:49 +08:00
parent db97135880
commit a3a2bb4cfe
5 changed files with 41 additions and 29 deletions

View File

@ -13,6 +13,6 @@ void serial_tty_init_i(NTTY* tty);
void serial_tty_init_o(NTTY* tty);
int serial_tty_read(NTTY* tty, char* buf, int nr);
void serial_tty_write(NTTY* tty, char ch);
void serial_tty_rcevbuf(NTTY* tty, char ch);
void serial_tty_recvbuf(NTTY* tty, u32 ch);
#endif

View File

@ -54,6 +54,9 @@ typedef struct n_tty
int driver_type; // 1-vga&kbd; 2-serial
void *input_buf;
void *output_buf;
void (*write)(struct n_tty *tty, char ch);
int (*read)(struct n_tty *tty, char* buf, int nr);
void (*recvbuf)(struct n_tty *tty, u32 ch);
} NTTY;
enum CSI_state
@ -117,6 +120,7 @@ void vga_tty_select(NTTY *tty);
void ps2_tty_init(NTTY *tty);
int ps2_tty_read(NTTY *tty, char *buf, int nr);
void ps2_tty_recvbuf(NTTY *tty, u32 key);
#define CYCLE_SUB(head, tail, _max) ((head) <= (tail) ? (tail) - (head) : (tail) + (_max) - (head))
#define NEXT(x, _max) (((x) + 1) % (_max))

View File

@ -33,7 +33,6 @@ static u8 mouse_data[MOUSE_IN_BYTES];
static void set_leds();
static void set_mouse_leds();
static void kb_wait();
static void ps2_push(NTTY *tty, u32 key);
static void kbd_process(unsigned char scode);
/*
@ -128,7 +127,7 @@ void mouse_handler(int irq)
aux_state ^= (1 << i);
pack |= !!(aux_state & (1 << i)) ? MOUSEBTN_CLICK : MOUSEBTN_RELEASE;
pack |= (1 << i);
ps2_push(cur_ntty, pack);
cur_ntty->recvbuf(cur_ntty, pack);
}
}
pack = ISMOUSE | MOUSESCR;
@ -136,13 +135,13 @@ void mouse_handler(int irq)
{
// actually it is 0x1
pack |= MOUSESCR_DOWN;
ps2_push(cur_ntty, pack);
cur_ntty->recvbuf(cur_ntty, pack);
}
else if ((signed char)mouse_data[3] < 0)
{
// actually it is 0xff
pack |= MOUSESCR_UP;
ps2_push(cur_ntty, pack);
cur_ntty->recvbuf(cur_ntty, pack);
}
for (i = 0; i < 2; i++)
{
@ -154,7 +153,7 @@ void mouse_handler(int irq)
pack |= MOUSEPOS_NEG;
if (i == 1)
pack |= MOUSEPOS_XY;
ps2_push(cur_ntty, pack);
cur_ntty->recvbuf(cur_ntty, pack);
}
}
// kprintf("0x%02x 0x%02x 0x%02x 0x%02x\n", mouse_data[0], mouse_data[1], mouse_data[2], mouse_data[3]);
@ -365,7 +364,7 @@ static void kbd_process(unsigned char scode)
// inputdriver_send_event(FALSE /*mouse*/, page, code, press, 0);
if (press)
ps2_push(cur_ntty, map_key(code));
cur_ntty->recvbuf(cur_ntty, map_key(code));
}
kbd_state = 0;
@ -425,7 +424,7 @@ void ps2_tty_flush(NTTY *tty)
enable_int();
}
static void ps2_push(NTTY *tty, u32 key)
void ps2_tty_recvbuf(NTTY *tty, u32 key)
{
// kprintf("%x\n", key);
disable_int();
@ -483,7 +482,7 @@ static void ps2_push(NTTY *tty, u32 key)
{
case '\r':
case '\n': // escape ENTER
vga_tty_write(tty, '\n');
tty->write(tty, '\n');
buf[kbd->tail] = '\n';
kbd->len++;
kbd->tail = NEXTKEY(kbd->tail);
@ -493,7 +492,10 @@ static void ps2_push(NTTY *tty, u32 key)
case '\b':
if (kbd->len > kbd->readable)
{
vga_tty_backspace(tty);
// vga_tty_backspace(tty);
tty->write(tty, '\b');
tty->write(tty, ' ');
tty->write(tty, '\b');
kbd->len--;
kbd->tail = LASTKEY(kbd->tail);
}
@ -504,7 +506,7 @@ static void ps2_push(NTTY *tty, u32 key)
return;
if (kbd->len == TTY_IN_BYTES - 1)
return; // leave one space for ctrl ascii
vga_tty_write(tty, key);
tty->write(tty, key);
buf[kbd->tail] = (u8)(key & 0xff);
// kprintf("%d %d %d", key & 0xff, kbd->tail, buf[kbd->tail]);
kbd->len++;

View File

@ -46,7 +46,7 @@ static void serial_buf_push(NTTY* tty, u32 key) {
sp->len++;
sp->tail = NEXTKEY(sp->tail);
sp->readable = CYCLE_SUB(sp->head, sp->tail, SERIAL_BUF_SIZE);
serial_tty_rcevbuf(tty, '\n');
tty->recvbuf(tty, '\n');
break;
case '\b': case 127:
key = '\b';
@ -54,9 +54,9 @@ static void serial_buf_push(NTTY* tty, u32 key) {
{
sp->len--;
sp->tail = LASTKEY(sp->tail);
serial_tty_rcevbuf(tty, '\b');
serial_tty_rcevbuf(tty, ' ');
serial_tty_rcevbuf(tty, '\b');
tty->recvbuf(tty, '\b');
tty->recvbuf(tty, ' ');
tty->recvbuf(tty, '\b');
}
break;
@ -68,7 +68,7 @@ static void serial_buf_push(NTTY* tty, u32 key) {
buf[sp->tail] = (u8)(key & 0xff);
sp->len++;
sp->tail = NEXTKEY(sp->tail);
serial_tty_rcevbuf(tty, key & 0xff);
tty->recvbuf(tty, key & 0xff);
break;
}
}
@ -102,8 +102,8 @@ void serial_tty_write(NTTY* tty, char ch) {
write_serial(ch);
}
void serial_tty_rcevbuf(NTTY* tty, char ch) {
serial_tty_write(tty, ch);
void serial_tty_recvbuf(NTTY* tty, u32 ch) {
tty->write(tty, ch & 0xff);
}
int init_serial() {

View File

@ -46,17 +46,23 @@ void init_tty_main()
tty->output_buf = (vga_buf*)K_PHY2LIN(do_kmalloc(sizeof(vga_buf)));
vga_tty_init(tty);
ps2_tty_init(tty);
tty->write = vga_tty_write;
tty->read = ps2_tty_read;
tty->recvbuf = ps2_tty_recvbuf;
ntty_table[i] = tty;
// kprintf("tty: %p, outbuf: %p\n", tty, tty->output_buf);
}
tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
tty->driver_type = 2;
tty->input_buf = (serial_buf*)K_PHY2LIN(do_kmalloc(sizeof(serial_buf)));
// kprintf("tty: %p, outbuf: %p\n", tty, tty->input_buf);
tty->output_buf = NULL;
serial_tty_init_i(tty);
serial_tty_init_o(tty);
ntty_table[2] = tty;
for (int i = 2; i < 3; ++ i) {
tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
tty->driver_type = 2;
tty->input_buf = (serial_buf*)K_PHY2LIN(do_kmalloc(sizeof(serial_buf)));
tty->output_buf = NULL;
serial_tty_init_i(tty);
serial_tty_init_o(tty);
tty->write = serial_tty_write;
tty->read = serial_tty_read;
tty->recvbuf = serial_tty_recvbuf;
ntty_table[2] = tty;
}
cur_ntty = ntty_table[0];
tty_ok = 1;
kprintf("TTY initialized\n");
@ -72,7 +78,7 @@ void task_tty()
volatile char serial_input;
while (1)
{
// sys_yield();
yield();
// vga_tty_flush(cur_ntty);
// serial_input = read_serial();
// vga_tty_write(get_tty(2), serial_input);
@ -119,5 +125,5 @@ void tty_write(NTTY *tty, char *buf, int len)
*****************************************************************************/
int tty_read(NTTY *tty, char *buf, int len)
{
return ps2_tty_read(tty, buf, len);
return tty->read(tty, buf, len);
}