diff --git a/stand/efi/libefi/efi_console.c b/stand/efi/libefi/efi_console.c index 010c949faf38..1a264b9821f8 100644 --- a/stand/efi/libefi/efi_console.c +++ b/stand/efi/libefi/efi_console.c @@ -1,1425 +1,1425 @@ /*- * Copyright (c) 2000 Doug Rabson * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include "bootstrap.h" extern int boot_services_gone; extern EFI_GUID gop_guid; static EFI_GUID simple_input_ex_guid = EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID; static SIMPLE_TEXT_OUTPUT_INTERFACE *conout; static SIMPLE_INPUT_INTERFACE *conin; static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *coninex; static bool efi_started; static int mode; /* Does ConOut have serial console? */ static uint32_t utf8_left; static uint32_t utf8_partial; #ifdef TERM_EMU #define DEFAULT_FGCOLOR EFI_LIGHTGRAY #define DEFAULT_BGCOLOR EFI_BLACK #define MAXARGS 8 static int args[MAXARGS], argc; static int fg_c, bg_c, curx, cury; static int esc; void get_pos(int *x, int *y); void curs_move(int *_x, int *_y, int x, int y); static void CL(int); void HO(void); void end_term(void); #endif #define TEXT_ROWS 24 #define TEXT_COLS 80 static tf_bell_t efi_cons_bell; static tf_cursor_t efi_text_cursor; static tf_putchar_t efi_text_putchar; static tf_fill_t efi_text_fill; static tf_copy_t efi_text_copy; static tf_param_t efi_text_param; static tf_respond_t efi_cons_respond; static teken_funcs_t tf = { .tf_bell = efi_cons_bell, .tf_cursor = efi_text_cursor, .tf_putchar = efi_text_putchar, .tf_fill = efi_text_fill, .tf_copy = efi_text_copy, .tf_param = efi_text_param, .tf_respond = efi_cons_respond, }; static teken_funcs_t tfx = { .tf_bell = efi_cons_bell, .tf_cursor = gfx_fb_cursor, .tf_putchar = gfx_fb_putchar, .tf_fill = gfx_fb_fill, .tf_copy = gfx_fb_copy, .tf_param = gfx_fb_param, .tf_respond = efi_cons_respond, }; #define KEYBUFSZ 10 static unsigned keybuf[KEYBUFSZ]; /* keybuf for extended codes */ static int key_pending; static const unsigned char teken_color_to_efi_color[16] = { EFI_BLACK, EFI_RED, EFI_GREEN, EFI_BROWN, EFI_BLUE, EFI_MAGENTA, EFI_CYAN, EFI_LIGHTGRAY, EFI_DARKGRAY, EFI_LIGHTRED, EFI_LIGHTGREEN, EFI_YELLOW, EFI_LIGHTBLUE, EFI_LIGHTMAGENTA, EFI_LIGHTCYAN, EFI_WHITE }; static void efi_cons_probe(struct console *); static int efi_cons_init(int); void efi_cons_putchar(int); int efi_cons_getchar(void); void efi_cons_efiputchar(int); int efi_cons_poll(void); static void cons_draw_frame(teken_attr_t *); struct console efi_console = { "efi", "EFI console", C_WIDEOUT, efi_cons_probe, efi_cons_init, efi_cons_putchar, efi_cons_getchar, efi_cons_poll }; /* * This function is used to mark a rectangular image area so the scrolling * will know we need to copy the data from there. */ void term_image_display(teken_gfx_t *state, const teken_rect_t *r) { teken_pos_t p; int idx; if (screen_buffer == NULL) return; for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++) { for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++) { idx = p.tp_col + p.tp_row * state->tg_tp.tp_col; if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row) return; screen_buffer[idx].a.ta_format |= TF_IMAGE; } } } /* * Not implemented. */ static void efi_cons_bell(void *s __unused) { } static void efi_text_cursor(void *arg, const teken_pos_t *p) { teken_gfx_t *state = arg; UINTN col, row; if (boot_services_gone) return; row = p->tp_row; if (p->tp_row >= state->tg_tp.tp_row) row = state->tg_tp.tp_row - 1; col = p->tp_col; if (p->tp_col >= state->tg_tp.tp_col) col = state->tg_tp.tp_col - 1; conout->SetCursorPosition(conout, col, row); } static void efi_text_printchar(teken_gfx_t *state, const teken_pos_t *p, bool autoscroll) { UINTN a, attr; struct text_pixel *px; teken_color_t fg, bg, tmp; px = screen_buffer + p->tp_col + p->tp_row * state->tg_tp.tp_col; a = conout->Mode->Attribute; fg = teken_256to16(px->a.ta_fgcolor); bg = teken_256to16(px->a.ta_bgcolor); if (px->a.ta_format & TF_BOLD) fg |= TC_LIGHT; if (px->a.ta_format & TF_BLINK) bg |= TC_LIGHT; if (px->a.ta_format & TF_REVERSE) { tmp = fg; fg = bg; bg = tmp; } attr = EFI_TEXT_ATTR(teken_color_to_efi_color[fg], teken_color_to_efi_color[bg] & 0x7); conout->SetCursorPosition(conout, p->tp_col, p->tp_row); /* to prevent autoscroll, skip print of lower right char */ if (!autoscroll && p->tp_row == state->tg_tp.tp_row - 1 && p->tp_col == state->tg_tp.tp_col - 1) return; (void) conout->SetAttribute(conout, attr); efi_cons_efiputchar(px->c); (void) conout->SetAttribute(conout, a); } static void efi_text_putchar(void *s, const teken_pos_t *p, teken_char_t c, const teken_attr_t *a) { teken_gfx_t *state = s; EFI_STATUS status; int idx; if (boot_services_gone) return; idx = p->tp_col + p->tp_row * state->tg_tp.tp_col; if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row) return; screen_buffer[idx].c = c; screen_buffer[idx].a = *a; efi_text_printchar(s, p, false); } static void efi_text_fill(void *arg, const teken_rect_t *r, teken_char_t c, const teken_attr_t *a) { teken_gfx_t *state = arg; teken_pos_t p; if (boot_services_gone) return; if (state->tg_cursor_visible) conout->EnableCursor(conout, FALSE); for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++) for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++) efi_text_putchar(state, &p, c, a); if (state->tg_cursor_visible) conout->EnableCursor(conout, TRUE); } static void efi_text_copy_line(teken_gfx_t *state, int ncol, teken_pos_t *s, teken_pos_t *d, bool scroll) { unsigned soffset, doffset; teken_pos_t sp, dp; int x; soffset = s->tp_col + s->tp_row * state->tg_tp.tp_col; doffset = d->tp_col + d->tp_row * state->tg_tp.tp_col; sp = *s; dp = *d; for (x = 0; x < ncol; x++) { sp.tp_col = s->tp_col + x; dp.tp_col = d->tp_col + x; if (!is_same_pixel(&screen_buffer[soffset + x], &screen_buffer[doffset + x])) { screen_buffer[doffset + x] = screen_buffer[soffset + x]; if (!scroll) efi_text_printchar(state, &dp, false); } else if (scroll) { /* Draw last char and trigger scroll. */ if (dp.tp_col + 1 == state->tg_tp.tp_col && dp.tp_row + 1 == state->tg_tp.tp_row) { efi_text_printchar(state, &dp, true); } } } } static void efi_text_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p) { teken_gfx_t *state = arg; unsigned doffset, soffset; teken_pos_t d, s; int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */ bool scroll = false; if (boot_services_gone) return; /* * Copying is a little tricky. We must make sure we do it in * correct order, to make sure we don't overwrite our own data. */ nrow = r->tr_end.tp_row - r->tr_begin.tp_row; ncol = r->tr_end.tp_col - r->tr_begin.tp_col; /* * Check if we do copy whole screen. */ if (p->tp_row == 0 && p->tp_col == 0 && nrow == state->tg_tp.tp_row - 2 && ncol == state->tg_tp.tp_col - 2) scroll = true; soffset = r->tr_begin.tp_col + r->tr_begin.tp_row * state->tg_tp.tp_col; doffset = p->tp_col + p->tp_row * state->tg_tp.tp_col; /* remove the cursor */ if (state->tg_cursor_visible) conout->EnableCursor(conout, FALSE); /* * Copy line by line. */ if (doffset <= soffset) { s = r->tr_begin; d = *p; for (y = 0; y < nrow; y++) { s.tp_row = r->tr_begin.tp_row + y; d.tp_row = p->tp_row + y; efi_text_copy_line(state, ncol, &s, &d, scroll); } } else { for (y = nrow - 1; y >= 0; y--) { s.tp_row = r->tr_begin.tp_row + y; d.tp_row = p->tp_row + y; efi_text_copy_line(state, ncol, &s, &d, false); } } /* display the cursor */ if (state->tg_cursor_visible) conout->EnableCursor(conout, TRUE); } static void efi_text_param(void *arg, int cmd, unsigned int value) { teken_gfx_t *state = arg; if (boot_services_gone) return; switch (cmd) { case TP_SETLOCALCURSOR: /* * 0 means normal (usually block), 1 means hidden, and * 2 means blinking (always block) for compatibility with * syscons. We don't support any changes except hiding, * so must map 2 to 0. */ value = (value == 1) ? 0 : 1; /* FALLTHROUGH */ case TP_SHOWCURSOR: if (value != 0) { conout->EnableCursor(conout, TRUE); state->tg_cursor_visible = true; } else { conout->EnableCursor(conout, FALSE); state->tg_cursor_visible = false; } break; default: /* Not yet implemented */ break; } } /* * Not implemented. */ static void efi_cons_respond(void *s __unused, const void *buf __unused, size_t len __unused) { } /* * Set up conin/conout/coninex to make sure we have input ready. */ static void efi_cons_probe(struct console *cp) { EFI_STATUS status; conout = ST->ConOut; conin = ST->ConIn; /* * Call SetMode to work around buggy firmware. */ status = conout->SetMode(conout, conout->Mode->Mode); if (coninex == NULL) { status = BS->OpenProtocol(ST->ConsoleInHandle, &simple_input_ex_guid, (void **)&coninex, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (status != EFI_SUCCESS) coninex = NULL; } cp->c_flags |= C_PRESENTIN | C_PRESENTOUT; } static bool color_name_to_teken(const char *name, int *val) { int light = 0; if (strncasecmp(name, "light", 5) == 0) { name += 5; light = TC_LIGHT; } else if (strncasecmp(name, "bright", 6) == 0) { name += 6; light = TC_LIGHT; } if (strcasecmp(name, "black") == 0) { *val = TC_BLACK | light; return (true); } if (strcasecmp(name, "red") == 0) { *val = TC_RED | light; return (true); } if (strcasecmp(name, "green") == 0) { *val = TC_GREEN | light; return (true); } if (strcasecmp(name, "yellow") == 0 || strcasecmp(name, "brown") == 0) { - *val = TC_BROWN | light; + *val = TC_YELLOW | light; return (true); } if (strcasecmp(name, "blue") == 0) { *val = TC_BLUE | light; return (true); } if (strcasecmp(name, "magenta") == 0) { *val = TC_MAGENTA | light; return (true); } if (strcasecmp(name, "cyan") == 0) { *val = TC_CYAN | light; return (true); } if (strcasecmp(name, "white") == 0) { *val = TC_WHITE | light; return (true); } return (false); } static int efi_set_colors(struct env_var *ev, int flags, const void *value) { int val = 0; char buf[3]; const void *evalue; const teken_attr_t *ap; teken_attr_t a; if (value == NULL) return (CMD_OK); if (color_name_to_teken(value, &val)) { snprintf(buf, sizeof (buf), "%d", val); evalue = buf; } else { char *end; long lval; errno = 0; lval = strtol(value, &end, 0); if (errno != 0 || *end != '\0' || lval < 0 || lval > 15) { printf("Allowed values are either ansi color name or " "number from range [0-15].\n"); return (CMD_OK); } val = (int)lval; evalue = value; } ap = teken_get_defattr(&gfx_state.tg_teken); a = *ap; if (strcmp(ev->ev_name, "teken.fg_color") == 0) { /* is it already set? */ if (ap->ta_fgcolor == val) return (CMD_OK); a.ta_fgcolor = val; } if (strcmp(ev->ev_name, "teken.bg_color") == 0) { /* is it already set? */ if (ap->ta_bgcolor == val) return (CMD_OK); a.ta_bgcolor = val; } /* Improve visibility */ if (a.ta_bgcolor == TC_WHITE) a.ta_bgcolor |= TC_LIGHT; teken_set_defattr(&gfx_state.tg_teken, &a); cons_draw_frame(&a); env_setenv(ev->ev_name, flags | EV_NOHOOK, evalue, NULL, NULL); teken_input(&gfx_state.tg_teken, "\e[2J", 4); return (CMD_OK); } #ifdef TERM_EMU /* Get cursor position. */ void get_pos(int *x, int *y) { *x = conout->Mode->CursorColumn; *y = conout->Mode->CursorRow; } /* Move cursor to x rows and y cols (0-based). */ void curs_move(int *_x, int *_y, int x, int y) { conout->SetCursorPosition(conout, x, y); if (_x != NULL) *_x = conout->Mode->CursorColumn; if (_y != NULL) *_y = conout->Mode->CursorRow; } /* Clear internal state of the terminal emulation code. */ void end_term(void) { esc = 0; argc = -1; } #endif static void efi_cons_rawputchar(int c) { int i; UINTN x, y; conout->QueryMode(conout, conout->Mode->Mode, &x, &y); if (c == '\t') { int n; n = 8 - ((conout->Mode->CursorColumn + 8) % 8); for (i = 0; i < n; i++) efi_cons_rawputchar(' '); } else { #ifndef TERM_EMU if (c == '\n') efi_cons_efiputchar('\r'); efi_cons_efiputchar(c); #else switch (c) { case '\r': curx = 0; efi_cons_efiputchar('\r'); return; case '\n': efi_cons_efiputchar('\n'); efi_cons_efiputchar('\r'); cury++; if (cury >= y) cury--; curx = 0; return; case '\b': if (curx > 0) { efi_cons_efiputchar('\b'); curx--; } return; default: efi_cons_efiputchar(c); curx++; if (curx > x-1) { curx = 0; cury++; } if (cury > y-1) { curx = 0; cury--; } } #endif } conout->EnableCursor(conout, TRUE); } #ifdef TERM_EMU /* Gracefully exit ESC-sequence processing in case of misunderstanding. */ static void bail_out(int c) { char buf[16], *ch; int i; if (esc) { efi_cons_rawputchar('\033'); if (esc != '\033') efi_cons_rawputchar(esc); for (i = 0; i <= argc; ++i) { sprintf(buf, "%d", args[i]); ch = buf; while (*ch) efi_cons_rawputchar(*ch++); } } efi_cons_rawputchar(c); end_term(); } /* Clear display from current position to end of screen. */ static void CD(void) { int i; UINTN x, y; get_pos(&curx, &cury); if (curx == 0 && cury == 0) { conout->ClearScreen(conout); end_term(); return; } conout->QueryMode(conout, conout->Mode->Mode, &x, &y); CL(0); /* clear current line from cursor to end */ for (i = cury + 1; i < y-1; i++) { curs_move(NULL, NULL, 0, i); CL(0); } curs_move(NULL, NULL, curx, cury); end_term(); } /* * Absolute cursor move to args[0] rows and args[1] columns * (the coordinates are 1-based). */ static void CM(void) { if (args[0] > 0) args[0]--; if (args[1] > 0) args[1]--; curs_move(&curx, &cury, args[1], args[0]); end_term(); } /* Home cursor (left top corner), also called from mode command. */ void HO(void) { argc = 1; args[0] = args[1] = 1; CM(); } /* Clear line from current position to end of line */ static void CL(int direction) { int i, len; UINTN x, y; CHAR16 *line; conout->QueryMode(conout, conout->Mode->Mode, &x, &y); switch (direction) { case 0: /* from cursor to end */ len = x - curx + 1; break; case 1: /* from beginning to cursor */ len = curx; break; case 2: /* entire line */ len = x; break; default: /* NOTREACHED */ __unreachable(); } if (cury == y - 1) len--; line = malloc(len * sizeof (CHAR16)); if (line == NULL) { printf("out of memory\n"); return; } for (i = 0; i < len; i++) line[i] = ' '; line[len-1] = 0; if (direction != 0) curs_move(NULL, NULL, 0, cury); conout->OutputString(conout, line); /* restore cursor position */ curs_move(NULL, NULL, curx, cury); free(line); end_term(); } static void get_arg(int c) { if (argc < 0) argc = 0; args[argc] *= 10; args[argc] += c - '0'; } #endif /* Emulate basic capabilities of cons25 terminal */ static void efi_term_emu(int c) { #ifdef TERM_EMU static int ansi_col[] = { 0, 4, 2, 6, 1, 5, 3, 7 }; int t, i; EFI_STATUS status; if (boot_services_gone) return; switch (esc) { case 0: switch (c) { case '\033': esc = c; break; default: efi_cons_rawputchar(c); break; } break; case '\033': switch (c) { case '[': esc = c; args[0] = 0; argc = -1; break; default: bail_out(c); break; } break; case '[': switch (c) { case ';': if (argc < 0) argc = 0; else if (argc + 1 >= MAXARGS) bail_out(c); else args[++argc] = 0; break; case 'H': /* ho = \E[H */ if (argc < 0) HO(); else if (argc == 1) CM(); else bail_out(c); break; case 'J': /* cd = \E[J */ if (argc < 0) CD(); else bail_out(c); break; case 'm': if (argc < 0) { fg_c = DEFAULT_FGCOLOR; bg_c = DEFAULT_BGCOLOR; } for (i = 0; i <= argc; ++i) { switch (args[i]) { case 0: /* back to normal */ fg_c = DEFAULT_FGCOLOR; bg_c = DEFAULT_BGCOLOR; break; case 1: /* bold */ fg_c |= 0x8; break; case 4: /* underline */ case 5: /* blink */ bg_c |= 0x8; break; case 7: /* reverse */ t = fg_c; fg_c = bg_c; bg_c = t; break; case 22: /* normal intensity */ fg_c &= ~0x8; break; case 24: /* not underline */ case 25: /* not blinking */ bg_c &= ~0x8; break; case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: fg_c = ansi_col[args[i] - 30]; break; case 39: /* normal */ fg_c = DEFAULT_FGCOLOR; break; case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: bg_c = ansi_col[args[i] - 40]; break; case 49: /* normal */ bg_c = DEFAULT_BGCOLOR; break; } } conout->SetAttribute(conout, EFI_TEXT_ATTR(fg_c, bg_c)); end_term(); break; default: if (isdigit(c)) get_arg(c); else bail_out(c); break; } break; default: bail_out(c); break; } #else if (!boot_services_gone) efi_cons_rawputchar(c); #endif } static int env_screen_nounset(struct env_var *ev __unused) { if (gfx_state.tg_fb_type == FB_TEXT) return (0); return (EPERM); } static void cons_draw_frame(teken_attr_t *a) { teken_attr_t attr = *a; teken_color_t fg = a->ta_fgcolor; attr.ta_fgcolor = attr.ta_bgcolor; teken_set_defattr(&gfx_state.tg_teken, &attr); gfx_fb_drawrect(0, 0, gfx_state.tg_fb.fb_width, gfx_state.tg_origin.tp_row, 1); gfx_fb_drawrect(0, gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row - 1, gfx_state.tg_fb.fb_width, gfx_state.tg_fb.fb_height, 1); gfx_fb_drawrect(0, gfx_state.tg_origin.tp_row, gfx_state.tg_origin.tp_col, gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row - 1, 1); gfx_fb_drawrect( gfx_state.tg_fb.fb_width - gfx_state.tg_origin.tp_col - 1, gfx_state.tg_origin.tp_row, gfx_state.tg_fb.fb_width, gfx_state.tg_fb.fb_height, 1); attr.ta_fgcolor = fg; teken_set_defattr(&gfx_state.tg_teken, &attr); } bool cons_update_mode(bool use_gfx_mode) { UINTN cols, rows; const teken_attr_t *a; teken_attr_t attr; EFI_STATUS status; char env[10], *ptr; if (!efi_started) return (false); /* * Despite the use_gfx_mode, we want to make sure we call * efi_find_framebuffer(). This will populate the fb data, * which will be passed to kernel. */ if (efi_find_framebuffer(&gfx_state) == 0 && use_gfx_mode) { int roff, goff, boff; roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1; goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1; boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1; (void) generate_cons_palette(cmap, COLOR_FORMAT_RGB, gfx_state.tg_fb.fb_mask_red >> roff, roff, gfx_state.tg_fb.fb_mask_green >> goff, goff, gfx_state.tg_fb.fb_mask_blue >> boff, boff); } else { /* * Either text mode was asked by user or we failed to * find frame buffer. */ gfx_state.tg_fb_type = FB_TEXT; } status = conout->QueryMode(conout, conout->Mode->Mode, &cols, &rows); if (EFI_ERROR(status) || cols * rows == 0) { cols = TEXT_COLS; rows = TEXT_ROWS; } /* * When we have serial port listed in ConOut, use pre-teken emulator, * if built with. * The problem is, we can not output text on efi and comconsole when * efi also has comconsole bound. But then again, we need to have * terminal emulator for efi text mode to support the menu. * While teken is too expensive to be used on serial console, the * pre-teken emulator is light enough to be used on serial console. * * When doing multiple consoles (both serial and video), * also just use the old emulator. RB_MULTIPLE also implies * we're using a serial console. */ mode = parse_uefi_con_out(); if ((mode & (RB_SERIAL | RB_MULTIPLE)) == 0) { conout->EnableCursor(conout, FALSE); gfx_state.tg_cursor_visible = false; if (gfx_state.tg_fb_type == FB_TEXT) { gfx_state.tg_functions = &tf; /* ensure the following are not set for text mode */ unsetenv("screen.height"); unsetenv("screen.width"); unsetenv("screen.depth"); } else { uint32_t fb_height, fb_width; fb_height = gfx_state.tg_fb.fb_height; fb_width = gfx_state.tg_fb.fb_width; /* * setup_font() can adjust terminal size. * We can see two kind of bad happening. * We either can get too small console font - requested * terminal size is large, display resolution is * large, and we get very small font. * Or, we can get too large font - requested * terminal size is small and this will cause large * font to be selected. * Now, the setup_font() is updated to consider * display density and this should give us mostly * acceptable font. However, the catch is, not all * display devices will give us display density. * Still, we do hope, external monitors do - this is * where the display size will matter the most. * And for laptop screens, we should still get good * results by requesting 80x25 terminal. */ gfx_state.tg_tp.tp_row = 25; gfx_state.tg_tp.tp_col = 80; setup_font(&gfx_state, fb_height, fb_width); rows = gfx_state.tg_tp.tp_row; cols = gfx_state.tg_tp.tp_col; /* Point of origin in pixels. */ gfx_state.tg_origin.tp_row = (fb_height - (rows * gfx_state.tg_font.vf_height)) / 2; gfx_state.tg_origin.tp_col = (fb_width - (cols * gfx_state.tg_font.vf_width)) / 2; /* UEFI gop has depth 32. */ gfx_state.tg_glyph_size = gfx_state.tg_font.vf_height * gfx_state.tg_font.vf_width * 4; free(gfx_state.tg_glyph); gfx_state.tg_glyph = malloc(gfx_state.tg_glyph_size); if (gfx_state.tg_glyph == NULL) return (false); gfx_state.tg_functions = &tfx; snprintf(env, sizeof (env), "%d", fb_height); env_setenv("screen.height", EV_VOLATILE | EV_NOHOOK, env, env_noset, env_screen_nounset); snprintf(env, sizeof (env), "%d", fb_width); env_setenv("screen.width", EV_VOLATILE | EV_NOHOOK, env, env_noset, env_screen_nounset); snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_bpp); env_setenv("screen.depth", EV_VOLATILE | EV_NOHOOK, env, env_noset, env_screen_nounset); } /* Record our terminal screen size. */ gfx_state.tg_tp.tp_row = rows; gfx_state.tg_tp.tp_col = cols; teken_init(&gfx_state.tg_teken, gfx_state.tg_functions, &gfx_state); free(screen_buffer); screen_buffer = malloc(rows * cols * sizeof(*screen_buffer)); if (screen_buffer != NULL) { teken_set_winsize(&gfx_state.tg_teken, &gfx_state.tg_tp); a = teken_get_defattr(&gfx_state.tg_teken); attr = *a; /* * On first run, we set up the efi_set_colors() * callback. If the env is already set, we * pick up fg and bg color values from the environment. */ ptr = getenv("teken.fg_color"); if (ptr != NULL) { attr.ta_fgcolor = strtol(ptr, NULL, 10); ptr = getenv("teken.bg_color"); attr.ta_bgcolor = strtol(ptr, NULL, 10); teken_set_defattr(&gfx_state.tg_teken, &attr); } else { snprintf(env, sizeof(env), "%d", attr.ta_fgcolor); env_setenv("teken.fg_color", EV_VOLATILE, env, efi_set_colors, env_nounset); snprintf(env, sizeof(env), "%d", attr.ta_bgcolor); env_setenv("teken.bg_color", EV_VOLATILE, env, efi_set_colors, env_nounset); } } } if (screen_buffer == NULL) { conout->EnableCursor(conout, TRUE); #ifdef TERM_EMU conout->SetAttribute(conout, EFI_TEXT_ATTR(DEFAULT_FGCOLOR, DEFAULT_BGCOLOR)); end_term(); get_pos(&curx, &cury); curs_move(&curx, &cury, curx, cury); fg_c = DEFAULT_FGCOLOR; bg_c = DEFAULT_BGCOLOR; #endif } else { /* Improve visibility */ if (attr.ta_bgcolor == TC_WHITE) attr.ta_bgcolor |= TC_LIGHT; teken_set_defattr(&gfx_state.tg_teken, &attr); /* Draw frame around terminal area. */ cons_draw_frame(&attr); /* * Erase display, this will also fill our screen * buffer. */ teken_input(&gfx_state.tg_teken, "\e[2J", 4); gfx_state.tg_functions->tf_param(&gfx_state, TP_SHOWCURSOR, 1); } snprintf(env, sizeof (env), "%u", (unsigned)rows); setenv("LINES", env, 1); snprintf(env, sizeof (env), "%u", (unsigned)cols); setenv("COLUMNS", env, 1); return (true); } static int efi_cons_init(int arg) { EFI_STATUS status; if (efi_started) return (0); efi_started = true; gfx_framework_init(); if (cons_update_mode(gfx_state.tg_fb_type != FB_TEXT)) return (0); return (1); } static void input_partial(void) { unsigned i; uint32_t c; if (utf8_left == 0) return; for (i = 0; i < sizeof(utf8_partial); i++) { c = (utf8_partial >> (24 - (i << 3))) & 0xff; if (c != 0) efi_term_emu(c); } utf8_left = 0; utf8_partial = 0; } static void input_byte(uint8_t c) { if ((c & 0x80) == 0x00) { /* One-byte sequence. */ input_partial(); efi_term_emu(c); return; } if ((c & 0xe0) == 0xc0) { /* Two-byte sequence. */ input_partial(); utf8_left = 1; utf8_partial = c; return; } if ((c & 0xf0) == 0xe0) { /* Three-byte sequence. */ input_partial(); utf8_left = 2; utf8_partial = c; return; } if ((c & 0xf8) == 0xf0) { /* Four-byte sequence. */ input_partial(); utf8_left = 3; utf8_partial = c; return; } if ((c & 0xc0) == 0x80) { /* Invalid state? */ if (utf8_left == 0) { efi_term_emu(c); return; } utf8_left--; utf8_partial = (utf8_partial << 8) | c; if (utf8_left == 0) { uint32_t v, u; uint8_t b; v = 0; u = utf8_partial; b = (u >> 24) & 0xff; if (b != 0) { /* Four-byte sequence */ v = b & 0x07; b = (u >> 16) & 0xff; v = (v << 6) | (b & 0x3f); b = (u >> 8) & 0xff; v = (v << 6) | (b & 0x3f); b = u & 0xff; v = (v << 6) | (b & 0x3f); } else if ((b = (u >> 16) & 0xff) != 0) { v = b & 0x0f; /* Three-byte sequence */ b = (u >> 8) & 0xff; v = (v << 6) | (b & 0x3f); b = u & 0xff; v = (v << 6) | (b & 0x3f); } else if ((b = (u >> 8) & 0xff) != 0) { v = b & 0x1f; /* Two-byte sequence */ b = u & 0xff; v = (v << 6) | (b & 0x3f); } /* Send unicode char directly to console. */ efi_cons_efiputchar(v); utf8_partial = 0; } return; } /* Anything left is illegal in UTF-8 sequence. */ input_partial(); efi_term_emu(c); } void efi_cons_putchar(int c) { unsigned char ch = c; /* * Don't use Teken when we're doing pure serial, or a multiple console * with video "primary" because that's also serial. */ if ((mode & (RB_SERIAL | RB_MULTIPLE)) != 0 || screen_buffer == NULL) { input_byte(ch); return; } teken_input(&gfx_state.tg_teken, &ch, sizeof (ch)); } static int keybuf_getchar(void) { int i, c = 0; for (i = 0; i < KEYBUFSZ; i++) { if (keybuf[i] != 0) { c = keybuf[i]; keybuf[i] = 0; break; } } return (c); } static bool keybuf_ischar(void) { int i; for (i = 0; i < KEYBUFSZ; i++) { if (keybuf[i] != 0) return (true); } return (false); } /* * We are not reading input before keybuf is empty, so we are safe * just to fill keybuf from the beginning. */ static void keybuf_inschar(EFI_INPUT_KEY *key) { switch (key->ScanCode) { case SCAN_UP: /* UP */ keybuf[0] = 0x1b; /* esc */ keybuf[1] = '['; keybuf[2] = 'A'; break; case SCAN_DOWN: /* DOWN */ keybuf[0] = 0x1b; /* esc */ keybuf[1] = '['; keybuf[2] = 'B'; break; case SCAN_RIGHT: /* RIGHT */ keybuf[0] = 0x1b; /* esc */ keybuf[1] = '['; keybuf[2] = 'C'; break; case SCAN_LEFT: /* LEFT */ keybuf[0] = 0x1b; /* esc */ keybuf[1] = '['; keybuf[2] = 'D'; break; case SCAN_DELETE: keybuf[0] = CHAR_BACKSPACE; break; case SCAN_ESC: keybuf[0] = 0x1b; /* esc */ break; default: keybuf[0] = key->UnicodeChar; break; } } static bool efi_readkey(void) { EFI_STATUS status; EFI_INPUT_KEY key; status = conin->ReadKeyStroke(conin, &key); if (status == EFI_SUCCESS) { keybuf_inschar(&key); return (true); } return (false); } static bool efi_readkey_ex(void) { EFI_STATUS status; EFI_INPUT_KEY *kp; EFI_KEY_DATA key_data; uint32_t kss; status = coninex->ReadKeyStrokeEx(coninex, &key_data); if (status == EFI_SUCCESS) { kss = key_data.KeyState.KeyShiftState; kp = &key_data.Key; if (kss & EFI_SHIFT_STATE_VALID) { /* * quick mapping to control chars, replace with * map lookup later. */ if (kss & EFI_RIGHT_CONTROL_PRESSED || kss & EFI_LEFT_CONTROL_PRESSED) { if (kp->UnicodeChar >= 'a' && kp->UnicodeChar <= 'z') { kp->UnicodeChar -= 'a'; kp->UnicodeChar++; } } } /* * The shift state and/or toggle state may not be valid, * but we still can have ScanCode or UnicodeChar. */ if (kp->ScanCode == 0 && kp->UnicodeChar == 0) return (false); keybuf_inschar(kp); return (true); } return (false); } int efi_cons_getchar(void) { int c; if ((c = keybuf_getchar()) != 0) return (c); key_pending = 0; if (coninex == NULL) { if (efi_readkey()) return (keybuf_getchar()); } else { if (efi_readkey_ex()) return (keybuf_getchar()); } return (-1); } int efi_cons_poll(void) { EFI_STATUS status; if (keybuf_ischar() || key_pending) return (1); /* * Some EFI implementation (u-boot for example) do not support * WaitForKey(). * CheckEvent() can clear the signaled state. */ if (coninex != NULL) { if (coninex->WaitForKeyEx == NULL) { key_pending = efi_readkey_ex(); } else { status = BS->CheckEvent(coninex->WaitForKeyEx); key_pending = status == EFI_SUCCESS; } } else { if (conin->WaitForKey == NULL) { key_pending = efi_readkey(); } else { status = BS->CheckEvent(conin->WaitForKey); key_pending = status == EFI_SUCCESS; } } return (key_pending); } /* Plain direct access to EFI OutputString(). */ void efi_cons_efiputchar(int c) { CHAR16 buf[2]; EFI_STATUS status; buf[0] = c; buf[1] = 0; /* terminate string */ status = conout->TestString(conout, buf); if (EFI_ERROR(status)) buf[0] = '?'; conout->OutputString(conout, buf); } diff --git a/stand/i386/libi386/vidconsole.c b/stand/i386/libi386/vidconsole.c index 4f1f22234dc3..1ec6b4f5393a 100644 --- a/stand/i386/libi386/vidconsole.c +++ b/stand/i386/libi386/vidconsole.c @@ -1,1252 +1,1252 @@ /*- * Copyright (c) 1998 Michael Smith (msmith@freebsd.org) * Copyright (c) 1997 Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Id: probe_keyboard.c,v 1.13 1997/06/09 05:10:55 bde Exp */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include "vbe.h" #include #include "libi386.h" #if KEYBOARD_PROBE static int probe_keyboard(void); #endif static void vidc_probe(struct console *cp); static int vidc_init(int arg); static void vidc_putchar(int c); static int vidc_getchar(void); static int vidc_ischar(void); static void cons_draw_frame(teken_attr_t *); static bool vidc_started; static uint16_t *vgatext; static tf_bell_t vidc_cons_bell; static tf_cursor_t vidc_text_cursor; static tf_putchar_t vidc_text_putchar; static tf_fill_t vidc_text_fill; static tf_copy_t vidc_text_copy; static tf_param_t vidc_text_param; static tf_respond_t vidc_cons_respond; static teken_funcs_t tf = { .tf_bell = vidc_cons_bell, .tf_cursor = vidc_text_cursor, .tf_putchar = vidc_text_putchar, .tf_fill = vidc_text_fill, .tf_copy = vidc_text_copy, .tf_param = vidc_text_param, .tf_respond = vidc_cons_respond, }; static teken_funcs_t tfx = { .tf_bell = vidc_cons_bell, .tf_cursor = gfx_fb_cursor, .tf_putchar = gfx_fb_putchar, .tf_fill = gfx_fb_fill, .tf_copy = gfx_fb_copy, .tf_param = gfx_fb_param, .tf_respond = vidc_cons_respond, }; #define KEYBUFSZ 10 static uint8_t keybuf[KEYBUFSZ]; /* keybuf for extended codes */ struct console vidconsole = { .c_name = "vidconsole", .c_desc = "internal video/keyboard", .c_flags = 0, .c_probe = vidc_probe, .c_init = vidc_init, .c_out = vidc_putchar, .c_in = vidc_getchar, .c_ready = vidc_ischar }; /* * This function is used to mark a rectangular image area so the scrolling * will know we need to copy the data from there. */ void term_image_display(teken_gfx_t *state, const teken_rect_t *r) { teken_pos_t p; int idx; if (screen_buffer == NULL) return; for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++) { for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++) { idx = p.tp_col + p.tp_row * state->tg_tp.tp_col; if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row) return; screen_buffer[idx].a.ta_format |= TF_IMAGE; } } } static void vidc_text_set_cursor(teken_unit_t row, teken_unit_t col, bool visible) { uint16_t addr; uint8_t msl, s, e; msl = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_MAX_SCAN_LINE) & 0x1f; s = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_START) & 0xC0; e = vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_END); if (visible == true) { addr = row * TEXT_COLS + col; vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_HIGH, addr >> 8); vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_LOW, addr & 0xff); e = msl; } else { s |= (1<<5); } vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_START, s); vga_set_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_END, e); } static void vidc_text_get_cursor(teken_unit_t *row, teken_unit_t *col) { uint16_t addr; addr = (vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_HIGH) << 8) + vga_get_crtc(VGA_REG_BASE, VGA_CRTC_CURSOR_LOC_LOW); *row = addr / TEXT_COLS; *col = addr % TEXT_COLS; } /* * Not implemented. */ static void vidc_cons_bell(void *s __unused) { } static void vidc_text_cursor(void *s __unused, const teken_pos_t *p) { teken_unit_t row, col; if (p->tp_col == TEXT_COLS) col = p->tp_col - 1; else col = p->tp_col; if (p->tp_row == TEXT_ROWS) row = p->tp_row - 1; else row = p->tp_row; vidc_text_set_cursor(row, col, true); } /* * Binary searchable table for Unicode to CP437 conversion. */ struct unicp437 { uint16_t unicode_base; uint8_t cp437_base; uint8_t length; }; static const struct unicp437 cp437table[] = { { 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 }, { 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 }, { 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 }, { 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 }, { 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 }, { 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 }, { 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 }, { 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 }, { 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 }, { 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 }, { 0x00bf, 0xa8, 0x00 }, { 0x00c4, 0x8e, 0x01 }, { 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 }, { 0x00c9, 0x90, 0x00 }, { 0x00d1, 0xa5, 0x00 }, { 0x00d6, 0x99, 0x00 }, { 0x00dc, 0x9a, 0x00 }, { 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 }, { 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 }, { 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 }, { 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 }, { 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 }, { 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 }, { 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 }, { 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 }, { 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 }, { 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 }, { 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 }, { 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 }, { 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 }, { 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 }, { 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 }, { 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 }, { 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 }, { 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 }, { 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 }, { 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 }, { 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 }, { 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 }, { 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 }, { 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 }, { 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 }, { 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 }, { 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 }, { 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 }, { 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 }, { 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 }, { 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 }, { 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 }, { 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 }, { 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 }, { 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 }, { 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 }, { 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 }, { 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 }, { 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 }, { 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 }, { 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 }, { 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 }, { 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 }, { 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 }, { 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 }, { 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 }, { 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 }, { 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 }, { 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 }, { 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 }, { 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 }, { 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 }, { 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 }, { 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 }, { 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 }, { 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 }, { 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 }, { 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 }, { 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 }, { 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 }, { 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 }, { 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 }, { 0x25ac, 0x16, 0x00 }, { 0x25b2, 0x1e, 0x00 }, { 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 }, { 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 }, { 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 }, { 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 }, { 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 }, { 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 }, { 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x00 }, { 0x266c, 0x0e, 0x00 } }; static uint8_t vga_get_cp437(teken_char_t c) { int min, mid, max; min = 0; max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1; if (c < cp437table[0].unicode_base || c > cp437table[max].unicode_base + cp437table[max].length) return ('?'); while (max >= min) { mid = (min + max) / 2; if (c < cp437table[mid].unicode_base) max = mid - 1; else if (c > cp437table[mid].unicode_base + cp437table[mid].length) min = mid + 1; else return (c - cp437table[mid].unicode_base + cp437table[mid].cp437_base); } return ('?'); } static void vidc_text_printchar(teken_gfx_t *state, const teken_pos_t *p) { int idx; uint8_t attr; struct text_pixel *px; teken_color_t fg, bg, tmp; struct cgatext { uint8_t ch; uint8_t attr; } *addr; idx = p->tp_col + p->tp_row * state->tg_tp.tp_col; px = &screen_buffer[idx]; fg = teken_256to16(px->a.ta_fgcolor); bg = teken_256to16(px->a.ta_bgcolor); if (px->a.ta_format & TF_BOLD) fg |= TC_LIGHT; if (px->a.ta_format & TF_BLINK) bg |= TC_LIGHT; if (px->a.ta_format & TF_REVERSE) { tmp = fg; fg = bg; bg = tmp; } attr = (cmap[bg & 0xf] << 4) | cmap[fg & 0xf]; addr = (struct cgatext *)vgatext; addr[idx].ch = vga_get_cp437(px->c); addr[idx].attr = attr; } static void vidc_text_putchar(void *s, const teken_pos_t *p, teken_char_t c, const teken_attr_t *a) { teken_gfx_t *state = s; int attr, idx; idx = p->tp_col + p->tp_row * state->tg_tp.tp_col; if (idx >= state->tg_tp.tp_col * state->tg_tp.tp_row) return; screen_buffer[idx].c = c; screen_buffer[idx].a = *a; vidc_text_printchar(state, p); } static void vidc_text_fill(void *arg, const teken_rect_t *r, teken_char_t c, const teken_attr_t *a) { teken_gfx_t *state = arg; teken_pos_t p; teken_unit_t row, col; vidc_text_get_cursor(&row, &col); vidc_text_set_cursor(row, col, false); for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row; p.tp_row++) for (p.tp_col = r->tr_begin.tp_col; p.tp_col < r->tr_end.tp_col; p.tp_col++) vidc_text_putchar(state, &p, c, a); vidc_text_set_cursor(row, col, true); } static void vidc_text_copy(void *ptr, const teken_rect_t *r, const teken_pos_t *p) { teken_gfx_t *state = ptr; int srow, drow; int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */ teken_pos_t d, s; teken_unit_t row, col; /* * Copying is a little tricky. We must make sure we do it in * correct order, to make sure we don't overwrite our own data. */ nrow = r->tr_end.tp_row - r->tr_begin.tp_row; ncol = r->tr_end.tp_col - r->tr_begin.tp_col; vidc_text_get_cursor(&row, &col); vidc_text_set_cursor(row, col, false); if (p->tp_row < r->tr_begin.tp_row) { /* Copy from bottom to top. */ for (y = 0; y < nrow; y++) { d.tp_row = p->tp_row + y; s.tp_row = r->tr_begin.tp_row + y; drow = d.tp_row * state->tg_tp.tp_col; srow = s.tp_row * state->tg_tp.tp_col; for (x = 0; x < ncol; x++) { d.tp_col = p->tp_col + x; s.tp_col = r->tr_begin.tp_col + x; if (!is_same_pixel( &screen_buffer[d.tp_col + drow], &screen_buffer[s.tp_col + srow])) { screen_buffer[d.tp_col + drow] = screen_buffer[s.tp_col + srow]; vidc_text_printchar(state, &d); } } } } else { /* Copy from top to bottom. */ if (p->tp_col < r->tr_begin.tp_col) { /* Copy from right to left. */ for (y = nrow - 1; y >= 0; y--) { d.tp_row = p->tp_row + y; s.tp_row = r->tr_begin.tp_row + y; drow = d.tp_row * state->tg_tp.tp_col; srow = s.tp_row * state->tg_tp.tp_col; for (x = 0; x < ncol; x++) { d.tp_col = p->tp_col + x; s.tp_col = r->tr_begin.tp_col + x; if (!is_same_pixel( &screen_buffer[d.tp_col + drow], &screen_buffer[s.tp_col + srow])) { screen_buffer[d.tp_col + drow] = screen_buffer[s.tp_col + srow]; vidc_text_printchar(state, &d); } } } } else { /* Copy from left to right. */ for (y = nrow - 1; y >= 0; y--) { d.tp_row = p->tp_row + y; s.tp_row = r->tr_begin.tp_row + y; drow = d.tp_row * state->tg_tp.tp_col; srow = s.tp_row * state->tg_tp.tp_col; for (x = ncol - 1; x >= 0; x--) { d.tp_col = p->tp_col + x; s.tp_col = r->tr_begin.tp_col + x; if (!is_same_pixel( &screen_buffer[d.tp_col + drow], &screen_buffer[s.tp_col + srow])) { screen_buffer[d.tp_col + drow] = screen_buffer[s.tp_col + srow]; vidc_text_printchar(state, &d); } } } } } vidc_text_set_cursor(row, col, true); } static void vidc_text_param(void *arg, int cmd, unsigned int value) { teken_gfx_t *state = arg; teken_unit_t row, col; switch (cmd) { case TP_SETLOCALCURSOR: /* * 0 means normal (usually block), 1 means hidden, and * 2 means blinking (always block) for compatibility with * syscons. We don't support any changes except hiding, * so must map 2 to 0. */ value = (value == 1) ? 0 : 1; /* FALLTHROUGH */ case TP_SHOWCURSOR: vidc_text_get_cursor(&row, &col); if (value != 0) { vidc_text_set_cursor(row, col, true); state->tg_cursor_visible = true; } else { vidc_text_set_cursor(row, col, false); state->tg_cursor_visible = false; } break; default: /* Not yet implemented */ break; } } /* * Not implemented. */ static void vidc_cons_respond(void *s __unused, const void *buf __unused, size_t len __unused) { } static void vidc_probe(struct console *cp) { /* look for a keyboard */ #if KEYBOARD_PROBE if (probe_keyboard()) #endif { cp->c_flags |= C_PRESENTIN; } /* XXX for now, always assume we can do BIOS screen output */ cp->c_flags |= C_PRESENTOUT; } static bool color_name_to_teken(const char *name, int *val) { int light = 0; if (strncasecmp(name, "light", 5) == 0) { name += 5; light = TC_LIGHT; } else if (strncasecmp(name, "bright", 6) == 0) { name += 6; light = TC_LIGHT; } if (strcasecmp(name, "black") == 0) { *val = TC_BLACK | light; return (true); } if (strcasecmp(name, "red") == 0) { *val = TC_RED | light; return (true); } if (strcasecmp(name, "green") == 0) { *val = TC_GREEN | light; return (true); } if (strcasecmp(name, "yellow") == 0 || strcasecmp(name, "brown") == 0) { - *val = TC_BROWN | light; + *val = TC_YELLOW | light; return (true); } if (strcasecmp(name, "blue") == 0) { *val = TC_BLUE | light; return (true); } if (strcasecmp(name, "magenta") == 0) { *val = TC_MAGENTA | light; return (true); } if (strcasecmp(name, "cyan") == 0) { *val = TC_CYAN | light; return (true); } if (strcasecmp(name, "white") == 0) { *val = TC_WHITE | light; return (true); } return (false); } static int vidc_set_colors(struct env_var *ev, int flags, const void *value) { int val = 0; char buf[3]; const void *evalue; const teken_attr_t *ap; teken_attr_t a; if (value == NULL) return (CMD_OK); if (color_name_to_teken(value, &val)) { snprintf(buf, sizeof (buf), "%d", val); evalue = buf; } else { char *end; long lval; errno = 0; lval = strtol(value, &end, 0); if (errno != 0 || *end != '\0' || lval < 0 || lval > 15) { printf("Allowed values are either ansi color name or " "number from range [0-15].\n"); return (CMD_OK); } val = (int)lval; evalue = value; } ap = teken_get_defattr(&gfx_state.tg_teken); a = *ap; if (strcmp(ev->ev_name, "teken.fg_color") == 0) { /* is it already set? */ if (ap->ta_fgcolor == val) return (CMD_OK); a.ta_fgcolor = val; } if (strcmp(ev->ev_name, "teken.bg_color") == 0) { /* is it already set? */ if (ap->ta_bgcolor == val) return (CMD_OK); a.ta_bgcolor = val; } /* Improve visibility */ if (a.ta_bgcolor == TC_WHITE) a.ta_bgcolor |= TC_LIGHT; teken_set_defattr(&gfx_state.tg_teken, &a); cons_draw_frame(&a); env_setenv(ev->ev_name, flags | EV_NOHOOK, evalue, NULL, NULL); teken_input(&gfx_state.tg_teken, "\e[2J", 4); return (CMD_OK); } static int env_screen_nounset(struct env_var *ev __unused) { if (gfx_state.tg_fb_type == FB_TEXT) return (0); return (EPERM); } static int vidc_load_palette(void) { int i, roff, goff, boff, rc; if (pe8 == NULL) pe8 = calloc(sizeof(*pe8), NCMAP); if (pe8 == NULL) return (ENOMEM); /* Generate VGA colors */ roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1; goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1; boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1; rc = generate_cons_palette((uint32_t *)pe8, COLOR_FORMAT_RGB, gfx_state.tg_fb.fb_mask_red >> roff, roff, gfx_state.tg_fb.fb_mask_green >> goff, goff, gfx_state.tg_fb.fb_mask_blue >> boff, boff); if (rc == 0) { for (i = 0; i < NCMAP; i++) { int idx; if (i < NCOLORS) idx = cons_to_vga_colors[i]; else idx = i; rc = vbe_set_palette(&pe8[i], idx); if (rc != 0) break; } } return (rc); } static void cons_draw_frame(teken_attr_t *a) { teken_attr_t attr = *a; teken_color_t fg = a->ta_fgcolor; attr.ta_fgcolor = attr.ta_bgcolor; teken_set_defattr(&gfx_state.tg_teken, &attr); gfx_fb_drawrect(0, 0, gfx_state.tg_fb.fb_width, gfx_state.tg_origin.tp_row, 1); gfx_fb_drawrect(0, gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row - 1, gfx_state.tg_fb.fb_width, gfx_state.tg_fb.fb_height, 1); gfx_fb_drawrect(0, gfx_state.tg_origin.tp_row, gfx_state.tg_origin.tp_col, gfx_state.tg_fb.fb_height - gfx_state.tg_origin.tp_row - 1, 1); gfx_fb_drawrect( gfx_state.tg_fb.fb_width - gfx_state.tg_origin.tp_col - 1, gfx_state.tg_origin.tp_row, gfx_state.tg_fb.fb_width, gfx_state.tg_fb.fb_height, 1); attr.ta_fgcolor = fg; teken_set_defattr(&gfx_state.tg_teken, &attr); } /* * Binary searchable table for CP437 to Unicode conversion. */ struct cp437uni { uint8_t cp437_base; uint16_t unicode_base; uint8_t length; }; static const struct cp437uni cp437unitable[] = { { 0, 0x0000, 0 }, { 1, 0x263A, 1 }, { 3, 0x2665, 1 }, { 5, 0x2663, 0 }, { 6, 0x2660, 0 }, { 7, 0x2022, 0 }, { 8, 0x25D8, 0 }, { 9, 0x25CB, 0 }, { 10, 0x25D9, 0 }, { 11, 0x2642, 0 }, { 12, 0x2640, 0 }, { 13, 0x266A, 1 }, { 15, 0x263C, 0 }, { 16, 0x25BA, 0 }, { 17, 0x25C4, 0 }, { 18, 0x2195, 0 }, { 19, 0x203C, 0 }, { 20, 0x00B6, 0 }, { 21, 0x00A7, 0 }, { 22, 0x25AC, 0 }, { 23, 0x21A8, 0 }, { 24, 0x2191, 0 }, { 25, 0x2193, 0 }, { 26, 0x2192, 0 }, { 27, 0x2190, 0 }, { 28, 0x221F, 0 }, { 29, 0x2194, 0 }, { 30, 0x25B2, 0 }, { 31, 0x25BC, 0 }, { 32, 0x0020, 0x5e }, { 127, 0x2302, 0 }, { 128, 0x00C7, 0 }, { 129, 0x00FC, 0 }, { 130, 0x00E9, 0 }, { 131, 0x00E2, 0 }, { 132, 0x00E4, 0 }, { 133, 0x00E0, 0 }, { 134, 0x00E5, 0 }, { 135, 0x00E7, 0 }, { 136, 0x00EA, 1 }, { 138, 0x00E8, 0 }, { 139, 0x00EF, 0 }, { 140, 0x00EE, 0 }, { 141, 0x00EC, 0 }, { 142, 0x00C4, 1 }, { 144, 0x00C9, 0 }, { 145, 0x00E6, 0 }, { 146, 0x00C6, 0 }, { 147, 0x00F4, 0 }, { 148, 0x00F6, 0 }, { 149, 0x00F2, 0 }, { 150, 0x00FB, 0 }, { 151, 0x00F9, 0 }, { 152, 0x00FF, 0 }, { 153, 0x00D6, 0 }, { 154, 0x00DC, 0 }, { 155, 0x00A2, 1 }, { 157, 0x00A5, 0 }, { 158, 0x20A7, 0 }, { 159, 0x0192, 0 }, { 160, 0x00E1, 0 }, { 161, 0x00ED, 0 }, { 162, 0x00F3, 0 }, { 163, 0x00FA, 0 }, { 164, 0x00F1, 0 }, { 165, 0x00D1, 0 }, { 166, 0x00AA, 0 }, { 167, 0x00BA, 0 }, { 168, 0x00BF, 0 }, { 169, 0x2310, 0 }, { 170, 0x00AC, 0 }, { 171, 0x00BD, 0 }, { 172, 0x00BC, 0 }, { 173, 0x00A1, 0 }, { 174, 0x00AB, 0 }, { 175, 0x00BB, 0 }, { 176, 0x2591, 2 }, { 179, 0x2502, 0 }, { 180, 0x2524, 0 }, { 181, 0x2561, 1 }, { 183, 0x2556, 0 }, { 184, 0x2555, 0 }, { 185, 0x2563, 0 }, { 186, 0x2551, 0 }, { 187, 0x2557, 0 }, { 188, 0x255D, 0 }, { 189, 0x255C, 0 }, { 190, 0x255B, 0 }, { 191, 0x2510, 0 }, { 192, 0x2514, 0 }, { 193, 0x2534, 0 }, { 194, 0x252C, 0 }, { 195, 0x251C, 0 }, { 196, 0x2500, 0 }, { 197, 0x253C, 0 }, { 198, 0x255E, 1 }, { 200, 0x255A, 0 }, { 201, 0x2554, 0 }, { 202, 0x2569, 0 }, { 203, 0x2566, 0 }, { 204, 0x2560, 0 }, { 205, 0x2550, 0 }, { 206, 0x256C, 0 }, { 207, 0x2567, 1 }, { 209, 0x2564, 1 }, { 211, 0x2559, 0 }, { 212, 0x2558, 0 }, { 213, 0x2552, 1 }, { 215, 0x256B, 0 }, { 216, 0x256A, 0 }, { 217, 0x2518, 0 }, { 218, 0x250C, 0 }, { 219, 0x2588, 0 }, { 220, 0x2584, 0 }, { 221, 0x258C, 0 }, { 222, 0x2590, 0 }, { 223, 0x2580, 0 }, { 224, 0x03B1, 0 }, { 225, 0x00DF, 0 }, { 226, 0x0393, 0 }, { 227, 0x03C0, 0 }, { 228, 0x03A3, 0 }, { 229, 0x03C3, 0 }, { 230, 0x00B5, 0 }, { 231, 0x03C4, 0 }, { 232, 0x03A6, 0 }, { 233, 0x0398, 0 }, { 234, 0x03A9, 0 }, { 235, 0x03B4, 0 }, { 236, 0x221E, 0 }, { 237, 0x03C6, 0 }, { 238, 0x03B5, 0 }, { 239, 0x2229, 0 }, { 240, 0x2261, 0 }, { 241, 0x00B1, 0 }, { 242, 0x2265, 0 }, { 243, 0x2264, 0 }, { 244, 0x2320, 1 }, { 246, 0x00F7, 0 }, { 247, 0x2248, 0 }, { 248, 0x00B0, 0 }, { 249, 0x2219, 0 }, { 250, 0x00B7, 0 }, { 251, 0x221A, 0 }, { 252, 0x207F, 0 }, { 253, 0x00B2, 0 }, { 254, 0x25A0, 0 }, { 255, 0x00A0, 0 } }; static uint16_t vga_cp437_to_uni(uint8_t c) { int min, mid, max; min = 0; max = (sizeof(cp437unitable) / sizeof(struct cp437uni)) - 1; while (max >= min) { mid = (min + max) / 2; if (c < cp437unitable[mid].cp437_base) max = mid - 1; else if (c > cp437unitable[mid].cp437_base + cp437unitable[mid].length) min = mid + 1; else return (c - cp437unitable[mid].cp437_base + cp437unitable[mid].unicode_base); } return ('?'); } /* * install font for text mode */ static void vidc_install_font(void) { uint8_t reg[7]; const uint8_t *from; uint8_t volatile *to; uint16_t c; int i, j, s; int bpc, f_offset; teken_attr_t a = { 0 }; /* We can only program VGA registers. */ if (!vbe_is_vga()) return; if (gfx_state.tg_fb_type != FB_TEXT) return; /* Sync-reset the sequencer registers */ vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET, VGA_SEQ_RST_NAR); reg[0] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK); reg[1] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE); reg[2] = vga_get_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE); reg[3] = vga_get_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT); reg[4] = vga_get_grc(VGA_REG_BASE, VGA_GC_MODE); reg[5] = vga_get_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS); reg[6] = vga_get_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL); /* Screen off */ vga_set_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE, reg[1] | VGA_SEQ_CM_SO); /* * enable write to plane2, since fonts * could only be loaded into plane2 */ vga_set_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK, VGA_SEQ_MM_EM2); /* * sequentially access data in the bit map being * selected by MapMask register (index 0x02) */ vga_set_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE, 0x07); /* Sync-reset ended, and allow the sequencer to operate */ vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET, VGA_SEQ_RST_SR | VGA_SEQ_RST_NAR); /* * select plane 2 on Read Mode 0 */ vga_set_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT, 0x02); /* * system addresses sequentially access data, follow * Memory Mode register bit 2 in the sequencer */ vga_set_grc(VGA_REG_BASE, VGA_GC_MODE, 0x00); /* * set range of host memory addresses decoded by VGA * hardware -- A0000h-BFFFFh (128K region) */ vga_set_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS, 0x00); /* * This assumes 8x16 characters, which yield the traditional 80x25 * screen. */ bpc = 16; s = 0; /* font slot, maybe should use tunable there. */ f_offset = s * 8 * 1024; for (i = 0; i < 256; i++) { c = vga_cp437_to_uni(i); from = font_lookup(&gfx_state.tg_font, c, &a); to = (unsigned char *)ptov(VGA_MEM_BASE) + f_offset + i * 0x20; for (j = 0; j < bpc; j++) *to++ = *from++; } vga_set_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL, reg[6]); /* Sync-reset the sequencer registers */ vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET, VGA_SEQ_RST_NAR); vga_set_seq(VGA_REG_BASE, VGA_SEQ_MAP_MASK, reg[0]); vga_set_seq(VGA_REG_BASE, VGA_SEQ_MEMORY_MODE, reg[2]); /* Sync-reset ended, and allow the sequencer to operate */ vga_set_seq(VGA_REG_BASE, VGA_SEQ_RESET, VGA_SEQ_RST_SR | VGA_SEQ_RST_NAR); /* restore graphic registers */ vga_set_grc(VGA_REG_BASE, VGA_GC_READ_MAP_SELECT, reg[3]); vga_set_grc(VGA_REG_BASE, VGA_GC_MODE, reg[4]); vga_set_grc(VGA_REG_BASE, VGA_GC_MISCELLANEOUS, (reg[5] & 0x03) | 0x0c); /* Screen on */ vga_set_seq(VGA_REG_BASE, VGA_SEQ_CLOCKING_MODE, reg[1] & 0xdf); } bool cons_update_mode(bool use_gfx_mode) { const teken_attr_t *a; teken_attr_t attr; char env[10], *ptr; int format, roff, goff, boff; /* vidc_init() is not called yet. */ if (!vidc_started) return (false); gfx_state.tg_tp.tp_row = TEXT_ROWS; gfx_state.tg_tp.tp_col = TEXT_COLS; if (use_gfx_mode) { setup_font(&gfx_state, gfx_state.tg_fb.fb_height, gfx_state.tg_fb.fb_width); /* Point of origin in pixels. */ gfx_state.tg_origin.tp_row = (gfx_state.tg_fb.fb_height - (gfx_state.tg_tp.tp_row * gfx_state.tg_font.vf_height)) / 2; gfx_state.tg_origin.tp_col = (gfx_state.tg_fb.fb_width - (gfx_state.tg_tp.tp_col * gfx_state.tg_font.vf_width)) / 2; gfx_state.tg_glyph_size = gfx_state.tg_font.vf_height * gfx_state.tg_font.vf_width * 4; free(gfx_state.tg_glyph); gfx_state.tg_glyph = malloc(gfx_state.tg_glyph_size); if (gfx_state.tg_glyph == NULL) return (false); gfx_state.tg_functions = &tfx; snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_height); env_setenv("screen.height", EV_VOLATILE | EV_NOHOOK, env, env_noset, env_screen_nounset); snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_width); env_setenv("screen.width", EV_VOLATILE | EV_NOHOOK, env, env_noset, env_screen_nounset); snprintf(env, sizeof (env), "%d", gfx_state.tg_fb.fb_bpp); env_setenv("screen.depth", EV_VOLATILE | EV_NOHOOK, env, env_noset, env_screen_nounset); } else { /* Trigger loading of 8x16 font. */ setup_font(&gfx_state, 16 * gfx_state.tg_fb.fb_height, 8 * gfx_state.tg_fb.fb_width); gfx_state.tg_functions = &tf; /* ensure the following are not set for text mode */ unsetenv("screen.height"); unsetenv("screen.width"); unsetenv("screen.depth"); vidc_install_font(); } free(screen_buffer); screen_buffer = malloc(gfx_state.tg_tp.tp_row * gfx_state.tg_tp.tp_col * sizeof(*screen_buffer)); if (screen_buffer == NULL) return (false); teken_init(&gfx_state.tg_teken, gfx_state.tg_functions, &gfx_state); if (gfx_state.tg_ctype == CT_INDEXED) format = COLOR_FORMAT_VGA; else format = COLOR_FORMAT_RGB; roff = ffs(gfx_state.tg_fb.fb_mask_red) - 1; goff = ffs(gfx_state.tg_fb.fb_mask_green) - 1; boff = ffs(gfx_state.tg_fb.fb_mask_blue) - 1; (void) generate_cons_palette(cmap, format, gfx_state.tg_fb.fb_mask_red >> roff, roff, gfx_state.tg_fb.fb_mask_green >> goff, goff, gfx_state.tg_fb.fb_mask_blue >> boff, boff); if (gfx_state.tg_ctype == CT_INDEXED && use_gfx_mode) vidc_load_palette(); teken_set_winsize(&gfx_state.tg_teken, &gfx_state.tg_tp); a = teken_get_defattr(&gfx_state.tg_teken); attr = *a; /* * On first run, we set up the vidc_set_colors() * callback. If the env is already set, we * pick up fg and bg color values from the environment. */ ptr = getenv("teken.fg_color"); if (ptr != NULL) { attr.ta_fgcolor = strtol(ptr, NULL, 10); ptr = getenv("teken.bg_color"); attr.ta_bgcolor = strtol(ptr, NULL, 10); teken_set_defattr(&gfx_state.tg_teken, &attr); } else { snprintf(env, sizeof(env), "%d", attr.ta_fgcolor); env_setenv("teken.fg_color", EV_VOLATILE, env, vidc_set_colors, env_nounset); snprintf(env, sizeof(env), "%d", attr.ta_bgcolor); env_setenv("teken.bg_color", EV_VOLATILE, env, vidc_set_colors, env_nounset); } /* Improve visibility */ if (attr.ta_bgcolor == TC_WHITE) attr.ta_bgcolor |= TC_LIGHT; teken_set_defattr(&gfx_state.tg_teken, &attr); snprintf(env, sizeof (env), "%u", (unsigned)gfx_state.tg_tp.tp_row); setenv("LINES", env, 1); snprintf(env, sizeof (env), "%u", (unsigned)gfx_state.tg_tp.tp_col); setenv("COLUMNS", env, 1); /* Draw frame around terminal area. */ cons_draw_frame(&attr); /* Erase display, this will also fill our screen buffer. */ teken_input(&gfx_state.tg_teken, "\e[2J", 4); gfx_state.tg_functions->tf_param(&gfx_state, TP_SHOWCURSOR, 1); return (true); } static int vidc_init(int arg) { const teken_attr_t *a; int val; char env[8]; if (vidc_started && arg == 0) return (0); vidc_started = true; vbe_init(); /* * Check Miscellaneous Output Register (Read at 3CCh, Write at 3C2h) * for bit 1 (Input/Output Address Select), which means * color/graphics adapter. */ if (vga_get_reg(VGA_REG_BASE, VGA_GEN_MISC_OUTPUT_R) & VGA_GEN_MO_IOA) vgatext = (uint16_t *)PTOV(VGA_TXT_BASE); else vgatext = (uint16_t *)PTOV(VGA_MEM_BASE + VGA_MEM_SIZE); /* set 16bit colors */ val = vga_get_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL); val &= ~VGA_AC_MC_BI; val &= ~VGA_AC_MC_ELG; vga_set_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL, val); #if defined(FRAMEBUFFER_MODE) val = vbe_default_mode(); /* if val is not legal VBE mode, use text mode */ if (VBE_VALID_MODE(val)) { if (vbe_set_mode(val) != 0) bios_set_text_mode(VGA_TEXT_MODE); } #endif gfx_framework_init(); if (!cons_update_mode(VBE_VALID_MODE(vbe_get_mode()))) return (1); for (int i = 0; i < 10 && vidc_ischar(); i++) (void) vidc_getchar(); return (0); /* XXX reinit? */ } void vidc_biosputchar(int c) { v86.ctl = 0; v86.addr = 0x10; v86.eax = 0xe00 | (c & 0xff); v86.ebx = 0x7; v86int(); } static void vidc_putchar(int c) { unsigned char ch = c; if (screen_buffer != NULL) teken_input(&gfx_state.tg_teken, &ch, sizeof (ch)); else vidc_biosputchar(c); } static int vidc_getchar(void) { int i, c; for (i = 0; i < KEYBUFSZ; i++) { if (keybuf[i] != 0) { c = keybuf[i]; keybuf[i] = 0; return (c); } } if (vidc_ischar()) { v86.ctl = 0; v86.addr = 0x16; v86.eax = 0x0; v86int(); if ((v86.eax & 0xff) != 0) { return (v86.eax & 0xff); } /* extended keys */ switch (v86.eax & 0xff00) { case 0x4800: /* up */ keybuf[0] = '['; keybuf[1] = 'A'; return (0x1b); /* esc */ case 0x4b00: /* left */ keybuf[0] = '['; keybuf[1] = 'D'; return (0x1b); /* esc */ case 0x4d00: /* right */ keybuf[0] = '['; keybuf[1] = 'C'; return (0x1b); /* esc */ case 0x5000: /* down */ keybuf[0] = '['; keybuf[1] = 'B'; return (0x1b); /* esc */ default: return (-1); } } else { return (-1); } } static int vidc_ischar(void) { int i; for (i = 0; i < KEYBUFSZ; i++) { if (keybuf[i] != 0) { return (1); } } v86.ctl = V86_FLAGS; v86.addr = 0x16; v86.eax = 0x100; v86int(); return (!V86_ZR(v86.efl)); } #if KEYBOARD_PROBE #define PROBE_MAXRETRY 5 #define PROBE_MAXWAIT 400 #define IO_DUMMY 0x84 #define IO_KBD 0x060 /* 8042 Keyboard */ /* selected defines from kbdio.h */ #define KBD_STATUS_PORT 4 /* status port, read */ #define KBD_DATA_PORT 0 /* data port, read/write * also used as keyboard command * and mouse command port */ #define KBDC_ECHO 0x00ee #define KBDS_ANY_BUFFER_FULL 0x0001 #define KBDS_INPUT_BUFFER_FULL 0x0002 #define KBD_ECHO 0x00ee /* 7 microsec delay necessary for some keyboard controllers */ static void delay7(void) { /* * I know this is broken, but no timer is available yet at this stage... * See also comments in `delay1ms()'. */ inb(IO_DUMMY); inb(IO_DUMMY); inb(IO_DUMMY); inb(IO_DUMMY); inb(IO_DUMMY); inb(IO_DUMMY); } /* * This routine uses an inb to an unused port, the time to execute that * inb is approximately 1.25uS. This value is pretty constant across * all CPU's and all buses, with the exception of some PCI implentations * that do not forward this I/O address to the ISA bus as they know it * is not a valid ISA bus address, those machines execute this inb in * 60 nS :-(. * */ static void delay1ms(void) { int i = 800; while (--i >= 0) (void) inb(0x84); } /* * We use the presence/absence of a keyboard to determine whether the internal * console can be used for input. * * Perform a simple test on the keyboard; issue the ECHO command and see * if the right answer is returned. We don't do anything as drastic as * full keyboard reset; it will be too troublesome and take too much time. */ static int probe_keyboard(void) { int retry = PROBE_MAXRETRY; int wait; int i; while (--retry >= 0) { /* flush any noise */ while (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) { delay7(); inb(IO_KBD + KBD_DATA_PORT); delay1ms(); } /* wait until the controller can accept a command */ for (wait = PROBE_MAXWAIT; wait > 0; --wait) { if (((i = inb(IO_KBD + KBD_STATUS_PORT)) & (KBDS_INPUT_BUFFER_FULL | KBDS_ANY_BUFFER_FULL)) == 0) break; if (i & KBDS_ANY_BUFFER_FULL) { delay7(); inb(IO_KBD + KBD_DATA_PORT); } delay1ms(); } if (wait <= 0) continue; /* send the ECHO command */ outb(IO_KBD + KBD_DATA_PORT, KBDC_ECHO); /* wait for a response */ for (wait = PROBE_MAXWAIT; wait > 0; --wait) { if (inb(IO_KBD + KBD_STATUS_PORT) & KBDS_ANY_BUFFER_FULL) break; delay1ms(); } if (wait <= 0) continue; delay7(); i = inb(IO_KBD + KBD_DATA_PORT); #ifdef PROBE_KBD_BEBUG printf("probe_keyboard: got 0x%x.\n", i); #endif if (i == KBD_ECHO) { /* got the right answer */ return (1); } } return (0); } #endif /* KEYBOARD_PROBE */ diff --git a/sys/dev/syscons/scterm-teken.c b/sys/dev/syscons/scterm-teken.c index b575b4ec3119..ed287a74b8f9 100644 --- a/sys/dev/syscons/scterm-teken.c +++ b/sys/dev/syscons/scterm-teken.c @@ -1,765 +1,765 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 1999 Kazutaka YOKOTA * All rights reserved. * * Copyright (c) 2008-2009 Ed Schouten * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer as * the first lines of this file unmodified. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_syscons.h" #include "opt_teken.h" #include #include #include #include #include #include #if defined(__arm__) || defined(__mips__) || defined(__powerpc__) #include #else #include #endif #include #include static void scteken_sc_to_te_attr(unsigned char, teken_attr_t *); static int scteken_te_to_sc_attr(const teken_attr_t *); static sc_term_init_t scteken_init; static sc_term_term_t scteken_term; static sc_term_puts_t scteken_puts; static sc_term_ioctl_t scteken_ioctl; static sc_term_default_attr_t scteken_default_attr; static sc_term_clear_t scteken_clear; static sc_term_input_t scteken_input; static sc_term_fkeystr_t scteken_fkeystr; static sc_term_sync_t scteken_sync; static void scteken_nop(void); typedef struct { teken_t ts_teken; int ts_busy; } teken_stat; static teken_stat reserved_teken_stat; static void scteken_sync_internal(scr_stat *, teken_stat *); static sc_term_sw_t sc_term_scteken = { { NULL, NULL }, "scteken", /* emulator name */ "teken terminal", /* description */ "*", /* matching renderer */ sizeof(teken_stat), /* softc size */ 0, scteken_init, scteken_term, scteken_puts, scteken_ioctl, (sc_term_reset_t *)scteken_nop, scteken_default_attr, scteken_clear, (sc_term_notify_t *)scteken_nop, scteken_input, scteken_fkeystr, scteken_sync, }; SCTERM_MODULE(scteken, sc_term_scteken); static tf_bell_t scteken_bell; static tf_cursor_t scteken_cursor; static tf_putchar_t scteken_putchar; static tf_fill_t scteken_fill; static tf_copy_t scteken_copy; static tf_param_t scteken_param; static tf_respond_t scteken_respond; static const teken_funcs_t scteken_funcs = { .tf_bell = scteken_bell, .tf_cursor = scteken_cursor, .tf_putchar = scteken_putchar, .tf_fill = scteken_fill, .tf_copy = scteken_copy, .tf_param = scteken_param, .tf_respond = scteken_respond, }; static int scteken_init(scr_stat *scp, void **softc, int code) { teken_stat *ts; teken_attr_t ta; if (*softc == NULL) { if (reserved_teken_stat.ts_busy) return (EINVAL); *softc = &reserved_teken_stat; } ts = *softc; switch (code) { case SC_TE_COLD_INIT: ++sc_term_scteken.te_refcount; ts->ts_busy = 1; /* FALLTHROUGH */ case SC_TE_WARM_INIT: ta = *teken_get_defattr(&ts->ts_teken); teken_init(&ts->ts_teken, &scteken_funcs, scp); teken_set_defattr(&ts->ts_teken, &ta); #ifndef TEKEN_UTF8 teken_set_8bit(&ts->ts_teken); #endif /* !TEKEN_UTF8 */ #ifdef TEKEN_CONS25 teken_set_cons25(&ts->ts_teken); #endif /* TEKEN_CONS25 */ teken_set_cons25keys(&ts->ts_teken); scteken_sync_internal(scp, ts); break; } return (0); } static int scteken_term(scr_stat *scp, void **softc) { if (*softc == &reserved_teken_stat) { *softc = NULL; reserved_teken_stat.ts_busy = 0; } --sc_term_scteken.te_refcount; return (0); } static void scteken_puts(scr_stat *scp, u_char *buf, int len) { teken_stat *ts = scp->ts; scp->sc->write_in_progress++; teken_input(&ts->ts_teken, buf, len); scp->sc->write_in_progress--; } static int scteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data, struct thread *td) { teken_stat *ts = scp->ts; vid_info_t *vi; int attr; switch (cmd) { case GIO_ATTR: /* get current attributes */ *(int*)data = scteken_te_to_sc_attr(teken_get_curattr(&ts->ts_teken)); return (0); case CONS_GETINFO: /* get current (virtual) console info */ vi = (vid_info_t *)data; if (vi->size != sizeof(struct vid_info)) return EINVAL; attr = scteken_te_to_sc_attr(teken_get_defattr(&ts->ts_teken)); vi->mv_norm.fore = attr & 0x0f; vi->mv_norm.back = (attr >> 4) & 0x0f; vi->mv_rev.fore = vi->mv_norm.back; vi->mv_rev.back = vi->mv_norm.fore; /* * The other fields are filled by the upper routine. XXX */ return (ENOIOCTL); } return (ENOIOCTL); } static void scteken_default_attr(scr_stat *scp, int color, int rev_color) { teken_stat *ts = scp->ts; teken_attr_t ta; scteken_sc_to_te_attr(color, &ta); teken_set_defattr(&ts->ts_teken, &ta); } static void scteken_clear(scr_stat *scp) { teken_stat *ts = scp->ts; sc_move_cursor(scp, 0, 0); scteken_sync_internal(scp, ts); sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], scteken_te_to_sc_attr(teken_get_curattr(&ts->ts_teken)) << 8); mark_all(scp); } static int scteken_input(scr_stat *scp, int c, struct tty *tp) { return FALSE; } static const char * scteken_fkeystr(scr_stat *scp, int c) { teken_stat *ts = scp->ts; unsigned int k; switch (c) { case FKEY | F(1): case FKEY | F(2): case FKEY | F(3): case FKEY | F(4): case FKEY | F(5): case FKEY | F(6): case FKEY | F(7): case FKEY | F(8): case FKEY | F(9): case FKEY | F(10): case FKEY | F(11): case FKEY | F(12): k = TKEY_F1 + c - (FKEY | F(1)); break; case FKEY | F(49): k = TKEY_HOME; break; case FKEY | F(50): k = TKEY_UP; break; case FKEY | F(51): k = TKEY_PAGE_UP; break; case FKEY | F(53): k = TKEY_LEFT; break; case FKEY | F(55): k = TKEY_RIGHT; break; case FKEY | F(57): k = TKEY_END; break; case FKEY | F(58): k = TKEY_DOWN; break; case FKEY | F(59): k = TKEY_PAGE_DOWN; break; case FKEY | F(60): k = TKEY_INSERT; break; case FKEY | F(61): k = TKEY_DELETE; break; default: return (NULL); } return (teken_get_sequence(&ts->ts_teken, k)); } static void scteken_sync_internal(scr_stat *scp, teken_stat *ts) { teken_pos_t tp; tp.tp_col = scp->xsize; tp.tp_row = scp->ysize; teken_set_winsize_noreset(&ts->ts_teken, &tp); tp.tp_col = scp->xpos; tp.tp_row = scp->ypos; teken_set_cursor(&ts->ts_teken, &tp); } static void scteken_sync(scr_stat *scp) { scteken_sync_internal(scp, scp->ts); } static void scteken_nop(void) { } /* * libteken routines. */ static const teken_color_t sc_to_te_color[] = { TC_BLACK, TC_BLUE, TC_GREEN, TC_CYAN, - TC_RED, TC_MAGENTA, TC_BROWN, TC_WHITE, + TC_RED, TC_MAGENTA, TC_YELLOW, TC_WHITE, }; static const unsigned char te_to_sc_color[] = { FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE, FG_MAGENTA, FG_CYAN, FG_LIGHTGREY, }; static void scteken_sc_to_te_attr(unsigned char color, teken_attr_t *a) { /* * Conversions of attrs are not reversible. Since sc attrs are * pure colors in the simplest mode (16-color graphics) and the * API is too deficient to tell us the mode, always convert to * pure colors. The conversion is essentially the identity except * for reordering the non-brightness bits in the 2 color numbers. */ a->ta_format = 0; a->ta_fgcolor = sc_to_te_color[color & 7] | (color & 8); a->ta_bgcolor = sc_to_te_color[(color >> 4) & 7] | ((color >> 4) & 8); } static int scteken_te_to_sc_attr(const teken_attr_t *a) { int attr; teken_color_t fg, bg; if (a->ta_format & TF_REVERSE) { fg = a->ta_bgcolor; bg = a->ta_fgcolor; } else { fg = a->ta_fgcolor; bg = a->ta_bgcolor; } if (fg >= 16) fg = teken_256to16(fg); if (bg >= 16) bg = teken_256to16(bg); attr = te_to_sc_color[fg & 7] | (fg & 8) | ((te_to_sc_color[bg & 7] | (bg & 8)) << 4); /* XXX: underline mapping for Hercules adapter can be better. */ if (a->ta_format & (TF_BOLD | TF_UNDERLINE)) attr ^= 8; if (a->ta_format & TF_BLINK) attr ^= 0x80; return (attr); } static void scteken_bell(void *arg) { scr_stat *scp = arg; sc_bell(scp, scp->bell_pitch, scp->bell_duration); } static void scteken_cursor(void *arg, const teken_pos_t *p) { scr_stat *scp = arg; sc_move_cursor(scp, p->tp_col, p->tp_row); } #ifdef TEKEN_UTF8 struct unicp437 { uint16_t unicode_base; uint8_t cp437_base; uint8_t length; }; static const struct unicp437 cp437table[] = { { 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 }, { 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 }, { 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 }, { 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 }, { 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 }, { 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 }, { 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 }, { 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 }, { 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 }, { 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 }, { 0x00bf, 0xa8, 0x00 }, { 0x00c0, 0x41, 0x00 }, { 0x00c1, 0x41, 0x00 }, { 0x00c2, 0x41, 0x00 }, { 0x00c4, 0x8e, 0x01 }, { 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 }, { 0x00c8, 0x45, 0x00 }, { 0x00c9, 0x90, 0x00 }, { 0x00ca, 0x45, 0x00 }, { 0x00cb, 0x45, 0x00 }, { 0x00cc, 0x49, 0x00 }, { 0x00cd, 0x49, 0x00 }, { 0x00ce, 0x49, 0x00 }, { 0x00cf, 0x49, 0x00 }, { 0x00d1, 0xa5, 0x00 }, { 0x00d2, 0x4f, 0x00 }, { 0x00d3, 0x4f, 0x00 }, { 0x00d4, 0x4f, 0x00 }, { 0x00d6, 0x99, 0x00 }, { 0x00d9, 0x55, 0x00 }, { 0x00da, 0x55, 0x00 }, { 0x00db, 0x55, 0x00 }, { 0x00dc, 0x9a, 0x00 }, { 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 }, { 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 }, { 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 }, { 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 }, { 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 }, { 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 }, { 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 }, { 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 }, { 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 }, { 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 }, { 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 }, { 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 }, { 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 }, { 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 }, { 0x013f, 0x4c, 0x00 }, { 0x0140, 0x6c, 0x00 }, { 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 }, { 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 }, { 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 }, { 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 }, { 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 }, { 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 }, { 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 }, { 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 }, { 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 }, { 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 }, { 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 }, { 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 }, { 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 }, { 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 }, { 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 }, { 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 }, { 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 }, { 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 }, { 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 }, { 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 }, { 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 }, { 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 }, { 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 }, { 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 }, { 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 }, { 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 }, { 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 }, { 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 }, { 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 }, { 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 }, { 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 }, { 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 }, { 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 }, { 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 }, { 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 }, { 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 }, { 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 }, { 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 }, { 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 }, { 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 }, { 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 }, { 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 }, { 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 }, { 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 }, { 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 }, { 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 }, { 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 }, { 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 }, { 0x25ac, 0x16, 0x00 }, { 0x25ae, 0xdb, 0x00 }, { 0x25b2, 0x1e, 0x00 }, { 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 }, { 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 }, { 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 }, { 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 }, { 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 }, { 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 }, { 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x01 }, }; static void scteken_get_cp437(teken_char_t *c, int *attr) { int min, mid, max; min = 0; max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1; if (*c < cp437table[0].unicode_base || *c > cp437table[max].unicode_base + cp437table[max].length) goto bad; while (max >= min) { mid = (min + max) / 2; if (*c < cp437table[mid].unicode_base) { max = mid - 1; } else if (*c > cp437table[mid].unicode_base + cp437table[mid].length) { min = mid + 1; } else { *c -= cp437table[mid].unicode_base; *c += cp437table[mid].cp437_base; return; } } bad: /* Character not present in CP437. */ *attr = (FG_RED|BG_BLACK) << 8; *c = '?'; } #endif /* TEKEN_UTF8 */ static void scteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c, const teken_attr_t *a) { scr_stat *scp = arg; u_char *map; u_char ch; vm_offset_t p; int cursor, attr; /* * No support for printing right hand sides for CJK fullwidth * characters. Simply print a space and assume that the left * hand side describes the entire character. */ attr = scteken_te_to_sc_attr(a) << 8; if (a->ta_format & TF_CJK_RIGHT) c = ' '; #ifdef TEKEN_UTF8 scteken_get_cp437(&c, &attr); #endif /* TEKEN_UTF8 */ ch = c; map = scp->sc->scr_map; cursor = tp->tp_row * scp->xsize + tp->tp_col; p = sc_vtb_pointer(&scp->vtb, cursor); sc_vtb_putchar(&scp->vtb, p, map[ch], attr); mark_for_update(scp, cursor); /* * XXX: Why do we need this? Only marking `cursor' should be * enough. Without this line, we get artifacts. */ mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1)); } static void scteken_fill(void *arg, const teken_rect_t *r, teken_char_t c, const teken_attr_t *a) { scr_stat *scp = arg; u_char *map; u_char ch; unsigned int width; int attr, row; attr = scteken_te_to_sc_attr(a) << 8; #ifdef TEKEN_UTF8 scteken_get_cp437(&c, &attr); #endif /* TEKEN_UTF8 */ ch = c; map = scp->sc->scr_map; if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) { /* Single contiguous region to fill. */ sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize, (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize, map[ch], attr); } else { /* Fill display line by line. */ width = r->tr_end.tp_col - r->tr_begin.tp_col; for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) { sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col, width, map[ch], attr); } } /* Mark begin and end positions to be refreshed. */ mark_for_update(scp, r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col); mark_for_update(scp, (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1)); sc_remove_cutmarking(scp); } static void scteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p) { scr_stat *scp = arg; unsigned int width; int src, dst, end; #ifndef SC_NO_HISTORY /* * We count a line of input as history if we perform a copy of * one whole line upward. In other words: if a line of text gets * overwritten by a rectangle that's right below it. */ if (scp->history != NULL && r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize && r->tr_begin.tp_row == p->tp_row + 1) { sc_hist_save_one_line(scp, p->tp_row); } #endif if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) { /* Single contiguous region to copy. */ sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize, p->tp_row * scp->xsize, (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize); } else { /* Copy line by line. */ width = r->tr_end.tp_col - r->tr_begin.tp_col; if (p->tp_row < r->tr_begin.tp_row) { /* Copy from top to bottom. */ src = r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col; end = r->tr_end.tp_row * scp->xsize + r->tr_end.tp_col; dst = p->tp_row * scp->xsize + p->tp_col; while (src < end) { sc_vtb_move(&scp->vtb, src, dst, width); src += scp->xsize; dst += scp->xsize; } } else { /* Copy from bottom to top. */ src = (r->tr_end.tp_row - 1) * scp->xsize + r->tr_begin.tp_col; end = r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col; dst = (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col; while (src >= end) { sc_vtb_move(&scp->vtb, src, dst, width); src -= scp->xsize; dst -= scp->xsize; } } } /* Mark begin and end positions to be refreshed. */ mark_for_update(scp, p->tp_row * scp->xsize + p->tp_col); mark_for_update(scp, (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) * scp->xsize + (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1)); sc_remove_cutmarking(scp); } static void scteken_param(void *arg, int cmd, unsigned int value) { static int cattrs[] = { 0, /* block */ CONS_BLINK_CURSOR, /* blinking block */ CONS_CHAR_CURSOR, /* underline */ CONS_CHAR_CURSOR | CONS_BLINK_CURSOR, /* blinking underline */ CONS_RESET_CURSOR, /* reset to default */ CONS_HIDDEN_CURSOR, /* hide cursor */ }; static int tcattrs[] = { CONS_RESET_CURSOR | CONS_LOCAL_CURSOR, /* normal */ CONS_HIDDEN_CURSOR | CONS_LOCAL_CURSOR, /* invisible */ }; scr_stat *scp = arg; int flags, n, v0, v1, v2; switch (cmd) { case TP_SETBORDER: scp->border = value & 0xff; if (scp == scp->sc->cur_scp) sc_set_border(scp, scp->border); break; case TP_SETGLOBALCURSOR: n = value & 0xff; v0 = (value >> 8) & 0xff; v1 = (value >> 16) & 0xff; v2 = (value >> 24) & 0xff; switch (n) { case 1: /* flags only */ if (v0 < sizeof(cattrs) / sizeof(cattrs[0])) v0 = cattrs[v0]; else /* backward compatibility */ v0 = cattrs[v0 & 0x3]; sc_change_cursor_shape(scp, v0, -1, -1); break; case 2: v2 = 0; v0 &= 0x1f; /* backward compatibility */ v1 &= 0x1f; /* FALL THROUGH */ case 3: /* base and height */ if (v2 == 0) /* count from top */ sc_change_cursor_shape(scp, -1, scp->font_size - v1 - 1, v1 - v0 + 1); else if (v2 == 1) /* count from bottom */ sc_change_cursor_shape(scp, -1, v0, v1 - v0 + 1); break; } break; case TP_SETLOCALCURSOR: if (value < sizeof(tcattrs) / sizeof(tcattrs[0])) sc_change_cursor_shape(scp, tcattrs[value], -1, -1); else if (value == 2) { sc_change_cursor_shape(scp, CONS_RESET_CURSOR | CONS_LOCAL_CURSOR, -1, -1); flags = scp->base_curs_attr.flags ^ CONS_BLINK_CURSOR; sc_change_cursor_shape(scp, flags | CONS_LOCAL_CURSOR, -1, -1); } break; case TP_SHOWCURSOR: if (value != 0) flags = scp->base_curs_attr.flags & ~CONS_HIDDEN_CURSOR; else flags = scp->base_curs_attr.flags | CONS_HIDDEN_CURSOR; sc_change_cursor_shape(scp, flags | CONS_LOCAL_CURSOR, -1, -1); break; case TP_SWITCHVT: sc_switch_scr(scp->sc, value); break; case TP_SETBELLPD: scp->bell_pitch = TP_SETBELLPD_PITCH(value); scp->bell_duration = TP_SETBELLPD_DURATION(value); break; case TP_MOUSE: scp->mouse_level = value; break; } } static void scteken_respond(void *arg, const void *buf, size_t len) { scr_stat *scp = arg; sc_respond(scp, buf, len, 0); } diff --git a/sys/sys/terminal.h b/sys/sys/terminal.h index a77c985e85ca..7a6ee7530294 100644 --- a/sys/sys/terminal.h +++ b/sys/sys/terminal.h @@ -1,239 +1,239 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2009 The FreeBSD Foundation * * This software was developed by Ed Schouten under sponsorship from the * FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _SYS_TERMINAL_H_ #define _SYS_TERMINAL_H_ #include #include #include #include #include #include #include #include "opt_syscons.h" #include "opt_teken.h" struct terminal; struct thread; struct tty; /* * The terminal layer is an abstraction on top of the TTY layer and the * console interface. It can be used by system console drivers to * easily interact with the kernel console and TTYs. * * Terminals contain terminal emulators, which means console drivers * don't need to implement their own terminal emulator. The terminal * emulator deals with UTF-8 exclusively. This means that term_char_t, * the data type used to store input/output characters will always * contain Unicode codepoints. * * To save memory usage, the top bits of term_char_t will contain other * attributes, like colors. Right now term_char_t is composed as * follows: * * Bits Meaning * 0-20: Character value * 21-25: Bold, underline, blink, reverse, right part of CJK fullwidth character * 26-28: Foreground color * 29-31: Background color */ typedef uint32_t term_char_t; #define TCHAR_CHARACTER(c) ((c) & 0x1fffff) #define TCHAR_FORMAT(c) (((c) >> 21) & 0x1f) #define TCHAR_FGCOLOR(c) (((c) >> 26) & 0x7) #define TCHAR_BGCOLOR(c) (((c) >> 29) & 0x7) typedef teken_attr_t term_attr_t; typedef teken_color_t term_color_t; #define TCOLOR_FG(c) (((c) & 0x7) << 26) #define TCOLOR_BG(c) (((c) & 0x7) << 29) #define TCOLOR_LIGHT(c) ((c) | 0x8) #define TCOLOR_DARK(c) ((c) & ~0x8) #define TFORMAT(c) (((c) & 0x1f) << 21) /* syscons(4) compatible color attributes for foreground text */ #define FG_BLACK TCOLOR_FG(TC_BLACK) #define FG_BLUE TCOLOR_FG(TC_BLUE) #define FG_GREEN TCOLOR_FG(TC_GREEN) #define FG_CYAN TCOLOR_FG(TC_CYAN) #define FG_RED TCOLOR_FG(TC_RED) #define FG_MAGENTA TCOLOR_FG(TC_MAGENTA) -#define FG_BROWN TCOLOR_FG(TC_BROWN) +#define FG_BROWN TCOLOR_FG(TC_YELLOW) #define FG_LIGHTGREY TCOLOR_FG(TC_WHITE) #define FG_DARKGREY (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_BLACK)) #define FG_LIGHTBLUE (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_BLUE)) #define FG_LIGHTGREEN (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_GREEN)) #define FG_LIGHTCYAN (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_CYAN)) #define FG_LIGHTRED (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_RED)) #define FG_LIGHTMAGENTA (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_MAGENTA)) -#define FG_YELLOW (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_BROWN)) +#define FG_YELLOW (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_YELLOW)) #define FG_WHITE (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_WHITE)) #define FG_BLINK TFORMAT(TF_BLINK) /* syscons(4) compatible color attributes for text background */ #define BG_BLACK TCOLOR_BG(TC_BLACK) #define BG_BLUE TCOLOR_BG(TC_BLUE) #define BG_GREEN TCOLOR_BG(TC_GREEN) #define BG_CYAN TCOLOR_BG(TC_CYAN) #define BG_RED TCOLOR_BG(TC_RED) #define BG_MAGENTA TCOLOR_BG(TC_MAGENTA) -#define BG_BROWN TCOLOR_BG(TC_BROWN) +#define BG_BROWN TCOLOR_BG(TC_YELLOW) #define BG_LIGHTGREY TCOLOR_BG(TC_WHITE) #define BG_DARKGREY (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_BLACK)) #define BG_LIGHTBLUE (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_BLUE)) #define BG_LIGHTGREEN (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_GREEN)) #define BG_LIGHTCYAN (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_CYAN)) #define BG_LIGHTRED (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_RED)) #define BG_LIGHTMAGENTA (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_MAGENTA)) -#define BG_YELLOW (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_BROWN)) +#define BG_YELLOW (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_YELLOW)) #define BG_WHITE (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_WHITE)) #ifndef TERMINAL_NORM_ATTR #ifdef SC_NORM_ATTR #define TERMINAL_NORM_ATTR SC_NORM_ATTR #else #define TERMINAL_NORM_ATTR (FG_LIGHTGREY | BG_BLACK) #endif #endif #ifndef TERMINAL_KERN_ATTR #ifdef SC_KERNEL_CONS_ATTR #define TERMINAL_KERN_ATTR SC_KERNEL_CONS_ATTR #else #define TERMINAL_KERN_ATTR (FG_WHITE | BG_BLACK) #endif #endif typedef teken_pos_t term_pos_t; typedef teken_rect_t term_rect_t; typedef void tc_cursor_t(struct terminal *tm, const term_pos_t *p); typedef void tc_putchar_t(struct terminal *tm, const term_pos_t *p, term_char_t c); typedef void tc_fill_t(struct terminal *tm, const term_rect_t *r, term_char_t c); typedef void tc_copy_t(struct terminal *tm, const term_rect_t *r, const term_pos_t *p); typedef void tc_pre_input_t(struct terminal *tm); typedef void tc_post_input_t(struct terminal *tm); typedef void tc_param_t(struct terminal *tm, int cmd, unsigned int arg); typedef void tc_done_t(struct terminal *tm); typedef void tc_cnprobe_t(struct terminal *tm, struct consdev *cd); typedef int tc_cngetc_t(struct terminal *tm); typedef void tc_cngrab_t(struct terminal *tm); typedef void tc_cnungrab_t(struct terminal *tm); typedef void tc_opened_t(struct terminal *tm, int opened); typedef int tc_ioctl_t(struct terminal *tm, u_long cmd, caddr_t data, struct thread *td); typedef int tc_mmap_t(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr, int nprot, vm_memattr_t *memattr); typedef void tc_bell_t(struct terminal *tm); struct terminal_class { /* Terminal emulator. */ tc_cursor_t *tc_cursor; tc_putchar_t *tc_putchar; tc_fill_t *tc_fill; tc_copy_t *tc_copy; tc_pre_input_t *tc_pre_input; tc_post_input_t *tc_post_input; tc_param_t *tc_param; tc_done_t *tc_done; /* Low-level console interface. */ tc_cnprobe_t *tc_cnprobe; tc_cngetc_t *tc_cngetc; /* DDB & panic handling. */ tc_cngrab_t *tc_cngrab; tc_cnungrab_t *tc_cnungrab; /* Misc. */ tc_opened_t *tc_opened; tc_ioctl_t *tc_ioctl; tc_mmap_t *tc_mmap; tc_bell_t *tc_bell; }; struct terminal { const struct terminal_class *tm_class; void *tm_softc; struct mtx tm_mtx; struct tty *tm_tty; teken_t tm_emulator; struct winsize tm_winsize; unsigned int tm_flags; #define TF_MUTE 0x1 /* Drop incoming data. */ #define TF_BELL 0x2 /* Bell needs to be sent. */ #define TF_CONS 0x4 /* Console device (needs spinlock). */ struct consdev *consdev; }; #ifdef _KERNEL struct terminal *terminal_alloc(const struct terminal_class *tc, void *softc); void terminal_maketty(struct terminal *tm, const char *fmt, ...); void terminal_set_cursor(struct terminal *tm, const term_pos_t *pos); void terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size, int blank, const term_attr_t *attr); void terminal_set_winsize(struct terminal *tm, const struct winsize *size); void terminal_mute(struct terminal *tm, int yes); void terminal_input_char(struct terminal *tm, term_char_t c); void terminal_input_raw(struct terminal *tm, char c); void terminal_input_special(struct terminal *tm, unsigned int k); void termcn_cnregister(struct terminal *tm); /* Kernel console helper interface. */ extern const struct consdev_ops termcn_cnops; #define TERMINAL_DECLARE_EARLY(name, class, softc) \ static struct terminal name = { \ .tm_class = &class, \ .tm_softc = softc, \ .tm_flags = TF_CONS, \ }; \ CONSOLE_DEVICE(name ## _consdev, termcn_cnops, &name) #endif /* _KERNEL */ #endif /* !_SYS_TERMINAL_H_ */ diff --git a/sys/teken/teken.c b/sys/teken/teken.c index cdd9ab4c8419..590e992fb238 100644 --- a/sys/teken/teken.c +++ b/sys/teken/teken.c @@ -1,757 +1,757 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2008-2009 Ed Schouten * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #if defined(__FreeBSD__) && defined(_KERNEL) #include #include #include #include #define teken_assert(x) MPASS(x) #elif defined(__FreeBSD__) && defined(_STANDALONE) #include #include #include #define teken_assert(x) assert(x) #else /* !(__FreeBSD__ && _STANDALONE) */ #include #include #include #include #include #include #define teken_assert(x) assert(x) #endif /* __FreeBSD__ && _STANDALONE */ /* debug messages */ #define teken_printf(x,...) /* Private flags for t_stateflags. */ #define TS_FIRSTDIGIT 0x0001 /* First numeric digit in escape sequence. */ #define TS_INSERT 0x0002 /* Insert mode. */ #define TS_AUTOWRAP 0x0004 /* Autowrap. */ #define TS_ORIGIN 0x0008 /* Origin mode. */ #define TS_WRAPPED 0x0010 /* Next character should be printed on col 0. */ #define TS_8BIT 0x0020 /* UTF-8 disabled. */ #define TS_CONS25 0x0040 /* cons25 emulation. */ #define TS_INSTRING 0x0080 /* Inside string. */ #define TS_CURSORKEYS 0x0100 /* Cursor keys mode. */ #define TS_CONS25KEYS 0x0400 /* Fuller cons25 emul (fix function keys). */ /* Character that blanks a cell. */ #define BLANK ' ' #include "teken.h" #include "teken_wcwidth.h" #include "teken_scs.h" static teken_state_t teken_state_init; /* * Wrappers for hooks. */ static inline void teken_funcs_bell(const teken_t *t) { teken_assert(t->t_funcs->tf_bell != NULL); t->t_funcs->tf_bell(t->t_softc); } static inline void teken_funcs_cursor(const teken_t *t) { teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row); teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col); teken_assert(t->t_funcs->tf_cursor != NULL); t->t_funcs->tf_cursor(t->t_softc, &t->t_cursor); } static inline void teken_funcs_putchar(const teken_t *t, const teken_pos_t *p, teken_char_t c, const teken_attr_t *a) { teken_assert(p->tp_row < t->t_winsize.tp_row); teken_assert(p->tp_col < t->t_winsize.tp_col); teken_assert(t->t_funcs->tf_putchar != NULL); t->t_funcs->tf_putchar(t->t_softc, p, c, a); } static inline void teken_funcs_fill(const teken_t *t, const teken_rect_t *r, const teken_char_t c, const teken_attr_t *a) { teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row); teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row); teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col); teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col); teken_assert(t->t_funcs->tf_fill != NULL); t->t_funcs->tf_fill(t->t_softc, r, c, a); } static inline void teken_funcs_copy(const teken_t *t, const teken_rect_t *r, const teken_pos_t *p) { teken_assert(r->tr_end.tp_row > r->tr_begin.tp_row); teken_assert(r->tr_end.tp_row <= t->t_winsize.tp_row); teken_assert(r->tr_end.tp_col > r->tr_begin.tp_col); teken_assert(r->tr_end.tp_col <= t->t_winsize.tp_col); teken_assert(p->tp_row + (r->tr_end.tp_row - r->tr_begin.tp_row) <= t->t_winsize.tp_row); teken_assert(p->tp_col + (r->tr_end.tp_col - r->tr_begin.tp_col) <= t->t_winsize.tp_col); teken_assert(t->t_funcs->tf_copy != NULL); t->t_funcs->tf_copy(t->t_softc, r, p); } static inline void teken_funcs_pre_input(const teken_t *t) { if (t->t_funcs->tf_pre_input != NULL) t->t_funcs->tf_pre_input(t->t_softc); } static inline void teken_funcs_post_input(const teken_t *t) { if (t->t_funcs->tf_post_input != NULL) t->t_funcs->tf_post_input(t->t_softc); } static inline void teken_funcs_param(const teken_t *t, int cmd, unsigned int value) { teken_assert(t->t_funcs->tf_param != NULL); t->t_funcs->tf_param(t->t_softc, cmd, value); } static inline void teken_funcs_respond(const teken_t *t, const void *buf, size_t len) { teken_assert(t->t_funcs->tf_respond != NULL); t->t_funcs->tf_respond(t->t_softc, buf, len); } #include "teken_subr.h" #include "teken_subr_compat.h" /* * Programming interface. */ void teken_init(teken_t *t, const teken_funcs_t *tf, void *softc) { teken_pos_t tp = { .tp_row = 24, .tp_col = 80 }; t->t_funcs = tf; t->t_softc = softc; t->t_nextstate = teken_state_init; t->t_stateflags = 0; t->t_utf8_left = 0; t->t_defattr.ta_format = 0; t->t_defattr.ta_fgcolor = TC_WHITE; t->t_defattr.ta_bgcolor = TC_BLACK; teken_subr_do_reset(t); teken_set_winsize(t, &tp); } static void teken_input_char(teken_t *t, teken_char_t c) { /* * There is no support for DCS and OSC. Just discard strings * until we receive characters that may indicate string * termination. */ if (t->t_stateflags & TS_INSTRING) { switch (c) { case '\x1B': t->t_stateflags &= ~TS_INSTRING; break; case '\a': t->t_stateflags &= ~TS_INSTRING; return; default: return; } } switch (c) { case '\0': break; case '\a': teken_subr_bell(t); break; case '\b': teken_subr_backspace(t); break; case '\n': case '\x0B': teken_subr_newline(t); break; case '\x0C': teken_subr_newpage(t); break; case '\x0E': if (t->t_stateflags & TS_CONS25) t->t_nextstate(t, c); else t->t_curscs = 1; break; case '\x0F': if (t->t_stateflags & TS_CONS25) t->t_nextstate(t, c); else t->t_curscs = 0; break; case '\r': teken_subr_carriage_return(t); break; case '\t': teken_subr_horizontal_tab(t); break; default: t->t_nextstate(t, c); break; } /* Post-processing assertions. */ teken_assert(t->t_cursor.tp_row >= t->t_originreg.ts_begin); teken_assert(t->t_cursor.tp_row < t->t_originreg.ts_end); teken_assert(t->t_cursor.tp_row < t->t_winsize.tp_row); teken_assert(t->t_cursor.tp_col < t->t_winsize.tp_col); teken_assert(t->t_saved_cursor.tp_row < t->t_winsize.tp_row); teken_assert(t->t_saved_cursor.tp_col < t->t_winsize.tp_col); teken_assert(t->t_scrollreg.ts_end <= t->t_winsize.tp_row); teken_assert(t->t_scrollreg.ts_begin < t->t_scrollreg.ts_end); /* Origin region has to be window size or the same as scrollreg. */ teken_assert((t->t_originreg.ts_begin == t->t_scrollreg.ts_begin && t->t_originreg.ts_end == t->t_scrollreg.ts_end) || (t->t_originreg.ts_begin == 0 && t->t_originreg.ts_end == t->t_winsize.tp_row)); } static void teken_input_byte(teken_t *t, unsigned char c) { /* * UTF-8 handling. */ if ((c & 0x80) == 0x00 || t->t_stateflags & TS_8BIT) { /* One-byte sequence. */ t->t_utf8_left = 0; teken_input_char(t, c); } else if ((c & 0xe0) == 0xc0) { /* Two-byte sequence. */ t->t_utf8_left = 1; t->t_utf8_partial = c & 0x1f; } else if ((c & 0xf0) == 0xe0) { /* Three-byte sequence. */ t->t_utf8_left = 2; t->t_utf8_partial = c & 0x0f; } else if ((c & 0xf8) == 0xf0) { /* Four-byte sequence. */ t->t_utf8_left = 3; t->t_utf8_partial = c & 0x07; } else if ((c & 0xc0) == 0x80) { if (t->t_utf8_left == 0) return; t->t_utf8_left--; t->t_utf8_partial = (t->t_utf8_partial << 6) | (c & 0x3f); if (t->t_utf8_left == 0) { teken_printf("Got UTF-8 char %x\n", t->t_utf8_partial); teken_input_char(t, t->t_utf8_partial); } } } void teken_input(teken_t *t, const void *buf, size_t len) { const char *c = buf; teken_funcs_pre_input(t); while (len-- > 0) teken_input_byte(t, *c++); teken_funcs_post_input(t); } const teken_pos_t * teken_get_cursor(const teken_t *t) { return (&t->t_cursor); } void teken_set_cursor(teken_t *t, const teken_pos_t *p) { /* XXX: bounds checking with originreg! */ teken_assert(p->tp_row < t->t_winsize.tp_row); teken_assert(p->tp_col < t->t_winsize.tp_col); t->t_cursor = *p; } const teken_attr_t * teken_get_curattr(const teken_t *t) { return (&t->t_curattr); } void teken_set_curattr(teken_t *t, const teken_attr_t *a) { t->t_curattr = *a; } const teken_attr_t * teken_get_defattr(const teken_t *t) { return (&t->t_defattr); } void teken_set_defattr(teken_t *t, const teken_attr_t *a) { t->t_curattr = t->t_saved_curattr = t->t_defattr = *a; } const teken_pos_t * teken_get_winsize(const teken_t *t) { return (&t->t_winsize); } static void teken_trim_cursor_pos(teken_t *t, const teken_pos_t *new) { const teken_pos_t *cur; cur = &t->t_winsize; if (cur->tp_row < new->tp_row || cur->tp_col < new->tp_col) return; if (t->t_cursor.tp_row >= new->tp_row) t->t_cursor.tp_row = new->tp_row - 1; if (t->t_cursor.tp_col >= new->tp_col) t->t_cursor.tp_col = new->tp_col - 1; } void teken_set_winsize(teken_t *t, const teken_pos_t *p) { teken_trim_cursor_pos(t, p); t->t_winsize = *p; teken_subr_do_reset(t); } void teken_set_winsize_noreset(teken_t *t, const teken_pos_t *p) { teken_trim_cursor_pos(t, p); t->t_winsize = *p; teken_subr_do_resize(t); } void teken_set_8bit(teken_t *t) { t->t_stateflags |= TS_8BIT; } void teken_set_cons25(teken_t *t) { t->t_stateflags |= TS_CONS25; } void teken_set_cons25keys(teken_t *t) { t->t_stateflags |= TS_CONS25KEYS; } /* * State machine. */ static void teken_state_switch(teken_t *t, teken_state_t *s) { t->t_nextstate = s; t->t_curnum = 0; t->t_stateflags |= TS_FIRSTDIGIT; } static int teken_state_numbers(teken_t *t, teken_char_t c) { teken_assert(t->t_curnum < T_NUMSIZE); if (c >= '0' && c <= '9') { if (t->t_stateflags & TS_FIRSTDIGIT) { /* First digit. */ t->t_stateflags &= ~TS_FIRSTDIGIT; t->t_nums[t->t_curnum] = c - '0'; } else if (t->t_nums[t->t_curnum] < UINT_MAX / 100) { /* * There is no need to continue parsing input * once the value exceeds the size of the * terminal. It would only allow for integer * overflows when performing arithmetic on the * cursor position. * * Ignore any further digits if the value is * already UINT_MAX / 100. */ t->t_nums[t->t_curnum] = t->t_nums[t->t_curnum] * 10 + c - '0'; } return (1); } else if (c == ';') { if (t->t_stateflags & TS_FIRSTDIGIT) t->t_nums[t->t_curnum] = 0; /* Only allow a limited set of arguments. */ if (++t->t_curnum == T_NUMSIZE) { teken_state_switch(t, teken_state_init); return (1); } t->t_stateflags |= TS_FIRSTDIGIT; return (1); } else { if (t->t_stateflags & TS_FIRSTDIGIT && t->t_curnum > 0) { /* Finish off the last empty argument. */ t->t_nums[t->t_curnum] = 0; t->t_curnum++; } else if ((t->t_stateflags & TS_FIRSTDIGIT) == 0) { /* Also count the last argument. */ t->t_curnum++; } } return (0); } #define k TC_BLACK #define b TC_BLUE -#define y TC_BROWN +#define y TC_YELLOW #define c TC_CYAN #define g TC_GREEN #define m TC_MAGENTA #define r TC_RED #define w TC_WHITE #define K (TC_BLACK | TC_LIGHT) #define B (TC_BLUE | TC_LIGHT) -#define Y (TC_BROWN | TC_LIGHT) +#define Y (TC_YELLOW | TC_LIGHT) #define C (TC_CYAN | TC_LIGHT) #define G (TC_GREEN | TC_LIGHT) #define M (TC_MAGENTA | TC_LIGHT) #define R (TC_RED | TC_LIGHT) #define W (TC_WHITE | TC_LIGHT) /** * The xterm-256 color map has steps of 0x28 (in the range 0-0xff), except * for the first step which is 0x5f. Scale to the range 0-6 by dividing * by 0x28 and rounding down. The range of 0-5 cannot represent the * larger first step. * * This table is generated by the follow rules: * - if all components are equal, the result is black for (0, 0, 0) and * (2, 2, 2), else white; otherwise: * - subtract the smallest component from all components * - if this gives only one nonzero component, then that is the color * - else if one component is 2 or more larger than the other nonzero one, * then that component gives the color * - else there are 2 nonzero components. The color is that of a small * equal mixture of these components (cyan, yellow or magenta). E.g., * (0, 5, 6) (Turquoise2) is a much purer cyan than (0, 2, 3) * (DeepSkyBlue4), but we map both to cyan since we can't represent * delicate shades of either blue or cyan and blue would be worse. * Here it is important that components of 1 never occur. Blue would * be twice as large as green in (0, 1, 2). */ static const teken_color_t teken_256to8tab[] = { /* xterm normal colors: */ k, r, g, y, b, m, c, w, /* xterm bright colors: */ k, r, g, y, b, m, c, w, /* Red0 submap. */ k, b, b, b, b, b, g, c, c, b, b, b, g, c, c, c, b, b, g, g, c, c, c, b, g, g, g, c, c, c, g, g, g, g, c, c, /* Red2 submap. */ r, m, m, b, b, b, y, k, b, b, b, b, y, g, c, c, b, b, g, g, c, c, c, b, g, g, g, c, c, c, g, g, g, g, c, c, /* Red3 submap. */ r, m, m, m, b, b, y, r, m, m, b, b, y, y, w, b, b, b, y, y, g, c, c, b, g, g, g, c, c, c, g, g, g, g, c, c, /* Red4 submap. */ r, r, m, m, m, b, r, r, m, m, m, b, y, y, r, m, m, b, y, y, y, w, b, b, y, y, y, g, c, c, g, g, g, g, c, c, /* Red5 submap. */ r, r, r, m, m, m, r, r, r, m, m, m, r, r, r, m, m, m, y, y, y, r, m, m, y, y, y, y, w, b, y, y, y, y, g, c, /* Red6 submap. */ r, r, r, r, m, m, r, r, r, r, m, m, r, r, r, r, m, m, r, r, r, r, m, m, y, y, y, y, r, m, y, y, y, y, y, w, /* Grey submap. */ k, k, k, k, k, k, k, k, k, k, k, k, w, w, w, w, w, w, w, w, w, w, w, w, }; /* * This table is generated from the previous one by setting TC_LIGHT for * entries whose luminosity in the xterm256 color map is 60% or larger. * Thus the previous table is currently not really needed. It will be * used for different fine tuning of the tables. */ static const teken_color_t teken_256to16tab[] = { /* xterm normal colors: */ k, r, g, y, b, m, c, w, /* xterm bright colors: */ K, R, G, Y, B, M, C, W, /* Red0 submap. */ k, b, b, b, b, b, g, c, c, b, b, b, g, c, c, c, b, b, g, g, c, c, c, b, g, g, g, c, c, c, g, g, g, g, c, c, /* Red2 submap. */ r, m, m, b, b, b, y, K, b, b, B, B, y, g, c, c, B, B, g, g, c, c, C, B, g, G, G, C, C, C, g, G, G, G, C, C, /* Red3 submap. */ r, m, m, m, b, b, y, r, m, m, B, B, y, y, w, B, B, B, y, y, G, C, C, B, g, G, G, C, C, C, g, G, G, G, C, C, /* Red4 submap. */ r, r, m, m, m, b, r, r, m, m, M, B, y, y, R, M, M, B, y, y, Y, W, B, B, y, Y, Y, G, C, C, g, G, G, G, C, C, /* Red5 submap. */ r, r, r, m, m, m, r, R, R, M, M, M, r, R, R, M, M, M, y, Y, Y, R, M, M, y, Y, Y, Y, W, B, y, Y, Y, Y, G, C, /* Red6 submap. */ r, r, r, r, m, m, r, R, R, R, M, M, r, R, R, R, M, M, r, R, R, R, M, M, y, Y, Y, Y, R, M, y, Y, Y, Y, Y, W, /* Grey submap. */ k, k, k, k, k, k, K, K, K, K, K, K, w, w, w, w, w, w, W, W, W, W, W, W, }; #undef k #undef b #undef y #undef c #undef g #undef m #undef r #undef w #undef K #undef B #undef Y #undef C #undef G #undef M #undef R #undef W teken_color_t teken_256to8(teken_color_t c) { return (teken_256to8tab[c % 256]); } teken_color_t teken_256to16(teken_color_t c) { return (teken_256to16tab[c % 256]); } static const char * const special_strings_cons25[] = { [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B", [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C", [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F", [TKEY_INSERT] = "\x1B[L", [TKEY_DELETE] = "\x7F", [TKEY_PAGE_UP] = "\x1B[I", [TKEY_PAGE_DOWN] = "\x1B[G", [TKEY_F1] = "\x1B[M", [TKEY_F2] = "\x1B[N", [TKEY_F3] = "\x1B[O", [TKEY_F4] = "\x1B[P", [TKEY_F5] = "\x1B[Q", [TKEY_F6] = "\x1B[R", [TKEY_F7] = "\x1B[S", [TKEY_F8] = "\x1B[T", [TKEY_F9] = "\x1B[U", [TKEY_F10] = "\x1B[V", [TKEY_F11] = "\x1B[W", [TKEY_F12] = "\x1B[X", }; static const char * const special_strings_ckeys[] = { [TKEY_UP] = "\x1BOA", [TKEY_DOWN] = "\x1BOB", [TKEY_LEFT] = "\x1BOD", [TKEY_RIGHT] = "\x1BOC", [TKEY_HOME] = "\x1BOH", [TKEY_END] = "\x1BOF", }; static const char * const special_strings_normal[] = { [TKEY_UP] = "\x1B[A", [TKEY_DOWN] = "\x1B[B", [TKEY_LEFT] = "\x1B[D", [TKEY_RIGHT] = "\x1B[C", [TKEY_HOME] = "\x1B[H", [TKEY_END] = "\x1B[F", [TKEY_INSERT] = "\x1B[2~", [TKEY_DELETE] = "\x1B[3~", [TKEY_PAGE_UP] = "\x1B[5~", [TKEY_PAGE_DOWN] = "\x1B[6~", [TKEY_F1] = "\x1BOP", [TKEY_F2] = "\x1BOQ", [TKEY_F3] = "\x1BOR", [TKEY_F4] = "\x1BOS", [TKEY_F5] = "\x1B[15~", [TKEY_F6] = "\x1B[17~", [TKEY_F7] = "\x1B[18~", [TKEY_F8] = "\x1B[19~", [TKEY_F9] = "\x1B[20~", [TKEY_F10] = "\x1B[21~", [TKEY_F11] = "\x1B[23~", [TKEY_F12] = "\x1B[24~", }; const char * teken_get_sequence(const teken_t *t, unsigned int k) { /* Cons25 mode. */ if ((t->t_stateflags & (TS_CONS25 | TS_CONS25KEYS)) == (TS_CONS25 | TS_CONS25KEYS)) return (NULL); /* Don't override good kbd(4) strings. */ if (t->t_stateflags & TS_CONS25 && k < sizeof special_strings_cons25 / sizeof(char *)) return (special_strings_cons25[k]); /* Cursor keys mode. */ if (t->t_stateflags & TS_CURSORKEYS && k < sizeof special_strings_ckeys / sizeof(char *)) return (special_strings_ckeys[k]); /* Default xterm sequences. */ if (k < sizeof special_strings_normal / sizeof(char *)) return (special_strings_normal[k]); return (NULL); } #include "teken_state.h" diff --git a/sys/teken/teken.h b/sys/teken/teken.h index 994298b0dbc6..7545db9b9cdf 100644 --- a/sys/teken/teken.h +++ b/sys/teken/teken.h @@ -1,222 +1,222 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2008-2009 Ed Schouten * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _TEKEN_H_ #define _TEKEN_H_ #include /* * libteken: terminal emulation library. * * This library converts an UTF-8 stream of bytes to terminal drawing * commands. */ typedef uint32_t teken_char_t; typedef unsigned short teken_unit_t; typedef unsigned char teken_format_t; #define TF_BOLD 0x01 /* Bold character. */ #define TF_UNDERLINE 0x02 /* Underline character. */ #define TF_BLINK 0x04 /* Blinking character. */ #define TF_REVERSE 0x08 /* Reverse rendered character. */ #define TF_CJK_RIGHT 0x10 /* Right-hand side of CJK character. */ #define TF_IMAGE 0x20 /* This character space has image. */ typedef unsigned char teken_color_t; #define TC_BLACK 0 #define TC_RED 1 #define TC_GREEN 2 -#define TC_BROWN 3 +#define TC_YELLOW 3 #define TC_BLUE 4 #define TC_MAGENTA 5 #define TC_CYAN 6 #define TC_WHITE 7 #define TC_NCOLORS 8 #define TC_LIGHT 8 /* ORed with the others. */ typedef struct { teken_unit_t tp_row; teken_unit_t tp_col; } teken_pos_t; typedef struct { teken_pos_t tr_begin; teken_pos_t tr_end; } teken_rect_t; typedef struct { teken_format_t ta_format; teken_color_t ta_fgcolor; teken_color_t ta_bgcolor; } teken_attr_t; typedef struct { teken_unit_t ts_begin; teken_unit_t ts_end; } teken_span_t; typedef struct __teken teken_t; typedef void teken_state_t(teken_t *, teken_char_t); /* * Drawing routines supplied by the user. */ typedef void tf_bell_t(void *); typedef void tf_cursor_t(void *, const teken_pos_t *); typedef void tf_putchar_t(void *, const teken_pos_t *, teken_char_t, const teken_attr_t *); typedef void tf_fill_t(void *, const teken_rect_t *, teken_char_t, const teken_attr_t *); typedef void tf_copy_t(void *, const teken_rect_t *, const teken_pos_t *); typedef void tf_pre_input_t(void *); typedef void tf_post_input_t(void *); typedef void tf_param_t(void *, int, unsigned int); #define TP_SHOWCURSOR 0 #define TP_KEYPADAPP 1 #define TP_AUTOREPEAT 2 #define TP_SWITCHVT 3 #define TP_132COLS 4 #define TP_SETBELLPD 5 #define TP_SETBELLPD_PITCH(pd) ((pd) >> 16) #define TP_SETBELLPD_DURATION(pd) ((pd) & 0xffff) #define TP_MOUSE 6 #define TP_SETBORDER 7 #define TP_SETLOCALCURSOR 8 #define TP_SETGLOBALCURSOR 9 typedef void tf_respond_t(void *, const void *, size_t); typedef struct { tf_bell_t *tf_bell; tf_cursor_t *tf_cursor; tf_putchar_t *tf_putchar; tf_fill_t *tf_fill; tf_copy_t *tf_copy; tf_pre_input_t *tf_pre_input; tf_post_input_t *tf_post_input; tf_param_t *tf_param; tf_respond_t *tf_respond; } teken_funcs_t; typedef teken_char_t teken_scs_t(const teken_t *, teken_char_t); /* * Terminal state. */ struct __teken { const teken_funcs_t *t_funcs; void *t_softc; teken_state_t *t_nextstate; unsigned int t_stateflags; #define T_NUMSIZE 8 unsigned int t_nums[T_NUMSIZE]; unsigned int t_curnum; teken_pos_t t_cursor; teken_attr_t t_curattr; teken_pos_t t_saved_cursor; teken_attr_t t_saved_curattr; teken_attr_t t_defattr; teken_pos_t t_winsize; /* For DECSTBM. */ teken_span_t t_scrollreg; /* For DECOM. */ teken_span_t t_originreg; #define T_NUMCOL 160 unsigned int t_tabstops[T_NUMCOL / (sizeof(unsigned int) * 8)]; unsigned int t_utf8_left; teken_char_t t_utf8_partial; teken_char_t t_last; unsigned int t_curscs; teken_scs_t *t_saved_curscs; teken_scs_t *t_scs[2]; }; /* Initialize teken structure. */ void teken_init(teken_t *, const teken_funcs_t *, void *); /* Deliver character input. */ void teken_input(teken_t *, const void *, size_t); /* Get/set teken attributes. */ const teken_pos_t *teken_get_cursor(const teken_t *); const teken_attr_t *teken_get_curattr(const teken_t *); const teken_attr_t *teken_get_defattr(const teken_t *); void teken_get_defattr_cons25(const teken_t *, int *, int *); const teken_pos_t *teken_get_winsize(const teken_t *); void teken_set_cursor(teken_t *, const teken_pos_t *); void teken_set_curattr(teken_t *, const teken_attr_t *); void teken_set_defattr(teken_t *, const teken_attr_t *); void teken_set_winsize(teken_t *, const teken_pos_t *); void teken_set_winsize_noreset(teken_t *, const teken_pos_t *); /* Key input escape sequences. */ #define TKEY_UP 0x00 #define TKEY_DOWN 0x01 #define TKEY_LEFT 0x02 #define TKEY_RIGHT 0x03 #define TKEY_HOME 0x04 #define TKEY_END 0x05 #define TKEY_INSERT 0x06 #define TKEY_DELETE 0x07 #define TKEY_PAGE_UP 0x08 #define TKEY_PAGE_DOWN 0x09 #define TKEY_F1 0x0a #define TKEY_F2 0x0b #define TKEY_F3 0x0c #define TKEY_F4 0x0d #define TKEY_F5 0x0e #define TKEY_F6 0x0f #define TKEY_F7 0x10 #define TKEY_F8 0x11 #define TKEY_F9 0x12 #define TKEY_F10 0x13 #define TKEY_F11 0x14 #define TKEY_F12 0x15 const char *teken_get_sequence(const teken_t *, unsigned int); /* Legacy features. */ void teken_set_8bit(teken_t *); void teken_set_cons25(teken_t *); void teken_set_cons25keys(teken_t *); /* Color conversion. */ teken_color_t teken_256to16(teken_color_t); teken_color_t teken_256to8(teken_color_t); #endif /* !_TEKEN_H_ */ diff --git a/sys/teken/teken_subr_compat.h b/sys/teken/teken_subr_compat.h index 4cf4f5e98c7b..c71fcea6ce9f 100644 --- a/sys/teken/teken_subr_compat.h +++ b/sys/teken/teken_subr_compat.h @@ -1,153 +1,153 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2008-2009 Ed Schouten * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ static void teken_subr_cons25_set_border(const teken_t *t, unsigned int c) { teken_funcs_param(t, TP_SETBORDER, c); } static void teken_subr_cons25_set_global_cursor_shape(const teken_t *t, unsigned int ncmds, const unsigned int cmds[]) { unsigned int code, i; /* * Pack the args to work around API deficiencies. This requires * knowing too much about the low level to be fully compatible. * Returning when ncmds > 3 is necessary and happens to be * compatible. Discarding high bits is necessary and happens to * be incompatible only for invalid args when ncmds == 3. */ if (ncmds > 3) return; code = 0; for (i = ncmds; i > 0; i--) code = (code << 8) | (cmds[i - 1] & 0xff); code = (code << 8) | ncmds; teken_funcs_param(t, TP_SETGLOBALCURSOR, code); } static void teken_subr_cons25_set_local_cursor_type(const teken_t *t, unsigned int type) { teken_funcs_param(t, TP_SETLOCALCURSOR, type); } static const teken_color_t cons25_colors[8] = { TC_BLACK, TC_BLUE, - TC_GREEN, TC_CYAN, TC_RED, TC_MAGENTA, TC_BROWN, TC_WHITE }; + TC_GREEN, TC_CYAN, TC_RED, TC_MAGENTA, TC_YELLOW, TC_WHITE }; static void teken_subr_cons25_set_default_background(teken_t *t, unsigned int c) { t->t_defattr.ta_bgcolor = cons25_colors[c % 8] | (c & 8); t->t_curattr.ta_bgcolor = cons25_colors[c % 8] | (c & 8); } static void teken_subr_cons25_set_default_foreground(teken_t *t, unsigned int c) { t->t_defattr.ta_fgcolor = cons25_colors[c % 8] | (c & 8); t->t_curattr.ta_fgcolor = cons25_colors[c % 8] | (c & 8); } static const teken_color_t cons25_revcolors[8] = { 0, 4, 2, 6, 1, 5, 3, 7 }; void teken_get_defattr_cons25(const teken_t *t, int *fg, int *bg) { *fg = cons25_revcolors[teken_256to8(t->t_defattr.ta_fgcolor)]; if (t->t_defattr.ta_format & TF_BOLD) *fg += 8; *bg = cons25_revcolors[teken_256to8(t->t_defattr.ta_bgcolor)]; } static void teken_subr_cons25_switch_virtual_terminal(const teken_t *t, unsigned int vt) { teken_funcs_param(t, TP_SWITCHVT, vt); } static void teken_subr_cons25_set_bell_pitch_duration(const teken_t *t, unsigned int pitch, unsigned int duration) { teken_funcs_param(t, TP_SETBELLPD, (pitch << 16) | (duration & 0xffff)); } static void teken_subr_cons25_set_graphic_rendition(teken_t *t, unsigned int cmd, unsigned int param) { (void)param; switch (cmd) { case 0: /* Reset. */ t->t_curattr = t->t_defattr; break; default: teken_printf("unsupported attribute %u\n", cmd); } } static void teken_subr_cons25_set_terminal_mode(teken_t *t, unsigned int mode) { switch (mode) { case 0: /* Switch terminal to xterm. */ t->t_stateflags &= ~TS_CONS25; break; case 1: /* Switch terminal to cons25. */ t->t_stateflags |= TS_CONS25; break; default: break; } } #if 0 static void teken_subr_vt52_decid(teken_t *t) { const char response[] = "\x1B/Z"; teken_funcs_respond(t, response, sizeof response - 1); } #endif