1219888Sed/*-
2219888Sed * Copyright (c) 2009 The FreeBSD Foundation
3219888Sed * All rights reserved.
4219888Sed *
5219888Sed * This software was developed by Ed Schouten under sponsorship from the
6219888Sed * FreeBSD Foundation.
7219888Sed *
8219888Sed * Redistribution and use in source and binary forms, with or without
9219888Sed * modification, are permitted provided that the following conditions
10219888Sed * are met:
11219888Sed * 1. Redistributions of source code must retain the above copyright
12219888Sed *    notice, this list of conditions and the following disclaimer.
13219888Sed * 2. Redistributions in binary form must reproduce the above copyright
14219888Sed *    notice, this list of conditions and the following disclaimer in the
15219888Sed *    documentation and/or other materials provided with the distribution.
16219888Sed *
17219888Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18219888Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219888Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219888Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21219888Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219888Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219888Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219888Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219888Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219888Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219888Sed * SUCH DAMAGE.
28219888Sed *
29219888Sed * $FreeBSD$
30219888Sed */
31219888Sed
32219888Sed#ifndef _SYS_TERMINAL_H_
33219888Sed#define	_SYS_TERMINAL_H_
34219888Sed
35219888Sed#include <sys/param.h>
36219888Sed#include <sys/_lock.h>
37219888Sed#include <sys/_mutex.h>
38219888Sed#include <sys/cons.h>
39219888Sed#include <sys/linker_set.h>
40219888Sed#include <sys/ttycom.h>
41219888Sed
42219888Sed#include <teken/teken.h>
43219888Sed
44268037Smarius#include "opt_syscons.h"
45268037Smarius#include "opt_teken.h"
46268037Smarius
47219888Sedstruct terminal;
48219888Sedstruct thread;
49219888Sedstruct tty;
50219888Sed
51219888Sed/*
52219888Sed * The terminal layer is an abstraction on top of the TTY layer and the
53219888Sed * console interface.  It can be used by system console drivers to
54219888Sed * easily interact with the kernel console and TTYs.
55219888Sed *
56219888Sed * Terminals contain terminal emulators, which means console drivers
57219888Sed * don't need to implement their own terminal emulator. The terminal
58219888Sed * emulator deals with UTF-8 exclusively. This means that term_char_t,
59219888Sed * the data type used to store input/output characters will always
60219888Sed * contain Unicode codepoints.
61219888Sed *
62219888Sed * To save memory usage, the top bits of term_char_t will contain other
63219888Sed * attributes, like colors. Right now term_char_t is composed as
64219888Sed * follows:
65219888Sed *
66219888Sed *  Bits  Meaning
67219888Sed *  0-20: Character value
68262861Sjhb * 21-25: Bold, underline, blink, reverse, right part of CJK fullwidth character
69219888Sed * 26-28: Foreground color
70219888Sed * 29-31: Background color
71219888Sed */
72219888Sed
73219888Sedtypedef uint32_t term_char_t;
74219888Sed#define	TCHAR_CHARACTER(c)	((c) & 0x1fffff)
75262861Sjhb#define	TCHAR_FORMAT(c)		(((c) >> 21) & 0x1f)
76219888Sed#define	TCHAR_FGCOLOR(c)	(((c) >> 26) & 0x7)
77268037Smarius#define	TCHAR_BGCOLOR(c)	(((c) >> 29) & 0x7)
78219888Sed
79268037Smariustypedef teken_attr_t term_attr_t;
80268037Smarius
81219888Sedtypedef teken_color_t term_color_t;
82268037Smarius#define	TCOLOR_FG(c)	(((c) & 0x7) << 26)
83268037Smarius#define	TCOLOR_BG(c)	(((c) & 0x7) << 29)
84219888Sed#define	TCOLOR_LIGHT(c)	((c) | 0x8)
85219888Sed#define	TCOLOR_DARK(c)	((c) & ~0x8)
86268037Smarius
87268037Smarius#define	TFORMAT(c)	(((c) & 0x1f) << 21)
88268037Smarius
89268037Smarius/* syscons(4) compatible color attributes for foreground text */
90268037Smarius#define	FG_BLACK		TCOLOR_FG(TC_BLACK)
91268037Smarius#define	FG_BLUE			TCOLOR_FG(TC_BLUE)
92268037Smarius#define	FG_GREEN		TCOLOR_FG(TC_GREEN)
93268037Smarius#define	FG_CYAN			TCOLOR_FG(TC_CYAN)
94268037Smarius#define	FG_RED			TCOLOR_FG(TC_RED)
95268037Smarius#define	FG_MAGENTA		TCOLOR_FG(TC_MAGENTA)
96268037Smarius#define	FG_BROWN		TCOLOR_FG(TC_BROWN)
97268037Smarius#define	FG_LIGHTGREY		TCOLOR_FG(TC_WHITE)
98268037Smarius#define	FG_DARKGREY		(TFORMAT(TF_BOLD) | TCOLOR_FG(TC_BLACK))
99268037Smarius#define	FG_LIGHTBLUE		(TFORMAT(TF_BOLD) | TCOLOR_FG(TC_BLUE))
100268037Smarius#define	FG_LIGHTGREEN		(TFORMAT(TF_BOLD) | TCOLOR_FG(TC_GREEN))
101268037Smarius#define	FG_LIGHTCYAN		(TFORMAT(TF_BOLD) | TCOLOR_FG(TC_CYAN))
102268037Smarius#define	FG_LIGHTRED		(TFORMAT(TF_BOLD) | TCOLOR_FG(TC_RED))
103268037Smarius#define	FG_LIGHTMAGENTA		(TFORMAT(TF_BOLD) | TCOLOR_FG(TC_MAGENTA))
104268037Smarius#define	FG_YELLOW		(TFORMAT(TF_BOLD) | TCOLOR_FG(TC_BROWN))
105268037Smarius#define	FG_WHITE		(TFORMAT(TF_BOLD) | TCOLOR_FG(TC_WHITE))
106268037Smarius#define	FG_BLINK		TFORMAT(TF_BLINK)
107268037Smarius
108268037Smarius/* syscons(4) compatible color attributes for text background */
109268037Smarius#define	BG_BLACK		TCOLOR_BG(TC_BLACK)
110268037Smarius#define	BG_BLUE			TCOLOR_BG(TC_BLUE)
111268037Smarius#define	BG_GREEN		TCOLOR_BG(TC_GREEN)
112268037Smarius#define	BG_CYAN			TCOLOR_BG(TC_CYAN)
113268037Smarius#define	BG_RED			TCOLOR_BG(TC_RED)
114268037Smarius#define	BG_MAGENTA		TCOLOR_BG(TC_MAGENTA)
115268037Smarius#define	BG_BROWN		TCOLOR_BG(TC_BROWN)
116268037Smarius#define	BG_LIGHTGREY		TCOLOR_BG(TC_WHITE)
117268037Smarius#define	BG_DARKGREY		(TFORMAT(TF_BOLD) | TCOLOR_BG(TC_BLACK))
118268037Smarius#define	BG_LIGHTBLUE		(TFORMAT(TF_BOLD) | TCOLOR_BG(TC_BLUE))
119268037Smarius#define	BG_LIGHTGREEN		(TFORMAT(TF_BOLD) | TCOLOR_BG(TC_GREEN))
120268037Smarius#define	BG_LIGHTCYAN		(TFORMAT(TF_BOLD) | TCOLOR_BG(TC_CYAN))
121268037Smarius#define	BG_LIGHTRED		(TFORMAT(TF_BOLD) | TCOLOR_BG(TC_RED))
122268037Smarius#define	BG_LIGHTMAGENTA		(TFORMAT(TF_BOLD) | TCOLOR_BG(TC_MAGENTA))
123268037Smarius#define	BG_YELLOW		(TFORMAT(TF_BOLD) | TCOLOR_BG(TC_BROWN))
124268037Smarius#define	BG_WHITE		(TFORMAT(TF_BOLD) | TCOLOR_BG(TC_WHITE))
125268037Smarius
126268037Smarius#ifndef TERMINAL_NORM_ATTR
127268037Smarius#ifdef SC_NORM_ATTR
128268037Smarius#define	TERMINAL_NORM_ATTR	SC_NORM_ATTR
129268037Smarius#else
130268037Smarius#define	TERMINAL_NORM_ATTR	(FG_LIGHTGREY | BG_BLACK)
131268037Smarius#endif
132268037Smarius#endif
133268037Smarius
134268037Smarius#ifndef TERMINAL_KERN_ATTR
135268037Smarius#ifdef SC_KERNEL_CONS_ATTR
136268037Smarius#define	TERMINAL_KERN_ATTR	SC_KERNEL_CONS_ATTR
137268037Smarius#else
138268037Smarius#define	TERMINAL_KERN_ATTR	(FG_WHITE | BG_BLACK)
139268037Smarius#endif
140268037Smarius#endif
141268037Smarius
142219888Sedtypedef teken_pos_t term_pos_t;
143219888Sedtypedef teken_rect_t term_rect_t;
144219888Sed
145219888Sedtypedef void tc_cursor_t(struct terminal *tm, const term_pos_t *p);
146219888Sedtypedef void tc_putchar_t(struct terminal *tm, const term_pos_t *p,
147219888Sed    term_char_t c);
148219888Sedtypedef void tc_fill_t(struct terminal *tm, const term_rect_t *r,
149219888Sed    term_char_t c);
150219888Sedtypedef void tc_copy_t(struct terminal *tm, const term_rect_t *r,
151219888Sed    const term_pos_t *p);
152219888Sedtypedef void tc_param_t(struct terminal *tm, int cmd, unsigned int arg);
153219888Sedtypedef void tc_done_t(struct terminal *tm);
154219888Sed
155219888Sedtypedef void tc_cnprobe_t(struct terminal *tm, struct consdev *cd);
156219888Sedtypedef int tc_cngetc_t(struct terminal *tm);
157219888Sed
158271769Sdumbbelltypedef void tc_cngrab_t(struct terminal *tm);
159271769Sdumbbelltypedef void tc_cnungrab_t(struct terminal *tm);
160271769Sdumbbell
161219888Sedtypedef void tc_opened_t(struct terminal *tm, int opened);
162219888Sedtypedef int tc_ioctl_t(struct terminal *tm, u_long cmd, caddr_t data,
163219888Sed    struct thread *td);
164262861Sjhbtypedef int tc_mmap_t(struct terminal *tm, vm_ooffset_t offset,
165262861Sjhb    vm_paddr_t * paddr, int nprot, vm_memattr_t *memattr);
166219888Sedtypedef void tc_bell_t(struct terminal *tm);
167219888Sed
168219888Sedstruct terminal_class {
169219888Sed	/* Terminal emulator. */
170219888Sed	tc_cursor_t	*tc_cursor;
171219888Sed	tc_putchar_t	*tc_putchar;
172219888Sed	tc_fill_t	*tc_fill;
173219888Sed	tc_copy_t	*tc_copy;
174219888Sed	tc_param_t	*tc_param;
175219888Sed	tc_done_t	*tc_done;
176219888Sed
177219888Sed	/* Low-level console interface. */
178219888Sed	tc_cnprobe_t	*tc_cnprobe;
179219888Sed	tc_cngetc_t	*tc_cngetc;
180262861Sjhb
181271769Sdumbbell	/* DDB & panic handling. */
182271769Sdumbbell	tc_cngrab_t	*tc_cngrab;
183271769Sdumbbell	tc_cnungrab_t	*tc_cnungrab;
184271769Sdumbbell
185219888Sed	/* Misc. */
186219888Sed	tc_opened_t	*tc_opened;
187219888Sed	tc_ioctl_t	*tc_ioctl;
188262861Sjhb	tc_mmap_t	*tc_mmap;
189219888Sed	tc_bell_t	*tc_bell;
190219888Sed};
191219888Sed
192219888Sedstruct terminal {
193219888Sed	const struct terminal_class *tm_class;
194219888Sed	void		*tm_softc;
195219888Sed	struct mtx	 tm_mtx;
196219888Sed	struct tty	*tm_tty;
197219888Sed	teken_t		 tm_emulator;
198219888Sed	struct winsize	 tm_winsize;
199219888Sed	unsigned int	 tm_flags;
200219888Sed#define	TF_MUTE		0x1	/* Drop incoming data. */
201219888Sed#define	TF_BELL		0x2	/* Bell needs to be sent. */
202219888Sed#define	TF_CONS		0x4	/* Console device (needs spinlock). */
203256143Sray	struct consdev	*consdev;
204219888Sed};
205219888Sed
206219888Sed#ifdef _KERNEL
207219888Sed
208219888Sedstruct terminal *terminal_alloc(const struct terminal_class *tc, void *softc);
209219888Sedvoid	terminal_maketty(struct terminal *tm, const char *fmt, ...);
210274860Sdumbbellvoid	terminal_set_cursor(struct terminal *tm, const term_pos_t *pos);
211256897Srayvoid	terminal_set_winsize_blank(struct terminal *tm,
212268037Smarius    const struct winsize *size, int blank, const term_attr_t *attr);
213219888Sedvoid	terminal_set_winsize(struct terminal *tm, const struct winsize *size);
214219888Sedvoid	terminal_mute(struct terminal *tm, int yes);
215219888Sedvoid	terminal_input_char(struct terminal *tm, term_char_t c);
216219888Sedvoid	terminal_input_raw(struct terminal *tm, char c);
217219888Sedvoid	terminal_input_special(struct terminal *tm, unsigned int k);
218219888Sed
219256143Srayvoid	termcn_cnregister(struct terminal *tm);
220256143Sray
221219888Sed/* Kernel console helper interface. */
222256143Srayextern const struct consdev_ops termcn_cnops;
223219888Sed
224219888Sed#define	TERMINAL_DECLARE_EARLY(name, class, softc)			\
225219888Sed	static struct terminal name = {					\
226219888Sed		.tm_class = &class,					\
227219888Sed		.tm_softc = softc,					\
228219888Sed		.tm_flags = TF_CONS,					\
229219888Sed	};								\
230256143Sray	CONSOLE_DEVICE(name ## _consdev, termcn_cnops, &name)
231219888Sed
232219888Sed#endif /* _KERNEL */
233219888Sed
234219888Sed#endif /* !_SYS_TERMINAL_H_ */
235