From: acevest Date: Thu, 1 Jun 2023 12:43:22 +0000 (+0800) Subject: F12支持在8个tty之间切换 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=1f1e78759a0fcf68989110a942b32978d9bb8a9b;p=kernel.git F12支持在8个tty之间切换 --- diff --git a/drivers/console.c b/drivers/console.c index 32860c7..c186740 100644 --- a/drivers/console.c +++ b/drivers/console.c @@ -69,10 +69,10 @@ int cnsl_kbd_write(char ch) { if (ch == 0) { return 0; } - extern tty_t default_tty; + extern tty_t *const default_tty; if (ch == '\b') { if (!empty(&cnsl.wr_q)) { - tty_color_putc(&default_tty, '\b', TTY_FG_HIGHLIGHT | TTY_WHITE, TTY_BLACK); + tty_color_putc(default_tty, '\b', TTY_FG_HIGHLIGHT | TTY_WHITE, TTY_BLACK); } // tty_color_putc(default_tty, '\b', TTY_WHITE, TTY_BLACK); erase(&cnsl.wr_q); @@ -80,7 +80,7 @@ int cnsl_kbd_write(char ch) { } else { put(&cnsl.wr_q, ch); put(&cnsl.sc_q, ch); - tty_color_putc(&default_tty, ch, TTY_FG_HIGHLIGHT | TTY_WHITE, TTY_BLACK); + tty_color_putc(default_tty, ch, TTY_FG_HIGHLIGHT | TTY_WHITE, TTY_BLACK); } if (ch == '\n') { diff --git a/drivers/keyboard.c b/drivers/keyboard.c index 4cb4359..e1f729a 100644 --- a/drivers/keyboard.c +++ b/drivers/keyboard.c @@ -69,12 +69,10 @@ void kbd_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { add_irq_bh_handler(kbd_bh_handler); } -extern tty_t default_tty; -extern tty_t monitor_tty; -extern tty_t debug_tty; - -static tty_t *ttys[] = {&default_tty, &monitor_tty, &debug_tty}; -static int tty_no = 0; +extern tty_t *const default_tty; +extern tty_t *const monitor_tty; +extern tty_t *const debug_tty; +extern void tty_switch_to_next(); void kbd_debug(uint8_t scan_code) { static unsigned long kbd_cnt = 0; @@ -88,11 +86,11 @@ void kbd_debug(uint8_t scan_code) { // printd("[%02x]", scan_code); if (scan_code == 0x3B) { // F1 - tty_switch(&default_tty); + tty_switch(default_tty); } else if (scan_code == 0x3C) { // F2 - tty_switch(&monitor_tty); + tty_switch(monitor_tty); } else if (scan_code == 0x3D) { // F3 - tty_switch(&debug_tty); + tty_switch(debug_tty); } if (scan_code == 0x3F) // F5 @@ -121,9 +119,7 @@ void kbd_debug(uint8_t scan_code) { } if (scan_code == 0x58) { // F12 - // current_tty = current_tty != &default_tty ? &default_tty : &monitor_tty; - current_tty = ttys[++tty_no % (sizeof(ttys) / sizeof(ttys[0]))]; - tty_switch(current_tty); + tty_switch_to_next(); } // ide_status(); diff --git a/include/tty.h b/include/tty.h index b162f3a..1a69558 100644 --- a/include/tty.h +++ b/include/tty.h @@ -44,4 +44,6 @@ void tty_color_putc(tty_t *tty, char c, unsigned int fg_color, unsigned bg_color void tty_set_cursor(tty_t *tty); void tty_switch(tty_t *tty); -extern tty_t *current_tty; \ No newline at end of file +void tty_switch_to_next(); + +extern tty_t *current_tty; diff --git a/kernel/printk.c b/kernel/printk.c index 08eee39..d2c5d1a 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -21,37 +21,37 @@ int vsprintf(char *buf, const char *fmt, char *args); char pkbuf[1024]; -extern tty_t default_tty; +extern tty_t *const default_tty; int printk(const char *fmtstr, ...) { unsigned long iflags; irq_save(iflags); char *args = (char *)(((char *)&fmtstr) + 4); int size = vsprintf(pkbuf, fmtstr, args); - tty_write(&default_tty, pkbuf, (size_t)size); + tty_write(default_tty, pkbuf, (size_t)size); irq_restore(iflags); return 0; } -extern tty_t debug_tty; +extern tty_t *const debug_tty; char pdbuf[1024]; int printd(const char *fmtstr, ...) { unsigned long iflags; irq_save(iflags); char *args = (char *)(((char *)&fmtstr) + 4); int size = vsprintf(pdbuf, fmtstr, args); - tty_write(&debug_tty, pdbuf, (size_t)size); + tty_write(debug_tty, pdbuf, (size_t)size); irq_restore(iflags); return 0; } char plobuf[1024]; -extern tty_t monitor_tty; +extern tty_t *const monitor_tty; int printlo(unsigned int xpos, unsigned int ypos, const char *fmtstr, ...) { unsigned long iflags; irq_save(iflags); char *args = (char *)(((char *)&fmtstr) + 4); int size = vsprintf(plobuf, fmtstr, args); - tty_write_at(&monitor_tty, xpos, ypos, plobuf, (size_t)size); + tty_write_at(monitor_tty, xpos, ypos, plobuf, (size_t)size); irq_restore(iflags); return 0; } diff --git a/kernel/setup.c b/kernel/setup.c index d27faab..a410fe3 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -79,8 +79,8 @@ void setup_kernel() { printk(version); boot_delay(DEFAULT_BOOT_DELAY_TICKS); - extern tty_t monitor_tty; - // tty_switch(&monitor_tty); + extern tty_t *const monitor_tty; + // tty_switch(monitor_tty); boot_delay(DEFAULT_BOOT_DELAY_TICKS); diff --git a/kernel/tty.c b/kernel/tty.c index 21976e0..3d78ab7 100644 --- a/kernel/tty.c +++ b/kernel/tty.c @@ -22,15 +22,18 @@ const uint32_t PHY_VADDR = 0xB8000; const uint32_t VADDR = (uint32_t)pa2va(PHY_VADDR); #define TTY_VRAM_SIZE (0x1000) +#define NR_TTYS 8 +tty_t ttys[NR_TTYS]; + #define MAX_X 80 #define MAX_Y 25 #define CHARS_PER_LINE (MAX_X) #define BYTES_PER_LINE (CHARS_PER_LINE * 2) #define TAB_SPACE 4 -tty_t default_tty; -tty_t monitor_tty; -tty_t debug_tty; +tty_t *const default_tty = ttys + 0; +tty_t *const monitor_tty = ttys + 1; +tty_t *const debug_tty = ttys + 2; void tty_clear(tty_t *tty) { char *dst = (char *)tty->base_addr; @@ -65,8 +68,6 @@ void __tty_set_next_pos_color(tty_t *tty, char color) { void init_tty(tty_t *tty, const char *name, unsigned long base) { assert(0 != tty); - memset(tty, 0, sizeof(tty_t)); - strlcpy(tty->name, name, sizeof(tty->name)); tty->fg_color = TTY_FG_HIGHLIGHT | TTY_GREEN; // 高亮 @@ -84,20 +85,35 @@ void init_tty(tty_t *tty, const char *name, unsigned long base) { void init_ttys() { assert(irq_disabled()); - init_tty(&default_tty, "tty.default", VADDR + 0 * TTY_VRAM_SIZE); - init_tty(&monitor_tty, "tty.monitor", VADDR + 1 * TTY_VRAM_SIZE); - init_tty(&debug_tty, "tty.debug", VADDR + 7 * TTY_VRAM_SIZE); + for (int i = 0; i < NR_TTYS; i++) { + tty_t *tty = ttys + i; + char name[sizeof(tty->name)]; + sprintf(name, "tty.%u", i); + init_tty(tty, name, VADDR + i * TTY_VRAM_SIZE); - monitor_tty.fg_color = TTY_FG_HIGHLIGHT | TTY_WHITE; - monitor_tty.bg_color = TTY_BLUE; + if (i != TTY_WHITE) { + tty->fg_color = TTY_FG_HIGHLIGHT | TTY_WHITE; + tty->bg_color = i; + } else { + tty->fg_color = TTY_FG_HIGHLIGHT | TTY_BLACK; + tty->bg_color = TTY_WHITE; + } + } - debug_tty.fg_color = TTY_FG_HIGHLIGHT | TTY_WHITE; - debug_tty.bg_color = TTY_CYAN; + default_tty->fg_color = TTY_FG_HIGHLIGHT | TTY_GREEN; + default_tty->bg_color = TTY_BLACK; - tty_clear(&monitor_tty); - tty_clear(&debug_tty); + monitor_tty->fg_color = TTY_FG_HIGHLIGHT | TTY_WHITE; + monitor_tty->bg_color = TTY_BLUE; - current_tty = &default_tty; + debug_tty->fg_color = TTY_FG_HIGHLIGHT | TTY_WHITE; + debug_tty->bg_color = TTY_BLACK; // TTY_CYAN; + + for (int i = 0; i < NR_TTYS; i++) { + tty_t *tty = ttys + i; + tty_clear(tty); + } + current_tty = default_tty; } void tty_do_scroll_up(tty_t *tty) { @@ -250,4 +266,9 @@ void tty_switch(tty_t *tty) { tty_set_cursor(current_tty); } +void tty_switch_to_next() { + tty_t *tty = ttys + ((current_tty - ttys + 1) % NR_TTYS); + tty_switch(tty); +} + tty_t *current_tty;