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