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 struct vt_device vt_consdev;
124 static unsigned int vt_unit = 0;
125 static MALLOC_DEFINE(M_VT, "vt", "vt device");
126 struct vt_device *main_vd = &vt_consdev;
129 extern unsigned int vt_logo_width;
130 extern unsigned int vt_logo_height;
131 extern unsigned int vt_logo_depth;
132 extern unsigned char vt_logo_image[];
135 extern struct vt_font vt_font_default;
136 #ifndef SC_NO_CUTPASTE
137 extern struct mouse_cursor vt_default_mouse_pointer;
140 static int signal_vt_rel(struct vt_window *);
141 static int signal_vt_acq(struct vt_window *);
142 static int finish_vt_rel(struct vt_window *, int, int *);
143 static int finish_vt_acq(struct vt_window *);
144 static int vt_window_switch(struct vt_window *);
145 static int vt_late_window_switch(struct vt_window *);
146 static int vt_proc_alive(struct vt_window *);
147 static void vt_resize(struct vt_device *);
148 static void vt_update_static(void *);
150 SET_DECLARE(vt_drv_set, struct vt_driver);
152 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
153 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
155 static struct terminal vt_consterm;
156 static struct vt_window vt_conswindow;
157 static struct vt_device vt_consdev = {
160 .vd_flags = VDF_INVALID,
161 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
162 .vd_curwindow = &vt_conswindow,
163 .vd_markedwin = NULL,
166 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
167 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
168 static struct vt_window vt_conswindow = {
169 .vw_number = VT_CONSWINDOW,
170 .vw_flags = VWF_CONSOLE,
172 .vb_buffer = vt_constextbuf,
173 .vb_rows = vt_constextbufrows,
174 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
177 .vb_flags = VBF_STATIC,
178 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
179 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
185 .vw_device = &vt_consdev,
186 .vw_terminal = &vt_consterm,
187 .vw_kbdmode = K_XLATE,
189 static struct terminal vt_consterm = {
190 .tm_class = &vt_termclass,
191 .tm_softc = &vt_conswindow,
194 static struct consdev vt_consterm_consdev = {
195 .cn_ops = &termcn_cnops,
196 .cn_arg = &vt_consterm,
200 /* Add to set of consoles. */
201 DATA_SET(cons_set, vt_consterm_consdev);
204 * Right after kmem is done to allow early drivers to use locking and allocate
207 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
209 /* Delay until all devices attached, to not waste time. */
210 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
213 /* Initialize locks/mem depended members. */
215 vt_update_static(void *dummy)
218 if (!vty_enabled(VTY_VT))
220 if (main_vd->vd_driver != NULL)
221 printf("VT: running with driver \"%s\".\n",
222 main_vd->vd_driver->vd_name);
224 printf("VT: init without driver.\n");
226 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
227 cv_init(&main_vd->vd_winswitch, "vtwswt");
231 vt_switch_timer(void *arg)
234 vt_late_window_switch((struct vt_window *)arg);
238 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
241 DPRINTF(40, "%s\n", __func__);
242 curvw->vw_switch_to = vw;
243 /* Set timer to allow switch in case when process hang. */
244 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
245 vt_switch_timer, (void *)vw);
246 /* Notify process about vt switch attempt. */
247 DPRINTF(30, "%s: Notify process.\n", __func__);
248 signal_vt_rel(curvw);
254 vt_window_postswitch(struct vt_window *vw)
261 /* vt_late_window_switch will done VT switching for regular case. */
263 vt_late_window_switch(struct vt_window *vw)
267 callout_stop(&vw->vw_proc_dead_timer);
269 ret = vt_window_switch(vw);
273 /* Notify owner process about terminal availability. */
274 if (vw->vw_smode.mode == VT_PROCESS) {
275 ret = vt_window_postswitch(vw);
282 vt_proc_window_switch(struct vt_window *vw)
284 struct vt_window *curvw;
285 struct vt_device *vd;
289 curvw = vd->vd_curwindow;
291 if (curvw->vw_flags & VWF_VTYLOCK)
294 /* Ask current process permitions to switch away. */
295 if (curvw->vw_smode.mode == VT_PROCESS) {
296 DPRINTF(30, "%s: VT_PROCESS ", __func__);
297 if (vt_proc_alive(curvw) == FALSE) {
298 DPRINTF(30, "Dead. Cleaning.");
301 DPRINTF(30, "%s: Signaling process.\n", __func__);
302 /* Alive, try to ask him. */
303 ret = vt_window_preswitch(vw, curvw);
304 /* Wait for process answer or timeout. */
310 ret = vt_late_window_switch(vw);
314 /* Switch window ignoring process locking. */
316 vt_window_switch(struct vt_window *vw)
318 struct vt_device *vd = vw->vw_device;
319 struct vt_window *curvw = vd->vd_curwindow;
328 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
333 vd->vd_curwindow = vw;
334 vd->vd_flags |= VDF_INVALID;
335 cv_broadcast(&vd->vd_winswitch);
338 if (vd->vd_driver->vd_postswitch)
339 vd->vd_driver->vd_postswitch(vd);
341 /* Restore per-window keyboard mode. */
343 kbd = kbd_get_keyboard(vd->vd_keyboard);
345 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
348 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
354 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
357 size->tp_row = vd->vd_height;
358 size->tp_col = vd->vd_width;
360 size->tp_row /= vf->vf_height;
361 size->tp_col /= vf->vf_width;
366 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
369 size->ws_row = size->ws_ypixel = vd->vd_height;
370 size->ws_col = size->ws_xpixel = vd->vd_width;
372 size->ws_row /= vf->vf_height;
373 size->ws_col /= vf->vf_width;
378 vt_scroll(struct vt_window *vw, int offset, int whence)
383 if ((vw->vw_flags & VWF_SCROLL) == 0)
386 vt_termsize(vw->vw_device, vw->vw_font, &size);
388 diff = vthistory_seek(&vw->vw_buf, offset, whence);
390 * Offset changed, please update Nth lines on sceen.
391 * +N - Nth lines at top;
392 * -N - Nth lines at bottom.
395 if (diff < -size.tp_row || diff > size.tp_row) {
396 vw->vw_device->vd_flags |= VDF_INVALID;
399 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
403 vt_machine_kbdevent(int c)
408 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
411 /* XXX: Make this configurable! */
415 shutdown_nice(RB_HALT);
418 shutdown_nice(RB_HALT|RB_POWEROFF);
426 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
428 struct vt_device *vd;
432 /* Only special keys handled in ScrollLock mode */
433 if ((c & SPCLKEY) == 0)
439 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
440 vw = vd->vd_windows[c - F_SCR];
442 vt_proc_window_switch(vw);
450 /* Turn scrolling off. */
451 vt_scroll(vw, 0, VHS_END);
452 VTBUF_SLCK_DISABLE(&vw->vw_buf);
453 vw->vw_flags &= ~VWF_SCROLL;
456 case FKEY | F(49): /* Home key. */
457 vt_scroll(vw, 0, VHS_SET);
459 case FKEY | F(50): /* Arrow up. */
460 vt_scroll(vw, -1, VHS_CUR);
462 case FKEY | F(51): /* Page up. */
463 vt_termsize(vd, vw->vw_font, &size);
464 vt_scroll(vw, -size.tp_row, VHS_CUR);
466 case FKEY | F(57): /* End key. */
467 vt_scroll(vw, 0, VHS_END);
469 case FKEY | F(58): /* Arrow down. */
470 vt_scroll(vw, 1, VHS_CUR);
472 case FKEY | F(59): /* Page down. */
473 vt_termsize(vd, vw->vw_font, &size);
474 vt_scroll(vw, size.tp_row, VHS_CUR);
483 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
485 struct vt_window *vw = vd->vd_curwindow;
488 #if VT_ALT_TO_ESC_HACK
490 switch (c & ~RELKEY) {
491 case (SPCLKEY | RALT):
492 if (vt_enable_altgr != 0)
494 case (SPCLKEY | LALT):
495 vd->vd_kbstate &= ~ALKED;
497 /* Other keys ignored for RELKEY event. */
500 switch (c & ~RELKEY) {
501 case (SPCLKEY | RALT):
502 if (vt_enable_altgr != 0)
504 case (SPCLKEY | LALT):
505 vd->vd_kbstate |= ALKED;
510 /* Other keys ignored for RELKEY event. */
514 if (vt_machine_kbdevent(c))
517 if (vw->vw_flags & VWF_SCROLL) {
518 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
519 /* Scroll mode keys handled, nothing to do more. */
526 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
527 vw = vd->vd_windows[c - F_SCR];
529 vt_proc_window_switch(vw);
536 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
539 /* Turn scrolling on. */
540 vw->vw_flags |= VWF_SCROLL;
541 VTBUF_SLCK_ENABLE(&vw->vw_buf);
543 /* Turn scrolling off. */
544 vw->vw_flags &= ~VWF_SCROLL;
545 VTBUF_SLCK_DISABLE(&vw->vw_buf);
546 vt_scroll(vw, 0, VHS_END);
551 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
552 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
553 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
554 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
555 /* F1 through F12 keys. */
556 terminal_input_special(vw->vw_terminal,
557 TKEY_F1 + c - (FKEY | F(1)));
559 case FKEY | F(49): /* Home key. */
560 terminal_input_special(vw->vw_terminal, TKEY_HOME);
562 case FKEY | F(50): /* Arrow up. */
563 terminal_input_special(vw->vw_terminal, TKEY_UP);
565 case FKEY | F(51): /* Page up. */
566 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
568 case FKEY | F(53): /* Arrow left. */
569 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
571 case FKEY | F(55): /* Arrow right. */
572 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
574 case FKEY | F(57): /* End key. */
575 terminal_input_special(vw->vw_terminal, TKEY_END);
577 case FKEY | F(58): /* Arrow down. */
578 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
580 case FKEY | F(59): /* Page down. */
581 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
583 case FKEY | F(60): /* Insert key. */
584 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
586 case FKEY | F(61): /* Delete key. */
587 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
590 } else if (KEYFLAGS(c) == 0) {
591 /* Don't do UTF-8 conversion when doing raw mode. */
592 if (vw->vw_kbdmode == K_XLATE) {
593 #if VT_ALT_TO_ESC_HACK
594 if (vd->vd_kbstate & ALKED) {
596 * Prepend ESC sequence if one of ALT keys down.
598 terminal_input_char(vw->vw_terminal, 0x1b);
602 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
604 terminal_input_raw(vw->vw_terminal, c);
610 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
612 struct vt_device *vd = arg;
618 case KBDIO_UNLOADING:
620 vd->vd_keyboard = -1;
621 kbd_release(kbd, (void *)vd);
628 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
629 vt_processkey(kbd, vd, c);
635 vt_allocate_keyboard(struct vt_device *vd)
641 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
642 vd->vd_keyboard = idx0;
644 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
645 k0 = kbd_get_keyboard(idx0);
647 for (idx = kbd_find_keyboard2("*", -1, 0);
649 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
650 k = kbd_get_keyboard(idx);
652 if (idx == idx0 || KBD_IS_BUSY(k))
655 bzero(&ki, sizeof(ki));
656 strcpy(ki.kb_name, k->kb_name);
657 ki.kb_unit = k->kb_unit;
659 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
662 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
663 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
665 DPRINTF(10, "%s: No keyboard found.\n", __func__);
669 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
675 vtterm_bell(struct terminal *tm)
677 struct vt_window *vw = tm->tm_softc;
678 struct vt_device *vd = vw->vw_device;
680 if (vd->vd_flags & VDF_QUIET_BELL)
683 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
687 vtterm_beep(struct terminal *tm, u_int param)
691 if ((param == 0) || ((param & 0xffff) == 0)) {
696 period = ((param >> 16) & 0xffff) * hz / 1000;
697 freq = 1193182 / (param & 0xffff);
699 sysbeep(freq, period);
703 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
705 struct vt_window *vw = tm->tm_softc;
707 vtbuf_cursor_position(&vw->vw_buf, p);
711 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
713 struct vt_window *vw = tm->tm_softc;
715 vtbuf_putchar(&vw->vw_buf, p, c);
719 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
721 struct vt_window *vw = tm->tm_softc;
723 vtbuf_fill_locked(&vw->vw_buf, r, c);
727 vtterm_copy(struct terminal *tm, const term_rect_t *r,
730 struct vt_window *vw = tm->tm_softc;
732 vtbuf_copy(&vw->vw_buf, r, p);
736 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
738 struct vt_window *vw = tm->tm_softc;
742 vtbuf_cursor_visibility(&vw->vw_buf, arg);
745 vw->vw_mouse_level = arg;
751 vt_determine_colors(term_char_t c, int cursor,
752 term_color_t *fg, term_color_t *bg)
759 *fg = TCHAR_FGCOLOR(c);
760 if (TCHAR_FORMAT(c) & TF_BOLD)
761 *fg = TCOLOR_LIGHT(*fg);
762 *bg = TCHAR_BGCOLOR(c);
764 if (TCHAR_FORMAT(c) & TF_REVERSE)
777 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
778 int iscursor, unsigned int row, unsigned int col)
782 vt_determine_colors(c, iscursor, &fg, &bg);
788 src = vtfont_lookup(vf, c);
791 * Align the terminal to the centre of the screen.
792 * Fonts may not always be able to fill the entire
795 top = row * vf->vf_height + vd->vd_offset.tp_row;
796 left = col * vf->vf_width + vd->vd_offset.tp_col;
798 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
799 vf->vf_width, vf->vf_height, fg, bg);
801 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
807 vt_flush(struct vt_device *vd)
809 struct vt_window *vw;
811 struct vt_bufmask tmask;
812 unsigned int row, col;
816 #ifndef SC_NO_CUTPASTE
817 struct mouse_cursor *m;
821 vw = vd->vd_curwindow;
825 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
828 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
831 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
832 vt_termsize(vd, vf, &size);
834 /* Force a full redraw when the screen contents are invalid. */
835 if (vd->vd_flags & VDF_INVALID) {
836 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
838 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
840 vd->vd_flags &= ~VDF_INVALID;
843 #ifndef SC_NO_CUTPASTE
844 if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) {
845 /* Mark last mouse position as dirty to erase. */
846 vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx,
851 for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
852 if (!VTBUF_DIRTYROW(&tmask, row))
854 r = VTBUF_GET_ROW(&vw->vw_buf, row);
855 for (col = tarea.tr_begin.tp_col;
856 col < tarea.tr_end.tp_col; col++) {
857 if (!VTBUF_DIRTYCOL(&tmask, col))
860 vt_bitblt_char(vd, vf, r[col],
861 VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
865 #ifndef SC_NO_CUTPASTE
866 /* Mouse disabled. */
867 if (vw->vw_flags & VWF_MOUSE_HIDE)
870 /* No mouse for DDB. */
871 if (kdb_active || panicstr != NULL)
874 if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
876 m = &vt_default_mouse_pointer;
877 bpl = (m->w + 7) >> 3; /* Bytes per source line. */
881 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
882 w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
883 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
884 h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
886 vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl,
887 vd->vd_offset.tp_row + vd->vd_my,
888 vd->vd_offset.tp_col + vd->vd_mx,
889 w, h, TC_WHITE, TC_BLACK);
890 /* Save point of last mouse cursor to erase it later. */
891 vd->vd_mdirtyx = vd->vd_mx / vf->vf_width;
892 vd->vd_mdirtyy = vd->vd_my / vf->vf_height;
900 struct vt_device *vd;
903 /* Update screen if required. */
906 /* Schedule for next update. */
907 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
911 vtterm_done(struct terminal *tm)
913 struct vt_window *vw = tm->tm_softc;
914 struct vt_device *vd = vw->vw_device;
916 if (kdb_active || panicstr != NULL) {
917 /* Switch to the debugger. */
918 if (vd->vd_curwindow != vw) {
919 vd->vd_curwindow = vw;
920 vd->vd_flags |= VDF_INVALID;
921 if (vd->vd_driver->vd_postswitch)
922 vd->vd_driver->vd_postswitch(vd);
924 vd->vd_flags &= ~VDF_SPLASH;
926 } else if (!(vd->vd_flags & VDF_ASYNC)) {
933 vtterm_splash(struct vt_device *vd)
937 /* Display a nice boot splash. */
938 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
940 top = (vd->vd_height - vt_logo_height) / 2;
941 left = (vd->vd_width - vt_logo_width) / 2;
942 switch (vt_logo_depth) {
944 /* XXX: Unhardcode colors! */
945 vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
946 top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
948 vd->vd_flags |= VDF_SPLASH;
955 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
957 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
958 struct vt_window *vw = tm->tm_softc;
959 struct vt_device *vd = vw->vw_device;
964 if (!vty_enabled(VTY_VT))
967 if (vd->vd_flags & VDF_INITIALIZED)
968 /* Initialization already done. */
971 SET_FOREACH(vtdlist, vt_drv_set) {
973 if (vtd->vd_probe == NULL)
975 if (vtd->vd_probe(vd) == CN_DEAD)
977 if ((vtdbest == NULL) ||
978 (vtd->vd_priority > vtdbest->vd_priority))
981 if (vtdbest == NULL) {
982 cp->cn_pri = CN_DEAD;
983 vd->vd_flags |= VDF_DEAD;
985 vd->vd_driver = vtdbest;
986 cp->cn_pri = vd->vd_driver->vd_init(vd);
989 /* Check if driver's vt_init return CN_DEAD. */
990 if (cp->cn_pri == CN_DEAD) {
991 vd->vd_flags |= VDF_DEAD;
994 /* Initialize any early-boot keyboard drivers */
995 kbd_configure(KB_CONF_PROBE_ONLY);
997 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
998 vd->vd_windows[VT_CONSWINDOW] = vw;
999 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1001 /* Attach default font if not in TEXTMODE. */
1002 if (!(vd->vd_flags & VDF_TEXTMODE))
1003 vw->vw_font = vtfont_ref(&vt_font_default);
1005 vtbuf_init_early(&vw->vw_buf);
1006 vt_winsize(vd, vw->vw_font, &wsz);
1007 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1009 attr.ta_format = TCHAR_FORMAT(c);
1010 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1011 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1012 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1014 if (vtdbest != NULL) {
1018 vd->vd_flags |= VDF_INITIALIZED;
1023 vtterm_cngetc(struct terminal *tm)
1025 struct vt_window *vw = tm->tm_softc;
1026 struct vt_device *vd = vw->vw_device;
1031 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1032 return (*vw->vw_kbdsq++);
1035 /* Make sure the splash screen is not there. */
1036 if (vd->vd_flags & VDF_SPLASH) {
1038 vd->vd_flags &= ~VDF_SPLASH;
1039 /* Mark screen as invalid to force update */
1040 vd->vd_flags |= VDF_INVALID;
1044 /* Stripped down keyboard handler. */
1045 kbd = kbd_get_keyboard(vd->vd_keyboard);
1049 /* Force keyboard input mode to K_XLATE */
1051 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1053 /* Switch the keyboard to polling to make it work here. */
1054 kbdd_poll(kbd, TRUE);
1055 c = kbdd_read_char(kbd, 0);
1056 kbdd_poll(kbd, FALSE);
1060 if (vw->vw_flags & VWF_SCROLL) {
1061 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1066 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1070 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1071 if (state & SLKED) {
1072 /* Turn scrolling on. */
1073 vw->vw_flags |= VWF_SCROLL;
1074 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1076 /* Turn scrolling off. */
1077 vt_scroll(vw, 0, VHS_END);
1078 vw->vw_flags &= ~VWF_SCROLL;
1079 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1082 /* XXX: KDB can handle history. */
1083 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1084 vw->vw_kbdsq = "\x1b[A";
1086 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1087 vw->vw_kbdsq = "\x1b[B";
1089 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1090 vw->vw_kbdsq = "\x1b[C";
1092 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1093 vw->vw_kbdsq = "\x1b[D";
1097 /* Force refresh to make scrollback work. */
1099 } else if (KEYFLAGS(c) == 0) {
1100 return (KEYCHAR(c));
1103 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1104 return (*vw->vw_kbdsq++);
1110 vtterm_opened(struct terminal *tm, int opened)
1112 struct vt_window *vw = tm->tm_softc;
1113 struct vt_device *vd = vw->vw_device;
1116 vd->vd_flags &= ~VDF_SPLASH;
1118 vw->vw_flags |= VWF_OPENED;
1120 vw->vw_flags &= ~VWF_OPENED;
1121 /* TODO: finish ACQ/REL */
1127 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1129 struct vt_device *vd = vw->vw_device;
1130 struct terminal *tm = vw->vw_terminal;
1137 * Changing fonts is a little tricky. We must prevent
1138 * simultaneous access to the device, so we must stop
1139 * the display timer and the terminal from accessing.
1140 * We need to switch fonts and grow our screen buffer.
1142 * XXX: Right now the code uses terminal_mute() to
1143 * prevent data from reaching the console driver while
1144 * resizing the screen buffer. This isn't elegant...
1148 if (vw->vw_flags & VWF_BUSY) {
1149 /* Another process is changing the font. */
1153 if (vw->vw_font == NULL) {
1154 /* Our device doesn't need fonts. */
1158 vw->vw_flags |= VWF_BUSY;
1161 vt_termsize(vd, vf, &size);
1162 vt_winsize(vd, vf, &wsz);
1163 /* Save offset to font aligned area. */
1164 vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1165 vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1167 /* Grow the screen buffer and terminal. */
1168 terminal_mute(tm, 1);
1169 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1170 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1171 terminal_mute(tm, 0);
1173 /* Actually apply the font to the current window. */
1175 vtfont_unref(vw->vw_font);
1176 vw->vw_font = vtfont_ref(vf);
1178 /* Force a full redraw the next timer tick. */
1179 if (vd->vd_curwindow == vw)
1180 vd->vd_flags |= VDF_INVALID;
1181 vw->vw_flags &= ~VWF_BUSY;
1187 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1189 struct vt_device *vd = vw->vw_device;
1190 int x, y, off_x, off_y;
1192 if (vd->vd_driver->vd_drawrect == NULL)
1195 x = vd->vd_width - 1;
1196 y = vd->vd_height - 1;
1197 off_x = vd->vd_offset.tp_col;
1198 off_y = vd->vd_offset.tp_row;
1202 vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c);
1205 vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y,
1207 /* Right bar. May be 1 pixel wider than necessary due to rounding. */
1208 vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c);
1209 /* Bottom bar. May be 1 mixel taller than necessary due to rounding. */
1210 vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c);
1216 vt_proc_alive(struct vt_window *vw)
1220 if (vw->vw_smode.mode != VT_PROCESS)
1224 if ((p = pfind(vw->vw_pid)) != NULL)
1226 if (vw->vw_proc == p)
1229 vw->vw_smode.mode = VT_AUTO;
1230 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1237 signal_vt_rel(struct vt_window *vw)
1240 if (vw->vw_smode.mode != VT_PROCESS)
1242 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1247 vw->vw_flags |= VWF_SWWAIT_REL;
1248 PROC_LOCK(vw->vw_proc);
1249 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1250 PROC_UNLOCK(vw->vw_proc);
1251 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1256 signal_vt_acq(struct vt_window *vw)
1259 if (vw->vw_smode.mode != VT_PROCESS)
1261 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1262 cnavailable(vw->vw_terminal->consdev, FALSE);
1263 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1268 vw->vw_flags |= VWF_SWWAIT_ACQ;
1269 PROC_LOCK(vw->vw_proc);
1270 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1271 PROC_UNLOCK(vw->vw_proc);
1272 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1277 finish_vt_rel(struct vt_window *vw, int release, int *s)
1280 if (vw->vw_flags & VWF_SWWAIT_REL) {
1281 vw->vw_flags &= ~VWF_SWWAIT_REL;
1283 callout_drain(&vw->vw_proc_dead_timer);
1284 vt_late_window_switch(vw->vw_switch_to);
1292 finish_vt_acq(struct vt_window *vw)
1295 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1296 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1302 #ifndef SC_NO_CUTPASTE
1304 vt_mouse_terminput_button(struct vt_device *vd, int button)
1306 struct vt_window *vw;
1308 char mouseb[6] = "\x1B[M";
1311 vw = vd->vd_curwindow;
1314 /* Translate to char position. */
1315 x = vd->vd_mx / vf->vf_width;
1316 y = vd->vd_my / vf->vf_height;
1317 /* Avoid overflow. */
1318 x = MIN(x, 255 - '!');
1319 y = MIN(y, 255 - '!');
1321 mouseb[3] = ' ' + button;
1322 mouseb[4] = '!' + x;
1323 mouseb[5] = '!' + y;
1325 for (i = 0; i < sizeof(mouseb); i++ )
1326 terminal_input_char(vw->vw_terminal, mouseb[i]);
1330 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1335 case MOUSE_BUTTON_EVENT:
1337 /* Mouse button pressed. */
1338 if (event & MOUSE_BUTTON1DOWN)
1339 vt_mouse_terminput_button(vd, 0);
1340 if (event & MOUSE_BUTTON2DOWN)
1341 vt_mouse_terminput_button(vd, 1);
1342 if (event & MOUSE_BUTTON3DOWN)
1343 vt_mouse_terminput_button(vd, 2);
1345 /* Mouse button released. */
1346 vt_mouse_terminput_button(vd, 3);
1350 case MOUSE_MOTION_EVENT:
1351 if (mouse->u.data.z < 0) {
1353 sc_mouse_input_button(vd, 64);
1354 } else if (mouse->u.data.z > 0) {
1356 sc_mouse_input_button(vd, 65);
1364 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1366 struct vt_device *vd;
1367 struct vt_window *vw;
1374 vw = vd->vd_curwindow;
1378 if (vw->vw_flags & VWF_MOUSE_HIDE)
1379 return; /* Mouse disabled. */
1381 if (vf == NULL) /* Text mode. */
1385 * TODO: add flag about pointer position changed, to not redraw chars
1386 * under mouse pointer when nothing changed.
1389 if (vw->vw_mouse_level > 0)
1390 vt_mouse_terminput(vd, type, x, y, event, cnt);
1394 case MOUSE_MOTION_EVENT:
1399 vt_termsize(vd, vf, &size);
1404 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1405 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1409 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1410 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1411 vd->vd_mx / vf->vf_width,
1412 vd->vd_my / vf->vf_height) == 1)) {
1415 * We have something marked to copy, so update pointer
1416 * to window with selection.
1418 vd->vd_markedwin = vw;
1421 case MOUSE_BUTTON_EVENT:
1429 case MOUSE_BUTTON1DOWN:
1432 mark = VTB_MARK_END;
1434 case 1: /* single click: start cut operation */
1435 mark = VTB_MARK_START;
1437 case 2: /* double click: cut a word */
1438 mark = VTB_MARK_WORD;
1440 case 3: /* triple click: cut a line */
1441 mark = VTB_MARK_ROW;
1445 case VT_MOUSE_PASTEBUTTON:
1450 if (vd->vd_markedwin == NULL)
1452 /* Get current selecton size in bytes. */
1453 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1457 buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1458 /* Request cupy/paste buffer data, no more than `len' */
1459 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1462 len /= sizeof(term_char_t);
1463 for (i = 0; i < len; i++ ) {
1466 terminal_input_char(vw->vw_terminal, buf[i]);
1469 /* Done, so cleanup. */
1474 case VT_MOUSE_EXTENDBUTTON:
1477 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1478 mark = VTB_MARK_EXTEND;
1483 mark = VTB_MARK_EXTEND;
1491 /* Save buttons state. */
1493 vd->vd_mstate |= event;
1495 vd->vd_mstate &= ~event;
1497 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1498 vd->vd_my / vf->vf_height) == 1) {
1500 * We have something marked to copy, so update pointer to
1501 * window with selection.
1503 vd->vd_markedwin = vw;
1508 vt_mouse_state(int show)
1510 struct vt_device *vd;
1511 struct vt_window *vw;
1514 vw = vd->vd_curwindow;
1518 vw->vw_flags |= VWF_MOUSE_HIDE;
1521 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1528 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1529 int nprot, vm_memattr_t *memattr)
1531 struct vt_window *vw = tm->tm_softc;
1532 struct vt_device *vd = vw->vw_device;
1534 if (vd->vd_driver->vd_fb_mmap)
1535 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1542 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1545 struct vt_window *vw = tm->tm_softc;
1546 struct vt_device *vd = vw->vw_device;
1549 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1550 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1561 cmd = VT_WAITACTIVE;
1587 ival = IOCPARM_IVAL(data);
1588 data = (caddr_t)&ival;
1593 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1594 if (*(int *)data & ~0x7f)
1598 case GIO_DEADKEYMAP:
1599 case PIO_DEADKEYMAP:
1604 case KDSKBSTATE: /* set keyboard state (locks) */
1605 case KDGKBSTATE: /* get keyboard state (locks) */
1606 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1607 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1608 case KDSETLED: /* set keyboard LED status */
1609 case KDGETLED: /* get keyboard LED status */
1610 case KBADDKBD: /* add/remove keyboard to/from mux */
1615 kbd = kbd_get_keyboard(vd->vd_keyboard);
1617 error = kbdd_ioctl(kbd, cmd, data);
1619 if (error == ENOIOCTL) {
1620 if (cmd == KDGKBTYPE) {
1621 /* always return something? XXX */
1633 kbd = kbd_get_keyboard(vd->vd_keyboard);
1635 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1638 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1639 *(int *)data = mode;
1645 mode = *(int *)data;
1650 vw->vw_kbdmode = mode;
1651 if (vw == vd->vd_curwindow) {
1656 kbd = kbd_get_keyboard(vd->vd_keyboard);
1658 error = kbdd_ioctl(kbd, KDSKBMODE,
1669 case FBIO_GETWINORG: /* get frame buffer window origin */
1670 case FBIO_GETDISPSTART: /* get display start address */
1671 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1672 case FBIO_BLANK: /* blank display */
1673 if (vd->vd_driver->vd_fb_ioctl)
1674 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1676 case CONS_BLANKTIME:
1681 *(int *)data = M_CG640x480;
1683 case CONS_BELLTYPE: /* set bell type sound */
1684 if ((*(int *)data) & CONS_QUIET_BELL)
1685 vd->vd_flags |= VDF_QUIET_BELL;
1687 vd->vd_flags &= ~VDF_QUIET_BELL;
1689 case CONS_GETINFO: {
1690 vid_info_t *vi = (vid_info_t *)data;
1692 vi->m_num = vd->vd_curwindow->vw_number + 1;
1693 /* XXX: other fields! */
1697 *(int *)data = 0x200;
1702 case CONS_MOUSECTL: {
1703 mouse_info_t *mouse = (mouse_info_t*)data;
1706 * This has no effect on vt(4). We don't draw any mouse
1707 * cursor. Just ignore MOUSE_HIDE and MOUSE_SHOW to
1708 * prevent excessive errors. All the other commands
1709 * should not be applied to individual TTYs, but only to
1712 switch (mouse->operation) {
1714 vd->vd_flags &= ~VDF_MOUSECURSOR;
1717 vd->vd_mx = vd->vd_width / 2;
1718 vd->vd_my = vd->vd_height / 2;
1719 vd->vd_flags |= VDF_MOUSECURSOR;
1728 error = vtfont_load((void *)data, &vf);
1732 error = vt_change_font(vw, vf);
1734 /* XXX: replace 0 with current bg color. */
1735 vt_set_border(vw, vf, 0);
1741 scrmap_t *sm = (scrmap_t *)data;
1744 /* We don't have screen maps, so return a handcrafted one. */
1745 for (i = 0; i < 256; i++)
1752 case KDENABIO: /* allow io operations */
1753 error = priv_check(td, PRIV_IO);
1756 error = securelevel_gt(td->td_ucred, 0);
1759 #if defined(__i386__)
1760 td->td_frame->tf_eflags |= PSL_IOPL;
1761 #elif defined(__amd64__)
1762 td->td_frame->tf_rflags |= PSL_IOPL;
1765 case KDDISABIO: /* disallow io operations (default) */
1766 #if defined(__i386__)
1767 td->td_frame->tf_eflags &= ~PSL_IOPL;
1768 #elif defined(__amd64__)
1769 td->td_frame->tf_rflags &= ~PSL_IOPL;
1772 case KDMKTONE: /* sound the bell */
1773 vtterm_beep(tm, *(u_int *)data);
1775 case KIOCSOUND: /* make tone (*data) hz */
1778 case CONS_SETKBD: /* set the new keyboard */
1781 if (vd->vd_keyboard != *(int *)data) {
1782 kbd = kbd_get_keyboard(*(int *)data);
1787 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1788 (void *)vd, vt_kbdevent, vd);
1790 if (vd->vd_keyboard != -1) {
1791 kbd_release(kbd, (void *)vd);
1793 kbd = kbd_get_keyboard(i);
1794 vd->vd_keyboard = i;
1796 (void)kbdd_ioctl(kbd, KDSKBMODE,
1797 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1799 error = EPERM; /* XXX */
1804 case CONS_RELKBD: /* release the current keyboard */
1807 if (vd->vd_keyboard != -1) {
1808 kbd = kbd_get_keyboard(vd->vd_keyboard);
1813 error = kbd_release(kbd, (void *)vd);
1815 vd->vd_keyboard = -1;
1822 win = *(int *)data - 1;
1823 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1825 if ((win > VT_MAXWINDOWS) || (win < 0))
1827 return (vt_proc_window_switch(vd->vd_windows[win]));
1830 *(int *)data = vd->vd_curwindow->vw_number + 1;
1833 *(int *)data = vw->vw_number + 1;
1836 /* TODO: Check current state, switching can be in progress. */
1837 if ((*(int *)data) == 0x01)
1838 vw->vw_flags |= VWF_VTYLOCK;
1839 else if ((*(int *)data) == 0x02)
1840 vw->vw_flags &= ~VWF_VTYLOCK;
1846 for (i = 0; i < VT_MAXWINDOWS; i++) {
1847 vw = vd->vd_windows[i];
1850 if (!(vw->vw_flags & VWF_OPENED)) {
1851 *(int *)data = vw->vw_number + 1;
1861 i = *(unsigned int *)data;
1862 if (i > VT_MAXWINDOWS)
1865 vw = vd->vd_windows[i - 1];
1868 while (vd->vd_curwindow != vw && error == 0)
1869 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1872 case VT_SETMODE: { /* set screen switcher mode */
1873 struct vt_mode *mode;
1876 mode = (struct vt_mode *)data;
1877 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1878 if (vw->vw_smode.mode == VT_PROCESS) {
1879 p1 = pfind(vw->vw_pid);
1880 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1883 DPRINTF(5, "error EPERM\n");
1889 if (mode->mode == VT_AUTO) {
1890 vw->vw_smode.mode = VT_AUTO;
1893 DPRINTF(5, "VT_AUTO, ");
1894 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1895 cnavailable(vw->vw_terminal->consdev, TRUE);
1896 /* were we in the middle of the vty switching process? */
1897 if (finish_vt_rel(vw, TRUE, &s) == 0)
1898 DPRINTF(5, "reset WAIT_REL, ");
1899 if (finish_vt_acq(vw) == 0)
1900 DPRINTF(5, "reset WAIT_ACQ, ");
1902 } else if (mode->mode == VT_PROCESS) {
1903 if (!ISSIGVALID(mode->relsig) ||
1904 !ISSIGVALID(mode->acqsig) ||
1905 !ISSIGVALID(mode->frsig)) {
1906 DPRINTF(5, "error EINVAL\n");
1909 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1910 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1911 vw->vw_proc = td->td_proc;
1912 vw->vw_pid = vw->vw_proc->p_pid;
1913 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1914 cnavailable(vw->vw_terminal->consdev, FALSE);
1916 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
1923 case VT_GETMODE: /* get screen switcher mode */
1924 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
1927 case VT_RELDISP: /* screen switcher ioctl */
1929 * This must be the current vty which is in the VT_PROCESS
1932 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
1936 /* ...and this process is controlling it. */
1937 if (vw->vw_proc != td->td_proc) {
1941 switch(*(int *)data) {
1942 case VT_FALSE: /* user refuses to release screen, abort */
1943 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
1944 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
1945 SC_DRIVER_NAME, VT_UNIT(vw));
1947 case VT_TRUE: /* user has released screen, go on */
1948 /* finish_vt_rel(..., TRUE, ...) should not be locked */
1949 if (vw->vw_flags & VWF_SWWAIT_REL) {
1950 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
1951 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
1952 SC_DRIVER_NAME, VT_UNIT(vw));
1957 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1958 if ((error = finish_vt_acq(vw)) == 0)
1959 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
1960 SC_DRIVER_NAME, VT_UNIT(vw));
1971 static struct vt_window *
1972 vt_allocate_window(struct vt_device *vd, unsigned int window)
1974 struct vt_window *vw;
1975 struct terminal *tm;
1979 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
1981 vw->vw_number = window;
1982 vw->vw_kbdmode = K_XLATE;
1984 if (!(vd->vd_flags & VDF_TEXTMODE))
1985 vw->vw_font = vtfont_ref(&vt_font_default);
1987 vt_termsize(vd, vw->vw_font, &size);
1988 vt_winsize(vd, vw->vw_font, &wsz);
1989 vtbuf_init(&vw->vw_buf, &size);
1991 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
1992 terminal_set_winsize(tm, &wsz);
1993 vd->vd_windows[window] = vw;
1994 callout_init(&vw->vw_proc_dead_timer, 0);
2000 vt_upgrade(struct vt_device *vd)
2002 struct vt_window *vw;
2005 if (!vty_enabled(VTY_VT))
2008 for (i = 0; i < VT_MAXWINDOWS; i++) {
2009 vw = vd->vd_windows[i];
2012 vw = vt_allocate_window(vd, i);
2014 if (!(vw->vw_flags & VWF_READY)) {
2015 callout_init(&vw->vw_proc_dead_timer, 0);
2016 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2017 vw->vw_flags |= VWF_READY;
2018 if (vw->vw_flags & VWF_CONSOLE) {
2019 /* For existing console window. */
2020 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2021 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2027 if (vd->vd_curwindow == NULL)
2028 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2030 if (!(vd->vd_flags & VDF_ASYNC)) {
2031 /* Attach keyboard. */
2032 vt_allocate_keyboard(vd);
2033 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
2035 /* Init 25 Hz timer. */
2036 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2038 /* Start timer when everything ready. */
2039 vd->vd_flags |= VDF_ASYNC;
2040 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2045 /* Refill settings with new sizes. */
2050 vt_resize(struct vt_device *vd)
2052 struct vt_window *vw;
2055 for (i = 0; i < VT_MAXWINDOWS; i++) {
2056 vw = vd->vd_windows[i];
2058 /* Assign default font to window, if not textmode. */
2059 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2060 vw->vw_font = vtfont_ref(&vt_font_default);
2062 /* Resize terminal windows */
2063 vt_change_font(vw, vw->vw_font);
2068 vt_allocate(struct vt_driver *drv, void *softc)
2070 struct vt_device *vd;
2073 if (!vty_enabled(VTY_VT))
2076 if (main_vd->vd_driver == NULL) {
2077 main_vd->vd_driver = drv;
2078 printf("VT: initialize with new VT driver \"%s\".\n",
2082 * Check if have rights to replace current driver. For example:
2083 * it is bad idea to replace KMS driver with generic VGA one.
2085 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2086 printf("VT: Driver priority %d too low. Current %d\n ",
2087 drv->vd_priority, main_vd->vd_driver->vd_priority);
2090 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2091 main_vd->vd_driver->vd_name, drv->vd_name);
2095 if (drv->vd_maskbitbltchr == NULL)
2096 drv->vd_maskbitbltchr = drv->vd_bitbltchr;
2098 if (vd->vd_flags & VDF_ASYNC) {
2099 /* Stop vt_flush periodic task. */
2100 callout_drain(&vd->vd_timer);
2102 * Mute current terminal until we done. vt_change_font (called
2103 * from vt_resize) will unmute it.
2105 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2109 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2112 vd->vd_flags &= ~VDF_TEXTMODE;
2114 vd->vd_driver = drv;
2115 vd->vd_softc = softc;
2116 vd->vd_driver->vd_init(vd);
2122 if (vd->vd_flags & VDF_SPLASH)
2126 if (vd->vd_flags & VDF_ASYNC) {
2127 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2128 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
2131 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2133 /* Update console window sizes to actual. */
2134 vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz);
2135 terminal_set_winsize_blank(vd->vd_windows[VT_CONSWINDOW]->vw_terminal,
2143 if (vt_suspendswitch == 0)
2145 /* Save current window. */
2146 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2147 /* Ask holding process to free window and switch to console window */
2148 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2155 if (vt_suspendswitch == 0)
2157 /* Switch back to saved window */
2158 if (main_vd->vd_savedwindow != NULL)
2159 vt_proc_window_switch(main_vd->vd_savedwindow);
2160 main_vd->vd_savedwindow = NULL;