1/*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009 The FreeBSD Foundation 5 * 6 * This software was developed by Ed Schouten under sponsorship from the 7 * FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31#ifndef _SYS_TERMINAL_H_ 32#define _SYS_TERMINAL_H_ 33 34#include <sys/param.h> 35#include <sys/_lock.h> 36#include <sys/_mutex.h> 37#include <sys/cons.h> 38#include <sys/linker_set.h> 39#include <sys/ttycom.h> 40 41#include <teken/teken.h> 42 43#include "opt_syscons.h" 44#include "opt_teken.h" 45 46struct terminal; 47struct thread; 48struct tty; 49 50/* 51 * The terminal layer is an abstraction on top of the TTY layer and the 52 * console interface. It can be used by system console drivers to 53 * easily interact with the kernel console and TTYs. 54 * 55 * Terminals contain terminal emulators, which means console drivers 56 * don't need to implement their own terminal emulator. The terminal 57 * emulator deals with UTF-8 exclusively. This means that term_char_t, 58 * the data type used to store input/output characters will always 59 * contain Unicode codepoints. 60 * 61 * To save memory usage, the top bits of term_char_t will contain other 62 * attributes, like colors. Right now term_char_t is composed as 63 * follows: 64 * 65 * Bits Meaning 66 * 0-20: Character value 67 * 21-25: Bold, underline, blink, reverse, right part of CJK fullwidth character 68 * 26-28: Foreground color 69 * 29-31: Background color 70 */ 71 72typedef uint32_t term_char_t; 73#define TCHAR_CHARACTER(c) ((c) & 0x1fffff) 74#define TCHAR_FORMAT(c) (((c) >> 21) & 0x1f) 75#define TCHAR_FGCOLOR(c) (((c) >> 26) & 0x7) 76#define TCHAR_BGCOLOR(c) (((c) >> 29) & 0x7) 77 78typedef teken_attr_t term_attr_t; 79 80typedef teken_color_t term_color_t; 81#define TCOLOR_FG(c) (((c) & 0x7) << 26) 82#define TCOLOR_BG(c) (((c) & 0x7) << 29) 83#define TCOLOR_LIGHT(c) ((c) | 0x8) 84#define TCOLOR_DARK(c) ((c) & ~0x8) 85 86#define TFORMAT(c) (((c) & 0x1f) << 21) 87 88/* syscons(4) compatible color attributes for foreground text */ 89#define FG_BLACK TCOLOR_FG(TC_BLACK) 90#define FG_BLUE TCOLOR_FG(TC_BLUE) 91#define FG_GREEN TCOLOR_FG(TC_GREEN) 92#define FG_CYAN TCOLOR_FG(TC_CYAN) 93#define FG_RED TCOLOR_FG(TC_RED) 94#define FG_MAGENTA TCOLOR_FG(TC_MAGENTA) 95#define FG_BROWN TCOLOR_FG(TC_YELLOW) 96#define FG_LIGHTGREY TCOLOR_FG(TC_WHITE) 97#define FG_DARKGREY (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_BLACK)) 98#define FG_LIGHTBLUE (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_BLUE)) 99#define FG_LIGHTGREEN (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_GREEN)) 100#define FG_LIGHTCYAN (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_CYAN)) 101#define FG_LIGHTRED (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_RED)) 102#define FG_LIGHTMAGENTA (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_MAGENTA)) 103#define FG_YELLOW (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_YELLOW)) 104#define FG_WHITE (TFORMAT(TF_BOLD) | TCOLOR_FG(TC_WHITE)) 105#define FG_BLINK TFORMAT(TF_BLINK) 106 107/* syscons(4) compatible color attributes for text background */ 108#define BG_BLACK TCOLOR_BG(TC_BLACK) 109#define BG_BLUE TCOLOR_BG(TC_BLUE) 110#define BG_GREEN TCOLOR_BG(TC_GREEN) 111#define BG_CYAN TCOLOR_BG(TC_CYAN) 112#define BG_RED TCOLOR_BG(TC_RED) 113#define BG_MAGENTA TCOLOR_BG(TC_MAGENTA) 114#define BG_BROWN TCOLOR_BG(TC_YELLOW) 115#define BG_LIGHTGREY TCOLOR_BG(TC_WHITE) 116#define BG_DARKGREY (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_BLACK)) 117#define BG_LIGHTBLUE (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_BLUE)) 118#define BG_LIGHTGREEN (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_GREEN)) 119#define BG_LIGHTCYAN (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_CYAN)) 120#define BG_LIGHTRED (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_RED)) 121#define BG_LIGHTMAGENTA (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_MAGENTA)) 122#define BG_YELLOW (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_YELLOW)) 123#define BG_WHITE (TFORMAT(TF_BOLD) | TCOLOR_BG(TC_WHITE)) 124 125#ifndef TERMINAL_NORM_ATTR 126#ifdef SC_NORM_ATTR 127#define TERMINAL_NORM_ATTR SC_NORM_ATTR 128#else 129#define TERMINAL_NORM_ATTR (FG_LIGHTGREY | BG_BLACK) 130#endif 131#endif 132 133#ifndef TERMINAL_KERN_ATTR 134#ifdef SC_KERNEL_CONS_ATTR 135#define TERMINAL_KERN_ATTR SC_KERNEL_CONS_ATTR 136#else 137#define TERMINAL_KERN_ATTR (FG_WHITE | BG_BLACK) 138#endif 139#endif 140 141typedef teken_pos_t term_pos_t; 142typedef teken_rect_t term_rect_t; 143 144typedef void tc_cursor_t(struct terminal *tm, const term_pos_t *p); 145typedef void tc_putchar_t(struct terminal *tm, const term_pos_t *p, 146 term_char_t c); 147typedef void tc_fill_t(struct terminal *tm, const term_rect_t *r, 148 term_char_t c); 149typedef void tc_copy_t(struct terminal *tm, const term_rect_t *r, 150 const term_pos_t *p); 151typedef void tc_pre_input_t(struct terminal *tm); 152typedef void tc_post_input_t(struct terminal *tm); 153typedef void tc_param_t(struct terminal *tm, int cmd, unsigned int arg); 154typedef void tc_done_t(struct terminal *tm); 155 156typedef void tc_cnprobe_t(struct terminal *tm, struct consdev *cd); 157typedef int tc_cngetc_t(struct terminal *tm); 158 159typedef void tc_cngrab_t(struct terminal *tm); 160typedef void tc_cnungrab_t(struct terminal *tm); 161 162typedef void tc_opened_t(struct terminal *tm, int opened); 163typedef int tc_ioctl_t(struct terminal *tm, u_long cmd, caddr_t data, 164 struct thread *td); 165typedef int tc_mmap_t(struct terminal *tm, vm_ooffset_t offset, 166 vm_paddr_t * paddr, int nprot, vm_memattr_t *memattr); 167typedef void tc_bell_t(struct terminal *tm); 168 169struct terminal_class { 170 /* Terminal emulator. */ 171 tc_cursor_t *tc_cursor; 172 tc_putchar_t *tc_putchar; 173 tc_fill_t *tc_fill; 174 tc_copy_t *tc_copy; 175 tc_pre_input_t *tc_pre_input; 176 tc_post_input_t *tc_post_input; 177 tc_param_t *tc_param; 178 tc_done_t *tc_done; 179 180 /* Low-level console interface. */ 181 tc_cnprobe_t *tc_cnprobe; 182 tc_cngetc_t *tc_cngetc; 183 184 /* DDB & panic handling. */ 185 tc_cngrab_t *tc_cngrab; 186 tc_cnungrab_t *tc_cnungrab; 187 188 /* Misc. */ 189 tc_opened_t *tc_opened; 190 tc_ioctl_t *tc_ioctl; 191 tc_mmap_t *tc_mmap; 192 tc_bell_t *tc_bell; 193}; 194 195struct terminal { 196 const struct terminal_class *tm_class; 197 void *tm_softc; 198 struct mtx tm_mtx; 199 struct tty *tm_tty; 200 teken_t tm_emulator; 201 struct winsize tm_winsize; 202 unsigned int tm_flags; 203#define TF_MUTE 0x1 /* Drop incoming data. */ 204#define TF_BELL 0x2 /* Bell needs to be sent. */ 205#define TF_CONS 0x4 /* Console device (needs spinlock). */ 206 struct consdev *consdev; 207}; 208 209#ifdef _KERNEL 210 211struct terminal *terminal_alloc(const struct terminal_class *tc, void *softc); 212void terminal_maketty(struct terminal *tm, const char *fmt, ...); 213void terminal_set_cursor(struct terminal *tm, const term_pos_t *pos); 214void terminal_set_winsize_blank(struct terminal *tm, 215 const struct winsize *size, int blank, const term_attr_t *attr); 216void terminal_set_winsize(struct terminal *tm, const struct winsize *size); 217void terminal_mute(struct terminal *tm, int yes); 218void terminal_input_char(struct terminal *tm, term_char_t c); 219void terminal_input_raw(struct terminal *tm, char c); 220void terminal_input_special(struct terminal *tm, unsigned int k); 221 222void termcn_cnregister(struct terminal *tm); 223 224/* Kernel console helper interface. */ 225extern const struct consdev_ops termcn_cnops; 226 227#define TERMINAL_DECLARE_EARLY(name, class, softc) \ 228 static struct terminal name = { \ 229 .tm_class = &class, \ 230 .tm_softc = softc, \ 231 .tm_flags = TF_CONS, \ 232 }; \ 233 CONSOLE_DEVICE(name ## _consdev, termcn_cnops, &name) 234 235#endif /* _KERNEL */ 236 237#endif /* !_SYS_TERMINAL_H_ */ 238