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(debug, 0, "vt(9) debug level");
124 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
125 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
127 /* Allow to disable some keyboard combinations. */
128 VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
129 "See kbdmap(5) to configure.");
130 VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
131 "See kbdmap(5) to configure.");
132 VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
133 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
134 VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
135 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
136 VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
137 "See kbdmap(5) to configure.");
139 static struct vt_device vt_consdev;
140 static unsigned int vt_unit = 0;
141 static MALLOC_DEFINE(M_VT, "vt", "vt device");
142 struct vt_device *main_vd = &vt_consdev;
145 extern unsigned int vt_logo_width;
146 extern unsigned int vt_logo_height;
147 extern unsigned int vt_logo_depth;
148 extern unsigned char vt_logo_image[];
151 extern struct vt_font vt_font_default;
152 #ifndef SC_NO_CUTPASTE
153 extern struct vt_mouse_cursor vt_default_mouse_pointer;
156 static int signal_vt_rel(struct vt_window *);
157 static int signal_vt_acq(struct vt_window *);
158 static int finish_vt_rel(struct vt_window *, int, int *);
159 static int finish_vt_acq(struct vt_window *);
160 static int vt_window_switch(struct vt_window *);
161 static int vt_late_window_switch(struct vt_window *);
162 static int vt_proc_alive(struct vt_window *);
163 static void vt_resize(struct vt_device *);
164 static void vt_update_static(void *);
165 #ifndef SC_NO_CUTPASTE
166 static void vt_mouse_paste(void);
169 SET_DECLARE(vt_drv_set, struct vt_driver);
171 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
172 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
174 static struct terminal vt_consterm;
175 static struct vt_window vt_conswindow;
176 static struct vt_device vt_consdev = {
179 .vd_flags = VDF_INVALID,
180 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
181 .vd_curwindow = &vt_conswindow,
184 #ifndef SC_NO_CUTPASTE
190 .vd_mcursor = &vt_default_mouse_pointer,
191 .vd_mcursor_fg = TC_WHITE,
192 .vd_mcursor_bg = TC_BLACK,
195 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
196 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
197 static struct vt_window vt_conswindow = {
198 .vw_number = VT_CONSWINDOW,
199 .vw_flags = VWF_CONSOLE,
201 .vb_buffer = &vt_constextbuf[0],
202 .vb_rows = &vt_constextbufrows[0],
203 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
206 .vb_flags = VBF_STATIC,
207 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
208 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
214 .vw_device = &vt_consdev,
215 .vw_terminal = &vt_consterm,
216 .vw_kbdmode = K_XLATE,
219 static struct terminal vt_consterm = {
220 .tm_class = &vt_termclass,
221 .tm_softc = &vt_conswindow,
224 static struct consdev vt_consterm_consdev = {
225 .cn_ops = &termcn_cnops,
226 .cn_arg = &vt_consterm,
230 /* Add to set of consoles. */
231 DATA_SET(cons_set, vt_consterm_consdev);
234 * Right after kmem is done to allow early drivers to use locking and allocate
237 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
239 /* Delay until all devices attached, to not waste time. */
240 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
243 /* Initialize locks/mem depended members. */
245 vt_update_static(void *dummy)
248 if (!vty_enabled(VTY_VT))
250 if (main_vd->vd_driver != NULL)
251 printf("VT: running with driver \"%s\".\n",
252 main_vd->vd_driver->vd_name);
254 printf("VT: init without driver.\n");
256 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
257 cv_init(&main_vd->vd_winswitch, "vtwswt");
261 vt_schedule_flush(struct vt_device *vd, int ms)
265 /* Default to initial value. */
266 ms = 1000 / VT_TIMERFREQ;
268 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
272 vt_resume_flush_timer(struct vt_device *vd, int ms)
275 if (!(vd->vd_flags & VDF_ASYNC) ||
276 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
279 vt_schedule_flush(vd, ms);
283 vt_suspend_flush_timer(struct vt_device *vd)
286 if (!(vd->vd_flags & VDF_ASYNC) ||
287 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
290 callout_drain(&vd->vd_timer);
294 vt_switch_timer(void *arg)
297 vt_late_window_switch((struct vt_window *)arg);
301 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
306 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
312 vw->vw_kbdmode = mode;
318 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
322 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
330 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
335 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
341 vw->vw_kbdstate &= ~LOCK_MASK;
342 vw->vw_kbdstate |= state & LOCK_MASK;
348 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
352 state = vw->vw_kbdstate & LOCK_MASK;
353 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
361 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
366 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
372 vw->vw_kbdstate &= ~LED_MASK;
373 vw->vw_kbdstate |= leds & LED_MASK;
379 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
383 leds = vw->vw_kbdstate & LED_MASK;
384 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
392 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
395 DPRINTF(40, "%s\n", __func__);
396 curvw->vw_switch_to = vw;
397 /* Set timer to allow switch in case when process hang. */
398 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
399 vt_switch_timer, (void *)vw);
400 /* Notify process about vt switch attempt. */
401 DPRINTF(30, "%s: Notify process.\n", __func__);
402 signal_vt_rel(curvw);
408 vt_window_postswitch(struct vt_window *vw)
415 /* vt_late_window_switch will done VT switching for regular case. */
417 vt_late_window_switch(struct vt_window *vw)
421 callout_stop(&vw->vw_proc_dead_timer);
423 ret = vt_window_switch(vw);
427 /* Notify owner process about terminal availability. */
428 if (vw->vw_smode.mode == VT_PROCESS) {
429 ret = vt_window_postswitch(vw);
436 vt_proc_window_switch(struct vt_window *vw)
438 struct vt_window *curvw;
439 struct vt_device *vd;
443 curvw = vd->vd_curwindow;
445 if (curvw->vw_flags & VWF_VTYLOCK)
448 /* Ask current process permission to switch away. */
449 if (curvw->vw_smode.mode == VT_PROCESS) {
450 DPRINTF(30, "%s: VT_PROCESS ", __func__);
451 if (vt_proc_alive(curvw) == FALSE) {
452 DPRINTF(30, "Dead. Cleaning.");
455 DPRINTF(30, "%s: Signaling process.\n", __func__);
456 /* Alive, try to ask him. */
457 ret = vt_window_preswitch(vw, curvw);
458 /* Wait for process answer or timeout. */
464 ret = vt_late_window_switch(vw);
468 /* Switch window ignoring process locking. */
470 vt_window_switch(struct vt_window *vw)
472 struct vt_device *vd = vw->vw_device;
473 struct vt_window *curvw = vd->vd_curwindow;
482 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
487 vt_suspend_flush_timer(vd);
489 vd->vd_curwindow = vw;
490 vd->vd_flags |= VDF_INVALID;
491 cv_broadcast(&vd->vd_winswitch);
494 if (vd->vd_driver->vd_postswitch)
495 vd->vd_driver->vd_postswitch(vd);
497 vt_resume_flush_timer(vd, 0);
499 /* Restore per-window keyboard mode. */
501 kbd = kbd_get_keyboard(vd->vd_keyboard);
503 if (curvw->vw_kbdmode == K_XLATE)
504 vt_save_kbd_state(curvw, kbd);
506 vt_update_kbd_mode(vw, kbd);
507 vt_update_kbd_state(vw, kbd);
510 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
516 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
519 size->tp_row = vd->vd_height;
520 size->tp_col = vd->vd_width;
522 size->tp_row /= vf->vf_height;
523 size->tp_col /= vf->vf_width;
528 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
531 size->ws_row = size->ws_ypixel = vd->vd_height;
532 size->ws_col = size->ws_xpixel = vd->vd_width;
534 size->ws_row /= vf->vf_height;
535 size->ws_col /= vf->vf_width;
540 vt_compute_drawable_area(struct vt_window *vw)
542 struct vt_device *vd;
547 if (vw->vw_font == NULL) {
548 vw->vw_draw_area.tr_begin.tp_col = 0;
549 vw->vw_draw_area.tr_begin.tp_row = 0;
550 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
551 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
558 * Compute the drawable area, so that the text is centered on
562 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
563 vw->vw_draw_area.tr_begin.tp_row = (vd->vd_height % vf->vf_height) / 2;
564 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
565 vd->vd_width / vf->vf_width * vf->vf_width;
566 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
567 vd->vd_height / vf->vf_height * vf->vf_height;
571 vt_scroll(struct vt_window *vw, int offset, int whence)
576 if ((vw->vw_flags & VWF_SCROLL) == 0)
579 vt_termsize(vw->vw_device, vw->vw_font, &size);
581 diff = vthistory_seek(&vw->vw_buf, offset, whence);
583 vw->vw_device->vd_flags |= VDF_INVALID;
584 vt_resume_flush_timer(vw->vw_device, 0);
588 vt_machine_kbdevent(int c)
592 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
594 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
596 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
598 shutdown_nice(RB_HALT);
600 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
601 #ifndef SC_NO_CUTPASTE
602 /* Insert text from cut-paste buffer. */
606 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
608 shutdown_nice(RB_HALT|RB_POWEROFF);
610 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
612 * Request to immediate panic if sysctl
613 * kern.vt.enable_panic_key allow it.
616 panic("Forced by the panic key");
618 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
620 shutdown_nice(RB_AUTOBOOT);
622 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
623 /* Force activatation/deactivation of the screen saver. */
626 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
627 /* Put machine into Stand-By mode. */
628 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
630 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
631 /* Suspend machine. */
632 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
640 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
642 struct vt_device *vd;
646 /* Only special keys handled in ScrollLock mode */
647 if ((c & SPCLKEY) == 0)
653 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
654 vw = vd->vd_windows[c - F_SCR];
656 vt_proc_window_switch(vw);
664 /* Turn scrolling off. */
665 vt_scroll(vw, 0, VHS_END);
666 VTBUF_SLCK_DISABLE(&vw->vw_buf);
667 vw->vw_flags &= ~VWF_SCROLL;
670 case FKEY | F(49): /* Home key. */
671 vt_scroll(vw, 0, VHS_SET);
673 case FKEY | F(50): /* Arrow up. */
674 vt_scroll(vw, -1, VHS_CUR);
676 case FKEY | F(51): /* Page up. */
677 vt_termsize(vd, vw->vw_font, &size);
678 vt_scroll(vw, -size.tp_row, VHS_CUR);
680 case FKEY | F(57): /* End key. */
681 vt_scroll(vw, 0, VHS_END);
683 case FKEY | F(58): /* Arrow down. */
684 vt_scroll(vw, 1, VHS_CUR);
686 case FKEY | F(59): /* Page down. */
687 vt_termsize(vd, vw->vw_font, &size);
688 vt_scroll(vw, size.tp_row, VHS_CUR);
697 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
699 struct vt_window *vw = vd->vd_curwindow;
701 #if VT_ALT_TO_ESC_HACK
703 switch (c & ~RELKEY) {
704 case (SPCLKEY | RALT):
705 if (vt_enable_altgr != 0)
707 case (SPCLKEY | LALT):
708 vd->vd_kbstate &= ~ALKED;
710 /* Other keys ignored for RELKEY event. */
713 switch (c & ~RELKEY) {
714 case (SPCLKEY | RALT):
715 if (vt_enable_altgr != 0)
717 case (SPCLKEY | LALT):
718 vd->vd_kbstate |= ALKED;
723 /* Other keys ignored for RELKEY event. */
727 if (vt_machine_kbdevent(c))
730 if (vw->vw_flags & VWF_SCROLL) {
731 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
732 /* Scroll mode keys handled, nothing to do more. */
739 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
740 vw = vd->vd_windows[c - F_SCR];
742 vt_proc_window_switch(vw);
748 /* Switch to next VT. */
749 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
750 vw = vd->vd_windows[c];
752 vt_proc_window_switch(vw);
755 /* Switch to previous VT. */
756 c = (vw->vw_number - 1) % VT_MAXWINDOWS;
757 vw = vd->vd_windows[c];
759 vt_proc_window_switch(vw);
762 vt_save_kbd_state(vw, kbd);
764 if (vw->vw_kbdstate & SLKED) {
765 /* Turn scrolling on. */
766 vw->vw_flags |= VWF_SCROLL;
767 VTBUF_SLCK_ENABLE(&vw->vw_buf);
769 /* Turn scrolling off. */
770 vw->vw_flags &= ~VWF_SCROLL;
771 VTBUF_SLCK_DISABLE(&vw->vw_buf);
772 vt_scroll(vw, 0, VHS_END);
777 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
778 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
779 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
780 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
781 /* F1 through F12 keys. */
782 terminal_input_special(vw->vw_terminal,
783 TKEY_F1 + c - (FKEY | F(1)));
785 case FKEY | F(49): /* Home key. */
786 terminal_input_special(vw->vw_terminal, TKEY_HOME);
788 case FKEY | F(50): /* Arrow up. */
789 terminal_input_special(vw->vw_terminal, TKEY_UP);
791 case FKEY | F(51): /* Page up. */
792 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
794 case FKEY | F(53): /* Arrow left. */
795 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
797 case FKEY | F(55): /* Arrow right. */
798 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
800 case FKEY | F(57): /* End key. */
801 terminal_input_special(vw->vw_terminal, TKEY_END);
803 case FKEY | F(58): /* Arrow down. */
804 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
806 case FKEY | F(59): /* Page down. */
807 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
809 case FKEY | F(60): /* Insert key. */
810 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
812 case FKEY | F(61): /* Delete key. */
813 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
816 } else if (KEYFLAGS(c) == 0) {
817 /* Don't do UTF-8 conversion when doing raw mode. */
818 if (vw->vw_kbdmode == K_XLATE) {
819 #if VT_ALT_TO_ESC_HACK
820 if (vd->vd_kbstate & ALKED) {
822 * Prepend ESC sequence if one of ALT keys down.
824 terminal_input_char(vw->vw_terminal, 0x1b);
828 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
830 terminal_input_raw(vw->vw_terminal, c);
836 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
838 struct vt_device *vd = arg;
844 case KBDIO_UNLOADING:
846 vd->vd_keyboard = -1;
847 kbd_release(kbd, (void *)vd);
854 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
855 vt_processkey(kbd, vd, c);
861 vt_allocate_keyboard(struct vt_device *vd)
867 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
868 vd->vd_keyboard = idx0;
870 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
871 k0 = kbd_get_keyboard(idx0);
873 for (idx = kbd_find_keyboard2("*", -1, 0);
875 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
876 k = kbd_get_keyboard(idx);
878 if (idx == idx0 || KBD_IS_BUSY(k))
881 bzero(&ki, sizeof(ki));
882 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
883 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
884 ki.kb_unit = k->kb_unit;
886 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
889 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
890 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
892 DPRINTF(10, "%s: No keyboard found.\n", __func__);
896 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
902 vtterm_bell(struct terminal *tm)
904 struct vt_window *vw = tm->tm_softc;
905 struct vt_device *vd = vw->vw_device;
907 if (vd->vd_flags & VDF_QUIET_BELL)
910 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
914 vtterm_beep(struct terminal *tm, u_int param)
918 if ((param == 0) || ((param & 0xffff) == 0)) {
923 period = ((param >> 16) & 0xffff) * hz / 1000;
924 freq = 1193182 / (param & 0xffff);
926 sysbeep(freq, period);
930 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
932 struct vt_window *vw = tm->tm_softc;
934 vtbuf_cursor_position(&vw->vw_buf, p);
935 vt_resume_flush_timer(vw->vw_device, 0);
939 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
941 struct vt_window *vw = tm->tm_softc;
943 vtbuf_putchar(&vw->vw_buf, p, c);
944 vt_resume_flush_timer(vw->vw_device, 0);
948 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
950 struct vt_window *vw = tm->tm_softc;
952 vtbuf_fill_locked(&vw->vw_buf, r, c);
953 vt_resume_flush_timer(vw->vw_device, 0);
957 vtterm_copy(struct terminal *tm, const term_rect_t *r,
960 struct vt_window *vw = tm->tm_softc;
962 vtbuf_copy(&vw->vw_buf, r, p);
963 vt_resume_flush_timer(vw->vw_device, 0);
967 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
969 struct vt_window *vw = tm->tm_softc;
973 vtbuf_cursor_visibility(&vw->vw_buf, arg);
974 vt_resume_flush_timer(vw->vw_device, 0);
977 vw->vw_mouse_level = arg;
983 vt_determine_colors(term_char_t c, int cursor,
984 term_color_t *fg, term_color_t *bg)
991 *fg = TCHAR_FGCOLOR(c);
992 if (TCHAR_FORMAT(c) & TF_BOLD)
993 *fg = TCOLOR_LIGHT(*fg);
994 *bg = TCHAR_BGCOLOR(c);
996 if (TCHAR_FORMAT(c) & TF_REVERSE)
1008 #ifndef SC_NO_CUTPASTE
1010 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1012 unsigned int mx, my, x1, y1, x2, y2;
1015 * We use the cursor position saved during the current refresh,
1016 * in case the cursor moved since.
1018 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1019 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1021 x1 = area->tr_begin.tp_col;
1022 y1 = area->tr_begin.tp_row;
1023 x2 = area->tr_end.tp_col;
1024 y2 = area->tr_end.tp_row;
1026 if (((mx >= x1 && x2 - 1 >= mx) ||
1027 (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
1028 ((my >= y1 && y2 - 1 >= my) ||
1029 (my < y1 && my + vd->vd_mcursor->height >= y1)))
1036 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1039 struct vt_window *vw;
1043 vw = vd->vd_curwindow;
1046 x = vd->vd_mx_drawn;
1047 y = vd->vd_my_drawn;
1050 area.tr_begin.tp_col = x / vf->vf_width;
1051 area.tr_begin.tp_row = y / vf->vf_height;
1052 area.tr_end.tp_col =
1053 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1054 area.tr_end.tp_row =
1055 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1058 * No font loaded (ie. vt_vga operating in textmode).
1060 * FIXME: This fake area needs to be revisited once the
1061 * mouse cursor is supported in vt_vga's textmode.
1063 area.tr_begin.tp_col = x;
1064 area.tr_begin.tp_row = y;
1065 area.tr_end.tp_col = x + 2;
1066 area.tr_end.tp_row = y + 2;
1069 vtbuf_dirty(&vw->vw_buf, &area);
1074 vt_flush(struct vt_device *vd)
1076 struct vt_window *vw;
1080 #ifndef SC_NO_CUTPASTE
1081 int cursor_was_shown, cursor_moved;
1084 vw = vd->vd_curwindow;
1088 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1092 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1095 #ifndef SC_NO_CUTPASTE
1096 cursor_was_shown = vd->vd_mshown;
1097 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1098 vd->vd_my != vd->vd_my_drawn);
1100 /* Check if the cursor should be displayed or not. */
1101 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1102 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1103 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1110 * If the cursor changed display state or moved, we must mark
1111 * the old position as dirty, so that it's erased.
1113 if (cursor_was_shown != vd->vd_mshown ||
1114 (vd->vd_mshown && cursor_moved))
1115 vt_mark_mouse_position_as_dirty(vd);
1118 * Save position of the mouse cursor. It's used by backends to
1119 * know where to draw the cursor and during the next refresh to
1120 * erase the previous position.
1122 vd->vd_mx_drawn = vd->vd_mx;
1123 vd->vd_my_drawn = vd->vd_my;
1126 * If the cursor is displayed and has moved since last refresh,
1127 * mark the new position as dirty.
1129 if (vd->vd_mshown && cursor_moved)
1130 vt_mark_mouse_position_as_dirty(vd);
1133 vtbuf_undirty(&vw->vw_buf, &tarea);
1134 vt_termsize(vd, vf, &size);
1136 /* Force a full redraw when the screen contents are invalid. */
1137 if (vd->vd_flags & VDF_INVALID) {
1138 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1139 tarea.tr_end = size;
1141 vd->vd_flags &= ~VDF_INVALID;
1144 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1145 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1155 struct vt_device *vd;
1159 /* Update screen if required. */
1160 changed = vt_flush(vd);
1162 /* Schedule for next update. */
1164 vt_schedule_flush(vd, 0);
1166 vd->vd_timer_armed = 0;
1170 vtterm_done(struct terminal *tm)
1172 struct vt_window *vw = tm->tm_softc;
1173 struct vt_device *vd = vw->vw_device;
1175 if (kdb_active || panicstr != NULL) {
1176 /* Switch to the debugger. */
1177 if (vd->vd_curwindow != vw) {
1178 vd->vd_curwindow = vw;
1179 vd->vd_flags |= VDF_INVALID;
1180 if (vd->vd_driver->vd_postswitch)
1181 vd->vd_driver->vd_postswitch(vd);
1183 vd->vd_flags &= ~VDF_SPLASH;
1185 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1192 vtterm_splash(struct vt_device *vd)
1194 vt_axis_t top, left;
1196 /* Display a nice boot splash. */
1197 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1199 top = (vd->vd_height - vt_logo_height) / 2;
1200 left = (vd->vd_width - vt_logo_width) / 2;
1201 switch (vt_logo_depth) {
1203 /* XXX: Unhardcode colors! */
1204 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1205 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1206 left, top, TC_WHITE, TC_BLACK);
1208 vd->vd_flags |= VDF_SPLASH;
1215 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1217 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1218 struct vt_window *vw = tm->tm_softc;
1219 struct vt_device *vd = vw->vw_device;
1224 if (!vty_enabled(VTY_VT))
1227 if (vd->vd_flags & VDF_INITIALIZED)
1228 /* Initialization already done. */
1231 SET_FOREACH(vtdlist, vt_drv_set) {
1233 if (vtd->vd_probe == NULL)
1235 if (vtd->vd_probe(vd) == CN_DEAD)
1237 if ((vtdbest == NULL) ||
1238 (vtd->vd_priority > vtdbest->vd_priority))
1241 if (vtdbest == NULL) {
1242 cp->cn_pri = CN_DEAD;
1243 vd->vd_flags |= VDF_DEAD;
1245 vd->vd_driver = vtdbest;
1246 cp->cn_pri = vd->vd_driver->vd_init(vd);
1249 /* Check if driver's vt_init return CN_DEAD. */
1250 if (cp->cn_pri == CN_DEAD) {
1251 vd->vd_flags |= VDF_DEAD;
1254 /* Initialize any early-boot keyboard drivers */
1255 kbd_configure(KB_CONF_PROBE_ONLY);
1257 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1258 vd->vd_windows[VT_CONSWINDOW] = vw;
1259 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1261 /* Attach default font if not in TEXTMODE. */
1262 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1263 vw->vw_font = vtfont_ref(&vt_font_default);
1264 vt_compute_drawable_area(vw);
1268 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1269 * that we have the real viewable size, fix it in the static
1272 if (vd->vd_width != 0 && vd->vd_height != 0)
1273 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1275 vtbuf_init_early(&vw->vw_buf);
1276 vt_winsize(vd, vw->vw_font, &wsz);
1277 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1279 attr.ta_format = TCHAR_FORMAT(c);
1280 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1281 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1282 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1284 if (vtdbest != NULL) {
1288 vd->vd_flags |= VDF_INITIALIZED;
1293 vtterm_cngetc(struct terminal *tm)
1295 struct vt_window *vw = tm->tm_softc;
1296 struct vt_device *vd = vw->vw_device;
1300 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1301 return (*vw->vw_kbdsq++);
1303 /* Make sure the splash screen is not there. */
1304 if (vd->vd_flags & VDF_SPLASH) {
1306 vd->vd_flags &= ~VDF_SPLASH;
1307 /* Mark screen as invalid to force update */
1308 vd->vd_flags |= VDF_INVALID;
1312 /* Stripped down keyboard handler. */
1313 kbd = kbd_get_keyboard(vd->vd_keyboard);
1317 /* Force keyboard input mode to K_XLATE */
1318 vw->vw_kbdmode = K_XLATE;
1319 vt_update_kbd_mode(vw, kbd);
1321 /* Switch the keyboard to polling to make it work here. */
1322 kbdd_poll(kbd, TRUE);
1323 c = kbdd_read_char(kbd, 0);
1324 kbdd_poll(kbd, FALSE);
1328 if (vw->vw_flags & VWF_SCROLL) {
1329 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1334 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1338 vt_save_kbd_state(vw, kbd);
1339 if (vw->vw_kbdstate & SLKED) {
1340 /* Turn scrolling on. */
1341 vw->vw_flags |= VWF_SCROLL;
1342 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1344 /* Turn scrolling off. */
1345 vt_scroll(vw, 0, VHS_END);
1346 vw->vw_flags &= ~VWF_SCROLL;
1347 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1350 /* XXX: KDB can handle history. */
1351 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1352 vw->vw_kbdsq = "\x1b[A";
1354 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1355 vw->vw_kbdsq = "\x1b[B";
1357 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1358 vw->vw_kbdsq = "\x1b[C";
1360 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1361 vw->vw_kbdsq = "\x1b[D";
1365 /* Force refresh to make scrollback work. */
1367 } else if (KEYFLAGS(c) == 0) {
1368 return (KEYCHAR(c));
1371 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1372 return (*vw->vw_kbdsq++);
1378 vtterm_cngrab(struct terminal *tm)
1380 struct vt_device *vd;
1381 struct vt_window *vw;
1388 vt_window_switch(vw);
1390 kbd = kbd_get_keyboard(vd->vd_keyboard);
1394 if (vw->vw_grabbed++ > 0)
1398 * Make sure the keyboard is accessible even when the kbd device
1399 * driver is disabled.
1403 /* We shall always use the keyboard in the XLATE mode here. */
1404 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1405 vw->vw_kbdmode = K_XLATE;
1406 vt_update_kbd_mode(vw, kbd);
1408 kbdd_poll(kbd, TRUE);
1412 vtterm_cnungrab(struct terminal *tm)
1414 struct vt_device *vd;
1415 struct vt_window *vw;
1421 kbd = kbd_get_keyboard(vd->vd_keyboard);
1425 if (--vw->vw_grabbed > 0)
1428 kbdd_poll(kbd, FALSE);
1430 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1431 vt_update_kbd_mode(vw, kbd);
1436 vtterm_opened(struct terminal *tm, int opened)
1438 struct vt_window *vw = tm->tm_softc;
1439 struct vt_device *vd = vw->vw_device;
1442 vd->vd_flags &= ~VDF_SPLASH;
1444 vw->vw_flags |= VWF_OPENED;
1446 vw->vw_flags &= ~VWF_OPENED;
1447 /* TODO: finish ACQ/REL */
1453 vt_set_border(struct vt_window *vw, term_color_t c)
1455 struct vt_device *vd = vw->vw_device;
1457 if (vd->vd_driver->vd_drawrect == NULL)
1461 if (vw->vw_draw_area.tr_begin.tp_row > 0)
1462 vd->vd_driver->vd_drawrect(vd,
1464 vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1468 if (vw->vw_draw_area.tr_begin.tp_col > 0)
1469 vd->vd_driver->vd_drawrect(vd,
1471 vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1475 if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1476 vd->vd_driver->vd_drawrect(vd,
1477 vw->vw_draw_area.tr_end.tp_col - 1, 0,
1478 vd->vd_width - 1, vd->vd_height - 1,
1482 if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1483 vd->vd_driver->vd_drawrect(vd,
1484 0, vw->vw_draw_area.tr_end.tp_row - 1,
1485 vd->vd_width - 1, vd->vd_height - 1,
1492 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1494 struct vt_device *vd = vw->vw_device;
1495 struct terminal *tm = vw->vw_terminal;
1502 * Changing fonts is a little tricky. We must prevent
1503 * simultaneous access to the device, so we must stop
1504 * the display timer and the terminal from accessing.
1505 * We need to switch fonts and grow our screen buffer.
1507 * XXX: Right now the code uses terminal_mute() to
1508 * prevent data from reaching the console driver while
1509 * resizing the screen buffer. This isn't elegant...
1513 if (vw->vw_flags & VWF_BUSY) {
1514 /* Another process is changing the font. */
1518 vw->vw_flags |= VWF_BUSY;
1521 vt_termsize(vd, vf, &size);
1522 vt_winsize(vd, vf, &wsz);
1524 /* Grow the screen buffer and terminal. */
1525 terminal_mute(tm, 1);
1526 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1527 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1528 terminal_mute(tm, 0);
1530 /* Actually apply the font to the current window. */
1532 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1534 * In case vt_change_font called to update size we don't need
1535 * to update font link.
1537 vtfont_unref(vw->vw_font);
1538 vw->vw_font = vtfont_ref(vf);
1542 * Compute the drawable area and move the mouse cursor inside
1543 * it, in case the new area is smaller than the previous one.
1545 vt_compute_drawable_area(vw);
1546 vd->vd_mx = min(vd->vd_mx,
1547 vw->vw_draw_area.tr_end.tp_col -
1548 vw->vw_draw_area.tr_begin.tp_col - 1);
1549 vd->vd_my = min(vd->vd_my,
1550 vw->vw_draw_area.tr_end.tp_row -
1551 vw->vw_draw_area.tr_begin.tp_row - 1);
1553 /* Force a full redraw the next timer tick. */
1554 if (vd->vd_curwindow == vw) {
1555 vt_set_border(vw, TC_BLACK);
1556 vd->vd_flags |= VDF_INVALID;
1557 vt_resume_flush_timer(vw->vw_device, 0);
1559 vw->vw_flags &= ~VWF_BUSY;
1565 vt_proc_alive(struct vt_window *vw)
1569 if (vw->vw_smode.mode != VT_PROCESS)
1573 if ((p = pfind(vw->vw_pid)) != NULL)
1575 if (vw->vw_proc == p)
1578 vw->vw_smode.mode = VT_AUTO;
1579 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1586 signal_vt_rel(struct vt_window *vw)
1589 if (vw->vw_smode.mode != VT_PROCESS)
1591 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1596 vw->vw_flags |= VWF_SWWAIT_REL;
1597 PROC_LOCK(vw->vw_proc);
1598 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1599 PROC_UNLOCK(vw->vw_proc);
1600 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1605 signal_vt_acq(struct vt_window *vw)
1608 if (vw->vw_smode.mode != VT_PROCESS)
1610 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1611 cnavailable(vw->vw_terminal->consdev, FALSE);
1612 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1617 vw->vw_flags |= VWF_SWWAIT_ACQ;
1618 PROC_LOCK(vw->vw_proc);
1619 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1620 PROC_UNLOCK(vw->vw_proc);
1621 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1626 finish_vt_rel(struct vt_window *vw, int release, int *s)
1629 if (vw->vw_flags & VWF_SWWAIT_REL) {
1630 vw->vw_flags &= ~VWF_SWWAIT_REL;
1632 callout_drain(&vw->vw_proc_dead_timer);
1633 vt_late_window_switch(vw->vw_switch_to);
1641 finish_vt_acq(struct vt_window *vw)
1644 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1645 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1651 #ifndef SC_NO_CUTPASTE
1653 vt_mouse_terminput_button(struct vt_device *vd, int button)
1655 struct vt_window *vw;
1657 char mouseb[6] = "\x1B[M";
1660 vw = vd->vd_curwindow;
1663 /* Translate to char position. */
1664 x = vd->vd_mx / vf->vf_width;
1665 y = vd->vd_my / vf->vf_height;
1666 /* Avoid overflow. */
1667 x = MIN(x, 255 - '!');
1668 y = MIN(y, 255 - '!');
1670 mouseb[3] = ' ' + button;
1671 mouseb[4] = '!' + x;
1672 mouseb[5] = '!' + y;
1674 for (i = 0; i < sizeof(mouseb); i++)
1675 terminal_input_char(vw->vw_terminal, mouseb[i]);
1679 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1684 case MOUSE_BUTTON_EVENT:
1686 /* Mouse button pressed. */
1687 if (event & MOUSE_BUTTON1DOWN)
1688 vt_mouse_terminput_button(vd, 0);
1689 if (event & MOUSE_BUTTON2DOWN)
1690 vt_mouse_terminput_button(vd, 1);
1691 if (event & MOUSE_BUTTON3DOWN)
1692 vt_mouse_terminput_button(vd, 2);
1694 /* Mouse button released. */
1695 vt_mouse_terminput_button(vd, 3);
1699 case MOUSE_MOTION_EVENT:
1700 if (mouse->u.data.z < 0) {
1702 sc_mouse_input_button(vd, 64);
1703 } else if (mouse->u.data.z > 0) {
1705 sc_mouse_input_button(vd, 65);
1718 len = VD_PASTEBUFLEN(main_vd);
1719 buf = VD_PASTEBUF(main_vd);
1720 len /= sizeof(term_char_t);
1721 for (i = 0; i < len; i++) {
1724 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1730 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1732 struct vt_device *vd;
1733 struct vt_window *vw;
1739 vw = vd->vd_curwindow;
1743 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1745 * Either the mouse is disabled, or the window is in
1746 * "graphics mode". The graphics mode is usually set by
1747 * an X server, using the KDSETMODE ioctl.
1751 if (vf == NULL) /* Text mode. */
1755 * TODO: add flag about pointer position changed, to not redraw chars
1756 * under mouse pointer when nothing changed.
1759 if (vw->vw_mouse_level > 0)
1760 vt_mouse_terminput(vd, type, x, y, event, cnt);
1764 case MOUSE_MOTION_EVENT:
1769 vt_termsize(vd, vf, &size);
1774 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1775 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1779 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1780 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1781 vd->vd_mx / vf->vf_width,
1782 vd->vd_my / vf->vf_height);
1784 vt_resume_flush_timer(vw->vw_device, 0);
1786 case MOUSE_BUTTON_EVENT:
1794 case MOUSE_BUTTON1DOWN:
1797 mark = VTB_MARK_END;
1799 case 1: /* single click: start cut operation */
1800 mark = VTB_MARK_START;
1802 case 2: /* double click: cut a word */
1803 mark = VTB_MARK_WORD;
1805 case 3: /* triple click: cut a line */
1806 mark = VTB_MARK_ROW;
1810 case VT_MOUSE_PASTEBUTTON:
1819 case VT_MOUSE_EXTENDBUTTON:
1822 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1823 mark = VTB_MARK_EXTEND;
1828 mark = VTB_MARK_EXTEND;
1836 /* Save buttons state. */
1838 vd->vd_mstate |= event;
1840 vd->vd_mstate &= ~event;
1842 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1843 vd->vd_my / vf->vf_height) == 1) {
1845 * We have something marked to copy, so update pointer to
1846 * window with selection.
1848 vt_resume_flush_timer(vw->vw_device, 0);
1854 case VTB_MARK_EXTEND:
1857 /* Other types of mark do not require to copy data. */
1861 /* Get current selection size in bytes. */
1862 len = vtbuf_get_marked_len(&vw->vw_buf);
1866 /* Reallocate buffer only if old one is too small. */
1867 if (len > VD_PASTEBUFSZ(vd)) {
1868 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1870 /* Update buffer size. */
1871 VD_PASTEBUFSZ(vd) = len;
1873 /* Request copy/paste buffer data, no more than `len' */
1874 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1877 VD_PASTEBUFLEN(vd) = len;
1879 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1884 vt_mouse_state(int show)
1886 struct vt_device *vd;
1887 struct vt_window *vw;
1890 vw = vd->vd_curwindow;
1894 vw->vw_flags |= VWF_MOUSE_HIDE;
1897 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1901 /* Mark mouse position as dirty. */
1902 vt_mark_mouse_position_as_dirty(vd);
1903 vt_resume_flush_timer(vw->vw_device, 0);
1908 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1909 int nprot, vm_memattr_t *memattr)
1911 struct vt_window *vw = tm->tm_softc;
1912 struct vt_device *vd = vw->vw_device;
1914 if (vd->vd_driver->vd_fb_mmap)
1915 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1922 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1925 struct vt_window *vw = tm->tm_softc;
1926 struct vt_device *vd = vw->vw_device;
1929 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1930 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1941 cmd = VT_WAITACTIVE;
1967 ival = IOCPARM_IVAL(data);
1968 data = (caddr_t)&ival;
1973 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1974 if (*(int *)data & ~0x7f)
1979 case GIO_DEADKEYMAP:
1980 case PIO_DEADKEYMAP:
1985 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1986 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1987 case KBADDKBD: /* add/remove keyboard to/from mux */
1992 kbd = kbd_get_keyboard(vd->vd_keyboard);
1994 error = kbdd_ioctl(kbd, cmd, data);
1996 if (error == ENOIOCTL) {
1997 if (cmd == KDGKBTYPE) {
1998 /* always return something? XXX */
2006 case KDGKBSTATE: { /* get keyboard state (locks) */
2009 if (vw == vd->vd_curwindow) {
2011 kbd = kbd_get_keyboard(vd->vd_keyboard);
2013 error = vt_save_kbd_state(vw, kbd);
2020 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2024 case KDSKBSTATE: { /* set keyboard state (locks) */
2027 state = *(int *)data;
2028 if (state & ~LOCK_MASK)
2031 vw->vw_kbdstate &= ~LOCK_MASK;
2032 vw->vw_kbdstate |= state;
2035 if (vw == vd->vd_curwindow) {
2037 kbd = kbd_get_keyboard(vd->vd_keyboard);
2039 error = vt_update_kbd_state(vw, kbd);
2045 case KDGETLED: { /* get keyboard LED status */
2048 if (vw == vd->vd_curwindow) {
2050 kbd = kbd_get_keyboard(vd->vd_keyboard);
2052 error = vt_save_kbd_leds(vw, kbd);
2059 *(int *)data = vw->vw_kbdstate & LED_MASK;
2063 case KDSETLED: { /* set keyboard LED status */
2066 leds = *(int *)data;
2067 if (leds & ~LED_MASK)
2070 vw->vw_kbdstate &= ~LED_MASK;
2071 vw->vw_kbdstate |= leds;
2074 if (vw == vd->vd_curwindow) {
2076 kbd = kbd_get_keyboard(vd->vd_keyboard);
2078 error = vt_update_kbd_leds(vw, kbd);
2087 if (vw == vd->vd_curwindow) {
2089 kbd = kbd_get_keyboard(vd->vd_keyboard);
2091 error = vt_save_kbd_mode(vw, kbd);
2098 *(int *)data = vw->vw_kbdmode;
2105 mode = *(int *)data;
2110 vw->vw_kbdmode = mode;
2113 if (vw == vd->vd_curwindow) {
2115 kbd = kbd_get_keyboard(vd->vd_keyboard);
2117 error = vt_update_kbd_mode(vw, kbd);
2127 case FBIO_GETWINORG: /* get frame buffer window origin */
2128 case FBIO_GETDISPSTART: /* get display start address */
2129 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2130 case FBIO_BLANK: /* blank display */
2131 if (vd->vd_driver->vd_fb_ioctl)
2132 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2134 case CONS_BLANKTIME:
2139 *(int *)data = M_CG640x480;
2141 case CONS_BELLTYPE: /* set bell type sound */
2142 if ((*(int *)data) & CONS_QUIET_BELL)
2143 vd->vd_flags |= VDF_QUIET_BELL;
2145 vd->vd_flags &= ~VDF_QUIET_BELL;
2147 case CONS_GETINFO: {
2148 vid_info_t *vi = (vid_info_t *)data;
2149 if (vi->size != sizeof(struct vid_info))
2152 if (vw == vd->vd_curwindow) {
2153 kbd = kbd_get_keyboard(vd->vd_keyboard);
2155 vt_save_kbd_state(vw, kbd);
2158 vi->m_num = vd->vd_curwindow->vw_number + 1;
2159 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2160 /* XXX: other fields! */
2164 *(int *)data = 0x200;
2169 case CONS_MOUSECTL: {
2170 mouse_info_t *mouse = (mouse_info_t*)data;
2173 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2174 * should not be applied to individual TTYs, but only to
2177 switch (mouse->operation) {
2179 if (vd->vd_flags & VDF_MOUSECURSOR) {
2180 vd->vd_flags &= ~VDF_MOUSECURSOR;
2181 #ifndef SC_NO_CUTPASTE
2182 vt_mouse_state(VT_MOUSE_HIDE);
2187 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2188 vd->vd_flags |= VDF_MOUSECURSOR;
2189 vd->vd_mx = vd->vd_width / 2;
2190 vd->vd_my = vd->vd_height / 2;
2191 #ifndef SC_NO_CUTPASTE
2192 vt_mouse_state(VT_MOUSE_SHOW);
2203 if (vd->vd_flags & VDF_TEXTMODE)
2206 error = vtfont_load((void *)data, &vf);
2210 error = vt_change_font(vw, vf);
2215 scrmap_t *sm = (scrmap_t *)data;
2217 /* We don't have screen maps, so return a handcrafted one. */
2218 for (i = 0; i < 256; i++)
2224 * FIXME: This implementation is incomplete compared to
2227 switch (*(int *)data) {
2231 vw->vw_flags &= ~VWF_GRAPHICS;
2234 vw->vw_flags |= VWF_GRAPHICS;
2238 case KDENABIO: /* allow io operations */
2239 error = priv_check(td, PRIV_IO);
2242 error = securelevel_gt(td->td_ucred, 0);
2245 #if defined(__i386__)
2246 td->td_frame->tf_eflags |= PSL_IOPL;
2247 #elif defined(__amd64__)
2248 td->td_frame->tf_rflags |= PSL_IOPL;
2251 case KDDISABIO: /* disallow io operations (default) */
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 KDMKTONE: /* sound the bell */
2259 vtterm_beep(tm, *(u_int *)data);
2261 case KIOCSOUND: /* make tone (*data) hz */
2264 case CONS_SETKBD: /* set the new keyboard */
2267 if (vd->vd_keyboard != *(int *)data) {
2268 kbd = kbd_get_keyboard(*(int *)data);
2273 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2274 (void *)vd, vt_kbdevent, vd);
2276 if (vd->vd_keyboard != -1) {
2277 vt_save_kbd_state(vd->vd_curwindow, kbd);
2278 kbd_release(kbd, (void *)vd);
2280 kbd = kbd_get_keyboard(i);
2281 vd->vd_keyboard = i;
2283 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2284 vt_update_kbd_state(vd->vd_curwindow, kbd);
2286 error = EPERM; /* XXX */
2291 case CONS_RELKBD: /* release the current keyboard */
2294 if (vd->vd_keyboard != -1) {
2295 kbd = kbd_get_keyboard(vd->vd_keyboard);
2300 vt_save_kbd_state(vd->vd_curwindow, kbd);
2301 error = kbd_release(kbd, (void *)vd);
2303 vd->vd_keyboard = -1;
2310 win = *(int *)data - 1;
2311 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2313 if ((win >= VT_MAXWINDOWS) || (win < 0))
2315 return (vt_proc_window_switch(vd->vd_windows[win]));
2318 *(int *)data = vd->vd_curwindow->vw_number + 1;
2321 *(int *)data = vw->vw_number + 1;
2324 /* TODO: Check current state, switching can be in progress. */
2325 if ((*(int *)data) == 0x01)
2326 vw->vw_flags |= VWF_VTYLOCK;
2327 else if ((*(int *)data) == 0x02)
2328 vw->vw_flags &= ~VWF_VTYLOCK;
2334 for (i = 0; i < VT_MAXWINDOWS; i++) {
2335 vw = vd->vd_windows[i];
2338 if (!(vw->vw_flags & VWF_OPENED)) {
2339 *(int *)data = vw->vw_number + 1;
2349 i = *(unsigned int *)data;
2350 if (i > VT_MAXWINDOWS)
2353 vw = vd->vd_windows[i - 1];
2356 while (vd->vd_curwindow != vw && error == 0)
2357 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2360 case VT_SETMODE: { /* set screen switcher mode */
2361 struct vt_mode *mode;
2364 mode = (struct vt_mode *)data;
2365 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2366 if (vw->vw_smode.mode == VT_PROCESS) {
2367 p1 = pfind(vw->vw_pid);
2368 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2371 DPRINTF(5, "error EPERM\n");
2377 if (mode->mode == VT_AUTO) {
2378 vw->vw_smode.mode = VT_AUTO;
2381 DPRINTF(5, "VT_AUTO, ");
2382 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2383 cnavailable(vw->vw_terminal->consdev, TRUE);
2384 /* were we in the middle of the vty switching process? */
2385 if (finish_vt_rel(vw, TRUE, &s) == 0)
2386 DPRINTF(5, "reset WAIT_REL, ");
2387 if (finish_vt_acq(vw) == 0)
2388 DPRINTF(5, "reset WAIT_ACQ, ");
2390 } else if (mode->mode == VT_PROCESS) {
2391 if (!ISSIGVALID(mode->relsig) ||
2392 !ISSIGVALID(mode->acqsig) ||
2393 !ISSIGVALID(mode->frsig)) {
2394 DPRINTF(5, "error EINVAL\n");
2397 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2398 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2399 vw->vw_proc = td->td_proc;
2400 vw->vw_pid = vw->vw_proc->p_pid;
2401 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2402 cnavailable(vw->vw_terminal->consdev, FALSE);
2404 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2411 case VT_GETMODE: /* get screen switcher mode */
2412 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2415 case VT_RELDISP: /* screen switcher ioctl */
2417 * This must be the current vty which is in the VT_PROCESS
2420 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2424 /* ...and this process is controlling it. */
2425 if (vw->vw_proc != td->td_proc) {
2429 switch(*(int *)data) {
2430 case VT_FALSE: /* user refuses to release screen, abort */
2431 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2432 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2433 SC_DRIVER_NAME, VT_UNIT(vw));
2435 case VT_TRUE: /* user has released screen, go on */
2436 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2437 if (vw->vw_flags & VWF_SWWAIT_REL) {
2438 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2439 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2440 SC_DRIVER_NAME, VT_UNIT(vw));
2445 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2446 if ((error = finish_vt_acq(vw)) == 0)
2447 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2448 SC_DRIVER_NAME, VT_UNIT(vw));
2459 static struct vt_window *
2460 vt_allocate_window(struct vt_device *vd, unsigned int window)
2462 struct vt_window *vw;
2463 struct terminal *tm;
2467 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2469 vw->vw_number = window;
2470 vw->vw_kbdmode = K_XLATE;
2472 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2473 vw->vw_font = vtfont_ref(&vt_font_default);
2474 vt_compute_drawable_area(vw);
2477 vt_termsize(vd, vw->vw_font, &size);
2478 vt_winsize(vd, vw->vw_font, &wsz);
2479 vtbuf_init(&vw->vw_buf, &size);
2481 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2482 terminal_set_winsize(tm, &wsz);
2483 vd->vd_windows[window] = vw;
2484 callout_init(&vw->vw_proc_dead_timer, 0);
2490 vt_upgrade(struct vt_device *vd)
2492 struct vt_window *vw;
2495 if (!vty_enabled(VTY_VT))
2497 if (main_vd->vd_driver == NULL)
2500 for (i = 0; i < VT_MAXWINDOWS; i++) {
2501 vw = vd->vd_windows[i];
2504 vw = vt_allocate_window(vd, i);
2506 if (!(vw->vw_flags & VWF_READY)) {
2507 callout_init(&vw->vw_proc_dead_timer, 0);
2508 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2509 vw->vw_flags |= VWF_READY;
2510 if (vw->vw_flags & VWF_CONSOLE) {
2511 /* For existing console window. */
2512 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2513 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2519 if (vd->vd_curwindow == NULL)
2520 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2522 if (!(vd->vd_flags & VDF_ASYNC)) {
2523 /* Attach keyboard. */
2524 vt_allocate_keyboard(vd);
2526 /* Init 25 Hz timer. */
2527 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2529 /* Start timer when everything ready. */
2530 vd->vd_flags |= VDF_ASYNC;
2531 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2532 vd->vd_timer_armed = 1;
2537 /* Refill settings with new sizes. */
2542 vt_resize(struct vt_device *vd)
2544 struct vt_window *vw;
2547 for (i = 0; i < VT_MAXWINDOWS; i++) {
2548 vw = vd->vd_windows[i];
2550 /* Assign default font to window, if not textmode. */
2551 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2552 vw->vw_font = vtfont_ref(&vt_font_default);
2555 /* Resize terminal windows */
2556 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2557 DPRINTF(100, "%s: vt_change_font() is busy, "
2558 "window %d\n", __func__, i);
2564 vt_allocate(struct vt_driver *drv, void *softc)
2566 struct vt_device *vd;
2568 if (!vty_enabled(VTY_VT))
2571 if (main_vd->vd_driver == NULL) {
2572 main_vd->vd_driver = drv;
2573 printf("VT: initialize with new VT driver \"%s\".\n",
2577 * Check if have rights to replace current driver. For example:
2578 * it is bad idea to replace KMS driver with generic VGA one.
2580 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2581 printf("VT: Driver priority %d too low. Current %d\n ",
2582 drv->vd_priority, main_vd->vd_driver->vd_priority);
2585 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2586 main_vd->vd_driver->vd_name, drv->vd_name);
2590 if (vd->vd_flags & VDF_ASYNC) {
2591 /* Stop vt_flush periodic task. */
2592 vt_suspend_flush_timer(vd);
2594 * Mute current terminal until we done. vt_change_font (called
2595 * from vt_resize) will unmute it.
2597 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2601 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2605 vd->vd_flags &= ~VDF_TEXTMODE;
2607 vd->vd_driver = drv;
2608 vd->vd_softc = softc;
2609 vd->vd_driver->vd_init(vd);
2612 /* Update windows sizes and initialize last items. */
2616 if (vd->vd_flags & VDF_SPLASH)
2620 if (vd->vd_flags & VDF_ASYNC) {
2621 /* Allow to put chars now. */
2622 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2623 /* Rerun timer for screen updates. */
2624 vt_resume_flush_timer(vd, 0);
2628 * Register as console. If it already registered, cnadd() will ignore
2631 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2638 if (vt_suspendswitch == 0)
2640 /* Save current window. */
2641 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2642 /* Ask holding process to free window and switch to console window */
2643 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2650 if (vt_suspendswitch == 0)
2652 /* Switch back to saved window */
2653 if (main_vd->vd_savedwindow != NULL)
2654 vt_proc_window_switch(main_vd->vd_savedwindow);
2655 main_vd->vd_savedwindow = NULL;