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)
117 #define VT_LOCK_ASSERT(vd, what) mtx_assert(&(vd)->vd_lock, what)
119 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
122 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
123 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
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 * As long as this function is called locked, callout_stop()
288 * has the same effect like callout_drain() with regard to
289 * preventing the callback function from executing.
291 VT_LOCK_ASSERT(vd, MA_OWNED);
293 if (!(vd->vd_flags & VDF_ASYNC) ||
294 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
297 callout_stop(&vd->vd_timer);
301 vt_switch_timer(void *arg)
304 vt_late_window_switch((struct vt_window *)arg);
308 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
313 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
319 vw->vw_kbdmode = mode;
325 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
329 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
337 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
342 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
348 vw->vw_kbdstate &= ~LOCK_MASK;
349 vw->vw_kbdstate |= state & LOCK_MASK;
355 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
359 state = vw->vw_kbdstate & LOCK_MASK;
360 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
368 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
373 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
379 vw->vw_kbdstate &= ~LED_MASK;
380 vw->vw_kbdstate |= leds & LED_MASK;
386 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
390 leds = vw->vw_kbdstate & LED_MASK;
391 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
399 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
402 DPRINTF(40, "%s\n", __func__);
403 curvw->vw_switch_to = vw;
404 /* Set timer to allow switch in case when process hang. */
405 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
406 vt_switch_timer, (void *)vw);
407 /* Notify process about vt switch attempt. */
408 DPRINTF(30, "%s: Notify process.\n", __func__);
409 signal_vt_rel(curvw);
415 vt_window_postswitch(struct vt_window *vw)
422 /* vt_late_window_switch will done VT switching for regular case. */
424 vt_late_window_switch(struct vt_window *vw)
428 callout_stop(&vw->vw_proc_dead_timer);
430 ret = vt_window_switch(vw);
434 /* Notify owner process about terminal availability. */
435 if (vw->vw_smode.mode == VT_PROCESS) {
436 ret = vt_window_postswitch(vw);
443 vt_proc_window_switch(struct vt_window *vw)
445 struct vt_window *curvw;
446 struct vt_device *vd;
450 curvw = vd->vd_curwindow;
452 if (curvw->vw_flags & VWF_VTYLOCK)
455 /* Ask current process permission to switch away. */
456 if (curvw->vw_smode.mode == VT_PROCESS) {
457 DPRINTF(30, "%s: VT_PROCESS ", __func__);
458 if (vt_proc_alive(curvw) == FALSE) {
459 DPRINTF(30, "Dead. Cleaning.");
462 DPRINTF(30, "%s: Signaling process.\n", __func__);
463 /* Alive, try to ask him. */
464 ret = vt_window_preswitch(vw, curvw);
465 /* Wait for process answer or timeout. */
471 ret = vt_late_window_switch(vw);
475 /* Switch window ignoring process locking. */
477 vt_window_switch(struct vt_window *vw)
479 struct vt_device *vd = vw->vw_device;
480 struct vt_window *curvw = vd->vd_curwindow;
489 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
494 vt_suspend_flush_timer(vd);
496 vd->vd_curwindow = vw;
497 vd->vd_flags |= VDF_INVALID;
498 cv_broadcast(&vd->vd_winswitch);
501 if (vd->vd_driver->vd_postswitch)
502 vd->vd_driver->vd_postswitch(vd);
504 vt_resume_flush_timer(vd, 0);
506 /* Restore per-window keyboard mode. */
508 kbd = kbd_get_keyboard(vd->vd_keyboard);
510 if (curvw->vw_kbdmode == K_XLATE)
511 vt_save_kbd_state(curvw, kbd);
513 vt_update_kbd_mode(vw, kbd);
514 vt_update_kbd_state(vw, kbd);
517 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
523 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
526 size->tp_row = vd->vd_height;
527 size->tp_col = vd->vd_width;
529 size->tp_row /= vf->vf_height;
530 size->tp_col /= vf->vf_width;
535 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
538 size->ws_row = size->ws_ypixel = vd->vd_height;
539 size->ws_col = size->ws_xpixel = vd->vd_width;
541 size->ws_row /= vf->vf_height;
542 size->ws_col /= vf->vf_width;
547 vt_compute_drawable_area(struct vt_window *vw)
549 struct vt_device *vd;
554 if (vw->vw_font == NULL) {
555 vw->vw_draw_area.tr_begin.tp_col = 0;
556 vw->vw_draw_area.tr_begin.tp_row = 0;
557 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
558 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
565 * Compute the drawable area, so that the text is centered on
569 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
570 vw->vw_draw_area.tr_begin.tp_row = (vd->vd_height % vf->vf_height) / 2;
571 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
572 vd->vd_width / vf->vf_width * vf->vf_width;
573 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
574 vd->vd_height / vf->vf_height * vf->vf_height;
578 vt_scroll(struct vt_window *vw, int offset, int whence)
583 if ((vw->vw_flags & VWF_SCROLL) == 0)
586 vt_termsize(vw->vw_device, vw->vw_font, &size);
588 diff = vthistory_seek(&vw->vw_buf, offset, whence);
590 vw->vw_device->vd_flags |= VDF_INVALID;
591 vt_resume_flush_timer(vw->vw_device, 0);
595 vt_machine_kbdevent(int c)
599 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
601 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
603 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
605 shutdown_nice(RB_HALT);
607 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
608 #ifndef SC_NO_CUTPASTE
609 /* Insert text from cut-paste buffer. */
613 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
615 shutdown_nice(RB_HALT|RB_POWEROFF);
617 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
619 * Request to immediate panic if sysctl
620 * kern.vt.enable_panic_key allow it.
623 panic("Forced by the panic key");
625 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
627 shutdown_nice(RB_AUTOBOOT);
629 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
630 /* Force activatation/deactivation of the screen saver. */
633 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
634 /* Put machine into Stand-By mode. */
635 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
637 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
638 /* Suspend machine. */
639 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
647 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
649 struct vt_device *vd;
653 /* Only special keys handled in ScrollLock mode */
654 if ((c & SPCLKEY) == 0)
660 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
661 vw = vd->vd_windows[c - F_SCR];
663 vt_proc_window_switch(vw);
671 /* Turn scrolling off. */
672 vt_scroll(vw, 0, VHS_END);
673 VTBUF_SLCK_DISABLE(&vw->vw_buf);
674 vw->vw_flags &= ~VWF_SCROLL;
677 case FKEY | F(49): /* Home key. */
678 vt_scroll(vw, 0, VHS_SET);
680 case FKEY | F(50): /* Arrow up. */
681 vt_scroll(vw, -1, VHS_CUR);
683 case FKEY | F(51): /* Page up. */
684 vt_termsize(vd, vw->vw_font, &size);
685 vt_scroll(vw, -size.tp_row, VHS_CUR);
687 case FKEY | F(57): /* End key. */
688 vt_scroll(vw, 0, VHS_END);
690 case FKEY | F(58): /* Arrow down. */
691 vt_scroll(vw, 1, VHS_CUR);
693 case FKEY | F(59): /* Page down. */
694 vt_termsize(vd, vw->vw_font, &size);
695 vt_scroll(vw, size.tp_row, VHS_CUR);
704 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
706 struct vt_window *vw = vd->vd_curwindow;
708 #if VT_ALT_TO_ESC_HACK
710 switch (c & ~RELKEY) {
711 case (SPCLKEY | RALT):
712 if (vt_enable_altgr != 0)
714 case (SPCLKEY | LALT):
715 vd->vd_kbstate &= ~ALKED;
717 /* Other keys ignored for RELKEY event. */
720 switch (c & ~RELKEY) {
721 case (SPCLKEY | RALT):
722 if (vt_enable_altgr != 0)
724 case (SPCLKEY | LALT):
725 vd->vd_kbstate |= ALKED;
730 /* Other keys ignored for RELKEY event. */
734 if (vt_machine_kbdevent(c))
737 if (vw->vw_flags & VWF_SCROLL) {
738 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
739 /* Scroll mode keys handled, nothing to do more. */
746 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
747 vw = vd->vd_windows[c - F_SCR];
749 vt_proc_window_switch(vw);
755 /* Switch to next VT. */
756 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
757 vw = vd->vd_windows[c];
759 vt_proc_window_switch(vw);
762 /* Switch to previous VT. */
763 c = (vw->vw_number - 1) % VT_MAXWINDOWS;
764 vw = vd->vd_windows[c];
766 vt_proc_window_switch(vw);
769 vt_save_kbd_state(vw, kbd);
771 if (vw->vw_kbdstate & SLKED) {
772 /* Turn scrolling on. */
773 vw->vw_flags |= VWF_SCROLL;
774 VTBUF_SLCK_ENABLE(&vw->vw_buf);
776 /* Turn scrolling off. */
777 vw->vw_flags &= ~VWF_SCROLL;
778 VTBUF_SLCK_DISABLE(&vw->vw_buf);
779 vt_scroll(vw, 0, VHS_END);
784 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
785 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
786 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
787 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
788 /* F1 through F12 keys. */
789 terminal_input_special(vw->vw_terminal,
790 TKEY_F1 + c - (FKEY | F(1)));
792 case FKEY | F(49): /* Home key. */
793 terminal_input_special(vw->vw_terminal, TKEY_HOME);
795 case FKEY | F(50): /* Arrow up. */
796 terminal_input_special(vw->vw_terminal, TKEY_UP);
798 case FKEY | F(51): /* Page up. */
799 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
801 case FKEY | F(53): /* Arrow left. */
802 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
804 case FKEY | F(55): /* Arrow right. */
805 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
807 case FKEY | F(57): /* End key. */
808 terminal_input_special(vw->vw_terminal, TKEY_END);
810 case FKEY | F(58): /* Arrow down. */
811 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
813 case FKEY | F(59): /* Page down. */
814 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
816 case FKEY | F(60): /* Insert key. */
817 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
819 case FKEY | F(61): /* Delete key. */
820 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
823 } else if (KEYFLAGS(c) == 0) {
824 /* Don't do UTF-8 conversion when doing raw mode. */
825 if (vw->vw_kbdmode == K_XLATE) {
826 #if VT_ALT_TO_ESC_HACK
827 if (vd->vd_kbstate & ALKED) {
829 * Prepend ESC sequence if one of ALT keys down.
831 terminal_input_char(vw->vw_terminal, 0x1b);
835 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
837 terminal_input_raw(vw->vw_terminal, c);
843 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
845 struct vt_device *vd = arg;
851 case KBDIO_UNLOADING:
853 vd->vd_keyboard = -1;
854 kbd_release(kbd, (void *)vd);
861 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
862 vt_processkey(kbd, vd, c);
868 vt_allocate_keyboard(struct vt_device *vd)
874 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
876 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
877 k0 = kbd_get_keyboard(idx0);
879 for (idx = kbd_find_keyboard2("*", -1, 0);
881 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
882 k = kbd_get_keyboard(idx);
884 if (idx == idx0 || KBD_IS_BUSY(k))
887 bzero(&ki, sizeof(ki));
888 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
889 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
890 ki.kb_unit = k->kb_unit;
892 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
895 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
896 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
898 DPRINTF(10, "%s: No keyboard found.\n", __func__);
902 vd->vd_keyboard = idx0;
903 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
909 vtterm_bell(struct terminal *tm)
911 struct vt_window *vw = tm->tm_softc;
912 struct vt_device *vd = vw->vw_device;
914 if (vd->vd_flags & VDF_QUIET_BELL)
917 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
921 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_set_cursor(tm, &vw->vw_buf.vb_cursor);
1536 terminal_mute(tm, 0);
1538 /* Actually apply the font to the current window. */
1540 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1542 * In case vt_change_font called to update size we don't need
1543 * to update font link.
1545 vtfont_unref(vw->vw_font);
1546 vw->vw_font = vtfont_ref(vf);
1550 * Compute the drawable area and move the mouse cursor inside
1551 * it, in case the new area is smaller than the previous one.
1553 vt_compute_drawable_area(vw);
1554 vd->vd_mx = min(vd->vd_mx,
1555 vw->vw_draw_area.tr_end.tp_col -
1556 vw->vw_draw_area.tr_begin.tp_col - 1);
1557 vd->vd_my = min(vd->vd_my,
1558 vw->vw_draw_area.tr_end.tp_row -
1559 vw->vw_draw_area.tr_begin.tp_row - 1);
1561 /* Force a full redraw the next timer tick. */
1562 if (vd->vd_curwindow == vw) {
1563 vt_set_border(vw, TC_BLACK);
1564 vd->vd_flags |= VDF_INVALID;
1565 vt_resume_flush_timer(vw->vw_device, 0);
1567 vw->vw_flags &= ~VWF_BUSY;
1573 vt_proc_alive(struct vt_window *vw)
1577 if (vw->vw_smode.mode != VT_PROCESS)
1581 if ((p = pfind(vw->vw_pid)) != NULL)
1583 if (vw->vw_proc == p)
1586 vw->vw_smode.mode = VT_AUTO;
1587 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1594 signal_vt_rel(struct vt_window *vw)
1597 if (vw->vw_smode.mode != VT_PROCESS)
1599 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1604 vw->vw_flags |= VWF_SWWAIT_REL;
1605 PROC_LOCK(vw->vw_proc);
1606 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1607 PROC_UNLOCK(vw->vw_proc);
1608 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1613 signal_vt_acq(struct vt_window *vw)
1616 if (vw->vw_smode.mode != VT_PROCESS)
1618 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1619 cnavailable(vw->vw_terminal->consdev, FALSE);
1620 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1625 vw->vw_flags |= VWF_SWWAIT_ACQ;
1626 PROC_LOCK(vw->vw_proc);
1627 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1628 PROC_UNLOCK(vw->vw_proc);
1629 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1634 finish_vt_rel(struct vt_window *vw, int release, int *s)
1637 if (vw->vw_flags & VWF_SWWAIT_REL) {
1638 vw->vw_flags &= ~VWF_SWWAIT_REL;
1640 callout_drain(&vw->vw_proc_dead_timer);
1641 vt_late_window_switch(vw->vw_switch_to);
1649 finish_vt_acq(struct vt_window *vw)
1652 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1653 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1659 #ifndef SC_NO_CUTPASTE
1661 vt_mouse_terminput_button(struct vt_device *vd, int button)
1663 struct vt_window *vw;
1665 char mouseb[6] = "\x1B[M";
1668 vw = vd->vd_curwindow;
1671 /* Translate to char position. */
1672 x = vd->vd_mx / vf->vf_width;
1673 y = vd->vd_my / vf->vf_height;
1674 /* Avoid overflow. */
1675 x = MIN(x, 255 - '!');
1676 y = MIN(y, 255 - '!');
1678 mouseb[3] = ' ' + button;
1679 mouseb[4] = '!' + x;
1680 mouseb[5] = '!' + y;
1682 for (i = 0; i < sizeof(mouseb); i++)
1683 terminal_input_char(vw->vw_terminal, mouseb[i]);
1687 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1692 case MOUSE_BUTTON_EVENT:
1694 /* Mouse button pressed. */
1695 if (event & MOUSE_BUTTON1DOWN)
1696 vt_mouse_terminput_button(vd, 0);
1697 if (event & MOUSE_BUTTON2DOWN)
1698 vt_mouse_terminput_button(vd, 1);
1699 if (event & MOUSE_BUTTON3DOWN)
1700 vt_mouse_terminput_button(vd, 2);
1702 /* Mouse button released. */
1703 vt_mouse_terminput_button(vd, 3);
1707 case MOUSE_MOTION_EVENT:
1708 if (mouse->u.data.z < 0) {
1710 sc_mouse_input_button(vd, 64);
1711 } else if (mouse->u.data.z > 0) {
1713 sc_mouse_input_button(vd, 65);
1726 len = VD_PASTEBUFLEN(main_vd);
1727 buf = VD_PASTEBUF(main_vd);
1728 len /= sizeof(term_char_t);
1729 for (i = 0; i < len; i++) {
1732 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1738 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1740 struct vt_device *vd;
1741 struct vt_window *vw;
1747 vw = vd->vd_curwindow;
1751 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1753 * Either the mouse is disabled, or the window is in
1754 * "graphics mode". The graphics mode is usually set by
1755 * an X server, using the KDSETMODE ioctl.
1759 if (vf == NULL) /* Text mode. */
1763 * TODO: add flag about pointer position changed, to not redraw chars
1764 * under mouse pointer when nothing changed.
1767 if (vw->vw_mouse_level > 0)
1768 vt_mouse_terminput(vd, type, x, y, event, cnt);
1772 case MOUSE_MOTION_EVENT:
1777 vt_termsize(vd, vf, &size);
1782 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1783 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1787 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1788 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1789 vd->vd_mx / vf->vf_width,
1790 vd->vd_my / vf->vf_height);
1792 vt_resume_flush_timer(vw->vw_device, 0);
1794 case MOUSE_BUTTON_EVENT:
1802 case MOUSE_BUTTON1DOWN:
1805 mark = VTB_MARK_END;
1807 case 1: /* single click: start cut operation */
1808 mark = VTB_MARK_START;
1810 case 2: /* double click: cut a word */
1811 mark = VTB_MARK_WORD;
1813 case 3: /* triple click: cut a line */
1814 mark = VTB_MARK_ROW;
1818 case VT_MOUSE_PASTEBUTTON:
1827 case VT_MOUSE_EXTENDBUTTON:
1830 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1831 mark = VTB_MARK_EXTEND;
1836 mark = VTB_MARK_EXTEND;
1844 /* Save buttons state. */
1846 vd->vd_mstate |= event;
1848 vd->vd_mstate &= ~event;
1850 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1851 vd->vd_my / vf->vf_height) == 1) {
1853 * We have something marked to copy, so update pointer to
1854 * window with selection.
1856 vt_resume_flush_timer(vw->vw_device, 0);
1862 case VTB_MARK_EXTEND:
1865 /* Other types of mark do not require to copy data. */
1869 /* Get current selection size in bytes. */
1870 len = vtbuf_get_marked_len(&vw->vw_buf);
1874 /* Reallocate buffer only if old one is too small. */
1875 if (len > VD_PASTEBUFSZ(vd)) {
1876 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1878 /* Update buffer size. */
1879 VD_PASTEBUFSZ(vd) = len;
1881 /* Request copy/paste buffer data, no more than `len' */
1882 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1885 VD_PASTEBUFLEN(vd) = len;
1887 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1892 vt_mouse_state(int show)
1894 struct vt_device *vd;
1895 struct vt_window *vw;
1898 vw = vd->vd_curwindow;
1902 vw->vw_flags |= VWF_MOUSE_HIDE;
1905 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1909 /* Mark mouse position as dirty. */
1910 vt_mark_mouse_position_as_dirty(vd);
1911 vt_resume_flush_timer(vw->vw_device, 0);
1916 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1917 int nprot, vm_memattr_t *memattr)
1919 struct vt_window *vw = tm->tm_softc;
1920 struct vt_device *vd = vw->vw_device;
1922 if (vd->vd_driver->vd_fb_mmap)
1923 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1930 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1933 struct vt_window *vw = tm->tm_softc;
1934 struct vt_device *vd = vw->vw_device;
1937 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1938 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1949 cmd = VT_WAITACTIVE;
1975 ival = IOCPARM_IVAL(data);
1976 data = (caddr_t)&ival;
1981 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1982 if (*(int *)data & ~0x7f)
1987 case GIO_DEADKEYMAP:
1988 case PIO_DEADKEYMAP:
1993 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1994 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1995 case KBADDKBD: /* add/remove keyboard to/from mux */
2000 kbd = kbd_get_keyboard(vd->vd_keyboard);
2002 error = kbdd_ioctl(kbd, cmd, data);
2004 if (error == ENOIOCTL) {
2005 if (cmd == KDGKBTYPE) {
2006 /* always return something? XXX */
2014 case KDGKBSTATE: { /* get keyboard state (locks) */
2017 if (vw == vd->vd_curwindow) {
2019 kbd = kbd_get_keyboard(vd->vd_keyboard);
2021 error = vt_save_kbd_state(vw, kbd);
2028 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2032 case KDSKBSTATE: { /* set keyboard state (locks) */
2035 state = *(int *)data;
2036 if (state & ~LOCK_MASK)
2039 vw->vw_kbdstate &= ~LOCK_MASK;
2040 vw->vw_kbdstate |= state;
2043 if (vw == vd->vd_curwindow) {
2045 kbd = kbd_get_keyboard(vd->vd_keyboard);
2047 error = vt_update_kbd_state(vw, kbd);
2053 case KDGETLED: { /* get keyboard LED status */
2056 if (vw == vd->vd_curwindow) {
2058 kbd = kbd_get_keyboard(vd->vd_keyboard);
2060 error = vt_save_kbd_leds(vw, kbd);
2067 *(int *)data = vw->vw_kbdstate & LED_MASK;
2071 case KDSETLED: { /* set keyboard LED status */
2074 leds = *(int *)data;
2075 if (leds & ~LED_MASK)
2078 vw->vw_kbdstate &= ~LED_MASK;
2079 vw->vw_kbdstate |= leds;
2082 if (vw == vd->vd_curwindow) {
2084 kbd = kbd_get_keyboard(vd->vd_keyboard);
2086 error = vt_update_kbd_leds(vw, kbd);
2095 if (vw == vd->vd_curwindow) {
2097 kbd = kbd_get_keyboard(vd->vd_keyboard);
2099 error = vt_save_kbd_mode(vw, kbd);
2106 *(int *)data = vw->vw_kbdmode;
2113 mode = *(int *)data;
2118 vw->vw_kbdmode = mode;
2121 if (vw == vd->vd_curwindow) {
2123 kbd = kbd_get_keyboard(vd->vd_keyboard);
2125 error = vt_update_kbd_mode(vw, kbd);
2135 case FBIO_GETWINORG: /* get frame buffer window origin */
2136 case FBIO_GETDISPSTART: /* get display start address */
2137 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2138 case FBIO_BLANK: /* blank display */
2139 if (vd->vd_driver->vd_fb_ioctl)
2140 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2142 case CONS_BLANKTIME:
2147 *(int *)data = M_CG640x480;
2149 case CONS_BELLTYPE: /* set bell type sound */
2150 if ((*(int *)data) & CONS_QUIET_BELL)
2151 vd->vd_flags |= VDF_QUIET_BELL;
2153 vd->vd_flags &= ~VDF_QUIET_BELL;
2155 case CONS_GETINFO: {
2156 vid_info_t *vi = (vid_info_t *)data;
2157 if (vi->size != sizeof(struct vid_info))
2160 if (vw == vd->vd_curwindow) {
2161 kbd = kbd_get_keyboard(vd->vd_keyboard);
2163 vt_save_kbd_state(vw, kbd);
2166 vi->m_num = vd->vd_curwindow->vw_number + 1;
2167 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2168 /* XXX: other fields! */
2172 *(int *)data = 0x200;
2177 case CONS_MOUSECTL: {
2178 mouse_info_t *mouse = (mouse_info_t*)data;
2181 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2182 * should not be applied to individual TTYs, but only to
2185 switch (mouse->operation) {
2187 if (vd->vd_flags & VDF_MOUSECURSOR) {
2188 vd->vd_flags &= ~VDF_MOUSECURSOR;
2189 #ifndef SC_NO_CUTPASTE
2190 vt_mouse_state(VT_MOUSE_HIDE);
2195 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2196 vd->vd_flags |= VDF_MOUSECURSOR;
2197 vd->vd_mx = vd->vd_width / 2;
2198 vd->vd_my = vd->vd_height / 2;
2199 #ifndef SC_NO_CUTPASTE
2200 vt_mouse_state(VT_MOUSE_SHOW);
2211 if (vd->vd_flags & VDF_TEXTMODE)
2214 error = vtfont_load((void *)data, &vf);
2218 error = vt_change_font(vw, vf);
2222 case PIO_VFONT_DEFAULT: {
2223 /* Reset to default font. */
2224 error = vt_change_font(vw, &vt_font_default);
2228 scrmap_t *sm = (scrmap_t *)data;
2230 /* We don't have screen maps, so return a handcrafted one. */
2231 for (i = 0; i < 256; i++)
2237 * FIXME: This implementation is incomplete compared to
2240 switch (*(int *)data) {
2244 vw->vw_flags &= ~VWF_GRAPHICS;
2247 vw->vw_flags |= VWF_GRAPHICS;
2251 case KDENABIO: /* allow io operations */
2252 error = priv_check(td, PRIV_IO);
2255 error = securelevel_gt(td->td_ucred, 0);
2258 #if defined(__i386__)
2259 td->td_frame->tf_eflags |= PSL_IOPL;
2260 #elif defined(__amd64__)
2261 td->td_frame->tf_rflags |= PSL_IOPL;
2264 case KDDISABIO: /* disallow io operations (default) */
2265 #if defined(__i386__)
2266 td->td_frame->tf_eflags &= ~PSL_IOPL;
2267 #elif defined(__amd64__)
2268 td->td_frame->tf_rflags &= ~PSL_IOPL;
2271 case KDMKTONE: /* sound the bell */
2272 vtterm_beep(tm, *(u_int *)data);
2274 case KIOCSOUND: /* make tone (*data) hz */
2277 case CONS_SETKBD: /* set the new keyboard */
2280 if (vd->vd_keyboard != *(int *)data) {
2281 kbd = kbd_get_keyboard(*(int *)data);
2286 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2287 (void *)vd, vt_kbdevent, vd);
2289 if (vd->vd_keyboard != -1) {
2290 vt_save_kbd_state(vd->vd_curwindow, kbd);
2291 kbd_release(kbd, (void *)vd);
2293 kbd = kbd_get_keyboard(i);
2294 vd->vd_keyboard = i;
2296 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2297 vt_update_kbd_state(vd->vd_curwindow, kbd);
2299 error = EPERM; /* XXX */
2304 case CONS_RELKBD: /* release the current keyboard */
2307 if (vd->vd_keyboard != -1) {
2308 kbd = kbd_get_keyboard(vd->vd_keyboard);
2313 vt_save_kbd_state(vd->vd_curwindow, kbd);
2314 error = kbd_release(kbd, (void *)vd);
2316 vd->vd_keyboard = -1;
2323 win = *(int *)data - 1;
2324 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2326 if ((win >= VT_MAXWINDOWS) || (win < 0))
2328 return (vt_proc_window_switch(vd->vd_windows[win]));
2331 *(int *)data = vd->vd_curwindow->vw_number + 1;
2334 *(int *)data = vw->vw_number + 1;
2337 /* TODO: Check current state, switching can be in progress. */
2338 if ((*(int *)data) == 0x01)
2339 vw->vw_flags |= VWF_VTYLOCK;
2340 else if ((*(int *)data) == 0x02)
2341 vw->vw_flags &= ~VWF_VTYLOCK;
2347 for (i = 0; i < VT_MAXWINDOWS; i++) {
2348 vw = vd->vd_windows[i];
2351 if (!(vw->vw_flags & VWF_OPENED)) {
2352 *(int *)data = vw->vw_number + 1;
2362 i = *(unsigned int *)data;
2363 if (i > VT_MAXWINDOWS)
2366 vw = vd->vd_windows[i - 1];
2369 while (vd->vd_curwindow != vw && error == 0)
2370 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2373 case VT_SETMODE: { /* set screen switcher mode */
2374 struct vt_mode *mode;
2377 mode = (struct vt_mode *)data;
2378 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2379 if (vw->vw_smode.mode == VT_PROCESS) {
2380 p1 = pfind(vw->vw_pid);
2381 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2384 DPRINTF(5, "error EPERM\n");
2390 if (mode->mode == VT_AUTO) {
2391 vw->vw_smode.mode = VT_AUTO;
2394 DPRINTF(5, "VT_AUTO, ");
2395 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2396 cnavailable(vw->vw_terminal->consdev, TRUE);
2397 /* were we in the middle of the vty switching process? */
2398 if (finish_vt_rel(vw, TRUE, &s) == 0)
2399 DPRINTF(5, "reset WAIT_REL, ");
2400 if (finish_vt_acq(vw) == 0)
2401 DPRINTF(5, "reset WAIT_ACQ, ");
2403 } else if (mode->mode == VT_PROCESS) {
2404 if (!ISSIGVALID(mode->relsig) ||
2405 !ISSIGVALID(mode->acqsig) ||
2406 !ISSIGVALID(mode->frsig)) {
2407 DPRINTF(5, "error EINVAL\n");
2410 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2411 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2412 vw->vw_proc = td->td_proc;
2413 vw->vw_pid = vw->vw_proc->p_pid;
2414 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2415 cnavailable(vw->vw_terminal->consdev, FALSE);
2417 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2424 case VT_GETMODE: /* get screen switcher mode */
2425 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2428 case VT_RELDISP: /* screen switcher ioctl */
2430 * This must be the current vty which is in the VT_PROCESS
2433 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2437 /* ...and this process is controlling it. */
2438 if (vw->vw_proc != td->td_proc) {
2442 switch(*(int *)data) {
2443 case VT_FALSE: /* user refuses to release screen, abort */
2444 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2445 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2446 SC_DRIVER_NAME, VT_UNIT(vw));
2448 case VT_TRUE: /* user has released screen, go on */
2449 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2450 if (vw->vw_flags & VWF_SWWAIT_REL) {
2451 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2452 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2453 SC_DRIVER_NAME, VT_UNIT(vw));
2458 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2459 if ((error = finish_vt_acq(vw)) == 0)
2460 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2461 SC_DRIVER_NAME, VT_UNIT(vw));
2472 static struct vt_window *
2473 vt_allocate_window(struct vt_device *vd, unsigned int window)
2475 struct vt_window *vw;
2476 struct terminal *tm;
2480 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2482 vw->vw_number = window;
2483 vw->vw_kbdmode = K_XLATE;
2485 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2486 vw->vw_font = vtfont_ref(&vt_font_default);
2487 vt_compute_drawable_area(vw);
2490 vt_termsize(vd, vw->vw_font, &size);
2491 vt_winsize(vd, vw->vw_font, &wsz);
2492 vtbuf_init(&vw->vw_buf, &size);
2494 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2495 terminal_set_winsize(tm, &wsz);
2496 vd->vd_windows[window] = vw;
2497 callout_init(&vw->vw_proc_dead_timer, 0);
2503 vt_upgrade(struct vt_device *vd)
2505 struct vt_window *vw;
2508 if (!vty_enabled(VTY_VT))
2510 if (main_vd->vd_driver == NULL)
2513 for (i = 0; i < VT_MAXWINDOWS; i++) {
2514 vw = vd->vd_windows[i];
2517 vw = vt_allocate_window(vd, i);
2519 if (!(vw->vw_flags & VWF_READY)) {
2520 callout_init(&vw->vw_proc_dead_timer, 0);
2521 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2522 vw->vw_flags |= VWF_READY;
2523 if (vw->vw_flags & VWF_CONSOLE) {
2524 /* For existing console window. */
2525 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2526 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2532 if (vd->vd_curwindow == NULL)
2533 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2535 if (!(vd->vd_flags & VDF_ASYNC)) {
2536 /* Attach keyboard. */
2537 vt_allocate_keyboard(vd);
2539 /* Init 25 Hz timer. */
2540 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2542 /* Start timer when everything ready. */
2543 vd->vd_flags |= VDF_ASYNC;
2544 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2545 vd->vd_timer_armed = 1;
2550 /* Refill settings with new sizes. */
2555 vt_resize(struct vt_device *vd)
2557 struct vt_window *vw;
2560 for (i = 0; i < VT_MAXWINDOWS; i++) {
2561 vw = vd->vd_windows[i];
2563 /* Assign default font to window, if not textmode. */
2564 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2565 vw->vw_font = vtfont_ref(&vt_font_default);
2568 /* Resize terminal windows */
2569 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2570 DPRINTF(100, "%s: vt_change_font() is busy, "
2571 "window %d\n", __func__, i);
2577 vt_allocate(struct vt_driver *drv, void *softc)
2579 struct vt_device *vd;
2581 if (!vty_enabled(VTY_VT))
2584 if (main_vd->vd_driver == NULL) {
2585 main_vd->vd_driver = drv;
2586 printf("VT: initialize with new VT driver \"%s\".\n",
2590 * Check if have rights to replace current driver. For example:
2591 * it is bad idea to replace KMS driver with generic VGA one.
2593 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2594 printf("VT: Driver priority %d too low. Current %d\n ",
2595 drv->vd_priority, main_vd->vd_driver->vd_priority);
2598 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2599 main_vd->vd_driver->vd_name, drv->vd_name);
2603 if (vd->vd_flags & VDF_ASYNC) {
2604 /* Stop vt_flush periodic task. */
2606 vt_suspend_flush_timer(vd);
2609 * Mute current terminal until we done. vt_change_font (called
2610 * from vt_resize) will unmute it.
2612 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2616 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2620 vd->vd_flags &= ~VDF_TEXTMODE;
2622 vd->vd_driver = drv;
2623 vd->vd_softc = softc;
2624 vd->vd_driver->vd_init(vd);
2627 /* Update windows sizes and initialize last items. */
2631 if (vd->vd_flags & VDF_SPLASH)
2635 if (vd->vd_flags & VDF_ASYNC) {
2636 /* Allow to put chars now. */
2637 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2638 /* Rerun timer for screen updates. */
2639 vt_resume_flush_timer(vd, 0);
2643 * Register as console. If it already registered, cnadd() will ignore
2646 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2653 if (vt_suspendswitch == 0)
2655 /* Save current window. */
2656 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2657 /* Ask holding process to free window and switch to console window */
2658 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2665 if (vt_suspendswitch == 0)
2667 /* Switch back to saved window */
2668 if (main_vd->vd_savedwindow != NULL)
2669 vt_proc_window_switch(main_vd->vd_savedwindow);
2670 main_vd->vd_savedwindow = NULL;