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);
169 static void vt_suspend_handler(void *priv);
170 static void vt_resume_handler(void *priv);
172 SET_DECLARE(vt_drv_set, struct vt_driver);
174 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
175 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
177 static struct terminal vt_consterm;
178 static struct vt_window vt_conswindow;
179 static struct vt_device vt_consdev = {
182 .vd_flags = VDF_INVALID,
183 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
184 .vd_curwindow = &vt_conswindow,
187 #ifndef SC_NO_CUTPASTE
193 .vd_mcursor = &vt_default_mouse_pointer,
194 .vd_mcursor_fg = TC_WHITE,
195 .vd_mcursor_bg = TC_BLACK,
198 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
199 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
200 static struct vt_window vt_conswindow = {
201 .vw_number = VT_CONSWINDOW,
202 .vw_flags = VWF_CONSOLE,
204 .vb_buffer = &vt_constextbuf[0],
205 .vb_rows = &vt_constextbufrows[0],
206 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
209 .vb_flags = VBF_STATIC,
210 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
211 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
217 .vw_device = &vt_consdev,
218 .vw_terminal = &vt_consterm,
219 .vw_kbdmode = K_XLATE,
222 static struct terminal vt_consterm = {
223 .tm_class = &vt_termclass,
224 .tm_softc = &vt_conswindow,
227 static struct consdev vt_consterm_consdev = {
228 .cn_ops = &termcn_cnops,
229 .cn_arg = &vt_consterm,
233 /* Add to set of consoles. */
234 DATA_SET(cons_set, vt_consterm_consdev);
237 * Right after kmem is done to allow early drivers to use locking and allocate
240 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
242 /* Delay until all devices attached, to not waste time. */
243 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
246 /* Initialize locks/mem depended members. */
248 vt_update_static(void *dummy)
251 if (!vty_enabled(VTY_VT))
253 if (main_vd->vd_driver != NULL)
254 printf("VT: running with driver \"%s\".\n",
255 main_vd->vd_driver->vd_name);
257 printf("VT: init without driver.\n");
259 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
260 cv_init(&main_vd->vd_winswitch, "vtwswt");
264 vt_schedule_flush(struct vt_device *vd, int ms)
268 /* Default to initial value. */
269 ms = 1000 / VT_TIMERFREQ;
271 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
275 vt_resume_flush_timer(struct vt_device *vd, int ms)
278 if (!(vd->vd_flags & VDF_ASYNC) ||
279 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
282 vt_schedule_flush(vd, ms);
286 vt_suspend_flush_timer(struct vt_device *vd)
289 * As long as this function is called locked, callout_stop()
290 * has the same effect like callout_drain() with regard to
291 * preventing the callback function from executing.
293 VT_LOCK_ASSERT(vd, MA_OWNED);
295 if (!(vd->vd_flags & VDF_ASYNC) ||
296 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
299 callout_stop(&vd->vd_timer);
303 vt_switch_timer(void *arg)
306 vt_late_window_switch((struct vt_window *)arg);
310 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
315 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
321 vw->vw_kbdmode = mode;
327 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
331 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
339 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
344 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
350 vw->vw_kbdstate &= ~LOCK_MASK;
351 vw->vw_kbdstate |= state & LOCK_MASK;
357 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
361 state = vw->vw_kbdstate & LOCK_MASK;
362 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
370 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
375 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
381 vw->vw_kbdstate &= ~LED_MASK;
382 vw->vw_kbdstate |= leds & LED_MASK;
388 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
392 leds = vw->vw_kbdstate & LED_MASK;
393 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
401 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
404 DPRINTF(40, "%s\n", __func__);
405 curvw->vw_switch_to = vw;
406 /* Set timer to allow switch in case when process hang. */
407 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
408 vt_switch_timer, (void *)vw);
409 /* Notify process about vt switch attempt. */
410 DPRINTF(30, "%s: Notify process.\n", __func__);
411 signal_vt_rel(curvw);
417 vt_window_postswitch(struct vt_window *vw)
424 /* vt_late_window_switch will done VT switching for regular case. */
426 vt_late_window_switch(struct vt_window *vw)
430 callout_stop(&vw->vw_proc_dead_timer);
432 ret = vt_window_switch(vw);
436 /* Notify owner process about terminal availability. */
437 if (vw->vw_smode.mode == VT_PROCESS) {
438 ret = vt_window_postswitch(vw);
445 vt_proc_window_switch(struct vt_window *vw)
447 struct vt_window *curvw;
448 struct vt_device *vd;
452 curvw = vd->vd_curwindow;
454 if (curvw->vw_flags & VWF_VTYLOCK)
457 /* Ask current process permission to switch away. */
458 if (curvw->vw_smode.mode == VT_PROCESS) {
459 DPRINTF(30, "%s: VT_PROCESS ", __func__);
460 if (vt_proc_alive(curvw) == FALSE) {
461 DPRINTF(30, "Dead. Cleaning.");
464 DPRINTF(30, "%s: Signaling process.\n", __func__);
465 /* Alive, try to ask him. */
466 ret = vt_window_preswitch(vw, curvw);
467 /* Wait for process answer or timeout. */
473 ret = vt_late_window_switch(vw);
477 /* Switch window ignoring process locking. */
479 vt_window_switch(struct vt_window *vw)
481 struct vt_device *vd = vw->vw_device;
482 struct vt_window *curvw = vd->vd_curwindow;
491 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
496 vt_suspend_flush_timer(vd);
498 vd->vd_curwindow = vw;
499 vd->vd_flags |= VDF_INVALID;
500 cv_broadcast(&vd->vd_winswitch);
503 if (vd->vd_driver->vd_postswitch)
504 vd->vd_driver->vd_postswitch(vd);
506 vt_resume_flush_timer(vd, 0);
508 /* Restore per-window keyboard mode. */
510 kbd = kbd_get_keyboard(vd->vd_keyboard);
512 if (curvw->vw_kbdmode == K_XLATE)
513 vt_save_kbd_state(curvw, kbd);
515 vt_update_kbd_mode(vw, kbd);
516 vt_update_kbd_state(vw, kbd);
519 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
525 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
528 size->tp_row = vd->vd_height;
529 size->tp_col = vd->vd_width;
531 size->tp_row /= vf->vf_height;
532 size->tp_col /= vf->vf_width;
537 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
540 size->ws_row = size->ws_ypixel = vd->vd_height;
541 size->ws_col = size->ws_xpixel = vd->vd_width;
543 size->ws_row /= vf->vf_height;
544 size->ws_col /= vf->vf_width;
549 vt_compute_drawable_area(struct vt_window *vw)
551 struct vt_device *vd;
556 if (vw->vw_font == NULL) {
557 vw->vw_draw_area.tr_begin.tp_col = 0;
558 vw->vw_draw_area.tr_begin.tp_row = 0;
559 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
560 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
567 * Compute the drawable area, so that the text is centered on
571 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
572 vw->vw_draw_area.tr_begin.tp_row = (vd->vd_height % vf->vf_height) / 2;
573 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
574 vd->vd_width / vf->vf_width * vf->vf_width;
575 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
576 vd->vd_height / vf->vf_height * vf->vf_height;
580 vt_scroll(struct vt_window *vw, int offset, int whence)
585 if ((vw->vw_flags & VWF_SCROLL) == 0)
588 vt_termsize(vw->vw_device, vw->vw_font, &size);
590 diff = vthistory_seek(&vw->vw_buf, offset, whence);
592 vw->vw_device->vd_flags |= VDF_INVALID;
593 vt_resume_flush_timer(vw->vw_device, 0);
597 vt_machine_kbdevent(int c)
601 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
603 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
605 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
607 shutdown_nice(RB_HALT);
609 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
610 #ifndef SC_NO_CUTPASTE
611 /* Insert text from cut-paste buffer. */
615 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
617 shutdown_nice(RB_HALT|RB_POWEROFF);
619 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
621 * Request to immediate panic if sysctl
622 * kern.vt.enable_panic_key allow it.
625 panic("Forced by the panic key");
627 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
629 shutdown_nice(RB_AUTOBOOT);
631 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
632 /* Force activatation/deactivation of the screen saver. */
635 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
636 /* Put machine into Stand-By mode. */
637 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
639 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
640 /* Suspend machine. */
641 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
649 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
651 struct vt_device *vd;
655 /* Only special keys handled in ScrollLock mode */
656 if ((c & SPCLKEY) == 0)
662 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
663 vw = vd->vd_windows[c - F_SCR];
665 vt_proc_window_switch(vw);
673 /* Turn scrolling off. */
674 vt_scroll(vw, 0, VHS_END);
675 VTBUF_SLCK_DISABLE(&vw->vw_buf);
676 vw->vw_flags &= ~VWF_SCROLL;
679 case FKEY | F(49): /* Home key. */
680 vt_scroll(vw, 0, VHS_SET);
682 case FKEY | F(50): /* Arrow up. */
683 vt_scroll(vw, -1, VHS_CUR);
685 case FKEY | F(51): /* Page up. */
686 vt_termsize(vd, vw->vw_font, &size);
687 vt_scroll(vw, -size.tp_row, VHS_CUR);
689 case FKEY | F(57): /* End key. */
690 vt_scroll(vw, 0, VHS_END);
692 case FKEY | F(58): /* Arrow down. */
693 vt_scroll(vw, 1, VHS_CUR);
695 case FKEY | F(59): /* Page down. */
696 vt_termsize(vd, vw->vw_font, &size);
697 vt_scroll(vw, size.tp_row, VHS_CUR);
706 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
708 struct vt_window *vw = vd->vd_curwindow;
710 #if VT_ALT_TO_ESC_HACK
712 switch (c & ~RELKEY) {
713 case (SPCLKEY | RALT):
714 if (vt_enable_altgr != 0)
716 case (SPCLKEY | LALT):
717 vd->vd_kbstate &= ~ALKED;
719 /* Other keys ignored for RELKEY event. */
722 switch (c & ~RELKEY) {
723 case (SPCLKEY | RALT):
724 if (vt_enable_altgr != 0)
726 case (SPCLKEY | LALT):
727 vd->vd_kbstate |= ALKED;
732 /* Other keys ignored for RELKEY event. */
736 if (vt_machine_kbdevent(c))
739 if (vw->vw_flags & VWF_SCROLL) {
740 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
741 /* Scroll mode keys handled, nothing to do more. */
748 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
749 vw = vd->vd_windows[c - F_SCR];
751 vt_proc_window_switch(vw);
757 /* Switch to next VT. */
758 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
759 vw = vd->vd_windows[c];
761 vt_proc_window_switch(vw);
764 /* Switch to previous VT. */
765 c = (vw->vw_number - 1) % VT_MAXWINDOWS;
766 vw = vd->vd_windows[c];
768 vt_proc_window_switch(vw);
771 vt_save_kbd_state(vw, kbd);
773 if (vw->vw_kbdstate & SLKED) {
774 /* Turn scrolling on. */
775 vw->vw_flags |= VWF_SCROLL;
776 VTBUF_SLCK_ENABLE(&vw->vw_buf);
778 /* Turn scrolling off. */
779 vw->vw_flags &= ~VWF_SCROLL;
780 VTBUF_SLCK_DISABLE(&vw->vw_buf);
781 vt_scroll(vw, 0, VHS_END);
786 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
787 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
788 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
789 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
790 /* F1 through F12 keys. */
791 terminal_input_special(vw->vw_terminal,
792 TKEY_F1 + c - (FKEY | F(1)));
794 case FKEY | F(49): /* Home key. */
795 terminal_input_special(vw->vw_terminal, TKEY_HOME);
797 case FKEY | F(50): /* Arrow up. */
798 terminal_input_special(vw->vw_terminal, TKEY_UP);
800 case FKEY | F(51): /* Page up. */
801 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
803 case FKEY | F(53): /* Arrow left. */
804 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
806 case FKEY | F(55): /* Arrow right. */
807 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
809 case FKEY | F(57): /* End key. */
810 terminal_input_special(vw->vw_terminal, TKEY_END);
812 case FKEY | F(58): /* Arrow down. */
813 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
815 case FKEY | F(59): /* Page down. */
816 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
818 case FKEY | F(60): /* Insert key. */
819 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
821 case FKEY | F(61): /* Delete key. */
822 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
825 } else if (KEYFLAGS(c) == 0) {
826 /* Don't do UTF-8 conversion when doing raw mode. */
827 if (vw->vw_kbdmode == K_XLATE) {
828 #if VT_ALT_TO_ESC_HACK
829 if (vd->vd_kbstate & ALKED) {
831 * Prepend ESC sequence if one of ALT keys down.
833 terminal_input_char(vw->vw_terminal, 0x1b);
837 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
839 terminal_input_raw(vw->vw_terminal, c);
845 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
847 struct vt_device *vd = arg;
853 case KBDIO_UNLOADING:
855 vd->vd_keyboard = -1;
856 kbd_release(kbd, (void *)vd);
863 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
864 vt_processkey(kbd, vd, c);
870 vt_allocate_keyboard(struct vt_device *vd)
876 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
878 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
879 k0 = kbd_get_keyboard(idx0);
881 for (idx = kbd_find_keyboard2("*", -1, 0);
883 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
884 k = kbd_get_keyboard(idx);
886 if (idx == idx0 || KBD_IS_BUSY(k))
889 bzero(&ki, sizeof(ki));
890 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
891 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
892 ki.kb_unit = k->kb_unit;
894 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
897 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
898 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
900 DPRINTF(10, "%s: No keyboard found.\n", __func__);
904 vd->vd_keyboard = idx0;
905 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
911 vtterm_bell(struct terminal *tm)
913 struct vt_window *vw = tm->tm_softc;
914 struct vt_device *vd = vw->vw_device;
916 if (vd->vd_flags & VDF_QUIET_BELL)
919 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
923 vtterm_beep(struct terminal *tm, u_int param)
927 if ((param == 0) || ((param & 0xffff) == 0)) {
932 period = ((param >> 16) & 0xffff) * hz / 1000;
933 freq = 1193182 / (param & 0xffff);
935 sysbeep(freq, period);
939 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
941 struct vt_window *vw = tm->tm_softc;
943 vtbuf_cursor_position(&vw->vw_buf, p);
944 vt_resume_flush_timer(vw->vw_device, 0);
948 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
950 struct vt_window *vw = tm->tm_softc;
952 vtbuf_putchar(&vw->vw_buf, p, c);
953 vt_resume_flush_timer(vw->vw_device, 0);
957 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
959 struct vt_window *vw = tm->tm_softc;
961 vtbuf_fill_locked(&vw->vw_buf, r, c);
962 vt_resume_flush_timer(vw->vw_device, 0);
966 vtterm_copy(struct terminal *tm, const term_rect_t *r,
969 struct vt_window *vw = tm->tm_softc;
971 vtbuf_copy(&vw->vw_buf, r, p);
972 vt_resume_flush_timer(vw->vw_device, 0);
976 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
978 struct vt_window *vw = tm->tm_softc;
982 vtbuf_cursor_visibility(&vw->vw_buf, arg);
983 vt_resume_flush_timer(vw->vw_device, 0);
986 vw->vw_mouse_level = arg;
992 vt_determine_colors(term_char_t c, int cursor,
993 term_color_t *fg, term_color_t *bg)
1000 *fg = TCHAR_FGCOLOR(c);
1001 if (TCHAR_FORMAT(c) & TF_BOLD)
1002 *fg = TCOLOR_LIGHT(*fg);
1003 *bg = TCHAR_BGCOLOR(c);
1005 if (TCHAR_FORMAT(c) & TF_REVERSE)
1017 #ifndef SC_NO_CUTPASTE
1019 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1021 unsigned int mx, my, x1, y1, x2, y2;
1024 * We use the cursor position saved during the current refresh,
1025 * in case the cursor moved since.
1027 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1028 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1030 x1 = area->tr_begin.tp_col;
1031 y1 = area->tr_begin.tp_row;
1032 x2 = area->tr_end.tp_col;
1033 y2 = area->tr_end.tp_row;
1035 if (((mx >= x1 && x2 - 1 >= mx) ||
1036 (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
1037 ((my >= y1 && y2 - 1 >= my) ||
1038 (my < y1 && my + vd->vd_mcursor->height >= y1)))
1045 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1048 struct vt_window *vw;
1052 vw = vd->vd_curwindow;
1055 x = vd->vd_mx_drawn;
1056 y = vd->vd_my_drawn;
1059 area.tr_begin.tp_col = x / vf->vf_width;
1060 area.tr_begin.tp_row = y / vf->vf_height;
1061 area.tr_end.tp_col =
1062 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1063 area.tr_end.tp_row =
1064 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1067 * No font loaded (ie. vt_vga operating in textmode).
1069 * FIXME: This fake area needs to be revisited once the
1070 * mouse cursor is supported in vt_vga's textmode.
1072 area.tr_begin.tp_col = x;
1073 area.tr_begin.tp_row = y;
1074 area.tr_end.tp_col = x + 2;
1075 area.tr_end.tp_row = y + 2;
1078 vtbuf_dirty(&vw->vw_buf, &area);
1083 vt_flush(struct vt_device *vd)
1085 struct vt_window *vw;
1089 #ifndef SC_NO_CUTPASTE
1090 int cursor_was_shown, cursor_moved;
1093 vw = vd->vd_curwindow;
1097 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1101 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1104 #ifndef SC_NO_CUTPASTE
1105 cursor_was_shown = vd->vd_mshown;
1106 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1107 vd->vd_my != vd->vd_my_drawn);
1109 /* Check if the cursor should be displayed or not. */
1110 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1111 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1112 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1119 * If the cursor changed display state or moved, we must mark
1120 * the old position as dirty, so that it's erased.
1122 if (cursor_was_shown != vd->vd_mshown ||
1123 (vd->vd_mshown && cursor_moved))
1124 vt_mark_mouse_position_as_dirty(vd);
1127 * Save position of the mouse cursor. It's used by backends to
1128 * know where to draw the cursor and during the next refresh to
1129 * erase the previous position.
1131 vd->vd_mx_drawn = vd->vd_mx;
1132 vd->vd_my_drawn = vd->vd_my;
1135 * If the cursor is displayed and has moved since last refresh,
1136 * mark the new position as dirty.
1138 if (vd->vd_mshown && cursor_moved)
1139 vt_mark_mouse_position_as_dirty(vd);
1142 vtbuf_undirty(&vw->vw_buf, &tarea);
1143 vt_termsize(vd, vf, &size);
1145 /* Force a full redraw when the screen contents are invalid. */
1146 if (vd->vd_flags & VDF_INVALID) {
1147 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1148 tarea.tr_end = size;
1150 vd->vd_flags &= ~VDF_INVALID;
1153 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1154 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1164 struct vt_device *vd;
1168 /* Update screen if required. */
1169 changed = vt_flush(vd);
1171 /* Schedule for next update. */
1173 vt_schedule_flush(vd, 0);
1175 vd->vd_timer_armed = 0;
1179 vtterm_done(struct terminal *tm)
1181 struct vt_window *vw = tm->tm_softc;
1182 struct vt_device *vd = vw->vw_device;
1184 if (kdb_active || panicstr != NULL) {
1185 /* Switch to the debugger. */
1186 if (vd->vd_curwindow != vw) {
1187 vd->vd_curwindow = vw;
1188 vd->vd_flags |= VDF_INVALID;
1189 if (vd->vd_driver->vd_postswitch)
1190 vd->vd_driver->vd_postswitch(vd);
1192 vd->vd_flags &= ~VDF_SPLASH;
1194 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1201 vtterm_splash(struct vt_device *vd)
1203 vt_axis_t top, left;
1205 /* Display a nice boot splash. */
1206 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1208 top = (vd->vd_height - vt_logo_height) / 2;
1209 left = (vd->vd_width - vt_logo_width) / 2;
1210 switch (vt_logo_depth) {
1212 /* XXX: Unhardcode colors! */
1213 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1214 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1215 left, top, TC_WHITE, TC_BLACK);
1217 vd->vd_flags |= VDF_SPLASH;
1224 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1226 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1227 struct vt_window *vw = tm->tm_softc;
1228 struct vt_device *vd = vw->vw_device;
1233 if (!vty_enabled(VTY_VT))
1236 if (vd->vd_flags & VDF_INITIALIZED)
1237 /* Initialization already done. */
1240 SET_FOREACH(vtdlist, vt_drv_set) {
1242 if (vtd->vd_probe == NULL)
1244 if (vtd->vd_probe(vd) == CN_DEAD)
1246 if ((vtdbest == NULL) ||
1247 (vtd->vd_priority > vtdbest->vd_priority))
1250 if (vtdbest == NULL) {
1251 cp->cn_pri = CN_DEAD;
1252 vd->vd_flags |= VDF_DEAD;
1254 vd->vd_driver = vtdbest;
1255 cp->cn_pri = vd->vd_driver->vd_init(vd);
1258 /* Check if driver's vt_init return CN_DEAD. */
1259 if (cp->cn_pri == CN_DEAD) {
1260 vd->vd_flags |= VDF_DEAD;
1263 /* Initialize any early-boot keyboard drivers */
1264 kbd_configure(KB_CONF_PROBE_ONLY);
1266 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1267 vd->vd_windows[VT_CONSWINDOW] = vw;
1268 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1270 /* Attach default font if not in TEXTMODE. */
1271 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1272 vw->vw_font = vtfont_ref(&vt_font_default);
1273 vt_compute_drawable_area(vw);
1277 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1278 * that we have the real viewable size, fix it in the static
1281 if (vd->vd_width != 0 && vd->vd_height != 0)
1282 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1284 vtbuf_init_early(&vw->vw_buf);
1285 vt_winsize(vd, vw->vw_font, &wsz);
1286 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1288 attr.ta_format = TCHAR_FORMAT(c);
1289 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1290 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1291 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1293 if (vtdbest != NULL) {
1297 vd->vd_flags |= VDF_INITIALIZED;
1302 vtterm_cngetc(struct terminal *tm)
1304 struct vt_window *vw = tm->tm_softc;
1305 struct vt_device *vd = vw->vw_device;
1309 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1310 return (*vw->vw_kbdsq++);
1312 /* Make sure the splash screen is not there. */
1313 if (vd->vd_flags & VDF_SPLASH) {
1315 vd->vd_flags &= ~VDF_SPLASH;
1316 /* Mark screen as invalid to force update */
1317 vd->vd_flags |= VDF_INVALID;
1321 /* Stripped down keyboard handler. */
1322 kbd = kbd_get_keyboard(vd->vd_keyboard);
1326 /* Force keyboard input mode to K_XLATE */
1327 vw->vw_kbdmode = K_XLATE;
1328 vt_update_kbd_mode(vw, kbd);
1330 /* Switch the keyboard to polling to make it work here. */
1331 kbdd_poll(kbd, TRUE);
1332 c = kbdd_read_char(kbd, 0);
1333 kbdd_poll(kbd, FALSE);
1337 if (vw->vw_flags & VWF_SCROLL) {
1338 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1343 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1347 vt_save_kbd_state(vw, kbd);
1348 if (vw->vw_kbdstate & SLKED) {
1349 /* Turn scrolling on. */
1350 vw->vw_flags |= VWF_SCROLL;
1351 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1353 /* Turn scrolling off. */
1354 vt_scroll(vw, 0, VHS_END);
1355 vw->vw_flags &= ~VWF_SCROLL;
1356 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1359 /* XXX: KDB can handle history. */
1360 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1361 vw->vw_kbdsq = "\x1b[A";
1363 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1364 vw->vw_kbdsq = "\x1b[B";
1366 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1367 vw->vw_kbdsq = "\x1b[C";
1369 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1370 vw->vw_kbdsq = "\x1b[D";
1374 /* Force refresh to make scrollback work. */
1376 } else if (KEYFLAGS(c) == 0) {
1377 return (KEYCHAR(c));
1380 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1381 return (*vw->vw_kbdsq++);
1387 vtterm_cngrab(struct terminal *tm)
1389 struct vt_device *vd;
1390 struct vt_window *vw;
1397 vt_window_switch(vw);
1399 kbd = kbd_get_keyboard(vd->vd_keyboard);
1403 if (vw->vw_grabbed++ > 0)
1407 * Make sure the keyboard is accessible even when the kbd device
1408 * driver is disabled.
1412 /* We shall always use the keyboard in the XLATE mode here. */
1413 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1414 vw->vw_kbdmode = K_XLATE;
1415 vt_update_kbd_mode(vw, kbd);
1417 kbdd_poll(kbd, TRUE);
1421 vtterm_cnungrab(struct terminal *tm)
1423 struct vt_device *vd;
1424 struct vt_window *vw;
1430 kbd = kbd_get_keyboard(vd->vd_keyboard);
1434 if (--vw->vw_grabbed > 0)
1437 kbdd_poll(kbd, FALSE);
1439 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1440 vt_update_kbd_mode(vw, kbd);
1445 vtterm_opened(struct terminal *tm, int opened)
1447 struct vt_window *vw = tm->tm_softc;
1448 struct vt_device *vd = vw->vw_device;
1451 vd->vd_flags &= ~VDF_SPLASH;
1453 vw->vw_flags |= VWF_OPENED;
1455 vw->vw_flags &= ~VWF_OPENED;
1456 /* TODO: finish ACQ/REL */
1462 vt_set_border(struct vt_window *vw, term_color_t c)
1464 struct vt_device *vd = vw->vw_device;
1466 if (vd->vd_driver->vd_drawrect == NULL)
1470 if (vw->vw_draw_area.tr_begin.tp_row > 0)
1471 vd->vd_driver->vd_drawrect(vd,
1473 vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1477 if (vw->vw_draw_area.tr_begin.tp_col > 0)
1478 vd->vd_driver->vd_drawrect(vd,
1480 vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1484 if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1485 vd->vd_driver->vd_drawrect(vd,
1486 vw->vw_draw_area.tr_end.tp_col - 1, 0,
1487 vd->vd_width - 1, vd->vd_height - 1,
1491 if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1492 vd->vd_driver->vd_drawrect(vd,
1493 0, vw->vw_draw_area.tr_end.tp_row - 1,
1494 vd->vd_width - 1, vd->vd_height - 1,
1501 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1503 struct vt_device *vd = vw->vw_device;
1504 struct terminal *tm = vw->vw_terminal;
1511 * Changing fonts is a little tricky. We must prevent
1512 * simultaneous access to the device, so we must stop
1513 * the display timer and the terminal from accessing.
1514 * We need to switch fonts and grow our screen buffer.
1516 * XXX: Right now the code uses terminal_mute() to
1517 * prevent data from reaching the console driver while
1518 * resizing the screen buffer. This isn't elegant...
1522 if (vw->vw_flags & VWF_BUSY) {
1523 /* Another process is changing the font. */
1527 vw->vw_flags |= VWF_BUSY;
1530 vt_termsize(vd, vf, &size);
1531 vt_winsize(vd, vf, &wsz);
1533 /* Grow the screen buffer and terminal. */
1534 terminal_mute(tm, 1);
1535 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1536 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1537 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1538 terminal_mute(tm, 0);
1540 /* Actually apply the font to the current window. */
1542 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1544 * In case vt_change_font called to update size we don't need
1545 * to update font link.
1547 vtfont_unref(vw->vw_font);
1548 vw->vw_font = vtfont_ref(vf);
1552 * Compute the drawable area and move the mouse cursor inside
1553 * it, in case the new area is smaller than the previous one.
1555 vt_compute_drawable_area(vw);
1556 vd->vd_mx = min(vd->vd_mx,
1557 vw->vw_draw_area.tr_end.tp_col -
1558 vw->vw_draw_area.tr_begin.tp_col - 1);
1559 vd->vd_my = min(vd->vd_my,
1560 vw->vw_draw_area.tr_end.tp_row -
1561 vw->vw_draw_area.tr_begin.tp_row - 1);
1563 /* Force a full redraw the next timer tick. */
1564 if (vd->vd_curwindow == vw) {
1565 vt_set_border(vw, TC_BLACK);
1566 vd->vd_flags |= VDF_INVALID;
1567 vt_resume_flush_timer(vw->vw_device, 0);
1569 vw->vw_flags &= ~VWF_BUSY;
1575 vt_proc_alive(struct vt_window *vw)
1579 if (vw->vw_smode.mode != VT_PROCESS)
1583 if ((p = pfind(vw->vw_pid)) != NULL)
1585 if (vw->vw_proc == p)
1588 vw->vw_smode.mode = VT_AUTO;
1589 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1596 signal_vt_rel(struct vt_window *vw)
1599 if (vw->vw_smode.mode != VT_PROCESS)
1601 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1606 vw->vw_flags |= VWF_SWWAIT_REL;
1607 PROC_LOCK(vw->vw_proc);
1608 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1609 PROC_UNLOCK(vw->vw_proc);
1610 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1615 signal_vt_acq(struct vt_window *vw)
1618 if (vw->vw_smode.mode != VT_PROCESS)
1620 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1621 cnavailable(vw->vw_terminal->consdev, FALSE);
1622 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1627 vw->vw_flags |= VWF_SWWAIT_ACQ;
1628 PROC_LOCK(vw->vw_proc);
1629 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1630 PROC_UNLOCK(vw->vw_proc);
1631 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1636 finish_vt_rel(struct vt_window *vw, int release, int *s)
1639 if (vw->vw_flags & VWF_SWWAIT_REL) {
1640 vw->vw_flags &= ~VWF_SWWAIT_REL;
1642 callout_drain(&vw->vw_proc_dead_timer);
1643 vt_late_window_switch(vw->vw_switch_to);
1651 finish_vt_acq(struct vt_window *vw)
1654 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1655 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1661 #ifndef SC_NO_CUTPASTE
1663 vt_mouse_terminput_button(struct vt_device *vd, int button)
1665 struct vt_window *vw;
1667 char mouseb[6] = "\x1B[M";
1670 vw = vd->vd_curwindow;
1673 /* Translate to char position. */
1674 x = vd->vd_mx / vf->vf_width;
1675 y = vd->vd_my / vf->vf_height;
1676 /* Avoid overflow. */
1677 x = MIN(x, 255 - '!');
1678 y = MIN(y, 255 - '!');
1680 mouseb[3] = ' ' + button;
1681 mouseb[4] = '!' + x;
1682 mouseb[5] = '!' + y;
1684 for (i = 0; i < sizeof(mouseb); i++)
1685 terminal_input_char(vw->vw_terminal, mouseb[i]);
1689 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1694 case MOUSE_BUTTON_EVENT:
1696 /* Mouse button pressed. */
1697 if (event & MOUSE_BUTTON1DOWN)
1698 vt_mouse_terminput_button(vd, 0);
1699 if (event & MOUSE_BUTTON2DOWN)
1700 vt_mouse_terminput_button(vd, 1);
1701 if (event & MOUSE_BUTTON3DOWN)
1702 vt_mouse_terminput_button(vd, 2);
1704 /* Mouse button released. */
1705 vt_mouse_terminput_button(vd, 3);
1709 case MOUSE_MOTION_EVENT:
1710 if (mouse->u.data.z < 0) {
1712 sc_mouse_input_button(vd, 64);
1713 } else if (mouse->u.data.z > 0) {
1715 sc_mouse_input_button(vd, 65);
1728 len = VD_PASTEBUFLEN(main_vd);
1729 buf = VD_PASTEBUF(main_vd);
1730 len /= sizeof(term_char_t);
1731 for (i = 0; i < len; i++) {
1734 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1740 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1742 struct vt_device *vd;
1743 struct vt_window *vw;
1749 vw = vd->vd_curwindow;
1753 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1755 * Either the mouse is disabled, or the window is in
1756 * "graphics mode". The graphics mode is usually set by
1757 * an X server, using the KDSETMODE ioctl.
1761 if (vf == NULL) /* Text mode. */
1765 * TODO: add flag about pointer position changed, to not redraw chars
1766 * under mouse pointer when nothing changed.
1769 if (vw->vw_mouse_level > 0)
1770 vt_mouse_terminput(vd, type, x, y, event, cnt);
1774 case MOUSE_MOTION_EVENT:
1779 vt_termsize(vd, vf, &size);
1784 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1785 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1789 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1790 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1791 vd->vd_mx / vf->vf_width,
1792 vd->vd_my / vf->vf_height);
1794 vt_resume_flush_timer(vw->vw_device, 0);
1796 case MOUSE_BUTTON_EVENT:
1804 case MOUSE_BUTTON1DOWN:
1807 mark = VTB_MARK_END;
1809 case 1: /* single click: start cut operation */
1810 mark = VTB_MARK_START;
1812 case 2: /* double click: cut a word */
1813 mark = VTB_MARK_WORD;
1815 case 3: /* triple click: cut a line */
1816 mark = VTB_MARK_ROW;
1820 case VT_MOUSE_PASTEBUTTON:
1829 case VT_MOUSE_EXTENDBUTTON:
1832 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1833 mark = VTB_MARK_EXTEND;
1838 mark = VTB_MARK_EXTEND;
1846 /* Save buttons state. */
1848 vd->vd_mstate |= event;
1850 vd->vd_mstate &= ~event;
1852 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1853 vd->vd_my / vf->vf_height) == 1) {
1855 * We have something marked to copy, so update pointer to
1856 * window with selection.
1858 vt_resume_flush_timer(vw->vw_device, 0);
1864 case VTB_MARK_EXTEND:
1867 /* Other types of mark do not require to copy data. */
1871 /* Get current selection size in bytes. */
1872 len = vtbuf_get_marked_len(&vw->vw_buf);
1876 /* Reallocate buffer only if old one is too small. */
1877 if (len > VD_PASTEBUFSZ(vd)) {
1878 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1880 /* Update buffer size. */
1881 VD_PASTEBUFSZ(vd) = len;
1883 /* Request copy/paste buffer data, no more than `len' */
1884 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1887 VD_PASTEBUFLEN(vd) = len;
1889 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1894 vt_mouse_state(int show)
1896 struct vt_device *vd;
1897 struct vt_window *vw;
1900 vw = vd->vd_curwindow;
1904 vw->vw_flags |= VWF_MOUSE_HIDE;
1907 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1911 /* Mark mouse position as dirty. */
1912 vt_mark_mouse_position_as_dirty(vd);
1913 vt_resume_flush_timer(vw->vw_device, 0);
1918 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1919 int nprot, vm_memattr_t *memattr)
1921 struct vt_window *vw = tm->tm_softc;
1922 struct vt_device *vd = vw->vw_device;
1924 if (vd->vd_driver->vd_fb_mmap)
1925 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1932 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1935 struct vt_window *vw = tm->tm_softc;
1936 struct vt_device *vd = vw->vw_device;
1939 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1940 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1951 cmd = VT_WAITACTIVE;
1977 ival = IOCPARM_IVAL(data);
1978 data = (caddr_t)&ival;
1983 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1984 if (*(int *)data & ~0x7f)
1989 case GIO_DEADKEYMAP:
1990 case PIO_DEADKEYMAP:
1995 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1996 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1997 case KBADDKBD: /* add/remove keyboard to/from mux */
2002 kbd = kbd_get_keyboard(vd->vd_keyboard);
2004 error = kbdd_ioctl(kbd, cmd, data);
2006 if (error == ENOIOCTL) {
2007 if (cmd == KDGKBTYPE) {
2008 /* always return something? XXX */
2016 case KDGKBSTATE: { /* get keyboard state (locks) */
2019 if (vw == vd->vd_curwindow) {
2021 kbd = kbd_get_keyboard(vd->vd_keyboard);
2023 error = vt_save_kbd_state(vw, kbd);
2030 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2034 case KDSKBSTATE: { /* set keyboard state (locks) */
2037 state = *(int *)data;
2038 if (state & ~LOCK_MASK)
2041 vw->vw_kbdstate &= ~LOCK_MASK;
2042 vw->vw_kbdstate |= state;
2045 if (vw == vd->vd_curwindow) {
2047 kbd = kbd_get_keyboard(vd->vd_keyboard);
2049 error = vt_update_kbd_state(vw, kbd);
2055 case KDGETLED: { /* get keyboard LED status */
2058 if (vw == vd->vd_curwindow) {
2060 kbd = kbd_get_keyboard(vd->vd_keyboard);
2062 error = vt_save_kbd_leds(vw, kbd);
2069 *(int *)data = vw->vw_kbdstate & LED_MASK;
2073 case KDSETLED: { /* set keyboard LED status */
2076 leds = *(int *)data;
2077 if (leds & ~LED_MASK)
2080 vw->vw_kbdstate &= ~LED_MASK;
2081 vw->vw_kbdstate |= leds;
2084 if (vw == vd->vd_curwindow) {
2086 kbd = kbd_get_keyboard(vd->vd_keyboard);
2088 error = vt_update_kbd_leds(vw, kbd);
2097 if (vw == vd->vd_curwindow) {
2099 kbd = kbd_get_keyboard(vd->vd_keyboard);
2101 error = vt_save_kbd_mode(vw, kbd);
2108 *(int *)data = vw->vw_kbdmode;
2115 mode = *(int *)data;
2120 vw->vw_kbdmode = mode;
2123 if (vw == vd->vd_curwindow) {
2125 kbd = kbd_get_keyboard(vd->vd_keyboard);
2127 error = vt_update_kbd_mode(vw, kbd);
2137 case FBIO_GETWINORG: /* get frame buffer window origin */
2138 case FBIO_GETDISPSTART: /* get display start address */
2139 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2140 case FBIO_BLANK: /* blank display */
2141 if (vd->vd_driver->vd_fb_ioctl)
2142 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2144 case CONS_BLANKTIME:
2149 *(int *)data = M_CG640x480;
2151 case CONS_BELLTYPE: /* set bell type sound */
2152 if ((*(int *)data) & CONS_QUIET_BELL)
2153 vd->vd_flags |= VDF_QUIET_BELL;
2155 vd->vd_flags &= ~VDF_QUIET_BELL;
2157 case CONS_GETINFO: {
2158 vid_info_t *vi = (vid_info_t *)data;
2159 if (vi->size != sizeof(struct vid_info))
2162 if (vw == vd->vd_curwindow) {
2163 kbd = kbd_get_keyboard(vd->vd_keyboard);
2165 vt_save_kbd_state(vw, kbd);
2168 vi->m_num = vd->vd_curwindow->vw_number + 1;
2169 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2170 /* XXX: other fields! */
2174 *(int *)data = 0x200;
2179 case CONS_MOUSECTL: {
2180 mouse_info_t *mouse = (mouse_info_t*)data;
2183 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2184 * should not be applied to individual TTYs, but only to
2187 switch (mouse->operation) {
2189 if (vd->vd_flags & VDF_MOUSECURSOR) {
2190 vd->vd_flags &= ~VDF_MOUSECURSOR;
2191 #ifndef SC_NO_CUTPASTE
2192 vt_mouse_state(VT_MOUSE_HIDE);
2197 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2198 vd->vd_flags |= VDF_MOUSECURSOR;
2199 vd->vd_mx = vd->vd_width / 2;
2200 vd->vd_my = vd->vd_height / 2;
2201 #ifndef SC_NO_CUTPASTE
2202 vt_mouse_state(VT_MOUSE_SHOW);
2213 if (vd->vd_flags & VDF_TEXTMODE)
2216 error = vtfont_load((void *)data, &vf);
2220 error = vt_change_font(vw, vf);
2224 case PIO_VFONT_DEFAULT: {
2225 /* Reset to default font. */
2226 error = vt_change_font(vw, &vt_font_default);
2230 scrmap_t *sm = (scrmap_t *)data;
2232 /* We don't have screen maps, so return a handcrafted one. */
2233 for (i = 0; i < 256; i++)
2239 * FIXME: This implementation is incomplete compared to
2242 switch (*(int *)data) {
2246 vw->vw_flags &= ~VWF_GRAPHICS;
2249 vw->vw_flags |= VWF_GRAPHICS;
2253 case KDENABIO: /* allow io operations */
2254 error = priv_check(td, PRIV_IO);
2257 error = securelevel_gt(td->td_ucred, 0);
2260 #if defined(__i386__)
2261 td->td_frame->tf_eflags |= PSL_IOPL;
2262 #elif defined(__amd64__)
2263 td->td_frame->tf_rflags |= PSL_IOPL;
2266 case KDDISABIO: /* disallow io operations (default) */
2267 #if defined(__i386__)
2268 td->td_frame->tf_eflags &= ~PSL_IOPL;
2269 #elif defined(__amd64__)
2270 td->td_frame->tf_rflags &= ~PSL_IOPL;
2273 case KDMKTONE: /* sound the bell */
2274 vtterm_beep(tm, *(u_int *)data);
2276 case KIOCSOUND: /* make tone (*data) hz */
2279 case CONS_SETKBD: /* set the new keyboard */
2282 if (vd->vd_keyboard != *(int *)data) {
2283 kbd = kbd_get_keyboard(*(int *)data);
2288 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2289 (void *)vd, vt_kbdevent, vd);
2291 if (vd->vd_keyboard != -1) {
2292 vt_save_kbd_state(vd->vd_curwindow, kbd);
2293 kbd_release(kbd, (void *)vd);
2295 kbd = kbd_get_keyboard(i);
2296 vd->vd_keyboard = i;
2298 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2299 vt_update_kbd_state(vd->vd_curwindow, kbd);
2301 error = EPERM; /* XXX */
2306 case CONS_RELKBD: /* release the current keyboard */
2309 if (vd->vd_keyboard != -1) {
2310 kbd = kbd_get_keyboard(vd->vd_keyboard);
2315 vt_save_kbd_state(vd->vd_curwindow, kbd);
2316 error = kbd_release(kbd, (void *)vd);
2318 vd->vd_keyboard = -1;
2325 win = *(int *)data - 1;
2326 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2328 if ((win >= VT_MAXWINDOWS) || (win < 0))
2330 return (vt_proc_window_switch(vd->vd_windows[win]));
2333 *(int *)data = vd->vd_curwindow->vw_number + 1;
2336 *(int *)data = vw->vw_number + 1;
2339 /* TODO: Check current state, switching can be in progress. */
2340 if ((*(int *)data) == 0x01)
2341 vw->vw_flags |= VWF_VTYLOCK;
2342 else if ((*(int *)data) == 0x02)
2343 vw->vw_flags &= ~VWF_VTYLOCK;
2349 for (i = 0; i < VT_MAXWINDOWS; i++) {
2350 vw = vd->vd_windows[i];
2353 if (!(vw->vw_flags & VWF_OPENED)) {
2354 *(int *)data = vw->vw_number + 1;
2361 case VT_WAITACTIVE: {
2366 idx = *(unsigned int *)data;
2367 if (idx > VT_MAXWINDOWS)
2370 vw = vd->vd_windows[idx - 1];
2373 while (vd->vd_curwindow != vw && error == 0)
2374 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2378 case VT_SETMODE: { /* set screen switcher mode */
2379 struct vt_mode *mode;
2382 mode = (struct vt_mode *)data;
2383 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2384 if (vw->vw_smode.mode == VT_PROCESS) {
2385 p1 = pfind(vw->vw_pid);
2386 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2389 DPRINTF(5, "error EPERM\n");
2395 if (mode->mode == VT_AUTO) {
2396 vw->vw_smode.mode = VT_AUTO;
2399 DPRINTF(5, "VT_AUTO, ");
2400 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2401 cnavailable(vw->vw_terminal->consdev, TRUE);
2402 /* were we in the middle of the vty switching process? */
2403 if (finish_vt_rel(vw, TRUE, &s) == 0)
2404 DPRINTF(5, "reset WAIT_REL, ");
2405 if (finish_vt_acq(vw) == 0)
2406 DPRINTF(5, "reset WAIT_ACQ, ");
2408 } else if (mode->mode == VT_PROCESS) {
2409 if (!ISSIGVALID(mode->relsig) ||
2410 !ISSIGVALID(mode->acqsig) ||
2411 !ISSIGVALID(mode->frsig)) {
2412 DPRINTF(5, "error EINVAL\n");
2415 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2416 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2417 vw->vw_proc = td->td_proc;
2418 vw->vw_pid = vw->vw_proc->p_pid;
2419 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2420 cnavailable(vw->vw_terminal->consdev, FALSE);
2422 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2429 case VT_GETMODE: /* get screen switcher mode */
2430 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2433 case VT_RELDISP: /* screen switcher ioctl */
2435 * This must be the current vty which is in the VT_PROCESS
2438 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2442 /* ...and this process is controlling it. */
2443 if (vw->vw_proc != td->td_proc) {
2447 switch(*(int *)data) {
2448 case VT_FALSE: /* user refuses to release screen, abort */
2449 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2450 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2451 SC_DRIVER_NAME, VT_UNIT(vw));
2453 case VT_TRUE: /* user has released screen, go on */
2454 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2455 if (vw->vw_flags & VWF_SWWAIT_REL) {
2456 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2457 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2458 SC_DRIVER_NAME, VT_UNIT(vw));
2463 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2464 if ((error = finish_vt_acq(vw)) == 0)
2465 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2466 SC_DRIVER_NAME, VT_UNIT(vw));
2477 static struct vt_window *
2478 vt_allocate_window(struct vt_device *vd, unsigned int window)
2480 struct vt_window *vw;
2481 struct terminal *tm;
2485 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2487 vw->vw_number = window;
2488 vw->vw_kbdmode = K_XLATE;
2490 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2491 vw->vw_font = vtfont_ref(&vt_font_default);
2492 vt_compute_drawable_area(vw);
2495 vt_termsize(vd, vw->vw_font, &size);
2496 vt_winsize(vd, vw->vw_font, &wsz);
2497 vtbuf_init(&vw->vw_buf, &size);
2499 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2500 terminal_set_winsize(tm, &wsz);
2501 vd->vd_windows[window] = vw;
2502 callout_init(&vw->vw_proc_dead_timer, 0);
2508 vt_upgrade(struct vt_device *vd)
2510 struct vt_window *vw;
2512 int register_handlers;
2514 if (!vty_enabled(VTY_VT))
2516 if (main_vd->vd_driver == NULL)
2519 for (i = 0; i < VT_MAXWINDOWS; i++) {
2520 vw = vd->vd_windows[i];
2523 vw = vt_allocate_window(vd, i);
2525 if (!(vw->vw_flags & VWF_READY)) {
2526 callout_init(&vw->vw_proc_dead_timer, 0);
2527 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2528 vw->vw_flags |= VWF_READY;
2529 if (vw->vw_flags & VWF_CONSOLE) {
2530 /* For existing console window. */
2531 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2532 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2538 if (vd->vd_curwindow == NULL)
2539 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2541 register_handlers = 0;
2542 if (!(vd->vd_flags & VDF_ASYNC)) {
2543 /* Attach keyboard. */
2544 vt_allocate_keyboard(vd);
2546 /* Init 25 Hz timer. */
2547 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2549 /* Start timer when everything ready. */
2550 vd->vd_flags |= VDF_ASYNC;
2551 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2552 vd->vd_timer_armed = 1;
2553 register_handlers = 1;
2558 /* Refill settings with new sizes. */
2561 if (register_handlers) {
2562 /* Register suspend/resume handlers. */
2563 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2564 vd, EVENTHANDLER_PRI_ANY);
2565 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2566 EVENTHANDLER_PRI_ANY);
2571 vt_resize(struct vt_device *vd)
2573 struct vt_window *vw;
2576 for (i = 0; i < VT_MAXWINDOWS; i++) {
2577 vw = vd->vd_windows[i];
2579 /* Assign default font to window, if not textmode. */
2580 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2581 vw->vw_font = vtfont_ref(&vt_font_default);
2584 /* Resize terminal windows */
2585 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2586 DPRINTF(100, "%s: vt_change_font() is busy, "
2587 "window %d\n", __func__, i);
2593 vt_allocate(struct vt_driver *drv, void *softc)
2595 struct vt_device *vd;
2597 if (!vty_enabled(VTY_VT))
2600 if (main_vd->vd_driver == NULL) {
2601 main_vd->vd_driver = drv;
2602 printf("VT: initialize with new VT driver \"%s\".\n",
2606 * Check if have rights to replace current driver. For example:
2607 * it is bad idea to replace KMS driver with generic VGA one.
2609 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2610 printf("VT: Driver priority %d too low. Current %d\n ",
2611 drv->vd_priority, main_vd->vd_driver->vd_priority);
2614 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2615 main_vd->vd_driver->vd_name, drv->vd_name);
2619 if (vd->vd_flags & VDF_ASYNC) {
2620 /* Stop vt_flush periodic task. */
2622 vt_suspend_flush_timer(vd);
2625 * Mute current terminal until we done. vt_change_font (called
2626 * from vt_resize) will unmute it.
2628 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2632 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2636 vd->vd_flags &= ~VDF_TEXTMODE;
2638 vd->vd_driver = drv;
2639 vd->vd_softc = softc;
2640 vd->vd_driver->vd_init(vd);
2643 /* Update windows sizes and initialize last items. */
2647 if (vd->vd_flags & VDF_SPLASH)
2651 if (vd->vd_flags & VDF_ASYNC) {
2652 /* Allow to put chars now. */
2653 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2654 /* Rerun timer for screen updates. */
2655 vt_resume_flush_timer(vd, 0);
2659 * Register as console. If it already registered, cnadd() will ignore
2662 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2666 vt_suspend_handler(void *priv)
2668 struct vt_device *vd;
2671 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2672 vd->vd_driver->vd_suspend(vd);
2676 vt_resume_handler(void *priv)
2678 struct vt_device *vd;
2681 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2682 vd->vd_driver->vd_resume(vd);
2686 vt_suspend(struct vt_device *vd)
2690 if (vt_suspendswitch == 0)
2692 /* Save current window. */
2693 vd->vd_savedwindow = vd->vd_curwindow;
2694 /* Ask holding process to free window and switch to console window */
2695 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2697 /* Wait for the window switch to complete. */
2700 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2701 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2706 vt_resume(struct vt_device *vd)
2709 if (vt_suspendswitch == 0)
2711 /* Switch back to saved window */
2712 if (vd->vd_savedwindow != NULL)
2713 vt_proc_window_switch(vd->vd_savedwindow);
2714 vd->vd_savedwindow = NULL;