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>
45 #include <sys/kernel.h>
46 #include <sys/linker.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/power.h>
53 #include <sys/random.h>
54 #include <sys/reboot.h>
55 #include <sys/systm.h>
56 #include <sys/terminal.h>
58 #include <dev/kbd/kbdreg.h>
59 #include <dev/vt/vt.h>
61 #if defined(__i386__) || defined(__amd64__)
62 #include <machine/psl.h>
63 #include <machine/frame.h>
66 static int vtterm_cngrab_noswitch(struct vt_device *, struct vt_window *);
67 static int vtterm_cnungrab_noswitch(struct vt_device *, struct vt_window *);
69 static tc_bell_t vtterm_bell;
70 static tc_cursor_t vtterm_cursor;
71 static tc_putchar_t vtterm_putchar;
72 static tc_fill_t vtterm_fill;
73 static tc_copy_t vtterm_copy;
74 static tc_pre_input_t vtterm_pre_input;
75 static tc_post_input_t vtterm_post_input;
76 static tc_param_t vtterm_param;
77 static tc_done_t vtterm_done;
79 static tc_cnprobe_t vtterm_cnprobe;
80 static tc_cngetc_t vtterm_cngetc;
82 static tc_cngrab_t vtterm_cngrab;
83 static tc_cnungrab_t vtterm_cnungrab;
85 static tc_opened_t vtterm_opened;
86 static tc_ioctl_t vtterm_ioctl;
87 static tc_mmap_t vtterm_mmap;
89 const struct terminal_class vt_termclass = {
90 .tc_bell = vtterm_bell,
91 .tc_cursor = vtterm_cursor,
92 .tc_putchar = vtterm_putchar,
93 .tc_fill = vtterm_fill,
94 .tc_copy = vtterm_copy,
95 .tc_pre_input = vtterm_pre_input,
96 .tc_post_input = vtterm_post_input,
97 .tc_param = vtterm_param,
98 .tc_done = vtterm_done,
100 .tc_cnprobe = vtterm_cnprobe,
101 .tc_cngetc = vtterm_cngetc,
103 .tc_cngrab = vtterm_cngrab,
104 .tc_cnungrab = vtterm_cnungrab,
106 .tc_opened = vtterm_opened,
107 .tc_ioctl = vtterm_ioctl,
108 .tc_mmap = vtterm_mmap,
112 * Use a constant timer of 25 Hz to redraw the screen.
114 * XXX: In theory we should only fire up the timer when there is really
115 * activity. Unfortunately we cannot always start timers. We really
116 * don't want to process kernel messages synchronously, because it
117 * really slows down the system.
119 #define VT_TIMERFREQ 25
121 /* Bell pitch/duration. */
122 #define VT_BELLDURATION ((5 * hz + 99) / 100)
123 #define VT_BELLPITCH 800
125 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
128 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
130 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
131 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
132 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
133 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
134 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
136 /* Allow to disable some keyboard combinations. */
137 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
138 "See kbdmap(5) to configure.");
139 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
140 "See kbdmap(5) to configure.");
141 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
142 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
143 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
144 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
145 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
146 "See kbdmap(5) to configure.");
148 /* Used internally, not a tunable. */
149 int vt_draw_logo_cpus;
150 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
151 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
152 "(0 = do not override)");
153 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
154 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
155 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
157 static unsigned int vt_unit = 0;
158 static MALLOC_DEFINE(M_VT, "vt", "vt device");
159 struct vt_device *main_vd = &vt_consdev;
162 extern unsigned int vt_logo_width;
163 extern unsigned int vt_logo_height;
164 extern unsigned int vt_logo_depth;
165 extern unsigned char vt_logo_image[];
167 #define vtterm_draw_cpu_logos(...) do {} while (0)
168 const unsigned int vt_logo_sprite_height;
172 * Console font. vt_font_loader will be filled with font data passed
173 * by loader. If there is no font passed by boot loader, we use built in
176 extern struct vt_font vt_font_default;
177 static struct vt_font vt_font_loader;
178 static struct vt_font *vt_font_assigned = &vt_font_default;
180 #ifndef SC_NO_CUTPASTE
181 extern struct vt_mouse_cursor vt_default_mouse_pointer;
184 static int signal_vt_rel(struct vt_window *);
185 static int signal_vt_acq(struct vt_window *);
186 static int finish_vt_rel(struct vt_window *, int, int *);
187 static int finish_vt_acq(struct vt_window *);
188 static int vt_window_switch(struct vt_window *);
189 static int vt_late_window_switch(struct vt_window *);
190 static int vt_proc_alive(struct vt_window *);
191 static void vt_resize(struct vt_device *);
192 static void vt_update_static(void *);
193 #ifndef SC_NO_CUTPASTE
194 static void vt_mouse_paste(void);
196 static void vt_suspend_handler(void *priv);
197 static void vt_resume_handler(void *priv);
199 SET_DECLARE(vt_drv_set, struct vt_driver);
201 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT))
202 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH))
204 struct terminal vt_consterm;
205 static struct vt_window vt_conswindow;
206 #ifndef SC_NO_CONSDRAWN
207 static term_char_t vt_consdrawn[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
208 static term_color_t vt_consdrawnfg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
209 static term_color_t vt_consdrawnbg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)];
211 struct vt_device vt_consdev = {
214 .vd_prev_driver = NULL,
215 .vd_prev_softc = NULL,
216 .vd_flags = VDF_INVALID,
217 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
218 .vd_curwindow = &vt_conswindow,
221 #ifndef SC_NO_CUTPASTE
227 .vd_mcursor = &vt_default_mouse_pointer,
228 .vd_mcursor_fg = TC_WHITE,
229 .vd_mcursor_bg = TC_BLACK,
232 #ifndef SC_NO_CONSDRAWN
233 .vd_drawn = vt_consdrawn,
234 .vd_drawnfg = vt_consdrawnfg,
235 .vd_drawnbg = vt_consdrawnbg,
238 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
239 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
240 static struct vt_window vt_conswindow = {
241 .vw_number = VT_CONSWINDOW,
242 .vw_flags = VWF_CONSOLE,
244 .vb_buffer = &vt_constextbuf[0],
245 .vb_rows = &vt_constextbufrows[0],
246 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
249 .vb_flags = VBF_STATIC,
250 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
251 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
257 .vw_device = &vt_consdev,
258 .vw_terminal = &vt_consterm,
259 .vw_kbdmode = K_XLATE,
262 struct terminal vt_consterm = {
263 .tm_class = &vt_termclass,
264 .tm_softc = &vt_conswindow,
267 static struct consdev vt_consterm_consdev = {
268 .cn_ops = &termcn_cnops,
269 .cn_arg = &vt_consterm,
273 /* Add to set of consoles. */
274 DATA_SET(cons_set, vt_consterm_consdev);
277 * Right after kmem is done to allow early drivers to use locking and allocate
280 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
282 /* Delay until all devices attached, to not waste time. */
283 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
286 /* Initialize locks/mem depended members. */
288 vt_update_static(void *dummy)
291 if (!vty_enabled(VTY_VT))
293 if (main_vd->vd_driver != NULL)
294 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
295 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
296 main_vd->vd_width, main_vd->vd_height);
298 printf("VT: init without driver.\n");
300 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
301 cv_init(&main_vd->vd_winswitch, "vtwswt");
305 vt_schedule_flush(struct vt_device *vd, int ms)
309 /* Default to initial value. */
310 ms = 1000 / VT_TIMERFREQ;
312 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
316 vt_resume_flush_timer(struct vt_window *vw, int ms)
318 struct vt_device *vd = vw->vw_device;
320 if (vd->vd_curwindow != vw)
323 if (!(vd->vd_flags & VDF_ASYNC) ||
324 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
327 vt_schedule_flush(vd, ms);
331 vt_suspend_flush_timer(struct vt_device *vd)
334 * As long as this function is called locked, callout_stop()
335 * has the same effect like callout_drain() with regard to
336 * preventing the callback function from executing.
338 VT_LOCK_ASSERT(vd, MA_OWNED);
340 if (!(vd->vd_flags & VDF_ASYNC) ||
341 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
344 callout_stop(&vd->vd_timer);
348 vt_switch_timer(void *arg)
351 (void)vt_late_window_switch((struct vt_window *)arg);
355 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
360 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
366 vw->vw_kbdmode = mode;
372 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
376 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
384 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
389 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
395 vw->vw_kbdstate &= ~LOCK_MASK;
396 vw->vw_kbdstate |= state & LOCK_MASK;
402 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
406 state = vw->vw_kbdstate & LOCK_MASK;
407 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
415 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
420 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
426 vw->vw_kbdstate &= ~LED_MASK;
427 vw->vw_kbdstate |= leds & LED_MASK;
433 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
437 leds = vw->vw_kbdstate & LED_MASK;
438 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
446 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
449 DPRINTF(40, "%s\n", __func__);
450 curvw->vw_switch_to = vw;
451 /* Set timer to allow switch in case when process hang. */
452 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
453 vt_switch_timer, (void *)vw);
454 /* Notify process about vt switch attempt. */
455 DPRINTF(30, "%s: Notify process.\n", __func__);
456 signal_vt_rel(curvw);
462 vt_window_postswitch(struct vt_window *vw)
469 /* vt_late_window_switch will do VT switching for regular case. */
471 vt_late_window_switch(struct vt_window *vw)
473 struct vt_window *curvw;
476 callout_stop(&vw->vw_proc_dead_timer);
478 ret = vt_window_switch(vw);
481 * If the switch hasn't happened, then return the VT
482 * to the current owner, if any.
484 curvw = vw->vw_device->vd_curwindow;
485 if (curvw->vw_smode.mode == VT_PROCESS)
486 (void)vt_window_postswitch(curvw);
490 /* Notify owner process about terminal availability. */
491 if (vw->vw_smode.mode == VT_PROCESS) {
492 ret = vt_window_postswitch(vw);
499 vt_proc_window_switch(struct vt_window *vw)
501 struct vt_window *curvw;
502 struct vt_device *vd;
505 /* Prevent switching to NULL */
507 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
511 curvw = vd->vd_curwindow;
513 /* Check if virtual terminal is locked */
514 if (curvw->vw_flags & VWF_VTYLOCK)
517 /* Check if switch already in progress */
518 if (curvw->vw_flags & VWF_SWWAIT_REL) {
519 /* Check if switching to same window */
520 if (curvw->vw_switch_to == vw) {
521 DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
522 return (0); /* success */
524 DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
528 /* Avoid switching to already selected window */
530 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
531 return (0); /* success */
535 * Early check for an attempt to switch to a non-functional VT.
536 * The same check is done in vt_window_switch(), but it's better
537 * to fail as early as possible to avoid needless pre-switch
541 if ((vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)) == 0) {
547 /* Ask current process permission to switch away. */
548 if (curvw->vw_smode.mode == VT_PROCESS) {
549 DPRINTF(30, "%s: VT_PROCESS ", __func__);
550 if (vt_proc_alive(curvw) == FALSE) {
551 DPRINTF(30, "Dead. Cleaning.");
554 DPRINTF(30, "%s: Signaling process.\n", __func__);
555 /* Alive, try to ask him. */
556 ret = vt_window_preswitch(vw, curvw);
557 /* Wait for process answer or timeout. */
563 ret = vt_late_window_switch(vw);
567 /* Switch window ignoring process locking. */
569 vt_window_switch(struct vt_window *vw)
571 struct vt_device *vd = vw->vw_device;
572 struct vt_window *curvw = vd->vd_curwindow;
577 * When grabbing the console for the debugger, avoid
578 * locks as that can result in deadlock. While this
579 * could use try locks, that wouldn't really make a
580 * difference as there are sufficient barriers in
581 * debugger entry/exit to be equivalent to
582 * successfully try-locking here.
586 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)))
589 vd->vd_curwindow = vw;
590 vd->vd_flags |= VDF_INVALID;
591 if (vd->vd_driver->vd_postswitch)
592 vd->vd_driver->vd_postswitch(vd);
602 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
607 vt_suspend_flush_timer(vd);
609 vd->vd_curwindow = vw;
610 vd->vd_flags |= VDF_INVALID;
611 cv_broadcast(&vd->vd_winswitch);
614 if (vd->vd_driver->vd_postswitch)
615 vd->vd_driver->vd_postswitch(vd);
617 vt_resume_flush_timer(vw, 0);
619 /* Restore per-window keyboard mode. */
621 if ((kbd = vd->vd_keyboard) != NULL) {
622 if (curvw->vw_kbdmode == K_XLATE)
623 vt_save_kbd_state(curvw, kbd);
625 vt_update_kbd_mode(vw, kbd);
626 vt_update_kbd_state(vw, kbd);
629 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
635 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
638 size->tp_row = vd->vd_height;
639 if (vt_draw_logo_cpus)
640 size->tp_row -= vt_logo_sprite_height;
641 size->tp_col = vd->vd_width;
643 size->tp_row /= vf->vf_height;
644 size->tp_col /= vf->vf_width;
649 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
652 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
653 if (vt_draw_logo_cpus)
654 rect->tr_begin.tp_row = vt_logo_sprite_height;
656 rect->tr_end.tp_row = vd->vd_height;
657 rect->tr_end.tp_col = vd->vd_width;
660 rect->tr_begin.tp_row =
661 howmany(rect->tr_begin.tp_row, vf->vf_height);
663 rect->tr_end.tp_row /= vf->vf_height;
664 rect->tr_end.tp_col /= vf->vf_width;
669 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
672 size->ws_ypixel = vd->vd_height;
673 if (vt_draw_logo_cpus)
674 size->ws_ypixel -= vt_logo_sprite_height;
675 size->ws_row = size->ws_ypixel;
676 size->ws_col = size->ws_xpixel = vd->vd_width;
678 size->ws_row /= vf->vf_height;
679 size->ws_col /= vf->vf_width;
684 vt_compute_drawable_area(struct vt_window *vw)
686 struct vt_device *vd;
692 if (vw->vw_font == NULL) {
693 vw->vw_draw_area.tr_begin.tp_col = 0;
694 vw->vw_draw_area.tr_begin.tp_row = 0;
695 if (vt_draw_logo_cpus)
696 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
697 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
698 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
705 * Compute the drawable area, so that the text is centered on
709 height = vd->vd_height;
710 if (vt_draw_logo_cpus)
711 height -= vt_logo_sprite_height;
712 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
713 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
714 if (vt_draw_logo_cpus)
715 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
716 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
717 rounddown(vd->vd_width, vf->vf_width);
718 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
719 rounddown(height, vf->vf_height);
723 vt_scroll(struct vt_window *vw, int offset, int whence)
728 if ((vw->vw_flags & VWF_SCROLL) == 0)
731 vt_termsize(vw->vw_device, vw->vw_font, &size);
733 diff = vthistory_seek(&vw->vw_buf, offset, whence);
735 vw->vw_device->vd_flags |= VDF_INVALID;
736 vt_resume_flush_timer(vw, 0);
740 vt_machine_kbdevent(struct vt_device *vd, int c)
744 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
746 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
747 #if VT_ALT_TO_ESC_HACK
749 * There's an unfortunate conflict between SPCLKEY|DBG
750 * and VT_ALT_TO_ESC_HACK. Just assume they didn't mean
751 * it if we got to here.
753 vd->vd_kbstate &= ~ALKED;
757 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
759 shutdown_nice(RB_HALT);
761 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
762 #ifndef SC_NO_CUTPASTE
763 /* Insert text from cut-paste buffer. */
767 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
769 shutdown_nice(RB_HALT|RB_POWEROFF);
771 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
773 * Request to immediate panic if sysctl
774 * kern.vt.enable_panic_key allow it.
777 panic("Forced by the panic key");
779 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
781 shutdown_nice(RB_AUTOBOOT);
783 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
784 /* Force activatation/deactivation of the screen saver. */
787 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
788 /* Put machine into Stand-By mode. */
789 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
791 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
792 /* Suspend machine. */
793 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
801 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
803 struct vt_device *vd;
807 /* Only special keys handled in ScrollLock mode */
808 if ((c & SPCLKEY) == 0)
814 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
815 vw = vd->vd_windows[c - F_SCR];
816 vt_proc_window_switch(vw);
824 /* Turn scrolling off. */
825 vt_scroll(vw, 0, VHS_END);
826 VTBUF_SLCK_DISABLE(&vw->vw_buf);
827 vw->vw_flags &= ~VWF_SCROLL;
830 case FKEY | F(49): /* Home key. */
831 vt_scroll(vw, 0, VHS_SET);
833 case FKEY | F(50): /* Arrow up. */
834 vt_scroll(vw, -1, VHS_CUR);
836 case FKEY | F(51): /* Page up. */
837 vt_termsize(vd, vw->vw_font, &size);
838 vt_scroll(vw, -size.tp_row, VHS_CUR);
840 case FKEY | F(57): /* End key. */
841 vt_scroll(vw, 0, VHS_END);
843 case FKEY | F(58): /* Arrow down. */
844 vt_scroll(vw, 1, VHS_CUR);
846 case FKEY | F(59): /* Page down. */
847 vt_termsize(vd, vw->vw_font, &size);
848 vt_scroll(vw, size.tp_row, VHS_CUR);
857 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
859 struct vt_window *vw = vd->vd_curwindow;
861 random_harvest_queue(&c, sizeof(c), RANDOM_KEYBOARD);
862 #if VT_ALT_TO_ESC_HACK
864 switch (c & ~RELKEY) {
865 case (SPCLKEY | RALT):
866 if (vt_enable_altgr != 0)
868 case (SPCLKEY | LALT):
869 vd->vd_kbstate &= ~ALKED;
871 /* Other keys ignored for RELKEY event. */
874 switch (c & ~RELKEY) {
875 case (SPCLKEY | RALT):
876 if (vt_enable_altgr != 0)
878 case (SPCLKEY | LALT):
879 vd->vd_kbstate |= ALKED;
884 /* Other keys ignored for RELKEY event. */
888 if (vt_machine_kbdevent(vd, c))
891 if (vw->vw_flags & VWF_SCROLL) {
892 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
893 /* Scroll mode keys handled, nothing to do more. */
900 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
901 vw = vd->vd_windows[c - F_SCR];
902 vt_proc_window_switch(vw);
908 /* Switch to next VT. */
909 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
910 vw = vd->vd_windows[c];
911 vt_proc_window_switch(vw);
914 /* Switch to previous VT. */
915 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
916 vw = vd->vd_windows[c];
917 vt_proc_window_switch(vw);
920 vt_save_kbd_state(vw, kbd);
922 if (vw->vw_kbdstate & SLKED) {
923 /* Turn scrolling on. */
924 vw->vw_flags |= VWF_SCROLL;
925 VTBUF_SLCK_ENABLE(&vw->vw_buf);
927 /* Turn scrolling off. */
928 vw->vw_flags &= ~VWF_SCROLL;
929 VTBUF_SLCK_DISABLE(&vw->vw_buf);
930 vt_scroll(vw, 0, VHS_END);
935 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
936 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
937 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
938 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
939 /* F1 through F12 keys. */
940 terminal_input_special(vw->vw_terminal,
941 TKEY_F1 + c - (FKEY | F(1)));
943 case FKEY | F(49): /* Home key. */
944 terminal_input_special(vw->vw_terminal, TKEY_HOME);
946 case FKEY | F(50): /* Arrow up. */
947 terminal_input_special(vw->vw_terminal, TKEY_UP);
949 case FKEY | F(51): /* Page up. */
950 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
952 case FKEY | F(53): /* Arrow left. */
953 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
955 case FKEY | F(55): /* Arrow right. */
956 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
958 case FKEY | F(57): /* End key. */
959 terminal_input_special(vw->vw_terminal, TKEY_END);
961 case FKEY | F(58): /* Arrow down. */
962 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
964 case FKEY | F(59): /* Page down. */
965 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
967 case FKEY | F(60): /* Insert key. */
968 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
970 case FKEY | F(61): /* Delete key. */
971 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
974 } else if (KEYFLAGS(c) == 0) {
975 /* Don't do UTF-8 conversion when doing raw mode. */
976 if (vw->vw_kbdmode == K_XLATE) {
977 #if VT_ALT_TO_ESC_HACK
978 if (vd->vd_kbstate & ALKED) {
980 * Prepend ESC sequence if one of ALT keys down.
982 terminal_input_char(vw->vw_terminal, 0x1b);
986 kdb_alt_break(c, &vd->vd_altbrk);
988 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
990 terminal_input_raw(vw->vw_terminal, c);
996 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
998 struct vt_device *vd = arg;
1002 case KBDIO_KEYINPUT:
1004 case KBDIO_UNLOADING:
1006 vd->vd_keyboard = NULL;
1007 kbd_release(kbd, (void *)vd);
1014 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
1015 vt_processkey(kbd, vd, c);
1021 vt_allocate_keyboard(struct vt_device *vd)
1023 int grabbed, i, idx0, idx;
1028 * If vt_upgrade() happens while the console is grabbed, we are
1029 * potentially going to switch keyboard devices while the keyboard is in
1030 * use. Unwind the grabbing of the current keyboard first, then we will
1031 * re-grab the new keyboard below, before we return.
1033 if (vd->vd_curwindow == &vt_conswindow) {
1034 grabbed = vd->vd_curwindow->vw_grabbed;
1035 for (i = 0; i < grabbed; ++i)
1036 vtterm_cnungrab_noswitch(vd, vd->vd_curwindow);
1039 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
1041 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
1042 k0 = kbd_get_keyboard(idx0);
1044 for (idx = kbd_find_keyboard2("*", -1, 0);
1046 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
1047 k = kbd_get_keyboard(idx);
1049 if (idx == idx0 || KBD_IS_BUSY(k))
1052 bzero(&ki, sizeof(ki));
1053 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
1054 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
1055 ki.kb_unit = k->kb_unit;
1057 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
1060 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
1061 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
1063 DPRINTF(10, "%s: No keyboard found.\n", __func__);
1066 k0 = kbd_get_keyboard(idx0);
1068 vd->vd_keyboard = k0;
1069 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__,
1070 vd->vd_keyboard->kb_index);
1072 if (vd->vd_curwindow == &vt_conswindow) {
1073 for (i = 0; i < grabbed; ++i)
1074 vtterm_cngrab_noswitch(vd, vd->vd_curwindow);
1081 vtterm_bell(struct terminal *tm)
1083 struct vt_window *vw = tm->tm_softc;
1084 struct vt_device *vd = vw->vw_device;
1086 if (!vt_enable_bell)
1089 if (vd->vd_flags & VDF_QUIET_BELL)
1092 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
1096 vtterm_beep(struct terminal *tm, u_int param)
1100 if (!vt_enable_bell)
1103 if ((param == 0) || ((param & 0xffff) == 0)) {
1108 period = ((param >> 16) & 0xffff) * hz / 1000;
1109 freq = 1193182 / (param & 0xffff);
1111 sysbeep(freq, period);
1115 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1117 struct vt_window *vw = tm->tm_softc;
1119 vtbuf_cursor_position(&vw->vw_buf, p);
1123 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1125 struct vt_window *vw = tm->tm_softc;
1127 vtbuf_putchar(&vw->vw_buf, p, c);
1131 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1133 struct vt_window *vw = tm->tm_softc;
1135 vtbuf_fill(&vw->vw_buf, r, c);
1139 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1140 const term_pos_t *p)
1142 struct vt_window *vw = tm->tm_softc;
1144 vtbuf_copy(&vw->vw_buf, r, p);
1148 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1150 struct vt_window *vw = tm->tm_softc;
1153 case TP_SETLOCALCURSOR:
1155 * 0 means normal (usually block), 1 means hidden, and
1156 * 2 means blinking (always block) for compatibility with
1157 * syscons. We don't support any changes except hiding,
1158 * so must map 2 to 0.
1160 arg = (arg == 1) ? 0 : 1;
1163 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1164 vt_resume_flush_timer(vw, 0);
1167 vw->vw_mouse_level = arg;
1173 vt_determine_colors(term_char_t c, int cursor,
1174 term_color_t *fg, term_color_t *bg)
1181 *fg = TCHAR_FGCOLOR(c);
1182 if (TCHAR_FORMAT(c) & TF_BOLD)
1183 *fg = TCOLOR_LIGHT(*fg);
1184 *bg = TCHAR_BGCOLOR(c);
1185 if (TCHAR_FORMAT(c) & TF_BLINK)
1186 *bg = TCOLOR_LIGHT(*bg);
1188 if (TCHAR_FORMAT(c) & TF_REVERSE)
1200 #ifndef SC_NO_CUTPASTE
1202 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1204 unsigned int mx, my;
1207 * We use the cursor position saved during the current refresh,
1208 * in case the cursor moved since.
1210 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1211 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1213 if (mx >= area->tr_end.tp_col ||
1214 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1215 my >= area->tr_end.tp_row ||
1216 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1222 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked)
1225 struct vt_window *vw;
1229 vw = vd->vd_curwindow;
1232 x = vd->vd_mx_drawn;
1233 y = vd->vd_my_drawn;
1236 area.tr_begin.tp_col = x / vf->vf_width;
1237 area.tr_begin.tp_row = y / vf->vf_height;
1238 area.tr_end.tp_col =
1239 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1240 area.tr_end.tp_row =
1241 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1244 * No font loaded (ie. vt_vga operating in textmode).
1246 * FIXME: This fake area needs to be revisited once the
1247 * mouse cursor is supported in vt_vga's textmode.
1249 area.tr_begin.tp_col = x;
1250 area.tr_begin.tp_row = y;
1251 area.tr_end.tp_col = x + 2;
1252 area.tr_end.tp_row = y + 2;
1256 vtbuf_lock(&vw->vw_buf);
1257 if (vd->vd_driver->vd_invalidate_text)
1258 vd->vd_driver->vd_invalidate_text(vd, &area);
1259 vtbuf_dirty(&vw->vw_buf, &area);
1261 vtbuf_unlock(&vw->vw_buf);
1266 vt_set_border(struct vt_device *vd, const term_rect_t *area,
1269 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
1271 if (drawrect == NULL)
1275 if (area->tr_begin.tp_row > 0)
1276 drawrect(vd, 0, 0, vd->vd_width - 1,
1277 area->tr_begin.tp_row - 1, 1, c);
1280 if (area->tr_begin.tp_col > 0)
1281 drawrect(vd, 0, area->tr_begin.tp_row,
1282 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
1285 if (area->tr_end.tp_col < vd->vd_width)
1286 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
1287 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
1290 if (area->tr_end.tp_row < vd->vd_height)
1291 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
1292 vd->vd_height - 1, 1, c);
1296 vt_flush(struct vt_device *vd)
1298 struct vt_window *vw;
1301 #ifndef SC_NO_CUTPASTE
1302 int cursor_was_shown, cursor_moved;
1305 vw = vd->vd_curwindow;
1309 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1313 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1316 vtbuf_lock(&vw->vw_buf);
1318 #ifndef SC_NO_CUTPASTE
1319 cursor_was_shown = vd->vd_mshown;
1320 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1321 vd->vd_my != vd->vd_my_drawn);
1323 /* Check if the cursor should be displayed or not. */
1324 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1325 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1326 !kdb_active && !KERNEL_PANICKED()) { /* DDB inactive. */
1333 * If the cursor changed display state or moved, we must mark
1334 * the old position as dirty, so that it's erased.
1336 if (cursor_was_shown != vd->vd_mshown ||
1337 (vd->vd_mshown && cursor_moved))
1338 vt_mark_mouse_position_as_dirty(vd, true);
1341 * Save position of the mouse cursor. It's used by backends to
1342 * know where to draw the cursor and during the next refresh to
1343 * erase the previous position.
1345 vd->vd_mx_drawn = vd->vd_mx;
1346 vd->vd_my_drawn = vd->vd_my;
1349 * If the cursor is displayed and has moved since last refresh,
1350 * mark the new position as dirty.
1352 if (vd->vd_mshown && cursor_moved)
1353 vt_mark_mouse_position_as_dirty(vd, true);
1356 vtbuf_undirty(&vw->vw_buf, &tarea);
1358 /* Force a full redraw when the screen contents might be invalid. */
1359 if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) {
1360 const teken_attr_t *a;
1362 vd->vd_flags &= ~VDF_INVALID;
1364 a = teken_get_curattr(&vw->vw_terminal->tm_emulator);
1365 vt_set_border(vd, &vw->vw_draw_area, a->ta_bgcolor);
1366 vt_termrect(vd, vf, &tarea);
1367 if (vd->vd_driver->vd_invalidate_text)
1368 vd->vd_driver->vd_invalidate_text(vd, &tarea);
1369 if (vt_draw_logo_cpus)
1370 vtterm_draw_cpu_logos(vd);
1373 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1374 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1375 vtbuf_unlock(&vw->vw_buf);
1379 vtbuf_unlock(&vw->vw_buf);
1386 struct vt_device *vd;
1390 /* Update screen if required. */
1391 changed = vt_flush(vd);
1393 /* Schedule for next update. */
1395 vt_schedule_flush(vd, 0);
1397 vd->vd_timer_armed = 0;
1401 vtterm_pre_input(struct terminal *tm)
1403 struct vt_window *vw = tm->tm_softc;
1405 vtbuf_lock(&vw->vw_buf);
1409 vtterm_post_input(struct terminal *tm)
1411 struct vt_window *vw = tm->tm_softc;
1413 vtbuf_unlock(&vw->vw_buf);
1414 vt_resume_flush_timer(vw, 0);
1418 vtterm_done(struct terminal *tm)
1420 struct vt_window *vw = tm->tm_softc;
1421 struct vt_device *vd = vw->vw_device;
1423 if (kdb_active || KERNEL_PANICKED()) {
1424 /* Switch to the debugger. */
1425 if (vd->vd_curwindow != vw) {
1426 vd->vd_curwindow = vw;
1427 vd->vd_flags |= VDF_INVALID;
1428 if (vd->vd_driver->vd_postswitch)
1429 vd->vd_driver->vd_postswitch(vd);
1431 vd->vd_flags &= ~VDF_SPLASH;
1433 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1440 vtterm_splash(struct vt_device *vd)
1442 vt_axis_t top, left;
1444 /* Display a nice boot splash. */
1445 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1446 top = (vd->vd_height - vt_logo_height) / 2;
1447 left = (vd->vd_width - vt_logo_width) / 2;
1448 switch (vt_logo_depth) {
1450 /* XXX: Unhardcode colors! */
1451 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1452 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1453 left, top, TC_WHITE, TC_BLACK);
1455 vd->vd_flags |= VDF_SPLASH;
1460 static struct vt_font *
1461 parse_font_info_static(struct font_info *fi)
1463 struct vt_font *vfp;
1470 ptr = (uintptr_t)fi;
1472 * Compute and verify checksum. The total sum of all the fields
1475 checksum = fi->fi_width;
1476 checksum += fi->fi_height;
1477 checksum += fi->fi_bitmap_size;
1478 for (unsigned i = 0; i < VFNT_MAPS; i++)
1479 checksum += fi->fi_map_count[i];
1481 if (checksum + fi->fi_checksum != 0)
1484 ptr += sizeof(struct font_info);
1485 ptr = roundup2(ptr, 8);
1487 vfp = &vt_font_loader;
1488 vfp->vf_height = fi->fi_height;
1489 vfp->vf_width = fi->fi_width;
1490 for (unsigned i = 0; i < VFNT_MAPS; i++) {
1491 if (fi->fi_map_count[i] == 0)
1493 vfp->vf_map_count[i] = fi->fi_map_count[i];
1494 vfp->vf_map[i] = (vfnt_map_t *)ptr;
1495 ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t));
1496 ptr = roundup2(ptr, 8);
1498 vfp->vf_bytes = (uint8_t *)ptr;
1502 static struct vt_font *
1503 parse_font_info(struct font_info *fi)
1505 struct vt_font *vfp;
1513 ptr = (uintptr_t)fi;
1515 * Compute and verify checksum. The total sum of all the fields
1518 checksum = fi->fi_width;
1519 checksum += fi->fi_height;
1520 checksum += fi->fi_bitmap_size;
1521 for (unsigned i = 0; i < VFNT_MAPS; i++)
1522 checksum += fi->fi_map_count[i];
1524 if (checksum + fi->fi_checksum != 0)
1527 ptr += sizeof(struct font_info);
1528 ptr = roundup2(ptr, 8);
1530 vfp = &vt_font_loader;
1531 vfp->vf_height = fi->fi_height;
1532 vfp->vf_width = fi->fi_width;
1533 for (unsigned i = 0; i < VFNT_MAPS; i++) {
1534 if (fi->fi_map_count[i] == 0)
1536 vfp->vf_map_count[i] = fi->fi_map_count[i];
1537 size = fi->fi_map_count[i] * sizeof(vfnt_map_t);
1538 vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO);
1539 bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size);
1541 ptr = roundup2(ptr, 8);
1543 vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO);
1544 bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size);
1549 vt_init_font(void *arg)
1552 struct font_info *fi;
1553 struct vt_font *font;
1555 kmdp = preload_search_by_type("elf kernel");
1557 kmdp = preload_search_by_type("elf64 kernel");
1558 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *);
1560 font = parse_font_info(fi);
1562 vt_font_assigned = font;
1565 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev);
1568 vt_init_font_static(void)
1571 struct font_info *fi;
1572 struct vt_font *font;
1574 kmdp = preload_search_by_type("elf kernel");
1576 kmdp = preload_search_by_type("elf64 kernel");
1577 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *);
1579 font = parse_font_info_static(fi);
1581 vt_font_assigned = font;
1585 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1587 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1588 struct vt_window *vw = tm->tm_softc;
1589 struct vt_device *vd = vw->vw_device;
1591 const term_attr_t *a;
1593 if (!vty_enabled(VTY_VT))
1596 if (vd->vd_flags & VDF_INITIALIZED)
1597 /* Initialization already done. */
1600 SET_FOREACH(vtdlist, vt_drv_set) {
1602 if (vtd->vd_probe == NULL)
1604 if (vtd->vd_probe(vd) == CN_DEAD)
1606 if ((vtdbest == NULL) ||
1607 (vtd->vd_priority > vtdbest->vd_priority))
1610 if (vtdbest == NULL) {
1611 cp->cn_pri = CN_DEAD;
1612 vd->vd_flags |= VDF_DEAD;
1614 vd->vd_driver = vtdbest;
1615 cp->cn_pri = vd->vd_driver->vd_init(vd);
1618 /* Check if driver's vt_init return CN_DEAD. */
1619 if (cp->cn_pri == CN_DEAD) {
1620 vd->vd_flags |= VDF_DEAD;
1623 /* Initialize any early-boot keyboard drivers */
1624 kbd_configure(KB_CONF_PROBE_ONLY);
1626 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1627 vd->vd_windows[VT_CONSWINDOW] = vw;
1628 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1630 vt_init_font_static();
1632 /* Attach default font if not in TEXTMODE. */
1633 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1634 vw->vw_font = vtfont_ref(vt_font_assigned);
1635 vt_compute_drawable_area(vw);
1639 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1640 * that we have the real viewable size, fix it in the static
1643 if (vd->vd_width != 0 && vd->vd_height != 0)
1644 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1646 /* We need to access terminal attributes from vtbuf */
1647 vw->vw_buf.vb_terminal = tm;
1648 vtbuf_init_early(&vw->vw_buf);
1649 vt_winsize(vd, vw->vw_font, &wsz);
1650 a = teken_get_curattr(&tm->tm_emulator);
1651 terminal_set_winsize_blank(tm, &wsz, 1, a);
1653 if (vtdbest != NULL) {
1658 vd->vd_flags |= VDF_INITIALIZED;
1663 vtterm_cngetc(struct terminal *tm)
1665 struct vt_window *vw = tm->tm_softc;
1666 struct vt_device *vd = vw->vw_device;
1670 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1671 return (*vw->vw_kbdsq++);
1673 /* Make sure the splash screen is not there. */
1674 if (vd->vd_flags & VDF_SPLASH) {
1676 vd->vd_flags &= ~VDF_SPLASH;
1677 /* Mark screen as invalid to force update */
1678 vd->vd_flags |= VDF_INVALID;
1682 /* Stripped down keyboard handler. */
1683 if ((kbd = vd->vd_keyboard) == NULL)
1686 /* Force keyboard input mode to K_XLATE */
1687 vw->vw_kbdmode = K_XLATE;
1688 vt_update_kbd_mode(vw, kbd);
1690 /* Switch the keyboard to polling to make it work here. */
1691 kbdd_poll(kbd, TRUE);
1692 c = kbdd_read_char(kbd, 0);
1693 kbdd_poll(kbd, FALSE);
1697 if (vw->vw_flags & VWF_SCROLL) {
1698 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1703 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1707 vt_save_kbd_state(vw, kbd);
1708 if (vw->vw_kbdstate & SLKED) {
1709 /* Turn scrolling on. */
1710 vw->vw_flags |= VWF_SCROLL;
1711 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1713 /* Turn scrolling off. */
1714 vt_scroll(vw, 0, VHS_END);
1715 vw->vw_flags &= ~VWF_SCROLL;
1716 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1719 /* XXX: KDB can handle history. */
1720 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1721 vw->vw_kbdsq = "\x1b[A";
1723 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1724 vw->vw_kbdsq = "\x1b[B";
1726 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1727 vw->vw_kbdsq = "\x1b[C";
1729 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1730 vw->vw_kbdsq = "\x1b[D";
1734 /* Force refresh to make scrollback work. */
1736 } else if (KEYFLAGS(c) == 0) {
1737 return (KEYCHAR(c));
1740 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1741 return (*vw->vw_kbdsq++);
1747 * These two do most of what we want to do in vtterm_cnungrab, but without
1748 * actually switching windows. This is necessary for, e.g.,
1749 * vt_allocate_keyboard() to get the current keyboard into the state it needs to
1750 * be in without damaging the device's window state.
1752 * Both return the current grab count, though it's only used in vtterm_cnungrab.
1755 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw)
1759 if (vw->vw_grabbed++ > 0)
1760 return (vw->vw_grabbed);
1762 if ((kbd = vd->vd_keyboard) == NULL)
1766 * Make sure the keyboard is accessible even when the kbd device
1767 * driver is disabled.
1771 /* We shall always use the keyboard in the XLATE mode here. */
1772 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1773 vw->vw_kbdmode = K_XLATE;
1774 vt_update_kbd_mode(vw, kbd);
1776 kbdd_poll(kbd, TRUE);
1781 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw)
1785 if (--vw->vw_grabbed > 0)
1786 return (vw->vw_grabbed);
1788 if ((kbd = vd->vd_keyboard) == NULL)
1791 kbdd_poll(kbd, FALSE);
1793 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1794 vt_update_kbd_mode(vw, kbd);
1800 vtterm_cngrab(struct terminal *tm)
1802 struct vt_device *vd;
1803 struct vt_window *vw;
1808 /* To be restored after we ungrab. */
1809 if (vd->vd_grabwindow == NULL)
1810 vd->vd_grabwindow = vd->vd_curwindow;
1813 vt_window_switch(vw);
1815 vtterm_cngrab_noswitch(vd, vw);
1819 vtterm_cnungrab(struct terminal *tm)
1821 struct vt_device *vd;
1822 struct vt_window *vw;
1827 MPASS(vd->vd_grabwindow != NULL);
1828 if (vtterm_cnungrab_noswitch(vd, vw) != 0)
1831 if (!cold && vd->vd_grabwindow != vw)
1832 vt_window_switch(vd->vd_grabwindow);
1834 vd->vd_grabwindow = NULL;
1838 vtterm_opened(struct terminal *tm, int opened)
1840 struct vt_window *vw = tm->tm_softc;
1841 struct vt_device *vd = vw->vw_device;
1844 vd->vd_flags &= ~VDF_SPLASH;
1846 vw->vw_flags |= VWF_OPENED;
1848 vw->vw_flags &= ~VWF_OPENED;
1849 /* TODO: finish ACQ/REL */
1855 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1857 struct vt_device *vd = vw->vw_device;
1858 struct terminal *tm = vw->vw_terminal;
1865 * Changing fonts is a little tricky. We must prevent
1866 * simultaneous access to the device, so we must stop
1867 * the display timer and the terminal from accessing.
1868 * We need to switch fonts and grow our screen buffer.
1870 * XXX: Right now the code uses terminal_mute() to
1871 * prevent data from reaching the console driver while
1872 * resizing the screen buffer. This isn't elegant...
1876 if (vw->vw_flags & VWF_BUSY) {
1877 /* Another process is changing the font. */
1881 vw->vw_flags |= VWF_BUSY;
1884 vt_termsize(vd, vf, &size);
1885 vt_winsize(vd, vf, &wsz);
1887 /* Grow the screen buffer and terminal. */
1888 terminal_mute(tm, 1);
1889 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1890 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1891 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1892 terminal_mute(tm, 0);
1894 /* Actually apply the font to the current window. */
1896 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1898 * In case vt_change_font called to update size we don't need
1899 * to update font link.
1901 vtfont_unref(vw->vw_font);
1902 vw->vw_font = vtfont_ref(vf);
1906 * Compute the drawable area and move the mouse cursor inside
1907 * it, in case the new area is smaller than the previous one.
1909 vt_compute_drawable_area(vw);
1910 vd->vd_mx = min(vd->vd_mx,
1911 vw->vw_draw_area.tr_end.tp_col -
1912 vw->vw_draw_area.tr_begin.tp_col - 1);
1913 vd->vd_my = min(vd->vd_my,
1914 vw->vw_draw_area.tr_end.tp_row -
1915 vw->vw_draw_area.tr_begin.tp_row - 1);
1917 /* Force a full redraw the next timer tick. */
1918 if (vd->vd_curwindow == vw) {
1919 vd->vd_flags |= VDF_INVALID;
1920 vt_resume_flush_timer(vw, 0);
1922 vw->vw_flags &= ~VWF_BUSY;
1928 vt_proc_alive(struct vt_window *vw)
1932 if (vw->vw_smode.mode != VT_PROCESS)
1936 if ((p = pfind(vw->vw_pid)) != NULL)
1938 if (vw->vw_proc == p)
1941 vw->vw_smode.mode = VT_AUTO;
1942 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1949 signal_vt_rel(struct vt_window *vw)
1952 if (vw->vw_smode.mode != VT_PROCESS)
1954 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1959 vw->vw_flags |= VWF_SWWAIT_REL;
1960 PROC_LOCK(vw->vw_proc);
1961 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1962 PROC_UNLOCK(vw->vw_proc);
1963 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1968 signal_vt_acq(struct vt_window *vw)
1971 if (vw->vw_smode.mode != VT_PROCESS)
1973 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1974 cnavailable(vw->vw_terminal->consdev, FALSE);
1975 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1980 vw->vw_flags |= VWF_SWWAIT_ACQ;
1981 PROC_LOCK(vw->vw_proc);
1982 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1983 PROC_UNLOCK(vw->vw_proc);
1984 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1989 finish_vt_rel(struct vt_window *vw, int release, int *s)
1992 if (vw->vw_flags & VWF_SWWAIT_REL) {
1993 vw->vw_flags &= ~VWF_SWWAIT_REL;
1995 callout_drain(&vw->vw_proc_dead_timer);
1996 (void)vt_late_window_switch(vw->vw_switch_to);
2004 finish_vt_acq(struct vt_window *vw)
2007 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
2008 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
2014 #ifndef SC_NO_CUTPASTE
2016 vt_mouse_terminput_button(struct vt_device *vd, int button)
2018 struct vt_window *vw;
2020 char mouseb[6] = "\x1B[M";
2023 vw = vd->vd_curwindow;
2026 /* Translate to char position. */
2027 x = vd->vd_mx / vf->vf_width;
2028 y = vd->vd_my / vf->vf_height;
2029 /* Avoid overflow. */
2030 x = MIN(x, 255 - '!');
2031 y = MIN(y, 255 - '!');
2033 mouseb[3] = ' ' + button;
2034 mouseb[4] = '!' + x;
2035 mouseb[5] = '!' + y;
2037 for (i = 0; i < sizeof(mouseb); i++)
2038 terminal_input_char(vw->vw_terminal, mouseb[i]);
2042 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
2047 case MOUSE_BUTTON_EVENT:
2049 /* Mouse button pressed. */
2050 if (event & MOUSE_BUTTON1DOWN)
2051 vt_mouse_terminput_button(vd, 0);
2052 if (event & MOUSE_BUTTON2DOWN)
2053 vt_mouse_terminput_button(vd, 1);
2054 if (event & MOUSE_BUTTON3DOWN)
2055 vt_mouse_terminput_button(vd, 2);
2057 /* Mouse button released. */
2058 vt_mouse_terminput_button(vd, 3);
2062 case MOUSE_MOTION_EVENT:
2063 if (mouse->u.data.z < 0) {
2065 sc_mouse_input_button(vd, 64);
2066 } else if (mouse->u.data.z > 0) {
2068 sc_mouse_input_button(vd, 65);
2081 len = VD_PASTEBUFLEN(main_vd);
2082 buf = VD_PASTEBUF(main_vd);
2083 len /= sizeof(term_char_t);
2084 for (i = 0; i < len; i++) {
2087 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
2093 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
2095 struct vt_device *vd;
2096 struct vt_window *vw;
2102 vw = vd->vd_curwindow;
2106 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
2108 * Either the mouse is disabled, or the window is in
2109 * "graphics mode". The graphics mode is usually set by
2110 * an X server, using the KDSETMODE ioctl.
2114 if (vf == NULL) /* Text mode. */
2118 * TODO: add flag about pointer position changed, to not redraw chars
2119 * under mouse pointer when nothing changed.
2122 if (vw->vw_mouse_level > 0)
2123 vt_mouse_terminput(vd, type, x, y, event, cnt);
2127 case MOUSE_MOTION_EVENT:
2132 vt_termsize(vd, vf, &size);
2137 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
2138 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
2142 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
2143 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
2144 vd->vd_mx / vf->vf_width,
2145 vd->vd_my / vf->vf_height);
2147 vt_resume_flush_timer(vw, 0);
2149 case MOUSE_BUTTON_EVENT:
2157 case MOUSE_BUTTON1DOWN:
2160 mark = VTB_MARK_END;
2162 case 1: /* single click: start cut operation */
2163 mark = VTB_MARK_START;
2165 case 2: /* double click: cut a word */
2166 mark = VTB_MARK_WORD;
2168 case 3: /* triple click: cut a line */
2169 mark = VTB_MARK_ROW;
2173 case VT_MOUSE_PASTEBUTTON:
2182 case VT_MOUSE_EXTENDBUTTON:
2185 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
2186 mark = VTB_MARK_EXTEND;
2191 mark = VTB_MARK_EXTEND;
2199 /* Save buttons state. */
2201 vd->vd_mstate |= event;
2203 vd->vd_mstate &= ~event;
2205 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
2206 vd->vd_my / vf->vf_height) == 1) {
2208 * We have something marked to copy, so update pointer to
2209 * window with selection.
2211 vt_resume_flush_timer(vw, 0);
2217 case VTB_MARK_EXTEND:
2220 /* Other types of mark do not require to copy data. */
2224 /* Get current selection size in bytes. */
2225 len = vtbuf_get_marked_len(&vw->vw_buf);
2229 /* Reallocate buffer only if old one is too small. */
2230 if (len > VD_PASTEBUFSZ(vd)) {
2231 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
2233 /* Update buffer size. */
2234 VD_PASTEBUFSZ(vd) = len;
2236 /* Request copy/paste buffer data, no more than `len' */
2237 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
2240 VD_PASTEBUFLEN(vd) = len;
2242 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
2247 vt_mouse_state(int show)
2249 struct vt_device *vd;
2250 struct vt_window *vw;
2253 vw = vd->vd_curwindow;
2257 vw->vw_flags |= VWF_MOUSE_HIDE;
2260 vw->vw_flags &= ~VWF_MOUSE_HIDE;
2264 /* Mark mouse position as dirty. */
2265 vt_mark_mouse_position_as_dirty(vd, false);
2266 vt_resume_flush_timer(vw, 0);
2271 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
2272 int nprot, vm_memattr_t *memattr)
2274 struct vt_window *vw = tm->tm_softc;
2275 struct vt_device *vd = vw->vw_device;
2277 if (vd->vd_driver->vd_fb_mmap)
2278 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2285 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2288 struct vt_window *vw = tm->tm_softc;
2289 struct vt_device *vd = vw->vw_device;
2292 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2293 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2304 cmd = VT_WAITACTIVE;
2331 cmd = CONS_SETWINORG;
2339 ival = IOCPARM_IVAL(data);
2340 data = (caddr_t)&ival;
2345 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2346 if (*(int *)data & ~0x7f)
2351 case GIO_DEADKEYMAP:
2352 case PIO_DEADKEYMAP:
2357 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2358 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2359 case KBADDKBD: /* add/remove keyboard to/from mux */
2364 if ((kbd = vd->vd_keyboard) != NULL)
2365 error = kbdd_ioctl(kbd, cmd, data);
2367 if (error == ENOIOCTL) {
2368 if (cmd == KDGKBTYPE) {
2369 /* always return something? XXX */
2377 case KDGKBSTATE: { /* get keyboard state (locks) */
2380 if (vw == vd->vd_curwindow) {
2382 if ((kbd = vd->vd_keyboard) != NULL)
2383 error = vt_save_kbd_state(vw, kbd);
2390 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2394 case KDSKBSTATE: { /* set keyboard state (locks) */
2397 state = *(int *)data;
2398 if (state & ~LOCK_MASK)
2401 vw->vw_kbdstate &= ~LOCK_MASK;
2402 vw->vw_kbdstate |= state;
2405 if (vw == vd->vd_curwindow) {
2407 if ((kbd = vd->vd_keyboard) != NULL)
2408 error = vt_update_kbd_state(vw, kbd);
2414 case KDGETLED: { /* get keyboard LED status */
2417 if (vw == vd->vd_curwindow) {
2419 if ((kbd = vd->vd_keyboard) != NULL)
2420 error = vt_save_kbd_leds(vw, kbd);
2427 *(int *)data = vw->vw_kbdstate & LED_MASK;
2431 case KDSETLED: { /* set keyboard LED status */
2434 leds = *(int *)data;
2435 if (leds & ~LED_MASK)
2438 vw->vw_kbdstate &= ~LED_MASK;
2439 vw->vw_kbdstate |= leds;
2442 if (vw == vd->vd_curwindow) {
2444 if ((kbd = vd->vd_keyboard) != NULL)
2445 error = vt_update_kbd_leds(vw, kbd);
2452 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2453 KD_GRAPHICS : KD_TEXT;
2458 if (vw == vd->vd_curwindow) {
2460 if ((kbd = vd->vd_keyboard) != NULL)
2461 error = vt_save_kbd_mode(vw, kbd);
2468 *(int *)data = vw->vw_kbdmode;
2475 mode = *(int *)data;
2480 vw->vw_kbdmode = mode;
2483 if (vw == vd->vd_curwindow) {
2485 if ((kbd = vd->vd_keyboard) != NULL)
2486 error = vt_update_kbd_mode(vw, kbd);
2496 case FBIO_GETWINORG: /* get frame buffer window origin */
2497 case FBIO_GETDISPSTART: /* get display start address */
2498 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2499 case FBIO_BLANK: /* blank display */
2500 if (vd->vd_driver->vd_fb_ioctl)
2501 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2503 case CONS_BLANKTIME:
2507 if (*(int *)data < 0)
2509 if (*(int *)data != vw->vw_buf.vb_history_size)
2510 vtbuf_sethistory_size(&vw->vw_buf, *(int *)data);
2513 vtbuf_clearhistory(&vw->vw_buf);
2515 * Invalidate the entire visible window; it is not guaranteed
2516 * that this operation will be immediately followed by a scroll
2517 * event, so it would otherwise be possible for prior artifacts
2518 * to remain visible.
2521 if (vw == vd->vd_curwindow) {
2522 vd->vd_flags |= VDF_INVALID;
2523 vt_resume_flush_timer(vw, 0);
2529 *(int *)data = M_CG640x480;
2531 case CONS_BELLTYPE: /* set bell type sound */
2532 if ((*(int *)data) & CONS_QUIET_BELL)
2533 vd->vd_flags |= VDF_QUIET_BELL;
2535 vd->vd_flags &= ~VDF_QUIET_BELL;
2537 case CONS_GETINFO: {
2538 vid_info_t *vi = (vid_info_t *)data;
2539 if (vi->size != sizeof(struct vid_info))
2542 if (vw == vd->vd_curwindow) {
2544 if ((kbd = vd->vd_keyboard) != NULL)
2545 vt_save_kbd_state(vw, kbd);
2549 vi->m_num = vd->vd_curwindow->vw_number + 1;
2550 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2551 /* XXX: other fields! */
2555 *(int *)data = 0x200;
2560 case CONS_MOUSECTL: {
2561 mouse_info_t *mouse = (mouse_info_t*)data;
2564 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2565 * should not be applied to individual TTYs, but only to
2568 switch (mouse->operation) {
2570 if (vd->vd_flags & VDF_MOUSECURSOR) {
2571 vd->vd_flags &= ~VDF_MOUSECURSOR;
2572 #ifndef SC_NO_CUTPASTE
2573 vt_mouse_state(VT_MOUSE_HIDE);
2578 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2579 vd->vd_flags |= VDF_MOUSECURSOR;
2580 vd->vd_mx = vd->vd_width / 2;
2581 vd->vd_my = vd->vd_height / 2;
2582 #ifndef SC_NO_CUTPASTE
2583 vt_mouse_state(VT_MOUSE_SHOW);
2594 if (vd->vd_flags & VDF_TEXTMODE)
2597 error = vtfont_load((void *)data, &vf);
2601 error = vt_change_font(vw, vf);
2605 case PIO_VFONT_DEFAULT: {
2606 /* Reset to default font. */
2607 error = vt_change_font(vw, vt_font_assigned);
2611 scrmap_t *sm = (scrmap_t *)data;
2613 /* We don't have screen maps, so return a handcrafted one. */
2614 for (i = 0; i < 256; i++)
2620 * FIXME: This implementation is incomplete compared to
2623 switch (*(int *)data) {
2627 vw->vw_flags &= ~VWF_GRAPHICS;
2630 vw->vw_flags |= VWF_GRAPHICS;
2634 case KDENABIO: /* allow io operations */
2635 error = priv_check(td, PRIV_IO);
2638 error = securelevel_gt(td->td_ucred, 0);
2641 #if defined(__i386__)
2642 td->td_frame->tf_eflags |= PSL_IOPL;
2643 #elif defined(__amd64__)
2644 td->td_frame->tf_rflags |= PSL_IOPL;
2647 case KDDISABIO: /* disallow io operations (default) */
2648 #if defined(__i386__)
2649 td->td_frame->tf_eflags &= ~PSL_IOPL;
2650 #elif defined(__amd64__)
2651 td->td_frame->tf_rflags &= ~PSL_IOPL;
2654 case KDMKTONE: /* sound the bell */
2655 vtterm_beep(tm, *(u_int *)data);
2657 case KIOCSOUND: /* make tone (*data) hz */
2660 case CONS_SETKBD: /* set the new keyboard */
2663 if (vd->vd_keyboard == NULL ||
2664 vd->vd_keyboard->kb_index != *(int *)data) {
2665 kbd = kbd_get_keyboard(*(int *)data);
2670 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2671 (void *)vd, vt_kbdevent, vd);
2673 if ((kbd = vd->vd_keyboard) != NULL) {
2674 vt_save_kbd_state(vd->vd_curwindow, kbd);
2675 kbd_release(kbd, (void *)vd);
2677 kbd = vd->vd_keyboard = kbd_get_keyboard(i);
2679 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2680 vt_update_kbd_state(vd->vd_curwindow, kbd);
2682 error = EPERM; /* XXX */
2687 case CONS_RELKBD: /* release the current keyboard */
2690 if ((kbd = vd->vd_keyboard) != NULL) {
2691 vt_save_kbd_state(vd->vd_curwindow, kbd);
2692 error = kbd_release(kbd, (void *)vd);
2694 vd->vd_keyboard = NULL;
2701 win = *(int *)data - 1;
2702 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2704 if ((win >= VT_MAXWINDOWS) || (win < 0))
2706 return (vt_proc_window_switch(vd->vd_windows[win]));
2709 *(int *)data = vd->vd_curwindow->vw_number + 1;
2712 *(int *)data = vw->vw_number + 1;
2715 /* TODO: Check current state, switching can be in progress. */
2716 if ((*(int *)data) == 0x01)
2717 vw->vw_flags |= VWF_VTYLOCK;
2718 else if ((*(int *)data) == 0x02)
2719 vw->vw_flags &= ~VWF_VTYLOCK;
2725 for (i = 0; i < VT_MAXWINDOWS; i++) {
2726 vw = vd->vd_windows[i];
2729 if (!(vw->vw_flags & VWF_OPENED)) {
2730 *(int *)data = vw->vw_number + 1;
2737 case VT_WAITACTIVE: {
2742 idx = *(unsigned int *)data;
2743 if (idx > VT_MAXWINDOWS)
2746 vw = vd->vd_windows[idx - 1];
2749 while (vd->vd_curwindow != vw && error == 0)
2750 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2754 case VT_SETMODE: { /* set screen switcher mode */
2755 struct vt_mode *mode;
2758 mode = (struct vt_mode *)data;
2759 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2760 if (vw->vw_smode.mode == VT_PROCESS) {
2761 p1 = pfind(vw->vw_pid);
2762 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2765 DPRINTF(5, "error EPERM\n");
2771 if (mode->mode == VT_AUTO) {
2772 vw->vw_smode.mode = VT_AUTO;
2775 DPRINTF(5, "VT_AUTO, ");
2776 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2777 cnavailable(vw->vw_terminal->consdev, TRUE);
2778 /* were we in the middle of the vty switching process? */
2779 if (finish_vt_rel(vw, TRUE, &s) == 0)
2780 DPRINTF(5, "reset WAIT_REL, ");
2781 if (finish_vt_acq(vw) == 0)
2782 DPRINTF(5, "reset WAIT_ACQ, ");
2784 } else if (mode->mode == VT_PROCESS) {
2785 if (!ISSIGVALID(mode->relsig) ||
2786 !ISSIGVALID(mode->acqsig) ||
2787 !ISSIGVALID(mode->frsig)) {
2788 DPRINTF(5, "error EINVAL\n");
2791 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2792 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2793 vw->vw_proc = td->td_proc;
2794 vw->vw_pid = vw->vw_proc->p_pid;
2795 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2796 cnavailable(vw->vw_terminal->consdev, FALSE);
2798 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2805 case VT_GETMODE: /* get screen switcher mode */
2806 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2809 case VT_RELDISP: /* screen switcher ioctl */
2811 * This must be the current vty which is in the VT_PROCESS
2814 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2818 /* ...and this process is controlling it. */
2819 if (vw->vw_proc != td->td_proc) {
2823 switch(*(int *)data) {
2824 case VT_FALSE: /* user refuses to release screen, abort */
2825 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2826 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2827 SC_DRIVER_NAME, VT_UNIT(vw));
2829 case VT_TRUE: /* user has released screen, go on */
2830 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2831 if (vw->vw_flags & VWF_SWWAIT_REL) {
2832 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2833 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2834 SC_DRIVER_NAME, VT_UNIT(vw));
2839 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2840 if ((error = finish_vt_acq(vw)) == 0)
2841 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2842 SC_DRIVER_NAME, VT_UNIT(vw));
2853 static struct vt_window *
2854 vt_allocate_window(struct vt_device *vd, unsigned int window)
2856 struct vt_window *vw;
2857 struct terminal *tm;
2861 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2863 vw->vw_number = window;
2864 vw->vw_kbdmode = K_XLATE;
2866 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2867 vw->vw_font = vtfont_ref(vt_font_assigned);
2868 vt_compute_drawable_area(vw);
2871 vt_termsize(vd, vw->vw_font, &size);
2872 vt_winsize(vd, vw->vw_font, &wsz);
2873 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2874 vw->vw_buf.vb_terminal = tm; /* must be set before vtbuf_init() */
2875 vtbuf_init(&vw->vw_buf, &size);
2877 terminal_set_winsize(tm, &wsz);
2878 vd->vd_windows[window] = vw;
2879 callout_init(&vw->vw_proc_dead_timer, 0);
2885 vt_upgrade(struct vt_device *vd)
2887 struct vt_window *vw;
2889 int register_handlers;
2891 if (!vty_enabled(VTY_VT))
2893 if (main_vd->vd_driver == NULL)
2896 for (i = 0; i < VT_MAXWINDOWS; i++) {
2897 vw = vd->vd_windows[i];
2900 vw = vt_allocate_window(vd, i);
2902 if (!(vw->vw_flags & VWF_READY)) {
2903 callout_init(&vw->vw_proc_dead_timer, 0);
2904 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2905 vw->vw_flags |= VWF_READY;
2906 if (vw->vw_flags & VWF_CONSOLE) {
2907 /* For existing console window. */
2908 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2909 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2914 if (vd->vd_curwindow == NULL)
2915 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2917 register_handlers = 0;
2918 if (!(vd->vd_flags & VDF_ASYNC)) {
2919 /* Attach keyboard. */
2920 vt_allocate_keyboard(vd);
2922 /* Init 25 Hz timer. */
2923 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2926 * Start timer when everything ready.
2927 * Note that the operations here are purposefully ordered.
2928 * We need to ensure vd_timer_armed is non-zero before we set
2929 * the VDF_ASYNC flag. That prevents this function from
2930 * racing with vt_resume_flush_timer() to update the
2931 * callout structure.
2933 atomic_add_acq_int(&vd->vd_timer_armed, 1);
2934 vd->vd_flags |= VDF_ASYNC;
2935 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2936 register_handlers = 1;
2941 /* Refill settings with new sizes. */
2944 if (register_handlers) {
2945 /* Register suspend/resume handlers. */
2946 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2947 vd, EVENTHANDLER_PRI_ANY);
2948 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2949 EVENTHANDLER_PRI_ANY);
2954 vt_resize(struct vt_device *vd)
2956 struct vt_window *vw;
2959 for (i = 0; i < VT_MAXWINDOWS; i++) {
2960 vw = vd->vd_windows[i];
2962 /* Assign default font to window, if not textmode. */
2963 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2964 vw->vw_font = vtfont_ref(vt_font_assigned);
2967 /* Resize terminal windows */
2968 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2969 DPRINTF(100, "%s: vt_change_font() is busy, "
2970 "window %d\n", __func__, i);
2976 vt_replace_backend(const struct vt_driver *drv, void *softc)
2978 struct vt_device *vd;
2982 if (vd->vd_flags & VDF_ASYNC) {
2983 /* Stop vt_flush periodic task. */
2985 vt_suspend_flush_timer(vd);
2988 * Mute current terminal until we done. vt_change_font (called
2989 * from vt_resize) will unmute it.
2991 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2995 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2999 vd->vd_flags &= ~VDF_TEXTMODE;
3003 * We want to upgrade from the current driver to the
3007 vd->vd_prev_driver = vd->vd_driver;
3008 vd->vd_prev_softc = vd->vd_softc;
3009 vd->vd_driver = drv;
3010 vd->vd_softc = softc;
3012 vd->vd_driver->vd_init(vd);
3013 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
3015 * No driver given: we want to downgrade to the previous
3018 const struct vt_driver *old_drv;
3021 old_drv = vd->vd_driver;
3022 old_softc = vd->vd_softc;
3024 vd->vd_driver = vd->vd_prev_driver;
3025 vd->vd_softc = vd->vd_prev_softc;
3026 vd->vd_prev_driver = NULL;
3027 vd->vd_prev_softc = NULL;
3029 vd->vd_flags |= VDF_DOWNGRADE;
3031 vd->vd_driver->vd_init(vd);
3033 if (old_drv->vd_fini)
3034 old_drv->vd_fini(vd, old_softc);
3036 vd->vd_flags &= ~VDF_DOWNGRADE;
3041 /* Update windows sizes and initialize last items. */
3045 if (vd->vd_flags & VDF_SPLASH)
3049 if (vd->vd_flags & VDF_ASYNC) {
3050 /* Allow to put chars now. */
3051 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
3052 /* Rerun timer for screen updates. */
3053 vt_resume_flush_timer(vd->vd_curwindow, 0);
3057 * Register as console. If it already registered, cnadd() will ignore
3060 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
3064 vt_suspend_handler(void *priv)
3066 struct vt_device *vd;
3069 vd->vd_flags |= VDF_SUSPENDED;
3070 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
3071 vd->vd_driver->vd_suspend(vd);
3075 vt_resume_handler(void *priv)
3077 struct vt_device *vd;
3080 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
3081 vd->vd_driver->vd_resume(vd);
3082 vd->vd_flags &= ~VDF_SUSPENDED;
3086 vt_allocate(const struct vt_driver *drv, void *softc)
3089 if (!vty_enabled(VTY_VT))
3092 if (main_vd->vd_driver == NULL) {
3093 main_vd->vd_driver = drv;
3094 printf("VT: initialize with new VT driver \"%s\".\n",
3098 * Check if have rights to replace current driver. For example:
3099 * it is bad idea to replace KMS driver with generic VGA one.
3101 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
3102 printf("VT: Driver priority %d too low. Current %d\n ",
3103 drv->vd_priority, main_vd->vd_driver->vd_priority);
3106 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
3107 main_vd->vd_driver->vd_name, drv->vd_name);
3110 vt_replace_backend(drv, softc);
3114 vt_deallocate(const struct vt_driver *drv, void *softc)
3117 if (!vty_enabled(VTY_VT))
3120 if (main_vd->vd_prev_driver == NULL ||
3121 main_vd->vd_driver != drv ||
3122 main_vd->vd_softc != softc)
3125 printf("VT: Switching back from \"%s\" to \"%s\".\n",
3126 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
3128 vt_replace_backend(NULL, NULL);
3132 vt_suspend(struct vt_device *vd)
3136 if (vt_suspendswitch == 0)
3138 /* Save current window. */
3139 vd->vd_savedwindow = vd->vd_curwindow;
3140 /* Ask holding process to free window and switch to console window */
3141 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
3143 /* Wait for the window switch to complete. */
3146 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
3147 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
3152 vt_resume(struct vt_device *vd)
3155 if (vt_suspendswitch == 0)
3157 /* Switch back to saved window, if any */
3158 vt_proc_window_switch(vd->vd_savedwindow);
3159 vd->vd_savedwindow = NULL;