2 * Copyright (c) 2009, 2013 The FreeBSD Foundation
5 * This software was developed by Ed Schouten under sponsorship from the
8 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_compat.h"
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <sys/reboot.h>
51 #include <sys/systm.h>
52 #include <sys/terminal.h>
54 #include <dev/kbd/kbdreg.h>
55 #include <dev/vt/vt.h>
57 #if defined(__i386__) || defined(__amd64__)
58 #include <machine/psl.h>
59 #include <machine/frame.h>
62 static tc_bell_t vtterm_bell;
63 static tc_cursor_t vtterm_cursor;
64 static tc_putchar_t vtterm_putchar;
65 static tc_fill_t vtterm_fill;
66 static tc_copy_t vtterm_copy;
67 static tc_param_t vtterm_param;
68 static tc_done_t vtterm_done;
70 static tc_cnprobe_t vtterm_cnprobe;
71 static tc_cngetc_t vtterm_cngetc;
73 static tc_opened_t vtterm_opened;
74 static tc_ioctl_t vtterm_ioctl;
75 static tc_mmap_t vtterm_mmap;
77 const struct terminal_class vt_termclass = {
78 .tc_bell = vtterm_bell,
79 .tc_cursor = vtterm_cursor,
80 .tc_putchar = vtterm_putchar,
81 .tc_fill = vtterm_fill,
82 .tc_copy = vtterm_copy,
83 .tc_param = vtterm_param,
84 .tc_done = vtterm_done,
86 .tc_cnprobe = vtterm_cnprobe,
87 .tc_cngetc = vtterm_cngetc,
89 .tc_opened = vtterm_opened,
90 .tc_ioctl = vtterm_ioctl,
91 .tc_mmap = vtterm_mmap,
95 * Use a constant timer of 25 Hz to redraw the screen.
97 * XXX: In theory we should only fire up the timer when there is really
98 * activity. Unfortunately we cannot always start timers. We really
99 * don't want to process kernel messages synchronously, because it
100 * really slows down the system.
102 #define VT_TIMERFREQ 25
104 /* Bell pitch/duration. */
105 #define VT_BELLDURATION ((5 * hz + 99) / 100)
106 #define VT_BELLPITCH 800
108 #define VT_LOCK(vd) mtx_lock(&(vd)->vd_lock)
109 #define VT_UNLOCK(vd) mtx_unlock(&(vd)->vd_lock)
111 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
114 /* XXX while syscons is here. */
115 int sc_txtmouse_no_retrace_wait;
117 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
118 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
119 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
120 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
121 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
123 static unsigned int vt_unit = 0;
124 static MALLOC_DEFINE(M_VT, "vt", "vt device");
125 struct vt_device *main_vd = NULL;
128 extern unsigned int vt_logo_width;
129 extern unsigned int vt_logo_height;
130 extern unsigned int vt_logo_depth;
131 extern unsigned char vt_logo_image[];
134 extern struct vt_font vt_font_default;
135 #ifndef SC_NO_CUTPASTE
136 extern struct mouse_cursor vt_default_mouse_pointer;
139 static int signal_vt_rel(struct vt_window *);
140 static int signal_vt_acq(struct vt_window *);
141 static int finish_vt_rel(struct vt_window *, int, int *);
142 static int finish_vt_acq(struct vt_window *);
143 static int vt_window_switch(struct vt_window *);
144 static int vt_late_window_switch(struct vt_window *);
145 static int vt_proc_alive(struct vt_window *);
146 static void vt_resize(struct vt_device *);
149 vt_switch_timer(void *arg)
152 vt_late_window_switch((struct vt_window *)arg);
156 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
159 DPRINTF(40, "%s\n", __func__);
160 curvw->vw_switch_to = vw;
161 /* Set timer to allow switch in case when process hang. */
162 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
163 vt_switch_timer, (void *)vw);
164 /* Notify process about vt switch attempt. */
165 DPRINTF(30, "%s: Notify process.\n", __func__);
166 signal_vt_rel(curvw);
172 vt_window_postswitch(struct vt_window *vw)
179 /* vt_late_window_switch will done VT switching for regular case. */
181 vt_late_window_switch(struct vt_window *vw)
185 callout_stop(&vw->vw_proc_dead_timer);
187 ret = vt_window_switch(vw);
191 /* Notify owner process about terminal availability. */
192 if (vw->vw_smode.mode == VT_PROCESS) {
193 ret = vt_window_postswitch(vw);
200 vt_proc_window_switch(struct vt_window *vw)
202 struct vt_window *curvw;
203 struct vt_device *vd;
206 if (vw->vw_flags & VWF_VTYLOCK)
210 curvw = vd->vd_curwindow;
212 /* Ask current process permitions to switch away. */
213 if (curvw->vw_smode.mode == VT_PROCESS) {
214 DPRINTF(30, "%s: VT_PROCESS ", __func__);
215 if (vt_proc_alive(curvw) == FALSE) {
216 DPRINTF(30, "Dead. Cleaning.");
219 DPRINTF(30, "%s: Signaling process.\n", __func__);
220 /* Alive, try to ask him. */
221 ret = vt_window_preswitch(vw, curvw);
222 /* Wait for process answer or timeout. */
228 ret = vt_late_window_switch(vw);
232 /* Switch window ignoring process locking. */
234 vt_window_switch(struct vt_window *vw)
236 struct vt_device *vd = vw->vw_device;
237 struct vt_window *curvw = vd->vd_curwindow;
246 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
251 vd->vd_curwindow = vw;
252 vd->vd_flags |= VDF_INVALID;
253 cv_broadcast(&vd->vd_winswitch);
256 if (vd->vd_driver->vd_postswitch)
257 vd->vd_driver->vd_postswitch(vd);
259 /* Restore per-window keyboard mode. */
261 kbd = kbd_get_keyboard(vd->vd_keyboard);
263 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
266 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
272 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
275 size->tp_row = vd->vd_height;
276 size->tp_col = vd->vd_width;
278 size->tp_row /= vf->vf_height;
279 size->tp_col /= vf->vf_width;
284 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
287 size->ws_row = size->ws_ypixel = vd->vd_height;
288 size->ws_col = size->ws_xpixel = vd->vd_width;
290 size->ws_row /= vf->vf_height;
291 size->ws_col /= vf->vf_width;
296 vt_scroll(struct vt_window *vw, int offset, int whence)
301 if ((vw->vw_flags & VWF_SCROLL) == 0)
304 vt_termsize(vw->vw_device, vw->vw_font, &size);
306 diff = vthistory_seek(&vw->vw_buf, offset, whence);
308 * Offset changed, please update Nth lines on sceen.
309 * +N - Nth lines at top;
310 * -N - Nth lines at bottom.
313 if (diff < -size.tp_row || diff > size.tp_row) {
314 vw->vw_device->vd_flags |= VDF_INVALID;
317 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
321 vt_machine_kbdevent(int c)
326 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
329 /* XXX: Make this configurable! */
333 shutdown_nice(RB_HALT);
336 shutdown_nice(RB_HALT|RB_POWEROFF);
344 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
346 struct vt_device *vd;
350 /* Only special keys handled in ScrollLock mode */
351 if ((c & SPCLKEY) == 0)
357 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
358 vw = vd->vd_windows[c - F_SCR];
360 vt_proc_window_switch(vw);
368 /* Turn scrolling off. */
369 vt_scroll(vw, 0, VHS_END);
370 VTBUF_SLCK_DISABLE(&vw->vw_buf);
371 vw->vw_flags &= ~VWF_SCROLL;
374 case FKEY | F(49): /* Home key. */
375 vt_scroll(vw, 0, VHS_SET);
377 case FKEY | F(50): /* Arrow up. */
378 vt_scroll(vw, -1, VHS_CUR);
380 case FKEY | F(51): /* Page up. */
381 vt_termsize(vd, vw->vw_font, &size);
382 vt_scroll(vw, -size.tp_row, VHS_CUR);
384 case FKEY | F(57): /* End key. */
385 vt_scroll(vw, 0, VHS_END);
387 case FKEY | F(58): /* Arrow down. */
388 vt_scroll(vw, 1, VHS_CUR);
390 case FKEY | F(59): /* Page down. */
391 vt_termsize(vd, vw->vw_font, &size);
392 vt_scroll(vw, size.tp_row, VHS_CUR);
401 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
403 struct vt_window *vw = vd->vd_curwindow;
406 #if VT_ALT_TO_ESC_HACK
408 switch (c & ~RELKEY) {
409 case (SPCLKEY | RALT):
410 if (vt_enable_altgr != 0)
412 case (SPCLKEY | LALT):
413 vd->vd_kbstate &= ~ALKED;
415 /* Other keys ignored for RELKEY event. */
418 switch (c & ~RELKEY) {
419 case (SPCLKEY | RALT):
420 if (vt_enable_altgr != 0)
422 case (SPCLKEY | LALT):
423 vd->vd_kbstate |= ALKED;
428 /* Other keys ignored for RELKEY event. */
432 if (vt_machine_kbdevent(c))
435 if (vw->vw_flags & VWF_SCROLL) {
436 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
437 /* Scroll mode keys handled, nothing to do more. */
444 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
445 vw = vd->vd_windows[c - F_SCR];
447 vt_proc_window_switch(vw);
454 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
457 /* Turn scrolling on. */
458 vw->vw_flags |= VWF_SCROLL;
459 VTBUF_SLCK_ENABLE(&vw->vw_buf);
461 /* Turn scrolling off. */
462 vw->vw_flags &= ~VWF_SCROLL;
463 VTBUF_SLCK_DISABLE(&vw->vw_buf);
464 vt_scroll(vw, 0, VHS_END);
469 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
470 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
471 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
472 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
473 /* F1 through F12 keys. */
474 terminal_input_special(vw->vw_terminal,
475 TKEY_F1 + c - (FKEY | F(1)));
477 case FKEY | F(49): /* Home key. */
478 terminal_input_special(vw->vw_terminal, TKEY_HOME);
480 case FKEY | F(50): /* Arrow up. */
481 terminal_input_special(vw->vw_terminal, TKEY_UP);
483 case FKEY | F(51): /* Page up. */
484 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
486 case FKEY | F(53): /* Arrow left. */
487 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
489 case FKEY | F(55): /* Arrow right. */
490 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
492 case FKEY | F(57): /* End key. */
493 terminal_input_special(vw->vw_terminal, TKEY_END);
495 case FKEY | F(58): /* Arrow down. */
496 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
498 case FKEY | F(59): /* Page down. */
499 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
501 case FKEY | F(60): /* Insert key. */
502 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
504 case FKEY | F(61): /* Delete key. */
505 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
508 } else if (KEYFLAGS(c) == 0) {
509 /* Don't do UTF-8 conversion when doing raw mode. */
510 if (vw->vw_kbdmode == K_XLATE) {
511 #if VT_ALT_TO_ESC_HACK
512 if (vd->vd_kbstate & ALKED) {
514 * Prepend ESC sequence if one of ALT keys down.
516 terminal_input_char(vw->vw_terminal, 0x1b);
520 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
522 terminal_input_raw(vw->vw_terminal, c);
528 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
530 struct vt_device *vd = arg;
536 case KBDIO_UNLOADING:
538 vd->vd_keyboard = -1;
539 kbd_release(kbd, (void *)&vd->vd_keyboard);
546 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
547 vt_processkey(kbd, vd, c);
553 vt_allocate_keyboard(struct vt_device *vd)
559 idx0 = kbd_allocate("kbdmux", -1, (void *)&vd->vd_keyboard,
561 /* XXX: kb_token lost */
562 vd->vd_keyboard = idx0;
564 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
565 k0 = kbd_get_keyboard(idx0);
567 for (idx = kbd_find_keyboard2("*", -1, 0);
569 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
570 k = kbd_get_keyboard(idx);
572 if (idx == idx0 || KBD_IS_BUSY(k))
575 bzero(&ki, sizeof(ki));
576 strcpy(ki.kb_name, k->kb_name);
577 ki.kb_unit = k->kb_unit;
579 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
582 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
583 idx0 = kbd_allocate("*", -1, (void *)&vd->vd_keyboard,
586 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
592 vtterm_bell(struct terminal *tm)
594 struct vt_window *vw = tm->tm_softc;
595 struct vt_device *vd = vw->vw_device;
597 if (vd->vd_flags & VDF_QUIET_BELL)
600 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
604 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
606 struct vt_window *vw = tm->tm_softc;
608 vtbuf_cursor_position(&vw->vw_buf, p);
612 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
614 struct vt_window *vw = tm->tm_softc;
616 vtbuf_putchar(&vw->vw_buf, p, c);
620 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
622 struct vt_window *vw = tm->tm_softc;
624 vtbuf_fill_locked(&vw->vw_buf, r, c);
628 vtterm_copy(struct terminal *tm, const term_rect_t *r,
631 struct vt_window *vw = tm->tm_softc;
633 vtbuf_copy(&vw->vw_buf, r, p);
637 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
639 struct vt_window *vw = tm->tm_softc;
643 vtbuf_cursor_visibility(&vw->vw_buf, arg);
646 vw->vw_mouse_level = arg;
652 vt_determine_colors(term_char_t c, int cursor,
653 term_color_t *fg, term_color_t *bg)
656 *fg = TCHAR_FGCOLOR(c);
657 if (TCHAR_FORMAT(c) & TF_BOLD)
658 *fg = TCOLOR_LIGHT(*fg);
659 *bg = TCHAR_BGCOLOR(c);
661 if (TCHAR_FORMAT(c) & TF_REVERSE) {
676 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
677 int iscursor, unsigned int row, unsigned int col)
681 vt_determine_colors(c, iscursor, &fg, &bg);
687 src = vtfont_lookup(vf, c);
690 * Align the terminal to the centre of the screen.
691 * Fonts may not always be able to fill the entire
694 top = row * vf->vf_height + vd->vd_offset.tp_row;
695 left = col * vf->vf_width + vd->vd_offset.tp_col;
697 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
698 vf->vf_width, vf->vf_height, fg, bg);
700 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
706 vt_flush(struct vt_device *vd)
708 struct vt_window *vw = vd->vd_curwindow;
709 struct vt_font *vf = vw->vw_font;
710 struct vt_bufmask tmask;
711 unsigned int row, col;
715 #ifndef SC_NO_CUTPASTE
716 struct mouse_cursor *m;
720 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
723 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
724 vt_termsize(vd, vf, &size);
726 /* Force a full redraw when the screen contents are invalid. */
727 if (vd->vd_flags & VDF_INVALID) {
728 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
730 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
732 vd->vd_flags &= ~VDF_INVALID;
735 #ifndef SC_NO_CUTPASTE
736 if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) {
737 /* Mark last mouse position as dirty to erase. */
738 vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx,
743 for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
744 if (!VTBUF_DIRTYROW(&tmask, row))
746 r = VTBUF_GET_ROW(&vw->vw_buf, row);
747 for (col = tarea.tr_begin.tp_col;
748 col < tarea.tr_end.tp_col; col++) {
749 if (!VTBUF_DIRTYCOL(&tmask, col))
752 vt_bitblt_char(vd, vf, r[col],
753 VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
757 #ifndef SC_NO_CUTPASTE
758 /* Mouse disabled. */
759 if (vw->vw_flags & VWF_MOUSE_HIDE)
762 /* No mouse for DDB. */
763 if (kdb_active || panicstr != NULL)
766 if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
768 m = &vt_default_mouse_pointer;
769 bpl = (m->w + 7) >> 3; /* Bytes per sorce line. */
773 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
774 w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
775 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
776 h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
778 vd->vd_driver->vd_bitbltchr(vd, m->map, m->mask, bpl,
779 vd->vd_offset.tp_row + vd->vd_my,
780 vd->vd_offset.tp_col + vd->vd_mx,
781 w, h, TC_WHITE, TC_BLACK);
782 /* Save point of last mouse cursor to erase it later. */
783 vd->vd_mdirtyx = vd->vd_mx / vf->vf_width;
784 vd->vd_mdirtyy = vd->vd_my / vf->vf_height;
792 struct vt_device *vd;
795 /* Update screen if required. */
797 /* Schedule for next update. */
798 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
802 vtterm_done(struct terminal *tm)
804 struct vt_window *vw = tm->tm_softc;
805 struct vt_device *vd = vw->vw_device;
807 if (kdb_active || panicstr != NULL) {
808 /* Switch to the debugger. */
809 if (vd->vd_curwindow != vw) {
810 vd->vd_curwindow = vw;
811 vd->vd_flags |= VDF_INVALID;
812 if (vd->vd_driver->vd_postswitch)
813 vd->vd_driver->vd_postswitch(vd);
815 vd->vd_flags &= ~VDF_SPLASH;
817 } else if (!(vd->vd_flags & VDF_ASYNC)) {
824 vtterm_splash(struct vt_device *vd)
828 /* Display a nice boot splash. */
829 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
831 top = (vd->vd_height - vt_logo_height) / 2;
832 left = (vd->vd_width - vt_logo_width) / 2;
833 switch (vt_logo_depth) {
835 /* XXX: Unhardcode colors! */
836 vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
837 top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
839 vd->vd_flags |= VDF_SPLASH;
845 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
847 struct vt_window *vw = tm->tm_softc;
848 struct vt_device *vd = vw->vw_device;
851 if (vd->vd_flags & VDF_INITIALIZED)
852 /* Initialization already done. */
855 cp->cn_pri = vd->vd_driver->vd_init(vd);
856 if (cp->cn_pri == CN_DEAD) {
857 vd->vd_flags |= VDF_DEAD;
861 /* Initialize any early-boot keyboard drivers */
862 kbd_configure(KB_CONF_PROBE_ONLY);
864 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
865 vd->vd_windows[VT_CONSWINDOW] = vw;
866 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
868 if (!(vd->vd_flags & VDF_TEXTMODE))
869 vw->vw_font = vtfont_ref(&vt_font_default);
871 vtbuf_init_early(&vw->vw_buf);
872 vt_winsize(vd, vw->vw_font, &wsz);
873 terminal_set_winsize(tm, &wsz);
879 vd->vd_flags |= VDF_INITIALIZED;
884 vtterm_cngetc(struct terminal *tm)
886 struct vt_window *vw = tm->tm_softc;
887 struct vt_device *vd = vw->vw_device;
892 if (vw->vw_kbdsq && *vw->vw_kbdsq)
893 return (*vw->vw_kbdsq++);
896 /* Make sure the splash screen is not there. */
897 if (vd->vd_flags & VDF_SPLASH) {
899 vd->vd_flags &= ~VDF_SPLASH;
900 /* Mark screen as invalid to force update */
901 vd->vd_flags |= VDF_INVALID;
905 /* Stripped down keyboard handler. */
906 kbd = kbd_get_keyboard(vd->vd_keyboard);
910 /* Force keyboard input mode to K_XLATE */
912 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
914 /* Switch the keyboard to polling to make it work here. */
915 kbdd_poll(kbd, TRUE);
916 c = kbdd_read_char(kbd, 0);
917 kbdd_poll(kbd, FALSE);
921 if (vw->vw_flags & VWF_SCROLL) {
922 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
927 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
931 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
933 /* Turn scrolling on. */
934 vw->vw_flags |= VWF_SCROLL;
935 VTBUF_SLCK_ENABLE(&vw->vw_buf);
937 /* Turn scrolling off. */
938 vt_scroll(vw, 0, VHS_END);
939 vw->vw_flags &= ~VWF_SCROLL;
940 VTBUF_SLCK_DISABLE(&vw->vw_buf);
943 /* XXX: KDB can handle history. */
944 case SPCLKEY | FKEY | F(50): /* Arrow up. */
945 vw->vw_kbdsq = "\x1b[A";
947 case SPCLKEY | FKEY | F(58): /* Arrow down. */
948 vw->vw_kbdsq = "\x1b[B";
950 case SPCLKEY | FKEY | F(55): /* Arrow right. */
951 vw->vw_kbdsq = "\x1b[C";
953 case SPCLKEY | FKEY | F(53): /* Arrow left. */
954 vw->vw_kbdsq = "\x1b[D";
958 /* Force refresh to make scrollback work. */
960 } else if (KEYFLAGS(c) == 0) {
964 if (vw->vw_kbdsq && *vw->vw_kbdsq)
965 return (*vw->vw_kbdsq++);
971 vtterm_opened(struct terminal *tm, int opened)
973 struct vt_window *vw = tm->tm_softc;
974 struct vt_device *vd = vw->vw_device;
977 vd->vd_flags &= ~VDF_SPLASH;
979 vw->vw_flags |= VWF_OPENED;
981 vw->vw_flags &= ~VWF_OPENED;
982 /* TODO: finish ACQ/REL */
988 vt_change_font(struct vt_window *vw, struct vt_font *vf)
990 struct vt_device *vd = vw->vw_device;
991 struct terminal *tm = vw->vw_terminal;
998 * Changing fonts is a little tricky. We must prevent
999 * simultaneous access to the device, so we must stop
1000 * the display timer and the terminal from accessing.
1001 * We need to switch fonts and grow our screen buffer.
1003 * XXX: Right now the code uses terminal_mute() to
1004 * prevent data from reaching the console driver while
1005 * resizing the screen buffer. This isn't elegant...
1009 if (vw->vw_flags & VWF_BUSY) {
1010 /* Another process is changing the font. */
1014 if (vw->vw_font == NULL) {
1015 /* Our device doesn't need fonts. */
1019 vw->vw_flags |= VWF_BUSY;
1022 vt_termsize(vd, vf, &size);
1023 vt_winsize(vd, vf, &wsz);
1024 /* Save offset to font aligned area. */
1025 vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1026 vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1028 /* Grow the screen buffer and terminal. */
1029 terminal_mute(tm, 1);
1030 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1031 terminal_set_winsize_blank(tm, &wsz, 0);
1032 terminal_mute(tm, 0);
1034 /* Actually apply the font to the current window. */
1036 vtfont_unref(vw->vw_font);
1037 vw->vw_font = vtfont_ref(vf);
1039 /* Force a full redraw the next timer tick. */
1040 if (vd->vd_curwindow == vw)
1041 vd->vd_flags |= VDF_INVALID;
1042 vw->vw_flags &= ~VWF_BUSY;
1048 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1050 struct vt_device *vd = vw->vw_device;
1051 int l, r, t, b, w, h;
1053 if (vd->vd_driver->vd_drawrect == NULL)
1056 w = vd->vd_width - 1;
1057 h = vd->vd_height - 1;
1058 l = vd->vd_offset.tp_col - 1;
1060 t = vd->vd_offset.tp_row - 1;
1063 vd->vd_driver->vd_drawrect(vd, 0, 0, w, t, 1, c); /* Top bar. */
1064 vd->vd_driver->vd_drawrect(vd, 0, t, l, b, 1, c); /* Left bar. */
1065 vd->vd_driver->vd_drawrect(vd, r, t, w, b, 1, c); /* Right bar. */
1066 vd->vd_driver->vd_drawrect(vd, 0, b, w, h, 1, c); /* Bottom bar. */
1072 vt_proc_alive(struct vt_window *vw)
1076 if (vw->vw_smode.mode != VT_PROCESS)
1080 if ((p = pfind(vw->vw_pid)) != NULL)
1082 if (vw->vw_proc == p)
1085 vw->vw_smode.mode = VT_AUTO;
1086 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1093 signal_vt_rel(struct vt_window *vw)
1096 if (vw->vw_smode.mode != VT_PROCESS)
1098 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1103 vw->vw_flags |= VWF_SWWAIT_REL;
1104 PROC_LOCK(vw->vw_proc);
1105 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1106 PROC_UNLOCK(vw->vw_proc);
1107 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1112 signal_vt_acq(struct vt_window *vw)
1115 if (vw->vw_smode.mode != VT_PROCESS)
1117 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1118 cnavailable(vw->vw_terminal->consdev, FALSE);
1119 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1124 vw->vw_flags |= VWF_SWWAIT_ACQ;
1125 PROC_LOCK(vw->vw_proc);
1126 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1127 PROC_UNLOCK(vw->vw_proc);
1128 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1133 finish_vt_rel(struct vt_window *vw, int release, int *s)
1136 if (vw->vw_flags & VWF_SWWAIT_REL) {
1137 vw->vw_flags &= ~VWF_SWWAIT_REL;
1139 callout_drain(&vw->vw_proc_dead_timer);
1140 vt_late_window_switch(vw->vw_switch_to);
1148 finish_vt_acq(struct vt_window *vw)
1151 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1152 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1158 #ifndef SC_NO_CUTPASTE
1160 vt_mouse_terminput_button(struct vt_device *vd, int button)
1162 struct vt_window *vw;
1164 char mouseb[6] = "\x1B[M";
1167 vw = vd->vd_curwindow;
1170 /* Translate to char position. */
1171 x = vd->vd_mx / vf->vf_width;
1172 y = vd->vd_my / vf->vf_height;
1173 /* Avoid overflow. */
1174 x = MIN(x, 255 - '!');
1175 y = MIN(y, 255 - '!');
1177 mouseb[3] = ' ' + button;
1178 mouseb[4] = '!' + x;
1179 mouseb[5] = '!' + y;
1181 for (i = 0; i < sizeof(mouseb); i++ )
1182 terminal_input_char(vw->vw_terminal, mouseb[i]);
1186 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1191 case MOUSE_BUTTON_EVENT:
1193 /* Mouse button pressed. */
1194 if (event & MOUSE_BUTTON1DOWN)
1195 vt_mouse_terminput_button(vd, 0);
1196 if (event & MOUSE_BUTTON2DOWN)
1197 vt_mouse_terminput_button(vd, 1);
1198 if (event & MOUSE_BUTTON3DOWN)
1199 vt_mouse_terminput_button(vd, 2);
1201 /* Mouse button released. */
1202 vt_mouse_terminput_button(vd, 3);
1206 case MOUSE_MOTION_EVENT:
1207 if (mouse->u.data.z < 0) {
1209 sc_mouse_input_button(vd, 64);
1210 } else if (mouse->u.data.z > 0) {
1212 sc_mouse_input_button(vd, 65);
1220 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1222 struct vt_device *vd;
1223 struct vt_window *vw;
1230 vw = vd->vd_curwindow;
1234 if (vw->vw_flags & VWF_MOUSE_HIDE)
1235 return; /* Mouse disabled. */
1237 if (vf == NULL) /* Text mode. */
1241 * TODO: add flag about pointer position changed, to not redraw chars
1242 * under mouse pointer when nothing changed.
1245 if (vw->vw_mouse_level > 0)
1246 vt_mouse_terminput(vd, type, x, y, event, cnt);
1250 case MOUSE_MOTION_EVENT:
1255 vt_termsize(vd, vf, &size);
1260 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1261 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1265 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1266 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1267 vd->vd_mx / vf->vf_width,
1268 vd->vd_my / vf->vf_height) == 1)) {
1271 * We have something marked to copy, so update pointer
1272 * to window with selection.
1274 vd->vd_markedwin = vw;
1277 case MOUSE_BUTTON_EVENT:
1285 case MOUSE_BUTTON1DOWN:
1288 mark = VTB_MARK_END;
1290 case 1: /* single click: start cut operation */
1291 mark = VTB_MARK_START;
1293 case 2: /* double click: cut a word */
1294 mark = VTB_MARK_WORD;
1296 case 3: /* triple click: cut a line */
1297 mark = VTB_MARK_ROW;
1301 case VT_MOUSE_PASTEBUTTON:
1306 if (vd->vd_markedwin == NULL)
1308 /* Get current selecton size in bytes. */
1309 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1313 buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1314 /* Request cupy/paste buffer data, no more than `len' */
1315 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1318 len /= sizeof(term_char_t);
1319 for (i = 0; i < len; i++ ) {
1322 terminal_input_char(vw->vw_terminal, buf[i]);
1325 /* Done, so cleanup. */
1330 case VT_MOUSE_EXTENDBUTTON:
1333 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1334 mark = VTB_MARK_EXTEND;
1339 mark = VTB_MARK_EXTEND;
1347 /* Save buttons state. */
1349 vd->vd_mstate |= event;
1351 vd->vd_mstate &= ~event;
1353 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1354 vd->vd_my / vf->vf_height) == 1) {
1356 * We have something marked to copy, so update pointer to
1357 * window with selection.
1359 vd->vd_markedwin = vw;
1364 vt_mouse_state(int show)
1366 struct vt_device *vd;
1367 struct vt_window *vw;
1370 vw = vd->vd_curwindow;
1374 vw->vw_flags |= VWF_MOUSE_HIDE;
1377 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1384 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1385 int nprot, vm_memattr_t *memattr)
1387 struct vt_window *vw = tm->tm_softc;
1388 struct vt_device *vd = vw->vw_device;
1390 if (vd->vd_driver->vd_fb_mmap)
1391 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1398 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1401 struct vt_window *vw = tm->tm_softc;
1402 struct vt_device *vd = vw->vw_device;
1405 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1406 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1417 cmd = VT_WAITACTIVE;
1443 ival = IOCPARM_IVAL(data);
1444 data = (caddr_t)&ival;
1449 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1450 if (*(int *)data & ~0x7f)
1454 case GIO_DEADKEYMAP:
1455 case PIO_DEADKEYMAP:
1460 case KDSKBSTATE: /* set keyboard state (locks) */
1461 case KDGKBSTATE: /* get keyboard state (locks) */
1462 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1463 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1464 case KDSETLED: /* set keyboard LED status */
1465 case KDGETLED: /* get keyboard LED status */
1466 case KBADDKBD: /* add/remove keyboard to/from mux */
1471 kbd = kbd_get_keyboard(vd->vd_keyboard);
1473 error = kbdd_ioctl(kbd, cmd, data);
1475 if (error == ENOIOCTL) {
1476 if (cmd == KDGKBTYPE) {
1477 /* always return something? XXX */
1489 kbd = kbd_get_keyboard(vd->vd_keyboard);
1491 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1494 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1495 *(int *)data = mode;
1501 mode = *(int *)data;
1506 vw->vw_kbdmode = mode;
1507 if (vw == vd->vd_curwindow) {
1512 kbd = kbd_get_keyboard(vd->vd_keyboard);
1514 error = kbdd_ioctl(kbd, KDSKBMODE,
1525 case FBIO_GETWINORG: /* get frame buffer window origin */
1526 case FBIO_GETDISPSTART: /* get display start address */
1527 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1528 case FBIO_BLANK: /* blank display */
1529 if (vd->vd_driver->vd_fb_ioctl)
1530 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1532 case CONS_BLANKTIME:
1537 *(int *)data = M_CG640x480;
1539 case CONS_BELLTYPE: /* set bell type sound */
1540 if ((*(int *)data) & CONS_QUIET_BELL)
1541 vd->vd_flags |= VDF_QUIET_BELL;
1543 vd->vd_flags &= ~VDF_QUIET_BELL;
1545 case CONS_GETINFO: {
1546 vid_info_t *vi = (vid_info_t *)data;
1548 vi->m_num = vd->vd_curwindow->vw_number + 1;
1549 /* XXX: other fields! */
1553 *(int *)data = 0x200;
1558 case CONS_MOUSECTL: {
1559 mouse_info_t *mouse = (mouse_info_t*)data;
1562 * This has no effect on vt(4). We don't draw any mouse
1563 * cursor. Just ignore MOUSE_HIDE and MOUSE_SHOW to
1564 * prevent excessive errors. All the other commands
1565 * should not be applied to individual TTYs, but only to
1568 switch (mouse->operation) {
1570 vd->vd_flags &= ~VDF_MOUSECURSOR;
1573 vd->vd_mx = vd->vd_width / 2;
1574 vd->vd_my = vd->vd_height / 2;
1575 vd->vd_flags |= VDF_MOUSECURSOR;
1584 error = vtfont_load((void *)data, &vf);
1588 error = vt_change_font(vw, vf);
1590 /* XXX: replace 0 with current bg color. */
1591 vt_set_border(vw, vf, 0);
1597 scrmap_t *sm = (scrmap_t *)data;
1600 /* We don't have screen maps, so return a handcrafted one. */
1601 for (i = 0; i < 256; i++)
1608 case KDENABIO: /* allow io operations */
1609 error = priv_check(td, PRIV_IO);
1612 error = securelevel_gt(td->td_ucred, 0);
1615 #if defined(__i386__)
1616 td->td_frame->tf_eflags |= PSL_IOPL;
1617 #elif defined(__amd64__)
1618 td->td_frame->tf_rflags |= PSL_IOPL;
1621 case KDDISABIO: /* disallow io operations (default) */
1622 #if defined(__i386__)
1623 td->td_frame->tf_eflags &= ~PSL_IOPL;
1624 #elif defined(__amd64__)
1625 td->td_frame->tf_rflags &= ~PSL_IOPL;
1628 case KDMKTONE: /* sound the bell */
1631 case KIOCSOUND: /* make tone (*data) hz */
1634 case CONS_SETKBD: /* set the new keyboard */
1637 if (vd->vd_keyboard != *(int *)data) {
1638 kbd = kbd_get_keyboard(*(int *)data);
1643 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1644 (void *)&vd->vd_keyboard, vt_kbdevent, vd);
1646 if (vd->vd_keyboard != -1) {
1648 (void *)&vd->vd_keyboard);
1650 kbd = kbd_get_keyboard(i);
1651 vd->vd_keyboard = i;
1653 (void)kbdd_ioctl(kbd, KDSKBMODE,
1654 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1656 error = EPERM; /* XXX */
1661 case CONS_RELKBD: /* release the current keyboard */
1664 if (vd->vd_keyboard != -1) {
1665 kbd = kbd_get_keyboard(vd->vd_keyboard);
1670 error = kbd_release(kbd, (void *)&vd->vd_keyboard);
1672 vd->vd_keyboard = -1;
1679 win = *(int *)data - 1;
1680 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1682 if ((win > VT_MAXWINDOWS) || (win < 0))
1684 return (vt_proc_window_switch(vd->vd_windows[win]));
1687 *(int *)data = vd->vd_curwindow->vw_number + 1;
1690 *(int *)data = vw->vw_number + 1;
1693 /* TODO: Check current state, switching can be in progress. */
1694 if ((*(int *)data) & 0x01)
1695 vw->vw_flags |= VWF_VTYLOCK;
1697 vw->vw_flags &= ~VWF_VTYLOCK;
1700 for (i = 0; i < VT_MAXWINDOWS; i++) {
1701 vw = vd->vd_windows[i];
1704 if (!(vw->vw_flags & VWF_OPENED)) {
1705 *(int *)data = vw->vw_number + 1;
1715 i = *(unsigned int *)data;
1716 if (i > VT_MAXWINDOWS)
1719 vw = vd->vd_windows[i - 1];
1722 while (vd->vd_curwindow != vw && error == 0)
1723 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1726 case VT_SETMODE: { /* set screen switcher mode */
1727 struct vt_mode *mode;
1730 mode = (struct vt_mode *)data;
1731 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1732 if (vw->vw_smode.mode == VT_PROCESS) {
1733 p1 = pfind(vw->vw_pid);
1734 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1737 DPRINTF(5, "error EPERM\n");
1743 if (mode->mode == VT_AUTO) {
1744 vw->vw_smode.mode = VT_AUTO;
1747 DPRINTF(5, "VT_AUTO, ");
1748 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1749 cnavailable(vw->vw_terminal->consdev, TRUE);
1750 /* were we in the middle of the vty switching process? */
1751 if (finish_vt_rel(vw, TRUE, &s) == 0)
1752 DPRINTF(5, "reset WAIT_REL, ");
1753 if (finish_vt_acq(vw) == 0)
1754 DPRINTF(5, "reset WAIT_ACQ, ");
1756 } else if (mode->mode == VT_PROCESS) {
1757 if (!ISSIGVALID(mode->relsig) ||
1758 !ISSIGVALID(mode->acqsig) ||
1759 !ISSIGVALID(mode->frsig)) {
1760 DPRINTF(5, "error EINVAL\n");
1763 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1764 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1765 vw->vw_proc = td->td_proc;
1766 vw->vw_pid = vw->vw_proc->p_pid;
1767 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1768 cnavailable(vw->vw_terminal->consdev, FALSE);
1770 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
1777 case VT_GETMODE: /* get screen switcher mode */
1778 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
1781 case VT_RELDISP: /* screen switcher ioctl */
1783 * This must be the current vty which is in the VT_PROCESS
1786 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
1790 /* ...and this process is controlling it. */
1791 if (vw->vw_proc != td->td_proc) {
1795 switch(*(int *)data) {
1796 case VT_FALSE: /* user refuses to release screen, abort */
1797 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
1798 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
1799 SC_DRIVER_NAME, VT_UNIT(vw));
1801 case VT_TRUE: /* user has released screen, go on */
1802 /* finish_vt_rel(..., TRUE, ...) should not be locked */
1803 if (vw->vw_flags & VWF_SWWAIT_REL) {
1804 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
1805 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
1806 SC_DRIVER_NAME, VT_UNIT(vw));
1811 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1812 if ((error = finish_vt_acq(vw)) == 0)
1813 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
1814 SC_DRIVER_NAME, VT_UNIT(vw));
1825 static struct vt_window *
1826 vt_allocate_window(struct vt_device *vd, unsigned int window)
1828 struct vt_window *vw;
1829 struct terminal *tm;
1833 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
1835 vw->vw_number = window;
1836 vw->vw_kbdmode = K_XLATE;
1838 if (!(vd->vd_flags & VDF_TEXTMODE))
1839 vw->vw_font = vtfont_ref(&vt_font_default);
1841 vt_termsize(vd, vw->vw_font, &size);
1842 vt_winsize(vd, vw->vw_font, &wsz);
1843 vtbuf_init(&vw->vw_buf, &size);
1845 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
1846 terminal_set_winsize(tm, &wsz);
1847 vd->vd_windows[window] = vw;
1848 callout_init(&vw->vw_proc_dead_timer, 0);
1854 vt_upgrade(struct vt_device *vd)
1856 struct vt_window *vw;
1859 /* Device didn't pass vd_init() or already upgraded. */
1860 if (vd->vd_flags & (VDF_ASYNC|VDF_DEAD))
1862 vd->vd_flags |= VDF_ASYNC;
1864 mtx_init(&vd->vd_lock, "vtdev", NULL, MTX_DEF);
1865 cv_init(&vd->vd_winswitch, "vtwswt");
1867 /* Init 25 Hz timer. */
1868 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
1870 for (i = 0; i < VT_MAXWINDOWS; i++) {
1871 vw = vd->vd_windows[i];
1874 vw = vt_allocate_window(vd, i);
1876 if (i == VT_CONSWINDOW) {
1877 /* Console window. */
1878 EVENTHANDLER_REGISTER(shutdown_pre_sync,
1879 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
1881 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
1883 if (vd->vd_curwindow == NULL)
1884 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
1886 /* Attach keyboard. */
1887 vt_allocate_keyboard(vd);
1888 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
1890 /* Start timer when everything ready. */
1891 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
1895 vt_resize(struct vt_device *vd)
1897 struct vt_window *vw;
1900 for (i = 0; i < VT_MAXWINDOWS; i++) {
1901 vw = vd->vd_windows[i];
1902 /* Resize terminal windows */
1903 vt_change_font(vw, vw->vw_font);
1908 vt_allocate(struct vt_driver *drv, void *softc)
1910 struct vt_device *vd;
1913 if (main_vd == NULL) {
1914 main_vd = malloc(sizeof *vd, M_VT, M_WAITOK|M_ZERO);
1915 printf("%s: VT initialize with new VT driver.\n", __func__);
1918 * Check if have rights to replace current driver. For example:
1919 * it is bad idea to replace KMS driver with generic VGA one.
1921 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
1922 printf("%s: Driver priority %d too low. Current %d\n ",
1923 __func__, drv->vd_priority,
1924 main_vd->vd_driver->vd_priority);
1927 printf("%s: Replace existing VT driver.\n", __func__);
1931 /* Stop vt_flush periodic task. */
1932 if (vd->vd_curwindow != NULL)
1933 callout_drain(&vd->vd_timer);
1935 vd->vd_driver = drv;
1936 vd->vd_softc = softc;
1937 vd->vd_driver->vd_init(vd);
1941 /* Refill settings with new sizes. */
1945 if (vd->vd_flags & VDF_SPLASH)
1949 if (vd->vd_curwindow != NULL)
1950 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
1952 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
1954 /* Update console window sizes to actual. */
1955 vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz);
1956 terminal_set_winsize(vd->vd_windows[VT_CONSWINDOW]->vw_terminal, &wsz);
1963 if (vt_suspendswitch == 0)
1965 /* Save current window. */
1966 main_vd->vd_savedwindow = main_vd->vd_curwindow;
1967 /* Ask holding process to free window and switch to console window */
1968 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
1975 if (vt_suspendswitch == 0)
1977 /* Switch back to saved window */
1978 if (main_vd->vd_savedwindow != NULL)
1979 vt_proc_window_switch(main_vd->vd_savedwindow);
1980 main_vd->vd_savedwindow = NULL;