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 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
115 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
116 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
117 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
118 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
120 static struct vt_device vt_consdev;
121 static unsigned int vt_unit = 0;
122 static MALLOC_DEFINE(M_VT, "vt", "vt device");
123 struct vt_device *main_vd = &vt_consdev;
126 extern unsigned int vt_logo_width;
127 extern unsigned int vt_logo_height;
128 extern unsigned int vt_logo_depth;
129 extern unsigned char vt_logo_image[];
132 extern struct vt_font vt_font_default;
133 #ifndef SC_NO_CUTPASTE
134 extern struct mouse_cursor vt_default_mouse_pointer;
137 static int signal_vt_rel(struct vt_window *);
138 static int signal_vt_acq(struct vt_window *);
139 static int finish_vt_rel(struct vt_window *, int, int *);
140 static int finish_vt_acq(struct vt_window *);
141 static int vt_window_switch(struct vt_window *);
142 static int vt_late_window_switch(struct vt_window *);
143 static int vt_proc_alive(struct vt_window *);
144 static void vt_resize(struct vt_device *);
145 static void vt_update_static(void *);
147 SET_DECLARE(vt_drv_set, struct vt_driver);
149 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
150 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
152 static struct terminal vt_consterm;
153 static struct vt_window vt_conswindow;
154 static struct vt_device vt_consdev = {
157 .vd_flags = VDF_INVALID,
158 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
159 .vd_curwindow = &vt_conswindow,
160 .vd_markedwin = NULL,
163 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
164 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
165 static struct vt_window vt_conswindow = {
166 .vw_number = VT_CONSWINDOW,
167 .vw_flags = VWF_CONSOLE,
169 .vb_buffer = vt_constextbuf,
170 .vb_rows = vt_constextbufrows,
171 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
174 .vb_flags = VBF_STATIC,
175 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
176 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
182 .vw_device = &vt_consdev,
183 .vw_terminal = &vt_consterm,
184 .vw_kbdmode = K_XLATE,
186 static struct terminal vt_consterm = {
187 .tm_class = &vt_termclass,
188 .tm_softc = &vt_conswindow,
191 static struct consdev vt_consterm_consdev = {
192 .cn_ops = &termcn_cnops,
193 .cn_arg = &vt_consterm,
197 /* Add to set of consoles. */
198 DATA_SET(cons_set, vt_consterm_consdev);
201 * Right after kmem is done to allow early drivers to use locking and allocate
204 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
206 /* Delay until all devices attached, to not waste time. */
207 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
210 /* Initialize locks/mem depended members. */
212 vt_update_static(void *dummy)
215 if (!vty_enabled(VTY_VT))
217 if (main_vd->vd_driver != NULL)
218 printf("VT: running with driver \"%s\".\n",
219 main_vd->vd_driver->vd_name);
221 printf("VT: init without driver.\n");
223 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
224 cv_init(&main_vd->vd_winswitch, "vtwswt");
228 vt_switch_timer(void *arg)
231 vt_late_window_switch((struct vt_window *)arg);
235 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
238 DPRINTF(40, "%s\n", __func__);
239 curvw->vw_switch_to = vw;
240 /* Set timer to allow switch in case when process hang. */
241 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
242 vt_switch_timer, (void *)vw);
243 /* Notify process about vt switch attempt. */
244 DPRINTF(30, "%s: Notify process.\n", __func__);
245 signal_vt_rel(curvw);
251 vt_window_postswitch(struct vt_window *vw)
258 /* vt_late_window_switch will done VT switching for regular case. */
260 vt_late_window_switch(struct vt_window *vw)
264 callout_stop(&vw->vw_proc_dead_timer);
266 ret = vt_window_switch(vw);
270 /* Notify owner process about terminal availability. */
271 if (vw->vw_smode.mode == VT_PROCESS) {
272 ret = vt_window_postswitch(vw);
279 vt_proc_window_switch(struct vt_window *vw)
281 struct vt_window *curvw;
282 struct vt_device *vd;
286 curvw = vd->vd_curwindow;
288 if (curvw->vw_flags & VWF_VTYLOCK)
291 /* Ask current process permitions to switch away. */
292 if (curvw->vw_smode.mode == VT_PROCESS) {
293 DPRINTF(30, "%s: VT_PROCESS ", __func__);
294 if (vt_proc_alive(curvw) == FALSE) {
295 DPRINTF(30, "Dead. Cleaning.");
298 DPRINTF(30, "%s: Signaling process.\n", __func__);
299 /* Alive, try to ask him. */
300 ret = vt_window_preswitch(vw, curvw);
301 /* Wait for process answer or timeout. */
307 ret = vt_late_window_switch(vw);
311 /* Switch window ignoring process locking. */
313 vt_window_switch(struct vt_window *vw)
315 struct vt_device *vd = vw->vw_device;
316 struct vt_window *curvw = vd->vd_curwindow;
325 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
330 vd->vd_curwindow = vw;
331 vd->vd_flags |= VDF_INVALID;
332 cv_broadcast(&vd->vd_winswitch);
335 if (vd->vd_driver->vd_postswitch)
336 vd->vd_driver->vd_postswitch(vd);
338 /* Restore per-window keyboard mode. */
340 kbd = kbd_get_keyboard(vd->vd_keyboard);
342 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
345 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
351 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
354 size->tp_row = vd->vd_height;
355 size->tp_col = vd->vd_width;
357 size->tp_row /= vf->vf_height;
358 size->tp_col /= vf->vf_width;
363 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
366 size->ws_row = size->ws_ypixel = vd->vd_height;
367 size->ws_col = size->ws_xpixel = vd->vd_width;
369 size->ws_row /= vf->vf_height;
370 size->ws_col /= vf->vf_width;
375 vt_scroll(struct vt_window *vw, int offset, int whence)
380 if ((vw->vw_flags & VWF_SCROLL) == 0)
383 vt_termsize(vw->vw_device, vw->vw_font, &size);
385 diff = vthistory_seek(&vw->vw_buf, offset, whence);
387 * Offset changed, please update Nth lines on sceen.
388 * +N - Nth lines at top;
389 * -N - Nth lines at bottom.
392 if (diff < -size.tp_row || diff > size.tp_row) {
393 vw->vw_device->vd_flags |= VDF_INVALID;
396 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
400 vt_machine_kbdevent(int c)
405 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
408 /* XXX: Make this configurable! */
412 shutdown_nice(RB_HALT);
415 shutdown_nice(RB_HALT|RB_POWEROFF);
423 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
425 struct vt_device *vd;
429 /* Only special keys handled in ScrollLock mode */
430 if ((c & SPCLKEY) == 0)
436 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
437 vw = vd->vd_windows[c - F_SCR];
439 vt_proc_window_switch(vw);
447 /* Turn scrolling off. */
448 vt_scroll(vw, 0, VHS_END);
449 VTBUF_SLCK_DISABLE(&vw->vw_buf);
450 vw->vw_flags &= ~VWF_SCROLL;
453 case FKEY | F(49): /* Home key. */
454 vt_scroll(vw, 0, VHS_SET);
456 case FKEY | F(50): /* Arrow up. */
457 vt_scroll(vw, -1, VHS_CUR);
459 case FKEY | F(51): /* Page up. */
460 vt_termsize(vd, vw->vw_font, &size);
461 vt_scroll(vw, -size.tp_row, VHS_CUR);
463 case FKEY | F(57): /* End key. */
464 vt_scroll(vw, 0, VHS_END);
466 case FKEY | F(58): /* Arrow down. */
467 vt_scroll(vw, 1, VHS_CUR);
469 case FKEY | F(59): /* Page down. */
470 vt_termsize(vd, vw->vw_font, &size);
471 vt_scroll(vw, size.tp_row, VHS_CUR);
480 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
482 struct vt_window *vw = vd->vd_curwindow;
485 #if VT_ALT_TO_ESC_HACK
487 switch (c & ~RELKEY) {
488 case (SPCLKEY | RALT):
489 if (vt_enable_altgr != 0)
491 case (SPCLKEY | LALT):
492 vd->vd_kbstate &= ~ALKED;
494 /* Other keys ignored for RELKEY event. */
497 switch (c & ~RELKEY) {
498 case (SPCLKEY | RALT):
499 if (vt_enable_altgr != 0)
501 case (SPCLKEY | LALT):
502 vd->vd_kbstate |= ALKED;
507 /* Other keys ignored for RELKEY event. */
511 if (vt_machine_kbdevent(c))
514 if (vw->vw_flags & VWF_SCROLL) {
515 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
516 /* Scroll mode keys handled, nothing to do more. */
523 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
524 vw = vd->vd_windows[c - F_SCR];
526 vt_proc_window_switch(vw);
533 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
536 /* Turn scrolling on. */
537 vw->vw_flags |= VWF_SCROLL;
538 VTBUF_SLCK_ENABLE(&vw->vw_buf);
540 /* Turn scrolling off. */
541 vw->vw_flags &= ~VWF_SCROLL;
542 VTBUF_SLCK_DISABLE(&vw->vw_buf);
543 vt_scroll(vw, 0, VHS_END);
548 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
549 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
550 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
551 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
552 /* F1 through F12 keys. */
553 terminal_input_special(vw->vw_terminal,
554 TKEY_F1 + c - (FKEY | F(1)));
556 case FKEY | F(49): /* Home key. */
557 terminal_input_special(vw->vw_terminal, TKEY_HOME);
559 case FKEY | F(50): /* Arrow up. */
560 terminal_input_special(vw->vw_terminal, TKEY_UP);
562 case FKEY | F(51): /* Page up. */
563 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
565 case FKEY | F(53): /* Arrow left. */
566 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
568 case FKEY | F(55): /* Arrow right. */
569 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
571 case FKEY | F(57): /* End key. */
572 terminal_input_special(vw->vw_terminal, TKEY_END);
574 case FKEY | F(58): /* Arrow down. */
575 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
577 case FKEY | F(59): /* Page down. */
578 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
580 case FKEY | F(60): /* Insert key. */
581 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
583 case FKEY | F(61): /* Delete key. */
584 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
587 } else if (KEYFLAGS(c) == 0) {
588 /* Don't do UTF-8 conversion when doing raw mode. */
589 if (vw->vw_kbdmode == K_XLATE) {
590 #if VT_ALT_TO_ESC_HACK
591 if (vd->vd_kbstate & ALKED) {
593 * Prepend ESC sequence if one of ALT keys down.
595 terminal_input_char(vw->vw_terminal, 0x1b);
599 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
601 terminal_input_raw(vw->vw_terminal, c);
607 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
609 struct vt_device *vd = arg;
615 case KBDIO_UNLOADING:
617 vd->vd_keyboard = -1;
618 kbd_release(kbd, (void *)vd);
625 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
626 vt_processkey(kbd, vd, c);
632 vt_allocate_keyboard(struct vt_device *vd)
638 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
639 vd->vd_keyboard = idx0;
641 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
642 k0 = kbd_get_keyboard(idx0);
644 for (idx = kbd_find_keyboard2("*", -1, 0);
646 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
647 k = kbd_get_keyboard(idx);
649 if (idx == idx0 || KBD_IS_BUSY(k))
652 bzero(&ki, sizeof(ki));
653 strcpy(ki.kb_name, k->kb_name);
654 ki.kb_unit = k->kb_unit;
656 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
659 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
660 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
662 DPRINTF(10, "%s: No keyboard found.\n", __func__);
666 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
672 vtterm_bell(struct terminal *tm)
674 struct vt_window *vw = tm->tm_softc;
675 struct vt_device *vd = vw->vw_device;
677 if (vd->vd_flags & VDF_QUIET_BELL)
680 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
684 vtterm_beep(struct terminal *tm, u_int param)
688 if ((param == 0) || ((param & 0xffff) == 0)) {
693 period = ((param >> 16) & 0xffff) * hz / 1000;
694 freq = 1193182 / (param & 0xffff);
696 sysbeep(freq, period);
700 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
702 struct vt_window *vw = tm->tm_softc;
704 vtbuf_cursor_position(&vw->vw_buf, p);
708 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
710 struct vt_window *vw = tm->tm_softc;
712 vtbuf_putchar(&vw->vw_buf, p, c);
716 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
718 struct vt_window *vw = tm->tm_softc;
720 vtbuf_fill_locked(&vw->vw_buf, r, c);
724 vtterm_copy(struct terminal *tm, const term_rect_t *r,
727 struct vt_window *vw = tm->tm_softc;
729 vtbuf_copy(&vw->vw_buf, r, p);
733 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
735 struct vt_window *vw = tm->tm_softc;
739 vtbuf_cursor_visibility(&vw->vw_buf, arg);
742 vw->vw_mouse_level = arg;
748 vt_determine_colors(term_char_t c, int cursor,
749 term_color_t *fg, term_color_t *bg)
756 *fg = TCHAR_FGCOLOR(c);
757 if (TCHAR_FORMAT(c) & TF_BOLD)
758 *fg = TCOLOR_LIGHT(*fg);
759 *bg = TCHAR_BGCOLOR(c);
761 if (TCHAR_FORMAT(c) & TF_REVERSE)
774 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
775 int iscursor, unsigned int row, unsigned int col)
779 vt_determine_colors(c, iscursor, &fg, &bg);
785 src = vtfont_lookup(vf, c);
788 * Align the terminal to the centre of the screen.
789 * Fonts may not always be able to fill the entire
792 top = row * vf->vf_height + vd->vd_offset.tp_row;
793 left = col * vf->vf_width + vd->vd_offset.tp_col;
795 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
796 vf->vf_width, vf->vf_height, fg, bg);
798 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
804 vt_flush(struct vt_device *vd)
806 struct vt_window *vw;
808 struct vt_bufmask tmask;
809 unsigned int row, col;
813 #ifndef SC_NO_CUTPASTE
814 struct mouse_cursor *m;
818 vw = vd->vd_curwindow;
822 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
825 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
828 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
829 vt_termsize(vd, vf, &size);
831 /* Force a full redraw when the screen contents are invalid. */
832 if (vd->vd_flags & VDF_INVALID) {
833 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
835 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
837 vd->vd_flags &= ~VDF_INVALID;
840 #ifndef SC_NO_CUTPASTE
841 if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) {
842 /* Mark last mouse position as dirty to erase. */
843 vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx,
848 for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
849 if (!VTBUF_DIRTYROW(&tmask, row))
851 r = VTBUF_GET_ROW(&vw->vw_buf, row);
852 for (col = tarea.tr_begin.tp_col;
853 col < tarea.tr_end.tp_col; col++) {
854 if (!VTBUF_DIRTYCOL(&tmask, col))
857 vt_bitblt_char(vd, vf, r[col],
858 VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
862 #ifndef SC_NO_CUTPASTE
863 /* Mouse disabled. */
864 if (vw->vw_flags & VWF_MOUSE_HIDE)
867 /* No mouse for DDB. */
868 if (kdb_active || panicstr != NULL)
871 if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
873 m = &vt_default_mouse_pointer;
874 bpl = (m->w + 7) >> 3; /* Bytes per source line. */
878 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
879 w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
880 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
881 h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
883 vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl,
884 vd->vd_offset.tp_row + vd->vd_my,
885 vd->vd_offset.tp_col + vd->vd_mx,
886 w, h, TC_WHITE, TC_BLACK);
887 /* Save point of last mouse cursor to erase it later. */
888 vd->vd_mdirtyx = vd->vd_mx / vf->vf_width;
889 vd->vd_mdirtyy = vd->vd_my / vf->vf_height;
897 struct vt_device *vd;
900 /* Update screen if required. */
903 /* Schedule for next update. */
904 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
908 vtterm_done(struct terminal *tm)
910 struct vt_window *vw = tm->tm_softc;
911 struct vt_device *vd = vw->vw_device;
913 if (kdb_active || panicstr != NULL) {
914 /* Switch to the debugger. */
915 if (vd->vd_curwindow != vw) {
916 vd->vd_curwindow = vw;
917 vd->vd_flags |= VDF_INVALID;
918 if (vd->vd_driver->vd_postswitch)
919 vd->vd_driver->vd_postswitch(vd);
921 vd->vd_flags &= ~VDF_SPLASH;
923 } else if (!(vd->vd_flags & VDF_ASYNC)) {
930 vtterm_splash(struct vt_device *vd)
934 /* Display a nice boot splash. */
935 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
937 top = (vd->vd_height - vt_logo_height) / 2;
938 left = (vd->vd_width - vt_logo_width) / 2;
939 switch (vt_logo_depth) {
941 /* XXX: Unhardcode colors! */
942 vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
943 top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
945 vd->vd_flags |= VDF_SPLASH;
952 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
954 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
955 struct vt_window *vw = tm->tm_softc;
956 struct vt_device *vd = vw->vw_device;
961 if (!vty_enabled(VTY_VT))
964 if (vd->vd_flags & VDF_INITIALIZED)
965 /* Initialization already done. */
968 SET_FOREACH(vtdlist, vt_drv_set) {
970 if (vtd->vd_probe == NULL)
972 if (vtd->vd_probe(vd) == CN_DEAD)
974 if ((vtdbest == NULL) ||
975 (vtd->vd_priority > vtdbest->vd_priority))
978 if (vtdbest == NULL) {
979 cp->cn_pri = CN_DEAD;
980 vd->vd_flags |= VDF_DEAD;
982 vd->vd_driver = vtdbest;
983 cp->cn_pri = vd->vd_driver->vd_init(vd);
986 /* Check if driver's vt_init return CN_DEAD. */
987 if (cp->cn_pri == CN_DEAD) {
988 vd->vd_flags |= VDF_DEAD;
991 /* Initialize any early-boot keyboard drivers */
992 kbd_configure(KB_CONF_PROBE_ONLY);
994 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
995 vd->vd_windows[VT_CONSWINDOW] = vw;
996 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
998 /* Attach default font if not in TEXTMODE. */
999 if (!(vd->vd_flags & VDF_TEXTMODE))
1000 vw->vw_font = vtfont_ref(&vt_font_default);
1002 vtbuf_init_early(&vw->vw_buf);
1003 vt_winsize(vd, vw->vw_font, &wsz);
1004 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1006 attr.ta_format = TCHAR_FORMAT(c);
1007 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1008 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1009 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1011 if (vtdbest != NULL) {
1015 vd->vd_flags |= VDF_INITIALIZED;
1020 vtterm_cngetc(struct terminal *tm)
1022 struct vt_window *vw = tm->tm_softc;
1023 struct vt_device *vd = vw->vw_device;
1028 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1029 return (*vw->vw_kbdsq++);
1032 /* Make sure the splash screen is not there. */
1033 if (vd->vd_flags & VDF_SPLASH) {
1035 vd->vd_flags &= ~VDF_SPLASH;
1036 /* Mark screen as invalid to force update */
1037 vd->vd_flags |= VDF_INVALID;
1041 /* Stripped down keyboard handler. */
1042 kbd = kbd_get_keyboard(vd->vd_keyboard);
1046 /* Force keyboard input mode to K_XLATE */
1048 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1050 /* Switch the keyboard to polling to make it work here. */
1051 kbdd_poll(kbd, TRUE);
1052 c = kbdd_read_char(kbd, 0);
1053 kbdd_poll(kbd, FALSE);
1057 if (vw->vw_flags & VWF_SCROLL) {
1058 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1063 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1067 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1068 if (state & SLKED) {
1069 /* Turn scrolling on. */
1070 vw->vw_flags |= VWF_SCROLL;
1071 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1073 /* Turn scrolling off. */
1074 vt_scroll(vw, 0, VHS_END);
1075 vw->vw_flags &= ~VWF_SCROLL;
1076 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1079 /* XXX: KDB can handle history. */
1080 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1081 vw->vw_kbdsq = "\x1b[A";
1083 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1084 vw->vw_kbdsq = "\x1b[B";
1086 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1087 vw->vw_kbdsq = "\x1b[C";
1089 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1090 vw->vw_kbdsq = "\x1b[D";
1094 /* Force refresh to make scrollback work. */
1096 } else if (KEYFLAGS(c) == 0) {
1097 return (KEYCHAR(c));
1100 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1101 return (*vw->vw_kbdsq++);
1107 vtterm_opened(struct terminal *tm, int opened)
1109 struct vt_window *vw = tm->tm_softc;
1110 struct vt_device *vd = vw->vw_device;
1113 vd->vd_flags &= ~VDF_SPLASH;
1115 vw->vw_flags |= VWF_OPENED;
1117 vw->vw_flags &= ~VWF_OPENED;
1118 /* TODO: finish ACQ/REL */
1124 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1126 struct vt_device *vd = vw->vw_device;
1127 struct terminal *tm = vw->vw_terminal;
1134 * Changing fonts is a little tricky. We must prevent
1135 * simultaneous access to the device, so we must stop
1136 * the display timer and the terminal from accessing.
1137 * We need to switch fonts and grow our screen buffer.
1139 * XXX: Right now the code uses terminal_mute() to
1140 * prevent data from reaching the console driver while
1141 * resizing the screen buffer. This isn't elegant...
1145 if (vw->vw_flags & VWF_BUSY) {
1146 /* Another process is changing the font. */
1150 if (vw->vw_font == NULL) {
1151 /* Our device doesn't need fonts. */
1155 vw->vw_flags |= VWF_BUSY;
1158 vt_termsize(vd, vf, &size);
1159 vt_winsize(vd, vf, &wsz);
1160 /* Save offset to font aligned area. */
1161 vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1162 vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1164 /* Grow the screen buffer and terminal. */
1165 terminal_mute(tm, 1);
1166 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1167 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1168 terminal_mute(tm, 0);
1170 /* Actually apply the font to the current window. */
1172 vtfont_unref(vw->vw_font);
1173 vw->vw_font = vtfont_ref(vf);
1175 /* Force a full redraw the next timer tick. */
1176 if (vd->vd_curwindow == vw)
1177 vd->vd_flags |= VDF_INVALID;
1178 vw->vw_flags &= ~VWF_BUSY;
1184 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1186 struct vt_device *vd = vw->vw_device;
1187 int x, y, off_x, off_y;
1189 if (vd->vd_driver->vd_drawrect == NULL)
1192 x = vd->vd_width - 1;
1193 y = vd->vd_height - 1;
1194 off_x = vd->vd_offset.tp_col;
1195 off_y = vd->vd_offset.tp_row;
1199 vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c);
1202 vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y,
1204 /* Right bar. May be 1 pixel wider than necessary due to rounding. */
1205 vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c);
1206 /* Bottom bar. May be 1 mixel taller than necessary due to rounding. */
1207 vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c);
1213 vt_proc_alive(struct vt_window *vw)
1217 if (vw->vw_smode.mode != VT_PROCESS)
1221 if ((p = pfind(vw->vw_pid)) != NULL)
1223 if (vw->vw_proc == p)
1226 vw->vw_smode.mode = VT_AUTO;
1227 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1234 signal_vt_rel(struct vt_window *vw)
1237 if (vw->vw_smode.mode != VT_PROCESS)
1239 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1244 vw->vw_flags |= VWF_SWWAIT_REL;
1245 PROC_LOCK(vw->vw_proc);
1246 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1247 PROC_UNLOCK(vw->vw_proc);
1248 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1253 signal_vt_acq(struct vt_window *vw)
1256 if (vw->vw_smode.mode != VT_PROCESS)
1258 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1259 cnavailable(vw->vw_terminal->consdev, FALSE);
1260 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1265 vw->vw_flags |= VWF_SWWAIT_ACQ;
1266 PROC_LOCK(vw->vw_proc);
1267 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1268 PROC_UNLOCK(vw->vw_proc);
1269 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1274 finish_vt_rel(struct vt_window *vw, int release, int *s)
1277 if (vw->vw_flags & VWF_SWWAIT_REL) {
1278 vw->vw_flags &= ~VWF_SWWAIT_REL;
1280 callout_drain(&vw->vw_proc_dead_timer);
1281 vt_late_window_switch(vw->vw_switch_to);
1289 finish_vt_acq(struct vt_window *vw)
1292 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1293 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1299 #ifndef SC_NO_CUTPASTE
1301 vt_mouse_terminput_button(struct vt_device *vd, int button)
1303 struct vt_window *vw;
1305 char mouseb[6] = "\x1B[M";
1308 vw = vd->vd_curwindow;
1311 /* Translate to char position. */
1312 x = vd->vd_mx / vf->vf_width;
1313 y = vd->vd_my / vf->vf_height;
1314 /* Avoid overflow. */
1315 x = MIN(x, 255 - '!');
1316 y = MIN(y, 255 - '!');
1318 mouseb[3] = ' ' + button;
1319 mouseb[4] = '!' + x;
1320 mouseb[5] = '!' + y;
1322 for (i = 0; i < sizeof(mouseb); i++ )
1323 terminal_input_char(vw->vw_terminal, mouseb[i]);
1327 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1332 case MOUSE_BUTTON_EVENT:
1334 /* Mouse button pressed. */
1335 if (event & MOUSE_BUTTON1DOWN)
1336 vt_mouse_terminput_button(vd, 0);
1337 if (event & MOUSE_BUTTON2DOWN)
1338 vt_mouse_terminput_button(vd, 1);
1339 if (event & MOUSE_BUTTON3DOWN)
1340 vt_mouse_terminput_button(vd, 2);
1342 /* Mouse button released. */
1343 vt_mouse_terminput_button(vd, 3);
1347 case MOUSE_MOTION_EVENT:
1348 if (mouse->u.data.z < 0) {
1350 sc_mouse_input_button(vd, 64);
1351 } else if (mouse->u.data.z > 0) {
1353 sc_mouse_input_button(vd, 65);
1361 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1363 struct vt_device *vd;
1364 struct vt_window *vw;
1371 vw = vd->vd_curwindow;
1375 if (vw->vw_flags & VWF_MOUSE_HIDE)
1376 return; /* Mouse disabled. */
1378 if (vf == NULL) /* Text mode. */
1382 * TODO: add flag about pointer position changed, to not redraw chars
1383 * under mouse pointer when nothing changed.
1386 if (vw->vw_mouse_level > 0)
1387 vt_mouse_terminput(vd, type, x, y, event, cnt);
1391 case MOUSE_MOTION_EVENT:
1396 vt_termsize(vd, vf, &size);
1401 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1402 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1406 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1407 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1408 vd->vd_mx / vf->vf_width,
1409 vd->vd_my / vf->vf_height) == 1)) {
1412 * We have something marked to copy, so update pointer
1413 * to window with selection.
1415 vd->vd_markedwin = vw;
1418 case MOUSE_BUTTON_EVENT:
1426 case MOUSE_BUTTON1DOWN:
1429 mark = VTB_MARK_END;
1431 case 1: /* single click: start cut operation */
1432 mark = VTB_MARK_START;
1434 case 2: /* double click: cut a word */
1435 mark = VTB_MARK_WORD;
1437 case 3: /* triple click: cut a line */
1438 mark = VTB_MARK_ROW;
1442 case VT_MOUSE_PASTEBUTTON:
1447 if (vd->vd_markedwin == NULL)
1449 /* Get current selecton size in bytes. */
1450 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1454 buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1455 /* Request cupy/paste buffer data, no more than `len' */
1456 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1459 len /= sizeof(term_char_t);
1460 for (i = 0; i < len; i++ ) {
1463 terminal_input_char(vw->vw_terminal, buf[i]);
1466 /* Done, so cleanup. */
1471 case VT_MOUSE_EXTENDBUTTON:
1474 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1475 mark = VTB_MARK_EXTEND;
1480 mark = VTB_MARK_EXTEND;
1488 /* Save buttons state. */
1490 vd->vd_mstate |= event;
1492 vd->vd_mstate &= ~event;
1494 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1495 vd->vd_my / vf->vf_height) == 1) {
1497 * We have something marked to copy, so update pointer to
1498 * window with selection.
1500 vd->vd_markedwin = vw;
1505 vt_mouse_state(int show)
1507 struct vt_device *vd;
1508 struct vt_window *vw;
1511 vw = vd->vd_curwindow;
1515 vw->vw_flags |= VWF_MOUSE_HIDE;
1518 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1525 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1526 int nprot, vm_memattr_t *memattr)
1528 struct vt_window *vw = tm->tm_softc;
1529 struct vt_device *vd = vw->vw_device;
1531 if (vd->vd_driver->vd_fb_mmap)
1532 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1539 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1542 struct vt_window *vw = tm->tm_softc;
1543 struct vt_device *vd = vw->vw_device;
1546 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1547 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1558 cmd = VT_WAITACTIVE;
1584 ival = IOCPARM_IVAL(data);
1585 data = (caddr_t)&ival;
1590 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1591 if (*(int *)data & ~0x7f)
1595 case GIO_DEADKEYMAP:
1596 case PIO_DEADKEYMAP:
1601 case KDSKBSTATE: /* set keyboard state (locks) */
1602 case KDGKBSTATE: /* get keyboard state (locks) */
1603 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1604 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1605 case KDSETLED: /* set keyboard LED status */
1606 case KDGETLED: /* get keyboard LED status */
1607 case KBADDKBD: /* add/remove keyboard to/from mux */
1612 kbd = kbd_get_keyboard(vd->vd_keyboard);
1614 error = kbdd_ioctl(kbd, cmd, data);
1616 if (error == ENOIOCTL) {
1617 if (cmd == KDGKBTYPE) {
1618 /* always return something? XXX */
1630 kbd = kbd_get_keyboard(vd->vd_keyboard);
1632 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1635 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1636 *(int *)data = mode;
1642 mode = *(int *)data;
1647 vw->vw_kbdmode = mode;
1648 if (vw == vd->vd_curwindow) {
1653 kbd = kbd_get_keyboard(vd->vd_keyboard);
1655 error = kbdd_ioctl(kbd, KDSKBMODE,
1666 case FBIO_GETWINORG: /* get frame buffer window origin */
1667 case FBIO_GETDISPSTART: /* get display start address */
1668 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1669 case FBIO_BLANK: /* blank display */
1670 if (vd->vd_driver->vd_fb_ioctl)
1671 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1673 case CONS_BLANKTIME:
1678 *(int *)data = M_CG640x480;
1680 case CONS_BELLTYPE: /* set bell type sound */
1681 if ((*(int *)data) & CONS_QUIET_BELL)
1682 vd->vd_flags |= VDF_QUIET_BELL;
1684 vd->vd_flags &= ~VDF_QUIET_BELL;
1686 case CONS_GETINFO: {
1687 vid_info_t *vi = (vid_info_t *)data;
1689 vi->m_num = vd->vd_curwindow->vw_number + 1;
1690 /* XXX: other fields! */
1694 *(int *)data = 0x200;
1699 case CONS_MOUSECTL: {
1700 mouse_info_t *mouse = (mouse_info_t*)data;
1703 * This has no effect on vt(4). We don't draw any mouse
1704 * cursor. Just ignore MOUSE_HIDE and MOUSE_SHOW to
1705 * prevent excessive errors. All the other commands
1706 * should not be applied to individual TTYs, but only to
1709 switch (mouse->operation) {
1711 vd->vd_flags &= ~VDF_MOUSECURSOR;
1714 vd->vd_mx = vd->vd_width / 2;
1715 vd->vd_my = vd->vd_height / 2;
1716 vd->vd_flags |= VDF_MOUSECURSOR;
1725 error = vtfont_load((void *)data, &vf);
1729 error = vt_change_font(vw, vf);
1731 /* XXX: replace 0 with current bg color. */
1732 vt_set_border(vw, vf, 0);
1738 scrmap_t *sm = (scrmap_t *)data;
1741 /* We don't have screen maps, so return a handcrafted one. */
1742 for (i = 0; i < 256; i++)
1749 case KDENABIO: /* allow io operations */
1750 error = priv_check(td, PRIV_IO);
1753 error = securelevel_gt(td->td_ucred, 0);
1756 #if defined(__i386__)
1757 td->td_frame->tf_eflags |= PSL_IOPL;
1758 #elif defined(__amd64__)
1759 td->td_frame->tf_rflags |= PSL_IOPL;
1762 case KDDISABIO: /* disallow io operations (default) */
1763 #if defined(__i386__)
1764 td->td_frame->tf_eflags &= ~PSL_IOPL;
1765 #elif defined(__amd64__)
1766 td->td_frame->tf_rflags &= ~PSL_IOPL;
1769 case KDMKTONE: /* sound the bell */
1770 vtterm_beep(tm, *(u_int *)data);
1772 case KIOCSOUND: /* make tone (*data) hz */
1775 case CONS_SETKBD: /* set the new keyboard */
1778 if (vd->vd_keyboard != *(int *)data) {
1779 kbd = kbd_get_keyboard(*(int *)data);
1784 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1785 (void *)vd, vt_kbdevent, vd);
1787 if (vd->vd_keyboard != -1) {
1788 kbd_release(kbd, (void *)vd);
1790 kbd = kbd_get_keyboard(i);
1791 vd->vd_keyboard = i;
1793 (void)kbdd_ioctl(kbd, KDSKBMODE,
1794 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1796 error = EPERM; /* XXX */
1801 case CONS_RELKBD: /* release the current keyboard */
1804 if (vd->vd_keyboard != -1) {
1805 kbd = kbd_get_keyboard(vd->vd_keyboard);
1810 error = kbd_release(kbd, (void *)vd);
1812 vd->vd_keyboard = -1;
1819 win = *(int *)data - 1;
1820 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1822 if ((win > VT_MAXWINDOWS) || (win < 0))
1824 return (vt_proc_window_switch(vd->vd_windows[win]));
1827 *(int *)data = vd->vd_curwindow->vw_number + 1;
1830 *(int *)data = vw->vw_number + 1;
1833 /* TODO: Check current state, switching can be in progress. */
1834 if ((*(int *)data) == 0x01)
1835 vw->vw_flags |= VWF_VTYLOCK;
1836 else if ((*(int *)data) == 0x02)
1837 vw->vw_flags &= ~VWF_VTYLOCK;
1843 for (i = 0; i < VT_MAXWINDOWS; i++) {
1844 vw = vd->vd_windows[i];
1847 if (!(vw->vw_flags & VWF_OPENED)) {
1848 *(int *)data = vw->vw_number + 1;
1858 i = *(unsigned int *)data;
1859 if (i > VT_MAXWINDOWS)
1862 vw = vd->vd_windows[i - 1];
1865 while (vd->vd_curwindow != vw && error == 0)
1866 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1869 case VT_SETMODE: { /* set screen switcher mode */
1870 struct vt_mode *mode;
1873 mode = (struct vt_mode *)data;
1874 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1875 if (vw->vw_smode.mode == VT_PROCESS) {
1876 p1 = pfind(vw->vw_pid);
1877 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1880 DPRINTF(5, "error EPERM\n");
1886 if (mode->mode == VT_AUTO) {
1887 vw->vw_smode.mode = VT_AUTO;
1890 DPRINTF(5, "VT_AUTO, ");
1891 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1892 cnavailable(vw->vw_terminal->consdev, TRUE);
1893 /* were we in the middle of the vty switching process? */
1894 if (finish_vt_rel(vw, TRUE, &s) == 0)
1895 DPRINTF(5, "reset WAIT_REL, ");
1896 if (finish_vt_acq(vw) == 0)
1897 DPRINTF(5, "reset WAIT_ACQ, ");
1899 } else if (mode->mode == VT_PROCESS) {
1900 if (!ISSIGVALID(mode->relsig) ||
1901 !ISSIGVALID(mode->acqsig) ||
1902 !ISSIGVALID(mode->frsig)) {
1903 DPRINTF(5, "error EINVAL\n");
1906 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1907 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1908 vw->vw_proc = td->td_proc;
1909 vw->vw_pid = vw->vw_proc->p_pid;
1910 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1911 cnavailable(vw->vw_terminal->consdev, FALSE);
1913 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
1920 case VT_GETMODE: /* get screen switcher mode */
1921 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
1924 case VT_RELDISP: /* screen switcher ioctl */
1926 * This must be the current vty which is in the VT_PROCESS
1929 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
1933 /* ...and this process is controlling it. */
1934 if (vw->vw_proc != td->td_proc) {
1938 switch(*(int *)data) {
1939 case VT_FALSE: /* user refuses to release screen, abort */
1940 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
1941 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
1942 SC_DRIVER_NAME, VT_UNIT(vw));
1944 case VT_TRUE: /* user has released screen, go on */
1945 /* finish_vt_rel(..., TRUE, ...) should not be locked */
1946 if (vw->vw_flags & VWF_SWWAIT_REL) {
1947 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
1948 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
1949 SC_DRIVER_NAME, VT_UNIT(vw));
1954 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1955 if ((error = finish_vt_acq(vw)) == 0)
1956 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
1957 SC_DRIVER_NAME, VT_UNIT(vw));
1968 static struct vt_window *
1969 vt_allocate_window(struct vt_device *vd, unsigned int window)
1971 struct vt_window *vw;
1972 struct terminal *tm;
1976 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
1978 vw->vw_number = window;
1979 vw->vw_kbdmode = K_XLATE;
1981 if (!(vd->vd_flags & VDF_TEXTMODE))
1982 vw->vw_font = vtfont_ref(&vt_font_default);
1984 vt_termsize(vd, vw->vw_font, &size);
1985 vt_winsize(vd, vw->vw_font, &wsz);
1986 vtbuf_init(&vw->vw_buf, &size);
1988 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
1989 terminal_set_winsize(tm, &wsz);
1990 vd->vd_windows[window] = vw;
1991 callout_init(&vw->vw_proc_dead_timer, 0);
1997 vt_upgrade(struct vt_device *vd)
1999 struct vt_window *vw;
2002 if (!vty_enabled(VTY_VT))
2005 for (i = 0; i < VT_MAXWINDOWS; i++) {
2006 vw = vd->vd_windows[i];
2009 vw = vt_allocate_window(vd, i);
2011 if (!(vw->vw_flags & VWF_READY)) {
2012 callout_init(&vw->vw_proc_dead_timer, 0);
2013 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2014 vw->vw_flags |= VWF_READY;
2015 if (vw->vw_flags & VWF_CONSOLE) {
2016 /* For existing console window. */
2017 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2018 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2024 if (vd->vd_curwindow == NULL)
2025 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2027 if (!(vd->vd_flags & VDF_ASYNC)) {
2028 /* Attach keyboard. */
2029 vt_allocate_keyboard(vd);
2030 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
2032 /* Init 25 Hz timer. */
2033 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2035 /* Start timer when everything ready. */
2036 vd->vd_flags |= VDF_ASYNC;
2037 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2042 /* Refill settings with new sizes. */
2047 vt_resize(struct vt_device *vd)
2049 struct vt_window *vw;
2052 for (i = 0; i < VT_MAXWINDOWS; i++) {
2053 vw = vd->vd_windows[i];
2055 /* Assign default font to window, if not textmode. */
2056 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2057 vw->vw_font = vtfont_ref(&vt_font_default);
2059 /* Resize terminal windows */
2060 vt_change_font(vw, vw->vw_font);
2065 vt_allocate(struct vt_driver *drv, void *softc)
2067 struct vt_device *vd;
2070 if (!vty_enabled(VTY_VT))
2073 if (main_vd->vd_driver == NULL) {
2074 main_vd->vd_driver = drv;
2075 printf("VT: initialize with new VT driver \"%s\".\n",
2079 * Check if have rights to replace current driver. For example:
2080 * it is bad idea to replace KMS driver with generic VGA one.
2082 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2083 printf("VT: Driver priority %d too low. Current %d\n ",
2084 drv->vd_priority, main_vd->vd_driver->vd_priority);
2087 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2088 main_vd->vd_driver->vd_name, drv->vd_name);
2092 if (drv->vd_maskbitbltchr == NULL)
2093 drv->vd_maskbitbltchr = drv->vd_bitbltchr;
2095 if (vd->vd_flags & VDF_ASYNC) {
2096 /* Stop vt_flush periodic task. */
2097 callout_drain(&vd->vd_timer);
2099 * Mute current terminal until we done. vt_change_font (called
2100 * from vt_resize) will unmute it.
2102 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2106 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2109 vd->vd_flags &= ~VDF_TEXTMODE;
2111 vd->vd_driver = drv;
2112 vd->vd_softc = softc;
2113 vd->vd_driver->vd_init(vd);
2119 if (vd->vd_flags & VDF_SPLASH)
2123 if (vd->vd_flags & VDF_ASYNC) {
2124 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2125 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
2128 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2130 /* Update console window sizes to actual. */
2131 vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz);
2132 terminal_set_winsize_blank(vd->vd_windows[VT_CONSWINDOW]->vw_terminal,
2140 if (vt_suspendswitch == 0)
2142 /* Save current window. */
2143 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2144 /* Ask holding process to free window and switch to console window */
2145 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2152 if (vt_suspendswitch == 0)
2154 /* Switch back to saved window */
2155 if (main_vd->vd_savedwindow != NULL)
2156 vt_proc_window_switch(main_vd->vd_savedwindow);
2157 main_vd->vd_savedwindow = NULL;