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_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
304 DPRINTF(40, "%s\n", __func__);
305 curvw->vw_switch_to = vw;
306 /* Set timer to allow switch in case when process hang. */
307 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
308 vt_switch_timer, (void *)vw);
309 /* Notify process about vt switch attempt. */
310 DPRINTF(30, "%s: Notify process.\n", __func__);
311 signal_vt_rel(curvw);
317 vt_window_postswitch(struct vt_window *vw)
324 /* vt_late_window_switch will done VT switching for regular case. */
326 vt_late_window_switch(struct vt_window *vw)
330 callout_stop(&vw->vw_proc_dead_timer);
332 ret = vt_window_switch(vw);
336 /* Notify owner process about terminal availability. */
337 if (vw->vw_smode.mode == VT_PROCESS) {
338 ret = vt_window_postswitch(vw);
345 vt_proc_window_switch(struct vt_window *vw)
347 struct vt_window *curvw;
348 struct vt_device *vd;
352 curvw = vd->vd_curwindow;
354 if (curvw->vw_flags & VWF_VTYLOCK)
357 /* Ask current process permission to switch away. */
358 if (curvw->vw_smode.mode == VT_PROCESS) {
359 DPRINTF(30, "%s: VT_PROCESS ", __func__);
360 if (vt_proc_alive(curvw) == FALSE) {
361 DPRINTF(30, "Dead. Cleaning.");
364 DPRINTF(30, "%s: Signaling process.\n", __func__);
365 /* Alive, try to ask him. */
366 ret = vt_window_preswitch(vw, curvw);
367 /* Wait for process answer or timeout. */
373 ret = vt_late_window_switch(vw);
377 /* Switch window ignoring process locking. */
379 vt_window_switch(struct vt_window *vw)
381 struct vt_device *vd = vw->vw_device;
382 struct vt_window *curvw = vd->vd_curwindow;
391 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
396 vt_suspend_flush_timer(vd);
398 vd->vd_curwindow = vw;
399 vd->vd_flags |= VDF_INVALID;
400 cv_broadcast(&vd->vd_winswitch);
403 if (vd->vd_driver->vd_postswitch)
404 vd->vd_driver->vd_postswitch(vd);
406 vt_resume_flush_timer(vd, 0);
408 /* Restore per-window keyboard mode. */
410 kbd = kbd_get_keyboard(vd->vd_keyboard);
412 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode);
415 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
421 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
424 size->tp_row = vd->vd_height;
425 size->tp_col = vd->vd_width;
427 size->tp_row /= vf->vf_height;
428 size->tp_col /= vf->vf_width;
433 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
436 size->ws_row = size->ws_ypixel = vd->vd_height;
437 size->ws_col = size->ws_xpixel = vd->vd_width;
439 size->ws_row /= vf->vf_height;
440 size->ws_col /= vf->vf_width;
445 vt_compute_drawable_area(struct vt_window *vw)
447 struct vt_device *vd;
452 if (vw->vw_font == NULL) {
453 vw->vw_draw_area.tr_begin.tp_col = 0;
454 vw->vw_draw_area.tr_begin.tp_row = 0;
455 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
456 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
463 * Compute the drawable area, so that the text is centered on
467 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
468 vw->vw_draw_area.tr_begin.tp_row = (vd->vd_height % vf->vf_height) / 2;
469 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
470 vd->vd_width / vf->vf_width * vf->vf_width;
471 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
472 vd->vd_height / vf->vf_height * vf->vf_height;
476 vt_scroll(struct vt_window *vw, int offset, int whence)
481 if ((vw->vw_flags & VWF_SCROLL) == 0)
484 vt_termsize(vw->vw_device, vw->vw_font, &size);
486 diff = vthistory_seek(&vw->vw_buf, offset, whence);
488 vw->vw_device->vd_flags |= VDF_INVALID;
489 vt_resume_flush_timer(vw->vw_device, 0);
493 vt_machine_kbdevent(int c)
497 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
499 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
501 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
503 shutdown_nice(RB_HALT);
505 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
506 #ifndef SC_NO_CUTPASTE
507 /* Insert text from cut-paste buffer. */
511 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
513 shutdown_nice(RB_HALT|RB_POWEROFF);
515 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
517 * Request to immediate panic if sysctl
518 * kern.vt.enable_panic_key allow it.
521 panic("Forced by the panic key");
523 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
525 shutdown_nice(RB_AUTOBOOT);
527 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
528 /* Force activatation/deactivation of the screen saver. */
531 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
532 /* Put machine into Stand-By mode. */
533 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
535 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
536 /* Suspend machine. */
537 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
545 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
547 struct vt_device *vd;
551 /* Only special keys handled in ScrollLock mode */
552 if ((c & SPCLKEY) == 0)
558 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
559 vw = vd->vd_windows[c - F_SCR];
561 vt_proc_window_switch(vw);
569 /* Turn scrolling off. */
570 vt_scroll(vw, 0, VHS_END);
571 VTBUF_SLCK_DISABLE(&vw->vw_buf);
572 vw->vw_flags &= ~VWF_SCROLL;
575 case FKEY | F(49): /* Home key. */
576 vt_scroll(vw, 0, VHS_SET);
578 case FKEY | F(50): /* Arrow up. */
579 vt_scroll(vw, -1, VHS_CUR);
581 case FKEY | F(51): /* Page up. */
582 vt_termsize(vd, vw->vw_font, &size);
583 vt_scroll(vw, -size.tp_row, VHS_CUR);
585 case FKEY | F(57): /* End key. */
586 vt_scroll(vw, 0, VHS_END);
588 case FKEY | F(58): /* Arrow down. */
589 vt_scroll(vw, 1, VHS_CUR);
591 case FKEY | F(59): /* Page down. */
592 vt_termsize(vd, vw->vw_font, &size);
593 vt_scroll(vw, size.tp_row, VHS_CUR);
602 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
604 struct vt_window *vw = vd->vd_curwindow;
607 #if VT_ALT_TO_ESC_HACK
609 switch (c & ~RELKEY) {
610 case (SPCLKEY | RALT):
611 if (vt_enable_altgr != 0)
613 case (SPCLKEY | LALT):
614 vd->vd_kbstate &= ~ALKED;
616 /* Other keys ignored for RELKEY event. */
619 switch (c & ~RELKEY) {
620 case (SPCLKEY | RALT):
621 if (vt_enable_altgr != 0)
623 case (SPCLKEY | LALT):
624 vd->vd_kbstate |= ALKED;
629 /* Other keys ignored for RELKEY event. */
633 if (vt_machine_kbdevent(c))
636 if (vw->vw_flags & VWF_SCROLL) {
637 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
638 /* Scroll mode keys handled, nothing to do more. */
645 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
646 vw = vd->vd_windows[c - F_SCR];
648 vt_proc_window_switch(vw);
654 /* Switch to next VT. */
655 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
656 vw = vd->vd_windows[c];
658 vt_proc_window_switch(vw);
661 /* Switch to previous VT. */
662 c = (vw->vw_number - 1) % VT_MAXWINDOWS;
663 vw = vd->vd_windows[c];
665 vt_proc_window_switch(vw);
669 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
672 /* Turn scrolling on. */
673 vw->vw_flags |= VWF_SCROLL;
674 VTBUF_SLCK_ENABLE(&vw->vw_buf);
676 /* Turn scrolling off. */
677 vw->vw_flags &= ~VWF_SCROLL;
678 VTBUF_SLCK_DISABLE(&vw->vw_buf);
679 vt_scroll(vw, 0, VHS_END);
684 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
685 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
686 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
687 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
688 /* F1 through F12 keys. */
689 terminal_input_special(vw->vw_terminal,
690 TKEY_F1 + c - (FKEY | F(1)));
692 case FKEY | F(49): /* Home key. */
693 terminal_input_special(vw->vw_terminal, TKEY_HOME);
695 case FKEY | F(50): /* Arrow up. */
696 terminal_input_special(vw->vw_terminal, TKEY_UP);
698 case FKEY | F(51): /* Page up. */
699 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
701 case FKEY | F(53): /* Arrow left. */
702 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
704 case FKEY | F(55): /* Arrow right. */
705 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
707 case FKEY | F(57): /* End key. */
708 terminal_input_special(vw->vw_terminal, TKEY_END);
710 case FKEY | F(58): /* Arrow down. */
711 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
713 case FKEY | F(59): /* Page down. */
714 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
716 case FKEY | F(60): /* Insert key. */
717 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
719 case FKEY | F(61): /* Delete key. */
720 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
723 } else if (KEYFLAGS(c) == 0) {
724 /* Don't do UTF-8 conversion when doing raw mode. */
725 if (vw->vw_kbdmode == K_XLATE) {
726 #if VT_ALT_TO_ESC_HACK
727 if (vd->vd_kbstate & ALKED) {
729 * Prepend ESC sequence if one of ALT keys down.
731 terminal_input_char(vw->vw_terminal, 0x1b);
735 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
737 terminal_input_raw(vw->vw_terminal, c);
743 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
745 struct vt_device *vd = arg;
751 case KBDIO_UNLOADING:
753 vd->vd_keyboard = -1;
754 kbd_release(kbd, (void *)vd);
761 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
762 vt_processkey(kbd, vd, c);
768 vt_allocate_keyboard(struct vt_device *vd)
774 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
775 vd->vd_keyboard = idx0;
777 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
778 k0 = kbd_get_keyboard(idx0);
780 for (idx = kbd_find_keyboard2("*", -1, 0);
782 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
783 k = kbd_get_keyboard(idx);
785 if (idx == idx0 || KBD_IS_BUSY(k))
788 bzero(&ki, sizeof(ki));
789 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
790 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
791 ki.kb_unit = k->kb_unit;
793 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
796 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
797 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
799 DPRINTF(10, "%s: No keyboard found.\n", __func__);
803 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
809 vtterm_bell(struct terminal *tm)
811 struct vt_window *vw = tm->tm_softc;
812 struct vt_device *vd = vw->vw_device;
814 if (vd->vd_flags & VDF_QUIET_BELL)
817 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
821 vtterm_beep(struct terminal *tm, u_int param)
825 if ((param == 0) || ((param & 0xffff) == 0)) {
830 period = ((param >> 16) & 0xffff) * hz / 1000;
831 freq = 1193182 / (param & 0xffff);
833 sysbeep(freq, period);
837 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
839 struct vt_window *vw = tm->tm_softc;
841 vtbuf_cursor_position(&vw->vw_buf, p);
842 vt_resume_flush_timer(vw->vw_device, 0);
846 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
848 struct vt_window *vw = tm->tm_softc;
850 vtbuf_putchar(&vw->vw_buf, p, c);
851 vt_resume_flush_timer(vw->vw_device, 0);
855 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
857 struct vt_window *vw = tm->tm_softc;
859 vtbuf_fill_locked(&vw->vw_buf, r, c);
860 vt_resume_flush_timer(vw->vw_device, 0);
864 vtterm_copy(struct terminal *tm, const term_rect_t *r,
867 struct vt_window *vw = tm->tm_softc;
869 vtbuf_copy(&vw->vw_buf, r, p);
870 vt_resume_flush_timer(vw->vw_device, 0);
874 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
876 struct vt_window *vw = tm->tm_softc;
880 vtbuf_cursor_visibility(&vw->vw_buf, arg);
881 vt_resume_flush_timer(vw->vw_device, 0);
884 vw->vw_mouse_level = arg;
890 vt_determine_colors(term_char_t c, int cursor,
891 term_color_t *fg, term_color_t *bg)
898 *fg = TCHAR_FGCOLOR(c);
899 if (TCHAR_FORMAT(c) & TF_BOLD)
900 *fg = TCOLOR_LIGHT(*fg);
901 *bg = TCHAR_BGCOLOR(c);
903 if (TCHAR_FORMAT(c) & TF_REVERSE)
915 #ifndef SC_NO_CUTPASTE
917 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
919 unsigned int mx, my, x1, y1, x2, y2;
922 * We use the cursor position saved during the current refresh,
923 * in case the cursor moved since.
925 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
926 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
928 x1 = area->tr_begin.tp_col;
929 y1 = area->tr_begin.tp_row;
930 x2 = area->tr_end.tp_col;
931 y2 = area->tr_end.tp_row;
933 if (((mx >= x1 && x2 - 1 >= mx) ||
934 (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
935 ((my >= y1 && y2 - 1 >= my) ||
936 (my < y1 && my + vd->vd_mcursor->height >= y1)))
943 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
946 struct vt_window *vw;
950 vw = vd->vd_curwindow;
957 area.tr_begin.tp_col = x / vf->vf_width;
958 area.tr_begin.tp_row = y / vf->vf_height;
960 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
962 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
965 * No font loaded (ie. vt_vga operating in textmode).
967 * FIXME: This fake area needs to be revisited once the
968 * mouse cursor is supported in vt_vga's textmode.
970 area.tr_begin.tp_col = x;
971 area.tr_begin.tp_row = y;
972 area.tr_end.tp_col = x + 2;
973 area.tr_end.tp_row = y + 2;
976 vtbuf_dirty(&vw->vw_buf, &area);
981 vt_flush(struct vt_device *vd)
983 struct vt_window *vw;
987 #ifndef SC_NO_CUTPASTE
988 int cursor_was_shown, cursor_moved;
991 vw = vd->vd_curwindow;
995 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
999 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1002 #ifndef SC_NO_CUTPASTE
1003 cursor_was_shown = vd->vd_mshown;
1004 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1005 vd->vd_my != vd->vd_my_drawn);
1007 /* Check if the cursor should be displayed or not. */
1008 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1009 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1010 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1017 * If the cursor changed display state or moved, we must mark
1018 * the old position as dirty, so that it's erased.
1020 if (cursor_was_shown != vd->vd_mshown ||
1021 (vd->vd_mshown && cursor_moved))
1022 vt_mark_mouse_position_as_dirty(vd);
1025 * Save position of the mouse cursor. It's used by backends to
1026 * know where to draw the cursor and during the next refresh to
1027 * erase the previous position.
1029 vd->vd_mx_drawn = vd->vd_mx;
1030 vd->vd_my_drawn = vd->vd_my;
1033 * If the cursor is displayed and has moved since last refresh,
1034 * mark the new position as dirty.
1036 if (vd->vd_mshown && cursor_moved)
1037 vt_mark_mouse_position_as_dirty(vd);
1040 vtbuf_undirty(&vw->vw_buf, &tarea);
1041 vt_termsize(vd, vf, &size);
1043 /* Force a full redraw when the screen contents are invalid. */
1044 if (vd->vd_flags & VDF_INVALID) {
1045 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1046 tarea.tr_end = size;
1048 vd->vd_flags &= ~VDF_INVALID;
1051 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1052 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1062 struct vt_device *vd;
1066 /* Update screen if required. */
1067 changed = vt_flush(vd);
1069 /* Schedule for next update. */
1071 vt_schedule_flush(vd, 0);
1073 vd->vd_timer_armed = 0;
1077 vtterm_done(struct terminal *tm)
1079 struct vt_window *vw = tm->tm_softc;
1080 struct vt_device *vd = vw->vw_device;
1082 if (kdb_active || panicstr != NULL) {
1083 /* Switch to the debugger. */
1084 if (vd->vd_curwindow != vw) {
1085 vd->vd_curwindow = vw;
1086 vd->vd_flags |= VDF_INVALID;
1087 if (vd->vd_driver->vd_postswitch)
1088 vd->vd_driver->vd_postswitch(vd);
1090 vd->vd_flags &= ~VDF_SPLASH;
1092 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1099 vtterm_splash(struct vt_device *vd)
1101 vt_axis_t top, left;
1103 /* Display a nice boot splash. */
1104 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1106 top = (vd->vd_height - vt_logo_height) / 2;
1107 left = (vd->vd_width - vt_logo_width) / 2;
1108 switch (vt_logo_depth) {
1110 /* XXX: Unhardcode colors! */
1111 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1112 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1113 left, top, TC_WHITE, TC_BLACK);
1115 vd->vd_flags |= VDF_SPLASH;
1122 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1124 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1125 struct vt_window *vw = tm->tm_softc;
1126 struct vt_device *vd = vw->vw_device;
1131 if (!vty_enabled(VTY_VT))
1134 if (vd->vd_flags & VDF_INITIALIZED)
1135 /* Initialization already done. */
1138 SET_FOREACH(vtdlist, vt_drv_set) {
1140 if (vtd->vd_probe == NULL)
1142 if (vtd->vd_probe(vd) == CN_DEAD)
1144 if ((vtdbest == NULL) ||
1145 (vtd->vd_priority > vtdbest->vd_priority))
1148 if (vtdbest == NULL) {
1149 cp->cn_pri = CN_DEAD;
1150 vd->vd_flags |= VDF_DEAD;
1152 vd->vd_driver = vtdbest;
1153 cp->cn_pri = vd->vd_driver->vd_init(vd);
1156 /* Check if driver's vt_init return CN_DEAD. */
1157 if (cp->cn_pri == CN_DEAD) {
1158 vd->vd_flags |= VDF_DEAD;
1161 /* Initialize any early-boot keyboard drivers */
1162 kbd_configure(KB_CONF_PROBE_ONLY);
1164 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1165 vd->vd_windows[VT_CONSWINDOW] = vw;
1166 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1168 /* Attach default font if not in TEXTMODE. */
1169 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1170 vw->vw_font = vtfont_ref(&vt_font_default);
1171 vt_compute_drawable_area(vw);
1175 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1176 * that we have the real viewable size, fix it in the static
1179 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1181 vtbuf_init_early(&vw->vw_buf);
1182 vt_winsize(vd, vw->vw_font, &wsz);
1183 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1185 attr.ta_format = TCHAR_FORMAT(c);
1186 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1187 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1188 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1190 if (vtdbest != NULL) {
1194 vd->vd_flags |= VDF_INITIALIZED;
1199 vtterm_cngetc(struct terminal *tm)
1201 struct vt_window *vw = tm->tm_softc;
1202 struct vt_device *vd = vw->vw_device;
1207 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1208 return (*vw->vw_kbdsq++);
1211 /* Make sure the splash screen is not there. */
1212 if (vd->vd_flags & VDF_SPLASH) {
1214 vd->vd_flags &= ~VDF_SPLASH;
1215 /* Mark screen as invalid to force update */
1216 vd->vd_flags |= VDF_INVALID;
1220 /* Stripped down keyboard handler. */
1221 kbd = kbd_get_keyboard(vd->vd_keyboard);
1225 /* Force keyboard input mode to K_XLATE */
1227 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1229 /* Switch the keyboard to polling to make it work here. */
1230 kbdd_poll(kbd, TRUE);
1231 c = kbdd_read_char(kbd, 0);
1232 kbdd_poll(kbd, FALSE);
1236 if (vw->vw_flags & VWF_SCROLL) {
1237 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1242 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1246 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1247 if (state & SLKED) {
1248 /* Turn scrolling on. */
1249 vw->vw_flags |= VWF_SCROLL;
1250 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1252 /* Turn scrolling off. */
1253 vt_scroll(vw, 0, VHS_END);
1254 vw->vw_flags &= ~VWF_SCROLL;
1255 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1258 /* XXX: KDB can handle history. */
1259 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1260 vw->vw_kbdsq = "\x1b[A";
1262 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1263 vw->vw_kbdsq = "\x1b[B";
1265 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1266 vw->vw_kbdsq = "\x1b[C";
1268 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1269 vw->vw_kbdsq = "\x1b[D";
1273 /* Force refresh to make scrollback work. */
1275 } else if (KEYFLAGS(c) == 0) {
1276 return (KEYCHAR(c));
1279 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1280 return (*vw->vw_kbdsq++);
1286 vtterm_cngrab(struct terminal *tm)
1288 struct vt_device *vd;
1289 struct vt_window *vw;
1296 vt_window_switch(vw);
1298 kbd = kbd_get_keyboard(vd->vd_keyboard);
1302 if (vw->vw_grabbed++ > 0)
1306 * Make sure the keyboard is accessible even when the kbd device
1307 * driver is disabled.
1311 /* We shall always use the keyboard in the XLATE mode here. */
1312 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1313 vw->vw_kbdmode = K_XLATE;
1314 (void)kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
1316 kbdd_poll(kbd, TRUE);
1320 vtterm_cnungrab(struct terminal *tm)
1322 struct vt_device *vd;
1323 struct vt_window *vw;
1329 kbd = kbd_get_keyboard(vd->vd_keyboard);
1333 if (--vw->vw_grabbed > 0)
1336 kbdd_poll(kbd, FALSE);
1338 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1339 (void)kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
1344 vtterm_opened(struct terminal *tm, int opened)
1346 struct vt_window *vw = tm->tm_softc;
1347 struct vt_device *vd = vw->vw_device;
1350 vd->vd_flags &= ~VDF_SPLASH;
1352 vw->vw_flags |= VWF_OPENED;
1354 vw->vw_flags &= ~VWF_OPENED;
1355 /* TODO: finish ACQ/REL */
1361 vt_set_border(struct vt_window *vw, term_color_t c)
1363 struct vt_device *vd = vw->vw_device;
1365 if (vd->vd_driver->vd_drawrect == NULL)
1369 if (vw->vw_draw_area.tr_begin.tp_row > 0)
1370 vd->vd_driver->vd_drawrect(vd,
1372 vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1376 if (vw->vw_draw_area.tr_begin.tp_col > 0)
1377 vd->vd_driver->vd_drawrect(vd,
1379 vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1383 if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1384 vd->vd_driver->vd_drawrect(vd,
1385 vw->vw_draw_area.tr_end.tp_col - 1, 0,
1386 vd->vd_width - 1, vd->vd_height - 1,
1390 if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1391 vd->vd_driver->vd_drawrect(vd,
1392 0, vw->vw_draw_area.tr_end.tp_row - 1,
1393 vd->vd_width - 1, vd->vd_height - 1,
1400 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1402 struct vt_device *vd = vw->vw_device;
1403 struct terminal *tm = vw->vw_terminal;
1410 * Changing fonts is a little tricky. We must prevent
1411 * simultaneous access to the device, so we must stop
1412 * the display timer and the terminal from accessing.
1413 * We need to switch fonts and grow our screen buffer.
1415 * XXX: Right now the code uses terminal_mute() to
1416 * prevent data from reaching the console driver while
1417 * resizing the screen buffer. This isn't elegant...
1421 if (vw->vw_flags & VWF_BUSY) {
1422 /* Another process is changing the font. */
1426 vw->vw_flags |= VWF_BUSY;
1429 vt_termsize(vd, vf, &size);
1430 vt_winsize(vd, vf, &wsz);
1432 /* Grow the screen buffer and terminal. */
1433 terminal_mute(tm, 1);
1434 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1435 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1436 terminal_mute(tm, 0);
1438 /* Actually apply the font to the current window. */
1440 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1442 * In case vt_change_font called to update size we don't need
1443 * to update font link.
1445 vtfont_unref(vw->vw_font);
1446 vw->vw_font = vtfont_ref(vf);
1450 * Compute the drawable area and move the mouse cursor inside
1451 * it, in case the new area is smaller than the previous one.
1453 vt_compute_drawable_area(vw);
1454 vd->vd_mx = min(vd->vd_mx,
1455 vw->vw_draw_area.tr_end.tp_col -
1456 vw->vw_draw_area.tr_begin.tp_col - 1);
1457 vd->vd_my = min(vd->vd_my,
1458 vw->vw_draw_area.tr_end.tp_row -
1459 vw->vw_draw_area.tr_begin.tp_row - 1);
1461 /* Force a full redraw the next timer tick. */
1462 if (vd->vd_curwindow == vw) {
1463 vt_set_border(vw, TC_BLACK);
1464 vd->vd_flags |= VDF_INVALID;
1465 vt_resume_flush_timer(vw->vw_device, 0);
1467 vw->vw_flags &= ~VWF_BUSY;
1473 vt_proc_alive(struct vt_window *vw)
1477 if (vw->vw_smode.mode != VT_PROCESS)
1481 if ((p = pfind(vw->vw_pid)) != NULL)
1483 if (vw->vw_proc == p)
1486 vw->vw_smode.mode = VT_AUTO;
1487 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1494 signal_vt_rel(struct vt_window *vw)
1497 if (vw->vw_smode.mode != VT_PROCESS)
1499 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1504 vw->vw_flags |= VWF_SWWAIT_REL;
1505 PROC_LOCK(vw->vw_proc);
1506 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1507 PROC_UNLOCK(vw->vw_proc);
1508 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1513 signal_vt_acq(struct vt_window *vw)
1516 if (vw->vw_smode.mode != VT_PROCESS)
1518 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1519 cnavailable(vw->vw_terminal->consdev, FALSE);
1520 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1525 vw->vw_flags |= VWF_SWWAIT_ACQ;
1526 PROC_LOCK(vw->vw_proc);
1527 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1528 PROC_UNLOCK(vw->vw_proc);
1529 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1534 finish_vt_rel(struct vt_window *vw, int release, int *s)
1537 if (vw->vw_flags & VWF_SWWAIT_REL) {
1538 vw->vw_flags &= ~VWF_SWWAIT_REL;
1540 callout_drain(&vw->vw_proc_dead_timer);
1541 vt_late_window_switch(vw->vw_switch_to);
1549 finish_vt_acq(struct vt_window *vw)
1552 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1553 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1559 #ifndef SC_NO_CUTPASTE
1561 vt_mouse_terminput_button(struct vt_device *vd, int button)
1563 struct vt_window *vw;
1565 char mouseb[6] = "\x1B[M";
1568 vw = vd->vd_curwindow;
1571 /* Translate to char position. */
1572 x = vd->vd_mx / vf->vf_width;
1573 y = vd->vd_my / vf->vf_height;
1574 /* Avoid overflow. */
1575 x = MIN(x, 255 - '!');
1576 y = MIN(y, 255 - '!');
1578 mouseb[3] = ' ' + button;
1579 mouseb[4] = '!' + x;
1580 mouseb[5] = '!' + y;
1582 for (i = 0; i < sizeof(mouseb); i++)
1583 terminal_input_char(vw->vw_terminal, mouseb[i]);
1587 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1592 case MOUSE_BUTTON_EVENT:
1594 /* Mouse button pressed. */
1595 if (event & MOUSE_BUTTON1DOWN)
1596 vt_mouse_terminput_button(vd, 0);
1597 if (event & MOUSE_BUTTON2DOWN)
1598 vt_mouse_terminput_button(vd, 1);
1599 if (event & MOUSE_BUTTON3DOWN)
1600 vt_mouse_terminput_button(vd, 2);
1602 /* Mouse button released. */
1603 vt_mouse_terminput_button(vd, 3);
1607 case MOUSE_MOTION_EVENT:
1608 if (mouse->u.data.z < 0) {
1610 sc_mouse_input_button(vd, 64);
1611 } else if (mouse->u.data.z > 0) {
1613 sc_mouse_input_button(vd, 65);
1626 len = VD_PASTEBUFLEN(main_vd);
1627 buf = VD_PASTEBUF(main_vd);
1628 len /= sizeof(term_char_t);
1629 for (i = 0; i < len; i++) {
1632 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1638 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1640 struct vt_device *vd;
1641 struct vt_window *vw;
1647 vw = vd->vd_curwindow;
1651 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1653 * Either the mouse is disabled, or the window is in
1654 * "graphics mode". The graphics mode is usually set by
1655 * an X server, using the KDSETMODE ioctl.
1659 if (vf == NULL) /* Text mode. */
1663 * TODO: add flag about pointer position changed, to not redraw chars
1664 * under mouse pointer when nothing changed.
1667 if (vw->vw_mouse_level > 0)
1668 vt_mouse_terminput(vd, type, x, y, event, cnt);
1672 case MOUSE_MOTION_EVENT:
1677 vt_termsize(vd, vf, &size);
1682 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1683 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1687 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1688 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1689 vd->vd_mx / vf->vf_width,
1690 vd->vd_my / vf->vf_height);
1692 vt_resume_flush_timer(vw->vw_device, 0);
1694 case MOUSE_BUTTON_EVENT:
1702 case MOUSE_BUTTON1DOWN:
1705 mark = VTB_MARK_END;
1707 case 1: /* single click: start cut operation */
1708 mark = VTB_MARK_START;
1710 case 2: /* double click: cut a word */
1711 mark = VTB_MARK_WORD;
1713 case 3: /* triple click: cut a line */
1714 mark = VTB_MARK_ROW;
1718 case VT_MOUSE_PASTEBUTTON:
1727 case VT_MOUSE_EXTENDBUTTON:
1730 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1731 mark = VTB_MARK_EXTEND;
1736 mark = VTB_MARK_EXTEND;
1744 /* Save buttons state. */
1746 vd->vd_mstate |= event;
1748 vd->vd_mstate &= ~event;
1750 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1751 vd->vd_my / vf->vf_height) == 1) {
1753 * We have something marked to copy, so update pointer to
1754 * window with selection.
1756 vt_resume_flush_timer(vw->vw_device, 0);
1762 case VTB_MARK_EXTEND:
1765 /* Other types of mark do not require to copy data. */
1769 /* Get current selection size in bytes. */
1770 len = vtbuf_get_marked_len(&vw->vw_buf);
1774 /* Reallocate buffer only if old one is too small. */
1775 if (len > VD_PASTEBUFSZ(vd)) {
1776 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1778 /* Update buffer size. */
1779 VD_PASTEBUFSZ(vd) = len;
1781 /* Request copy/paste buffer data, no more than `len' */
1782 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1785 VD_PASTEBUFLEN(vd) = len;
1787 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1792 vt_mouse_state(int show)
1794 struct vt_device *vd;
1795 struct vt_window *vw;
1798 vw = vd->vd_curwindow;
1802 vw->vw_flags |= VWF_MOUSE_HIDE;
1805 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1809 /* Mark mouse position as dirty. */
1810 vt_mark_mouse_position_as_dirty(vd);
1811 vt_resume_flush_timer(vw->vw_device, 0);
1816 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1817 int nprot, vm_memattr_t *memattr)
1819 struct vt_window *vw = tm->tm_softc;
1820 struct vt_device *vd = vw->vw_device;
1822 if (vd->vd_driver->vd_fb_mmap)
1823 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1830 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1833 struct vt_window *vw = tm->tm_softc;
1834 struct vt_device *vd = vw->vw_device;
1837 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1838 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1849 cmd = VT_WAITACTIVE;
1875 ival = IOCPARM_IVAL(data);
1876 data = (caddr_t)&ival;
1881 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1882 if (*(int *)data & ~0x7f)
1887 case GIO_DEADKEYMAP:
1888 case PIO_DEADKEYMAP:
1893 case KDSKBSTATE: /* set keyboard state (locks) */
1894 case KDGKBSTATE: /* get keyboard state (locks) */
1895 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1896 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1897 case KDSETLED: /* set keyboard LED status */
1898 case KDGETLED: /* get keyboard LED status */
1899 case KBADDKBD: /* add/remove keyboard to/from mux */
1904 kbd = kbd_get_keyboard(vd->vd_keyboard);
1906 error = kbdd_ioctl(kbd, cmd, data);
1908 if (error == ENOIOCTL) {
1909 if (cmd == KDGKBTYPE) {
1910 /* always return something? XXX */
1922 kbd = kbd_get_keyboard(vd->vd_keyboard);
1924 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1927 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1928 *(int *)data = mode;
1934 mode = *(int *)data;
1939 vw->vw_kbdmode = mode;
1940 if (vw == vd->vd_curwindow) {
1945 kbd = kbd_get_keyboard(vd->vd_keyboard);
1947 error = kbdd_ioctl(kbd, KDSKBMODE,
1958 case FBIO_GETWINORG: /* get frame buffer window origin */
1959 case FBIO_GETDISPSTART: /* get display start address */
1960 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1961 case FBIO_BLANK: /* blank display */
1962 if (vd->vd_driver->vd_fb_ioctl)
1963 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1965 case CONS_BLANKTIME:
1970 *(int *)data = M_CG640x480;
1972 case CONS_BELLTYPE: /* set bell type sound */
1973 if ((*(int *)data) & CONS_QUIET_BELL)
1974 vd->vd_flags |= VDF_QUIET_BELL;
1976 vd->vd_flags &= ~VDF_QUIET_BELL;
1978 case CONS_GETINFO: {
1979 vid_info_t *vi = (vid_info_t *)data;
1981 vi->m_num = vd->vd_curwindow->vw_number + 1;
1982 /* XXX: other fields! */
1986 *(int *)data = 0x200;
1991 case CONS_MOUSECTL: {
1992 mouse_info_t *mouse = (mouse_info_t*)data;
1995 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
1996 * should not be applied to individual TTYs, but only to
1999 switch (mouse->operation) {
2001 if (vd->vd_flags & VDF_MOUSECURSOR) {
2002 vd->vd_flags &= ~VDF_MOUSECURSOR;
2003 #ifndef SC_NO_CUTPASTE
2004 vt_mouse_state(VT_MOUSE_HIDE);
2009 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2010 vd->vd_flags |= VDF_MOUSECURSOR;
2011 vd->vd_mx = vd->vd_width / 2;
2012 vd->vd_my = vd->vd_height / 2;
2013 #ifndef SC_NO_CUTPASTE
2014 vt_mouse_state(VT_MOUSE_SHOW);
2025 error = vtfont_load((void *)data, &vf);
2029 error = vt_change_font(vw, vf);
2034 scrmap_t *sm = (scrmap_t *)data;
2036 /* We don't have screen maps, so return a handcrafted one. */
2037 for (i = 0; i < 256; i++)
2043 * FIXME: This implementation is incomplete compared to
2046 switch (*(int *)data) {
2050 vw->vw_flags &= ~VWF_GRAPHICS;
2053 vw->vw_flags |= VWF_GRAPHICS;
2057 case KDENABIO: /* allow io operations */
2058 error = priv_check(td, PRIV_IO);
2061 error = securelevel_gt(td->td_ucred, 0);
2064 #if defined(__i386__)
2065 td->td_frame->tf_eflags |= PSL_IOPL;
2066 #elif defined(__amd64__)
2067 td->td_frame->tf_rflags |= PSL_IOPL;
2070 case KDDISABIO: /* disallow io operations (default) */
2071 #if defined(__i386__)
2072 td->td_frame->tf_eflags &= ~PSL_IOPL;
2073 #elif defined(__amd64__)
2074 td->td_frame->tf_rflags &= ~PSL_IOPL;
2077 case KDMKTONE: /* sound the bell */
2078 vtterm_beep(tm, *(u_int *)data);
2080 case KIOCSOUND: /* make tone (*data) hz */
2083 case CONS_SETKBD: /* set the new keyboard */
2086 if (vd->vd_keyboard != *(int *)data) {
2087 kbd = kbd_get_keyboard(*(int *)data);
2092 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2093 (void *)vd, vt_kbdevent, vd);
2095 if (vd->vd_keyboard != -1) {
2096 kbd_release(kbd, (void *)vd);
2098 kbd = kbd_get_keyboard(i);
2099 vd->vd_keyboard = i;
2101 (void)kbdd_ioctl(kbd, KDSKBMODE,
2102 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
2104 error = EPERM; /* XXX */
2109 case CONS_RELKBD: /* release the current keyboard */
2112 if (vd->vd_keyboard != -1) {
2113 kbd = kbd_get_keyboard(vd->vd_keyboard);
2118 error = kbd_release(kbd, (void *)vd);
2120 vd->vd_keyboard = -1;
2127 win = *(int *)data - 1;
2128 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2130 if ((win >= VT_MAXWINDOWS) || (win < 0))
2132 return (vt_proc_window_switch(vd->vd_windows[win]));
2135 *(int *)data = vd->vd_curwindow->vw_number + 1;
2138 *(int *)data = vw->vw_number + 1;
2141 /* TODO: Check current state, switching can be in progress. */
2142 if ((*(int *)data) == 0x01)
2143 vw->vw_flags |= VWF_VTYLOCK;
2144 else if ((*(int *)data) == 0x02)
2145 vw->vw_flags &= ~VWF_VTYLOCK;
2151 for (i = 0; i < VT_MAXWINDOWS; i++) {
2152 vw = vd->vd_windows[i];
2155 if (!(vw->vw_flags & VWF_OPENED)) {
2156 *(int *)data = vw->vw_number + 1;
2166 i = *(unsigned int *)data;
2167 if (i > VT_MAXWINDOWS)
2170 vw = vd->vd_windows[i - 1];
2173 while (vd->vd_curwindow != vw && error == 0)
2174 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2177 case VT_SETMODE: { /* set screen switcher mode */
2178 struct vt_mode *mode;
2181 mode = (struct vt_mode *)data;
2182 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2183 if (vw->vw_smode.mode == VT_PROCESS) {
2184 p1 = pfind(vw->vw_pid);
2185 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2188 DPRINTF(5, "error EPERM\n");
2194 if (mode->mode == VT_AUTO) {
2195 vw->vw_smode.mode = VT_AUTO;
2198 DPRINTF(5, "VT_AUTO, ");
2199 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2200 cnavailable(vw->vw_terminal->consdev, TRUE);
2201 /* were we in the middle of the vty switching process? */
2202 if (finish_vt_rel(vw, TRUE, &s) == 0)
2203 DPRINTF(5, "reset WAIT_REL, ");
2204 if (finish_vt_acq(vw) == 0)
2205 DPRINTF(5, "reset WAIT_ACQ, ");
2207 } else if (mode->mode == VT_PROCESS) {
2208 if (!ISSIGVALID(mode->relsig) ||
2209 !ISSIGVALID(mode->acqsig) ||
2210 !ISSIGVALID(mode->frsig)) {
2211 DPRINTF(5, "error EINVAL\n");
2214 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2215 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2216 vw->vw_proc = td->td_proc;
2217 vw->vw_pid = vw->vw_proc->p_pid;
2218 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2219 cnavailable(vw->vw_terminal->consdev, FALSE);
2221 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2228 case VT_GETMODE: /* get screen switcher mode */
2229 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2232 case VT_RELDISP: /* screen switcher ioctl */
2234 * This must be the current vty which is in the VT_PROCESS
2237 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2241 /* ...and this process is controlling it. */
2242 if (vw->vw_proc != td->td_proc) {
2246 switch(*(int *)data) {
2247 case VT_FALSE: /* user refuses to release screen, abort */
2248 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2249 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2250 SC_DRIVER_NAME, VT_UNIT(vw));
2252 case VT_TRUE: /* user has released screen, go on */
2253 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2254 if (vw->vw_flags & VWF_SWWAIT_REL) {
2255 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2256 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2257 SC_DRIVER_NAME, VT_UNIT(vw));
2262 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2263 if ((error = finish_vt_acq(vw)) == 0)
2264 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2265 SC_DRIVER_NAME, VT_UNIT(vw));
2276 static struct vt_window *
2277 vt_allocate_window(struct vt_device *vd, unsigned int window)
2279 struct vt_window *vw;
2280 struct terminal *tm;
2284 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2286 vw->vw_number = window;
2287 vw->vw_kbdmode = K_XLATE;
2289 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2290 vw->vw_font = vtfont_ref(&vt_font_default);
2291 vt_compute_drawable_area(vw);
2294 vt_termsize(vd, vw->vw_font, &size);
2295 vt_winsize(vd, vw->vw_font, &wsz);
2296 vtbuf_init(&vw->vw_buf, &size);
2298 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2299 terminal_set_winsize(tm, &wsz);
2300 vd->vd_windows[window] = vw;
2301 callout_init(&vw->vw_proc_dead_timer, 0);
2307 vt_upgrade(struct vt_device *vd)
2309 struct vt_window *vw;
2312 if (!vty_enabled(VTY_VT))
2315 for (i = 0; i < VT_MAXWINDOWS; i++) {
2316 vw = vd->vd_windows[i];
2319 vw = vt_allocate_window(vd, i);
2321 if (!(vw->vw_flags & VWF_READY)) {
2322 callout_init(&vw->vw_proc_dead_timer, 0);
2323 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2324 vw->vw_flags |= VWF_READY;
2325 if (vw->vw_flags & VWF_CONSOLE) {
2326 /* For existing console window. */
2327 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2328 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2334 if (vd->vd_curwindow == NULL)
2335 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2337 if (!(vd->vd_flags & VDF_ASYNC)) {
2338 /* Attach keyboard. */
2339 vt_allocate_keyboard(vd);
2341 /* Init 25 Hz timer. */
2342 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2344 /* Start timer when everything ready. */
2345 vd->vd_flags |= VDF_ASYNC;
2346 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2347 vd->vd_timer_armed = 1;
2352 /* Refill settings with new sizes. */
2357 vt_resize(struct vt_device *vd)
2359 struct vt_window *vw;
2362 for (i = 0; i < VT_MAXWINDOWS; i++) {
2363 vw = vd->vd_windows[i];
2365 /* Assign default font to window, if not textmode. */
2366 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2367 vw->vw_font = vtfont_ref(&vt_font_default);
2370 /* Resize terminal windows */
2371 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2372 DPRINTF(100, "%s: vt_change_font() is busy, "
2373 "window %d\n", __func__, i);
2379 vt_allocate(struct vt_driver *drv, void *softc)
2381 struct vt_device *vd;
2383 if (!vty_enabled(VTY_VT))
2386 if (main_vd->vd_driver == NULL) {
2387 main_vd->vd_driver = drv;
2388 printf("VT: initialize with new VT driver \"%s\".\n",
2392 * Check if have rights to replace current driver. For example:
2393 * it is bad idea to replace KMS driver with generic VGA one.
2395 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2396 printf("VT: Driver priority %d too low. Current %d\n ",
2397 drv->vd_priority, main_vd->vd_driver->vd_priority);
2400 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2401 main_vd->vd_driver->vd_name, drv->vd_name);
2405 if (vd->vd_flags & VDF_ASYNC) {
2406 /* Stop vt_flush periodic task. */
2407 vt_suspend_flush_timer(vd);
2409 * Mute current terminal until we done. vt_change_font (called
2410 * from vt_resize) will unmute it.
2412 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2416 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2420 vd->vd_flags &= ~VDF_TEXTMODE;
2422 vd->vd_driver = drv;
2423 vd->vd_softc = softc;
2424 vd->vd_driver->vd_init(vd);
2427 /* Update windows sizes and initialize last items. */
2431 if (vd->vd_flags & VDF_SPLASH)
2435 if (vd->vd_flags & VDF_ASYNC) {
2436 /* Allow to put chars now. */
2437 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2438 /* Rerun timer for screen updates. */
2439 vt_resume_flush_timer(vd, 0);
2443 * Register as console. If it already registered, cnadd() will ignore
2446 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2453 if (vt_suspendswitch == 0)
2455 /* Save current window. */
2456 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2457 /* Ask holding process to free window and switch to console window */
2458 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2465 if (vt_suspendswitch == 0)
2467 /* Switch back to saved window */
2468 if (main_vd->vd_savedwindow != NULL)
2469 vt_proc_window_switch(main_vd->vd_savedwindow);
2470 main_vd->vd_savedwindow = NULL;