BigOS/kernel/tty.c
2023-01-15 16:10:09 +08:00

175 lines
4.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "type.h"
#include "const.h"
#include "protect.h"
#include "string.h"
#include "proc.h"
#include "tty.h"
#include "console.h"
#include "global.h"
#include "proto.h"
#include "keyboard.h"
#include "x86.h"
#include "memman.h"
#include "stdio.h"
#include "serialport.h"
#include "bga.h"
int tty_ok = 0;
void tty_write(NTTY *tty, char *buf, int len);
int tty_read(NTTY *tty, char *buf, int len);
NTTY *cur_ntty;
NTTY *ntty_table[NR_CONSOLES];
static void write_default_tty(char ch)
{
cur_ntty->write(cur_ntty, ch);
}
inline NTTY *get_tty(const int nr_tty)
{
if (nr_tty >= NR_CONSOLES || nr_tty < 0)
return NULL;
return ntty_table[nr_tty];
}
static int ps2_vga_ioctl(NTTY *tty, u32 cmd, long arg)
{
int retval = 0;
switch (cmd)
{
case IOCTL_CMD_TTY_SCROLL:
vga_tty_scroll(tty, arg);
break;
case IOCTL_CMD_TTY_SELECTCON:
if (get_tty(arg) == NULL || get_tty(arg)->driver_type != 1)
retval = -1;
else
vga_tty_select(get_tty(arg));
break;
case IOCTL_CMD_TTY_FLUSH:
if (tty == cur_ntty)
vga_tty_flush(tty);
break;
default:
break;
}
return retval;
}
static int dummy_ioctl(NTTY *tty, u32 cmd, long arg) { return 0; }
void init_tty_main()
{
NTTY *tty;
for (int i = 0; i < 1; ++i)
{
tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
tty->driver_type = 1;
#ifdef USE_FBCON
tty->input_buf = (keyboard_buf*)K_PHY2LIN(do_kmalloc(sizeof(keyboard_buf)));
tty->output_buf = NULL;
// vga_tty_init(tty);
ps2_tty_init(tty);
tty->write = fbcon_tty_write;
tty->read = ps2_tty_read;
tty->recvbuf = ps2_tty_recvbuf;
tty->ioctl = dummy_ioctl;
#else
tty = (NTTY*)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
tty->driver_type = 1;
tty->input_buf = (keyboard_buf*)K_PHY2LIN(do_kmalloc(sizeof(keyboard_buf)));
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;
tty->ioctl = ps2_vga_ioctl;
#endif
ntty_table[i] = tty;
}
for (int i = 1; i < 2; ++i)
{
// tty = &ntty_table[i];
tty = (NTTY *)K_PHY2LIN(do_kmalloc(sizeof(NTTY)));
tty->driver_type = 1; // vga
// tty->input_buf = (void*)do_kmalloc(sizeof(keyboard_buf));
// tty->output_buf = (void*)do_kmalloc(sizeof(vga_buf));
tty->input_buf = (keyboard_buf *)K_PHY2LIN(do_kmalloc(sizeof(keyboard_buf)));
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;
tty->ioctl = ps2_vga_ioctl;
ntty_table[i] = 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;
tty->ioctl = dummy_ioctl;
ntty_table[2] = tty;
}
cur_ntty = ntty_table[0];
tty_ok = 1;
simpleconsole_transfer(write_default_tty);
kprintf("TTY initialized\n");
}
/*****************************************************************************
* tty_write
****************************************************************************
* 当fd=STD_OUT时write()系统调用转发到此函数
*****************************************************************************/
void tty_write(NTTY *tty, char *buf, int len)
{
if (!tty_ok)
{
simpleconsole_write(buf, len);
// while (--len >= 0) write_serial(*buf++);
return;
}
else
{
while (--len >= 0)
{
tty->write(tty, *buf++);
}
tty->ioctl(tty, IOCTL_CMD_TTY_FLUSH, 0);
}
// else if (tty->driver_type == 1) {
// while (--len >= 0)
// {
// vga_tty_write(tty, *buf++);
// }
// if (cur_ntty == tty) vga_tty_flush(tty);
// }
// else if (tty->driver_type == 2) {
// while(--len >= 0) {
// serial_tty_write(tty, *buf++);
// }
// }
}
/*****************************************************************************
* tty_read
****************************************************************************
* 当fd=STD_IN时read()系统调用转发到此函数
*****************************************************************************/
int tty_read(NTTY *tty, char *buf, int len)
{
return tty->read(tty, buf, len);
}