2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, 2013 The FreeBSD Foundation
7 * This software was developed by Ed Schouten under sponsorship from the
10 * Portions of this software were developed by Oleksandr Rybalko
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/power.h>
51 #include <sys/random.h>
52 #include <sys/reboot.h>
53 #include <sys/systm.h>
54 #include <sys/terminal.h>
56 #include <dev/kbd/kbdreg.h>
57 #include <dev/vt/vt.h>
59 #if defined(__i386__) || defined(__amd64__)
60 #include <machine/psl.h>
61 #include <machine/frame.h>
64 static tc_bell_t vtterm_bell;
65 static tc_cursor_t vtterm_cursor;
66 static tc_putchar_t vtterm_putchar;
67 static tc_fill_t vtterm_fill;
68 static tc_copy_t vtterm_copy;
69 static tc_pre_input_t vtterm_pre_input;
70 static tc_post_input_t vtterm_post_input;
71 static tc_param_t vtterm_param;
72 static tc_done_t vtterm_done;
74 static tc_cnprobe_t vtterm_cnprobe;
75 static tc_cngetc_t vtterm_cngetc;
77 static tc_cngrab_t vtterm_cngrab;
78 static tc_cnungrab_t vtterm_cnungrab;
80 static tc_opened_t vtterm_opened;
81 static tc_ioctl_t vtterm_ioctl;
82 static tc_mmap_t vtterm_mmap;
84 const struct terminal_class vt_termclass = {
85 .tc_bell = vtterm_bell,
86 .tc_cursor = vtterm_cursor,
87 .tc_putchar = vtterm_putchar,
88 .tc_fill = vtterm_fill,
89 .tc_copy = vtterm_copy,
90 .tc_pre_input = vtterm_pre_input,
91 .tc_post_input = vtterm_post_input,
92 .tc_param = vtterm_param,
93 .tc_done = vtterm_done,
95 .tc_cnprobe = vtterm_cnprobe,
96 .tc_cngetc = vtterm_cngetc,
98 .tc_cngrab = vtterm_cngrab,
99 .tc_cnungrab = vtterm_cnungrab,
101 .tc_opened = vtterm_opened,
102 .tc_ioctl = vtterm_ioctl,
103 .tc_mmap = vtterm_mmap,
107 * Use a constant timer of 25 Hz to redraw the screen.
109 * XXX: In theory we should only fire up the timer when there is really
110 * activity. Unfortunately we cannot always start timers. We really
111 * don't want to process kernel messages synchronously, because it
112 * really slows down the system.
114 #define VT_TIMERFREQ 25
116 /* Bell pitch/duration. */
117 #define VT_BELLDURATION ((5 * hz + 99) / 100)
118 #define VT_BELLPITCH 800
120 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
123 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
124 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
125 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
126 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
127 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
128 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
130 /* Allow to disable some keyboard combinations. */
131 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
132 "See kbdmap(5) to configure.");
133 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
134 "See kbdmap(5) to configure.");
135 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
136 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
137 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
138 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
139 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
140 "See kbdmap(5) to configure.");
142 /* Used internally, not a tunable. */
143 int vt_draw_logo_cpus;
144 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot");
145 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed "
146 "(0 = do not override)");
147 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style "
148 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)");
149 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)");
151 static unsigned int vt_unit = 0;
152 static MALLOC_DEFINE(M_VT, "vt", "vt device");
153 struct vt_device *main_vd = &vt_consdev;
156 extern unsigned int vt_logo_width;
157 extern unsigned int vt_logo_height;
158 extern unsigned int vt_logo_depth;
159 extern unsigned char vt_logo_image[];
161 #define vtterm_draw_cpu_logos(...) do {} while (0)
162 const unsigned int vt_logo_sprite_height;
166 extern struct vt_font vt_font_default;
167 #ifndef SC_NO_CUTPASTE
168 extern struct vt_mouse_cursor vt_default_mouse_pointer;
171 static int signal_vt_rel(struct vt_window *);
172 static int signal_vt_acq(struct vt_window *);
173 static int finish_vt_rel(struct vt_window *, int, int *);
174 static int finish_vt_acq(struct vt_window *);
175 static int vt_window_switch(struct vt_window *);
176 static int vt_late_window_switch(struct vt_window *);
177 static int vt_proc_alive(struct vt_window *);
178 static void vt_resize(struct vt_device *);
179 static void vt_update_static(void *);
180 #ifndef SC_NO_CUTPASTE
181 static void vt_mouse_paste(void);
183 static void vt_suspend_handler(void *priv);
184 static void vt_resume_handler(void *priv);
186 SET_DECLARE(vt_drv_set, struct vt_driver);
188 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT))
189 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH))
191 struct terminal vt_consterm;
192 static struct vt_window vt_conswindow;
193 struct vt_device vt_consdev = {
196 .vd_prev_driver = NULL,
197 .vd_prev_softc = NULL,
198 .vd_flags = VDF_INVALID,
199 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
200 .vd_curwindow = &vt_conswindow,
203 #ifndef SC_NO_CUTPASTE
209 .vd_mcursor = &vt_default_mouse_pointer,
210 .vd_mcursor_fg = TC_WHITE,
211 .vd_mcursor_bg = TC_BLACK,
214 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
215 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
216 static struct vt_window vt_conswindow = {
217 .vw_number = VT_CONSWINDOW,
218 .vw_flags = VWF_CONSOLE,
220 .vb_buffer = &vt_constextbuf[0],
221 .vb_rows = &vt_constextbufrows[0],
222 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
225 .vb_flags = VBF_STATIC,
226 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
227 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
233 .vw_device = &vt_consdev,
234 .vw_terminal = &vt_consterm,
235 .vw_kbdmode = K_XLATE,
238 struct terminal vt_consterm = {
239 .tm_class = &vt_termclass,
240 .tm_softc = &vt_conswindow,
243 static struct consdev vt_consterm_consdev = {
244 .cn_ops = &termcn_cnops,
245 .cn_arg = &vt_consterm,
249 /* Add to set of consoles. */
250 DATA_SET(cons_set, vt_consterm_consdev);
253 * Right after kmem is done to allow early drivers to use locking and allocate
256 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
258 /* Delay until all devices attached, to not waste time. */
259 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
262 /* Initialize locks/mem depended members. */
264 vt_update_static(void *dummy)
267 if (!vty_enabled(VTY_VT))
269 if (main_vd->vd_driver != NULL)
270 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name,
271 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution",
272 main_vd->vd_width, main_vd->vd_height);
274 printf("VT: init without driver.\n");
276 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
277 cv_init(&main_vd->vd_winswitch, "vtwswt");
281 vt_schedule_flush(struct vt_device *vd, int ms)
285 /* Default to initial value. */
286 ms = 1000 / VT_TIMERFREQ;
288 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
292 vt_resume_flush_timer(struct vt_window *vw, int ms)
294 struct vt_device *vd = vw->vw_device;
296 if (vd->vd_curwindow != vw)
299 if (!(vd->vd_flags & VDF_ASYNC) ||
300 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
303 vt_schedule_flush(vd, ms);
307 vt_suspend_flush_timer(struct vt_device *vd)
310 * As long as this function is called locked, callout_stop()
311 * has the same effect like callout_drain() with regard to
312 * preventing the callback function from executing.
314 VT_LOCK_ASSERT(vd, MA_OWNED);
316 if (!(vd->vd_flags & VDF_ASYNC) ||
317 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
320 callout_stop(&vd->vd_timer);
324 vt_switch_timer(void *arg)
327 vt_late_window_switch((struct vt_window *)arg);
331 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
336 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
342 vw->vw_kbdmode = mode;
348 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
352 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
360 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
365 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
371 vw->vw_kbdstate &= ~LOCK_MASK;
372 vw->vw_kbdstate |= state & LOCK_MASK;
378 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
382 state = vw->vw_kbdstate & LOCK_MASK;
383 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
391 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
396 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
402 vw->vw_kbdstate &= ~LED_MASK;
403 vw->vw_kbdstate |= leds & LED_MASK;
409 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
413 leds = vw->vw_kbdstate & LED_MASK;
414 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
422 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
425 DPRINTF(40, "%s\n", __func__);
426 curvw->vw_switch_to = vw;
427 /* Set timer to allow switch in case when process hang. */
428 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
429 vt_switch_timer, (void *)vw);
430 /* Notify process about vt switch attempt. */
431 DPRINTF(30, "%s: Notify process.\n", __func__);
432 signal_vt_rel(curvw);
438 vt_window_postswitch(struct vt_window *vw)
445 /* vt_late_window_switch will done VT switching for regular case. */
447 vt_late_window_switch(struct vt_window *vw)
451 callout_stop(&vw->vw_proc_dead_timer);
453 ret = vt_window_switch(vw);
457 /* Notify owner process about terminal availability. */
458 if (vw->vw_smode.mode == VT_PROCESS) {
459 ret = vt_window_postswitch(vw);
466 vt_proc_window_switch(struct vt_window *vw)
468 struct vt_window *curvw;
469 struct vt_device *vd;
472 /* Prevent switching to NULL */
474 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__);
478 curvw = vd->vd_curwindow;
480 /* Check if virtual terminal is locked */
481 if (curvw->vw_flags & VWF_VTYLOCK)
484 /* Check if switch already in progress */
485 if (curvw->vw_flags & VWF_SWWAIT_REL) {
486 /* Check if switching to same window */
487 if (curvw->vw_switch_to == vw) {
488 DPRINTF(30, "%s: Switch in progress to same vw.", __func__);
489 return (0); /* success */
491 DPRINTF(30, "%s: Switch in progress to different vw.", __func__);
495 /* Avoid switching to already selected window */
497 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__);
498 return (0); /* success */
501 /* Ask current process permission to switch away. */
502 if (curvw->vw_smode.mode == VT_PROCESS) {
503 DPRINTF(30, "%s: VT_PROCESS ", __func__);
504 if (vt_proc_alive(curvw) == FALSE) {
505 DPRINTF(30, "Dead. Cleaning.");
508 DPRINTF(30, "%s: Signaling process.\n", __func__);
509 /* Alive, try to ask him. */
510 ret = vt_window_preswitch(vw, curvw);
511 /* Wait for process answer or timeout. */
517 ret = vt_late_window_switch(vw);
521 /* Switch window ignoring process locking. */
523 vt_window_switch(struct vt_window *vw)
525 struct vt_device *vd = vw->vw_device;
526 struct vt_window *curvw = vd->vd_curwindow;
531 * When grabbing the console for the debugger, avoid
532 * locks as that can result in deadlock. While this
533 * could use try locks, that wouldn't really make a
534 * difference as there are sufficient barriers in
535 * debugger entry/exit to be equivalent to
536 * successfully try-locking here.
540 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)))
543 vd->vd_curwindow = vw;
544 vd->vd_flags |= VDF_INVALID;
545 if (vd->vd_driver->vd_postswitch)
546 vd->vd_driver->vd_postswitch(vd);
556 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
561 vt_suspend_flush_timer(vd);
563 vd->vd_curwindow = vw;
564 vd->vd_flags |= VDF_INVALID;
565 cv_broadcast(&vd->vd_winswitch);
568 if (vd->vd_driver->vd_postswitch)
569 vd->vd_driver->vd_postswitch(vd);
571 vt_resume_flush_timer(vw, 0);
573 /* Restore per-window keyboard mode. */
575 kbd = kbd_get_keyboard(vd->vd_keyboard);
577 if (curvw->vw_kbdmode == K_XLATE)
578 vt_save_kbd_state(curvw, kbd);
580 vt_update_kbd_mode(vw, kbd);
581 vt_update_kbd_state(vw, kbd);
584 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
590 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
593 size->tp_row = vd->vd_height;
594 if (vt_draw_logo_cpus)
595 size->tp_row -= vt_logo_sprite_height;
596 size->tp_col = vd->vd_width;
598 size->tp_row /= vf->vf_height;
599 size->tp_col /= vf->vf_width;
604 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect)
607 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0;
608 if (vt_draw_logo_cpus)
609 rect->tr_begin.tp_row = vt_logo_sprite_height;
611 rect->tr_end.tp_row = vd->vd_height;
612 rect->tr_end.tp_col = vd->vd_width;
615 rect->tr_begin.tp_row =
616 howmany(rect->tr_begin.tp_row, vf->vf_height);
618 rect->tr_end.tp_row /= vf->vf_height;
619 rect->tr_end.tp_col /= vf->vf_width;
624 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
627 size->ws_ypixel = vd->vd_height;
628 if (vt_draw_logo_cpus)
629 size->ws_ypixel -= vt_logo_sprite_height;
630 size->ws_row = size->ws_ypixel;
631 size->ws_col = size->ws_xpixel = vd->vd_width;
633 size->ws_row /= vf->vf_height;
634 size->ws_col /= vf->vf_width;
639 vt_compute_drawable_area(struct vt_window *vw)
641 struct vt_device *vd;
647 if (vw->vw_font == NULL) {
648 vw->vw_draw_area.tr_begin.tp_col = 0;
649 vw->vw_draw_area.tr_begin.tp_row = 0;
650 if (vt_draw_logo_cpus)
651 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height;
652 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
653 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
660 * Compute the drawable area, so that the text is centered on
664 height = vd->vd_height;
665 if (vt_draw_logo_cpus)
666 height -= vt_logo_sprite_height;
667 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
668 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2;
669 if (vt_draw_logo_cpus)
670 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height;
671 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
672 rounddown(vd->vd_width, vf->vf_width);
673 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
674 rounddown(height, vf->vf_height);
678 vt_scroll(struct vt_window *vw, int offset, int whence)
683 if ((vw->vw_flags & VWF_SCROLL) == 0)
686 vt_termsize(vw->vw_device, vw->vw_font, &size);
688 diff = vthistory_seek(&vw->vw_buf, offset, whence);
690 vw->vw_device->vd_flags |= VDF_INVALID;
691 vt_resume_flush_timer(vw, 0);
695 vt_machine_kbdevent(int c)
699 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
701 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
703 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
705 shutdown_nice(RB_HALT);
707 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
708 #ifndef SC_NO_CUTPASTE
709 /* Insert text from cut-paste buffer. */
713 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
715 shutdown_nice(RB_HALT|RB_POWEROFF);
717 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
719 * Request to immediate panic if sysctl
720 * kern.vt.enable_panic_key allow it.
723 panic("Forced by the panic key");
725 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
727 shutdown_nice(RB_AUTOBOOT);
729 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
730 /* Force activatation/deactivation of the screen saver. */
733 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
734 /* Put machine into Stand-By mode. */
735 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
737 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
738 /* Suspend machine. */
739 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
747 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
749 struct vt_device *vd;
753 /* Only special keys handled in ScrollLock mode */
754 if ((c & SPCLKEY) == 0)
760 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
761 vw = vd->vd_windows[c - F_SCR];
762 vt_proc_window_switch(vw);
770 /* Turn scrolling off. */
771 vt_scroll(vw, 0, VHS_END);
772 VTBUF_SLCK_DISABLE(&vw->vw_buf);
773 vw->vw_flags &= ~VWF_SCROLL;
776 case FKEY | F(49): /* Home key. */
777 vt_scroll(vw, 0, VHS_SET);
779 case FKEY | F(50): /* Arrow up. */
780 vt_scroll(vw, -1, VHS_CUR);
782 case FKEY | F(51): /* Page up. */
783 vt_termsize(vd, vw->vw_font, &size);
784 vt_scroll(vw, -size.tp_row, VHS_CUR);
786 case FKEY | F(57): /* End key. */
787 vt_scroll(vw, 0, VHS_END);
789 case FKEY | F(58): /* Arrow down. */
790 vt_scroll(vw, 1, VHS_CUR);
792 case FKEY | F(59): /* Page down. */
793 vt_termsize(vd, vw->vw_font, &size);
794 vt_scroll(vw, size.tp_row, VHS_CUR);
803 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
805 struct vt_window *vw = vd->vd_curwindow;
807 random_harvest_queue(&c, sizeof(c), 1, RANDOM_KEYBOARD);
808 #if VT_ALT_TO_ESC_HACK
810 switch (c & ~RELKEY) {
811 case (SPCLKEY | RALT):
812 if (vt_enable_altgr != 0)
814 case (SPCLKEY | LALT):
815 vd->vd_kbstate &= ~ALKED;
817 /* Other keys ignored for RELKEY event. */
820 switch (c & ~RELKEY) {
821 case (SPCLKEY | RALT):
822 if (vt_enable_altgr != 0)
824 case (SPCLKEY | LALT):
825 vd->vd_kbstate |= ALKED;
830 /* Other keys ignored for RELKEY event. */
834 if (vt_machine_kbdevent(c))
837 if (vw->vw_flags & VWF_SCROLL) {
838 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
839 /* Scroll mode keys handled, nothing to do more. */
846 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
847 vw = vd->vd_windows[c - F_SCR];
848 vt_proc_window_switch(vw);
854 /* Switch to next VT. */
855 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
856 vw = vd->vd_windows[c];
857 vt_proc_window_switch(vw);
860 /* Switch to previous VT. */
861 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS;
862 vw = vd->vd_windows[c];
863 vt_proc_window_switch(vw);
866 vt_save_kbd_state(vw, kbd);
868 if (vw->vw_kbdstate & SLKED) {
869 /* Turn scrolling on. */
870 vw->vw_flags |= VWF_SCROLL;
871 VTBUF_SLCK_ENABLE(&vw->vw_buf);
873 /* Turn scrolling off. */
874 vw->vw_flags &= ~VWF_SCROLL;
875 VTBUF_SLCK_DISABLE(&vw->vw_buf);
876 vt_scroll(vw, 0, VHS_END);
881 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
882 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
883 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
884 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
885 /* F1 through F12 keys. */
886 terminal_input_special(vw->vw_terminal,
887 TKEY_F1 + c - (FKEY | F(1)));
889 case FKEY | F(49): /* Home key. */
890 terminal_input_special(vw->vw_terminal, TKEY_HOME);
892 case FKEY | F(50): /* Arrow up. */
893 terminal_input_special(vw->vw_terminal, TKEY_UP);
895 case FKEY | F(51): /* Page up. */
896 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
898 case FKEY | F(53): /* Arrow left. */
899 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
901 case FKEY | F(55): /* Arrow right. */
902 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
904 case FKEY | F(57): /* End key. */
905 terminal_input_special(vw->vw_terminal, TKEY_END);
907 case FKEY | F(58): /* Arrow down. */
908 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
910 case FKEY | F(59): /* Page down. */
911 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
913 case FKEY | F(60): /* Insert key. */
914 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
916 case FKEY | F(61): /* Delete key. */
917 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
920 } else if (KEYFLAGS(c) == 0) {
921 /* Don't do UTF-8 conversion when doing raw mode. */
922 if (vw->vw_kbdmode == K_XLATE) {
923 #if VT_ALT_TO_ESC_HACK
924 if (vd->vd_kbstate & ALKED) {
926 * Prepend ESC sequence if one of ALT keys down.
928 terminal_input_char(vw->vw_terminal, 0x1b);
932 kdb_alt_break(c, &vd->vd_altbrk);
934 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
936 terminal_input_raw(vw->vw_terminal, c);
942 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
944 struct vt_device *vd = arg;
950 case KBDIO_UNLOADING:
952 vd->vd_keyboard = -1;
953 kbd_release(kbd, (void *)vd);
960 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
961 vt_processkey(kbd, vd, c);
967 vt_allocate_keyboard(struct vt_device *vd)
973 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
975 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
976 k0 = kbd_get_keyboard(idx0);
978 for (idx = kbd_find_keyboard2("*", -1, 0);
980 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
981 k = kbd_get_keyboard(idx);
983 if (idx == idx0 || KBD_IS_BUSY(k))
986 bzero(&ki, sizeof(ki));
987 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
988 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
989 ki.kb_unit = k->kb_unit;
991 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
994 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
995 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
997 DPRINTF(10, "%s: No keyboard found.\n", __func__);
1001 vd->vd_keyboard = idx0;
1002 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
1008 vtterm_bell(struct terminal *tm)
1010 struct vt_window *vw = tm->tm_softc;
1011 struct vt_device *vd = vw->vw_device;
1013 if (!vt_enable_bell)
1016 if (vd->vd_flags & VDF_QUIET_BELL)
1019 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
1023 vtterm_beep(struct terminal *tm, u_int param)
1027 if (!vt_enable_bell)
1030 if ((param == 0) || ((param & 0xffff) == 0)) {
1035 period = ((param >> 16) & 0xffff) * hz / 1000;
1036 freq = 1193182 / (param & 0xffff);
1038 sysbeep(freq, period);
1042 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
1044 struct vt_window *vw = tm->tm_softc;
1046 vtbuf_cursor_position(&vw->vw_buf, p);
1050 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
1052 struct vt_window *vw = tm->tm_softc;
1054 vtbuf_putchar(&vw->vw_buf, p, c);
1058 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
1060 struct vt_window *vw = tm->tm_softc;
1062 vtbuf_fill(&vw->vw_buf, r, c);
1066 vtterm_copy(struct terminal *tm, const term_rect_t *r,
1067 const term_pos_t *p)
1069 struct vt_window *vw = tm->tm_softc;
1071 vtbuf_copy(&vw->vw_buf, r, p);
1075 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
1077 struct vt_window *vw = tm->tm_softc;
1080 case TP_SETLOCALCURSOR:
1082 * 0 means normal (usually block), 1 means hidden, and
1083 * 2 means blinking (always block) for compatibility with
1084 * syscons. We don't support any changes except hiding,
1085 * so must map 2 to 0.
1087 arg = (arg == 1) ? 0 : 1;
1090 vtbuf_cursor_visibility(&vw->vw_buf, arg);
1091 vt_resume_flush_timer(vw, 0);
1094 vw->vw_mouse_level = arg;
1100 vt_determine_colors(term_char_t c, int cursor,
1101 term_color_t *fg, term_color_t *bg)
1108 *fg = TCHAR_FGCOLOR(c);
1109 if (TCHAR_FORMAT(c) & TF_BOLD)
1110 *fg = TCOLOR_LIGHT(*fg);
1111 *bg = TCHAR_BGCOLOR(c);
1112 if (TCHAR_FORMAT(c) & TF_BLINK)
1113 *bg = TCOLOR_LIGHT(*bg);
1115 if (TCHAR_FORMAT(c) & TF_REVERSE)
1127 #ifndef SC_NO_CUTPASTE
1129 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1131 unsigned int mx, my;
1134 * We use the cursor position saved during the current refresh,
1135 * in case the cursor moved since.
1137 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1138 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1140 if (mx >= area->tr_end.tp_col ||
1141 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col ||
1142 my >= area->tr_end.tp_row ||
1143 my + vd->vd_mcursor->height <= area->tr_begin.tp_row)
1149 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked)
1152 struct vt_window *vw;
1156 vw = vd->vd_curwindow;
1159 x = vd->vd_mx_drawn;
1160 y = vd->vd_my_drawn;
1163 area.tr_begin.tp_col = x / vf->vf_width;
1164 area.tr_begin.tp_row = y / vf->vf_height;
1165 area.tr_end.tp_col =
1166 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1167 area.tr_end.tp_row =
1168 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1171 * No font loaded (ie. vt_vga operating in textmode).
1173 * FIXME: This fake area needs to be revisited once the
1174 * mouse cursor is supported in vt_vga's textmode.
1176 area.tr_begin.tp_col = x;
1177 area.tr_begin.tp_row = y;
1178 area.tr_end.tp_col = x + 2;
1179 area.tr_end.tp_row = y + 2;
1183 vtbuf_lock(&vw->vw_buf);
1184 vtbuf_dirty(&vw->vw_buf, &area);
1186 vtbuf_unlock(&vw->vw_buf);
1191 vt_set_border(struct vt_device *vd, const term_rect_t *area,
1192 const term_color_t c)
1194 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect;
1196 if (drawrect == NULL)
1200 if (area->tr_begin.tp_row > 0)
1201 drawrect(vd, 0, 0, vd->vd_width - 1,
1202 area->tr_begin.tp_row - 1, 1, c);
1205 if (area->tr_begin.tp_col > 0)
1206 drawrect(vd, 0, area->tr_begin.tp_row,
1207 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c);
1210 if (area->tr_end.tp_col < vd->vd_width)
1211 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row,
1212 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c);
1215 if (area->tr_end.tp_row < vd->vd_height)
1216 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1,
1217 vd->vd_height - 1, 1, c);
1221 vt_flush(struct vt_device *vd)
1223 struct vt_window *vw;
1226 #ifndef SC_NO_CUTPASTE
1227 int cursor_was_shown, cursor_moved;
1230 vw = vd->vd_curwindow;
1234 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1238 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1241 vtbuf_lock(&vw->vw_buf);
1243 #ifndef SC_NO_CUTPASTE
1244 cursor_was_shown = vd->vd_mshown;
1245 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1246 vd->vd_my != vd->vd_my_drawn);
1248 /* Check if the cursor should be displayed or not. */
1249 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1250 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1251 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1258 * If the cursor changed display state or moved, we must mark
1259 * the old position as dirty, so that it's erased.
1261 if (cursor_was_shown != vd->vd_mshown ||
1262 (vd->vd_mshown && cursor_moved))
1263 vt_mark_mouse_position_as_dirty(vd, true);
1266 * Save position of the mouse cursor. It's used by backends to
1267 * know where to draw the cursor and during the next refresh to
1268 * erase the previous position.
1270 vd->vd_mx_drawn = vd->vd_mx;
1271 vd->vd_my_drawn = vd->vd_my;
1274 * If the cursor is displayed and has moved since last refresh,
1275 * mark the new position as dirty.
1277 if (vd->vd_mshown && cursor_moved)
1278 vt_mark_mouse_position_as_dirty(vd, true);
1281 vtbuf_undirty(&vw->vw_buf, &tarea);
1283 /* Force a full redraw when the screen contents are invalid. */
1284 if (vd->vd_flags & VDF_INVALID) {
1285 vd->vd_flags &= ~VDF_INVALID;
1287 vt_set_border(vd, &vw->vw_draw_area, TC_BLACK);
1288 vt_termrect(vd, vf, &tarea);
1289 if (vt_draw_logo_cpus)
1290 vtterm_draw_cpu_logos(vd);
1293 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1294 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1295 vtbuf_unlock(&vw->vw_buf);
1299 vtbuf_unlock(&vw->vw_buf);
1306 struct vt_device *vd;
1310 /* Update screen if required. */
1311 changed = vt_flush(vd);
1313 /* Schedule for next update. */
1315 vt_schedule_flush(vd, 0);
1317 vd->vd_timer_armed = 0;
1321 vtterm_pre_input(struct terminal *tm)
1323 struct vt_window *vw = tm->tm_softc;
1325 vtbuf_lock(&vw->vw_buf);
1329 vtterm_post_input(struct terminal *tm)
1331 struct vt_window *vw = tm->tm_softc;
1333 vtbuf_unlock(&vw->vw_buf);
1334 vt_resume_flush_timer(vw, 0);
1338 vtterm_done(struct terminal *tm)
1340 struct vt_window *vw = tm->tm_softc;
1341 struct vt_device *vd = vw->vw_device;
1343 if (kdb_active || panicstr != NULL) {
1344 /* Switch to the debugger. */
1345 if (vd->vd_curwindow != vw) {
1346 vd->vd_curwindow = vw;
1347 vd->vd_flags |= VDF_INVALID;
1348 if (vd->vd_driver->vd_postswitch)
1349 vd->vd_driver->vd_postswitch(vd);
1351 vd->vd_flags &= ~VDF_SPLASH;
1353 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1360 vtterm_splash(struct vt_device *vd)
1362 vt_axis_t top, left;
1364 /* Display a nice boot splash. */
1365 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1367 top = (vd->vd_height - vt_logo_height) / 2;
1368 left = (vd->vd_width - vt_logo_width) / 2;
1369 switch (vt_logo_depth) {
1371 /* XXX: Unhardcode colors! */
1372 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1373 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1374 left, top, TC_WHITE, TC_BLACK);
1376 vd->vd_flags |= VDF_SPLASH;
1383 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1385 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1386 struct vt_window *vw = tm->tm_softc;
1387 struct vt_device *vd = vw->vw_device;
1392 if (!vty_enabled(VTY_VT))
1395 if (vd->vd_flags & VDF_INITIALIZED)
1396 /* Initialization already done. */
1399 SET_FOREACH(vtdlist, vt_drv_set) {
1401 if (vtd->vd_probe == NULL)
1403 if (vtd->vd_probe(vd) == CN_DEAD)
1405 if ((vtdbest == NULL) ||
1406 (vtd->vd_priority > vtdbest->vd_priority))
1409 if (vtdbest == NULL) {
1410 cp->cn_pri = CN_DEAD;
1411 vd->vd_flags |= VDF_DEAD;
1413 vd->vd_driver = vtdbest;
1414 cp->cn_pri = vd->vd_driver->vd_init(vd);
1417 /* Check if driver's vt_init return CN_DEAD. */
1418 if (cp->cn_pri == CN_DEAD) {
1419 vd->vd_flags |= VDF_DEAD;
1422 /* Initialize any early-boot keyboard drivers */
1423 kbd_configure(KB_CONF_PROBE_ONLY);
1425 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1426 vd->vd_windows[VT_CONSWINDOW] = vw;
1427 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1429 /* Attach default font if not in TEXTMODE. */
1430 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1431 vw->vw_font = vtfont_ref(&vt_font_default);
1432 vt_compute_drawable_area(vw);
1436 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1437 * that we have the real viewable size, fix it in the static
1440 if (vd->vd_width != 0 && vd->vd_height != 0)
1441 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1443 vtbuf_init_early(&vw->vw_buf);
1444 vt_winsize(vd, vw->vw_font, &wsz);
1445 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1447 attr.ta_format = TCHAR_FORMAT(c);
1448 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1449 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1450 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1452 if (vtdbest != NULL) {
1457 vd->vd_flags |= VDF_INITIALIZED;
1462 vtterm_cngetc(struct terminal *tm)
1464 struct vt_window *vw = tm->tm_softc;
1465 struct vt_device *vd = vw->vw_device;
1469 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1470 return (*vw->vw_kbdsq++);
1472 /* Make sure the splash screen is not there. */
1473 if (vd->vd_flags & VDF_SPLASH) {
1475 vd->vd_flags &= ~VDF_SPLASH;
1476 /* Mark screen as invalid to force update */
1477 vd->vd_flags |= VDF_INVALID;
1481 /* Stripped down keyboard handler. */
1482 kbd = kbd_get_keyboard(vd->vd_keyboard);
1486 /* Force keyboard input mode to K_XLATE */
1487 vw->vw_kbdmode = K_XLATE;
1488 vt_update_kbd_mode(vw, kbd);
1490 /* Switch the keyboard to polling to make it work here. */
1491 kbdd_poll(kbd, TRUE);
1492 c = kbdd_read_char(kbd, 0);
1493 kbdd_poll(kbd, FALSE);
1497 if (vw->vw_flags & VWF_SCROLL) {
1498 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1503 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1507 vt_save_kbd_state(vw, kbd);
1508 if (vw->vw_kbdstate & SLKED) {
1509 /* Turn scrolling on. */
1510 vw->vw_flags |= VWF_SCROLL;
1511 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1513 /* Turn scrolling off. */
1514 vt_scroll(vw, 0, VHS_END);
1515 vw->vw_flags &= ~VWF_SCROLL;
1516 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1519 /* XXX: KDB can handle history. */
1520 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1521 vw->vw_kbdsq = "\x1b[A";
1523 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1524 vw->vw_kbdsq = "\x1b[B";
1526 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1527 vw->vw_kbdsq = "\x1b[C";
1529 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1530 vw->vw_kbdsq = "\x1b[D";
1534 /* Force refresh to make scrollback work. */
1536 } else if (KEYFLAGS(c) == 0) {
1537 return (KEYCHAR(c));
1540 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1541 return (*vw->vw_kbdsq++);
1547 vtterm_cngrab(struct terminal *tm)
1549 struct vt_device *vd;
1550 struct vt_window *vw;
1557 vt_window_switch(vw);
1559 kbd = kbd_get_keyboard(vd->vd_keyboard);
1563 if (vw->vw_grabbed++ > 0)
1567 * Make sure the keyboard is accessible even when the kbd device
1568 * driver is disabled.
1572 /* We shall always use the keyboard in the XLATE mode here. */
1573 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1574 vw->vw_kbdmode = K_XLATE;
1575 vt_update_kbd_mode(vw, kbd);
1577 kbdd_poll(kbd, TRUE);
1581 vtterm_cnungrab(struct terminal *tm)
1583 struct vt_device *vd;
1584 struct vt_window *vw;
1590 kbd = kbd_get_keyboard(vd->vd_keyboard);
1594 if (--vw->vw_grabbed > 0)
1597 kbdd_poll(kbd, FALSE);
1599 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1600 vt_update_kbd_mode(vw, kbd);
1605 vtterm_opened(struct terminal *tm, int opened)
1607 struct vt_window *vw = tm->tm_softc;
1608 struct vt_device *vd = vw->vw_device;
1611 vd->vd_flags &= ~VDF_SPLASH;
1613 vw->vw_flags |= VWF_OPENED;
1615 vw->vw_flags &= ~VWF_OPENED;
1616 /* TODO: finish ACQ/REL */
1622 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1624 struct vt_device *vd = vw->vw_device;
1625 struct terminal *tm = vw->vw_terminal;
1632 * Changing fonts is a little tricky. We must prevent
1633 * simultaneous access to the device, so we must stop
1634 * the display timer and the terminal from accessing.
1635 * We need to switch fonts and grow our screen buffer.
1637 * XXX: Right now the code uses terminal_mute() to
1638 * prevent data from reaching the console driver while
1639 * resizing the screen buffer. This isn't elegant...
1643 if (vw->vw_flags & VWF_BUSY) {
1644 /* Another process is changing the font. */
1648 vw->vw_flags |= VWF_BUSY;
1651 vt_termsize(vd, vf, &size);
1652 vt_winsize(vd, vf, &wsz);
1654 /* Grow the screen buffer and terminal. */
1655 terminal_mute(tm, 1);
1656 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1657 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1658 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1659 terminal_mute(tm, 0);
1661 /* Actually apply the font to the current window. */
1663 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1665 * In case vt_change_font called to update size we don't need
1666 * to update font link.
1668 vtfont_unref(vw->vw_font);
1669 vw->vw_font = vtfont_ref(vf);
1673 * Compute the drawable area and move the mouse cursor inside
1674 * it, in case the new area is smaller than the previous one.
1676 vt_compute_drawable_area(vw);
1677 vd->vd_mx = min(vd->vd_mx,
1678 vw->vw_draw_area.tr_end.tp_col -
1679 vw->vw_draw_area.tr_begin.tp_col - 1);
1680 vd->vd_my = min(vd->vd_my,
1681 vw->vw_draw_area.tr_end.tp_row -
1682 vw->vw_draw_area.tr_begin.tp_row - 1);
1684 /* Force a full redraw the next timer tick. */
1685 if (vd->vd_curwindow == vw) {
1686 vd->vd_flags |= VDF_INVALID;
1687 vt_resume_flush_timer(vw, 0);
1689 vw->vw_flags &= ~VWF_BUSY;
1695 vt_proc_alive(struct vt_window *vw)
1699 if (vw->vw_smode.mode != VT_PROCESS)
1703 if ((p = pfind(vw->vw_pid)) != NULL)
1705 if (vw->vw_proc == p)
1708 vw->vw_smode.mode = VT_AUTO;
1709 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1716 signal_vt_rel(struct vt_window *vw)
1719 if (vw->vw_smode.mode != VT_PROCESS)
1721 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1726 vw->vw_flags |= VWF_SWWAIT_REL;
1727 PROC_LOCK(vw->vw_proc);
1728 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1729 PROC_UNLOCK(vw->vw_proc);
1730 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1735 signal_vt_acq(struct vt_window *vw)
1738 if (vw->vw_smode.mode != VT_PROCESS)
1740 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1741 cnavailable(vw->vw_terminal->consdev, FALSE);
1742 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1747 vw->vw_flags |= VWF_SWWAIT_ACQ;
1748 PROC_LOCK(vw->vw_proc);
1749 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1750 PROC_UNLOCK(vw->vw_proc);
1751 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1756 finish_vt_rel(struct vt_window *vw, int release, int *s)
1759 if (vw->vw_flags & VWF_SWWAIT_REL) {
1760 vw->vw_flags &= ~VWF_SWWAIT_REL;
1762 callout_drain(&vw->vw_proc_dead_timer);
1763 vt_late_window_switch(vw->vw_switch_to);
1771 finish_vt_acq(struct vt_window *vw)
1774 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1775 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1781 #ifndef SC_NO_CUTPASTE
1783 vt_mouse_terminput_button(struct vt_device *vd, int button)
1785 struct vt_window *vw;
1787 char mouseb[6] = "\x1B[M";
1790 vw = vd->vd_curwindow;
1793 /* Translate to char position. */
1794 x = vd->vd_mx / vf->vf_width;
1795 y = vd->vd_my / vf->vf_height;
1796 /* Avoid overflow. */
1797 x = MIN(x, 255 - '!');
1798 y = MIN(y, 255 - '!');
1800 mouseb[3] = ' ' + button;
1801 mouseb[4] = '!' + x;
1802 mouseb[5] = '!' + y;
1804 for (i = 0; i < sizeof(mouseb); i++)
1805 terminal_input_char(vw->vw_terminal, mouseb[i]);
1809 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1814 case MOUSE_BUTTON_EVENT:
1816 /* Mouse button pressed. */
1817 if (event & MOUSE_BUTTON1DOWN)
1818 vt_mouse_terminput_button(vd, 0);
1819 if (event & MOUSE_BUTTON2DOWN)
1820 vt_mouse_terminput_button(vd, 1);
1821 if (event & MOUSE_BUTTON3DOWN)
1822 vt_mouse_terminput_button(vd, 2);
1824 /* Mouse button released. */
1825 vt_mouse_terminput_button(vd, 3);
1829 case MOUSE_MOTION_EVENT:
1830 if (mouse->u.data.z < 0) {
1832 sc_mouse_input_button(vd, 64);
1833 } else if (mouse->u.data.z > 0) {
1835 sc_mouse_input_button(vd, 65);
1848 len = VD_PASTEBUFLEN(main_vd);
1849 buf = VD_PASTEBUF(main_vd);
1850 len /= sizeof(term_char_t);
1851 for (i = 0; i < len; i++) {
1854 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1860 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1862 struct vt_device *vd;
1863 struct vt_window *vw;
1869 vw = vd->vd_curwindow;
1873 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1875 * Either the mouse is disabled, or the window is in
1876 * "graphics mode". The graphics mode is usually set by
1877 * an X server, using the KDSETMODE ioctl.
1881 if (vf == NULL) /* Text mode. */
1885 * TODO: add flag about pointer position changed, to not redraw chars
1886 * under mouse pointer when nothing changed.
1889 if (vw->vw_mouse_level > 0)
1890 vt_mouse_terminput(vd, type, x, y, event, cnt);
1894 case MOUSE_MOTION_EVENT:
1899 vt_termsize(vd, vf, &size);
1904 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1905 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1909 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1910 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1911 vd->vd_mx / vf->vf_width,
1912 vd->vd_my / vf->vf_height);
1914 vt_resume_flush_timer(vw, 0);
1916 case MOUSE_BUTTON_EVENT:
1924 case MOUSE_BUTTON1DOWN:
1927 mark = VTB_MARK_END;
1929 case 1: /* single click: start cut operation */
1930 mark = VTB_MARK_START;
1932 case 2: /* double click: cut a word */
1933 mark = VTB_MARK_WORD;
1935 case 3: /* triple click: cut a line */
1936 mark = VTB_MARK_ROW;
1940 case VT_MOUSE_PASTEBUTTON:
1949 case VT_MOUSE_EXTENDBUTTON:
1952 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1953 mark = VTB_MARK_EXTEND;
1958 mark = VTB_MARK_EXTEND;
1966 /* Save buttons state. */
1968 vd->vd_mstate |= event;
1970 vd->vd_mstate &= ~event;
1972 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1973 vd->vd_my / vf->vf_height) == 1) {
1975 * We have something marked to copy, so update pointer to
1976 * window with selection.
1978 vt_resume_flush_timer(vw, 0);
1984 case VTB_MARK_EXTEND:
1987 /* Other types of mark do not require to copy data. */
1991 /* Get current selection size in bytes. */
1992 len = vtbuf_get_marked_len(&vw->vw_buf);
1996 /* Reallocate buffer only if old one is too small. */
1997 if (len > VD_PASTEBUFSZ(vd)) {
1998 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
2000 /* Update buffer size. */
2001 VD_PASTEBUFSZ(vd) = len;
2003 /* Request copy/paste buffer data, no more than `len' */
2004 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
2007 VD_PASTEBUFLEN(vd) = len;
2009 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
2014 vt_mouse_state(int show)
2016 struct vt_device *vd;
2017 struct vt_window *vw;
2020 vw = vd->vd_curwindow;
2024 vw->vw_flags |= VWF_MOUSE_HIDE;
2027 vw->vw_flags &= ~VWF_MOUSE_HIDE;
2031 /* Mark mouse position as dirty. */
2032 vt_mark_mouse_position_as_dirty(vd, false);
2033 vt_resume_flush_timer(vw, 0);
2038 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
2039 int nprot, vm_memattr_t *memattr)
2041 struct vt_window *vw = tm->tm_softc;
2042 struct vt_device *vd = vw->vw_device;
2044 if (vd->vd_driver->vd_fb_mmap)
2045 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
2052 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
2055 struct vt_window *vw = tm->tm_softc;
2056 struct vt_device *vd = vw->vw_device;
2059 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
2060 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
2071 cmd = VT_WAITACTIVE;
2097 ival = IOCPARM_IVAL(data);
2098 data = (caddr_t)&ival;
2103 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
2104 if (*(int *)data & ~0x7f)
2109 case GIO_DEADKEYMAP:
2110 case PIO_DEADKEYMAP:
2115 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2116 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2117 case KBADDKBD: /* add/remove keyboard to/from mux */
2122 kbd = kbd_get_keyboard(vd->vd_keyboard);
2124 error = kbdd_ioctl(kbd, cmd, data);
2126 if (error == ENOIOCTL) {
2127 if (cmd == KDGKBTYPE) {
2128 /* always return something? XXX */
2136 case KDGKBSTATE: { /* get keyboard state (locks) */
2139 if (vw == vd->vd_curwindow) {
2141 kbd = kbd_get_keyboard(vd->vd_keyboard);
2143 error = vt_save_kbd_state(vw, kbd);
2150 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2154 case KDSKBSTATE: { /* set keyboard state (locks) */
2157 state = *(int *)data;
2158 if (state & ~LOCK_MASK)
2161 vw->vw_kbdstate &= ~LOCK_MASK;
2162 vw->vw_kbdstate |= state;
2165 if (vw == vd->vd_curwindow) {
2167 kbd = kbd_get_keyboard(vd->vd_keyboard);
2169 error = vt_update_kbd_state(vw, kbd);
2175 case KDGETLED: { /* get keyboard LED status */
2178 if (vw == vd->vd_curwindow) {
2180 kbd = kbd_get_keyboard(vd->vd_keyboard);
2182 error = vt_save_kbd_leds(vw, kbd);
2189 *(int *)data = vw->vw_kbdstate & LED_MASK;
2193 case KDSETLED: { /* set keyboard LED status */
2196 leds = *(int *)data;
2197 if (leds & ~LED_MASK)
2200 vw->vw_kbdstate &= ~LED_MASK;
2201 vw->vw_kbdstate |= leds;
2204 if (vw == vd->vd_curwindow) {
2206 kbd = kbd_get_keyboard(vd->vd_keyboard);
2208 error = vt_update_kbd_leds(vw, kbd);
2215 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ?
2216 KD_GRAPHICS : KD_TEXT;
2221 if (vw == vd->vd_curwindow) {
2223 kbd = kbd_get_keyboard(vd->vd_keyboard);
2225 error = vt_save_kbd_mode(vw, kbd);
2232 *(int *)data = vw->vw_kbdmode;
2239 mode = *(int *)data;
2244 vw->vw_kbdmode = mode;
2247 if (vw == vd->vd_curwindow) {
2249 kbd = kbd_get_keyboard(vd->vd_keyboard);
2251 error = vt_update_kbd_mode(vw, kbd);
2261 case FBIO_GETWINORG: /* get frame buffer window origin */
2262 case FBIO_GETDISPSTART: /* get display start address */
2263 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2264 case FBIO_BLANK: /* blank display */
2265 if (vd->vd_driver->vd_fb_ioctl)
2266 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2268 case CONS_BLANKTIME:
2272 if (*(int *)data < 0)
2274 if (*(int *)data != vd->vd_curwindow->vw_buf.vb_history_size)
2275 vtbuf_sethistory_size(&vd->vd_curwindow->vw_buf,
2280 *(int *)data = M_CG640x480;
2282 case CONS_BELLTYPE: /* set bell type sound */
2283 if ((*(int *)data) & CONS_QUIET_BELL)
2284 vd->vd_flags |= VDF_QUIET_BELL;
2286 vd->vd_flags &= ~VDF_QUIET_BELL;
2288 case CONS_GETINFO: {
2289 vid_info_t *vi = (vid_info_t *)data;
2290 if (vi->size != sizeof(struct vid_info))
2293 if (vw == vd->vd_curwindow) {
2295 kbd = kbd_get_keyboard(vd->vd_keyboard);
2297 vt_save_kbd_state(vw, kbd);
2301 vi->m_num = vd->vd_curwindow->vw_number + 1;
2302 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2303 /* XXX: other fields! */
2307 *(int *)data = 0x200;
2312 case CONS_MOUSECTL: {
2313 mouse_info_t *mouse = (mouse_info_t*)data;
2316 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2317 * should not be applied to individual TTYs, but only to
2320 switch (mouse->operation) {
2322 if (vd->vd_flags & VDF_MOUSECURSOR) {
2323 vd->vd_flags &= ~VDF_MOUSECURSOR;
2324 #ifndef SC_NO_CUTPASTE
2325 vt_mouse_state(VT_MOUSE_HIDE);
2330 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2331 vd->vd_flags |= VDF_MOUSECURSOR;
2332 vd->vd_mx = vd->vd_width / 2;
2333 vd->vd_my = vd->vd_height / 2;
2334 #ifndef SC_NO_CUTPASTE
2335 vt_mouse_state(VT_MOUSE_SHOW);
2346 if (vd->vd_flags & VDF_TEXTMODE)
2349 error = vtfont_load((void *)data, &vf);
2353 error = vt_change_font(vw, vf);
2357 case PIO_VFONT_DEFAULT: {
2358 /* Reset to default font. */
2359 error = vt_change_font(vw, &vt_font_default);
2363 scrmap_t *sm = (scrmap_t *)data;
2365 /* We don't have screen maps, so return a handcrafted one. */
2366 for (i = 0; i < 256; i++)
2372 * FIXME: This implementation is incomplete compared to
2375 switch (*(int *)data) {
2379 vw->vw_flags &= ~VWF_GRAPHICS;
2382 vw->vw_flags |= VWF_GRAPHICS;
2386 case KDENABIO: /* allow io operations */
2387 error = priv_check(td, PRIV_IO);
2390 error = securelevel_gt(td->td_ucred, 0);
2393 #if defined(__i386__)
2394 td->td_frame->tf_eflags |= PSL_IOPL;
2395 #elif defined(__amd64__)
2396 td->td_frame->tf_rflags |= PSL_IOPL;
2399 case KDDISABIO: /* disallow io operations (default) */
2400 #if defined(__i386__)
2401 td->td_frame->tf_eflags &= ~PSL_IOPL;
2402 #elif defined(__amd64__)
2403 td->td_frame->tf_rflags &= ~PSL_IOPL;
2406 case KDMKTONE: /* sound the bell */
2407 vtterm_beep(tm, *(u_int *)data);
2409 case KIOCSOUND: /* make tone (*data) hz */
2412 case CONS_SETKBD: /* set the new keyboard */
2415 if (vd->vd_keyboard != *(int *)data) {
2416 kbd = kbd_get_keyboard(*(int *)data);
2421 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2422 (void *)vd, vt_kbdevent, vd);
2424 if (vd->vd_keyboard != -1) {
2425 kbd = kbd_get_keyboard(vd->vd_keyboard);
2426 vt_save_kbd_state(vd->vd_curwindow, kbd);
2427 kbd_release(kbd, (void *)vd);
2429 kbd = kbd_get_keyboard(i);
2430 vd->vd_keyboard = i;
2432 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2433 vt_update_kbd_state(vd->vd_curwindow, kbd);
2435 error = EPERM; /* XXX */
2440 case CONS_RELKBD: /* release the current keyboard */
2443 if (vd->vd_keyboard != -1) {
2444 kbd = kbd_get_keyboard(vd->vd_keyboard);
2449 vt_save_kbd_state(vd->vd_curwindow, kbd);
2450 error = kbd_release(kbd, (void *)vd);
2452 vd->vd_keyboard = -1;
2459 win = *(int *)data - 1;
2460 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2462 if ((win >= VT_MAXWINDOWS) || (win < 0))
2464 return (vt_proc_window_switch(vd->vd_windows[win]));
2467 *(int *)data = vd->vd_curwindow->vw_number + 1;
2470 *(int *)data = vw->vw_number + 1;
2473 /* TODO: Check current state, switching can be in progress. */
2474 if ((*(int *)data) == 0x01)
2475 vw->vw_flags |= VWF_VTYLOCK;
2476 else if ((*(int *)data) == 0x02)
2477 vw->vw_flags &= ~VWF_VTYLOCK;
2483 for (i = 0; i < VT_MAXWINDOWS; i++) {
2484 vw = vd->vd_windows[i];
2487 if (!(vw->vw_flags & VWF_OPENED)) {
2488 *(int *)data = vw->vw_number + 1;
2495 case VT_WAITACTIVE: {
2500 idx = *(unsigned int *)data;
2501 if (idx > VT_MAXWINDOWS)
2504 vw = vd->vd_windows[idx - 1];
2507 while (vd->vd_curwindow != vw && error == 0)
2508 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2512 case VT_SETMODE: { /* set screen switcher mode */
2513 struct vt_mode *mode;
2516 mode = (struct vt_mode *)data;
2517 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2518 if (vw->vw_smode.mode == VT_PROCESS) {
2519 p1 = pfind(vw->vw_pid);
2520 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2523 DPRINTF(5, "error EPERM\n");
2529 if (mode->mode == VT_AUTO) {
2530 vw->vw_smode.mode = VT_AUTO;
2533 DPRINTF(5, "VT_AUTO, ");
2534 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2535 cnavailable(vw->vw_terminal->consdev, TRUE);
2536 /* were we in the middle of the vty switching process? */
2537 if (finish_vt_rel(vw, TRUE, &s) == 0)
2538 DPRINTF(5, "reset WAIT_REL, ");
2539 if (finish_vt_acq(vw) == 0)
2540 DPRINTF(5, "reset WAIT_ACQ, ");
2542 } else if (mode->mode == VT_PROCESS) {
2543 if (!ISSIGVALID(mode->relsig) ||
2544 !ISSIGVALID(mode->acqsig) ||
2545 !ISSIGVALID(mode->frsig)) {
2546 DPRINTF(5, "error EINVAL\n");
2549 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2550 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2551 vw->vw_proc = td->td_proc;
2552 vw->vw_pid = vw->vw_proc->p_pid;
2553 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2554 cnavailable(vw->vw_terminal->consdev, FALSE);
2556 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2563 case VT_GETMODE: /* get screen switcher mode */
2564 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2567 case VT_RELDISP: /* screen switcher ioctl */
2569 * This must be the current vty which is in the VT_PROCESS
2572 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2576 /* ...and this process is controlling it. */
2577 if (vw->vw_proc != td->td_proc) {
2581 switch(*(int *)data) {
2582 case VT_FALSE: /* user refuses to release screen, abort */
2583 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2584 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2585 SC_DRIVER_NAME, VT_UNIT(vw));
2587 case VT_TRUE: /* user has released screen, go on */
2588 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2589 if (vw->vw_flags & VWF_SWWAIT_REL) {
2590 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2591 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2592 SC_DRIVER_NAME, VT_UNIT(vw));
2597 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2598 if ((error = finish_vt_acq(vw)) == 0)
2599 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2600 SC_DRIVER_NAME, VT_UNIT(vw));
2611 static struct vt_window *
2612 vt_allocate_window(struct vt_device *vd, unsigned int window)
2614 struct vt_window *vw;
2615 struct terminal *tm;
2619 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2621 vw->vw_number = window;
2622 vw->vw_kbdmode = K_XLATE;
2624 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2625 vw->vw_font = vtfont_ref(&vt_font_default);
2626 vt_compute_drawable_area(vw);
2629 vt_termsize(vd, vw->vw_font, &size);
2630 vt_winsize(vd, vw->vw_font, &wsz);
2631 vtbuf_init(&vw->vw_buf, &size);
2633 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2634 terminal_set_winsize(tm, &wsz);
2635 vd->vd_windows[window] = vw;
2636 callout_init(&vw->vw_proc_dead_timer, 0);
2642 vt_upgrade(struct vt_device *vd)
2644 struct vt_window *vw;
2646 int register_handlers;
2648 if (!vty_enabled(VTY_VT))
2650 if (main_vd->vd_driver == NULL)
2653 for (i = 0; i < VT_MAXWINDOWS; i++) {
2654 vw = vd->vd_windows[i];
2657 vw = vt_allocate_window(vd, i);
2659 if (!(vw->vw_flags & VWF_READY)) {
2660 callout_init(&vw->vw_proc_dead_timer, 0);
2661 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2662 vw->vw_flags |= VWF_READY;
2663 if (vw->vw_flags & VWF_CONSOLE) {
2664 /* For existing console window. */
2665 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2666 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2672 if (vd->vd_curwindow == NULL)
2673 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2675 register_handlers = 0;
2676 if (!(vd->vd_flags & VDF_ASYNC)) {
2677 /* Attach keyboard. */
2678 vt_allocate_keyboard(vd);
2680 /* Init 25 Hz timer. */
2681 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2684 * Start timer when everything ready.
2685 * Note that the operations here are purposefully ordered.
2686 * We need to ensure vd_timer_armed is non-zero before we set
2687 * the VDF_ASYNC flag. That prevents this function from
2688 * racing with vt_resume_flush_timer() to update the
2689 * callout structure.
2691 atomic_add_acq_int(&vd->vd_timer_armed, 1);
2692 vd->vd_flags |= VDF_ASYNC;
2693 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2694 register_handlers = 1;
2699 /* Refill settings with new sizes. */
2702 if (register_handlers) {
2703 /* Register suspend/resume handlers. */
2704 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2705 vd, EVENTHANDLER_PRI_ANY);
2706 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2707 EVENTHANDLER_PRI_ANY);
2712 vt_resize(struct vt_device *vd)
2714 struct vt_window *vw;
2717 for (i = 0; i < VT_MAXWINDOWS; i++) {
2718 vw = vd->vd_windows[i];
2720 /* Assign default font to window, if not textmode. */
2721 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2722 vw->vw_font = vtfont_ref(&vt_font_default);
2725 /* Resize terminal windows */
2726 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2727 DPRINTF(100, "%s: vt_change_font() is busy, "
2728 "window %d\n", __func__, i);
2734 vt_replace_backend(const struct vt_driver *drv, void *softc)
2736 struct vt_device *vd;
2740 if (vd->vd_flags & VDF_ASYNC) {
2741 /* Stop vt_flush periodic task. */
2743 vt_suspend_flush_timer(vd);
2746 * Mute current terminal until we done. vt_change_font (called
2747 * from vt_resize) will unmute it.
2749 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2753 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2757 vd->vd_flags &= ~VDF_TEXTMODE;
2761 * We want to upgrade from the current driver to the
2765 vd->vd_prev_driver = vd->vd_driver;
2766 vd->vd_prev_softc = vd->vd_softc;
2767 vd->vd_driver = drv;
2768 vd->vd_softc = softc;
2770 vd->vd_driver->vd_init(vd);
2771 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) {
2773 * No driver given: we want to downgrade to the previous
2776 const struct vt_driver *old_drv;
2779 old_drv = vd->vd_driver;
2780 old_softc = vd->vd_softc;
2782 vd->vd_driver = vd->vd_prev_driver;
2783 vd->vd_softc = vd->vd_prev_softc;
2784 vd->vd_prev_driver = NULL;
2785 vd->vd_prev_softc = NULL;
2787 vd->vd_flags |= VDF_DOWNGRADE;
2789 vd->vd_driver->vd_init(vd);
2791 if (old_drv->vd_fini)
2792 old_drv->vd_fini(vd, old_softc);
2794 vd->vd_flags &= ~VDF_DOWNGRADE;
2799 /* Update windows sizes and initialize last items. */
2803 if (vd->vd_flags & VDF_SPLASH)
2807 if (vd->vd_flags & VDF_ASYNC) {
2808 /* Allow to put chars now. */
2809 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2810 /* Rerun timer for screen updates. */
2811 vt_resume_flush_timer(vd->vd_curwindow, 0);
2815 * Register as console. If it already registered, cnadd() will ignore
2818 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2822 vt_suspend_handler(void *priv)
2824 struct vt_device *vd;
2827 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2828 vd->vd_driver->vd_suspend(vd);
2832 vt_resume_handler(void *priv)
2834 struct vt_device *vd;
2837 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2838 vd->vd_driver->vd_resume(vd);
2842 vt_allocate(const struct vt_driver *drv, void *softc)
2845 if (!vty_enabled(VTY_VT))
2848 if (main_vd->vd_driver == NULL) {
2849 main_vd->vd_driver = drv;
2850 printf("VT: initialize with new VT driver \"%s\".\n",
2854 * Check if have rights to replace current driver. For example:
2855 * it is bad idea to replace KMS driver with generic VGA one.
2857 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2858 printf("VT: Driver priority %d too low. Current %d\n ",
2859 drv->vd_priority, main_vd->vd_driver->vd_priority);
2862 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2863 main_vd->vd_driver->vd_name, drv->vd_name);
2866 vt_replace_backend(drv, softc);
2870 vt_deallocate(const struct vt_driver *drv, void *softc)
2873 if (!vty_enabled(VTY_VT))
2876 if (main_vd->vd_prev_driver == NULL ||
2877 main_vd->vd_driver != drv ||
2878 main_vd->vd_softc != softc)
2881 printf("VT: Switching back from \"%s\" to \"%s\".\n",
2882 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name);
2884 vt_replace_backend(NULL, NULL);
2888 vt_suspend(struct vt_device *vd)
2892 if (vt_suspendswitch == 0)
2894 /* Save current window. */
2895 vd->vd_savedwindow = vd->vd_curwindow;
2896 /* Ask holding process to free window and switch to console window */
2897 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2899 /* Wait for the window switch to complete. */
2902 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2903 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2908 vt_resume(struct vt_device *vd)
2911 if (vt_suspendswitch == 0)
2913 /* Switch back to saved window, if any */
2914 vt_proc_window_switch(vd->vd_savedwindow);
2915 vd->vd_savedwindow = NULL;