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 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1274 vtbuf_init_early(&vw->vw_buf);
1275 vt_winsize(vd, vw->vw_font, &wsz);
1276 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1278 attr.ta_format = TCHAR_FORMAT(c);
1279 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1280 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1281 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1283 if (vtdbest != NULL) {
1287 vd->vd_flags |= VDF_INITIALIZED;
1292 vtterm_cngetc(struct terminal *tm)
1294 struct vt_window *vw = tm->tm_softc;
1295 struct vt_device *vd = vw->vw_device;
1299 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1300 return (*vw->vw_kbdsq++);
1302 /* Make sure the splash screen is not there. */
1303 if (vd->vd_flags & VDF_SPLASH) {
1305 vd->vd_flags &= ~VDF_SPLASH;
1306 /* Mark screen as invalid to force update */
1307 vd->vd_flags |= VDF_INVALID;
1311 /* Stripped down keyboard handler. */
1312 kbd = kbd_get_keyboard(vd->vd_keyboard);
1316 /* Force keyboard input mode to K_XLATE */
1317 vw->vw_kbdmode = K_XLATE;
1318 vt_update_kbd_mode(vw, kbd);
1320 /* Switch the keyboard to polling to make it work here. */
1321 kbdd_poll(kbd, TRUE);
1322 c = kbdd_read_char(kbd, 0);
1323 kbdd_poll(kbd, FALSE);
1327 if (vw->vw_flags & VWF_SCROLL) {
1328 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1333 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1337 vt_save_kbd_state(vw, kbd);
1338 if (vw->vw_kbdstate & SLKED) {
1339 /* Turn scrolling on. */
1340 vw->vw_flags |= VWF_SCROLL;
1341 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1343 /* Turn scrolling off. */
1344 vt_scroll(vw, 0, VHS_END);
1345 vw->vw_flags &= ~VWF_SCROLL;
1346 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1349 /* XXX: KDB can handle history. */
1350 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1351 vw->vw_kbdsq = "\x1b[A";
1353 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1354 vw->vw_kbdsq = "\x1b[B";
1356 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1357 vw->vw_kbdsq = "\x1b[C";
1359 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1360 vw->vw_kbdsq = "\x1b[D";
1364 /* Force refresh to make scrollback work. */
1366 } else if (KEYFLAGS(c) == 0) {
1367 return (KEYCHAR(c));
1370 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1371 return (*vw->vw_kbdsq++);
1377 vtterm_cngrab(struct terminal *tm)
1379 struct vt_device *vd;
1380 struct vt_window *vw;
1387 vt_window_switch(vw);
1389 kbd = kbd_get_keyboard(vd->vd_keyboard);
1393 if (vw->vw_grabbed++ > 0)
1397 * Make sure the keyboard is accessible even when the kbd device
1398 * driver is disabled.
1402 /* We shall always use the keyboard in the XLATE mode here. */
1403 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1404 vw->vw_kbdmode = K_XLATE;
1405 vt_update_kbd_mode(vw, kbd);
1407 kbdd_poll(kbd, TRUE);
1411 vtterm_cnungrab(struct terminal *tm)
1413 struct vt_device *vd;
1414 struct vt_window *vw;
1420 kbd = kbd_get_keyboard(vd->vd_keyboard);
1424 if (--vw->vw_grabbed > 0)
1427 kbdd_poll(kbd, FALSE);
1429 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1430 vt_update_kbd_mode(vw, kbd);
1435 vtterm_opened(struct terminal *tm, int opened)
1437 struct vt_window *vw = tm->tm_softc;
1438 struct vt_device *vd = vw->vw_device;
1441 vd->vd_flags &= ~VDF_SPLASH;
1443 vw->vw_flags |= VWF_OPENED;
1445 vw->vw_flags &= ~VWF_OPENED;
1446 /* TODO: finish ACQ/REL */
1452 vt_set_border(struct vt_window *vw, term_color_t c)
1454 struct vt_device *vd = vw->vw_device;
1456 if (vd->vd_driver->vd_drawrect == NULL)
1460 if (vw->vw_draw_area.tr_begin.tp_row > 0)
1461 vd->vd_driver->vd_drawrect(vd,
1463 vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1467 if (vw->vw_draw_area.tr_begin.tp_col > 0)
1468 vd->vd_driver->vd_drawrect(vd,
1470 vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1474 if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1475 vd->vd_driver->vd_drawrect(vd,
1476 vw->vw_draw_area.tr_end.tp_col - 1, 0,
1477 vd->vd_width - 1, vd->vd_height - 1,
1481 if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1482 vd->vd_driver->vd_drawrect(vd,
1483 0, vw->vw_draw_area.tr_end.tp_row - 1,
1484 vd->vd_width - 1, vd->vd_height - 1,
1491 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1493 struct vt_device *vd = vw->vw_device;
1494 struct terminal *tm = vw->vw_terminal;
1501 * Changing fonts is a little tricky. We must prevent
1502 * simultaneous access to the device, so we must stop
1503 * the display timer and the terminal from accessing.
1504 * We need to switch fonts and grow our screen buffer.
1506 * XXX: Right now the code uses terminal_mute() to
1507 * prevent data from reaching the console driver while
1508 * resizing the screen buffer. This isn't elegant...
1512 if (vw->vw_flags & VWF_BUSY) {
1513 /* Another process is changing the font. */
1517 vw->vw_flags |= VWF_BUSY;
1520 vt_termsize(vd, vf, &size);
1521 vt_winsize(vd, vf, &wsz);
1523 /* Grow the screen buffer and terminal. */
1524 terminal_mute(tm, 1);
1525 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1526 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1527 terminal_mute(tm, 0);
1529 /* Actually apply the font to the current window. */
1531 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1533 * In case vt_change_font called to update size we don't need
1534 * to update font link.
1536 vtfont_unref(vw->vw_font);
1537 vw->vw_font = vtfont_ref(vf);
1541 * Compute the drawable area and move the mouse cursor inside
1542 * it, in case the new area is smaller than the previous one.
1544 vt_compute_drawable_area(vw);
1545 vd->vd_mx = min(vd->vd_mx,
1546 vw->vw_draw_area.tr_end.tp_col -
1547 vw->vw_draw_area.tr_begin.tp_col - 1);
1548 vd->vd_my = min(vd->vd_my,
1549 vw->vw_draw_area.tr_end.tp_row -
1550 vw->vw_draw_area.tr_begin.tp_row - 1);
1552 /* Force a full redraw the next timer tick. */
1553 if (vd->vd_curwindow == vw) {
1554 vt_set_border(vw, TC_BLACK);
1555 vd->vd_flags |= VDF_INVALID;
1556 vt_resume_flush_timer(vw->vw_device, 0);
1558 vw->vw_flags &= ~VWF_BUSY;
1564 vt_proc_alive(struct vt_window *vw)
1568 if (vw->vw_smode.mode != VT_PROCESS)
1572 if ((p = pfind(vw->vw_pid)) != NULL)
1574 if (vw->vw_proc == p)
1577 vw->vw_smode.mode = VT_AUTO;
1578 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1585 signal_vt_rel(struct vt_window *vw)
1588 if (vw->vw_smode.mode != VT_PROCESS)
1590 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1595 vw->vw_flags |= VWF_SWWAIT_REL;
1596 PROC_LOCK(vw->vw_proc);
1597 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1598 PROC_UNLOCK(vw->vw_proc);
1599 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1604 signal_vt_acq(struct vt_window *vw)
1607 if (vw->vw_smode.mode != VT_PROCESS)
1609 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1610 cnavailable(vw->vw_terminal->consdev, FALSE);
1611 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1616 vw->vw_flags |= VWF_SWWAIT_ACQ;
1617 PROC_LOCK(vw->vw_proc);
1618 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1619 PROC_UNLOCK(vw->vw_proc);
1620 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1625 finish_vt_rel(struct vt_window *vw, int release, int *s)
1628 if (vw->vw_flags & VWF_SWWAIT_REL) {
1629 vw->vw_flags &= ~VWF_SWWAIT_REL;
1631 callout_drain(&vw->vw_proc_dead_timer);
1632 vt_late_window_switch(vw->vw_switch_to);
1640 finish_vt_acq(struct vt_window *vw)
1643 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1644 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1650 #ifndef SC_NO_CUTPASTE
1652 vt_mouse_terminput_button(struct vt_device *vd, int button)
1654 struct vt_window *vw;
1656 char mouseb[6] = "\x1B[M";
1659 vw = vd->vd_curwindow;
1662 /* Translate to char position. */
1663 x = vd->vd_mx / vf->vf_width;
1664 y = vd->vd_my / vf->vf_height;
1665 /* Avoid overflow. */
1666 x = MIN(x, 255 - '!');
1667 y = MIN(y, 255 - '!');
1669 mouseb[3] = ' ' + button;
1670 mouseb[4] = '!' + x;
1671 mouseb[5] = '!' + y;
1673 for (i = 0; i < sizeof(mouseb); i++)
1674 terminal_input_char(vw->vw_terminal, mouseb[i]);
1678 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1683 case MOUSE_BUTTON_EVENT:
1685 /* Mouse button pressed. */
1686 if (event & MOUSE_BUTTON1DOWN)
1687 vt_mouse_terminput_button(vd, 0);
1688 if (event & MOUSE_BUTTON2DOWN)
1689 vt_mouse_terminput_button(vd, 1);
1690 if (event & MOUSE_BUTTON3DOWN)
1691 vt_mouse_terminput_button(vd, 2);
1693 /* Mouse button released. */
1694 vt_mouse_terminput_button(vd, 3);
1698 case MOUSE_MOTION_EVENT:
1699 if (mouse->u.data.z < 0) {
1701 sc_mouse_input_button(vd, 64);
1702 } else if (mouse->u.data.z > 0) {
1704 sc_mouse_input_button(vd, 65);
1717 len = VD_PASTEBUFLEN(main_vd);
1718 buf = VD_PASTEBUF(main_vd);
1719 len /= sizeof(term_char_t);
1720 for (i = 0; i < len; i++) {
1723 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1729 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1731 struct vt_device *vd;
1732 struct vt_window *vw;
1738 vw = vd->vd_curwindow;
1742 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1744 * Either the mouse is disabled, or the window is in
1745 * "graphics mode". The graphics mode is usually set by
1746 * an X server, using the KDSETMODE ioctl.
1750 if (vf == NULL) /* Text mode. */
1754 * TODO: add flag about pointer position changed, to not redraw chars
1755 * under mouse pointer when nothing changed.
1758 if (vw->vw_mouse_level > 0)
1759 vt_mouse_terminput(vd, type, x, y, event, cnt);
1763 case MOUSE_MOTION_EVENT:
1768 vt_termsize(vd, vf, &size);
1773 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1774 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1778 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1779 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1780 vd->vd_mx / vf->vf_width,
1781 vd->vd_my / vf->vf_height);
1783 vt_resume_flush_timer(vw->vw_device, 0);
1785 case MOUSE_BUTTON_EVENT:
1793 case MOUSE_BUTTON1DOWN:
1796 mark = VTB_MARK_END;
1798 case 1: /* single click: start cut operation */
1799 mark = VTB_MARK_START;
1801 case 2: /* double click: cut a word */
1802 mark = VTB_MARK_WORD;
1804 case 3: /* triple click: cut a line */
1805 mark = VTB_MARK_ROW;
1809 case VT_MOUSE_PASTEBUTTON:
1818 case VT_MOUSE_EXTENDBUTTON:
1821 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1822 mark = VTB_MARK_EXTEND;
1827 mark = VTB_MARK_EXTEND;
1835 /* Save buttons state. */
1837 vd->vd_mstate |= event;
1839 vd->vd_mstate &= ~event;
1841 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1842 vd->vd_my / vf->vf_height) == 1) {
1844 * We have something marked to copy, so update pointer to
1845 * window with selection.
1847 vt_resume_flush_timer(vw->vw_device, 0);
1853 case VTB_MARK_EXTEND:
1856 /* Other types of mark do not require to copy data. */
1860 /* Get current selection size in bytes. */
1861 len = vtbuf_get_marked_len(&vw->vw_buf);
1865 /* Reallocate buffer only if old one is too small. */
1866 if (len > VD_PASTEBUFSZ(vd)) {
1867 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1869 /* Update buffer size. */
1870 VD_PASTEBUFSZ(vd) = len;
1872 /* Request copy/paste buffer data, no more than `len' */
1873 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1876 VD_PASTEBUFLEN(vd) = len;
1878 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1883 vt_mouse_state(int show)
1885 struct vt_device *vd;
1886 struct vt_window *vw;
1889 vw = vd->vd_curwindow;
1893 vw->vw_flags |= VWF_MOUSE_HIDE;
1896 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1900 /* Mark mouse position as dirty. */
1901 vt_mark_mouse_position_as_dirty(vd);
1902 vt_resume_flush_timer(vw->vw_device, 0);
1907 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1908 int nprot, vm_memattr_t *memattr)
1910 struct vt_window *vw = tm->tm_softc;
1911 struct vt_device *vd = vw->vw_device;
1913 if (vd->vd_driver->vd_fb_mmap)
1914 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1921 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1924 struct vt_window *vw = tm->tm_softc;
1925 struct vt_device *vd = vw->vw_device;
1928 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1929 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1940 cmd = VT_WAITACTIVE;
1966 ival = IOCPARM_IVAL(data);
1967 data = (caddr_t)&ival;
1972 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1973 if (*(int *)data & ~0x7f)
1978 case GIO_DEADKEYMAP:
1979 case PIO_DEADKEYMAP:
1984 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1985 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1986 case KBADDKBD: /* add/remove keyboard to/from mux */
1991 kbd = kbd_get_keyboard(vd->vd_keyboard);
1993 error = kbdd_ioctl(kbd, cmd, data);
1995 if (error == ENOIOCTL) {
1996 if (cmd == KDGKBTYPE) {
1997 /* always return something? XXX */
2005 case KDGKBSTATE: { /* get keyboard state (locks) */
2008 if (vw == vd->vd_curwindow) {
2010 kbd = kbd_get_keyboard(vd->vd_keyboard);
2012 error = vt_save_kbd_state(vw, kbd);
2019 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2023 case KDSKBSTATE: { /* set keyboard state (locks) */
2026 state = *(int *)data;
2027 if (state & ~LOCK_MASK)
2030 vw->vw_kbdstate &= ~LOCK_MASK;
2031 vw->vw_kbdstate |= state;
2034 if (vw == vd->vd_curwindow) {
2036 kbd = kbd_get_keyboard(vd->vd_keyboard);
2038 error = vt_update_kbd_state(vw, kbd);
2044 case KDGETLED: { /* get keyboard LED status */
2047 if (vw == vd->vd_curwindow) {
2049 kbd = kbd_get_keyboard(vd->vd_keyboard);
2051 error = vt_save_kbd_leds(vw, kbd);
2058 *(int *)data = vw->vw_kbdstate & LED_MASK;
2062 case KDSETLED: { /* set keyboard LED status */
2065 leds = *(int *)data;
2066 if (leds & ~LED_MASK)
2069 vw->vw_kbdstate &= ~LED_MASK;
2070 vw->vw_kbdstate |= leds;
2073 if (vw == vd->vd_curwindow) {
2075 kbd = kbd_get_keyboard(vd->vd_keyboard);
2077 error = vt_update_kbd_leds(vw, kbd);
2086 if (vw == vd->vd_curwindow) {
2088 kbd = kbd_get_keyboard(vd->vd_keyboard);
2090 error = vt_save_kbd_mode(vw, kbd);
2097 *(int *)data = vw->vw_kbdmode;
2104 mode = *(int *)data;
2109 vw->vw_kbdmode = mode;
2112 if (vw == vd->vd_curwindow) {
2114 kbd = kbd_get_keyboard(vd->vd_keyboard);
2116 error = vt_update_kbd_mode(vw, kbd);
2126 case FBIO_GETWINORG: /* get frame buffer window origin */
2127 case FBIO_GETDISPSTART: /* get display start address */
2128 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2129 case FBIO_BLANK: /* blank display */
2130 if (vd->vd_driver->vd_fb_ioctl)
2131 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2133 case CONS_BLANKTIME:
2138 *(int *)data = M_CG640x480;
2140 case CONS_BELLTYPE: /* set bell type sound */
2141 if ((*(int *)data) & CONS_QUIET_BELL)
2142 vd->vd_flags |= VDF_QUIET_BELL;
2144 vd->vd_flags &= ~VDF_QUIET_BELL;
2146 case CONS_GETINFO: {
2147 vid_info_t *vi = (vid_info_t *)data;
2148 if (vi->size != sizeof(struct vid_info))
2151 if (vw == vd->vd_curwindow) {
2152 kbd = kbd_get_keyboard(vd->vd_keyboard);
2154 vt_save_kbd_state(vw, kbd);
2157 vi->m_num = vd->vd_curwindow->vw_number + 1;
2158 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2159 /* XXX: other fields! */
2163 *(int *)data = 0x200;
2168 case CONS_MOUSECTL: {
2169 mouse_info_t *mouse = (mouse_info_t*)data;
2172 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2173 * should not be applied to individual TTYs, but only to
2176 switch (mouse->operation) {
2178 if (vd->vd_flags & VDF_MOUSECURSOR) {
2179 vd->vd_flags &= ~VDF_MOUSECURSOR;
2180 #ifndef SC_NO_CUTPASTE
2181 vt_mouse_state(VT_MOUSE_HIDE);
2186 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2187 vd->vd_flags |= VDF_MOUSECURSOR;
2188 vd->vd_mx = vd->vd_width / 2;
2189 vd->vd_my = vd->vd_height / 2;
2190 #ifndef SC_NO_CUTPASTE
2191 vt_mouse_state(VT_MOUSE_SHOW);
2202 error = vtfont_load((void *)data, &vf);
2206 error = vt_change_font(vw, vf);
2211 scrmap_t *sm = (scrmap_t *)data;
2213 /* We don't have screen maps, so return a handcrafted one. */
2214 for (i = 0; i < 256; i++)
2220 * FIXME: This implementation is incomplete compared to
2223 switch (*(int *)data) {
2227 vw->vw_flags &= ~VWF_GRAPHICS;
2230 vw->vw_flags |= VWF_GRAPHICS;
2234 case KDENABIO: /* allow io operations */
2235 error = priv_check(td, PRIV_IO);
2238 error = securelevel_gt(td->td_ucred, 0);
2241 #if defined(__i386__)
2242 td->td_frame->tf_eflags |= PSL_IOPL;
2243 #elif defined(__amd64__)
2244 td->td_frame->tf_rflags |= PSL_IOPL;
2247 case KDDISABIO: /* disallow io operations (default) */
2248 #if defined(__i386__)
2249 td->td_frame->tf_eflags &= ~PSL_IOPL;
2250 #elif defined(__amd64__)
2251 td->td_frame->tf_rflags &= ~PSL_IOPL;
2254 case KDMKTONE: /* sound the bell */
2255 vtterm_beep(tm, *(u_int *)data);
2257 case KIOCSOUND: /* make tone (*data) hz */
2260 case CONS_SETKBD: /* set the new keyboard */
2263 if (vd->vd_keyboard != *(int *)data) {
2264 kbd = kbd_get_keyboard(*(int *)data);
2269 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2270 (void *)vd, vt_kbdevent, vd);
2272 if (vd->vd_keyboard != -1) {
2273 vt_save_kbd_state(vd->vd_curwindow, kbd);
2274 kbd_release(kbd, (void *)vd);
2276 kbd = kbd_get_keyboard(i);
2277 vd->vd_keyboard = i;
2279 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2280 vt_update_kbd_state(vd->vd_curwindow, kbd);
2282 error = EPERM; /* XXX */
2287 case CONS_RELKBD: /* release the current keyboard */
2290 if (vd->vd_keyboard != -1) {
2291 kbd = kbd_get_keyboard(vd->vd_keyboard);
2296 vt_save_kbd_state(vd->vd_curwindow, kbd);
2297 error = kbd_release(kbd, (void *)vd);
2299 vd->vd_keyboard = -1;
2306 win = *(int *)data - 1;
2307 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2309 if ((win >= VT_MAXWINDOWS) || (win < 0))
2311 return (vt_proc_window_switch(vd->vd_windows[win]));
2314 *(int *)data = vd->vd_curwindow->vw_number + 1;
2317 *(int *)data = vw->vw_number + 1;
2320 /* TODO: Check current state, switching can be in progress. */
2321 if ((*(int *)data) == 0x01)
2322 vw->vw_flags |= VWF_VTYLOCK;
2323 else if ((*(int *)data) == 0x02)
2324 vw->vw_flags &= ~VWF_VTYLOCK;
2330 for (i = 0; i < VT_MAXWINDOWS; i++) {
2331 vw = vd->vd_windows[i];
2334 if (!(vw->vw_flags & VWF_OPENED)) {
2335 *(int *)data = vw->vw_number + 1;
2345 i = *(unsigned int *)data;
2346 if (i > VT_MAXWINDOWS)
2349 vw = vd->vd_windows[i - 1];
2352 while (vd->vd_curwindow != vw && error == 0)
2353 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2356 case VT_SETMODE: { /* set screen switcher mode */
2357 struct vt_mode *mode;
2360 mode = (struct vt_mode *)data;
2361 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2362 if (vw->vw_smode.mode == VT_PROCESS) {
2363 p1 = pfind(vw->vw_pid);
2364 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2367 DPRINTF(5, "error EPERM\n");
2373 if (mode->mode == VT_AUTO) {
2374 vw->vw_smode.mode = VT_AUTO;
2377 DPRINTF(5, "VT_AUTO, ");
2378 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2379 cnavailable(vw->vw_terminal->consdev, TRUE);
2380 /* were we in the middle of the vty switching process? */
2381 if (finish_vt_rel(vw, TRUE, &s) == 0)
2382 DPRINTF(5, "reset WAIT_REL, ");
2383 if (finish_vt_acq(vw) == 0)
2384 DPRINTF(5, "reset WAIT_ACQ, ");
2386 } else if (mode->mode == VT_PROCESS) {
2387 if (!ISSIGVALID(mode->relsig) ||
2388 !ISSIGVALID(mode->acqsig) ||
2389 !ISSIGVALID(mode->frsig)) {
2390 DPRINTF(5, "error EINVAL\n");
2393 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2394 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2395 vw->vw_proc = td->td_proc;
2396 vw->vw_pid = vw->vw_proc->p_pid;
2397 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2398 cnavailable(vw->vw_terminal->consdev, FALSE);
2400 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2407 case VT_GETMODE: /* get screen switcher mode */
2408 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2411 case VT_RELDISP: /* screen switcher ioctl */
2413 * This must be the current vty which is in the VT_PROCESS
2416 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2420 /* ...and this process is controlling it. */
2421 if (vw->vw_proc != td->td_proc) {
2425 switch(*(int *)data) {
2426 case VT_FALSE: /* user refuses to release screen, abort */
2427 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2428 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2429 SC_DRIVER_NAME, VT_UNIT(vw));
2431 case VT_TRUE: /* user has released screen, go on */
2432 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2433 if (vw->vw_flags & VWF_SWWAIT_REL) {
2434 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2435 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2436 SC_DRIVER_NAME, VT_UNIT(vw));
2441 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2442 if ((error = finish_vt_acq(vw)) == 0)
2443 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2444 SC_DRIVER_NAME, VT_UNIT(vw));
2455 static struct vt_window *
2456 vt_allocate_window(struct vt_device *vd, unsigned int window)
2458 struct vt_window *vw;
2459 struct terminal *tm;
2463 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2465 vw->vw_number = window;
2466 vw->vw_kbdmode = K_XLATE;
2468 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2469 vw->vw_font = vtfont_ref(&vt_font_default);
2470 vt_compute_drawable_area(vw);
2473 vt_termsize(vd, vw->vw_font, &size);
2474 vt_winsize(vd, vw->vw_font, &wsz);
2475 vtbuf_init(&vw->vw_buf, &size);
2477 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2478 terminal_set_winsize(tm, &wsz);
2479 vd->vd_windows[window] = vw;
2480 callout_init(&vw->vw_proc_dead_timer, 0);
2486 vt_upgrade(struct vt_device *vd)
2488 struct vt_window *vw;
2491 if (!vty_enabled(VTY_VT))
2494 for (i = 0; i < VT_MAXWINDOWS; i++) {
2495 vw = vd->vd_windows[i];
2498 vw = vt_allocate_window(vd, i);
2500 if (!(vw->vw_flags & VWF_READY)) {
2501 callout_init(&vw->vw_proc_dead_timer, 0);
2502 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2503 vw->vw_flags |= VWF_READY;
2504 if (vw->vw_flags & VWF_CONSOLE) {
2505 /* For existing console window. */
2506 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2507 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2513 if (vd->vd_curwindow == NULL)
2514 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2516 if (!(vd->vd_flags & VDF_ASYNC)) {
2517 /* Attach keyboard. */
2518 vt_allocate_keyboard(vd);
2520 /* Init 25 Hz timer. */
2521 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2523 /* Start timer when everything ready. */
2524 vd->vd_flags |= VDF_ASYNC;
2525 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2526 vd->vd_timer_armed = 1;
2531 /* Refill settings with new sizes. */
2536 vt_resize(struct vt_device *vd)
2538 struct vt_window *vw;
2541 for (i = 0; i < VT_MAXWINDOWS; i++) {
2542 vw = vd->vd_windows[i];
2544 /* Assign default font to window, if not textmode. */
2545 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2546 vw->vw_font = vtfont_ref(&vt_font_default);
2549 /* Resize terminal windows */
2550 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2551 DPRINTF(100, "%s: vt_change_font() is busy, "
2552 "window %d\n", __func__, i);
2558 vt_allocate(struct vt_driver *drv, void *softc)
2560 struct vt_device *vd;
2562 if (!vty_enabled(VTY_VT))
2565 if (main_vd->vd_driver == NULL) {
2566 main_vd->vd_driver = drv;
2567 printf("VT: initialize with new VT driver \"%s\".\n",
2571 * Check if have rights to replace current driver. For example:
2572 * it is bad idea to replace KMS driver with generic VGA one.
2574 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2575 printf("VT: Driver priority %d too low. Current %d\n ",
2576 drv->vd_priority, main_vd->vd_driver->vd_priority);
2579 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2580 main_vd->vd_driver->vd_name, drv->vd_name);
2584 if (vd->vd_flags & VDF_ASYNC) {
2585 /* Stop vt_flush periodic task. */
2586 vt_suspend_flush_timer(vd);
2588 * Mute current terminal until we done. vt_change_font (called
2589 * from vt_resize) will unmute it.
2591 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2595 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2599 vd->vd_flags &= ~VDF_TEXTMODE;
2601 vd->vd_driver = drv;
2602 vd->vd_softc = softc;
2603 vd->vd_driver->vd_init(vd);
2606 /* Update windows sizes and initialize last items. */
2610 if (vd->vd_flags & VDF_SPLASH)
2614 if (vd->vd_flags & VDF_ASYNC) {
2615 /* Allow to put chars now. */
2616 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2617 /* Rerun timer for screen updates. */
2618 vt_resume_flush_timer(vd, 0);
2622 * Register as console. If it already registered, cnadd() will ignore
2625 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2632 if (vt_suspendswitch == 0)
2634 /* Save current window. */
2635 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2636 /* Ask holding process to free window and switch to console window */
2637 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2644 if (vt_suspendswitch == 0)
2646 /* Switch back to saved window */
2647 if (main_vd->vd_savedwindow != NULL)
2648 vt_proc_window_switch(main_vd->vd_savedwindow);
2649 main_vd->vd_savedwindow = NULL;