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");
119 VT_SYSCTL_INT(spclkeys, (VT_DEBUG_KEY_ENABLED|VT_REBOOT_KEY_ENABLED|
120 VT_HALT_KEY_ENABLED|VT_POWEROFF_KEY_ENABLED), "Enabled special keys "
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 vt_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[0],
173 .vb_rows = &vt_constextbufrows[0],
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_schedule_flush(struct vt_device *vd, int ms)
235 /* Default to initial value. */
236 ms = 1000 / VT_TIMERFREQ;
238 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
242 vt_resume_flush_timer(struct vt_device *vd, int ms)
245 if (!atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
248 vt_schedule_flush(vd, ms);
252 vt_suspend_flush_timer(struct vt_device *vd)
255 if (!atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
258 callout_drain(&vd->vd_timer);
262 vt_switch_timer(void *arg)
265 vt_late_window_switch((struct vt_window *)arg);
269 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
272 DPRINTF(40, "%s\n", __func__);
273 curvw->vw_switch_to = vw;
274 /* Set timer to allow switch in case when process hang. */
275 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
276 vt_switch_timer, (void *)vw);
277 /* Notify process about vt switch attempt. */
278 DPRINTF(30, "%s: Notify process.\n", __func__);
279 signal_vt_rel(curvw);
285 vt_window_postswitch(struct vt_window *vw)
292 /* vt_late_window_switch will done VT switching for regular case. */
294 vt_late_window_switch(struct vt_window *vw)
298 callout_stop(&vw->vw_proc_dead_timer);
300 ret = vt_window_switch(vw);
304 /* Notify owner process about terminal availability. */
305 if (vw->vw_smode.mode == VT_PROCESS) {
306 ret = vt_window_postswitch(vw);
313 vt_proc_window_switch(struct vt_window *vw)
315 struct vt_window *curvw;
316 struct vt_device *vd;
320 curvw = vd->vd_curwindow;
322 if (curvw->vw_flags & VWF_VTYLOCK)
325 /* Ask current process permitions to switch away. */
326 if (curvw->vw_smode.mode == VT_PROCESS) {
327 DPRINTF(30, "%s: VT_PROCESS ", __func__);
328 if (vt_proc_alive(curvw) == FALSE) {
329 DPRINTF(30, "Dead. Cleaning.");
332 DPRINTF(30, "%s: Signaling process.\n", __func__);
333 /* Alive, try to ask him. */
334 ret = vt_window_preswitch(vw, curvw);
335 /* Wait for process answer or timeout. */
341 ret = vt_late_window_switch(vw);
345 /* Switch window ignoring process locking. */
347 vt_window_switch(struct vt_window *vw)
349 struct vt_device *vd = vw->vw_device;
350 struct vt_window *curvw = vd->vd_curwindow;
359 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
364 vt_suspend_flush_timer(vd);
366 vd->vd_curwindow = vw;
367 vd->vd_flags |= VDF_INVALID;
368 cv_broadcast(&vd->vd_winswitch);
371 if (vd->vd_driver->vd_postswitch)
372 vd->vd_driver->vd_postswitch(vd);
374 vt_resume_flush_timer(vd, 0);
376 /* Restore per-window keyboard mode. */
378 kbd = kbd_get_keyboard(vd->vd_keyboard);
380 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
383 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
389 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
392 size->tp_row = vd->vd_height;
393 size->tp_col = vd->vd_width;
395 size->tp_row /= vf->vf_height;
396 size->tp_col /= vf->vf_width;
401 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
404 size->ws_row = size->ws_ypixel = vd->vd_height;
405 size->ws_col = size->ws_xpixel = vd->vd_width;
407 size->ws_row /= vf->vf_height;
408 size->ws_col /= vf->vf_width;
413 vt_scroll(struct vt_window *vw, int offset, int whence)
418 if ((vw->vw_flags & VWF_SCROLL) == 0)
421 vt_termsize(vw->vw_device, vw->vw_font, &size);
423 diff = vthistory_seek(&vw->vw_buf, offset, whence);
425 * Offset changed, please update Nth lines on sceen.
426 * +N - Nth lines at top;
427 * -N - Nth lines at bottom.
430 if (diff < -size.tp_row || diff > size.tp_row) {
431 vw->vw_device->vd_flags |= VDF_INVALID;
434 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
438 vt_machine_kbdevent(int c)
443 if (vt_spclkeys & VT_DEBUG_KEY_ENABLED)
444 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
447 if (vt_spclkeys & VT_REBOOT_KEY_ENABLED)
448 /* XXX: Make this configurable! */
452 if (vt_spclkeys & VT_HALT_KEY_ENABLED)
453 shutdown_nice(RB_HALT);
456 if (vt_spclkeys & VT_POWEROFF_KEY_ENABLED)
457 shutdown_nice(RB_HALT|RB_POWEROFF);
465 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
467 struct vt_device *vd;
471 /* Only special keys handled in ScrollLock mode */
472 if ((c & SPCLKEY) == 0)
478 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
479 vw = vd->vd_windows[c - F_SCR];
481 vt_proc_window_switch(vw);
489 /* Turn scrolling off. */
490 vt_scroll(vw, 0, VHS_END);
491 VTBUF_SLCK_DISABLE(&vw->vw_buf);
492 vw->vw_flags &= ~VWF_SCROLL;
495 case FKEY | F(49): /* Home key. */
496 vt_scroll(vw, 0, VHS_SET);
498 case FKEY | F(50): /* Arrow up. */
499 vt_scroll(vw, -1, VHS_CUR);
501 case FKEY | F(51): /* Page up. */
502 vt_termsize(vd, vw->vw_font, &size);
503 vt_scroll(vw, -size.tp_row, VHS_CUR);
505 case FKEY | F(57): /* End key. */
506 vt_scroll(vw, 0, VHS_END);
508 case FKEY | F(58): /* Arrow down. */
509 vt_scroll(vw, 1, VHS_CUR);
511 case FKEY | F(59): /* Page down. */
512 vt_termsize(vd, vw->vw_font, &size);
513 vt_scroll(vw, size.tp_row, VHS_CUR);
522 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
524 struct vt_window *vw = vd->vd_curwindow;
527 #if VT_ALT_TO_ESC_HACK
529 switch (c & ~RELKEY) {
530 case (SPCLKEY | RALT):
531 if (vt_enable_altgr != 0)
533 case (SPCLKEY | LALT):
534 vd->vd_kbstate &= ~ALKED;
536 /* Other keys ignored for RELKEY event. */
539 switch (c & ~RELKEY) {
540 case (SPCLKEY | RALT):
541 if (vt_enable_altgr != 0)
543 case (SPCLKEY | LALT):
544 vd->vd_kbstate |= ALKED;
549 /* Other keys ignored for RELKEY event. */
553 if (vt_machine_kbdevent(c))
556 if (vw->vw_flags & VWF_SCROLL) {
557 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
558 /* Scroll mode keys handled, nothing to do more. */
565 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
566 vw = vd->vd_windows[c - F_SCR];
568 vt_proc_window_switch(vw);
575 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
578 /* Turn scrolling on. */
579 vw->vw_flags |= VWF_SCROLL;
580 VTBUF_SLCK_ENABLE(&vw->vw_buf);
582 /* Turn scrolling off. */
583 vw->vw_flags &= ~VWF_SCROLL;
584 VTBUF_SLCK_DISABLE(&vw->vw_buf);
585 vt_scroll(vw, 0, VHS_END);
590 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
591 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
592 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
593 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
594 /* F1 through F12 keys. */
595 terminal_input_special(vw->vw_terminal,
596 TKEY_F1 + c - (FKEY | F(1)));
598 case FKEY | F(49): /* Home key. */
599 terminal_input_special(vw->vw_terminal, TKEY_HOME);
601 case FKEY | F(50): /* Arrow up. */
602 terminal_input_special(vw->vw_terminal, TKEY_UP);
604 case FKEY | F(51): /* Page up. */
605 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
607 case FKEY | F(53): /* Arrow left. */
608 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
610 case FKEY | F(55): /* Arrow right. */
611 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
613 case FKEY | F(57): /* End key. */
614 terminal_input_special(vw->vw_terminal, TKEY_END);
616 case FKEY | F(58): /* Arrow down. */
617 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
619 case FKEY | F(59): /* Page down. */
620 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
622 case FKEY | F(60): /* Insert key. */
623 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
625 case FKEY | F(61): /* Delete key. */
626 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
629 } else if (KEYFLAGS(c) == 0) {
630 /* Don't do UTF-8 conversion when doing raw mode. */
631 if (vw->vw_kbdmode == K_XLATE) {
632 #if VT_ALT_TO_ESC_HACK
633 if (vd->vd_kbstate & ALKED) {
635 * Prepend ESC sequence if one of ALT keys down.
637 terminal_input_char(vw->vw_terminal, 0x1b);
641 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
643 terminal_input_raw(vw->vw_terminal, c);
649 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
651 struct vt_device *vd = arg;
657 case KBDIO_UNLOADING:
659 vd->vd_keyboard = -1;
660 kbd_release(kbd, (void *)vd);
667 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
668 vt_processkey(kbd, vd, c);
674 vt_allocate_keyboard(struct vt_device *vd)
680 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
681 vd->vd_keyboard = idx0;
683 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
684 k0 = kbd_get_keyboard(idx0);
686 for (idx = kbd_find_keyboard2("*", -1, 0);
688 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
689 k = kbd_get_keyboard(idx);
691 if (idx == idx0 || KBD_IS_BUSY(k))
694 bzero(&ki, sizeof(ki));
695 strcpy(ki.kb_name, k->kb_name);
696 ki.kb_unit = k->kb_unit;
698 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
701 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
702 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
704 DPRINTF(10, "%s: No keyboard found.\n", __func__);
708 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
714 vtterm_bell(struct terminal *tm)
716 struct vt_window *vw = tm->tm_softc;
717 struct vt_device *vd = vw->vw_device;
719 if (vd->vd_flags & VDF_QUIET_BELL)
722 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
726 vtterm_beep(struct terminal *tm, u_int param)
730 if ((param == 0) || ((param & 0xffff) == 0)) {
735 period = ((param >> 16) & 0xffff) * hz / 1000;
736 freq = 1193182 / (param & 0xffff);
738 sysbeep(freq, period);
742 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
744 struct vt_window *vw = tm->tm_softc;
746 vtbuf_cursor_position(&vw->vw_buf, p);
750 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
752 struct vt_window *vw = tm->tm_softc;
754 vtbuf_putchar(&vw->vw_buf, p, c);
758 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
760 struct vt_window *vw = tm->tm_softc;
762 vtbuf_fill_locked(&vw->vw_buf, r, c);
766 vtterm_copy(struct terminal *tm, const term_rect_t *r,
769 struct vt_window *vw = tm->tm_softc;
771 vtbuf_copy(&vw->vw_buf, r, p);
775 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
777 struct vt_window *vw = tm->tm_softc;
781 vtbuf_cursor_visibility(&vw->vw_buf, arg);
784 vw->vw_mouse_level = arg;
790 vt_determine_colors(term_char_t c, int cursor,
791 term_color_t *fg, term_color_t *bg)
798 *fg = TCHAR_FGCOLOR(c);
799 if (TCHAR_FORMAT(c) & TF_BOLD)
800 *fg = TCOLOR_LIGHT(*fg);
801 *bg = TCHAR_BGCOLOR(c);
803 if (TCHAR_FORMAT(c) & TF_REVERSE)
816 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
817 int iscursor, unsigned int row, unsigned int col)
821 vt_determine_colors(c, iscursor, &fg, &bg);
827 src = vtfont_lookup(vf, c);
830 * Align the terminal to the centre of the screen.
831 * Fonts may not always be able to fill the entire
834 top = row * vf->vf_height + vd->vd_offset.tp_row;
835 left = col * vf->vf_width + vd->vd_offset.tp_col;
837 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
838 vf->vf_width, vf->vf_height, fg, bg);
840 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
846 vt_flush(struct vt_device *vd)
848 struct vt_window *vw;
850 struct vt_bufmask tmask;
851 unsigned int row, col;
855 #ifndef SC_NO_CUTPASTE
856 struct vt_mouse_cursor *cursor;
860 vw = vd->vd_curwindow;
864 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
867 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
870 #ifndef SC_NO_CUTPASTE
872 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
873 !(vw->vw_flags & VWF_MOUSE_HIDE)) { /* Cursor displayed. */
874 if (vd->vd_moldx != vd->vd_mx ||
875 vd->vd_moldy != vd->vd_my) {
877 * Mark last mouse position as dirty to erase.
879 * FIXME: The font size could be different among
880 * all windows, so the column/row calculation
881 * below isn't correct for all windows.
883 * FIXME: The cursor can span more than one
884 * character cell. vtbuf_mouse_cursor_position
885 * marks surrounding cells as dirty. But due
886 * to font size possibly inconsistent across
887 * windows, this may not be sufficient. This
888 * causes part of the cursor to not be erased.
890 * FIXME: The vt_buf lock is acquired twice in a
893 vtbuf_mouse_cursor_position(&vw->vw_buf,
894 vd->vd_moldx / vf->vf_width,
895 vd->vd_moldy / vf->vf_height);
896 vtbuf_mouse_cursor_position(&vw->vw_buf,
897 vd->vd_mx / vf->vf_width,
898 vd->vd_my / vf->vf_height);
901 * Save point of last mouse cursor to erase it
904 vd->vd_moldx = vd->vd_mx;
905 vd->vd_moldy = vd->vd_my;
908 if (!kdb_active && panicstr == NULL) {
909 /* Mouse enabled, and DDB isn't active. */
910 cursor = &vt_default_mouse_pointer;
915 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
916 vt_termsize(vd, vf, &size);
918 /* Force a full redraw when the screen contents are invalid. */
919 if (vd->vd_flags & VDF_INVALID) {
920 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
922 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
924 vd->vd_flags &= ~VDF_INVALID;
927 if (vd->vd_driver->vd_bitblt_text != NULL) {
928 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
929 vd->vd_driver->vd_bitblt_text(vd, &vw->vw_buf, vf, &tarea
930 #ifndef SC_NO_CUTPASTE
931 , cursor, TC_WHITE, TC_BLACK
937 * FIXME: Once all backend drivers expose the
938 * vd_bitblt_text_t callback, this code can be removed.
940 for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
941 if (!VTBUF_DIRTYROW(&tmask, row))
943 r = VTBUF_GET_ROW(&vw->vw_buf, row);
944 for (col = tarea.tr_begin.tp_col;
945 col < tarea.tr_end.tp_col; col++) {
946 if (!VTBUF_DIRTYCOL(&tmask, col))
949 vt_bitblt_char(vd, vf, r[col],
950 VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
954 #ifndef SC_NO_CUTPASTE
955 if (cursor != NULL) {
956 bpl = (cursor->width + 7) >> 3; /* Bytes per source line. */
960 if ((vd->vd_mx + cursor->width) >
961 (size.tp_col * vf->vf_width))
962 w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
963 if ((vd->vd_my + cursor->height) >
964 (size.tp_row * vf->vf_height))
965 h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
967 vd->vd_driver->vd_bitbltchr(vd, cursor->map, cursor->mask, bpl,
968 vd->vd_offset.tp_row + vd->vd_my,
969 vd->vd_offset.tp_col + vd->vd_mx,
970 w, h, TC_WHITE, TC_BLACK);
979 struct vt_device *vd;
982 /* Update screen if required. */
985 /* Schedule for next update. */
986 vt_schedule_flush(vd, 0);
990 vtterm_done(struct terminal *tm)
992 struct vt_window *vw = tm->tm_softc;
993 struct vt_device *vd = vw->vw_device;
995 if (kdb_active || panicstr != NULL) {
996 /* Switch to the debugger. */
997 if (vd->vd_curwindow != vw) {
998 vd->vd_curwindow = vw;
999 vd->vd_flags |= VDF_INVALID;
1000 if (vd->vd_driver->vd_postswitch)
1001 vd->vd_driver->vd_postswitch(vd);
1003 vd->vd_flags &= ~VDF_SPLASH;
1005 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1012 vtterm_splash(struct vt_device *vd)
1014 vt_axis_t top, left;
1016 /* Display a nice boot splash. */
1017 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1019 top = (vd->vd_height - vt_logo_height) / 2;
1020 left = (vd->vd_width - vt_logo_width) / 2;
1021 switch (vt_logo_depth) {
1023 /* XXX: Unhardcode colors! */
1024 vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
1025 top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
1027 vd->vd_flags |= VDF_SPLASH;
1034 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1036 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1037 struct vt_window *vw = tm->tm_softc;
1038 struct vt_device *vd = vw->vw_device;
1043 if (!vty_enabled(VTY_VT))
1046 if (vd->vd_flags & VDF_INITIALIZED)
1047 /* Initialization already done. */
1050 SET_FOREACH(vtdlist, vt_drv_set) {
1052 if (vtd->vd_probe == NULL)
1054 if (vtd->vd_probe(vd) == CN_DEAD)
1056 if ((vtdbest == NULL) ||
1057 (vtd->vd_priority > vtdbest->vd_priority))
1060 if (vtdbest == NULL) {
1061 cp->cn_pri = CN_DEAD;
1062 vd->vd_flags |= VDF_DEAD;
1064 vd->vd_driver = vtdbest;
1065 cp->cn_pri = vd->vd_driver->vd_init(vd);
1068 /* Check if driver's vt_init return CN_DEAD. */
1069 if (cp->cn_pri == CN_DEAD) {
1070 vd->vd_flags |= VDF_DEAD;
1073 /* Initialize any early-boot keyboard drivers */
1074 kbd_configure(KB_CONF_PROBE_ONLY);
1076 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1077 vd->vd_windows[VT_CONSWINDOW] = vw;
1078 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1080 /* Attach default font if not in TEXTMODE. */
1081 if ((vd->vd_flags & VDF_TEXTMODE) == 0)
1082 vw->vw_font = vtfont_ref(&vt_font_default);
1084 vtbuf_init_early(&vw->vw_buf);
1085 vt_winsize(vd, vw->vw_font, &wsz);
1086 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1088 attr.ta_format = TCHAR_FORMAT(c);
1089 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1090 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1091 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1093 if (vtdbest != NULL) {
1097 vd->vd_flags |= VDF_INITIALIZED;
1102 vtterm_cngetc(struct terminal *tm)
1104 struct vt_window *vw = tm->tm_softc;
1105 struct vt_device *vd = vw->vw_device;
1110 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1111 return (*vw->vw_kbdsq++);
1114 /* Make sure the splash screen is not there. */
1115 if (vd->vd_flags & VDF_SPLASH) {
1117 vd->vd_flags &= ~VDF_SPLASH;
1118 /* Mark screen as invalid to force update */
1119 vd->vd_flags |= VDF_INVALID;
1123 /* Stripped down keyboard handler. */
1124 kbd = kbd_get_keyboard(vd->vd_keyboard);
1128 /* Force keyboard input mode to K_XLATE */
1130 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1132 /* Switch the keyboard to polling to make it work here. */
1133 kbdd_poll(kbd, TRUE);
1134 c = kbdd_read_char(kbd, 0);
1135 kbdd_poll(kbd, FALSE);
1139 if (vw->vw_flags & VWF_SCROLL) {
1140 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1145 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1149 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1150 if (state & SLKED) {
1151 /* Turn scrolling on. */
1152 vw->vw_flags |= VWF_SCROLL;
1153 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1155 /* Turn scrolling off. */
1156 vt_scroll(vw, 0, VHS_END);
1157 vw->vw_flags &= ~VWF_SCROLL;
1158 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1161 /* XXX: KDB can handle history. */
1162 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1163 vw->vw_kbdsq = "\x1b[A";
1165 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1166 vw->vw_kbdsq = "\x1b[B";
1168 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1169 vw->vw_kbdsq = "\x1b[C";
1171 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1172 vw->vw_kbdsq = "\x1b[D";
1176 /* Force refresh to make scrollback work. */
1178 } else if (KEYFLAGS(c) == 0) {
1179 return (KEYCHAR(c));
1182 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1183 return (*vw->vw_kbdsq++);
1189 vtterm_opened(struct terminal *tm, int opened)
1191 struct vt_window *vw = tm->tm_softc;
1192 struct vt_device *vd = vw->vw_device;
1195 vd->vd_flags &= ~VDF_SPLASH;
1197 vw->vw_flags |= VWF_OPENED;
1199 vw->vw_flags &= ~VWF_OPENED;
1200 /* TODO: finish ACQ/REL */
1206 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1208 struct vt_device *vd = vw->vw_device;
1209 struct terminal *tm = vw->vw_terminal;
1216 * Changing fonts is a little tricky. We must prevent
1217 * simultaneous access to the device, so we must stop
1218 * the display timer and the terminal from accessing.
1219 * We need to switch fonts and grow our screen buffer.
1221 * XXX: Right now the code uses terminal_mute() to
1222 * prevent data from reaching the console driver while
1223 * resizing the screen buffer. This isn't elegant...
1227 if (vw->vw_flags & VWF_BUSY) {
1228 /* Another process is changing the font. */
1232 if (vd->vd_flags & VDF_TEXTMODE) {
1233 /* Our device doesn't need fonts. */
1237 vw->vw_flags |= VWF_BUSY;
1240 vt_termsize(vd, vf, &size);
1241 vt_winsize(vd, vf, &wsz);
1242 /* Save offset to font aligned area. */
1243 vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1244 vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1246 /* Grow the screen buffer and terminal. */
1247 terminal_mute(tm, 1);
1248 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1249 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1250 terminal_mute(tm, 0);
1252 /* Actually apply the font to the current window. */
1254 if (vw->vw_font != vf) {
1256 * In case vt_change_font called to update size we don't need
1257 * to update font link.
1259 vtfont_unref(vw->vw_font);
1260 vw->vw_font = vtfont_ref(vf);
1263 /* Force a full redraw the next timer tick. */
1264 if (vd->vd_curwindow == vw)
1265 vd->vd_flags |= VDF_INVALID;
1266 vw->vw_flags &= ~VWF_BUSY;
1272 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1274 struct vt_device *vd = vw->vw_device;
1275 int x, y, off_x, off_y;
1277 if (vd->vd_driver->vd_drawrect == NULL)
1280 x = vd->vd_width - 1;
1281 y = vd->vd_height - 1;
1282 off_x = vd->vd_offset.tp_col;
1283 off_y = vd->vd_offset.tp_row;
1287 vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c);
1290 vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y,
1292 /* Right bar. May be 1 pixel wider than necessary due to rounding. */
1293 vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c);
1294 /* Bottom bar. May be 1 mixel taller than necessary due to rounding. */
1295 vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c);
1301 vt_proc_alive(struct vt_window *vw)
1305 if (vw->vw_smode.mode != VT_PROCESS)
1309 if ((p = pfind(vw->vw_pid)) != NULL)
1311 if (vw->vw_proc == p)
1314 vw->vw_smode.mode = VT_AUTO;
1315 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1322 signal_vt_rel(struct vt_window *vw)
1325 if (vw->vw_smode.mode != VT_PROCESS)
1327 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1332 vw->vw_flags |= VWF_SWWAIT_REL;
1333 PROC_LOCK(vw->vw_proc);
1334 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1335 PROC_UNLOCK(vw->vw_proc);
1336 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1341 signal_vt_acq(struct vt_window *vw)
1344 if (vw->vw_smode.mode != VT_PROCESS)
1346 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1347 cnavailable(vw->vw_terminal->consdev, FALSE);
1348 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1353 vw->vw_flags |= VWF_SWWAIT_ACQ;
1354 PROC_LOCK(vw->vw_proc);
1355 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1356 PROC_UNLOCK(vw->vw_proc);
1357 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1362 finish_vt_rel(struct vt_window *vw, int release, int *s)
1365 if (vw->vw_flags & VWF_SWWAIT_REL) {
1366 vw->vw_flags &= ~VWF_SWWAIT_REL;
1368 callout_drain(&vw->vw_proc_dead_timer);
1369 vt_late_window_switch(vw->vw_switch_to);
1377 finish_vt_acq(struct vt_window *vw)
1380 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1381 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1387 #ifndef SC_NO_CUTPASTE
1389 vt_mouse_terminput_button(struct vt_device *vd, int button)
1391 struct vt_window *vw;
1393 char mouseb[6] = "\x1B[M";
1396 vw = vd->vd_curwindow;
1399 /* Translate to char position. */
1400 x = vd->vd_mx / vf->vf_width;
1401 y = vd->vd_my / vf->vf_height;
1402 /* Avoid overflow. */
1403 x = MIN(x, 255 - '!');
1404 y = MIN(y, 255 - '!');
1406 mouseb[3] = ' ' + button;
1407 mouseb[4] = '!' + x;
1408 mouseb[5] = '!' + y;
1410 for (i = 0; i < sizeof(mouseb); i++ )
1411 terminal_input_char(vw->vw_terminal, mouseb[i]);
1415 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1420 case MOUSE_BUTTON_EVENT:
1422 /* Mouse button pressed. */
1423 if (event & MOUSE_BUTTON1DOWN)
1424 vt_mouse_terminput_button(vd, 0);
1425 if (event & MOUSE_BUTTON2DOWN)
1426 vt_mouse_terminput_button(vd, 1);
1427 if (event & MOUSE_BUTTON3DOWN)
1428 vt_mouse_terminput_button(vd, 2);
1430 /* Mouse button released. */
1431 vt_mouse_terminput_button(vd, 3);
1435 case MOUSE_MOTION_EVENT:
1436 if (mouse->u.data.z < 0) {
1438 sc_mouse_input_button(vd, 64);
1439 } else if (mouse->u.data.z > 0) {
1441 sc_mouse_input_button(vd, 65);
1449 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1451 struct vt_device *vd;
1452 struct vt_window *vw;
1459 vw = vd->vd_curwindow;
1463 if (vw->vw_flags & VWF_MOUSE_HIDE)
1464 return; /* Mouse disabled. */
1466 if (vf == NULL) /* Text mode. */
1470 * TODO: add flag about pointer position changed, to not redraw chars
1471 * under mouse pointer when nothing changed.
1474 if (vw->vw_mouse_level > 0)
1475 vt_mouse_terminput(vd, type, x, y, event, cnt);
1479 case MOUSE_MOTION_EVENT:
1484 vt_termsize(vd, vf, &size);
1489 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1490 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1494 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1495 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1496 vd->vd_mx / vf->vf_width,
1497 vd->vd_my / vf->vf_height) == 1)) {
1500 * We have something marked to copy, so update pointer
1501 * to window with selection.
1503 vd->vd_markedwin = vw;
1506 case MOUSE_BUTTON_EVENT:
1514 case MOUSE_BUTTON1DOWN:
1517 mark = VTB_MARK_END;
1519 case 1: /* single click: start cut operation */
1520 mark = VTB_MARK_START;
1522 case 2: /* double click: cut a word */
1523 mark = VTB_MARK_WORD;
1525 case 3: /* triple click: cut a line */
1526 mark = VTB_MARK_ROW;
1530 case VT_MOUSE_PASTEBUTTON:
1535 if (vd->vd_markedwin == NULL)
1537 /* Get current selecton size in bytes. */
1538 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1542 buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1543 /* Request cupy/paste buffer data, no more than `len' */
1544 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1547 len /= sizeof(term_char_t);
1548 for (i = 0; i < len; i++ ) {
1551 terminal_input_char(vw->vw_terminal, buf[i]);
1554 /* Done, so cleanup. */
1559 case VT_MOUSE_EXTENDBUTTON:
1562 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1563 mark = VTB_MARK_EXTEND;
1568 mark = VTB_MARK_EXTEND;
1576 /* Save buttons state. */
1578 vd->vd_mstate |= event;
1580 vd->vd_mstate &= ~event;
1582 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1583 vd->vd_my / vf->vf_height) == 1) {
1585 * We have something marked to copy, so update pointer to
1586 * window with selection.
1588 vd->vd_markedwin = vw;
1593 vt_mouse_state(int show)
1595 struct vt_device *vd;
1596 struct vt_window *vw;
1599 vw = vd->vd_curwindow;
1603 vw->vw_flags |= VWF_MOUSE_HIDE;
1606 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1611 * Mark mouse position as dirty.
1613 * FIXME: See comments in vt_flush().
1615 vtbuf_mouse_cursor_position(&vw->vw_buf,
1616 vd->vd_mx / vw->vw_font->vf_width,
1617 vd->vd_my / vw->vw_font->vf_height);
1622 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1623 int nprot, vm_memattr_t *memattr)
1625 struct vt_window *vw = tm->tm_softc;
1626 struct vt_device *vd = vw->vw_device;
1628 if (vd->vd_driver->vd_fb_mmap)
1629 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1636 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1639 struct vt_window *vw = tm->tm_softc;
1640 struct vt_device *vd = vw->vw_device;
1643 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1644 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1655 cmd = VT_WAITACTIVE;
1681 ival = IOCPARM_IVAL(data);
1682 data = (caddr_t)&ival;
1687 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1688 if (*(int *)data & ~0x7f)
1692 case GIO_DEADKEYMAP:
1693 case PIO_DEADKEYMAP:
1698 case KDSKBSTATE: /* set keyboard state (locks) */
1699 case KDGKBSTATE: /* get keyboard state (locks) */
1700 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1701 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1702 case KDSETLED: /* set keyboard LED status */
1703 case KDGETLED: /* get keyboard LED status */
1704 case KBADDKBD: /* add/remove keyboard to/from mux */
1709 kbd = kbd_get_keyboard(vd->vd_keyboard);
1711 error = kbdd_ioctl(kbd, cmd, data);
1713 if (error == ENOIOCTL) {
1714 if (cmd == KDGKBTYPE) {
1715 /* always return something? XXX */
1727 kbd = kbd_get_keyboard(vd->vd_keyboard);
1729 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1732 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1733 *(int *)data = mode;
1739 mode = *(int *)data;
1744 vw->vw_kbdmode = mode;
1745 if (vw == vd->vd_curwindow) {
1750 kbd = kbd_get_keyboard(vd->vd_keyboard);
1752 error = kbdd_ioctl(kbd, KDSKBMODE,
1763 case FBIO_GETWINORG: /* get frame buffer window origin */
1764 case FBIO_GETDISPSTART: /* get display start address */
1765 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1766 case FBIO_BLANK: /* blank display */
1767 if (vd->vd_driver->vd_fb_ioctl)
1768 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1770 case CONS_BLANKTIME:
1775 *(int *)data = M_CG640x480;
1777 case CONS_BELLTYPE: /* set bell type sound */
1778 if ((*(int *)data) & CONS_QUIET_BELL)
1779 vd->vd_flags |= VDF_QUIET_BELL;
1781 vd->vd_flags &= ~VDF_QUIET_BELL;
1783 case CONS_GETINFO: {
1784 vid_info_t *vi = (vid_info_t *)data;
1786 vi->m_num = vd->vd_curwindow->vw_number + 1;
1787 /* XXX: other fields! */
1791 *(int *)data = 0x200;
1796 case CONS_MOUSECTL: {
1797 mouse_info_t *mouse = (mouse_info_t*)data;
1800 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
1801 * should not be applied to individual TTYs, but only to
1804 switch (mouse->operation) {
1806 if (vd->vd_flags & VDF_MOUSECURSOR) {
1807 vd->vd_flags &= ~VDF_MOUSECURSOR;
1808 #ifndef SC_NO_CUTPASTE
1809 vt_mouse_state(VT_MOUSE_HIDE);
1814 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
1815 vd->vd_flags |= VDF_MOUSECURSOR;
1816 vd->vd_mx = vd->vd_width / 2;
1817 vd->vd_my = vd->vd_height / 2;
1818 #ifndef SC_NO_CUTPASTE
1819 vt_mouse_state(VT_MOUSE_SHOW);
1830 error = vtfont_load((void *)data, &vf);
1834 error = vt_change_font(vw, vf);
1836 /* XXX: replace 0 with current bg color. */
1837 vt_set_border(vw, vf, 0);
1843 scrmap_t *sm = (scrmap_t *)data;
1845 /* We don't have screen maps, so return a handcrafted one. */
1846 for (i = 0; i < 256; i++)
1853 case KDENABIO: /* allow io operations */
1854 error = priv_check(td, PRIV_IO);
1857 error = securelevel_gt(td->td_ucred, 0);
1860 #if defined(__i386__)
1861 td->td_frame->tf_eflags |= PSL_IOPL;
1862 #elif defined(__amd64__)
1863 td->td_frame->tf_rflags |= PSL_IOPL;
1866 case KDDISABIO: /* disallow io operations (default) */
1867 #if defined(__i386__)
1868 td->td_frame->tf_eflags &= ~PSL_IOPL;
1869 #elif defined(__amd64__)
1870 td->td_frame->tf_rflags &= ~PSL_IOPL;
1873 case KDMKTONE: /* sound the bell */
1874 vtterm_beep(tm, *(u_int *)data);
1876 case KIOCSOUND: /* make tone (*data) hz */
1879 case CONS_SETKBD: /* set the new keyboard */
1882 if (vd->vd_keyboard != *(int *)data) {
1883 kbd = kbd_get_keyboard(*(int *)data);
1888 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1889 (void *)vd, vt_kbdevent, vd);
1891 if (vd->vd_keyboard != -1) {
1892 kbd_release(kbd, (void *)vd);
1894 kbd = kbd_get_keyboard(i);
1895 vd->vd_keyboard = i;
1897 (void)kbdd_ioctl(kbd, KDSKBMODE,
1898 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1900 error = EPERM; /* XXX */
1905 case CONS_RELKBD: /* release the current keyboard */
1908 if (vd->vd_keyboard != -1) {
1909 kbd = kbd_get_keyboard(vd->vd_keyboard);
1914 error = kbd_release(kbd, (void *)vd);
1916 vd->vd_keyboard = -1;
1923 win = *(int *)data - 1;
1924 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1926 if ((win > VT_MAXWINDOWS) || (win < 0))
1928 return (vt_proc_window_switch(vd->vd_windows[win]));
1931 *(int *)data = vd->vd_curwindow->vw_number + 1;
1934 *(int *)data = vw->vw_number + 1;
1937 /* TODO: Check current state, switching can be in progress. */
1938 if ((*(int *)data) == 0x01)
1939 vw->vw_flags |= VWF_VTYLOCK;
1940 else if ((*(int *)data) == 0x02)
1941 vw->vw_flags &= ~VWF_VTYLOCK;
1947 for (i = 0; i < VT_MAXWINDOWS; i++) {
1948 vw = vd->vd_windows[i];
1951 if (!(vw->vw_flags & VWF_OPENED)) {
1952 *(int *)data = vw->vw_number + 1;
1962 i = *(unsigned int *)data;
1963 if (i > VT_MAXWINDOWS)
1966 vw = vd->vd_windows[i - 1];
1969 while (vd->vd_curwindow != vw && error == 0)
1970 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1973 case VT_SETMODE: { /* set screen switcher mode */
1974 struct vt_mode *mode;
1977 mode = (struct vt_mode *)data;
1978 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1979 if (vw->vw_smode.mode == VT_PROCESS) {
1980 p1 = pfind(vw->vw_pid);
1981 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1984 DPRINTF(5, "error EPERM\n");
1990 if (mode->mode == VT_AUTO) {
1991 vw->vw_smode.mode = VT_AUTO;
1994 DPRINTF(5, "VT_AUTO, ");
1995 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1996 cnavailable(vw->vw_terminal->consdev, TRUE);
1997 /* were we in the middle of the vty switching process? */
1998 if (finish_vt_rel(vw, TRUE, &s) == 0)
1999 DPRINTF(5, "reset WAIT_REL, ");
2000 if (finish_vt_acq(vw) == 0)
2001 DPRINTF(5, "reset WAIT_ACQ, ");
2003 } else if (mode->mode == VT_PROCESS) {
2004 if (!ISSIGVALID(mode->relsig) ||
2005 !ISSIGVALID(mode->acqsig) ||
2006 !ISSIGVALID(mode->frsig)) {
2007 DPRINTF(5, "error EINVAL\n");
2010 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2011 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2012 vw->vw_proc = td->td_proc;
2013 vw->vw_pid = vw->vw_proc->p_pid;
2014 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2015 cnavailable(vw->vw_terminal->consdev, FALSE);
2017 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2024 case VT_GETMODE: /* get screen switcher mode */
2025 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2028 case VT_RELDISP: /* screen switcher ioctl */
2030 * This must be the current vty which is in the VT_PROCESS
2033 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2037 /* ...and this process is controlling it. */
2038 if (vw->vw_proc != td->td_proc) {
2042 switch(*(int *)data) {
2043 case VT_FALSE: /* user refuses to release screen, abort */
2044 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2045 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2046 SC_DRIVER_NAME, VT_UNIT(vw));
2048 case VT_TRUE: /* user has released screen, go on */
2049 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2050 if (vw->vw_flags & VWF_SWWAIT_REL) {
2051 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2052 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2053 SC_DRIVER_NAME, VT_UNIT(vw));
2058 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2059 if ((error = finish_vt_acq(vw)) == 0)
2060 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2061 SC_DRIVER_NAME, VT_UNIT(vw));
2072 static struct vt_window *
2073 vt_allocate_window(struct vt_device *vd, unsigned int window)
2075 struct vt_window *vw;
2076 struct terminal *tm;
2080 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2082 vw->vw_number = window;
2083 vw->vw_kbdmode = K_XLATE;
2085 if ((vd->vd_flags & VDF_TEXTMODE) == 0)
2086 vw->vw_font = vtfont_ref(&vt_font_default);
2088 vt_termsize(vd, vw->vw_font, &size);
2089 vt_winsize(vd, vw->vw_font, &wsz);
2090 vtbuf_init(&vw->vw_buf, &size);
2092 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2093 terminal_set_winsize(tm, &wsz);
2094 vd->vd_windows[window] = vw;
2095 callout_init(&vw->vw_proc_dead_timer, 0);
2101 vt_upgrade(struct vt_device *vd)
2103 struct vt_window *vw;
2106 if (!vty_enabled(VTY_VT))
2109 for (i = 0; i < VT_MAXWINDOWS; i++) {
2110 vw = vd->vd_windows[i];
2113 vw = vt_allocate_window(vd, i);
2115 if (!(vw->vw_flags & VWF_READY)) {
2116 callout_init(&vw->vw_proc_dead_timer, 0);
2117 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2118 vw->vw_flags |= VWF_READY;
2119 if (vw->vw_flags & VWF_CONSOLE) {
2120 /* For existing console window. */
2121 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2122 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2128 if (vd->vd_curwindow == NULL)
2129 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2131 if (!(vd->vd_flags & VDF_ASYNC)) {
2132 /* Attach keyboard. */
2133 vt_allocate_keyboard(vd);
2135 /* Init 25 Hz timer. */
2136 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2138 /* Start timer when everything ready. */
2139 vd->vd_flags |= VDF_ASYNC;
2140 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2141 vd->vd_timer_armed = 1;
2146 /* Refill settings with new sizes. */
2151 vt_resize(struct vt_device *vd)
2153 struct vt_window *vw;
2156 for (i = 0; i < VT_MAXWINDOWS; i++) {
2157 vw = vd->vd_windows[i];
2159 /* Assign default font to window, if not textmode. */
2160 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2161 vw->vw_font = vtfont_ref(&vt_font_default);
2163 /* Resize terminal windows */
2164 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2165 DPRINTF(100, "%s: vt_change_font() is busy, "
2166 "window %d\n", __func__, i);
2172 vt_allocate(struct vt_driver *drv, void *softc)
2174 struct vt_device *vd;
2176 if (!vty_enabled(VTY_VT))
2179 if (main_vd->vd_driver == NULL) {
2180 main_vd->vd_driver = drv;
2181 printf("VT: initialize with new VT driver \"%s\".\n",
2185 * Check if have rights to replace current driver. For example:
2186 * it is bad idea to replace KMS driver with generic VGA one.
2188 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2189 printf("VT: Driver priority %d too low. Current %d\n ",
2190 drv->vd_priority, main_vd->vd_driver->vd_priority);
2193 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2194 main_vd->vd_driver->vd_name, drv->vd_name);
2199 if (vd->vd_flags & VDF_ASYNC) {
2200 /* Stop vt_flush periodic task. */
2201 vt_suspend_flush_timer(vd);
2203 * Mute current terminal until we done. vt_change_font (called
2204 * from vt_resize) will unmute it.
2206 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2210 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2213 vd->vd_flags &= ~VDF_TEXTMODE;
2215 vd->vd_driver = drv;
2216 vd->vd_softc = softc;
2217 vd->vd_driver->vd_init(vd);
2220 /* Update windows sizes and initialize last items. */
2224 if (vd->vd_flags & VDF_SPLASH)
2228 if (vd->vd_flags & VDF_ASYNC) {
2229 /* Allow to put chars now. */
2230 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2231 /* Rerun timer for screen updates. */
2232 vt_resume_flush_timer(vd, 0);
2236 * Register as console. If it already registered, cnadd() will ignore
2239 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2246 if (vt_suspendswitch == 0)
2248 /* Save current window. */
2249 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2250 /* Ask holding process to free window and switch to console window */
2251 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2258 if (vt_suspendswitch == 0)
2260 /* Switch back to saved window */
2261 if (main_vd->vd_savedwindow != NULL)
2262 vt_proc_window_switch(main_vd->vd_savedwindow);
2263 main_vd->vd_savedwindow = NULL;