]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/vt/vt_core.c
vt(4): Refuse to load a font if hw.vga.textmode is selected
[FreeBSD/FreeBSD.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/power.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/reboot.h>
52 #include <sys/systm.h>
53 #include <sys/terminal.h>
54
55 #include <dev/kbd/kbdreg.h>
56 #include <dev/vt/vt.h>
57
58 #if defined(__i386__) || defined(__amd64__)
59 #include <machine/psl.h>
60 #include <machine/frame.h>
61 #endif
62
63 static tc_bell_t        vtterm_bell;
64 static tc_cursor_t      vtterm_cursor;
65 static tc_putchar_t     vtterm_putchar;
66 static tc_fill_t        vtterm_fill;
67 static tc_copy_t        vtterm_copy;
68 static tc_param_t       vtterm_param;
69 static tc_done_t        vtterm_done;
70
71 static tc_cnprobe_t     vtterm_cnprobe;
72 static tc_cngetc_t      vtterm_cngetc;
73
74 static tc_cngrab_t      vtterm_cngrab;
75 static tc_cnungrab_t    vtterm_cnungrab;
76
77 static tc_opened_t      vtterm_opened;
78 static tc_ioctl_t       vtterm_ioctl;
79 static tc_mmap_t        vtterm_mmap;
80
81 const struct terminal_class vt_termclass = {
82         .tc_bell        = vtterm_bell,
83         .tc_cursor      = vtterm_cursor,
84         .tc_putchar     = vtterm_putchar,
85         .tc_fill        = vtterm_fill,
86         .tc_copy        = vtterm_copy,
87         .tc_param       = vtterm_param,
88         .tc_done        = vtterm_done,
89
90         .tc_cnprobe     = vtterm_cnprobe,
91         .tc_cngetc      = vtterm_cngetc,
92
93         .tc_cngrab      = vtterm_cngrab,
94         .tc_cnungrab    = vtterm_cnungrab,
95
96         .tc_opened      = vtterm_opened,
97         .tc_ioctl       = vtterm_ioctl,
98         .tc_mmap        = vtterm_mmap,
99 };
100
101 /*
102  * Use a constant timer of 25 Hz to redraw the screen.
103  *
104  * XXX: In theory we should only fire up the timer when there is really
105  * activity. Unfortunately we cannot always start timers. We really
106  * don't want to process kernel messages synchronously, because it
107  * really slows down the system.
108  */
109 #define VT_TIMERFREQ    25
110
111 /* Bell pitch/duration. */
112 #define VT_BELLDURATION ((5 * hz + 99) / 100)
113 #define VT_BELLPITCH    800
114
115 #define VT_LOCK(vd)     mtx_lock(&(vd)->vd_lock)
116 #define VT_UNLOCK(vd)   mtx_unlock(&(vd)->vd_lock)
117
118 #define VT_UNIT(vw)     ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
119                         (vw)->vw_number)
120
121 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
122 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
123 VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
124 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
125 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
126 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
127
128 /* Allow to disable some keyboard combinations. */
129 VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination.  "
130     "See kbdmap(5) to configure.");
131 VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination.  "
132     "See kbdmap(5) to configure.");
133 VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination.  "
134     "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
135 VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger.  "
136     "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
137 VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic.  "
138     "See kbdmap(5) to configure.");
139
140 static struct vt_device vt_consdev;
141 static unsigned int vt_unit = 0;
142 static MALLOC_DEFINE(M_VT, "vt", "vt device");
143 struct vt_device *main_vd = &vt_consdev;
144
145 /* Boot logo. */
146 extern unsigned int vt_logo_width;
147 extern unsigned int vt_logo_height;
148 extern unsigned int vt_logo_depth;
149 extern unsigned char vt_logo_image[];
150
151 /* Font. */
152 extern struct vt_font vt_font_default;
153 #ifndef SC_NO_CUTPASTE
154 extern struct vt_mouse_cursor vt_default_mouse_pointer;
155 #endif
156
157 static int signal_vt_rel(struct vt_window *);
158 static int signal_vt_acq(struct vt_window *);
159 static int finish_vt_rel(struct vt_window *, int, int *);
160 static int finish_vt_acq(struct vt_window *);
161 static int vt_window_switch(struct vt_window *);
162 static int vt_late_window_switch(struct vt_window *);
163 static int vt_proc_alive(struct vt_window *);
164 static void vt_resize(struct vt_device *);
165 static void vt_update_static(void *);
166 #ifndef SC_NO_CUTPASTE
167 static void vt_mouse_paste(void);
168 #endif
169
170 SET_DECLARE(vt_drv_set, struct vt_driver);
171
172 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
173 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
174
175 static struct terminal  vt_consterm;
176 static struct vt_window vt_conswindow;
177 static struct vt_device vt_consdev = {
178         .vd_driver = NULL,
179         .vd_softc = NULL,
180         .vd_flags = VDF_INVALID,
181         .vd_windows = { [VT_CONSWINDOW] =  &vt_conswindow, },
182         .vd_curwindow = &vt_conswindow,
183         .vd_kbstate = 0,
184
185 #ifndef SC_NO_CUTPASTE
186         .vd_pastebuf = {
187                 .vpb_buf = NULL,
188                 .vpb_bufsz = 0,
189                 .vpb_len = 0
190         },
191         .vd_mcursor = &vt_default_mouse_pointer,
192         .vd_mcursor_fg = TC_WHITE,
193         .vd_mcursor_bg = TC_BLACK,
194 #endif
195 };
196 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
197 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
198 static struct vt_window vt_conswindow = {
199         .vw_number = VT_CONSWINDOW,
200         .vw_flags = VWF_CONSOLE,
201         .vw_buf = {
202                 .vb_buffer = &vt_constextbuf[0],
203                 .vb_rows = &vt_constextbufrows[0],
204                 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
205                 .vb_curroffset = 0,
206                 .vb_roffset = 0,
207                 .vb_flags = VBF_STATIC,
208                 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
209                 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
210                 .vb_scr_size = {
211                         .tp_row = _VTDEFH,
212                         .tp_col = _VTDEFW,
213                 },
214         },
215         .vw_device = &vt_consdev,
216         .vw_terminal = &vt_consterm,
217         .vw_kbdmode = K_XLATE,
218         .vw_grabbed = 0,
219 };
220 static struct terminal vt_consterm = {
221         .tm_class = &vt_termclass,
222         .tm_softc = &vt_conswindow,
223         .tm_flags = TF_CONS,
224 };
225 static struct consdev vt_consterm_consdev = {
226         .cn_ops = &termcn_cnops,
227         .cn_arg = &vt_consterm,
228         .cn_name = "ttyv0",
229 };
230
231 /* Add to set of consoles. */
232 DATA_SET(cons_set, vt_consterm_consdev);
233
234 /*
235  * Right after kmem is done to allow early drivers to use locking and allocate
236  * memory.
237  */
238 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
239     &vt_consdev);
240 /* Delay until all devices attached, to not waste time. */
241 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
242     &vt_consdev);
243
244 /* Initialize locks/mem depended members. */
245 static void
246 vt_update_static(void *dummy)
247 {
248
249         if (!vty_enabled(VTY_VT))
250                 return;
251         if (main_vd->vd_driver != NULL)
252                 printf("VT: running with driver \"%s\".\n",
253                     main_vd->vd_driver->vd_name);
254         else
255                 printf("VT: init without driver.\n");
256
257         mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
258         cv_init(&main_vd->vd_winswitch, "vtwswt");
259 }
260
261 static void
262 vt_schedule_flush(struct vt_device *vd, int ms)
263 {
264
265         if (ms <= 0)
266                 /* Default to initial value. */
267                 ms = 1000 / VT_TIMERFREQ;
268
269         callout_schedule(&vd->vd_timer, hz / (1000 / ms));
270 }
271
272 static void
273 vt_resume_flush_timer(struct vt_device *vd, int ms)
274 {
275
276         if (!(vd->vd_flags & VDF_ASYNC) ||
277             !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
278                 return;
279
280         vt_schedule_flush(vd, ms);
281 }
282
283 static void
284 vt_suspend_flush_timer(struct vt_device *vd)
285 {
286
287         if (!(vd->vd_flags & VDF_ASYNC) ||
288             !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
289                 return;
290
291         callout_drain(&vd->vd_timer);
292 }
293
294 static void
295 vt_switch_timer(void *arg)
296 {
297
298         vt_late_window_switch((struct vt_window *)arg);
299 }
300
301 static int
302 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
303 {
304         int mode, ret;
305
306         mode = 0;
307         ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
308         if (ret == ENOIOCTL)
309                 ret = ENODEV;
310         if (ret != 0)
311                 return (ret);
312
313         vw->vw_kbdmode = mode;
314
315         return (0);
316 }
317
318 static int
319 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
320 {
321         int ret;
322
323         ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
324         if (ret == ENOIOCTL)
325                 ret = ENODEV;
326
327         return (ret);
328 }
329
330 static int
331 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
332 {
333         int state, ret;
334
335         state = 0;
336         ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
337         if (ret == ENOIOCTL)
338                 ret = ENODEV;
339         if (ret != 0)
340                 return (ret);
341
342         vw->vw_kbdstate &= ~LOCK_MASK;
343         vw->vw_kbdstate |= state & LOCK_MASK;
344
345         return (0);
346 }
347
348 static int
349 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
350 {
351         int state, ret;
352
353         state = vw->vw_kbdstate & LOCK_MASK;
354         ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
355         if (ret == ENOIOCTL)
356                 ret = ENODEV;
357
358         return (ret);
359 }
360
361 static int
362 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
363 {
364         int leds, ret;
365
366         leds = 0;
367         ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
368         if (ret == ENOIOCTL)
369                 ret = ENODEV;
370         if (ret != 0)
371                 return (ret);
372
373         vw->vw_kbdstate &= ~LED_MASK;
374         vw->vw_kbdstate |= leds & LED_MASK;
375
376         return (0);
377 }
378
379 static int
380 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
381 {
382         int leds, ret;
383
384         leds = vw->vw_kbdstate & LED_MASK;
385         ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
386         if (ret == ENOIOCTL)
387                 ret = ENODEV;
388
389         return (ret);
390 }
391
392 static int
393 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
394 {
395
396         DPRINTF(40, "%s\n", __func__);
397         curvw->vw_switch_to = vw;
398         /* Set timer to allow switch in case when process hang. */
399         callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
400             vt_switch_timer, (void *)vw);
401         /* Notify process about vt switch attempt. */
402         DPRINTF(30, "%s: Notify process.\n", __func__);
403         signal_vt_rel(curvw);
404
405         return (0);
406 }
407
408 static int
409 vt_window_postswitch(struct vt_window *vw)
410 {
411
412         signal_vt_acq(vw);
413         return (0);
414 }
415
416 /* vt_late_window_switch will done VT switching for regular case. */
417 static int
418 vt_late_window_switch(struct vt_window *vw)
419 {
420         int ret;
421
422         callout_stop(&vw->vw_proc_dead_timer);
423
424         ret = vt_window_switch(vw);
425         if (ret)
426                 return (ret);
427
428         /* Notify owner process about terminal availability. */
429         if (vw->vw_smode.mode == VT_PROCESS) {
430                 ret = vt_window_postswitch(vw);
431         }
432         return (ret);
433 }
434
435 /* Switch window. */
436 static int
437 vt_proc_window_switch(struct vt_window *vw)
438 {
439         struct vt_window *curvw;
440         struct vt_device *vd;
441         int ret;
442
443         vd = vw->vw_device;
444         curvw = vd->vd_curwindow;
445
446         if (curvw->vw_flags & VWF_VTYLOCK)
447                 return (EBUSY);
448
449         /* Ask current process permission to switch away. */
450         if (curvw->vw_smode.mode == VT_PROCESS) {
451                 DPRINTF(30, "%s: VT_PROCESS ", __func__);
452                 if (vt_proc_alive(curvw) == FALSE) {
453                         DPRINTF(30, "Dead. Cleaning.");
454                         /* Dead */
455                 } else {
456                         DPRINTF(30, "%s: Signaling process.\n", __func__);
457                         /* Alive, try to ask him. */
458                         ret = vt_window_preswitch(vw, curvw);
459                         /* Wait for process answer or timeout. */
460                         return (ret);
461                 }
462                 DPRINTF(30, "\n");
463         }
464
465         ret = vt_late_window_switch(vw);
466         return (ret);
467 }
468
469 /* Switch window ignoring process locking. */
470 static int
471 vt_window_switch(struct vt_window *vw)
472 {
473         struct vt_device *vd = vw->vw_device;
474         struct vt_window *curvw = vd->vd_curwindow;
475         keyboard_t *kbd;
476
477         VT_LOCK(vd);
478         if (curvw == vw) {
479                 /* Nothing to do. */
480                 VT_UNLOCK(vd);
481                 return (0);
482         }
483         if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
484                 VT_UNLOCK(vd);
485                 return (EINVAL);
486         }
487
488         vt_suspend_flush_timer(vd);
489
490         vd->vd_curwindow = vw;
491         vd->vd_flags |= VDF_INVALID;
492         cv_broadcast(&vd->vd_winswitch);
493         VT_UNLOCK(vd);
494
495         if (vd->vd_driver->vd_postswitch)
496                 vd->vd_driver->vd_postswitch(vd);
497
498         vt_resume_flush_timer(vd, 0);
499
500         /* Restore per-window keyboard mode. */
501         mtx_lock(&Giant);
502         kbd = kbd_get_keyboard(vd->vd_keyboard);
503         if (kbd != NULL) {
504                 if (curvw->vw_kbdmode == K_XLATE)
505                         vt_save_kbd_state(curvw, kbd);
506
507                 vt_update_kbd_mode(vw, kbd);
508                 vt_update_kbd_state(vw, kbd);
509         }
510         mtx_unlock(&Giant);
511         DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
512
513         return (0);
514 }
515
516 static inline void
517 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
518 {
519
520         size->tp_row = vd->vd_height;
521         size->tp_col = vd->vd_width;
522         if (vf != NULL) {
523                 size->tp_row /= vf->vf_height;
524                 size->tp_col /= vf->vf_width;
525         }
526 }
527
528 static inline void
529 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
530 {
531
532         size->ws_row = size->ws_ypixel = vd->vd_height;
533         size->ws_col = size->ws_xpixel = vd->vd_width;
534         if (vf != NULL) {
535                 size->ws_row /= vf->vf_height;
536                 size->ws_col /= vf->vf_width;
537         }
538 }
539
540 static inline void
541 vt_compute_drawable_area(struct vt_window *vw)
542 {
543         struct vt_device *vd;
544         struct vt_font *vf;
545
546         vd = vw->vw_device;
547
548         if (vw->vw_font == NULL) {
549                 vw->vw_draw_area.tr_begin.tp_col = 0;
550                 vw->vw_draw_area.tr_begin.tp_row = 0;
551                 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
552                 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
553                 return;
554         }
555
556         vf = vw->vw_font;
557
558         /*
559          * Compute the drawable area, so that the text is centered on
560          * the screen.
561          */
562
563         vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
564         vw->vw_draw_area.tr_begin.tp_row = (vd->vd_height % vf->vf_height) / 2;
565         vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
566             vd->vd_width / vf->vf_width * vf->vf_width;
567         vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
568             vd->vd_height / vf->vf_height * vf->vf_height;
569 }
570
571 static void
572 vt_scroll(struct vt_window *vw, int offset, int whence)
573 {
574         int diff;
575         term_pos_t size;
576
577         if ((vw->vw_flags & VWF_SCROLL) == 0)
578                 return;
579
580         vt_termsize(vw->vw_device, vw->vw_font, &size);
581
582         diff = vthistory_seek(&vw->vw_buf, offset, whence);
583         if (diff)
584                 vw->vw_device->vd_flags |= VDF_INVALID;
585         vt_resume_flush_timer(vw->vw_device, 0);
586 }
587
588 static int
589 vt_machine_kbdevent(int c)
590 {
591
592         switch (c) {
593         case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
594                 if (vt_kbd_debug)
595                         kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
596                 return (1);
597         case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
598                 if (vt_kbd_halt)
599                         shutdown_nice(RB_HALT);
600                 return (1);
601         case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
602 #ifndef SC_NO_CUTPASTE
603                 /* Insert text from cut-paste buffer. */
604                 vt_mouse_paste();
605 #endif
606                 break;
607         case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
608                 if (vt_kbd_poweroff)
609                         shutdown_nice(RB_HALT|RB_POWEROFF);
610                 return (1);
611         case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
612                 /*
613                  * Request to immediate panic if sysctl
614                  * kern.vt.enable_panic_key allow it.
615                  */
616                 if (vt_kbd_panic)
617                         panic("Forced by the panic key");
618                 return (1);
619         case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
620                 if (vt_kbd_reboot)
621                         shutdown_nice(RB_AUTOBOOT);
622                 return (1);
623         case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
624                 /* Force activatation/deactivation of the screen saver. */
625                 /* TODO */
626                 return (1);
627         case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
628                 /* Put machine into Stand-By mode. */
629                 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
630                 return (1);
631         case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
632                 /* Suspend machine. */
633                 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
634                 return (1);
635         };
636
637         return (0);
638 }
639
640 static void
641 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
642 {
643         struct vt_device *vd;
644         term_pos_t size;
645
646         vd = vw->vw_device;
647         /* Only special keys handled in ScrollLock mode */
648         if ((c & SPCLKEY) == 0)
649                 return;
650
651         c &= ~SPCLKEY;
652
653         if (console == 0) {
654                 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
655                         vw = vd->vd_windows[c - F_SCR];
656                         if (vw != NULL)
657                                 vt_proc_window_switch(vw);
658                         return;
659                 }
660                 VT_LOCK(vd);
661         }
662
663         switch (c) {
664         case SLK: {
665                 /* Turn scrolling off. */
666                 vt_scroll(vw, 0, VHS_END);
667                 VTBUF_SLCK_DISABLE(&vw->vw_buf);
668                 vw->vw_flags &= ~VWF_SCROLL;
669                 break;
670         }
671         case FKEY | F(49): /* Home key. */
672                 vt_scroll(vw, 0, VHS_SET);
673                 break;
674         case FKEY | F(50): /* Arrow up. */
675                 vt_scroll(vw, -1, VHS_CUR);
676                 break;
677         case FKEY | F(51): /* Page up. */
678                 vt_termsize(vd, vw->vw_font, &size);
679                 vt_scroll(vw, -size.tp_row, VHS_CUR);
680                 break;
681         case FKEY | F(57): /* End key. */
682                 vt_scroll(vw, 0, VHS_END);
683                 break;
684         case FKEY | F(58): /* Arrow down. */
685                 vt_scroll(vw, 1, VHS_CUR);
686                 break;
687         case FKEY | F(59): /* Page down. */
688                 vt_termsize(vd, vw->vw_font, &size);
689                 vt_scroll(vw, size.tp_row, VHS_CUR);
690                 break;
691         }
692
693         if (console == 0)
694                 VT_UNLOCK(vd);
695 }
696
697 static int
698 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
699 {
700         struct vt_window *vw = vd->vd_curwindow;
701
702 #if VT_ALT_TO_ESC_HACK
703         if (c & RELKEY) {
704                 switch (c & ~RELKEY) {
705                 case (SPCLKEY | RALT):
706                         if (vt_enable_altgr != 0)
707                                 break;
708                 case (SPCLKEY | LALT):
709                         vd->vd_kbstate &= ~ALKED;
710                 }
711                 /* Other keys ignored for RELKEY event. */
712                 return (0);
713         } else {
714                 switch (c & ~RELKEY) {
715                 case (SPCLKEY | RALT):
716                         if (vt_enable_altgr != 0)
717                                 break;
718                 case (SPCLKEY | LALT):
719                         vd->vd_kbstate |= ALKED;
720                 }
721         }
722 #else
723         if (c & RELKEY)
724                 /* Other keys ignored for RELKEY event. */
725                 return (0);
726 #endif
727
728         if (vt_machine_kbdevent(c))
729                 return (0);
730
731         if (vw->vw_flags & VWF_SCROLL) {
732                 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
733                 /* Scroll mode keys handled, nothing to do more. */
734                 return (0);
735         }
736
737         if (c & SPCLKEY) {
738                 c &= ~SPCLKEY;
739
740                 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
741                         vw = vd->vd_windows[c - F_SCR];
742                         if (vw != NULL)
743                                 vt_proc_window_switch(vw);
744                         return (0);
745                 }
746
747                 switch (c) {
748                 case NEXT:
749                         /* Switch to next VT. */
750                         c = (vw->vw_number + 1) % VT_MAXWINDOWS;
751                         vw = vd->vd_windows[c];
752                         if (vw != NULL)
753                                 vt_proc_window_switch(vw);
754                         return (0);
755                 case PREV:
756                         /* Switch to previous VT. */
757                         c = (vw->vw_number - 1) % VT_MAXWINDOWS;
758                         vw = vd->vd_windows[c];
759                         if (vw != NULL)
760                                 vt_proc_window_switch(vw);
761                         return (0);
762                 case SLK: {
763                         vt_save_kbd_state(vw, kbd);
764                         VT_LOCK(vd);
765                         if (vw->vw_kbdstate & SLKED) {
766                                 /* Turn scrolling on. */
767                                 vw->vw_flags |= VWF_SCROLL;
768                                 VTBUF_SLCK_ENABLE(&vw->vw_buf);
769                         } else {
770                                 /* Turn scrolling off. */
771                                 vw->vw_flags &= ~VWF_SCROLL;
772                                 VTBUF_SLCK_DISABLE(&vw->vw_buf);
773                                 vt_scroll(vw, 0, VHS_END);
774                         }
775                         VT_UNLOCK(vd);
776                         break;
777                 }
778                 case FKEY | F(1):  case FKEY | F(2):  case FKEY | F(3):
779                 case FKEY | F(4):  case FKEY | F(5):  case FKEY | F(6):
780                 case FKEY | F(7):  case FKEY | F(8):  case FKEY | F(9):
781                 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
782                         /* F1 through F12 keys. */
783                         terminal_input_special(vw->vw_terminal,
784                             TKEY_F1 + c - (FKEY | F(1)));
785                         break;
786                 case FKEY | F(49): /* Home key. */
787                         terminal_input_special(vw->vw_terminal, TKEY_HOME);
788                         break;
789                 case FKEY | F(50): /* Arrow up. */
790                         terminal_input_special(vw->vw_terminal, TKEY_UP);
791                         break;
792                 case FKEY | F(51): /* Page up. */
793                         terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
794                         break;
795                 case FKEY | F(53): /* Arrow left. */
796                         terminal_input_special(vw->vw_terminal, TKEY_LEFT);
797                         break;
798                 case FKEY | F(55): /* Arrow right. */
799                         terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
800                         break;
801                 case FKEY | F(57): /* End key. */
802                         terminal_input_special(vw->vw_terminal, TKEY_END);
803                         break;
804                 case FKEY | F(58): /* Arrow down. */
805                         terminal_input_special(vw->vw_terminal, TKEY_DOWN);
806                         break;
807                 case FKEY | F(59): /* Page down. */
808                         terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
809                         break;
810                 case FKEY | F(60): /* Insert key. */
811                         terminal_input_special(vw->vw_terminal, TKEY_INSERT);
812                         break;
813                 case FKEY | F(61): /* Delete key. */
814                         terminal_input_special(vw->vw_terminal, TKEY_DELETE);
815                         break;
816                 }
817         } else if (KEYFLAGS(c) == 0) {
818                 /* Don't do UTF-8 conversion when doing raw mode. */
819                 if (vw->vw_kbdmode == K_XLATE) {
820 #if VT_ALT_TO_ESC_HACK
821                         if (vd->vd_kbstate & ALKED) {
822                                 /*
823                                  * Prepend ESC sequence if one of ALT keys down.
824                                  */
825                                 terminal_input_char(vw->vw_terminal, 0x1b);
826                         }
827 #endif
828
829                         terminal_input_char(vw->vw_terminal, KEYCHAR(c));
830                 } else
831                         terminal_input_raw(vw->vw_terminal, c);
832         }
833         return (0);
834 }
835
836 static int
837 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
838 {
839         struct vt_device *vd = arg;
840         int c;
841
842         switch (event) {
843         case KBDIO_KEYINPUT:
844                 break;
845         case KBDIO_UNLOADING:
846                 mtx_lock(&Giant);
847                 vd->vd_keyboard = -1;
848                 kbd_release(kbd, (void *)vd);
849                 mtx_unlock(&Giant);
850                 return (0);
851         default:
852                 return (EINVAL);
853         }
854
855         while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
856                 vt_processkey(kbd, vd, c);
857
858         return (0);
859 }
860
861 static int
862 vt_allocate_keyboard(struct vt_device *vd)
863 {
864         int              idx0, idx;
865         keyboard_t      *k0, *k;
866         keyboard_info_t  ki;
867
868         idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
869         vd->vd_keyboard = idx0;
870         if (idx0 >= 0) {
871                 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
872                 k0 = kbd_get_keyboard(idx0);
873
874                 for (idx = kbd_find_keyboard2("*", -1, 0);
875                      idx != -1;
876                      idx = kbd_find_keyboard2("*", -1, idx + 1)) {
877                         k = kbd_get_keyboard(idx);
878
879                         if (idx == idx0 || KBD_IS_BUSY(k))
880                                 continue;
881
882                         bzero(&ki, sizeof(ki));
883                         strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
884                         ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
885                         ki.kb_unit = k->kb_unit;
886
887                         kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
888                 }
889         } else {
890                 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
891                 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
892                 if (idx0 < 0) {
893                         DPRINTF(10, "%s: No keyboard found.\n", __func__);
894                         return (-1);
895                 }
896         }
897         DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
898
899         return (idx0);
900 }
901
902 static void
903 vtterm_bell(struct terminal *tm)
904 {
905         struct vt_window *vw = tm->tm_softc;
906         struct vt_device *vd = vw->vw_device;
907
908         if (!vt_enable_bell)
909                 return;
910
911         if (vd->vd_flags & VDF_QUIET_BELL)
912                 return;
913
914         sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
915 }
916
917 static void
918 vtterm_beep(struct terminal *tm, u_int param)
919 {
920         u_int freq, period;
921
922         if (!vt_enable_bell)
923                 return;
924
925         if ((param == 0) || ((param & 0xffff) == 0)) {
926                 vtterm_bell(tm);
927                 return;
928         }
929
930         period = ((param >> 16) & 0xffff) * hz / 1000;
931         freq = 1193182 / (param & 0xffff);
932
933         sysbeep(freq, period);
934 }
935
936 static void
937 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
938 {
939         struct vt_window *vw = tm->tm_softc;
940
941         vtbuf_cursor_position(&vw->vw_buf, p);
942         vt_resume_flush_timer(vw->vw_device, 0);
943 }
944
945 static void
946 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
947 {
948         struct vt_window *vw = tm->tm_softc;
949
950         vtbuf_putchar(&vw->vw_buf, p, c);
951         vt_resume_flush_timer(vw->vw_device, 0);
952 }
953
954 static void
955 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
956 {
957         struct vt_window *vw = tm->tm_softc;
958
959         vtbuf_fill_locked(&vw->vw_buf, r, c);
960         vt_resume_flush_timer(vw->vw_device, 0);
961 }
962
963 static void
964 vtterm_copy(struct terminal *tm, const term_rect_t *r,
965     const term_pos_t *p)
966 {
967         struct vt_window *vw = tm->tm_softc;
968
969         vtbuf_copy(&vw->vw_buf, r, p);
970         vt_resume_flush_timer(vw->vw_device, 0);
971 }
972
973 static void
974 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
975 {
976         struct vt_window *vw = tm->tm_softc;
977
978         switch (cmd) {
979         case TP_SHOWCURSOR:
980                 vtbuf_cursor_visibility(&vw->vw_buf, arg);
981                 vt_resume_flush_timer(vw->vw_device, 0);
982                 break;
983         case TP_MOUSE:
984                 vw->vw_mouse_level = arg;
985                 break;
986         }
987 }
988
989 void
990 vt_determine_colors(term_char_t c, int cursor,
991     term_color_t *fg, term_color_t *bg)
992 {
993         term_color_t tmp;
994         int invert;
995
996         invert = 0;
997
998         *fg = TCHAR_FGCOLOR(c);
999         if (TCHAR_FORMAT(c) & TF_BOLD)
1000                 *fg = TCOLOR_LIGHT(*fg);
1001         *bg = TCHAR_BGCOLOR(c);
1002
1003         if (TCHAR_FORMAT(c) & TF_REVERSE)
1004                 invert ^= 1;
1005         if (cursor)
1006                 invert ^= 1;
1007
1008         if (invert) {
1009                 tmp = *fg;
1010                 *fg = *bg;
1011                 *bg = tmp;
1012         }
1013 }
1014
1015 #ifndef SC_NO_CUTPASTE
1016 int
1017 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1018 {
1019         unsigned int mx, my, x1, y1, x2, y2;
1020
1021         /*
1022          * We use the cursor position saved during the current refresh,
1023          * in case the cursor moved since.
1024          */
1025         mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1026         my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1027
1028         x1 = area->tr_begin.tp_col;
1029         y1 = area->tr_begin.tp_row;
1030         x2 = area->tr_end.tp_col;
1031         y2 = area->tr_end.tp_row;
1032
1033         if (((mx >= x1 && x2 - 1 >= mx) ||
1034              (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
1035             ((my >= y1 && y2 - 1 >= my) ||
1036              (my < y1 && my + vd->vd_mcursor->height >= y1)))
1037                 return (1);
1038
1039         return (0);
1040 }
1041
1042 static void
1043 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1044 {
1045         term_rect_t area;
1046         struct vt_window *vw;
1047         struct vt_font *vf;
1048         int x, y;
1049
1050         vw = vd->vd_curwindow;
1051         vf = vw->vw_font;
1052
1053         x = vd->vd_mx_drawn;
1054         y = vd->vd_my_drawn;
1055
1056         if (vf != NULL) {
1057                 area.tr_begin.tp_col = x / vf->vf_width;
1058                 area.tr_begin.tp_row = y / vf->vf_height;
1059                 area.tr_end.tp_col =
1060                     ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1061                 area.tr_end.tp_row =
1062                     ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1063         } else {
1064                 /*
1065                  * No font loaded (ie. vt_vga operating in textmode).
1066                  *
1067                  * FIXME: This fake area needs to be revisited once the
1068                  * mouse cursor is supported in vt_vga's textmode.
1069                  */
1070                 area.tr_begin.tp_col = x;
1071                 area.tr_begin.tp_row = y;
1072                 area.tr_end.tp_col = x + 2;
1073                 area.tr_end.tp_row = y + 2;
1074         }
1075
1076         vtbuf_dirty(&vw->vw_buf, &area);
1077 }
1078 #endif
1079
1080 static int
1081 vt_flush(struct vt_device *vd)
1082 {
1083         struct vt_window *vw;
1084         struct vt_font *vf;
1085         term_rect_t tarea;
1086         term_pos_t size;
1087 #ifndef SC_NO_CUTPASTE
1088         int cursor_was_shown, cursor_moved;
1089 #endif
1090
1091         vw = vd->vd_curwindow;
1092         if (vw == NULL)
1093                 return (0);
1094
1095         if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1096                 return (0);
1097
1098         vf = vw->vw_font;
1099         if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1100                 return (0);
1101
1102 #ifndef SC_NO_CUTPASTE
1103         cursor_was_shown = vd->vd_mshown;
1104         cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1105             vd->vd_my != vd->vd_my_drawn);
1106
1107         /* Check if the cursor should be displayed or not. */
1108         if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1109             !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed.      */
1110             !kdb_active && panicstr == NULL) {  /* DDB inactive.          */
1111                 vd->vd_mshown = 1;
1112         } else {
1113                 vd->vd_mshown = 0;
1114         }
1115
1116         /*
1117          * If the cursor changed display state or moved, we must mark
1118          * the old position as dirty, so that it's erased.
1119          */
1120         if (cursor_was_shown != vd->vd_mshown ||
1121             (vd->vd_mshown && cursor_moved))
1122                 vt_mark_mouse_position_as_dirty(vd);
1123
1124         /*
1125          * Save position of the mouse cursor. It's used by backends to
1126          * know where to draw the cursor and during the next refresh to
1127          * erase the previous position.
1128          */
1129         vd->vd_mx_drawn = vd->vd_mx;
1130         vd->vd_my_drawn = vd->vd_my;
1131
1132         /*
1133          * If the cursor is displayed and has moved since last refresh,
1134          * mark the new position as dirty.
1135          */
1136         if (vd->vd_mshown && cursor_moved)
1137                 vt_mark_mouse_position_as_dirty(vd);
1138 #endif
1139
1140         vtbuf_undirty(&vw->vw_buf, &tarea);
1141         vt_termsize(vd, vf, &size);
1142
1143         /* Force a full redraw when the screen contents are invalid. */
1144         if (vd->vd_flags & VDF_INVALID) {
1145                 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1146                 tarea.tr_end = size;
1147
1148                 vd->vd_flags &= ~VDF_INVALID;
1149         }
1150
1151         if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1152                 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1153                 return (1);
1154         }
1155
1156         return (0);
1157 }
1158
1159 static void
1160 vt_timer(void *arg)
1161 {
1162         struct vt_device *vd;
1163         int changed;
1164
1165         vd = arg;
1166         /* Update screen if required. */
1167         changed = vt_flush(vd);
1168
1169         /* Schedule for next update. */
1170         if (changed)
1171                 vt_schedule_flush(vd, 0);
1172         else
1173                 vd->vd_timer_armed = 0;
1174 }
1175
1176 static void
1177 vtterm_done(struct terminal *tm)
1178 {
1179         struct vt_window *vw = tm->tm_softc;
1180         struct vt_device *vd = vw->vw_device;
1181
1182         if (kdb_active || panicstr != NULL) {
1183                 /* Switch to the debugger. */
1184                 if (vd->vd_curwindow != vw) {
1185                         vd->vd_curwindow = vw;
1186                         vd->vd_flags |= VDF_INVALID;
1187                         if (vd->vd_driver->vd_postswitch)
1188                                 vd->vd_driver->vd_postswitch(vd);
1189                 }
1190                 vd->vd_flags &= ~VDF_SPLASH;
1191                 vt_flush(vd);
1192         } else if (!(vd->vd_flags & VDF_ASYNC)) {
1193                 vt_flush(vd);
1194         }
1195 }
1196
1197 #ifdef DEV_SPLASH
1198 static void
1199 vtterm_splash(struct vt_device *vd)
1200 {
1201         vt_axis_t top, left;
1202
1203         /* Display a nice boot splash. */
1204         if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1205
1206                 top = (vd->vd_height - vt_logo_height) / 2;
1207                 left = (vd->vd_width - vt_logo_width) / 2;
1208                 switch (vt_logo_depth) {
1209                 case 1:
1210                         /* XXX: Unhardcode colors! */
1211                         vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1212                             vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1213                             left, top, TC_WHITE, TC_BLACK);
1214                 }
1215                 vd->vd_flags |= VDF_SPLASH;
1216         }
1217 }
1218 #endif
1219
1220
1221 static void
1222 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1223 {
1224         struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1225         struct vt_window *vw = tm->tm_softc;
1226         struct vt_device *vd = vw->vw_device;
1227         struct winsize wsz;
1228         term_attr_t attr;
1229         term_char_t c;
1230
1231         if (!vty_enabled(VTY_VT))
1232                 return;
1233
1234         if (vd->vd_flags & VDF_INITIALIZED)
1235                 /* Initialization already done. */
1236                 return;
1237
1238         SET_FOREACH(vtdlist, vt_drv_set) {
1239                 vtd = *vtdlist;
1240                 if (vtd->vd_probe == NULL)
1241                         continue;
1242                 if (vtd->vd_probe(vd) == CN_DEAD)
1243                         continue;
1244                 if ((vtdbest == NULL) ||
1245                     (vtd->vd_priority > vtdbest->vd_priority))
1246                         vtdbest = vtd;
1247         }
1248         if (vtdbest == NULL) {
1249                 cp->cn_pri = CN_DEAD;
1250                 vd->vd_flags |= VDF_DEAD;
1251         } else {
1252                 vd->vd_driver = vtdbest;
1253                 cp->cn_pri = vd->vd_driver->vd_init(vd);
1254         }
1255
1256         /* Check if driver's vt_init return CN_DEAD. */
1257         if (cp->cn_pri == CN_DEAD) {
1258                 vd->vd_flags |= VDF_DEAD;
1259         }
1260
1261         /* Initialize any early-boot keyboard drivers */
1262         kbd_configure(KB_CONF_PROBE_ONLY);
1263
1264         vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1265         vd->vd_windows[VT_CONSWINDOW] = vw;
1266         sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1267
1268         /* Attach default font if not in TEXTMODE. */
1269         if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1270                 vw->vw_font = vtfont_ref(&vt_font_default);
1271                 vt_compute_drawable_area(vw);
1272         }
1273
1274         /*
1275          * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1276          * that we have the real viewable size, fix it in the static
1277          * buffer.
1278          */
1279         if (vd->vd_width != 0 && vd->vd_height != 0)
1280                 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1281
1282         vtbuf_init_early(&vw->vw_buf);
1283         vt_winsize(vd, vw->vw_font, &wsz);
1284         c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1285             TERMINAL_NORM_ATTR;
1286         attr.ta_format = TCHAR_FORMAT(c);
1287         attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1288         attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1289         terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1290
1291         if (vtdbest != NULL) {
1292 #ifdef DEV_SPLASH
1293                 vtterm_splash(vd);
1294 #endif
1295                 vd->vd_flags |= VDF_INITIALIZED;
1296         }
1297 }
1298
1299 static int
1300 vtterm_cngetc(struct terminal *tm)
1301 {
1302         struct vt_window *vw = tm->tm_softc;
1303         struct vt_device *vd = vw->vw_device;
1304         keyboard_t *kbd;
1305         u_int c;
1306
1307         if (vw->vw_kbdsq && *vw->vw_kbdsq)
1308                 return (*vw->vw_kbdsq++);
1309
1310         /* Make sure the splash screen is not there. */
1311         if (vd->vd_flags & VDF_SPLASH) {
1312                 /* Remove splash */
1313                 vd->vd_flags &= ~VDF_SPLASH;
1314                 /* Mark screen as invalid to force update */
1315                 vd->vd_flags |= VDF_INVALID;
1316                 vt_flush(vd);
1317         }
1318
1319         /* Stripped down keyboard handler. */
1320         kbd = kbd_get_keyboard(vd->vd_keyboard);
1321         if (kbd == NULL)
1322                 return (-1);
1323
1324         /* Force keyboard input mode to K_XLATE */
1325         vw->vw_kbdmode = K_XLATE;
1326         vt_update_kbd_mode(vw, kbd);
1327
1328         /* Switch the keyboard to polling to make it work here. */
1329         kbdd_poll(kbd, TRUE);
1330         c = kbdd_read_char(kbd, 0);
1331         kbdd_poll(kbd, FALSE);
1332         if (c & RELKEY)
1333                 return (-1);
1334
1335         if (vw->vw_flags & VWF_SCROLL) {
1336                 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1337                 vt_flush(vd);
1338                 return (-1);
1339         }
1340
1341         /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1342         if (c & SPCLKEY) {
1343                 switch (c) {
1344                 case SPCLKEY | SLK:
1345                         vt_save_kbd_state(vw, kbd);
1346                         if (vw->vw_kbdstate & SLKED) {
1347                                 /* Turn scrolling on. */
1348                                 vw->vw_flags |= VWF_SCROLL;
1349                                 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1350                         } else {
1351                                 /* Turn scrolling off. */
1352                                 vt_scroll(vw, 0, VHS_END);
1353                                 vw->vw_flags &= ~VWF_SCROLL;
1354                                 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1355                         }
1356                         break;
1357                 /* XXX: KDB can handle history. */
1358                 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1359                         vw->vw_kbdsq = "\x1b[A";
1360                         break;
1361                 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1362                         vw->vw_kbdsq = "\x1b[B";
1363                         break;
1364                 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1365                         vw->vw_kbdsq = "\x1b[C";
1366                         break;
1367                 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1368                         vw->vw_kbdsq = "\x1b[D";
1369                         break;
1370                 }
1371
1372                 /* Force refresh to make scrollback work. */
1373                 vt_flush(vd);
1374         } else if (KEYFLAGS(c) == 0) {
1375                 return (KEYCHAR(c));
1376         }
1377
1378         if (vw->vw_kbdsq && *vw->vw_kbdsq)
1379                 return (*vw->vw_kbdsq++);
1380
1381         return (-1);
1382 }
1383
1384 static void
1385 vtterm_cngrab(struct terminal *tm)
1386 {
1387         struct vt_device *vd;
1388         struct vt_window *vw;
1389         keyboard_t *kbd;
1390
1391         vw = tm->tm_softc;
1392         vd = vw->vw_device;
1393
1394         if (!cold)
1395                 vt_window_switch(vw);
1396
1397         kbd = kbd_get_keyboard(vd->vd_keyboard);
1398         if (kbd == NULL)
1399                 return;
1400
1401         if (vw->vw_grabbed++ > 0)
1402                 return;
1403
1404         /*
1405          * Make sure the keyboard is accessible even when the kbd device
1406          * driver is disabled.
1407          */
1408         kbdd_enable(kbd);
1409
1410         /* We shall always use the keyboard in the XLATE mode here. */
1411         vw->vw_prev_kbdmode = vw->vw_kbdmode;
1412         vw->vw_kbdmode = K_XLATE;
1413         vt_update_kbd_mode(vw, kbd);
1414
1415         kbdd_poll(kbd, TRUE);
1416 }
1417
1418 static void
1419 vtterm_cnungrab(struct terminal *tm)
1420 {
1421         struct vt_device *vd;
1422         struct vt_window *vw;
1423         keyboard_t *kbd;
1424
1425         vw = tm->tm_softc;
1426         vd = vw->vw_device;
1427
1428         kbd = kbd_get_keyboard(vd->vd_keyboard);
1429         if (kbd == NULL)
1430                 return;
1431
1432         if (--vw->vw_grabbed > 0)
1433                 return;
1434
1435         kbdd_poll(kbd, FALSE);
1436
1437         vw->vw_kbdmode = vw->vw_prev_kbdmode;
1438         vt_update_kbd_mode(vw, kbd);
1439         kbdd_disable(kbd);
1440 }
1441
1442 static void
1443 vtterm_opened(struct terminal *tm, int opened)
1444 {
1445         struct vt_window *vw = tm->tm_softc;
1446         struct vt_device *vd = vw->vw_device;
1447
1448         VT_LOCK(vd);
1449         vd->vd_flags &= ~VDF_SPLASH;
1450         if (opened)
1451                 vw->vw_flags |= VWF_OPENED;
1452         else {
1453                 vw->vw_flags &= ~VWF_OPENED;
1454                 /* TODO: finish ACQ/REL */
1455         }
1456         VT_UNLOCK(vd);
1457 }
1458
1459 static int
1460 vt_set_border(struct vt_window *vw, term_color_t c)
1461 {
1462         struct vt_device *vd = vw->vw_device;
1463
1464         if (vd->vd_driver->vd_drawrect == NULL)
1465                 return (ENOTSUP);
1466
1467         /* Top bar. */
1468         if (vw->vw_draw_area.tr_begin.tp_row > 0)
1469                 vd->vd_driver->vd_drawrect(vd,
1470                     0, 0,
1471                     vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1472                     1, c);
1473
1474         /* Left bar. */
1475         if (vw->vw_draw_area.tr_begin.tp_col > 0)
1476                 vd->vd_driver->vd_drawrect(vd,
1477                     0, 0,
1478                     vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1479                     1, c);
1480
1481         /* Right bar. */
1482         if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1483                 vd->vd_driver->vd_drawrect(vd,
1484                     vw->vw_draw_area.tr_end.tp_col - 1, 0,
1485                     vd->vd_width - 1, vd->vd_height - 1,
1486                     1, c);
1487
1488         /* Bottom bar. */
1489         if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1490                 vd->vd_driver->vd_drawrect(vd,
1491                     0, vw->vw_draw_area.tr_end.tp_row - 1,
1492                     vd->vd_width - 1, vd->vd_height - 1,
1493                     1, c);
1494
1495         return (0);
1496 }
1497
1498 static int
1499 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1500 {
1501         struct vt_device *vd = vw->vw_device;
1502         struct terminal *tm = vw->vw_terminal;
1503         term_pos_t size;
1504         struct winsize wsz;
1505
1506         /*
1507          * Changing fonts.
1508          *
1509          * Changing fonts is a little tricky.  We must prevent
1510          * simultaneous access to the device, so we must stop
1511          * the display timer and the terminal from accessing.
1512          * We need to switch fonts and grow our screen buffer.
1513          *
1514          * XXX: Right now the code uses terminal_mute() to
1515          * prevent data from reaching the console driver while
1516          * resizing the screen buffer.  This isn't elegant...
1517          */
1518
1519         VT_LOCK(vd);
1520         if (vw->vw_flags & VWF_BUSY) {
1521                 /* Another process is changing the font. */
1522                 VT_UNLOCK(vd);
1523                 return (EBUSY);
1524         }
1525         vw->vw_flags |= VWF_BUSY;
1526         VT_UNLOCK(vd);
1527
1528         vt_termsize(vd, vf, &size);
1529         vt_winsize(vd, vf, &wsz);
1530
1531         /* Grow the screen buffer and terminal. */
1532         terminal_mute(tm, 1);
1533         vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1534         terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1535         terminal_mute(tm, 0);
1536
1537         /* Actually apply the font to the current window. */
1538         VT_LOCK(vd);
1539         if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1540                 /*
1541                  * In case vt_change_font called to update size we don't need
1542                  * to update font link.
1543                  */
1544                 vtfont_unref(vw->vw_font);
1545                 vw->vw_font = vtfont_ref(vf);
1546         }
1547
1548         /*
1549          * Compute the drawable area and move the mouse cursor inside
1550          * it, in case the new area is smaller than the previous one.
1551          */
1552         vt_compute_drawable_area(vw);
1553         vd->vd_mx = min(vd->vd_mx,
1554             vw->vw_draw_area.tr_end.tp_col -
1555             vw->vw_draw_area.tr_begin.tp_col - 1);
1556         vd->vd_my = min(vd->vd_my,
1557             vw->vw_draw_area.tr_end.tp_row -
1558             vw->vw_draw_area.tr_begin.tp_row - 1);
1559
1560         /* Force a full redraw the next timer tick. */
1561         if (vd->vd_curwindow == vw) {
1562                 vt_set_border(vw, TC_BLACK);
1563                 vd->vd_flags |= VDF_INVALID;
1564                 vt_resume_flush_timer(vw->vw_device, 0);
1565         }
1566         vw->vw_flags &= ~VWF_BUSY;
1567         VT_UNLOCK(vd);
1568         return (0);
1569 }
1570
1571 static int
1572 vt_proc_alive(struct vt_window *vw)
1573 {
1574         struct proc *p;
1575
1576         if (vw->vw_smode.mode != VT_PROCESS)
1577                 return (FALSE);
1578
1579         if (vw->vw_proc) {
1580                 if ((p = pfind(vw->vw_pid)) != NULL)
1581                         PROC_UNLOCK(p);
1582                 if (vw->vw_proc == p)
1583                         return (TRUE);
1584                 vw->vw_proc = NULL;
1585                 vw->vw_smode.mode = VT_AUTO;
1586                 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1587                 vw->vw_pid = 0;
1588         }
1589         return (FALSE);
1590 }
1591
1592 static int
1593 signal_vt_rel(struct vt_window *vw)
1594 {
1595
1596         if (vw->vw_smode.mode != VT_PROCESS)
1597                 return (FALSE);
1598         if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1599                 vw->vw_proc = NULL;
1600                 vw->vw_pid = 0;
1601                 return (TRUE);
1602         }
1603         vw->vw_flags |= VWF_SWWAIT_REL;
1604         PROC_LOCK(vw->vw_proc);
1605         kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1606         PROC_UNLOCK(vw->vw_proc);
1607         DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1608         return (TRUE);
1609 }
1610
1611 static int
1612 signal_vt_acq(struct vt_window *vw)
1613 {
1614
1615         if (vw->vw_smode.mode != VT_PROCESS)
1616                 return (FALSE);
1617         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1618                 cnavailable(vw->vw_terminal->consdev, FALSE);
1619         if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1620                 vw->vw_proc = NULL;
1621                 vw->vw_pid = 0;
1622                 return (TRUE);
1623         }
1624         vw->vw_flags |= VWF_SWWAIT_ACQ;
1625         PROC_LOCK(vw->vw_proc);
1626         kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1627         PROC_UNLOCK(vw->vw_proc);
1628         DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1629         return (TRUE);
1630 }
1631
1632 static int
1633 finish_vt_rel(struct vt_window *vw, int release, int *s)
1634 {
1635
1636         if (vw->vw_flags & VWF_SWWAIT_REL) {
1637                 vw->vw_flags &= ~VWF_SWWAIT_REL;
1638                 if (release) {
1639                         callout_drain(&vw->vw_proc_dead_timer);
1640                         vt_late_window_switch(vw->vw_switch_to);
1641                 }
1642                 return (0);
1643         }
1644         return (EINVAL);
1645 }
1646
1647 static int
1648 finish_vt_acq(struct vt_window *vw)
1649 {
1650
1651         if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1652                 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1653                 return (0);
1654         }
1655         return (EINVAL);
1656 }
1657
1658 #ifndef SC_NO_CUTPASTE
1659 static void
1660 vt_mouse_terminput_button(struct vt_device *vd, int button)
1661 {
1662         struct vt_window *vw;
1663         struct vt_font *vf;
1664         char mouseb[6] = "\x1B[M";
1665         int i, x, y;
1666
1667         vw = vd->vd_curwindow;
1668         vf = vw->vw_font;
1669
1670         /* Translate to char position. */
1671         x = vd->vd_mx / vf->vf_width;
1672         y = vd->vd_my / vf->vf_height;
1673         /* Avoid overflow. */
1674         x = MIN(x, 255 - '!');
1675         y = MIN(y, 255 - '!');
1676
1677         mouseb[3] = ' ' + button;
1678         mouseb[4] = '!' + x;
1679         mouseb[5] = '!' + y;
1680
1681         for (i = 0; i < sizeof(mouseb); i++)
1682                 terminal_input_char(vw->vw_terminal, mouseb[i]);
1683 }
1684
1685 static void
1686 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1687     int cnt)
1688 {
1689
1690         switch (type) {
1691         case MOUSE_BUTTON_EVENT:
1692                 if (cnt > 0) {
1693                         /* Mouse button pressed. */
1694                         if (event & MOUSE_BUTTON1DOWN)
1695                                 vt_mouse_terminput_button(vd, 0);
1696                         if (event & MOUSE_BUTTON2DOWN)
1697                                 vt_mouse_terminput_button(vd, 1);
1698                         if (event & MOUSE_BUTTON3DOWN)
1699                                 vt_mouse_terminput_button(vd, 2);
1700                 } else {
1701                         /* Mouse button released. */
1702                         vt_mouse_terminput_button(vd, 3);
1703                 }
1704                 break;
1705 #ifdef notyet
1706         case MOUSE_MOTION_EVENT:
1707                 if (mouse->u.data.z < 0) {
1708                         /* Scroll up. */
1709                         sc_mouse_input_button(vd, 64);
1710                 } else if (mouse->u.data.z > 0) {
1711                         /* Scroll down. */
1712                         sc_mouse_input_button(vd, 65);
1713                 }
1714                 break;
1715 #endif
1716         }
1717 }
1718
1719 static void
1720 vt_mouse_paste()
1721 {
1722         term_char_t *buf;
1723         int i, len;
1724
1725         len = VD_PASTEBUFLEN(main_vd);
1726         buf = VD_PASTEBUF(main_vd);
1727         len /= sizeof(term_char_t);
1728         for (i = 0; i < len; i++) {
1729                 if (buf[i] == '\0')
1730                         continue;
1731                 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1732                     buf[i]);
1733         }
1734 }
1735
1736 void
1737 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1738 {
1739         struct vt_device *vd;
1740         struct vt_window *vw;
1741         struct vt_font *vf;
1742         term_pos_t size;
1743         int len, mark;
1744
1745         vd = main_vd;
1746         vw = vd->vd_curwindow;
1747         vf = vw->vw_font;
1748         mark = 0;
1749
1750         if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1751                 /*
1752                  * Either the mouse is disabled, or the window is in
1753                  * "graphics mode". The graphics mode is usually set by
1754                  * an X server, using the KDSETMODE ioctl.
1755                  */
1756                 return;
1757
1758         if (vf == NULL) /* Text mode. */
1759                 return;
1760
1761         /*
1762          * TODO: add flag about pointer position changed, to not redraw chars
1763          * under mouse pointer when nothing changed.
1764          */
1765
1766         if (vw->vw_mouse_level > 0)
1767                 vt_mouse_terminput(vd, type, x, y, event, cnt);
1768
1769         switch (type) {
1770         case MOUSE_ACTION:
1771         case MOUSE_MOTION_EVENT:
1772                 /* Movement */
1773                 x += vd->vd_mx;
1774                 y += vd->vd_my;
1775
1776                 vt_termsize(vd, vf, &size);
1777
1778                 /* Apply limits. */
1779                 x = MAX(x, 0);
1780                 y = MAX(y, 0);
1781                 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1782                 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1783
1784                 vd->vd_mx = x;
1785                 vd->vd_my = y;
1786                 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1787                         vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1788                             vd->vd_mx / vf->vf_width,
1789                             vd->vd_my / vf->vf_height);
1790
1791                 vt_resume_flush_timer(vw->vw_device, 0);
1792                 return; /* Done */
1793         case MOUSE_BUTTON_EVENT:
1794                 /* Buttons */
1795                 break;
1796         default:
1797                 return; /* Done */
1798         }
1799
1800         switch (event) {
1801         case MOUSE_BUTTON1DOWN:
1802                 switch (cnt % 4) {
1803                 case 0: /* up */
1804                         mark = VTB_MARK_END;
1805                         break;
1806                 case 1: /* single click: start cut operation */
1807                         mark = VTB_MARK_START;
1808                         break;
1809                 case 2: /* double click: cut a word */
1810                         mark = VTB_MARK_WORD;
1811                         break;
1812                 case 3: /* triple click: cut a line */
1813                         mark = VTB_MARK_ROW;
1814                         break;
1815                 }
1816                 break;
1817         case VT_MOUSE_PASTEBUTTON:
1818                 switch (cnt) {
1819                 case 0: /* up */
1820                         break;
1821                 default:
1822                         vt_mouse_paste();
1823                         break;
1824                 }
1825                 return; /* Done */
1826         case VT_MOUSE_EXTENDBUTTON:
1827                 switch (cnt) {
1828                 case 0: /* up */
1829                         if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1830                                 mark = VTB_MARK_EXTEND;
1831                         else
1832                                 mark = 0;
1833                         break;
1834                 default:
1835                         mark = VTB_MARK_EXTEND;
1836                         break;
1837                 }
1838                 break;
1839         default:
1840                 return; /* Done */
1841         }
1842
1843         /* Save buttons state. */
1844         if (cnt > 0)
1845                 vd->vd_mstate |= event;
1846         else
1847                 vd->vd_mstate &= ~event;
1848
1849         if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1850             vd->vd_my / vf->vf_height) == 1) {
1851                 /*
1852                  * We have something marked to copy, so update pointer to
1853                  * window with selection.
1854                  */
1855                 vt_resume_flush_timer(vw->vw_device, 0);
1856
1857                 switch (mark) {
1858                 case VTB_MARK_END:
1859                 case VTB_MARK_WORD:
1860                 case VTB_MARK_ROW:
1861                 case VTB_MARK_EXTEND:
1862                         break;
1863                 default:
1864                         /* Other types of mark do not require to copy data. */
1865                         return;
1866                 }
1867
1868                 /* Get current selection size in bytes. */
1869                 len = vtbuf_get_marked_len(&vw->vw_buf);
1870                 if (len <= 0)
1871                         return;
1872
1873                 /* Reallocate buffer only if old one is too small. */
1874                 if (len > VD_PASTEBUFSZ(vd)) {
1875                         VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1876                             M_WAITOK | M_ZERO);
1877                         /* Update buffer size. */
1878                         VD_PASTEBUFSZ(vd) = len;
1879                 }
1880                 /* Request copy/paste buffer data, no more than `len' */
1881                 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1882                     VD_PASTEBUFSZ(vd));
1883
1884                 VD_PASTEBUFLEN(vd) = len;
1885
1886                 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1887         }
1888 }
1889
1890 void
1891 vt_mouse_state(int show)
1892 {
1893         struct vt_device *vd;
1894         struct vt_window *vw;
1895
1896         vd = main_vd;
1897         vw = vd->vd_curwindow;
1898
1899         switch (show) {
1900         case VT_MOUSE_HIDE:
1901                 vw->vw_flags |= VWF_MOUSE_HIDE;
1902                 break;
1903         case VT_MOUSE_SHOW:
1904                 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1905                 break;
1906         }
1907
1908         /* Mark mouse position as dirty. */
1909         vt_mark_mouse_position_as_dirty(vd);
1910         vt_resume_flush_timer(vw->vw_device, 0);
1911 }
1912 #endif
1913
1914 static int
1915 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1916     int nprot, vm_memattr_t *memattr)
1917 {
1918         struct vt_window *vw = tm->tm_softc;
1919         struct vt_device *vd = vw->vw_device;
1920
1921         if (vd->vd_driver->vd_fb_mmap)
1922                 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1923                     memattr));
1924
1925         return (ENXIO);
1926 }
1927
1928 static int
1929 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1930     struct thread *td)
1931 {
1932         struct vt_window *vw = tm->tm_softc;
1933         struct vt_device *vd = vw->vw_device;
1934         keyboard_t *kbd;
1935         int error, i, s;
1936 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1937     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1938         int ival;
1939
1940         switch (cmd) {
1941         case _IO('v', 4):
1942                 cmd = VT_RELDISP;
1943                 break;
1944         case _IO('v', 5):
1945                 cmd = VT_ACTIVATE;
1946                 break;
1947         case _IO('v', 6):
1948                 cmd = VT_WAITACTIVE;
1949                 break;
1950         case _IO('K', 20):
1951                 cmd = KDSKBSTATE;
1952                 break;
1953         case _IO('K', 67):
1954                 cmd = KDSETRAD;
1955                 break;
1956         case _IO('K', 7):
1957                 cmd = KDSKBMODE;
1958                 break;
1959         case _IO('K', 8):
1960                 cmd = KDMKTONE;
1961                 break;
1962         case _IO('K', 63):
1963                 cmd = KIOCSOUND;
1964                 break;
1965         case _IO('K', 66):
1966                 cmd = KDSETLED;
1967                 break;
1968         case _IO('c', 110):
1969                 cmd = CONS_SETKBD;
1970                 break;
1971         default:
1972                 goto skip_thunk;
1973         }
1974         ival = IOCPARM_IVAL(data);
1975         data = (caddr_t)&ival;
1976 skip_thunk:
1977 #endif
1978
1979         switch (cmd) {
1980         case KDSETRAD:          /* set keyboard repeat & delay rates (old) */
1981                 if (*(int *)data & ~0x7f)
1982                         return (EINVAL);
1983                 /* FALLTHROUGH */
1984         case GIO_KEYMAP:
1985         case PIO_KEYMAP:
1986         case GIO_DEADKEYMAP:
1987         case PIO_DEADKEYMAP:
1988         case GETFKEY:
1989         case SETFKEY:
1990         case KDGKBINFO:
1991         case KDGKBTYPE:
1992         case KDGETREPEAT:       /* get keyboard repeat & delay rates */
1993         case KDSETREPEAT:       /* set keyboard repeat & delay rates (new) */
1994         case KBADDKBD:          /* add/remove keyboard to/from mux */
1995         case KBRELKBD: {
1996                 error = 0;
1997
1998                 mtx_lock(&Giant);
1999                 kbd = kbd_get_keyboard(vd->vd_keyboard);
2000                 if (kbd != NULL)
2001                         error = kbdd_ioctl(kbd, cmd, data);
2002                 mtx_unlock(&Giant);
2003                 if (error == ENOIOCTL) {
2004                         if (cmd == KDGKBTYPE) {
2005                                 /* always return something? XXX */
2006                                 *(int *)data = 0;
2007                         } else {
2008                                 return (ENODEV);
2009                         }
2010                 }
2011                 return (error);
2012         }
2013         case KDGKBSTATE: {      /* get keyboard state (locks) */
2014                 error = 0;
2015
2016                 if (vw == vd->vd_curwindow) {
2017                         mtx_lock(&Giant);
2018                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2019                         if (kbd != NULL)
2020                                 error = vt_save_kbd_state(vw, kbd);
2021                         mtx_unlock(&Giant);
2022
2023                         if (error != 0)
2024                                 return (error);
2025                 }
2026
2027                 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2028
2029                 return (error);
2030         }
2031         case KDSKBSTATE: {      /* set keyboard state (locks) */
2032                 int state;
2033
2034                 state = *(int *)data;
2035                 if (state & ~LOCK_MASK)
2036                         return (EINVAL);
2037
2038                 vw->vw_kbdstate &= ~LOCK_MASK;
2039                 vw->vw_kbdstate |= state;
2040
2041                 error = 0;
2042                 if (vw == vd->vd_curwindow) {
2043                         mtx_lock(&Giant);
2044                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2045                         if (kbd != NULL)
2046                                 error = vt_update_kbd_state(vw, kbd);
2047                         mtx_unlock(&Giant);
2048                 }
2049
2050                 return (error);
2051         }
2052         case KDGETLED: {        /* get keyboard LED status */
2053                 error = 0;
2054
2055                 if (vw == vd->vd_curwindow) {
2056                         mtx_lock(&Giant);
2057                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2058                         if (kbd != NULL)
2059                                 error = vt_save_kbd_leds(vw, kbd);
2060                         mtx_unlock(&Giant);
2061
2062                         if (error != 0)
2063                                 return (error);
2064                 }
2065
2066                 *(int *)data = vw->vw_kbdstate & LED_MASK;
2067
2068                 return (error);
2069         }
2070         case KDSETLED: {        /* set keyboard LED status */
2071                 int leds;
2072
2073                 leds = *(int *)data;
2074                 if (leds & ~LED_MASK)
2075                         return (EINVAL);
2076
2077                 vw->vw_kbdstate &= ~LED_MASK;
2078                 vw->vw_kbdstate |= leds;
2079
2080                 error = 0;
2081                 if (vw == vd->vd_curwindow) {
2082                         mtx_lock(&Giant);
2083                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2084                         if (kbd != NULL)
2085                                 error = vt_update_kbd_leds(vw, kbd);
2086                         mtx_unlock(&Giant);
2087                 }
2088
2089                 return (error);
2090         }
2091         case KDGKBMODE: {
2092                 error = 0;
2093
2094                 if (vw == vd->vd_curwindow) {
2095                         mtx_lock(&Giant);
2096                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2097                         if (kbd != NULL)
2098                                 error = vt_save_kbd_mode(vw, kbd);
2099                         mtx_unlock(&Giant);
2100
2101                         if (error != 0)
2102                                 return (error);
2103                 }
2104
2105                 *(int *)data = vw->vw_kbdmode;
2106
2107                 return (error);
2108         }
2109         case KDSKBMODE: {
2110                 int mode;
2111
2112                 mode = *(int *)data;
2113                 switch (mode) {
2114                 case K_XLATE:
2115                 case K_RAW:
2116                 case K_CODE:
2117                         vw->vw_kbdmode = mode;
2118
2119                         error = 0;
2120                         if (vw == vd->vd_curwindow) {
2121                                 mtx_lock(&Giant);
2122                                 kbd = kbd_get_keyboard(vd->vd_keyboard);
2123                                 if (kbd != NULL)
2124                                         error = vt_update_kbd_mode(vw, kbd);
2125                                 mtx_unlock(&Giant);
2126                         }
2127
2128                         return (error);
2129                 default:
2130                         return (EINVAL);
2131                 }
2132         }
2133         case FBIOGTYPE:
2134         case FBIO_GETWINORG:    /* get frame buffer window origin */
2135         case FBIO_GETDISPSTART: /* get display start address */
2136         case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2137         case FBIO_BLANK:        /* blank display */
2138                 if (vd->vd_driver->vd_fb_ioctl)
2139                         return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2140                 break;
2141         case CONS_BLANKTIME:
2142                 /* XXX */
2143                 return (0);
2144         case CONS_GET:
2145                 /* XXX */
2146                 *(int *)data = M_CG640x480;
2147                 return (0);
2148         case CONS_BELLTYPE:     /* set bell type sound */
2149                 if ((*(int *)data) & CONS_QUIET_BELL)
2150                         vd->vd_flags |= VDF_QUIET_BELL;
2151                 else
2152                         vd->vd_flags &= ~VDF_QUIET_BELL;
2153                 return (0);
2154         case CONS_GETINFO: {
2155                 vid_info_t *vi = (vid_info_t *)data;
2156                 if (vi->size != sizeof(struct vid_info))
2157                         return (EINVAL);
2158
2159                 if (vw == vd->vd_curwindow) {
2160                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2161                         if (kbd != NULL)
2162                                 vt_save_kbd_state(vw, kbd);
2163                 }
2164
2165                 vi->m_num = vd->vd_curwindow->vw_number + 1;
2166                 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2167                 /* XXX: other fields! */
2168                 return (0);
2169         }
2170         case CONS_GETVERS:
2171                 *(int *)data = 0x200;
2172                 return (0);
2173         case CONS_MODEINFO:
2174                 /* XXX */
2175                 return (0);
2176         case CONS_MOUSECTL: {
2177                 mouse_info_t *mouse = (mouse_info_t*)data;
2178
2179                 /*
2180                  * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2181                  * should not be applied to individual TTYs, but only to
2182                  * consolectl.
2183                  */
2184                 switch (mouse->operation) {
2185                 case MOUSE_HIDE:
2186                         if (vd->vd_flags & VDF_MOUSECURSOR) {
2187                                 vd->vd_flags &= ~VDF_MOUSECURSOR;
2188 #ifndef SC_NO_CUTPASTE
2189                                 vt_mouse_state(VT_MOUSE_HIDE);
2190 #endif
2191                         }
2192                         return (0);
2193                 case MOUSE_SHOW:
2194                         if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2195                                 vd->vd_flags |= VDF_MOUSECURSOR;
2196                                 vd->vd_mx = vd->vd_width / 2;
2197                                 vd->vd_my = vd->vd_height / 2;
2198 #ifndef SC_NO_CUTPASTE
2199                                 vt_mouse_state(VT_MOUSE_SHOW);
2200 #endif
2201                         }
2202                         return (0);
2203                 default:
2204                         return (EINVAL);
2205                 }
2206         }
2207         case PIO_VFONT: {
2208                 struct vt_font *vf;
2209
2210                 if (vd->vd_flags & VDF_TEXTMODE)
2211                         return (ENOTSUP);
2212
2213                 error = vtfont_load((void *)data, &vf);
2214                 if (error != 0)
2215                         return (error);
2216
2217                 error = vt_change_font(vw, vf);
2218                 vtfont_unref(vf);
2219                 return (error);
2220         }
2221         case GIO_SCRNMAP: {
2222                 scrmap_t *sm = (scrmap_t *)data;
2223
2224                 /* We don't have screen maps, so return a handcrafted one. */
2225                 for (i = 0; i < 256; i++)
2226                         sm->scrmap[i] = i;
2227                 return (0);
2228         }
2229         case KDSETMODE:
2230                 /*
2231                  * FIXME: This implementation is incomplete compared to
2232                  * syscons.
2233                  */
2234                 switch (*(int *)data) {
2235                 case KD_TEXT:
2236                 case KD_TEXT1:
2237                 case KD_PIXEL:
2238                         vw->vw_flags &= ~VWF_GRAPHICS;
2239                         break;
2240                 case KD_GRAPHICS:
2241                         vw->vw_flags |= VWF_GRAPHICS;
2242                         break;
2243                 }
2244                 return (0);
2245         case KDENABIO:          /* allow io operations */
2246                 error = priv_check(td, PRIV_IO);
2247                 if (error != 0)
2248                         return (error);
2249                 error = securelevel_gt(td->td_ucred, 0);
2250                 if (error != 0)
2251                         return (error);
2252 #if defined(__i386__)
2253                 td->td_frame->tf_eflags |= PSL_IOPL;
2254 #elif defined(__amd64__)
2255                 td->td_frame->tf_rflags |= PSL_IOPL;
2256 #endif
2257                 return (0);
2258         case KDDISABIO:         /* disallow io operations (default) */
2259 #if defined(__i386__)
2260                 td->td_frame->tf_eflags &= ~PSL_IOPL;
2261 #elif defined(__amd64__)
2262                 td->td_frame->tf_rflags &= ~PSL_IOPL;
2263 #endif
2264                 return (0);
2265         case KDMKTONE:          /* sound the bell */
2266                 vtterm_beep(tm, *(u_int *)data);
2267                 return (0);
2268         case KIOCSOUND:         /* make tone (*data) hz */
2269                 /* TODO */
2270                 return (0);
2271         case CONS_SETKBD:               /* set the new keyboard */
2272                 mtx_lock(&Giant);
2273                 error = 0;
2274                 if (vd->vd_keyboard != *(int *)data) {
2275                         kbd = kbd_get_keyboard(*(int *)data);
2276                         if (kbd == NULL) {
2277                                 mtx_unlock(&Giant);
2278                                 return (EINVAL);
2279                         }
2280                         i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2281                             (void *)vd, vt_kbdevent, vd);
2282                         if (i >= 0) {
2283                                 if (vd->vd_keyboard != -1) {
2284                                         vt_save_kbd_state(vd->vd_curwindow, kbd);
2285                                         kbd_release(kbd, (void *)vd);
2286                                 }
2287                                 kbd = kbd_get_keyboard(i);
2288                                 vd->vd_keyboard = i;
2289
2290                                 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2291                                 vt_update_kbd_state(vd->vd_curwindow, kbd);
2292                         } else {
2293                                 error = EPERM;  /* XXX */
2294                         }
2295                 }
2296                 mtx_unlock(&Giant);
2297                 return (error);
2298         case CONS_RELKBD:               /* release the current keyboard */
2299                 mtx_lock(&Giant);
2300                 error = 0;
2301                 if (vd->vd_keyboard != -1) {
2302                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2303                         if (kbd == NULL) {
2304                                 mtx_unlock(&Giant);
2305                                 return (EINVAL);
2306                         }
2307                         vt_save_kbd_state(vd->vd_curwindow, kbd);
2308                         error = kbd_release(kbd, (void *)vd);
2309                         if (error == 0) {
2310                                 vd->vd_keyboard = -1;
2311                         }
2312                 }
2313                 mtx_unlock(&Giant);
2314                 return (error);
2315         case VT_ACTIVATE: {
2316                 int win;
2317                 win = *(int *)data - 1;
2318                 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2319                     VT_UNIT(vw), win);
2320                 if ((win >= VT_MAXWINDOWS) || (win < 0))
2321                         return (EINVAL);
2322                 return (vt_proc_window_switch(vd->vd_windows[win]));
2323         }
2324         case VT_GETACTIVE:
2325                 *(int *)data = vd->vd_curwindow->vw_number + 1;
2326                 return (0);
2327         case VT_GETINDEX:
2328                 *(int *)data = vw->vw_number + 1;
2329                 return (0);
2330         case VT_LOCKSWITCH:
2331                 /* TODO: Check current state, switching can be in progress. */
2332                 if ((*(int *)data) == 0x01)
2333                         vw->vw_flags |= VWF_VTYLOCK;
2334                 else if ((*(int *)data) == 0x02)
2335                         vw->vw_flags &= ~VWF_VTYLOCK;
2336                 else
2337                         return (EINVAL);
2338                 return (0);
2339         case VT_OPENQRY:
2340                 VT_LOCK(vd);
2341                 for (i = 0; i < VT_MAXWINDOWS; i++) {
2342                         vw = vd->vd_windows[i];
2343                         if (vw == NULL)
2344                                 continue;
2345                         if (!(vw->vw_flags & VWF_OPENED)) {
2346                                 *(int *)data = vw->vw_number + 1;
2347                                 VT_UNLOCK(vd);
2348                                 return (0);
2349                         }
2350                 }
2351                 VT_UNLOCK(vd);
2352                 return (EINVAL);
2353         case VT_WAITACTIVE:
2354                 error = 0;
2355
2356                 i = *(unsigned int *)data;
2357                 if (i > VT_MAXWINDOWS)
2358                         return (EINVAL);
2359                 if (i != 0)
2360                         vw = vd->vd_windows[i - 1];
2361
2362                 VT_LOCK(vd);
2363                 while (vd->vd_curwindow != vw && error == 0)
2364                         error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2365                 VT_UNLOCK(vd);
2366                 return (error);
2367         case VT_SETMODE: {      /* set screen switcher mode */
2368                 struct vt_mode *mode;
2369                 struct proc *p1;
2370
2371                 mode = (struct vt_mode *)data;
2372                 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2373                 if (vw->vw_smode.mode == VT_PROCESS) {
2374                         p1 = pfind(vw->vw_pid);
2375                         if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2376                                 if (p1)
2377                                         PROC_UNLOCK(p1);
2378                                 DPRINTF(5, "error EPERM\n");
2379                                 return (EPERM);
2380                         }
2381                         if (p1)
2382                                 PROC_UNLOCK(p1);
2383                 }
2384                 if (mode->mode == VT_AUTO) {
2385                         vw->vw_smode.mode = VT_AUTO;
2386                         vw->vw_proc = NULL;
2387                         vw->vw_pid = 0;
2388                         DPRINTF(5, "VT_AUTO, ");
2389                         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2390                                 cnavailable(vw->vw_terminal->consdev, TRUE);
2391                         /* were we in the middle of the vty switching process? */
2392                         if (finish_vt_rel(vw, TRUE, &s) == 0)
2393                                 DPRINTF(5, "reset WAIT_REL, ");
2394                         if (finish_vt_acq(vw) == 0)
2395                                 DPRINTF(5, "reset WAIT_ACQ, ");
2396                         return (0);
2397                 } else if (mode->mode == VT_PROCESS) {
2398                         if (!ISSIGVALID(mode->relsig) ||
2399                             !ISSIGVALID(mode->acqsig) ||
2400                             !ISSIGVALID(mode->frsig)) {
2401                                 DPRINTF(5, "error EINVAL\n");
2402                                 return (EINVAL);
2403                         }
2404                         DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2405                         bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2406                         vw->vw_proc = td->td_proc;
2407                         vw->vw_pid = vw->vw_proc->p_pid;
2408                         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2409                                 cnavailable(vw->vw_terminal->consdev, FALSE);
2410                 } else {
2411                         DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2412                             mode->mode);
2413                         return (EINVAL);
2414                 }
2415                 DPRINTF(5, "\n");
2416                 return (0);
2417         }
2418         case VT_GETMODE:        /* get screen switcher mode */
2419                 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2420                 return (0);
2421
2422         case VT_RELDISP:        /* screen switcher ioctl */
2423                 /*
2424                  * This must be the current vty which is in the VT_PROCESS
2425                  * switching mode...
2426                  */
2427                 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2428                     VT_PROCESS)) {
2429                         return (EINVAL);
2430                 }
2431                 /* ...and this process is controlling it. */
2432                 if (vw->vw_proc != td->td_proc) {
2433                         return (EPERM);
2434                 }
2435                 error = EINVAL;
2436                 switch(*(int *)data) {
2437                 case VT_FALSE:  /* user refuses to release screen, abort */
2438                         if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2439                                 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2440                                     SC_DRIVER_NAME, VT_UNIT(vw));
2441                         break;
2442                 case VT_TRUE:   /* user has released screen, go on */
2443                         /* finish_vt_rel(..., TRUE, ...) should not be locked */
2444                         if (vw->vw_flags & VWF_SWWAIT_REL) {
2445                                 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2446                                         DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2447                                             SC_DRIVER_NAME, VT_UNIT(vw));
2448                         } else {
2449                                 error = EINVAL;
2450                         }
2451                         return (error);
2452                 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2453                         if ((error = finish_vt_acq(vw)) == 0)
2454                                 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2455                                     SC_DRIVER_NAME, VT_UNIT(vw));
2456                         break;
2457                 default:
2458                         break;
2459                 }
2460                 return (error);
2461         }
2462
2463         return (ENOIOCTL);
2464 }
2465
2466 static struct vt_window *
2467 vt_allocate_window(struct vt_device *vd, unsigned int window)
2468 {
2469         struct vt_window *vw;
2470         struct terminal *tm;
2471         term_pos_t size;
2472         struct winsize wsz;
2473
2474         vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2475         vw->vw_device = vd;
2476         vw->vw_number = window;
2477         vw->vw_kbdmode = K_XLATE;
2478
2479         if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2480                 vw->vw_font = vtfont_ref(&vt_font_default);
2481                 vt_compute_drawable_area(vw);
2482         }
2483
2484         vt_termsize(vd, vw->vw_font, &size);
2485         vt_winsize(vd, vw->vw_font, &wsz);
2486         vtbuf_init(&vw->vw_buf, &size);
2487
2488         tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2489         terminal_set_winsize(tm, &wsz);
2490         vd->vd_windows[window] = vw;
2491         callout_init(&vw->vw_proc_dead_timer, 0);
2492
2493         return (vw);
2494 }
2495
2496 void
2497 vt_upgrade(struct vt_device *vd)
2498 {
2499         struct vt_window *vw;
2500         unsigned int i;
2501
2502         if (!vty_enabled(VTY_VT))
2503                 return;
2504         if (main_vd->vd_driver == NULL)
2505                 return;
2506
2507         for (i = 0; i < VT_MAXWINDOWS; i++) {
2508                 vw = vd->vd_windows[i];
2509                 if (vw == NULL) {
2510                         /* New window. */
2511                         vw = vt_allocate_window(vd, i);
2512                 }
2513                 if (!(vw->vw_flags & VWF_READY)) {
2514                         callout_init(&vw->vw_proc_dead_timer, 0);
2515                         terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2516                         vw->vw_flags |= VWF_READY;
2517                         if (vw->vw_flags & VWF_CONSOLE) {
2518                                 /* For existing console window. */
2519                                 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2520                                     vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2521                         }
2522                 }
2523
2524         }
2525         VT_LOCK(vd);
2526         if (vd->vd_curwindow == NULL)
2527                 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2528
2529         if (!(vd->vd_flags & VDF_ASYNC)) {
2530                 /* Attach keyboard. */
2531                 vt_allocate_keyboard(vd);
2532
2533                 /* Init 25 Hz timer. */
2534                 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2535
2536                 /* Start timer when everything ready. */
2537                 vd->vd_flags |= VDF_ASYNC;
2538                 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2539                 vd->vd_timer_armed = 1;
2540         }
2541
2542         VT_UNLOCK(vd);
2543
2544         /* Refill settings with new sizes. */
2545         vt_resize(vd);
2546 }
2547
2548 static void
2549 vt_resize(struct vt_device *vd)
2550 {
2551         struct vt_window *vw;
2552         int i;
2553
2554         for (i = 0; i < VT_MAXWINDOWS; i++) {
2555                 vw = vd->vd_windows[i];
2556                 VT_LOCK(vd);
2557                 /* Assign default font to window, if not textmode. */
2558                 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2559                         vw->vw_font = vtfont_ref(&vt_font_default);
2560                 VT_UNLOCK(vd);
2561
2562                 /* Resize terminal windows */
2563                 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2564                         DPRINTF(100, "%s: vt_change_font() is busy, "
2565                             "window %d\n", __func__, i);
2566                 }
2567         }
2568 }
2569
2570 void
2571 vt_allocate(struct vt_driver *drv, void *softc)
2572 {
2573         struct vt_device *vd;
2574
2575         if (!vty_enabled(VTY_VT))
2576                 return;
2577
2578         if (main_vd->vd_driver == NULL) {
2579                 main_vd->vd_driver = drv;
2580                 printf("VT: initialize with new VT driver \"%s\".\n",
2581                     drv->vd_name);
2582         } else {
2583                 /*
2584                  * Check if have rights to replace current driver. For example:
2585                  * it is bad idea to replace KMS driver with generic VGA one.
2586                  */
2587                 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2588                         printf("VT: Driver priority %d too low. Current %d\n ",
2589                             drv->vd_priority, main_vd->vd_driver->vd_priority);
2590                         return;
2591                 }
2592                 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2593                     main_vd->vd_driver->vd_name, drv->vd_name);
2594         }
2595         vd = main_vd;
2596
2597         if (vd->vd_flags & VDF_ASYNC) {
2598                 /* Stop vt_flush periodic task. */
2599                 vt_suspend_flush_timer(vd);
2600                 /*
2601                  * Mute current terminal until we done. vt_change_font (called
2602                  * from vt_resize) will unmute it.
2603                  */
2604                 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2605         }
2606
2607         /*
2608          * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2609          * set it.
2610          */
2611         VT_LOCK(vd);
2612         vd->vd_flags &= ~VDF_TEXTMODE;
2613
2614         vd->vd_driver = drv;
2615         vd->vd_softc = softc;
2616         vd->vd_driver->vd_init(vd);
2617         VT_UNLOCK(vd);
2618
2619         /* Update windows sizes and initialize last items. */
2620         vt_upgrade(vd);
2621
2622 #ifdef DEV_SPLASH
2623         if (vd->vd_flags & VDF_SPLASH)
2624                 vtterm_splash(vd);
2625 #endif
2626
2627         if (vd->vd_flags & VDF_ASYNC) {
2628                 /* Allow to put chars now. */
2629                 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2630                 /* Rerun timer for screen updates. */
2631                 vt_resume_flush_timer(vd, 0);
2632         }
2633
2634         /*
2635          * Register as console. If it already registered, cnadd() will ignore
2636          * it.
2637          */
2638         termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2639 }
2640
2641 void
2642 vt_suspend()
2643 {
2644
2645         if (vt_suspendswitch == 0)
2646                 return;
2647         /* Save current window. */
2648         main_vd->vd_savedwindow = main_vd->vd_curwindow;
2649         /* Ask holding process to free window and switch to console window */
2650         vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2651 }
2652
2653 void
2654 vt_resume()
2655 {
2656
2657         if (vt_suspendswitch == 0)
2658                 return;
2659         /* Switch back to saved window */
2660         if (main_vd->vd_savedwindow != NULL)
2661                 vt_proc_window_switch(main_vd->vd_savedwindow);
2662         main_vd->vd_savedwindow = NULL;
2663 }