terminal.h revision 256143
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: user/ed/newcons/sys/sys/terminal.h 256143 2013-10-08 12:01:17Z ray $
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
44219888Sedstruct terminal;
45219888Sedstruct thread;
46219888Sedstruct tty;
47219888Sed
48219888Sed/*
49219888Sed * The terminal layer is an abstraction on top of the TTY layer and the
50219888Sed * console interface.  It can be used by system console drivers to
51219888Sed * easily interact with the kernel console and TTYs.
52219888Sed *
53219888Sed * Terminals contain terminal emulators, which means console drivers
54219888Sed * don't need to implement their own terminal emulator. The terminal
55219888Sed * emulator deals with UTF-8 exclusively. This means that term_char_t,
56219888Sed * the data type used to store input/output characters will always
57219888Sed * contain Unicode codepoints.
58219888Sed *
59219888Sed * To save memory usage, the top bits of term_char_t will contain other
60219888Sed * attributes, like colors. Right now term_char_t is composed as
61219888Sed * follows:
62219888Sed *
63219888Sed *  Bits  Meaning
64219888Sed *  0-20: Character value
65219888Sed *    21: Unused
66219888Sed * 22-25: Bold, underline, blink, reverse
67219888Sed * 26-28: Foreground color
68219888Sed * 29-31: Background color
69219888Sed */
70219888Sed
71219888Sedtypedef uint32_t term_char_t;
72219888Sed#define	TCHAR_CHARACTER(c)	((c) & 0x1fffff)
73219888Sed#define	TCHAR_FORMAT(c)		(((c) >> 22) & 0xf)
74219888Sed#define	TCHAR_FGCOLOR(c)	(((c) >> 26) & 0x7)
75219888Sed#define	TCHAR_BGCOLOR(c)	((c) >> 29)
76219888Sed
77219888Sedtypedef teken_color_t term_color_t;
78219888Sed#define	TCOLOR_LIGHT(c)	((c) | 0x8)
79219888Sed#define	TCOLOR_DARK(c)	((c) & ~0x8)
80219888Sedtypedef teken_pos_t term_pos_t;
81219888Sedtypedef teken_rect_t term_rect_t;
82219888Sed
83219888Sedtypedef void tc_cursor_t(struct terminal *tm, const term_pos_t *p);
84219888Sedtypedef void tc_putchar_t(struct terminal *tm, const term_pos_t *p,
85219888Sed    term_char_t c);
86219888Sedtypedef void tc_fill_t(struct terminal *tm, const term_rect_t *r,
87219888Sed    term_char_t c);
88219888Sedtypedef void tc_copy_t(struct terminal *tm, const term_rect_t *r,
89219888Sed    const term_pos_t *p);
90219888Sedtypedef void tc_param_t(struct terminal *tm, int cmd, unsigned int arg);
91219888Sedtypedef void tc_done_t(struct terminal *tm);
92219888Sed
93219888Sedtypedef void tc_cnprobe_t(struct terminal *tm, struct consdev *cd);
94219888Sedtypedef int tc_cngetc_t(struct terminal *tm);
95219888Sed
96219888Sedtypedef void tc_opened_t(struct terminal *tm, int opened);
97219888Sedtypedef int tc_ioctl_t(struct terminal *tm, u_long cmd, caddr_t data,
98219888Sed    struct thread *td);
99219888Sedtypedef void tc_bell_t(struct terminal *tm);
100219888Sed
101219888Sedstruct terminal_class {
102219888Sed	/* Terminal emulator. */
103219888Sed	tc_cursor_t	*tc_cursor;
104219888Sed	tc_putchar_t	*tc_putchar;
105219888Sed	tc_fill_t	*tc_fill;
106219888Sed	tc_copy_t	*tc_copy;
107219888Sed	tc_param_t	*tc_param;
108219888Sed	tc_done_t	*tc_done;
109219888Sed
110219888Sed	/* Low-level console interface. */
111219888Sed	tc_cnprobe_t	*tc_cnprobe;
112219888Sed	tc_cngetc_t	*tc_cngetc;
113219888Sed
114219888Sed	/* Misc. */
115219888Sed	tc_opened_t	*tc_opened;
116219888Sed	tc_ioctl_t	*tc_ioctl;
117219888Sed	tc_bell_t	*tc_bell;
118219888Sed};
119219888Sed
120219888Sedstruct terminal {
121219888Sed	const struct terminal_class *tm_class;
122219888Sed	void		*tm_softc;
123219888Sed	struct mtx	 tm_mtx;
124219888Sed	struct tty	*tm_tty;
125219888Sed	teken_t		 tm_emulator;
126219888Sed	struct winsize	 tm_winsize;
127219888Sed	unsigned int	 tm_flags;
128219888Sed#define	TF_MUTE		0x1	/* Drop incoming data. */
129219888Sed#define	TF_BELL		0x2	/* Bell needs to be sent. */
130219888Sed#define	TF_CONS		0x4	/* Console device (needs spinlock). */
131256143Sray	struct consdev	*consdev;
132219888Sed};
133219888Sed
134219888Sed#ifdef _KERNEL
135219888Sed
136219888Sedstruct terminal *terminal_alloc(const struct terminal_class *tc, void *softc);
137219888Sedvoid	terminal_maketty(struct terminal *tm, const char *fmt, ...);
138219888Sedvoid	terminal_set_winsize(struct terminal *tm, const struct winsize *size);
139219888Sedvoid	terminal_mute(struct terminal *tm, int yes);
140219888Sedvoid	terminal_input_char(struct terminal *tm, term_char_t c);
141219888Sedvoid	terminal_input_raw(struct terminal *tm, char c);
142219888Sedvoid	terminal_input_special(struct terminal *tm, unsigned int k);
143219888Sed
144256143Srayvoid	termcn_cnregister(struct terminal *tm);
145256143Sray
146219888Sed/* Kernel console helper interface. */
147256143Srayextern const struct consdev_ops termcn_cnops;
148219888Sed
149219888Sed#define	TERMINAL_DECLARE_EARLY(name, class, softc)			\
150219888Sed	static struct terminal name = {					\
151219888Sed		.tm_class = &class,					\
152219888Sed		.tm_softc = softc,					\
153219888Sed		.tm_flags = TF_CONS,					\
154219888Sed	};								\
155256143Sray	CONSOLE_DEVICE(name ## _consdev, termcn_cnops, &name)
156219888Sed
157219888Sed#endif /* _KERNEL */
158219888Sed
159219888Sed#endif /* !_SYS_TERMINAL_H_ */
160