2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, 2013 The FreeBSD Foundation
7 * This software was developed by Ed Schouten under sponsorship from the
10 * Portions of this software were developed by Oleksandr Rybalko
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_compat.h"
40 #include <sys/param.h>
41 #include <sys/consio.h>
42 #include <sys/eventhandler.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/power.h>
53 #include <sys/random.h>
54 #include <sys/reboot.h>
55 #include <sys/systm.h>
56 #include <sys/terminal.h>
58 #include <dev/kbd/kbdreg.h>
59 #include <dev/vt/vt.h>
61 #if defined(__i386__) || defined(__amd64__)
62 #include <machine/psl.h>
63 #include <machine/frame.h>
66 static tc_bell_t vtterm_bell;
67 static tc_cursor_t vtterm_cursor;
68 static tc_putchar_t vtterm_putchar;
69 static tc_fill_t vtterm_fill;
70 static tc_copy_t vtterm_copy;
71 static tc_param_t vtterm_param;
72 static tc_done_t vtterm_done;
74 static tc_cnprobe_t vtterm_cnprobe;
75 static tc_cngetc_t vtterm_cngetc;
77 static tc_cngrab_t vtterm_cngrab;
78 static tc_cnungrab_t vtterm_cnungrab;
80 static tc_opened_t vtterm_opened;
81 static tc_ioctl_t vtterm_ioctl;
82 static tc_mmap_t vtterm_mmap;
84 const struct terminal_class vt_termclass = {
85 .tc_bell = vtterm_bell,
86 .tc_cursor = vtterm_cursor,
87 .tc_putchar = vtterm_putchar,
88 .tc_fill = vtterm_fill,
89 .tc_copy = vtterm_copy,
90 .tc_param = vtterm_param,
91 .tc_done = vtterm_done,
93 .tc_cnprobe = vtterm_cnprobe,
94 .tc_cngetc = vtterm_cngetc,
96 .tc_cngrab = vtterm_cngrab,
97 .tc_cnungrab = vtterm_cnungrab,
99 .tc_opened = vtterm_opened,
100 .tc_ioctl = vtterm_ioctl,
101 .tc_mmap = vtterm_mmap,
105 * Use a constant timer of 25 Hz to redraw the screen.
107 * XXX: In theory we should only fire up the timer when there is really
108 * activity. Unfortunately we cannot always start timers. We really
109 * don't want to process kernel messages synchronously, because it
110 * really slows down the system.
112 #define VT_TIMERFREQ 25
114 /* Bell pitch/duration. */
115 #define VT_BELLDURATION ((5 * hz + 99) / 100)
116 #define VT_BELLPITCH 800
118 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
121 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
122 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
123 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
124 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
125 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
126 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
128 /* Allow to disable some keyboard combinations. */
129 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
130 "See kbdmap(5) to configure.");
131 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
132 "See kbdmap(5) to configure.");
133 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
134 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
135 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
136 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
137 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
138 "See kbdmap(5) to configure.");
140 /* Used internally, not a tunable. */
141 int vt_draw_logo_cpus;
142 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
143 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
144 "(0 = do not override)");
145 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
146 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
147 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
149 static unsigned int vt_unit = 0;
150 static MALLOC_DEFINE(M_VT, "vt", "vt device");
151 struct vt_device *main_vd = &vt_consdev;
154 extern unsigned int vt_logo_width;
155 extern unsigned int vt_logo_height;
156 extern unsigned int vt_logo_depth;
157 extern unsigned char vt_logo_image[];
159 #define vtterm_draw_cpu_logos(...) do {} while (0)
160 const unsigned int vt_logo_sprite_height;
164 extern struct vt_font vt_font_default;
165 #ifndef SC_NO_CUTPASTE
166 extern struct vt_mouse_cursor vt_default_mouse_pointer;
169 static int signal_vt_rel(struct vt_window *);
170 static int signal_vt_acq(struct vt_window *);
171 static int finish_vt_rel(struct vt_window *, int, int *);
172 static int finish_vt_acq(struct vt_window *);
173 static int vt_window_switch(struct vt_window *);
174 static int vt_late_window_switch(struct vt_window *);
175 static int vt_proc_alive(struct vt_window *);
176 static void vt_resize(struct vt_device *);
177 static void vt_update_static(void *);
178 #ifndef SC_NO_CUTPASTE
179 static void vt_mouse_paste(void);
181 static void vt_suspend_handler(void *priv);
182 static void vt_resume_handler(void *priv);
184 SET_DECLARE(vt_drv_set, struct vt_driver);
186 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT))
187 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH))
189 struct terminal vt_consterm;
190 static struct vt_window vt_conswindow;
191 struct vt_device vt_consdev = {
194 .vd_prev_driver = NULL,
195 .vd_prev_softc = NULL,
196 .vd_flags = VDF_INVALID,
197 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
198 .vd_curwindow = &vt_conswindow,
201 #ifndef SC_NO_CUTPASTE
207 .vd_mcursor = &vt_default_mouse_pointer,
208 .vd_mcursor_fg = TC_WHITE,
209 .vd_mcursor_bg = TC_BLACK,
212 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
213 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
214 static struct vt_window vt_conswindow = {
215 .vw_number = VT_CONSWINDOW,
216 .vw_flags = VWF_CONSOLE,
218 .vb_buffer = &vt_constextbuf[0],
219 .vb_rows = &vt_constextbufrows[0],
220 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
223 .vb_flags = VBF_STATIC,
224 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
225 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
231 .vw_device = &vt_consdev,
232 .vw_terminal = &vt_consterm,
233 .vw_kbdmode = K_XLATE,
236 struct terminal vt_consterm = {
237 .tm_class = &vt_termclass,
238 .tm_softc = &vt_conswindow,
241 static struct consdev vt_consterm_consdev = {
242 .cn_ops = &termcn_cnops,
243 .cn_arg = &vt_consterm,
247 /* Add to set of consoles. */
248 DATA_SET(cons_set, vt_consterm_consdev);
251 * Right after kmem is done to allow early drivers to use locking and allocate
254 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
256 /* Delay until all devices attached, to not waste time. */
257 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
260 /* Initialize locks/mem depended members. */
262 vt_update_static(void *dummy)
265 if (!vty_enabled(VTY_VT))
267 if (main_vd->vd_driver != NULL)
268 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
269 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
270 main_vd->vd_width, main_vd->vd_height);
272 printf("VT: init without driver.\n");
274 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
275 cv_init(&main_vd->vd_winswitch, "vtwswt");
279 vt_schedule_flush(struct vt_device *vd, int ms)
283 /* Default to initial value. */
284 ms = 1000 / VT_TIMERFREQ;
286 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
290 vt_resume_flush_timer(struct vt_device *vd, int ms)
293 if (!(vd->vd_flags & VDF_ASYNC) ||
294 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
297 vt_schedule_flush(vd, ms);
301 vt_suspend_flush_timer(struct vt_device *vd)
304 * As long as this function is called locked, callout_stop()
305 * has the same effect like callout_drain() with regard to
306 * preventing the callback function from executing.
308 VT_LOCK_ASSERT(vd, MA_OWNED);
310 if (!(vd->vd_flags & VDF_ASYNC) ||
311 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
314 callout_stop(&vd->vd_timer);
318 vt_switch_timer(void *arg)
321 vt_late_window_switch((struct vt_window *)arg);
325 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
330 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
336 vw->vw_kbdmode = mode;
342 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
346 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
354 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
359 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
365 vw->vw_kbdstate &= ~LOCK_MASK;
366 vw->vw_kbdstate |= state & LOCK_MASK;
372 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
376 state = vw->vw_kbdstate & LOCK_MASK;
377 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
385 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
390 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
396 vw->vw_kbdstate &= ~LED_MASK;
397 vw->vw_kbdstate |= leds & LED_MASK;
403 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
407 leds = vw->vw_kbdstate & LED_MASK;
408 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
416 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
419 DPRINTF(40, "%s\n", __func__);
420 curvw->vw_switch_to = vw;
421 /* Set timer to allow switch in case when process hang. */
422 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
423 vt_switch_timer, (void *)vw);
424 /* Notify process about vt switch attempt. */
425 DPRINTF(30, "%s: Notify process.\n", __func__);
426 signal_vt_rel(curvw);
432 vt_window_postswitch(struct vt_window *vw)
439 /* vt_late_window_switch will done VT switching for regular case. */
441 vt_late_window_switch(struct vt_window *vw)
445 callout_stop(&vw->vw_proc_dead_timer);
447 ret = vt_window_switch(vw);
451 /* Notify owner process about terminal availability. */
452 if (vw->vw_smode.mode == VT_PROCESS) {
453 ret = vt_window_postswitch(vw);
460 vt_proc_window_switch(struct vt_window *vw)
462 struct vt_window *curvw;
463 struct vt_device *vd;
466 /* Prevent switching to NULL */
468 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
472 curvw = vd->vd_curwindow;
474 /* Check if virtual terminal is locked */
475 if (curvw->vw_flags & VWF_VTYLOCK)
478 /* Check if switch already in progress */
479 if (curvw->vw_flags & VWF_SWWAIT_REL) {
480 /* Check if switching to same window */
481 if (curvw->vw_switch_to == vw) {
482 DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
483 return (0); /* success */
485 DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
489 /* Avoid switching to already selected window */
491 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
492 return (0); /* success */
495 /* Ask current process permission to switch away. */
496 if (curvw->vw_smode.mode == VT_PROCESS) {
497 DPRINTF(30, "%s: VT_PROCESS ", __func__);
498 if (vt_proc_alive(curvw) == FALSE) {
499 DPRINTF(30, "Dead. Cleaning.");
502 DPRINTF(30, "%s: Signaling process.\n", __func__);
503 /* Alive, try to ask him. */
504 ret = vt_window_preswitch(vw, curvw);
505 /* Wait for process answer or timeout. */
511 ret = vt_late_window_switch(vw);
515 /* Switch window ignoring process locking. */
517 vt_window_switch(struct vt_window *vw)
519 struct vt_device *vd = vw->vw_device;
520 struct vt_window *curvw = vd->vd_curwindow;
529 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
534 vt_suspend_flush_timer(vd);
536 vd->vd_curwindow = vw;
537 vd->vd_flags |= VDF_INVALID;
538 cv_broadcast(&vd->vd_winswitch);
541 if (vd->vd_driver->vd_postswitch)
542 vd->vd_driver->vd_postswitch(vd);
544 vt_resume_flush_timer(vd, 0);
546 /* Restore per-window keyboard mode. */
548 kbd = kbd_get_keyboard(vd->vd_keyboard);
550 if (curvw->vw_kbdmode == K_XLATE)
551 vt_save_kbd_state(curvw, kbd);
553 vt_update_kbd_mode(vw, kbd);
554 vt_update_kbd_state(vw, kbd);
557 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
563 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
566 size->tp_row = vd->vd_height;
567 if (vt_draw_logo_cpus)
568 size->tp_row -= vt_logo_sprite_height;
569 size->tp_col = vd->vd_width;
571 size->tp_row /= vf->vf_height;
572 size->tp_col /= vf->vf_width;
577 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
580 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
581 if (vt_draw_logo_cpus)
582 rect->tr_begin.tp_row = vt_logo_sprite_height;
584 rect->tr_end.tp_row = vd->vd_height;
585 rect->tr_end.tp_col = vd->vd_width;
588 rect->tr_begin.tp_row =
589 howmany(rect->tr_begin.tp_row, vf->vf_height);
591 rect->tr_end.tp_row /= vf->vf_height;
592 rect->tr_end.tp_col /= vf->vf_width;
597 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
600 size->ws_ypixel = vd->vd_height;
601 if (vt_draw_logo_cpus)
602 size->ws_ypixel -= vt_logo_sprite_height;
603 size->ws_row = size->ws_ypixel;
604 size->ws_col = size->ws_xpixel = vd->vd_width;
606 size->ws_row /= vf->vf_height;
607 size->ws_col /= vf->vf_width;
612 vt_compute_drawable_area(struct vt_window *vw)
614 struct vt_device *vd;
620 if (vw->vw_font == NULL) {
621 vw->vw_draw_area.tr_begin.tp_col = 0;
622 vw->vw_draw_area.tr_begin.tp_row = 0;
623 if (vt_draw_logo_cpus)
624 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
625 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
626 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
633 * Compute the drawable area, so that the text is centered on
637 height = vd->vd_height;
638 if (vt_draw_logo_cpus)
639 height -= vt_logo_sprite_height;
640 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
641 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
642 if (vt_draw_logo_cpus)
643 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
644 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
645 rounddown(vd->vd_width, vf->vf_width);
646 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
647 rounddown(height, vf->vf_height);
651 vt_scroll(struct vt_window *vw, int offset, int whence)
656 if ((vw->vw_flags & VWF_SCROLL) == 0)
659 vt_termsize(vw->vw_device, vw->vw_font, &size);
661 diff = vthistory_seek(&vw->vw_buf, offset, whence);
663 vw->vw_device->vd_flags |= VDF_INVALID;
664 vt_resume_flush_timer(vw->vw_device, 0);
668 vt_machine_kbdevent(int c)
672 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
674 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
676 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
678 shutdown_nice(RB_HALT);
680 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
681 #ifndef SC_NO_CUTPASTE
682 /* Insert text from cut-paste buffer. */
686 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
688 shutdown_nice(RB_HALT|RB_POWEROFF);
690 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
692 * Request to immediate panic if sysctl
693 * kern.vt.enable_panic_key allow it.
696 panic("Forced by the panic key");
698 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
700 shutdown_nice(RB_AUTOBOOT);
702 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
703 /* Force activatation/deactivation of the screen saver. */
706 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
707 /* Put machine into Stand-By mode. */
708 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
710 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
711 /* Suspend machine. */
712 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
720 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
722 struct vt_device *vd;
726 /* Only special keys handled in ScrollLock mode */
727 if ((c & SPCLKEY) == 0)
733 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
734 vw = vd->vd_windows[c - F_SCR];
735 vt_proc_window_switch(vw);
743 /* Turn scrolling off. */
744 vt_scroll(vw, 0, VHS_END);
745 VTBUF_SLCK_DISABLE(&vw->vw_buf);
746 vw->vw_flags &= ~VWF_SCROLL;
749 case FKEY | F(49): /* Home key. */
750 vt_scroll(vw, 0, VHS_SET);
752 case FKEY | F(50): /* Arrow up. */
753 vt_scroll(vw, -1, VHS_CUR);
755 case FKEY | F(51): /* Page up. */
756 vt_termsize(vd, vw->vw_font, &size);
757 vt_scroll(vw, -size.tp_row, VHS_CUR);
759 case FKEY | F(57): /* End key. */
760 vt_scroll(vw, 0, VHS_END);
762 case FKEY | F(58): /* Arrow down. */
763 vt_scroll(vw, 1, VHS_CUR);
765 case FKEY | F(59): /* Page down. */
766 vt_termsize(vd, vw->vw_font, &size);
767 vt_scroll(vw, size.tp_row, VHS_CUR);
776 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
778 struct vt_window *vw = vd->vd_curwindow;
780 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
781 #if VT_ALT_TO_ESC_HACK
783 switch (c & ~RELKEY) {
784 case (SPCLKEY | RALT):
785 if (vt_enable_altgr != 0)
787 case (SPCLKEY | LALT):
788 vd->vd_kbstate &= ~ALKED;
790 /* Other keys ignored for RELKEY event. */
793 switch (c & ~RELKEY) {
794 case (SPCLKEY | RALT):
795 if (vt_enable_altgr != 0)
797 case (SPCLKEY | LALT):
798 vd->vd_kbstate |= ALKED;
803 /* Other keys ignored for RELKEY event. */
807 if (vt_machine_kbdevent(c))
810 if (vw->vw_flags & VWF_SCROLL) {
811 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
812 /* Scroll mode keys handled, nothing to do more. */
819 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
820 vw = vd->vd_windows[c - F_SCR];
821 vt_proc_window_switch(vw);
827 /* Switch to next VT. */
828 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
829 vw = vd->vd_windows[c];
830 vt_proc_window_switch(vw);
833 /* Switch to previous VT. */
834 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
835 vw = vd->vd_windows[c];
836 vt_proc_window_switch(vw);
839 vt_save_kbd_state(vw, kbd);
841 if (vw->vw_kbdstate & SLKED) {
842 /* Turn scrolling on. */
843 vw->vw_flags |= VWF_SCROLL;
844 VTBUF_SLCK_ENABLE(&vw->vw_buf);
846 /* Turn scrolling off. */
847 vw->vw_flags &= ~VWF_SCROLL;
848 VTBUF_SLCK_DISABLE(&vw->vw_buf);
849 vt_scroll(vw, 0, VHS_END);
854 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
855 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
856 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
857 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
858 /* F1 through F12 keys. */
859 terminal_input_special(vw->vw_terminal,
860 TKEY_F1 + c - (FKEY | F(1)));
862 case FKEY | F(49): /* Home key. */
863 terminal_input_special(vw->vw_terminal, TKEY_HOME);
865 case FKEY | F(50): /* Arrow up. */
866 terminal_input_special(vw->vw_terminal, TKEY_UP);
868 case FKEY | F(51): /* Page up. */
869 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
871 case FKEY | F(53): /* Arrow left. */
872 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
874 case FKEY | F(55): /* Arrow right. */
875 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
877 case FKEY | F(57): /* End key. */
878 terminal_input_special(vw->vw_terminal, TKEY_END);
880 case FKEY | F(58): /* Arrow down. */
881 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
883 case FKEY | F(59): /* Page down. */
884 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
886 case FKEY | F(60): /* Insert key. */
887 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
889 case FKEY | F(61): /* Delete key. */
890 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
893 } else if (KEYFLAGS(c) == 0) {
894 /* Don't do UTF-8 conversion when doing raw mode. */
895 if (vw->vw_kbdmode == K_XLATE) {
896 #if VT_ALT_TO_ESC_HACK
897 if (vd->vd_kbstate & ALKED) {
899 * Prepend ESC sequence if one of ALT keys down.
901 terminal_input_char(vw->vw_terminal, 0x1b);
905 kdb_alt_break(c, &vd->vd_altbrk);
907 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
909 terminal_input_raw(vw->vw_terminal, c);
915 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
917 struct vt_device *vd = arg;
923 case KBDIO_UNLOADING:
925 vd->vd_keyboard = -1;
926 kbd_release(kbd, (void *)vd);
933 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
934 vt_processkey(kbd, vd, c);
940 vt_allocate_keyboard(struct vt_device *vd)
946 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
948 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
949 k0 = kbd_get_keyboard(idx0);
951 for (idx = kbd_find_keyboard2("*", -1, 0);
953 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
954 k = kbd_get_keyboard(idx);
956 if (idx == idx0 || KBD_IS_BUSY(k))
959 bzero(&ki, sizeof(ki));
960 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
961 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
962 ki.kb_unit = k->kb_unit;
964 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
967 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
968 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
970 DPRINTF(10, "%s: No keyboard found.\n", __func__);
974 vd->vd_keyboard = idx0;
975 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
981 vtterm_bell(struct terminal *tm)
983 struct vt_window *vw = tm->tm_softc;
984 struct vt_device *vd = vw->vw_device;
989 if (vd->vd_flags & VDF_QUIET_BELL)
992 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
996 vtterm_beep(struct terminal *tm, u_int param)
1000 if (!vt_enable_bell)
1003 if ((param == 0) || ((param & 0xffff) == 0)) {
1008 period = ((param >> 16) & 0xffff) * hz / 1000;
1009 freq = 1193182 / (param & 0xffff);
1011 sysbeep(freq, period);
1015 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1017 struct vt_window *vw = tm->tm_softc;
1019 vtbuf_cursor_position(&vw->vw_buf, p);
1020 vt_resume_flush_timer(vw->vw_device, 0);
1024 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1026 struct vt_window *vw = tm->tm_softc;
1028 vtbuf_putchar(&vw->vw_buf, p, c);
1029 vt_resume_flush_timer(vw->vw_device, 0);
1033 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1035 struct vt_window *vw = tm->tm_softc;
1037 vtbuf_fill_locked(&vw->vw_buf, r, c);
1038 vt_resume_flush_timer(vw->vw_device, 0);
1042 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1043 const term_pos_t *p)
1045 struct vt_window *vw = tm->tm_softc;
1047 vtbuf_copy(&vw->vw_buf, r, p);
1048 vt_resume_flush_timer(vw->vw_device, 0);
1052 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1054 struct vt_window *vw = tm->tm_softc;
1057 case TP_SETLOCALCURSOR:
1059 * 0 means normal (usually block), 1 means hidden, and
1060 * 2 means blinking (always block) for compatibility with
1061 * syscons. We don't support any changes except hiding,
1062 * so must map 2 to 0.
1064 arg = (arg == 1) ? 0 : 1;
1067 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1068 vt_resume_flush_timer(vw->vw_device, 0);
1071 vw->vw_mouse_level = arg;
1077 vt_determine_colors(term_char_t c, int cursor,
1078 term_color_t *fg, term_color_t *bg)
1085 *fg = TCHAR_FGCOLOR(c);
1086 if (TCHAR_FORMAT(c) & TF_BOLD)
1087 *fg = TCOLOR_LIGHT(*fg);
1088 *bg = TCHAR_BGCOLOR(c);
1089 if (TCHAR_FORMAT(c) & TF_BLINK)
1090 *bg = TCOLOR_LIGHT(*bg);
1092 if (TCHAR_FORMAT(c) & TF_REVERSE)
1104 #ifndef SC_NO_CUTPASTE
1106 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1108 unsigned int mx, my;
1111 * We use the cursor position saved during the current refresh,
1112 * in case the cursor moved since.
1114 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1115 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1117 if (mx >= area->tr_end.tp_col ||
1118 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1119 my >= area->tr_end.tp_row ||
1120 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1126 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1129 struct vt_window *vw;
1133 vw = vd->vd_curwindow;
1136 x = vd->vd_mx_drawn;
1137 y = vd->vd_my_drawn;
1140 area.tr_begin.tp_col = x / vf->vf_width;
1141 area.tr_begin.tp_row = y / vf->vf_height;
1142 area.tr_end.tp_col =
1143 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1144 area.tr_end.tp_row =
1145 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1148 * No font loaded (ie. vt_vga operating in textmode).
1150 * FIXME: This fake area needs to be revisited once the
1151 * mouse cursor is supported in vt_vga's textmode.
1153 area.tr_begin.tp_col = x;
1154 area.tr_begin.tp_row = y;
1155 area.tr_end.tp_col = x + 2;
1156 area.tr_end.tp_row = y + 2;
1159 vtbuf_dirty(&vw->vw_buf, &area);
1164 vt_set_border(struct vt_device *vd, const term_rect_t *area,
1165 const term_color_t c)
1167 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
1169 if (drawrect == NULL)
1173 if (area->tr_begin.tp_row > 0)
1174 drawrect(vd, 0, 0, vd->vd_width - 1,
1175 area->tr_begin.tp_row - 1, 1, c);
1178 if (area->tr_begin.tp_col > 0)
1179 drawrect(vd, 0, area->tr_begin.tp_row,
1180 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
1183 if (area->tr_end.tp_col < vd->vd_width)
1184 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
1185 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
1188 if (area->tr_end.tp_row < vd->vd_height)
1189 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
1190 vd->vd_height - 1, 1, c);
1194 vt_flush(struct vt_device *vd)
1196 struct vt_window *vw;
1199 #ifndef SC_NO_CUTPASTE
1200 int cursor_was_shown, cursor_moved;
1203 vw = vd->vd_curwindow;
1207 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1211 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1214 #ifndef SC_NO_CUTPASTE
1215 cursor_was_shown = vd->vd_mshown;
1216 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1217 vd->vd_my != vd->vd_my_drawn);
1219 /* Check if the cursor should be displayed or not. */
1220 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1221 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1222 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1229 * If the cursor changed display state or moved, we must mark
1230 * the old position as dirty, so that it's erased.
1232 if (cursor_was_shown != vd->vd_mshown ||
1233 (vd->vd_mshown && cursor_moved))
1234 vt_mark_mouse_position_as_dirty(vd);
1237 * Save position of the mouse cursor. It's used by backends to
1238 * know where to draw the cursor and during the next refresh to
1239 * erase the previous position.
1241 vd->vd_mx_drawn = vd->vd_mx;
1242 vd->vd_my_drawn = vd->vd_my;
1245 * If the cursor is displayed and has moved since last refresh,
1246 * mark the new position as dirty.
1248 if (vd->vd_mshown && cursor_moved)
1249 vt_mark_mouse_position_as_dirty(vd);
1252 vtbuf_undirty(&vw->vw_buf, &tarea);
1254 /* Force a full redraw when the screen contents are invalid. */
1255 if (vd->vd_flags & VDF_INVALID) {
1256 vd->vd_flags &= ~VDF_INVALID;
1258 vt_set_border(vd, &vw->vw_draw_area, TC_BLACK);
1259 vt_termrect(vd, vf, &tarea);
1260 if (vt_draw_logo_cpus)
1261 vtterm_draw_cpu_logos(vd);
1264 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1265 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1275 struct vt_device *vd;
1279 /* Update screen if required. */
1280 changed = vt_flush(vd);
1282 /* Schedule for next update. */
1284 vt_schedule_flush(vd, 0);
1286 vd->vd_timer_armed = 0;
1290 vtterm_done(struct terminal *tm)
1292 struct vt_window *vw = tm->tm_softc;
1293 struct vt_device *vd = vw->vw_device;
1295 if (kdb_active || panicstr != NULL) {
1296 /* Switch to the debugger. */
1297 if (vd->vd_curwindow != vw) {
1298 vd->vd_curwindow = vw;
1299 vd->vd_flags |= VDF_INVALID;
1300 if (vd->vd_driver->vd_postswitch)
1301 vd->vd_driver->vd_postswitch(vd);
1303 vd->vd_flags &= ~VDF_SPLASH;
1305 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1312 vtterm_splash(struct vt_device *vd)
1314 vt_axis_t top, left;
1316 /* Display a nice boot splash. */
1317 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1319 top = (vd->vd_height - vt_logo_height) / 2;
1320 left = (vd->vd_width - vt_logo_width) / 2;
1321 switch (vt_logo_depth) {
1323 /* XXX: Unhardcode colors! */
1324 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1325 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1326 left, top, TC_WHITE, TC_BLACK);
1328 vd->vd_flags |= VDF_SPLASH;
1335 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1337 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1338 struct vt_window *vw = tm->tm_softc;
1339 struct vt_device *vd = vw->vw_device;
1344 if (!vty_enabled(VTY_VT))
1347 if (vd->vd_flags & VDF_INITIALIZED)
1348 /* Initialization already done. */
1351 SET_FOREACH(vtdlist, vt_drv_set) {
1353 if (vtd->vd_probe == NULL)
1355 if (vtd->vd_probe(vd) == CN_DEAD)
1357 if ((vtdbest == NULL) ||
1358 (vtd->vd_priority > vtdbest->vd_priority))
1361 if (vtdbest == NULL) {
1362 cp->cn_pri = CN_DEAD;
1363 vd->vd_flags |= VDF_DEAD;
1365 vd->vd_driver = vtdbest;
1366 cp->cn_pri = vd->vd_driver->vd_init(vd);
1369 /* Check if driver's vt_init return CN_DEAD. */
1370 if (cp->cn_pri == CN_DEAD) {
1371 vd->vd_flags |= VDF_DEAD;
1374 /* Initialize any early-boot keyboard drivers */
1375 kbd_configure(KB_CONF_PROBE_ONLY);
1377 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1378 vd->vd_windows[VT_CONSWINDOW] = vw;
1379 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1381 /* Attach default font if not in TEXTMODE. */
1382 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1383 vw->vw_font = vtfont_ref(&vt_font_default);
1384 vt_compute_drawable_area(vw);
1388 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1389 * that we have the real viewable size, fix it in the static
1392 if (vd->vd_width != 0 && vd->vd_height != 0)
1393 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1395 vtbuf_init_early(&vw->vw_buf);
1396 vt_winsize(vd, vw->vw_font, &wsz);
1397 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1399 attr.ta_format = TCHAR_FORMAT(c);
1400 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1401 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1402 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1404 if (vtdbest != NULL) {
1409 vd->vd_flags |= VDF_INITIALIZED;
1414 vtterm_cngetc(struct terminal *tm)
1416 struct vt_window *vw = tm->tm_softc;
1417 struct vt_device *vd = vw->vw_device;
1421 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1422 return (*vw->vw_kbdsq++);
1424 /* Make sure the splash screen is not there. */
1425 if (vd->vd_flags & VDF_SPLASH) {
1427 vd->vd_flags &= ~VDF_SPLASH;
1428 /* Mark screen as invalid to force update */
1429 vd->vd_flags |= VDF_INVALID;
1433 /* Stripped down keyboard handler. */
1434 kbd = kbd_get_keyboard(vd->vd_keyboard);
1438 /* Force keyboard input mode to K_XLATE */
1439 vw->vw_kbdmode = K_XLATE;
1440 vt_update_kbd_mode(vw, kbd);
1442 /* Switch the keyboard to polling to make it work here. */
1443 kbdd_poll(kbd, TRUE);
1444 c = kbdd_read_char(kbd, 0);
1445 kbdd_poll(kbd, FALSE);
1449 if (vw->vw_flags & VWF_SCROLL) {
1450 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1455 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1459 vt_save_kbd_state(vw, kbd);
1460 if (vw->vw_kbdstate & SLKED) {
1461 /* Turn scrolling on. */
1462 vw->vw_flags |= VWF_SCROLL;
1463 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1465 /* Turn scrolling off. */
1466 vt_scroll(vw, 0, VHS_END);
1467 vw->vw_flags &= ~VWF_SCROLL;
1468 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1471 /* XXX: KDB can handle history. */
1472 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1473 vw->vw_kbdsq = "\x1b[A";
1475 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1476 vw->vw_kbdsq = "\x1b[B";
1478 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1479 vw->vw_kbdsq = "\x1b[C";
1481 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1482 vw->vw_kbdsq = "\x1b[D";
1486 /* Force refresh to make scrollback work. */
1488 } else if (KEYFLAGS(c) == 0) {
1489 return (KEYCHAR(c));
1492 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1493 return (*vw->vw_kbdsq++);
1499 vtterm_cngrab(struct terminal *tm)
1501 struct vt_device *vd;
1502 struct vt_window *vw;
1509 vt_window_switch(vw);
1511 kbd = kbd_get_keyboard(vd->vd_keyboard);
1515 if (vw->vw_grabbed++ > 0)
1519 * Make sure the keyboard is accessible even when the kbd device
1520 * driver is disabled.
1524 /* We shall always use the keyboard in the XLATE mode here. */
1525 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1526 vw->vw_kbdmode = K_XLATE;
1527 vt_update_kbd_mode(vw, kbd);
1529 kbdd_poll(kbd, TRUE);
1533 vtterm_cnungrab(struct terminal *tm)
1535 struct vt_device *vd;
1536 struct vt_window *vw;
1542 kbd = kbd_get_keyboard(vd->vd_keyboard);
1546 if (--vw->vw_grabbed > 0)
1549 kbdd_poll(kbd, FALSE);
1551 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1552 vt_update_kbd_mode(vw, kbd);
1557 vtterm_opened(struct terminal *tm, int opened)
1559 struct vt_window *vw = tm->tm_softc;
1560 struct vt_device *vd = vw->vw_device;
1563 vd->vd_flags &= ~VDF_SPLASH;
1565 vw->vw_flags |= VWF_OPENED;
1567 vw->vw_flags &= ~VWF_OPENED;
1568 /* TODO: finish ACQ/REL */
1574 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1576 struct vt_device *vd = vw->vw_device;
1577 struct terminal *tm = vw->vw_terminal;
1584 * Changing fonts is a little tricky. We must prevent
1585 * simultaneous access to the device, so we must stop
1586 * the display timer and the terminal from accessing.
1587 * We need to switch fonts and grow our screen buffer.
1589 * XXX: Right now the code uses terminal_mute() to
1590 * prevent data from reaching the console driver while
1591 * resizing the screen buffer. This isn't elegant...
1595 if (vw->vw_flags & VWF_BUSY) {
1596 /* Another process is changing the font. */
1600 vw->vw_flags |= VWF_BUSY;
1603 vt_termsize(vd, vf, &size);
1604 vt_winsize(vd, vf, &wsz);
1606 /* Grow the screen buffer and terminal. */
1607 terminal_mute(tm, 1);
1608 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1609 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1610 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1611 terminal_mute(tm, 0);
1613 /* Actually apply the font to the current window. */
1615 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1617 * In case vt_change_font called to update size we don't need
1618 * to update font link.
1620 vtfont_unref(vw->vw_font);
1621 vw->vw_font = vtfont_ref(vf);
1625 * Compute the drawable area and move the mouse cursor inside
1626 * it, in case the new area is smaller than the previous one.
1628 vt_compute_drawable_area(vw);
1629 vd->vd_mx = min(vd->vd_mx,
1630 vw->vw_draw_area.tr_end.tp_col -
1631 vw->vw_draw_area.tr_begin.tp_col - 1);
1632 vd->vd_my = min(vd->vd_my,
1633 vw->vw_draw_area.tr_end.tp_row -
1634 vw->vw_draw_area.tr_begin.tp_row - 1);
1636 /* Force a full redraw the next timer tick. */
1637 if (vd->vd_curwindow == vw) {
1638 vd->vd_flags |= VDF_INVALID;
1639 vt_resume_flush_timer(vw->vw_device, 0);
1641 vw->vw_flags &= ~VWF_BUSY;
1647 vt_proc_alive(struct vt_window *vw)
1651 if (vw->vw_smode.mode != VT_PROCESS)
1655 if ((p = pfind(vw->vw_pid)) != NULL)
1657 if (vw->vw_proc == p)
1660 vw->vw_smode.mode = VT_AUTO;
1661 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1668 signal_vt_rel(struct vt_window *vw)
1671 if (vw->vw_smode.mode != VT_PROCESS)
1673 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1678 vw->vw_flags |= VWF_SWWAIT_REL;
1679 PROC_LOCK(vw->vw_proc);
1680 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1681 PROC_UNLOCK(vw->vw_proc);
1682 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1687 signal_vt_acq(struct vt_window *vw)
1690 if (vw->vw_smode.mode != VT_PROCESS)
1692 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1693 cnavailable(vw->vw_terminal->consdev, FALSE);
1694 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1699 vw->vw_flags |= VWF_SWWAIT_ACQ;
1700 PROC_LOCK(vw->vw_proc);
1701 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1702 PROC_UNLOCK(vw->vw_proc);
1703 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1708 finish_vt_rel(struct vt_window *vw, int release, int *s)
1711 if (vw->vw_flags & VWF_SWWAIT_REL) {
1712 vw->vw_flags &= ~VWF_SWWAIT_REL;
1714 callout_drain(&vw->vw_proc_dead_timer);
1715 vt_late_window_switch(vw->vw_switch_to);
1723 finish_vt_acq(struct vt_window *vw)
1726 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1727 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1733 #ifndef SC_NO_CUTPASTE
1735 vt_mouse_terminput_button(struct vt_device *vd, int button)
1737 struct vt_window *vw;
1739 char mouseb[6] = "\x1B[M";
1742 vw = vd->vd_curwindow;
1745 /* Translate to char position. */
1746 x = vd->vd_mx / vf->vf_width;
1747 y = vd->vd_my / vf->vf_height;
1748 /* Avoid overflow. */
1749 x = MIN(x, 255 - '!');
1750 y = MIN(y, 255 - '!');
1752 mouseb[3] = ' ' + button;
1753 mouseb[4] = '!' + x;
1754 mouseb[5] = '!' + y;
1756 for (i = 0; i < sizeof(mouseb); i++)
1757 terminal_input_char(vw->vw_terminal, mouseb[i]);
1761 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1766 case MOUSE_BUTTON_EVENT:
1768 /* Mouse button pressed. */
1769 if (event & MOUSE_BUTTON1DOWN)
1770 vt_mouse_terminput_button(vd, 0);
1771 if (event & MOUSE_BUTTON2DOWN)
1772 vt_mouse_terminput_button(vd, 1);
1773 if (event & MOUSE_BUTTON3DOWN)
1774 vt_mouse_terminput_button(vd, 2);
1776 /* Mouse button released. */
1777 vt_mouse_terminput_button(vd, 3);
1781 case MOUSE_MOTION_EVENT:
1782 if (mouse->u.data.z < 0) {
1784 sc_mouse_input_button(vd, 64);
1785 } else if (mouse->u.data.z > 0) {
1787 sc_mouse_input_button(vd, 65);
1800 len = VD_PASTEBUFLEN(main_vd);
1801 buf = VD_PASTEBUF(main_vd);
1802 len /= sizeof(term_char_t);
1803 for (i = 0; i < len; i++) {
1806 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1812 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1814 struct vt_device *vd;
1815 struct vt_window *vw;
1821 vw = vd->vd_curwindow;
1825 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1827 * Either the mouse is disabled, or the window is in
1828 * "graphics mode". The graphics mode is usually set by
1829 * an X server, using the KDSETMODE ioctl.
1833 if (vf == NULL) /* Text mode. */
1837 * TODO: add flag about pointer position changed, to not redraw chars
1838 * under mouse pointer when nothing changed.
1841 if (vw->vw_mouse_level > 0)
1842 vt_mouse_terminput(vd, type, x, y, event, cnt);
1846 case MOUSE_MOTION_EVENT:
1851 vt_termsize(vd, vf, &size);
1856 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1857 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1861 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1862 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1863 vd->vd_mx / vf->vf_width,
1864 vd->vd_my / vf->vf_height);
1866 vt_resume_flush_timer(vw->vw_device, 0);
1868 case MOUSE_BUTTON_EVENT:
1876 case MOUSE_BUTTON1DOWN:
1879 mark = VTB_MARK_END;
1881 case 1: /* single click: start cut operation */
1882 mark = VTB_MARK_START;
1884 case 2: /* double click: cut a word */
1885 mark = VTB_MARK_WORD;
1887 case 3: /* triple click: cut a line */
1888 mark = VTB_MARK_ROW;
1892 case VT_MOUSE_PASTEBUTTON:
1901 case VT_MOUSE_EXTENDBUTTON:
1904 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1905 mark = VTB_MARK_EXTEND;
1910 mark = VTB_MARK_EXTEND;
1918 /* Save buttons state. */
1920 vd->vd_mstate |= event;
1922 vd->vd_mstate &= ~event;
1924 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1925 vd->vd_my / vf->vf_height) == 1) {
1927 * We have something marked to copy, so update pointer to
1928 * window with selection.
1930 vt_resume_flush_timer(vw->vw_device, 0);
1936 case VTB_MARK_EXTEND:
1939 /* Other types of mark do not require to copy data. */
1943 /* Get current selection size in bytes. */
1944 len = vtbuf_get_marked_len(&vw->vw_buf);
1948 /* Reallocate buffer only if old one is too small. */
1949 if (len > VD_PASTEBUFSZ(vd)) {
1950 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1952 /* Update buffer size. */
1953 VD_PASTEBUFSZ(vd) = len;
1955 /* Request copy/paste buffer data, no more than `len' */
1956 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1959 VD_PASTEBUFLEN(vd) = len;
1961 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1966 vt_mouse_state(int show)
1968 struct vt_device *vd;
1969 struct vt_window *vw;
1972 vw = vd->vd_curwindow;
1976 vw->vw_flags |= VWF_MOUSE_HIDE;
1979 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1983 /* Mark mouse position as dirty. */
1984 vt_mark_mouse_position_as_dirty(vd);
1985 vt_resume_flush_timer(vw->vw_device, 0);
1990 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1991 int nprot, vm_memattr_t *memattr)
1993 struct vt_window *vw = tm->tm_softc;
1994 struct vt_device *vd = vw->vw_device;
1996 if (vd->vd_driver->vd_fb_mmap)
1997 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2004 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2007 struct vt_window *vw = tm->tm_softc;
2008 struct vt_device *vd = vw->vw_device;
2011 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2012 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2023 cmd = VT_WAITACTIVE;
2049 ival = IOCPARM_IVAL(data);
2050 data = (caddr_t)&ival;
2055 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2056 if (*(int *)data & ~0x7f)
2061 case GIO_DEADKEYMAP:
2062 case PIO_DEADKEYMAP:
2067 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2068 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2069 case KBADDKBD: /* add/remove keyboard to/from mux */
2074 kbd = kbd_get_keyboard(vd->vd_keyboard);
2076 error = kbdd_ioctl(kbd, cmd, data);
2078 if (error == ENOIOCTL) {
2079 if (cmd == KDGKBTYPE) {
2080 /* always return something? XXX */
2088 case KDGKBSTATE: { /* get keyboard state (locks) */
2091 if (vw == vd->vd_curwindow) {
2093 kbd = kbd_get_keyboard(vd->vd_keyboard);
2095 error = vt_save_kbd_state(vw, kbd);
2102 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2106 case KDSKBSTATE: { /* set keyboard state (locks) */
2109 state = *(int *)data;
2110 if (state & ~LOCK_MASK)
2113 vw->vw_kbdstate &= ~LOCK_MASK;
2114 vw->vw_kbdstate |= state;
2117 if (vw == vd->vd_curwindow) {
2119 kbd = kbd_get_keyboard(vd->vd_keyboard);
2121 error = vt_update_kbd_state(vw, kbd);
2127 case KDGETLED: { /* get keyboard LED status */
2130 if (vw == vd->vd_curwindow) {
2132 kbd = kbd_get_keyboard(vd->vd_keyboard);
2134 error = vt_save_kbd_leds(vw, kbd);
2141 *(int *)data = vw->vw_kbdstate & LED_MASK;
2145 case KDSETLED: { /* set keyboard LED status */
2148 leds = *(int *)data;
2149 if (leds & ~LED_MASK)
2152 vw->vw_kbdstate &= ~LED_MASK;
2153 vw->vw_kbdstate |= leds;
2156 if (vw == vd->vd_curwindow) {
2158 kbd = kbd_get_keyboard(vd->vd_keyboard);
2160 error = vt_update_kbd_leds(vw, kbd);
2167 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2168 KD_GRAPHICS : KD_TEXT;
2173 if (vw == vd->vd_curwindow) {
2175 kbd = kbd_get_keyboard(vd->vd_keyboard);
2177 error = vt_save_kbd_mode(vw, kbd);
2184 *(int *)data = vw->vw_kbdmode;
2191 mode = *(int *)data;
2196 vw->vw_kbdmode = mode;
2199 if (vw == vd->vd_curwindow) {
2201 kbd = kbd_get_keyboard(vd->vd_keyboard);
2203 error = vt_update_kbd_mode(vw, kbd);
2213 case FBIO_GETWINORG: /* get frame buffer window origin */
2214 case FBIO_GETDISPSTART: /* get display start address */
2215 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2216 case FBIO_BLANK: /* blank display */
2217 if (vd->vd_driver->vd_fb_ioctl)
2218 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2220 case CONS_BLANKTIME:
2225 *(int *)data = M_CG640x480;
2227 case CONS_BELLTYPE: /* set bell type sound */
2228 if ((*(int *)data) & CONS_QUIET_BELL)
2229 vd->vd_flags |= VDF_QUIET_BELL;
2231 vd->vd_flags &= ~VDF_QUIET_BELL;
2233 case CONS_GETINFO: {
2234 vid_info_t *vi = (vid_info_t *)data;
2235 if (vi->size != sizeof(struct vid_info))
2238 if (vw == vd->vd_curwindow) {
2240 kbd = kbd_get_keyboard(vd->vd_keyboard);
2242 vt_save_kbd_state(vw, kbd);
2246 vi->m_num = vd->vd_curwindow->vw_number + 1;
2247 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2248 /* XXX: other fields! */
2252 *(int *)data = 0x200;
2257 case CONS_MOUSECTL: {
2258 mouse_info_t *mouse = (mouse_info_t*)data;
2261 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2262 * should not be applied to individual TTYs, but only to
2265 switch (mouse->operation) {
2267 if (vd->vd_flags & VDF_MOUSECURSOR) {
2268 vd->vd_flags &= ~VDF_MOUSECURSOR;
2269 #ifndef SC_NO_CUTPASTE
2270 vt_mouse_state(VT_MOUSE_HIDE);
2275 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2276 vd->vd_flags |= VDF_MOUSECURSOR;
2277 vd->vd_mx = vd->vd_width / 2;
2278 vd->vd_my = vd->vd_height / 2;
2279 #ifndef SC_NO_CUTPASTE
2280 vt_mouse_state(VT_MOUSE_SHOW);
2291 if (vd->vd_flags & VDF_TEXTMODE)
2294 error = vtfont_load((void *)data, &vf);
2298 error = vt_change_font(vw, vf);
2302 case PIO_VFONT_DEFAULT: {
2303 /* Reset to default font. */
2304 error = vt_change_font(vw, &vt_font_default);
2308 scrmap_t *sm = (scrmap_t *)data;
2310 /* We don't have screen maps, so return a handcrafted one. */
2311 for (i = 0; i < 256; i++)
2317 * FIXME: This implementation is incomplete compared to
2320 switch (*(int *)data) {
2324 vw->vw_flags &= ~VWF_GRAPHICS;
2327 vw->vw_flags |= VWF_GRAPHICS;
2331 case KDENABIO: /* allow io operations */
2332 error = priv_check(td, PRIV_IO);
2335 error = securelevel_gt(td->td_ucred, 0);
2338 #if defined(__i386__)
2339 td->td_frame->tf_eflags |= PSL_IOPL;
2340 #elif defined(__amd64__)
2341 td->td_frame->tf_rflags |= PSL_IOPL;
2344 case KDDISABIO: /* disallow io operations (default) */
2345 #if defined(__i386__)
2346 td->td_frame->tf_eflags &= ~PSL_IOPL;
2347 #elif defined(__amd64__)
2348 td->td_frame->tf_rflags &= ~PSL_IOPL;
2351 case KDMKTONE: /* sound the bell */
2352 vtterm_beep(tm, *(u_int *)data);
2354 case KIOCSOUND: /* make tone (*data) hz */
2357 case CONS_SETKBD: /* set the new keyboard */
2360 if (vd->vd_keyboard != *(int *)data) {
2361 kbd = kbd_get_keyboard(*(int *)data);
2366 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2367 (void *)vd, vt_kbdevent, vd);
2369 if (vd->vd_keyboard != -1) {
2370 kbd = kbd_get_keyboard(vd->vd_keyboard);
2371 vt_save_kbd_state(vd->vd_curwindow, kbd);
2372 kbd_release(kbd, (void *)vd);
2374 kbd = kbd_get_keyboard(i);
2375 vd->vd_keyboard = i;
2377 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2378 vt_update_kbd_state(vd->vd_curwindow, kbd);
2380 error = EPERM; /* XXX */
2385 case CONS_RELKBD: /* release the current keyboard */
2388 if (vd->vd_keyboard != -1) {
2389 kbd = kbd_get_keyboard(vd->vd_keyboard);
2394 vt_save_kbd_state(vd->vd_curwindow, kbd);
2395 error = kbd_release(kbd, (void *)vd);
2397 vd->vd_keyboard = -1;
2404 win = *(int *)data - 1;
2405 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2407 if ((win >= VT_MAXWINDOWS) || (win < 0))
2409 return (vt_proc_window_switch(vd->vd_windows[win]));
2412 *(int *)data = vd->vd_curwindow->vw_number + 1;
2415 *(int *)data = vw->vw_number + 1;
2418 /* TODO: Check current state, switching can be in progress. */
2419 if ((*(int *)data) == 0x01)
2420 vw->vw_flags |= VWF_VTYLOCK;
2421 else if ((*(int *)data) == 0x02)
2422 vw->vw_flags &= ~VWF_VTYLOCK;
2428 for (i = 0; i < VT_MAXWINDOWS; i++) {
2429 vw = vd->vd_windows[i];
2432 if (!(vw->vw_flags & VWF_OPENED)) {
2433 *(int *)data = vw->vw_number + 1;
2440 case VT_WAITACTIVE: {
2445 idx = *(unsigned int *)data;
2446 if (idx > VT_MAXWINDOWS)
2449 vw = vd->vd_windows[idx - 1];
2452 while (vd->vd_curwindow != vw && error == 0)
2453 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2457 case VT_SETMODE: { /* set screen switcher mode */
2458 struct vt_mode *mode;
2461 mode = (struct vt_mode *)data;
2462 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2463 if (vw->vw_smode.mode == VT_PROCESS) {
2464 p1 = pfind(vw->vw_pid);
2465 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2468 DPRINTF(5, "error EPERM\n");
2474 if (mode->mode == VT_AUTO) {
2475 vw->vw_smode.mode = VT_AUTO;
2478 DPRINTF(5, "VT_AUTO, ");
2479 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2480 cnavailable(vw->vw_terminal->consdev, TRUE);
2481 /* were we in the middle of the vty switching process? */
2482 if (finish_vt_rel(vw, TRUE, &s) == 0)
2483 DPRINTF(5, "reset WAIT_REL, ");
2484 if (finish_vt_acq(vw) == 0)
2485 DPRINTF(5, "reset WAIT_ACQ, ");
2487 } else if (mode->mode == VT_PROCESS) {
2488 if (!ISSIGVALID(mode->relsig) ||
2489 !ISSIGVALID(mode->acqsig) ||
2490 !ISSIGVALID(mode->frsig)) {
2491 DPRINTF(5, "error EINVAL\n");
2494 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2495 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2496 vw->vw_proc = td->td_proc;
2497 vw->vw_pid = vw->vw_proc->p_pid;
2498 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2499 cnavailable(vw->vw_terminal->consdev, FALSE);
2501 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2508 case VT_GETMODE: /* get screen switcher mode */
2509 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2512 case VT_RELDISP: /* screen switcher ioctl */
2514 * This must be the current vty which is in the VT_PROCESS
2517 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2521 /* ...and this process is controlling it. */
2522 if (vw->vw_proc != td->td_proc) {
2526 switch(*(int *)data) {
2527 case VT_FALSE: /* user refuses to release screen, abort */
2528 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2529 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2530 SC_DRIVER_NAME, VT_UNIT(vw));
2532 case VT_TRUE: /* user has released screen, go on */
2533 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2534 if (vw->vw_flags & VWF_SWWAIT_REL) {
2535 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2536 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2537 SC_DRIVER_NAME, VT_UNIT(vw));
2542 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2543 if ((error = finish_vt_acq(vw)) == 0)
2544 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2545 SC_DRIVER_NAME, VT_UNIT(vw));
2556 static struct vt_window *
2557 vt_allocate_window(struct vt_device *vd, unsigned int window)
2559 struct vt_window *vw;
2560 struct terminal *tm;
2564 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2566 vw->vw_number = window;
2567 vw->vw_kbdmode = K_XLATE;
2569 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2570 vw->vw_font = vtfont_ref(&vt_font_default);
2571 vt_compute_drawable_area(vw);
2574 vt_termsize(vd, vw->vw_font, &size);
2575 vt_winsize(vd, vw->vw_font, &wsz);
2576 vtbuf_init(&vw->vw_buf, &size);
2578 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2579 terminal_set_winsize(tm, &wsz);
2580 vd->vd_windows[window] = vw;
2581 callout_init(&vw->vw_proc_dead_timer, 0);
2587 vt_upgrade(struct vt_device *vd)
2589 struct vt_window *vw;
2591 int register_handlers;
2593 if (!vty_enabled(VTY_VT))
2595 if (main_vd->vd_driver == NULL)
2598 for (i = 0; i < VT_MAXWINDOWS; i++) {
2599 vw = vd->vd_windows[i];
2602 vw = vt_allocate_window(vd, i);
2604 if (!(vw->vw_flags & VWF_READY)) {
2605 callout_init(&vw->vw_proc_dead_timer, 0);
2606 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2607 vw->vw_flags |= VWF_READY;
2608 if (vw->vw_flags & VWF_CONSOLE) {
2609 /* For existing console window. */
2610 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2611 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2617 if (vd->vd_curwindow == NULL)
2618 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2620 register_handlers = 0;
2621 if (!(vd->vd_flags & VDF_ASYNC)) {
2622 /* Attach keyboard. */
2623 vt_allocate_keyboard(vd);
2625 /* Init 25 Hz timer. */
2626 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2629 * Start timer when everything ready.
2630 * Note that the operations here are purposefully ordered.
2631 * We need to ensure vd_timer_armed is non-zero before we set
2632 * the VDF_ASYNC flag. That prevents this function from
2633 * racing with vt_resume_flush_timer() to update the
2634 * callout structure.
2636 atomic_add_acq_int(&vd->vd_timer_armed, 1);
2637 vd->vd_flags |= VDF_ASYNC;
2638 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2639 register_handlers = 1;
2644 /* Refill settings with new sizes. */
2647 if (register_handlers) {
2648 /* Register suspend/resume handlers. */
2649 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2650 vd, EVENTHANDLER_PRI_ANY);
2651 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2652 EVENTHANDLER_PRI_ANY);
2657 vt_resize(struct vt_device *vd)
2659 struct vt_window *vw;
2662 for (i = 0; i < VT_MAXWINDOWS; i++) {
2663 vw = vd->vd_windows[i];
2665 /* Assign default font to window, if not textmode. */
2666 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2667 vw->vw_font = vtfont_ref(&vt_font_default);
2670 /* Resize terminal windows */
2671 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2672 DPRINTF(100, "%s: vt_change_font() is busy, "
2673 "window %d\n", __func__, i);
2679 vt_replace_backend(const struct vt_driver *drv, void *softc)
2681 struct vt_device *vd;
2685 if (vd->vd_flags & VDF_ASYNC) {
2686 /* Stop vt_flush periodic task. */
2688 vt_suspend_flush_timer(vd);
2691 * Mute current terminal until we done. vt_change_font (called
2692 * from vt_resize) will unmute it.
2694 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2698 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2702 vd->vd_flags &= ~VDF_TEXTMODE;
2706 * We want to upgrade from the current driver to the
2710 vd->vd_prev_driver = vd->vd_driver;
2711 vd->vd_prev_softc = vd->vd_softc;
2712 vd->vd_driver = drv;
2713 vd->vd_softc = softc;
2715 vd->vd_driver->vd_init(vd);
2716 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
2718 * No driver given: we want to downgrade to the previous
2721 const struct vt_driver *old_drv;
2724 old_drv = vd->vd_driver;
2725 old_softc = vd->vd_softc;
2727 vd->vd_driver = vd->vd_prev_driver;
2728 vd->vd_softc = vd->vd_prev_softc;
2729 vd->vd_prev_driver = NULL;
2730 vd->vd_prev_softc = NULL;
2732 vd->vd_flags |= VDF_DOWNGRADE;
2734 vd->vd_driver->vd_init(vd);
2736 if (old_drv->vd_fini)
2737 old_drv->vd_fini(vd, old_softc);
2739 vd->vd_flags &= ~VDF_DOWNGRADE;
2744 /* Update windows sizes and initialize last items. */
2748 if (vd->vd_flags & VDF_SPLASH)
2752 if (vd->vd_flags & VDF_ASYNC) {
2753 /* Allow to put chars now. */
2754 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2755 /* Rerun timer for screen updates. */
2756 vt_resume_flush_timer(vd, 0);
2760 * Register as console. If it already registered, cnadd() will ignore
2763 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2767 vt_suspend_handler(void *priv)
2769 struct vt_device *vd;
2772 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2773 vd->vd_driver->vd_suspend(vd);
2777 vt_resume_handler(void *priv)
2779 struct vt_device *vd;
2782 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2783 vd->vd_driver->vd_resume(vd);
2787 vt_allocate(const struct vt_driver *drv, void *softc)
2790 if (!vty_enabled(VTY_VT))
2793 if (main_vd->vd_driver == NULL) {
2794 main_vd->vd_driver = drv;
2795 printf("VT: initialize with new VT driver \"%s\".\n",
2799 * Check if have rights to replace current driver. For example:
2800 * it is bad idea to replace KMS driver with generic VGA one.
2802 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2803 printf("VT: Driver priority %d too low. Current %d\n ",
2804 drv->vd_priority, main_vd->vd_driver->vd_priority);
2807 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2808 main_vd->vd_driver->vd_name, drv->vd_name);
2811 vt_replace_backend(drv, softc);
2815 vt_deallocate(const struct vt_driver *drv, void *softc)
2818 if (!vty_enabled(VTY_VT))
2821 if (main_vd->vd_prev_driver == NULL ||
2822 main_vd->vd_driver != drv ||
2823 main_vd->vd_softc != softc)
2826 printf("VT: Switching back from \"%s\" to \"%s\".\n",
2827 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
2829 vt_replace_backend(NULL, NULL);
2833 vt_suspend(struct vt_device *vd)
2837 if (vt_suspendswitch == 0)
2839 /* Save current window. */
2840 vd->vd_savedwindow = vd->vd_curwindow;
2841 /* Ask holding process to free window and switch to console window */
2842 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2844 /* Wait for the window switch to complete. */
2847 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2848 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2853 vt_resume(struct vt_device *vd)
2856 if (vt_suspendswitch == 0)
2858 /* Switch back to saved window, if any */
2859 vt_proc_window_switch(vd->vd_savedwindow);
2860 vd->vd_savedwindow = NULL;