xref: /freebsd/sys/dev/vt/vt_core.c (revision 6d732c66bca5da4d261577aad2c8ea84519b0bea)
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 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_compat.h"
37 
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
41 #include <sys/fbio.h>
42 #include <sys/kbio.h>
43 #include <sys/kdb.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/reboot.h>
51 #include <sys/systm.h>
52 #include <sys/terminal.h>
53 
54 #include <dev/kbd/kbdreg.h>
55 #include <dev/vt/vt.h>
56 
57 #if defined(__i386__) || defined(__amd64__)
58 #include <machine/psl.h>
59 #include <machine/frame.h>
60 #endif
61 
62 static tc_bell_t	vtterm_bell;
63 static tc_cursor_t	vtterm_cursor;
64 static tc_putchar_t	vtterm_putchar;
65 static tc_fill_t	vtterm_fill;
66 static tc_copy_t	vtterm_copy;
67 static tc_param_t	vtterm_param;
68 static tc_done_t	vtterm_done;
69 
70 static tc_cnprobe_t	vtterm_cnprobe;
71 static tc_cngetc_t	vtterm_cngetc;
72 
73 static tc_opened_t	vtterm_opened;
74 static tc_ioctl_t	vtterm_ioctl;
75 static tc_mmap_t	vtterm_mmap;
76 
77 const struct terminal_class vt_termclass = {
78 	.tc_bell	= vtterm_bell,
79 	.tc_cursor	= vtterm_cursor,
80 	.tc_putchar	= vtterm_putchar,
81 	.tc_fill	= vtterm_fill,
82 	.tc_copy	= vtterm_copy,
83 	.tc_param	= vtterm_param,
84 	.tc_done	= vtterm_done,
85 
86 	.tc_cnprobe	= vtterm_cnprobe,
87 	.tc_cngetc	= vtterm_cngetc,
88 
89 	.tc_opened	= vtterm_opened,
90 	.tc_ioctl	= vtterm_ioctl,
91 	.tc_mmap	= vtterm_mmap,
92 };
93 
94 /*
95  * Use a constant timer of 25 Hz to redraw the screen.
96  *
97  * XXX: In theory we should only fire up the timer when there is really
98  * activity. Unfortunately we cannot always start timers. We really
99  * don't want to process kernel messages synchronously, because it
100  * really slows down the system.
101  */
102 #define	VT_TIMERFREQ	25
103 
104 /* Bell pitch/duration. */
105 #define VT_BELLDURATION	((5 * hz + 99) / 100)
106 #define VT_BELLPITCH	800
107 
108 #define	VT_LOCK(vd)	mtx_lock(&(vd)->vd_lock)
109 #define	VT_UNLOCK(vd)	mtx_unlock(&(vd)->vd_lock)
110 
111 #define	VT_UNIT(vw)	((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
112 			(vw)->vw_number)
113 
114 /* XXX while syscons is here. */
115 int sc_txtmouse_no_retrace_wait;
116 
117 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
118 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
119 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
120 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
121 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
122 
123 static unsigned int vt_unit = 0;
124 static MALLOC_DEFINE(M_VT, "vt", "vt device");
125 struct vt_device *main_vd = NULL;
126 
127 /* Boot logo. */
128 extern unsigned int vt_logo_width;
129 extern unsigned int vt_logo_height;
130 extern unsigned int vt_logo_depth;
131 extern unsigned char vt_logo_image[];
132 
133 /* Font. */
134 extern struct vt_font vt_font_default;
135 #ifndef SC_NO_CUTPASTE
136 extern struct mouse_cursor vt_default_mouse_pointer;
137 #endif
138 
139 static int signal_vt_rel(struct vt_window *);
140 static int signal_vt_acq(struct vt_window *);
141 static int finish_vt_rel(struct vt_window *, int, int *);
142 static int finish_vt_acq(struct vt_window *);
143 static int vt_window_switch(struct vt_window *);
144 static int vt_late_window_switch(struct vt_window *);
145 static int vt_proc_alive(struct vt_window *);
146 static void vt_resize(struct vt_device *);
147 
148 static void
149 vt_switch_timer(void *arg)
150 {
151 
152 	vt_late_window_switch((struct vt_window *)arg);
153 }
154 
155 static int
156 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
157 {
158 
159 	DPRINTF(40, "%s\n", __func__);
160 	curvw->vw_switch_to = vw;
161 	/* Set timer to allow switch in case when process hang. */
162 	callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
163 	    vt_switch_timer, (void *)vw);
164 	/* Notify process about vt switch attempt. */
165 	DPRINTF(30, "%s: Notify process.\n", __func__);
166 	signal_vt_rel(curvw);
167 
168 	return (0);
169 }
170 
171 static int
172 vt_window_postswitch(struct vt_window *vw)
173 {
174 
175 	signal_vt_acq(vw);
176 	return (0);
177 }
178 
179 /* vt_late_window_switch will done VT switching for regular case. */
180 static int
181 vt_late_window_switch(struct vt_window *vw)
182 {
183 	int ret;
184 
185 	callout_stop(&vw->vw_proc_dead_timer);
186 
187 	ret = vt_window_switch(vw);
188 	if (ret)
189 		return (ret);
190 
191 	/* Notify owner process about terminal availability. */
192 	if (vw->vw_smode.mode == VT_PROCESS) {
193 		ret = vt_window_postswitch(vw);
194 	}
195 	return (ret);
196 }
197 
198 /* Switch window. */
199 static int
200 vt_proc_window_switch(struct vt_window *vw)
201 {
202 	struct vt_window *curvw;
203 	struct vt_device *vd;
204 	int ret;
205 
206 	if (vw->vw_flags & VWF_VTYLOCK)
207 		return (EBUSY);
208 
209 	vd = vw->vw_device;
210 	curvw = vd->vd_curwindow;
211 
212 	/* Ask current process permitions to switch away. */
213 	if (curvw->vw_smode.mode == VT_PROCESS) {
214 		DPRINTF(30, "%s: VT_PROCESS ", __func__);
215 		if (vt_proc_alive(curvw) == FALSE) {
216 			DPRINTF(30, "Dead. Cleaning.");
217 			/* Dead */
218 		} else {
219 			DPRINTF(30, "%s: Signaling process.\n", __func__);
220 			/* Alive, try to ask him. */
221 			ret = vt_window_preswitch(vw, curvw);
222 			/* Wait for process answer or timeout. */
223 			return (ret);
224 		}
225 		DPRINTF(30, "\n");
226 	}
227 
228 	ret = vt_late_window_switch(vw);
229 	return (ret);
230 }
231 
232 /* Switch window ignoring process locking. */
233 static int
234 vt_window_switch(struct vt_window *vw)
235 {
236 	struct vt_device *vd = vw->vw_device;
237 	struct vt_window *curvw = vd->vd_curwindow;
238 	keyboard_t *kbd;
239 
240 	VT_LOCK(vd);
241 	if (curvw == vw) {
242 		/* Nothing to do. */
243 		VT_UNLOCK(vd);
244 		return (0);
245 	}
246 	if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
247 		VT_UNLOCK(vd);
248 		return (EINVAL);
249 	}
250 
251 	vd->vd_curwindow = vw;
252 	vd->vd_flags |= VDF_INVALID;
253 	cv_broadcast(&vd->vd_winswitch);
254 	VT_UNLOCK(vd);
255 
256 	if (vd->vd_driver->vd_postswitch)
257 		vd->vd_driver->vd_postswitch(vd);
258 
259 	/* Restore per-window keyboard mode. */
260 	mtx_lock(&Giant);
261 	kbd = kbd_get_keyboard(vd->vd_keyboard);
262 	if (kbd != NULL) {
263 		kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
264 	}
265 	mtx_unlock(&Giant);
266 	DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
267 
268 	return (0);
269 }
270 
271 static inline void
272 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
273 {
274 
275 	size->tp_row = vd->vd_height;
276 	size->tp_col = vd->vd_width;
277 	if (vf != NULL) {
278 		size->tp_row /= vf->vf_height;
279 		size->tp_col /= vf->vf_width;
280 	}
281 }
282 
283 static inline void
284 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
285 {
286 
287 	size->ws_row = size->ws_ypixel = vd->vd_height;
288 	size->ws_col = size->ws_xpixel = vd->vd_width;
289 	if (vf != NULL) {
290 		size->ws_row /= vf->vf_height;
291 		size->ws_col /= vf->vf_width;
292 	}
293 }
294 
295 static void
296 vt_scroll(struct vt_window *vw, int offset, int whence)
297 {
298 	int diff;
299 	term_pos_t size;
300 
301 	if ((vw->vw_flags & VWF_SCROLL) == 0)
302 		return;
303 
304 	vt_termsize(vw->vw_device, vw->vw_font, &size);
305 
306 	diff = vthistory_seek(&vw->vw_buf, offset, whence);
307 	/*
308 	 * Offset changed, please update Nth lines on sceen.
309 	 * +N - Nth lines at top;
310 	 * -N - Nth lines at bottom.
311 	 */
312 
313 	if (diff < -size.tp_row || diff > size.tp_row) {
314 		vw->vw_device->vd_flags |= VDF_INVALID;
315 		return;
316 	}
317 	vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
318 }
319 
320 static int
321 vt_machine_kbdevent(int c)
322 {
323 
324 	switch (c) {
325 	case SPCLKEY | DBG:
326 		kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
327 		return (1);
328 	case SPCLKEY | RBT:
329 		/* XXX: Make this configurable! */
330 		shutdown_nice(0);
331 		return (1);
332 	case SPCLKEY | HALT:
333 		shutdown_nice(RB_HALT);
334 		return (1);
335 	case SPCLKEY | PDWN:
336 		shutdown_nice(RB_HALT|RB_POWEROFF);
337 		return (1);
338 	};
339 
340 	return (0);
341 }
342 
343 static void
344 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
345 {
346 	struct vt_device *vd;
347 	term_pos_t size;
348 
349 	vd = vw->vw_device;
350 	/* Only special keys handled in ScrollLock mode */
351 	if ((c & SPCLKEY) == 0)
352 		return;
353 
354 	c &= ~SPCLKEY;
355 
356 	if (console == 0) {
357 		if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
358 			vw = vd->vd_windows[c - F_SCR];
359 			if (vw != NULL)
360 				vt_proc_window_switch(vw);
361 			return;
362 		}
363 		VT_LOCK(vd);
364 	}
365 
366 	switch (c) {
367 	case SLK: {
368 		/* Turn scrolling off. */
369 		vt_scroll(vw, 0, VHS_END);
370 		VTBUF_SLCK_DISABLE(&vw->vw_buf);
371 		vw->vw_flags &= ~VWF_SCROLL;
372 		break;
373 	}
374 	case FKEY | F(49): /* Home key. */
375 		vt_scroll(vw, 0, VHS_SET);
376 		break;
377 	case FKEY | F(50): /* Arrow up. */
378 		vt_scroll(vw, -1, VHS_CUR);
379 		break;
380 	case FKEY | F(51): /* Page up. */
381 		vt_termsize(vd, vw->vw_font, &size);
382 		vt_scroll(vw, -size.tp_row, VHS_CUR);
383 		break;
384 	case FKEY | F(57): /* End key. */
385 		vt_scroll(vw, 0, VHS_END);
386 		break;
387 	case FKEY | F(58): /* Arrow down. */
388 		vt_scroll(vw, 1, VHS_CUR);
389 		break;
390 	case FKEY | F(59): /* Page down. */
391 		vt_termsize(vd, vw->vw_font, &size);
392 		vt_scroll(vw, size.tp_row, VHS_CUR);
393 		break;
394 	}
395 
396 	if (console == 0)
397 		VT_UNLOCK(vd);
398 }
399 
400 static int
401 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
402 {
403 	struct vt_window *vw = vd->vd_curwindow;
404 	int state = 0;
405 
406 #if VT_ALT_TO_ESC_HACK
407 	if (c & RELKEY) {
408 		switch (c & ~RELKEY) {
409 		case (SPCLKEY | RALT):
410 			if (vt_enable_altgr != 0)
411 				break;
412 		case (SPCLKEY | LALT):
413 			vd->vd_kbstate &= ~ALKED;
414 		}
415 		/* Other keys ignored for RELKEY event. */
416 		return (0);
417 	} else {
418 		switch (c & ~RELKEY) {
419 		case (SPCLKEY | RALT):
420 			if (vt_enable_altgr != 0)
421 				break;
422 		case (SPCLKEY | LALT):
423 			vd->vd_kbstate |= ALKED;
424 		}
425 	}
426 #else
427 	if (c & RELKEY)
428 		/* Other keys ignored for RELKEY event. */
429 		return (0);
430 #endif
431 
432 	if (vt_machine_kbdevent(c))
433 		return (0);
434 
435 	if (vw->vw_flags & VWF_SCROLL) {
436 		vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
437 		/* Scroll mode keys handled, nothing to do more. */
438 		return (0);
439 	}
440 
441 	if (c & SPCLKEY) {
442 		c &= ~SPCLKEY;
443 
444 		if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
445 			vw = vd->vd_windows[c - F_SCR];
446 			if (vw != NULL)
447 				vt_proc_window_switch(vw);
448 			return (0);
449 		}
450 
451 		switch (c) {
452 		case SLK: {
453 
454 			kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
455 			VT_LOCK(vd);
456 			if (state & SLKED) {
457 				/* Turn scrolling on. */
458 				vw->vw_flags |= VWF_SCROLL;
459 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
460 			} else {
461 				/* Turn scrolling off. */
462 				vw->vw_flags &= ~VWF_SCROLL;
463 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
464 				vt_scroll(vw, 0, VHS_END);
465 			}
466 			VT_UNLOCK(vd);
467 			break;
468 		}
469 		case FKEY | F(1):  case FKEY | F(2):  case FKEY | F(3):
470 		case FKEY | F(4):  case FKEY | F(5):  case FKEY | F(6):
471 		case FKEY | F(7):  case FKEY | F(8):  case FKEY | F(9):
472 		case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
473 			/* F1 through F12 keys. */
474 			terminal_input_special(vw->vw_terminal,
475 			    TKEY_F1 + c - (FKEY | F(1)));
476 			break;
477 		case FKEY | F(49): /* Home key. */
478 			terminal_input_special(vw->vw_terminal, TKEY_HOME);
479 			break;
480 		case FKEY | F(50): /* Arrow up. */
481 			terminal_input_special(vw->vw_terminal, TKEY_UP);
482 			break;
483 		case FKEY | F(51): /* Page up. */
484 			terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
485 			break;
486 		case FKEY | F(53): /* Arrow left. */
487 			terminal_input_special(vw->vw_terminal, TKEY_LEFT);
488 			break;
489 		case FKEY | F(55): /* Arrow right. */
490 			terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
491 			break;
492 		case FKEY | F(57): /* End key. */
493 			terminal_input_special(vw->vw_terminal, TKEY_END);
494 			break;
495 		case FKEY | F(58): /* Arrow down. */
496 			terminal_input_special(vw->vw_terminal, TKEY_DOWN);
497 			break;
498 		case FKEY | F(59): /* Page down. */
499 			terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
500 			break;
501 		case FKEY | F(60): /* Insert key. */
502 			terminal_input_special(vw->vw_terminal, TKEY_INSERT);
503 			break;
504 		case FKEY | F(61): /* Delete key. */
505 			terminal_input_special(vw->vw_terminal, TKEY_DELETE);
506 			break;
507 		}
508 	} else if (KEYFLAGS(c) == 0) {
509 		/* Don't do UTF-8 conversion when doing raw mode. */
510 		if (vw->vw_kbdmode == K_XLATE) {
511 #if VT_ALT_TO_ESC_HACK
512 			if (vd->vd_kbstate & ALKED) {
513 				/*
514 				 * Prepend ESC sequence if one of ALT keys down.
515 				 */
516 				terminal_input_char(vw->vw_terminal, 0x1b);
517 			}
518 #endif
519 
520 			terminal_input_char(vw->vw_terminal, KEYCHAR(c));
521 		} else
522 			terminal_input_raw(vw->vw_terminal, c);
523 	}
524 	return (0);
525 }
526 
527 static int
528 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
529 {
530 	struct vt_device *vd = arg;
531 	int c;
532 
533 	switch (event) {
534 	case KBDIO_KEYINPUT:
535 		break;
536 	case KBDIO_UNLOADING:
537 		mtx_lock(&Giant);
538 		vd->vd_keyboard = -1;
539 		kbd_release(kbd, (void *)&vd->vd_keyboard);
540 		mtx_unlock(&Giant);
541 		return (0);
542 	default:
543 		return (EINVAL);
544 	}
545 
546 	while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
547 		vt_processkey(kbd, vd, c);
548 
549 	return (0);
550 }
551 
552 static int
553 vt_allocate_keyboard(struct vt_device *vd)
554 {
555 	int		 idx0, idx;
556 	keyboard_t	*k0, *k;
557 	keyboard_info_t	 ki;
558 
559 	idx0 = kbd_allocate("kbdmux", -1, (void *)&vd->vd_keyboard,
560 	    vt_kbdevent, vd);
561 	/* XXX: kb_token lost */
562 	vd->vd_keyboard = idx0;
563 	if (idx0 != -1) {
564 		DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
565 		k0 = kbd_get_keyboard(idx0);
566 
567 		for (idx = kbd_find_keyboard2("*", -1, 0);
568 		     idx != -1;
569 		     idx = kbd_find_keyboard2("*", -1, idx + 1)) {
570 			k = kbd_get_keyboard(idx);
571 
572 			if (idx == idx0 || KBD_IS_BUSY(k))
573 				continue;
574 
575 			bzero(&ki, sizeof(ki));
576 			strcpy(ki.kb_name, k->kb_name);
577 			ki.kb_unit = k->kb_unit;
578 
579 			kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
580 		}
581 	} else {
582 		DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
583 		idx0 = kbd_allocate("*", -1, (void *)&vd->vd_keyboard,
584 		    vt_kbdevent, vd);
585 	}
586 	DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
587 
588 	return (idx0);
589 }
590 
591 static void
592 vtterm_bell(struct terminal *tm)
593 {
594 	struct vt_window *vw = tm->tm_softc;
595 	struct vt_device *vd = vw->vw_device;
596 
597 	if (vd->vd_flags & VDF_QUIET_BELL)
598 		return;
599 
600 	sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
601 }
602 
603 static void
604 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
605 {
606 	struct vt_window *vw = tm->tm_softc;
607 
608 	vtbuf_cursor_position(&vw->vw_buf, p);
609 }
610 
611 static void
612 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
613 {
614 	struct vt_window *vw = tm->tm_softc;
615 
616 	vtbuf_putchar(&vw->vw_buf, p, c);
617 }
618 
619 static void
620 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
621 {
622 	struct vt_window *vw = tm->tm_softc;
623 
624 	vtbuf_fill_locked(&vw->vw_buf, r, c);
625 }
626 
627 static void
628 vtterm_copy(struct terminal *tm, const term_rect_t *r,
629     const term_pos_t *p)
630 {
631 	struct vt_window *vw = tm->tm_softc;
632 
633 	vtbuf_copy(&vw->vw_buf, r, p);
634 }
635 
636 static void
637 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
638 {
639 	struct vt_window *vw = tm->tm_softc;
640 
641 	switch (cmd) {
642 	case TP_SHOWCURSOR:
643 		vtbuf_cursor_visibility(&vw->vw_buf, arg);
644 		break;
645 	case TP_MOUSE:
646 		vw->vw_mouse_level = arg;
647 		break;
648 	}
649 }
650 
651 static inline void
652 vt_determine_colors(term_char_t c, int cursor,
653     term_color_t *fg, term_color_t *bg)
654 {
655 
656 	*fg = TCHAR_FGCOLOR(c);
657 	if (TCHAR_FORMAT(c) & TF_BOLD)
658 		*fg = TCOLOR_LIGHT(*fg);
659 	*bg = TCHAR_BGCOLOR(c);
660 
661 	if (TCHAR_FORMAT(c) & TF_REVERSE) {
662 		term_color_t tmp;
663 
664 		tmp = *fg;
665 		*fg = *bg;
666 		*bg = tmp;
667 	}
668 
669 	if (cursor) {
670 		*fg = *bg;
671 		*bg = TC_WHITE;
672 	}
673 }
674 
675 static void
676 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
677     int iscursor, unsigned int row, unsigned int col)
678 {
679 	term_color_t fg, bg;
680 
681 	vt_determine_colors(c, iscursor, &fg, &bg);
682 
683 	if (vf != NULL) {
684 		const uint8_t *src;
685 		vt_axis_t top, left;
686 
687 		src = vtfont_lookup(vf, c);
688 
689 		/*
690 		 * Align the terminal to the centre of the screen.
691 		 * Fonts may not always be able to fill the entire
692 		 * screen.
693 		 */
694 		top = row * vf->vf_height + vd->vd_offset.tp_row;
695 		left = col * vf->vf_width + vd->vd_offset.tp_col;
696 
697 		vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
698 		    vf->vf_width, vf->vf_height, fg, bg);
699 	} else {
700 		vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
701 		    row, col, fg, bg);
702 	}
703 }
704 
705 static void
706 vt_flush(struct vt_device *vd)
707 {
708 	struct vt_window *vw = vd->vd_curwindow;
709 	struct vt_font *vf = vw->vw_font;
710 	struct vt_bufmask tmask;
711 	unsigned int row, col;
712 	term_rect_t tarea;
713 	term_pos_t size;
714 	term_char_t *r;
715 #ifndef SC_NO_CUTPASTE
716 	struct mouse_cursor *m;
717 	int bpl, h, w;
718 #endif
719 
720 	if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
721 		return;
722 
723 	vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
724 	vt_termsize(vd, vf, &size);
725 
726 	/* Force a full redraw when the screen contents are invalid. */
727 	if (vd->vd_flags & VDF_INVALID) {
728 		tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
729 		tarea.tr_end = size;
730 		tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
731 
732 		vd->vd_flags &= ~VDF_INVALID;
733 	}
734 
735 #ifndef SC_NO_CUTPASTE
736 	if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) {
737 		/* Mark last mouse position as dirty to erase. */
738 		vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx,
739 		    vd->vd_mdirtyy);
740 	}
741 #endif
742 
743 	for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
744 		if (!VTBUF_DIRTYROW(&tmask, row))
745 			continue;
746 		r = VTBUF_GET_ROW(&vw->vw_buf, row);
747 		for (col = tarea.tr_begin.tp_col;
748 		    col < tarea.tr_end.tp_col; col++) {
749 			if (!VTBUF_DIRTYCOL(&tmask, col))
750 				continue;
751 
752 			vt_bitblt_char(vd, vf, r[col],
753 			    VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
754 		}
755 	}
756 
757 #ifndef SC_NO_CUTPASTE
758 	/* Mouse disabled. */
759 	if (vw->vw_flags & VWF_MOUSE_HIDE)
760 		return;
761 
762 	/* No mouse for DDB. */
763 	if (kdb_active || panicstr != NULL)
764 		return;
765 
766 	if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
767 	    VDF_MOUSECURSOR) {
768 		m = &vt_default_mouse_pointer;
769 		bpl = (m->w + 7) >> 3; /* Bytes per sorce line. */
770 		w = m->w;
771 		h = m->h;
772 
773 		if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
774 			w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
775 		if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
776 			h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
777 
778 		vd->vd_driver->vd_bitbltchr(vd, m->map, m->mask, bpl,
779 		    vd->vd_offset.tp_row + vd->vd_my,
780 		    vd->vd_offset.tp_col + vd->vd_mx,
781 		    w, h, TC_WHITE, TC_BLACK);
782 		/* Save point of last mouse cursor to erase it later. */
783 		vd->vd_mdirtyx = vd->vd_mx / vf->vf_width;
784 		vd->vd_mdirtyy = vd->vd_my / vf->vf_height;
785 	}
786 #endif
787 }
788 
789 static void
790 vt_timer(void *arg)
791 {
792 	struct vt_device *vd;
793 
794 	vd = arg;
795 	/* Update screen if required. */
796 	vt_flush(vd);
797 	/* Schedule for next update. */
798 	callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
799 }
800 
801 static void
802 vtterm_done(struct terminal *tm)
803 {
804 	struct vt_window *vw = tm->tm_softc;
805 	struct vt_device *vd = vw->vw_device;
806 
807 	if (kdb_active || panicstr != NULL) {
808 		/* Switch to the debugger. */
809 		if (vd->vd_curwindow != vw) {
810 			vd->vd_curwindow = vw;
811 			vd->vd_flags |= VDF_INVALID;
812 			if (vd->vd_driver->vd_postswitch)
813 				vd->vd_driver->vd_postswitch(vd);
814 		}
815 		vd->vd_flags &= ~VDF_SPLASH;
816 		vt_flush(vd);
817 	} else if (!(vd->vd_flags & VDF_ASYNC)) {
818 		vt_flush(vd);
819 	}
820 }
821 
822 #ifdef DEV_SPLASH
823 static void
824 vtterm_splash(struct vt_device *vd)
825 {
826 	vt_axis_t top, left;
827 
828 	/* Display a nice boot splash. */
829 	if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
830 
831 		top = (vd->vd_height - vt_logo_height) / 2;
832 		left = (vd->vd_width - vt_logo_width) / 2;
833 		switch (vt_logo_depth) {
834 		case 1:
835 			/* XXX: Unhardcode colors! */
836 			vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
837 			    top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
838 		}
839 		vd->vd_flags |= VDF_SPLASH;
840 	}
841 }
842 #endif
843 
844 static void
845 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
846 {
847 	struct vt_window *vw = tm->tm_softc;
848 	struct vt_device *vd = vw->vw_device;
849 	struct winsize wsz;
850 
851 	if (vd->vd_flags & VDF_INITIALIZED)
852 		/* Initialization already done. */
853 		return;
854 
855 	cp->cn_pri = vd->vd_driver->vd_init(vd);
856 	if (cp->cn_pri == CN_DEAD) {
857 		vd->vd_flags |= VDF_DEAD;
858 		return;
859 	}
860 
861 	/* Initialize any early-boot keyboard drivers */
862 	kbd_configure(KB_CONF_PROBE_ONLY);
863 
864 	vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
865 	vd->vd_windows[VT_CONSWINDOW] = vw;
866 	sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
867 
868 	if (!(vd->vd_flags & VDF_TEXTMODE))
869 		vw->vw_font = vtfont_ref(&vt_font_default);
870 
871 	vtbuf_init_early(&vw->vw_buf);
872 	vt_winsize(vd, vw->vw_font, &wsz);
873 	terminal_set_winsize(tm, &wsz);
874 
875 #ifdef DEV_SPLASH
876 	vtterm_splash(vd);
877 #endif
878 
879 	vd->vd_flags |= VDF_INITIALIZED;
880 	main_vd = vd;
881 }
882 
883 static int
884 vtterm_cngetc(struct terminal *tm)
885 {
886 	struct vt_window *vw = tm->tm_softc;
887 	struct vt_device *vd = vw->vw_device;
888 	keyboard_t *kbd;
889 	int state;
890 	u_int c;
891 
892 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
893 		return (*vw->vw_kbdsq++);
894 
895 	state = 0;
896 	/* Make sure the splash screen is not there. */
897 	if (vd->vd_flags & VDF_SPLASH) {
898 		/* Remove splash */
899 		vd->vd_flags &= ~VDF_SPLASH;
900 		/* Mark screen as invalid to force update */
901 		vd->vd_flags |= VDF_INVALID;
902 		vt_flush(vd);
903 	}
904 
905 	/* Stripped down keyboard handler. */
906 	kbd = kbd_get_keyboard(vd->vd_keyboard);
907 	if (kbd == NULL)
908 		return (-1);
909 
910 	/* Force keyboard input mode to K_XLATE */
911 	c = K_XLATE;
912 	kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
913 
914 	/* Switch the keyboard to polling to make it work here. */
915 	kbdd_poll(kbd, TRUE);
916 	c = kbdd_read_char(kbd, 0);
917 	kbdd_poll(kbd, FALSE);
918 	if (c & RELKEY)
919 		return (-1);
920 
921 	if (vw->vw_flags & VWF_SCROLL) {
922 		vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
923 		vt_flush(vd);
924 		return (-1);
925 	}
926 
927 	/* Stripped down handling of vt_kbdevent(), without locking, etc. */
928 	if (c & SPCLKEY) {
929 		switch (c) {
930 		case SPCLKEY | SLK:
931 			kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
932 			if (state & SLKED) {
933 				/* Turn scrolling on. */
934 				vw->vw_flags |= VWF_SCROLL;
935 				VTBUF_SLCK_ENABLE(&vw->vw_buf);
936 			} else {
937 				/* Turn scrolling off. */
938 				vt_scroll(vw, 0, VHS_END);
939 				vw->vw_flags &= ~VWF_SCROLL;
940 				VTBUF_SLCK_DISABLE(&vw->vw_buf);
941 			}
942 			break;
943 		/* XXX: KDB can handle history. */
944 		case SPCLKEY | FKEY | F(50): /* Arrow up. */
945 			vw->vw_kbdsq = "\x1b[A";
946 			break;
947 		case SPCLKEY | FKEY | F(58): /* Arrow down. */
948 			vw->vw_kbdsq = "\x1b[B";
949 			break;
950 		case SPCLKEY | FKEY | F(55): /* Arrow right. */
951 			vw->vw_kbdsq = "\x1b[C";
952 			break;
953 		case SPCLKEY | FKEY | F(53): /* Arrow left. */
954 			vw->vw_kbdsq = "\x1b[D";
955 			break;
956 		}
957 
958 		/* Force refresh to make scrollback work. */
959 		vt_flush(vd);
960 	} else if (KEYFLAGS(c) == 0) {
961 		return (KEYCHAR(c));
962 	}
963 
964 	if (vw->vw_kbdsq && *vw->vw_kbdsq)
965 		return (*vw->vw_kbdsq++);
966 
967 	return (-1);
968 }
969 
970 static void
971 vtterm_opened(struct terminal *tm, int opened)
972 {
973 	struct vt_window *vw = tm->tm_softc;
974 	struct vt_device *vd = vw->vw_device;
975 
976 	VT_LOCK(vd);
977 	vd->vd_flags &= ~VDF_SPLASH;
978 	if (opened)
979 		vw->vw_flags |= VWF_OPENED;
980 	else {
981 		vw->vw_flags &= ~VWF_OPENED;
982 		/* TODO: finish ACQ/REL */
983 	}
984 	VT_UNLOCK(vd);
985 }
986 
987 static int
988 vt_change_font(struct vt_window *vw, struct vt_font *vf)
989 {
990 	struct vt_device *vd = vw->vw_device;
991 	struct terminal *tm = vw->vw_terminal;
992 	term_pos_t size;
993 	struct winsize wsz;
994 
995 	/*
996 	 * Changing fonts.
997 	 *
998 	 * Changing fonts is a little tricky.  We must prevent
999 	 * simultaneous access to the device, so we must stop
1000 	 * the display timer and the terminal from accessing.
1001 	 * We need to switch fonts and grow our screen buffer.
1002 	 *
1003 	 * XXX: Right now the code uses terminal_mute() to
1004 	 * prevent data from reaching the console driver while
1005 	 * resizing the screen buffer.  This isn't elegant...
1006 	 */
1007 
1008 	VT_LOCK(vd);
1009 	if (vw->vw_flags & VWF_BUSY) {
1010 		/* Another process is changing the font. */
1011 		VT_UNLOCK(vd);
1012 		return (EBUSY);
1013 	}
1014 	if (vw->vw_font == NULL) {
1015 		/* Our device doesn't need fonts. */
1016 		VT_UNLOCK(vd);
1017 		return (ENOTTY);
1018 	}
1019 	vw->vw_flags |= VWF_BUSY;
1020 	VT_UNLOCK(vd);
1021 
1022 	vt_termsize(vd, vf, &size);
1023 	vt_winsize(vd, vf, &wsz);
1024 	/* Save offset to font aligned area. */
1025 	vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1026 	vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1027 
1028 	/* Grow the screen buffer and terminal. */
1029 	terminal_mute(tm, 1);
1030 	vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1031 	terminal_set_winsize_blank(tm, &wsz, 0);
1032 	terminal_mute(tm, 0);
1033 
1034 	/* Actually apply the font to the current window. */
1035 	VT_LOCK(vd);
1036 	vtfont_unref(vw->vw_font);
1037 	vw->vw_font = vtfont_ref(vf);
1038 
1039 	/* Force a full redraw the next timer tick. */
1040 	if (vd->vd_curwindow == vw)
1041 		vd->vd_flags |= VDF_INVALID;
1042 	vw->vw_flags &= ~VWF_BUSY;
1043 	VT_UNLOCK(vd);
1044 	return (0);
1045 }
1046 
1047 static int
1048 vt_proc_alive(struct vt_window *vw)
1049 {
1050 	struct proc *p;
1051 
1052 	if (vw->vw_smode.mode != VT_PROCESS)
1053 		return (FALSE);
1054 
1055 	if (vw->vw_proc) {
1056 		if ((p = pfind(vw->vw_pid)) != NULL)
1057 			PROC_UNLOCK(p);
1058 		if (vw->vw_proc == p)
1059 			return (TRUE);
1060 		vw->vw_proc = NULL;
1061 		vw->vw_smode.mode = VT_AUTO;
1062 		DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1063 		vw->vw_pid = 0;
1064 	}
1065 	return (FALSE);
1066 }
1067 
1068 static int
1069 signal_vt_rel(struct vt_window *vw)
1070 {
1071 
1072 	if (vw->vw_smode.mode != VT_PROCESS)
1073 		return (FALSE);
1074 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1075 		vw->vw_proc = NULL;
1076 		vw->vw_pid = 0;
1077 		return (TRUE);
1078 	}
1079 	vw->vw_flags |= VWF_SWWAIT_REL;
1080 	PROC_LOCK(vw->vw_proc);
1081 	kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1082 	PROC_UNLOCK(vw->vw_proc);
1083 	DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1084 	return (TRUE);
1085 }
1086 
1087 static int
1088 signal_vt_acq(struct vt_window *vw)
1089 {
1090 
1091 	if (vw->vw_smode.mode != VT_PROCESS)
1092 		return (FALSE);
1093 	if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1094 		cnavailable(vw->vw_terminal->consdev, FALSE);
1095 	if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1096 		vw->vw_proc = NULL;
1097 		vw->vw_pid = 0;
1098 		return (TRUE);
1099 	}
1100 	vw->vw_flags |= VWF_SWWAIT_ACQ;
1101 	PROC_LOCK(vw->vw_proc);
1102 	kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1103 	PROC_UNLOCK(vw->vw_proc);
1104 	DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1105 	return (TRUE);
1106 }
1107 
1108 static int
1109 finish_vt_rel(struct vt_window *vw, int release, int *s)
1110 {
1111 
1112 	if (vw->vw_flags & VWF_SWWAIT_REL) {
1113 		vw->vw_flags &= ~VWF_SWWAIT_REL;
1114 		if (release) {
1115 			callout_drain(&vw->vw_proc_dead_timer);
1116 			vt_late_window_switch(vw->vw_switch_to);
1117 		}
1118 		return (0);
1119 	}
1120 	return (EINVAL);
1121 }
1122 
1123 static int
1124 finish_vt_acq(struct vt_window *vw)
1125 {
1126 
1127 	if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1128 		vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1129 		return (0);
1130 	}
1131 	return (EINVAL);
1132 }
1133 
1134 #ifndef SC_NO_CUTPASTE
1135 static void
1136 vt_mouse_terminput_button(struct vt_device *vd, int button)
1137 {
1138 	struct vt_window *vw;
1139 	struct vt_font *vf;
1140 	char mouseb[6] = "\x1B[M";
1141 	int i, x, y;
1142 
1143 	vw = vd->vd_curwindow;
1144 	vf = vw->vw_font;
1145 
1146 	/* Translate to char position. */
1147 	x = vd->vd_mx / vf->vf_width;
1148 	y = vd->vd_my / vf->vf_height;
1149 	/* Avoid overflow. */
1150 	x = MIN(x, 255 - '!');
1151 	y = MIN(y, 255 - '!');
1152 
1153 	mouseb[3] = ' ' + button;
1154 	mouseb[4] = '!' + x;
1155 	mouseb[5] = '!' + y;
1156 
1157 	for (i = 0; i < sizeof(mouseb); i++ )
1158 		terminal_input_char(vw->vw_terminal, mouseb[i]);
1159 }
1160 
1161 static void
1162 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1163     int cnt)
1164 {
1165 
1166 	switch (type) {
1167 	case MOUSE_BUTTON_EVENT:
1168 		if (cnt > 0) {
1169 			/* Mouse button pressed. */
1170 			if (event & MOUSE_BUTTON1DOWN)
1171 				vt_mouse_terminput_button(vd, 0);
1172 			if (event & MOUSE_BUTTON2DOWN)
1173 				vt_mouse_terminput_button(vd, 1);
1174 			if (event & MOUSE_BUTTON3DOWN)
1175 				vt_mouse_terminput_button(vd, 2);
1176 		} else {
1177 			/* Mouse button released. */
1178 			vt_mouse_terminput_button(vd, 3);
1179 		}
1180 		break;
1181 #ifdef notyet
1182 	case MOUSE_MOTION_EVENT:
1183 		if (mouse->u.data.z < 0) {
1184 			/* Scroll up. */
1185 			sc_mouse_input_button(vd, 64);
1186 		} else if (mouse->u.data.z > 0) {
1187 			/* Scroll down. */
1188 			sc_mouse_input_button(vd, 65);
1189 		}
1190 		break;
1191 #endif
1192 	}
1193 }
1194 
1195 void
1196 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1197 {
1198 	struct vt_device *vd;
1199 	struct vt_window *vw;
1200 	struct vt_font *vf;
1201 	term_pos_t size;
1202 	term_char_t *buf;
1203 	int i, len, mark;
1204 
1205 	vd = main_vd;
1206 	vw = vd->vd_curwindow;
1207 	vf = vw->vw_font;
1208 	mark = 0;
1209 
1210 	if (vw->vw_flags & VWF_MOUSE_HIDE)
1211 		return; /* Mouse disabled. */
1212 
1213 	if (vf == NULL)	/* Text mode. */
1214 		return;
1215 
1216 	/*
1217 	 * TODO: add flag about pointer position changed, to not redraw chars
1218 	 * under mouse pointer when nothing changed.
1219 	 */
1220 
1221 	if (vw->vw_mouse_level > 0)
1222 		vt_mouse_terminput(vd, type, x, y, event, cnt);
1223 
1224 	switch (type) {
1225 	case MOUSE_ACTION:
1226 	case MOUSE_MOTION_EVENT:
1227 		/* Movement */
1228 		x += vd->vd_mx;
1229 		y += vd->vd_my;
1230 
1231 		vt_termsize(vd, vf, &size);
1232 
1233 		/* Apply limits. */
1234 		x = MAX(x, 0);
1235 		y = MAX(y, 0);
1236 		x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1237 		y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1238 
1239 		vd->vd_mx = x;
1240 		vd->vd_my = y;
1241 		if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1242 		    (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1243 			vd->vd_mx / vf->vf_width,
1244 			vd->vd_my / vf->vf_height) == 1)) {
1245 
1246 			/*
1247 			 * We have something marked to copy, so update pointer
1248 			 * to window with selection.
1249 			 */
1250 			vd->vd_markedwin = vw;
1251 		}
1252 		return; /* Done */
1253 	case MOUSE_BUTTON_EVENT:
1254 		/* Buttons */
1255 		break;
1256 	default:
1257 		return; /* Done */
1258 	}
1259 
1260 	switch (event) {
1261 	case MOUSE_BUTTON1DOWN:
1262 		switch (cnt % 4) {
1263 		case 0:	/* up */
1264 			mark = VTB_MARK_END;
1265 			break;
1266 		case 1: /* single click: start cut operation */
1267 			mark = VTB_MARK_START;
1268 			break;
1269 		case 2:	/* double click: cut a word */
1270 			mark = VTB_MARK_WORD;
1271 			break;
1272 		case 3:	/* triple click: cut a line */
1273 			mark = VTB_MARK_ROW;
1274 			break;
1275 		}
1276 		break;
1277 	case VT_MOUSE_PASTEBUTTON:
1278 		switch (cnt) {
1279 		case 0:	/* up */
1280 			break;
1281 		default:
1282 			if (vd->vd_markedwin == NULL)
1283 				return;
1284 			/* Get current selecton size in bytes. */
1285 			len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1286 			if (len <= 0)
1287 				return;
1288 
1289 			buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1290 			/* Request cupy/paste buffer data, no more than `len' */
1291 			vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1292 			    len);
1293 
1294 			len /= sizeof(term_char_t);
1295 			for (i = 0; i < len; i++ ) {
1296 				if (buf[i] == '\0')
1297 					continue;
1298 				terminal_input_char(vw->vw_terminal, buf[i]);
1299 			}
1300 
1301 			/* Done, so cleanup. */
1302 			free(buf, M_VT);
1303 			break;
1304 		}
1305 		return; /* Done */
1306 	case VT_MOUSE_EXTENDBUTTON:
1307 		switch (cnt) {
1308 		case 0:	/* up */
1309 			if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1310 				mark = VTB_MARK_EXTEND;
1311 			else
1312 				mark = 0;
1313 			break;
1314 		default:
1315 			mark = VTB_MARK_EXTEND;
1316 			break;
1317 		}
1318 		break;
1319 	default:
1320 		return; /* Done */
1321 	}
1322 
1323 	/* Save buttons state. */
1324 	if (cnt > 0)
1325 		vd->vd_mstate |= event;
1326 	else
1327 		vd->vd_mstate &= ~event;
1328 
1329 	if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1330 	    vd->vd_my / vf->vf_height) == 1) {
1331 		/*
1332 		 * We have something marked to copy, so update pointer to
1333 		 * window with selection.
1334 		 */
1335 		vd->vd_markedwin = vw;
1336 	}
1337 }
1338 
1339 void
1340 vt_mouse_state(int show)
1341 {
1342 	struct vt_device *vd;
1343 	struct vt_window *vw;
1344 
1345 	vd = main_vd;
1346 	vw = vd->vd_curwindow;
1347 
1348 	switch (show) {
1349 	case VT_MOUSE_HIDE:
1350 		vw->vw_flags |= VWF_MOUSE_HIDE;
1351 		break;
1352 	case VT_MOUSE_SHOW:
1353 		vw->vw_flags &= ~VWF_MOUSE_HIDE;
1354 		break;
1355 	}
1356 }
1357 #endif
1358 
1359 static int
1360 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1361     int nprot, vm_memattr_t *memattr)
1362 {
1363 	struct vt_window *vw = tm->tm_softc;
1364 	struct vt_device *vd = vw->vw_device;
1365 
1366 	if (vd->vd_driver->vd_fb_mmap)
1367 		return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1368 		    memattr));
1369 
1370 	return (ENXIO);
1371 }
1372 
1373 static int
1374 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1375     struct thread *td)
1376 {
1377 	struct vt_window *vw = tm->tm_softc;
1378 	struct vt_device *vd = vw->vw_device;
1379 	keyboard_t *kbd;
1380 	int error, i, s;
1381 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1382     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1383 	int ival;
1384 
1385 	switch (cmd) {
1386 	case _IO('v', 4):
1387 		cmd = VT_RELDISP;
1388 		break;
1389 	case _IO('v', 5):
1390 		cmd = VT_ACTIVATE;
1391 		break;
1392 	case _IO('v', 6):
1393 		cmd = VT_WAITACTIVE;
1394 		break;
1395 	case _IO('K', 20):
1396 		cmd = KDSKBSTATE;
1397 		break;
1398 	case _IO('K', 67):
1399 		cmd = KDSETRAD;
1400 		break;
1401 	case _IO('K', 7):
1402 		cmd = KDSKBMODE;
1403 		break;
1404 	case _IO('K', 8):
1405 		cmd = KDMKTONE;
1406 		break;
1407 	case _IO('K', 63):
1408 		cmd = KIOCSOUND;
1409 		break;
1410 	case _IO('K', 66):
1411 		cmd = KDSETLED;
1412 		break;
1413 	case _IO('c', 110):
1414 		cmd = CONS_SETKBD;
1415 		break;
1416 	default:
1417 		goto skip_thunk;
1418 	}
1419 	ival = IOCPARM_IVAL(data);
1420 	data = (caddr_t)&ival;
1421 skip_thunk:
1422 #endif
1423 
1424 	switch (cmd) {
1425 	case KDSETRAD:		/* set keyboard repeat & delay rates (old) */
1426 		if (*(int *)data & ~0x7f)
1427 			return (EINVAL);
1428 	case GIO_KEYMAP:
1429 	case PIO_KEYMAP:
1430 	case GIO_DEADKEYMAP:
1431 	case PIO_DEADKEYMAP:
1432 	case GETFKEY:
1433 	case SETFKEY:
1434 	case KDGKBINFO:
1435 	case KDGKBTYPE:
1436 	case KDSKBSTATE:	/* set keyboard state (locks) */
1437 	case KDGKBSTATE:	/* get keyboard state (locks) */
1438 	case KDGETREPEAT:	/* get keyboard repeat & delay rates */
1439 	case KDSETREPEAT:	/* set keyboard repeat & delay rates (new) */
1440 	case KDSETLED:		/* set keyboard LED status */
1441 	case KDGETLED:		/* get keyboard LED status */
1442 	case KBADDKBD:		/* add/remove keyboard to/from mux */
1443 	case KBRELKBD: {
1444 		error = 0;
1445 
1446 		mtx_lock(&Giant);
1447 		kbd = kbd_get_keyboard(vd->vd_keyboard);
1448 		if (kbd != NULL)
1449 			error = kbdd_ioctl(kbd, cmd, data);
1450 		mtx_unlock(&Giant);
1451 		if (error == ENOIOCTL) {
1452 			if (cmd == KDGKBTYPE) {
1453 				/* always return something? XXX */
1454 				*(int *)data = 0;
1455 			} else {
1456 				return (ENODEV);
1457 			}
1458 		}
1459 		return (error);
1460 	}
1461 	case KDGKBMODE: {
1462 		int mode = -1;
1463 
1464 		mtx_lock(&Giant);
1465 		kbd = kbd_get_keyboard(vd->vd_keyboard);
1466 		if (kbd != NULL) {
1467 			kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1468 		}
1469 		mtx_unlock(&Giant);
1470 		DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1471 		*(int *)data = mode;
1472 		return (0);
1473 	}
1474 	case KDSKBMODE: {
1475 		int mode;
1476 
1477 		mode = *(int *)data;
1478 		switch (mode) {
1479 		case K_XLATE:
1480 		case K_RAW:
1481 		case K_CODE:
1482 			vw->vw_kbdmode = mode;
1483 			if (vw == vd->vd_curwindow) {
1484 				keyboard_t *kbd;
1485 				error = 0;
1486 
1487 				mtx_lock(&Giant);
1488 				kbd = kbd_get_keyboard(vd->vd_keyboard);
1489 				if (kbd != NULL) {
1490 					error = kbdd_ioctl(kbd, KDSKBMODE,
1491 					    (void *)&mode);
1492 				}
1493 				mtx_unlock(&Giant);
1494 			}
1495 			return (0);
1496 		default:
1497 			return (EINVAL);
1498 		}
1499 	}
1500 	case FBIOGTYPE:
1501 	case FBIO_GETWINORG:	/* get frame buffer window origin */
1502 	case FBIO_GETDISPSTART:	/* get display start address */
1503 	case FBIO_GETLINEWIDTH:	/* get scan line width in bytes */
1504 	case FBIO_BLANK:	/* blank display */
1505 		if (vd->vd_driver->vd_fb_ioctl)
1506 			return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1507 		break;
1508 	case CONS_BLANKTIME:
1509 		/* XXX */
1510 		return (0);
1511 	case CONS_GET:
1512 		/* XXX */
1513 		*(int *)data = M_CG640x480;
1514 		return (0);
1515 	case CONS_BELLTYPE: 	/* set bell type sound */
1516 		if ((*(int *)data) & CONS_QUIET_BELL)
1517 			vd->vd_flags |= VDF_QUIET_BELL;
1518 		else
1519 			vd->vd_flags &= ~VDF_QUIET_BELL;
1520 		return (0);
1521 	case CONS_GETINFO: {
1522 		vid_info_t *vi = (vid_info_t *)data;
1523 
1524 		vi->m_num = vd->vd_curwindow->vw_number + 1;
1525 		/* XXX: other fields! */
1526 		return (0);
1527 	}
1528 	case CONS_GETVERS:
1529 		*(int *)data = 0x200;
1530 		return (0);
1531 	case CONS_MODEINFO:
1532 		/* XXX */
1533 		return (0);
1534 	case CONS_MOUSECTL: {
1535 		mouse_info_t *mouse = (mouse_info_t*)data;
1536 
1537 		/*
1538 		 * This has no effect on vt(4).  We don't draw any mouse
1539 		 * cursor.  Just ignore MOUSE_HIDE and MOUSE_SHOW to
1540 		 * prevent excessive errors.  All the other commands
1541 		 * should not be applied to individual TTYs, but only to
1542 		 * consolectl.
1543 		 */
1544 		switch (mouse->operation) {
1545 		case MOUSE_HIDE:
1546 			vd->vd_flags &= ~VDF_MOUSECURSOR;
1547 			return (0);
1548 		case MOUSE_SHOW:
1549 			vd->vd_mx = vd->vd_width / 2;
1550 			vd->vd_my = vd->vd_height / 2;
1551 			vd->vd_flags |= VDF_MOUSECURSOR;
1552 			return (0);
1553 		default:
1554 			return (EINVAL);
1555 		}
1556 	}
1557 	case PIO_VFONT: {
1558 		struct vt_font *vf;
1559 
1560 		error = vtfont_load((void *)data, &vf);
1561 		if (error != 0)
1562 			return (error);
1563 
1564 		error = vt_change_font(vw, vf);
1565 		vtfont_unref(vf);
1566 		return (error);
1567 	}
1568 	case GIO_SCRNMAP: {
1569 		scrmap_t *sm = (scrmap_t *)data;
1570 		int i;
1571 
1572 		/* We don't have screen maps, so return a handcrafted one. */
1573 		for (i = 0; i < 256; i++)
1574 			sm->scrmap[i] = i;
1575 		return (0);
1576 	}
1577 	case KDSETMODE:
1578 		/* XXX */
1579 		return (0);
1580 	case KDENABIO:      	/* allow io operations */
1581 		error = priv_check(td, PRIV_IO);
1582 		if (error != 0)
1583 			return (error);
1584 		error = securelevel_gt(td->td_ucred, 0);
1585 		if (error != 0)
1586 			return (error);
1587 #if defined(__i386__)
1588 		td->td_frame->tf_eflags |= PSL_IOPL;
1589 #elif defined(__amd64__)
1590 		td->td_frame->tf_rflags |= PSL_IOPL;
1591 #endif
1592 		return (0);
1593 	case KDDISABIO:     	/* disallow io operations (default) */
1594 #if defined(__i386__)
1595 		td->td_frame->tf_eflags &= ~PSL_IOPL;
1596 #elif defined(__amd64__)
1597 		td->td_frame->tf_rflags &= ~PSL_IOPL;
1598 #endif
1599 		return (0);
1600 	case KDMKTONE:      	/* sound the bell */
1601 		/* TODO */
1602 		return (0);
1603 	case KIOCSOUND:     	/* make tone (*data) hz */
1604 		/* TODO */
1605 		return (0);
1606 	case CONS_SETKBD: 		/* set the new keyboard */
1607 		mtx_lock(&Giant);
1608 		error = 0;
1609 		if (vd->vd_keyboard != *(int *)data) {
1610 			kbd = kbd_get_keyboard(*(int *)data);
1611 			if (kbd == NULL) {
1612 				mtx_unlock(&Giant);
1613 				return (EINVAL);
1614 			}
1615 			i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1616 			    (void *)&vd->vd_keyboard, vt_kbdevent, vd);
1617 			if (i >= 0) {
1618 				if (vd->vd_keyboard != -1) {
1619 					kbd_release(kbd,
1620 					    (void *)&vd->vd_keyboard);
1621 				}
1622 				kbd = kbd_get_keyboard(i);
1623 				vd->vd_keyboard = i;
1624 
1625 				(void)kbdd_ioctl(kbd, KDSKBMODE,
1626 				    (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1627 			} else {
1628 				error = EPERM;	/* XXX */
1629 			}
1630 		}
1631 		mtx_unlock(&Giant);
1632 		return (error);
1633 	case CONS_RELKBD: 		/* release the current keyboard */
1634 		mtx_lock(&Giant);
1635 		error = 0;
1636 		if (vd->vd_keyboard != -1) {
1637 			kbd = kbd_get_keyboard(vd->vd_keyboard);
1638 			if (kbd == NULL) {
1639 				mtx_unlock(&Giant);
1640 				return (EINVAL);
1641 			}
1642 			error = kbd_release(kbd, (void *)&vd->vd_keyboard);
1643 			if (error == 0) {
1644 				vd->vd_keyboard = -1;
1645 			}
1646 		}
1647 		mtx_unlock(&Giant);
1648 		return (error);
1649 	case VT_ACTIVATE: {
1650 		int win;
1651 		win = *(int *)data - 1;
1652 		DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1653 		    VT_UNIT(vw), win);
1654 		if ((win > VT_MAXWINDOWS) || (win < 0))
1655 			return (EINVAL);
1656 		return (vt_proc_window_switch(vd->vd_windows[win]));
1657 	}
1658 	case VT_GETACTIVE:
1659 		*(int *)data = vd->vd_curwindow->vw_number + 1;
1660 		return (0);
1661 	case VT_GETINDEX:
1662 		*(int *)data = vw->vw_number + 1;
1663 		return (0);
1664 	case VT_LOCKSWITCH:
1665 		/* TODO: Check current state, switching can be in progress. */
1666 		if ((*(int *)data) & 0x01)
1667 			vw->vw_flags |= VWF_VTYLOCK;
1668 		else
1669 			vw->vw_flags &= ~VWF_VTYLOCK;
1670 	case VT_OPENQRY:
1671 		VT_LOCK(vd);
1672 		for (i = 0; i < VT_MAXWINDOWS; i++) {
1673 			vw = vd->vd_windows[i];
1674 			if (vw == NULL)
1675 				continue;
1676 			if (!(vw->vw_flags & VWF_OPENED)) {
1677 				*(int *)data = vw->vw_number + 1;
1678 				VT_UNLOCK(vd);
1679 				return (0);
1680 			}
1681 		}
1682 		VT_UNLOCK(vd);
1683 		return (EINVAL);
1684 	case VT_WAITACTIVE:
1685 		error = 0;
1686 
1687 		i = *(unsigned int *)data;
1688 		if (i > VT_MAXWINDOWS)
1689 			return (EINVAL);
1690 		if (i != 0)
1691 			vw = vd->vd_windows[i - 1];
1692 
1693 		VT_LOCK(vd);
1694 		while (vd->vd_curwindow != vw && error == 0)
1695 			error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1696 		VT_UNLOCK(vd);
1697 		return (error);
1698 	case VT_SETMODE: {    	/* set screen switcher mode */
1699 		struct vt_mode *mode;
1700 		struct proc *p1;
1701 
1702 		mode = (struct vt_mode *)data;
1703 		DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1704 		if (vw->vw_smode.mode == VT_PROCESS) {
1705 			p1 = pfind(vw->vw_pid);
1706 			if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1707 				if (p1)
1708 					PROC_UNLOCK(p1);
1709 				DPRINTF(5, "error EPERM\n");
1710 				return (EPERM);
1711 			}
1712 			if (p1)
1713 				PROC_UNLOCK(p1);
1714 		}
1715 		if (mode->mode == VT_AUTO) {
1716 			vw->vw_smode.mode = VT_AUTO;
1717 			vw->vw_proc = NULL;
1718 			vw->vw_pid = 0;
1719 			DPRINTF(5, "VT_AUTO, ");
1720 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1721 				cnavailable(vw->vw_terminal->consdev, TRUE);
1722 			/* were we in the middle of the vty switching process? */
1723 			if (finish_vt_rel(vw, TRUE, &s) == 0)
1724 				DPRINTF(5, "reset WAIT_REL, ");
1725 			if (finish_vt_acq(vw) == 0)
1726 				DPRINTF(5, "reset WAIT_ACQ, ");
1727 			return (0);
1728 		} else if (mode->mode == VT_PROCESS) {
1729 			if (!ISSIGVALID(mode->relsig) ||
1730 			    !ISSIGVALID(mode->acqsig) ||
1731 			    !ISSIGVALID(mode->frsig)) {
1732 				DPRINTF(5, "error EINVAL\n");
1733 				return (EINVAL);
1734 			}
1735 			DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1736 			bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1737 			vw->vw_proc = td->td_proc;
1738 			vw->vw_pid = vw->vw_proc->p_pid;
1739 			if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1740 				cnavailable(vw->vw_terminal->consdev, FALSE);
1741 		} else {
1742 			DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
1743 			    mode->mode);
1744 			return (EINVAL);
1745 		}
1746 		DPRINTF(5, "\n");
1747 		return (0);
1748 	}
1749 	case VT_GETMODE:	/* get screen switcher mode */
1750 		bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
1751 		return (0);
1752 
1753 	case VT_RELDISP:	/* screen switcher ioctl */
1754 		/*
1755 		 * This must be the current vty which is in the VT_PROCESS
1756 		 * switching mode...
1757 		 */
1758 		if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
1759 		    VT_PROCESS)) {
1760 			return (EINVAL);
1761 		}
1762 		/* ...and this process is controlling it. */
1763 		if (vw->vw_proc != td->td_proc) {
1764 			return (EPERM);
1765 		}
1766 		error = EINVAL;
1767 		switch(*(int *)data) {
1768 		case VT_FALSE:	/* user refuses to release screen, abort */
1769 			if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
1770 				DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
1771 				    SC_DRIVER_NAME, VT_UNIT(vw));
1772 			break;
1773 		case VT_TRUE:	/* user has released screen, go on */
1774 			/* finish_vt_rel(..., TRUE, ...) should not be locked */
1775 			if (vw->vw_flags & VWF_SWWAIT_REL) {
1776 				if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
1777 					DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
1778 					    SC_DRIVER_NAME, VT_UNIT(vw));
1779 			} else {
1780 				error = EINVAL;
1781 			}
1782 			return (error);
1783 		case VT_ACKACQ:	/* acquire acknowledged, switch completed */
1784 			if ((error = finish_vt_acq(vw)) == 0)
1785 				DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
1786 				    SC_DRIVER_NAME, VT_UNIT(vw));
1787 			break;
1788 		default:
1789 			break;
1790 		}
1791 		return (error);
1792 	}
1793 
1794 	return (ENOIOCTL);
1795 }
1796 
1797 static struct vt_window *
1798 vt_allocate_window(struct vt_device *vd, unsigned int window)
1799 {
1800 	struct vt_window *vw;
1801 	struct terminal *tm;
1802 	term_pos_t size;
1803 	struct winsize wsz;
1804 
1805 	vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
1806 	vw->vw_device = vd;
1807 	vw->vw_number = window;
1808 	vw->vw_kbdmode = K_XLATE;
1809 
1810 	if (!(vd->vd_flags & VDF_TEXTMODE))
1811 		vw->vw_font = vtfont_ref(&vt_font_default);
1812 
1813 	vt_termsize(vd, vw->vw_font, &size);
1814 	vt_winsize(vd, vw->vw_font, &wsz);
1815 	vtbuf_init(&vw->vw_buf, &size);
1816 
1817 	tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
1818 	terminal_set_winsize(tm, &wsz);
1819 	vd->vd_windows[window] = vw;
1820 	callout_init(&vw->vw_proc_dead_timer, 0);
1821 
1822 	return (vw);
1823 }
1824 
1825 void
1826 vt_upgrade(struct vt_device *vd)
1827 {
1828 	struct vt_window *vw;
1829 	unsigned int i;
1830 
1831 	/* Device didn't pass vd_init() or already upgraded. */
1832 	if (vd->vd_flags & (VDF_ASYNC|VDF_DEAD))
1833 		return;
1834 	vd->vd_flags |= VDF_ASYNC;
1835 
1836 	mtx_init(&vd->vd_lock, "vtdev", NULL, MTX_DEF);
1837 	cv_init(&vd->vd_winswitch, "vtwswt");
1838 
1839 	/* Init 25 Hz timer. */
1840 	callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
1841 
1842 	for (i = 0; i < VT_MAXWINDOWS; i++) {
1843 		vw = vd->vd_windows[i];
1844 		if (vw == NULL) {
1845 			/* New window. */
1846 			vw = vt_allocate_window(vd, i);
1847 		}
1848 		if (i == VT_CONSWINDOW) {
1849 			/* Console window. */
1850 			EVENTHANDLER_REGISTER(shutdown_pre_sync,
1851 			    vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
1852 		}
1853 		terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
1854 	}
1855 	if (vd->vd_curwindow == NULL)
1856 		vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
1857 
1858 	/* Attach keyboard. */
1859 	vt_allocate_keyboard(vd);
1860 	DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
1861 
1862 	/* Start timer when everything ready. */
1863 	callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
1864 }
1865 
1866 static void
1867 vt_resize(struct vt_device *vd)
1868 {
1869 	struct vt_window *vw;
1870 	int i;
1871 
1872 	for (i = 0; i < VT_MAXWINDOWS; i++) {
1873 		vw = vd->vd_windows[i];
1874 		/* Resize terminal windows */
1875 		vt_change_font(vw, vw->vw_font);
1876 	}
1877 }
1878 
1879 void
1880 vt_allocate(struct vt_driver *drv, void *softc)
1881 {
1882 	struct vt_device *vd;
1883 	struct winsize wsz;
1884 
1885 	if (main_vd == NULL) {
1886 		main_vd = malloc(sizeof *vd, M_VT, M_WAITOK|M_ZERO);
1887 		printf("%s: VT initialize with new VT driver.\n", __func__);
1888 	} else {
1889 		/*
1890 		 * Check if have rights to replace current driver. For example:
1891 		 * it is bad idea to replace KMS driver with generic VGA one.
1892 		 */
1893 		if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
1894 			printf("%s: Driver priority %d too low. Current %d\n ",
1895 			    __func__, drv->vd_priority,
1896 			    main_vd->vd_driver->vd_priority);
1897 			return;
1898 		}
1899 		printf("%s: Replace existing VT driver.\n", __func__);
1900 	}
1901 	vd = main_vd;
1902 
1903 	/* Stop vt_flush periodic task. */
1904 	if (vd->vd_curwindow != NULL)
1905 		callout_drain(&vd->vd_timer);
1906 
1907 	vd->vd_driver = drv;
1908 	vd->vd_softc = softc;
1909 	vd->vd_driver->vd_init(vd);
1910 
1911 	vt_upgrade(vd);
1912 
1913 	/* Refill settings with new sizes. */
1914 	vt_resize(vd);
1915 
1916 #ifdef DEV_SPLASH
1917 	if (vd->vd_flags & VDF_SPLASH)
1918 		vtterm_splash(vd);
1919 #endif
1920 
1921 	if (vd->vd_curwindow != NULL)
1922 		callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
1923 
1924 	termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
1925 
1926 	/* Update console window sizes to actual. */
1927 	vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz);
1928 	terminal_set_winsize(vd->vd_windows[VT_CONSWINDOW]->vw_terminal, &wsz);
1929 }
1930 
1931 void
1932 vt_suspend()
1933 {
1934 
1935 	if (vt_suspendswitch == 0)
1936 		return;
1937 	/* Save current window. */
1938 	main_vd->vd_savedwindow = main_vd->vd_curwindow;
1939 	/* Ask holding process to free window and switch to console window */
1940 	vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
1941 }
1942 
1943 void
1944 vt_resume()
1945 {
1946 
1947 	if (vt_suspendswitch == 0)
1948 		return;
1949 	/* Switch back to saved window */
1950 	if (main_vd->vd_savedwindow != NULL)
1951 		vt_proc_window_switch(main_vd->vd_savedwindow);
1952 	main_vd->vd_savedwindow = NULL;
1953 }
1954