2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, 2013 The FreeBSD Foundation
7 * This software was developed by Ed Schouten under sponsorship from the
10 * Portions of this software were developed by Oleksandr Rybalko
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_compat.h"
40 #include <sys/param.h>
41 #include <sys/consio.h>
42 #include <sys/eventhandler.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/power.h>
53 #include <sys/random.h>
54 #include <sys/reboot.h>
55 #include <sys/systm.h>
56 #include <sys/terminal.h>
58 #include <dev/kbd/kbdreg.h>
59 #include <dev/vt/vt.h>
61 #if defined(__i386__) || defined(__amd64__)
62 #include <machine/psl.h>
63 #include <machine/frame.h>
66 static tc_bell_t vtterm_bell;
67 static tc_cursor_t vtterm_cursor;
68 static tc_putchar_t vtterm_putchar;
69 static tc_fill_t vtterm_fill;
70 static tc_copy_t vtterm_copy;
71 static tc_param_t vtterm_param;
72 static tc_done_t vtterm_done;
74 static tc_cnprobe_t vtterm_cnprobe;
75 static tc_cngetc_t vtterm_cngetc;
77 static tc_cngrab_t vtterm_cngrab;
78 static tc_cnungrab_t vtterm_cnungrab;
80 static tc_opened_t vtterm_opened;
81 static tc_ioctl_t vtterm_ioctl;
82 static tc_mmap_t vtterm_mmap;
84 const struct terminal_class vt_termclass = {
85 .tc_bell = vtterm_bell,
86 .tc_cursor = vtterm_cursor,
87 .tc_putchar = vtterm_putchar,
88 .tc_fill = vtterm_fill,
89 .tc_copy = vtterm_copy,
90 .tc_param = vtterm_param,
91 .tc_done = vtterm_done,
93 .tc_cnprobe = vtterm_cnprobe,
94 .tc_cngetc = vtterm_cngetc,
96 .tc_cngrab = vtterm_cngrab,
97 .tc_cnungrab = vtterm_cnungrab,
99 .tc_opened = vtterm_opened,
100 .tc_ioctl = vtterm_ioctl,
101 .tc_mmap = vtterm_mmap,
105 * Use a constant timer of 25 Hz to redraw the screen.
107 * XXX: In theory we should only fire up the timer when there is really
108 * activity. Unfortunately we cannot always start timers. We really
109 * don't want to process kernel messages synchronously, because it
110 * really slows down the system.
112 #define VT_TIMERFREQ 25
114 /* Bell pitch/duration. */
115 #define VT_BELLDURATION ((5 * hz + 99) / 100)
116 #define VT_BELLPITCH 800
118 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
121 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
122 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
123 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
124 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
125 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
126 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
128 /* Allow to disable some keyboard combinations. */
129 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
130 "See kbdmap(5) to configure.");
131 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
132 "See kbdmap(5) to configure.");
133 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
134 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
135 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
136 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
137 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
138 "See kbdmap(5) to configure.");
140 /* Used internally, not a tunable. */
141 int vt_draw_logo_cpus;
142 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
143 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
144 "(0 = do not override)");
145 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
146 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
147 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
149 static unsigned int vt_unit = 0;
150 static MALLOC_DEFINE(M_VT, "vt", "vt device");
151 struct vt_device *main_vd = &vt_consdev;
154 extern unsigned int vt_logo_width;
155 extern unsigned int vt_logo_height;
156 extern unsigned int vt_logo_depth;
157 extern unsigned char vt_logo_image[];
159 #define vtterm_draw_cpu_logos(...) do {} while (0)
160 const unsigned int vt_logo_sprite_height;
164 extern struct vt_font vt_font_default;
165 #ifndef SC_NO_CUTPASTE
166 extern struct vt_mouse_cursor vt_default_mouse_pointer;
169 static int signal_vt_rel(struct vt_window *);
170 static int signal_vt_acq(struct vt_window *);
171 static int finish_vt_rel(struct vt_window *, int, int *);
172 static int finish_vt_acq(struct vt_window *);
173 static int vt_window_switch(struct vt_window *);
174 static int vt_late_window_switch(struct vt_window *);
175 static int vt_proc_alive(struct vt_window *);
176 static void vt_resize(struct vt_device *);
177 static void vt_update_static(void *);
178 #ifndef SC_NO_CUTPASTE
179 static void vt_mouse_paste(void);
181 static void vt_suspend_handler(void *priv);
182 static void vt_resume_handler(void *priv);
184 SET_DECLARE(vt_drv_set, struct vt_driver);
186 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT))
187 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH))
189 struct terminal vt_consterm;
190 static struct vt_window vt_conswindow;
191 struct vt_device vt_consdev = {
194 .vd_prev_driver = NULL,
195 .vd_prev_softc = NULL,
196 .vd_flags = VDF_INVALID,
197 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
198 .vd_curwindow = &vt_conswindow,
201 #ifndef SC_NO_CUTPASTE
207 .vd_mcursor = &vt_default_mouse_pointer,
208 .vd_mcursor_fg = TC_WHITE,
209 .vd_mcursor_bg = TC_BLACK,
212 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
213 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
214 static struct vt_window vt_conswindow = {
215 .vw_number = VT_CONSWINDOW,
216 .vw_flags = VWF_CONSOLE,
218 .vb_buffer = &vt_constextbuf[0],
219 .vb_rows = &vt_constextbufrows[0],
220 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
223 .vb_flags = VBF_STATIC,
224 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
225 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
231 .vw_device = &vt_consdev,
232 .vw_terminal = &vt_consterm,
233 .vw_kbdmode = K_XLATE,
236 struct terminal vt_consterm = {
237 .tm_class = &vt_termclass,
238 .tm_softc = &vt_conswindow,
241 static struct consdev vt_consterm_consdev = {
242 .cn_ops = &termcn_cnops,
243 .cn_arg = &vt_consterm,
247 /* Add to set of consoles. */
248 DATA_SET(cons_set, vt_consterm_consdev);
251 * Right after kmem is done to allow early drivers to use locking and allocate
254 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
256 /* Delay until all devices attached, to not waste time. */
257 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
260 /* Initialize locks/mem depended members. */
262 vt_update_static(void *dummy)
265 if (!vty_enabled(VTY_VT))
267 if (main_vd->vd_driver != NULL)
268 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
269 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
270 main_vd->vd_width, main_vd->vd_height);
272 printf("VT: init without driver.\n");
274 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
275 cv_init(&main_vd->vd_winswitch, "vtwswt");
279 vt_schedule_flush(struct vt_device *vd, int ms)
283 /* Default to initial value. */
284 ms = 1000 / VT_TIMERFREQ;
286 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
290 vt_resume_flush_timer(struct vt_device *vd, int ms)
293 if (!(vd->vd_flags & VDF_ASYNC) ||
294 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
297 vt_schedule_flush(vd, ms);
301 vt_suspend_flush_timer(struct vt_device *vd)
304 * As long as this function is called locked, callout_stop()
305 * has the same effect like callout_drain() with regard to
306 * preventing the callback function from executing.
308 VT_LOCK_ASSERT(vd, MA_OWNED);
310 if (!(vd->vd_flags & VDF_ASYNC) ||
311 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
314 callout_stop(&vd->vd_timer);
318 vt_switch_timer(void *arg)
321 vt_late_window_switch((struct vt_window *)arg);
325 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
330 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
336 vw->vw_kbdmode = mode;
342 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
346 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
354 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
359 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
365 vw->vw_kbdstate &= ~LOCK_MASK;
366 vw->vw_kbdstate |= state & LOCK_MASK;
372 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
376 state = vw->vw_kbdstate & LOCK_MASK;
377 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
385 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
390 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
396 vw->vw_kbdstate &= ~LED_MASK;
397 vw->vw_kbdstate |= leds & LED_MASK;
403 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
407 leds = vw->vw_kbdstate & LED_MASK;
408 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
416 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
419 DPRINTF(40, "%s\n", __func__);
420 curvw->vw_switch_to = vw;
421 /* Set timer to allow switch in case when process hang. */
422 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
423 vt_switch_timer, (void *)vw);
424 /* Notify process about vt switch attempt. */
425 DPRINTF(30, "%s: Notify process.\n", __func__);
426 signal_vt_rel(curvw);
432 vt_window_postswitch(struct vt_window *vw)
439 /* vt_late_window_switch will done VT switching for regular case. */
441 vt_late_window_switch(struct vt_window *vw)
445 callout_stop(&vw->vw_proc_dead_timer);
447 ret = vt_window_switch(vw);
451 /* Notify owner process about terminal availability. */
452 if (vw->vw_smode.mode == VT_PROCESS) {
453 ret = vt_window_postswitch(vw);
460 vt_proc_window_switch(struct vt_window *vw)
462 struct vt_window *curvw;
463 struct vt_device *vd;
466 /* Prevent switching to NULL */
468 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
472 curvw = vd->vd_curwindow;
474 /* Check if virtual terminal is locked */
475 if (curvw->vw_flags & VWF_VTYLOCK)
478 /* Check if switch already in progress */
479 if (curvw->vw_flags & VWF_SWWAIT_REL) {
480 /* Check if switching to same window */
481 if (curvw->vw_switch_to == vw) {
482 DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
483 return (0); /* success */
485 DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
489 /* Avoid switching to already selected window */
491 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
492 return (0); /* success */
495 /* Ask current process permission to switch away. */
496 if (curvw->vw_smode.mode == VT_PROCESS) {
497 DPRINTF(30, "%s: VT_PROCESS ", __func__);
498 if (vt_proc_alive(curvw) == FALSE) {
499 DPRINTF(30, "Dead. Cleaning.");
502 DPRINTF(30, "%s: Signaling process.\n", __func__);
503 /* Alive, try to ask him. */
504 ret = vt_window_preswitch(vw, curvw);
505 /* Wait for process answer or timeout. */
511 ret = vt_late_window_switch(vw);
515 /* Switch window ignoring process locking. */
517 vt_window_switch(struct vt_window *vw)
519 struct vt_device *vd = vw->vw_device;
520 struct vt_window *curvw = vd->vd_curwindow;
525 * When grabbing the console for the debugger, avoid
526 * locks as that can result in deadlock. While this
527 * could use try locks, that wouldn't really make a
528 * difference as there are sufficient barriers in
529 * debugger entry/exit to be equivalent to
530 * successfully try-locking here.
534 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)))
537 vd->vd_curwindow = vw;
538 vd->vd_flags |= VDF_INVALID;
539 if (vd->vd_driver->vd_postswitch)
540 vd->vd_driver->vd_postswitch(vd);
550 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
555 vt_suspend_flush_timer(vd);
557 vd->vd_curwindow = vw;
558 vd->vd_flags |= VDF_INVALID;
559 cv_broadcast(&vd->vd_winswitch);
562 if (vd->vd_driver->vd_postswitch)
563 vd->vd_driver->vd_postswitch(vd);
565 vt_resume_flush_timer(vd, 0);
567 /* Restore per-window keyboard mode. */
569 kbd = kbd_get_keyboard(vd->vd_keyboard);
571 if (curvw->vw_kbdmode == K_XLATE)
572 vt_save_kbd_state(curvw, kbd);
574 vt_update_kbd_mode(vw, kbd);
575 vt_update_kbd_state(vw, kbd);
578 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
584 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
587 size->tp_row = vd->vd_height;
588 if (vt_draw_logo_cpus)
589 size->tp_row -= vt_logo_sprite_height;
590 size->tp_col = vd->vd_width;
592 size->tp_row /= vf->vf_height;
593 size->tp_col /= vf->vf_width;
598 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
601 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
602 if (vt_draw_logo_cpus)
603 rect->tr_begin.tp_row = vt_logo_sprite_height;
605 rect->tr_end.tp_row = vd->vd_height;
606 rect->tr_end.tp_col = vd->vd_width;
609 rect->tr_begin.tp_row =
610 howmany(rect->tr_begin.tp_row, vf->vf_height);
612 rect->tr_end.tp_row /= vf->vf_height;
613 rect->tr_end.tp_col /= vf->vf_width;
618 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
621 size->ws_ypixel = vd->vd_height;
622 if (vt_draw_logo_cpus)
623 size->ws_ypixel -= vt_logo_sprite_height;
624 size->ws_row = size->ws_ypixel;
625 size->ws_col = size->ws_xpixel = vd->vd_width;
627 size->ws_row /= vf->vf_height;
628 size->ws_col /= vf->vf_width;
633 vt_compute_drawable_area(struct vt_window *vw)
635 struct vt_device *vd;
641 if (vw->vw_font == NULL) {
642 vw->vw_draw_area.tr_begin.tp_col = 0;
643 vw->vw_draw_area.tr_begin.tp_row = 0;
644 if (vt_draw_logo_cpus)
645 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
646 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
647 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
654 * Compute the drawable area, so that the text is centered on
658 height = vd->vd_height;
659 if (vt_draw_logo_cpus)
660 height -= vt_logo_sprite_height;
661 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
662 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
663 if (vt_draw_logo_cpus)
664 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
665 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
666 rounddown(vd->vd_width, vf->vf_width);
667 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
668 rounddown(height, vf->vf_height);
672 vt_scroll(struct vt_window *vw, int offset, int whence)
677 if ((vw->vw_flags & VWF_SCROLL) == 0)
680 vt_termsize(vw->vw_device, vw->vw_font, &size);
682 diff = vthistory_seek(&vw->vw_buf, offset, whence);
684 vw->vw_device->vd_flags |= VDF_INVALID;
685 vt_resume_flush_timer(vw->vw_device, 0);
689 vt_machine_kbdevent(int c)
693 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
695 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
697 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
699 shutdown_nice(RB_HALT);
701 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
702 #ifndef SC_NO_CUTPASTE
703 /* Insert text from cut-paste buffer. */
707 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
709 shutdown_nice(RB_HALT|RB_POWEROFF);
711 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
713 * Request to immediate panic if sysctl
714 * kern.vt.enable_panic_key allow it.
717 panic("Forced by the panic key");
719 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
721 shutdown_nice(RB_AUTOBOOT);
723 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
724 /* Force activatation/deactivation of the screen saver. */
727 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
728 /* Put machine into Stand-By mode. */
729 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
731 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
732 /* Suspend machine. */
733 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
741 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
743 struct vt_device *vd;
747 /* Only special keys handled in ScrollLock mode */
748 if ((c & SPCLKEY) == 0)
754 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
755 vw = vd->vd_windows[c - F_SCR];
756 vt_proc_window_switch(vw);
764 /* Turn scrolling off. */
765 vt_scroll(vw, 0, VHS_END);
766 VTBUF_SLCK_DISABLE(&vw->vw_buf);
767 vw->vw_flags &= ~VWF_SCROLL;
770 case FKEY | F(49): /* Home key. */
771 vt_scroll(vw, 0, VHS_SET);
773 case FKEY | F(50): /* Arrow up. */
774 vt_scroll(vw, -1, VHS_CUR);
776 case FKEY | F(51): /* Page up. */
777 vt_termsize(vd, vw->vw_font, &size);
778 vt_scroll(vw, -size.tp_row, VHS_CUR);
780 case FKEY | F(57): /* End key. */
781 vt_scroll(vw, 0, VHS_END);
783 case FKEY | F(58): /* Arrow down. */
784 vt_scroll(vw, 1, VHS_CUR);
786 case FKEY | F(59): /* Page down. */
787 vt_termsize(vd, vw->vw_font, &size);
788 vt_scroll(vw, size.tp_row, VHS_CUR);
797 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
799 struct vt_window *vw = vd->vd_curwindow;
801 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
802 #if VT_ALT_TO_ESC_HACK
804 switch (c & ~RELKEY) {
805 case (SPCLKEY | RALT):
806 if (vt_enable_altgr != 0)
808 case (SPCLKEY | LALT):
809 vd->vd_kbstate &= ~ALKED;
811 /* Other keys ignored for RELKEY event. */
814 switch (c & ~RELKEY) {
815 case (SPCLKEY | RALT):
816 if (vt_enable_altgr != 0)
818 case (SPCLKEY | LALT):
819 vd->vd_kbstate |= ALKED;
824 /* Other keys ignored for RELKEY event. */
828 if (vt_machine_kbdevent(c))
831 if (vw->vw_flags & VWF_SCROLL) {
832 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
833 /* Scroll mode keys handled, nothing to do more. */
840 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
841 vw = vd->vd_windows[c - F_SCR];
842 vt_proc_window_switch(vw);
848 /* Switch to next VT. */
849 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
850 vw = vd->vd_windows[c];
851 vt_proc_window_switch(vw);
854 /* Switch to previous VT. */
855 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
856 vw = vd->vd_windows[c];
857 vt_proc_window_switch(vw);
860 vt_save_kbd_state(vw, kbd);
862 if (vw->vw_kbdstate & SLKED) {
863 /* Turn scrolling on. */
864 vw->vw_flags |= VWF_SCROLL;
865 VTBUF_SLCK_ENABLE(&vw->vw_buf);
867 /* Turn scrolling off. */
868 vw->vw_flags &= ~VWF_SCROLL;
869 VTBUF_SLCK_DISABLE(&vw->vw_buf);
870 vt_scroll(vw, 0, VHS_END);
875 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
876 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
877 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
878 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
879 /* F1 through F12 keys. */
880 terminal_input_special(vw->vw_terminal,
881 TKEY_F1 + c - (FKEY | F(1)));
883 case FKEY | F(49): /* Home key. */
884 terminal_input_special(vw->vw_terminal, TKEY_HOME);
886 case FKEY | F(50): /* Arrow up. */
887 terminal_input_special(vw->vw_terminal, TKEY_UP);
889 case FKEY | F(51): /* Page up. */
890 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
892 case FKEY | F(53): /* Arrow left. */
893 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
895 case FKEY | F(55): /* Arrow right. */
896 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
898 case FKEY | F(57): /* End key. */
899 terminal_input_special(vw->vw_terminal, TKEY_END);
901 case FKEY | F(58): /* Arrow down. */
902 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
904 case FKEY | F(59): /* Page down. */
905 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
907 case FKEY | F(60): /* Insert key. */
908 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
910 case FKEY | F(61): /* Delete key. */
911 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
914 } else if (KEYFLAGS(c) == 0) {
915 /* Don't do UTF-8 conversion when doing raw mode. */
916 if (vw->vw_kbdmode == K_XLATE) {
917 #if VT_ALT_TO_ESC_HACK
918 if (vd->vd_kbstate & ALKED) {
920 * Prepend ESC sequence if one of ALT keys down.
922 terminal_input_char(vw->vw_terminal, 0x1b);
926 kdb_alt_break(c, &vd->vd_altbrk);
928 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
930 terminal_input_raw(vw->vw_terminal, c);
936 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
938 struct vt_device *vd = arg;
944 case KBDIO_UNLOADING:
946 vd->vd_keyboard = -1;
947 kbd_release(kbd, (void *)vd);
954 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
955 vt_processkey(kbd, vd, c);
961 vt_allocate_keyboard(struct vt_device *vd)
967 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
969 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
970 k0 = kbd_get_keyboard(idx0);
972 for (idx = kbd_find_keyboard2("*", -1, 0);
974 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
975 k = kbd_get_keyboard(idx);
977 if (idx == idx0 || KBD_IS_BUSY(k))
980 bzero(&ki, sizeof(ki));
981 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
982 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
983 ki.kb_unit = k->kb_unit;
985 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
988 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
989 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
991 DPRINTF(10, "%s: No keyboard found.\n", __func__);
995 vd->vd_keyboard = idx0;
996 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
1002 vtterm_bell(struct terminal *tm)
1004 struct vt_window *vw = tm->tm_softc;
1005 struct vt_device *vd = vw->vw_device;
1007 if (!vt_enable_bell)
1010 if (vd->vd_flags & VDF_QUIET_BELL)
1013 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
1017 vtterm_beep(struct terminal *tm, u_int param)
1021 if (!vt_enable_bell)
1024 if ((param == 0) || ((param & 0xffff) == 0)) {
1029 period = ((param >> 16) & 0xffff) * hz / 1000;
1030 freq = 1193182 / (param & 0xffff);
1032 sysbeep(freq, period);
1036 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1038 struct vt_window *vw = tm->tm_softc;
1040 vtbuf_cursor_position(&vw->vw_buf, p);
1041 vt_resume_flush_timer(vw->vw_device, 0);
1045 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1047 struct vt_window *vw = tm->tm_softc;
1049 vtbuf_putchar(&vw->vw_buf, p, c);
1050 vt_resume_flush_timer(vw->vw_device, 0);
1054 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1056 struct vt_window *vw = tm->tm_softc;
1058 vtbuf_fill_locked(&vw->vw_buf, r, c);
1059 vt_resume_flush_timer(vw->vw_device, 0);
1063 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1064 const term_pos_t *p)
1066 struct vt_window *vw = tm->tm_softc;
1068 vtbuf_copy(&vw->vw_buf, r, p);
1069 vt_resume_flush_timer(vw->vw_device, 0);
1073 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1075 struct vt_window *vw = tm->tm_softc;
1078 case TP_SETLOCALCURSOR:
1080 * 0 means normal (usually block), 1 means hidden, and
1081 * 2 means blinking (always block) for compatibility with
1082 * syscons. We don't support any changes except hiding,
1083 * so must map 2 to 0.
1085 arg = (arg == 1) ? 0 : 1;
1088 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1089 vt_resume_flush_timer(vw->vw_device, 0);
1092 vw->vw_mouse_level = arg;
1098 vt_determine_colors(term_char_t c, int cursor,
1099 term_color_t *fg, term_color_t *bg)
1106 *fg = TCHAR_FGCOLOR(c);
1107 if (TCHAR_FORMAT(c) & TF_BOLD)
1108 *fg = TCOLOR_LIGHT(*fg);
1109 *bg = TCHAR_BGCOLOR(c);
1110 if (TCHAR_FORMAT(c) & TF_BLINK)
1111 *bg = TCOLOR_LIGHT(*bg);
1113 if (TCHAR_FORMAT(c) & TF_REVERSE)
1125 #ifndef SC_NO_CUTPASTE
1127 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1129 unsigned int mx, my;
1132 * We use the cursor position saved during the current refresh,
1133 * in case the cursor moved since.
1135 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1136 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1138 if (mx >= area->tr_end.tp_col ||
1139 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1140 my >= area->tr_end.tp_row ||
1141 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1147 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1150 struct vt_window *vw;
1154 vw = vd->vd_curwindow;
1157 x = vd->vd_mx_drawn;
1158 y = vd->vd_my_drawn;
1161 area.tr_begin.tp_col = x / vf->vf_width;
1162 area.tr_begin.tp_row = y / vf->vf_height;
1163 area.tr_end.tp_col =
1164 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1165 area.tr_end.tp_row =
1166 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1169 * No font loaded (ie. vt_vga operating in textmode).
1171 * FIXME: This fake area needs to be revisited once the
1172 * mouse cursor is supported in vt_vga's textmode.
1174 area.tr_begin.tp_col = x;
1175 area.tr_begin.tp_row = y;
1176 area.tr_end.tp_col = x + 2;
1177 area.tr_end.tp_row = y + 2;
1180 vtbuf_dirty(&vw->vw_buf, &area);
1185 vt_set_border(struct vt_device *vd, const term_rect_t *area,
1186 const term_color_t c)
1188 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
1190 if (drawrect == NULL)
1194 if (area->tr_begin.tp_row > 0)
1195 drawrect(vd, 0, 0, vd->vd_width - 1,
1196 area->tr_begin.tp_row - 1, 1, c);
1199 if (area->tr_begin.tp_col > 0)
1200 drawrect(vd, 0, area->tr_begin.tp_row,
1201 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
1204 if (area->tr_end.tp_col < vd->vd_width)
1205 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
1206 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
1209 if (area->tr_end.tp_row < vd->vd_height)
1210 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
1211 vd->vd_height - 1, 1, c);
1215 vt_flush(struct vt_device *vd)
1217 struct vt_window *vw;
1220 #ifndef SC_NO_CUTPASTE
1221 int cursor_was_shown, cursor_moved;
1224 vw = vd->vd_curwindow;
1228 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1232 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1235 #ifndef SC_NO_CUTPASTE
1236 cursor_was_shown = vd->vd_mshown;
1237 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1238 vd->vd_my != vd->vd_my_drawn);
1240 /* Check if the cursor should be displayed or not. */
1241 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1242 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1243 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1250 * If the cursor changed display state or moved, we must mark
1251 * the old position as dirty, so that it's erased.
1253 if (cursor_was_shown != vd->vd_mshown ||
1254 (vd->vd_mshown && cursor_moved))
1255 vt_mark_mouse_position_as_dirty(vd);
1258 * Save position of the mouse cursor. It's used by backends to
1259 * know where to draw the cursor and during the next refresh to
1260 * erase the previous position.
1262 vd->vd_mx_drawn = vd->vd_mx;
1263 vd->vd_my_drawn = vd->vd_my;
1266 * If the cursor is displayed and has moved since last refresh,
1267 * mark the new position as dirty.
1269 if (vd->vd_mshown && cursor_moved)
1270 vt_mark_mouse_position_as_dirty(vd);
1273 vtbuf_undirty(&vw->vw_buf, &tarea);
1275 /* Force a full redraw when the screen contents are invalid. */
1276 if (vd->vd_flags & VDF_INVALID) {
1277 vd->vd_flags &= ~VDF_INVALID;
1279 vt_set_border(vd, &vw->vw_draw_area, TC_BLACK);
1280 vt_termrect(vd, vf, &tarea);
1281 if (vt_draw_logo_cpus)
1282 vtterm_draw_cpu_logos(vd);
1285 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1286 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1296 struct vt_device *vd;
1300 /* Update screen if required. */
1301 changed = vt_flush(vd);
1303 /* Schedule for next update. */
1305 vt_schedule_flush(vd, 0);
1307 vd->vd_timer_armed = 0;
1311 vtterm_done(struct terminal *tm)
1313 struct vt_window *vw = tm->tm_softc;
1314 struct vt_device *vd = vw->vw_device;
1316 if (kdb_active || panicstr != NULL) {
1317 /* Switch to the debugger. */
1318 if (vd->vd_curwindow != vw) {
1319 vd->vd_curwindow = vw;
1320 vd->vd_flags |= VDF_INVALID;
1321 if (vd->vd_driver->vd_postswitch)
1322 vd->vd_driver->vd_postswitch(vd);
1324 vd->vd_flags &= ~VDF_SPLASH;
1326 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1333 vtterm_splash(struct vt_device *vd)
1335 vt_axis_t top, left;
1337 /* Display a nice boot splash. */
1338 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1340 top = (vd->vd_height - vt_logo_height) / 2;
1341 left = (vd->vd_width - vt_logo_width) / 2;
1342 switch (vt_logo_depth) {
1344 /* XXX: Unhardcode colors! */
1345 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1346 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1347 left, top, TC_WHITE, TC_BLACK);
1349 vd->vd_flags |= VDF_SPLASH;
1356 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1358 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1359 struct vt_window *vw = tm->tm_softc;
1360 struct vt_device *vd = vw->vw_device;
1365 if (!vty_enabled(VTY_VT))
1368 if (vd->vd_flags & VDF_INITIALIZED)
1369 /* Initialization already done. */
1372 SET_FOREACH(vtdlist, vt_drv_set) {
1374 if (vtd->vd_probe == NULL)
1376 if (vtd->vd_probe(vd) == CN_DEAD)
1378 if ((vtdbest == NULL) ||
1379 (vtd->vd_priority > vtdbest->vd_priority))
1382 if (vtdbest == NULL) {
1383 cp->cn_pri = CN_DEAD;
1384 vd->vd_flags |= VDF_DEAD;
1386 vd->vd_driver = vtdbest;
1387 cp->cn_pri = vd->vd_driver->vd_init(vd);
1390 /* Check if driver's vt_init return CN_DEAD. */
1391 if (cp->cn_pri == CN_DEAD) {
1392 vd->vd_flags |= VDF_DEAD;
1395 /* Initialize any early-boot keyboard drivers */
1396 kbd_configure(KB_CONF_PROBE_ONLY);
1398 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1399 vd->vd_windows[VT_CONSWINDOW] = vw;
1400 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1402 /* Attach default font if not in TEXTMODE. */
1403 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1404 vw->vw_font = vtfont_ref(&vt_font_default);
1405 vt_compute_drawable_area(vw);
1409 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1410 * that we have the real viewable size, fix it in the static
1413 if (vd->vd_width != 0 && vd->vd_height != 0)
1414 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1416 vtbuf_init_early(&vw->vw_buf);
1417 vt_winsize(vd, vw->vw_font, &wsz);
1418 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1420 attr.ta_format = TCHAR_FORMAT(c);
1421 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1422 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1423 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1425 if (vtdbest != NULL) {
1430 vd->vd_flags |= VDF_INITIALIZED;
1435 vtterm_cngetc(struct terminal *tm)
1437 struct vt_window *vw = tm->tm_softc;
1438 struct vt_device *vd = vw->vw_device;
1442 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1443 return (*vw->vw_kbdsq++);
1445 /* Make sure the splash screen is not there. */
1446 if (vd->vd_flags & VDF_SPLASH) {
1448 vd->vd_flags &= ~VDF_SPLASH;
1449 /* Mark screen as invalid to force update */
1450 vd->vd_flags |= VDF_INVALID;
1454 /* Stripped down keyboard handler. */
1455 kbd = kbd_get_keyboard(vd->vd_keyboard);
1459 /* Force keyboard input mode to K_XLATE */
1460 vw->vw_kbdmode = K_XLATE;
1461 vt_update_kbd_mode(vw, kbd);
1463 /* Switch the keyboard to polling to make it work here. */
1464 kbdd_poll(kbd, TRUE);
1465 c = kbdd_read_char(kbd, 0);
1466 kbdd_poll(kbd, FALSE);
1470 if (vw->vw_flags & VWF_SCROLL) {
1471 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1476 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1480 vt_save_kbd_state(vw, kbd);
1481 if (vw->vw_kbdstate & SLKED) {
1482 /* Turn scrolling on. */
1483 vw->vw_flags |= VWF_SCROLL;
1484 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1486 /* Turn scrolling off. */
1487 vt_scroll(vw, 0, VHS_END);
1488 vw->vw_flags &= ~VWF_SCROLL;
1489 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1492 /* XXX: KDB can handle history. */
1493 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1494 vw->vw_kbdsq = "\x1b[A";
1496 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1497 vw->vw_kbdsq = "\x1b[B";
1499 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1500 vw->vw_kbdsq = "\x1b[C";
1502 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1503 vw->vw_kbdsq = "\x1b[D";
1507 /* Force refresh to make scrollback work. */
1509 } else if (KEYFLAGS(c) == 0) {
1510 return (KEYCHAR(c));
1513 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1514 return (*vw->vw_kbdsq++);
1520 vtterm_cngrab(struct terminal *tm)
1522 struct vt_device *vd;
1523 struct vt_window *vw;
1530 vt_window_switch(vw);
1532 kbd = kbd_get_keyboard(vd->vd_keyboard);
1536 if (vw->vw_grabbed++ > 0)
1540 * Make sure the keyboard is accessible even when the kbd device
1541 * driver is disabled.
1545 /* We shall always use the keyboard in the XLATE mode here. */
1546 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1547 vw->vw_kbdmode = K_XLATE;
1548 vt_update_kbd_mode(vw, kbd);
1550 kbdd_poll(kbd, TRUE);
1554 vtterm_cnungrab(struct terminal *tm)
1556 struct vt_device *vd;
1557 struct vt_window *vw;
1563 kbd = kbd_get_keyboard(vd->vd_keyboard);
1567 if (--vw->vw_grabbed > 0)
1570 kbdd_poll(kbd, FALSE);
1572 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1573 vt_update_kbd_mode(vw, kbd);
1578 vtterm_opened(struct terminal *tm, int opened)
1580 struct vt_window *vw = tm->tm_softc;
1581 struct vt_device *vd = vw->vw_device;
1584 vd->vd_flags &= ~VDF_SPLASH;
1586 vw->vw_flags |= VWF_OPENED;
1588 vw->vw_flags &= ~VWF_OPENED;
1589 /* TODO: finish ACQ/REL */
1595 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1597 struct vt_device *vd = vw->vw_device;
1598 struct terminal *tm = vw->vw_terminal;
1605 * Changing fonts is a little tricky. We must prevent
1606 * simultaneous access to the device, so we must stop
1607 * the display timer and the terminal from accessing.
1608 * We need to switch fonts and grow our screen buffer.
1610 * XXX: Right now the code uses terminal_mute() to
1611 * prevent data from reaching the console driver while
1612 * resizing the screen buffer. This isn't elegant...
1616 if (vw->vw_flags & VWF_BUSY) {
1617 /* Another process is changing the font. */
1621 vw->vw_flags |= VWF_BUSY;
1624 vt_termsize(vd, vf, &size);
1625 vt_winsize(vd, vf, &wsz);
1627 /* Grow the screen buffer and terminal. */
1628 terminal_mute(tm, 1);
1629 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1630 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1631 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1632 terminal_mute(tm, 0);
1634 /* Actually apply the font to the current window. */
1636 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1638 * In case vt_change_font called to update size we don't need
1639 * to update font link.
1641 vtfont_unref(vw->vw_font);
1642 vw->vw_font = vtfont_ref(vf);
1646 * Compute the drawable area and move the mouse cursor inside
1647 * it, in case the new area is smaller than the previous one.
1649 vt_compute_drawable_area(vw);
1650 vd->vd_mx = min(vd->vd_mx,
1651 vw->vw_draw_area.tr_end.tp_col -
1652 vw->vw_draw_area.tr_begin.tp_col - 1);
1653 vd->vd_my = min(vd->vd_my,
1654 vw->vw_draw_area.tr_end.tp_row -
1655 vw->vw_draw_area.tr_begin.tp_row - 1);
1657 /* Force a full redraw the next timer tick. */
1658 if (vd->vd_curwindow == vw) {
1659 vd->vd_flags |= VDF_INVALID;
1660 vt_resume_flush_timer(vw->vw_device, 0);
1662 vw->vw_flags &= ~VWF_BUSY;
1668 vt_proc_alive(struct vt_window *vw)
1672 if (vw->vw_smode.mode != VT_PROCESS)
1676 if ((p = pfind(vw->vw_pid)) != NULL)
1678 if (vw->vw_proc == p)
1681 vw->vw_smode.mode = VT_AUTO;
1682 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1689 signal_vt_rel(struct vt_window *vw)
1692 if (vw->vw_smode.mode != VT_PROCESS)
1694 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1699 vw->vw_flags |= VWF_SWWAIT_REL;
1700 PROC_LOCK(vw->vw_proc);
1701 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1702 PROC_UNLOCK(vw->vw_proc);
1703 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1708 signal_vt_acq(struct vt_window *vw)
1711 if (vw->vw_smode.mode != VT_PROCESS)
1713 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1714 cnavailable(vw->vw_terminal->consdev, FALSE);
1715 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1720 vw->vw_flags |= VWF_SWWAIT_ACQ;
1721 PROC_LOCK(vw->vw_proc);
1722 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1723 PROC_UNLOCK(vw->vw_proc);
1724 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1729 finish_vt_rel(struct vt_window *vw, int release, int *s)
1732 if (vw->vw_flags & VWF_SWWAIT_REL) {
1733 vw->vw_flags &= ~VWF_SWWAIT_REL;
1735 callout_drain(&vw->vw_proc_dead_timer);
1736 vt_late_window_switch(vw->vw_switch_to);
1744 finish_vt_acq(struct vt_window *vw)
1747 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1748 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1754 #ifndef SC_NO_CUTPASTE
1756 vt_mouse_terminput_button(struct vt_device *vd, int button)
1758 struct vt_window *vw;
1760 char mouseb[6] = "\x1B[M";
1763 vw = vd->vd_curwindow;
1766 /* Translate to char position. */
1767 x = vd->vd_mx / vf->vf_width;
1768 y = vd->vd_my / vf->vf_height;
1769 /* Avoid overflow. */
1770 x = MIN(x, 255 - '!');
1771 y = MIN(y, 255 - '!');
1773 mouseb[3] = ' ' + button;
1774 mouseb[4] = '!' + x;
1775 mouseb[5] = '!' + y;
1777 for (i = 0; i < sizeof(mouseb); i++)
1778 terminal_input_char(vw->vw_terminal, mouseb[i]);
1782 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1787 case MOUSE_BUTTON_EVENT:
1789 /* Mouse button pressed. */
1790 if (event & MOUSE_BUTTON1DOWN)
1791 vt_mouse_terminput_button(vd, 0);
1792 if (event & MOUSE_BUTTON2DOWN)
1793 vt_mouse_terminput_button(vd, 1);
1794 if (event & MOUSE_BUTTON3DOWN)
1795 vt_mouse_terminput_button(vd, 2);
1797 /* Mouse button released. */
1798 vt_mouse_terminput_button(vd, 3);
1802 case MOUSE_MOTION_EVENT:
1803 if (mouse->u.data.z < 0) {
1805 sc_mouse_input_button(vd, 64);
1806 } else if (mouse->u.data.z > 0) {
1808 sc_mouse_input_button(vd, 65);
1821 len = VD_PASTEBUFLEN(main_vd);
1822 buf = VD_PASTEBUF(main_vd);
1823 len /= sizeof(term_char_t);
1824 for (i = 0; i < len; i++) {
1827 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1833 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1835 struct vt_device *vd;
1836 struct vt_window *vw;
1842 vw = vd->vd_curwindow;
1846 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1848 * Either the mouse is disabled, or the window is in
1849 * "graphics mode". The graphics mode is usually set by
1850 * an X server, using the KDSETMODE ioctl.
1854 if (vf == NULL) /* Text mode. */
1858 * TODO: add flag about pointer position changed, to not redraw chars
1859 * under mouse pointer when nothing changed.
1862 if (vw->vw_mouse_level > 0)
1863 vt_mouse_terminput(vd, type, x, y, event, cnt);
1867 case MOUSE_MOTION_EVENT:
1872 vt_termsize(vd, vf, &size);
1877 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1878 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1882 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1883 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1884 vd->vd_mx / vf->vf_width,
1885 vd->vd_my / vf->vf_height);
1887 vt_resume_flush_timer(vw->vw_device, 0);
1889 case MOUSE_BUTTON_EVENT:
1897 case MOUSE_BUTTON1DOWN:
1900 mark = VTB_MARK_END;
1902 case 1: /* single click: start cut operation */
1903 mark = VTB_MARK_START;
1905 case 2: /* double click: cut a word */
1906 mark = VTB_MARK_WORD;
1908 case 3: /* triple click: cut a line */
1909 mark = VTB_MARK_ROW;
1913 case VT_MOUSE_PASTEBUTTON:
1922 case VT_MOUSE_EXTENDBUTTON:
1925 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1926 mark = VTB_MARK_EXTEND;
1931 mark = VTB_MARK_EXTEND;
1939 /* Save buttons state. */
1941 vd->vd_mstate |= event;
1943 vd->vd_mstate &= ~event;
1945 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1946 vd->vd_my / vf->vf_height) == 1) {
1948 * We have something marked to copy, so update pointer to
1949 * window with selection.
1951 vt_resume_flush_timer(vw->vw_device, 0);
1957 case VTB_MARK_EXTEND:
1960 /* Other types of mark do not require to copy data. */
1964 /* Get current selection size in bytes. */
1965 len = vtbuf_get_marked_len(&vw->vw_buf);
1969 /* Reallocate buffer only if old one is too small. */
1970 if (len > VD_PASTEBUFSZ(vd)) {
1971 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1973 /* Update buffer size. */
1974 VD_PASTEBUFSZ(vd) = len;
1976 /* Request copy/paste buffer data, no more than `len' */
1977 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1980 VD_PASTEBUFLEN(vd) = len;
1982 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1987 vt_mouse_state(int show)
1989 struct vt_device *vd;
1990 struct vt_window *vw;
1993 vw = vd->vd_curwindow;
1997 vw->vw_flags |= VWF_MOUSE_HIDE;
2000 vw->vw_flags &= ~VWF_MOUSE_HIDE;
2004 /* Mark mouse position as dirty. */
2005 vt_mark_mouse_position_as_dirty(vd);
2006 vt_resume_flush_timer(vw->vw_device, 0);
2011 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
2012 int nprot, vm_memattr_t *memattr)
2014 struct vt_window *vw = tm->tm_softc;
2015 struct vt_device *vd = vw->vw_device;
2017 if (vd->vd_driver->vd_fb_mmap)
2018 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2025 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2028 struct vt_window *vw = tm->tm_softc;
2029 struct vt_device *vd = vw->vw_device;
2032 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2033 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2044 cmd = VT_WAITACTIVE;
2070 ival = IOCPARM_IVAL(data);
2071 data = (caddr_t)&ival;
2076 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2077 if (*(int *)data & ~0x7f)
2082 case GIO_DEADKEYMAP:
2083 case PIO_DEADKEYMAP:
2088 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2089 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2090 case KBADDKBD: /* add/remove keyboard to/from mux */
2095 kbd = kbd_get_keyboard(vd->vd_keyboard);
2097 error = kbdd_ioctl(kbd, cmd, data);
2099 if (error == ENOIOCTL) {
2100 if (cmd == KDGKBTYPE) {
2101 /* always return something? XXX */
2109 case KDGKBSTATE: { /* get keyboard state (locks) */
2112 if (vw == vd->vd_curwindow) {
2114 kbd = kbd_get_keyboard(vd->vd_keyboard);
2116 error = vt_save_kbd_state(vw, kbd);
2123 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2127 case KDSKBSTATE: { /* set keyboard state (locks) */
2130 state = *(int *)data;
2131 if (state & ~LOCK_MASK)
2134 vw->vw_kbdstate &= ~LOCK_MASK;
2135 vw->vw_kbdstate |= state;
2138 if (vw == vd->vd_curwindow) {
2140 kbd = kbd_get_keyboard(vd->vd_keyboard);
2142 error = vt_update_kbd_state(vw, kbd);
2148 case KDGETLED: { /* get keyboard LED status */
2151 if (vw == vd->vd_curwindow) {
2153 kbd = kbd_get_keyboard(vd->vd_keyboard);
2155 error = vt_save_kbd_leds(vw, kbd);
2162 *(int *)data = vw->vw_kbdstate & LED_MASK;
2166 case KDSETLED: { /* set keyboard LED status */
2169 leds = *(int *)data;
2170 if (leds & ~LED_MASK)
2173 vw->vw_kbdstate &= ~LED_MASK;
2174 vw->vw_kbdstate |= leds;
2177 if (vw == vd->vd_curwindow) {
2179 kbd = kbd_get_keyboard(vd->vd_keyboard);
2181 error = vt_update_kbd_leds(vw, kbd);
2188 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2189 KD_GRAPHICS : KD_TEXT;
2194 if (vw == vd->vd_curwindow) {
2196 kbd = kbd_get_keyboard(vd->vd_keyboard);
2198 error = vt_save_kbd_mode(vw, kbd);
2205 *(int *)data = vw->vw_kbdmode;
2212 mode = *(int *)data;
2217 vw->vw_kbdmode = mode;
2220 if (vw == vd->vd_curwindow) {
2222 kbd = kbd_get_keyboard(vd->vd_keyboard);
2224 error = vt_update_kbd_mode(vw, kbd);
2234 case FBIO_GETWINORG: /* get frame buffer window origin */
2235 case FBIO_GETDISPSTART: /* get display start address */
2236 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2237 case FBIO_BLANK: /* blank display */
2238 if (vd->vd_driver->vd_fb_ioctl)
2239 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2241 case CONS_BLANKTIME:
2245 if (*(int *)data < 0)
2247 if (*(int *)data != vd->vd_curwindow->vw_buf.vb_history_size)
2248 vtbuf_sethistory_size(&vd->vd_curwindow->vw_buf,
2253 *(int *)data = M_CG640x480;
2255 case CONS_BELLTYPE: /* set bell type sound */
2256 if ((*(int *)data) & CONS_QUIET_BELL)
2257 vd->vd_flags |= VDF_QUIET_BELL;
2259 vd->vd_flags &= ~VDF_QUIET_BELL;
2261 case CONS_GETINFO: {
2262 vid_info_t *vi = (vid_info_t *)data;
2263 if (vi->size != sizeof(struct vid_info))
2266 if (vw == vd->vd_curwindow) {
2268 kbd = kbd_get_keyboard(vd->vd_keyboard);
2270 vt_save_kbd_state(vw, kbd);
2274 vi->m_num = vd->vd_curwindow->vw_number + 1;
2275 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2276 /* XXX: other fields! */
2280 *(int *)data = 0x200;
2285 case CONS_MOUSECTL: {
2286 mouse_info_t *mouse = (mouse_info_t*)data;
2289 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2290 * should not be applied to individual TTYs, but only to
2293 switch (mouse->operation) {
2295 if (vd->vd_flags & VDF_MOUSECURSOR) {
2296 vd->vd_flags &= ~VDF_MOUSECURSOR;
2297 #ifndef SC_NO_CUTPASTE
2298 vt_mouse_state(VT_MOUSE_HIDE);
2303 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2304 vd->vd_flags |= VDF_MOUSECURSOR;
2305 vd->vd_mx = vd->vd_width / 2;
2306 vd->vd_my = vd->vd_height / 2;
2307 #ifndef SC_NO_CUTPASTE
2308 vt_mouse_state(VT_MOUSE_SHOW);
2319 if (vd->vd_flags & VDF_TEXTMODE)
2322 error = vtfont_load((void *)data, &vf);
2326 error = vt_change_font(vw, vf);
2330 case PIO_VFONT_DEFAULT: {
2331 /* Reset to default font. */
2332 error = vt_change_font(vw, &vt_font_default);
2336 scrmap_t *sm = (scrmap_t *)data;
2338 /* We don't have screen maps, so return a handcrafted one. */
2339 for (i = 0; i < 256; i++)
2345 * FIXME: This implementation is incomplete compared to
2348 switch (*(int *)data) {
2352 vw->vw_flags &= ~VWF_GRAPHICS;
2355 vw->vw_flags |= VWF_GRAPHICS;
2359 case KDENABIO: /* allow io operations */
2360 error = priv_check(td, PRIV_IO);
2363 error = securelevel_gt(td->td_ucred, 0);
2366 #if defined(__i386__)
2367 td->td_frame->tf_eflags |= PSL_IOPL;
2368 #elif defined(__amd64__)
2369 td->td_frame->tf_rflags |= PSL_IOPL;
2372 case KDDISABIO: /* disallow io operations (default) */
2373 #if defined(__i386__)
2374 td->td_frame->tf_eflags &= ~PSL_IOPL;
2375 #elif defined(__amd64__)
2376 td->td_frame->tf_rflags &= ~PSL_IOPL;
2379 case KDMKTONE: /* sound the bell */
2380 vtterm_beep(tm, *(u_int *)data);
2382 case KIOCSOUND: /* make tone (*data) hz */
2385 case CONS_SETKBD: /* set the new keyboard */
2388 if (vd->vd_keyboard != *(int *)data) {
2389 kbd = kbd_get_keyboard(*(int *)data);
2394 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2395 (void *)vd, vt_kbdevent, vd);
2397 if (vd->vd_keyboard != -1) {
2398 kbd = kbd_get_keyboard(vd->vd_keyboard);
2399 vt_save_kbd_state(vd->vd_curwindow, kbd);
2400 kbd_release(kbd, (void *)vd);
2402 kbd = kbd_get_keyboard(i);
2403 vd->vd_keyboard = i;
2405 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2406 vt_update_kbd_state(vd->vd_curwindow, kbd);
2408 error = EPERM; /* XXX */
2413 case CONS_RELKBD: /* release the current keyboard */
2416 if (vd->vd_keyboard != -1) {
2417 kbd = kbd_get_keyboard(vd->vd_keyboard);
2422 vt_save_kbd_state(vd->vd_curwindow, kbd);
2423 error = kbd_release(kbd, (void *)vd);
2425 vd->vd_keyboard = -1;
2432 win = *(int *)data - 1;
2433 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2435 if ((win >= VT_MAXWINDOWS) || (win < 0))
2437 return (vt_proc_window_switch(vd->vd_windows[win]));
2440 *(int *)data = vd->vd_curwindow->vw_number + 1;
2443 *(int *)data = vw->vw_number + 1;
2446 /* TODO: Check current state, switching can be in progress. */
2447 if ((*(int *)data) == 0x01)
2448 vw->vw_flags |= VWF_VTYLOCK;
2449 else if ((*(int *)data) == 0x02)
2450 vw->vw_flags &= ~VWF_VTYLOCK;
2456 for (i = 0; i < VT_MAXWINDOWS; i++) {
2457 vw = vd->vd_windows[i];
2460 if (!(vw->vw_flags & VWF_OPENED)) {
2461 *(int *)data = vw->vw_number + 1;
2468 case VT_WAITACTIVE: {
2473 idx = *(unsigned int *)data;
2474 if (idx > VT_MAXWINDOWS)
2477 vw = vd->vd_windows[idx - 1];
2480 while (vd->vd_curwindow != vw && error == 0)
2481 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2485 case VT_SETMODE: { /* set screen switcher mode */
2486 struct vt_mode *mode;
2489 mode = (struct vt_mode *)data;
2490 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2491 if (vw->vw_smode.mode == VT_PROCESS) {
2492 p1 = pfind(vw->vw_pid);
2493 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2496 DPRINTF(5, "error EPERM\n");
2502 if (mode->mode == VT_AUTO) {
2503 vw->vw_smode.mode = VT_AUTO;
2506 DPRINTF(5, "VT_AUTO, ");
2507 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2508 cnavailable(vw->vw_terminal->consdev, TRUE);
2509 /* were we in the middle of the vty switching process? */
2510 if (finish_vt_rel(vw, TRUE, &s) == 0)
2511 DPRINTF(5, "reset WAIT_REL, ");
2512 if (finish_vt_acq(vw) == 0)
2513 DPRINTF(5, "reset WAIT_ACQ, ");
2515 } else if (mode->mode == VT_PROCESS) {
2516 if (!ISSIGVALID(mode->relsig) ||
2517 !ISSIGVALID(mode->acqsig) ||
2518 !ISSIGVALID(mode->frsig)) {
2519 DPRINTF(5, "error EINVAL\n");
2522 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2523 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2524 vw->vw_proc = td->td_proc;
2525 vw->vw_pid = vw->vw_proc->p_pid;
2526 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2527 cnavailable(vw->vw_terminal->consdev, FALSE);
2529 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2536 case VT_GETMODE: /* get screen switcher mode */
2537 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2540 case VT_RELDISP: /* screen switcher ioctl */
2542 * This must be the current vty which is in the VT_PROCESS
2545 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2549 /* ...and this process is controlling it. */
2550 if (vw->vw_proc != td->td_proc) {
2554 switch(*(int *)data) {
2555 case VT_FALSE: /* user refuses to release screen, abort */
2556 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2557 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2558 SC_DRIVER_NAME, VT_UNIT(vw));
2560 case VT_TRUE: /* user has released screen, go on */
2561 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2562 if (vw->vw_flags & VWF_SWWAIT_REL) {
2563 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2564 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2565 SC_DRIVER_NAME, VT_UNIT(vw));
2570 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2571 if ((error = finish_vt_acq(vw)) == 0)
2572 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2573 SC_DRIVER_NAME, VT_UNIT(vw));
2584 static struct vt_window *
2585 vt_allocate_window(struct vt_device *vd, unsigned int window)
2587 struct vt_window *vw;
2588 struct terminal *tm;
2592 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2594 vw->vw_number = window;
2595 vw->vw_kbdmode = K_XLATE;
2597 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2598 vw->vw_font = vtfont_ref(&vt_font_default);
2599 vt_compute_drawable_area(vw);
2602 vt_termsize(vd, vw->vw_font, &size);
2603 vt_winsize(vd, vw->vw_font, &wsz);
2604 vtbuf_init(&vw->vw_buf, &size);
2606 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2607 terminal_set_winsize(tm, &wsz);
2608 vd->vd_windows[window] = vw;
2609 callout_init(&vw->vw_proc_dead_timer, 0);
2615 vt_upgrade(struct vt_device *vd)
2617 struct vt_window *vw;
2619 int register_handlers;
2621 if (!vty_enabled(VTY_VT))
2623 if (main_vd->vd_driver == NULL)
2626 for (i = 0; i < VT_MAXWINDOWS; i++) {
2627 vw = vd->vd_windows[i];
2630 vw = vt_allocate_window(vd, i);
2632 if (!(vw->vw_flags & VWF_READY)) {
2633 callout_init(&vw->vw_proc_dead_timer, 0);
2634 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2635 vw->vw_flags |= VWF_READY;
2636 if (vw->vw_flags & VWF_CONSOLE) {
2637 /* For existing console window. */
2638 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2639 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2645 if (vd->vd_curwindow == NULL)
2646 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2648 register_handlers = 0;
2649 if (!(vd->vd_flags & VDF_ASYNC)) {
2650 /* Attach keyboard. */
2651 vt_allocate_keyboard(vd);
2653 /* Init 25 Hz timer. */
2654 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2657 * Start timer when everything ready.
2658 * Note that the operations here are purposefully ordered.
2659 * We need to ensure vd_timer_armed is non-zero before we set
2660 * the VDF_ASYNC flag. That prevents this function from
2661 * racing with vt_resume_flush_timer() to update the
2662 * callout structure.
2664 atomic_add_acq_int(&vd->vd_timer_armed, 1);
2665 vd->vd_flags |= VDF_ASYNC;
2666 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2667 register_handlers = 1;
2672 /* Refill settings with new sizes. */
2675 if (register_handlers) {
2676 /* Register suspend/resume handlers. */
2677 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2678 vd, EVENTHANDLER_PRI_ANY);
2679 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2680 EVENTHANDLER_PRI_ANY);
2685 vt_resize(struct vt_device *vd)
2687 struct vt_window *vw;
2690 for (i = 0; i < VT_MAXWINDOWS; i++) {
2691 vw = vd->vd_windows[i];
2693 /* Assign default font to window, if not textmode. */
2694 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2695 vw->vw_font = vtfont_ref(&vt_font_default);
2698 /* Resize terminal windows */
2699 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2700 DPRINTF(100, "%s: vt_change_font() is busy, "
2701 "window %d\n", __func__, i);
2707 vt_replace_backend(const struct vt_driver *drv, void *softc)
2709 struct vt_device *vd;
2713 if (vd->vd_flags & VDF_ASYNC) {
2714 /* Stop vt_flush periodic task. */
2716 vt_suspend_flush_timer(vd);
2719 * Mute current terminal until we done. vt_change_font (called
2720 * from vt_resize) will unmute it.
2722 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2726 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2730 vd->vd_flags &= ~VDF_TEXTMODE;
2734 * We want to upgrade from the current driver to the
2738 vd->vd_prev_driver = vd->vd_driver;
2739 vd->vd_prev_softc = vd->vd_softc;
2740 vd->vd_driver = drv;
2741 vd->vd_softc = softc;
2743 vd->vd_driver->vd_init(vd);
2744 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
2746 * No driver given: we want to downgrade to the previous
2749 const struct vt_driver *old_drv;
2752 old_drv = vd->vd_driver;
2753 old_softc = vd->vd_softc;
2755 vd->vd_driver = vd->vd_prev_driver;
2756 vd->vd_softc = vd->vd_prev_softc;
2757 vd->vd_prev_driver = NULL;
2758 vd->vd_prev_softc = NULL;
2760 vd->vd_flags |= VDF_DOWNGRADE;
2762 vd->vd_driver->vd_init(vd);
2764 if (old_drv->vd_fini)
2765 old_drv->vd_fini(vd, old_softc);
2767 vd->vd_flags &= ~VDF_DOWNGRADE;
2772 /* Update windows sizes and initialize last items. */
2776 if (vd->vd_flags & VDF_SPLASH)
2780 if (vd->vd_flags & VDF_ASYNC) {
2781 /* Allow to put chars now. */
2782 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2783 /* Rerun timer for screen updates. */
2784 vt_resume_flush_timer(vd, 0);
2788 * Register as console. If it already registered, cnadd() will ignore
2791 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2795 vt_suspend_handler(void *priv)
2797 struct vt_device *vd;
2800 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2801 vd->vd_driver->vd_suspend(vd);
2805 vt_resume_handler(void *priv)
2807 struct vt_device *vd;
2810 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2811 vd->vd_driver->vd_resume(vd);
2815 vt_allocate(const struct vt_driver *drv, void *softc)
2818 if (!vty_enabled(VTY_VT))
2821 if (main_vd->vd_driver == NULL) {
2822 main_vd->vd_driver = drv;
2823 printf("VT: initialize with new VT driver \"%s\".\n",
2827 * Check if have rights to replace current driver. For example:
2828 * it is bad idea to replace KMS driver with generic VGA one.
2830 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2831 printf("VT: Driver priority %d too low. Current %d\n ",
2832 drv->vd_priority, main_vd->vd_driver->vd_priority);
2835 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2836 main_vd->vd_driver->vd_name, drv->vd_name);
2839 vt_replace_backend(drv, softc);
2843 vt_deallocate(const struct vt_driver *drv, void *softc)
2846 if (!vty_enabled(VTY_VT))
2849 if (main_vd->vd_prev_driver == NULL ||
2850 main_vd->vd_driver != drv ||
2851 main_vd->vd_softc != softc)
2854 printf("VT: Switching back from \"%s\" to \"%s\".\n",
2855 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
2857 vt_replace_backend(NULL, NULL);
2861 vt_suspend(struct vt_device *vd)
2865 if (vt_suspendswitch == 0)
2867 /* Save current window. */
2868 vd->vd_savedwindow = vd->vd_curwindow;
2869 /* Ask holding process to free window and switch to console window */
2870 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2872 /* Wait for the window switch to complete. */
2875 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2876 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2881 vt_resume(struct vt_device *vd)
2884 if (vt_suspendswitch == 0)
2886 /* Switch back to saved window, if any */
2887 vt_proc_window_switch(vd->vd_savedwindow);
2888 vd->vd_savedwindow = NULL;