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, 0, "vt(9) parameters");
124 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
125 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
126 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
127 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
128 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
130 /* Allow to disable some keyboard combinations. */
131 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
132 "See kbdmap(5) to configure.");
133 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
134 "See kbdmap(5) to configure.");
135 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
136 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
137 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
138 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
139 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
140 "See kbdmap(5) to configure.");
142 /* Used internally, not a tunable. */
143 int vt_draw_logo_cpus;
144 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
145 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
146 "(0 = do not override)");
147 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
148 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
149 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
151 static unsigned int vt_unit = 0;
152 static MALLOC_DEFINE(M_VT, "vt", "vt device");
153 struct vt_device *main_vd = &vt_consdev;
156 extern unsigned int vt_logo_width;
157 extern unsigned int vt_logo_height;
158 extern unsigned int vt_logo_depth;
159 extern unsigned char vt_logo_image[];
161 #define vtterm_draw_cpu_logos(...) do {} while (0)
162 const unsigned int vt_logo_sprite_height;
166 extern struct vt_font vt_font_default;
167 #ifndef SC_NO_CUTPASTE
168 extern struct vt_mouse_cursor vt_default_mouse_pointer;
171 static int signal_vt_rel(struct vt_window *);
172 static int signal_vt_acq(struct vt_window *);
173 static int finish_vt_rel(struct vt_window *, int, int *);
174 static int finish_vt_acq(struct vt_window *);
175 static int vt_window_switch(struct vt_window *);
176 static int vt_late_window_switch(struct vt_window *);
177 static int vt_proc_alive(struct vt_window *);
178 static void vt_resize(struct vt_device *);
179 static void vt_update_static(void *);
180 #ifndef SC_NO_CUTPASTE
181 static void vt_mouse_paste(void);
183 static void vt_suspend_handler(void *priv);
184 static void vt_resume_handler(void *priv);
186 SET_DECLARE(vt_drv_set, struct vt_driver);
188 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT))
189 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH))
191 struct terminal vt_consterm;
192 static struct vt_window vt_conswindow;
193 #ifndef SC_NO_CONSDRAWN
194 static term_char_t vt_consdrawn[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
195 static term_color_t vt_consdrawnfg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
196 static term_color_t vt_consdrawnbg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
198 struct vt_device vt_consdev = {
201 .vd_prev_driver = NULL,
202 .vd_prev_softc = NULL,
203 .vd_flags = VDF_INVALID,
204 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
205 .vd_curwindow = &vt_conswindow,
208 #ifndef SC_NO_CUTPASTE
214 .vd_mcursor = &vt_default_mouse_pointer,
215 .vd_mcursor_fg = TC_WHITE,
216 .vd_mcursor_bg = TC_BLACK,
219 #ifndef SC_NO_CONSDRAWN
220 .vd_drawn = vt_consdrawn,
221 .vd_drawnfg = vt_consdrawnfg,
222 .vd_drawnbg = vt_consdrawnbg,
225 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
226 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
227 static struct vt_window vt_conswindow = {
228 .vw_number = VT_CONSWINDOW,
229 .vw_flags = VWF_CONSOLE,
231 .vb_buffer = &vt_constextbuf[0],
232 .vb_rows = &vt_constextbufrows[0],
233 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
236 .vb_flags = VBF_STATIC,
237 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
238 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
244 .vw_device = &vt_consdev,
245 .vw_terminal = &vt_consterm,
246 .vw_kbdmode = K_XLATE,
249 struct terminal vt_consterm = {
250 .tm_class = &vt_termclass,
251 .tm_softc = &vt_conswindow,
254 static struct consdev vt_consterm_consdev = {
255 .cn_ops = &termcn_cnops,
256 .cn_arg = &vt_consterm,
260 /* Add to set of consoles. */
261 DATA_SET(cons_set, vt_consterm_consdev);
264 * Right after kmem is done to allow early drivers to use locking and allocate
267 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
269 /* Delay until all devices attached, to not waste time. */
270 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
273 /* Initialize locks/mem depended members. */
275 vt_update_static(void *dummy)
278 if (!vty_enabled(VTY_VT))
280 if (main_vd->vd_driver != NULL)
281 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
282 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
283 main_vd->vd_width, main_vd->vd_height);
285 printf("VT: init without driver.\n");
287 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
288 cv_init(&main_vd->vd_winswitch, "vtwswt");
292 vt_schedule_flush(struct vt_device *vd, int ms)
296 /* Default to initial value. */
297 ms = 1000 / VT_TIMERFREQ;
299 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
303 vt_resume_flush_timer(struct vt_window *vw, int ms)
305 struct vt_device *vd = vw->vw_device;
307 if (vd->vd_curwindow != vw)
310 if (!(vd->vd_flags & VDF_ASYNC) ||
311 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
314 vt_schedule_flush(vd, ms);
318 vt_suspend_flush_timer(struct vt_device *vd)
321 * As long as this function is called locked, callout_stop()
322 * has the same effect like callout_drain() with regard to
323 * preventing the callback function from executing.
325 VT_LOCK_ASSERT(vd, MA_OWNED);
327 if (!(vd->vd_flags & VDF_ASYNC) ||
328 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
331 callout_stop(&vd->vd_timer);
335 vt_switch_timer(void *arg)
338 vt_late_window_switch((struct vt_window *)arg);
342 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
347 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
353 vw->vw_kbdmode = mode;
359 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
363 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
371 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
376 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
382 vw->vw_kbdstate &= ~LOCK_MASK;
383 vw->vw_kbdstate |= state & LOCK_MASK;
389 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
393 state = vw->vw_kbdstate & LOCK_MASK;
394 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
402 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
407 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
413 vw->vw_kbdstate &= ~LED_MASK;
414 vw->vw_kbdstate |= leds & LED_MASK;
420 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
424 leds = vw->vw_kbdstate & LED_MASK;
425 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
433 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
436 DPRINTF(40, "%s\n", __func__);
437 curvw->vw_switch_to = vw;
438 /* Set timer to allow switch in case when process hang. */
439 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
440 vt_switch_timer, (void *)vw);
441 /* Notify process about vt switch attempt. */
442 DPRINTF(30, "%s: Notify process.\n", __func__);
443 signal_vt_rel(curvw);
449 vt_window_postswitch(struct vt_window *vw)
456 /* vt_late_window_switch will done VT switching for regular case. */
458 vt_late_window_switch(struct vt_window *vw)
462 callout_stop(&vw->vw_proc_dead_timer);
464 ret = vt_window_switch(vw);
468 /* Notify owner process about terminal availability. */
469 if (vw->vw_smode.mode == VT_PROCESS) {
470 ret = vt_window_postswitch(vw);
477 vt_proc_window_switch(struct vt_window *vw)
479 struct vt_window *curvw;
480 struct vt_device *vd;
483 /* Prevent switching to NULL */
485 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
489 curvw = vd->vd_curwindow;
491 /* Check if virtual terminal is locked */
492 if (curvw->vw_flags & VWF_VTYLOCK)
495 /* Check if switch already in progress */
496 if (curvw->vw_flags & VWF_SWWAIT_REL) {
497 /* Check if switching to same window */
498 if (curvw->vw_switch_to == vw) {
499 DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
500 return (0); /* success */
502 DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
506 /* Avoid switching to already selected window */
508 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
509 return (0); /* success */
512 /* Ask current process permission to switch away. */
513 if (curvw->vw_smode.mode == VT_PROCESS) {
514 DPRINTF(30, "%s: VT_PROCESS ", __func__);
515 if (vt_proc_alive(curvw) == FALSE) {
516 DPRINTF(30, "Dead. Cleaning.");
519 DPRINTF(30, "%s: Signaling process.\n", __func__);
520 /* Alive, try to ask him. */
521 ret = vt_window_preswitch(vw, curvw);
522 /* Wait for process answer or timeout. */
528 ret = vt_late_window_switch(vw);
532 /* Switch window ignoring process locking. */
534 vt_window_switch(struct vt_window *vw)
536 struct vt_device *vd = vw->vw_device;
537 struct vt_window *curvw = vd->vd_curwindow;
542 * When grabbing the console for the debugger, avoid
543 * locks as that can result in deadlock. While this
544 * could use try locks, that wouldn't really make a
545 * difference as there are sufficient barriers in
546 * debugger entry/exit to be equivalent to
547 * successfully try-locking here.
551 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)))
554 vd->vd_curwindow = vw;
555 vd->vd_flags |= VDF_INVALID;
556 if (vd->vd_driver->vd_postswitch)
557 vd->vd_driver->vd_postswitch(vd);
567 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
572 vt_suspend_flush_timer(vd);
574 vd->vd_curwindow = vw;
575 vd->vd_flags |= VDF_INVALID;
576 cv_broadcast(&vd->vd_winswitch);
579 if (vd->vd_driver->vd_postswitch)
580 vd->vd_driver->vd_postswitch(vd);
582 vt_resume_flush_timer(vw, 0);
584 /* Restore per-window keyboard mode. */
586 kbd = kbd_get_keyboard(vd->vd_keyboard);
588 if (curvw->vw_kbdmode == K_XLATE)
589 vt_save_kbd_state(curvw, kbd);
591 vt_update_kbd_mode(vw, kbd);
592 vt_update_kbd_state(vw, kbd);
595 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
601 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
604 size->tp_row = vd->vd_height;
605 if (vt_draw_logo_cpus)
606 size->tp_row -= vt_logo_sprite_height;
607 size->tp_col = vd->vd_width;
609 size->tp_row /= vf->vf_height;
610 size->tp_col /= vf->vf_width;
615 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
618 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
619 if (vt_draw_logo_cpus)
620 rect->tr_begin.tp_row = vt_logo_sprite_height;
622 rect->tr_end.tp_row = vd->vd_height;
623 rect->tr_end.tp_col = vd->vd_width;
626 rect->tr_begin.tp_row =
627 howmany(rect->tr_begin.tp_row, vf->vf_height);
629 rect->tr_end.tp_row /= vf->vf_height;
630 rect->tr_end.tp_col /= vf->vf_width;
635 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
638 size->ws_ypixel = vd->vd_height;
639 if (vt_draw_logo_cpus)
640 size->ws_ypixel -= vt_logo_sprite_height;
641 size->ws_row = size->ws_ypixel;
642 size->ws_col = size->ws_xpixel = vd->vd_width;
644 size->ws_row /= vf->vf_height;
645 size->ws_col /= vf->vf_width;
650 vt_compute_drawable_area(struct vt_window *vw)
652 struct vt_device *vd;
658 if (vw->vw_font == NULL) {
659 vw->vw_draw_area.tr_begin.tp_col = 0;
660 vw->vw_draw_area.tr_begin.tp_row = 0;
661 if (vt_draw_logo_cpus)
662 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
663 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
664 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
671 * Compute the drawable area, so that the text is centered on
675 height = vd->vd_height;
676 if (vt_draw_logo_cpus)
677 height -= vt_logo_sprite_height;
678 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
679 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
680 if (vt_draw_logo_cpus)
681 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
682 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
683 rounddown(vd->vd_width, vf->vf_width);
684 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
685 rounddown(height, vf->vf_height);
689 vt_scroll(struct vt_window *vw, int offset, int whence)
694 if ((vw->vw_flags & VWF_SCROLL) == 0)
697 vt_termsize(vw->vw_device, vw->vw_font, &size);
699 diff = vthistory_seek(&vw->vw_buf, offset, whence);
701 vw->vw_device->vd_flags |= VDF_INVALID;
702 vt_resume_flush_timer(vw, 0);
706 vt_machine_kbdevent(int c)
710 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
712 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
714 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
716 shutdown_nice(RB_HALT);
718 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
719 #ifndef SC_NO_CUTPASTE
720 /* Insert text from cut-paste buffer. */
724 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
726 shutdown_nice(RB_HALT|RB_POWEROFF);
728 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
730 * Request to immediate panic if sysctl
731 * kern.vt.enable_panic_key allow it.
734 panic("Forced by the panic key");
736 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
738 shutdown_nice(RB_AUTOBOOT);
740 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
741 /* Force activatation/deactivation of the screen saver. */
744 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
745 /* Put machine into Stand-By mode. */
746 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
748 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
749 /* Suspend machine. */
750 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
758 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
760 struct vt_device *vd;
764 /* Only special keys handled in ScrollLock mode */
765 if ((c & SPCLKEY) == 0)
771 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
772 vw = vd->vd_windows[c - F_SCR];
773 vt_proc_window_switch(vw);
781 /* Turn scrolling off. */
782 vt_scroll(vw, 0, VHS_END);
783 VTBUF_SLCK_DISABLE(&vw->vw_buf);
784 vw->vw_flags &= ~VWF_SCROLL;
787 case FKEY | F(49): /* Home key. */
788 vt_scroll(vw, 0, VHS_SET);
790 case FKEY | F(50): /* Arrow up. */
791 vt_scroll(vw, -1, VHS_CUR);
793 case FKEY | F(51): /* Page up. */
794 vt_termsize(vd, vw->vw_font, &size);
795 vt_scroll(vw, -size.tp_row, VHS_CUR);
797 case FKEY | F(57): /* End key. */
798 vt_scroll(vw, 0, VHS_END);
800 case FKEY | F(58): /* Arrow down. */
801 vt_scroll(vw, 1, VHS_CUR);
803 case FKEY | F(59): /* Page down. */
804 vt_termsize(vd, vw->vw_font, &size);
805 vt_scroll(vw, size.tp_row, VHS_CUR);
814 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
816 struct vt_window *vw = vd->vd_curwindow;
818 random_harvest_queue(&c, sizeof(c), RANDOM_KEYBOARD);
819 #if VT_ALT_TO_ESC_HACK
821 switch (c & ~RELKEY) {
822 case (SPCLKEY | RALT):
823 if (vt_enable_altgr != 0)
825 case (SPCLKEY | LALT):
826 vd->vd_kbstate &= ~ALKED;
828 /* Other keys ignored for RELKEY event. */
831 switch (c & ~RELKEY) {
832 case (SPCLKEY | RALT):
833 if (vt_enable_altgr != 0)
835 case (SPCLKEY | LALT):
836 vd->vd_kbstate |= ALKED;
841 /* Other keys ignored for RELKEY event. */
845 if (vt_machine_kbdevent(c))
848 if (vw->vw_flags & VWF_SCROLL) {
849 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
850 /* Scroll mode keys handled, nothing to do more. */
857 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
858 vw = vd->vd_windows[c - F_SCR];
859 vt_proc_window_switch(vw);
865 /* Switch to next VT. */
866 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
867 vw = vd->vd_windows[c];
868 vt_proc_window_switch(vw);
871 /* Switch to previous VT. */
872 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
873 vw = vd->vd_windows[c];
874 vt_proc_window_switch(vw);
877 vt_save_kbd_state(vw, kbd);
879 if (vw->vw_kbdstate & SLKED) {
880 /* Turn scrolling on. */
881 vw->vw_flags |= VWF_SCROLL;
882 VTBUF_SLCK_ENABLE(&vw->vw_buf);
884 /* Turn scrolling off. */
885 vw->vw_flags &= ~VWF_SCROLL;
886 VTBUF_SLCK_DISABLE(&vw->vw_buf);
887 vt_scroll(vw, 0, VHS_END);
892 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
893 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
894 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
895 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
896 /* F1 through F12 keys. */
897 terminal_input_special(vw->vw_terminal,
898 TKEY_F1 + c - (FKEY | F(1)));
900 case FKEY | F(49): /* Home key. */
901 terminal_input_special(vw->vw_terminal, TKEY_HOME);
903 case FKEY | F(50): /* Arrow up. */
904 terminal_input_special(vw->vw_terminal, TKEY_UP);
906 case FKEY | F(51): /* Page up. */
907 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
909 case FKEY | F(53): /* Arrow left. */
910 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
912 case FKEY | F(55): /* Arrow right. */
913 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
915 case FKEY | F(57): /* End key. */
916 terminal_input_special(vw->vw_terminal, TKEY_END);
918 case FKEY | F(58): /* Arrow down. */
919 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
921 case FKEY | F(59): /* Page down. */
922 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
924 case FKEY | F(60): /* Insert key. */
925 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
927 case FKEY | F(61): /* Delete key. */
928 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
931 } else if (KEYFLAGS(c) == 0) {
932 /* Don't do UTF-8 conversion when doing raw mode. */
933 if (vw->vw_kbdmode == K_XLATE) {
934 #if VT_ALT_TO_ESC_HACK
935 if (vd->vd_kbstate & ALKED) {
937 * Prepend ESC sequence if one of ALT keys down.
939 terminal_input_char(vw->vw_terminal, 0x1b);
943 kdb_alt_break(c, &vd->vd_altbrk);
945 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
947 terminal_input_raw(vw->vw_terminal, c);
953 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
955 struct vt_device *vd = arg;
961 case KBDIO_UNLOADING:
963 vd->vd_keyboard = -1;
964 kbd_release(kbd, (void *)vd);
971 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
972 vt_processkey(kbd, vd, c);
978 vt_allocate_keyboard(struct vt_device *vd)
980 int grabbed, i, idx0, idx;
985 * If vt_upgrade() happens while the console is grabbed, we are
986 * potentially going to switch keyboard devices while the keyboard is in
987 * use. Unwind the grabbing of the current keyboard first, then we will
988 * re-grab the new keyboard below, before we return.
990 if (vd->vd_curwindow == &vt_conswindow) {
991 grabbed = vd->vd_curwindow->vw_grabbed;
992 for (i = 0; i < grabbed; ++i)
993 vtterm_cnungrab(vd->vd_curwindow->vw_terminal);
996 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
998 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
999 k0 = kbd_get_keyboard(idx0);
1001 for (idx = kbd_find_keyboard2("*", -1, 0);
1003 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
1004 k = kbd_get_keyboard(idx);
1006 if (idx == idx0 || KBD_IS_BUSY(k))
1009 bzero(&ki, sizeof(ki));
1010 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
1011 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
1012 ki.kb_unit = k->kb_unit;
1014 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
1017 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
1018 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
1020 DPRINTF(10, "%s: No keyboard found.\n", __func__);
1024 vd->vd_keyboard = idx0;
1025 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
1027 if (vd->vd_curwindow == &vt_conswindow) {
1028 for (i = 0; i < grabbed; ++i)
1029 vtterm_cngrab(vd->vd_curwindow->vw_terminal);
1036 vtterm_bell(struct terminal *tm)
1038 struct vt_window *vw = tm->tm_softc;
1039 struct vt_device *vd = vw->vw_device;
1041 if (!vt_enable_bell)
1044 if (vd->vd_flags & VDF_QUIET_BELL)
1047 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
1051 vtterm_beep(struct terminal *tm, u_int param)
1055 if (!vt_enable_bell)
1058 if ((param == 0) || ((param & 0xffff) == 0)) {
1063 period = ((param >> 16) & 0xffff) * hz / 1000;
1064 freq = 1193182 / (param & 0xffff);
1066 sysbeep(freq, period);
1070 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1072 struct vt_window *vw = tm->tm_softc;
1074 vtbuf_cursor_position(&vw->vw_buf, p);
1078 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1080 struct vt_window *vw = tm->tm_softc;
1082 vtbuf_putchar(&vw->vw_buf, p, c);
1086 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1088 struct vt_window *vw = tm->tm_softc;
1090 vtbuf_fill(&vw->vw_buf, r, c);
1094 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1095 const term_pos_t *p)
1097 struct vt_window *vw = tm->tm_softc;
1099 vtbuf_copy(&vw->vw_buf, r, p);
1103 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1105 struct vt_window *vw = tm->tm_softc;
1108 case TP_SETLOCALCURSOR:
1110 * 0 means normal (usually block), 1 means hidden, and
1111 * 2 means blinking (always block) for compatibility with
1112 * syscons. We don't support any changes except hiding,
1113 * so must map 2 to 0.
1115 arg = (arg == 1) ? 0 : 1;
1118 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1119 vt_resume_flush_timer(vw, 0);
1122 vw->vw_mouse_level = arg;
1128 vt_determine_colors(term_char_t c, int cursor,
1129 term_color_t *fg, term_color_t *bg)
1136 *fg = TCHAR_FGCOLOR(c);
1137 if (TCHAR_FORMAT(c) & TF_BOLD)
1138 *fg = TCOLOR_LIGHT(*fg);
1139 *bg = TCHAR_BGCOLOR(c);
1140 if (TCHAR_FORMAT(c) & TF_BLINK)
1141 *bg = TCOLOR_LIGHT(*bg);
1143 if (TCHAR_FORMAT(c) & TF_REVERSE)
1155 #ifndef SC_NO_CUTPASTE
1157 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1159 unsigned int mx, my;
1162 * We use the cursor position saved during the current refresh,
1163 * in case the cursor moved since.
1165 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1166 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1168 if (mx >= area->tr_end.tp_col ||
1169 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1170 my >= area->tr_end.tp_row ||
1171 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1177 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked)
1180 struct vt_window *vw;
1184 vw = vd->vd_curwindow;
1187 x = vd->vd_mx_drawn;
1188 y = vd->vd_my_drawn;
1191 area.tr_begin.tp_col = x / vf->vf_width;
1192 area.tr_begin.tp_row = y / vf->vf_height;
1193 area.tr_end.tp_col =
1194 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1195 area.tr_end.tp_row =
1196 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1199 * No font loaded (ie. vt_vga operating in textmode).
1201 * FIXME: This fake area needs to be revisited once the
1202 * mouse cursor is supported in vt_vga's textmode.
1204 area.tr_begin.tp_col = x;
1205 area.tr_begin.tp_row = y;
1206 area.tr_end.tp_col = x + 2;
1207 area.tr_end.tp_row = y + 2;
1211 vtbuf_lock(&vw->vw_buf);
1212 if (vd->vd_driver->vd_invalidate_text)
1213 vd->vd_driver->vd_invalidate_text(vd, &area);
1214 vtbuf_dirty(&vw->vw_buf, &area);
1216 vtbuf_unlock(&vw->vw_buf);
1221 vt_set_border(struct vt_device *vd, const term_rect_t *area,
1222 const term_color_t c)
1224 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
1226 if (drawrect == NULL)
1230 if (area->tr_begin.tp_row > 0)
1231 drawrect(vd, 0, 0, vd->vd_width - 1,
1232 area->tr_begin.tp_row - 1, 1, c);
1235 if (area->tr_begin.tp_col > 0)
1236 drawrect(vd, 0, area->tr_begin.tp_row,
1237 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
1240 if (area->tr_end.tp_col < vd->vd_width)
1241 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
1242 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
1245 if (area->tr_end.tp_row < vd->vd_height)
1246 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
1247 vd->vd_height - 1, 1, c);
1251 vt_flush(struct vt_device *vd)
1253 struct vt_window *vw;
1256 #ifndef SC_NO_CUTPASTE
1257 int cursor_was_shown, cursor_moved;
1260 vw = vd->vd_curwindow;
1264 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1268 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1271 vtbuf_lock(&vw->vw_buf);
1273 #ifndef SC_NO_CUTPASTE
1274 cursor_was_shown = vd->vd_mshown;
1275 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1276 vd->vd_my != vd->vd_my_drawn);
1278 /* Check if the cursor should be displayed or not. */
1279 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1280 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1281 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1288 * If the cursor changed display state or moved, we must mark
1289 * the old position as dirty, so that it's erased.
1291 if (cursor_was_shown != vd->vd_mshown ||
1292 (vd->vd_mshown && cursor_moved))
1293 vt_mark_mouse_position_as_dirty(vd, true);
1296 * Save position of the mouse cursor. It's used by backends to
1297 * know where to draw the cursor and during the next refresh to
1298 * erase the previous position.
1300 vd->vd_mx_drawn = vd->vd_mx;
1301 vd->vd_my_drawn = vd->vd_my;
1304 * If the cursor is displayed and has moved since last refresh,
1305 * mark the new position as dirty.
1307 if (vd->vd_mshown && cursor_moved)
1308 vt_mark_mouse_position_as_dirty(vd, true);
1311 vtbuf_undirty(&vw->vw_buf, &tarea);
1313 /* Force a full redraw when the screen contents might be invalid. */
1314 if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) {
1315 vd->vd_flags &= ~VDF_INVALID;
1317 vt_set_border(vd, &vw->vw_draw_area, TC_BLACK);
1318 vt_termrect(vd, vf, &tarea);
1319 if (vd->vd_driver->vd_invalidate_text)
1320 vd->vd_driver->vd_invalidate_text(vd, &tarea);
1321 if (vt_draw_logo_cpus)
1322 vtterm_draw_cpu_logos(vd);
1325 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1326 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1327 vtbuf_unlock(&vw->vw_buf);
1331 vtbuf_unlock(&vw->vw_buf);
1338 struct vt_device *vd;
1342 /* Update screen if required. */
1343 changed = vt_flush(vd);
1345 /* Schedule for next update. */
1347 vt_schedule_flush(vd, 0);
1349 vd->vd_timer_armed = 0;
1353 vtterm_pre_input(struct terminal *tm)
1355 struct vt_window *vw = tm->tm_softc;
1357 vtbuf_lock(&vw->vw_buf);
1361 vtterm_post_input(struct terminal *tm)
1363 struct vt_window *vw = tm->tm_softc;
1365 vtbuf_unlock(&vw->vw_buf);
1366 vt_resume_flush_timer(vw, 0);
1370 vtterm_done(struct terminal *tm)
1372 struct vt_window *vw = tm->tm_softc;
1373 struct vt_device *vd = vw->vw_device;
1375 if (kdb_active || panicstr != NULL) {
1376 /* Switch to the debugger. */
1377 if (vd->vd_curwindow != vw) {
1378 vd->vd_curwindow = vw;
1379 vd->vd_flags |= VDF_INVALID;
1380 if (vd->vd_driver->vd_postswitch)
1381 vd->vd_driver->vd_postswitch(vd);
1383 vd->vd_flags &= ~VDF_SPLASH;
1385 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1392 vtterm_splash(struct vt_device *vd)
1394 vt_axis_t top, left;
1396 /* Display a nice boot splash. */
1397 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1399 top = (vd->vd_height - vt_logo_height) / 2;
1400 left = (vd->vd_width - vt_logo_width) / 2;
1401 switch (vt_logo_depth) {
1403 /* XXX: Unhardcode colors! */
1404 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1405 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1406 left, top, TC_WHITE, TC_BLACK);
1408 vd->vd_flags |= VDF_SPLASH;
1415 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1417 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1418 struct vt_window *vw = tm->tm_softc;
1419 struct vt_device *vd = vw->vw_device;
1424 if (!vty_enabled(VTY_VT))
1427 if (vd->vd_flags & VDF_INITIALIZED)
1428 /* Initialization already done. */
1431 SET_FOREACH(vtdlist, vt_drv_set) {
1433 if (vtd->vd_probe == NULL)
1435 if (vtd->vd_probe(vd) == CN_DEAD)
1437 if ((vtdbest == NULL) ||
1438 (vtd->vd_priority > vtdbest->vd_priority))
1441 if (vtdbest == NULL) {
1442 cp->cn_pri = CN_DEAD;
1443 vd->vd_flags |= VDF_DEAD;
1445 vd->vd_driver = vtdbest;
1446 cp->cn_pri = vd->vd_driver->vd_init(vd);
1449 /* Check if driver's vt_init return CN_DEAD. */
1450 if (cp->cn_pri == CN_DEAD) {
1451 vd->vd_flags |= VDF_DEAD;
1454 /* Initialize any early-boot keyboard drivers */
1455 kbd_configure(KB_CONF_PROBE_ONLY);
1457 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1458 vd->vd_windows[VT_CONSWINDOW] = vw;
1459 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1461 /* Attach default font if not in TEXTMODE. */
1462 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1463 vw->vw_font = vtfont_ref(&vt_font_default);
1464 vt_compute_drawable_area(vw);
1468 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1469 * that we have the real viewable size, fix it in the static
1472 if (vd->vd_width != 0 && vd->vd_height != 0)
1473 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1475 vtbuf_init_early(&vw->vw_buf);
1476 vt_winsize(vd, vw->vw_font, &wsz);
1477 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1479 attr.ta_format = TCHAR_FORMAT(c);
1480 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1481 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1482 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1484 if (vtdbest != NULL) {
1489 vd->vd_flags |= VDF_INITIALIZED;
1494 vtterm_cngetc(struct terminal *tm)
1496 struct vt_window *vw = tm->tm_softc;
1497 struct vt_device *vd = vw->vw_device;
1501 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1502 return (*vw->vw_kbdsq++);
1504 /* Make sure the splash screen is not there. */
1505 if (vd->vd_flags & VDF_SPLASH) {
1507 vd->vd_flags &= ~VDF_SPLASH;
1508 /* Mark screen as invalid to force update */
1509 vd->vd_flags |= VDF_INVALID;
1513 /* Stripped down keyboard handler. */
1514 kbd = kbd_get_keyboard(vd->vd_keyboard);
1518 /* Force keyboard input mode to K_XLATE */
1519 vw->vw_kbdmode = K_XLATE;
1520 vt_update_kbd_mode(vw, kbd);
1522 /* Switch the keyboard to polling to make it work here. */
1523 kbdd_poll(kbd, TRUE);
1524 c = kbdd_read_char(kbd, 0);
1525 kbdd_poll(kbd, FALSE);
1529 if (vw->vw_flags & VWF_SCROLL) {
1530 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1535 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1539 vt_save_kbd_state(vw, kbd);
1540 if (vw->vw_kbdstate & SLKED) {
1541 /* Turn scrolling on. */
1542 vw->vw_flags |= VWF_SCROLL;
1543 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1545 /* Turn scrolling off. */
1546 vt_scroll(vw, 0, VHS_END);
1547 vw->vw_flags &= ~VWF_SCROLL;
1548 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1551 /* XXX: KDB can handle history. */
1552 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1553 vw->vw_kbdsq = "\x1b[A";
1555 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1556 vw->vw_kbdsq = "\x1b[B";
1558 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1559 vw->vw_kbdsq = "\x1b[C";
1561 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1562 vw->vw_kbdsq = "\x1b[D";
1566 /* Force refresh to make scrollback work. */
1568 } else if (KEYFLAGS(c) == 0) {
1569 return (KEYCHAR(c));
1572 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1573 return (*vw->vw_kbdsq++);
1579 vtterm_cngrab(struct terminal *tm)
1581 struct vt_device *vd;
1582 struct vt_window *vw;
1589 vt_window_switch(vw);
1591 kbd = kbd_get_keyboard(vd->vd_keyboard);
1595 if (vw->vw_grabbed++ > 0)
1599 * Make sure the keyboard is accessible even when the kbd device
1600 * driver is disabled.
1604 /* We shall always use the keyboard in the XLATE mode here. */
1605 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1606 vw->vw_kbdmode = K_XLATE;
1607 vt_update_kbd_mode(vw, kbd);
1609 kbdd_poll(kbd, TRUE);
1613 vtterm_cnungrab(struct terminal *tm)
1615 struct vt_device *vd;
1616 struct vt_window *vw;
1622 kbd = kbd_get_keyboard(vd->vd_keyboard);
1626 if (--vw->vw_grabbed > 0)
1629 kbdd_poll(kbd, FALSE);
1631 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1632 vt_update_kbd_mode(vw, kbd);
1637 vtterm_opened(struct terminal *tm, int opened)
1639 struct vt_window *vw = tm->tm_softc;
1640 struct vt_device *vd = vw->vw_device;
1643 vd->vd_flags &= ~VDF_SPLASH;
1645 vw->vw_flags |= VWF_OPENED;
1647 vw->vw_flags &= ~VWF_OPENED;
1648 /* TODO: finish ACQ/REL */
1654 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1656 struct vt_device *vd = vw->vw_device;
1657 struct terminal *tm = vw->vw_terminal;
1664 * Changing fonts is a little tricky. We must prevent
1665 * simultaneous access to the device, so we must stop
1666 * the display timer and the terminal from accessing.
1667 * We need to switch fonts and grow our screen buffer.
1669 * XXX: Right now the code uses terminal_mute() to
1670 * prevent data from reaching the console driver while
1671 * resizing the screen buffer. This isn't elegant...
1675 if (vw->vw_flags & VWF_BUSY) {
1676 /* Another process is changing the font. */
1680 vw->vw_flags |= VWF_BUSY;
1683 vt_termsize(vd, vf, &size);
1684 vt_winsize(vd, vf, &wsz);
1686 /* Grow the screen buffer and terminal. */
1687 terminal_mute(tm, 1);
1688 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1689 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1690 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1691 terminal_mute(tm, 0);
1693 /* Actually apply the font to the current window. */
1695 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1697 * In case vt_change_font called to update size we don't need
1698 * to update font link.
1700 vtfont_unref(vw->vw_font);
1701 vw->vw_font = vtfont_ref(vf);
1705 * Compute the drawable area and move the mouse cursor inside
1706 * it, in case the new area is smaller than the previous one.
1708 vt_compute_drawable_area(vw);
1709 vd->vd_mx = min(vd->vd_mx,
1710 vw->vw_draw_area.tr_end.tp_col -
1711 vw->vw_draw_area.tr_begin.tp_col - 1);
1712 vd->vd_my = min(vd->vd_my,
1713 vw->vw_draw_area.tr_end.tp_row -
1714 vw->vw_draw_area.tr_begin.tp_row - 1);
1716 /* Force a full redraw the next timer tick. */
1717 if (vd->vd_curwindow == vw) {
1718 vd->vd_flags |= VDF_INVALID;
1719 vt_resume_flush_timer(vw, 0);
1721 vw->vw_flags &= ~VWF_BUSY;
1727 vt_proc_alive(struct vt_window *vw)
1731 if (vw->vw_smode.mode != VT_PROCESS)
1735 if ((p = pfind(vw->vw_pid)) != NULL)
1737 if (vw->vw_proc == p)
1740 vw->vw_smode.mode = VT_AUTO;
1741 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1748 signal_vt_rel(struct vt_window *vw)
1751 if (vw->vw_smode.mode != VT_PROCESS)
1753 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1758 vw->vw_flags |= VWF_SWWAIT_REL;
1759 PROC_LOCK(vw->vw_proc);
1760 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1761 PROC_UNLOCK(vw->vw_proc);
1762 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1767 signal_vt_acq(struct vt_window *vw)
1770 if (vw->vw_smode.mode != VT_PROCESS)
1772 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1773 cnavailable(vw->vw_terminal->consdev, FALSE);
1774 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1779 vw->vw_flags |= VWF_SWWAIT_ACQ;
1780 PROC_LOCK(vw->vw_proc);
1781 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1782 PROC_UNLOCK(vw->vw_proc);
1783 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1788 finish_vt_rel(struct vt_window *vw, int release, int *s)
1791 if (vw->vw_flags & VWF_SWWAIT_REL) {
1792 vw->vw_flags &= ~VWF_SWWAIT_REL;
1794 callout_drain(&vw->vw_proc_dead_timer);
1795 vt_late_window_switch(vw->vw_switch_to);
1803 finish_vt_acq(struct vt_window *vw)
1806 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1807 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1813 #ifndef SC_NO_CUTPASTE
1815 vt_mouse_terminput_button(struct vt_device *vd, int button)
1817 struct vt_window *vw;
1819 char mouseb[6] = "\x1B[M";
1822 vw = vd->vd_curwindow;
1825 /* Translate to char position. */
1826 x = vd->vd_mx / vf->vf_width;
1827 y = vd->vd_my / vf->vf_height;
1828 /* Avoid overflow. */
1829 x = MIN(x, 255 - '!');
1830 y = MIN(y, 255 - '!');
1832 mouseb[3] = ' ' + button;
1833 mouseb[4] = '!' + x;
1834 mouseb[5] = '!' + y;
1836 for (i = 0; i < sizeof(mouseb); i++)
1837 terminal_input_char(vw->vw_terminal, mouseb[i]);
1841 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1846 case MOUSE_BUTTON_EVENT:
1848 /* Mouse button pressed. */
1849 if (event & MOUSE_BUTTON1DOWN)
1850 vt_mouse_terminput_button(vd, 0);
1851 if (event & MOUSE_BUTTON2DOWN)
1852 vt_mouse_terminput_button(vd, 1);
1853 if (event & MOUSE_BUTTON3DOWN)
1854 vt_mouse_terminput_button(vd, 2);
1856 /* Mouse button released. */
1857 vt_mouse_terminput_button(vd, 3);
1861 case MOUSE_MOTION_EVENT:
1862 if (mouse->u.data.z < 0) {
1864 sc_mouse_input_button(vd, 64);
1865 } else if (mouse->u.data.z > 0) {
1867 sc_mouse_input_button(vd, 65);
1880 len = VD_PASTEBUFLEN(main_vd);
1881 buf = VD_PASTEBUF(main_vd);
1882 len /= sizeof(term_char_t);
1883 for (i = 0; i < len; i++) {
1886 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1892 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1894 struct vt_device *vd;
1895 struct vt_window *vw;
1901 vw = vd->vd_curwindow;
1905 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1907 * Either the mouse is disabled, or the window is in
1908 * "graphics mode". The graphics mode is usually set by
1909 * an X server, using the KDSETMODE ioctl.
1913 if (vf == NULL) /* Text mode. */
1917 * TODO: add flag about pointer position changed, to not redraw chars
1918 * under mouse pointer when nothing changed.
1921 if (vw->vw_mouse_level > 0)
1922 vt_mouse_terminput(vd, type, x, y, event, cnt);
1926 case MOUSE_MOTION_EVENT:
1931 vt_termsize(vd, vf, &size);
1936 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1937 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1941 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1942 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1943 vd->vd_mx / vf->vf_width,
1944 vd->vd_my / vf->vf_height);
1946 vt_resume_flush_timer(vw, 0);
1948 case MOUSE_BUTTON_EVENT:
1956 case MOUSE_BUTTON1DOWN:
1959 mark = VTB_MARK_END;
1961 case 1: /* single click: start cut operation */
1962 mark = VTB_MARK_START;
1964 case 2: /* double click: cut a word */
1965 mark = VTB_MARK_WORD;
1967 case 3: /* triple click: cut a line */
1968 mark = VTB_MARK_ROW;
1972 case VT_MOUSE_PASTEBUTTON:
1981 case VT_MOUSE_EXTENDBUTTON:
1984 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1985 mark = VTB_MARK_EXTEND;
1990 mark = VTB_MARK_EXTEND;
1998 /* Save buttons state. */
2000 vd->vd_mstate |= event;
2002 vd->vd_mstate &= ~event;
2004 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
2005 vd->vd_my / vf->vf_height) == 1) {
2007 * We have something marked to copy, so update pointer to
2008 * window with selection.
2010 vt_resume_flush_timer(vw, 0);
2016 case VTB_MARK_EXTEND:
2019 /* Other types of mark do not require to copy data. */
2023 /* Get current selection size in bytes. */
2024 len = vtbuf_get_marked_len(&vw->vw_buf);
2028 /* Reallocate buffer only if old one is too small. */
2029 if (len > VD_PASTEBUFSZ(vd)) {
2030 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
2032 /* Update buffer size. */
2033 VD_PASTEBUFSZ(vd) = len;
2035 /* Request copy/paste buffer data, no more than `len' */
2036 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
2039 VD_PASTEBUFLEN(vd) = len;
2041 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
2046 vt_mouse_state(int show)
2048 struct vt_device *vd;
2049 struct vt_window *vw;
2052 vw = vd->vd_curwindow;
2056 vw->vw_flags |= VWF_MOUSE_HIDE;
2059 vw->vw_flags &= ~VWF_MOUSE_HIDE;
2063 /* Mark mouse position as dirty. */
2064 vt_mark_mouse_position_as_dirty(vd, false);
2065 vt_resume_flush_timer(vw, 0);
2070 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
2071 int nprot, vm_memattr_t *memattr)
2073 struct vt_window *vw = tm->tm_softc;
2074 struct vt_device *vd = vw->vw_device;
2076 if (vd->vd_driver->vd_fb_mmap)
2077 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2084 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2087 struct vt_window *vw = tm->tm_softc;
2088 struct vt_device *vd = vw->vw_device;
2091 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2092 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2103 cmd = VT_WAITACTIVE;
2130 cmd = CONS_SETWINORG;
2138 ival = IOCPARM_IVAL(data);
2139 data = (caddr_t)&ival;
2144 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2145 if (*(int *)data & ~0x7f)
2150 case GIO_DEADKEYMAP:
2151 case PIO_DEADKEYMAP:
2156 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2157 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2158 case KBADDKBD: /* add/remove keyboard to/from mux */
2163 kbd = kbd_get_keyboard(vd->vd_keyboard);
2165 error = kbdd_ioctl(kbd, cmd, data);
2167 if (error == ENOIOCTL) {
2168 if (cmd == KDGKBTYPE) {
2169 /* always return something? XXX */
2177 case KDGKBSTATE: { /* get keyboard state (locks) */
2180 if (vw == vd->vd_curwindow) {
2182 kbd = kbd_get_keyboard(vd->vd_keyboard);
2184 error = vt_save_kbd_state(vw, kbd);
2191 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2195 case KDSKBSTATE: { /* set keyboard state (locks) */
2198 state = *(int *)data;
2199 if (state & ~LOCK_MASK)
2202 vw->vw_kbdstate &= ~LOCK_MASK;
2203 vw->vw_kbdstate |= state;
2206 if (vw == vd->vd_curwindow) {
2208 kbd = kbd_get_keyboard(vd->vd_keyboard);
2210 error = vt_update_kbd_state(vw, kbd);
2216 case KDGETLED: { /* get keyboard LED status */
2219 if (vw == vd->vd_curwindow) {
2221 kbd = kbd_get_keyboard(vd->vd_keyboard);
2223 error = vt_save_kbd_leds(vw, kbd);
2230 *(int *)data = vw->vw_kbdstate & LED_MASK;
2234 case KDSETLED: { /* set keyboard LED status */
2237 leds = *(int *)data;
2238 if (leds & ~LED_MASK)
2241 vw->vw_kbdstate &= ~LED_MASK;
2242 vw->vw_kbdstate |= leds;
2245 if (vw == vd->vd_curwindow) {
2247 kbd = kbd_get_keyboard(vd->vd_keyboard);
2249 error = vt_update_kbd_leds(vw, kbd);
2256 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2257 KD_GRAPHICS : KD_TEXT;
2262 if (vw == vd->vd_curwindow) {
2264 kbd = kbd_get_keyboard(vd->vd_keyboard);
2266 error = vt_save_kbd_mode(vw, kbd);
2273 *(int *)data = vw->vw_kbdmode;
2280 mode = *(int *)data;
2285 vw->vw_kbdmode = mode;
2288 if (vw == vd->vd_curwindow) {
2290 kbd = kbd_get_keyboard(vd->vd_keyboard);
2292 error = vt_update_kbd_mode(vw, kbd);
2302 case FBIO_GETWINORG: /* get frame buffer window origin */
2303 case FBIO_GETDISPSTART: /* get display start address */
2304 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2305 case FBIO_BLANK: /* blank display */
2306 if (vd->vd_driver->vd_fb_ioctl)
2307 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2309 case CONS_BLANKTIME:
2313 if (*(int *)data < 0)
2315 if (*(int *)data != vd->vd_curwindow->vw_buf.vb_history_size)
2316 vtbuf_sethistory_size(&vd->vd_curwindow->vw_buf,
2321 *(int *)data = M_CG640x480;
2323 case CONS_BELLTYPE: /* set bell type sound */
2324 if ((*(int *)data) & CONS_QUIET_BELL)
2325 vd->vd_flags |= VDF_QUIET_BELL;
2327 vd->vd_flags &= ~VDF_QUIET_BELL;
2329 case CONS_GETINFO: {
2330 vid_info_t *vi = (vid_info_t *)data;
2331 if (vi->size != sizeof(struct vid_info))
2334 if (vw == vd->vd_curwindow) {
2336 kbd = kbd_get_keyboard(vd->vd_keyboard);
2338 vt_save_kbd_state(vw, kbd);
2342 vi->m_num = vd->vd_curwindow->vw_number + 1;
2343 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2344 /* XXX: other fields! */
2348 *(int *)data = 0x200;
2353 case CONS_MOUSECTL: {
2354 mouse_info_t *mouse = (mouse_info_t*)data;
2357 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2358 * should not be applied to individual TTYs, but only to
2361 switch (mouse->operation) {
2363 if (vd->vd_flags & VDF_MOUSECURSOR) {
2364 vd->vd_flags &= ~VDF_MOUSECURSOR;
2365 #ifndef SC_NO_CUTPASTE
2366 vt_mouse_state(VT_MOUSE_HIDE);
2371 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2372 vd->vd_flags |= VDF_MOUSECURSOR;
2373 vd->vd_mx = vd->vd_width / 2;
2374 vd->vd_my = vd->vd_height / 2;
2375 #ifndef SC_NO_CUTPASTE
2376 vt_mouse_state(VT_MOUSE_SHOW);
2387 if (vd->vd_flags & VDF_TEXTMODE)
2390 error = vtfont_load((void *)data, &vf);
2394 error = vt_change_font(vw, vf);
2398 case PIO_VFONT_DEFAULT: {
2399 /* Reset to default font. */
2400 error = vt_change_font(vw, &vt_font_default);
2404 scrmap_t *sm = (scrmap_t *)data;
2406 /* We don't have screen maps, so return a handcrafted one. */
2407 for (i = 0; i < 256; i++)
2413 * FIXME: This implementation is incomplete compared to
2416 switch (*(int *)data) {
2420 vw->vw_flags &= ~VWF_GRAPHICS;
2423 vw->vw_flags |= VWF_GRAPHICS;
2427 case KDENABIO: /* allow io operations */
2428 error = priv_check(td, PRIV_IO);
2431 error = securelevel_gt(td->td_ucred, 0);
2434 #if defined(__i386__)
2435 td->td_frame->tf_eflags |= PSL_IOPL;
2436 #elif defined(__amd64__)
2437 td->td_frame->tf_rflags |= PSL_IOPL;
2440 case KDDISABIO: /* disallow io operations (default) */
2441 #if defined(__i386__)
2442 td->td_frame->tf_eflags &= ~PSL_IOPL;
2443 #elif defined(__amd64__)
2444 td->td_frame->tf_rflags &= ~PSL_IOPL;
2447 case KDMKTONE: /* sound the bell */
2448 vtterm_beep(tm, *(u_int *)data);
2450 case KIOCSOUND: /* make tone (*data) hz */
2453 case CONS_SETKBD: /* set the new keyboard */
2456 if (vd->vd_keyboard != *(int *)data) {
2457 kbd = kbd_get_keyboard(*(int *)data);
2462 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2463 (void *)vd, vt_kbdevent, vd);
2465 if (vd->vd_keyboard != -1) {
2466 kbd = kbd_get_keyboard(vd->vd_keyboard);
2467 vt_save_kbd_state(vd->vd_curwindow, kbd);
2468 kbd_release(kbd, (void *)vd);
2470 kbd = kbd_get_keyboard(i);
2471 vd->vd_keyboard = i;
2473 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2474 vt_update_kbd_state(vd->vd_curwindow, kbd);
2476 error = EPERM; /* XXX */
2481 case CONS_RELKBD: /* release the current keyboard */
2484 if (vd->vd_keyboard != -1) {
2485 kbd = kbd_get_keyboard(vd->vd_keyboard);
2490 vt_save_kbd_state(vd->vd_curwindow, kbd);
2491 error = kbd_release(kbd, (void *)vd);
2493 vd->vd_keyboard = -1;
2500 win = *(int *)data - 1;
2501 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2503 if ((win >= VT_MAXWINDOWS) || (win < 0))
2505 return (vt_proc_window_switch(vd->vd_windows[win]));
2508 *(int *)data = vd->vd_curwindow->vw_number + 1;
2511 *(int *)data = vw->vw_number + 1;
2514 /* TODO: Check current state, switching can be in progress. */
2515 if ((*(int *)data) == 0x01)
2516 vw->vw_flags |= VWF_VTYLOCK;
2517 else if ((*(int *)data) == 0x02)
2518 vw->vw_flags &= ~VWF_VTYLOCK;
2524 for (i = 0; i < VT_MAXWINDOWS; i++) {
2525 vw = vd->vd_windows[i];
2528 if (!(vw->vw_flags & VWF_OPENED)) {
2529 *(int *)data = vw->vw_number + 1;
2536 case VT_WAITACTIVE: {
2541 idx = *(unsigned int *)data;
2542 if (idx > VT_MAXWINDOWS)
2545 vw = vd->vd_windows[idx - 1];
2548 while (vd->vd_curwindow != vw && error == 0)
2549 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2553 case VT_SETMODE: { /* set screen switcher mode */
2554 struct vt_mode *mode;
2557 mode = (struct vt_mode *)data;
2558 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2559 if (vw->vw_smode.mode == VT_PROCESS) {
2560 p1 = pfind(vw->vw_pid);
2561 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2564 DPRINTF(5, "error EPERM\n");
2570 if (mode->mode == VT_AUTO) {
2571 vw->vw_smode.mode = VT_AUTO;
2574 DPRINTF(5, "VT_AUTO, ");
2575 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2576 cnavailable(vw->vw_terminal->consdev, TRUE);
2577 /* were we in the middle of the vty switching process? */
2578 if (finish_vt_rel(vw, TRUE, &s) == 0)
2579 DPRINTF(5, "reset WAIT_REL, ");
2580 if (finish_vt_acq(vw) == 0)
2581 DPRINTF(5, "reset WAIT_ACQ, ");
2583 } else if (mode->mode == VT_PROCESS) {
2584 if (!ISSIGVALID(mode->relsig) ||
2585 !ISSIGVALID(mode->acqsig) ||
2586 !ISSIGVALID(mode->frsig)) {
2587 DPRINTF(5, "error EINVAL\n");
2590 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2591 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2592 vw->vw_proc = td->td_proc;
2593 vw->vw_pid = vw->vw_proc->p_pid;
2594 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2595 cnavailable(vw->vw_terminal->consdev, FALSE);
2597 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2604 case VT_GETMODE: /* get screen switcher mode */
2605 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2608 case VT_RELDISP: /* screen switcher ioctl */
2610 * This must be the current vty which is in the VT_PROCESS
2613 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2617 /* ...and this process is controlling it. */
2618 if (vw->vw_proc != td->td_proc) {
2622 switch(*(int *)data) {
2623 case VT_FALSE: /* user refuses to release screen, abort */
2624 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2625 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2626 SC_DRIVER_NAME, VT_UNIT(vw));
2628 case VT_TRUE: /* user has released screen, go on */
2629 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2630 if (vw->vw_flags & VWF_SWWAIT_REL) {
2631 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2632 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2633 SC_DRIVER_NAME, VT_UNIT(vw));
2638 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2639 if ((error = finish_vt_acq(vw)) == 0)
2640 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2641 SC_DRIVER_NAME, VT_UNIT(vw));
2652 static struct vt_window *
2653 vt_allocate_window(struct vt_device *vd, unsigned int window)
2655 struct vt_window *vw;
2656 struct terminal *tm;
2660 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2662 vw->vw_number = window;
2663 vw->vw_kbdmode = K_XLATE;
2665 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2666 vw->vw_font = vtfont_ref(&vt_font_default);
2667 vt_compute_drawable_area(vw);
2670 vt_termsize(vd, vw->vw_font, &size);
2671 vt_winsize(vd, vw->vw_font, &wsz);
2672 vtbuf_init(&vw->vw_buf, &size);
2674 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2675 terminal_set_winsize(tm, &wsz);
2676 vd->vd_windows[window] = vw;
2677 callout_init(&vw->vw_proc_dead_timer, 0);
2683 vt_upgrade(struct vt_device *vd)
2685 struct vt_window *vw;
2687 int register_handlers;
2689 if (!vty_enabled(VTY_VT))
2691 if (main_vd->vd_driver == NULL)
2694 for (i = 0; i < VT_MAXWINDOWS; i++) {
2695 vw = vd->vd_windows[i];
2698 vw = vt_allocate_window(vd, i);
2700 if (!(vw->vw_flags & VWF_READY)) {
2701 callout_init(&vw->vw_proc_dead_timer, 0);
2702 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2703 vw->vw_flags |= VWF_READY;
2704 if (vw->vw_flags & VWF_CONSOLE) {
2705 /* For existing console window. */
2706 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2707 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2713 if (vd->vd_curwindow == NULL)
2714 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2716 register_handlers = 0;
2717 if (!(vd->vd_flags & VDF_ASYNC)) {
2718 /* Attach keyboard. */
2719 vt_allocate_keyboard(vd);
2721 /* Init 25 Hz timer. */
2722 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2725 * Start timer when everything ready.
2726 * Note that the operations here are purposefully ordered.
2727 * We need to ensure vd_timer_armed is non-zero before we set
2728 * the VDF_ASYNC flag. That prevents this function from
2729 * racing with vt_resume_flush_timer() to update the
2730 * callout structure.
2732 atomic_add_acq_int(&vd->vd_timer_armed, 1);
2733 vd->vd_flags |= VDF_ASYNC;
2734 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2735 register_handlers = 1;
2740 /* Refill settings with new sizes. */
2743 if (register_handlers) {
2744 /* Register suspend/resume handlers. */
2745 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2746 vd, EVENTHANDLER_PRI_ANY);
2747 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2748 EVENTHANDLER_PRI_ANY);
2753 vt_resize(struct vt_device *vd)
2755 struct vt_window *vw;
2758 for (i = 0; i < VT_MAXWINDOWS; i++) {
2759 vw = vd->vd_windows[i];
2761 /* Assign default font to window, if not textmode. */
2762 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2763 vw->vw_font = vtfont_ref(&vt_font_default);
2766 /* Resize terminal windows */
2767 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2768 DPRINTF(100, "%s: vt_change_font() is busy, "
2769 "window %d\n", __func__, i);
2775 vt_replace_backend(const struct vt_driver *drv, void *softc)
2777 struct vt_device *vd;
2781 if (vd->vd_flags & VDF_ASYNC) {
2782 /* Stop vt_flush periodic task. */
2784 vt_suspend_flush_timer(vd);
2787 * Mute current terminal until we done. vt_change_font (called
2788 * from vt_resize) will unmute it.
2790 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2794 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2798 vd->vd_flags &= ~VDF_TEXTMODE;
2802 * We want to upgrade from the current driver to the
2806 vd->vd_prev_driver = vd->vd_driver;
2807 vd->vd_prev_softc = vd->vd_softc;
2808 vd->vd_driver = drv;
2809 vd->vd_softc = softc;
2811 vd->vd_driver->vd_init(vd);
2812 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
2814 * No driver given: we want to downgrade to the previous
2817 const struct vt_driver *old_drv;
2820 old_drv = vd->vd_driver;
2821 old_softc = vd->vd_softc;
2823 vd->vd_driver = vd->vd_prev_driver;
2824 vd->vd_softc = vd->vd_prev_softc;
2825 vd->vd_prev_driver = NULL;
2826 vd->vd_prev_softc = NULL;
2828 vd->vd_flags |= VDF_DOWNGRADE;
2830 vd->vd_driver->vd_init(vd);
2832 if (old_drv->vd_fini)
2833 old_drv->vd_fini(vd, old_softc);
2835 vd->vd_flags &= ~VDF_DOWNGRADE;
2840 /* Update windows sizes and initialize last items. */
2844 if (vd->vd_flags & VDF_SPLASH)
2848 if (vd->vd_flags & VDF_ASYNC) {
2849 /* Allow to put chars now. */
2850 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2851 /* Rerun timer for screen updates. */
2852 vt_resume_flush_timer(vd->vd_curwindow, 0);
2856 * Register as console. If it already registered, cnadd() will ignore
2859 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2863 vt_suspend_handler(void *priv)
2865 struct vt_device *vd;
2868 vd->vd_flags |= VDF_SUSPENDED;
2869 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2870 vd->vd_driver->vd_suspend(vd);
2874 vt_resume_handler(void *priv)
2876 struct vt_device *vd;
2879 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2880 vd->vd_driver->vd_resume(vd);
2881 vd->vd_flags &= ~VDF_SUSPENDED;
2885 vt_allocate(const struct vt_driver *drv, void *softc)
2888 if (!vty_enabled(VTY_VT))
2891 if (main_vd->vd_driver == NULL) {
2892 main_vd->vd_driver = drv;
2893 printf("VT: initialize with new VT driver \"%s\".\n",
2897 * Check if have rights to replace current driver. For example:
2898 * it is bad idea to replace KMS driver with generic VGA one.
2900 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2901 printf("VT: Driver priority %d too low. Current %d\n ",
2902 drv->vd_priority, main_vd->vd_driver->vd_priority);
2905 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2906 main_vd->vd_driver->vd_name, drv->vd_name);
2909 vt_replace_backend(drv, softc);
2913 vt_deallocate(const struct vt_driver *drv, void *softc)
2916 if (!vty_enabled(VTY_VT))
2919 if (main_vd->vd_prev_driver == NULL ||
2920 main_vd->vd_driver != drv ||
2921 main_vd->vd_softc != softc)
2924 printf("VT: Switching back from \"%s\" to \"%s\".\n",
2925 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
2927 vt_replace_backend(NULL, NULL);
2931 vt_suspend(struct vt_device *vd)
2935 if (vt_suspendswitch == 0)
2937 /* Save current window. */
2938 vd->vd_savedwindow = vd->vd_curwindow;
2939 /* Ask holding process to free window and switch to console window */
2940 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2942 /* Wait for the window switch to complete. */
2945 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2946 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2951 vt_resume(struct vt_device *vd)
2954 if (vt_suspendswitch == 0)
2956 /* Switch back to saved window, if any */
2957 vt_proc_window_switch(vd->vd_savedwindow);
2958 vd->vd_savedwindow = NULL;