2 * Copyright (c) 2009, 2013 The FreeBSD Foundation
5 * This software was developed by Ed Schouten under sponsorship from the
8 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_compat.h"
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <sys/reboot.h>
51 #include <sys/systm.h>
52 #include <sys/terminal.h>
54 #include <dev/kbd/kbdreg.h>
55 #include <dev/vt/vt.h>
57 #if defined(__i386__) || defined(__amd64__)
58 #include <machine/psl.h>
59 #include <machine/frame.h>
62 static tc_bell_t vtterm_bell;
63 static tc_cursor_t vtterm_cursor;
64 static tc_putchar_t vtterm_putchar;
65 static tc_fill_t vtterm_fill;
66 static tc_copy_t vtterm_copy;
67 static tc_param_t vtterm_param;
68 static tc_done_t vtterm_done;
70 static tc_cnprobe_t vtterm_cnprobe;
71 static tc_cngetc_t vtterm_cngetc;
73 static tc_opened_t vtterm_opened;
74 static tc_ioctl_t vtterm_ioctl;
75 static tc_mmap_t vtterm_mmap;
77 const struct terminal_class vt_termclass = {
78 .tc_bell = vtterm_bell,
79 .tc_cursor = vtterm_cursor,
80 .tc_putchar = vtterm_putchar,
81 .tc_fill = vtterm_fill,
82 .tc_copy = vtterm_copy,
83 .tc_param = vtterm_param,
84 .tc_done = vtterm_done,
86 .tc_cnprobe = vtterm_cnprobe,
87 .tc_cngetc = vtterm_cngetc,
89 .tc_opened = vtterm_opened,
90 .tc_ioctl = vtterm_ioctl,
91 .tc_mmap = vtterm_mmap,
95 * Use a constant timer of 25 Hz to redraw the screen.
97 * XXX: In theory we should only fire up the timer when there is really
98 * activity. Unfortunately we cannot always start timers. We really
99 * don't want to process kernel messages synchronously, because it
100 * really slows down the system.
102 #define VT_TIMERFREQ 25
104 /* Bell pitch/duration. */
105 #define VT_BELLDURATION ((5 * hz + 99) / 100)
106 #define VT_BELLPITCH 800
108 #define VT_LOCK(vd) mtx_lock(&(vd)->vd_lock)
109 #define VT_UNLOCK(vd) mtx_unlock(&(vd)->vd_lock)
111 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
114 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
115 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
116 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
117 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
118 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
120 static struct vt_device vt_consdev;
121 static unsigned int vt_unit = 0;
122 static MALLOC_DEFINE(M_VT, "vt", "vt device");
123 struct vt_device *main_vd = &vt_consdev;
126 extern unsigned int vt_logo_width;
127 extern unsigned int vt_logo_height;
128 extern unsigned int vt_logo_depth;
129 extern unsigned char vt_logo_image[];
132 extern struct vt_font vt_font_default;
133 #ifndef SC_NO_CUTPASTE
134 extern struct mouse_cursor vt_default_mouse_pointer;
137 static int signal_vt_rel(struct vt_window *);
138 static int signal_vt_acq(struct vt_window *);
139 static int finish_vt_rel(struct vt_window *, int, int *);
140 static int finish_vt_acq(struct vt_window *);
141 static int vt_window_switch(struct vt_window *);
142 static int vt_late_window_switch(struct vt_window *);
143 static int vt_proc_alive(struct vt_window *);
144 static void vt_resize(struct vt_device *);
145 static void vt_update_static(void *);
147 SET_DECLARE(vt_drv_set, struct vt_driver);
149 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
150 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
152 static struct terminal vt_consterm;
153 static struct vt_window vt_conswindow;
154 static struct vt_device vt_consdev = {
157 .vd_flags = VDF_INVALID,
158 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
159 .vd_curwindow = &vt_conswindow,
160 .vd_markedwin = NULL,
163 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
164 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
165 static struct vt_window vt_conswindow = {
166 .vw_number = VT_CONSWINDOW,
167 .vw_flags = VWF_CONSOLE,
169 .vb_buffer = &vt_constextbuf[0],
170 .vb_rows = &vt_constextbufrows[0],
171 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
174 .vb_flags = VBF_STATIC,
175 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
176 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
182 .vw_device = &vt_consdev,
183 .vw_terminal = &vt_consterm,
184 .vw_kbdmode = K_XLATE,
186 static struct terminal vt_consterm = {
187 .tm_class = &vt_termclass,
188 .tm_softc = &vt_conswindow,
191 static struct consdev vt_consterm_consdev = {
192 .cn_ops = &termcn_cnops,
193 .cn_arg = &vt_consterm,
197 /* Add to set of consoles. */
198 DATA_SET(cons_set, vt_consterm_consdev);
201 * Right after kmem is done to allow early drivers to use locking and allocate
204 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
206 /* Delay until all devices attached, to not waste time. */
207 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
210 /* Initialize locks/mem depended members. */
212 vt_update_static(void *dummy)
215 if (!vty_enabled(VTY_VT))
217 if (main_vd->vd_driver != NULL)
218 printf("VT: running with driver \"%s\".\n",
219 main_vd->vd_driver->vd_name);
221 printf("VT: init without driver.\n");
223 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
224 cv_init(&main_vd->vd_winswitch, "vtwswt");
228 vt_schedule_flush(struct vt_device *vd, int ms)
232 /* Default to initial value. */
233 ms = 1000 / VT_TIMERFREQ;
235 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
239 vt_resume_flush_timer(struct vt_device *vd, int ms)
242 if (!atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
245 vt_schedule_flush(vd, ms);
249 vt_suspend_flush_timer(struct vt_device *vd)
252 if (!atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
255 callout_drain(&vd->vd_timer);
259 vt_switch_timer(void *arg)
262 vt_late_window_switch((struct vt_window *)arg);
266 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
269 DPRINTF(40, "%s\n", __func__);
270 curvw->vw_switch_to = vw;
271 /* Set timer to allow switch in case when process hang. */
272 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
273 vt_switch_timer, (void *)vw);
274 /* Notify process about vt switch attempt. */
275 DPRINTF(30, "%s: Notify process.\n", __func__);
276 signal_vt_rel(curvw);
282 vt_window_postswitch(struct vt_window *vw)
289 /* vt_late_window_switch will done VT switching for regular case. */
291 vt_late_window_switch(struct vt_window *vw)
295 callout_stop(&vw->vw_proc_dead_timer);
297 ret = vt_window_switch(vw);
301 /* Notify owner process about terminal availability. */
302 if (vw->vw_smode.mode == VT_PROCESS) {
303 ret = vt_window_postswitch(vw);
310 vt_proc_window_switch(struct vt_window *vw)
312 struct vt_window *curvw;
313 struct vt_device *vd;
317 curvw = vd->vd_curwindow;
319 if (curvw->vw_flags & VWF_VTYLOCK)
322 /* Ask current process permitions to switch away. */
323 if (curvw->vw_smode.mode == VT_PROCESS) {
324 DPRINTF(30, "%s: VT_PROCESS ", __func__);
325 if (vt_proc_alive(curvw) == FALSE) {
326 DPRINTF(30, "Dead. Cleaning.");
329 DPRINTF(30, "%s: Signaling process.\n", __func__);
330 /* Alive, try to ask him. */
331 ret = vt_window_preswitch(vw, curvw);
332 /* Wait for process answer or timeout. */
338 ret = vt_late_window_switch(vw);
342 /* Switch window ignoring process locking. */
344 vt_window_switch(struct vt_window *vw)
346 struct vt_device *vd = vw->vw_device;
347 struct vt_window *curvw = vd->vd_curwindow;
356 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
361 vt_suspend_flush_timer(vd);
363 vd->vd_curwindow = vw;
364 vd->vd_flags |= VDF_INVALID;
365 cv_broadcast(&vd->vd_winswitch);
368 if (vd->vd_driver->vd_postswitch)
369 vd->vd_driver->vd_postswitch(vd);
371 vt_resume_flush_timer(vd, 0);
373 /* Restore per-window keyboard mode. */
375 kbd = kbd_get_keyboard(vd->vd_keyboard);
377 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
380 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
386 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
389 size->tp_row = vd->vd_height;
390 size->tp_col = vd->vd_width;
392 size->tp_row /= vf->vf_height;
393 size->tp_col /= vf->vf_width;
398 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
401 size->ws_row = size->ws_ypixel = vd->vd_height;
402 size->ws_col = size->ws_xpixel = vd->vd_width;
404 size->ws_row /= vf->vf_height;
405 size->ws_col /= vf->vf_width;
410 vt_scroll(struct vt_window *vw, int offset, int whence)
415 if ((vw->vw_flags & VWF_SCROLL) == 0)
418 vt_termsize(vw->vw_device, vw->vw_font, &size);
420 diff = vthistory_seek(&vw->vw_buf, offset, whence);
422 * Offset changed, please update Nth lines on sceen.
423 * +N - Nth lines at top;
424 * -N - Nth lines at bottom.
427 if (diff < -size.tp_row || diff > size.tp_row) {
428 vw->vw_device->vd_flags |= VDF_INVALID;
431 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
435 vt_machine_kbdevent(int c)
440 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
443 /* XXX: Make this configurable! */
447 shutdown_nice(RB_HALT);
450 shutdown_nice(RB_HALT|RB_POWEROFF);
458 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
460 struct vt_device *vd;
464 /* Only special keys handled in ScrollLock mode */
465 if ((c & SPCLKEY) == 0)
471 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
472 vw = vd->vd_windows[c - F_SCR];
474 vt_proc_window_switch(vw);
482 /* Turn scrolling off. */
483 vt_scroll(vw, 0, VHS_END);
484 VTBUF_SLCK_DISABLE(&vw->vw_buf);
485 vw->vw_flags &= ~VWF_SCROLL;
488 case FKEY | F(49): /* Home key. */
489 vt_scroll(vw, 0, VHS_SET);
491 case FKEY | F(50): /* Arrow up. */
492 vt_scroll(vw, -1, VHS_CUR);
494 case FKEY | F(51): /* Page up. */
495 vt_termsize(vd, vw->vw_font, &size);
496 vt_scroll(vw, -size.tp_row, VHS_CUR);
498 case FKEY | F(57): /* End key. */
499 vt_scroll(vw, 0, VHS_END);
501 case FKEY | F(58): /* Arrow down. */
502 vt_scroll(vw, 1, VHS_CUR);
504 case FKEY | F(59): /* Page down. */
505 vt_termsize(vd, vw->vw_font, &size);
506 vt_scroll(vw, size.tp_row, VHS_CUR);
515 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
517 struct vt_window *vw = vd->vd_curwindow;
520 #if VT_ALT_TO_ESC_HACK
522 switch (c & ~RELKEY) {
523 case (SPCLKEY | RALT):
524 if (vt_enable_altgr != 0)
526 case (SPCLKEY | LALT):
527 vd->vd_kbstate &= ~ALKED;
529 /* Other keys ignored for RELKEY event. */
532 switch (c & ~RELKEY) {
533 case (SPCLKEY | RALT):
534 if (vt_enable_altgr != 0)
536 case (SPCLKEY | LALT):
537 vd->vd_kbstate |= ALKED;
542 /* Other keys ignored for RELKEY event. */
546 if (vt_machine_kbdevent(c))
549 if (vw->vw_flags & VWF_SCROLL) {
550 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
551 /* Scroll mode keys handled, nothing to do more. */
558 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
559 vw = vd->vd_windows[c - F_SCR];
561 vt_proc_window_switch(vw);
568 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
571 /* Turn scrolling on. */
572 vw->vw_flags |= VWF_SCROLL;
573 VTBUF_SLCK_ENABLE(&vw->vw_buf);
575 /* Turn scrolling off. */
576 vw->vw_flags &= ~VWF_SCROLL;
577 VTBUF_SLCK_DISABLE(&vw->vw_buf);
578 vt_scroll(vw, 0, VHS_END);
583 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
584 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
585 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
586 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
587 /* F1 through F12 keys. */
588 terminal_input_special(vw->vw_terminal,
589 TKEY_F1 + c - (FKEY | F(1)));
591 case FKEY | F(49): /* Home key. */
592 terminal_input_special(vw->vw_terminal, TKEY_HOME);
594 case FKEY | F(50): /* Arrow up. */
595 terminal_input_special(vw->vw_terminal, TKEY_UP);
597 case FKEY | F(51): /* Page up. */
598 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
600 case FKEY | F(53): /* Arrow left. */
601 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
603 case FKEY | F(55): /* Arrow right. */
604 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
606 case FKEY | F(57): /* End key. */
607 terminal_input_special(vw->vw_terminal, TKEY_END);
609 case FKEY | F(58): /* Arrow down. */
610 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
612 case FKEY | F(59): /* Page down. */
613 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
615 case FKEY | F(60): /* Insert key. */
616 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
618 case FKEY | F(61): /* Delete key. */
619 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
622 } else if (KEYFLAGS(c) == 0) {
623 /* Don't do UTF-8 conversion when doing raw mode. */
624 if (vw->vw_kbdmode == K_XLATE) {
625 #if VT_ALT_TO_ESC_HACK
626 if (vd->vd_kbstate & ALKED) {
628 * Prepend ESC sequence if one of ALT keys down.
630 terminal_input_char(vw->vw_terminal, 0x1b);
634 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
636 terminal_input_raw(vw->vw_terminal, c);
642 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
644 struct vt_device *vd = arg;
650 case KBDIO_UNLOADING:
652 vd->vd_keyboard = -1;
653 kbd_release(kbd, (void *)vd);
660 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
661 vt_processkey(kbd, vd, c);
667 vt_allocate_keyboard(struct vt_device *vd)
673 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
674 vd->vd_keyboard = idx0;
676 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
677 k0 = kbd_get_keyboard(idx0);
679 for (idx = kbd_find_keyboard2("*", -1, 0);
681 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
682 k = kbd_get_keyboard(idx);
684 if (idx == idx0 || KBD_IS_BUSY(k))
687 bzero(&ki, sizeof(ki));
688 strcpy(ki.kb_name, k->kb_name);
689 ki.kb_unit = k->kb_unit;
691 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
694 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
695 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
697 DPRINTF(10, "%s: No keyboard found.\n", __func__);
701 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
707 vtterm_bell(struct terminal *tm)
709 struct vt_window *vw = tm->tm_softc;
710 struct vt_device *vd = vw->vw_device;
712 if (vd->vd_flags & VDF_QUIET_BELL)
715 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
719 vtterm_beep(struct terminal *tm, u_int param)
723 if ((param == 0) || ((param & 0xffff) == 0)) {
728 period = ((param >> 16) & 0xffff) * hz / 1000;
729 freq = 1193182 / (param & 0xffff);
731 sysbeep(freq, period);
735 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
737 struct vt_window *vw = tm->tm_softc;
739 vtbuf_cursor_position(&vw->vw_buf, p);
743 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
745 struct vt_window *vw = tm->tm_softc;
747 vtbuf_putchar(&vw->vw_buf, p, c);
751 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
753 struct vt_window *vw = tm->tm_softc;
755 vtbuf_fill_locked(&vw->vw_buf, r, c);
759 vtterm_copy(struct terminal *tm, const term_rect_t *r,
762 struct vt_window *vw = tm->tm_softc;
764 vtbuf_copy(&vw->vw_buf, r, p);
768 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
770 struct vt_window *vw = tm->tm_softc;
774 vtbuf_cursor_visibility(&vw->vw_buf, arg);
777 vw->vw_mouse_level = arg;
783 vt_determine_colors(term_char_t c, int cursor,
784 term_color_t *fg, term_color_t *bg)
791 *fg = TCHAR_FGCOLOR(c);
792 if (TCHAR_FORMAT(c) & TF_BOLD)
793 *fg = TCOLOR_LIGHT(*fg);
794 *bg = TCHAR_BGCOLOR(c);
796 if (TCHAR_FORMAT(c) & TF_REVERSE)
809 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c,
810 int iscursor, unsigned int row, unsigned int col)
814 vt_determine_colors(c, iscursor, &fg, &bg);
820 src = vtfont_lookup(vf, c);
823 * Align the terminal to the centre of the screen.
824 * Fonts may not always be able to fill the entire
827 top = row * vf->vf_height + vd->vd_offset.tp_row;
828 left = col * vf->vf_width + vd->vd_offset.tp_col;
830 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left,
831 vf->vf_width, vf->vf_height, fg, bg);
833 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c),
839 vt_flush(struct vt_device *vd)
841 struct vt_window *vw;
843 struct vt_bufmask tmask;
844 unsigned int row, col;
848 #ifndef SC_NO_CUTPASTE
849 struct mouse_cursor *m;
853 vw = vd->vd_curwindow;
857 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
860 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
863 #ifndef SC_NO_CUTPASTE
864 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
865 !(vw->vw_flags & VWF_MOUSE_HIDE)) { /* Cursor displayed. */
866 if (vd->vd_moldx != vd->vd_mx ||
867 vd->vd_moldy != vd->vd_my) {
869 * Mark last mouse position as dirty to erase.
871 * FIXME: The font size could be different among
872 * all windows, so the column/row calculation
873 * below isn't correct for all windows.
875 * FIXME: The cursor can span more than one
876 * character cell. vtbuf_mouse_cursor_position
877 * marks surrounding cells as dirty. But due
878 * to font size possibly inconsistent across
879 * windows, this may not be sufficient. This
880 * causes part of the cursor to not be erased.
882 * FIXME: The vt_buf lock is acquired twice in a
885 vtbuf_mouse_cursor_position(&vw->vw_buf,
886 vd->vd_moldx / vf->vf_width,
887 vd->vd_moldy / vf->vf_height);
888 vtbuf_mouse_cursor_position(&vw->vw_buf,
889 vd->vd_mx / vf->vf_width,
890 vd->vd_my / vf->vf_height);
893 * Save point of last mouse cursor to erase it
896 vd->vd_moldx = vd->vd_mx;
897 vd->vd_moldy = vd->vd_my;
902 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
903 vt_termsize(vd, vf, &size);
905 /* Force a full redraw when the screen contents are invalid. */
906 if (vd->vd_flags & VDF_INVALID) {
907 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
909 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
911 vd->vd_flags &= ~VDF_INVALID;
914 for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) {
915 if (!VTBUF_DIRTYROW(&tmask, row))
917 r = VTBUF_GET_ROW(&vw->vw_buf, row);
918 for (col = tarea.tr_begin.tp_col;
919 col < tarea.tr_end.tp_col; col++) {
920 if (!VTBUF_DIRTYCOL(&tmask, col))
923 vt_bitblt_char(vd, vf, r[col],
924 VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col);
928 #ifndef SC_NO_CUTPASTE
929 /* Mouse disabled. */
930 if (vw->vw_flags & VWF_MOUSE_HIDE)
933 /* No mouse for DDB. */
934 if (kdb_active || panicstr != NULL)
937 if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) ==
939 m = &vt_default_mouse_pointer;
940 bpl = (m->w + 7) >> 3; /* Bytes per source line. */
944 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width))
945 w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1;
946 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height))
947 h = (size.tp_row * vf->vf_height) - vd->vd_my - 1;
949 vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl,
950 vd->vd_offset.tp_row + vd->vd_my,
951 vd->vd_offset.tp_col + vd->vd_mx,
952 w, h, TC_WHITE, TC_BLACK);
960 struct vt_device *vd;
963 /* Update screen if required. */
966 /* Schedule for next update. */
967 vt_schedule_flush(vd, 0);
971 vtterm_done(struct terminal *tm)
973 struct vt_window *vw = tm->tm_softc;
974 struct vt_device *vd = vw->vw_device;
976 if (kdb_active || panicstr != NULL) {
977 /* Switch to the debugger. */
978 if (vd->vd_curwindow != vw) {
979 vd->vd_curwindow = vw;
980 vd->vd_flags |= VDF_INVALID;
981 if (vd->vd_driver->vd_postswitch)
982 vd->vd_driver->vd_postswitch(vd);
984 vd->vd_flags &= ~VDF_SPLASH;
986 } else if (!(vd->vd_flags & VDF_ASYNC)) {
993 vtterm_splash(struct vt_device *vd)
997 /* Display a nice boot splash. */
998 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1000 top = (vd->vd_height - vt_logo_height) / 2;
1001 left = (vd->vd_width - vt_logo_width) / 2;
1002 switch (vt_logo_depth) {
1004 /* XXX: Unhardcode colors! */
1005 vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0,
1006 top, left, vt_logo_width, vt_logo_height, 0xf, 0x0);
1008 vd->vd_flags |= VDF_SPLASH;
1015 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1017 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1018 struct vt_window *vw = tm->tm_softc;
1019 struct vt_device *vd = vw->vw_device;
1024 if (!vty_enabled(VTY_VT))
1027 if (vd->vd_flags & VDF_INITIALIZED)
1028 /* Initialization already done. */
1031 SET_FOREACH(vtdlist, vt_drv_set) {
1033 if (vtd->vd_probe == NULL)
1035 if (vtd->vd_probe(vd) == CN_DEAD)
1037 if ((vtdbest == NULL) ||
1038 (vtd->vd_priority > vtdbest->vd_priority))
1041 if (vtdbest == NULL) {
1042 cp->cn_pri = CN_DEAD;
1043 vd->vd_flags |= VDF_DEAD;
1045 vd->vd_driver = vtdbest;
1046 cp->cn_pri = vd->vd_driver->vd_init(vd);
1049 /* Check if driver's vt_init return CN_DEAD. */
1050 if (cp->cn_pri == CN_DEAD) {
1051 vd->vd_flags |= VDF_DEAD;
1054 /* Initialize any early-boot keyboard drivers */
1055 kbd_configure(KB_CONF_PROBE_ONLY);
1057 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1058 vd->vd_windows[VT_CONSWINDOW] = vw;
1059 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1061 /* Attach default font if not in TEXTMODE. */
1062 if ((vd->vd_flags & VDF_TEXTMODE) == 0)
1063 vw->vw_font = vtfont_ref(&vt_font_default);
1065 vtbuf_init_early(&vw->vw_buf);
1066 vt_winsize(vd, vw->vw_font, &wsz);
1067 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1069 attr.ta_format = TCHAR_FORMAT(c);
1070 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1071 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1072 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1074 if (vtdbest != NULL) {
1078 vd->vd_flags |= VDF_INITIALIZED;
1083 vtterm_cngetc(struct terminal *tm)
1085 struct vt_window *vw = tm->tm_softc;
1086 struct vt_device *vd = vw->vw_device;
1091 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1092 return (*vw->vw_kbdsq++);
1095 /* Make sure the splash screen is not there. */
1096 if (vd->vd_flags & VDF_SPLASH) {
1098 vd->vd_flags &= ~VDF_SPLASH;
1099 /* Mark screen as invalid to force update */
1100 vd->vd_flags |= VDF_INVALID;
1104 /* Stripped down keyboard handler. */
1105 kbd = kbd_get_keyboard(vd->vd_keyboard);
1109 /* Force keyboard input mode to K_XLATE */
1111 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1113 /* Switch the keyboard to polling to make it work here. */
1114 kbdd_poll(kbd, TRUE);
1115 c = kbdd_read_char(kbd, 0);
1116 kbdd_poll(kbd, FALSE);
1120 if (vw->vw_flags & VWF_SCROLL) {
1121 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1126 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1130 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1131 if (state & SLKED) {
1132 /* Turn scrolling on. */
1133 vw->vw_flags |= VWF_SCROLL;
1134 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1136 /* Turn scrolling off. */
1137 vt_scroll(vw, 0, VHS_END);
1138 vw->vw_flags &= ~VWF_SCROLL;
1139 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1142 /* XXX: KDB can handle history. */
1143 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1144 vw->vw_kbdsq = "\x1b[A";
1146 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1147 vw->vw_kbdsq = "\x1b[B";
1149 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1150 vw->vw_kbdsq = "\x1b[C";
1152 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1153 vw->vw_kbdsq = "\x1b[D";
1157 /* Force refresh to make scrollback work. */
1159 } else if (KEYFLAGS(c) == 0) {
1160 return (KEYCHAR(c));
1163 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1164 return (*vw->vw_kbdsq++);
1170 vtterm_opened(struct terminal *tm, int opened)
1172 struct vt_window *vw = tm->tm_softc;
1173 struct vt_device *vd = vw->vw_device;
1176 vd->vd_flags &= ~VDF_SPLASH;
1178 vw->vw_flags |= VWF_OPENED;
1180 vw->vw_flags &= ~VWF_OPENED;
1181 /* TODO: finish ACQ/REL */
1187 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1189 struct vt_device *vd = vw->vw_device;
1190 struct terminal *tm = vw->vw_terminal;
1197 * Changing fonts is a little tricky. We must prevent
1198 * simultaneous access to the device, so we must stop
1199 * the display timer and the terminal from accessing.
1200 * We need to switch fonts and grow our screen buffer.
1202 * XXX: Right now the code uses terminal_mute() to
1203 * prevent data from reaching the console driver while
1204 * resizing the screen buffer. This isn't elegant...
1208 if (vw->vw_flags & VWF_BUSY) {
1209 /* Another process is changing the font. */
1213 if (vd->vd_flags & VDF_TEXTMODE) {
1214 /* Our device doesn't need fonts. */
1218 vw->vw_flags |= VWF_BUSY;
1221 vt_termsize(vd, vf, &size);
1222 vt_winsize(vd, vf, &wsz);
1223 /* Save offset to font aligned area. */
1224 vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2;
1225 vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2;
1227 /* Grow the screen buffer and terminal. */
1228 terminal_mute(tm, 1);
1229 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1230 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1231 terminal_mute(tm, 0);
1233 /* Actually apply the font to the current window. */
1235 if (vw->vw_font != vf) {
1237 * In case vt_change_font called to update size we don't need
1238 * to update font link.
1240 vtfont_unref(vw->vw_font);
1241 vw->vw_font = vtfont_ref(vf);
1244 /* Force a full redraw the next timer tick. */
1245 if (vd->vd_curwindow == vw)
1246 vd->vd_flags |= VDF_INVALID;
1247 vw->vw_flags &= ~VWF_BUSY;
1253 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c)
1255 struct vt_device *vd = vw->vw_device;
1256 int x, y, off_x, off_y;
1258 if (vd->vd_driver->vd_drawrect == NULL)
1261 x = vd->vd_width - 1;
1262 y = vd->vd_height - 1;
1263 off_x = vd->vd_offset.tp_col;
1264 off_y = vd->vd_offset.tp_row;
1268 vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c);
1271 vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y,
1273 /* Right bar. May be 1 pixel wider than necessary due to rounding. */
1274 vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c);
1275 /* Bottom bar. May be 1 mixel taller than necessary due to rounding. */
1276 vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c);
1282 vt_proc_alive(struct vt_window *vw)
1286 if (vw->vw_smode.mode != VT_PROCESS)
1290 if ((p = pfind(vw->vw_pid)) != NULL)
1292 if (vw->vw_proc == p)
1295 vw->vw_smode.mode = VT_AUTO;
1296 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1303 signal_vt_rel(struct vt_window *vw)
1306 if (vw->vw_smode.mode != VT_PROCESS)
1308 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1313 vw->vw_flags |= VWF_SWWAIT_REL;
1314 PROC_LOCK(vw->vw_proc);
1315 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1316 PROC_UNLOCK(vw->vw_proc);
1317 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1322 signal_vt_acq(struct vt_window *vw)
1325 if (vw->vw_smode.mode != VT_PROCESS)
1327 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1328 cnavailable(vw->vw_terminal->consdev, FALSE);
1329 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1334 vw->vw_flags |= VWF_SWWAIT_ACQ;
1335 PROC_LOCK(vw->vw_proc);
1336 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1337 PROC_UNLOCK(vw->vw_proc);
1338 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1343 finish_vt_rel(struct vt_window *vw, int release, int *s)
1346 if (vw->vw_flags & VWF_SWWAIT_REL) {
1347 vw->vw_flags &= ~VWF_SWWAIT_REL;
1349 callout_drain(&vw->vw_proc_dead_timer);
1350 vt_late_window_switch(vw->vw_switch_to);
1358 finish_vt_acq(struct vt_window *vw)
1361 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1362 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1368 #ifndef SC_NO_CUTPASTE
1370 vt_mouse_terminput_button(struct vt_device *vd, int button)
1372 struct vt_window *vw;
1374 char mouseb[6] = "\x1B[M";
1377 vw = vd->vd_curwindow;
1380 /* Translate to char position. */
1381 x = vd->vd_mx / vf->vf_width;
1382 y = vd->vd_my / vf->vf_height;
1383 /* Avoid overflow. */
1384 x = MIN(x, 255 - '!');
1385 y = MIN(y, 255 - '!');
1387 mouseb[3] = ' ' + button;
1388 mouseb[4] = '!' + x;
1389 mouseb[5] = '!' + y;
1391 for (i = 0; i < sizeof(mouseb); i++ )
1392 terminal_input_char(vw->vw_terminal, mouseb[i]);
1396 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1401 case MOUSE_BUTTON_EVENT:
1403 /* Mouse button pressed. */
1404 if (event & MOUSE_BUTTON1DOWN)
1405 vt_mouse_terminput_button(vd, 0);
1406 if (event & MOUSE_BUTTON2DOWN)
1407 vt_mouse_terminput_button(vd, 1);
1408 if (event & MOUSE_BUTTON3DOWN)
1409 vt_mouse_terminput_button(vd, 2);
1411 /* Mouse button released. */
1412 vt_mouse_terminput_button(vd, 3);
1416 case MOUSE_MOTION_EVENT:
1417 if (mouse->u.data.z < 0) {
1419 sc_mouse_input_button(vd, 64);
1420 } else if (mouse->u.data.z > 0) {
1422 sc_mouse_input_button(vd, 65);
1430 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1432 struct vt_device *vd;
1433 struct vt_window *vw;
1440 vw = vd->vd_curwindow;
1444 if (vw->vw_flags & VWF_MOUSE_HIDE)
1445 return; /* Mouse disabled. */
1447 if (vf == NULL) /* Text mode. */
1451 * TODO: add flag about pointer position changed, to not redraw chars
1452 * under mouse pointer when nothing changed.
1455 if (vw->vw_mouse_level > 0)
1456 vt_mouse_terminput(vd, type, x, y, event, cnt);
1460 case MOUSE_MOTION_EVENT:
1465 vt_termsize(vd, vf, &size);
1470 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1471 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1475 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) &&
1476 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1477 vd->vd_mx / vf->vf_width,
1478 vd->vd_my / vf->vf_height) == 1)) {
1481 * We have something marked to copy, so update pointer
1482 * to window with selection.
1484 vd->vd_markedwin = vw;
1487 case MOUSE_BUTTON_EVENT:
1495 case MOUSE_BUTTON1DOWN:
1498 mark = VTB_MARK_END;
1500 case 1: /* single click: start cut operation */
1501 mark = VTB_MARK_START;
1503 case 2: /* double click: cut a word */
1504 mark = VTB_MARK_WORD;
1506 case 3: /* triple click: cut a line */
1507 mark = VTB_MARK_ROW;
1511 case VT_MOUSE_PASTEBUTTON:
1516 if (vd->vd_markedwin == NULL)
1518 /* Get current selecton size in bytes. */
1519 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf);
1523 buf = malloc(len, M_VT, M_WAITOK | M_ZERO);
1524 /* Request cupy/paste buffer data, no more than `len' */
1525 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf,
1528 len /= sizeof(term_char_t);
1529 for (i = 0; i < len; i++ ) {
1532 terminal_input_char(vw->vw_terminal, buf[i]);
1535 /* Done, so cleanup. */
1540 case VT_MOUSE_EXTENDBUTTON:
1543 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1544 mark = VTB_MARK_EXTEND;
1549 mark = VTB_MARK_EXTEND;
1557 /* Save buttons state. */
1559 vd->vd_mstate |= event;
1561 vd->vd_mstate &= ~event;
1563 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1564 vd->vd_my / vf->vf_height) == 1) {
1566 * We have something marked to copy, so update pointer to
1567 * window with selection.
1569 vd->vd_markedwin = vw;
1574 vt_mouse_state(int show)
1576 struct vt_device *vd;
1577 struct vt_window *vw;
1580 vw = vd->vd_curwindow;
1584 vw->vw_flags |= VWF_MOUSE_HIDE;
1587 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1592 * Mark mouse position as dirty.
1594 * FIXME: See comments in vt_flush().
1596 vtbuf_mouse_cursor_position(&vw->vw_buf,
1597 vd->vd_mx / vw->vw_font->vf_width,
1598 vd->vd_my / vw->vw_font->vf_height);
1603 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1604 int nprot, vm_memattr_t *memattr)
1606 struct vt_window *vw = tm->tm_softc;
1607 struct vt_device *vd = vw->vw_device;
1609 if (vd->vd_driver->vd_fb_mmap)
1610 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1617 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1620 struct vt_window *vw = tm->tm_softc;
1621 struct vt_device *vd = vw->vw_device;
1624 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1625 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1636 cmd = VT_WAITACTIVE;
1662 ival = IOCPARM_IVAL(data);
1663 data = (caddr_t)&ival;
1668 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1669 if (*(int *)data & ~0x7f)
1673 case GIO_DEADKEYMAP:
1674 case PIO_DEADKEYMAP:
1679 case KDSKBSTATE: /* set keyboard state (locks) */
1680 case KDGKBSTATE: /* get keyboard state (locks) */
1681 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1682 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1683 case KDSETLED: /* set keyboard LED status */
1684 case KDGETLED: /* get keyboard LED status */
1685 case KBADDKBD: /* add/remove keyboard to/from mux */
1690 kbd = kbd_get_keyboard(vd->vd_keyboard);
1692 error = kbdd_ioctl(kbd, cmd, data);
1694 if (error == ENOIOCTL) {
1695 if (cmd == KDGKBTYPE) {
1696 /* always return something? XXX */
1708 kbd = kbd_get_keyboard(vd->vd_keyboard);
1710 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1713 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1714 *(int *)data = mode;
1720 mode = *(int *)data;
1725 vw->vw_kbdmode = mode;
1726 if (vw == vd->vd_curwindow) {
1731 kbd = kbd_get_keyboard(vd->vd_keyboard);
1733 error = kbdd_ioctl(kbd, KDSKBMODE,
1744 case FBIO_GETWINORG: /* get frame buffer window origin */
1745 case FBIO_GETDISPSTART: /* get display start address */
1746 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1747 case FBIO_BLANK: /* blank display */
1748 if (vd->vd_driver->vd_fb_ioctl)
1749 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1751 case CONS_BLANKTIME:
1756 *(int *)data = M_CG640x480;
1758 case CONS_BELLTYPE: /* set bell type sound */
1759 if ((*(int *)data) & CONS_QUIET_BELL)
1760 vd->vd_flags |= VDF_QUIET_BELL;
1762 vd->vd_flags &= ~VDF_QUIET_BELL;
1764 case CONS_GETINFO: {
1765 vid_info_t *vi = (vid_info_t *)data;
1767 vi->m_num = vd->vd_curwindow->vw_number + 1;
1768 /* XXX: other fields! */
1772 *(int *)data = 0x200;
1777 case CONS_MOUSECTL: {
1778 mouse_info_t *mouse = (mouse_info_t*)data;
1781 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
1782 * should not be applied to individual TTYs, but only to
1785 switch (mouse->operation) {
1787 if (vd->vd_flags & VDF_MOUSECURSOR) {
1788 vd->vd_flags &= ~VDF_MOUSECURSOR;
1789 #ifndef SC_NO_CUTPASTE
1790 vt_mouse_state(VT_MOUSE_HIDE);
1795 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
1796 vd->vd_flags |= VDF_MOUSECURSOR;
1797 vd->vd_mx = vd->vd_width / 2;
1798 vd->vd_my = vd->vd_height / 2;
1799 #ifndef SC_NO_CUTPASTE
1800 vt_mouse_state(VT_MOUSE_SHOW);
1811 error = vtfont_load((void *)data, &vf);
1815 error = vt_change_font(vw, vf);
1817 /* XXX: replace 0 with current bg color. */
1818 vt_set_border(vw, vf, 0);
1824 scrmap_t *sm = (scrmap_t *)data;
1826 /* We don't have screen maps, so return a handcrafted one. */
1827 for (i = 0; i < 256; i++)
1834 case KDENABIO: /* allow io operations */
1835 error = priv_check(td, PRIV_IO);
1838 error = securelevel_gt(td->td_ucred, 0);
1841 #if defined(__i386__)
1842 td->td_frame->tf_eflags |= PSL_IOPL;
1843 #elif defined(__amd64__)
1844 td->td_frame->tf_rflags |= PSL_IOPL;
1847 case KDDISABIO: /* disallow io operations (default) */
1848 #if defined(__i386__)
1849 td->td_frame->tf_eflags &= ~PSL_IOPL;
1850 #elif defined(__amd64__)
1851 td->td_frame->tf_rflags &= ~PSL_IOPL;
1854 case KDMKTONE: /* sound the bell */
1855 vtterm_beep(tm, *(u_int *)data);
1857 case KIOCSOUND: /* make tone (*data) hz */
1860 case CONS_SETKBD: /* set the new keyboard */
1863 if (vd->vd_keyboard != *(int *)data) {
1864 kbd = kbd_get_keyboard(*(int *)data);
1869 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
1870 (void *)vd, vt_kbdevent, vd);
1872 if (vd->vd_keyboard != -1) {
1873 kbd_release(kbd, (void *)vd);
1875 kbd = kbd_get_keyboard(i);
1876 vd->vd_keyboard = i;
1878 (void)kbdd_ioctl(kbd, KDSKBMODE,
1879 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
1881 error = EPERM; /* XXX */
1886 case CONS_RELKBD: /* release the current keyboard */
1889 if (vd->vd_keyboard != -1) {
1890 kbd = kbd_get_keyboard(vd->vd_keyboard);
1895 error = kbd_release(kbd, (void *)vd);
1897 vd->vd_keyboard = -1;
1904 win = *(int *)data - 1;
1905 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
1907 if ((win > VT_MAXWINDOWS) || (win < 0))
1909 return (vt_proc_window_switch(vd->vd_windows[win]));
1912 *(int *)data = vd->vd_curwindow->vw_number + 1;
1915 *(int *)data = vw->vw_number + 1;
1918 /* TODO: Check current state, switching can be in progress. */
1919 if ((*(int *)data) == 0x01)
1920 vw->vw_flags |= VWF_VTYLOCK;
1921 else if ((*(int *)data) == 0x02)
1922 vw->vw_flags &= ~VWF_VTYLOCK;
1928 for (i = 0; i < VT_MAXWINDOWS; i++) {
1929 vw = vd->vd_windows[i];
1932 if (!(vw->vw_flags & VWF_OPENED)) {
1933 *(int *)data = vw->vw_number + 1;
1943 i = *(unsigned int *)data;
1944 if (i > VT_MAXWINDOWS)
1947 vw = vd->vd_windows[i - 1];
1950 while (vd->vd_curwindow != vw && error == 0)
1951 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
1954 case VT_SETMODE: { /* set screen switcher mode */
1955 struct vt_mode *mode;
1958 mode = (struct vt_mode *)data;
1959 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
1960 if (vw->vw_smode.mode == VT_PROCESS) {
1961 p1 = pfind(vw->vw_pid);
1962 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
1965 DPRINTF(5, "error EPERM\n");
1971 if (mode->mode == VT_AUTO) {
1972 vw->vw_smode.mode = VT_AUTO;
1975 DPRINTF(5, "VT_AUTO, ");
1976 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1977 cnavailable(vw->vw_terminal->consdev, TRUE);
1978 /* were we in the middle of the vty switching process? */
1979 if (finish_vt_rel(vw, TRUE, &s) == 0)
1980 DPRINTF(5, "reset WAIT_REL, ");
1981 if (finish_vt_acq(vw) == 0)
1982 DPRINTF(5, "reset WAIT_ACQ, ");
1984 } else if (mode->mode == VT_PROCESS) {
1985 if (!ISSIGVALID(mode->relsig) ||
1986 !ISSIGVALID(mode->acqsig) ||
1987 !ISSIGVALID(mode->frsig)) {
1988 DPRINTF(5, "error EINVAL\n");
1991 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
1992 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
1993 vw->vw_proc = td->td_proc;
1994 vw->vw_pid = vw->vw_proc->p_pid;
1995 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1996 cnavailable(vw->vw_terminal->consdev, FALSE);
1998 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2005 case VT_GETMODE: /* get screen switcher mode */
2006 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2009 case VT_RELDISP: /* screen switcher ioctl */
2011 * This must be the current vty which is in the VT_PROCESS
2014 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2018 /* ...and this process is controlling it. */
2019 if (vw->vw_proc != td->td_proc) {
2023 switch(*(int *)data) {
2024 case VT_FALSE: /* user refuses to release screen, abort */
2025 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2026 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2027 SC_DRIVER_NAME, VT_UNIT(vw));
2029 case VT_TRUE: /* user has released screen, go on */
2030 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2031 if (vw->vw_flags & VWF_SWWAIT_REL) {
2032 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2033 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2034 SC_DRIVER_NAME, VT_UNIT(vw));
2039 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2040 if ((error = finish_vt_acq(vw)) == 0)
2041 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2042 SC_DRIVER_NAME, VT_UNIT(vw));
2053 static struct vt_window *
2054 vt_allocate_window(struct vt_device *vd, unsigned int window)
2056 struct vt_window *vw;
2057 struct terminal *tm;
2061 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2063 vw->vw_number = window;
2064 vw->vw_kbdmode = K_XLATE;
2066 if ((vd->vd_flags & VDF_TEXTMODE) == 0)
2067 vw->vw_font = vtfont_ref(&vt_font_default);
2069 vt_termsize(vd, vw->vw_font, &size);
2070 vt_winsize(vd, vw->vw_font, &wsz);
2071 vtbuf_init(&vw->vw_buf, &size);
2073 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2074 terminal_set_winsize(tm, &wsz);
2075 vd->vd_windows[window] = vw;
2076 callout_init(&vw->vw_proc_dead_timer, 0);
2082 vt_upgrade(struct vt_device *vd)
2084 struct vt_window *vw;
2087 if (!vty_enabled(VTY_VT))
2090 for (i = 0; i < VT_MAXWINDOWS; i++) {
2091 vw = vd->vd_windows[i];
2094 vw = vt_allocate_window(vd, i);
2096 if (!(vw->vw_flags & VWF_READY)) {
2097 callout_init(&vw->vw_proc_dead_timer, 0);
2098 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2099 vw->vw_flags |= VWF_READY;
2100 if (vw->vw_flags & VWF_CONSOLE) {
2101 /* For existing console window. */
2102 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2103 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2109 if (vd->vd_curwindow == NULL)
2110 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2112 if (!(vd->vd_flags & VDF_ASYNC)) {
2113 /* Attach keyboard. */
2114 vt_allocate_keyboard(vd);
2116 /* Init 25 Hz timer. */
2117 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2119 /* Start timer when everything ready. */
2120 vd->vd_flags |= VDF_ASYNC;
2121 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2122 vd->vd_timer_armed = 1;
2127 /* Refill settings with new sizes. */
2132 vt_resize(struct vt_device *vd)
2134 struct vt_window *vw;
2137 for (i = 0; i < VT_MAXWINDOWS; i++) {
2138 vw = vd->vd_windows[i];
2140 /* Assign default font to window, if not textmode. */
2141 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2142 vw->vw_font = vtfont_ref(&vt_font_default);
2144 /* Resize terminal windows */
2145 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2146 DPRINTF(100, "%s: vt_change_font() is busy, "
2147 "window %d\n", __func__, i);
2153 vt_allocate(struct vt_driver *drv, void *softc)
2155 struct vt_device *vd;
2157 if (!vty_enabled(VTY_VT))
2160 if (main_vd->vd_driver == NULL) {
2161 main_vd->vd_driver = drv;
2162 printf("VT: initialize with new VT driver \"%s\".\n",
2166 * Check if have rights to replace current driver. For example:
2167 * it is bad idea to replace KMS driver with generic VGA one.
2169 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2170 printf("VT: Driver priority %d too low. Current %d\n ",
2171 drv->vd_priority, main_vd->vd_driver->vd_priority);
2174 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2175 main_vd->vd_driver->vd_name, drv->vd_name);
2179 if (drv->vd_maskbitbltchr == NULL)
2180 drv->vd_maskbitbltchr = drv->vd_bitbltchr;
2182 if (vd->vd_flags & VDF_ASYNC) {
2183 /* Stop vt_flush periodic task. */
2184 vt_suspend_flush_timer(vd);
2186 * Mute current terminal until we done. vt_change_font (called
2187 * from vt_resize) will unmute it.
2189 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2193 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2196 vd->vd_flags &= ~VDF_TEXTMODE;
2198 vd->vd_driver = drv;
2199 vd->vd_softc = softc;
2200 vd->vd_driver->vd_init(vd);
2203 /* Update windows sizes and initialize last items. */
2207 if (vd->vd_flags & VDF_SPLASH)
2211 if (vd->vd_flags & VDF_ASYNC) {
2212 /* Allow to put chars now. */
2213 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2214 /* Rerun timer for screen updates. */
2215 vt_resume_flush_timer(vd, 0);
2219 * Register as console. If it already registered, cnadd() will ignore
2222 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2229 if (vt_suspendswitch == 0)
2231 /* Save current window. */
2232 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2233 /* Ask holding process to free window and switch to console window */
2234 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2241 if (vt_suspendswitch == 0)
2243 /* Switch back to saved window */
2244 if (main_vd->vd_savedwindow != NULL)
2245 vt_proc_window_switch(main_vd->vd_savedwindow);
2246 main_vd->vd_savedwindow = NULL;