2 * Copyright (c) 2009, 2013 The FreeBSD Foundation
5 * This software was developed by Ed Schouten under sponsorship from the
8 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_compat.h"
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/power.h>
51 #include <sys/random.h>
52 #include <sys/reboot.h>
53 #include <sys/systm.h>
54 #include <sys/terminal.h>
56 #include <dev/kbd/kbdreg.h>
57 #include <dev/vt/vt.h>
59 #if defined(__i386__) || defined(__amd64__)
60 #include <machine/psl.h>
61 #include <machine/frame.h>
64 static tc_bell_t vtterm_bell;
65 static tc_cursor_t vtterm_cursor;
66 static tc_putchar_t vtterm_putchar;
67 static tc_fill_t vtterm_fill;
68 static tc_copy_t vtterm_copy;
69 static tc_param_t vtterm_param;
70 static tc_done_t vtterm_done;
72 static tc_cnprobe_t vtterm_cnprobe;
73 static tc_cngetc_t vtterm_cngetc;
75 static tc_cngrab_t vtterm_cngrab;
76 static tc_cnungrab_t vtterm_cnungrab;
78 static tc_opened_t vtterm_opened;
79 static tc_ioctl_t vtterm_ioctl;
80 static tc_mmap_t vtterm_mmap;
82 const struct terminal_class vt_termclass = {
83 .tc_bell = vtterm_bell,
84 .tc_cursor = vtterm_cursor,
85 .tc_putchar = vtterm_putchar,
86 .tc_fill = vtterm_fill,
87 .tc_copy = vtterm_copy,
88 .tc_param = vtterm_param,
89 .tc_done = vtterm_done,
91 .tc_cnprobe = vtterm_cnprobe,
92 .tc_cngetc = vtterm_cngetc,
94 .tc_cngrab = vtterm_cngrab,
95 .tc_cnungrab = vtterm_cnungrab,
97 .tc_opened = vtterm_opened,
98 .tc_ioctl = vtterm_ioctl,
99 .tc_mmap = vtterm_mmap,
103 * Use a constant timer of 25 Hz to redraw the screen.
105 * XXX: In theory we should only fire up the timer when there is really
106 * activity. Unfortunately we cannot always start timers. We really
107 * don't want to process kernel messages synchronously, because it
108 * really slows down the system.
110 #define VT_TIMERFREQ 25
112 /* Bell pitch/duration. */
113 #define VT_BELLDURATION ((5 * hz + 99) / 100)
114 #define VT_BELLPITCH 800
116 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
119 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
120 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
121 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
122 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
123 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
124 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
126 /* Allow to disable some keyboard combinations. */
127 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
128 "See kbdmap(5) to configure.");
129 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
130 "See kbdmap(5) to configure.");
131 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
132 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
133 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
134 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
135 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
136 "See kbdmap(5) to configure.");
138 /* Used internally, not a tunable. */
139 int vt_draw_logo_cpus;
140 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
141 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
142 "(0 = do not override)");
143 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
144 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
145 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
147 static unsigned int vt_unit = 0;
148 static MALLOC_DEFINE(M_VT, "vt", "vt device");
149 struct vt_device *main_vd = &vt_consdev;
152 extern unsigned int vt_logo_width;
153 extern unsigned int vt_logo_height;
154 extern unsigned int vt_logo_depth;
155 extern unsigned char vt_logo_image[];
157 #define vtterm_draw_cpu_logos(...) do {} while (0)
158 const unsigned int vt_logo_sprite_height;
162 extern struct vt_font vt_font_default;
163 #ifndef SC_NO_CUTPASTE
164 extern struct vt_mouse_cursor vt_default_mouse_pointer;
167 static int signal_vt_rel(struct vt_window *);
168 static int signal_vt_acq(struct vt_window *);
169 static int finish_vt_rel(struct vt_window *, int, int *);
170 static int finish_vt_acq(struct vt_window *);
171 static int vt_window_switch(struct vt_window *);
172 static int vt_late_window_switch(struct vt_window *);
173 static int vt_proc_alive(struct vt_window *);
174 static void vt_resize(struct vt_device *);
175 static void vt_update_static(void *);
176 #ifndef SC_NO_CUTPASTE
177 static void vt_mouse_paste(void);
179 static void vt_suspend_handler(void *priv);
180 static void vt_resume_handler(void *priv);
182 SET_DECLARE(vt_drv_set, struct vt_driver);
184 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT))
185 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH))
187 struct terminal vt_consterm;
188 static struct vt_window vt_conswindow;
189 struct vt_device vt_consdev = {
192 .vd_prev_driver = NULL,
193 .vd_prev_softc = NULL,
194 .vd_flags = VDF_INVALID,
195 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
196 .vd_curwindow = &vt_conswindow,
199 #ifndef SC_NO_CUTPASTE
205 .vd_mcursor = &vt_default_mouse_pointer,
206 .vd_mcursor_fg = TC_WHITE,
207 .vd_mcursor_bg = TC_BLACK,
210 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
211 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
212 static struct vt_window vt_conswindow = {
213 .vw_number = VT_CONSWINDOW,
214 .vw_flags = VWF_CONSOLE,
216 .vb_buffer = &vt_constextbuf[0],
217 .vb_rows = &vt_constextbufrows[0],
218 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
221 .vb_flags = VBF_STATIC,
222 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
223 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
229 .vw_device = &vt_consdev,
230 .vw_terminal = &vt_consterm,
231 .vw_kbdmode = K_XLATE,
234 struct terminal vt_consterm = {
235 .tm_class = &vt_termclass,
236 .tm_softc = &vt_conswindow,
239 static struct consdev vt_consterm_consdev = {
240 .cn_ops = &termcn_cnops,
241 .cn_arg = &vt_consterm,
245 /* Add to set of consoles. */
246 DATA_SET(cons_set, vt_consterm_consdev);
249 * Right after kmem is done to allow early drivers to use locking and allocate
252 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
254 /* Delay until all devices attached, to not waste time. */
255 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
258 /* Initialize locks/mem depended members. */
260 vt_update_static(void *dummy)
263 if (!vty_enabled(VTY_VT))
265 if (main_vd->vd_driver != NULL)
266 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
267 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
268 main_vd->vd_width, main_vd->vd_height);
270 printf("VT: init without driver.\n");
272 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
273 cv_init(&main_vd->vd_winswitch, "vtwswt");
277 vt_schedule_flush(struct vt_device *vd, int ms)
281 /* Default to initial value. */
282 ms = 1000 / VT_TIMERFREQ;
284 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
288 vt_resume_flush_timer(struct vt_device *vd, int ms)
291 if (!(vd->vd_flags & VDF_ASYNC) ||
292 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
295 vt_schedule_flush(vd, ms);
299 vt_suspend_flush_timer(struct vt_device *vd)
302 * As long as this function is called locked, callout_stop()
303 * has the same effect like callout_drain() with regard to
304 * preventing the callback function from executing.
306 VT_LOCK_ASSERT(vd, MA_OWNED);
308 if (!(vd->vd_flags & VDF_ASYNC) ||
309 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
312 callout_stop(&vd->vd_timer);
316 vt_switch_timer(void *arg)
319 vt_late_window_switch((struct vt_window *)arg);
323 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
328 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
334 vw->vw_kbdmode = mode;
340 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
344 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
352 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
357 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
363 vw->vw_kbdstate &= ~LOCK_MASK;
364 vw->vw_kbdstate |= state & LOCK_MASK;
370 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
374 state = vw->vw_kbdstate & LOCK_MASK;
375 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
383 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
388 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
394 vw->vw_kbdstate &= ~LED_MASK;
395 vw->vw_kbdstate |= leds & LED_MASK;
401 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
405 leds = vw->vw_kbdstate & LED_MASK;
406 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
414 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
417 DPRINTF(40, "%s\n", __func__);
418 curvw->vw_switch_to = vw;
419 /* Set timer to allow switch in case when process hang. */
420 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
421 vt_switch_timer, (void *)vw);
422 /* Notify process about vt switch attempt. */
423 DPRINTF(30, "%s: Notify process.\n", __func__);
424 signal_vt_rel(curvw);
430 vt_window_postswitch(struct vt_window *vw)
437 /* vt_late_window_switch will done VT switching for regular case. */
439 vt_late_window_switch(struct vt_window *vw)
443 callout_stop(&vw->vw_proc_dead_timer);
445 ret = vt_window_switch(vw);
449 /* Notify owner process about terminal availability. */
450 if (vw->vw_smode.mode == VT_PROCESS) {
451 ret = vt_window_postswitch(vw);
458 vt_proc_window_switch(struct vt_window *vw)
460 struct vt_window *curvw;
461 struct vt_device *vd;
464 /* Prevent switching to NULL */
466 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
470 curvw = vd->vd_curwindow;
472 /* Check if virtual terminal is locked */
473 if (curvw->vw_flags & VWF_VTYLOCK)
476 /* Check if switch already in progress */
477 if (curvw->vw_flags & VWF_SWWAIT_REL) {
478 /* Check if switching to same window */
479 if (curvw->vw_switch_to == vw) {
480 DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
481 return (0); /* success */
483 DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
487 /* Avoid switching to already selected window */
489 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
490 return (0); /* success */
493 /* Ask current process permission to switch away. */
494 if (curvw->vw_smode.mode == VT_PROCESS) {
495 DPRINTF(30, "%s: VT_PROCESS ", __func__);
496 if (vt_proc_alive(curvw) == FALSE) {
497 DPRINTF(30, "Dead. Cleaning.");
500 DPRINTF(30, "%s: Signaling process.\n", __func__);
501 /* Alive, try to ask him. */
502 ret = vt_window_preswitch(vw, curvw);
503 /* Wait for process answer or timeout. */
509 ret = vt_late_window_switch(vw);
513 /* Switch window ignoring process locking. */
515 vt_window_switch(struct vt_window *vw)
517 struct vt_device *vd = vw->vw_device;
518 struct vt_window *curvw = vd->vd_curwindow;
527 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
532 vt_suspend_flush_timer(vd);
534 vd->vd_curwindow = vw;
535 vd->vd_flags |= VDF_INVALID;
536 cv_broadcast(&vd->vd_winswitch);
539 if (vd->vd_driver->vd_postswitch)
540 vd->vd_driver->vd_postswitch(vd);
542 vt_resume_flush_timer(vd, 0);
544 /* Restore per-window keyboard mode. */
546 kbd = kbd_get_keyboard(vd->vd_keyboard);
548 if (curvw->vw_kbdmode == K_XLATE)
549 vt_save_kbd_state(curvw, kbd);
551 vt_update_kbd_mode(vw, kbd);
552 vt_update_kbd_state(vw, kbd);
555 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
561 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
564 size->tp_row = vd->vd_height;
565 if (vt_draw_logo_cpus)
566 size->tp_row -= vt_logo_sprite_height;
567 size->tp_col = vd->vd_width;
569 size->tp_row /= vf->vf_height;
570 size->tp_col /= vf->vf_width;
575 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
578 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
579 if (vt_draw_logo_cpus)
580 rect->tr_begin.tp_row = vt_logo_sprite_height;
582 rect->tr_end.tp_row = vd->vd_height;
583 rect->tr_end.tp_col = vd->vd_width;
586 rect->tr_begin.tp_row =
587 howmany(rect->tr_begin.tp_row, vf->vf_height);
589 rect->tr_end.tp_row /= vf->vf_height;
590 rect->tr_end.tp_col /= vf->vf_width;
595 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
598 size->ws_ypixel = vd->vd_height;
599 if (vt_draw_logo_cpus)
600 size->ws_ypixel -= vt_logo_sprite_height;
601 size->ws_row = size->ws_ypixel;
602 size->ws_col = size->ws_xpixel = vd->vd_width;
604 size->ws_row /= vf->vf_height;
605 size->ws_col /= vf->vf_width;
610 vt_compute_drawable_area(struct vt_window *vw)
612 struct vt_device *vd;
618 if (vw->vw_font == NULL) {
619 vw->vw_draw_area.tr_begin.tp_col = 0;
620 vw->vw_draw_area.tr_begin.tp_row = 0;
621 if (vt_draw_logo_cpus)
622 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
623 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
624 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
631 * Compute the drawable area, so that the text is centered on
635 height = vd->vd_height;
636 if (vt_draw_logo_cpus)
637 height -= vt_logo_sprite_height;
638 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
639 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
640 if (vt_draw_logo_cpus)
641 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
642 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
643 rounddown(vd->vd_width, vf->vf_width);
644 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
645 rounddown(height, vf->vf_height);
649 vt_scroll(struct vt_window *vw, int offset, int whence)
654 if ((vw->vw_flags & VWF_SCROLL) == 0)
657 vt_termsize(vw->vw_device, vw->vw_font, &size);
659 diff = vthistory_seek(&vw->vw_buf, offset, whence);
661 vw->vw_device->vd_flags |= VDF_INVALID;
662 vt_resume_flush_timer(vw->vw_device, 0);
666 vt_machine_kbdevent(int c)
670 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
672 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
674 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
676 shutdown_nice(RB_HALT);
678 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
679 #ifndef SC_NO_CUTPASTE
680 /* Insert text from cut-paste buffer. */
684 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
686 shutdown_nice(RB_HALT|RB_POWEROFF);
688 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
690 * Request to immediate panic if sysctl
691 * kern.vt.enable_panic_key allow it.
694 panic("Forced by the panic key");
696 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
698 shutdown_nice(RB_AUTOBOOT);
700 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
701 /* Force activatation/deactivation of the screen saver. */
704 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
705 /* Put machine into Stand-By mode. */
706 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
708 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
709 /* Suspend machine. */
710 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
718 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
720 struct vt_device *vd;
724 /* Only special keys handled in ScrollLock mode */
725 if ((c & SPCLKEY) == 0)
731 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
732 vw = vd->vd_windows[c - F_SCR];
733 vt_proc_window_switch(vw);
741 /* Turn scrolling off. */
742 vt_scroll(vw, 0, VHS_END);
743 VTBUF_SLCK_DISABLE(&vw->vw_buf);
744 vw->vw_flags &= ~VWF_SCROLL;
747 case FKEY | F(49): /* Home key. */
748 vt_scroll(vw, 0, VHS_SET);
750 case FKEY | F(50): /* Arrow up. */
751 vt_scroll(vw, -1, VHS_CUR);
753 case FKEY | F(51): /* Page up. */
754 vt_termsize(vd, vw->vw_font, &size);
755 vt_scroll(vw, -size.tp_row, VHS_CUR);
757 case FKEY | F(57): /* End key. */
758 vt_scroll(vw, 0, VHS_END);
760 case FKEY | F(58): /* Arrow down. */
761 vt_scroll(vw, 1, VHS_CUR);
763 case FKEY | F(59): /* Page down. */
764 vt_termsize(vd, vw->vw_font, &size);
765 vt_scroll(vw, size.tp_row, VHS_CUR);
774 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
776 struct vt_window *vw = vd->vd_curwindow;
778 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
779 #if VT_ALT_TO_ESC_HACK
781 switch (c & ~RELKEY) {
782 case (SPCLKEY | RALT):
783 if (vt_enable_altgr != 0)
785 case (SPCLKEY | LALT):
786 vd->vd_kbstate &= ~ALKED;
788 /* Other keys ignored for RELKEY event. */
791 switch (c & ~RELKEY) {
792 case (SPCLKEY | RALT):
793 if (vt_enable_altgr != 0)
795 case (SPCLKEY | LALT):
796 vd->vd_kbstate |= ALKED;
801 /* Other keys ignored for RELKEY event. */
805 if (vt_machine_kbdevent(c))
808 if (vw->vw_flags & VWF_SCROLL) {
809 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
810 /* Scroll mode keys handled, nothing to do more. */
817 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
818 vw = vd->vd_windows[c - F_SCR];
819 vt_proc_window_switch(vw);
825 /* Switch to next VT. */
826 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
827 vw = vd->vd_windows[c];
828 vt_proc_window_switch(vw);
831 /* Switch to previous VT. */
832 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
833 vw = vd->vd_windows[c];
834 vt_proc_window_switch(vw);
837 vt_save_kbd_state(vw, kbd);
839 if (vw->vw_kbdstate & SLKED) {
840 /* Turn scrolling on. */
841 vw->vw_flags |= VWF_SCROLL;
842 VTBUF_SLCK_ENABLE(&vw->vw_buf);
844 /* Turn scrolling off. */
845 vw->vw_flags &= ~VWF_SCROLL;
846 VTBUF_SLCK_DISABLE(&vw->vw_buf);
847 vt_scroll(vw, 0, VHS_END);
852 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
853 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
854 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
855 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
856 /* F1 through F12 keys. */
857 terminal_input_special(vw->vw_terminal,
858 TKEY_F1 + c - (FKEY | F(1)));
860 case FKEY | F(49): /* Home key. */
861 terminal_input_special(vw->vw_terminal, TKEY_HOME);
863 case FKEY | F(50): /* Arrow up. */
864 terminal_input_special(vw->vw_terminal, TKEY_UP);
866 case FKEY | F(51): /* Page up. */
867 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
869 case FKEY | F(53): /* Arrow left. */
870 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
872 case FKEY | F(55): /* Arrow right. */
873 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
875 case FKEY | F(57): /* End key. */
876 terminal_input_special(vw->vw_terminal, TKEY_END);
878 case FKEY | F(58): /* Arrow down. */
879 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
881 case FKEY | F(59): /* Page down. */
882 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
884 case FKEY | F(60): /* Insert key. */
885 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
887 case FKEY | F(61): /* Delete key. */
888 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
891 } else if (KEYFLAGS(c) == 0) {
892 /* Don't do UTF-8 conversion when doing raw mode. */
893 if (vw->vw_kbdmode == K_XLATE) {
894 #if VT_ALT_TO_ESC_HACK
895 if (vd->vd_kbstate & ALKED) {
897 * Prepend ESC sequence if one of ALT keys down.
899 terminal_input_char(vw->vw_terminal, 0x1b);
903 kdb_alt_break(c, &vd->vd_altbrk);
905 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
907 terminal_input_raw(vw->vw_terminal, c);
913 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
915 struct vt_device *vd = arg;
921 case KBDIO_UNLOADING:
923 vd->vd_keyboard = -1;
924 kbd_release(kbd, (void *)vd);
931 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
932 vt_processkey(kbd, vd, c);
938 vt_allocate_keyboard(struct vt_device *vd)
944 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
946 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
947 k0 = kbd_get_keyboard(idx0);
949 for (idx = kbd_find_keyboard2("*", -1, 0);
951 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
952 k = kbd_get_keyboard(idx);
954 if (idx == idx0 || KBD_IS_BUSY(k))
957 bzero(&ki, sizeof(ki));
958 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
959 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
960 ki.kb_unit = k->kb_unit;
962 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
965 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
966 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
968 DPRINTF(10, "%s: No keyboard found.\n", __func__);
972 vd->vd_keyboard = idx0;
973 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
979 vtterm_bell(struct terminal *tm)
981 struct vt_window *vw = tm->tm_softc;
982 struct vt_device *vd = vw->vw_device;
987 if (vd->vd_flags & VDF_QUIET_BELL)
990 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
994 vtterm_beep(struct terminal *tm, u_int param)
1001 if ((param == 0) || ((param & 0xffff) == 0)) {
1006 period = ((param >> 16) & 0xffff) * hz / 1000;
1007 freq = 1193182 / (param & 0xffff);
1009 sysbeep(freq, period);
1013 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1015 struct vt_window *vw = tm->tm_softc;
1017 vtbuf_cursor_position(&vw->vw_buf, p);
1018 vt_resume_flush_timer(vw->vw_device, 0);
1022 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1024 struct vt_window *vw = tm->tm_softc;
1026 vtbuf_putchar(&vw->vw_buf, p, c);
1027 vt_resume_flush_timer(vw->vw_device, 0);
1031 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1033 struct vt_window *vw = tm->tm_softc;
1035 vtbuf_fill_locked(&vw->vw_buf, r, c);
1036 vt_resume_flush_timer(vw->vw_device, 0);
1040 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1041 const term_pos_t *p)
1043 struct vt_window *vw = tm->tm_softc;
1045 vtbuf_copy(&vw->vw_buf, r, p);
1046 vt_resume_flush_timer(vw->vw_device, 0);
1050 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1052 struct vt_window *vw = tm->tm_softc;
1056 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1057 vt_resume_flush_timer(vw->vw_device, 0);
1060 vw->vw_mouse_level = arg;
1066 vt_determine_colors(term_char_t c, int cursor,
1067 term_color_t *fg, term_color_t *bg)
1074 *fg = TCHAR_FGCOLOR(c);
1075 if (TCHAR_FORMAT(c) & TF_BOLD)
1076 *fg = TCOLOR_LIGHT(*fg);
1077 *bg = TCHAR_BGCOLOR(c);
1078 if (TCHAR_FORMAT(c) & TF_BLINK)
1079 *bg = TCOLOR_LIGHT(*bg);
1081 if (TCHAR_FORMAT(c) & TF_REVERSE)
1093 #ifndef SC_NO_CUTPASTE
1095 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1097 unsigned int mx, my;
1100 * We use the cursor position saved during the current refresh,
1101 * in case the cursor moved since.
1103 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1104 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1106 if (mx >= area->tr_end.tp_col ||
1107 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1108 my >= area->tr_end.tp_row ||
1109 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1115 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1118 struct vt_window *vw;
1122 vw = vd->vd_curwindow;
1125 x = vd->vd_mx_drawn;
1126 y = vd->vd_my_drawn;
1129 area.tr_begin.tp_col = x / vf->vf_width;
1130 area.tr_begin.tp_row = y / vf->vf_height;
1131 area.tr_end.tp_col =
1132 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1133 area.tr_end.tp_row =
1134 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1137 * No font loaded (ie. vt_vga operating in textmode).
1139 * FIXME: This fake area needs to be revisited once the
1140 * mouse cursor is supported in vt_vga's textmode.
1142 area.tr_begin.tp_col = x;
1143 area.tr_begin.tp_row = y;
1144 area.tr_end.tp_col = x + 2;
1145 area.tr_end.tp_row = y + 2;
1148 vtbuf_dirty(&vw->vw_buf, &area);
1153 vt_set_border(struct vt_window *vw, term_color_t c)
1155 struct vt_device *vd = vw->vw_device;
1156 term_rect_t *vda = &vw->vw_draw_area;
1160 for (y = 0; y < vda->tr_begin.tp_row; y++)
1161 for (x = 0; x < vd->vd_width; x++)
1162 vd->vd_driver->vd_setpixel(vd, x, y, c);
1164 for (y = vda->tr_begin.tp_row; y <= vda->tr_end.tp_row; y++) {
1166 for (x = 0; x < vda->tr_begin.tp_col; x++)
1167 vd->vd_driver->vd_setpixel(vd, x, y, c);
1170 for (x = vda->tr_end.tp_col + 1; x < vd->vd_width; x++)
1171 vd->vd_driver->vd_setpixel(vd, x, y, c);
1175 for (y = vda->tr_end.tp_row + 1; y < vd->vd_height; y++)
1176 for (x = 0; x < vd->vd_width; x++)
1177 vd->vd_driver->vd_setpixel(vd, x, y, c);
1181 vt_flush(struct vt_device *vd)
1183 struct vt_window *vw;
1186 #ifndef SC_NO_CUTPASTE
1187 int cursor_was_shown, cursor_moved;
1190 vw = vd->vd_curwindow;
1194 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1198 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1201 #ifndef SC_NO_CUTPASTE
1202 cursor_was_shown = vd->vd_mshown;
1203 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1204 vd->vd_my != vd->vd_my_drawn);
1206 /* Check if the cursor should be displayed or not. */
1207 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1208 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1209 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1216 * If the cursor changed display state or moved, we must mark
1217 * the old position as dirty, so that it's erased.
1219 if (cursor_was_shown != vd->vd_mshown ||
1220 (vd->vd_mshown && cursor_moved))
1221 vt_mark_mouse_position_as_dirty(vd);
1224 * Save position of the mouse cursor. It's used by backends to
1225 * know where to draw the cursor and during the next refresh to
1226 * erase the previous position.
1228 vd->vd_mx_drawn = vd->vd_mx;
1229 vd->vd_my_drawn = vd->vd_my;
1232 * If the cursor is displayed and has moved since last refresh,
1233 * mark the new position as dirty.
1235 if (vd->vd_mshown && cursor_moved)
1236 vt_mark_mouse_position_as_dirty(vd);
1239 vtbuf_undirty(&vw->vw_buf, &tarea);
1241 /* Force a full redraw when the screen contents are invalid. */
1242 if (vd->vd_flags & VDF_INVALID) {
1243 vd->vd_flags &= ~VDF_INVALID;
1245 vt_set_border(vw, TC_BLACK);
1246 vt_termrect(vd, vf, &tarea);
1247 if (vt_draw_logo_cpus)
1248 vtterm_draw_cpu_logos(vd);
1251 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1252 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1262 struct vt_device *vd;
1266 /* Update screen if required. */
1267 changed = vt_flush(vd);
1269 /* Schedule for next update. */
1271 vt_schedule_flush(vd, 0);
1273 vd->vd_timer_armed = 0;
1277 vtterm_done(struct terminal *tm)
1279 struct vt_window *vw = tm->tm_softc;
1280 struct vt_device *vd = vw->vw_device;
1282 if (kdb_active || panicstr != NULL) {
1283 /* Switch to the debugger. */
1284 if (vd->vd_curwindow != vw) {
1285 vd->vd_curwindow = vw;
1286 vd->vd_flags |= VDF_INVALID;
1287 if (vd->vd_driver->vd_postswitch)
1288 vd->vd_driver->vd_postswitch(vd);
1290 vd->vd_flags &= ~VDF_SPLASH;
1292 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1299 vtterm_splash(struct vt_device *vd)
1301 vt_axis_t top, left;
1303 /* Display a nice boot splash. */
1304 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1306 top = (vd->vd_height - vt_logo_height) / 2;
1307 left = (vd->vd_width - vt_logo_width) / 2;
1308 switch (vt_logo_depth) {
1310 /* XXX: Unhardcode colors! */
1311 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1312 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1313 left, top, TC_WHITE, TC_BLACK);
1315 vd->vd_flags |= VDF_SPLASH;
1322 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1324 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1325 struct vt_window *vw = tm->tm_softc;
1326 struct vt_device *vd = vw->vw_device;
1331 if (!vty_enabled(VTY_VT))
1334 if (vd->vd_flags & VDF_INITIALIZED)
1335 /* Initialization already done. */
1338 SET_FOREACH(vtdlist, vt_drv_set) {
1340 if (vtd->vd_probe == NULL)
1342 if (vtd->vd_probe(vd) == CN_DEAD)
1344 if ((vtdbest == NULL) ||
1345 (vtd->vd_priority > vtdbest->vd_priority))
1348 if (vtdbest == NULL) {
1349 cp->cn_pri = CN_DEAD;
1350 vd->vd_flags |= VDF_DEAD;
1352 vd->vd_driver = vtdbest;
1353 cp->cn_pri = vd->vd_driver->vd_init(vd);
1356 /* Check if driver's vt_init return CN_DEAD. */
1357 if (cp->cn_pri == CN_DEAD) {
1358 vd->vd_flags |= VDF_DEAD;
1361 /* Initialize any early-boot keyboard drivers */
1362 kbd_configure(KB_CONF_PROBE_ONLY);
1364 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1365 vd->vd_windows[VT_CONSWINDOW] = vw;
1366 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1368 /* Attach default font if not in TEXTMODE. */
1369 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1370 vw->vw_font = vtfont_ref(&vt_font_default);
1371 vt_compute_drawable_area(vw);
1375 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1376 * that we have the real viewable size, fix it in the static
1379 if (vd->vd_width != 0 && vd->vd_height != 0)
1380 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1382 vtbuf_init_early(&vw->vw_buf);
1383 vt_winsize(vd, vw->vw_font, &wsz);
1384 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1386 attr.ta_format = TCHAR_FORMAT(c);
1387 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1388 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1389 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1391 if (vtdbest != NULL) {
1396 vd->vd_flags |= VDF_INITIALIZED;
1401 vtterm_cngetc(struct terminal *tm)
1403 struct vt_window *vw = tm->tm_softc;
1404 struct vt_device *vd = vw->vw_device;
1408 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1409 return (*vw->vw_kbdsq++);
1411 /* Make sure the splash screen is not there. */
1412 if (vd->vd_flags & VDF_SPLASH) {
1414 vd->vd_flags &= ~VDF_SPLASH;
1415 /* Mark screen as invalid to force update */
1416 vd->vd_flags |= VDF_INVALID;
1420 /* Stripped down keyboard handler. */
1421 kbd = kbd_get_keyboard(vd->vd_keyboard);
1425 /* Force keyboard input mode to K_XLATE */
1426 vw->vw_kbdmode = K_XLATE;
1427 vt_update_kbd_mode(vw, kbd);
1429 /* Switch the keyboard to polling to make it work here. */
1430 kbdd_poll(kbd, TRUE);
1431 c = kbdd_read_char(kbd, 0);
1432 kbdd_poll(kbd, FALSE);
1436 if (vw->vw_flags & VWF_SCROLL) {
1437 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1442 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1446 vt_save_kbd_state(vw, kbd);
1447 if (vw->vw_kbdstate & SLKED) {
1448 /* Turn scrolling on. */
1449 vw->vw_flags |= VWF_SCROLL;
1450 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1452 /* Turn scrolling off. */
1453 vt_scroll(vw, 0, VHS_END);
1454 vw->vw_flags &= ~VWF_SCROLL;
1455 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1458 /* XXX: KDB can handle history. */
1459 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1460 vw->vw_kbdsq = "\x1b[A";
1462 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1463 vw->vw_kbdsq = "\x1b[B";
1465 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1466 vw->vw_kbdsq = "\x1b[C";
1468 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1469 vw->vw_kbdsq = "\x1b[D";
1473 /* Force refresh to make scrollback work. */
1475 } else if (KEYFLAGS(c) == 0) {
1476 return (KEYCHAR(c));
1479 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1480 return (*vw->vw_kbdsq++);
1486 vtterm_cngrab(struct terminal *tm)
1488 struct vt_device *vd;
1489 struct vt_window *vw;
1496 vt_window_switch(vw);
1498 kbd = kbd_get_keyboard(vd->vd_keyboard);
1502 if (vw->vw_grabbed++ > 0)
1506 * Make sure the keyboard is accessible even when the kbd device
1507 * driver is disabled.
1511 /* We shall always use the keyboard in the XLATE mode here. */
1512 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1513 vw->vw_kbdmode = K_XLATE;
1514 vt_update_kbd_mode(vw, kbd);
1516 kbdd_poll(kbd, TRUE);
1520 vtterm_cnungrab(struct terminal *tm)
1522 struct vt_device *vd;
1523 struct vt_window *vw;
1529 kbd = kbd_get_keyboard(vd->vd_keyboard);
1533 if (--vw->vw_grabbed > 0)
1536 kbdd_poll(kbd, FALSE);
1538 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1539 vt_update_kbd_mode(vw, kbd);
1544 vtterm_opened(struct terminal *tm, int opened)
1546 struct vt_window *vw = tm->tm_softc;
1547 struct vt_device *vd = vw->vw_device;
1550 vd->vd_flags &= ~VDF_SPLASH;
1552 vw->vw_flags |= VWF_OPENED;
1554 vw->vw_flags &= ~VWF_OPENED;
1555 /* TODO: finish ACQ/REL */
1561 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1563 struct vt_device *vd = vw->vw_device;
1564 struct terminal *tm = vw->vw_terminal;
1571 * Changing fonts is a little tricky. We must prevent
1572 * simultaneous access to the device, so we must stop
1573 * the display timer and the terminal from accessing.
1574 * We need to switch fonts and grow our screen buffer.
1576 * XXX: Right now the code uses terminal_mute() to
1577 * prevent data from reaching the console driver while
1578 * resizing the screen buffer. This isn't elegant...
1582 if (vw->vw_flags & VWF_BUSY) {
1583 /* Another process is changing the font. */
1587 vw->vw_flags |= VWF_BUSY;
1590 vt_termsize(vd, vf, &size);
1591 vt_winsize(vd, vf, &wsz);
1593 /* Grow the screen buffer and terminal. */
1594 terminal_mute(tm, 1);
1595 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1596 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1597 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1598 terminal_mute(tm, 0);
1600 /* Actually apply the font to the current window. */
1602 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1604 * In case vt_change_font called to update size we don't need
1605 * to update font link.
1607 vtfont_unref(vw->vw_font);
1608 vw->vw_font = vtfont_ref(vf);
1612 * Compute the drawable area and move the mouse cursor inside
1613 * it, in case the new area is smaller than the previous one.
1615 vt_compute_drawable_area(vw);
1616 vd->vd_mx = min(vd->vd_mx,
1617 vw->vw_draw_area.tr_end.tp_col -
1618 vw->vw_draw_area.tr_begin.tp_col - 1);
1619 vd->vd_my = min(vd->vd_my,
1620 vw->vw_draw_area.tr_end.tp_row -
1621 vw->vw_draw_area.tr_begin.tp_row - 1);
1623 /* Force a full redraw the next timer tick. */
1624 if (vd->vd_curwindow == vw) {
1625 vd->vd_flags |= VDF_INVALID;
1626 vt_resume_flush_timer(vw->vw_device, 0);
1628 vw->vw_flags &= ~VWF_BUSY;
1634 vt_proc_alive(struct vt_window *vw)
1638 if (vw->vw_smode.mode != VT_PROCESS)
1642 if ((p = pfind(vw->vw_pid)) != NULL)
1644 if (vw->vw_proc == p)
1647 vw->vw_smode.mode = VT_AUTO;
1648 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1655 signal_vt_rel(struct vt_window *vw)
1658 if (vw->vw_smode.mode != VT_PROCESS)
1660 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1665 vw->vw_flags |= VWF_SWWAIT_REL;
1666 PROC_LOCK(vw->vw_proc);
1667 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1668 PROC_UNLOCK(vw->vw_proc);
1669 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1674 signal_vt_acq(struct vt_window *vw)
1677 if (vw->vw_smode.mode != VT_PROCESS)
1679 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1680 cnavailable(vw->vw_terminal->consdev, FALSE);
1681 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1686 vw->vw_flags |= VWF_SWWAIT_ACQ;
1687 PROC_LOCK(vw->vw_proc);
1688 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1689 PROC_UNLOCK(vw->vw_proc);
1690 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1695 finish_vt_rel(struct vt_window *vw, int release, int *s)
1698 if (vw->vw_flags & VWF_SWWAIT_REL) {
1699 vw->vw_flags &= ~VWF_SWWAIT_REL;
1701 callout_drain(&vw->vw_proc_dead_timer);
1702 vt_late_window_switch(vw->vw_switch_to);
1710 finish_vt_acq(struct vt_window *vw)
1713 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1714 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1720 #ifndef SC_NO_CUTPASTE
1722 vt_mouse_terminput_button(struct vt_device *vd, int button)
1724 struct vt_window *vw;
1726 char mouseb[6] = "\x1B[M";
1729 vw = vd->vd_curwindow;
1732 /* Translate to char position. */
1733 x = vd->vd_mx / vf->vf_width;
1734 y = vd->vd_my / vf->vf_height;
1735 /* Avoid overflow. */
1736 x = MIN(x, 255 - '!');
1737 y = MIN(y, 255 - '!');
1739 mouseb[3] = ' ' + button;
1740 mouseb[4] = '!' + x;
1741 mouseb[5] = '!' + y;
1743 for (i = 0; i < sizeof(mouseb); i++)
1744 terminal_input_char(vw->vw_terminal, mouseb[i]);
1748 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1753 case MOUSE_BUTTON_EVENT:
1755 /* Mouse button pressed. */
1756 if (event & MOUSE_BUTTON1DOWN)
1757 vt_mouse_terminput_button(vd, 0);
1758 if (event & MOUSE_BUTTON2DOWN)
1759 vt_mouse_terminput_button(vd, 1);
1760 if (event & MOUSE_BUTTON3DOWN)
1761 vt_mouse_terminput_button(vd, 2);
1763 /* Mouse button released. */
1764 vt_mouse_terminput_button(vd, 3);
1768 case MOUSE_MOTION_EVENT:
1769 if (mouse->u.data.z < 0) {
1771 sc_mouse_input_button(vd, 64);
1772 } else if (mouse->u.data.z > 0) {
1774 sc_mouse_input_button(vd, 65);
1787 len = VD_PASTEBUFLEN(main_vd);
1788 buf = VD_PASTEBUF(main_vd);
1789 len /= sizeof(term_char_t);
1790 for (i = 0; i < len; i++) {
1793 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1799 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1801 struct vt_device *vd;
1802 struct vt_window *vw;
1808 vw = vd->vd_curwindow;
1812 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1814 * Either the mouse is disabled, or the window is in
1815 * "graphics mode". The graphics mode is usually set by
1816 * an X server, using the KDSETMODE ioctl.
1820 if (vf == NULL) /* Text mode. */
1824 * TODO: add flag about pointer position changed, to not redraw chars
1825 * under mouse pointer when nothing changed.
1828 if (vw->vw_mouse_level > 0)
1829 vt_mouse_terminput(vd, type, x, y, event, cnt);
1833 case MOUSE_MOTION_EVENT:
1838 vt_termsize(vd, vf, &size);
1843 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1844 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1848 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1849 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1850 vd->vd_mx / vf->vf_width,
1851 vd->vd_my / vf->vf_height);
1853 vt_resume_flush_timer(vw->vw_device, 0);
1855 case MOUSE_BUTTON_EVENT:
1863 case MOUSE_BUTTON1DOWN:
1866 mark = VTB_MARK_END;
1868 case 1: /* single click: start cut operation */
1869 mark = VTB_MARK_START;
1871 case 2: /* double click: cut a word */
1872 mark = VTB_MARK_WORD;
1874 case 3: /* triple click: cut a line */
1875 mark = VTB_MARK_ROW;
1879 case VT_MOUSE_PASTEBUTTON:
1888 case VT_MOUSE_EXTENDBUTTON:
1891 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1892 mark = VTB_MARK_EXTEND;
1897 mark = VTB_MARK_EXTEND;
1905 /* Save buttons state. */
1907 vd->vd_mstate |= event;
1909 vd->vd_mstate &= ~event;
1911 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1912 vd->vd_my / vf->vf_height) == 1) {
1914 * We have something marked to copy, so update pointer to
1915 * window with selection.
1917 vt_resume_flush_timer(vw->vw_device, 0);
1923 case VTB_MARK_EXTEND:
1926 /* Other types of mark do not require to copy data. */
1930 /* Get current selection size in bytes. */
1931 len = vtbuf_get_marked_len(&vw->vw_buf);
1935 /* Reallocate buffer only if old one is too small. */
1936 if (len > VD_PASTEBUFSZ(vd)) {
1937 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1939 /* Update buffer size. */
1940 VD_PASTEBUFSZ(vd) = len;
1942 /* Request copy/paste buffer data, no more than `len' */
1943 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1946 VD_PASTEBUFLEN(vd) = len;
1948 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1953 vt_mouse_state(int show)
1955 struct vt_device *vd;
1956 struct vt_window *vw;
1959 vw = vd->vd_curwindow;
1963 vw->vw_flags |= VWF_MOUSE_HIDE;
1966 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1970 /* Mark mouse position as dirty. */
1971 vt_mark_mouse_position_as_dirty(vd);
1972 vt_resume_flush_timer(vw->vw_device, 0);
1977 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1978 int nprot, vm_memattr_t *memattr)
1980 struct vt_window *vw = tm->tm_softc;
1981 struct vt_device *vd = vw->vw_device;
1983 if (vd->vd_driver->vd_fb_mmap)
1984 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1991 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1994 struct vt_window *vw = tm->tm_softc;
1995 struct vt_device *vd = vw->vw_device;
1998 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1999 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2010 cmd = VT_WAITACTIVE;
2036 ival = IOCPARM_IVAL(data);
2037 data = (caddr_t)&ival;
2042 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2043 if (*(int *)data & ~0x7f)
2048 case GIO_DEADKEYMAP:
2049 case PIO_DEADKEYMAP:
2054 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2055 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2056 case KBADDKBD: /* add/remove keyboard to/from mux */
2061 kbd = kbd_get_keyboard(vd->vd_keyboard);
2063 error = kbdd_ioctl(kbd, cmd, data);
2065 if (error == ENOIOCTL) {
2066 if (cmd == KDGKBTYPE) {
2067 /* always return something? XXX */
2075 case KDGKBSTATE: { /* get keyboard state (locks) */
2078 if (vw == vd->vd_curwindow) {
2080 kbd = kbd_get_keyboard(vd->vd_keyboard);
2082 error = vt_save_kbd_state(vw, kbd);
2089 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2093 case KDSKBSTATE: { /* set keyboard state (locks) */
2096 state = *(int *)data;
2097 if (state & ~LOCK_MASK)
2100 vw->vw_kbdstate &= ~LOCK_MASK;
2101 vw->vw_kbdstate |= state;
2104 if (vw == vd->vd_curwindow) {
2106 kbd = kbd_get_keyboard(vd->vd_keyboard);
2108 error = vt_update_kbd_state(vw, kbd);
2114 case KDGETLED: { /* get keyboard LED status */
2117 if (vw == vd->vd_curwindow) {
2119 kbd = kbd_get_keyboard(vd->vd_keyboard);
2121 error = vt_save_kbd_leds(vw, kbd);
2128 *(int *)data = vw->vw_kbdstate & LED_MASK;
2132 case KDSETLED: { /* set keyboard LED status */
2135 leds = *(int *)data;
2136 if (leds & ~LED_MASK)
2139 vw->vw_kbdstate &= ~LED_MASK;
2140 vw->vw_kbdstate |= leds;
2143 if (vw == vd->vd_curwindow) {
2145 kbd = kbd_get_keyboard(vd->vd_keyboard);
2147 error = vt_update_kbd_leds(vw, kbd);
2156 if (vw == vd->vd_curwindow) {
2158 kbd = kbd_get_keyboard(vd->vd_keyboard);
2160 error = vt_save_kbd_mode(vw, kbd);
2167 *(int *)data = vw->vw_kbdmode;
2174 mode = *(int *)data;
2179 vw->vw_kbdmode = mode;
2182 if (vw == vd->vd_curwindow) {
2184 kbd = kbd_get_keyboard(vd->vd_keyboard);
2186 error = vt_update_kbd_mode(vw, kbd);
2196 case FBIO_GETWINORG: /* get frame buffer window origin */
2197 case FBIO_GETDISPSTART: /* get display start address */
2198 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2199 case FBIO_BLANK: /* blank display */
2200 if (vd->vd_driver->vd_fb_ioctl)
2201 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2203 case CONS_BLANKTIME:
2208 *(int *)data = M_CG640x480;
2210 case CONS_BELLTYPE: /* set bell type sound */
2211 if ((*(int *)data) & CONS_QUIET_BELL)
2212 vd->vd_flags |= VDF_QUIET_BELL;
2214 vd->vd_flags &= ~VDF_QUIET_BELL;
2216 case CONS_GETINFO: {
2217 vid_info_t *vi = (vid_info_t *)data;
2218 if (vi->size != sizeof(struct vid_info))
2221 if (vw == vd->vd_curwindow) {
2223 kbd = kbd_get_keyboard(vd->vd_keyboard);
2225 vt_save_kbd_state(vw, kbd);
2229 vi->m_num = vd->vd_curwindow->vw_number + 1;
2230 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2231 /* XXX: other fields! */
2235 *(int *)data = 0x200;
2240 case CONS_MOUSECTL: {
2241 mouse_info_t *mouse = (mouse_info_t*)data;
2244 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2245 * should not be applied to individual TTYs, but only to
2248 switch (mouse->operation) {
2250 if (vd->vd_flags & VDF_MOUSECURSOR) {
2251 vd->vd_flags &= ~VDF_MOUSECURSOR;
2252 #ifndef SC_NO_CUTPASTE
2253 vt_mouse_state(VT_MOUSE_HIDE);
2258 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2259 vd->vd_flags |= VDF_MOUSECURSOR;
2260 vd->vd_mx = vd->vd_width / 2;
2261 vd->vd_my = vd->vd_height / 2;
2262 #ifndef SC_NO_CUTPASTE
2263 vt_mouse_state(VT_MOUSE_SHOW);
2274 if (vd->vd_flags & VDF_TEXTMODE)
2277 error = vtfont_load((void *)data, &vf);
2281 error = vt_change_font(vw, vf);
2285 case PIO_VFONT_DEFAULT: {
2286 /* Reset to default font. */
2287 error = vt_change_font(vw, &vt_font_default);
2291 scrmap_t *sm = (scrmap_t *)data;
2293 /* We don't have screen maps, so return a handcrafted one. */
2294 for (i = 0; i < 256; i++)
2300 * FIXME: This implementation is incomplete compared to
2303 switch (*(int *)data) {
2307 vw->vw_flags &= ~VWF_GRAPHICS;
2310 vw->vw_flags |= VWF_GRAPHICS;
2314 case KDENABIO: /* allow io operations */
2315 error = priv_check(td, PRIV_IO);
2318 error = securelevel_gt(td->td_ucred, 0);
2321 #if defined(__i386__)
2322 td->td_frame->tf_eflags |= PSL_IOPL;
2323 #elif defined(__amd64__)
2324 td->td_frame->tf_rflags |= PSL_IOPL;
2327 case KDDISABIO: /* disallow io operations (default) */
2328 #if defined(__i386__)
2329 td->td_frame->tf_eflags &= ~PSL_IOPL;
2330 #elif defined(__amd64__)
2331 td->td_frame->tf_rflags &= ~PSL_IOPL;
2334 case KDMKTONE: /* sound the bell */
2335 vtterm_beep(tm, *(u_int *)data);
2337 case KIOCSOUND: /* make tone (*data) hz */
2340 case CONS_SETKBD: /* set the new keyboard */
2343 if (vd->vd_keyboard != *(int *)data) {
2344 kbd = kbd_get_keyboard(*(int *)data);
2349 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2350 (void *)vd, vt_kbdevent, vd);
2352 if (vd->vd_keyboard != -1) {
2353 kbd = kbd_get_keyboard(vd->vd_keyboard);
2354 vt_save_kbd_state(vd->vd_curwindow, kbd);
2355 kbd_release(kbd, (void *)vd);
2357 kbd = kbd_get_keyboard(i);
2358 vd->vd_keyboard = i;
2360 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2361 vt_update_kbd_state(vd->vd_curwindow, kbd);
2363 error = EPERM; /* XXX */
2368 case CONS_RELKBD: /* release the current keyboard */
2371 if (vd->vd_keyboard != -1) {
2372 kbd = kbd_get_keyboard(vd->vd_keyboard);
2377 vt_save_kbd_state(vd->vd_curwindow, kbd);
2378 error = kbd_release(kbd, (void *)vd);
2380 vd->vd_keyboard = -1;
2387 win = *(int *)data - 1;
2388 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2390 if ((win >= VT_MAXWINDOWS) || (win < 0))
2392 return (vt_proc_window_switch(vd->vd_windows[win]));
2395 *(int *)data = vd->vd_curwindow->vw_number + 1;
2398 *(int *)data = vw->vw_number + 1;
2401 /* TODO: Check current state, switching can be in progress. */
2402 if ((*(int *)data) == 0x01)
2403 vw->vw_flags |= VWF_VTYLOCK;
2404 else if ((*(int *)data) == 0x02)
2405 vw->vw_flags &= ~VWF_VTYLOCK;
2411 for (i = 0; i < VT_MAXWINDOWS; i++) {
2412 vw = vd->vd_windows[i];
2415 if (!(vw->vw_flags & VWF_OPENED)) {
2416 *(int *)data = vw->vw_number + 1;
2423 case VT_WAITACTIVE: {
2428 idx = *(unsigned int *)data;
2429 if (idx > VT_MAXWINDOWS)
2432 vw = vd->vd_windows[idx - 1];
2435 while (vd->vd_curwindow != vw && error == 0)
2436 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2440 case VT_SETMODE: { /* set screen switcher mode */
2441 struct vt_mode *mode;
2444 mode = (struct vt_mode *)data;
2445 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2446 if (vw->vw_smode.mode == VT_PROCESS) {
2447 p1 = pfind(vw->vw_pid);
2448 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2451 DPRINTF(5, "error EPERM\n");
2457 if (mode->mode == VT_AUTO) {
2458 vw->vw_smode.mode = VT_AUTO;
2461 DPRINTF(5, "VT_AUTO, ");
2462 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2463 cnavailable(vw->vw_terminal->consdev, TRUE);
2464 /* were we in the middle of the vty switching process? */
2465 if (finish_vt_rel(vw, TRUE, &s) == 0)
2466 DPRINTF(5, "reset WAIT_REL, ");
2467 if (finish_vt_acq(vw) == 0)
2468 DPRINTF(5, "reset WAIT_ACQ, ");
2470 } else if (mode->mode == VT_PROCESS) {
2471 if (!ISSIGVALID(mode->relsig) ||
2472 !ISSIGVALID(mode->acqsig) ||
2473 !ISSIGVALID(mode->frsig)) {
2474 DPRINTF(5, "error EINVAL\n");
2477 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2478 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2479 vw->vw_proc = td->td_proc;
2480 vw->vw_pid = vw->vw_proc->p_pid;
2481 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2482 cnavailable(vw->vw_terminal->consdev, FALSE);
2484 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2491 case VT_GETMODE: /* get screen switcher mode */
2492 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2495 case VT_RELDISP: /* screen switcher ioctl */
2497 * This must be the current vty which is in the VT_PROCESS
2500 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2504 /* ...and this process is controlling it. */
2505 if (vw->vw_proc != td->td_proc) {
2509 switch(*(int *)data) {
2510 case VT_FALSE: /* user refuses to release screen, abort */
2511 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2512 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2513 SC_DRIVER_NAME, VT_UNIT(vw));
2515 case VT_TRUE: /* user has released screen, go on */
2516 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2517 if (vw->vw_flags & VWF_SWWAIT_REL) {
2518 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2519 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2520 SC_DRIVER_NAME, VT_UNIT(vw));
2525 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2526 if ((error = finish_vt_acq(vw)) == 0)
2527 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2528 SC_DRIVER_NAME, VT_UNIT(vw));
2539 static struct vt_window *
2540 vt_allocate_window(struct vt_device *vd, unsigned int window)
2542 struct vt_window *vw;
2543 struct terminal *tm;
2547 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2549 vw->vw_number = window;
2550 vw->vw_kbdmode = K_XLATE;
2552 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2553 vw->vw_font = vtfont_ref(&vt_font_default);
2554 vt_compute_drawable_area(vw);
2557 vt_termsize(vd, vw->vw_font, &size);
2558 vt_winsize(vd, vw->vw_font, &wsz);
2559 vtbuf_init(&vw->vw_buf, &size);
2561 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2562 terminal_set_winsize(tm, &wsz);
2563 vd->vd_windows[window] = vw;
2564 callout_init(&vw->vw_proc_dead_timer, 0);
2570 vt_upgrade(struct vt_device *vd)
2572 struct vt_window *vw;
2574 int register_handlers;
2576 if (!vty_enabled(VTY_VT))
2578 if (main_vd->vd_driver == NULL)
2581 for (i = 0; i < VT_MAXWINDOWS; i++) {
2582 vw = vd->vd_windows[i];
2585 vw = vt_allocate_window(vd, i);
2587 if (!(vw->vw_flags & VWF_READY)) {
2588 callout_init(&vw->vw_proc_dead_timer, 0);
2589 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2590 vw->vw_flags |= VWF_READY;
2591 if (vw->vw_flags & VWF_CONSOLE) {
2592 /* For existing console window. */
2593 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2594 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2600 if (vd->vd_curwindow == NULL)
2601 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2603 register_handlers = 0;
2604 if (!(vd->vd_flags & VDF_ASYNC)) {
2605 /* Attach keyboard. */
2606 vt_allocate_keyboard(vd);
2608 /* Init 25 Hz timer. */
2609 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2611 /* Start timer when everything ready. */
2612 vd->vd_flags |= VDF_ASYNC;
2613 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2614 vd->vd_timer_armed = 1;
2615 register_handlers = 1;
2620 /* Refill settings with new sizes. */
2623 if (register_handlers) {
2624 /* Register suspend/resume handlers. */
2625 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2626 vd, EVENTHANDLER_PRI_ANY);
2627 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2628 EVENTHANDLER_PRI_ANY);
2633 vt_resize(struct vt_device *vd)
2635 struct vt_window *vw;
2638 for (i = 0; i < VT_MAXWINDOWS; i++) {
2639 vw = vd->vd_windows[i];
2641 /* Assign default font to window, if not textmode. */
2642 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2643 vw->vw_font = vtfont_ref(&vt_font_default);
2646 /* Resize terminal windows */
2647 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2648 DPRINTF(100, "%s: vt_change_font() is busy, "
2649 "window %d\n", __func__, i);
2655 vt_replace_backend(const struct vt_driver *drv, void *softc)
2657 struct vt_device *vd;
2661 if (vd->vd_flags & VDF_ASYNC) {
2662 /* Stop vt_flush periodic task. */
2664 vt_suspend_flush_timer(vd);
2667 * Mute current terminal until we done. vt_change_font (called
2668 * from vt_resize) will unmute it.
2670 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2674 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2678 vd->vd_flags &= ~VDF_TEXTMODE;
2682 * We want to upgrade from the current driver to the
2686 vd->vd_prev_driver = vd->vd_driver;
2687 vd->vd_prev_softc = vd->vd_softc;
2688 vd->vd_driver = drv;
2689 vd->vd_softc = softc;
2691 vd->vd_driver->vd_init(vd);
2692 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
2694 * No driver given: we want to downgrade to the previous
2697 const struct vt_driver *old_drv;
2700 old_drv = vd->vd_driver;
2701 old_softc = vd->vd_softc;
2703 vd->vd_driver = vd->vd_prev_driver;
2704 vd->vd_softc = vd->vd_prev_softc;
2705 vd->vd_prev_driver = NULL;
2706 vd->vd_prev_softc = NULL;
2708 vd->vd_flags |= VDF_DOWNGRADE;
2710 vd->vd_driver->vd_init(vd);
2712 if (old_drv->vd_fini)
2713 old_drv->vd_fini(vd, old_softc);
2715 vd->vd_flags &= ~VDF_DOWNGRADE;
2720 /* Update windows sizes and initialize last items. */
2724 if (vd->vd_flags & VDF_SPLASH)
2728 if (vd->vd_flags & VDF_ASYNC) {
2729 /* Allow to put chars now. */
2730 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2731 /* Rerun timer for screen updates. */
2732 vt_resume_flush_timer(vd, 0);
2736 * Register as console. If it already registered, cnadd() will ignore
2739 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2743 vt_suspend_handler(void *priv)
2745 struct vt_device *vd;
2748 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2749 vd->vd_driver->vd_suspend(vd);
2753 vt_resume_handler(void *priv)
2755 struct vt_device *vd;
2758 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2759 vd->vd_driver->vd_resume(vd);
2763 vt_allocate(const struct vt_driver *drv, void *softc)
2766 if (!vty_enabled(VTY_VT))
2769 if (main_vd->vd_driver == NULL) {
2770 main_vd->vd_driver = drv;
2771 printf("VT: initialize with new VT driver \"%s\".\n",
2775 * Check if have rights to replace current driver. For example:
2776 * it is bad idea to replace KMS driver with generic VGA one.
2778 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2779 printf("VT: Driver priority %d too low. Current %d\n ",
2780 drv->vd_priority, main_vd->vd_driver->vd_priority);
2783 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2784 main_vd->vd_driver->vd_name, drv->vd_name);
2787 vt_replace_backend(drv, softc);
2791 vt_deallocate(const struct vt_driver *drv, void *softc)
2794 if (!vty_enabled(VTY_VT))
2797 if (main_vd->vd_prev_driver == NULL ||
2798 main_vd->vd_driver != drv ||
2799 main_vd->vd_softc != softc)
2802 printf("VT: Switching back from \"%s\" to \"%s\".\n",
2803 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
2805 vt_replace_backend(NULL, NULL);
2809 vt_suspend(struct vt_device *vd)
2813 if (vt_suspendswitch == 0)
2815 /* Save current window. */
2816 vd->vd_savedwindow = vd->vd_curwindow;
2817 /* Ask holding process to free window and switch to console window */
2818 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2820 /* Wait for the window switch to complete. */
2823 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2824 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2829 vt_resume(struct vt_device *vd)
2832 if (vt_suspendswitch == 0)
2834 /* Switch back to saved window, if any */
2835 vt_proc_window_switch(vd->vd_savedwindow);
2836 vd->vd_savedwindow = NULL;