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 (main_vd->vd_driver != NULL)
219 printf("VT: running with driver \"%s\".\n",
220 main_vd->vd_driver->vd_name);
222 printf("VT: init without driver.\n");
224 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
225 cv_init(&main_vd->vd_winswitch, "vtwswt");
229 vt_switch_timer(void *arg)
232 vt_late_window_switch((struct vt_window *)arg);
236 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
239 DPRINTF(40, "%s\n", __func__);
240 curvw->vw_switch_to = vw;
241 /* Set timer to allow switch in case when process hang. */
242 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
243 vt_switch_timer, (void *)vw);
244 /* Notify process about vt switch attempt. */
245 DPRINTF(30, "%s: Notify process.\n", __func__);
246 signal_vt_rel(curvw);
252 vt_window_postswitch(struct vt_window *vw)
259 /* vt_late_window_switch will done VT switching for regular case. */
261 vt_late_window_switch(struct vt_window *vw)
265 callout_stop(&vw->vw_proc_dead_timer);
267 ret = vt_window_switch(vw);
271 /* Notify owner process about terminal availability. */
272 if (vw->vw_smode.mode == VT_PROCESS) {
273 ret = vt_window_postswitch(vw);
280 vt_proc_window_switch(struct vt_window *vw)
282 struct vt_window *curvw;
283 struct vt_device *vd;
287 curvw = vd->vd_curwindow;
289 if (curvw->vw_flags & VWF_VTYLOCK)
292 /* Ask current process permitions to switch away. */
293 if (curvw->vw_smode.mode == VT_PROCESS) {
294 DPRINTF(30, "%s: VT_PROCESS ", __func__);
295 if (vt_proc_alive(curvw) == FALSE) {
296 DPRINTF(30, "Dead. Cleaning.");
299 DPRINTF(30, "%s: Signaling process.\n", __func__);
300 /* Alive, try to ask him. */
301 ret = vt_window_preswitch(vw, curvw);
302 /* Wait for process answer or timeout. */
308 ret = vt_late_window_switch(vw);
312 /* Switch window ignoring process locking. */
314 vt_window_switch(struct vt_window *vw)
316 struct vt_device *vd = vw->vw_device;
317 struct vt_window *curvw = vd->vd_curwindow;
326 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
331 vd->vd_curwindow = vw;
332 vd->vd_flags |= VDF_INVALID;
333 cv_broadcast(&vd->vd_winswitch);
336 if (vd->vd_driver->vd_postswitch)
337 vd->vd_driver->vd_postswitch(vd);
339 /* Restore per-window keyboard mode. */
341 kbd = kbd_get_keyboard(vd->vd_keyboard);
343 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
346 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
352 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
355 size->tp_row = vd->vd_height;
356 size->tp_col = vd->vd_width;
358 size->tp_row /= vf->vf_height;
359 size->tp_col /= vf->vf_width;
364 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
367 size->ws_row = size->ws_ypixel = vd->vd_height;
368 size->ws_col = size->ws_xpixel = vd->vd_width;
370 size->ws_row /= vf->vf_height;
371 size->ws_col /= vf->vf_width;
376 vt_scroll(struct vt_window *vw, int offset, int whence)
381 if ((vw->vw_flags & VWF_SCROLL) == 0)
384 vt_termsize(vw->vw_device, vw->vw_font, &size);
386 diff = vthistory_seek(&vw->vw_buf, offset, whence);
388 * Offset changed, please update Nth lines on sceen.
389 * +N - Nth lines at top;
390 * -N - Nth lines at bottom.
393 if (diff < -size.tp_row || diff > size.tp_row) {
394 vw->vw_device->vd_flags |= VDF_INVALID;
397 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
401 vt_machine_kbdevent(int c)
406 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
409 /* XXX: Make this configurable! */
413 shutdown_nice(RB_HALT);
416 shutdown_nice(RB_HALT|RB_POWEROFF);
424 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
426 struct vt_device *vd;
430 /* Only special keys handled in ScrollLock mode */
431 if ((c & SPCLKEY) == 0)
437 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
438 vw = vd->vd_windows[c - F_SCR];
440 vt_proc_window_switch(vw);
448 /* Turn scrolling off. */
449 vt_scroll(vw, 0, VHS_END);
450 VTBUF_SLCK_DISABLE(&vw->vw_buf);
451 vw->vw_flags &= ~VWF_SCROLL;
454 case FKEY | F(49): /* Home key. */
455 vt_scroll(vw, 0, VHS_SET);
457 case FKEY | F(50): /* Arrow up. */
458 vt_scroll(vw, -1, VHS_CUR);
460 case FKEY | F(51): /* Page up. */
461 vt_termsize(vd, vw->vw_font, &size);
462 vt_scroll(vw, -size.tp_row, VHS_CUR);
464 case FKEY | F(57): /* End key. */
465 vt_scroll(vw, 0, VHS_END);
467 case FKEY | F(58): /* Arrow down. */
468 vt_scroll(vw, 1, VHS_CUR);
470 case FKEY | F(59): /* Page down. */
471 vt_termsize(vd, vw->vw_font, &size);
472 vt_scroll(vw, size.tp_row, VHS_CUR);
481 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
483 struct vt_window *vw = vd->vd_curwindow;
486 #if VT_ALT_TO_ESC_HACK
488 switch (c & ~RELKEY) {
489 case (SPCLKEY | RALT):
490 if (vt_enable_altgr != 0)
492 case (SPCLKEY | LALT):
493 vd->vd_kbstate &= ~ALKED;
495 /* Other keys ignored for RELKEY event. */
498 switch (c & ~RELKEY) {
499 case (SPCLKEY | RALT):
500 if (vt_enable_altgr != 0)
502 case (SPCLKEY | LALT):
503 vd->vd_kbstate |= ALKED;
508 /* Other keys ignored for RELKEY event. */
512 if (vt_machine_kbdevent(c))
515 if (vw->vw_flags & VWF_SCROLL) {
516 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
517 /* Scroll mode keys handled, nothing to do more. */
524 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
525 vw = vd->vd_windows[c - F_SCR];
527 vt_proc_window_switch(vw);
534 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
537 /* Turn scrolling on. */
538 vw->vw_flags |= VWF_SCROLL;
539 VTBUF_SLCK_ENABLE(&vw->vw_buf);
541 /* Turn scrolling off. */
542 vw->vw_flags &= ~VWF_SCROLL;
543 VTBUF_SLCK_DISABLE(&vw->vw_buf);
544 vt_scroll(vw, 0, VHS_END);
549 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
550 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
551 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
552 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
553 /* F1 through F12 keys. */
554 terminal_input_special(vw->vw_terminal,
555 TKEY_F1 + c - (FKEY | F(1)));
557 case FKEY | F(49): /* Home key. */
558 terminal_input_special(vw->vw_terminal, TKEY_HOME);
560 case FKEY | F(50): /* Arrow up. */
561 terminal_input_special(vw->vw_terminal, TKEY_UP);
563 case FKEY | F(51): /* Page up. */
564 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
566 case FKEY | F(53): /* Arrow left. */
567 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
569 case FKEY | F(55): /* Arrow right. */
570 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
572 case FKEY | F(57): /* End key. */
573 terminal_input_special(vw->vw_terminal, TKEY_END);
575 case FKEY | F(58): /* Arrow down. */
576 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
578 case FKEY | F(59): /* Page down. */
579 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
581 case FKEY | F(60): /* Insert key. */
582 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
584 case FKEY | F(61): /* Delete key. */
585 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
588 } else if (KEYFLAGS(c) == 0) {
589 /* Don't do UTF-8 conversion when doing raw mode. */
590 if (vw->vw_kbdmode == K_XLATE) {
591 #if VT_ALT_TO_ESC_HACK
592 if (vd->vd_kbstate & ALKED) {
594 * Prepend ESC sequence if one of ALT keys down.
596 terminal_input_char(vw->vw_terminal, 0x1b);
600 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
602 terminal_input_raw(vw->vw_terminal, c);
608 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
610 struct vt_device *vd = arg;
616 case KBDIO_UNLOADING:
618 vd->vd_keyboard = -1;
619 kbd_release(kbd, (void *)&vd->vd_keyboard);
626 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
627 vt_processkey(kbd, vd, c);
633 vt_allocate_keyboard(struct vt_device *vd)
639 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
640 vd->vd_keyboard = idx0;
642 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
643 k0 = kbd_get_keyboard(idx0);
645 for (idx = kbd_find_keyboard2("*", -1, 0);
647 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
648 k = kbd_get_keyboard(idx);
650 if (idx == idx0 || KBD_IS_BUSY(k))
653 bzero(&ki, sizeof(ki));
654 strcpy(ki.kb_name, k->kb_name);
655 ki.kb_unit = k->kb_unit;
657 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
660 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
661 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
663 DPRINTF(10, "%s: No keyboard found.\n", __func__);
667 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
673 vtterm_bell(struct terminal *tm)
675 struct vt_window *vw = tm->tm_softc;
676 struct vt_device *vd = vw->vw_device;
678 if (vd->vd_flags & VDF_QUIET_BELL)
681 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
685 vtterm_beep(struct terminal *tm, u_int param)
689 if ((param == 0) || ((param & 0xffff) == 0)) {
694 period = ((param >> 16) & 0xffff) * hz / 1000;
695 freq = 1193182 / (param & 0xffff);
697 sysbeep(freq, period);
701 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
703 struct vt_window *vw = tm->tm_softc;
705 vtbuf_cursor_position(&vw->vw_buf, p);
709 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
711 struct vt_window *vw = tm->tm_softc;
713 vtbuf_putchar(&vw->vw_buf, p, c);
717 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
719 struct vt_window *vw = tm->tm_softc;
721 vtbuf_fill_locked(&vw->vw_buf, r, c);
725 vtterm_copy(struct terminal *tm, const term_rect_t *r,
728 struct vt_window *vw = tm->tm_softc;
730 vtbuf_copy(&vw->vw_buf, r, p);
734 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
736 struct vt_window *vw = tm->tm_softc;
740 vtbuf_cursor_visibility(&vw->vw_buf, arg);
743 vw->vw_mouse_level = arg;
749 vt_determine_colors(term_char_t c, int cursor,
750 term_color_t *fg, term_color_t *bg)
757 *fg = TCHAR_FGCOLOR(c);
758 if (TCHAR_FORMAT(c) & TF_BOLD)
759 *fg = TCOLOR_LIGHT(*fg);
760 *bg = TCHAR_BGCOLOR(c);
762 if (TCHAR_FORMAT(c) & TF_REVERSE)
775 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
776 int iscursor, unsigned int row, unsigned int col)
780 vt_determine_colors(c, iscursor, &fg, &bg);
786 src = vtfont_lookup(vf, c);
789 * Align the terminal to the centre of the screen.
790 * Fonts may not always be able to fill the entire
793 top = row * vf->vf_height + vd->vd_offset.tp_row;
794 left = col * vf->vf_width + vd->vd_offset.tp_col;
796 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
797 vf->vf_width, vf->vf_height, fg, bg);
799 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
805 vt_flush(struct vt_device *vd)
807 struct vt_window *vw;
809 struct vt_bufmask tmask;
810 unsigned int row, col;
814 #ifndef SC_NO_CUTPASTE
815 struct mouse_cursor *m;
819 vw = vd->vd_curwindow;
823 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
826 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
829 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
830 vt_termsize(vd, vf, &size);
832 /* Force a full redraw when the screen contents are invalid. */
833 if (vd->vd_flags & VDF_INVALID) {
834 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
836 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
838 vd->vd_flags &= ~VDF_INVALID;
841 #ifndef SC_NO_CUTPASTE
842 if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) {
843 /* Mark last mouse position as dirty to erase. */
844 vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx,
849 for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
850 if (!VTBUF_DIRTYROW(&tmask, row))
852 r = VTBUF_GET_ROW(&vw->vw_buf, row);
853 for (col = tarea.tr_begin.tp_col;
854 col < tarea.tr_end.tp_col; col++) {
855 if (!VTBUF_DIRTYCOL(&tmask, col))
858 vt_bitblt_char(vd, vf, r[col],
859 VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
863 #ifndef SC_NO_CUTPASTE
864 /* Mouse disabled. */
865 if (vw->vw_flags & VWF_MOUSE_HIDE)
868 /* No mouse for DDB. */
869 if (kdb_active || panicstr != NULL)
872 if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
874 m = &vt_default_mouse_pointer;
875 bpl = (m->w + 7) >> 3; /* Bytes per source line. */
879 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
880 w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
881 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
882 h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
884 vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl,
885 vd->vd_offset.tp_row + vd->vd_my,
886 vd->vd_offset.tp_col + vd->vd_mx,
887 w, h, TC_WHITE, TC_BLACK);
888 /* Save point of last mouse cursor to erase it later. */
889 vd->vd_mdirtyx = vd->vd_mx / vf->vf_width;
890 vd->vd_mdirtyy = vd->vd_my / vf->vf_height;
898 struct vt_device *vd;
901 /* Update screen if required. */
904 /* Schedule for next update. */
905 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
909 vtterm_done(struct terminal *tm)
911 struct vt_window *vw = tm->tm_softc;
912 struct vt_device *vd = vw->vw_device;
914 if (kdb_active || panicstr != NULL) {
915 /* Switch to the debugger. */
916 if (vd->vd_curwindow != vw) {
917 vd->vd_curwindow = vw;
918 vd->vd_flags |= VDF_INVALID;
919 if (vd->vd_driver->vd_postswitch)
920 vd->vd_driver->vd_postswitch(vd);
922 vd->vd_flags &= ~VDF_SPLASH;
924 } else if (!(vd->vd_flags & VDF_ASYNC)) {
931 vtterm_splash(struct vt_device *vd)
935 /* Display a nice boot splash. */
936 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
938 top = (vd->vd_height - vt_logo_height) / 2;
939 left = (vd->vd_width - vt_logo_width) / 2;
940 switch (vt_logo_depth) {
942 /* XXX: Unhardcode colors! */
943 vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
944 top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
946 vd->vd_flags |= VDF_SPLASH;
953 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
955 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
956 struct vt_window *vw = tm->tm_softc;
957 struct vt_device *vd = vw->vw_device;
962 if (vd->vd_flags & VDF_INITIALIZED)
963 /* Initialization already done. */
966 SET_FOREACH(vtdlist, vt_drv_set) {
968 if (vtd->vd_probe == NULL)
970 if (vtd->vd_probe(vd) == CN_DEAD)
972 if ((vtdbest == NULL) ||
973 (vtd->vd_priority > vtdbest->vd_priority))
976 if (vtdbest == NULL) {
977 cp->cn_pri = CN_DEAD;
978 vd->vd_flags |= VDF_DEAD;
980 vd->vd_driver = vtdbest;
981 cp->cn_pri = vd->vd_driver->vd_init(vd);
984 /* Check if driver's vt_init return CN_DEAD. */
985 if (cp->cn_pri == CN_DEAD) {
986 vd->vd_flags |= VDF_DEAD;
989 /* Initialize any early-boot keyboard drivers */
990 kbd_configure(KB_CONF_PROBE_ONLY);
992 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
993 vd->vd_windows[VT_CONSWINDOW] = vw;
994 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
996 /* Attach default font if not in TEXTMODE. */
997 if (!(vd->vd_flags & VDF_TEXTMODE))
998 vw->vw_font = vtfont_ref(&vt_font_default);
1000 vtbuf_init_early(&vw->vw_buf);
1001 vt_winsize(vd, vw->vw_font, &wsz);
1002 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1004 attr.ta_format = TCHAR_FORMAT(c);
1005 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1006 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1007 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1009 if (vtdbest != NULL) {
1013 vd->vd_flags |= VDF_INITIALIZED;
1018 vtterm_cngetc(struct terminal *tm)
1020 struct vt_window *vw = tm->tm_softc;
1021 struct vt_device *vd = vw->vw_device;
1026 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1027 return (*vw->vw_kbdsq++);
1030 /* Make sure the splash screen is not there. */
1031 if (vd->vd_flags & VDF_SPLASH) {
1033 vd->vd_flags &= ~VDF_SPLASH;
1034 /* Mark screen as invalid to force update */
1035 vd->vd_flags |= VDF_INVALID;
1039 /* Stripped down keyboard handler. */
1040 kbd = kbd_get_keyboard(vd->vd_keyboard);
1044 /* Force keyboard input mode to K_XLATE */
1046 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1048 /* Switch the keyboard to polling to make it work here. */
1049 kbdd_poll(kbd, TRUE);
1050 c = kbdd_read_char(kbd, 0);
1051 kbdd_poll(kbd, FALSE);
1055 if (vw->vw_flags & VWF_SCROLL) {
1056 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1061 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1065 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1066 if (state & SLKED) {
1067 /* Turn scrolling on. */
1068 vw->vw_flags |= VWF_SCROLL;
1069 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1071 /* Turn scrolling off. */
1072 vt_scroll(vw, 0, VHS_END);
1073 vw->vw_flags &= ~VWF_SCROLL;
1074 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1077 /* XXX: KDB can handle history. */
1078 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1079 vw->vw_kbdsq = "\x1b[A";
1081 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1082 vw->vw_kbdsq = "\x1b[B";
1084 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1085 vw->vw_kbdsq = "\x1b[C";
1087 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1088 vw->vw_kbdsq = "\x1b[D";
1092 /* Force refresh to make scrollback work. */
1094 } else if (KEYFLAGS(c) == 0) {
1095 return (KEYCHAR(c));
1098 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1099 return (*vw->vw_kbdsq++);
1105 vtterm_opened(struct terminal *tm, int opened)
1107 struct vt_window *vw = tm->tm_softc;
1108 struct vt_device *vd = vw->vw_device;
1111 vd->vd_flags &= ~VDF_SPLASH;
1113 vw->vw_flags |= VWF_OPENED;
1115 vw->vw_flags &= ~VWF_OPENED;
1116 /* TODO: finish ACQ/REL */
1122 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1124 struct vt_device *vd = vw->vw_device;
1125 struct terminal *tm = vw->vw_terminal;
1132 * Changing fonts is a little tricky. We must prevent
1133 * simultaneous access to the device, so we must stop
1134 * the display timer and the terminal from accessing.
1135 * We need to switch fonts and grow our screen buffer.
1137 * XXX: Right now the code uses terminal_mute() to
1138 * prevent data from reaching the console driver while
1139 * resizing the screen buffer. This isn't elegant...
1143 if (vw->vw_flags & VWF_BUSY) {
1144 /* Another process is changing the font. */
1148 if (vw->vw_font == NULL) {
1149 /* Our device doesn't need fonts. */
1153 vw->vw_flags |= VWF_BUSY;
1156 vt_termsize(vd, vf, &size);
1157 vt_winsize(vd, vf, &wsz);
1158 /* Save offset to font aligned area. */
1159 vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1160 vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1162 /* Grow the screen buffer and terminal. */
1163 terminal_mute(tm, 1);
1164 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1165 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1166 terminal_mute(tm, 0);
1168 /* Actually apply the font to the current window. */
1170 vtfont_unref(vw->vw_font);
1171 vw->vw_font = vtfont_ref(vf);
1173 /* Force a full redraw the next timer tick. */
1174 if (vd->vd_curwindow == vw)
1175 vd->vd_flags |= VDF_INVALID;
1176 vw->vw_flags &= ~VWF_BUSY;
1182 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1184 struct vt_device *vd = vw->vw_device;
1185 int x, y, off_x, off_y;
1187 if (vd->vd_driver->vd_drawrect == NULL)
1190 x = vd->vd_width - 1;
1191 y = vd->vd_height - 1;
1192 off_x = vd->vd_offset.tp_col;
1193 off_y = vd->vd_offset.tp_row;
1197 vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c);
1200 vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y,
1202 /* Right bar. May be 1 pixel wider than necessary due to rounding. */
1203 vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c);
1204 /* Bottom bar. May be 1 mixel taller than necessary due to rounding. */
1205 vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c);
1211 vt_proc_alive(struct vt_window *vw)
1215 if (vw->vw_smode.mode != VT_PROCESS)
1219 if ((p = pfind(vw->vw_pid)) != NULL)
1221 if (vw->vw_proc == p)
1224 vw->vw_smode.mode = VT_AUTO;
1225 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1232 signal_vt_rel(struct vt_window *vw)
1235 if (vw->vw_smode.mode != VT_PROCESS)
1237 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1242 vw->vw_flags |= VWF_SWWAIT_REL;
1243 PROC_LOCK(vw->vw_proc);
1244 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1245 PROC_UNLOCK(vw->vw_proc);
1246 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1251 signal_vt_acq(struct vt_window *vw)
1254 if (vw->vw_smode.mode != VT_PROCESS)
1256 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1257 cnavailable(vw->vw_terminal->consdev, FALSE);
1258 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1263 vw->vw_flags |= VWF_SWWAIT_ACQ;
1264 PROC_LOCK(vw->vw_proc);
1265 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1266 PROC_UNLOCK(vw->vw_proc);
1267 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1272 finish_vt_rel(struct vt_window *vw, int release, int *s)
1275 if (vw->vw_flags & VWF_SWWAIT_REL) {
1276 vw->vw_flags &= ~VWF_SWWAIT_REL;
1278 callout_drain(&vw->vw_proc_dead_timer);
1279 vt_late_window_switch(vw->vw_switch_to);
1287 finish_vt_acq(struct vt_window *vw)
1290 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1291 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1297 #ifndef SC_NO_CUTPASTE
1299 vt_mouse_terminput_button(struct vt_device *vd, int button)
1301 struct vt_window *vw;
1303 char mouseb[6] = "\x1B[M";
1306 vw = vd->vd_curwindow;
1309 /* Translate to char position. */
1310 x = vd->vd_mx / vf->vf_width;
1311 y = vd->vd_my / vf->vf_height;
1312 /* Avoid overflow. */
1313 x = MIN(x, 255 - '!');
1314 y = MIN(y, 255 - '!');
1316 mouseb[3] = ' ' + button;
1317 mouseb[4] = '!' + x;
1318 mouseb[5] = '!' + y;
1320 for (i = 0; i < sizeof(mouseb); i++ )
1321 terminal_input_char(vw->vw_terminal, mouseb[i]);
1325 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1330 case MOUSE_BUTTON_EVENT:
1332 /* Mouse button pressed. */
1333 if (event & MOUSE_BUTTON1DOWN)
1334 vt_mouse_terminput_button(vd, 0);
1335 if (event & MOUSE_BUTTON2DOWN)
1336 vt_mouse_terminput_button(vd, 1);
1337 if (event & MOUSE_BUTTON3DOWN)
1338 vt_mouse_terminput_button(vd, 2);
1340 /* Mouse button released. */
1341 vt_mouse_terminput_button(vd, 3);
1345 case MOUSE_MOTION_EVENT:
1346 if (mouse->u.data.z < 0) {
1348 sc_mouse_input_button(vd, 64);
1349 } else if (mouse->u.data.z > 0) {
1351 sc_mouse_input_button(vd, 65);
1359 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1361 struct vt_device *vd;
1362 struct vt_window *vw;
1369 vw = vd->vd_curwindow;
1373 if (vw->vw_flags & VWF_MOUSE_HIDE)
1374 return; /* Mouse disabled. */
1376 if (vf == NULL) /* Text mode. */
1380 * TODO: add flag about pointer position changed, to not redraw chars
1381 * under mouse pointer when nothing changed.
1384 if (vw->vw_mouse_level > 0)
1385 vt_mouse_terminput(vd, type, x, y, event, cnt);
1389 case MOUSE_MOTION_EVENT:
1394 vt_termsize(vd, vf, &size);
1399 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1400 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1404 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1405 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1406 vd->vd_mx / vf->vf_width,
1407 vd->vd_my / vf->vf_height) == 1)) {
1410 * We have something marked to copy, so update pointer
1411 * to window with selection.
1413 vd->vd_markedwin = vw;
1416 case MOUSE_BUTTON_EVENT:
1424 case MOUSE_BUTTON1DOWN:
1427 mark = VTB_MARK_END;
1429 case 1: /* single click: start cut operation */
1430 mark = VTB_MARK_START;
1432 case 2: /* double click: cut a word */
1433 mark = VTB_MARK_WORD;
1435 case 3: /* triple click: cut a line */
1436 mark = VTB_MARK_ROW;
1440 case VT_MOUSE_PASTEBUTTON:
1445 if (vd->vd_markedwin == NULL)
1447 /* Get current selecton size in bytes. */
1448 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1452 buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1453 /* Request cupy/paste buffer data, no more than `len' */
1454 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1457 len /= sizeof(term_char_t);
1458 for (i = 0; i < len; i++ ) {
1461 terminal_input_char(vw->vw_terminal, buf[i]);
1464 /* Done, so cleanup. */
1469 case VT_MOUSE_EXTENDBUTTON:
1472 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1473 mark = VTB_MARK_EXTEND;
1478 mark = VTB_MARK_EXTEND;
1486 /* Save buttons state. */
1488 vd->vd_mstate |= event;
1490 vd->vd_mstate &= ~event;
1492 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1493 vd->vd_my / vf->vf_height) == 1) {
1495 * We have something marked to copy, so update pointer to
1496 * window with selection.
1498 vd->vd_markedwin = vw;
1503 vt_mouse_state(int show)
1505 struct vt_device *vd;
1506 struct vt_window *vw;
1509 vw = vd->vd_curwindow;
1513 vw->vw_flags |= VWF_MOUSE_HIDE;
1516 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1523 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1524 int nprot, vm_memattr_t *memattr)
1526 struct vt_window *vw = tm->tm_softc;
1527 struct vt_device *vd = vw->vw_device;
1529 if (vd->vd_driver->vd_fb_mmap)
1530 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1537 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1540 struct vt_window *vw = tm->tm_softc;
1541 struct vt_device *vd = vw->vw_device;
1544 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1545 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1556 cmd = VT_WAITACTIVE;
1582 ival = IOCPARM_IVAL(data);
1583 data = (caddr_t)&ival;
1588 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1589 if (*(int *)data & ~0x7f)
1593 case GIO_DEADKEYMAP:
1594 case PIO_DEADKEYMAP:
1599 case KDSKBSTATE: /* set keyboard state (locks) */
1600 case KDGKBSTATE: /* get keyboard state (locks) */
1601 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1602 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1603 case KDSETLED: /* set keyboard LED status */
1604 case KDGETLED: /* get keyboard LED status */
1605 case KBADDKBD: /* add/remove keyboard to/from mux */
1610 kbd = kbd_get_keyboard(vd->vd_keyboard);
1612 error = kbdd_ioctl(kbd, cmd, data);
1614 if (error == ENOIOCTL) {
1615 if (cmd == KDGKBTYPE) {
1616 /* always return something? XXX */
1628 kbd = kbd_get_keyboard(vd->vd_keyboard);
1630 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1633 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1634 *(int *)data = mode;
1640 mode = *(int *)data;
1645 vw->vw_kbdmode = mode;
1646 if (vw == vd->vd_curwindow) {
1651 kbd = kbd_get_keyboard(vd->vd_keyboard);
1653 error = kbdd_ioctl(kbd, KDSKBMODE,
1664 case FBIO_GETWINORG: /* get frame buffer window origin */
1665 case FBIO_GETDISPSTART: /* get display start address */
1666 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1667 case FBIO_BLANK: /* blank display */
1668 if (vd->vd_driver->vd_fb_ioctl)
1669 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1671 case CONS_BLANKTIME:
1676 *(int *)data = M_CG640x480;
1678 case CONS_BELLTYPE: /* set bell type sound */
1679 if ((*(int *)data) & CONS_QUIET_BELL)
1680 vd->vd_flags |= VDF_QUIET_BELL;
1682 vd->vd_flags &= ~VDF_QUIET_BELL;
1684 case CONS_GETINFO: {
1685 vid_info_t *vi = (vid_info_t *)data;
1687 vi->m_num = vd->vd_curwindow->vw_number + 1;
1688 /* XXX: other fields! */
1692 *(int *)data = 0x200;
1697 case CONS_MOUSECTL: {
1698 mouse_info_t *mouse = (mouse_info_t*)data;
1701 * This has no effect on vt(4). We don't draw any mouse
1702 * cursor. Just ignore MOUSE_HIDE and MOUSE_SHOW to
1703 * prevent excessive errors. All the other commands
1704 * should not be applied to individual TTYs, but only to
1707 switch (mouse->operation) {
1709 vd->vd_flags &= ~VDF_MOUSECURSOR;
1712 vd->vd_mx = vd->vd_width / 2;
1713 vd->vd_my = vd->vd_height / 2;
1714 vd->vd_flags |= VDF_MOUSECURSOR;
1723 error = vtfont_load((void *)data, &vf);
1727 error = vt_change_font(vw, vf);
1729 /* XXX: replace 0 with current bg color. */
1730 vt_set_border(vw, vf, 0);
1736 scrmap_t *sm = (scrmap_t *)data;
1739 /* We don't have screen maps, so return a handcrafted one. */
1740 for (i = 0; i < 256; i++)
1747 case KDENABIO: /* allow io operations */
1748 error = priv_check(td, PRIV_IO);
1751 error = securelevel_gt(td->td_ucred, 0);
1754 #if defined(__i386__)
1755 td->td_frame->tf_eflags |= PSL_IOPL;
1756 #elif defined(__amd64__)
1757 td->td_frame->tf_rflags |= PSL_IOPL;
1760 case KDDISABIO: /* disallow io operations (default) */
1761 #if defined(__i386__)
1762 td->td_frame->tf_eflags &= ~PSL_IOPL;
1763 #elif defined(__amd64__)
1764 td->td_frame->tf_rflags &= ~PSL_IOPL;
1767 case KDMKTONE: /* sound the bell */
1768 vtterm_beep(tm, *(u_int *)data);
1770 case KIOCSOUND: /* make tone (*data) hz */
1773 case CONS_SETKBD: /* set the new keyboard */
1776 if (vd->vd_keyboard != *(int *)data) {
1777 kbd = kbd_get_keyboard(*(int *)data);
1782 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1783 (void *)&vd->vd_keyboard, vt_kbdevent, vd);
1785 if (vd->vd_keyboard != -1) {
1787 (void *)&vd->vd_keyboard);
1789 kbd = kbd_get_keyboard(i);
1790 vd->vd_keyboard = i;
1792 (void)kbdd_ioctl(kbd, KDSKBMODE,
1793 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1795 error = EPERM; /* XXX */
1800 case CONS_RELKBD: /* release the current keyboard */
1803 if (vd->vd_keyboard != -1) {
1804 kbd = kbd_get_keyboard(vd->vd_keyboard);
1809 error = kbd_release(kbd, (void *)&vd->vd_keyboard);
1811 vd->vd_keyboard = -1;
1818 win = *(int *)data - 1;
1819 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1821 if ((win > VT_MAXWINDOWS) || (win < 0))
1823 return (vt_proc_window_switch(vd->vd_windows[win]));
1826 *(int *)data = vd->vd_curwindow->vw_number + 1;
1829 *(int *)data = vw->vw_number + 1;
1832 /* TODO: Check current state, switching can be in progress. */
1833 if ((*(int *)data) == 0x01)
1834 vw->vw_flags |= VWF_VTYLOCK;
1835 else if ((*(int *)data) == 0x02)
1836 vw->vw_flags &= ~VWF_VTYLOCK;
1842 for (i = 0; i < VT_MAXWINDOWS; i++) {
1843 vw = vd->vd_windows[i];
1846 if (!(vw->vw_flags & VWF_OPENED)) {
1847 *(int *)data = vw->vw_number + 1;
1857 i = *(unsigned int *)data;
1858 if (i > VT_MAXWINDOWS)
1861 vw = vd->vd_windows[i - 1];
1864 while (vd->vd_curwindow != vw && error == 0)
1865 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1868 case VT_SETMODE: { /* set screen switcher mode */
1869 struct vt_mode *mode;
1872 mode = (struct vt_mode *)data;
1873 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1874 if (vw->vw_smode.mode == VT_PROCESS) {
1875 p1 = pfind(vw->vw_pid);
1876 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1879 DPRINTF(5, "error EPERM\n");
1885 if (mode->mode == VT_AUTO) {
1886 vw->vw_smode.mode = VT_AUTO;
1889 DPRINTF(5, "VT_AUTO, ");
1890 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1891 cnavailable(vw->vw_terminal->consdev, TRUE);
1892 /* were we in the middle of the vty switching process? */
1893 if (finish_vt_rel(vw, TRUE, &s) == 0)
1894 DPRINTF(5, "reset WAIT_REL, ");
1895 if (finish_vt_acq(vw) == 0)
1896 DPRINTF(5, "reset WAIT_ACQ, ");
1898 } else if (mode->mode == VT_PROCESS) {
1899 if (!ISSIGVALID(mode->relsig) ||
1900 !ISSIGVALID(mode->acqsig) ||
1901 !ISSIGVALID(mode->frsig)) {
1902 DPRINTF(5, "error EINVAL\n");
1905 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1906 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1907 vw->vw_proc = td->td_proc;
1908 vw->vw_pid = vw->vw_proc->p_pid;
1909 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1910 cnavailable(vw->vw_terminal->consdev, FALSE);
1912 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
1919 case VT_GETMODE: /* get screen switcher mode */
1920 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
1923 case VT_RELDISP: /* screen switcher ioctl */
1925 * This must be the current vty which is in the VT_PROCESS
1928 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
1932 /* ...and this process is controlling it. */
1933 if (vw->vw_proc != td->td_proc) {
1937 switch(*(int *)data) {
1938 case VT_FALSE: /* user refuses to release screen, abort */
1939 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
1940 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
1941 SC_DRIVER_NAME, VT_UNIT(vw));
1943 case VT_TRUE: /* user has released screen, go on */
1944 /* finish_vt_rel(..., TRUE, ...) should not be locked */
1945 if (vw->vw_flags & VWF_SWWAIT_REL) {
1946 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
1947 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
1948 SC_DRIVER_NAME, VT_UNIT(vw));
1953 case VT_ACKACQ: /* acquire acknowledged, switch completed */
1954 if ((error = finish_vt_acq(vw)) == 0)
1955 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
1956 SC_DRIVER_NAME, VT_UNIT(vw));
1967 static struct vt_window *
1968 vt_allocate_window(struct vt_device *vd, unsigned int window)
1970 struct vt_window *vw;
1971 struct terminal *tm;
1975 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
1977 vw->vw_number = window;
1978 vw->vw_kbdmode = K_XLATE;
1980 if (!(vd->vd_flags & VDF_TEXTMODE))
1981 vw->vw_font = vtfont_ref(&vt_font_default);
1983 vt_termsize(vd, vw->vw_font, &size);
1984 vt_winsize(vd, vw->vw_font, &wsz);
1985 vtbuf_init(&vw->vw_buf, &size);
1987 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
1988 terminal_set_winsize(tm, &wsz);
1989 vd->vd_windows[window] = vw;
1990 callout_init(&vw->vw_proc_dead_timer, 0);
1996 vt_upgrade(struct vt_device *vd)
1998 struct vt_window *vw;
2001 for (i = 0; i < VT_MAXWINDOWS; i++) {
2002 vw = vd->vd_windows[i];
2005 vw = vt_allocate_window(vd, i);
2007 if (!(vw->vw_flags & VWF_READY)) {
2008 callout_init(&vw->vw_proc_dead_timer, 0);
2009 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2010 vw->vw_flags |= VWF_READY;
2011 if (vw->vw_flags & VWF_CONSOLE) {
2012 /* For existing console window. */
2013 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2014 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2020 if (vd->vd_curwindow == NULL)
2021 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2023 if (!(vd->vd_flags & VDF_ASYNC)) {
2024 /* Attach keyboard. */
2025 vt_allocate_keyboard(vd);
2026 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
2028 /* Init 25 Hz timer. */
2029 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2031 /* Start timer when everything ready. */
2032 vd->vd_flags |= VDF_ASYNC;
2033 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2038 /* Refill settings with new sizes. */
2043 vt_resize(struct vt_device *vd)
2045 struct vt_window *vw;
2048 for (i = 0; i < VT_MAXWINDOWS; i++) {
2049 vw = vd->vd_windows[i];
2051 /* Assign default font to window, if not textmode. */
2052 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2053 vw->vw_font = vtfont_ref(&vt_font_default);
2055 /* Resize terminal windows */
2056 vt_change_font(vw, vw->vw_font);
2061 vt_allocate(struct vt_driver *drv, void *softc)
2063 struct vt_device *vd;
2066 if (main_vd->vd_driver == NULL) {
2067 main_vd->vd_driver = drv;
2068 printf("VT: initialize with new VT driver \"%s\".\n",
2072 * Check if have rights to replace current driver. For example:
2073 * it is bad idea to replace KMS driver with generic VGA one.
2075 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2076 printf("VT: Driver priority %d too low. Current %d\n ",
2077 drv->vd_priority, main_vd->vd_driver->vd_priority);
2080 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2081 main_vd->vd_driver->vd_name, drv->vd_name);
2085 if (drv->vd_maskbitbltchr == NULL)
2086 drv->vd_maskbitbltchr = drv->vd_bitbltchr;
2088 if (vd->vd_flags & VDF_ASYNC) {
2089 /* Stop vt_flush periodic task. */
2090 callout_drain(&vd->vd_timer);
2092 * Mute current terminal until we done. vt_change_font (called
2093 * from vt_resize) will unmute it.
2095 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2099 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2102 vd->vd_flags &= ~VDF_TEXTMODE;
2104 vd->vd_driver = drv;
2105 vd->vd_softc = softc;
2106 vd->vd_driver->vd_init(vd);
2112 if (vd->vd_flags & VDF_SPLASH)
2116 if (vd->vd_flags & VDF_ASYNC) {
2117 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2118 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ);
2121 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2123 /* Update console window sizes to actual. */
2124 vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz);
2125 terminal_set_winsize_blank(vd->vd_windows[VT_CONSWINDOW]->vw_terminal,
2133 if (vt_suspendswitch == 0)
2135 /* Save current window. */
2136 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2137 /* Ask holding process to free window and switch to console window */
2138 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2145 if (vt_suspendswitch == 0)
2147 /* Switch back to saved window */
2148 if (main_vd->vd_savedwindow != NULL)
2149 vt_proc_window_switch(main_vd->vd_savedwindow);
2150 main_vd->vd_savedwindow = NULL;