]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/vt/vt_core.c
MFC r276282: Support ALT_BREAK_TO_DEBUGGER in vt(4)
[FreeBSD/stable/10.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 #if defined(KDB)
856                         kdb_alt_break(c, &vd->vd_altbrk);
857 #endif
858                         terminal_input_char(vw->vw_terminal, KEYCHAR(c));
859                 } else
860                         terminal_input_raw(vw->vw_terminal, c);
861         }
862         return (0);
863 }
864
865 static int
866 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
867 {
868         struct vt_device *vd = arg;
869         int c;
870
871         switch (event) {
872         case KBDIO_KEYINPUT:
873                 break;
874         case KBDIO_UNLOADING:
875                 mtx_lock(&Giant);
876                 vd->vd_keyboard = -1;
877                 kbd_release(kbd, (void *)vd);
878                 mtx_unlock(&Giant);
879                 return (0);
880         default:
881                 return (EINVAL);
882         }
883
884         while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
885                 vt_processkey(kbd, vd, c);
886
887         return (0);
888 }
889
890 static int
891 vt_allocate_keyboard(struct vt_device *vd)
892 {
893         int              idx0, idx;
894         keyboard_t      *k0, *k;
895         keyboard_info_t  ki;
896
897         idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
898         if (idx0 >= 0) {
899                 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
900                 k0 = kbd_get_keyboard(idx0);
901
902                 for (idx = kbd_find_keyboard2("*", -1, 0);
903                      idx != -1;
904                      idx = kbd_find_keyboard2("*", -1, idx + 1)) {
905                         k = kbd_get_keyboard(idx);
906
907                         if (idx == idx0 || KBD_IS_BUSY(k))
908                                 continue;
909
910                         bzero(&ki, sizeof(ki));
911                         strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
912                         ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
913                         ki.kb_unit = k->kb_unit;
914
915                         kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
916                 }
917         } else {
918                 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
919                 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
920                 if (idx0 < 0) {
921                         DPRINTF(10, "%s: No keyboard found.\n", __func__);
922                         return (-1);
923                 }
924         }
925         vd->vd_keyboard = idx0;
926         DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
927
928         return (idx0);
929 }
930
931 static void
932 vtterm_bell(struct terminal *tm)
933 {
934         struct vt_window *vw = tm->tm_softc;
935         struct vt_device *vd = vw->vw_device;
936
937         if (vd->vd_flags & VDF_QUIET_BELL)
938                 return;
939
940         sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
941 }
942
943 static void
944 vtterm_beep(struct terminal *tm, u_int param)
945 {
946         u_int freq, period;
947
948         if ((param == 0) || ((param & 0xffff) == 0)) {
949                 vtterm_bell(tm);
950                 return;
951         }
952
953         period = ((param >> 16) & 0xffff) * hz / 1000;
954         freq = 1193182 / (param & 0xffff);
955
956         sysbeep(freq, period);
957 }
958
959 static void
960 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
961 {
962         struct vt_window *vw = tm->tm_softc;
963
964         vtbuf_cursor_position(&vw->vw_buf, p);
965         vt_resume_flush_timer(vw->vw_device, 0);
966 }
967
968 static void
969 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
970 {
971         struct vt_window *vw = tm->tm_softc;
972
973         vtbuf_putchar(&vw->vw_buf, p, c);
974         vt_resume_flush_timer(vw->vw_device, 0);
975 }
976
977 static void
978 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
979 {
980         struct vt_window *vw = tm->tm_softc;
981
982         vtbuf_fill_locked(&vw->vw_buf, r, c);
983         vt_resume_flush_timer(vw->vw_device, 0);
984 }
985
986 static void
987 vtterm_copy(struct terminal *tm, const term_rect_t *r,
988     const term_pos_t *p)
989 {
990         struct vt_window *vw = tm->tm_softc;
991
992         vtbuf_copy(&vw->vw_buf, r, p);
993         vt_resume_flush_timer(vw->vw_device, 0);
994 }
995
996 static void
997 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
998 {
999         struct vt_window *vw = tm->tm_softc;
1000
1001         switch (cmd) {
1002         case TP_SHOWCURSOR:
1003                 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1004                 vt_resume_flush_timer(vw->vw_device, 0);
1005                 break;
1006         case TP_MOUSE:
1007                 vw->vw_mouse_level = arg;
1008                 break;
1009         }
1010 }
1011
1012 void
1013 vt_determine_colors(term_char_t c, int cursor,
1014     term_color_t *fg, term_color_t *bg)
1015 {
1016         term_color_t tmp;
1017         int invert;
1018
1019         invert = 0;
1020
1021         *fg = TCHAR_FGCOLOR(c);
1022         if (TCHAR_FORMAT(c) & TF_BOLD)
1023                 *fg = TCOLOR_LIGHT(*fg);
1024         *bg = TCHAR_BGCOLOR(c);
1025
1026         if (TCHAR_FORMAT(c) & TF_REVERSE)
1027                 invert ^= 1;
1028         if (cursor)
1029                 invert ^= 1;
1030
1031         if (invert) {
1032                 tmp = *fg;
1033                 *fg = *bg;
1034                 *bg = tmp;
1035         }
1036 }
1037
1038 #ifndef SC_NO_CUTPASTE
1039 int
1040 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1041 {
1042         unsigned int mx, my;
1043
1044         /*
1045          * We use the cursor position saved during the current refresh,
1046          * in case the cursor moved since.
1047          */
1048         mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1049         my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1050
1051         if (mx >= area->tr_end.tp_col ||
1052             mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1053             my >= area->tr_end.tp_row ||
1054             my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1055                 return (0);
1056         return (1);
1057 }
1058
1059 static void
1060 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1061 {
1062         term_rect_t area;
1063         struct vt_window *vw;
1064         struct vt_font *vf;
1065         int x, y;
1066
1067         vw = vd->vd_curwindow;
1068         vf = vw->vw_font;
1069
1070         x = vd->vd_mx_drawn;
1071         y = vd->vd_my_drawn;
1072
1073         if (vf != NULL) {
1074                 area.tr_begin.tp_col = x / vf->vf_width;
1075                 area.tr_begin.tp_row = y / vf->vf_height;
1076                 area.tr_end.tp_col =
1077                     ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1078                 area.tr_end.tp_row =
1079                     ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1080         } else {
1081                 /*
1082                  * No font loaded (ie. vt_vga operating in textmode).
1083                  *
1084                  * FIXME: This fake area needs to be revisited once the
1085                  * mouse cursor is supported in vt_vga's textmode.
1086                  */
1087                 area.tr_begin.tp_col = x;
1088                 area.tr_begin.tp_row = y;
1089                 area.tr_end.tp_col = x + 2;
1090                 area.tr_end.tp_row = y + 2;
1091         }
1092
1093         vtbuf_dirty(&vw->vw_buf, &area);
1094 }
1095 #endif
1096
1097 static int
1098 vt_flush(struct vt_device *vd)
1099 {
1100         struct vt_window *vw;
1101         struct vt_font *vf;
1102         term_rect_t tarea;
1103         term_pos_t size;
1104 #ifndef SC_NO_CUTPASTE
1105         int cursor_was_shown, cursor_moved;
1106 #endif
1107
1108         vw = vd->vd_curwindow;
1109         if (vw == NULL)
1110                 return (0);
1111
1112         if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1113                 return (0);
1114
1115         vf = vw->vw_font;
1116         if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1117                 return (0);
1118
1119 #ifndef SC_NO_CUTPASTE
1120         cursor_was_shown = vd->vd_mshown;
1121         cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1122             vd->vd_my != vd->vd_my_drawn);
1123
1124         /* Check if the cursor should be displayed or not. */
1125         if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1126             !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed.      */
1127             !kdb_active && panicstr == NULL) {  /* DDB inactive.          */
1128                 vd->vd_mshown = 1;
1129         } else {
1130                 vd->vd_mshown = 0;
1131         }
1132
1133         /*
1134          * If the cursor changed display state or moved, we must mark
1135          * the old position as dirty, so that it's erased.
1136          */
1137         if (cursor_was_shown != vd->vd_mshown ||
1138             (vd->vd_mshown && cursor_moved))
1139                 vt_mark_mouse_position_as_dirty(vd);
1140
1141         /*
1142          * Save position of the mouse cursor. It's used by backends to
1143          * know where to draw the cursor and during the next refresh to
1144          * erase the previous position.
1145          */
1146         vd->vd_mx_drawn = vd->vd_mx;
1147         vd->vd_my_drawn = vd->vd_my;
1148
1149         /*
1150          * If the cursor is displayed and has moved since last refresh,
1151          * mark the new position as dirty.
1152          */
1153         if (vd->vd_mshown && cursor_moved)
1154                 vt_mark_mouse_position_as_dirty(vd);
1155 #endif
1156
1157         vtbuf_undirty(&vw->vw_buf, &tarea);
1158         vt_termsize(vd, vf, &size);
1159
1160         /* Force a full redraw when the screen contents are invalid. */
1161         if (vd->vd_flags & VDF_INVALID) {
1162                 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1163                 tarea.tr_end = size;
1164
1165                 vd->vd_flags &= ~VDF_INVALID;
1166         }
1167
1168         if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1169                 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1170                 return (1);
1171         }
1172
1173         return (0);
1174 }
1175
1176 static void
1177 vt_timer(void *arg)
1178 {
1179         struct vt_device *vd;
1180         int changed;
1181
1182         vd = arg;
1183         /* Update screen if required. */
1184         changed = vt_flush(vd);
1185
1186         /* Schedule for next update. */
1187         if (changed)
1188                 vt_schedule_flush(vd, 0);
1189         else
1190                 vd->vd_timer_armed = 0;
1191 }
1192
1193 static void
1194 vtterm_done(struct terminal *tm)
1195 {
1196         struct vt_window *vw = tm->tm_softc;
1197         struct vt_device *vd = vw->vw_device;
1198
1199         if (kdb_active || panicstr != NULL) {
1200                 /* Switch to the debugger. */
1201                 if (vd->vd_curwindow != vw) {
1202                         vd->vd_curwindow = vw;
1203                         vd->vd_flags |= VDF_INVALID;
1204                         if (vd->vd_driver->vd_postswitch)
1205                                 vd->vd_driver->vd_postswitch(vd);
1206                 }
1207                 vd->vd_flags &= ~VDF_SPLASH;
1208                 vt_flush(vd);
1209         } else if (!(vd->vd_flags & VDF_ASYNC)) {
1210                 vt_flush(vd);
1211         }
1212 }
1213
1214 #ifdef DEV_SPLASH
1215 static void
1216 vtterm_splash(struct vt_device *vd)
1217 {
1218         vt_axis_t top, left;
1219
1220         /* Display a nice boot splash. */
1221         if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1222
1223                 top = (vd->vd_height - vt_logo_height) / 2;
1224                 left = (vd->vd_width - vt_logo_width) / 2;
1225                 switch (vt_logo_depth) {
1226                 case 1:
1227                         /* XXX: Unhardcode colors! */
1228                         vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1229                             vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1230                             left, top, TC_WHITE, TC_BLACK);
1231                 }
1232                 vd->vd_flags |= VDF_SPLASH;
1233         }
1234 }
1235 #endif
1236
1237
1238 static void
1239 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1240 {
1241         struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1242         struct vt_window *vw = tm->tm_softc;
1243         struct vt_device *vd = vw->vw_device;
1244         struct winsize wsz;
1245         term_attr_t attr;
1246         term_char_t c;
1247
1248         if (!vty_enabled(VTY_VT))
1249                 return;
1250
1251         if (vd->vd_flags & VDF_INITIALIZED)
1252                 /* Initialization already done. */
1253                 return;
1254
1255         SET_FOREACH(vtdlist, vt_drv_set) {
1256                 vtd = *vtdlist;
1257                 if (vtd->vd_probe == NULL)
1258                         continue;
1259                 if (vtd->vd_probe(vd) == CN_DEAD)
1260                         continue;
1261                 if ((vtdbest == NULL) ||
1262                     (vtd->vd_priority > vtdbest->vd_priority))
1263                         vtdbest = vtd;
1264         }
1265         if (vtdbest == NULL) {
1266                 cp->cn_pri = CN_DEAD;
1267                 vd->vd_flags |= VDF_DEAD;
1268         } else {
1269                 vd->vd_driver = vtdbest;
1270                 cp->cn_pri = vd->vd_driver->vd_init(vd);
1271         }
1272
1273         /* Check if driver's vt_init return CN_DEAD. */
1274         if (cp->cn_pri == CN_DEAD) {
1275                 vd->vd_flags |= VDF_DEAD;
1276         }
1277
1278         /* Initialize any early-boot keyboard drivers */
1279         kbd_configure(KB_CONF_PROBE_ONLY);
1280
1281         vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1282         vd->vd_windows[VT_CONSWINDOW] = vw;
1283         sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1284
1285         /* Attach default font if not in TEXTMODE. */
1286         if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1287                 vw->vw_font = vtfont_ref(&vt_font_default);
1288                 vt_compute_drawable_area(vw);
1289         }
1290
1291         /*
1292          * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1293          * that we have the real viewable size, fix it in the static
1294          * buffer.
1295          */
1296         if (vd->vd_width != 0 && vd->vd_height != 0)
1297                 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1298
1299         vtbuf_init_early(&vw->vw_buf);
1300         vt_winsize(vd, vw->vw_font, &wsz);
1301         c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1302             TERMINAL_NORM_ATTR;
1303         attr.ta_format = TCHAR_FORMAT(c);
1304         attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1305         attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1306         terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1307
1308         if (vtdbest != NULL) {
1309 #ifdef DEV_SPLASH
1310                 vtterm_splash(vd);
1311 #endif
1312                 vd->vd_flags |= VDF_INITIALIZED;
1313         }
1314 }
1315
1316 static int
1317 vtterm_cngetc(struct terminal *tm)
1318 {
1319         struct vt_window *vw = tm->tm_softc;
1320         struct vt_device *vd = vw->vw_device;
1321         keyboard_t *kbd;
1322         u_int c;
1323
1324         if (vw->vw_kbdsq && *vw->vw_kbdsq)
1325                 return (*vw->vw_kbdsq++);
1326
1327         /* Make sure the splash screen is not there. */
1328         if (vd->vd_flags & VDF_SPLASH) {
1329                 /* Remove splash */
1330                 vd->vd_flags &= ~VDF_SPLASH;
1331                 /* Mark screen as invalid to force update */
1332                 vd->vd_flags |= VDF_INVALID;
1333                 vt_flush(vd);
1334         }
1335
1336         /* Stripped down keyboard handler. */
1337         kbd = kbd_get_keyboard(vd->vd_keyboard);
1338         if (kbd == NULL)
1339                 return (-1);
1340
1341         /* Force keyboard input mode to K_XLATE */
1342         vw->vw_kbdmode = K_XLATE;
1343         vt_update_kbd_mode(vw, kbd);
1344
1345         /* Switch the keyboard to polling to make it work here. */
1346         kbdd_poll(kbd, TRUE);
1347         c = kbdd_read_char(kbd, 0);
1348         kbdd_poll(kbd, FALSE);
1349         if (c & RELKEY)
1350                 return (-1);
1351
1352         if (vw->vw_flags & VWF_SCROLL) {
1353                 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1354                 vt_flush(vd);
1355                 return (-1);
1356         }
1357
1358         /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1359         if (c & SPCLKEY) {
1360                 switch (c) {
1361                 case SPCLKEY | SLK:
1362                         vt_save_kbd_state(vw, kbd);
1363                         if (vw->vw_kbdstate & SLKED) {
1364                                 /* Turn scrolling on. */
1365                                 vw->vw_flags |= VWF_SCROLL;
1366                                 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1367                         } else {
1368                                 /* Turn scrolling off. */
1369                                 vt_scroll(vw, 0, VHS_END);
1370                                 vw->vw_flags &= ~VWF_SCROLL;
1371                                 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1372                         }
1373                         break;
1374                 /* XXX: KDB can handle history. */
1375                 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1376                         vw->vw_kbdsq = "\x1b[A";
1377                         break;
1378                 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1379                         vw->vw_kbdsq = "\x1b[B";
1380                         break;
1381                 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1382                         vw->vw_kbdsq = "\x1b[C";
1383                         break;
1384                 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1385                         vw->vw_kbdsq = "\x1b[D";
1386                         break;
1387                 }
1388
1389                 /* Force refresh to make scrollback work. */
1390                 vt_flush(vd);
1391         } else if (KEYFLAGS(c) == 0) {
1392                 return (KEYCHAR(c));
1393         }
1394
1395         if (vw->vw_kbdsq && *vw->vw_kbdsq)
1396                 return (*vw->vw_kbdsq++);
1397
1398         return (-1);
1399 }
1400
1401 static void
1402 vtterm_cngrab(struct terminal *tm)
1403 {
1404         struct vt_device *vd;
1405         struct vt_window *vw;
1406         keyboard_t *kbd;
1407
1408         vw = tm->tm_softc;
1409         vd = vw->vw_device;
1410
1411         if (!cold)
1412                 vt_window_switch(vw);
1413
1414         kbd = kbd_get_keyboard(vd->vd_keyboard);
1415         if (kbd == NULL)
1416                 return;
1417
1418         if (vw->vw_grabbed++ > 0)
1419                 return;
1420
1421         /*
1422          * Make sure the keyboard is accessible even when the kbd device
1423          * driver is disabled.
1424          */
1425         kbdd_enable(kbd);
1426
1427         /* We shall always use the keyboard in the XLATE mode here. */
1428         vw->vw_prev_kbdmode = vw->vw_kbdmode;
1429         vw->vw_kbdmode = K_XLATE;
1430         vt_update_kbd_mode(vw, kbd);
1431
1432         kbdd_poll(kbd, TRUE);
1433 }
1434
1435 static void
1436 vtterm_cnungrab(struct terminal *tm)
1437 {
1438         struct vt_device *vd;
1439         struct vt_window *vw;
1440         keyboard_t *kbd;
1441
1442         vw = tm->tm_softc;
1443         vd = vw->vw_device;
1444
1445         kbd = kbd_get_keyboard(vd->vd_keyboard);
1446         if (kbd == NULL)
1447                 return;
1448
1449         if (--vw->vw_grabbed > 0)
1450                 return;
1451
1452         kbdd_poll(kbd, FALSE);
1453
1454         vw->vw_kbdmode = vw->vw_prev_kbdmode;
1455         vt_update_kbd_mode(vw, kbd);
1456         kbdd_disable(kbd);
1457 }
1458
1459 static void
1460 vtterm_opened(struct terminal *tm, int opened)
1461 {
1462         struct vt_window *vw = tm->tm_softc;
1463         struct vt_device *vd = vw->vw_device;
1464
1465         VT_LOCK(vd);
1466         vd->vd_flags &= ~VDF_SPLASH;
1467         if (opened)
1468                 vw->vw_flags |= VWF_OPENED;
1469         else {
1470                 vw->vw_flags &= ~VWF_OPENED;
1471                 /* TODO: finish ACQ/REL */
1472         }
1473         VT_UNLOCK(vd);
1474 }
1475
1476 static int
1477 vt_set_border(struct vt_window *vw, term_color_t c)
1478 {
1479         struct vt_device *vd = vw->vw_device;
1480
1481         if (vd->vd_driver->vd_drawrect == NULL)
1482                 return (ENOTSUP);
1483
1484         /* Top bar. */
1485         if (vw->vw_draw_area.tr_begin.tp_row > 0)
1486                 vd->vd_driver->vd_drawrect(vd,
1487                     0, 0,
1488                     vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1489                     1, c);
1490
1491         /* Left bar. */
1492         if (vw->vw_draw_area.tr_begin.tp_col > 0)
1493                 vd->vd_driver->vd_drawrect(vd,
1494                     0, 0,
1495                     vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1496                     1, c);
1497
1498         /* Right bar. */
1499         if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1500                 vd->vd_driver->vd_drawrect(vd,
1501                     vw->vw_draw_area.tr_end.tp_col - 1, 0,
1502                     vd->vd_width - 1, vd->vd_height - 1,
1503                     1, c);
1504
1505         /* Bottom bar. */
1506         if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1507                 vd->vd_driver->vd_drawrect(vd,
1508                     0, vw->vw_draw_area.tr_end.tp_row - 1,
1509                     vd->vd_width - 1, vd->vd_height - 1,
1510                     1, c);
1511
1512         return (0);
1513 }
1514
1515 static int
1516 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1517 {
1518         struct vt_device *vd = vw->vw_device;
1519         struct terminal *tm = vw->vw_terminal;
1520         term_pos_t size;
1521         struct winsize wsz;
1522
1523         /*
1524          * Changing fonts.
1525          *
1526          * Changing fonts is a little tricky.  We must prevent
1527          * simultaneous access to the device, so we must stop
1528          * the display timer and the terminal from accessing.
1529          * We need to switch fonts and grow our screen buffer.
1530          *
1531          * XXX: Right now the code uses terminal_mute() to
1532          * prevent data from reaching the console driver while
1533          * resizing the screen buffer.  This isn't elegant...
1534          */
1535
1536         VT_LOCK(vd);
1537         if (vw->vw_flags & VWF_BUSY) {
1538                 /* Another process is changing the font. */
1539                 VT_UNLOCK(vd);
1540                 return (EBUSY);
1541         }
1542         vw->vw_flags |= VWF_BUSY;
1543         VT_UNLOCK(vd);
1544
1545         vt_termsize(vd, vf, &size);
1546         vt_winsize(vd, vf, &wsz);
1547
1548         /* Grow the screen buffer and terminal. */
1549         terminal_mute(tm, 1);
1550         vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1551         terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1552         terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1553         terminal_mute(tm, 0);
1554
1555         /* Actually apply the font to the current window. */
1556         VT_LOCK(vd);
1557         if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1558                 /*
1559                  * In case vt_change_font called to update size we don't need
1560                  * to update font link.
1561                  */
1562                 vtfont_unref(vw->vw_font);
1563                 vw->vw_font = vtfont_ref(vf);
1564         }
1565
1566         /*
1567          * Compute the drawable area and move the mouse cursor inside
1568          * it, in case the new area is smaller than the previous one.
1569          */
1570         vt_compute_drawable_area(vw);
1571         vd->vd_mx = min(vd->vd_mx,
1572             vw->vw_draw_area.tr_end.tp_col -
1573             vw->vw_draw_area.tr_begin.tp_col - 1);
1574         vd->vd_my = min(vd->vd_my,
1575             vw->vw_draw_area.tr_end.tp_row -
1576             vw->vw_draw_area.tr_begin.tp_row - 1);
1577
1578         /* Force a full redraw the next timer tick. */
1579         if (vd->vd_curwindow == vw) {
1580                 vt_set_border(vw, TC_BLACK);
1581                 vd->vd_flags |= VDF_INVALID;
1582                 vt_resume_flush_timer(vw->vw_device, 0);
1583         }
1584         vw->vw_flags &= ~VWF_BUSY;
1585         VT_UNLOCK(vd);
1586         return (0);
1587 }
1588
1589 static int
1590 vt_proc_alive(struct vt_window *vw)
1591 {
1592         struct proc *p;
1593
1594         if (vw->vw_smode.mode != VT_PROCESS)
1595                 return (FALSE);
1596
1597         if (vw->vw_proc) {
1598                 if ((p = pfind(vw->vw_pid)) != NULL)
1599                         PROC_UNLOCK(p);
1600                 if (vw->vw_proc == p)
1601                         return (TRUE);
1602                 vw->vw_proc = NULL;
1603                 vw->vw_smode.mode = VT_AUTO;
1604                 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1605                 vw->vw_pid = 0;
1606         }
1607         return (FALSE);
1608 }
1609
1610 static int
1611 signal_vt_rel(struct vt_window *vw)
1612 {
1613
1614         if (vw->vw_smode.mode != VT_PROCESS)
1615                 return (FALSE);
1616         if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1617                 vw->vw_proc = NULL;
1618                 vw->vw_pid = 0;
1619                 return (TRUE);
1620         }
1621         vw->vw_flags |= VWF_SWWAIT_REL;
1622         PROC_LOCK(vw->vw_proc);
1623         kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1624         PROC_UNLOCK(vw->vw_proc);
1625         DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1626         return (TRUE);
1627 }
1628
1629 static int
1630 signal_vt_acq(struct vt_window *vw)
1631 {
1632
1633         if (vw->vw_smode.mode != VT_PROCESS)
1634                 return (FALSE);
1635         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1636                 cnavailable(vw->vw_terminal->consdev, FALSE);
1637         if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1638                 vw->vw_proc = NULL;
1639                 vw->vw_pid = 0;
1640                 return (TRUE);
1641         }
1642         vw->vw_flags |= VWF_SWWAIT_ACQ;
1643         PROC_LOCK(vw->vw_proc);
1644         kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1645         PROC_UNLOCK(vw->vw_proc);
1646         DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1647         return (TRUE);
1648 }
1649
1650 static int
1651 finish_vt_rel(struct vt_window *vw, int release, int *s)
1652 {
1653
1654         if (vw->vw_flags & VWF_SWWAIT_REL) {
1655                 vw->vw_flags &= ~VWF_SWWAIT_REL;
1656                 if (release) {
1657                         callout_drain(&vw->vw_proc_dead_timer);
1658                         vt_late_window_switch(vw->vw_switch_to);
1659                 }
1660                 return (0);
1661         }
1662         return (EINVAL);
1663 }
1664
1665 static int
1666 finish_vt_acq(struct vt_window *vw)
1667 {
1668
1669         if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1670                 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1671                 return (0);
1672         }
1673         return (EINVAL);
1674 }
1675
1676 #ifndef SC_NO_CUTPASTE
1677 static void
1678 vt_mouse_terminput_button(struct vt_device *vd, int button)
1679 {
1680         struct vt_window *vw;
1681         struct vt_font *vf;
1682         char mouseb[6] = "\x1B[M";
1683         int i, x, y;
1684
1685         vw = vd->vd_curwindow;
1686         vf = vw->vw_font;
1687
1688         /* Translate to char position. */
1689         x = vd->vd_mx / vf->vf_width;
1690         y = vd->vd_my / vf->vf_height;
1691         /* Avoid overflow. */
1692         x = MIN(x, 255 - '!');
1693         y = MIN(y, 255 - '!');
1694
1695         mouseb[3] = ' ' + button;
1696         mouseb[4] = '!' + x;
1697         mouseb[5] = '!' + y;
1698
1699         for (i = 0; i < sizeof(mouseb); i++)
1700                 terminal_input_char(vw->vw_terminal, mouseb[i]);
1701 }
1702
1703 static void
1704 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1705     int cnt)
1706 {
1707
1708         switch (type) {
1709         case MOUSE_BUTTON_EVENT:
1710                 if (cnt > 0) {
1711                         /* Mouse button pressed. */
1712                         if (event & MOUSE_BUTTON1DOWN)
1713                                 vt_mouse_terminput_button(vd, 0);
1714                         if (event & MOUSE_BUTTON2DOWN)
1715                                 vt_mouse_terminput_button(vd, 1);
1716                         if (event & MOUSE_BUTTON3DOWN)
1717                                 vt_mouse_terminput_button(vd, 2);
1718                 } else {
1719                         /* Mouse button released. */
1720                         vt_mouse_terminput_button(vd, 3);
1721                 }
1722                 break;
1723 #ifdef notyet
1724         case MOUSE_MOTION_EVENT:
1725                 if (mouse->u.data.z < 0) {
1726                         /* Scroll up. */
1727                         sc_mouse_input_button(vd, 64);
1728                 } else if (mouse->u.data.z > 0) {
1729                         /* Scroll down. */
1730                         sc_mouse_input_button(vd, 65);
1731                 }
1732                 break;
1733 #endif
1734         }
1735 }
1736
1737 static void
1738 vt_mouse_paste()
1739 {
1740         term_char_t *buf;
1741         int i, len;
1742
1743         len = VD_PASTEBUFLEN(main_vd);
1744         buf = VD_PASTEBUF(main_vd);
1745         len /= sizeof(term_char_t);
1746         for (i = 0; i < len; i++) {
1747                 if (buf[i] == '\0')
1748                         continue;
1749                 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1750                     buf[i]);
1751         }
1752 }
1753
1754 void
1755 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1756 {
1757         struct vt_device *vd;
1758         struct vt_window *vw;
1759         struct vt_font *vf;
1760         term_pos_t size;
1761         int len, mark;
1762
1763         vd = main_vd;
1764         vw = vd->vd_curwindow;
1765         vf = vw->vw_font;
1766         mark = 0;
1767
1768         if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1769                 /*
1770                  * Either the mouse is disabled, or the window is in
1771                  * "graphics mode". The graphics mode is usually set by
1772                  * an X server, using the KDSETMODE ioctl.
1773                  */
1774                 return;
1775
1776         if (vf == NULL) /* Text mode. */
1777                 return;
1778
1779         /*
1780          * TODO: add flag about pointer position changed, to not redraw chars
1781          * under mouse pointer when nothing changed.
1782          */
1783
1784         if (vw->vw_mouse_level > 0)
1785                 vt_mouse_terminput(vd, type, x, y, event, cnt);
1786
1787         switch (type) {
1788         case MOUSE_ACTION:
1789         case MOUSE_MOTION_EVENT:
1790                 /* Movement */
1791                 x += vd->vd_mx;
1792                 y += vd->vd_my;
1793
1794                 vt_termsize(vd, vf, &size);
1795
1796                 /* Apply limits. */
1797                 x = MAX(x, 0);
1798                 y = MAX(y, 0);
1799                 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1800                 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1801
1802                 vd->vd_mx = x;
1803                 vd->vd_my = y;
1804                 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1805                         vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1806                             vd->vd_mx / vf->vf_width,
1807                             vd->vd_my / vf->vf_height);
1808
1809                 vt_resume_flush_timer(vw->vw_device, 0);
1810                 return; /* Done */
1811         case MOUSE_BUTTON_EVENT:
1812                 /* Buttons */
1813                 break;
1814         default:
1815                 return; /* Done */
1816         }
1817
1818         switch (event) {
1819         case MOUSE_BUTTON1DOWN:
1820                 switch (cnt % 4) {
1821                 case 0: /* up */
1822                         mark = VTB_MARK_END;
1823                         break;
1824                 case 1: /* single click: start cut operation */
1825                         mark = VTB_MARK_START;
1826                         break;
1827                 case 2: /* double click: cut a word */
1828                         mark = VTB_MARK_WORD;
1829                         break;
1830                 case 3: /* triple click: cut a line */
1831                         mark = VTB_MARK_ROW;
1832                         break;
1833                 }
1834                 break;
1835         case VT_MOUSE_PASTEBUTTON:
1836                 switch (cnt) {
1837                 case 0: /* up */
1838                         break;
1839                 default:
1840                         vt_mouse_paste();
1841                         break;
1842                 }
1843                 return; /* Done */
1844         case VT_MOUSE_EXTENDBUTTON:
1845                 switch (cnt) {
1846                 case 0: /* up */
1847                         if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1848                                 mark = VTB_MARK_EXTEND;
1849                         else
1850                                 mark = 0;
1851                         break;
1852                 default:
1853                         mark = VTB_MARK_EXTEND;
1854                         break;
1855                 }
1856                 break;
1857         default:
1858                 return; /* Done */
1859         }
1860
1861         /* Save buttons state. */
1862         if (cnt > 0)
1863                 vd->vd_mstate |= event;
1864         else
1865                 vd->vd_mstate &= ~event;
1866
1867         if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1868             vd->vd_my / vf->vf_height) == 1) {
1869                 /*
1870                  * We have something marked to copy, so update pointer to
1871                  * window with selection.
1872                  */
1873                 vt_resume_flush_timer(vw->vw_device, 0);
1874
1875                 switch (mark) {
1876                 case VTB_MARK_END:
1877                 case VTB_MARK_WORD:
1878                 case VTB_MARK_ROW:
1879                 case VTB_MARK_EXTEND:
1880                         break;
1881                 default:
1882                         /* Other types of mark do not require to copy data. */
1883                         return;
1884                 }
1885
1886                 /* Get current selection size in bytes. */
1887                 len = vtbuf_get_marked_len(&vw->vw_buf);
1888                 if (len <= 0)
1889                         return;
1890
1891                 /* Reallocate buffer only if old one is too small. */
1892                 if (len > VD_PASTEBUFSZ(vd)) {
1893                         VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1894                             M_WAITOK | M_ZERO);
1895                         /* Update buffer size. */
1896                         VD_PASTEBUFSZ(vd) = len;
1897                 }
1898                 /* Request copy/paste buffer data, no more than `len' */
1899                 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1900                     VD_PASTEBUFSZ(vd));
1901
1902                 VD_PASTEBUFLEN(vd) = len;
1903
1904                 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1905         }
1906 }
1907
1908 void
1909 vt_mouse_state(int show)
1910 {
1911         struct vt_device *vd;
1912         struct vt_window *vw;
1913
1914         vd = main_vd;
1915         vw = vd->vd_curwindow;
1916
1917         switch (show) {
1918         case VT_MOUSE_HIDE:
1919                 vw->vw_flags |= VWF_MOUSE_HIDE;
1920                 break;
1921         case VT_MOUSE_SHOW:
1922                 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1923                 break;
1924         }
1925
1926         /* Mark mouse position as dirty. */
1927         vt_mark_mouse_position_as_dirty(vd);
1928         vt_resume_flush_timer(vw->vw_device, 0);
1929 }
1930 #endif
1931
1932 static int
1933 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1934     int nprot, vm_memattr_t *memattr)
1935 {
1936         struct vt_window *vw = tm->tm_softc;
1937         struct vt_device *vd = vw->vw_device;
1938
1939         if (vd->vd_driver->vd_fb_mmap)
1940                 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1941                     memattr));
1942
1943         return (ENXIO);
1944 }
1945
1946 static int
1947 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1948     struct thread *td)
1949 {
1950         struct vt_window *vw = tm->tm_softc;
1951         struct vt_device *vd = vw->vw_device;
1952         keyboard_t *kbd;
1953         int error, i, s;
1954 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1955     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1956         int ival;
1957
1958         switch (cmd) {
1959         case _IO('v', 4):
1960                 cmd = VT_RELDISP;
1961                 break;
1962         case _IO('v', 5):
1963                 cmd = VT_ACTIVATE;
1964                 break;
1965         case _IO('v', 6):
1966                 cmd = VT_WAITACTIVE;
1967                 break;
1968         case _IO('K', 20):
1969                 cmd = KDSKBSTATE;
1970                 break;
1971         case _IO('K', 67):
1972                 cmd = KDSETRAD;
1973                 break;
1974         case _IO('K', 7):
1975                 cmd = KDSKBMODE;
1976                 break;
1977         case _IO('K', 8):
1978                 cmd = KDMKTONE;
1979                 break;
1980         case _IO('K', 63):
1981                 cmd = KIOCSOUND;
1982                 break;
1983         case _IO('K', 66):
1984                 cmd = KDSETLED;
1985                 break;
1986         case _IO('c', 110):
1987                 cmd = CONS_SETKBD;
1988                 break;
1989         default:
1990                 goto skip_thunk;
1991         }
1992         ival = IOCPARM_IVAL(data);
1993         data = (caddr_t)&ival;
1994 skip_thunk:
1995 #endif
1996
1997         switch (cmd) {
1998         case KDSETRAD:          /* set keyboard repeat & delay rates (old) */
1999                 if (*(int *)data & ~0x7f)
2000                         return (EINVAL);
2001                 /* FALLTHROUGH */
2002         case GIO_KEYMAP:
2003         case PIO_KEYMAP:
2004         case GIO_DEADKEYMAP:
2005         case PIO_DEADKEYMAP:
2006         case GETFKEY:
2007         case SETFKEY:
2008         case KDGKBINFO:
2009         case KDGKBTYPE:
2010         case KDGETREPEAT:       /* get keyboard repeat & delay rates */
2011         case KDSETREPEAT:       /* set keyboard repeat & delay rates (new) */
2012         case KBADDKBD:          /* add/remove keyboard to/from mux */
2013         case KBRELKBD: {
2014                 error = 0;
2015
2016                 mtx_lock(&Giant);
2017                 kbd = kbd_get_keyboard(vd->vd_keyboard);
2018                 if (kbd != NULL)
2019                         error = kbdd_ioctl(kbd, cmd, data);
2020                 mtx_unlock(&Giant);
2021                 if (error == ENOIOCTL) {
2022                         if (cmd == KDGKBTYPE) {
2023                                 /* always return something? XXX */
2024                                 *(int *)data = 0;
2025                         } else {
2026                                 return (ENODEV);
2027                         }
2028                 }
2029                 return (error);
2030         }
2031         case KDGKBSTATE: {      /* get keyboard state (locks) */
2032                 error = 0;
2033
2034                 if (vw == vd->vd_curwindow) {
2035                         mtx_lock(&Giant);
2036                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2037                         if (kbd != NULL)
2038                                 error = vt_save_kbd_state(vw, kbd);
2039                         mtx_unlock(&Giant);
2040
2041                         if (error != 0)
2042                                 return (error);
2043                 }
2044
2045                 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2046
2047                 return (error);
2048         }
2049         case KDSKBSTATE: {      /* set keyboard state (locks) */
2050                 int state;
2051
2052                 state = *(int *)data;
2053                 if (state & ~LOCK_MASK)
2054                         return (EINVAL);
2055
2056                 vw->vw_kbdstate &= ~LOCK_MASK;
2057                 vw->vw_kbdstate |= state;
2058
2059                 error = 0;
2060                 if (vw == vd->vd_curwindow) {
2061                         mtx_lock(&Giant);
2062                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2063                         if (kbd != NULL)
2064                                 error = vt_update_kbd_state(vw, kbd);
2065                         mtx_unlock(&Giant);
2066                 }
2067
2068                 return (error);
2069         }
2070         case KDGETLED: {        /* get keyboard LED status */
2071                 error = 0;
2072
2073                 if (vw == vd->vd_curwindow) {
2074                         mtx_lock(&Giant);
2075                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2076                         if (kbd != NULL)
2077                                 error = vt_save_kbd_leds(vw, kbd);
2078                         mtx_unlock(&Giant);
2079
2080                         if (error != 0)
2081                                 return (error);
2082                 }
2083
2084                 *(int *)data = vw->vw_kbdstate & LED_MASK;
2085
2086                 return (error);
2087         }
2088         case KDSETLED: {        /* set keyboard LED status */
2089                 int leds;
2090
2091                 leds = *(int *)data;
2092                 if (leds & ~LED_MASK)
2093                         return (EINVAL);
2094
2095                 vw->vw_kbdstate &= ~LED_MASK;
2096                 vw->vw_kbdstate |= leds;
2097
2098                 error = 0;
2099                 if (vw == vd->vd_curwindow) {
2100                         mtx_lock(&Giant);
2101                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2102                         if (kbd != NULL)
2103                                 error = vt_update_kbd_leds(vw, kbd);
2104                         mtx_unlock(&Giant);
2105                 }
2106
2107                 return (error);
2108         }
2109         case KDGKBMODE: {
2110                 error = 0;
2111
2112                 if (vw == vd->vd_curwindow) {
2113                         mtx_lock(&Giant);
2114                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2115                         if (kbd != NULL)
2116                                 error = vt_save_kbd_mode(vw, kbd);
2117                         mtx_unlock(&Giant);
2118
2119                         if (error != 0)
2120                                 return (error);
2121                 }
2122
2123                 *(int *)data = vw->vw_kbdmode;
2124
2125                 return (error);
2126         }
2127         case KDSKBMODE: {
2128                 int mode;
2129
2130                 mode = *(int *)data;
2131                 switch (mode) {
2132                 case K_XLATE:
2133                 case K_RAW:
2134                 case K_CODE:
2135                         vw->vw_kbdmode = mode;
2136
2137                         error = 0;
2138                         if (vw == vd->vd_curwindow) {
2139                                 mtx_lock(&Giant);
2140                                 kbd = kbd_get_keyboard(vd->vd_keyboard);
2141                                 if (kbd != NULL)
2142                                         error = vt_update_kbd_mode(vw, kbd);
2143                                 mtx_unlock(&Giant);
2144                         }
2145
2146                         return (error);
2147                 default:
2148                         return (EINVAL);
2149                 }
2150         }
2151         case FBIOGTYPE:
2152         case FBIO_GETWINORG:    /* get frame buffer window origin */
2153         case FBIO_GETDISPSTART: /* get display start address */
2154         case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2155         case FBIO_BLANK:        /* blank display */
2156                 if (vd->vd_driver->vd_fb_ioctl)
2157                         return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2158                 break;
2159         case CONS_BLANKTIME:
2160                 /* XXX */
2161                 return (0);
2162         case CONS_GET:
2163                 /* XXX */
2164                 *(int *)data = M_CG640x480;
2165                 return (0);
2166         case CONS_BELLTYPE:     /* set bell type sound */
2167                 if ((*(int *)data) & CONS_QUIET_BELL)
2168                         vd->vd_flags |= VDF_QUIET_BELL;
2169                 else
2170                         vd->vd_flags &= ~VDF_QUIET_BELL;
2171                 return (0);
2172         case CONS_GETINFO: {
2173                 vid_info_t *vi = (vid_info_t *)data;
2174                 if (vi->size != sizeof(struct vid_info))
2175                         return (EINVAL);
2176
2177                 if (vw == vd->vd_curwindow) {
2178                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2179                         if (kbd != NULL)
2180                                 vt_save_kbd_state(vw, kbd);
2181                 }
2182
2183                 vi->m_num = vd->vd_curwindow->vw_number + 1;
2184                 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2185                 /* XXX: other fields! */
2186                 return (0);
2187         }
2188         case CONS_GETVERS:
2189                 *(int *)data = 0x200;
2190                 return (0);
2191         case CONS_MODEINFO:
2192                 /* XXX */
2193                 return (0);
2194         case CONS_MOUSECTL: {
2195                 mouse_info_t *mouse = (mouse_info_t*)data;
2196
2197                 /*
2198                  * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2199                  * should not be applied to individual TTYs, but only to
2200                  * consolectl.
2201                  */
2202                 switch (mouse->operation) {
2203                 case MOUSE_HIDE:
2204                         if (vd->vd_flags & VDF_MOUSECURSOR) {
2205                                 vd->vd_flags &= ~VDF_MOUSECURSOR;
2206 #ifndef SC_NO_CUTPASTE
2207                                 vt_mouse_state(VT_MOUSE_HIDE);
2208 #endif
2209                         }
2210                         return (0);
2211                 case MOUSE_SHOW:
2212                         if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2213                                 vd->vd_flags |= VDF_MOUSECURSOR;
2214                                 vd->vd_mx = vd->vd_width / 2;
2215                                 vd->vd_my = vd->vd_height / 2;
2216 #ifndef SC_NO_CUTPASTE
2217                                 vt_mouse_state(VT_MOUSE_SHOW);
2218 #endif
2219                         }
2220                         return (0);
2221                 default:
2222                         return (EINVAL);
2223                 }
2224         }
2225         case PIO_VFONT: {
2226                 struct vt_font *vf;
2227
2228                 if (vd->vd_flags & VDF_TEXTMODE)
2229                         return (ENOTSUP);
2230
2231                 error = vtfont_load((void *)data, &vf);
2232                 if (error != 0)
2233                         return (error);
2234
2235                 error = vt_change_font(vw, vf);
2236                 vtfont_unref(vf);
2237                 return (error);
2238         }
2239         case PIO_VFONT_DEFAULT: {
2240                 /* Reset to default font. */
2241                 error = vt_change_font(vw, &vt_font_default);
2242                 return (error);
2243         }
2244         case GIO_SCRNMAP: {
2245                 scrmap_t *sm = (scrmap_t *)data;
2246
2247                 /* We don't have screen maps, so return a handcrafted one. */
2248                 for (i = 0; i < 256; i++)
2249                         sm->scrmap[i] = i;
2250                 return (0);
2251         }
2252         case KDSETMODE:
2253                 /*
2254                  * FIXME: This implementation is incomplete compared to
2255                  * syscons.
2256                  */
2257                 switch (*(int *)data) {
2258                 case KD_TEXT:
2259                 case KD_TEXT1:
2260                 case KD_PIXEL:
2261                         vw->vw_flags &= ~VWF_GRAPHICS;
2262                         break;
2263                 case KD_GRAPHICS:
2264                         vw->vw_flags |= VWF_GRAPHICS;
2265                         break;
2266                 }
2267                 return (0);
2268         case KDENABIO:          /* allow io operations */
2269                 error = priv_check(td, PRIV_IO);
2270                 if (error != 0)
2271                         return (error);
2272                 error = securelevel_gt(td->td_ucred, 0);
2273                 if (error != 0)
2274                         return (error);
2275 #if defined(__i386__)
2276                 td->td_frame->tf_eflags |= PSL_IOPL;
2277 #elif defined(__amd64__)
2278                 td->td_frame->tf_rflags |= PSL_IOPL;
2279 #endif
2280                 return (0);
2281         case KDDISABIO:         /* disallow io operations (default) */
2282 #if defined(__i386__)
2283                 td->td_frame->tf_eflags &= ~PSL_IOPL;
2284 #elif defined(__amd64__)
2285                 td->td_frame->tf_rflags &= ~PSL_IOPL;
2286 #endif
2287                 return (0);
2288         case KDMKTONE:          /* sound the bell */
2289                 vtterm_beep(tm, *(u_int *)data);
2290                 return (0);
2291         case KIOCSOUND:         /* make tone (*data) hz */
2292                 /* TODO */
2293                 return (0);
2294         case CONS_SETKBD:               /* set the new keyboard */
2295                 mtx_lock(&Giant);
2296                 error = 0;
2297                 if (vd->vd_keyboard != *(int *)data) {
2298                         kbd = kbd_get_keyboard(*(int *)data);
2299                         if (kbd == NULL) {
2300                                 mtx_unlock(&Giant);
2301                                 return (EINVAL);
2302                         }
2303                         i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2304                             (void *)vd, vt_kbdevent, vd);
2305                         if (i >= 0) {
2306                                 if (vd->vd_keyboard != -1) {
2307                                         vt_save_kbd_state(vd->vd_curwindow, kbd);
2308                                         kbd_release(kbd, (void *)vd);
2309                                 }
2310                                 kbd = kbd_get_keyboard(i);
2311                                 vd->vd_keyboard = i;
2312
2313                                 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2314                                 vt_update_kbd_state(vd->vd_curwindow, kbd);
2315                         } else {
2316                                 error = EPERM;  /* XXX */
2317                         }
2318                 }
2319                 mtx_unlock(&Giant);
2320                 return (error);
2321         case CONS_RELKBD:               /* release the current keyboard */
2322                 mtx_lock(&Giant);
2323                 error = 0;
2324                 if (vd->vd_keyboard != -1) {
2325                         kbd = kbd_get_keyboard(vd->vd_keyboard);
2326                         if (kbd == NULL) {
2327                                 mtx_unlock(&Giant);
2328                                 return (EINVAL);
2329                         }
2330                         vt_save_kbd_state(vd->vd_curwindow, kbd);
2331                         error = kbd_release(kbd, (void *)vd);
2332                         if (error == 0) {
2333                                 vd->vd_keyboard = -1;
2334                         }
2335                 }
2336                 mtx_unlock(&Giant);
2337                 return (error);
2338         case VT_ACTIVATE: {
2339                 int win;
2340                 win = *(int *)data - 1;
2341                 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2342                     VT_UNIT(vw), win);
2343                 if ((win >= VT_MAXWINDOWS) || (win < 0))
2344                         return (EINVAL);
2345                 return (vt_proc_window_switch(vd->vd_windows[win]));
2346         }
2347         case VT_GETACTIVE:
2348                 *(int *)data = vd->vd_curwindow->vw_number + 1;
2349                 return (0);
2350         case VT_GETINDEX:
2351                 *(int *)data = vw->vw_number + 1;
2352                 return (0);
2353         case VT_LOCKSWITCH:
2354                 /* TODO: Check current state, switching can be in progress. */
2355                 if ((*(int *)data) == 0x01)
2356                         vw->vw_flags |= VWF_VTYLOCK;
2357                 else if ((*(int *)data) == 0x02)
2358                         vw->vw_flags &= ~VWF_VTYLOCK;
2359                 else
2360                         return (EINVAL);
2361                 return (0);
2362         case VT_OPENQRY:
2363                 VT_LOCK(vd);
2364                 for (i = 0; i < VT_MAXWINDOWS; i++) {
2365                         vw = vd->vd_windows[i];
2366                         if (vw == NULL)
2367                                 continue;
2368                         if (!(vw->vw_flags & VWF_OPENED)) {
2369                                 *(int *)data = vw->vw_number + 1;
2370                                 VT_UNLOCK(vd);
2371                                 return (0);
2372                         }
2373                 }
2374                 VT_UNLOCK(vd);
2375                 return (EINVAL);
2376         case VT_WAITACTIVE: {
2377                 unsigned int idx;
2378
2379                 error = 0;
2380
2381                 idx = *(unsigned int *)data;
2382                 if (idx > VT_MAXWINDOWS)
2383                         return (EINVAL);
2384                 if (idx > 0)
2385                         vw = vd->vd_windows[idx - 1];
2386
2387                 VT_LOCK(vd);
2388                 while (vd->vd_curwindow != vw && error == 0)
2389                         error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2390                 VT_UNLOCK(vd);
2391                 return (error);
2392         }
2393         case VT_SETMODE: {      /* set screen switcher mode */
2394                 struct vt_mode *mode;
2395                 struct proc *p1;
2396
2397                 mode = (struct vt_mode *)data;
2398                 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2399                 if (vw->vw_smode.mode == VT_PROCESS) {
2400                         p1 = pfind(vw->vw_pid);
2401                         if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2402                                 if (p1)
2403                                         PROC_UNLOCK(p1);
2404                                 DPRINTF(5, "error EPERM\n");
2405                                 return (EPERM);
2406                         }
2407                         if (p1)
2408                                 PROC_UNLOCK(p1);
2409                 }
2410                 if (mode->mode == VT_AUTO) {
2411                         vw->vw_smode.mode = VT_AUTO;
2412                         vw->vw_proc = NULL;
2413                         vw->vw_pid = 0;
2414                         DPRINTF(5, "VT_AUTO, ");
2415                         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2416                                 cnavailable(vw->vw_terminal->consdev, TRUE);
2417                         /* were we in the middle of the vty switching process? */
2418                         if (finish_vt_rel(vw, TRUE, &s) == 0)
2419                                 DPRINTF(5, "reset WAIT_REL, ");
2420                         if (finish_vt_acq(vw) == 0)
2421                                 DPRINTF(5, "reset WAIT_ACQ, ");
2422                         return (0);
2423                 } else if (mode->mode == VT_PROCESS) {
2424                         if (!ISSIGVALID(mode->relsig) ||
2425                             !ISSIGVALID(mode->acqsig) ||
2426                             !ISSIGVALID(mode->frsig)) {
2427                                 DPRINTF(5, "error EINVAL\n");
2428                                 return (EINVAL);
2429                         }
2430                         DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2431                         bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2432                         vw->vw_proc = td->td_proc;
2433                         vw->vw_pid = vw->vw_proc->p_pid;
2434                         if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2435                                 cnavailable(vw->vw_terminal->consdev, FALSE);
2436                 } else {
2437                         DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2438                             mode->mode);
2439                         return (EINVAL);
2440                 }
2441                 DPRINTF(5, "\n");
2442                 return (0);
2443         }
2444         case VT_GETMODE:        /* get screen switcher mode */
2445                 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2446                 return (0);
2447
2448         case VT_RELDISP:        /* screen switcher ioctl */
2449                 /*
2450                  * This must be the current vty which is in the VT_PROCESS
2451                  * switching mode...
2452                  */
2453                 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2454                     VT_PROCESS)) {
2455                         return (EINVAL);
2456                 }
2457                 /* ...and this process is controlling it. */
2458                 if (vw->vw_proc != td->td_proc) {
2459                         return (EPERM);
2460                 }
2461                 error = EINVAL;
2462                 switch(*(int *)data) {
2463                 case VT_FALSE:  /* user refuses to release screen, abort */
2464                         if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2465                                 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2466                                     SC_DRIVER_NAME, VT_UNIT(vw));
2467                         break;
2468                 case VT_TRUE:   /* user has released screen, go on */
2469                         /* finish_vt_rel(..., TRUE, ...) should not be locked */
2470                         if (vw->vw_flags & VWF_SWWAIT_REL) {
2471                                 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2472                                         DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2473                                             SC_DRIVER_NAME, VT_UNIT(vw));
2474                         } else {
2475                                 error = EINVAL;
2476                         }
2477                         return (error);
2478                 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2479                         if ((error = finish_vt_acq(vw)) == 0)
2480                                 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2481                                     SC_DRIVER_NAME, VT_UNIT(vw));
2482                         break;
2483                 default:
2484                         break;
2485                 }
2486                 return (error);
2487         }
2488
2489         return (ENOIOCTL);
2490 }
2491
2492 static struct vt_window *
2493 vt_allocate_window(struct vt_device *vd, unsigned int window)
2494 {
2495         struct vt_window *vw;
2496         struct terminal *tm;
2497         term_pos_t size;
2498         struct winsize wsz;
2499
2500         vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2501         vw->vw_device = vd;
2502         vw->vw_number = window;
2503         vw->vw_kbdmode = K_XLATE;
2504
2505         if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2506                 vw->vw_font = vtfont_ref(&vt_font_default);
2507                 vt_compute_drawable_area(vw);
2508         }
2509
2510         vt_termsize(vd, vw->vw_font, &size);
2511         vt_winsize(vd, vw->vw_font, &wsz);
2512         vtbuf_init(&vw->vw_buf, &size);
2513
2514         tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2515         terminal_set_winsize(tm, &wsz);
2516         vd->vd_windows[window] = vw;
2517         callout_init(&vw->vw_proc_dead_timer, 0);
2518
2519         return (vw);
2520 }
2521
2522 void
2523 vt_upgrade(struct vt_device *vd)
2524 {
2525         struct vt_window *vw;
2526         unsigned int i;
2527         int register_handlers;
2528
2529         if (!vty_enabled(VTY_VT))
2530                 return;
2531         if (main_vd->vd_driver == NULL)
2532                 return;
2533
2534         for (i = 0; i < VT_MAXWINDOWS; i++) {
2535                 vw = vd->vd_windows[i];
2536                 if (vw == NULL) {
2537                         /* New window. */
2538                         vw = vt_allocate_window(vd, i);
2539                 }
2540                 if (!(vw->vw_flags & VWF_READY)) {
2541                         callout_init(&vw->vw_proc_dead_timer, 0);
2542                         terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2543                         vw->vw_flags |= VWF_READY;
2544                         if (vw->vw_flags & VWF_CONSOLE) {
2545                                 /* For existing console window. */
2546                                 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2547                                     vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2548                         }
2549                 }
2550
2551         }
2552         VT_LOCK(vd);
2553         if (vd->vd_curwindow == NULL)
2554                 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2555
2556         register_handlers = 0;
2557         if (!(vd->vd_flags & VDF_ASYNC)) {
2558                 /* Attach keyboard. */
2559                 vt_allocate_keyboard(vd);
2560
2561                 /* Init 25 Hz timer. */
2562                 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2563
2564                 /* Start timer when everything ready. */
2565                 vd->vd_flags |= VDF_ASYNC;
2566                 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2567                 vd->vd_timer_armed = 1;
2568                 register_handlers = 1;
2569         }
2570
2571         VT_UNLOCK(vd);
2572
2573         /* Refill settings with new sizes. */
2574         vt_resize(vd);
2575
2576         if (register_handlers) {
2577                 /* Register suspend/resume handlers. */
2578                 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2579                     vd, EVENTHANDLER_PRI_ANY);
2580                 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2581                     EVENTHANDLER_PRI_ANY);
2582         }
2583 }
2584
2585 static void
2586 vt_resize(struct vt_device *vd)
2587 {
2588         struct vt_window *vw;
2589         int i;
2590
2591         for (i = 0; i < VT_MAXWINDOWS; i++) {
2592                 vw = vd->vd_windows[i];
2593                 VT_LOCK(vd);
2594                 /* Assign default font to window, if not textmode. */
2595                 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2596                         vw->vw_font = vtfont_ref(&vt_font_default);
2597                 VT_UNLOCK(vd);
2598
2599                 /* Resize terminal windows */
2600                 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2601                         DPRINTF(100, "%s: vt_change_font() is busy, "
2602                             "window %d\n", __func__, i);
2603                 }
2604         }
2605 }
2606
2607 void
2608 vt_allocate(struct vt_driver *drv, void *softc)
2609 {
2610         struct vt_device *vd;
2611
2612         if (!vty_enabled(VTY_VT))
2613                 return;
2614
2615         if (main_vd->vd_driver == NULL) {
2616                 main_vd->vd_driver = drv;
2617                 printf("VT: initialize with new VT driver \"%s\".\n",
2618                     drv->vd_name);
2619         } else {
2620                 /*
2621                  * Check if have rights to replace current driver. For example:
2622                  * it is bad idea to replace KMS driver with generic VGA one.
2623                  */
2624                 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2625                         printf("VT: Driver priority %d too low. Current %d\n ",
2626                             drv->vd_priority, main_vd->vd_driver->vd_priority);
2627                         return;
2628                 }
2629                 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2630                     main_vd->vd_driver->vd_name, drv->vd_name);
2631         }
2632         vd = main_vd;
2633
2634         if (vd->vd_flags & VDF_ASYNC) {
2635                 /* Stop vt_flush periodic task. */
2636                 VT_LOCK(vd);
2637                 vt_suspend_flush_timer(vd);
2638                 VT_UNLOCK(vd);
2639                 /*
2640                  * Mute current terminal until we done. vt_change_font (called
2641                  * from vt_resize) will unmute it.
2642                  */
2643                 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2644         }
2645
2646         /*
2647          * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2648          * set it.
2649          */
2650         VT_LOCK(vd);
2651         vd->vd_flags &= ~VDF_TEXTMODE;
2652
2653         vd->vd_driver = drv;
2654         vd->vd_softc = softc;
2655         vd->vd_driver->vd_init(vd);
2656         VT_UNLOCK(vd);
2657
2658         /* Update windows sizes and initialize last items. */
2659         vt_upgrade(vd);
2660
2661 #ifdef DEV_SPLASH
2662         if (vd->vd_flags & VDF_SPLASH)
2663                 vtterm_splash(vd);
2664 #endif
2665
2666         if (vd->vd_flags & VDF_ASYNC) {
2667                 /* Allow to put chars now. */
2668                 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2669                 /* Rerun timer for screen updates. */
2670                 vt_resume_flush_timer(vd, 0);
2671         }
2672
2673         /*
2674          * Register as console. If it already registered, cnadd() will ignore
2675          * it.
2676          */
2677         termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2678 }
2679
2680 static void
2681 vt_suspend_handler(void *priv)
2682 {
2683         struct vt_device *vd;
2684
2685         vd = priv;
2686         if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2687                 vd->vd_driver->vd_suspend(vd);
2688 }
2689
2690 static void
2691 vt_resume_handler(void *priv)
2692 {
2693         struct vt_device *vd;
2694
2695         vd = priv;
2696         if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2697                 vd->vd_driver->vd_resume(vd);
2698 }
2699
2700 void
2701 vt_suspend(struct vt_device *vd)
2702 {
2703         int error;
2704
2705         if (vt_suspendswitch == 0)
2706                 return;
2707         /* Save current window. */
2708         vd->vd_savedwindow = vd->vd_curwindow;
2709         /* Ask holding process to free window and switch to console window */
2710         vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2711
2712         /* Wait for the window switch to complete. */
2713         error = 0;
2714         VT_LOCK(vd);
2715         while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2716                 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2717         VT_UNLOCK(vd);
2718 }
2719
2720 void
2721 vt_resume(struct vt_device *vd)
2722 {
2723
2724         if (vt_suspendswitch == 0)
2725                 return;
2726         /* Switch back to saved window, if any */
2727         vt_proc_window_switch(vd->vd_savedwindow);
2728         vd->vd_savedwindow = NULL;
2729 }