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