]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/vt/vt_core.c
MFC r363988:
[FreeBSD/stable/9.git] / sys / dev / vt / vt_core.c
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 #if defined(KDB)
520                         kdb_alt_break(c, &vd->vd_altbrk);
521 #endif
522                         terminal_input_char(vw->vw_terminal, KEYCHAR(c));
523                 } else
524                         terminal_input_raw(vw->vw_terminal, c);
525         }
526         return (0);
527 }
528
529 static int
530 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
531 {
532         struct vt_device *vd = arg;
533         int c;
534
535         switch (event) {
536         case KBDIO_KEYINPUT:
537                 break;
538         case KBDIO_UNLOADING:
539                 mtx_lock(&Giant);
540                 vd->vd_keyboard = -1;
541                 kbd_release(kbd, (void *)vd);
542                 mtx_unlock(&Giant);
543                 return (0);
544         default:
545                 return (EINVAL);
546         }
547
548         while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
549                 vt_processkey(kbd, vd, c);
550
551         return (0);
552 }
553
554 static int
555 vt_allocate_keyboard(struct vt_device *vd)
556 {
557         int              idx0, idx;
558         keyboard_t      *k0, *k;
559         keyboard_info_t  ki;
560
561         idx0 = kbd_allocate("kbdmux", -1, (void *)&vd->vd_keyboard,
562             vt_kbdevent, vd);
563         /* XXX: kb_token lost */
564         if (idx0 != -1) {
565                 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
566                 k0 = kbd_get_keyboard(idx0);
567
568                 for (idx = kbd_find_keyboard2("*", -1, 0);
569                      idx != -1;
570                      idx = kbd_find_keyboard2("*", -1, idx + 1)) {
571                         k = kbd_get_keyboard(idx);
572
573                         if (idx == idx0 || KBD_IS_BUSY(k))
574                                 continue;
575
576                         bzero(&ki, sizeof(ki));
577                         strcpy(ki.kb_name, k->kb_name);
578                         ki.kb_unit = k->kb_unit;
579
580                         kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
581                 }
582         } else {
583                 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
584                 idx0 = kbd_allocate("*", -1, (void *)&vd->vd_keyboard,
585                     vt_kbdevent, vd);
586         }
587         vd->vd_keyboard = idx0;
588         DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
589
590         return (idx0);
591 }
592
593 static void
594 vtterm_bell(struct terminal *tm)
595 {
596         struct vt_window *vw = tm->tm_softc;
597         struct vt_device *vd = vw->vw_device;
598
599         if (vd->vd_flags & VDF_QUIET_BELL)
600                 return;
601
602         sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
603 }
604
605 static void
606 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
607 {
608         struct vt_window *vw = tm->tm_softc;
609
610         vtbuf_cursor_position(&vw->vw_buf, p);
611 }
612
613 static void
614 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
615 {
616         struct vt_window *vw = tm->tm_softc;
617
618         vtbuf_putchar(&vw->vw_buf, p, c);
619 }
620
621 static void
622 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
623 {
624         struct vt_window *vw = tm->tm_softc;
625
626         vtbuf_fill_locked(&vw->vw_buf, r, c);
627 }
628
629 static void
630 vtterm_copy(struct terminal *tm, const term_rect_t *r,
631     const term_pos_t *p)
632 {
633         struct vt_window *vw = tm->tm_softc;
634
635         vtbuf_copy(&vw->vw_buf, r, p);
636 }
637
638 static void
639 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
640 {
641         struct vt_window *vw = tm->tm_softc;
642
643         switch (cmd) {
644         case TP_SHOWCURSOR:
645                 vtbuf_cursor_visibility(&vw->vw_buf, arg);
646                 break;
647         case TP_MOUSE:
648                 vw->vw_mouse_level = arg;
649                 break;
650         }
651 }
652
653 static inline void
654 vt_determine_colors(term_char_t c, int cursor,
655     term_color_t *fg, term_color_t *bg)
656 {
657         term_color_t tmp;
658         int invert;
659
660         invert = 0;
661
662         *fg = TCHAR_FGCOLOR(c);
663         if (TCHAR_FORMAT(c) & TF_BOLD)
664                 *fg = TCOLOR_LIGHT(*fg);
665         *bg = TCHAR_BGCOLOR(c);
666
667         if (TCHAR_FORMAT(c) & TF_REVERSE)
668                 invert ^= 1;
669         if (cursor)
670                 invert ^= 1;
671
672         if (invert) {
673                 tmp = *fg;
674                 *fg = *bg;
675                 *bg = tmp;
676         }
677 }
678
679 static void
680 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
681     int iscursor, unsigned int row, unsigned int col)
682 {
683         term_color_t fg, bg;
684
685         vt_determine_colors(c, iscursor, &fg, &bg);
686
687         if (vf != NULL) {
688                 const uint8_t *src;
689                 vt_axis_t top, left;
690
691                 src = vtfont_lookup(vf, c);
692
693                 /*
694                  * Align the terminal to the centre of the screen.
695                  * Fonts may not always be able to fill the entire
696                  * screen.
697                  */
698                 top = row * vf->vf_height + vd->vd_offset.tp_row;
699                 left = col * vf->vf_width + vd->vd_offset.tp_col;
700
701                 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
702                     vf->vf_width, vf->vf_height, fg, bg);
703         } else {
704                 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
705                     row, col, fg, bg);
706         }
707 }
708
709 static void
710 vt_flush(struct vt_device *vd)
711 {
712         struct vt_window *vw;
713         struct vt_font *vf;
714         struct vt_bufmask tmask;
715         unsigned int row, col;
716         term_rect_t tarea;
717         term_pos_t size;
718         term_char_t *r;
719 #ifndef SC_NO_CUTPASTE
720         struct mouse_cursor *m;
721         int bpl, h, w;
722 #endif
723
724         vw = vd->vd_curwindow;
725         if (vw == NULL)
726                 return;
727         vf = vw->vw_font;
728         if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
729                 return;
730
731         if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
732                 return;
733
734         vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
735         vt_termsize(vd, vf, &size);
736
737         /* Force a full redraw when the screen contents are invalid. */
738         if (vd->vd_flags & VDF_INVALID) {
739                 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
740                 tarea.tr_end = size;
741                 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
742
743                 vd->vd_flags &= ~VDF_INVALID;
744         }
745
746 #ifndef SC_NO_CUTPASTE
747         if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) {
748                 /* Mark last mouse position as dirty to erase. */
749                 vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx,
750                     vd->vd_mdirtyy);
751         }
752 #endif
753
754         for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
755                 if (!VTBUF_DIRTYROW(&tmask, row))
756                         continue;
757                 r = VTBUF_GET_ROW(&vw->vw_buf, row);
758                 for (col = tarea.tr_begin.tp_col;
759                     col < tarea.tr_end.tp_col; col++) {
760                         if (!VTBUF_DIRTYCOL(&tmask, col))
761                                 continue;
762
763                         vt_bitblt_char(vd, vf, r[col],
764                             VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
765                 }
766         }
767
768 #ifndef SC_NO_CUTPASTE
769         /* Mouse disabled. */
770         if (vw->vw_flags & VWF_MOUSE_HIDE)
771                 return;
772
773         /* No mouse for DDB. */
774         if (kdb_active || panicstr != NULL)
775                 return;
776
777         if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
778             VDF_MOUSECURSOR) {
779                 m = &vt_default_mouse_pointer;
780                 bpl = (m->w + 7) >> 3; /* Bytes per sorce line. */
781                 w = m->w;
782                 h = m->h;
783
784                 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
785                         w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
786                 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
787                         h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
788
789                 vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl,
790                     vd->vd_offset.tp_row + vd->vd_my,
791                     vd->vd_offset.tp_col + vd->vd_mx,
792                     w, h, TC_WHITE, TC_BLACK);
793                 /* Save point of last mouse cursor to erase it later. */
794                 vd->vd_mdirtyx = vd->vd_mx / vf->vf_width;
795                 vd->vd_mdirtyy = vd->vd_my / vf->vf_height;
796         }
797 #endif
798 }
799
800 static void
801 vt_timer(void *arg)
802 {
803         struct vt_device *vd;
804
805         vd = arg;
806         /* Update screen if required. */
807         vt_flush(vd);
808
809         /* Schedule for next update. */
810         callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
811 }
812
813 static void
814 vtterm_done(struct terminal *tm)
815 {
816         struct vt_window *vw = tm->tm_softc;
817         struct vt_device *vd = vw->vw_device;
818
819         if (kdb_active || panicstr != NULL) {
820                 /* Switch to the debugger. */
821                 if (vd->vd_curwindow != vw) {
822                         vd->vd_curwindow = vw;
823                         vd->vd_flags |= VDF_INVALID;
824                         if (vd->vd_driver->vd_postswitch)
825                                 vd->vd_driver->vd_postswitch(vd);
826                 }
827                 vd->vd_flags &= ~VDF_SPLASH;
828                 vt_flush(vd);
829         } else if (!(vd->vd_flags & VDF_ASYNC)) {
830                 vt_flush(vd);
831         }
832 }
833
834 #ifdef DEV_SPLASH
835 static void
836 vtterm_splash(struct vt_device *vd)
837 {
838         vt_axis_t top, left;
839
840         /* Display a nice boot splash. */
841         if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
842
843                 top = (vd->vd_height - vt_logo_height) / 2;
844                 left = (vd->vd_width - vt_logo_width) / 2;
845                 switch (vt_logo_depth) {
846                 case 1:
847                         /* XXX: Unhardcode colors! */
848                         vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
849                             top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
850                 }
851                 vd->vd_flags |= VDF_SPLASH;
852         }
853 }
854 #endif
855
856 static void
857 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
858 {
859         struct vt_window *vw = tm->tm_softc;
860         struct vt_device *vd = vw->vw_device;
861         struct winsize wsz;
862
863         if (vd->vd_flags & VDF_INITIALIZED)
864                 /* Initialization already done. */
865                 return;
866
867         cp->cn_pri = vd->vd_driver->vd_init(vd);
868         if (cp->cn_pri == CN_DEAD) {
869                 vd->vd_flags |= VDF_DEAD;
870                 return;
871         }
872
873         /* Initialize any early-boot keyboard drivers */
874         kbd_configure(KB_CONF_PROBE_ONLY);
875
876         vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
877         vd->vd_windows[VT_CONSWINDOW] = vw;
878         sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
879
880         if (!(vd->vd_flags & VDF_TEXTMODE))
881                 vw->vw_font = vtfont_ref(&vt_font_default);
882
883         vtbuf_init_early(&vw->vw_buf);
884         vt_winsize(vd, vw->vw_font, &wsz);
885         terminal_set_winsize(tm, &wsz);
886
887 #ifdef DEV_SPLASH
888         vtterm_splash(vd);
889 #endif
890
891         vd->vd_flags |= VDF_INITIALIZED;
892         main_vd = vd;
893 }
894
895 static int
896 vtterm_cngetc(struct terminal *tm)
897 {
898         struct vt_window *vw = tm->tm_softc;
899         struct vt_device *vd = vw->vw_device;
900         keyboard_t *kbd;
901         int state;
902         u_int c;
903
904         if (vw->vw_kbdsq && *vw->vw_kbdsq)
905                 return (*vw->vw_kbdsq++);
906
907         state = 0;
908         /* Make sure the splash screen is not there. */
909         if (vd->vd_flags & VDF_SPLASH) {
910                 /* Remove splash */
911                 vd->vd_flags &= ~VDF_SPLASH;
912                 /* Mark screen as invalid to force update */
913                 vd->vd_flags |= VDF_INVALID;
914                 vt_flush(vd);
915         }
916
917         /* Stripped down keyboard handler. */
918         kbd = kbd_get_keyboard(vd->vd_keyboard);
919         if (kbd == NULL)
920                 return (-1);
921
922         /* Force keyboard input mode to K_XLATE */
923         c = K_XLATE;
924         kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
925
926         /* Switch the keyboard to polling to make it work here. */
927         kbdd_poll(kbd, TRUE);
928         c = kbdd_read_char(kbd, 0);
929         kbdd_poll(kbd, FALSE);
930         if (c & RELKEY)
931                 return (-1);
932
933         if (vw->vw_flags & VWF_SCROLL) {
934                 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
935                 vt_flush(vd);
936                 return (-1);
937         }
938
939         /* Stripped down handling of vt_kbdevent(), without locking, etc. */
940         if (c & SPCLKEY) {
941                 switch (c) {
942                 case SPCLKEY | SLK:
943                         kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
944                         if (state & SLKED) {
945                                 /* Turn scrolling on. */
946                                 vw->vw_flags |= VWF_SCROLL;
947                                 VTBUF_SLCK_ENABLE(&vw->vw_buf);
948                         } else {
949                                 /* Turn scrolling off. */
950                                 vt_scroll(vw, 0, VHS_END);
951                                 vw->vw_flags &= ~VWF_SCROLL;
952                                 VTBUF_SLCK_DISABLE(&vw->vw_buf);
953                         }
954                         break;
955                 /* XXX: KDB can handle history. */
956                 case SPCLKEY | FKEY | F(50): /* Arrow up. */
957                         vw->vw_kbdsq = "\x1b[A";
958                         break;
959                 case SPCLKEY | FKEY | F(58): /* Arrow down. */
960                         vw->vw_kbdsq = "\x1b[B";
961                         break;
962                 case SPCLKEY | FKEY | F(55): /* Arrow right. */
963                         vw->vw_kbdsq = "\x1b[C";
964                         break;
965                 case SPCLKEY | FKEY | F(53): /* Arrow left. */
966                         vw->vw_kbdsq = "\x1b[D";
967                         break;
968                 }
969
970                 /* Force refresh to make scrollback work. */
971                 vt_flush(vd);
972         } else if (KEYFLAGS(c) == 0) {
973                 return (KEYCHAR(c));
974         }
975
976         if (vw->vw_kbdsq && *vw->vw_kbdsq)
977                 return (*vw->vw_kbdsq++);
978
979         return (-1);
980 }
981
982 static void
983 vtterm_opened(struct terminal *tm, int opened)
984 {
985         struct vt_window *vw = tm->tm_softc;
986         struct vt_device *vd = vw->vw_device;
987
988         VT_LOCK(vd);
989         vd->vd_flags &= ~VDF_SPLASH;
990         if (opened)
991                 vw->vw_flags |= VWF_OPENED;
992         else {
993                 vw->vw_flags &= ~VWF_OPENED;
994                 /* TODO: finish ACQ/REL */
995         }
996         VT_UNLOCK(vd);
997 }
998
999 static int
1000 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1001 {
1002         struct vt_device *vd = vw->vw_device;
1003         struct terminal *tm = vw->vw_terminal;
1004         term_pos_t size;
1005         struct winsize wsz;
1006
1007         /*
1008          * Changing fonts.
1009          *
1010          * Changing fonts is a little tricky.  We must prevent
1011          * simultaneous access to the device, so we must stop
1012          * the display timer and the terminal from accessing.
1013          * We need to switch fonts and grow our screen buffer.
1014          *
1015          * XXX: Right now the code uses terminal_mute() to
1016          * prevent data from reaching the console driver while
1017          * resizing the screen buffer.  This isn't elegant...
1018          */
1019
1020         VT_LOCK(vd);
1021         if (vw->vw_flags & VWF_BUSY) {
1022                 /* Another process is changing the font. */
1023                 VT_UNLOCK(vd);
1024                 return (EBUSY);
1025         }
1026         if (vw->vw_font == NULL) {
1027                 /* Our device doesn't need fonts. */
1028                 VT_UNLOCK(vd);
1029                 return (ENOTTY);
1030         }
1031         vw->vw_flags |= VWF_BUSY;
1032         VT_UNLOCK(vd);
1033
1034         vt_termsize(vd, vf, &size);
1035         vt_winsize(vd, vf, &wsz);
1036         /* Save offset to font aligned area. */
1037         vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1038         vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1039
1040         /* Grow the screen buffer and terminal. */
1041         terminal_mute(tm, 1);
1042         vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1043         terminal_set_winsize_blank(tm, &wsz, 0);
1044         terminal_mute(tm, 0);
1045
1046         /* Actually apply the font to the current window. */
1047         VT_LOCK(vd);
1048         vtfont_unref(vw->vw_font);
1049         vw->vw_font = vtfont_ref(vf);
1050
1051         /* Force a full redraw the next timer tick. */
1052         if (vd->vd_curwindow == vw)
1053                 vd->vd_flags |= VDF_INVALID;
1054         vw->vw_flags &= ~VWF_BUSY;
1055         VT_UNLOCK(vd);
1056         return (0);
1057 }
1058
1059 static int
1060 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1061 {
1062         struct vt_device *vd = vw->vw_device;
1063         int l, r, t, b, w, h;
1064
1065         if (vd->vd_driver->vd_drawrect == NULL)
1066                 return (ENOTSUP);
1067
1068         w = vd->vd_width - 1;
1069         h = vd->vd_height - 1;
1070         l = vd->vd_offset.tp_col - 1;
1071         r = w - l;
1072         t = vd->vd_offset.tp_row - 1;
1073         b = h - t;
1074
1075         vd->vd_driver->vd_drawrect(vd, 0, 0, w, t, 1, c); /* Top bar. */
1076         vd->vd_driver->vd_drawrect(vd, 0, t, l, b, 1, c); /* Left bar. */
1077         vd->vd_driver->vd_drawrect(vd, r, t, w, b, 1, c); /* Right bar. */
1078         vd->vd_driver->vd_drawrect(vd, 0, b, w, h, 1, c); /* Bottom bar. */
1079
1080         return (0);
1081 }
1082
1083 static int
1084 vt_proc_alive(struct vt_window *vw)
1085 {
1086         struct proc *p;
1087
1088         if (vw->vw_smode.mode != VT_PROCESS)
1089                 return (FALSE);
1090
1091         if (vw->vw_proc) {
1092                 if ((p = pfind(vw->vw_pid)) != NULL)
1093                         PROC_UNLOCK(p);
1094                 if (vw->vw_proc == p)
1095                         return (TRUE);
1096                 vw->vw_proc = NULL;
1097                 vw->vw_smode.mode = VT_AUTO;
1098                 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1099                 vw->vw_pid = 0;
1100         }
1101         return (FALSE);
1102 }
1103
1104 static int
1105 signal_vt_rel(struct vt_window *vw)
1106 {
1107
1108         if (vw->vw_smode.mode != VT_PROCESS)
1109                 return (FALSE);
1110         if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1111                 vw->vw_proc = NULL;
1112                 vw->vw_pid = 0;
1113                 return (TRUE);
1114         }
1115         vw->vw_flags |= VWF_SWWAIT_REL;
1116         PROC_LOCK(vw->vw_proc);
1117         kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1118         PROC_UNLOCK(vw->vw_proc);
1119         DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1120         return (TRUE);
1121 }
1122
1123 static int
1124 signal_vt_acq(struct vt_window *vw)
1125 {
1126
1127         if (vw->vw_smode.mode != VT_PROCESS)
1128                 return (FALSE);
1129         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1130                 cnavailable(vw->vw_terminal->consdev, FALSE);
1131         if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1132                 vw->vw_proc = NULL;
1133                 vw->vw_pid = 0;
1134                 return (TRUE);
1135         }
1136         vw->vw_flags |= VWF_SWWAIT_ACQ;
1137         PROC_LOCK(vw->vw_proc);
1138         kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1139         PROC_UNLOCK(vw->vw_proc);
1140         DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1141         return (TRUE);
1142 }
1143
1144 static int
1145 finish_vt_rel(struct vt_window *vw, int release, int *s)
1146 {
1147
1148         if (vw->vw_flags & VWF_SWWAIT_REL) {
1149                 vw->vw_flags &= ~VWF_SWWAIT_REL;
1150                 if (release) {
1151                         callout_drain(&vw->vw_proc_dead_timer);
1152                         vt_late_window_switch(vw->vw_switch_to);
1153                 }
1154                 return (0);
1155         }
1156         return (EINVAL);
1157 }
1158
1159 static int
1160 finish_vt_acq(struct vt_window *vw)
1161 {
1162
1163         if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1164                 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1165                 return (0);
1166         }
1167         return (EINVAL);
1168 }
1169
1170 #ifndef SC_NO_CUTPASTE
1171 static void
1172 vt_mouse_terminput_button(struct vt_device *vd, int button)
1173 {
1174         struct vt_window *vw;
1175         struct vt_font *vf;
1176         char mouseb[6] = "\x1B[M";
1177         int i, x, y;
1178
1179         vw = vd->vd_curwindow;
1180         vf = vw->vw_font;
1181
1182         /* Translate to char position. */
1183         x = vd->vd_mx / vf->vf_width;
1184         y = vd->vd_my / vf->vf_height;
1185         /* Avoid overflow. */
1186         x = MIN(x, 255 - '!');
1187         y = MIN(y, 255 - '!');
1188
1189         mouseb[3] = ' ' + button;
1190         mouseb[4] = '!' + x;
1191         mouseb[5] = '!' + y;
1192
1193         for (i = 0; i < sizeof(mouseb); i++ )
1194                 terminal_input_char(vw->vw_terminal, mouseb[i]);
1195 }
1196
1197 static void
1198 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1199     int cnt)
1200 {
1201
1202         switch (type) {
1203         case MOUSE_BUTTON_EVENT:
1204                 if (cnt > 0) {
1205                         /* Mouse button pressed. */
1206                         if (event & MOUSE_BUTTON1DOWN)
1207                                 vt_mouse_terminput_button(vd, 0);
1208                         if (event & MOUSE_BUTTON2DOWN)
1209                                 vt_mouse_terminput_button(vd, 1);
1210                         if (event & MOUSE_BUTTON3DOWN)
1211                                 vt_mouse_terminput_button(vd, 2);
1212                 } else {
1213                         /* Mouse button released. */
1214                         vt_mouse_terminput_button(vd, 3);
1215                 }
1216                 break;
1217 #ifdef notyet
1218         case MOUSE_MOTION_EVENT:
1219                 if (mouse->u.data.z < 0) {
1220                         /* Scroll up. */
1221                         sc_mouse_input_button(vd, 64);
1222                 } else if (mouse->u.data.z > 0) {
1223                         /* Scroll down. */
1224                         sc_mouse_input_button(vd, 65);
1225                 }
1226                 break;
1227 #endif
1228         }
1229 }
1230
1231 void
1232 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1233 {
1234         struct vt_device *vd;
1235         struct vt_window *vw;
1236         struct vt_font *vf;
1237         term_pos_t size;
1238         term_char_t *buf;
1239         int i, len, mark;
1240
1241         vd = main_vd;
1242         vw = vd->vd_curwindow;
1243         vf = vw->vw_font;
1244         mark = 0;
1245
1246         if (vw->vw_flags & VWF_MOUSE_HIDE)
1247                 return; /* Mouse disabled. */
1248
1249         if (vf == NULL) /* Text mode. */
1250                 return;
1251
1252         /*
1253          * TODO: add flag about pointer position changed, to not redraw chars
1254          * under mouse pointer when nothing changed.
1255          */
1256
1257         if (vw->vw_mouse_level > 0)
1258                 vt_mouse_terminput(vd, type, x, y, event, cnt);
1259
1260         switch (type) {
1261         case MOUSE_ACTION:
1262         case MOUSE_MOTION_EVENT:
1263                 /* Movement */
1264                 x += vd->vd_mx;
1265                 y += vd->vd_my;
1266
1267                 vt_termsize(vd, vf, &size);
1268
1269                 /* Apply limits. */
1270                 x = MAX(x, 0);
1271                 y = MAX(y, 0);
1272                 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1273                 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1274
1275                 vd->vd_mx = x;
1276                 vd->vd_my = y;
1277                 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1278                     (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1279                         vd->vd_mx / vf->vf_width, 
1280                         vd->vd_my / vf->vf_height) == 1)) {
1281
1282                         /*
1283                          * We have something marked to copy, so update pointer
1284                          * to window with selection.
1285                          */
1286                         vd->vd_markedwin = vw;
1287                 }
1288                 return; /* Done */
1289         case MOUSE_BUTTON_EVENT:
1290                 /* Buttons */
1291                 break;
1292         default:
1293                 return; /* Done */
1294         }
1295
1296         switch (event) {
1297         case MOUSE_BUTTON1DOWN:
1298                 switch (cnt % 4) {
1299                 case 0: /* up */
1300                         mark = VTB_MARK_END;
1301                         break;
1302                 case 1: /* single click: start cut operation */
1303                         mark = VTB_MARK_START;
1304                         break;
1305                 case 2: /* double click: cut a word */
1306                         mark = VTB_MARK_WORD;
1307                         break;
1308                 case 3: /* triple click: cut a line */
1309                         mark = VTB_MARK_ROW;
1310                         break;
1311                 }
1312                 break;
1313         case VT_MOUSE_PASTEBUTTON:
1314                 switch (cnt) {
1315                 case 0: /* up */
1316                         break;
1317                 default:
1318                         if (vd->vd_markedwin == NULL)
1319                                 return;
1320                         /* Get current selecton size in bytes. */
1321                         len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1322                         if (len <= 0)
1323                                 return;
1324
1325                         buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1326                         /* Request cupy/paste buffer data, no more than `len' */
1327                         vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1328                             len);
1329
1330                         len /= sizeof(term_char_t);
1331                         for (i = 0; i < len; i++ ) {
1332                                 if (buf[i] == '\0')
1333                                         continue;
1334                                 terminal_input_char(vw->vw_terminal, buf[i]);
1335                         }
1336
1337                         /* Done, so cleanup. */
1338                         free(buf, M_VT);
1339                         break;
1340                 }
1341                 return; /* Done */
1342         case VT_MOUSE_EXTENDBUTTON:
1343                 switch (cnt) {
1344                 case 0: /* up */
1345                         if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1346                                 mark = VTB_MARK_EXTEND;
1347                         else
1348                                 mark = 0;
1349                         break;
1350                 default:
1351                         mark = VTB_MARK_EXTEND;
1352                         break;
1353                 }
1354                 break;
1355         default:
1356                 return; /* Done */
1357         }
1358
1359         /* Save buttons state. */
1360         if (cnt > 0)
1361                 vd->vd_mstate |= event;
1362         else
1363                 vd->vd_mstate &= ~event;
1364
1365         if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1366             vd->vd_my / vf->vf_height) == 1) {
1367                 /*
1368                  * We have something marked to copy, so update pointer to
1369                  * window with selection.
1370                  */
1371                 vd->vd_markedwin = vw;
1372         }
1373 }
1374
1375 void
1376 vt_mouse_state(int show)
1377 {
1378         struct vt_device *vd;
1379         struct vt_window *vw;
1380
1381         vd = main_vd;
1382         vw = vd->vd_curwindow;
1383
1384         switch (show) {
1385         case VT_MOUSE_HIDE:
1386                 vw->vw_flags |= VWF_MOUSE_HIDE;
1387                 break;
1388         case VT_MOUSE_SHOW:
1389                 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1390                 break;
1391         }
1392 }
1393 #endif
1394
1395 static int
1396 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1397     int nprot, vm_memattr_t *memattr)
1398 {
1399         struct vt_window *vw = tm->tm_softc;
1400         struct vt_device *vd = vw->vw_device;
1401
1402         if (vd->vd_driver->vd_fb_mmap)
1403                 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1404                     memattr));
1405
1406         return (ENXIO);
1407 }
1408
1409 static int
1410 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1411     struct thread *td)
1412 {
1413         struct vt_window *vw = tm->tm_softc;
1414         struct vt_device *vd = vw->vw_device;
1415         keyboard_t *kbd;
1416         int error, i, s;
1417 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1418     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1419         int ival;
1420
1421         switch (cmd) {
1422         case _IO('v', 4):
1423                 cmd = VT_RELDISP;
1424                 break;
1425         case _IO('v', 5):
1426                 cmd = VT_ACTIVATE;
1427                 break;
1428         case _IO('v', 6):
1429                 cmd = VT_WAITACTIVE;
1430                 break;
1431         case _IO('K', 20):
1432                 cmd = KDSKBSTATE;
1433                 break;
1434         case _IO('K', 67):
1435                 cmd = KDSETRAD;
1436                 break;
1437         case _IO('K', 7):
1438                 cmd = KDSKBMODE;
1439                 break;
1440         case _IO('K', 8):
1441                 cmd = KDMKTONE;
1442                 break;
1443         case _IO('K', 63):
1444                 cmd = KIOCSOUND;
1445                 break;
1446         case _IO('K', 66):
1447                 cmd = KDSETLED;
1448                 break;
1449         case _IO('c', 110):
1450                 cmd = CONS_SETKBD;
1451                 break;
1452         default:
1453                 goto skip_thunk;
1454         }
1455         ival = IOCPARM_IVAL(data);
1456         data = (caddr_t)&ival;
1457 skip_thunk:
1458 #endif
1459
1460         switch (cmd) {
1461         case KDSETRAD:          /* set keyboard repeat & delay rates (old) */
1462                 if (*(int *)data & ~0x7f)
1463                         return (EINVAL);
1464         case GIO_KEYMAP:
1465         case PIO_KEYMAP:
1466         case GIO_DEADKEYMAP:
1467         case PIO_DEADKEYMAP:
1468         case GETFKEY:
1469         case SETFKEY:
1470         case KDGKBINFO:
1471         case KDGKBTYPE:
1472         case KDSKBSTATE:        /* set keyboard state (locks) */
1473         case KDGKBSTATE:        /* get keyboard state (locks) */
1474         case KDGETREPEAT:       /* get keyboard repeat & delay rates */
1475         case KDSETREPEAT:       /* set keyboard repeat & delay rates (new) */
1476         case KDSETLED:          /* set keyboard LED status */
1477         case KDGETLED:          /* get keyboard LED status */
1478         case KBADDKBD:          /* add/remove keyboard to/from mux */
1479         case KBRELKBD: {
1480                 error = 0;
1481
1482                 mtx_lock(&Giant);
1483                 kbd = kbd_get_keyboard(vd->vd_keyboard);
1484                 if (kbd != NULL)
1485                         error = kbdd_ioctl(kbd, cmd, data);
1486                 mtx_unlock(&Giant);
1487                 if (error == ENOIOCTL) {
1488                         if (cmd == KDGKBTYPE) {
1489                                 /* always return something? XXX */
1490                                 *(int *)data = 0;
1491                         } else {
1492                                 return (ENODEV);
1493                         }
1494                 }
1495                 return (error);
1496         }
1497         case KDGKBMODE: {
1498                 int mode = -1;
1499
1500                 mtx_lock(&Giant);
1501                 kbd = kbd_get_keyboard(vd->vd_keyboard);
1502                 if (kbd != NULL) {
1503                         kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1504                 }
1505                 mtx_unlock(&Giant);
1506                 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1507                 *(int *)data = mode;
1508                 return (0);
1509         }
1510         case KDSKBMODE: {
1511                 int mode;
1512
1513                 mode = *(int *)data;
1514                 switch (mode) {
1515                 case K_XLATE:
1516                 case K_RAW:
1517                 case K_CODE:
1518                         vw->vw_kbdmode = mode;
1519                         if (vw == vd->vd_curwindow) {
1520                                 keyboard_t *kbd;
1521                                 error = 0;
1522
1523                                 mtx_lock(&Giant);
1524                                 kbd = kbd_get_keyboard(vd->vd_keyboard);
1525                                 if (kbd != NULL) {
1526                                         error = kbdd_ioctl(kbd, KDSKBMODE,
1527                                             (void *)&mode);
1528                                 }
1529                                 mtx_unlock(&Giant);
1530                         }
1531                         return (0);
1532                 default:
1533                         return (EINVAL);
1534                 }
1535         }
1536         case FBIOGTYPE:
1537         case FBIO_GETWINORG:    /* get frame buffer window origin */
1538         case FBIO_GETDISPSTART: /* get display start address */
1539         case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1540         case FBIO_BLANK:        /* blank display */
1541                 if (vd->vd_driver->vd_fb_ioctl)
1542                         return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1543                 break;
1544         case CONS_BLANKTIME:
1545                 /* XXX */
1546                 return (0);
1547         case CONS_GET:
1548                 /* XXX */
1549                 *(int *)data = M_CG640x480;
1550                 return (0);
1551         case CONS_BELLTYPE:     /* set bell type sound */
1552                 if ((*(int *)data) & CONS_QUIET_BELL)
1553                         vd->vd_flags |= VDF_QUIET_BELL;
1554                 else
1555                         vd->vd_flags &= ~VDF_QUIET_BELL;
1556                 return (0);
1557         case CONS_GETINFO: {
1558                 vid_info_t *vi = (vid_info_t *)data;
1559
1560                 vi->m_num = vd->vd_curwindow->vw_number + 1;
1561                 /* XXX: other fields! */
1562                 return (0);
1563         }
1564         case CONS_GETVERS: 
1565                 *(int *)data = 0x200;
1566                 return (0);
1567         case CONS_MODEINFO:
1568                 /* XXX */
1569                 return (0);
1570         case CONS_MOUSECTL: {
1571                 mouse_info_t *mouse = (mouse_info_t*)data;
1572
1573                 /*
1574                  * This has no effect on vt(4).  We don't draw any mouse
1575                  * cursor.  Just ignore MOUSE_HIDE and MOUSE_SHOW to
1576                  * prevent excessive errors.  All the other commands
1577                  * should not be applied to individual TTYs, but only to
1578                  * consolectl.
1579                  */
1580                 switch (mouse->operation) {
1581                 case MOUSE_HIDE:
1582                         vd->vd_flags &= ~VDF_MOUSECURSOR;
1583                         return (0);
1584                 case MOUSE_SHOW:
1585                         vd->vd_mx = vd->vd_width / 2;
1586                         vd->vd_my = vd->vd_height / 2;
1587                         vd->vd_flags |= VDF_MOUSECURSOR;
1588                         return (0);
1589                 default:
1590                         return (EINVAL);
1591                 }
1592         }
1593         case PIO_VFONT: {
1594                 struct vt_font *vf;
1595
1596                 error = vtfont_load((void *)data, &vf);
1597                 if (error != 0)
1598                         return (error);
1599
1600                 error = vt_change_font(vw, vf);
1601                 if (error == 0) {
1602                         /* XXX: replace 0 with current bg color. */
1603                         vt_set_border(vw, vf, 0);
1604                 }
1605                 vtfont_unref(vf);
1606                 return (error);
1607         }
1608         case GIO_SCRNMAP: {
1609                 scrmap_t *sm = (scrmap_t *)data;
1610                 int i;
1611
1612                 /* We don't have screen maps, so return a handcrafted one. */
1613                 for (i = 0; i < 256; i++)
1614                         sm->scrmap[i] = i;
1615                 return (0);
1616         }
1617         case KDSETMODE:
1618                 /* XXX */
1619                 return (0);
1620         case KDENABIO:          /* allow io operations */
1621                 error = priv_check(td, PRIV_IO);
1622                 if (error != 0)
1623                         return (error);
1624                 error = securelevel_gt(td->td_ucred, 0);
1625                 if (error != 0)
1626                         return (error);
1627 #if defined(__i386__)
1628                 td->td_frame->tf_eflags |= PSL_IOPL;
1629 #elif defined(__amd64__)
1630                 td->td_frame->tf_rflags |= PSL_IOPL;
1631 #endif
1632                 return (0);
1633         case KDDISABIO:         /* disallow io operations (default) */
1634 #if defined(__i386__)
1635                 td->td_frame->tf_eflags &= ~PSL_IOPL;
1636 #elif defined(__amd64__)
1637                 td->td_frame->tf_rflags &= ~PSL_IOPL;
1638 #endif
1639                 return (0);
1640         case KDMKTONE:          /* sound the bell */
1641                 /* TODO */
1642                 return (0);
1643         case KIOCSOUND:         /* make tone (*data) hz */
1644                 /* TODO */
1645                 return (0);
1646         case CONS_SETKBD:               /* set the new keyboard */
1647                 mtx_lock(&Giant);
1648                 error = 0;
1649                 if (vd->vd_keyboard != *(int *)data) {
1650                         kbd = kbd_get_keyboard(*(int *)data);
1651                         if (kbd == NULL) {
1652                                 mtx_unlock(&Giant);
1653                                 return (EINVAL);
1654                         }
1655                         i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1656                             (void *)vd, vt_kbdevent, vd);
1657                         if (i >= 0) {
1658                                 if (vd->vd_keyboard != -1) {
1659                                         kbd_release(kbd, (void *)vd);
1660                                 }
1661                                 kbd = kbd_get_keyboard(i);
1662                                 vd->vd_keyboard = i;
1663
1664                                 (void)kbdd_ioctl(kbd, KDSKBMODE,
1665                                     (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1666                         } else {
1667                                 error = EPERM;  /* XXX */
1668                         }
1669                 }
1670                 mtx_unlock(&Giant);
1671                 return (error);
1672         case CONS_RELKBD:               /* release the current keyboard */
1673                 mtx_lock(&Giant);
1674                 error = 0;
1675                 if (vd->vd_keyboard != -1) {
1676                         kbd = kbd_get_keyboard(vd->vd_keyboard);
1677                         if (kbd == NULL) {
1678                                 mtx_unlock(&Giant);
1679                                 return (EINVAL);
1680                         }
1681                         error = kbd_release(kbd, (void *)vd);
1682                         if (error == 0) {
1683                                 vd->vd_keyboard = -1;
1684                         }
1685                 }
1686                 mtx_unlock(&Giant);
1687                 return (error);
1688         case VT_ACTIVATE: {
1689                 int win;
1690                 win = *(int *)data - 1;
1691                 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1692                     VT_UNIT(vw), win);
1693                 if ((win > VT_MAXWINDOWS) || (win < 0))
1694                         return (EINVAL);
1695                 return (vt_proc_window_switch(vd->vd_windows[win]));
1696         }
1697         case VT_GETACTIVE:
1698                 *(int *)data = vd->vd_curwindow->vw_number + 1;
1699                 return (0);
1700         case VT_GETINDEX:
1701                 *(int *)data = vw->vw_number + 1;
1702                 return (0);
1703         case VT_LOCKSWITCH:
1704                 /* TODO: Check current state, switching can be in progress. */
1705                 if ((*(int *)data) & 0x01)
1706                         vw->vw_flags |= VWF_VTYLOCK;
1707                 else
1708                         vw->vw_flags &= ~VWF_VTYLOCK;
1709         case VT_OPENQRY:
1710                 VT_LOCK(vd);
1711                 for (i = 0; i < VT_MAXWINDOWS; i++) {
1712                         vw = vd->vd_windows[i];
1713                         if (vw == NULL)
1714                                 continue;
1715                         if (!(vw->vw_flags & VWF_OPENED)) {
1716                                 *(int *)data = vw->vw_number + 1;
1717                                 VT_UNLOCK(vd);
1718                                 return (0);
1719                         }
1720                 }
1721                 VT_UNLOCK(vd);
1722                 return (EINVAL);
1723         case VT_WAITACTIVE: {
1724                 unsigned int idx;
1725
1726                 error = 0;
1727
1728                 idx = *(unsigned int *)data;
1729                 if (idx > VT_MAXWINDOWS)
1730                         return (EINVAL);
1731                 if (idx > 0)
1732                         vw = vd->vd_windows[idx - 1];
1733
1734                 VT_LOCK(vd);
1735                 while (vd->vd_curwindow != vw && error == 0)
1736                         error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1737                 VT_UNLOCK(vd);
1738                 return (error);
1739         }
1740         case VT_SETMODE: {      /* set screen switcher mode */
1741                 struct vt_mode *mode;
1742                 struct proc *p1;
1743
1744                 mode = (struct vt_mode *)data;
1745                 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1746                 if (vw->vw_smode.mode == VT_PROCESS) {
1747                         p1 = pfind(vw->vw_pid);
1748                         if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1749                                 if (p1)
1750                                         PROC_UNLOCK(p1);
1751                                 DPRINTF(5, "error EPERM\n");
1752                                 return (EPERM);
1753                         }
1754                         if (p1)
1755                                 PROC_UNLOCK(p1);
1756                 }
1757                 if (mode->mode == VT_AUTO) {
1758                         vw->vw_smode.mode = VT_AUTO;
1759                         vw->vw_proc = NULL;
1760                         vw->vw_pid = 0;
1761                         DPRINTF(5, "VT_AUTO, ");
1762                         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1763                                 cnavailable(vw->vw_terminal->consdev, TRUE);
1764                         /* were we in the middle of the vty switching process? */
1765                         if (finish_vt_rel(vw, TRUE, &s) == 0)
1766                                 DPRINTF(5, "reset WAIT_REL, ");
1767                         if (finish_vt_acq(vw) == 0)
1768                                 DPRINTF(5, "reset WAIT_ACQ, ");
1769                         return (0);
1770                 } else if (mode->mode == VT_PROCESS) {
1771                         if (!ISSIGVALID(mode->relsig) ||
1772                             !ISSIGVALID(mode->acqsig) ||
1773                             !ISSIGVALID(mode->frsig)) {
1774                                 DPRINTF(5, "error EINVAL\n");
1775                                 return (EINVAL);
1776                         }
1777                         DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1778                         bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1779                         vw->vw_proc = td->td_proc;
1780                         vw->vw_pid = vw->vw_proc->p_pid;
1781                         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1782                                 cnavailable(vw->vw_terminal->consdev, FALSE);
1783                 } else {
1784                         DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
1785                             mode->mode);
1786                         return (EINVAL);
1787                 }
1788                 DPRINTF(5, "\n");
1789                 return (0);
1790         }
1791         case VT_GETMODE:        /* get screen switcher mode */
1792                 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
1793                 return (0);
1794
1795         case VT_RELDISP:        /* screen switcher ioctl */
1796                 /*
1797                  * This must be the current vty which is in the VT_PROCESS
1798                  * switching mode...
1799                  */
1800                 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
1801                     VT_PROCESS)) {
1802                         return (EINVAL);
1803                 }
1804                 /* ...and this process is controlling it. */
1805                 if (vw->vw_proc != td->td_proc) {
1806                         return (EPERM);
1807                 }
1808                 error = EINVAL;
1809                 switch(*(int *)data) {
1810                 case VT_FALSE:  /* user refuses to release screen, abort */
1811                         if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
1812                                 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
1813                                     SC_DRIVER_NAME, VT_UNIT(vw));
1814                         break;
1815                 case VT_TRUE:   /* user has released screen, go on */
1816                         /* finish_vt_rel(..., TRUE, ...) should not be locked */
1817                         if (vw->vw_flags & VWF_SWWAIT_REL) {
1818                                 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
1819                                         DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
1820                                             SC_DRIVER_NAME, VT_UNIT(vw));
1821                         } else {
1822                                 error = EINVAL;
1823                         }
1824                         return (error);
1825                 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1826                         if ((error = finish_vt_acq(vw)) == 0)
1827                                 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
1828                                     SC_DRIVER_NAME, VT_UNIT(vw));
1829                         break;
1830                 default:
1831                         break;
1832                 }
1833                 return (error);
1834         }
1835
1836         return (ENOIOCTL);
1837 }
1838
1839 static struct vt_window *
1840 vt_allocate_window(struct vt_device *vd, unsigned int window)
1841 {
1842         struct vt_window *vw;
1843         struct terminal *tm;
1844         term_pos_t size;
1845         struct winsize wsz;
1846
1847         vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
1848         vw->vw_device = vd;
1849         vw->vw_number = window;
1850         vw->vw_kbdmode = K_XLATE;
1851
1852         if (!(vd->vd_flags & VDF_TEXTMODE))
1853                 vw->vw_font = vtfont_ref(&vt_font_default);
1854
1855         vt_termsize(vd, vw->vw_font, &size);
1856         vt_winsize(vd, vw->vw_font, &wsz);
1857         vtbuf_init(&vw->vw_buf, &size);
1858
1859         tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
1860         terminal_set_winsize(tm, &wsz);
1861         vd->vd_windows[window] = vw;
1862         callout_init(&vw->vw_proc_dead_timer, 0);
1863
1864         return (vw);
1865 }
1866
1867 void
1868 vt_upgrade(struct vt_device *vd)
1869 {
1870         struct vt_window *vw;
1871         unsigned int i;
1872
1873         /* Device didn't pass vd_init() or already upgraded. */
1874         if (vd->vd_flags & (VDF_ASYNC|VDF_DEAD))
1875                 return;
1876         vd->vd_flags |= VDF_ASYNC;
1877
1878         mtx_init(&vd->vd_lock, "vtdev", NULL, MTX_DEF);
1879         cv_init(&vd->vd_winswitch, "vtwswt");
1880
1881         /* Init 25 Hz timer. */
1882         callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
1883
1884         for (i = 0; i < VT_MAXWINDOWS; i++) {
1885                 vw = vd->vd_windows[i];
1886                 if (vw == NULL) {
1887                         /* New window. */
1888                         vw = vt_allocate_window(vd, i);
1889                 } else if (vw->vw_flags & VWF_CONSOLE) {
1890                         /* For existing console window. */
1891                         callout_init(&vw->vw_proc_dead_timer, 0);
1892                 }
1893                 if (i == VT_CONSWINDOW) {
1894                         /* Console window. */
1895                         EVENTHANDLER_REGISTER(shutdown_pre_sync,
1896                             vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
1897                 }
1898                 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
1899
1900         }
1901         if (vd->vd_curwindow == NULL)
1902                 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
1903
1904         /* Attach keyboard. */
1905         vt_allocate_keyboard(vd);
1906         DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
1907
1908         /* Start timer when everything ready. */
1909         callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
1910 }
1911
1912 static void
1913 vt_resize(struct vt_device *vd)
1914 {
1915         struct vt_window *vw;
1916         int i;
1917
1918         for (i = 0; i < VT_MAXWINDOWS; i++) {
1919                 vw = vd->vd_windows[i];
1920                 /* Assign default font to window, if not textmode. */
1921                 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
1922                         vw->vw_font = vtfont_ref(&vt_font_default);
1923                 /* Resize terminal windows */
1924                 vt_change_font(vw, vw->vw_font);
1925         }
1926 }
1927
1928 void
1929 vt_allocate(struct vt_driver *drv, void *softc)
1930 {
1931         struct vt_device *vd;
1932         struct winsize wsz;
1933
1934         if (main_vd == NULL) {
1935                 main_vd = malloc(sizeof *vd, M_VT, M_WAITOK|M_ZERO);
1936                 printf("%s: VT initialize with new VT driver.\n", __func__);
1937         } else {
1938                 /*
1939                  * Check if have rights to replace current driver. For example:
1940                  * it is bad idea to replace KMS driver with generic VGA one.
1941                  */
1942                 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
1943                         printf("%s: Driver priority %d too low. Current %d\n ",
1944                             __func__, drv->vd_priority,
1945                             main_vd->vd_driver->vd_priority);
1946                         return;
1947                 }
1948                 printf("%s: Replace existing VT driver.\n", __func__);
1949         }
1950         vd = main_vd;
1951         if (drv->vd_maskbitbltchr == NULL)
1952                 drv->vd_maskbitbltchr = drv->vd_bitbltchr;
1953
1954         if (vd->vd_flags & VDF_ASYNC) {
1955                 /* Stop vt_flush periodic task. */
1956                 callout_drain(&vd->vd_timer);
1957                 /*
1958                  * Mute current terminal until we done. vt_change_font (called
1959                  * from vt_resize) will unmute it.
1960                  */
1961                 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
1962         }
1963
1964         /*
1965          * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
1966          * set it.
1967          */
1968         vd->vd_flags &= ~VDF_TEXTMODE;
1969
1970         vd->vd_driver = drv;
1971         vd->vd_softc = softc;
1972         vd->vd_driver->vd_init(vd);
1973
1974         vt_upgrade(vd);
1975
1976         /* Refill settings with new sizes. */
1977         vt_resize(vd);
1978
1979 #ifdef DEV_SPLASH
1980         if (vd->vd_flags & VDF_SPLASH)
1981                 vtterm_splash(vd);
1982 #endif
1983
1984         if (vd->vd_flags & VDF_ASYNC) {
1985                 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
1986                 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
1987         }
1988
1989         termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
1990
1991         /* Update console window sizes to actual. */
1992         vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz);
1993         terminal_set_winsize(vd->vd_windows[VT_CONSWINDOW]->vw_terminal, &wsz);
1994 }
1995
1996 void
1997 vt_suspend()
1998 {
1999
2000         if (vt_suspendswitch == 0)
2001                 return;
2002         /* Save current window. */
2003         main_vd->vd_savedwindow = main_vd->vd_curwindow;
2004         /* Ask holding process to free window and switch to console window */
2005         vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2006 }
2007
2008 void
2009 vt_resume()
2010 {
2011
2012         if (vt_suspendswitch == 0)
2013                 return;
2014         /* Switch back to saved window */
2015         if (main_vd->vd_savedwindow != NULL)
2016                 vt_proc_window_switch(main_vd->vd_savedwindow);
2017         main_vd->vd_savedwindow = NULL;
2018 }