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