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/reboot.h>
52 #include <sys/systm.h>
53 #include <sys/terminal.h>
55 #include <dev/kbd/kbdreg.h>
56 #include <dev/vt/vt.h>
58 #if defined(__i386__) || defined(__amd64__)
59 #include <machine/psl.h>
60 #include <machine/frame.h>
63 static tc_bell_t vtterm_bell;
64 static tc_cursor_t vtterm_cursor;
65 static tc_putchar_t vtterm_putchar;
66 static tc_fill_t vtterm_fill;
67 static tc_copy_t vtterm_copy;
68 static tc_param_t vtterm_param;
69 static tc_done_t vtterm_done;
71 static tc_cnprobe_t vtterm_cnprobe;
72 static tc_cngetc_t vtterm_cngetc;
74 static tc_cngrab_t vtterm_cngrab;
75 static tc_cnungrab_t vtterm_cnungrab;
77 static tc_opened_t vtterm_opened;
78 static tc_ioctl_t vtterm_ioctl;
79 static tc_mmap_t vtterm_mmap;
81 const struct terminal_class vt_termclass = {
82 .tc_bell = vtterm_bell,
83 .tc_cursor = vtterm_cursor,
84 .tc_putchar = vtterm_putchar,
85 .tc_fill = vtterm_fill,
86 .tc_copy = vtterm_copy,
87 .tc_param = vtterm_param,
88 .tc_done = vtterm_done,
90 .tc_cnprobe = vtterm_cnprobe,
91 .tc_cngetc = vtterm_cngetc,
93 .tc_cngrab = vtterm_cngrab,
94 .tc_cnungrab = vtterm_cnungrab,
96 .tc_opened = vtterm_opened,
97 .tc_ioctl = vtterm_ioctl,
98 .tc_mmap = vtterm_mmap,
102 * Use a constant timer of 25 Hz to redraw the screen.
104 * XXX: In theory we should only fire up the timer when there is really
105 * activity. Unfortunately we cannot always start timers. We really
106 * don't want to process kernel messages synchronously, because it
107 * really slows down the system.
109 #define VT_TIMERFREQ 25
111 /* Bell pitch/duration. */
112 #define VT_BELLDURATION ((5 * hz + 99) / 100)
113 #define VT_BELLPITCH 800
115 #define VT_LOCK(vd) mtx_lock(&(vd)->vd_lock)
116 #define VT_UNLOCK(vd) mtx_unlock(&(vd)->vd_lock)
118 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
121 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
122 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
123 VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
124 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
125 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
126 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
128 /* Allow to disable some keyboard combinations. */
129 VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
130 "See kbdmap(5) to configure.");
131 VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
132 "See kbdmap(5) to configure.");
133 VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
134 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
135 VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
136 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
137 VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
138 "See kbdmap(5) to configure.");
140 static struct vt_device vt_consdev;
141 static unsigned int vt_unit = 0;
142 static MALLOC_DEFINE(M_VT, "vt", "vt device");
143 struct vt_device *main_vd = &vt_consdev;
146 extern unsigned int vt_logo_width;
147 extern unsigned int vt_logo_height;
148 extern unsigned int vt_logo_depth;
149 extern unsigned char vt_logo_image[];
152 extern struct vt_font vt_font_default;
153 #ifndef SC_NO_CUTPASTE
154 extern struct vt_mouse_cursor vt_default_mouse_pointer;
157 static int signal_vt_rel(struct vt_window *);
158 static int signal_vt_acq(struct vt_window *);
159 static int finish_vt_rel(struct vt_window *, int, int *);
160 static int finish_vt_acq(struct vt_window *);
161 static int vt_window_switch(struct vt_window *);
162 static int vt_late_window_switch(struct vt_window *);
163 static int vt_proc_alive(struct vt_window *);
164 static void vt_resize(struct vt_device *);
165 static void vt_update_static(void *);
166 #ifndef SC_NO_CUTPASTE
167 static void vt_mouse_paste(void);
170 SET_DECLARE(vt_drv_set, struct vt_driver);
172 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
173 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
175 static struct terminal vt_consterm;
176 static struct vt_window vt_conswindow;
177 static struct vt_device vt_consdev = {
180 .vd_flags = VDF_INVALID,
181 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
182 .vd_curwindow = &vt_conswindow,
185 #ifndef SC_NO_CUTPASTE
191 .vd_mcursor = &vt_default_mouse_pointer,
192 .vd_mcursor_fg = TC_WHITE,
193 .vd_mcursor_bg = TC_BLACK,
196 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
197 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
198 static struct vt_window vt_conswindow = {
199 .vw_number = VT_CONSWINDOW,
200 .vw_flags = VWF_CONSOLE,
202 .vb_buffer = &vt_constextbuf[0],
203 .vb_rows = &vt_constextbufrows[0],
204 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
207 .vb_flags = VBF_STATIC,
208 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
209 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
215 .vw_device = &vt_consdev,
216 .vw_terminal = &vt_consterm,
217 .vw_kbdmode = K_XLATE,
220 static struct terminal vt_consterm = {
221 .tm_class = &vt_termclass,
222 .tm_softc = &vt_conswindow,
225 static struct consdev vt_consterm_consdev = {
226 .cn_ops = &termcn_cnops,
227 .cn_arg = &vt_consterm,
231 /* Add to set of consoles. */
232 DATA_SET(cons_set, vt_consterm_consdev);
235 * Right after kmem is done to allow early drivers to use locking and allocate
238 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
240 /* Delay until all devices attached, to not waste time. */
241 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
244 /* Initialize locks/mem depended members. */
246 vt_update_static(void *dummy)
249 if (!vty_enabled(VTY_VT))
251 if (main_vd->vd_driver != NULL)
252 printf("VT: running with driver \"%s\".\n",
253 main_vd->vd_driver->vd_name);
255 printf("VT: init without driver.\n");
257 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
258 cv_init(&main_vd->vd_winswitch, "vtwswt");
262 vt_schedule_flush(struct vt_device *vd, int ms)
266 /* Default to initial value. */
267 ms = 1000 / VT_TIMERFREQ;
269 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
273 vt_resume_flush_timer(struct vt_device *vd, int ms)
276 if (!(vd->vd_flags & VDF_ASYNC) ||
277 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
280 vt_schedule_flush(vd, ms);
284 vt_suspend_flush_timer(struct vt_device *vd)
287 if (!(vd->vd_flags & VDF_ASYNC) ||
288 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
291 callout_drain(&vd->vd_timer);
295 vt_switch_timer(void *arg)
298 vt_late_window_switch((struct vt_window *)arg);
302 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
307 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
313 vw->vw_kbdmode = mode;
319 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
323 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
331 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
336 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
342 vw->vw_kbdstate &= ~LOCK_MASK;
343 vw->vw_kbdstate |= state & LOCK_MASK;
349 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
353 state = vw->vw_kbdstate & LOCK_MASK;
354 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
362 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
367 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
373 vw->vw_kbdstate &= ~LED_MASK;
374 vw->vw_kbdstate |= leds & LED_MASK;
380 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
384 leds = vw->vw_kbdstate & LED_MASK;
385 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
393 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
396 DPRINTF(40, "%s\n", __func__);
397 curvw->vw_switch_to = vw;
398 /* Set timer to allow switch in case when process hang. */
399 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
400 vt_switch_timer, (void *)vw);
401 /* Notify process about vt switch attempt. */
402 DPRINTF(30, "%s: Notify process.\n", __func__);
403 signal_vt_rel(curvw);
409 vt_window_postswitch(struct vt_window *vw)
416 /* vt_late_window_switch will done VT switching for regular case. */
418 vt_late_window_switch(struct vt_window *vw)
422 callout_stop(&vw->vw_proc_dead_timer);
424 ret = vt_window_switch(vw);
428 /* Notify owner process about terminal availability. */
429 if (vw->vw_smode.mode == VT_PROCESS) {
430 ret = vt_window_postswitch(vw);
437 vt_proc_window_switch(struct vt_window *vw)
439 struct vt_window *curvw;
440 struct vt_device *vd;
444 curvw = vd->vd_curwindow;
446 if (curvw->vw_flags & VWF_VTYLOCK)
449 /* Ask current process permission to switch away. */
450 if (curvw->vw_smode.mode == VT_PROCESS) {
451 DPRINTF(30, "%s: VT_PROCESS ", __func__);
452 if (vt_proc_alive(curvw) == FALSE) {
453 DPRINTF(30, "Dead. Cleaning.");
456 DPRINTF(30, "%s: Signaling process.\n", __func__);
457 /* Alive, try to ask him. */
458 ret = vt_window_preswitch(vw, curvw);
459 /* Wait for process answer or timeout. */
465 ret = vt_late_window_switch(vw);
469 /* Switch window ignoring process locking. */
471 vt_window_switch(struct vt_window *vw)
473 struct vt_device *vd = vw->vw_device;
474 struct vt_window *curvw = vd->vd_curwindow;
483 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
488 vt_suspend_flush_timer(vd);
490 vd->vd_curwindow = vw;
491 vd->vd_flags |= VDF_INVALID;
492 cv_broadcast(&vd->vd_winswitch);
495 if (vd->vd_driver->vd_postswitch)
496 vd->vd_driver->vd_postswitch(vd);
498 vt_resume_flush_timer(vd, 0);
500 /* Restore per-window keyboard mode. */
502 kbd = kbd_get_keyboard(vd->vd_keyboard);
504 if (curvw->vw_kbdmode == K_XLATE)
505 vt_save_kbd_state(curvw, kbd);
507 vt_update_kbd_mode(vw, kbd);
508 vt_update_kbd_state(vw, kbd);
511 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
517 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
520 size->tp_row = vd->vd_height;
521 size->tp_col = vd->vd_width;
523 size->tp_row /= vf->vf_height;
524 size->tp_col /= vf->vf_width;
529 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
532 size->ws_row = size->ws_ypixel = vd->vd_height;
533 size->ws_col = size->ws_xpixel = vd->vd_width;
535 size->ws_row /= vf->vf_height;
536 size->ws_col /= vf->vf_width;
541 vt_compute_drawable_area(struct vt_window *vw)
543 struct vt_device *vd;
548 if (vw->vw_font == NULL) {
549 vw->vw_draw_area.tr_begin.tp_col = 0;
550 vw->vw_draw_area.tr_begin.tp_row = 0;
551 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
552 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
559 * Compute the drawable area, so that the text is centered on
563 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
564 vw->vw_draw_area.tr_begin.tp_row = (vd->vd_height % vf->vf_height) / 2;
565 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
566 vd->vd_width / vf->vf_width * vf->vf_width;
567 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
568 vd->vd_height / vf->vf_height * vf->vf_height;
572 vt_scroll(struct vt_window *vw, int offset, int whence)
577 if ((vw->vw_flags & VWF_SCROLL) == 0)
580 vt_termsize(vw->vw_device, vw->vw_font, &size);
582 diff = vthistory_seek(&vw->vw_buf, offset, whence);
584 vw->vw_device->vd_flags |= VDF_INVALID;
585 vt_resume_flush_timer(vw->vw_device, 0);
589 vt_machine_kbdevent(int c)
593 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
595 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
597 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
599 shutdown_nice(RB_HALT);
601 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
602 #ifndef SC_NO_CUTPASTE
603 /* Insert text from cut-paste buffer. */
607 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
609 shutdown_nice(RB_HALT|RB_POWEROFF);
611 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
613 * Request to immediate panic if sysctl
614 * kern.vt.enable_panic_key allow it.
617 panic("Forced by the panic key");
619 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
621 shutdown_nice(RB_AUTOBOOT);
623 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
624 /* Force activatation/deactivation of the screen saver. */
627 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
628 /* Put machine into Stand-By mode. */
629 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
631 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
632 /* Suspend machine. */
633 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
641 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
643 struct vt_device *vd;
647 /* Only special keys handled in ScrollLock mode */
648 if ((c & SPCLKEY) == 0)
654 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
655 vw = vd->vd_windows[c - F_SCR];
657 vt_proc_window_switch(vw);
665 /* Turn scrolling off. */
666 vt_scroll(vw, 0, VHS_END);
667 VTBUF_SLCK_DISABLE(&vw->vw_buf);
668 vw->vw_flags &= ~VWF_SCROLL;
671 case FKEY | F(49): /* Home key. */
672 vt_scroll(vw, 0, VHS_SET);
674 case FKEY | F(50): /* Arrow up. */
675 vt_scroll(vw, -1, VHS_CUR);
677 case FKEY | F(51): /* Page up. */
678 vt_termsize(vd, vw->vw_font, &size);
679 vt_scroll(vw, -size.tp_row, VHS_CUR);
681 case FKEY | F(57): /* End key. */
682 vt_scroll(vw, 0, VHS_END);
684 case FKEY | F(58): /* Arrow down. */
685 vt_scroll(vw, 1, VHS_CUR);
687 case FKEY | F(59): /* Page down. */
688 vt_termsize(vd, vw->vw_font, &size);
689 vt_scroll(vw, size.tp_row, VHS_CUR);
698 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
700 struct vt_window *vw = vd->vd_curwindow;
702 #if VT_ALT_TO_ESC_HACK
704 switch (c & ~RELKEY) {
705 case (SPCLKEY | RALT):
706 if (vt_enable_altgr != 0)
708 case (SPCLKEY | LALT):
709 vd->vd_kbstate &= ~ALKED;
711 /* Other keys ignored for RELKEY event. */
714 switch (c & ~RELKEY) {
715 case (SPCLKEY | RALT):
716 if (vt_enable_altgr != 0)
718 case (SPCLKEY | LALT):
719 vd->vd_kbstate |= ALKED;
724 /* Other keys ignored for RELKEY event. */
728 if (vt_machine_kbdevent(c))
731 if (vw->vw_flags & VWF_SCROLL) {
732 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
733 /* Scroll mode keys handled, nothing to do more. */
740 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
741 vw = vd->vd_windows[c - F_SCR];
743 vt_proc_window_switch(vw);
749 /* Switch to next VT. */
750 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
751 vw = vd->vd_windows[c];
753 vt_proc_window_switch(vw);
756 /* Switch to previous VT. */
757 c = (vw->vw_number - 1) % VT_MAXWINDOWS;
758 vw = vd->vd_windows[c];
760 vt_proc_window_switch(vw);
763 vt_save_kbd_state(vw, kbd);
765 if (vw->vw_kbdstate & SLKED) {
766 /* Turn scrolling on. */
767 vw->vw_flags |= VWF_SCROLL;
768 VTBUF_SLCK_ENABLE(&vw->vw_buf);
770 /* Turn scrolling off. */
771 vw->vw_flags &= ~VWF_SCROLL;
772 VTBUF_SLCK_DISABLE(&vw->vw_buf);
773 vt_scroll(vw, 0, VHS_END);
778 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
779 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
780 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
781 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
782 /* F1 through F12 keys. */
783 terminal_input_special(vw->vw_terminal,
784 TKEY_F1 + c - (FKEY | F(1)));
786 case FKEY | F(49): /* Home key. */
787 terminal_input_special(vw->vw_terminal, TKEY_HOME);
789 case FKEY | F(50): /* Arrow up. */
790 terminal_input_special(vw->vw_terminal, TKEY_UP);
792 case FKEY | F(51): /* Page up. */
793 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
795 case FKEY | F(53): /* Arrow left. */
796 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
798 case FKEY | F(55): /* Arrow right. */
799 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
801 case FKEY | F(57): /* End key. */
802 terminal_input_special(vw->vw_terminal, TKEY_END);
804 case FKEY | F(58): /* Arrow down. */
805 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
807 case FKEY | F(59): /* Page down. */
808 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
810 case FKEY | F(60): /* Insert key. */
811 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
813 case FKEY | F(61): /* Delete key. */
814 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
817 } else if (KEYFLAGS(c) == 0) {
818 /* Don't do UTF-8 conversion when doing raw mode. */
819 if (vw->vw_kbdmode == K_XLATE) {
820 #if VT_ALT_TO_ESC_HACK
821 if (vd->vd_kbstate & ALKED) {
823 * Prepend ESC sequence if one of ALT keys down.
825 terminal_input_char(vw->vw_terminal, 0x1b);
829 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
831 terminal_input_raw(vw->vw_terminal, c);
837 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
839 struct vt_device *vd = arg;
845 case KBDIO_UNLOADING:
847 vd->vd_keyboard = -1;
848 kbd_release(kbd, (void *)vd);
855 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
856 vt_processkey(kbd, vd, c);
862 vt_allocate_keyboard(struct vt_device *vd)
868 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
869 vd->vd_keyboard = idx0;
871 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
872 k0 = kbd_get_keyboard(idx0);
874 for (idx = kbd_find_keyboard2("*", -1, 0);
876 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
877 k = kbd_get_keyboard(idx);
879 if (idx == idx0 || KBD_IS_BUSY(k))
882 bzero(&ki, sizeof(ki));
883 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
884 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
885 ki.kb_unit = k->kb_unit;
887 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
890 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
891 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
893 DPRINTF(10, "%s: No keyboard found.\n", __func__);
897 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
903 vtterm_bell(struct terminal *tm)
905 struct vt_window *vw = tm->tm_softc;
906 struct vt_device *vd = vw->vw_device;
911 if (vd->vd_flags & VDF_QUIET_BELL)
914 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
918 vtterm_beep(struct terminal *tm, u_int param)
925 if ((param == 0) || ((param & 0xffff) == 0)) {
930 period = ((param >> 16) & 0xffff) * hz / 1000;
931 freq = 1193182 / (param & 0xffff);
933 sysbeep(freq, period);
937 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
939 struct vt_window *vw = tm->tm_softc;
941 vtbuf_cursor_position(&vw->vw_buf, p);
942 vt_resume_flush_timer(vw->vw_device, 0);
946 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
948 struct vt_window *vw = tm->tm_softc;
950 vtbuf_putchar(&vw->vw_buf, p, c);
951 vt_resume_flush_timer(vw->vw_device, 0);
955 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
957 struct vt_window *vw = tm->tm_softc;
959 vtbuf_fill_locked(&vw->vw_buf, r, c);
960 vt_resume_flush_timer(vw->vw_device, 0);
964 vtterm_copy(struct terminal *tm, const term_rect_t *r,
967 struct vt_window *vw = tm->tm_softc;
969 vtbuf_copy(&vw->vw_buf, r, p);
970 vt_resume_flush_timer(vw->vw_device, 0);
974 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
976 struct vt_window *vw = tm->tm_softc;
980 vtbuf_cursor_visibility(&vw->vw_buf, arg);
981 vt_resume_flush_timer(vw->vw_device, 0);
984 vw->vw_mouse_level = arg;
990 vt_determine_colors(term_char_t c, int cursor,
991 term_color_t *fg, term_color_t *bg)
998 *fg = TCHAR_FGCOLOR(c);
999 if (TCHAR_FORMAT(c) & TF_BOLD)
1000 *fg = TCOLOR_LIGHT(*fg);
1001 *bg = TCHAR_BGCOLOR(c);
1003 if (TCHAR_FORMAT(c) & TF_REVERSE)
1015 #ifndef SC_NO_CUTPASTE
1017 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1019 unsigned int mx, my, x1, y1, x2, y2;
1022 * We use the cursor position saved during the current refresh,
1023 * in case the cursor moved since.
1025 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1026 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1028 x1 = area->tr_begin.tp_col;
1029 y1 = area->tr_begin.tp_row;
1030 x2 = area->tr_end.tp_col;
1031 y2 = area->tr_end.tp_row;
1033 if (((mx >= x1 && x2 - 1 >= mx) ||
1034 (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
1035 ((my >= y1 && y2 - 1 >= my) ||
1036 (my < y1 && my + vd->vd_mcursor->height >= y1)))
1043 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1046 struct vt_window *vw;
1050 vw = vd->vd_curwindow;
1053 x = vd->vd_mx_drawn;
1054 y = vd->vd_my_drawn;
1057 area.tr_begin.tp_col = x / vf->vf_width;
1058 area.tr_begin.tp_row = y / vf->vf_height;
1059 area.tr_end.tp_col =
1060 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1061 area.tr_end.tp_row =
1062 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1065 * No font loaded (ie. vt_vga operating in textmode).
1067 * FIXME: This fake area needs to be revisited once the
1068 * mouse cursor is supported in vt_vga's textmode.
1070 area.tr_begin.tp_col = x;
1071 area.tr_begin.tp_row = y;
1072 area.tr_end.tp_col = x + 2;
1073 area.tr_end.tp_row = y + 2;
1076 vtbuf_dirty(&vw->vw_buf, &area);
1081 vt_flush(struct vt_device *vd)
1083 struct vt_window *vw;
1087 #ifndef SC_NO_CUTPASTE
1088 int cursor_was_shown, cursor_moved;
1091 vw = vd->vd_curwindow;
1095 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1099 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1102 #ifndef SC_NO_CUTPASTE
1103 cursor_was_shown = vd->vd_mshown;
1104 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1105 vd->vd_my != vd->vd_my_drawn);
1107 /* Check if the cursor should be displayed or not. */
1108 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1109 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1110 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1117 * If the cursor changed display state or moved, we must mark
1118 * the old position as dirty, so that it's erased.
1120 if (cursor_was_shown != vd->vd_mshown ||
1121 (vd->vd_mshown && cursor_moved))
1122 vt_mark_mouse_position_as_dirty(vd);
1125 * Save position of the mouse cursor. It's used by backends to
1126 * know where to draw the cursor and during the next refresh to
1127 * erase the previous position.
1129 vd->vd_mx_drawn = vd->vd_mx;
1130 vd->vd_my_drawn = vd->vd_my;
1133 * If the cursor is displayed and has moved since last refresh,
1134 * mark the new position as dirty.
1136 if (vd->vd_mshown && cursor_moved)
1137 vt_mark_mouse_position_as_dirty(vd);
1140 vtbuf_undirty(&vw->vw_buf, &tarea);
1141 vt_termsize(vd, vf, &size);
1143 /* Force a full redraw when the screen contents are invalid. */
1144 if (vd->vd_flags & VDF_INVALID) {
1145 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1146 tarea.tr_end = size;
1148 vd->vd_flags &= ~VDF_INVALID;
1151 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1152 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1162 struct vt_device *vd;
1166 /* Update screen if required. */
1167 changed = vt_flush(vd);
1169 /* Schedule for next update. */
1171 vt_schedule_flush(vd, 0);
1173 vd->vd_timer_armed = 0;
1177 vtterm_done(struct terminal *tm)
1179 struct vt_window *vw = tm->tm_softc;
1180 struct vt_device *vd = vw->vw_device;
1182 if (kdb_active || panicstr != NULL) {
1183 /* Switch to the debugger. */
1184 if (vd->vd_curwindow != vw) {
1185 vd->vd_curwindow = vw;
1186 vd->vd_flags |= VDF_INVALID;
1187 if (vd->vd_driver->vd_postswitch)
1188 vd->vd_driver->vd_postswitch(vd);
1190 vd->vd_flags &= ~VDF_SPLASH;
1192 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1199 vtterm_splash(struct vt_device *vd)
1201 vt_axis_t top, left;
1203 /* Display a nice boot splash. */
1204 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1206 top = (vd->vd_height - vt_logo_height) / 2;
1207 left = (vd->vd_width - vt_logo_width) / 2;
1208 switch (vt_logo_depth) {
1210 /* XXX: Unhardcode colors! */
1211 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1212 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1213 left, top, TC_WHITE, TC_BLACK);
1215 vd->vd_flags |= VDF_SPLASH;
1222 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1224 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1225 struct vt_window *vw = tm->tm_softc;
1226 struct vt_device *vd = vw->vw_device;
1231 if (!vty_enabled(VTY_VT))
1234 if (vd->vd_flags & VDF_INITIALIZED)
1235 /* Initialization already done. */
1238 SET_FOREACH(vtdlist, vt_drv_set) {
1240 if (vtd->vd_probe == NULL)
1242 if (vtd->vd_probe(vd) == CN_DEAD)
1244 if ((vtdbest == NULL) ||
1245 (vtd->vd_priority > vtdbest->vd_priority))
1248 if (vtdbest == NULL) {
1249 cp->cn_pri = CN_DEAD;
1250 vd->vd_flags |= VDF_DEAD;
1252 vd->vd_driver = vtdbest;
1253 cp->cn_pri = vd->vd_driver->vd_init(vd);
1256 /* Check if driver's vt_init return CN_DEAD. */
1257 if (cp->cn_pri == CN_DEAD) {
1258 vd->vd_flags |= VDF_DEAD;
1261 /* Initialize any early-boot keyboard drivers */
1262 kbd_configure(KB_CONF_PROBE_ONLY);
1264 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1265 vd->vd_windows[VT_CONSWINDOW] = vw;
1266 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1268 /* Attach default font if not in TEXTMODE. */
1269 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1270 vw->vw_font = vtfont_ref(&vt_font_default);
1271 vt_compute_drawable_area(vw);
1275 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1276 * that we have the real viewable size, fix it in the static
1279 if (vd->vd_width != 0 && vd->vd_height != 0)
1280 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1282 vtbuf_init_early(&vw->vw_buf);
1283 vt_winsize(vd, vw->vw_font, &wsz);
1284 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1286 attr.ta_format = TCHAR_FORMAT(c);
1287 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1288 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1289 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1291 if (vtdbest != NULL) {
1295 vd->vd_flags |= VDF_INITIALIZED;
1300 vtterm_cngetc(struct terminal *tm)
1302 struct vt_window *vw = tm->tm_softc;
1303 struct vt_device *vd = vw->vw_device;
1307 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1308 return (*vw->vw_kbdsq++);
1310 /* Make sure the splash screen is not there. */
1311 if (vd->vd_flags & VDF_SPLASH) {
1313 vd->vd_flags &= ~VDF_SPLASH;
1314 /* Mark screen as invalid to force update */
1315 vd->vd_flags |= VDF_INVALID;
1319 /* Stripped down keyboard handler. */
1320 kbd = kbd_get_keyboard(vd->vd_keyboard);
1324 /* Force keyboard input mode to K_XLATE */
1325 vw->vw_kbdmode = K_XLATE;
1326 vt_update_kbd_mode(vw, kbd);
1328 /* Switch the keyboard to polling to make it work here. */
1329 kbdd_poll(kbd, TRUE);
1330 c = kbdd_read_char(kbd, 0);
1331 kbdd_poll(kbd, FALSE);
1335 if (vw->vw_flags & VWF_SCROLL) {
1336 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1341 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1345 vt_save_kbd_state(vw, kbd);
1346 if (vw->vw_kbdstate & SLKED) {
1347 /* Turn scrolling on. */
1348 vw->vw_flags |= VWF_SCROLL;
1349 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1351 /* Turn scrolling off. */
1352 vt_scroll(vw, 0, VHS_END);
1353 vw->vw_flags &= ~VWF_SCROLL;
1354 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1357 /* XXX: KDB can handle history. */
1358 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1359 vw->vw_kbdsq = "\x1b[A";
1361 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1362 vw->vw_kbdsq = "\x1b[B";
1364 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1365 vw->vw_kbdsq = "\x1b[C";
1367 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1368 vw->vw_kbdsq = "\x1b[D";
1372 /* Force refresh to make scrollback work. */
1374 } else if (KEYFLAGS(c) == 0) {
1375 return (KEYCHAR(c));
1378 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1379 return (*vw->vw_kbdsq++);
1385 vtterm_cngrab(struct terminal *tm)
1387 struct vt_device *vd;
1388 struct vt_window *vw;
1395 vt_window_switch(vw);
1397 kbd = kbd_get_keyboard(vd->vd_keyboard);
1401 if (vw->vw_grabbed++ > 0)
1405 * Make sure the keyboard is accessible even when the kbd device
1406 * driver is disabled.
1410 /* We shall always use the keyboard in the XLATE mode here. */
1411 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1412 vw->vw_kbdmode = K_XLATE;
1413 vt_update_kbd_mode(vw, kbd);
1415 kbdd_poll(kbd, TRUE);
1419 vtterm_cnungrab(struct terminal *tm)
1421 struct vt_device *vd;
1422 struct vt_window *vw;
1428 kbd = kbd_get_keyboard(vd->vd_keyboard);
1432 if (--vw->vw_grabbed > 0)
1435 kbdd_poll(kbd, FALSE);
1437 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1438 vt_update_kbd_mode(vw, kbd);
1443 vtterm_opened(struct terminal *tm, int opened)
1445 struct vt_window *vw = tm->tm_softc;
1446 struct vt_device *vd = vw->vw_device;
1449 vd->vd_flags &= ~VDF_SPLASH;
1451 vw->vw_flags |= VWF_OPENED;
1453 vw->vw_flags &= ~VWF_OPENED;
1454 /* TODO: finish ACQ/REL */
1460 vt_set_border(struct vt_window *vw, term_color_t c)
1462 struct vt_device *vd = vw->vw_device;
1464 if (vd->vd_driver->vd_drawrect == NULL)
1468 if (vw->vw_draw_area.tr_begin.tp_row > 0)
1469 vd->vd_driver->vd_drawrect(vd,
1471 vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1475 if (vw->vw_draw_area.tr_begin.tp_col > 0)
1476 vd->vd_driver->vd_drawrect(vd,
1478 vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1482 if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1483 vd->vd_driver->vd_drawrect(vd,
1484 vw->vw_draw_area.tr_end.tp_col - 1, 0,
1485 vd->vd_width - 1, vd->vd_height - 1,
1489 if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1490 vd->vd_driver->vd_drawrect(vd,
1491 0, vw->vw_draw_area.tr_end.tp_row - 1,
1492 vd->vd_width - 1, vd->vd_height - 1,
1499 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1501 struct vt_device *vd = vw->vw_device;
1502 struct terminal *tm = vw->vw_terminal;
1509 * Changing fonts is a little tricky. We must prevent
1510 * simultaneous access to the device, so we must stop
1511 * the display timer and the terminal from accessing.
1512 * We need to switch fonts and grow our screen buffer.
1514 * XXX: Right now the code uses terminal_mute() to
1515 * prevent data from reaching the console driver while
1516 * resizing the screen buffer. This isn't elegant...
1520 if (vw->vw_flags & VWF_BUSY) {
1521 /* Another process is changing the font. */
1525 vw->vw_flags |= VWF_BUSY;
1528 vt_termsize(vd, vf, &size);
1529 vt_winsize(vd, vf, &wsz);
1531 /* Grow the screen buffer and terminal. */
1532 terminal_mute(tm, 1);
1533 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1534 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1535 terminal_mute(tm, 0);
1537 /* Actually apply the font to the current window. */
1539 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1541 * In case vt_change_font called to update size we don't need
1542 * to update font link.
1544 vtfont_unref(vw->vw_font);
1545 vw->vw_font = vtfont_ref(vf);
1549 * Compute the drawable area and move the mouse cursor inside
1550 * it, in case the new area is smaller than the previous one.
1552 vt_compute_drawable_area(vw);
1553 vd->vd_mx = min(vd->vd_mx,
1554 vw->vw_draw_area.tr_end.tp_col -
1555 vw->vw_draw_area.tr_begin.tp_col - 1);
1556 vd->vd_my = min(vd->vd_my,
1557 vw->vw_draw_area.tr_end.tp_row -
1558 vw->vw_draw_area.tr_begin.tp_row - 1);
1560 /* Force a full redraw the next timer tick. */
1561 if (vd->vd_curwindow == vw) {
1562 vt_set_border(vw, TC_BLACK);
1563 vd->vd_flags |= VDF_INVALID;
1564 vt_resume_flush_timer(vw->vw_device, 0);
1566 vw->vw_flags &= ~VWF_BUSY;
1572 vt_proc_alive(struct vt_window *vw)
1576 if (vw->vw_smode.mode != VT_PROCESS)
1580 if ((p = pfind(vw->vw_pid)) != NULL)
1582 if (vw->vw_proc == p)
1585 vw->vw_smode.mode = VT_AUTO;
1586 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1593 signal_vt_rel(struct vt_window *vw)
1596 if (vw->vw_smode.mode != VT_PROCESS)
1598 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1603 vw->vw_flags |= VWF_SWWAIT_REL;
1604 PROC_LOCK(vw->vw_proc);
1605 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1606 PROC_UNLOCK(vw->vw_proc);
1607 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1612 signal_vt_acq(struct vt_window *vw)
1615 if (vw->vw_smode.mode != VT_PROCESS)
1617 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1618 cnavailable(vw->vw_terminal->consdev, FALSE);
1619 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1624 vw->vw_flags |= VWF_SWWAIT_ACQ;
1625 PROC_LOCK(vw->vw_proc);
1626 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1627 PROC_UNLOCK(vw->vw_proc);
1628 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1633 finish_vt_rel(struct vt_window *vw, int release, int *s)
1636 if (vw->vw_flags & VWF_SWWAIT_REL) {
1637 vw->vw_flags &= ~VWF_SWWAIT_REL;
1639 callout_drain(&vw->vw_proc_dead_timer);
1640 vt_late_window_switch(vw->vw_switch_to);
1648 finish_vt_acq(struct vt_window *vw)
1651 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1652 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1658 #ifndef SC_NO_CUTPASTE
1660 vt_mouse_terminput_button(struct vt_device *vd, int button)
1662 struct vt_window *vw;
1664 char mouseb[6] = "\x1B[M";
1667 vw = vd->vd_curwindow;
1670 /* Translate to char position. */
1671 x = vd->vd_mx / vf->vf_width;
1672 y = vd->vd_my / vf->vf_height;
1673 /* Avoid overflow. */
1674 x = MIN(x, 255 - '!');
1675 y = MIN(y, 255 - '!');
1677 mouseb[3] = ' ' + button;
1678 mouseb[4] = '!' + x;
1679 mouseb[5] = '!' + y;
1681 for (i = 0; i < sizeof(mouseb); i++)
1682 terminal_input_char(vw->vw_terminal, mouseb[i]);
1686 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1691 case MOUSE_BUTTON_EVENT:
1693 /* Mouse button pressed. */
1694 if (event & MOUSE_BUTTON1DOWN)
1695 vt_mouse_terminput_button(vd, 0);
1696 if (event & MOUSE_BUTTON2DOWN)
1697 vt_mouse_terminput_button(vd, 1);
1698 if (event & MOUSE_BUTTON3DOWN)
1699 vt_mouse_terminput_button(vd, 2);
1701 /* Mouse button released. */
1702 vt_mouse_terminput_button(vd, 3);
1706 case MOUSE_MOTION_EVENT:
1707 if (mouse->u.data.z < 0) {
1709 sc_mouse_input_button(vd, 64);
1710 } else if (mouse->u.data.z > 0) {
1712 sc_mouse_input_button(vd, 65);
1725 len = VD_PASTEBUFLEN(main_vd);
1726 buf = VD_PASTEBUF(main_vd);
1727 len /= sizeof(term_char_t);
1728 for (i = 0; i < len; i++) {
1731 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1737 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1739 struct vt_device *vd;
1740 struct vt_window *vw;
1746 vw = vd->vd_curwindow;
1750 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1752 * Either the mouse is disabled, or the window is in
1753 * "graphics mode". The graphics mode is usually set by
1754 * an X server, using the KDSETMODE ioctl.
1758 if (vf == NULL) /* Text mode. */
1762 * TODO: add flag about pointer position changed, to not redraw chars
1763 * under mouse pointer when nothing changed.
1766 if (vw->vw_mouse_level > 0)
1767 vt_mouse_terminput(vd, type, x, y, event, cnt);
1771 case MOUSE_MOTION_EVENT:
1776 vt_termsize(vd, vf, &size);
1781 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1782 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1786 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1787 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1788 vd->vd_mx / vf->vf_width,
1789 vd->vd_my / vf->vf_height);
1791 vt_resume_flush_timer(vw->vw_device, 0);
1793 case MOUSE_BUTTON_EVENT:
1801 case MOUSE_BUTTON1DOWN:
1804 mark = VTB_MARK_END;
1806 case 1: /* single click: start cut operation */
1807 mark = VTB_MARK_START;
1809 case 2: /* double click: cut a word */
1810 mark = VTB_MARK_WORD;
1812 case 3: /* triple click: cut a line */
1813 mark = VTB_MARK_ROW;
1817 case VT_MOUSE_PASTEBUTTON:
1826 case VT_MOUSE_EXTENDBUTTON:
1829 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1830 mark = VTB_MARK_EXTEND;
1835 mark = VTB_MARK_EXTEND;
1843 /* Save buttons state. */
1845 vd->vd_mstate |= event;
1847 vd->vd_mstate &= ~event;
1849 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1850 vd->vd_my / vf->vf_height) == 1) {
1852 * We have something marked to copy, so update pointer to
1853 * window with selection.
1855 vt_resume_flush_timer(vw->vw_device, 0);
1861 case VTB_MARK_EXTEND:
1864 /* Other types of mark do not require to copy data. */
1868 /* Get current selection size in bytes. */
1869 len = vtbuf_get_marked_len(&vw->vw_buf);
1873 /* Reallocate buffer only if old one is too small. */
1874 if (len > VD_PASTEBUFSZ(vd)) {
1875 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1877 /* Update buffer size. */
1878 VD_PASTEBUFSZ(vd) = len;
1880 /* Request copy/paste buffer data, no more than `len' */
1881 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1884 VD_PASTEBUFLEN(vd) = len;
1886 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1891 vt_mouse_state(int show)
1893 struct vt_device *vd;
1894 struct vt_window *vw;
1897 vw = vd->vd_curwindow;
1901 vw->vw_flags |= VWF_MOUSE_HIDE;
1904 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1908 /* Mark mouse position as dirty. */
1909 vt_mark_mouse_position_as_dirty(vd);
1910 vt_resume_flush_timer(vw->vw_device, 0);
1915 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1916 int nprot, vm_memattr_t *memattr)
1918 struct vt_window *vw = tm->tm_softc;
1919 struct vt_device *vd = vw->vw_device;
1921 if (vd->vd_driver->vd_fb_mmap)
1922 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1929 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1932 struct vt_window *vw = tm->tm_softc;
1933 struct vt_device *vd = vw->vw_device;
1936 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1937 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1948 cmd = VT_WAITACTIVE;
1974 ival = IOCPARM_IVAL(data);
1975 data = (caddr_t)&ival;
1980 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1981 if (*(int *)data & ~0x7f)
1986 case GIO_DEADKEYMAP:
1987 case PIO_DEADKEYMAP:
1992 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1993 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1994 case KBADDKBD: /* add/remove keyboard to/from mux */
1999 kbd = kbd_get_keyboard(vd->vd_keyboard);
2001 error = kbdd_ioctl(kbd, cmd, data);
2003 if (error == ENOIOCTL) {
2004 if (cmd == KDGKBTYPE) {
2005 /* always return something? XXX */
2013 case KDGKBSTATE: { /* get keyboard state (locks) */
2016 if (vw == vd->vd_curwindow) {
2018 kbd = kbd_get_keyboard(vd->vd_keyboard);
2020 error = vt_save_kbd_state(vw, kbd);
2027 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2031 case KDSKBSTATE: { /* set keyboard state (locks) */
2034 state = *(int *)data;
2035 if (state & ~LOCK_MASK)
2038 vw->vw_kbdstate &= ~LOCK_MASK;
2039 vw->vw_kbdstate |= state;
2042 if (vw == vd->vd_curwindow) {
2044 kbd = kbd_get_keyboard(vd->vd_keyboard);
2046 error = vt_update_kbd_state(vw, kbd);
2052 case KDGETLED: { /* get keyboard LED status */
2055 if (vw == vd->vd_curwindow) {
2057 kbd = kbd_get_keyboard(vd->vd_keyboard);
2059 error = vt_save_kbd_leds(vw, kbd);
2066 *(int *)data = vw->vw_kbdstate & LED_MASK;
2070 case KDSETLED: { /* set keyboard LED status */
2073 leds = *(int *)data;
2074 if (leds & ~LED_MASK)
2077 vw->vw_kbdstate &= ~LED_MASK;
2078 vw->vw_kbdstate |= leds;
2081 if (vw == vd->vd_curwindow) {
2083 kbd = kbd_get_keyboard(vd->vd_keyboard);
2085 error = vt_update_kbd_leds(vw, kbd);
2094 if (vw == vd->vd_curwindow) {
2096 kbd = kbd_get_keyboard(vd->vd_keyboard);
2098 error = vt_save_kbd_mode(vw, kbd);
2105 *(int *)data = vw->vw_kbdmode;
2112 mode = *(int *)data;
2117 vw->vw_kbdmode = mode;
2120 if (vw == vd->vd_curwindow) {
2122 kbd = kbd_get_keyboard(vd->vd_keyboard);
2124 error = vt_update_kbd_mode(vw, kbd);
2134 case FBIO_GETWINORG: /* get frame buffer window origin */
2135 case FBIO_GETDISPSTART: /* get display start address */
2136 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2137 case FBIO_BLANK: /* blank display */
2138 if (vd->vd_driver->vd_fb_ioctl)
2139 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2141 case CONS_BLANKTIME:
2146 *(int *)data = M_CG640x480;
2148 case CONS_BELLTYPE: /* set bell type sound */
2149 if ((*(int *)data) & CONS_QUIET_BELL)
2150 vd->vd_flags |= VDF_QUIET_BELL;
2152 vd->vd_flags &= ~VDF_QUIET_BELL;
2154 case CONS_GETINFO: {
2155 vid_info_t *vi = (vid_info_t *)data;
2156 if (vi->size != sizeof(struct vid_info))
2159 if (vw == vd->vd_curwindow) {
2160 kbd = kbd_get_keyboard(vd->vd_keyboard);
2162 vt_save_kbd_state(vw, kbd);
2165 vi->m_num = vd->vd_curwindow->vw_number + 1;
2166 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2167 /* XXX: other fields! */
2171 *(int *)data = 0x200;
2176 case CONS_MOUSECTL: {
2177 mouse_info_t *mouse = (mouse_info_t*)data;
2180 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2181 * should not be applied to individual TTYs, but only to
2184 switch (mouse->operation) {
2186 if (vd->vd_flags & VDF_MOUSECURSOR) {
2187 vd->vd_flags &= ~VDF_MOUSECURSOR;
2188 #ifndef SC_NO_CUTPASTE
2189 vt_mouse_state(VT_MOUSE_HIDE);
2194 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2195 vd->vd_flags |= VDF_MOUSECURSOR;
2196 vd->vd_mx = vd->vd_width / 2;
2197 vd->vd_my = vd->vd_height / 2;
2198 #ifndef SC_NO_CUTPASTE
2199 vt_mouse_state(VT_MOUSE_SHOW);
2210 if (vd->vd_flags & VDF_TEXTMODE)
2213 error = vtfont_load((void *)data, &vf);
2217 error = vt_change_font(vw, vf);
2222 scrmap_t *sm = (scrmap_t *)data;
2224 /* We don't have screen maps, so return a handcrafted one. */
2225 for (i = 0; i < 256; i++)
2231 * FIXME: This implementation is incomplete compared to
2234 switch (*(int *)data) {
2238 vw->vw_flags &= ~VWF_GRAPHICS;
2241 vw->vw_flags |= VWF_GRAPHICS;
2245 case KDENABIO: /* allow io operations */
2246 error = priv_check(td, PRIV_IO);
2249 error = securelevel_gt(td->td_ucred, 0);
2252 #if defined(__i386__)
2253 td->td_frame->tf_eflags |= PSL_IOPL;
2254 #elif defined(__amd64__)
2255 td->td_frame->tf_rflags |= PSL_IOPL;
2258 case KDDISABIO: /* disallow io operations (default) */
2259 #if defined(__i386__)
2260 td->td_frame->tf_eflags &= ~PSL_IOPL;
2261 #elif defined(__amd64__)
2262 td->td_frame->tf_rflags &= ~PSL_IOPL;
2265 case KDMKTONE: /* sound the bell */
2266 vtterm_beep(tm, *(u_int *)data);
2268 case KIOCSOUND: /* make tone (*data) hz */
2271 case CONS_SETKBD: /* set the new keyboard */
2274 if (vd->vd_keyboard != *(int *)data) {
2275 kbd = kbd_get_keyboard(*(int *)data);
2280 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2281 (void *)vd, vt_kbdevent, vd);
2283 if (vd->vd_keyboard != -1) {
2284 vt_save_kbd_state(vd->vd_curwindow, kbd);
2285 kbd_release(kbd, (void *)vd);
2287 kbd = kbd_get_keyboard(i);
2288 vd->vd_keyboard = i;
2290 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2291 vt_update_kbd_state(vd->vd_curwindow, kbd);
2293 error = EPERM; /* XXX */
2298 case CONS_RELKBD: /* release the current keyboard */
2301 if (vd->vd_keyboard != -1) {
2302 kbd = kbd_get_keyboard(vd->vd_keyboard);
2307 vt_save_kbd_state(vd->vd_curwindow, kbd);
2308 error = kbd_release(kbd, (void *)vd);
2310 vd->vd_keyboard = -1;
2317 win = *(int *)data - 1;
2318 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2320 if ((win >= VT_MAXWINDOWS) || (win < 0))
2322 return (vt_proc_window_switch(vd->vd_windows[win]));
2325 *(int *)data = vd->vd_curwindow->vw_number + 1;
2328 *(int *)data = vw->vw_number + 1;
2331 /* TODO: Check current state, switching can be in progress. */
2332 if ((*(int *)data) == 0x01)
2333 vw->vw_flags |= VWF_VTYLOCK;
2334 else if ((*(int *)data) == 0x02)
2335 vw->vw_flags &= ~VWF_VTYLOCK;
2341 for (i = 0; i < VT_MAXWINDOWS; i++) {
2342 vw = vd->vd_windows[i];
2345 if (!(vw->vw_flags & VWF_OPENED)) {
2346 *(int *)data = vw->vw_number + 1;
2356 i = *(unsigned int *)data;
2357 if (i > VT_MAXWINDOWS)
2360 vw = vd->vd_windows[i - 1];
2363 while (vd->vd_curwindow != vw && error == 0)
2364 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2367 case VT_SETMODE: { /* set screen switcher mode */
2368 struct vt_mode *mode;
2371 mode = (struct vt_mode *)data;
2372 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2373 if (vw->vw_smode.mode == VT_PROCESS) {
2374 p1 = pfind(vw->vw_pid);
2375 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2378 DPRINTF(5, "error EPERM\n");
2384 if (mode->mode == VT_AUTO) {
2385 vw->vw_smode.mode = VT_AUTO;
2388 DPRINTF(5, "VT_AUTO, ");
2389 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2390 cnavailable(vw->vw_terminal->consdev, TRUE);
2391 /* were we in the middle of the vty switching process? */
2392 if (finish_vt_rel(vw, TRUE, &s) == 0)
2393 DPRINTF(5, "reset WAIT_REL, ");
2394 if (finish_vt_acq(vw) == 0)
2395 DPRINTF(5, "reset WAIT_ACQ, ");
2397 } else if (mode->mode == VT_PROCESS) {
2398 if (!ISSIGVALID(mode->relsig) ||
2399 !ISSIGVALID(mode->acqsig) ||
2400 !ISSIGVALID(mode->frsig)) {
2401 DPRINTF(5, "error EINVAL\n");
2404 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2405 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2406 vw->vw_proc = td->td_proc;
2407 vw->vw_pid = vw->vw_proc->p_pid;
2408 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2409 cnavailable(vw->vw_terminal->consdev, FALSE);
2411 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2418 case VT_GETMODE: /* get screen switcher mode */
2419 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2422 case VT_RELDISP: /* screen switcher ioctl */
2424 * This must be the current vty which is in the VT_PROCESS
2427 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2431 /* ...and this process is controlling it. */
2432 if (vw->vw_proc != td->td_proc) {
2436 switch(*(int *)data) {
2437 case VT_FALSE: /* user refuses to release screen, abort */
2438 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2439 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2440 SC_DRIVER_NAME, VT_UNIT(vw));
2442 case VT_TRUE: /* user has released screen, go on */
2443 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2444 if (vw->vw_flags & VWF_SWWAIT_REL) {
2445 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2446 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2447 SC_DRIVER_NAME, VT_UNIT(vw));
2452 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2453 if ((error = finish_vt_acq(vw)) == 0)
2454 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2455 SC_DRIVER_NAME, VT_UNIT(vw));
2466 static struct vt_window *
2467 vt_allocate_window(struct vt_device *vd, unsigned int window)
2469 struct vt_window *vw;
2470 struct terminal *tm;
2474 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2476 vw->vw_number = window;
2477 vw->vw_kbdmode = K_XLATE;
2479 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2480 vw->vw_font = vtfont_ref(&vt_font_default);
2481 vt_compute_drawable_area(vw);
2484 vt_termsize(vd, vw->vw_font, &size);
2485 vt_winsize(vd, vw->vw_font, &wsz);
2486 vtbuf_init(&vw->vw_buf, &size);
2488 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2489 terminal_set_winsize(tm, &wsz);
2490 vd->vd_windows[window] = vw;
2491 callout_init(&vw->vw_proc_dead_timer, 0);
2497 vt_upgrade(struct vt_device *vd)
2499 struct vt_window *vw;
2502 if (!vty_enabled(VTY_VT))
2504 if (main_vd->vd_driver == NULL)
2507 for (i = 0; i < VT_MAXWINDOWS; i++) {
2508 vw = vd->vd_windows[i];
2511 vw = vt_allocate_window(vd, i);
2513 if (!(vw->vw_flags & VWF_READY)) {
2514 callout_init(&vw->vw_proc_dead_timer, 0);
2515 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2516 vw->vw_flags |= VWF_READY;
2517 if (vw->vw_flags & VWF_CONSOLE) {
2518 /* For existing console window. */
2519 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2520 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2526 if (vd->vd_curwindow == NULL)
2527 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2529 if (!(vd->vd_flags & VDF_ASYNC)) {
2530 /* Attach keyboard. */
2531 vt_allocate_keyboard(vd);
2533 /* Init 25 Hz timer. */
2534 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2536 /* Start timer when everything ready. */
2537 vd->vd_flags |= VDF_ASYNC;
2538 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2539 vd->vd_timer_armed = 1;
2544 /* Refill settings with new sizes. */
2549 vt_resize(struct vt_device *vd)
2551 struct vt_window *vw;
2554 for (i = 0; i < VT_MAXWINDOWS; i++) {
2555 vw = vd->vd_windows[i];
2557 /* Assign default font to window, if not textmode. */
2558 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2559 vw->vw_font = vtfont_ref(&vt_font_default);
2562 /* Resize terminal windows */
2563 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2564 DPRINTF(100, "%s: vt_change_font() is busy, "
2565 "window %d\n", __func__, i);
2571 vt_allocate(struct vt_driver *drv, void *softc)
2573 struct vt_device *vd;
2575 if (!vty_enabled(VTY_VT))
2578 if (main_vd->vd_driver == NULL) {
2579 main_vd->vd_driver = drv;
2580 printf("VT: initialize with new VT driver \"%s\".\n",
2584 * Check if have rights to replace current driver. For example:
2585 * it is bad idea to replace KMS driver with generic VGA one.
2587 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2588 printf("VT: Driver priority %d too low. Current %d\n ",
2589 drv->vd_priority, main_vd->vd_driver->vd_priority);
2592 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2593 main_vd->vd_driver->vd_name, drv->vd_name);
2597 if (vd->vd_flags & VDF_ASYNC) {
2598 /* Stop vt_flush periodic task. */
2599 vt_suspend_flush_timer(vd);
2601 * Mute current terminal until we done. vt_change_font (called
2602 * from vt_resize) will unmute it.
2604 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2608 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2612 vd->vd_flags &= ~VDF_TEXTMODE;
2614 vd->vd_driver = drv;
2615 vd->vd_softc = softc;
2616 vd->vd_driver->vd_init(vd);
2619 /* Update windows sizes and initialize last items. */
2623 if (vd->vd_flags & VDF_SPLASH)
2627 if (vd->vd_flags & VDF_ASYNC) {
2628 /* Allow to put chars now. */
2629 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2630 /* Rerun timer for screen updates. */
2631 vt_resume_flush_timer(vd, 0);
2635 * Register as console. If it already registered, cnadd() will ignore
2638 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2645 if (vt_suspendswitch == 0)
2647 /* Save current window. */
2648 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2649 /* Ask holding process to free window and switch to console window */
2650 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2657 if (vt_suspendswitch == 0)
2659 /* Switch back to saved window */
2660 if (main_vd->vd_savedwindow != NULL)
2661 vt_proc_window_switch(main_vd->vd_savedwindow);
2662 main_vd->vd_savedwindow = NULL;