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 <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>
48 #include <sys/power.h>
51 #include <sys/random.h>
52 #include <sys/reboot.h>
53 #include <sys/systm.h>
54 #include <sys/terminal.h>
56 #include <dev/kbd/kbdreg.h>
57 #include <dev/vt/vt.h>
59 #if defined(__i386__) || defined(__amd64__)
60 #include <machine/psl.h>
61 #include <machine/frame.h>
64 static tc_bell_t vtterm_bell;
65 static tc_cursor_t vtterm_cursor;
66 static tc_putchar_t vtterm_putchar;
67 static tc_fill_t vtterm_fill;
68 static tc_copy_t vtterm_copy;
69 static tc_pre_input_t vtterm_pre_input;
70 static tc_post_input_t vtterm_post_input;
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_pre_input = vtterm_pre_input,
91 .tc_post_input = vtterm_post_input,
92 .tc_param = vtterm_param,
93 .tc_done = vtterm_done,
95 .tc_cnprobe = vtterm_cnprobe,
96 .tc_cngetc = vtterm_cngetc,
98 .tc_cngrab = vtterm_cngrab,
99 .tc_cnungrab = vtterm_cnungrab,
101 .tc_opened = vtterm_opened,
102 .tc_ioctl = vtterm_ioctl,
103 .tc_mmap = vtterm_mmap,
107 * Use a constant timer of 25 Hz to redraw the screen.
109 * XXX: In theory we should only fire up the timer when there is really
110 * activity. Unfortunately we cannot always start timers. We really
111 * don't want to process kernel messages synchronously, because it
112 * really slows down the system.
114 #define VT_TIMERFREQ 25
116 /* Bell pitch/duration. */
117 #define VT_BELLDURATION ((5 * hz + 99) / 100)
118 #define VT_BELLPITCH 800
120 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
123 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
125 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
126 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
127 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
128 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
129 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
131 /* Allow to disable some keyboard combinations. */
132 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
133 "See kbdmap(5) to configure.");
134 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
135 "See kbdmap(5) to configure.");
136 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
137 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
138 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
139 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
140 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
141 "See kbdmap(5) to configure.");
143 /* Used internally, not a tunable. */
144 int vt_draw_logo_cpus;
145 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
146 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
147 "(0 = do not override)");
148 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
149 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
150 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
152 static unsigned int vt_unit = 0;
153 static MALLOC_DEFINE(M_VT, "vt", "vt device");
154 struct vt_device *main_vd = &vt_consdev;
157 extern unsigned int vt_logo_width;
158 extern unsigned int vt_logo_height;
159 extern unsigned int vt_logo_depth;
160 extern unsigned char vt_logo_image[];
162 #define vtterm_draw_cpu_logos(...) do {} while (0)
163 const unsigned int vt_logo_sprite_height;
167 extern struct vt_font vt_font_default;
168 #ifndef SC_NO_CUTPASTE
169 extern struct vt_mouse_cursor vt_default_mouse_pointer;
172 static int signal_vt_rel(struct vt_window *);
173 static int signal_vt_acq(struct vt_window *);
174 static int finish_vt_rel(struct vt_window *, int, int *);
175 static int finish_vt_acq(struct vt_window *);
176 static int vt_window_switch(struct vt_window *);
177 static int vt_late_window_switch(struct vt_window *);
178 static int vt_proc_alive(struct vt_window *);
179 static void vt_resize(struct vt_device *);
180 static void vt_update_static(void *);
181 #ifndef SC_NO_CUTPASTE
182 static void vt_mouse_paste(void);
184 static void vt_suspend_handler(void *priv);
185 static void vt_resume_handler(void *priv);
187 SET_DECLARE(vt_drv_set, struct vt_driver);
189 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT))
190 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH))
192 struct terminal vt_consterm;
193 static struct vt_window vt_conswindow;
194 #ifndef SC_NO_CONSDRAWN
195 static term_char_t vt_consdrawn[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
196 static term_color_t vt_consdrawnfg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
197 static term_color_t vt_consdrawnbg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
199 struct vt_device vt_consdev = {
202 .vd_prev_driver = NULL,
203 .vd_prev_softc = NULL,
204 .vd_flags = VDF_INVALID,
205 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
206 .vd_curwindow = &vt_conswindow,
209 #ifndef SC_NO_CUTPASTE
215 .vd_mcursor = &vt_default_mouse_pointer,
216 .vd_mcursor_fg = TC_WHITE,
217 .vd_mcursor_bg = TC_BLACK,
220 #ifndef SC_NO_CONSDRAWN
221 .vd_drawn = vt_consdrawn,
222 .vd_drawnfg = vt_consdrawnfg,
223 .vd_drawnbg = vt_consdrawnbg,
226 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
227 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
228 static struct vt_window vt_conswindow = {
229 .vw_number = VT_CONSWINDOW,
230 .vw_flags = VWF_CONSOLE,
232 .vb_buffer = &vt_constextbuf[0],
233 .vb_rows = &vt_constextbufrows[0],
234 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
237 .vb_flags = VBF_STATIC,
238 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
239 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
245 .vw_device = &vt_consdev,
246 .vw_terminal = &vt_consterm,
247 .vw_kbdmode = K_XLATE,
250 struct terminal vt_consterm = {
251 .tm_class = &vt_termclass,
252 .tm_softc = &vt_conswindow,
255 static struct consdev vt_consterm_consdev = {
256 .cn_ops = &termcn_cnops,
257 .cn_arg = &vt_consterm,
261 /* Add to set of consoles. */
262 DATA_SET(cons_set, vt_consterm_consdev);
265 * Right after kmem is done to allow early drivers to use locking and allocate
268 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
270 /* Delay until all devices attached, to not waste time. */
271 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
274 /* Initialize locks/mem depended members. */
276 vt_update_static(void *dummy)
279 if (!vty_enabled(VTY_VT))
281 if (main_vd->vd_driver != NULL)
282 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
283 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
284 main_vd->vd_width, main_vd->vd_height);
286 printf("VT: init without driver.\n");
288 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
289 cv_init(&main_vd->vd_winswitch, "vtwswt");
293 vt_schedule_flush(struct vt_device *vd, int ms)
297 /* Default to initial value. */
298 ms = 1000 / VT_TIMERFREQ;
300 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
304 vt_resume_flush_timer(struct vt_window *vw, int ms)
306 struct vt_device *vd = vw->vw_device;
308 if (vd->vd_curwindow != vw)
311 if (!(vd->vd_flags & VDF_ASYNC) ||
312 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
315 vt_schedule_flush(vd, ms);
319 vt_suspend_flush_timer(struct vt_device *vd)
322 * As long as this function is called locked, callout_stop()
323 * has the same effect like callout_drain() with regard to
324 * preventing the callback function from executing.
326 VT_LOCK_ASSERT(vd, MA_OWNED);
328 if (!(vd->vd_flags & VDF_ASYNC) ||
329 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
332 callout_stop(&vd->vd_timer);
336 vt_switch_timer(void *arg)
339 (void)vt_late_window_switch((struct vt_window *)arg);
343 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
348 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
354 vw->vw_kbdmode = mode;
360 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
364 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
372 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
377 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
383 vw->vw_kbdstate &= ~LOCK_MASK;
384 vw->vw_kbdstate |= state & LOCK_MASK;
390 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
394 state = vw->vw_kbdstate & LOCK_MASK;
395 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
403 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
408 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
414 vw->vw_kbdstate &= ~LED_MASK;
415 vw->vw_kbdstate |= leds & LED_MASK;
421 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
425 leds = vw->vw_kbdstate & LED_MASK;
426 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
434 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
437 DPRINTF(40, "%s\n", __func__);
438 curvw->vw_switch_to = vw;
439 /* Set timer to allow switch in case when process hang. */
440 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
441 vt_switch_timer, (void *)vw);
442 /* Notify process about vt switch attempt. */
443 DPRINTF(30, "%s: Notify process.\n", __func__);
444 signal_vt_rel(curvw);
450 vt_window_postswitch(struct vt_window *vw)
457 /* vt_late_window_switch will do VT switching for regular case. */
459 vt_late_window_switch(struct vt_window *vw)
461 struct vt_window *curvw;
464 callout_stop(&vw->vw_proc_dead_timer);
466 ret = vt_window_switch(vw);
469 * If the switch hasn't happened, then return the VT
470 * to the current owner, if any.
472 curvw = vw->vw_device->vd_curwindow;
473 if (curvw->vw_smode.mode == VT_PROCESS)
474 (void)vt_window_postswitch(curvw);
478 /* Notify owner process about terminal availability. */
479 if (vw->vw_smode.mode == VT_PROCESS) {
480 ret = vt_window_postswitch(vw);
487 vt_proc_window_switch(struct vt_window *vw)
489 struct vt_window *curvw;
490 struct vt_device *vd;
493 /* Prevent switching to NULL */
495 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
499 curvw = vd->vd_curwindow;
501 /* Check if virtual terminal is locked */
502 if (curvw->vw_flags & VWF_VTYLOCK)
505 /* Check if switch already in progress */
506 if (curvw->vw_flags & VWF_SWWAIT_REL) {
507 /* Check if switching to same window */
508 if (curvw->vw_switch_to == vw) {
509 DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
510 return (0); /* success */
512 DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
516 /* Avoid switching to already selected window */
518 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
519 return (0); /* success */
523 * Early check for an attempt to switch to a non-functional VT.
524 * The same check is done in vt_window_switch(), but it's better
525 * to fail as early as possible to avoid needless pre-switch
529 if ((vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)) == 0) {
535 /* Ask current process permission to switch away. */
536 if (curvw->vw_smode.mode == VT_PROCESS) {
537 DPRINTF(30, "%s: VT_PROCESS ", __func__);
538 if (vt_proc_alive(curvw) == FALSE) {
539 DPRINTF(30, "Dead. Cleaning.");
542 DPRINTF(30, "%s: Signaling process.\n", __func__);
543 /* Alive, try to ask him. */
544 ret = vt_window_preswitch(vw, curvw);
545 /* Wait for process answer or timeout. */
551 ret = vt_late_window_switch(vw);
555 /* Switch window ignoring process locking. */
557 vt_window_switch(struct vt_window *vw)
559 struct vt_device *vd = vw->vw_device;
560 struct vt_window *curvw = vd->vd_curwindow;
565 * When grabbing the console for the debugger, avoid
566 * locks as that can result in deadlock. While this
567 * could use try locks, that wouldn't really make a
568 * difference as there are sufficient barriers in
569 * debugger entry/exit to be equivalent to
570 * successfully try-locking here.
574 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)))
577 vd->vd_curwindow = vw;
578 vd->vd_flags |= VDF_INVALID;
579 if (vd->vd_driver->vd_postswitch)
580 vd->vd_driver->vd_postswitch(vd);
590 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
595 vt_suspend_flush_timer(vd);
597 vd->vd_curwindow = vw;
598 vd->vd_flags |= VDF_INVALID;
599 cv_broadcast(&vd->vd_winswitch);
602 if (vd->vd_driver->vd_postswitch)
603 vd->vd_driver->vd_postswitch(vd);
605 vt_resume_flush_timer(vw, 0);
607 /* Restore per-window keyboard mode. */
609 if ((kbd = vd->vd_keyboard) != NULL) {
610 if (curvw->vw_kbdmode == K_XLATE)
611 vt_save_kbd_state(curvw, kbd);
613 vt_update_kbd_mode(vw, kbd);
614 vt_update_kbd_state(vw, kbd);
617 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
623 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
626 size->tp_row = vd->vd_height;
627 if (vt_draw_logo_cpus)
628 size->tp_row -= vt_logo_sprite_height;
629 size->tp_col = vd->vd_width;
631 size->tp_row /= vf->vf_height;
632 size->tp_col /= vf->vf_width;
637 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
640 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
641 if (vt_draw_logo_cpus)
642 rect->tr_begin.tp_row = vt_logo_sprite_height;
644 rect->tr_end.tp_row = vd->vd_height;
645 rect->tr_end.tp_col = vd->vd_width;
648 rect->tr_begin.tp_row =
649 howmany(rect->tr_begin.tp_row, vf->vf_height);
651 rect->tr_end.tp_row /= vf->vf_height;
652 rect->tr_end.tp_col /= vf->vf_width;
657 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
660 size->ws_ypixel = vd->vd_height;
661 if (vt_draw_logo_cpus)
662 size->ws_ypixel -= vt_logo_sprite_height;
663 size->ws_row = size->ws_ypixel;
664 size->ws_col = size->ws_xpixel = vd->vd_width;
666 size->ws_row /= vf->vf_height;
667 size->ws_col /= vf->vf_width;
672 vt_compute_drawable_area(struct vt_window *vw)
674 struct vt_device *vd;
680 if (vw->vw_font == NULL) {
681 vw->vw_draw_area.tr_begin.tp_col = 0;
682 vw->vw_draw_area.tr_begin.tp_row = 0;
683 if (vt_draw_logo_cpus)
684 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
685 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
686 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
693 * Compute the drawable area, so that the text is centered on
697 height = vd->vd_height;
698 if (vt_draw_logo_cpus)
699 height -= vt_logo_sprite_height;
700 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
701 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
702 if (vt_draw_logo_cpus)
703 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
704 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
705 rounddown(vd->vd_width, vf->vf_width);
706 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
707 rounddown(height, vf->vf_height);
711 vt_scroll(struct vt_window *vw, int offset, int whence)
716 if ((vw->vw_flags & VWF_SCROLL) == 0)
719 vt_termsize(vw->vw_device, vw->vw_font, &size);
721 diff = vthistory_seek(&vw->vw_buf, offset, whence);
723 vw->vw_device->vd_flags |= VDF_INVALID;
724 vt_resume_flush_timer(vw, 0);
728 vt_machine_kbdevent(int c)
732 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
734 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
736 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
738 shutdown_nice(RB_HALT);
740 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
741 #ifndef SC_NO_CUTPASTE
742 /* Insert text from cut-paste buffer. */
746 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
748 shutdown_nice(RB_HALT|RB_POWEROFF);
750 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
752 * Request to immediate panic if sysctl
753 * kern.vt.enable_panic_key allow it.
756 panic("Forced by the panic key");
758 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
760 shutdown_nice(RB_AUTOBOOT);
762 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
763 /* Force activatation/deactivation of the screen saver. */
766 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
767 /* Put machine into Stand-By mode. */
768 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
770 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
771 /* Suspend machine. */
772 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
780 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
782 struct vt_device *vd;
786 /* Only special keys handled in ScrollLock mode */
787 if ((c & SPCLKEY) == 0)
793 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
794 vw = vd->vd_windows[c - F_SCR];
795 vt_proc_window_switch(vw);
803 /* Turn scrolling off. */
804 vt_scroll(vw, 0, VHS_END);
805 VTBUF_SLCK_DISABLE(&vw->vw_buf);
806 vw->vw_flags &= ~VWF_SCROLL;
809 case FKEY | F(49): /* Home key. */
810 vt_scroll(vw, 0, VHS_SET);
812 case FKEY | F(50): /* Arrow up. */
813 vt_scroll(vw, -1, VHS_CUR);
815 case FKEY | F(51): /* Page up. */
816 vt_termsize(vd, vw->vw_font, &size);
817 vt_scroll(vw, -size.tp_row, VHS_CUR);
819 case FKEY | F(57): /* End key. */
820 vt_scroll(vw, 0, VHS_END);
822 case FKEY | F(58): /* Arrow down. */
823 vt_scroll(vw, 1, VHS_CUR);
825 case FKEY | F(59): /* Page down. */
826 vt_termsize(vd, vw->vw_font, &size);
827 vt_scroll(vw, size.tp_row, VHS_CUR);
836 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
838 struct vt_window *vw = vd->vd_curwindow;
840 random_harvest_queue(&c, sizeof(c), RANDOM_KEYBOARD);
841 #if VT_ALT_TO_ESC_HACK
843 switch (c & ~RELKEY) {
844 case (SPCLKEY | RALT):
845 if (vt_enable_altgr != 0)
847 case (SPCLKEY | LALT):
848 vd->vd_kbstate &= ~ALKED;
850 /* Other keys ignored for RELKEY event. */
853 switch (c & ~RELKEY) {
854 case (SPCLKEY | RALT):
855 if (vt_enable_altgr != 0)
857 case (SPCLKEY | LALT):
858 vd->vd_kbstate |= ALKED;
863 /* Other keys ignored for RELKEY event. */
867 if (vt_machine_kbdevent(c))
870 if (vw->vw_flags & VWF_SCROLL) {
871 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
872 /* Scroll mode keys handled, nothing to do more. */
879 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
880 vw = vd->vd_windows[c - F_SCR];
881 vt_proc_window_switch(vw);
887 /* Switch to next VT. */
888 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
889 vw = vd->vd_windows[c];
890 vt_proc_window_switch(vw);
893 /* Switch to previous VT. */
894 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
895 vw = vd->vd_windows[c];
896 vt_proc_window_switch(vw);
899 vt_save_kbd_state(vw, kbd);
901 if (vw->vw_kbdstate & SLKED) {
902 /* Turn scrolling on. */
903 vw->vw_flags |= VWF_SCROLL;
904 VTBUF_SLCK_ENABLE(&vw->vw_buf);
906 /* Turn scrolling off. */
907 vw->vw_flags &= ~VWF_SCROLL;
908 VTBUF_SLCK_DISABLE(&vw->vw_buf);
909 vt_scroll(vw, 0, VHS_END);
914 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
915 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
916 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
917 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
918 /* F1 through F12 keys. */
919 terminal_input_special(vw->vw_terminal,
920 TKEY_F1 + c - (FKEY | F(1)));
922 case FKEY | F(49): /* Home key. */
923 terminal_input_special(vw->vw_terminal, TKEY_HOME);
925 case FKEY | F(50): /* Arrow up. */
926 terminal_input_special(vw->vw_terminal, TKEY_UP);
928 case FKEY | F(51): /* Page up. */
929 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
931 case FKEY | F(53): /* Arrow left. */
932 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
934 case FKEY | F(55): /* Arrow right. */
935 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
937 case FKEY | F(57): /* End key. */
938 terminal_input_special(vw->vw_terminal, TKEY_END);
940 case FKEY | F(58): /* Arrow down. */
941 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
943 case FKEY | F(59): /* Page down. */
944 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
946 case FKEY | F(60): /* Insert key. */
947 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
949 case FKEY | F(61): /* Delete key. */
950 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
953 } else if (KEYFLAGS(c) == 0) {
954 /* Don't do UTF-8 conversion when doing raw mode. */
955 if (vw->vw_kbdmode == K_XLATE) {
956 #if VT_ALT_TO_ESC_HACK
957 if (vd->vd_kbstate & ALKED) {
959 * Prepend ESC sequence if one of ALT keys down.
961 terminal_input_char(vw->vw_terminal, 0x1b);
965 kdb_alt_break(c, &vd->vd_altbrk);
967 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
969 terminal_input_raw(vw->vw_terminal, c);
975 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
977 struct vt_device *vd = arg;
983 case KBDIO_UNLOADING:
985 vd->vd_keyboard = NULL;
986 kbd_release(kbd, (void *)vd);
993 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
994 vt_processkey(kbd, vd, c);
1000 vt_allocate_keyboard(struct vt_device *vd)
1002 int grabbed, i, idx0, idx;
1007 * If vt_upgrade() happens while the console is grabbed, we are
1008 * potentially going to switch keyboard devices while the keyboard is in
1009 * use. Unwind the grabbing of the current keyboard first, then we will
1010 * re-grab the new keyboard below, before we return.
1012 if (vd->vd_curwindow == &vt_conswindow) {
1013 grabbed = vd->vd_curwindow->vw_grabbed;
1014 for (i = 0; i < grabbed; ++i)
1015 vtterm_cnungrab(vd->vd_curwindow->vw_terminal);
1018 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
1020 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
1021 k0 = kbd_get_keyboard(idx0);
1023 for (idx = kbd_find_keyboard2("*", -1, 0);
1025 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
1026 k = kbd_get_keyboard(idx);
1028 if (idx == idx0 || KBD_IS_BUSY(k))
1031 bzero(&ki, sizeof(ki));
1032 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
1033 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
1034 ki.kb_unit = k->kb_unit;
1036 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
1039 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
1040 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
1042 DPRINTF(10, "%s: No keyboard found.\n", __func__);
1045 k0 = kbd_get_keyboard(idx0);
1047 vd->vd_keyboard = k0;
1048 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__,
1049 vd->vd_keyboard->kb_index);
1051 if (vd->vd_curwindow == &vt_conswindow) {
1052 for (i = 0; i < grabbed; ++i)
1053 vtterm_cngrab(vd->vd_curwindow->vw_terminal);
1060 vtterm_bell(struct terminal *tm)
1062 struct vt_window *vw = tm->tm_softc;
1063 struct vt_device *vd = vw->vw_device;
1065 if (!vt_enable_bell)
1068 if (vd->vd_flags & VDF_QUIET_BELL)
1071 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
1075 vtterm_beep(struct terminal *tm, u_int param)
1079 if (!vt_enable_bell)
1082 if ((param == 0) || ((param & 0xffff) == 0)) {
1087 period = ((param >> 16) & 0xffff) * hz / 1000;
1088 freq = 1193182 / (param & 0xffff);
1090 sysbeep(freq, period);
1094 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1096 struct vt_window *vw = tm->tm_softc;
1098 vtbuf_cursor_position(&vw->vw_buf, p);
1102 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1104 struct vt_window *vw = tm->tm_softc;
1106 vtbuf_putchar(&vw->vw_buf, p, c);
1110 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1112 struct vt_window *vw = tm->tm_softc;
1114 vtbuf_fill(&vw->vw_buf, r, c);
1118 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1119 const term_pos_t *p)
1121 struct vt_window *vw = tm->tm_softc;
1123 vtbuf_copy(&vw->vw_buf, r, p);
1127 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1129 struct vt_window *vw = tm->tm_softc;
1132 case TP_SETLOCALCURSOR:
1134 * 0 means normal (usually block), 1 means hidden, and
1135 * 2 means blinking (always block) for compatibility with
1136 * syscons. We don't support any changes except hiding,
1137 * so must map 2 to 0.
1139 arg = (arg == 1) ? 0 : 1;
1142 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1143 vt_resume_flush_timer(vw, 0);
1146 vw->vw_mouse_level = arg;
1152 vt_determine_colors(term_char_t c, int cursor,
1153 term_color_t *fg, term_color_t *bg)
1160 *fg = TCHAR_FGCOLOR(c);
1161 if (TCHAR_FORMAT(c) & TF_BOLD)
1162 *fg = TCOLOR_LIGHT(*fg);
1163 *bg = TCHAR_BGCOLOR(c);
1164 if (TCHAR_FORMAT(c) & TF_BLINK)
1165 *bg = TCOLOR_LIGHT(*bg);
1167 if (TCHAR_FORMAT(c) & TF_REVERSE)
1179 #ifndef SC_NO_CUTPASTE
1181 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1183 unsigned int mx, my;
1186 * We use the cursor position saved during the current refresh,
1187 * in case the cursor moved since.
1189 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1190 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1192 if (mx >= area->tr_end.tp_col ||
1193 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1194 my >= area->tr_end.tp_row ||
1195 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1201 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked)
1204 struct vt_window *vw;
1208 vw = vd->vd_curwindow;
1211 x = vd->vd_mx_drawn;
1212 y = vd->vd_my_drawn;
1215 area.tr_begin.tp_col = x / vf->vf_width;
1216 area.tr_begin.tp_row = y / vf->vf_height;
1217 area.tr_end.tp_col =
1218 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1219 area.tr_end.tp_row =
1220 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1223 * No font loaded (ie. vt_vga operating in textmode).
1225 * FIXME: This fake area needs to be revisited once the
1226 * mouse cursor is supported in vt_vga's textmode.
1228 area.tr_begin.tp_col = x;
1229 area.tr_begin.tp_row = y;
1230 area.tr_end.tp_col = x + 2;
1231 area.tr_end.tp_row = y + 2;
1235 vtbuf_lock(&vw->vw_buf);
1236 if (vd->vd_driver->vd_invalidate_text)
1237 vd->vd_driver->vd_invalidate_text(vd, &area);
1238 vtbuf_dirty(&vw->vw_buf, &area);
1240 vtbuf_unlock(&vw->vw_buf);
1245 vt_set_border(struct vt_device *vd, const term_rect_t *area,
1248 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
1250 if (drawrect == NULL)
1254 if (area->tr_begin.tp_row > 0)
1255 drawrect(vd, 0, 0, vd->vd_width - 1,
1256 area->tr_begin.tp_row - 1, 1, c);
1259 if (area->tr_begin.tp_col > 0)
1260 drawrect(vd, 0, area->tr_begin.tp_row,
1261 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
1264 if (area->tr_end.tp_col < vd->vd_width)
1265 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
1266 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
1269 if (area->tr_end.tp_row < vd->vd_height)
1270 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
1271 vd->vd_height - 1, 1, c);
1275 vt_flush(struct vt_device *vd)
1277 struct vt_window *vw;
1280 #ifndef SC_NO_CUTPASTE
1281 int cursor_was_shown, cursor_moved;
1284 vw = vd->vd_curwindow;
1288 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1292 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1295 vtbuf_lock(&vw->vw_buf);
1297 #ifndef SC_NO_CUTPASTE
1298 cursor_was_shown = vd->vd_mshown;
1299 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1300 vd->vd_my != vd->vd_my_drawn);
1302 /* Check if the cursor should be displayed or not. */
1303 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1304 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1305 !kdb_active && !KERNEL_PANICKED()) { /* DDB inactive. */
1312 * If the cursor changed display state or moved, we must mark
1313 * the old position as dirty, so that it's erased.
1315 if (cursor_was_shown != vd->vd_mshown ||
1316 (vd->vd_mshown && cursor_moved))
1317 vt_mark_mouse_position_as_dirty(vd, true);
1320 * Save position of the mouse cursor. It's used by backends to
1321 * know where to draw the cursor and during the next refresh to
1322 * erase the previous position.
1324 vd->vd_mx_drawn = vd->vd_mx;
1325 vd->vd_my_drawn = vd->vd_my;
1328 * If the cursor is displayed and has moved since last refresh,
1329 * mark the new position as dirty.
1331 if (vd->vd_mshown && cursor_moved)
1332 vt_mark_mouse_position_as_dirty(vd, true);
1335 vtbuf_undirty(&vw->vw_buf, &tarea);
1337 /* Force a full redraw when the screen contents might be invalid. */
1338 if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) {
1339 const teken_attr_t *a;
1341 vd->vd_flags &= ~VDF_INVALID;
1343 a = teken_get_curattr(&vw->vw_terminal->tm_emulator);
1344 vt_set_border(vd, &vw->vw_draw_area, a->ta_bgcolor);
1345 vt_termrect(vd, vf, &tarea);
1346 if (vd->vd_driver->vd_invalidate_text)
1347 vd->vd_driver->vd_invalidate_text(vd, &tarea);
1348 if (vt_draw_logo_cpus)
1349 vtterm_draw_cpu_logos(vd);
1352 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1353 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1354 vtbuf_unlock(&vw->vw_buf);
1358 vtbuf_unlock(&vw->vw_buf);
1365 struct vt_device *vd;
1369 /* Update screen if required. */
1370 changed = vt_flush(vd);
1372 /* Schedule for next update. */
1374 vt_schedule_flush(vd, 0);
1376 vd->vd_timer_armed = 0;
1380 vtterm_pre_input(struct terminal *tm)
1382 struct vt_window *vw = tm->tm_softc;
1384 vtbuf_lock(&vw->vw_buf);
1388 vtterm_post_input(struct terminal *tm)
1390 struct vt_window *vw = tm->tm_softc;
1392 vtbuf_unlock(&vw->vw_buf);
1393 vt_resume_flush_timer(vw, 0);
1397 vtterm_done(struct terminal *tm)
1399 struct vt_window *vw = tm->tm_softc;
1400 struct vt_device *vd = vw->vw_device;
1402 if (kdb_active || KERNEL_PANICKED()) {
1403 /* Switch to the debugger. */
1404 if (vd->vd_curwindow != vw) {
1405 vd->vd_curwindow = vw;
1406 vd->vd_flags |= VDF_INVALID;
1407 if (vd->vd_driver->vd_postswitch)
1408 vd->vd_driver->vd_postswitch(vd);
1410 vd->vd_flags &= ~VDF_SPLASH;
1412 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1419 vtterm_splash(struct vt_device *vd)
1421 vt_axis_t top, left;
1423 /* Display a nice boot splash. */
1424 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1425 top = (vd->vd_height - vt_logo_height) / 2;
1426 left = (vd->vd_width - vt_logo_width) / 2;
1427 switch (vt_logo_depth) {
1429 /* XXX: Unhardcode colors! */
1430 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1431 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1432 left, top, TC_WHITE, TC_BLACK);
1434 vd->vd_flags |= VDF_SPLASH;
1440 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1442 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1443 struct vt_window *vw = tm->tm_softc;
1444 struct vt_device *vd = vw->vw_device;
1446 const term_attr_t *a;
1448 if (!vty_enabled(VTY_VT))
1451 if (vd->vd_flags & VDF_INITIALIZED)
1452 /* Initialization already done. */
1455 SET_FOREACH(vtdlist, vt_drv_set) {
1457 if (vtd->vd_probe == NULL)
1459 if (vtd->vd_probe(vd) == CN_DEAD)
1461 if ((vtdbest == NULL) ||
1462 (vtd->vd_priority > vtdbest->vd_priority))
1465 if (vtdbest == NULL) {
1466 cp->cn_pri = CN_DEAD;
1467 vd->vd_flags |= VDF_DEAD;
1469 vd->vd_driver = vtdbest;
1470 cp->cn_pri = vd->vd_driver->vd_init(vd);
1473 /* Check if driver's vt_init return CN_DEAD. */
1474 if (cp->cn_pri == CN_DEAD) {
1475 vd->vd_flags |= VDF_DEAD;
1478 /* Initialize any early-boot keyboard drivers */
1479 kbd_configure(KB_CONF_PROBE_ONLY);
1481 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1482 vd->vd_windows[VT_CONSWINDOW] = vw;
1483 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1485 /* Attach default font if not in TEXTMODE. */
1486 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1487 vw->vw_font = vtfont_ref(&vt_font_default);
1488 vt_compute_drawable_area(vw);
1492 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1493 * that we have the real viewable size, fix it in the static
1496 if (vd->vd_width != 0 && vd->vd_height != 0)
1497 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1499 /* We need to access terminal attributes from vtbuf */
1500 vw->vw_buf.vb_terminal = tm;
1501 vtbuf_init_early(&vw->vw_buf);
1502 vt_winsize(vd, vw->vw_font, &wsz);
1503 a = teken_get_curattr(&tm->tm_emulator);
1504 terminal_set_winsize_blank(tm, &wsz, 1, a);
1506 if (vtdbest != NULL) {
1511 vd->vd_flags |= VDF_INITIALIZED;
1516 vtterm_cngetc(struct terminal *tm)
1518 struct vt_window *vw = tm->tm_softc;
1519 struct vt_device *vd = vw->vw_device;
1523 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1524 return (*vw->vw_kbdsq++);
1526 /* Make sure the splash screen is not there. */
1527 if (vd->vd_flags & VDF_SPLASH) {
1529 vd->vd_flags &= ~VDF_SPLASH;
1530 /* Mark screen as invalid to force update */
1531 vd->vd_flags |= VDF_INVALID;
1535 /* Stripped down keyboard handler. */
1536 if ((kbd = vd->vd_keyboard) == NULL)
1539 /* Force keyboard input mode to K_XLATE */
1540 vw->vw_kbdmode = K_XLATE;
1541 vt_update_kbd_mode(vw, kbd);
1543 /* Switch the keyboard to polling to make it work here. */
1544 kbdd_poll(kbd, TRUE);
1545 c = kbdd_read_char(kbd, 0);
1546 kbdd_poll(kbd, FALSE);
1550 if (vw->vw_flags & VWF_SCROLL) {
1551 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1556 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1560 vt_save_kbd_state(vw, kbd);
1561 if (vw->vw_kbdstate & SLKED) {
1562 /* Turn scrolling on. */
1563 vw->vw_flags |= VWF_SCROLL;
1564 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1566 /* Turn scrolling off. */
1567 vt_scroll(vw, 0, VHS_END);
1568 vw->vw_flags &= ~VWF_SCROLL;
1569 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1572 /* XXX: KDB can handle history. */
1573 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1574 vw->vw_kbdsq = "\x1b[A";
1576 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1577 vw->vw_kbdsq = "\x1b[B";
1579 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1580 vw->vw_kbdsq = "\x1b[C";
1582 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1583 vw->vw_kbdsq = "\x1b[D";
1587 /* Force refresh to make scrollback work. */
1589 } else if (KEYFLAGS(c) == 0) {
1590 return (KEYCHAR(c));
1593 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1594 return (*vw->vw_kbdsq++);
1600 vtterm_cngrab(struct terminal *tm)
1602 struct vt_device *vd;
1603 struct vt_window *vw;
1610 vt_window_switch(vw);
1612 if ((kbd = vd->vd_keyboard) == NULL)
1615 if (vw->vw_grabbed++ > 0)
1619 * Make sure the keyboard is accessible even when the kbd device
1620 * driver is disabled.
1624 /* We shall always use the keyboard in the XLATE mode here. */
1625 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1626 vw->vw_kbdmode = K_XLATE;
1627 vt_update_kbd_mode(vw, kbd);
1629 kbdd_poll(kbd, TRUE);
1633 vtterm_cnungrab(struct terminal *tm)
1635 struct vt_device *vd;
1636 struct vt_window *vw;
1642 if ((kbd = vd->vd_keyboard) == NULL)
1645 if (--vw->vw_grabbed > 0)
1648 kbdd_poll(kbd, FALSE);
1650 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1651 vt_update_kbd_mode(vw, kbd);
1656 vtterm_opened(struct terminal *tm, int opened)
1658 struct vt_window *vw = tm->tm_softc;
1659 struct vt_device *vd = vw->vw_device;
1662 vd->vd_flags &= ~VDF_SPLASH;
1664 vw->vw_flags |= VWF_OPENED;
1666 vw->vw_flags &= ~VWF_OPENED;
1667 /* TODO: finish ACQ/REL */
1673 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1675 struct vt_device *vd = vw->vw_device;
1676 struct terminal *tm = vw->vw_terminal;
1683 * Changing fonts is a little tricky. We must prevent
1684 * simultaneous access to the device, so we must stop
1685 * the display timer and the terminal from accessing.
1686 * We need to switch fonts and grow our screen buffer.
1688 * XXX: Right now the code uses terminal_mute() to
1689 * prevent data from reaching the console driver while
1690 * resizing the screen buffer. This isn't elegant...
1694 if (vw->vw_flags & VWF_BUSY) {
1695 /* Another process is changing the font. */
1699 vw->vw_flags |= VWF_BUSY;
1702 vt_termsize(vd, vf, &size);
1703 vt_winsize(vd, vf, &wsz);
1705 /* Grow the screen buffer and terminal. */
1706 terminal_mute(tm, 1);
1707 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1708 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1709 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1710 terminal_mute(tm, 0);
1712 /* Actually apply the font to the current window. */
1714 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1716 * In case vt_change_font called to update size we don't need
1717 * to update font link.
1719 vtfont_unref(vw->vw_font);
1720 vw->vw_font = vtfont_ref(vf);
1724 * Compute the drawable area and move the mouse cursor inside
1725 * it, in case the new area is smaller than the previous one.
1727 vt_compute_drawable_area(vw);
1728 vd->vd_mx = min(vd->vd_mx,
1729 vw->vw_draw_area.tr_end.tp_col -
1730 vw->vw_draw_area.tr_begin.tp_col - 1);
1731 vd->vd_my = min(vd->vd_my,
1732 vw->vw_draw_area.tr_end.tp_row -
1733 vw->vw_draw_area.tr_begin.tp_row - 1);
1735 /* Force a full redraw the next timer tick. */
1736 if (vd->vd_curwindow == vw) {
1737 vd->vd_flags |= VDF_INVALID;
1738 vt_resume_flush_timer(vw, 0);
1740 vw->vw_flags &= ~VWF_BUSY;
1746 vt_proc_alive(struct vt_window *vw)
1750 if (vw->vw_smode.mode != VT_PROCESS)
1754 if ((p = pfind(vw->vw_pid)) != NULL)
1756 if (vw->vw_proc == p)
1759 vw->vw_smode.mode = VT_AUTO;
1760 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1767 signal_vt_rel(struct vt_window *vw)
1770 if (vw->vw_smode.mode != VT_PROCESS)
1772 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1777 vw->vw_flags |= VWF_SWWAIT_REL;
1778 PROC_LOCK(vw->vw_proc);
1779 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1780 PROC_UNLOCK(vw->vw_proc);
1781 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1786 signal_vt_acq(struct vt_window *vw)
1789 if (vw->vw_smode.mode != VT_PROCESS)
1791 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1792 cnavailable(vw->vw_terminal->consdev, FALSE);
1793 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1798 vw->vw_flags |= VWF_SWWAIT_ACQ;
1799 PROC_LOCK(vw->vw_proc);
1800 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1801 PROC_UNLOCK(vw->vw_proc);
1802 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1807 finish_vt_rel(struct vt_window *vw, int release, int *s)
1810 if (vw->vw_flags & VWF_SWWAIT_REL) {
1811 vw->vw_flags &= ~VWF_SWWAIT_REL;
1813 callout_drain(&vw->vw_proc_dead_timer);
1814 (void)vt_late_window_switch(vw->vw_switch_to);
1822 finish_vt_acq(struct vt_window *vw)
1825 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1826 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1832 #ifndef SC_NO_CUTPASTE
1834 vt_mouse_terminput_button(struct vt_device *vd, int button)
1836 struct vt_window *vw;
1838 char mouseb[6] = "\x1B[M";
1841 vw = vd->vd_curwindow;
1844 /* Translate to char position. */
1845 x = vd->vd_mx / vf->vf_width;
1846 y = vd->vd_my / vf->vf_height;
1847 /* Avoid overflow. */
1848 x = MIN(x, 255 - '!');
1849 y = MIN(y, 255 - '!');
1851 mouseb[3] = ' ' + button;
1852 mouseb[4] = '!' + x;
1853 mouseb[5] = '!' + y;
1855 for (i = 0; i < sizeof(mouseb); i++)
1856 terminal_input_char(vw->vw_terminal, mouseb[i]);
1860 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1865 case MOUSE_BUTTON_EVENT:
1867 /* Mouse button pressed. */
1868 if (event & MOUSE_BUTTON1DOWN)
1869 vt_mouse_terminput_button(vd, 0);
1870 if (event & MOUSE_BUTTON2DOWN)
1871 vt_mouse_terminput_button(vd, 1);
1872 if (event & MOUSE_BUTTON3DOWN)
1873 vt_mouse_terminput_button(vd, 2);
1875 /* Mouse button released. */
1876 vt_mouse_terminput_button(vd, 3);
1880 case MOUSE_MOTION_EVENT:
1881 if (mouse->u.data.z < 0) {
1883 sc_mouse_input_button(vd, 64);
1884 } else if (mouse->u.data.z > 0) {
1886 sc_mouse_input_button(vd, 65);
1899 len = VD_PASTEBUFLEN(main_vd);
1900 buf = VD_PASTEBUF(main_vd);
1901 len /= sizeof(term_char_t);
1902 for (i = 0; i < len; i++) {
1905 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1911 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1913 struct vt_device *vd;
1914 struct vt_window *vw;
1920 vw = vd->vd_curwindow;
1924 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1926 * Either the mouse is disabled, or the window is in
1927 * "graphics mode". The graphics mode is usually set by
1928 * an X server, using the KDSETMODE ioctl.
1932 if (vf == NULL) /* Text mode. */
1936 * TODO: add flag about pointer position changed, to not redraw chars
1937 * under mouse pointer when nothing changed.
1940 if (vw->vw_mouse_level > 0)
1941 vt_mouse_terminput(vd, type, x, y, event, cnt);
1945 case MOUSE_MOTION_EVENT:
1950 vt_termsize(vd, vf, &size);
1955 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1956 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1960 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1961 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1962 vd->vd_mx / vf->vf_width,
1963 vd->vd_my / vf->vf_height);
1965 vt_resume_flush_timer(vw, 0);
1967 case MOUSE_BUTTON_EVENT:
1975 case MOUSE_BUTTON1DOWN:
1978 mark = VTB_MARK_END;
1980 case 1: /* single click: start cut operation */
1981 mark = VTB_MARK_START;
1983 case 2: /* double click: cut a word */
1984 mark = VTB_MARK_WORD;
1986 case 3: /* triple click: cut a line */
1987 mark = VTB_MARK_ROW;
1991 case VT_MOUSE_PASTEBUTTON:
2000 case VT_MOUSE_EXTENDBUTTON:
2003 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
2004 mark = VTB_MARK_EXTEND;
2009 mark = VTB_MARK_EXTEND;
2017 /* Save buttons state. */
2019 vd->vd_mstate |= event;
2021 vd->vd_mstate &= ~event;
2023 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
2024 vd->vd_my / vf->vf_height) == 1) {
2026 * We have something marked to copy, so update pointer to
2027 * window with selection.
2029 vt_resume_flush_timer(vw, 0);
2035 case VTB_MARK_EXTEND:
2038 /* Other types of mark do not require to copy data. */
2042 /* Get current selection size in bytes. */
2043 len = vtbuf_get_marked_len(&vw->vw_buf);
2047 /* Reallocate buffer only if old one is too small. */
2048 if (len > VD_PASTEBUFSZ(vd)) {
2049 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
2051 /* Update buffer size. */
2052 VD_PASTEBUFSZ(vd) = len;
2054 /* Request copy/paste buffer data, no more than `len' */
2055 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
2058 VD_PASTEBUFLEN(vd) = len;
2060 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
2065 vt_mouse_state(int show)
2067 struct vt_device *vd;
2068 struct vt_window *vw;
2071 vw = vd->vd_curwindow;
2075 vw->vw_flags |= VWF_MOUSE_HIDE;
2078 vw->vw_flags &= ~VWF_MOUSE_HIDE;
2082 /* Mark mouse position as dirty. */
2083 vt_mark_mouse_position_as_dirty(vd, false);
2084 vt_resume_flush_timer(vw, 0);
2089 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
2090 int nprot, vm_memattr_t *memattr)
2092 struct vt_window *vw = tm->tm_softc;
2093 struct vt_device *vd = vw->vw_device;
2095 if (vd->vd_driver->vd_fb_mmap)
2096 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2103 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2106 struct vt_window *vw = tm->tm_softc;
2107 struct vt_device *vd = vw->vw_device;
2110 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2111 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2122 cmd = VT_WAITACTIVE;
2149 cmd = CONS_SETWINORG;
2157 ival = IOCPARM_IVAL(data);
2158 data = (caddr_t)&ival;
2163 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2164 if (*(int *)data & ~0x7f)
2169 case GIO_DEADKEYMAP:
2170 case PIO_DEADKEYMAP:
2175 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2176 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2177 case KBADDKBD: /* add/remove keyboard to/from mux */
2182 if ((kbd = vd->vd_keyboard) != NULL)
2183 error = kbdd_ioctl(kbd, cmd, data);
2185 if (error == ENOIOCTL) {
2186 if (cmd == KDGKBTYPE) {
2187 /* always return something? XXX */
2195 case KDGKBSTATE: { /* get keyboard state (locks) */
2198 if (vw == vd->vd_curwindow) {
2200 if ((kbd = vd->vd_keyboard) != NULL)
2201 error = vt_save_kbd_state(vw, kbd);
2208 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2212 case KDSKBSTATE: { /* set keyboard state (locks) */
2215 state = *(int *)data;
2216 if (state & ~LOCK_MASK)
2219 vw->vw_kbdstate &= ~LOCK_MASK;
2220 vw->vw_kbdstate |= state;
2223 if (vw == vd->vd_curwindow) {
2225 if ((kbd = vd->vd_keyboard) != NULL)
2226 error = vt_update_kbd_state(vw, kbd);
2232 case KDGETLED: { /* get keyboard LED status */
2235 if (vw == vd->vd_curwindow) {
2237 if ((kbd = vd->vd_keyboard) != NULL)
2238 error = vt_save_kbd_leds(vw, kbd);
2245 *(int *)data = vw->vw_kbdstate & LED_MASK;
2249 case KDSETLED: { /* set keyboard LED status */
2252 leds = *(int *)data;
2253 if (leds & ~LED_MASK)
2256 vw->vw_kbdstate &= ~LED_MASK;
2257 vw->vw_kbdstate |= leds;
2260 if (vw == vd->vd_curwindow) {
2262 if ((kbd = vd->vd_keyboard) != NULL)
2263 error = vt_update_kbd_leds(vw, kbd);
2270 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2271 KD_GRAPHICS : KD_TEXT;
2276 if (vw == vd->vd_curwindow) {
2278 if ((kbd = vd->vd_keyboard) != NULL)
2279 error = vt_save_kbd_mode(vw, kbd);
2286 *(int *)data = vw->vw_kbdmode;
2293 mode = *(int *)data;
2298 vw->vw_kbdmode = mode;
2301 if (vw == vd->vd_curwindow) {
2303 if ((kbd = vd->vd_keyboard) != NULL)
2304 error = vt_update_kbd_mode(vw, kbd);
2314 case FBIO_GETWINORG: /* get frame buffer window origin */
2315 case FBIO_GETDISPSTART: /* get display start address */
2316 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2317 case FBIO_BLANK: /* blank display */
2318 if (vd->vd_driver->vd_fb_ioctl)
2319 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2321 case CONS_BLANKTIME:
2325 if (*(int *)data < 0)
2327 if (*(int *)data != vw->vw_buf.vb_history_size)
2328 vtbuf_sethistory_size(&vw->vw_buf, *(int *)data);
2331 vtbuf_clearhistory(&vw->vw_buf);
2333 * Invalidate the entire visible window; it is not guaranteed
2334 * that this operation will be immediately followed by a scroll
2335 * event, so it would otherwise be possible for prior artifacts
2336 * to remain visible.
2339 if (vw == vd->vd_curwindow) {
2340 vd->vd_flags |= VDF_INVALID;
2341 vt_resume_flush_timer(vw, 0);
2347 *(int *)data = M_CG640x480;
2349 case CONS_BELLTYPE: /* set bell type sound */
2350 if ((*(int *)data) & CONS_QUIET_BELL)
2351 vd->vd_flags |= VDF_QUIET_BELL;
2353 vd->vd_flags &= ~VDF_QUIET_BELL;
2355 case CONS_GETINFO: {
2356 vid_info_t *vi = (vid_info_t *)data;
2357 if (vi->size != sizeof(struct vid_info))
2360 if (vw == vd->vd_curwindow) {
2362 if ((kbd = vd->vd_keyboard) != NULL)
2363 vt_save_kbd_state(vw, kbd);
2367 vi->m_num = vd->vd_curwindow->vw_number + 1;
2368 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2369 /* XXX: other fields! */
2373 *(int *)data = 0x200;
2378 case CONS_MOUSECTL: {
2379 mouse_info_t *mouse = (mouse_info_t*)data;
2382 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2383 * should not be applied to individual TTYs, but only to
2386 switch (mouse->operation) {
2388 if (vd->vd_flags & VDF_MOUSECURSOR) {
2389 vd->vd_flags &= ~VDF_MOUSECURSOR;
2390 #ifndef SC_NO_CUTPASTE
2391 vt_mouse_state(VT_MOUSE_HIDE);
2396 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2397 vd->vd_flags |= VDF_MOUSECURSOR;
2398 vd->vd_mx = vd->vd_width / 2;
2399 vd->vd_my = vd->vd_height / 2;
2400 #ifndef SC_NO_CUTPASTE
2401 vt_mouse_state(VT_MOUSE_SHOW);
2412 if (vd->vd_flags & VDF_TEXTMODE)
2415 error = vtfont_load((void *)data, &vf);
2419 error = vt_change_font(vw, vf);
2423 case PIO_VFONT_DEFAULT: {
2424 /* Reset to default font. */
2425 error = vt_change_font(vw, &vt_font_default);
2429 scrmap_t *sm = (scrmap_t *)data;
2431 /* We don't have screen maps, so return a handcrafted one. */
2432 for (i = 0; i < 256; i++)
2438 * FIXME: This implementation is incomplete compared to
2441 switch (*(int *)data) {
2445 vw->vw_flags &= ~VWF_GRAPHICS;
2448 vw->vw_flags |= VWF_GRAPHICS;
2452 case KDENABIO: /* allow io operations */
2453 error = priv_check(td, PRIV_IO);
2456 error = securelevel_gt(td->td_ucred, 0);
2459 #if defined(__i386__)
2460 td->td_frame->tf_eflags |= PSL_IOPL;
2461 #elif defined(__amd64__)
2462 td->td_frame->tf_rflags |= PSL_IOPL;
2465 case KDDISABIO: /* disallow io operations (default) */
2466 #if defined(__i386__)
2467 td->td_frame->tf_eflags &= ~PSL_IOPL;
2468 #elif defined(__amd64__)
2469 td->td_frame->tf_rflags &= ~PSL_IOPL;
2472 case KDMKTONE: /* sound the bell */
2473 vtterm_beep(tm, *(u_int *)data);
2475 case KIOCSOUND: /* make tone (*data) hz */
2478 case CONS_SETKBD: /* set the new keyboard */
2481 if (vd->vd_keyboard == NULL ||
2482 vd->vd_keyboard->kb_index != *(int *)data) {
2483 kbd = kbd_get_keyboard(*(int *)data);
2488 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2489 (void *)vd, vt_kbdevent, vd);
2491 if ((kbd = vd->vd_keyboard) != NULL) {
2492 vt_save_kbd_state(vd->vd_curwindow, kbd);
2493 kbd_release(kbd, (void *)vd);
2495 kbd = vd->vd_keyboard = kbd_get_keyboard(i);
2497 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2498 vt_update_kbd_state(vd->vd_curwindow, kbd);
2500 error = EPERM; /* XXX */
2505 case CONS_RELKBD: /* release the current keyboard */
2508 if ((kbd = vd->vd_keyboard) != NULL) {
2509 vt_save_kbd_state(vd->vd_curwindow, kbd);
2510 error = kbd_release(kbd, (void *)vd);
2512 vd->vd_keyboard = NULL;
2519 win = *(int *)data - 1;
2520 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2522 if ((win >= VT_MAXWINDOWS) || (win < 0))
2524 return (vt_proc_window_switch(vd->vd_windows[win]));
2527 *(int *)data = vd->vd_curwindow->vw_number + 1;
2530 *(int *)data = vw->vw_number + 1;
2533 /* TODO: Check current state, switching can be in progress. */
2534 if ((*(int *)data) == 0x01)
2535 vw->vw_flags |= VWF_VTYLOCK;
2536 else if ((*(int *)data) == 0x02)
2537 vw->vw_flags &= ~VWF_VTYLOCK;
2543 for (i = 0; i < VT_MAXWINDOWS; i++) {
2544 vw = vd->vd_windows[i];
2547 if (!(vw->vw_flags & VWF_OPENED)) {
2548 *(int *)data = vw->vw_number + 1;
2555 case VT_WAITACTIVE: {
2560 idx = *(unsigned int *)data;
2561 if (idx > VT_MAXWINDOWS)
2564 vw = vd->vd_windows[idx - 1];
2567 while (vd->vd_curwindow != vw && error == 0)
2568 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2572 case VT_SETMODE: { /* set screen switcher mode */
2573 struct vt_mode *mode;
2576 mode = (struct vt_mode *)data;
2577 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2578 if (vw->vw_smode.mode == VT_PROCESS) {
2579 p1 = pfind(vw->vw_pid);
2580 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2583 DPRINTF(5, "error EPERM\n");
2589 if (mode->mode == VT_AUTO) {
2590 vw->vw_smode.mode = VT_AUTO;
2593 DPRINTF(5, "VT_AUTO, ");
2594 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2595 cnavailable(vw->vw_terminal->consdev, TRUE);
2596 /* were we in the middle of the vty switching process? */
2597 if (finish_vt_rel(vw, TRUE, &s) == 0)
2598 DPRINTF(5, "reset WAIT_REL, ");
2599 if (finish_vt_acq(vw) == 0)
2600 DPRINTF(5, "reset WAIT_ACQ, ");
2602 } else if (mode->mode == VT_PROCESS) {
2603 if (!ISSIGVALID(mode->relsig) ||
2604 !ISSIGVALID(mode->acqsig) ||
2605 !ISSIGVALID(mode->frsig)) {
2606 DPRINTF(5, "error EINVAL\n");
2609 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2610 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2611 vw->vw_proc = td->td_proc;
2612 vw->vw_pid = vw->vw_proc->p_pid;
2613 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2614 cnavailable(vw->vw_terminal->consdev, FALSE);
2616 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2623 case VT_GETMODE: /* get screen switcher mode */
2624 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2627 case VT_RELDISP: /* screen switcher ioctl */
2629 * This must be the current vty which is in the VT_PROCESS
2632 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2636 /* ...and this process is controlling it. */
2637 if (vw->vw_proc != td->td_proc) {
2641 switch(*(int *)data) {
2642 case VT_FALSE: /* user refuses to release screen, abort */
2643 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2644 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2645 SC_DRIVER_NAME, VT_UNIT(vw));
2647 case VT_TRUE: /* user has released screen, go on */
2648 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2649 if (vw->vw_flags & VWF_SWWAIT_REL) {
2650 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2651 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2652 SC_DRIVER_NAME, VT_UNIT(vw));
2657 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2658 if ((error = finish_vt_acq(vw)) == 0)
2659 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2660 SC_DRIVER_NAME, VT_UNIT(vw));
2671 static struct vt_window *
2672 vt_allocate_window(struct vt_device *vd, unsigned int window)
2674 struct vt_window *vw;
2675 struct terminal *tm;
2679 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2681 vw->vw_number = window;
2682 vw->vw_kbdmode = K_XLATE;
2684 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2685 vw->vw_font = vtfont_ref(&vt_font_default);
2686 vt_compute_drawable_area(vw);
2689 vt_termsize(vd, vw->vw_font, &size);
2690 vt_winsize(vd, vw->vw_font, &wsz);
2691 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2692 vw->vw_buf.vb_terminal = tm; /* must be set before vtbuf_init() */
2693 vtbuf_init(&vw->vw_buf, &size);
2695 terminal_set_winsize(tm, &wsz);
2696 vd->vd_windows[window] = vw;
2697 callout_init(&vw->vw_proc_dead_timer, 0);
2703 vt_upgrade(struct vt_device *vd)
2705 struct vt_window *vw;
2707 int register_handlers;
2709 if (!vty_enabled(VTY_VT))
2711 if (main_vd->vd_driver == NULL)
2714 for (i = 0; i < VT_MAXWINDOWS; i++) {
2715 vw = vd->vd_windows[i];
2718 vw = vt_allocate_window(vd, i);
2720 if (!(vw->vw_flags & VWF_READY)) {
2721 callout_init(&vw->vw_proc_dead_timer, 0);
2722 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2723 vw->vw_flags |= VWF_READY;
2724 if (vw->vw_flags & VWF_CONSOLE) {
2725 /* For existing console window. */
2726 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2727 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2732 if (vd->vd_curwindow == NULL)
2733 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2735 register_handlers = 0;
2736 if (!(vd->vd_flags & VDF_ASYNC)) {
2737 /* Attach keyboard. */
2738 vt_allocate_keyboard(vd);
2740 /* Init 25 Hz timer. */
2741 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2744 * Start timer when everything ready.
2745 * Note that the operations here are purposefully ordered.
2746 * We need to ensure vd_timer_armed is non-zero before we set
2747 * the VDF_ASYNC flag. That prevents this function from
2748 * racing with vt_resume_flush_timer() to update the
2749 * callout structure.
2751 atomic_add_acq_int(&vd->vd_timer_armed, 1);
2752 vd->vd_flags |= VDF_ASYNC;
2753 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2754 register_handlers = 1;
2759 /* Refill settings with new sizes. */
2762 if (register_handlers) {
2763 /* Register suspend/resume handlers. */
2764 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2765 vd, EVENTHANDLER_PRI_ANY);
2766 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2767 EVENTHANDLER_PRI_ANY);
2772 vt_resize(struct vt_device *vd)
2774 struct vt_window *vw;
2777 for (i = 0; i < VT_MAXWINDOWS; i++) {
2778 vw = vd->vd_windows[i];
2780 /* Assign default font to window, if not textmode. */
2781 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2782 vw->vw_font = vtfont_ref(&vt_font_default);
2785 /* Resize terminal windows */
2786 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2787 DPRINTF(100, "%s: vt_change_font() is busy, "
2788 "window %d\n", __func__, i);
2794 vt_replace_backend(const struct vt_driver *drv, void *softc)
2796 struct vt_device *vd;
2800 if (vd->vd_flags & VDF_ASYNC) {
2801 /* Stop vt_flush periodic task. */
2803 vt_suspend_flush_timer(vd);
2806 * Mute current terminal until we done. vt_change_font (called
2807 * from vt_resize) will unmute it.
2809 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2813 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2817 vd->vd_flags &= ~VDF_TEXTMODE;
2821 * We want to upgrade from the current driver to the
2825 vd->vd_prev_driver = vd->vd_driver;
2826 vd->vd_prev_softc = vd->vd_softc;
2827 vd->vd_driver = drv;
2828 vd->vd_softc = softc;
2830 vd->vd_driver->vd_init(vd);
2831 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
2833 * No driver given: we want to downgrade to the previous
2836 const struct vt_driver *old_drv;
2839 old_drv = vd->vd_driver;
2840 old_softc = vd->vd_softc;
2842 vd->vd_driver = vd->vd_prev_driver;
2843 vd->vd_softc = vd->vd_prev_softc;
2844 vd->vd_prev_driver = NULL;
2845 vd->vd_prev_softc = NULL;
2847 vd->vd_flags |= VDF_DOWNGRADE;
2849 vd->vd_driver->vd_init(vd);
2851 if (old_drv->vd_fini)
2852 old_drv->vd_fini(vd, old_softc);
2854 vd->vd_flags &= ~VDF_DOWNGRADE;
2859 /* Update windows sizes and initialize last items. */
2863 if (vd->vd_flags & VDF_SPLASH)
2867 if (vd->vd_flags & VDF_ASYNC) {
2868 /* Allow to put chars now. */
2869 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2870 /* Rerun timer for screen updates. */
2871 vt_resume_flush_timer(vd->vd_curwindow, 0);
2875 * Register as console. If it already registered, cnadd() will ignore
2878 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2882 vt_suspend_handler(void *priv)
2884 struct vt_device *vd;
2887 vd->vd_flags |= VDF_SUSPENDED;
2888 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2889 vd->vd_driver->vd_suspend(vd);
2893 vt_resume_handler(void *priv)
2895 struct vt_device *vd;
2898 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2899 vd->vd_driver->vd_resume(vd);
2900 vd->vd_flags &= ~VDF_SUSPENDED;
2904 vt_allocate(const struct vt_driver *drv, void *softc)
2907 if (!vty_enabled(VTY_VT))
2910 if (main_vd->vd_driver == NULL) {
2911 main_vd->vd_driver = drv;
2912 printf("VT: initialize with new VT driver \"%s\".\n",
2916 * Check if have rights to replace current driver. For example:
2917 * it is bad idea to replace KMS driver with generic VGA one.
2919 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2920 printf("VT: Driver priority %d too low. Current %d\n ",
2921 drv->vd_priority, main_vd->vd_driver->vd_priority);
2924 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2925 main_vd->vd_driver->vd_name, drv->vd_name);
2928 vt_replace_backend(drv, softc);
2932 vt_deallocate(const struct vt_driver *drv, void *softc)
2935 if (!vty_enabled(VTY_VT))
2938 if (main_vd->vd_prev_driver == NULL ||
2939 main_vd->vd_driver != drv ||
2940 main_vd->vd_softc != softc)
2943 printf("VT: Switching back from \"%s\" to \"%s\".\n",
2944 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
2946 vt_replace_backend(NULL, NULL);
2950 vt_suspend(struct vt_device *vd)
2954 if (vt_suspendswitch == 0)
2956 /* Save current window. */
2957 vd->vd_savedwindow = vd->vd_curwindow;
2958 /* Ask holding process to free window and switch to console window */
2959 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2961 /* Wait for the window switch to complete. */
2964 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2965 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2970 vt_resume(struct vt_device *vd)
2973 if (vt_suspendswitch == 0)
2975 /* Switch back to saved window, if any */
2976 vt_proc_window_switch(vd->vd_savedwindow);
2977 vd->vd_savedwindow = NULL;