xref: /freebsd/sys/sys/terminal.h (revision 3b8f08459569bf0faa21473e5cec2491e95c9349)
1 /*-
2  * Copyright (c) 2009 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Ed Schouten under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _SYS_TERMINAL_H_
33 #define	_SYS_TERMINAL_H_
34 
35 #include <sys/param.h>
36 #include <sys/_lock.h>
37 #include <sys/_mutex.h>
38 #include <sys/cons.h>
39 #include <sys/linker_set.h>
40 #include <sys/ttycom.h>
41 
42 #include <teken/teken.h>
43 
44 struct terminal;
45 struct thread;
46 struct tty;
47 
48 /*
49  * The terminal layer is an abstraction on top of the TTY layer and the
50  * console interface.  It can be used by system console drivers to
51  * easily interact with the kernel console and TTYs.
52  *
53  * Terminals contain terminal emulators, which means console drivers
54  * don't need to implement their own terminal emulator. The terminal
55  * emulator deals with UTF-8 exclusively. This means that term_char_t,
56  * the data type used to store input/output characters will always
57  * contain Unicode codepoints.
58  *
59  * To save memory usage, the top bits of term_char_t will contain other
60  * attributes, like colors. Right now term_char_t is composed as
61  * follows:
62  *
63  *  Bits  Meaning
64  *  0-20: Character value
65  * 21-25: Bold, underline, blink, reverse, right part of CJK fullwidth character
66  * 26-28: Foreground color
67  * 29-31: Background color
68  */
69 
70 typedef uint32_t term_char_t;
71 #define	TCHAR_CHARACTER(c)	((c) & 0x1fffff)
72 #define	TCHAR_FORMAT(c)		(((c) >> 21) & 0x1f)
73 #define	TCHAR_FGCOLOR(c)	(((c) >> 26) & 0x7)
74 #define	TCHAR_BGCOLOR(c)	((c) >> 29)
75 
76 typedef teken_color_t term_color_t;
77 #define	TCOLOR_LIGHT(c)	((c) | 0x8)
78 #define	TCOLOR_DARK(c)	((c) & ~0x8)
79 typedef teken_pos_t term_pos_t;
80 typedef teken_rect_t term_rect_t;
81 
82 typedef void tc_cursor_t(struct terminal *tm, const term_pos_t *p);
83 typedef void tc_putchar_t(struct terminal *tm, const term_pos_t *p,
84     term_char_t c);
85 typedef void tc_fill_t(struct terminal *tm, const term_rect_t *r,
86     term_char_t c);
87 typedef void tc_copy_t(struct terminal *tm, const term_rect_t *r,
88     const term_pos_t *p);
89 typedef void tc_param_t(struct terminal *tm, int cmd, unsigned int arg);
90 typedef void tc_done_t(struct terminal *tm);
91 
92 typedef void tc_cnprobe_t(struct terminal *tm, struct consdev *cd);
93 typedef int tc_cngetc_t(struct terminal *tm);
94 
95 typedef void tc_opened_t(struct terminal *tm, int opened);
96 typedef int tc_ioctl_t(struct terminal *tm, u_long cmd, caddr_t data,
97     struct thread *td);
98 typedef int tc_mmap_t(struct terminal *tm, vm_ooffset_t offset,
99     vm_paddr_t * paddr, int nprot, vm_memattr_t *memattr);
100 typedef void tc_bell_t(struct terminal *tm);
101 
102 struct terminal_class {
103 	/* Terminal emulator. */
104 	tc_cursor_t	*tc_cursor;
105 	tc_putchar_t	*tc_putchar;
106 	tc_fill_t	*tc_fill;
107 	tc_copy_t	*tc_copy;
108 	tc_param_t	*tc_param;
109 	tc_done_t	*tc_done;
110 
111 	/* Low-level console interface. */
112 	tc_cnprobe_t	*tc_cnprobe;
113 	tc_cngetc_t	*tc_cngetc;
114 
115 	/* Misc. */
116 	tc_opened_t	*tc_opened;
117 	tc_ioctl_t	*tc_ioctl;
118 	tc_mmap_t	*tc_mmap;
119 	tc_bell_t	*tc_bell;
120 };
121 
122 struct terminal {
123 	const struct terminal_class *tm_class;
124 	void		*tm_softc;
125 	struct mtx	 tm_mtx;
126 	struct tty	*tm_tty;
127 	teken_t		 tm_emulator;
128 	struct winsize	 tm_winsize;
129 	unsigned int	 tm_flags;
130 #define	TF_MUTE		0x1	/* Drop incoming data. */
131 #define	TF_BELL		0x2	/* Bell needs to be sent. */
132 #define	TF_CONS		0x4	/* Console device (needs spinlock). */
133 	struct consdev	*consdev;
134 };
135 
136 #ifdef _KERNEL
137 
138 struct terminal *terminal_alloc(const struct terminal_class *tc, void *softc);
139 void	terminal_maketty(struct terminal *tm, const char *fmt, ...);
140 void	terminal_set_winsize_blank(struct terminal *tm,
141     const struct winsize *size, int blank);
142 void	terminal_set_winsize(struct terminal *tm, const struct winsize *size);
143 void	terminal_mute(struct terminal *tm, int yes);
144 void	terminal_input_char(struct terminal *tm, term_char_t c);
145 void	terminal_input_raw(struct terminal *tm, char c);
146 void	terminal_input_special(struct terminal *tm, unsigned int k);
147 
148 void	termcn_cnregister(struct terminal *tm);
149 
150 /* Kernel console helper interface. */
151 extern const struct consdev_ops termcn_cnops;
152 
153 #define	TERMINAL_DECLARE_EARLY(name, class, softc)			\
154 	static struct terminal name = {					\
155 		.tm_class = &class,					\
156 		.tm_softc = softc,					\
157 		.tm_flags = TF_CONS,					\
158 	};								\
159 	CONSOLE_DEVICE(name ## _consdev, termcn_cnops, &name)
160 
161 #endif /* _KERNEL */
162 
163 #endif /* !_SYS_TERMINAL_H_ */
164