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