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 = MIN(size->tp_row / vf->vf_height,
644 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT));
645 size->tp_col = MIN(size->tp_col / vf->vf_width,
646 PIXEL_WIDTH(VT_FB_MAX_WIDTH));
651 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
654 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
655 if (vt_draw_logo_cpus)
656 rect->tr_begin.tp_row = vt_logo_sprite_height;
658 rect->tr_end.tp_row = vd->vd_height;
659 rect->tr_end.tp_col = vd->vd_width;
662 rect->tr_begin.tp_row =
663 howmany(rect->tr_begin.tp_row, vf->vf_height);
665 rect->tr_end.tp_row = MIN(rect->tr_end.tp_row / vf->vf_height,
666 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT));
667 rect->tr_end.tp_col = MIN(rect->tr_end.tp_col / vf->vf_width,
668 PIXEL_WIDTH(VT_FB_MAX_WIDTH));
673 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
676 size->ws_ypixel = vd->vd_height;
677 if (vt_draw_logo_cpus)
678 size->ws_ypixel -= vt_logo_sprite_height;
679 size->ws_row = size->ws_ypixel;
680 size->ws_col = size->ws_xpixel = vd->vd_width;
682 size->ws_row = MIN(size->ws_row / vf->vf_height,
683 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT));
684 size->ws_col = MIN(size->ws_col / vf->vf_width,
685 PIXEL_WIDTH(VT_FB_MAX_WIDTH));
690 vt_compute_drawable_area(struct vt_window *vw)
692 struct vt_device *vd;
698 if (vw->vw_font == NULL) {
699 vw->vw_draw_area.tr_begin.tp_col = 0;
700 vw->vw_draw_area.tr_begin.tp_row = 0;
701 if (vt_draw_logo_cpus)
702 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
703 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
704 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
711 * Compute the drawable area, so that the text is centered on
715 height = vd->vd_height;
716 if (vt_draw_logo_cpus)
717 height -= vt_logo_sprite_height;
718 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
719 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
720 if (vt_draw_logo_cpus)
721 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
722 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
723 rounddown(vd->vd_width, vf->vf_width);
724 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
725 rounddown(height, vf->vf_height);
729 vt_scroll(struct vt_window *vw, int offset, int whence)
734 if ((vw->vw_flags & VWF_SCROLL) == 0)
737 vt_termsize(vw->vw_device, vw->vw_font, &size);
739 diff = vthistory_seek(&vw->vw_buf, offset, whence);
741 vw->vw_device->vd_flags |= VDF_INVALID;
742 vt_resume_flush_timer(vw, 0);
746 vt_machine_kbdevent(struct vt_device *vd, int c)
750 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
752 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
753 #if VT_ALT_TO_ESC_HACK
755 * There's an unfortunate conflict between SPCLKEY|DBG
756 * and VT_ALT_TO_ESC_HACK. Just assume they didn't mean
757 * it if we got to here.
759 vd->vd_kbstate &= ~ALKED;
763 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
765 shutdown_nice(RB_HALT);
767 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
768 #ifndef SC_NO_CUTPASTE
769 /* Insert text from cut-paste buffer. */
773 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
775 shutdown_nice(RB_HALT|RB_POWEROFF);
777 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
779 * Request to immediate panic if sysctl
780 * kern.vt.enable_panic_key allow it.
783 panic("Forced by the panic key");
785 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
787 shutdown_nice(RB_AUTOBOOT);
789 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
790 /* Force activatation/deactivation of the screen saver. */
793 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
794 /* Put machine into Stand-By mode. */
795 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
797 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
798 /* Suspend machine. */
799 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
807 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
809 struct vt_device *vd;
813 /* Only special keys handled in ScrollLock mode */
814 if ((c & SPCLKEY) == 0)
820 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
821 vw = vd->vd_windows[c - F_SCR];
822 vt_proc_window_switch(vw);
830 /* Turn scrolling off. */
831 vt_scroll(vw, 0, VHS_END);
832 VTBUF_SLCK_DISABLE(&vw->vw_buf);
833 vw->vw_flags &= ~VWF_SCROLL;
836 case FKEY | F(49): /* Home key. */
837 vt_scroll(vw, 0, VHS_SET);
839 case FKEY | F(50): /* Arrow up. */
840 vt_scroll(vw, -1, VHS_CUR);
842 case FKEY | F(51): /* Page up. */
843 vt_termsize(vd, vw->vw_font, &size);
844 vt_scroll(vw, -size.tp_row, VHS_CUR);
846 case FKEY | F(57): /* End key. */
847 vt_scroll(vw, 0, VHS_END);
849 case FKEY | F(58): /* Arrow down. */
850 vt_scroll(vw, 1, VHS_CUR);
852 case FKEY | F(59): /* Page down. */
853 vt_termsize(vd, vw->vw_font, &size);
854 vt_scroll(vw, size.tp_row, VHS_CUR);
863 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
865 struct vt_window *vw = vd->vd_curwindow;
867 random_harvest_queue(&c, sizeof(c), RANDOM_KEYBOARD);
868 #if VT_ALT_TO_ESC_HACK
870 switch (c & ~RELKEY) {
871 case (SPCLKEY | RALT):
872 if (vt_enable_altgr != 0)
874 case (SPCLKEY | LALT):
875 vd->vd_kbstate &= ~ALKED;
877 /* Other keys ignored for RELKEY event. */
880 switch (c & ~RELKEY) {
881 case (SPCLKEY | RALT):
882 if (vt_enable_altgr != 0)
884 case (SPCLKEY | LALT):
885 vd->vd_kbstate |= ALKED;
890 /* Other keys ignored for RELKEY event. */
894 if (vt_machine_kbdevent(vd, c))
897 if (vw->vw_flags & VWF_SCROLL) {
898 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
899 /* Scroll mode keys handled, nothing to do more. */
906 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
907 vw = vd->vd_windows[c - F_SCR];
908 vt_proc_window_switch(vw);
914 /* Switch to next VT. */
915 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
916 vw = vd->vd_windows[c];
917 vt_proc_window_switch(vw);
920 /* Switch to previous VT. */
921 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
922 vw = vd->vd_windows[c];
923 vt_proc_window_switch(vw);
926 vt_save_kbd_state(vw, kbd);
928 if (vw->vw_kbdstate & SLKED) {
929 /* Turn scrolling on. */
930 vw->vw_flags |= VWF_SCROLL;
931 VTBUF_SLCK_ENABLE(&vw->vw_buf);
933 /* Turn scrolling off. */
934 vw->vw_flags &= ~VWF_SCROLL;
935 VTBUF_SLCK_DISABLE(&vw->vw_buf);
936 vt_scroll(vw, 0, VHS_END);
941 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
942 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
943 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
944 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
945 /* F1 through F12 keys. */
946 terminal_input_special(vw->vw_terminal,
947 TKEY_F1 + c - (FKEY | F(1)));
949 case FKEY | F(49): /* Home key. */
950 terminal_input_special(vw->vw_terminal, TKEY_HOME);
952 case FKEY | F(50): /* Arrow up. */
953 terminal_input_special(vw->vw_terminal, TKEY_UP);
955 case FKEY | F(51): /* Page up. */
956 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
958 case FKEY | F(53): /* Arrow left. */
959 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
961 case FKEY | F(55): /* Arrow right. */
962 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
964 case FKEY | F(57): /* End key. */
965 terminal_input_special(vw->vw_terminal, TKEY_END);
967 case FKEY | F(58): /* Arrow down. */
968 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
970 case FKEY | F(59): /* Page down. */
971 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
973 case FKEY | F(60): /* Insert key. */
974 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
976 case FKEY | F(61): /* Delete key. */
977 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
980 } else if (KEYFLAGS(c) == 0) {
981 /* Don't do UTF-8 conversion when doing raw mode. */
982 if (vw->vw_kbdmode == K_XLATE) {
983 #if VT_ALT_TO_ESC_HACK
984 if (vd->vd_kbstate & ALKED) {
986 * Prepend ESC sequence if one of ALT keys down.
988 terminal_input_char(vw->vw_terminal, 0x1b);
992 kdb_alt_break(c, &vd->vd_altbrk);
994 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
996 terminal_input_raw(vw->vw_terminal, c);
1002 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
1004 struct vt_device *vd = arg;
1008 case KBDIO_KEYINPUT:
1010 case KBDIO_UNLOADING:
1012 vd->vd_keyboard = NULL;
1013 kbd_release(kbd, (void *)vd);
1020 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
1021 vt_processkey(kbd, vd, c);
1027 vt_allocate_keyboard(struct vt_device *vd)
1029 int grabbed, i, idx0, idx;
1034 * If vt_upgrade() happens while the console is grabbed, we are
1035 * potentially going to switch keyboard devices while the keyboard is in
1036 * use. Unwind the grabbing of the current keyboard first, then we will
1037 * re-grab the new keyboard below, before we return.
1039 if (vd->vd_curwindow == &vt_conswindow) {
1040 grabbed = vd->vd_curwindow->vw_grabbed;
1041 for (i = 0; i < grabbed; ++i)
1042 vtterm_cnungrab_noswitch(vd, vd->vd_curwindow);
1045 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
1047 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
1048 k0 = kbd_get_keyboard(idx0);
1050 for (idx = kbd_find_keyboard2("*", -1, 0);
1052 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
1053 k = kbd_get_keyboard(idx);
1055 if (idx == idx0 || KBD_IS_BUSY(k))
1058 bzero(&ki, sizeof(ki));
1059 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
1060 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
1061 ki.kb_unit = k->kb_unit;
1063 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
1066 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
1067 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
1069 DPRINTF(10, "%s: No keyboard found.\n", __func__);
1072 k0 = kbd_get_keyboard(idx0);
1074 vd->vd_keyboard = k0;
1075 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__,
1076 vd->vd_keyboard->kb_index);
1078 if (vd->vd_curwindow == &vt_conswindow) {
1079 for (i = 0; i < grabbed; ++i)
1080 vtterm_cngrab_noswitch(vd, vd->vd_curwindow);
1087 vtterm_bell(struct terminal *tm)
1089 struct vt_window *vw = tm->tm_softc;
1090 struct vt_device *vd = vw->vw_device;
1092 if (!vt_enable_bell)
1095 if (vd->vd_flags & VDF_QUIET_BELL)
1098 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
1102 vtterm_beep(struct terminal *tm, u_int param)
1106 if (!vt_enable_bell)
1109 if ((param == 0) || ((param & 0xffff) == 0)) {
1114 period = ((param >> 16) & 0xffff) * hz / 1000;
1115 freq = 1193182 / (param & 0xffff);
1117 sysbeep(freq, period);
1121 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1123 struct vt_window *vw = tm->tm_softc;
1125 vtbuf_cursor_position(&vw->vw_buf, p);
1129 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1131 struct vt_window *vw = tm->tm_softc;
1133 vtbuf_putchar(&vw->vw_buf, p, c);
1137 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1139 struct vt_window *vw = tm->tm_softc;
1141 vtbuf_fill(&vw->vw_buf, r, c);
1145 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1146 const term_pos_t *p)
1148 struct vt_window *vw = tm->tm_softc;
1150 vtbuf_copy(&vw->vw_buf, r, p);
1154 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1156 struct vt_window *vw = tm->tm_softc;
1159 case TP_SETLOCALCURSOR:
1161 * 0 means normal (usually block), 1 means hidden, and
1162 * 2 means blinking (always block) for compatibility with
1163 * syscons. We don't support any changes except hiding,
1164 * so must map 2 to 0.
1166 arg = (arg == 1) ? 0 : 1;
1169 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1170 vt_resume_flush_timer(vw, 0);
1173 vw->vw_mouse_level = arg;
1179 vt_determine_colors(term_char_t c, int cursor,
1180 term_color_t *fg, term_color_t *bg)
1187 *fg = TCHAR_FGCOLOR(c);
1188 if (TCHAR_FORMAT(c) & TF_BOLD)
1189 *fg = TCOLOR_LIGHT(*fg);
1190 *bg = TCHAR_BGCOLOR(c);
1191 if (TCHAR_FORMAT(c) & TF_BLINK)
1192 *bg = TCOLOR_LIGHT(*bg);
1194 if (TCHAR_FORMAT(c) & TF_REVERSE)
1206 #ifndef SC_NO_CUTPASTE
1208 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1210 unsigned int mx, my;
1213 * We use the cursor position saved during the current refresh,
1214 * in case the cursor moved since.
1216 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1217 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1219 if (mx >= area->tr_end.tp_col ||
1220 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1221 my >= area->tr_end.tp_row ||
1222 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1228 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked)
1231 struct vt_window *vw;
1235 vw = vd->vd_curwindow;
1238 x = vd->vd_mx_drawn;
1239 y = vd->vd_my_drawn;
1242 area.tr_begin.tp_col = x / vf->vf_width;
1243 area.tr_begin.tp_row = y / vf->vf_height;
1244 area.tr_end.tp_col =
1245 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1246 area.tr_end.tp_row =
1247 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1250 * No font loaded (ie. vt_vga operating in textmode).
1252 * FIXME: This fake area needs to be revisited once the
1253 * mouse cursor is supported in vt_vga's textmode.
1255 area.tr_begin.tp_col = x;
1256 area.tr_begin.tp_row = y;
1257 area.tr_end.tp_col = x + 2;
1258 area.tr_end.tp_row = y + 2;
1262 vtbuf_lock(&vw->vw_buf);
1263 if (vd->vd_driver->vd_invalidate_text)
1264 vd->vd_driver->vd_invalidate_text(vd, &area);
1265 vtbuf_dirty(&vw->vw_buf, &area);
1267 vtbuf_unlock(&vw->vw_buf);
1272 vt_set_border(struct vt_device *vd, const term_rect_t *area,
1275 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
1277 if (drawrect == NULL)
1281 if (area->tr_begin.tp_row > 0)
1282 drawrect(vd, 0, 0, vd->vd_width - 1,
1283 area->tr_begin.tp_row - 1, 1, c);
1286 if (area->tr_begin.tp_col > 0)
1287 drawrect(vd, 0, area->tr_begin.tp_row,
1288 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
1291 if (area->tr_end.tp_col < vd->vd_width)
1292 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
1293 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
1296 if (area->tr_end.tp_row < vd->vd_height)
1297 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
1298 vd->vd_height - 1, 1, c);
1302 vt_flush(struct vt_device *vd)
1304 struct vt_window *vw;
1307 #ifndef SC_NO_CUTPASTE
1308 int cursor_was_shown, cursor_moved;
1311 vw = vd->vd_curwindow;
1315 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1319 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1322 vtbuf_lock(&vw->vw_buf);
1324 #ifndef SC_NO_CUTPASTE
1325 cursor_was_shown = vd->vd_mshown;
1326 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1327 vd->vd_my != vd->vd_my_drawn);
1329 /* Check if the cursor should be displayed or not. */
1330 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1331 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1332 !kdb_active && !KERNEL_PANICKED()) { /* DDB inactive. */
1339 * If the cursor changed display state or moved, we must mark
1340 * the old position as dirty, so that it's erased.
1342 if (cursor_was_shown != vd->vd_mshown ||
1343 (vd->vd_mshown && cursor_moved))
1344 vt_mark_mouse_position_as_dirty(vd, true);
1347 * Save position of the mouse cursor. It's used by backends to
1348 * know where to draw the cursor and during the next refresh to
1349 * erase the previous position.
1351 vd->vd_mx_drawn = vd->vd_mx;
1352 vd->vd_my_drawn = vd->vd_my;
1355 * If the cursor is displayed and has moved since last refresh,
1356 * mark the new position as dirty.
1358 if (vd->vd_mshown && cursor_moved)
1359 vt_mark_mouse_position_as_dirty(vd, true);
1362 vtbuf_undirty(&vw->vw_buf, &tarea);
1364 /* Force a full redraw when the screen contents might be invalid. */
1365 if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) {
1366 const teken_attr_t *a;
1368 vd->vd_flags &= ~VDF_INVALID;
1370 a = teken_get_curattr(&vw->vw_terminal->tm_emulator);
1371 vt_set_border(vd, &vw->vw_draw_area, a->ta_bgcolor);
1372 vt_termrect(vd, vf, &tarea);
1373 if (vd->vd_driver->vd_invalidate_text)
1374 vd->vd_driver->vd_invalidate_text(vd, &tarea);
1375 if (vt_draw_logo_cpus)
1376 vtterm_draw_cpu_logos(vd);
1379 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1380 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1381 vtbuf_unlock(&vw->vw_buf);
1385 vtbuf_unlock(&vw->vw_buf);
1392 struct vt_device *vd;
1396 /* Update screen if required. */
1397 changed = vt_flush(vd);
1399 /* Schedule for next update. */
1401 vt_schedule_flush(vd, 0);
1403 vd->vd_timer_armed = 0;
1407 vtterm_pre_input(struct terminal *tm)
1409 struct vt_window *vw = tm->tm_softc;
1411 vtbuf_lock(&vw->vw_buf);
1415 vtterm_post_input(struct terminal *tm)
1417 struct vt_window *vw = tm->tm_softc;
1419 vtbuf_unlock(&vw->vw_buf);
1420 vt_resume_flush_timer(vw, 0);
1424 vtterm_done(struct terminal *tm)
1426 struct vt_window *vw = tm->tm_softc;
1427 struct vt_device *vd = vw->vw_device;
1429 if (kdb_active || KERNEL_PANICKED()) {
1430 /* Switch to the debugger. */
1431 if (vd->vd_curwindow != vw) {
1432 vd->vd_curwindow = vw;
1433 vd->vd_flags |= VDF_INVALID;
1434 if (vd->vd_driver->vd_postswitch)
1435 vd->vd_driver->vd_postswitch(vd);
1437 vd->vd_flags &= ~VDF_SPLASH;
1439 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1446 vtterm_splash(struct vt_device *vd)
1448 vt_axis_t top, left;
1450 /* Display a nice boot splash. */
1451 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1452 top = (vd->vd_height - vt_logo_height) / 2;
1453 left = (vd->vd_width - vt_logo_width) / 2;
1454 switch (vt_logo_depth) {
1456 /* XXX: Unhardcode colors! */
1457 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1458 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1459 left, top, TC_WHITE, TC_BLACK);
1461 vd->vd_flags |= VDF_SPLASH;
1466 static struct vt_font *
1467 parse_font_info_static(struct font_info *fi)
1469 struct vt_font *vfp;
1476 ptr = (uintptr_t)fi;
1478 * Compute and verify checksum. The total sum of all the fields
1481 checksum = fi->fi_width;
1482 checksum += fi->fi_height;
1483 checksum += fi->fi_bitmap_size;
1484 for (unsigned i = 0; i < VFNT_MAPS; i++)
1485 checksum += fi->fi_map_count[i];
1487 if (checksum + fi->fi_checksum != 0)
1490 ptr += sizeof(struct font_info);
1491 ptr = roundup2(ptr, 8);
1493 vfp = &vt_font_loader;
1494 vfp->vf_height = fi->fi_height;
1495 vfp->vf_width = fi->fi_width;
1496 /* This is default font, set refcount 1 to disable removal. */
1497 vfp->vf_refcount = 1;
1498 for (unsigned i = 0; i < VFNT_MAPS; i++) {
1499 if (fi->fi_map_count[i] == 0)
1501 vfp->vf_map_count[i] = fi->fi_map_count[i];
1502 vfp->vf_map[i] = (vfnt_map_t *)ptr;
1503 ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t));
1504 ptr = roundup2(ptr, 8);
1506 vfp->vf_bytes = (uint8_t *)ptr;
1511 * Set up default font with allocated data structures.
1512 * However, we can not set refcount here, because it is already set and
1513 * incremented in vtterm_cnprobe() to avoid being released by font load from
1516 static struct vt_font *
1517 parse_font_info(struct font_info *fi)
1519 struct vt_font *vfp;
1527 ptr = (uintptr_t)fi;
1529 * Compute and verify checksum. The total sum of all the fields
1532 checksum = fi->fi_width;
1533 checksum += fi->fi_height;
1534 checksum += fi->fi_bitmap_size;
1535 for (unsigned i = 0; i < VFNT_MAPS; i++)
1536 checksum += fi->fi_map_count[i];
1538 if (checksum + fi->fi_checksum != 0)
1541 ptr += sizeof(struct font_info);
1542 ptr = roundup2(ptr, 8);
1544 vfp = &vt_font_loader;
1545 vfp->vf_height = fi->fi_height;
1546 vfp->vf_width = fi->fi_width;
1547 for (unsigned i = 0; i < VFNT_MAPS; i++) {
1548 if (fi->fi_map_count[i] == 0)
1550 vfp->vf_map_count[i] = fi->fi_map_count[i];
1551 size = fi->fi_map_count[i] * sizeof(vfnt_map_t);
1552 vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO);
1553 bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size);
1555 ptr = roundup2(ptr, 8);
1557 vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO);
1558 bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size);
1563 vt_init_font(void *arg)
1566 struct font_info *fi;
1567 struct vt_font *font;
1569 kmdp = preload_search_by_type("elf kernel");
1571 kmdp = preload_search_by_type("elf64 kernel");
1572 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *);
1574 font = parse_font_info(fi);
1576 vt_font_assigned = font;
1579 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev);
1582 vt_init_font_static(void)
1585 struct font_info *fi;
1586 struct vt_font *font;
1588 kmdp = preload_search_by_type("elf kernel");
1590 kmdp = preload_search_by_type("elf64 kernel");
1591 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *);
1593 font = parse_font_info_static(fi);
1595 vt_font_assigned = font;
1599 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1601 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1602 struct vt_window *vw = tm->tm_softc;
1603 struct vt_device *vd = vw->vw_device;
1605 const term_attr_t *a;
1607 if (!vty_enabled(VTY_VT))
1610 if (vd->vd_flags & VDF_INITIALIZED)
1611 /* Initialization already done. */
1614 SET_FOREACH(vtdlist, vt_drv_set) {
1616 if (vtd->vd_probe == NULL)
1618 if (vtd->vd_probe(vd) == CN_DEAD)
1620 if ((vtdbest == NULL) ||
1621 (vtd->vd_priority > vtdbest->vd_priority))
1624 if (vtdbest == NULL) {
1625 cp->cn_pri = CN_DEAD;
1626 vd->vd_flags |= VDF_DEAD;
1628 vd->vd_driver = vtdbest;
1629 cp->cn_pri = vd->vd_driver->vd_init(vd);
1632 /* Check if driver's vt_init return CN_DEAD. */
1633 if (cp->cn_pri == CN_DEAD) {
1634 vd->vd_flags |= VDF_DEAD;
1637 /* Initialize any early-boot keyboard drivers */
1638 kbd_configure(KB_CONF_PROBE_ONLY);
1640 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1641 vd->vd_windows[VT_CONSWINDOW] = vw;
1642 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1644 vt_init_font_static();
1646 /* Attach default font if not in TEXTMODE. */
1647 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1648 vw->vw_font = vtfont_ref(vt_font_assigned);
1649 vt_compute_drawable_area(vw);
1653 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1654 * that we have the real viewable size, fix it in the static
1657 if (vd->vd_width != 0 && vd->vd_height != 0)
1658 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1660 /* We need to access terminal attributes from vtbuf */
1661 vw->vw_buf.vb_terminal = tm;
1662 vtbuf_init_early(&vw->vw_buf);
1663 vt_winsize(vd, vw->vw_font, &wsz);
1664 a = teken_get_curattr(&tm->tm_emulator);
1665 terminal_set_winsize_blank(tm, &wsz, 1, a);
1667 if (vtdbest != NULL) {
1672 vd->vd_flags |= VDF_INITIALIZED;
1677 vtterm_cngetc(struct terminal *tm)
1679 struct vt_window *vw = tm->tm_softc;
1680 struct vt_device *vd = vw->vw_device;
1684 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1685 return (*vw->vw_kbdsq++);
1687 /* Make sure the splash screen is not there. */
1688 if (vd->vd_flags & VDF_SPLASH) {
1690 vd->vd_flags &= ~VDF_SPLASH;
1691 /* Mark screen as invalid to force update */
1692 vd->vd_flags |= VDF_INVALID;
1696 /* Stripped down keyboard handler. */
1697 if ((kbd = vd->vd_keyboard) == NULL)
1700 /* Force keyboard input mode to K_XLATE */
1701 vw->vw_kbdmode = K_XLATE;
1702 vt_update_kbd_mode(vw, kbd);
1704 /* Switch the keyboard to polling to make it work here. */
1705 kbdd_poll(kbd, TRUE);
1706 c = kbdd_read_char(kbd, 0);
1707 kbdd_poll(kbd, FALSE);
1711 if (vw->vw_flags & VWF_SCROLL) {
1712 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1717 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1721 vt_save_kbd_state(vw, kbd);
1722 if (vw->vw_kbdstate & SLKED) {
1723 /* Turn scrolling on. */
1724 vw->vw_flags |= VWF_SCROLL;
1725 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1727 /* Turn scrolling off. */
1728 vt_scroll(vw, 0, VHS_END);
1729 vw->vw_flags &= ~VWF_SCROLL;
1730 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1733 /* XXX: KDB can handle history. */
1734 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1735 vw->vw_kbdsq = "\x1b[A";
1737 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1738 vw->vw_kbdsq = "\x1b[B";
1740 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1741 vw->vw_kbdsq = "\x1b[C";
1743 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1744 vw->vw_kbdsq = "\x1b[D";
1748 /* Force refresh to make scrollback work. */
1750 } else if (KEYFLAGS(c) == 0) {
1751 return (KEYCHAR(c));
1754 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1755 return (*vw->vw_kbdsq++);
1761 * These two do most of what we want to do in vtterm_cnungrab, but without
1762 * actually switching windows. This is necessary for, e.g.,
1763 * vt_allocate_keyboard() to get the current keyboard into the state it needs to
1764 * be in without damaging the device's window state.
1766 * Both return the current grab count, though it's only used in vtterm_cnungrab.
1769 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw)
1773 if (vw->vw_grabbed++ > 0)
1774 return (vw->vw_grabbed);
1776 if ((kbd = vd->vd_keyboard) == NULL)
1780 * Make sure the keyboard is accessible even when the kbd device
1781 * driver is disabled.
1785 /* We shall always use the keyboard in the XLATE mode here. */
1786 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1787 vw->vw_kbdmode = K_XLATE;
1788 vt_update_kbd_mode(vw, kbd);
1790 kbdd_poll(kbd, TRUE);
1795 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw)
1799 if (--vw->vw_grabbed > 0)
1800 return (vw->vw_grabbed);
1802 if ((kbd = vd->vd_keyboard) == NULL)
1805 kbdd_poll(kbd, FALSE);
1807 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1808 vt_update_kbd_mode(vw, kbd);
1814 vtterm_cngrab(struct terminal *tm)
1816 struct vt_device *vd;
1817 struct vt_window *vw;
1822 /* To be restored after we ungrab. */
1823 if (vd->vd_grabwindow == NULL)
1824 vd->vd_grabwindow = vd->vd_curwindow;
1827 vt_window_switch(vw);
1829 vtterm_cngrab_noswitch(vd, vw);
1833 vtterm_cnungrab(struct terminal *tm)
1835 struct vt_device *vd;
1836 struct vt_window *vw;
1841 MPASS(vd->vd_grabwindow != NULL);
1842 if (vtterm_cnungrab_noswitch(vd, vw) != 0)
1845 if (!cold && vd->vd_grabwindow != vw)
1846 vt_window_switch(vd->vd_grabwindow);
1848 vd->vd_grabwindow = NULL;
1852 vtterm_opened(struct terminal *tm, int opened)
1854 struct vt_window *vw = tm->tm_softc;
1855 struct vt_device *vd = vw->vw_device;
1858 vd->vd_flags &= ~VDF_SPLASH;
1860 vw->vw_flags |= VWF_OPENED;
1862 vw->vw_flags &= ~VWF_OPENED;
1863 /* TODO: finish ACQ/REL */
1869 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1871 struct vt_device *vd = vw->vw_device;
1872 struct terminal *tm = vw->vw_terminal;
1879 * Changing fonts is a little tricky. We must prevent
1880 * simultaneous access to the device, so we must stop
1881 * the display timer and the terminal from accessing.
1882 * We need to switch fonts and grow our screen buffer.
1884 * XXX: Right now the code uses terminal_mute() to
1885 * prevent data from reaching the console driver while
1886 * resizing the screen buffer. This isn't elegant...
1890 if (vw->vw_flags & VWF_BUSY) {
1891 /* Another process is changing the font. */
1895 vw->vw_flags |= VWF_BUSY;
1898 vt_termsize(vd, vf, &size);
1899 vt_winsize(vd, vf, &wsz);
1901 /* Grow the screen buffer and terminal. */
1902 terminal_mute(tm, 1);
1903 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1904 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1905 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1906 terminal_mute(tm, 0);
1908 /* Actually apply the font to the current window. */
1910 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1912 * In case vt_change_font called to update size we don't need
1913 * to update font link.
1915 vtfont_unref(vw->vw_font);
1916 vw->vw_font = vtfont_ref(vf);
1920 * Compute the drawable area and move the mouse cursor inside
1921 * it, in case the new area is smaller than the previous one.
1923 vt_compute_drawable_area(vw);
1924 vd->vd_mx = min(vd->vd_mx,
1925 vw->vw_draw_area.tr_end.tp_col -
1926 vw->vw_draw_area.tr_begin.tp_col - 1);
1927 vd->vd_my = min(vd->vd_my,
1928 vw->vw_draw_area.tr_end.tp_row -
1929 vw->vw_draw_area.tr_begin.tp_row - 1);
1931 /* Force a full redraw the next timer tick. */
1932 if (vd->vd_curwindow == vw) {
1933 vd->vd_flags |= VDF_INVALID;
1934 vt_resume_flush_timer(vw, 0);
1936 vw->vw_flags &= ~VWF_BUSY;
1942 vt_proc_alive(struct vt_window *vw)
1946 if (vw->vw_smode.mode != VT_PROCESS)
1950 if ((p = pfind(vw->vw_pid)) != NULL)
1952 if (vw->vw_proc == p)
1955 vw->vw_smode.mode = VT_AUTO;
1956 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1963 signal_vt_rel(struct vt_window *vw)
1966 if (vw->vw_smode.mode != VT_PROCESS)
1968 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1973 vw->vw_flags |= VWF_SWWAIT_REL;
1974 PROC_LOCK(vw->vw_proc);
1975 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1976 PROC_UNLOCK(vw->vw_proc);
1977 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1982 signal_vt_acq(struct vt_window *vw)
1985 if (vw->vw_smode.mode != VT_PROCESS)
1987 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1988 cnavailable(vw->vw_terminal->consdev, FALSE);
1989 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1994 vw->vw_flags |= VWF_SWWAIT_ACQ;
1995 PROC_LOCK(vw->vw_proc);
1996 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1997 PROC_UNLOCK(vw->vw_proc);
1998 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
2003 finish_vt_rel(struct vt_window *vw, int release, int *s)
2006 if (vw->vw_flags & VWF_SWWAIT_REL) {
2007 vw->vw_flags &= ~VWF_SWWAIT_REL;
2009 callout_drain(&vw->vw_proc_dead_timer);
2010 (void)vt_late_window_switch(vw->vw_switch_to);
2018 finish_vt_acq(struct vt_window *vw)
2021 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
2022 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
2028 #ifndef SC_NO_CUTPASTE
2030 vt_mouse_terminput_button(struct vt_device *vd, int button)
2032 struct vt_window *vw;
2034 char mouseb[6] = "\x1B[M";
2037 vw = vd->vd_curwindow;
2040 /* Translate to char position. */
2041 x = vd->vd_mx / vf->vf_width;
2042 y = vd->vd_my / vf->vf_height;
2043 /* Avoid overflow. */
2044 x = MIN(x, 255 - '!');
2045 y = MIN(y, 255 - '!');
2047 mouseb[3] = ' ' + button;
2048 mouseb[4] = '!' + x;
2049 mouseb[5] = '!' + y;
2051 for (i = 0; i < sizeof(mouseb); i++)
2052 terminal_input_char(vw->vw_terminal, mouseb[i]);
2056 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
2061 case MOUSE_BUTTON_EVENT:
2063 /* Mouse button pressed. */
2064 if (event & MOUSE_BUTTON1DOWN)
2065 vt_mouse_terminput_button(vd, 0);
2066 if (event & MOUSE_BUTTON2DOWN)
2067 vt_mouse_terminput_button(vd, 1);
2068 if (event & MOUSE_BUTTON3DOWN)
2069 vt_mouse_terminput_button(vd, 2);
2071 /* Mouse button released. */
2072 vt_mouse_terminput_button(vd, 3);
2076 case MOUSE_MOTION_EVENT:
2077 if (mouse->u.data.z < 0) {
2079 sc_mouse_input_button(vd, 64);
2080 } else if (mouse->u.data.z > 0) {
2082 sc_mouse_input_button(vd, 65);
2095 len = VD_PASTEBUFLEN(main_vd);
2096 buf = VD_PASTEBUF(main_vd);
2097 len /= sizeof(term_char_t);
2098 for (i = 0; i < len; i++) {
2101 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
2107 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
2109 struct vt_device *vd;
2110 struct vt_window *vw;
2116 vw = vd->vd_curwindow;
2120 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
2122 * Either the mouse is disabled, or the window is in
2123 * "graphics mode". The graphics mode is usually set by
2124 * an X server, using the KDSETMODE ioctl.
2128 if (vf == NULL) /* Text mode. */
2132 * TODO: add flag about pointer position changed, to not redraw chars
2133 * under mouse pointer when nothing changed.
2136 if (vw->vw_mouse_level > 0)
2137 vt_mouse_terminput(vd, type, x, y, event, cnt);
2141 case MOUSE_MOTION_EVENT:
2146 vt_termsize(vd, vf, &size);
2151 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
2152 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
2156 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
2157 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
2158 vd->vd_mx / vf->vf_width,
2159 vd->vd_my / vf->vf_height);
2161 vt_resume_flush_timer(vw, 0);
2163 case MOUSE_BUTTON_EVENT:
2171 case MOUSE_BUTTON1DOWN:
2174 mark = VTB_MARK_END;
2176 case 1: /* single click: start cut operation */
2177 mark = VTB_MARK_START;
2179 case 2: /* double click: cut a word */
2180 mark = VTB_MARK_WORD;
2182 case 3: /* triple click: cut a line */
2183 mark = VTB_MARK_ROW;
2187 case VT_MOUSE_PASTEBUTTON:
2196 case VT_MOUSE_EXTENDBUTTON:
2199 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
2200 mark = VTB_MARK_EXTEND;
2205 mark = VTB_MARK_EXTEND;
2213 /* Save buttons state. */
2215 vd->vd_mstate |= event;
2217 vd->vd_mstate &= ~event;
2219 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
2220 vd->vd_my / vf->vf_height) == 1) {
2222 * We have something marked to copy, so update pointer to
2223 * window with selection.
2225 vt_resume_flush_timer(vw, 0);
2231 case VTB_MARK_EXTEND:
2234 /* Other types of mark do not require to copy data. */
2238 /* Get current selection size in bytes. */
2239 len = vtbuf_get_marked_len(&vw->vw_buf);
2243 /* Reallocate buffer only if old one is too small. */
2244 if (len > VD_PASTEBUFSZ(vd)) {
2245 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
2247 /* Update buffer size. */
2248 VD_PASTEBUFSZ(vd) = len;
2250 /* Request copy/paste buffer data, no more than `len' */
2251 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
2254 VD_PASTEBUFLEN(vd) = len;
2256 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
2261 vt_mouse_state(int show)
2263 struct vt_device *vd;
2264 struct vt_window *vw;
2267 vw = vd->vd_curwindow;
2271 vw->vw_flags |= VWF_MOUSE_HIDE;
2274 vw->vw_flags &= ~VWF_MOUSE_HIDE;
2278 /* Mark mouse position as dirty. */
2279 vt_mark_mouse_position_as_dirty(vd, false);
2280 vt_resume_flush_timer(vw, 0);
2285 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
2286 int nprot, vm_memattr_t *memattr)
2288 struct vt_window *vw = tm->tm_softc;
2289 struct vt_device *vd = vw->vw_device;
2291 if (vd->vd_driver->vd_fb_mmap)
2292 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2299 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2302 struct vt_window *vw = tm->tm_softc;
2303 struct vt_device *vd = vw->vw_device;
2306 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2307 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2318 cmd = VT_WAITACTIVE;
2345 cmd = CONS_SETWINORG;
2353 ival = IOCPARM_IVAL(data);
2354 data = (caddr_t)&ival;
2359 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2360 if (*(int *)data & ~0x7f)
2365 case GIO_DEADKEYMAP:
2366 case PIO_DEADKEYMAP:
2371 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2372 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2373 case KBADDKBD: /* add/remove keyboard to/from mux */
2378 if ((kbd = vd->vd_keyboard) != NULL)
2379 error = kbdd_ioctl(kbd, cmd, data);
2381 if (error == ENOIOCTL) {
2382 if (cmd == KDGKBTYPE) {
2383 /* always return something? XXX */
2391 case KDGKBSTATE: { /* get keyboard state (locks) */
2394 if (vw == vd->vd_curwindow) {
2396 if ((kbd = vd->vd_keyboard) != NULL)
2397 error = vt_save_kbd_state(vw, kbd);
2404 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2408 case KDSKBSTATE: { /* set keyboard state (locks) */
2411 state = *(int *)data;
2412 if (state & ~LOCK_MASK)
2415 vw->vw_kbdstate &= ~LOCK_MASK;
2416 vw->vw_kbdstate |= state;
2419 if (vw == vd->vd_curwindow) {
2421 if ((kbd = vd->vd_keyboard) != NULL)
2422 error = vt_update_kbd_state(vw, kbd);
2428 case KDGETLED: { /* get keyboard LED status */
2431 if (vw == vd->vd_curwindow) {
2433 if ((kbd = vd->vd_keyboard) != NULL)
2434 error = vt_save_kbd_leds(vw, kbd);
2441 *(int *)data = vw->vw_kbdstate & LED_MASK;
2445 case KDSETLED: { /* set keyboard LED status */
2448 leds = *(int *)data;
2449 if (leds & ~LED_MASK)
2452 vw->vw_kbdstate &= ~LED_MASK;
2453 vw->vw_kbdstate |= leds;
2456 if (vw == vd->vd_curwindow) {
2458 if ((kbd = vd->vd_keyboard) != NULL)
2459 error = vt_update_kbd_leds(vw, kbd);
2466 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2467 KD_GRAPHICS : KD_TEXT;
2472 if (vw == vd->vd_curwindow) {
2474 if ((kbd = vd->vd_keyboard) != NULL)
2475 error = vt_save_kbd_mode(vw, kbd);
2482 *(int *)data = vw->vw_kbdmode;
2489 mode = *(int *)data;
2494 vw->vw_kbdmode = mode;
2497 if (vw == vd->vd_curwindow) {
2499 if ((kbd = vd->vd_keyboard) != NULL)
2500 error = vt_update_kbd_mode(vw, kbd);
2510 case FBIO_GETWINORG: /* get frame buffer window origin */
2511 case FBIO_GETDISPSTART: /* get display start address */
2512 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2513 case FBIO_BLANK: /* blank display */
2514 if (vd->vd_driver->vd_fb_ioctl)
2515 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2517 case CONS_BLANKTIME:
2521 if (*(int *)data < 0)
2523 if (*(int *)data != vw->vw_buf.vb_history_size)
2524 vtbuf_sethistory_size(&vw->vw_buf, *(int *)data);
2527 vtbuf_clearhistory(&vw->vw_buf);
2529 * Invalidate the entire visible window; it is not guaranteed
2530 * that this operation will be immediately followed by a scroll
2531 * event, so it would otherwise be possible for prior artifacts
2532 * to remain visible.
2535 if (vw == vd->vd_curwindow) {
2536 vd->vd_flags |= VDF_INVALID;
2537 vt_resume_flush_timer(vw, 0);
2543 *(int *)data = M_CG640x480;
2545 case CONS_BELLTYPE: /* set bell type sound */
2546 if ((*(int *)data) & CONS_QUIET_BELL)
2547 vd->vd_flags |= VDF_QUIET_BELL;
2549 vd->vd_flags &= ~VDF_QUIET_BELL;
2551 case CONS_GETINFO: {
2552 vid_info_t *vi = (vid_info_t *)data;
2553 if (vi->size != sizeof(struct vid_info))
2556 if (vw == vd->vd_curwindow) {
2558 if ((kbd = vd->vd_keyboard) != NULL)
2559 vt_save_kbd_state(vw, kbd);
2563 vi->m_num = vd->vd_curwindow->vw_number + 1;
2564 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2565 /* XXX: other fields! */
2569 *(int *)data = 0x200;
2574 case CONS_MOUSECTL: {
2575 mouse_info_t *mouse = (mouse_info_t*)data;
2578 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2579 * should not be applied to individual TTYs, but only to
2582 switch (mouse->operation) {
2584 if (vd->vd_flags & VDF_MOUSECURSOR) {
2585 vd->vd_flags &= ~VDF_MOUSECURSOR;
2586 #ifndef SC_NO_CUTPASTE
2587 vt_mouse_state(VT_MOUSE_HIDE);
2592 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2593 vd->vd_flags |= VDF_MOUSECURSOR;
2594 vd->vd_mx = vd->vd_width / 2;
2595 vd->vd_my = vd->vd_height / 2;
2596 #ifndef SC_NO_CUTPASTE
2597 vt_mouse_state(VT_MOUSE_SHOW);
2608 if (vd->vd_flags & VDF_TEXTMODE)
2611 error = vtfont_load((void *)data, &vf);
2615 error = vt_change_font(vw, vf);
2619 case PIO_VFONT_DEFAULT: {
2620 /* Reset to default font. */
2621 error = vt_change_font(vw, vt_font_assigned);
2625 scrmap_t *sm = (scrmap_t *)data;
2627 /* We don't have screen maps, so return a handcrafted one. */
2628 for (i = 0; i < 256; i++)
2634 * FIXME: This implementation is incomplete compared to
2637 switch (*(int *)data) {
2641 vw->vw_flags &= ~VWF_GRAPHICS;
2644 vw->vw_flags |= VWF_GRAPHICS;
2648 case KDENABIO: /* allow io operations */
2649 error = priv_check(td, PRIV_IO);
2652 error = securelevel_gt(td->td_ucred, 0);
2655 #if defined(__i386__)
2656 td->td_frame->tf_eflags |= PSL_IOPL;
2657 #elif defined(__amd64__)
2658 td->td_frame->tf_rflags |= PSL_IOPL;
2661 case KDDISABIO: /* disallow io operations (default) */
2662 #if defined(__i386__)
2663 td->td_frame->tf_eflags &= ~PSL_IOPL;
2664 #elif defined(__amd64__)
2665 td->td_frame->tf_rflags &= ~PSL_IOPL;
2668 case KDMKTONE: /* sound the bell */
2669 vtterm_beep(tm, *(u_int *)data);
2671 case KIOCSOUND: /* make tone (*data) hz */
2674 case CONS_SETKBD: /* set the new keyboard */
2677 if (vd->vd_keyboard == NULL ||
2678 vd->vd_keyboard->kb_index != *(int *)data) {
2679 kbd = kbd_get_keyboard(*(int *)data);
2684 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2685 (void *)vd, vt_kbdevent, vd);
2687 if ((kbd = vd->vd_keyboard) != NULL) {
2688 vt_save_kbd_state(vd->vd_curwindow, kbd);
2689 kbd_release(kbd, (void *)vd);
2691 kbd = vd->vd_keyboard = kbd_get_keyboard(i);
2693 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2694 vt_update_kbd_state(vd->vd_curwindow, kbd);
2696 error = EPERM; /* XXX */
2701 case CONS_RELKBD: /* release the current keyboard */
2704 if ((kbd = vd->vd_keyboard) != NULL) {
2705 vt_save_kbd_state(vd->vd_curwindow, kbd);
2706 error = kbd_release(kbd, (void *)vd);
2708 vd->vd_keyboard = NULL;
2715 win = *(int *)data - 1;
2716 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2718 if ((win >= VT_MAXWINDOWS) || (win < 0))
2720 return (vt_proc_window_switch(vd->vd_windows[win]));
2723 *(int *)data = vd->vd_curwindow->vw_number + 1;
2726 *(int *)data = vw->vw_number + 1;
2729 /* TODO: Check current state, switching can be in progress. */
2730 if ((*(int *)data) == 0x01)
2731 vw->vw_flags |= VWF_VTYLOCK;
2732 else if ((*(int *)data) == 0x02)
2733 vw->vw_flags &= ~VWF_VTYLOCK;
2739 for (i = 0; i < VT_MAXWINDOWS; i++) {
2740 vw = vd->vd_windows[i];
2743 if (!(vw->vw_flags & VWF_OPENED)) {
2744 *(int *)data = vw->vw_number + 1;
2751 case VT_WAITACTIVE: {
2756 idx = *(unsigned int *)data;
2757 if (idx > VT_MAXWINDOWS)
2760 vw = vd->vd_windows[idx - 1];
2763 while (vd->vd_curwindow != vw && error == 0)
2764 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2768 case VT_SETMODE: { /* set screen switcher mode */
2769 struct vt_mode *mode;
2772 mode = (struct vt_mode *)data;
2773 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2774 if (vw->vw_smode.mode == VT_PROCESS) {
2775 p1 = pfind(vw->vw_pid);
2776 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2779 DPRINTF(5, "error EPERM\n");
2785 if (mode->mode == VT_AUTO) {
2786 vw->vw_smode.mode = VT_AUTO;
2789 DPRINTF(5, "VT_AUTO, ");
2790 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2791 cnavailable(vw->vw_terminal->consdev, TRUE);
2792 /* were we in the middle of the vty switching process? */
2793 if (finish_vt_rel(vw, TRUE, &s) == 0)
2794 DPRINTF(5, "reset WAIT_REL, ");
2795 if (finish_vt_acq(vw) == 0)
2796 DPRINTF(5, "reset WAIT_ACQ, ");
2798 } else if (mode->mode == VT_PROCESS) {
2799 if (!ISSIGVALID(mode->relsig) ||
2800 !ISSIGVALID(mode->acqsig) ||
2801 !ISSIGVALID(mode->frsig)) {
2802 DPRINTF(5, "error EINVAL\n");
2805 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2806 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2807 vw->vw_proc = td->td_proc;
2808 vw->vw_pid = vw->vw_proc->p_pid;
2809 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2810 cnavailable(vw->vw_terminal->consdev, FALSE);
2812 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2819 case VT_GETMODE: /* get screen switcher mode */
2820 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2823 case VT_RELDISP: /* screen switcher ioctl */
2825 * This must be the current vty which is in the VT_PROCESS
2828 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2832 /* ...and this process is controlling it. */
2833 if (vw->vw_proc != td->td_proc) {
2837 switch(*(int *)data) {
2838 case VT_FALSE: /* user refuses to release screen, abort */
2839 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2840 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2841 SC_DRIVER_NAME, VT_UNIT(vw));
2843 case VT_TRUE: /* user has released screen, go on */
2844 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2845 if (vw->vw_flags & VWF_SWWAIT_REL) {
2846 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2847 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2848 SC_DRIVER_NAME, VT_UNIT(vw));
2853 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2854 if ((error = finish_vt_acq(vw)) == 0)
2855 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2856 SC_DRIVER_NAME, VT_UNIT(vw));
2867 static struct vt_window *
2868 vt_allocate_window(struct vt_device *vd, unsigned int window)
2870 struct vt_window *vw;
2871 struct terminal *tm;
2875 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2877 vw->vw_number = window;
2878 vw->vw_kbdmode = K_XLATE;
2880 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2881 vw->vw_font = vtfont_ref(vt_font_assigned);
2882 vt_compute_drawable_area(vw);
2885 vt_termsize(vd, vw->vw_font, &size);
2886 vt_winsize(vd, vw->vw_font, &wsz);
2887 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2888 vw->vw_buf.vb_terminal = tm; /* must be set before vtbuf_init() */
2889 vtbuf_init(&vw->vw_buf, &size);
2891 terminal_set_winsize(tm, &wsz);
2892 vd->vd_windows[window] = vw;
2893 callout_init(&vw->vw_proc_dead_timer, 0);
2899 vt_upgrade(struct vt_device *vd)
2901 struct vt_window *vw;
2903 int register_handlers;
2905 if (!vty_enabled(VTY_VT))
2907 if (main_vd->vd_driver == NULL)
2910 for (i = 0; i < VT_MAXWINDOWS; i++) {
2911 vw = vd->vd_windows[i];
2914 vw = vt_allocate_window(vd, i);
2916 if (!(vw->vw_flags & VWF_READY)) {
2917 callout_init(&vw->vw_proc_dead_timer, 0);
2918 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2919 vw->vw_flags |= VWF_READY;
2920 if (vw->vw_flags & VWF_CONSOLE) {
2921 /* For existing console window. */
2922 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2923 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2928 if (vd->vd_curwindow == NULL)
2929 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2931 register_handlers = 0;
2932 if (!(vd->vd_flags & VDF_ASYNC)) {
2933 /* Attach keyboard. */
2934 vt_allocate_keyboard(vd);
2936 /* Init 25 Hz timer. */
2937 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2940 * Start timer when everything ready.
2941 * Note that the operations here are purposefully ordered.
2942 * We need to ensure vd_timer_armed is non-zero before we set
2943 * the VDF_ASYNC flag. That prevents this function from
2944 * racing with vt_resume_flush_timer() to update the
2945 * callout structure.
2947 atomic_add_acq_int(&vd->vd_timer_armed, 1);
2948 vd->vd_flags |= VDF_ASYNC;
2949 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2950 register_handlers = 1;
2955 /* Refill settings with new sizes. */
2958 if (register_handlers) {
2959 /* Register suspend/resume handlers. */
2960 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2961 vd, EVENTHANDLER_PRI_ANY);
2962 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2963 EVENTHANDLER_PRI_ANY);
2968 vt_resize(struct vt_device *vd)
2970 struct vt_window *vw;
2973 for (i = 0; i < VT_MAXWINDOWS; i++) {
2974 vw = vd->vd_windows[i];
2976 /* Assign default font to window, if not textmode. */
2977 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2978 vw->vw_font = vtfont_ref(vt_font_assigned);
2981 /* Resize terminal windows */
2982 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2983 DPRINTF(100, "%s: vt_change_font() is busy, "
2984 "window %d\n", __func__, i);
2990 vt_replace_backend(const struct vt_driver *drv, void *softc)
2992 struct vt_device *vd;
2996 if (vd->vd_flags & VDF_ASYNC) {
2997 /* Stop vt_flush periodic task. */
2999 vt_suspend_flush_timer(vd);
3002 * Mute current terminal until we done. vt_change_font (called
3003 * from vt_resize) will unmute it.
3005 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
3009 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
3013 vd->vd_flags &= ~VDF_TEXTMODE;
3017 * We want to upgrade from the current driver to the
3021 vd->vd_prev_driver = vd->vd_driver;
3022 vd->vd_prev_softc = vd->vd_softc;
3023 vd->vd_driver = drv;
3024 vd->vd_softc = softc;
3026 vd->vd_driver->vd_init(vd);
3027 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
3029 * No driver given: we want to downgrade to the previous
3032 const struct vt_driver *old_drv;
3035 old_drv = vd->vd_driver;
3036 old_softc = vd->vd_softc;
3038 vd->vd_driver = vd->vd_prev_driver;
3039 vd->vd_softc = vd->vd_prev_softc;
3040 vd->vd_prev_driver = NULL;
3041 vd->vd_prev_softc = NULL;
3043 vd->vd_flags |= VDF_DOWNGRADE;
3045 vd->vd_driver->vd_init(vd);
3047 if (old_drv->vd_fini)
3048 old_drv->vd_fini(vd, old_softc);
3050 vd->vd_flags &= ~VDF_DOWNGRADE;
3055 /* Update windows sizes and initialize last items. */
3059 if (vd->vd_flags & VDF_SPLASH)
3063 if (vd->vd_flags & VDF_ASYNC) {
3064 /* Allow to put chars now. */
3065 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
3066 /* Rerun timer for screen updates. */
3067 vt_resume_flush_timer(vd->vd_curwindow, 0);
3071 * Register as console. If it already registered, cnadd() will ignore
3074 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
3078 vt_suspend_handler(void *priv)
3080 struct vt_device *vd;
3083 vd->vd_flags |= VDF_SUSPENDED;
3084 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
3085 vd->vd_driver->vd_suspend(vd);
3089 vt_resume_handler(void *priv)
3091 struct vt_device *vd;
3094 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
3095 vd->vd_driver->vd_resume(vd);
3096 vd->vd_flags &= ~VDF_SUSPENDED;
3100 vt_allocate(const struct vt_driver *drv, void *softc)
3103 if (!vty_enabled(VTY_VT))
3106 if (main_vd->vd_driver == NULL) {
3107 main_vd->vd_driver = drv;
3108 printf("VT: initialize with new VT driver \"%s\".\n",
3112 * Check if have rights to replace current driver. For example:
3113 * it is bad idea to replace KMS driver with generic VGA one.
3115 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
3116 printf("VT: Driver priority %d too low. Current %d\n ",
3117 drv->vd_priority, main_vd->vd_driver->vd_priority);
3120 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
3121 main_vd->vd_driver->vd_name, drv->vd_name);
3124 vt_replace_backend(drv, softc);
3128 vt_deallocate(const struct vt_driver *drv, void *softc)
3131 if (!vty_enabled(VTY_VT))
3134 if (main_vd->vd_prev_driver == NULL ||
3135 main_vd->vd_driver != drv ||
3136 main_vd->vd_softc != softc)
3139 printf("VT: Switching back from \"%s\" to \"%s\".\n",
3140 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
3142 vt_replace_backend(NULL, NULL);
3146 vt_suspend(struct vt_device *vd)
3150 if (vt_suspendswitch == 0)
3152 /* Save current window. */
3153 vd->vd_savedwindow = vd->vd_curwindow;
3154 /* Ask holding process to free window and switch to console window */
3155 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
3157 /* Wait for the window switch to complete. */
3160 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
3161 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
3166 vt_resume(struct vt_device *vd)
3169 if (vt_suspendswitch == 0)
3171 /* Switch back to saved window, if any */
3172 vt_proc_window_switch(vd->vd_savedwindow);
3173 vd->vd_savedwindow = NULL;