1/*-
2 * Copyright (c) 2009, 2013 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 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: stable/10/sys/dev/vt/vt.h 321198 2017-07-19 13:11:35Z emaste $
33 */
34
35#ifndef _DEV_VT_VT_H_
36#define	_DEV_VT_VT_H_
37
38#include <sys/param.h>
39#include <sys/_lock.h>
40#include <sys/_mutex.h>
41#include <sys/callout.h>
42#include <sys/condvar.h>
43#include <sys/conf.h>
44#include <sys/consio.h>
45#include <sys/kbio.h>
46#include <sys/mouse.h>
47#include <sys/terminal.h>
48#include <sys/sysctl.h>
49
50#include "opt_syscons.h"
51#include "opt_splash.h"
52
53#ifndef	VT_MAXWINDOWS
54#ifdef	MAXCONS
55#define	VT_MAXWINDOWS	MAXCONS
56#else
57#define	VT_MAXWINDOWS	12
58#endif
59#endif
60
61#ifndef VT_ALT_TO_ESC_HACK
62#define	VT_ALT_TO_ESC_HACK	1
63#endif
64
65#define	VT_CONSWINDOW	0
66
67#if defined(SC_TWOBUTTON_MOUSE) || defined(VT_TWOBUTTON_MOUSE)
68#define VT_MOUSE_PASTEBUTTON	MOUSE_BUTTON3DOWN	/* right button */
69#define VT_MOUSE_EXTENDBUTTON	MOUSE_BUTTON2DOWN	/* not really used */
70#else
71#define VT_MOUSE_PASTEBUTTON	MOUSE_BUTTON2DOWN	/* middle button */
72#define VT_MOUSE_EXTENDBUTTON	MOUSE_BUTTON3DOWN	/* right button */
73#endif /* defined(SC_TWOBUTTON_MOUSE) || defined(VT_TWOBUTTON_MOUSE) */
74
75#define	SC_DRIVER_NAME	"vt"
76#ifdef VT_DEBUG
77#define	DPRINTF(_l, ...)	if (vt_debug > (_l)) printf( __VA_ARGS__ )
78#define VT_CONSOLECTL_DEBUG
79#define VT_SYSMOUSE_DEBUG
80#else
81#define	DPRINTF(_l, ...)	do {} while (0)
82#endif
83#define	ISSIGVALID(sig)	((sig) > 0 && (sig) < NSIG)
84
85#define	VT_SYSCTL_INT(_name, _default, _descr)				\
86static int vt_##_name = (_default);					\
87SYSCTL_INT(_kern_vt, OID_AUTO, _name, CTLFLAG_RWTUN, &vt_##_name, 0,	\
88		_descr);						\
89TUNABLE_INT("kern.vt." #_name, &vt_##_name)
90
91struct vt_driver;
92
93void vt_allocate(struct vt_driver *, void *);
94
95typedef unsigned int 	vt_axis_t;
96
97/*
98 * List of locks
99 * (d)	locked by vd_lock
100 * (b)	locked by vb_lock
101 * (G)	locked by Giant
102 * (u)	unlocked, locked by higher levels
103 * (c)	const until freeing
104 * (?)	yet to be determined
105 */
106
107/*
108 * Per-device datastructure.
109 */
110
111#ifndef SC_NO_CUTPASTE
112struct vt_mouse_cursor;
113#endif
114
115struct vt_pastebuf {
116	term_char_t		*vpb_buf;	/* Copy-paste buffer. */
117	unsigned int		 vpb_bufsz;	/* Buffer size. */
118	unsigned int		 vpb_len;	/* Length of a last selection. */
119};
120
121struct vt_device {
122	struct vt_window	*vd_windows[VT_MAXWINDOWS]; /* (c) Windows. */
123	struct vt_window	*vd_curwindow;	/* (d) Current window. */
124	struct vt_window	*vd_savedwindow;/* (?) Saved for suspend. */
125	struct vt_pastebuf	 vd_pastebuf;	/* (?) Copy/paste buf. */
126	const struct vt_driver	*vd_driver;	/* (c) Graphics driver. */
127	void			*vd_softc;	/* (u) Driver data. */
128#ifndef SC_NO_CUTPASTE
129	struct vt_mouse_cursor	*vd_mcursor;	/* (?) Cursor bitmap. */
130	term_color_t		 vd_mcursor_fg;	/* (?) Cursor fg color. */
131	term_color_t		 vd_mcursor_bg;	/* (?) Cursor bg color. */
132	vt_axis_t		 vd_mx_drawn;	/* (?) Mouse X and Y      */
133	vt_axis_t		 vd_my_drawn;	/*     as of last redraw. */
134	int			 vd_mshown;	/* (?) Mouse shown during */
135#endif						/*     last redrawn.      */
136	uint16_t		 vd_mx;		/* (?) Current mouse X. */
137	uint16_t		 vd_my;		/* (?) current mouse Y. */
138	uint32_t		 vd_mstate;	/* (?) Mouse state. */
139	vt_axis_t		 vd_width;	/* (?) Screen width. */
140	vt_axis_t		 vd_height;	/* (?) Screen height. */
141	size_t			 vd_transpose;	/* (?) Screen offset in FB */
142	struct mtx		 vd_lock;	/* Per-device lock. */
143	struct cv		 vd_winswitch;	/* (d) Window switch notify. */
144	struct callout		 vd_timer;	/* (d) Display timer. */
145	volatile unsigned int	 vd_timer_armed;/* (?) Display timer started.*/
146	int			 vd_flags;	/* (d) Device flags. */
147#define	VDF_TEXTMODE	0x01	/* Do text mode rendering. */
148#define	VDF_SPLASH	0x02	/* Splash screen active. */
149#define	VDF_ASYNC	0x04	/* vt_timer() running. */
150#define	VDF_INVALID	0x08	/* Entire screen should be re-rendered. */
151#define	VDF_DEAD	0x10	/* Early probing found nothing. */
152#define	VDF_INITIALIZED	0x20	/* vtterm_cnprobe already done. */
153#define	VDF_MOUSECURSOR	0x40	/* Mouse cursor visible. */
154#define	VDF_QUIET_BELL	0x80	/* Disable bell. */
155	int			 vd_keyboard;	/* (G) Keyboard index. */
156	unsigned int		 vd_kbstate;	/* (?) Device unit. */
157	unsigned int		 vd_unit;	/* (c) Device unit. */
158	int			 vd_altbrk;	/* (?) Alt break seq. state */
159};
160
161#define	VD_PASTEBUF(vd)	((vd)->vd_pastebuf.vpb_buf)
162#define	VD_PASTEBUFSZ(vd)	((vd)->vd_pastebuf.vpb_bufsz)
163#define	VD_PASTEBUFLEN(vd)	((vd)->vd_pastebuf.vpb_len)
164
165void vt_resume(struct vt_device *vd);
166void vt_suspend(struct vt_device *vd);
167
168/*
169 * Per-window terminal screen buffer.
170 *
171 * Because redrawing is performed asynchronously, the buffer keeps track
172 * of a rectangle that needs to be redrawn (vb_dirtyrect).  Because this
173 * approach seemed to cause suboptimal performance (when the top left
174 * and the bottom right of the screen are modified), it also uses a set
175 * of bitmasks to keep track of the rows and columns (mod 64) that have
176 * been modified.
177 */
178
179struct vt_buf {
180	struct mtx		 vb_lock;	/* Buffer lock. */
181	term_pos_t		 vb_scr_size;	/* (b) Screen dimensions. */
182	int			 vb_flags;	/* (b) Flags. */
183#define	VBF_CURSOR	0x1	/* Cursor visible. */
184#define	VBF_STATIC	0x2	/* Buffer is statically allocated. */
185#define	VBF_MTX_INIT	0x4	/* Mutex initialized. */
186#define	VBF_SCROLL	0x8	/* scroll locked mode. */
187#define	VBF_HISTORY_FULL 0x10	/* All rows filled. */
188	unsigned int		 vb_history_size;
189	int			 vb_roffset;	/* (b) History rows offset. */
190	int			 vb_curroffset;	/* (b) Saved rows offset. */
191	term_pos_t		 vb_cursor;	/* (u) Cursor position. */
192	term_pos_t		 vb_mark_start;	/* (b) Copy region start. */
193	term_pos_t		 vb_mark_end;	/* (b) Copy region end. */
194	int			 vb_mark_last;	/* Last mouse event. */
195	term_rect_t		 vb_dirtyrect;	/* (b) Dirty rectangle. */
196	term_char_t		*vb_buffer;	/* (u) Data buffer. */
197	term_char_t		**vb_rows;	/* (u) Array of rows */
198};
199
200#ifdef SC_HISTORY_SIZE
201#define	VBF_DEFAULT_HISTORY_SIZE	SC_HISTORY_SIZE
202#else
203#define	VBF_DEFAULT_HISTORY_SIZE	500
204#endif
205
206void vtbuf_copy(struct vt_buf *, const term_rect_t *, const term_pos_t *);
207void vtbuf_fill_locked(struct vt_buf *, const term_rect_t *, term_char_t);
208void vtbuf_init_early(struct vt_buf *);
209void vtbuf_init(struct vt_buf *, const term_pos_t *);
210void vtbuf_grow(struct vt_buf *, const term_pos_t *, unsigned int);
211void vtbuf_putchar(struct vt_buf *, const term_pos_t *, term_char_t);
212void vtbuf_cursor_position(struct vt_buf *, const term_pos_t *);
213void vtbuf_scroll_mode(struct vt_buf *vb, int yes);
214void vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area);
215void vtbuf_undirty(struct vt_buf *, term_rect_t *);
216void vtbuf_sethistory_size(struct vt_buf *, int);
217int vtbuf_iscursor(const struct vt_buf *vb, int row, int col);
218void vtbuf_cursor_visibility(struct vt_buf *, int);
219#ifndef SC_NO_CUTPASTE
220int vtbuf_set_mark(struct vt_buf *vb, int type, int col, int row);
221int vtbuf_get_marked_len(struct vt_buf *vb);
222void vtbuf_extract_marked(struct vt_buf *vb, term_char_t *buf, int sz);
223#endif
224
225#define	VTB_MARK_NONE		0
226#define	VTB_MARK_END		1
227#define	VTB_MARK_START		2
228#define	VTB_MARK_WORD		3
229#define	VTB_MARK_ROW		4
230#define	VTB_MARK_EXTEND		5
231#define	VTB_MARK_MOVE		6
232
233#define	VTBUF_SLCK_ENABLE(vb)	vtbuf_scroll_mode((vb), 1)
234#define	VTBUF_SLCK_DISABLE(vb)	vtbuf_scroll_mode((vb), 0)
235
236#define	VTBUF_MAX_HEIGHT(vb) \
237	((vb)->vb_history_size)
238#define	VTBUF_GET_ROW(vb, r) \
239	((vb)->vb_rows[((vb)->vb_roffset + (r)) % VTBUF_MAX_HEIGHT(vb)])
240#define	VTBUF_GET_FIELD(vb, r, c) \
241	((vb)->vb_rows[((vb)->vb_roffset + (r)) % VTBUF_MAX_HEIGHT(vb)][(c)])
242#define	VTBUF_FIELD(vb, r, c) \
243	((vb)->vb_rows[((vb)->vb_curroffset + (r)) % VTBUF_MAX_HEIGHT(vb)][(c)])
244#define	VTBUF_ISCURSOR(vb, r, c) \
245	vtbuf_iscursor((vb), (r), (c))
246#define	VTBUF_DIRTYROW(mask, row) \
247	((mask)->vbm_row & ((uint64_t)1 << ((row) % 64)))
248#define	VTBUF_DIRTYCOL(mask, col) \
249	((mask)->vbm_col & ((uint64_t)1 << ((col) % 64)))
250#define	VTBUF_SPACE_CHAR(attr)	(' ' | (attr))
251
252#define	VHS_SET	0
253#define	VHS_CUR	1
254#define	VHS_END	2
255int vthistory_seek(struct vt_buf *, int offset, int whence);
256void vthistory_addlines(struct vt_buf *vb, int offset);
257void vthistory_getpos(const struct vt_buf *, unsigned int *offset);
258
259/*
260 * Per-window datastructure.
261 */
262
263struct vt_window {
264	struct vt_device	*vw_device;	/* (c) Device. */
265	struct terminal		*vw_terminal;	/* (c) Terminal. */
266	struct vt_buf		 vw_buf;	/* (u) Screen buffer. */
267	struct vt_font		*vw_font;	/* (d) Graphical font. */
268	term_rect_t		 vw_draw_area;	/* (?) Drawable area. */
269	unsigned int		 vw_number;	/* (c) Window number. */
270	int			 vw_kbdmode;	/* (?) Keyboard mode. */
271	int			 vw_prev_kbdmode;/* (?) Previous mode. */
272	int			 vw_kbdstate;	/* (?) Keyboard state. */
273	int			 vw_grabbed;	/* (?) Grab count. */
274	char			*vw_kbdsq;	/* Escape sequence queue*/
275	unsigned int		 vw_flags;	/* (d) Per-window flags. */
276	int			 vw_mouse_level;/* Mouse op mode. */
277#define	VWF_BUSY	0x1	/* Busy reconfiguring device. */
278#define	VWF_OPENED	0x2	/* TTY in use. */
279#define	VWF_SCROLL	0x4	/* Keys influence scrollback. */
280#define	VWF_CONSOLE	0x8	/* Kernel message console window. */
281#define	VWF_VTYLOCK	0x10	/* Prevent window switch. */
282#define	VWF_MOUSE_HIDE	0x20	/* Disable mouse events processing. */
283#define	VWF_READY	0x40	/* Window fully initialized. */
284#define	VWF_GRAPHICS	0x80	/* Window in graphics mode (KDSETMODE). */
285#define	VWF_SWWAIT_REL	0x10000	/* Program wait for VT acquire is done. */
286#define	VWF_SWWAIT_ACQ	0x20000	/* Program wait for VT release is done. */
287	pid_t			 vw_pid;	/* Terminal holding process */
288	struct proc		*vw_proc;
289	struct vt_mode		 vw_smode;	/* switch mode */
290	struct callout		 vw_proc_dead_timer;
291	struct vt_window	*vw_switch_to;
292};
293
294#define	VT_AUTO		0		/* switching is automatic */
295#define	VT_PROCESS	1		/* switching controlled by prog */
296#define	VT_KERNEL	255		/* switching controlled in kernel */
297
298#define	IS_VT_PROC_MODE(vw)	((vw)->vw_smode.mode == VT_PROCESS)
299
300/*
301 * Per-device driver routines.
302 */
303
304typedef int vd_init_t(struct vt_device *vd);
305typedef int vd_probe_t(struct vt_device *vd);
306typedef void vd_postswitch_t(struct vt_device *vd);
307typedef void vd_blank_t(struct vt_device *vd, term_color_t color);
308typedef void vd_bitblt_text_t(struct vt_device *vd, const struct vt_window *vw,
309    const term_rect_t *area);
310typedef void vd_bitblt_bmp_t(struct vt_device *vd, const struct vt_window *vw,
311    const uint8_t *pattern, const uint8_t *mask,
312    unsigned int width, unsigned int height,
313    unsigned int x, unsigned int y, term_color_t fg, term_color_t bg);
314typedef int vd_fb_ioctl_t(struct vt_device *, u_long, caddr_t, struct thread *);
315typedef int vd_fb_mmap_t(struct vt_device *, vm_ooffset_t, vm_paddr_t *, int,
316    vm_memattr_t *);
317typedef void vd_drawrect_t(struct vt_device *, int, int, int, int, int,
318    term_color_t);
319typedef void vd_setpixel_t(struct vt_device *, int, int, term_color_t);
320typedef void vd_suspend_t(struct vt_device *);
321typedef void vd_resume_t(struct vt_device *);
322
323struct vt_driver {
324	char		 vd_name[16];
325	/* Console attachment. */
326	vd_probe_t	*vd_probe;
327	vd_init_t	*vd_init;
328
329	/* Drawing. */
330	vd_blank_t	*vd_blank;
331	vd_drawrect_t	*vd_drawrect;
332	vd_setpixel_t	*vd_setpixel;
333	vd_bitblt_text_t *vd_bitblt_text;
334	vd_bitblt_bmp_t	*vd_bitblt_bmp;
335
336	/* Framebuffer ioctls, if present. */
337	vd_fb_ioctl_t	*vd_fb_ioctl;
338
339	/* Framebuffer mmap, if present. */
340	vd_fb_mmap_t	*vd_fb_mmap;
341
342	/* Update display setting on vt switch. */
343	vd_postswitch_t	*vd_postswitch;
344
345	/* Suspend/resume handlers. */
346	vd_suspend_t	*vd_suspend;
347	vd_resume_t	*vd_resume;
348
349	/* Priority to know which one can override */
350	int		vd_priority;
351#define	VD_PRIORITY_DUMB	10
352#define	VD_PRIORITY_GENERIC	100
353#define	VD_PRIORITY_SPECIFIC	1000
354};
355
356/*
357 * Console device madness.
358 *
359 * Utility macro to make early vt(4) instances work.
360 */
361
362extern const struct terminal_class vt_termclass;
363void vt_upgrade(struct vt_device *vd);
364
365#define	PIXEL_WIDTH(w)	((w) / 8)
366#define	PIXEL_HEIGHT(h)	((h) / 16)
367
368#ifndef VT_FB_MAX_WIDTH
369#define	VT_FB_MAX_WIDTH	4096
370#endif
371#ifndef VT_FB_MAX_HEIGHT
372#define	VT_FB_MAX_HEIGHT	2400
373#endif
374
375/* name argument is not used yet. */
376#define VT_DRIVER_DECLARE(name, drv) DATA_SET(vt_drv_set, drv)
377
378/*
379 * Fonts.
380 *
381 * Remapping tables are used to map Unicode points to glyphs.  They need
382 * to be sorted, because vtfont_lookup() performs a binary search.  Each
383 * font has two remapping tables, for normal and bold.  When a character
384 * is not present in bold, it uses a normal glyph.  When no glyph is
385 * available, it uses glyph 0, which is normally equal to U+FFFD.
386 */
387
388struct vt_font_map {
389	uint32_t		 vfm_src;
390	uint16_t		 vfm_dst;
391	uint16_t		 vfm_len;
392};
393
394struct vt_font {
395	struct vt_font_map	*vf_map[VFNT_MAPS];
396	uint8_t			*vf_bytes;
397	unsigned int		 vf_height, vf_width;
398	unsigned int		 vf_map_count[VFNT_MAPS];
399	unsigned int		 vf_refcount;
400};
401
402#ifndef SC_NO_CUTPASTE
403struct vt_mouse_cursor {
404	uint8_t map[64 * 64 / 8];
405	uint8_t mask[64 * 64 / 8];
406	uint8_t width;
407	uint8_t height;
408};
409#endif
410
411const uint8_t	*vtfont_lookup(const struct vt_font *vf, term_char_t c);
412struct vt_font	*vtfont_ref(struct vt_font *vf);
413void		 vtfont_unref(struct vt_font *vf);
414int		 vtfont_load(vfnt_t *f, struct vt_font **ret);
415
416/* Sysmouse. */
417void sysmouse_process_event(mouse_info_t *mi);
418#ifndef SC_NO_CUTPASTE
419void vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel);
420void vt_mouse_state(int show);
421#endif
422#define	VT_MOUSE_SHOW 1
423#define	VT_MOUSE_HIDE 0
424
425/* Utilities. */
426void	vt_determine_colors(term_char_t c, int cursor,
427	    term_color_t *fg, term_color_t *bg);
428int	vt_is_cursor_in_area(const struct vt_device *vd,
429	    const term_rect_t *area);
430
431#endif /* !_DEV_VT_VT_H_ */
432
433