vt.h revision 257723
1178431Sscf/*-
2178431Sscf * Copyright (c) 2009, 2013 The FreeBSD Foundation
3178431Sscf * All rights reserved.
4178431Sscf *
5178431Sscf * This software was developed by Ed Schouten under sponsorship from the
6178431Sscf * FreeBSD Foundation.
7178431Sscf *
8178431Sscf * Portions of this software were developed by Oleksandr Rybalko
9178431Sscf * under sponsorship from the FreeBSD Foundation.
10178431Sscf *
11178431Sscf * Redistribution and use in source and binary forms, with or without
12178431Sscf * modification, are permitted provided that the following conditions
13178431Sscf * are met:
14178431Sscf * 1. Redistributions of source code must retain the above copyright
15178431Sscf *    notice, this list of conditions and the following disclaimer.
16178431Sscf * 2. Redistributions in binary form must reproduce the above copyright
17178431Sscf *    notice, this list of conditions and the following disclaimer in the
18178431Sscf *    documentation and/or other materials provided with the distribution.
19178431Sscf *
20178431Sscf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21178431Sscf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22178431Sscf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23178431Sscf * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24178431Sscf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25178431Sscf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26178431Sscf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27178431Sscf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28178431Sscf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29178431Sscf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30178431Sscf * SUCH DAMAGE.
31228545Sbapt *
32228545Sbapt * $FreeBSD: user/ed/newcons/sys/dev/vt/vt.h 257723 2013-11-05 22:59:11Z ray $
33184831Sscf */
34228545Sbapt
35228545Sbapt#ifndef _DEV_VT_VT_H_
36228545Sbapt#define	_DEV_VT_VT_H_
37178431Sscf
38178431Sscf#include <sys/param.h>
39184831Sscf#include <sys/_lock.h>
40228545Sbapt#include <sys/_mutex.h>
41178431Sscf#include <sys/callout.h>
42178431Sscf#include <sys/condvar.h>
43178431Sscf#include <sys/consio.h>
44178431Sscf#include <sys/kbio.h>
45228545Sbapt#include <sys/terminal.h>
46178431Sscf#include <sys/sysctl.h>
47185237Sscf
48185237Sscf#define	VT_MAXWINDOWS	12
49185237Sscf#define	VT_CONSWINDOW	0
50185237Sscf
51178431Sscf#define	SC_DRIVER_NAME	"vt"
52228545Sbapt#define	DPRINTF(_l, ...)	if (vt_debug > (_l)) printf( __VA_ARGS__ )
53228545Sbapt#define	ISSIGVALID(sig)	((sig) > 0 && (sig) < NSIG)
54228545Sbapt
55228545Sbapt#define	VT_SYSCTL_INT(_name, _default, _descr)				\
56228545Sbaptstatic int vt_##_name = _default;					\
57228545SbaptSYSCTL_INT(_kern_vt, OID_AUTO, _name, CTLFLAG_RW, &vt_##_name, _default,\
58185237Sscf		_descr);						\
59185237SscfTUNABLE_INT("kern.vt." #_name, &vt_##_name);
60178431Sscf
61228545Sbaptstruct vt_driver;
62228545Sbapt
63228545Sbaptvoid vt_allocate(struct vt_driver *, void *);
64228545Sbapt
65228545Sbapttypedef unsigned int 	vt_axis_t;
66242319Sbapt
67228545Sbapt/*
68228545Sbapt * List of locks
69228545Sbapt * (d)	locked by vd_lock
70228545Sbapt * (b)	locked by vb_lock
71228545Sbapt * (G)	locked by Giant
72228545Sbapt * (u)	unlocked, locked by higher levels
73228545Sbapt * (c)	const until freeing
74228545Sbapt * (?)	yet to be determined
75228545Sbapt */
76228545Sbapt
77228545Sbapt/*
78228545Sbapt * Per-device datastructure.
79228545Sbapt */
80228545Sbapt
81228545Sbaptstruct vt_device {
82228545Sbapt	struct vt_window	*vd_windows[VT_MAXWINDOWS]; /* (c) Windows. */
83228545Sbapt	struct vt_window	*vd_curwindow;	/* (d) Current window. */
84228545Sbapt	const struct vt_driver	*vd_driver;	/* (c) Graphics driver. */
85228545Sbapt	void			*vd_softc;	/* (u) Driver data. */
86228545Sbapt	vt_axis_t		 vd_width;	/* (?) Screen width. */
87228545Sbapt	vt_axis_t		 vd_height;	/* (?) Screen height. */
88228545Sbapt	struct mtx		 vd_lock;	/* Per-device lock. */
89228545Sbapt	struct cv		 vd_winswitch;	/* (d) Window switch notify. */
90228545Sbapt	struct callout		 vd_timer;	/* (d) Display timer. */
91228545Sbapt	int			 vd_flags;	/* (d) Device flags. */
92242319Sbapt#define	VDF_TEXTMODE	0x01	/* Do text mode rendering. */
93228545Sbapt#define	VDF_SPLASH	0x02	/* Splash screen active. */
94228545Sbapt#define	VDF_ASYNC	0x04	/* vt_timer() running. */
95228545Sbapt#define	VDF_INVALID	0x08	/* Entire screen should be re-rendered. */
96228545Sbapt#define	VDF_DEAD	0x10	/* Early probing found nothing. */
97228545Sbapt#define	VDF_INITIALIZED	0x20	/* vtterm_cnprobe already done. */
98228545Sbapt	int			 vd_keyboard;	/* (G) Keyboard index. */
99228545Sbapt	unsigned int		 vd_unit;	/* (c) Device unit. */
100228545Sbapt};
101228545Sbapt
102228545Sbapt/*
103228545Sbapt * Per-window terminal screen buffer.
104228545Sbapt *
105228545Sbapt * Because redrawing is performed asynchronously, the buffer keeps track
106228545Sbapt * of a rectangle that needs to be redrawn (vb_dirtyrect).  Because this
107228545Sbapt * approach seemed to cause suboptimal performance (when the top left
108228545Sbapt * and the bottom right of the screen are modified), it also uses a set
109244735Sbapt * of bitmasks to keep track of the rows and columns (mod 64) that have
110244735Sbapt * been modified.
111228545Sbapt */
112228545Sbapt
113228545Sbaptstruct vt_bufmask {
114228545Sbapt	uint64_t		 vbm_row, vbm_col;
115228545Sbapt#define	VBM_DIRTY		UINT64_MAX
116228545Sbapt};
117228545Sbapt
118228545Sbaptstruct vt_buf {
119228545Sbapt	struct mtx		 vb_lock;	/* Buffer lock. */
120228545Sbapt	term_pos_t		 vb_scr_size;	/* (b) Screen dimensions. */
121228545Sbapt	int			 vb_flags;	/* (b) Flags. */
122228545Sbapt#define	VBF_CURSOR	0x1	/* Cursor visible. */
123228545Sbapt#define	VBF_STATIC	0x2	/* Buffer is statically allocated. */
124228545Sbapt#define	VBF_MTX_INIT	0x4	/* Mutex initialized. */
125228545Sbapt#define	VBF_SCROLL	0x8	/* scroll locked mode. */
126228545Sbapt#define	VBF_HISTORY_FULL 0x10	/* All rows filled. */
127228545Sbapt	int			 vb_history_size;
128228545Sbapt#define	VBF_DEFAULT_HISTORY_SIZE	500
129228545Sbapt	int			 vb_roffset;	/* (b) History rows offset. */
130228545Sbapt	int			 vb_curroffset;	/* (b) Saved rows offset. */
131228545Sbapt	term_pos_t		 vb_cursor;	/* (u) Cursor position. */
132228545Sbapt	term_rect_t		 vb_dirtyrect;	/* (b) Dirty rectangle. */
133228545Sbapt	struct vt_bufmask	 vb_dirtymask;	/* (b) Dirty bitmasks. */
134228545Sbapt	term_char_t		*vb_buffer;	/* (u) Data buffer. */
135228545Sbapt	term_char_t		**vb_rows;	/* (u) Array of rows */
136228545Sbapt};
137228545Sbapt
138228545Sbaptvoid vtbuf_copy(struct vt_buf *, const term_rect_t *, const term_pos_t *);
139228545Sbaptvoid vtbuf_fill_locked(struct vt_buf *, const term_rect_t *, term_char_t);
140228545Sbaptvoid vtbuf_init_early(struct vt_buf *);
141228545Sbaptvoid vtbuf_init(struct vt_buf *, const term_pos_t *);
142228545Sbaptvoid vtbuf_grow(struct vt_buf *, const term_pos_t *, int);
143228545Sbaptvoid vtbuf_putchar(struct vt_buf *, const term_pos_t *, term_char_t);
144228545Sbaptvoid vtbuf_cursor_position(struct vt_buf *, const term_pos_t *);
145228545Sbaptvoid vtbuf_cursor_visibility(struct vt_buf *, int);
146228545Sbaptvoid vtbuf_undirty(struct vt_buf *, term_rect_t *, struct vt_bufmask *);
147228545Sbaptvoid vtbuf_sethistory_size(struct vt_buf *, int);
148228545Sbapt
149228545Sbapt#define	VTBUF_SLCK_ENABLE(vb)	(vb)->vb_flags |= VBF_SCROLL
150228545Sbapt#define	VTBUF_SLCK_DISABLE(vb)	(vb)->vb_flags &= ~VBF_SCROLL
151228545Sbapt
152228545Sbapt#define	VTBUF_MAX_HEIGHT(vb) \
153228545Sbapt	((vb)->vb_history_size)
154228545Sbapt#define	VTBUF_GET_ROW(vb, r) \
155228545Sbapt	((vb)->vb_rows[((vb)->vb_roffset + (r)) % VTBUF_MAX_HEIGHT(vb)])
156228545Sbapt#define	VTBUF_GET_FIELD(vb, r, c) \
157228545Sbapt	((vb)->vb_rows[((vb)->vb_roffset + (r)) % VTBUF_MAX_HEIGHT(vb)][(c)])
158228545Sbapt#define	VTBUF_FIELD(vb, r, c) \
159228545Sbapt	((vb)->vb_rows[((vb)->vb_curroffset + (r)) % VTBUF_MAX_HEIGHT(vb)][(c)])
160228545Sbapt#define	VTBUF_ISCURSOR(vb, r, c) \
161228545Sbapt	((vb)->vb_flags & VBF_CURSOR && \
162228545Sbapt	(vb)->vb_cursor.tp_row == (r) && (vb)->vb_cursor.tp_col == (c))
163228545Sbapt#define	VTBUF_DIRTYROW(mask, row) \
164228545Sbapt	((mask)->vbm_row & ((uint64_t)1 << ((row) % 64)))
165228545Sbapt#define	VTBUF_DIRTYCOL(mask, col) \
166228545Sbapt	((mask)->vbm_col & ((uint64_t)1 << ((col) % 64)))
167228545Sbapt#define	VTBUF_SPACE_CHAR	(' ' | TC_WHITE << 26 | TC_BLACK << 29)
168228545Sbapt
169228545Sbapt#define	VHS_SET	0
170228545Sbapt#define	VHS_CUR	1
171228545Sbapt#define	VHS_END	2
172228545Sbaptint vthistory_seek(struct vt_buf *, int offset, int whence);
173228545Sbaptvoid vthistory_addlines(struct vt_buf *vb, int offset);
174228545Sbaptvoid vthistory_getpos(const struct vt_buf *, unsigned int *offset);
175228545Sbapt
176228545Sbapt/*
177228545Sbapt * Per-window datastructure.
178228545Sbapt */
179228545Sbapt
180228545Sbaptstruct vt_window {
181228545Sbapt	struct vt_device	*vw_device;	/* (c) Device. */
182228545Sbapt	struct terminal		*vw_terminal;	/* (c) Terminal. */
183228545Sbapt	struct vt_buf		 vw_buf;	/* (u) Screen buffer. */
184228545Sbapt	struct vt_font		*vw_font;	/* (d) Graphical font. */
185228545Sbapt	unsigned int		 vw_number;	/* (c) Window number. */
186228545Sbapt	int			 vw_kbdmode;	/* (?) Keyboard mode. */
187228545Sbapt	char			*vw_kbdsq;	/* Escape sequence queue*/
188228545Sbapt	unsigned int		 vw_flags;	/* (d) Per-window flags. */
189228545Sbapt#define	VWF_BUSY	0x1	/* Busy reconfiguring device. */
190228545Sbapt#define	VWF_OPENED	0x2	/* TTY in use. */
191228545Sbapt#define	VWF_SCROLL	0x4	/* Keys influence scrollback. */
192228545Sbapt#define	VWF_CONSOLE	0x8	/* Kernel message console window. */
193228545Sbapt#define	VWF_VTYLOCK	0x10	/* Prevent window switch. */
194228545Sbapt#define	VWF_SWWAIT_REL	0x10000	/* Program wait for VT acquire is done. */
195228545Sbapt#define	VWF_SWWAIT_ACQ	0x20000	/* Program wait for VT release is done. */
196228545Sbapt	pid_t			 vw_pid;	/* Terminal holding process */
197228545Sbapt	struct proc		*vw_proc;
198228545Sbapt	struct vt_mode		 vw_smode;	/* switch mode */
199228545Sbapt	struct callout		 vw_proc_dead_timer;
200228545Sbapt	struct vt_window	*vw_switch_to;
201228545Sbapt};
202228545Sbapt
203228545Sbapt#define	VT_AUTO		0		/* switching is automatic */
204228545Sbapt#define	VT_PROCESS	1		/* switching controlled by prog */
205228545Sbapt#define	VT_KERNEL	255		/* switching controlled in kernel */
206228545Sbapt
207228545Sbapt#define	IS_VT_PROC_MODE(vw)	((vw)->vw_smode.mode == VT_PROCESS)
208228545Sbapt
209228545Sbapt/*
210228545Sbapt * Per-device driver routines.
211228545Sbapt *
212228545Sbapt * vd_bitbltchr is used when the driver operates in graphics mode, while
213228545Sbapt * vd_putchar is used when the driver operates in text mode
214228545Sbapt * (VDF_TEXTMODE).
215228545Sbapt */
216228545Sbapt
217228545Sbapttypedef int vd_init_t(struct vt_device *vd);
218228545Sbapttypedef void vd_postswitch_t(struct vt_device *vd);
219228545Sbapttypedef void vd_blank_t(struct vt_device *vd, term_color_t color);
220228545Sbapttypedef void vd_bitbltchr_t(struct vt_device *vd, const uint8_t *src,
221228545Sbapt    vt_axis_t top, vt_axis_t left, unsigned int width, unsigned int height,
222228545Sbapt    term_color_t fg, term_color_t bg);
223228545Sbapttypedef void vd_putchar_t(struct vt_device *vd, term_char_t,
224228545Sbapt    vt_axis_t top, vt_axis_t left, term_color_t fg, term_color_t bg);
225228545Sbapt
226228545Sbaptstruct vt_driver {
227228545Sbapt	/* Console attachment. */
228228545Sbapt	vd_init_t	*vd_init;
229228545Sbapt
230228545Sbapt	/* Drawing. */
231228545Sbapt	vd_blank_t	*vd_blank;
232228545Sbapt	vd_bitbltchr_t	*vd_bitbltchr;
233228545Sbapt
234228545Sbapt	/* Text mode operation. */
235228545Sbapt	vd_putchar_t	*vd_putchar;
236228545Sbapt
237228545Sbapt	/* Update display setting on vt switch. */
238228545Sbapt	vd_postswitch_t	*vd_postswitch;
239228545Sbapt
240228545Sbapt	/* Priority to know which one can override */
241228545Sbapt	int		vd_priority;
242228545Sbapt#define	VD_PRIORITY_DUMB	10
243228545Sbapt#define	VD_PRIORITY_GENERIC	100
244228545Sbapt#define	VD_PRIORITY_SPECIFIC	1000
245228545Sbapt};
246228545Sbapt
247228545Sbapt/*
248228545Sbapt * Console device madness.
249228545Sbapt *
250228545Sbapt * Utility macro to make early vt(4) instances work.
251228545Sbapt */
252228545Sbapt
253228545Sbaptextern const struct terminal_class vt_termclass;
254228545Sbaptvoid vt_upgrade(struct vt_device *vd);
255228545Sbapt
256228545Sbapt#define	PIXEL_WIDTH(w)	((w) / 8)
257228545Sbapt#define	PIXEL_HEIGHT(h)	((h) / 16)
258228545Sbapt
259228545Sbapt#define	VT_CONSDEV_DECLARE(driver, width, height, softc)		\
260228545Sbaptstatic struct terminal	driver ## _consterm;				\
261228545Sbaptstatic struct vt_window	driver ## _conswindow;				\
262228545Sbaptstatic struct vt_device	driver ## _consdev = {				\
263228545Sbapt	.vd_driver = &driver,						\
264228545Sbapt	.vd_softc = (softc),						\
265228545Sbapt	.vd_flags = VDF_INVALID,					\
266228545Sbapt	.vd_windows = { [VT_CONSWINDOW] =  &driver ## _conswindow, },	\
267228545Sbapt	.vd_curwindow = &driver ## _conswindow,				\
268228545Sbapt};									\
269228545Sbaptstatic term_char_t	driver ## _constextbuf[(width) * 		\
270228545Sbapt	    (VBF_DEFAULT_HISTORY_SIZE)];				\
271228545Sbaptstatic term_char_t	*driver ## _constextbufrows[			\
272228545Sbapt	    VBF_DEFAULT_HISTORY_SIZE];					\
273228545Sbaptstatic struct vt_window	driver ## _conswindow = {			\
274228545Sbapt	.vw_number = VT_CONSWINDOW,					\
275228545Sbapt	.vw_flags = VWF_CONSOLE,					\
276228545Sbapt	.vw_buf = {							\
277228545Sbapt		.vb_buffer = driver ## _constextbuf,			\
278228545Sbapt		.vb_rows = driver ## _constextbufrows,			\
279228545Sbapt		.vb_history_size = VBF_DEFAULT_HISTORY_SIZE,		\
280228545Sbapt		.vb_curroffset = 0,					\
281228545Sbapt		.vb_roffset = 0,					\
282228545Sbapt		.vb_flags = VBF_STATIC,					\
283228545Sbapt		.vb_scr_size = {					\
284228545Sbapt			.tp_row = height,				\
285228545Sbapt			.tp_col = width,				\
286228545Sbapt		},							\
287228545Sbapt	},								\
288228545Sbapt	.vw_device = &driver ## _consdev,				\
289228545Sbapt	.vw_terminal = &driver ## _consterm,				\
290228545Sbapt	.vw_kbdmode = K_XLATE,						\
291228545Sbapt};									\
292228545SbaptTERMINAL_DECLARE_EARLY(driver ## _consterm, vt_termclass,		\
293228545Sbapt    &driver ## _conswindow);						\
294228545SbaptSYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY,		\
295228545Sbapt    vt_upgrade, &driver ## _consdev)
296228545Sbapt
297228545Sbapt/*
298228545Sbapt * Fonts.
299228545Sbapt *
300228545Sbapt * Remapping tables are used to map Unicode points to glyphs.  They need
301228545Sbapt * to be sorted, because vtfont_lookup() performs a binary search.  Each
302228545Sbapt * font has two remapping tables, for normal and bold.  When a character
303228545Sbapt * is not present in bold, it uses a normal glyph.  When no glyph is
304228545Sbapt * available, it uses glyph 0, which is normally equal to U+FFFD.
305228545Sbapt */
306228545Sbapt
307228545Sbaptstruct vt_font_map {
308228545Sbapt	uint32_t		 vfm_src;
309228545Sbapt	uint16_t		 vfm_dst;
310228545Sbapt	uint16_t		 vfm_len;
311228545Sbapt};
312228545Sbapt
313228545Sbaptstruct vt_font {
314228545Sbapt	struct vt_font_map	*vf_bold;
315228545Sbapt	struct vt_font_map	*vf_normal;
316228545Sbapt	uint8_t			*vf_bytes;
317228545Sbapt	unsigned int		 vf_height, vf_width,
318228545Sbapt				 vf_normal_length, vf_bold_length;
319243334Sbapt	unsigned int		 vf_refcount;
320243334Sbapt};
321243328Sbapt
322243334Sbaptconst uint8_t	*vtfont_lookup(const struct vt_font *vf, term_char_t c);
323228545Sbaptstruct vt_font	*vtfont_ref(struct vt_font *vf);
324228545Sbaptvoid		 vtfont_unref(struct vt_font *vf);
325228545Sbaptint		 vtfont_load(vfnt_t *f, struct vt_font **ret);
326228545Sbapt
327228545Sbapt/* Sysmouse. */
328228545Sbaptvoid sysmouse_process_event(mouse_info_t *mi);
329228545Sbapt
330228545Sbapt#endif /* !_DEV_VT_VT_H_ */
331228545Sbapt