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 #ifndef SC_NO_CUTPASTE
167 .vd_mcursor = &vt_default_mouse_pointer,
168 .vd_mcursor_fg = TC_WHITE,
169 .vd_mcursor_bg = TC_BLACK,
172 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
173 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
174 static struct vt_window vt_conswindow = {
175 .vw_number = VT_CONSWINDOW,
176 .vw_flags = VWF_CONSOLE,
178 .vb_buffer = &vt_constextbuf[0],
179 .vb_rows = &vt_constextbufrows[0],
180 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
183 .vb_flags = VBF_STATIC,
184 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
185 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
191 .vw_device = &vt_consdev,
192 .vw_terminal = &vt_consterm,
193 .vw_kbdmode = K_XLATE,
195 static struct terminal vt_consterm = {
196 .tm_class = &vt_termclass,
197 .tm_softc = &vt_conswindow,
200 static struct consdev vt_consterm_consdev = {
201 .cn_ops = &termcn_cnops,
202 .cn_arg = &vt_consterm,
206 /* Add to set of consoles. */
207 DATA_SET(cons_set, vt_consterm_consdev);
210 * Right after kmem is done to allow early drivers to use locking and allocate
213 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
215 /* Delay until all devices attached, to not waste time. */
216 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
219 /* Initialize locks/mem depended members. */
221 vt_update_static(void *dummy)
224 if (!vty_enabled(VTY_VT))
226 if (main_vd->vd_driver != NULL)
227 printf("VT: running with driver \"%s\".\n",
228 main_vd->vd_driver->vd_name);
230 printf("VT: init without driver.\n");
232 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
233 cv_init(&main_vd->vd_winswitch, "vtwswt");
237 vt_schedule_flush(struct vt_device *vd, int ms)
241 /* Default to initial value. */
242 ms = 1000 / VT_TIMERFREQ;
244 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
248 vt_resume_flush_timer(struct vt_device *vd, int ms)
251 if (!atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
254 vt_schedule_flush(vd, ms);
258 vt_suspend_flush_timer(struct vt_device *vd)
261 if (!atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
264 callout_drain(&vd->vd_timer);
268 vt_switch_timer(void *arg)
271 vt_late_window_switch((struct vt_window *)arg);
275 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
278 DPRINTF(40, "%s\n", __func__);
279 curvw->vw_switch_to = vw;
280 /* Set timer to allow switch in case when process hang. */
281 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
282 vt_switch_timer, (void *)vw);
283 /* Notify process about vt switch attempt. */
284 DPRINTF(30, "%s: Notify process.\n", __func__);
285 signal_vt_rel(curvw);
291 vt_window_postswitch(struct vt_window *vw)
298 /* vt_late_window_switch will done VT switching for regular case. */
300 vt_late_window_switch(struct vt_window *vw)
304 callout_stop(&vw->vw_proc_dead_timer);
306 ret = vt_window_switch(vw);
310 /* Notify owner process about terminal availability. */
311 if (vw->vw_smode.mode == VT_PROCESS) {
312 ret = vt_window_postswitch(vw);
319 vt_proc_window_switch(struct vt_window *vw)
321 struct vt_window *curvw;
322 struct vt_device *vd;
326 curvw = vd->vd_curwindow;
328 if (curvw->vw_flags & VWF_VTYLOCK)
331 /* Ask current process permitions to switch away. */
332 if (curvw->vw_smode.mode == VT_PROCESS) {
333 DPRINTF(30, "%s: VT_PROCESS ", __func__);
334 if (vt_proc_alive(curvw) == FALSE) {
335 DPRINTF(30, "Dead. Cleaning.");
338 DPRINTF(30, "%s: Signaling process.\n", __func__);
339 /* Alive, try to ask him. */
340 ret = vt_window_preswitch(vw, curvw);
341 /* Wait for process answer or timeout. */
347 ret = vt_late_window_switch(vw);
351 /* Switch window ignoring process locking. */
353 vt_window_switch(struct vt_window *vw)
355 struct vt_device *vd = vw->vw_device;
356 struct vt_window *curvw = vd->vd_curwindow;
365 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
370 vt_suspend_flush_timer(vd);
372 vd->vd_curwindow = vw;
373 vd->vd_flags |= VDF_INVALID;
374 cv_broadcast(&vd->vd_winswitch);
377 if (vd->vd_driver->vd_postswitch)
378 vd->vd_driver->vd_postswitch(vd);
380 vt_resume_flush_timer(vd, 0);
382 /* Restore per-window keyboard mode. */
384 kbd = kbd_get_keyboard(vd->vd_keyboard);
386 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
389 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
395 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
398 size->tp_row = vd->vd_height;
399 size->tp_col = vd->vd_width;
401 size->tp_row /= vf->vf_height;
402 size->tp_col /= vf->vf_width;
407 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
410 size->ws_row = size->ws_ypixel = vd->vd_height;
411 size->ws_col = size->ws_xpixel = vd->vd_width;
413 size->ws_row /= vf->vf_height;
414 size->ws_col /= vf->vf_width;
419 vt_scroll(struct vt_window *vw, int offset, int whence)
424 if ((vw->vw_flags & VWF_SCROLL) == 0)
427 vt_termsize(vw->vw_device, vw->vw_font, &size);
429 diff = vthistory_seek(&vw->vw_buf, offset, whence);
431 * Offset changed, please update Nth lines on screen.
432 * +N - Nth lines at top;
433 * -N - Nth lines at bottom.
436 if (diff < -size.tp_row || diff > size.tp_row) {
437 vw->vw_device->vd_flags |= VDF_INVALID;
440 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
444 vt_machine_kbdevent(int c)
449 if (vt_spclkeys & VT_DEBUG_KEY_ENABLED)
450 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
453 if (vt_spclkeys & VT_REBOOT_KEY_ENABLED)
454 /* XXX: Make this configurable! */
458 if (vt_spclkeys & VT_HALT_KEY_ENABLED)
459 shutdown_nice(RB_HALT);
462 if (vt_spclkeys & VT_POWEROFF_KEY_ENABLED)
463 shutdown_nice(RB_HALT|RB_POWEROFF);
471 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
473 struct vt_device *vd;
477 /* Only special keys handled in ScrollLock mode */
478 if ((c & SPCLKEY) == 0)
484 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
485 vw = vd->vd_windows[c - F_SCR];
487 vt_proc_window_switch(vw);
495 /* Turn scrolling off. */
496 vt_scroll(vw, 0, VHS_END);
497 VTBUF_SLCK_DISABLE(&vw->vw_buf);
498 vw->vw_flags &= ~VWF_SCROLL;
501 case FKEY | F(49): /* Home key. */
502 vt_scroll(vw, 0, VHS_SET);
504 case FKEY | F(50): /* Arrow up. */
505 vt_scroll(vw, -1, VHS_CUR);
507 case FKEY | F(51): /* Page up. */
508 vt_termsize(vd, vw->vw_font, &size);
509 vt_scroll(vw, -size.tp_row, VHS_CUR);
511 case FKEY | F(57): /* End key. */
512 vt_scroll(vw, 0, VHS_END);
514 case FKEY | F(58): /* Arrow down. */
515 vt_scroll(vw, 1, VHS_CUR);
517 case FKEY | F(59): /* Page down. */
518 vt_termsize(vd, vw->vw_font, &size);
519 vt_scroll(vw, size.tp_row, VHS_CUR);
528 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
530 struct vt_window *vw = vd->vd_curwindow;
533 #if VT_ALT_TO_ESC_HACK
535 switch (c & ~RELKEY) {
536 case (SPCLKEY | RALT):
537 if (vt_enable_altgr != 0)
539 case (SPCLKEY | LALT):
540 vd->vd_kbstate &= ~ALKED;
542 /* Other keys ignored for RELKEY event. */
545 switch (c & ~RELKEY) {
546 case (SPCLKEY | RALT):
547 if (vt_enable_altgr != 0)
549 case (SPCLKEY | LALT):
550 vd->vd_kbstate |= ALKED;
555 /* Other keys ignored for RELKEY event. */
559 if (vt_machine_kbdevent(c))
562 if (vw->vw_flags & VWF_SCROLL) {
563 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
564 /* Scroll mode keys handled, nothing to do more. */
571 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
572 vw = vd->vd_windows[c - F_SCR];
574 vt_proc_window_switch(vw);
581 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
584 /* Turn scrolling on. */
585 vw->vw_flags |= VWF_SCROLL;
586 VTBUF_SLCK_ENABLE(&vw->vw_buf);
588 /* Turn scrolling off. */
589 vw->vw_flags &= ~VWF_SCROLL;
590 VTBUF_SLCK_DISABLE(&vw->vw_buf);
591 vt_scroll(vw, 0, VHS_END);
596 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
597 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
598 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
599 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
600 /* F1 through F12 keys. */
601 terminal_input_special(vw->vw_terminal,
602 TKEY_F1 + c - (FKEY | F(1)));
604 case FKEY | F(49): /* Home key. */
605 terminal_input_special(vw->vw_terminal, TKEY_HOME);
607 case FKEY | F(50): /* Arrow up. */
608 terminal_input_special(vw->vw_terminal, TKEY_UP);
610 case FKEY | F(51): /* Page up. */
611 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
613 case FKEY | F(53): /* Arrow left. */
614 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
616 case FKEY | F(55): /* Arrow right. */
617 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
619 case FKEY | F(57): /* End key. */
620 terminal_input_special(vw->vw_terminal, TKEY_END);
622 case FKEY | F(58): /* Arrow down. */
623 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
625 case FKEY | F(59): /* Page down. */
626 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
628 case FKEY | F(60): /* Insert key. */
629 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
631 case FKEY | F(61): /* Delete key. */
632 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
635 } else if (KEYFLAGS(c) == 0) {
636 /* Don't do UTF-8 conversion when doing raw mode. */
637 if (vw->vw_kbdmode == K_XLATE) {
638 #if VT_ALT_TO_ESC_HACK
639 if (vd->vd_kbstate & ALKED) {
641 * Prepend ESC sequence if one of ALT keys down.
643 terminal_input_char(vw->vw_terminal, 0x1b);
647 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
649 terminal_input_raw(vw->vw_terminal, c);
655 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
657 struct vt_device *vd = arg;
663 case KBDIO_UNLOADING:
665 vd->vd_keyboard = -1;
666 kbd_release(kbd, (void *)vd);
673 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
674 vt_processkey(kbd, vd, c);
680 vt_allocate_keyboard(struct vt_device *vd)
686 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
687 vd->vd_keyboard = idx0;
689 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
690 k0 = kbd_get_keyboard(idx0);
692 for (idx = kbd_find_keyboard2("*", -1, 0);
694 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
695 k = kbd_get_keyboard(idx);
697 if (idx == idx0 || KBD_IS_BUSY(k))
700 bzero(&ki, sizeof(ki));
701 strcpy(ki.kb_name, k->kb_name);
702 ki.kb_unit = k->kb_unit;
704 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
707 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
708 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
710 DPRINTF(10, "%s: No keyboard found.\n", __func__);
714 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
720 vtterm_bell(struct terminal *tm)
722 struct vt_window *vw = tm->tm_softc;
723 struct vt_device *vd = vw->vw_device;
725 if (vd->vd_flags & VDF_QUIET_BELL)
728 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
732 vtterm_beep(struct terminal *tm, u_int param)
736 if ((param == 0) || ((param & 0xffff) == 0)) {
741 period = ((param >> 16) & 0xffff) * hz / 1000;
742 freq = 1193182 / (param & 0xffff);
744 sysbeep(freq, period);
748 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
750 struct vt_window *vw = tm->tm_softc;
752 vtbuf_cursor_position(&vw->vw_buf, p);
756 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
758 struct vt_window *vw = tm->tm_softc;
760 vtbuf_putchar(&vw->vw_buf, p, c);
764 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
766 struct vt_window *vw = tm->tm_softc;
768 vtbuf_fill_locked(&vw->vw_buf, r, c);
772 vtterm_copy(struct terminal *tm, const term_rect_t *r,
775 struct vt_window *vw = tm->tm_softc;
777 vtbuf_copy(&vw->vw_buf, r, p);
781 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
783 struct vt_window *vw = tm->tm_softc;
787 vtbuf_cursor_visibility(&vw->vw_buf, arg);
790 vw->vw_mouse_level = arg;
796 vt_determine_colors(term_char_t c, int cursor,
797 term_color_t *fg, term_color_t *bg)
804 *fg = TCHAR_FGCOLOR(c);
805 if (TCHAR_FORMAT(c) & TF_BOLD)
806 *fg = TCOLOR_LIGHT(*fg);
807 *bg = TCHAR_BGCOLOR(c);
809 if (TCHAR_FORMAT(c) & TF_REVERSE)
821 #ifndef SC_NO_CUTPASTE
823 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
825 unsigned int mx, my, x1, y1, x2, y2;
828 * We use the cursor position saved during the current refresh,
829 * in case the cursor moved since.
831 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_offset.tp_col;
832 my = vd->vd_my_drawn + vd->vd_curwindow->vw_offset.tp_row;
834 x1 = area->tr_begin.tp_col;
835 y1 = area->tr_begin.tp_row;
836 x2 = area->tr_end.tp_col;
837 y2 = area->tr_end.tp_row;
839 if (((mx >= x1 && x2 - 1 >= mx) ||
840 (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
841 ((my >= y1 && y2 - 1 >= my) ||
842 (my < y1 && my + vd->vd_mcursor->height >= y1)))
849 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
852 struct vt_window *vw;
856 vw = vd->vd_curwindow;
863 area.tr_begin.tp_col = (x - vw->vw_offset.tp_col) /
865 area.tr_begin.tp_row = (y - vw->vw_offset.tp_row) /
868 ((x + vd->vd_mcursor->width - vw->vw_offset.tp_col) /
871 ((y + vd->vd_mcursor->height - vw->vw_offset.tp_row) /
875 * No font loaded (ie. vt_vga operating in textmode).
877 * FIXME: This fake area needs to be revisited once the
878 * mouse cursor is supported in vt_vga's textmode.
880 area.tr_begin.tp_col = x;
881 area.tr_begin.tp_row = y;
882 area.tr_end.tp_col = x + 2;
883 area.tr_end.tp_row = y + 2;
886 vtbuf_dirty(&vw->vw_buf, &area);
891 vt_flush(struct vt_device *vd)
893 struct vt_window *vw;
895 struct vt_bufmask tmask;
898 #ifndef SC_NO_CUTPASTE
899 int cursor_was_shown, cursor_moved;
902 vw = vd->vd_curwindow;
906 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
910 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
913 #ifndef SC_NO_CUTPASTE
914 cursor_was_shown = vd->vd_mshown;
915 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
916 vd->vd_my != vd->vd_my_drawn);
918 /* Check if the cursor should be displayed or not. */
919 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
920 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
921 !kdb_active && panicstr == NULL) { /* DDB inactive. */
928 * If the cursor changed display state or moved, we must mark
929 * the old position as dirty, so that it's erased.
931 if (cursor_was_shown != vd->vd_mshown ||
932 (vd->vd_mshown && cursor_moved))
933 vt_mark_mouse_position_as_dirty(vd);
936 * Save position of the mouse cursor. It's used by backends to
937 * know where to draw the cursor and during the next refresh to
938 * erase the previous position.
940 vd->vd_mx_drawn = vd->vd_mx;
941 vd->vd_my_drawn = vd->vd_my;
944 * If the cursor is displayed and has moved since last refresh,
945 * mark the new position as dirty.
947 if (vd->vd_mshown && cursor_moved)
948 vt_mark_mouse_position_as_dirty(vd);
951 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
952 vt_termsize(vd, vf, &size);
954 /* Force a full redraw when the screen contents are invalid. */
955 if (vd->vd_flags & VDF_INVALID) {
956 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
958 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
960 vd->vd_flags &= ~VDF_INVALID;
963 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
964 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
971 struct vt_device *vd;
974 /* Update screen if required. */
977 /* Schedule for next update. */
978 vt_schedule_flush(vd, 0);
982 vtterm_done(struct terminal *tm)
984 struct vt_window *vw = tm->tm_softc;
985 struct vt_device *vd = vw->vw_device;
987 if (kdb_active || panicstr != NULL) {
988 /* Switch to the debugger. */
989 if (vd->vd_curwindow != vw) {
990 vd->vd_curwindow = vw;
991 vd->vd_flags |= VDF_INVALID;
992 if (vd->vd_driver->vd_postswitch)
993 vd->vd_driver->vd_postswitch(vd);
995 vd->vd_flags &= ~VDF_SPLASH;
997 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1004 vtterm_splash(struct vt_device *vd)
1006 vt_axis_t top, left;
1008 /* Display a nice boot splash. */
1009 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1011 top = (vd->vd_height - vt_logo_height) / 2;
1012 left = (vd->vd_width - vt_logo_width) / 2;
1013 switch (vt_logo_depth) {
1015 /* XXX: Unhardcode colors! */
1016 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1017 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1018 left, top, TC_WHITE, TC_BLACK);
1020 vd->vd_flags |= VDF_SPLASH;
1027 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1029 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1030 struct vt_window *vw = tm->tm_softc;
1031 struct vt_device *vd = vw->vw_device;
1036 if (!vty_enabled(VTY_VT))
1039 if (vd->vd_flags & VDF_INITIALIZED)
1040 /* Initialization already done. */
1043 SET_FOREACH(vtdlist, vt_drv_set) {
1045 if (vtd->vd_probe == NULL)
1047 if (vtd->vd_probe(vd) == CN_DEAD)
1049 if ((vtdbest == NULL) ||
1050 (vtd->vd_priority > vtdbest->vd_priority))
1053 if (vtdbest == NULL) {
1054 cp->cn_pri = CN_DEAD;
1055 vd->vd_flags |= VDF_DEAD;
1057 vd->vd_driver = vtdbest;
1058 cp->cn_pri = vd->vd_driver->vd_init(vd);
1061 /* Check if driver's vt_init return CN_DEAD. */
1062 if (cp->cn_pri == CN_DEAD) {
1063 vd->vd_flags |= VDF_DEAD;
1066 /* Initialize any early-boot keyboard drivers */
1067 kbd_configure(KB_CONF_PROBE_ONLY);
1069 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1070 vd->vd_windows[VT_CONSWINDOW] = vw;
1071 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1073 /* Attach default font if not in TEXTMODE. */
1074 if ((vd->vd_flags & VDF_TEXTMODE) == 0)
1075 vw->vw_font = vtfont_ref(&vt_font_default);
1077 vtbuf_init_early(&vw->vw_buf);
1078 vt_winsize(vd, vw->vw_font, &wsz);
1079 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1081 attr.ta_format = TCHAR_FORMAT(c);
1082 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1083 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1084 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1086 if (vtdbest != NULL) {
1090 vd->vd_flags |= VDF_INITIALIZED;
1095 vtterm_cngetc(struct terminal *tm)
1097 struct vt_window *vw = tm->tm_softc;
1098 struct vt_device *vd = vw->vw_device;
1103 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1104 return (*vw->vw_kbdsq++);
1107 /* Make sure the splash screen is not there. */
1108 if (vd->vd_flags & VDF_SPLASH) {
1110 vd->vd_flags &= ~VDF_SPLASH;
1111 /* Mark screen as invalid to force update */
1112 vd->vd_flags |= VDF_INVALID;
1116 /* Stripped down keyboard handler. */
1117 kbd = kbd_get_keyboard(vd->vd_keyboard);
1121 /* Force keyboard input mode to K_XLATE */
1123 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1125 /* Switch the keyboard to polling to make it work here. */
1126 kbdd_poll(kbd, TRUE);
1127 c = kbdd_read_char(kbd, 0);
1128 kbdd_poll(kbd, FALSE);
1132 if (vw->vw_flags & VWF_SCROLL) {
1133 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1138 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1142 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1143 if (state & SLKED) {
1144 /* Turn scrolling on. */
1145 vw->vw_flags |= VWF_SCROLL;
1146 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1148 /* Turn scrolling off. */
1149 vt_scroll(vw, 0, VHS_END);
1150 vw->vw_flags &= ~VWF_SCROLL;
1151 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1154 /* XXX: KDB can handle history. */
1155 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1156 vw->vw_kbdsq = "\x1b[A";
1158 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1159 vw->vw_kbdsq = "\x1b[B";
1161 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1162 vw->vw_kbdsq = "\x1b[C";
1164 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1165 vw->vw_kbdsq = "\x1b[D";
1169 /* Force refresh to make scrollback work. */
1171 } else if (KEYFLAGS(c) == 0) {
1172 return (KEYCHAR(c));
1175 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1176 return (*vw->vw_kbdsq++);
1182 vtterm_opened(struct terminal *tm, int opened)
1184 struct vt_window *vw = tm->tm_softc;
1185 struct vt_device *vd = vw->vw_device;
1188 vd->vd_flags &= ~VDF_SPLASH;
1190 vw->vw_flags |= VWF_OPENED;
1192 vw->vw_flags &= ~VWF_OPENED;
1193 /* TODO: finish ACQ/REL */
1199 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1201 struct vt_device *vd = vw->vw_device;
1202 int x, y, off_x, off_y;
1204 if (vd->vd_driver->vd_drawrect == NULL)
1207 x = vd->vd_width - 1;
1208 y = vd->vd_height - 1;
1209 off_x = vw->vw_offset.tp_col;
1210 off_y = vw->vw_offset.tp_row;
1214 vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c);
1217 vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y,
1219 /* Right bar. May be 1 pixel wider than necessary due to rounding. */
1220 vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c);
1221 /* Bottom bar. May be 1 mixel taller than necessary due to rounding. */
1222 vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c);
1228 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1230 struct vt_device *vd = vw->vw_device;
1231 struct terminal *tm = vw->vw_terminal;
1238 * Changing fonts is a little tricky. We must prevent
1239 * simultaneous access to the device, so we must stop
1240 * the display timer and the terminal from accessing.
1241 * We need to switch fonts and grow our screen buffer.
1243 * XXX: Right now the code uses terminal_mute() to
1244 * prevent data from reaching the console driver while
1245 * resizing the screen buffer. This isn't elegant...
1249 if (vw->vw_flags & VWF_BUSY) {
1250 /* Another process is changing the font. */
1254 if (vd->vd_flags & VDF_TEXTMODE) {
1255 /* Our device doesn't need fonts. */
1259 vw->vw_flags |= VWF_BUSY;
1262 vt_termsize(vd, vf, &size);
1263 vt_winsize(vd, vf, &wsz);
1264 /* Save offset to font aligned area. */
1265 vw->vw_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1266 vw->vw_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1268 /* Grow the screen buffer and terminal. */
1269 terminal_mute(tm, 1);
1270 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1271 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1272 terminal_mute(tm, 0);
1274 /* Actually apply the font to the current window. */
1276 if (vw->vw_font != vf) {
1278 * In case vt_change_font called to update size we don't need
1279 * to update font link.
1281 vtfont_unref(vw->vw_font);
1282 vw->vw_font = vtfont_ref(vf);
1285 /* Force a full redraw the next timer tick. */
1286 if (vd->vd_curwindow == vw) {
1287 vt_set_border(vw, vf, TC_BLACK);
1288 vd->vd_flags |= VDF_INVALID;
1290 vw->vw_flags &= ~VWF_BUSY;
1296 vt_proc_alive(struct vt_window *vw)
1300 if (vw->vw_smode.mode != VT_PROCESS)
1304 if ((p = pfind(vw->vw_pid)) != NULL)
1306 if (vw->vw_proc == p)
1309 vw->vw_smode.mode = VT_AUTO;
1310 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1317 signal_vt_rel(struct vt_window *vw)
1320 if (vw->vw_smode.mode != VT_PROCESS)
1322 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1327 vw->vw_flags |= VWF_SWWAIT_REL;
1328 PROC_LOCK(vw->vw_proc);
1329 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1330 PROC_UNLOCK(vw->vw_proc);
1331 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1336 signal_vt_acq(struct vt_window *vw)
1339 if (vw->vw_smode.mode != VT_PROCESS)
1341 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1342 cnavailable(vw->vw_terminal->consdev, FALSE);
1343 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1348 vw->vw_flags |= VWF_SWWAIT_ACQ;
1349 PROC_LOCK(vw->vw_proc);
1350 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1351 PROC_UNLOCK(vw->vw_proc);
1352 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1357 finish_vt_rel(struct vt_window *vw, int release, int *s)
1360 if (vw->vw_flags & VWF_SWWAIT_REL) {
1361 vw->vw_flags &= ~VWF_SWWAIT_REL;
1363 callout_drain(&vw->vw_proc_dead_timer);
1364 vt_late_window_switch(vw->vw_switch_to);
1372 finish_vt_acq(struct vt_window *vw)
1375 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1376 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1382 #ifndef SC_NO_CUTPASTE
1384 vt_mouse_terminput_button(struct vt_device *vd, int button)
1386 struct vt_window *vw;
1388 char mouseb[6] = "\x1B[M";
1391 vw = vd->vd_curwindow;
1394 /* Translate to char position. */
1395 x = vd->vd_mx / vf->vf_width;
1396 y = vd->vd_my / vf->vf_height;
1397 /* Avoid overflow. */
1398 x = MIN(x, 255 - '!');
1399 y = MIN(y, 255 - '!');
1401 mouseb[3] = ' ' + button;
1402 mouseb[4] = '!' + x;
1403 mouseb[5] = '!' + y;
1405 for (i = 0; i < sizeof(mouseb); i++ )
1406 terminal_input_char(vw->vw_terminal, mouseb[i]);
1410 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1415 case MOUSE_BUTTON_EVENT:
1417 /* Mouse button pressed. */
1418 if (event & MOUSE_BUTTON1DOWN)
1419 vt_mouse_terminput_button(vd, 0);
1420 if (event & MOUSE_BUTTON2DOWN)
1421 vt_mouse_terminput_button(vd, 1);
1422 if (event & MOUSE_BUTTON3DOWN)
1423 vt_mouse_terminput_button(vd, 2);
1425 /* Mouse button released. */
1426 vt_mouse_terminput_button(vd, 3);
1430 case MOUSE_MOTION_EVENT:
1431 if (mouse->u.data.z < 0) {
1433 sc_mouse_input_button(vd, 64);
1434 } else if (mouse->u.data.z > 0) {
1436 sc_mouse_input_button(vd, 65);
1444 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1446 struct vt_device *vd;
1447 struct vt_window *vw;
1454 vw = vd->vd_curwindow;
1458 if (vw->vw_flags & VWF_MOUSE_HIDE)
1459 return; /* Mouse disabled. */
1461 if (vf == NULL) /* Text mode. */
1465 * TODO: add flag about pointer position changed, to not redraw chars
1466 * under mouse pointer when nothing changed.
1469 if (vw->vw_mouse_level > 0)
1470 vt_mouse_terminput(vd, type, x, y, event, cnt);
1474 case MOUSE_MOTION_EVENT:
1479 vt_termsize(vd, vf, &size);
1484 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1485 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1489 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1490 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1491 vd->vd_mx / vf->vf_width,
1492 vd->vd_my / vf->vf_height) == 1)) {
1495 * We have something marked to copy, so update pointer
1496 * to window with selection.
1498 vd->vd_markedwin = vw;
1501 case MOUSE_BUTTON_EVENT:
1509 case MOUSE_BUTTON1DOWN:
1512 mark = VTB_MARK_END;
1514 case 1: /* single click: start cut operation */
1515 mark = VTB_MARK_START;
1517 case 2: /* double click: cut a word */
1518 mark = VTB_MARK_WORD;
1520 case 3: /* triple click: cut a line */
1521 mark = VTB_MARK_ROW;
1525 case VT_MOUSE_PASTEBUTTON:
1530 if (vd->vd_markedwin == NULL)
1532 /* Get current selecton size in bytes. */
1533 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1537 buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1538 /* Request cupy/paste buffer data, no more than `len' */
1539 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1542 len /= sizeof(term_char_t);
1543 for (i = 0; i < len; i++ ) {
1546 terminal_input_char(vw->vw_terminal, buf[i]);
1549 /* Done, so cleanup. */
1554 case VT_MOUSE_EXTENDBUTTON:
1557 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1558 mark = VTB_MARK_EXTEND;
1563 mark = VTB_MARK_EXTEND;
1571 /* Save buttons state. */
1573 vd->vd_mstate |= event;
1575 vd->vd_mstate &= ~event;
1577 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1578 vd->vd_my / vf->vf_height) == 1) {
1580 * We have something marked to copy, so update pointer to
1581 * window with selection.
1583 vd->vd_markedwin = vw;
1588 vt_mouse_state(int show)
1590 struct vt_device *vd;
1591 struct vt_window *vw;
1594 vw = vd->vd_curwindow;
1598 vw->vw_flags |= VWF_MOUSE_HIDE;
1601 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1605 /* Mark mouse position as dirty. */
1606 vt_mark_mouse_position_as_dirty(vd);
1611 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1612 int nprot, vm_memattr_t *memattr)
1614 struct vt_window *vw = tm->tm_softc;
1615 struct vt_device *vd = vw->vw_device;
1617 if (vd->vd_driver->vd_fb_mmap)
1618 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1625 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1628 struct vt_window *vw = tm->tm_softc;
1629 struct vt_device *vd = vw->vw_device;
1632 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1633 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1644 cmd = VT_WAITACTIVE;
1670 ival = IOCPARM_IVAL(data);
1671 data = (caddr_t)&ival;
1676 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1677 if (*(int *)data & ~0x7f)
1681 case GIO_DEADKEYMAP:
1682 case PIO_DEADKEYMAP:
1687 case KDSKBSTATE: /* set keyboard state (locks) */
1688 case KDGKBSTATE: /* get keyboard state (locks) */
1689 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1690 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1691 case KDSETLED: /* set keyboard LED status */
1692 case KDGETLED: /* get keyboard LED status */
1693 case KBADDKBD: /* add/remove keyboard to/from mux */
1698 kbd = kbd_get_keyboard(vd->vd_keyboard);
1700 error = kbdd_ioctl(kbd, cmd, data);
1702 if (error == ENOIOCTL) {
1703 if (cmd == KDGKBTYPE) {
1704 /* always return something? XXX */
1716 kbd = kbd_get_keyboard(vd->vd_keyboard);
1718 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1721 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1722 *(int *)data = mode;
1728 mode = *(int *)data;
1733 vw->vw_kbdmode = mode;
1734 if (vw == vd->vd_curwindow) {
1739 kbd = kbd_get_keyboard(vd->vd_keyboard);
1741 error = kbdd_ioctl(kbd, KDSKBMODE,
1752 case FBIO_GETWINORG: /* get frame buffer window origin */
1753 case FBIO_GETDISPSTART: /* get display start address */
1754 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1755 case FBIO_BLANK: /* blank display */
1756 if (vd->vd_driver->vd_fb_ioctl)
1757 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1759 case CONS_BLANKTIME:
1764 *(int *)data = M_CG640x480;
1766 case CONS_BELLTYPE: /* set bell type sound */
1767 if ((*(int *)data) & CONS_QUIET_BELL)
1768 vd->vd_flags |= VDF_QUIET_BELL;
1770 vd->vd_flags &= ~VDF_QUIET_BELL;
1772 case CONS_GETINFO: {
1773 vid_info_t *vi = (vid_info_t *)data;
1775 vi->m_num = vd->vd_curwindow->vw_number + 1;
1776 /* XXX: other fields! */
1780 *(int *)data = 0x200;
1785 case CONS_MOUSECTL: {
1786 mouse_info_t *mouse = (mouse_info_t*)data;
1789 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
1790 * should not be applied to individual TTYs, but only to
1793 switch (mouse->operation) {
1795 if (vd->vd_flags & VDF_MOUSECURSOR) {
1796 vd->vd_flags &= ~VDF_MOUSECURSOR;
1797 #ifndef SC_NO_CUTPASTE
1798 vt_mouse_state(VT_MOUSE_HIDE);
1803 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
1804 vd->vd_flags |= VDF_MOUSECURSOR;
1805 vd->vd_mx = vd->vd_width / 2;
1806 vd->vd_my = vd->vd_height / 2;
1807 #ifndef SC_NO_CUTPASTE
1808 vt_mouse_state(VT_MOUSE_SHOW);
1819 error = vtfont_load((void *)data, &vf);
1823 error = vt_change_font(vw, vf);
1828 scrmap_t *sm = (scrmap_t *)data;
1830 /* We don't have screen maps, so return a handcrafted one. */
1831 for (i = 0; i < 256; i++)
1838 case KDENABIO: /* allow io operations */
1839 error = priv_check(td, PRIV_IO);
1842 error = securelevel_gt(td->td_ucred, 0);
1845 #if defined(__i386__)
1846 td->td_frame->tf_eflags |= PSL_IOPL;
1847 #elif defined(__amd64__)
1848 td->td_frame->tf_rflags |= PSL_IOPL;
1851 case KDDISABIO: /* disallow io operations (default) */
1852 #if defined(__i386__)
1853 td->td_frame->tf_eflags &= ~PSL_IOPL;
1854 #elif defined(__amd64__)
1855 td->td_frame->tf_rflags &= ~PSL_IOPL;
1858 case KDMKTONE: /* sound the bell */
1859 vtterm_beep(tm, *(u_int *)data);
1861 case KIOCSOUND: /* make tone (*data) hz */
1864 case CONS_SETKBD: /* set the new keyboard */
1867 if (vd->vd_keyboard != *(int *)data) {
1868 kbd = kbd_get_keyboard(*(int *)data);
1873 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1874 (void *)vd, vt_kbdevent, vd);
1876 if (vd->vd_keyboard != -1) {
1877 kbd_release(kbd, (void *)vd);
1879 kbd = kbd_get_keyboard(i);
1880 vd->vd_keyboard = i;
1882 (void)kbdd_ioctl(kbd, KDSKBMODE,
1883 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1885 error = EPERM; /* XXX */
1890 case CONS_RELKBD: /* release the current keyboard */
1893 if (vd->vd_keyboard != -1) {
1894 kbd = kbd_get_keyboard(vd->vd_keyboard);
1899 error = kbd_release(kbd, (void *)vd);
1901 vd->vd_keyboard = -1;
1908 win = *(int *)data - 1;
1909 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1911 if ((win > VT_MAXWINDOWS) || (win < 0))
1913 return (vt_proc_window_switch(vd->vd_windows[win]));
1916 *(int *)data = vd->vd_curwindow->vw_number + 1;
1919 *(int *)data = vw->vw_number + 1;
1922 /* TODO: Check current state, switching can be in progress. */
1923 if ((*(int *)data) == 0x01)
1924 vw->vw_flags |= VWF_VTYLOCK;
1925 else if ((*(int *)data) == 0x02)
1926 vw->vw_flags &= ~VWF_VTYLOCK;
1932 for (i = 0; i < VT_MAXWINDOWS; i++) {
1933 vw = vd->vd_windows[i];
1936 if (!(vw->vw_flags & VWF_OPENED)) {
1937 *(int *)data = vw->vw_number + 1;
1947 i = *(unsigned int *)data;
1948 if (i > VT_MAXWINDOWS)
1951 vw = vd->vd_windows[i - 1];
1954 while (vd->vd_curwindow != vw && error == 0)
1955 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1958 case VT_SETMODE: { /* set screen switcher mode */
1959 struct vt_mode *mode;
1962 mode = (struct vt_mode *)data;
1963 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1964 if (vw->vw_smode.mode == VT_PROCESS) {
1965 p1 = pfind(vw->vw_pid);
1966 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1969 DPRINTF(5, "error EPERM\n");
1975 if (mode->mode == VT_AUTO) {
1976 vw->vw_smode.mode = VT_AUTO;
1979 DPRINTF(5, "VT_AUTO, ");
1980 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1981 cnavailable(vw->vw_terminal->consdev, TRUE);
1982 /* were we in the middle of the vty switching process? */
1983 if (finish_vt_rel(vw, TRUE, &s) == 0)
1984 DPRINTF(5, "reset WAIT_REL, ");
1985 if (finish_vt_acq(vw) == 0)
1986 DPRINTF(5, "reset WAIT_ACQ, ");
1988 } else if (mode->mode == VT_PROCESS) {
1989 if (!ISSIGVALID(mode->relsig) ||
1990 !ISSIGVALID(mode->acqsig) ||
1991 !ISSIGVALID(mode->frsig)) {
1992 DPRINTF(5, "error EINVAL\n");
1995 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1996 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1997 vw->vw_proc = td->td_proc;
1998 vw->vw_pid = vw->vw_proc->p_pid;
1999 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2000 cnavailable(vw->vw_terminal->consdev, FALSE);
2002 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2009 case VT_GETMODE: /* get screen switcher mode */
2010 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2013 case VT_RELDISP: /* screen switcher ioctl */
2015 * This must be the current vty which is in the VT_PROCESS
2018 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2022 /* ...and this process is controlling it. */
2023 if (vw->vw_proc != td->td_proc) {
2027 switch(*(int *)data) {
2028 case VT_FALSE: /* user refuses to release screen, abort */
2029 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2030 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2031 SC_DRIVER_NAME, VT_UNIT(vw));
2033 case VT_TRUE: /* user has released screen, go on */
2034 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2035 if (vw->vw_flags & VWF_SWWAIT_REL) {
2036 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2037 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2038 SC_DRIVER_NAME, VT_UNIT(vw));
2043 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2044 if ((error = finish_vt_acq(vw)) == 0)
2045 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2046 SC_DRIVER_NAME, VT_UNIT(vw));
2057 static struct vt_window *
2058 vt_allocate_window(struct vt_device *vd, unsigned int window)
2060 struct vt_window *vw;
2061 struct terminal *tm;
2065 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2067 vw->vw_number = window;
2068 vw->vw_kbdmode = K_XLATE;
2070 if ((vd->vd_flags & VDF_TEXTMODE) == 0)
2071 vw->vw_font = vtfont_ref(&vt_font_default);
2073 vt_termsize(vd, vw->vw_font, &size);
2074 vt_winsize(vd, vw->vw_font, &wsz);
2075 vtbuf_init(&vw->vw_buf, &size);
2077 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2078 terminal_set_winsize(tm, &wsz);
2079 vd->vd_windows[window] = vw;
2080 callout_init(&vw->vw_proc_dead_timer, 0);
2086 vt_upgrade(struct vt_device *vd)
2088 struct vt_window *vw;
2091 if (!vty_enabled(VTY_VT))
2094 for (i = 0; i < VT_MAXWINDOWS; i++) {
2095 vw = vd->vd_windows[i];
2098 vw = vt_allocate_window(vd, i);
2100 if (!(vw->vw_flags & VWF_READY)) {
2101 callout_init(&vw->vw_proc_dead_timer, 0);
2102 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2103 vw->vw_flags |= VWF_READY;
2104 if (vw->vw_flags & VWF_CONSOLE) {
2105 /* For existing console window. */
2106 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2107 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2113 if (vd->vd_curwindow == NULL)
2114 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2116 if (!(vd->vd_flags & VDF_ASYNC)) {
2117 /* Attach keyboard. */
2118 vt_allocate_keyboard(vd);
2120 /* Init 25 Hz timer. */
2121 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2123 /* Start timer when everything ready. */
2124 vd->vd_flags |= VDF_ASYNC;
2125 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2126 vd->vd_timer_armed = 1;
2131 /* Refill settings with new sizes. */
2136 vt_resize(struct vt_device *vd)
2138 struct vt_window *vw;
2141 for (i = 0; i < VT_MAXWINDOWS; i++) {
2142 vw = vd->vd_windows[i];
2144 /* Assign default font to window, if not textmode. */
2145 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2146 vw->vw_font = vtfont_ref(&vt_font_default);
2148 /* Resize terminal windows */
2149 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2150 DPRINTF(100, "%s: vt_change_font() is busy, "
2151 "window %d\n", __func__, i);
2157 vt_allocate(struct vt_driver *drv, void *softc)
2159 struct vt_device *vd;
2161 if (!vty_enabled(VTY_VT))
2164 if (main_vd->vd_driver == NULL) {
2165 main_vd->vd_driver = drv;
2166 printf("VT: initialize with new VT driver \"%s\".\n",
2170 * Check if have rights to replace current driver. For example:
2171 * it is bad idea to replace KMS driver with generic VGA one.
2173 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2174 printf("VT: Driver priority %d too low. Current %d\n ",
2175 drv->vd_priority, main_vd->vd_driver->vd_priority);
2178 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2179 main_vd->vd_driver->vd_name, drv->vd_name);
2184 if (vd->vd_flags & VDF_ASYNC) {
2185 /* Stop vt_flush periodic task. */
2186 vt_suspend_flush_timer(vd);
2188 * Mute current terminal until we done. vt_change_font (called
2189 * from vt_resize) will unmute it.
2191 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2195 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2198 vd->vd_flags &= ~VDF_TEXTMODE;
2200 vd->vd_driver = drv;
2201 vd->vd_softc = softc;
2202 vd->vd_driver->vd_init(vd);
2205 /* Update windows sizes and initialize last items. */
2209 if (vd->vd_flags & VDF_SPLASH)
2213 if (vd->vd_flags & VDF_ASYNC) {
2214 /* Allow to put chars now. */
2215 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2216 /* Rerun timer for screen updates. */
2217 vt_resume_flush_timer(vd, 0);
2221 * Register as console. If it already registered, cnadd() will ignore
2224 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2231 if (vt_suspendswitch == 0)
2233 /* Save current window. */
2234 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2235 /* Ask holding process to free window and switch to console window */
2236 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2243 if (vt_suspendswitch == 0)
2245 /* Switch back to saved window */
2246 if (main_vd->vd_savedwindow != NULL)
2247 vt_proc_window_switch(main_vd->vd_savedwindow);
2248 main_vd->vd_savedwindow = NULL;