vt.h revision 257967
1219888Sed/*-
2257547Sray * Copyright (c) 2009, 2013 The FreeBSD Foundation
3219888Sed * All rights reserved.
4219888Sed *
5219888Sed * This software was developed by Ed Schouten under sponsorship from the
6219888Sed * FreeBSD Foundation.
7219888Sed *
8257547Sray * Portions of this software were developed by Oleksandr Rybalko
9257547Sray * under sponsorship from the FreeBSD Foundation.
10257547Sray *
11219888Sed * Redistribution and use in source and binary forms, with or without
12219888Sed * modification, are permitted provided that the following conditions
13219888Sed * are met:
14219888Sed * 1. Redistributions of source code must retain the above copyright
15219888Sed *    notice, this list of conditions and the following disclaimer.
16219888Sed * 2. Redistributions in binary form must reproduce the above copyright
17219888Sed *    notice, this list of conditions and the following disclaimer in the
18219888Sed *    documentation and/or other materials provided with the distribution.
19219888Sed *
20219888Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21219888Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22219888Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23219888Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24219888Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25219888Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26219888Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27219888Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28219888Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29219888Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30219888Sed * SUCH DAMAGE.
31219888Sed *
32219888Sed * $FreeBSD: user/ed/newcons/sys/dev/vt/vt.h 257967 2013-11-11 11:37:02Z ray $
33219888Sed */
34219888Sed
35219888Sed#ifndef _DEV_VT_VT_H_
36219888Sed#define	_DEV_VT_VT_H_
37219888Sed
38219888Sed#include <sys/param.h>
39219888Sed#include <sys/_lock.h>
40219888Sed#include <sys/_mutex.h>
41219888Sed#include <sys/callout.h>
42219888Sed#include <sys/condvar.h>
43219888Sed#include <sys/consio.h>
44219888Sed#include <sys/kbio.h>
45219888Sed#include <sys/terminal.h>
46256145Sray#include <sys/sysctl.h>
47219888Sed
48257966Sray#include "opt_syscons.h"
49257966Sray
50257966Sray#ifndef	VT_MAXWINDOWS
51257966Sray#ifdef	MAXCONS
52257966Sray#define	VT_MAXWINDOWS	MAXCONS
53257966Sray#else
54219888Sed#define	VT_MAXWINDOWS	12
55257966Sray#endif
56257966Sray#endif
57257966Sray
58219888Sed#define	VT_CONSWINDOW	0
59219888Sed
60257966Sray#if defined(SC_TWOBUTTON_MOUSE) || defined(VT_TWOBUTTON_MOUSE)
61257966Sray#define VT_MOUSE_PASTEBUTTON	MOUSE_BUTTON3DOWN	/* right button */
62257966Sray#define VT_MOUSE_EXTENDBUTTON	MOUSE_BUTTON2DOWN	/* not really used */
63257966Sray#else
64257966Sray#define VT_MOUSE_PASTEBUTTON	MOUSE_BUTTON2DOWN	/* middle button */
65257966Sray#define VT_MOUSE_EXTENDBUTTON	MOUSE_BUTTON3DOWN	/* right button */
66257966Sray#endif /* defined(SC_TWOBUTTON_MOUSE) || defined(VT_TWOBUTTON_MOUSE) */
67257966Sray
68256145Sray#define	SC_DRIVER_NAME	"vt"
69256145Sray#define	DPRINTF(_l, ...)	if (vt_debug > (_l)) printf( __VA_ARGS__ )
70256145Sray#define	ISSIGVALID(sig)	((sig) > 0 && (sig) < NSIG)
71256145Sray
72256145Sray#define	VT_SYSCTL_INT(_name, _default, _descr)				\
73256145Sraystatic int vt_##_name = _default;					\
74256145SraySYSCTL_INT(_kern_vt, OID_AUTO, _name, CTLFLAG_RW, &vt_##_name, _default,\
75256145Sray		_descr);						\
76256145SrayTUNABLE_INT("kern.vt." #_name, &vt_##_name);
77256145Sray
78219888Sedstruct vt_driver;
79219888Sed
80219888Sedvoid vt_allocate(struct vt_driver *, void *);
81257815Srayvoid vt_resume(void);
82257815Srayvoid vt_suspend(void);
83219888Sed
84219888Sedtypedef unsigned int 	vt_axis_t;
85219888Sed
86219888Sed/*
87219888Sed * List of locks
88219888Sed * (d)	locked by vd_lock
89219888Sed * (b)	locked by vb_lock
90219888Sed * (G)	locked by Giant
91219888Sed * (u)	unlocked, locked by higher levels
92219888Sed * (c)	const until freeing
93219888Sed * (?)	yet to be determined
94219888Sed */
95219888Sed
96219888Sed/*
97219888Sed * Per-device datastructure.
98219888Sed */
99219888Sed
100219888Sedstruct vt_device {
101219888Sed	struct vt_window	*vd_windows[VT_MAXWINDOWS]; /* (c) Windows. */
102219888Sed	struct vt_window	*vd_curwindow;	/* (d) Current window. */
103257815Sray	struct vt_window	*vd_savedwindow;/* (?) Saved for suspend. */
104219888Sed	const struct vt_driver	*vd_driver;	/* (c) Graphics driver. */
105219888Sed	void			*vd_softc;	/* (u) Driver data. */
106219888Sed	vt_axis_t		 vd_width;	/* (?) Screen width. */
107219888Sed	vt_axis_t		 vd_height;	/* (?) Screen height. */
108219888Sed	struct mtx		 vd_lock;	/* Per-device lock. */
109219888Sed	struct cv		 vd_winswitch;	/* (d) Window switch notify. */
110219888Sed	struct callout		 vd_timer;	/* (d) Display timer. */
111219888Sed	int			 vd_flags;	/* (d) Device flags. */
112219888Sed#define	VDF_TEXTMODE	0x01	/* Do text mode rendering. */
113219888Sed#define	VDF_SPLASH	0x02	/* Splash screen active. */
114219888Sed#define	VDF_ASYNC	0x04	/* vt_timer() running. */
115219888Sed#define	VDF_INVALID	0x08	/* Entire screen should be re-rendered. */
116219888Sed#define	VDF_DEAD	0x10	/* Early probing found nothing. */
117256145Sray#define	VDF_INITIALIZED	0x20	/* vtterm_cnprobe already done. */
118219888Sed	int			 vd_keyboard;	/* (G) Keyboard index. */
119219888Sed	unsigned int		 vd_unit;	/* (c) Device unit. */
120219888Sed};
121219888Sed
122219888Sed/*
123219888Sed * Per-window terminal screen buffer.
124219888Sed *
125219888Sed * Because redrawing is performed asynchronously, the buffer keeps track
126219888Sed * of a rectangle that needs to be redrawn (vb_dirtyrect).  Because this
127219888Sed * approach seemed to cause suboptimal performance (when the top left
128219888Sed * and the bottom right of the screen are modified), it also uses a set
129219888Sed * of bitmasks to keep track of the rows and columns (mod 64) that have
130219888Sed * been modified.
131219888Sed */
132219888Sed
133219888Sedstruct vt_bufmask {
134219888Sed	uint64_t		 vbm_row, vbm_col;
135219888Sed#define	VBM_DIRTY		UINT64_MAX
136219888Sed};
137219888Sed
138219888Sedstruct vt_buf {
139219888Sed	struct mtx		 vb_lock;	/* Buffer lock. */
140256145Sray	term_pos_t		 vb_scr_size;	/* (b) Screen dimensions. */
141219888Sed	int			 vb_flags;	/* (b) Flags. */
142219888Sed#define	VBF_CURSOR	0x1	/* Cursor visible. */
143219888Sed#define	VBF_STATIC	0x2	/* Buffer is statically allocated. */
144256145Sray#define	VBF_MTX_INIT	0x4	/* Mutex initialized. */
145256145Sray#define	VBF_SCROLL	0x8	/* scroll locked mode. */
146256964Sray#define	VBF_HISTORY_FULL 0x10	/* All rows filled. */
147256145Sray	int			 vb_history_size;
148257723Sray#define	VBF_DEFAULT_HISTORY_SIZE	500
149256145Sray	int			 vb_roffset;	/* (b) History rows offset. */
150256145Sray	int			 vb_curroffset;	/* (b) Saved rows offset. */
151219888Sed	term_pos_t		 vb_cursor;	/* (u) Cursor position. */
152219888Sed	term_rect_t		 vb_dirtyrect;	/* (b) Dirty rectangle. */
153219888Sed	struct vt_bufmask	 vb_dirtymask;	/* (b) Dirty bitmasks. */
154219888Sed	term_char_t		*vb_buffer;	/* (u) Data buffer. */
155256145Sray	term_char_t		**vb_rows;	/* (u) Array of rows */
156219888Sed};
157219888Sed
158219888Sedvoid vtbuf_copy(struct vt_buf *, const term_rect_t *, const term_pos_t *);
159256145Srayvoid vtbuf_fill_locked(struct vt_buf *, const term_rect_t *, term_char_t);
160219888Sedvoid vtbuf_init_early(struct vt_buf *);
161219888Sedvoid vtbuf_init(struct vt_buf *, const term_pos_t *);
162256145Srayvoid vtbuf_grow(struct vt_buf *, const term_pos_t *, int);
163219888Sedvoid vtbuf_putchar(struct vt_buf *, const term_pos_t *, term_char_t);
164219888Sedvoid vtbuf_cursor_position(struct vt_buf *, const term_pos_t *);
165219888Sedvoid vtbuf_cursor_visibility(struct vt_buf *, int);
166219888Sedvoid vtbuf_undirty(struct vt_buf *, term_rect_t *, struct vt_bufmask *);
167256145Srayvoid vtbuf_sethistory_size(struct vt_buf *, int);
168256145Sray
169256145Sray#define	VTBUF_SLCK_ENABLE(vb)	(vb)->vb_flags |= VBF_SCROLL
170256145Sray#define	VTBUF_SLCK_DISABLE(vb)	(vb)->vb_flags &= ~VBF_SCROLL
171256145Sray
172256145Sray#define	VTBUF_MAX_HEIGHT(vb) \
173256145Sray	((vb)->vb_history_size)
174256145Sray#define	VTBUF_GET_ROW(vb, r) \
175256145Sray	((vb)->vb_rows[((vb)->vb_roffset + (r)) % VTBUF_MAX_HEIGHT(vb)])
176256145Sray#define	VTBUF_GET_FIELD(vb, r, c) \
177256145Sray	((vb)->vb_rows[((vb)->vb_roffset + (r)) % VTBUF_MAX_HEIGHT(vb)][(c)])
178219888Sed#define	VTBUF_FIELD(vb, r, c) \
179256145Sray	((vb)->vb_rows[((vb)->vb_curroffset + (r)) % VTBUF_MAX_HEIGHT(vb)][(c)])
180219888Sed#define	VTBUF_ISCURSOR(vb, r, c) \
181219888Sed	((vb)->vb_flags & VBF_CURSOR && \
182219888Sed	(vb)->vb_cursor.tp_row == (r) && (vb)->vb_cursor.tp_col == (c))
183219888Sed#define	VTBUF_DIRTYROW(mask, row) \
184219888Sed	((mask)->vbm_row & ((uint64_t)1 << ((row) % 64)))
185219888Sed#define	VTBUF_DIRTYCOL(mask, col) \
186219888Sed	((mask)->vbm_col & ((uint64_t)1 << ((col) % 64)))
187256145Sray#define	VTBUF_SPACE_CHAR	(' ' | TC_WHITE << 26 | TC_BLACK << 29)
188219888Sed
189219888Sed#define	VHS_SET	0
190219888Sed#define	VHS_CUR	1
191219888Sed#define	VHS_END	2
192256145Srayint vthistory_seek(struct vt_buf *, int offset, int whence);
193256145Srayvoid vthistory_addlines(struct vt_buf *vb, int offset);
194256145Srayvoid vthistory_getpos(const struct vt_buf *, unsigned int *offset);
195219888Sed
196219888Sed/*
197219888Sed * Per-window datastructure.
198219888Sed */
199219888Sed
200219888Sedstruct vt_window {
201219888Sed	struct vt_device	*vw_device;	/* (c) Device. */
202219888Sed	struct terminal		*vw_terminal;	/* (c) Terminal. */
203219888Sed	struct vt_buf		 vw_buf;	/* (u) Screen buffer. */
204219888Sed	struct vt_font		*vw_font;	/* (d) Graphical font. */
205219888Sed	unsigned int		 vw_number;	/* (c) Window number. */
206219888Sed	int			 vw_kbdmode;	/* (?) Keyboard mode. */
207257076Sray	char			*vw_kbdsq;	/* Escape sequence queue*/
208219888Sed	unsigned int		 vw_flags;	/* (d) Per-window flags. */
209219888Sed#define	VWF_BUSY	0x1	/* Busy reconfiguring device. */
210219888Sed#define	VWF_OPENED	0x2	/* TTY in use. */
211219888Sed#define	VWF_SCROLL	0x4	/* Keys influence scrollback. */
212219888Sed#define	VWF_CONSOLE	0x8	/* Kernel message console window. */
213256145Sray#define	VWF_VTYLOCK	0x10	/* Prevent window switch. */
214256145Sray#define	VWF_SWWAIT_REL	0x10000	/* Program wait for VT acquire is done. */
215256145Sray#define	VWF_SWWAIT_ACQ	0x20000	/* Program wait for VT release is done. */
216256145Sray	pid_t			 vw_pid;	/* Terminal holding process */
217256145Sray	struct proc		*vw_proc;
218256145Sray	struct vt_mode		 vw_smode;	/* switch mode */
219256145Sray	struct callout		 vw_proc_dead_timer;
220256145Sray	struct vt_window	*vw_switch_to;
221219888Sed};
222219888Sed
223256145Sray#define	VT_AUTO		0		/* switching is automatic */
224256145Sray#define	VT_PROCESS	1		/* switching controlled by prog */
225256145Sray#define	VT_KERNEL	255		/* switching controlled in kernel */
226256145Sray
227256145Sray#define	IS_VT_PROC_MODE(vw)	((vw)->vw_smode.mode == VT_PROCESS)
228256145Sray
229219888Sed/*
230219888Sed * Per-device driver routines.
231219888Sed *
232256527Sray * vd_bitbltchr is used when the driver operates in graphics mode, while
233219888Sed * vd_putchar is used when the driver operates in text mode
234219888Sed * (VDF_TEXTMODE).
235219888Sed */
236219888Sed
237219888Sedtypedef int vd_init_t(struct vt_device *vd);
238256145Sraytypedef void vd_postswitch_t(struct vt_device *vd);
239219888Sedtypedef void vd_blank_t(struct vt_device *vd, term_color_t color);
240256527Sraytypedef void vd_bitbltchr_t(struct vt_device *vd, const uint8_t *src,
241219888Sed    vt_axis_t top, vt_axis_t left, unsigned int width, unsigned int height,
242219888Sed    term_color_t fg, term_color_t bg);
243219888Sedtypedef void vd_putchar_t(struct vt_device *vd, term_char_t,
244219888Sed    vt_axis_t top, vt_axis_t left, term_color_t fg, term_color_t bg);
245219888Sed
246219888Sedstruct vt_driver {
247219888Sed	/* Console attachment. */
248256145Sray	vd_init_t	*vd_init;
249219888Sed
250219888Sed	/* Drawing. */
251256145Sray	vd_blank_t	*vd_blank;
252256527Sray	vd_bitbltchr_t	*vd_bitbltchr;
253219888Sed
254219888Sed	/* Text mode operation. */
255256145Sray	vd_putchar_t	*vd_putchar;
256256145Sray
257256145Sray	/* Update display setting on vt switch. */
258256145Sray	vd_postswitch_t	*vd_postswitch;
259256145Sray
260256145Sray	/* Priority to know which one can override */
261256145Sray	int		vd_priority;
262256902Sray#define	VD_PRIORITY_DUMB	10
263256902Sray#define	VD_PRIORITY_GENERIC	100
264256902Sray#define	VD_PRIORITY_SPECIFIC	1000
265219888Sed};
266219888Sed
267219888Sed/*
268219888Sed * Console device madness.
269219888Sed *
270219888Sed * Utility macro to make early vt(4) instances work.
271219888Sed */
272219888Sed
273219888Sedextern const struct terminal_class vt_termclass;
274219888Sedvoid vt_upgrade(struct vt_device *vd);
275219888Sed
276219888Sed#define	PIXEL_WIDTH(w)	((w) / 8)
277219888Sed#define	PIXEL_HEIGHT(h)	((h) / 16)
278219888Sed
279257724Sray#ifndef VT_FB_DEFAULT_WIDTH
280257724Sray#define	VT_FB_DEFAULT_WIDTH	640
281257724Sray#endif
282257724Sray#ifndef VT_FB_DEFAULT_HEIGHT
283257724Sray#define	VT_FB_DEFAULT_HEIGHT	480
284257724Sray#endif
285257724Sray
286219888Sed#define	VT_CONSDEV_DECLARE(driver, width, height, softc)		\
287219888Sedstatic struct terminal	driver ## _consterm;				\
288219888Sedstatic struct vt_window	driver ## _conswindow;				\
289219888Sedstatic struct vt_device	driver ## _consdev = {				\
290219888Sed	.vd_driver = &driver,						\
291219888Sed	.vd_softc = (softc),						\
292219888Sed	.vd_flags = VDF_INVALID,					\
293219888Sed	.vd_windows = { [VT_CONSWINDOW] =  &driver ## _conswindow, },	\
294219888Sed	.vd_curwindow = &driver ## _conswindow,				\
295219888Sed};									\
296256145Sraystatic term_char_t	driver ## _constextbuf[(width) * 		\
297256145Sray	    (VBF_DEFAULT_HISTORY_SIZE)];				\
298256145Sraystatic term_char_t	*driver ## _constextbufrows[			\
299256145Sray	    VBF_DEFAULT_HISTORY_SIZE];					\
300219888Sedstatic struct vt_window	driver ## _conswindow = {			\
301219888Sed	.vw_number = VT_CONSWINDOW,					\
302219888Sed	.vw_flags = VWF_CONSOLE,					\
303219888Sed	.vw_buf = {							\
304219888Sed		.vb_buffer = driver ## _constextbuf,			\
305256145Sray		.vb_rows = driver ## _constextbufrows,			\
306256145Sray		.vb_history_size = VBF_DEFAULT_HISTORY_SIZE,		\
307256145Sray		.vb_curroffset = 0,					\
308256145Sray		.vb_roffset = 0,					\
309219888Sed		.vb_flags = VBF_STATIC,					\
310256145Sray		.vb_scr_size = {					\
311219888Sed			.tp_row = height,				\
312219888Sed			.tp_col = width,				\
313219888Sed		},							\
314219888Sed	},								\
315219888Sed	.vw_device = &driver ## _consdev,				\
316219888Sed	.vw_terminal = &driver ## _consterm,				\
317219888Sed	.vw_kbdmode = K_XLATE,						\
318219888Sed};									\
319219888SedTERMINAL_DECLARE_EARLY(driver ## _consterm, vt_termclass,		\
320219888Sed    &driver ## _conswindow);						\
321219888SedSYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY,		\
322219888Sed    vt_upgrade, &driver ## _consdev)
323219888Sed
324219888Sed/*
325219888Sed * Fonts.
326219888Sed *
327219888Sed * Remapping tables are used to map Unicode points to glyphs.  They need
328219888Sed * to be sorted, because vtfont_lookup() performs a binary search.  Each
329219888Sed * font has two remapping tables, for normal and bold.  When a character
330219888Sed * is not present in bold, it uses a normal glyph.  When no glyph is
331219888Sed * available, it uses glyph 0, which is normally equal to U+FFFD.
332219888Sed */
333219888Sed
334219888Sedstruct vt_font_map {
335219888Sed	uint32_t		 vfm_src;
336219888Sed	uint16_t		 vfm_dst;
337219888Sed	uint16_t		 vfm_len;
338219888Sed};
339219888Sed
340219888Sedstruct vt_font {
341219888Sed	struct vt_font_map	*vf_bold;
342219888Sed	struct vt_font_map	*vf_normal;
343219888Sed	uint8_t			*vf_bytes;
344219888Sed	unsigned int		 vf_height, vf_width,
345219888Sed				 vf_normal_length, vf_bold_length;
346219888Sed	unsigned int		 vf_refcount;
347219888Sed};
348219888Sed
349257967Sraystruct mouse_cursor {
350257967Sray	uint8_t map[64 * 64 / 8];
351257967Sray	uint8_t mask[64 * 64 / 8];
352257967Sray	uint8_t w;
353257967Sray	uint8_t h;
354257967Sray};
355257967Sray
356219888Sedconst uint8_t	*vtfont_lookup(const struct vt_font *vf, term_char_t c);
357219888Sedstruct vt_font	*vtfont_ref(struct vt_font *vf);
358219888Sedvoid		 vtfont_unref(struct vt_font *vf);
359219888Sedint		 vtfont_load(vfnt_t *f, struct vt_font **ret);
360219888Sed
361219888Sed/* Sysmouse. */
362219888Sedvoid sysmouse_process_event(mouse_info_t *mi);
363219888Sed
364219888Sed#endif /* !_DEV_VT_VT_H_ */
365