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 * Offset changed, please update Nth lines on screen.
489 * +N - Nth lines at top;
490 * -N - Nth lines at bottom.
493 if (diff < -size.tp_row || diff > size.tp_row) {
494 vw->vw_device->vd_flags |= VDF_INVALID;
495 vt_resume_flush_timer(vw->vw_device, 0);
498 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/
499 vt_resume_flush_timer(vw->vw_device, 0);
503 vt_machine_kbdevent(int c)
507 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
509 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
511 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
513 shutdown_nice(RB_HALT);
515 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
516 #ifndef SC_NO_CUTPASTE
517 /* Insert text from cut-paste buffer. */
521 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
523 shutdown_nice(RB_HALT|RB_POWEROFF);
525 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
527 * Request to immediate panic if sysctl
528 * kern.vt.enable_panic_key allow it.
531 panic("Forced by the panic key");
533 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
535 shutdown_nice(RB_AUTOBOOT);
537 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
538 /* Force activatation/deactivation of the screen saver. */
541 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
542 /* Put machine into Stand-By mode. */
543 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
545 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
546 /* Suspend machine. */
547 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
555 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
557 struct vt_device *vd;
561 /* Only special keys handled in ScrollLock mode */
562 if ((c & SPCLKEY) == 0)
568 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
569 vw = vd->vd_windows[c - F_SCR];
571 vt_proc_window_switch(vw);
579 /* Turn scrolling off. */
580 vt_scroll(vw, 0, VHS_END);
581 VTBUF_SLCK_DISABLE(&vw->vw_buf);
582 vw->vw_flags &= ~VWF_SCROLL;
585 case FKEY | F(49): /* Home key. */
586 vt_scroll(vw, 0, VHS_SET);
588 case FKEY | F(50): /* Arrow up. */
589 vt_scroll(vw, -1, VHS_CUR);
591 case FKEY | F(51): /* Page up. */
592 vt_termsize(vd, vw->vw_font, &size);
593 vt_scroll(vw, -size.tp_row, VHS_CUR);
595 case FKEY | F(57): /* End key. */
596 vt_scroll(vw, 0, VHS_END);
598 case FKEY | F(58): /* Arrow down. */
599 vt_scroll(vw, 1, VHS_CUR);
601 case FKEY | F(59): /* Page down. */
602 vt_termsize(vd, vw->vw_font, &size);
603 vt_scroll(vw, size.tp_row, VHS_CUR);
612 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
614 struct vt_window *vw = vd->vd_curwindow;
617 #if VT_ALT_TO_ESC_HACK
619 switch (c & ~RELKEY) {
620 case (SPCLKEY | RALT):
621 if (vt_enable_altgr != 0)
623 case (SPCLKEY | LALT):
624 vd->vd_kbstate &= ~ALKED;
626 /* Other keys ignored for RELKEY event. */
629 switch (c & ~RELKEY) {
630 case (SPCLKEY | RALT):
631 if (vt_enable_altgr != 0)
633 case (SPCLKEY | LALT):
634 vd->vd_kbstate |= ALKED;
639 /* Other keys ignored for RELKEY event. */
643 if (vt_machine_kbdevent(c))
646 if (vw->vw_flags & VWF_SCROLL) {
647 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
648 /* Scroll mode keys handled, nothing to do more. */
655 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
656 vw = vd->vd_windows[c - F_SCR];
658 vt_proc_window_switch(vw);
664 /* Switch to next VT. */
665 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
666 vw = vd->vd_windows[c];
668 vt_proc_window_switch(vw);
671 /* Switch to previous VT. */
672 c = (vw->vw_number - 1) % VT_MAXWINDOWS;
673 vw = vd->vd_windows[c];
675 vt_proc_window_switch(vw);
679 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
682 /* Turn scrolling on. */
683 vw->vw_flags |= VWF_SCROLL;
684 VTBUF_SLCK_ENABLE(&vw->vw_buf);
686 /* Turn scrolling off. */
687 vw->vw_flags &= ~VWF_SCROLL;
688 VTBUF_SLCK_DISABLE(&vw->vw_buf);
689 vt_scroll(vw, 0, VHS_END);
694 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
695 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
696 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
697 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
698 /* F1 through F12 keys. */
699 terminal_input_special(vw->vw_terminal,
700 TKEY_F1 + c - (FKEY | F(1)));
702 case FKEY | F(49): /* Home key. */
703 terminal_input_special(vw->vw_terminal, TKEY_HOME);
705 case FKEY | F(50): /* Arrow up. */
706 terminal_input_special(vw->vw_terminal, TKEY_UP);
708 case FKEY | F(51): /* Page up. */
709 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
711 case FKEY | F(53): /* Arrow left. */
712 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
714 case FKEY | F(55): /* Arrow right. */
715 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
717 case FKEY | F(57): /* End key. */
718 terminal_input_special(vw->vw_terminal, TKEY_END);
720 case FKEY | F(58): /* Arrow down. */
721 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
723 case FKEY | F(59): /* Page down. */
724 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
726 case FKEY | F(60): /* Insert key. */
727 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
729 case FKEY | F(61): /* Delete key. */
730 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
733 } else if (KEYFLAGS(c) == 0) {
734 /* Don't do UTF-8 conversion when doing raw mode. */
735 if (vw->vw_kbdmode == K_XLATE) {
736 #if VT_ALT_TO_ESC_HACK
737 if (vd->vd_kbstate & ALKED) {
739 * Prepend ESC sequence if one of ALT keys down.
741 terminal_input_char(vw->vw_terminal, 0x1b);
745 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
747 terminal_input_raw(vw->vw_terminal, c);
753 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
755 struct vt_device *vd = arg;
761 case KBDIO_UNLOADING:
763 vd->vd_keyboard = -1;
764 kbd_release(kbd, (void *)vd);
771 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
772 vt_processkey(kbd, vd, c);
778 vt_allocate_keyboard(struct vt_device *vd)
784 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
785 vd->vd_keyboard = idx0;
787 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
788 k0 = kbd_get_keyboard(idx0);
790 for (idx = kbd_find_keyboard2("*", -1, 0);
792 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
793 k = kbd_get_keyboard(idx);
795 if (idx == idx0 || KBD_IS_BUSY(k))
798 bzero(&ki, sizeof(ki));
799 strcpy(ki.kb_name, k->kb_name);
800 ki.kb_unit = k->kb_unit;
802 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
805 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
806 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
808 DPRINTF(10, "%s: No keyboard found.\n", __func__);
812 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
818 vtterm_bell(struct terminal *tm)
820 struct vt_window *vw = tm->tm_softc;
821 struct vt_device *vd = vw->vw_device;
823 if (vd->vd_flags & VDF_QUIET_BELL)
826 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
830 vtterm_beep(struct terminal *tm, u_int param)
834 if ((param == 0) || ((param & 0xffff) == 0)) {
839 period = ((param >> 16) & 0xffff) * hz / 1000;
840 freq = 1193182 / (param & 0xffff);
842 sysbeep(freq, period);
846 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
848 struct vt_window *vw = tm->tm_softc;
850 vtbuf_cursor_position(&vw->vw_buf, p);
851 vt_resume_flush_timer(vw->vw_device, 0);
855 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
857 struct vt_window *vw = tm->tm_softc;
859 vtbuf_putchar(&vw->vw_buf, p, c);
860 vt_resume_flush_timer(vw->vw_device, 0);
864 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
866 struct vt_window *vw = tm->tm_softc;
868 vtbuf_fill_locked(&vw->vw_buf, r, c);
869 vt_resume_flush_timer(vw->vw_device, 0);
873 vtterm_copy(struct terminal *tm, const term_rect_t *r,
876 struct vt_window *vw = tm->tm_softc;
878 vtbuf_copy(&vw->vw_buf, r, p);
879 vt_resume_flush_timer(vw->vw_device, 0);
883 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
885 struct vt_window *vw = tm->tm_softc;
889 vtbuf_cursor_visibility(&vw->vw_buf, arg);
890 vt_resume_flush_timer(vw->vw_device, 0);
893 vw->vw_mouse_level = arg;
899 vt_determine_colors(term_char_t c, int cursor,
900 term_color_t *fg, term_color_t *bg)
907 *fg = TCHAR_FGCOLOR(c);
908 if (TCHAR_FORMAT(c) & TF_BOLD)
909 *fg = TCOLOR_LIGHT(*fg);
910 *bg = TCHAR_BGCOLOR(c);
912 if (TCHAR_FORMAT(c) & TF_REVERSE)
924 #ifndef SC_NO_CUTPASTE
926 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
928 unsigned int mx, my, x1, y1, x2, y2;
931 * We use the cursor position saved during the current refresh,
932 * in case the cursor moved since.
934 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
935 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
937 x1 = area->tr_begin.tp_col;
938 y1 = area->tr_begin.tp_row;
939 x2 = area->tr_end.tp_col;
940 y2 = area->tr_end.tp_row;
942 if (((mx >= x1 && x2 - 1 >= mx) ||
943 (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
944 ((my >= y1 && y2 - 1 >= my) ||
945 (my < y1 && my + vd->vd_mcursor->height >= y1)))
952 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
955 struct vt_window *vw;
959 vw = vd->vd_curwindow;
966 area.tr_begin.tp_col = x / vf->vf_width;
967 area.tr_begin.tp_row = y / vf->vf_height;
969 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
971 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
974 * No font loaded (ie. vt_vga operating in textmode).
976 * FIXME: This fake area needs to be revisited once the
977 * mouse cursor is supported in vt_vga's textmode.
979 area.tr_begin.tp_col = x;
980 area.tr_begin.tp_row = y;
981 area.tr_end.tp_col = x + 2;
982 area.tr_end.tp_row = y + 2;
985 vtbuf_dirty(&vw->vw_buf, &area);
990 vt_flush(struct vt_device *vd)
992 struct vt_window *vw;
994 struct vt_bufmask tmask;
997 #ifndef SC_NO_CUTPASTE
998 int cursor_was_shown, cursor_moved;
1001 vw = vd->vd_curwindow;
1005 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1009 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1012 #ifndef SC_NO_CUTPASTE
1013 cursor_was_shown = vd->vd_mshown;
1014 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1015 vd->vd_my != vd->vd_my_drawn);
1017 /* Check if the cursor should be displayed or not. */
1018 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1019 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1020 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1027 * If the cursor changed display state or moved, we must mark
1028 * the old position as dirty, so that it's erased.
1030 if (cursor_was_shown != vd->vd_mshown ||
1031 (vd->vd_mshown && cursor_moved))
1032 vt_mark_mouse_position_as_dirty(vd);
1035 * Save position of the mouse cursor. It's used by backends to
1036 * know where to draw the cursor and during the next refresh to
1037 * erase the previous position.
1039 vd->vd_mx_drawn = vd->vd_mx;
1040 vd->vd_my_drawn = vd->vd_my;
1043 * If the cursor is displayed and has moved since last refresh,
1044 * mark the new position as dirty.
1046 if (vd->vd_mshown && cursor_moved)
1047 vt_mark_mouse_position_as_dirty(vd);
1050 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask);
1051 vt_termsize(vd, vf, &size);
1053 /* Force a full redraw when the screen contents are invalid. */
1054 if (vd->vd_flags & VDF_INVALID) {
1055 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1056 tarea.tr_end = size;
1057 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY;
1059 vd->vd_flags &= ~VDF_INVALID;
1062 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1063 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1073 struct vt_device *vd;
1077 /* Update screen if required. */
1078 changed = vt_flush(vd);
1080 /* Schedule for next update. */
1082 vt_schedule_flush(vd, 0);
1084 vd->vd_timer_armed = 0;
1088 vtterm_done(struct terminal *tm)
1090 struct vt_window *vw = tm->tm_softc;
1091 struct vt_device *vd = vw->vw_device;
1093 if (kdb_active || panicstr != NULL) {
1094 /* Switch to the debugger. */
1095 if (vd->vd_curwindow != vw) {
1096 vd->vd_curwindow = vw;
1097 vd->vd_flags |= VDF_INVALID;
1098 if (vd->vd_driver->vd_postswitch)
1099 vd->vd_driver->vd_postswitch(vd);
1101 vd->vd_flags &= ~VDF_SPLASH;
1103 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1110 vtterm_splash(struct vt_device *vd)
1112 vt_axis_t top, left;
1114 /* Display a nice boot splash. */
1115 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1117 top = (vd->vd_height - vt_logo_height) / 2;
1118 left = (vd->vd_width - vt_logo_width) / 2;
1119 switch (vt_logo_depth) {
1121 /* XXX: Unhardcode colors! */
1122 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1123 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1124 left, top, TC_WHITE, TC_BLACK);
1126 vd->vd_flags |= VDF_SPLASH;
1133 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1135 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1136 struct vt_window *vw = tm->tm_softc;
1137 struct vt_device *vd = vw->vw_device;
1142 if (!vty_enabled(VTY_VT))
1145 if (vd->vd_flags & VDF_INITIALIZED)
1146 /* Initialization already done. */
1149 SET_FOREACH(vtdlist, vt_drv_set) {
1151 if (vtd->vd_probe == NULL)
1153 if (vtd->vd_probe(vd) == CN_DEAD)
1155 if ((vtdbest == NULL) ||
1156 (vtd->vd_priority > vtdbest->vd_priority))
1159 if (vtdbest == NULL) {
1160 cp->cn_pri = CN_DEAD;
1161 vd->vd_flags |= VDF_DEAD;
1163 vd->vd_driver = vtdbest;
1164 cp->cn_pri = vd->vd_driver->vd_init(vd);
1167 /* Check if driver's vt_init return CN_DEAD. */
1168 if (cp->cn_pri == CN_DEAD) {
1169 vd->vd_flags |= VDF_DEAD;
1172 /* Initialize any early-boot keyboard drivers */
1173 kbd_configure(KB_CONF_PROBE_ONLY);
1175 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1176 vd->vd_windows[VT_CONSWINDOW] = vw;
1177 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1179 /* Attach default font if not in TEXTMODE. */
1180 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1181 vw->vw_font = vtfont_ref(&vt_font_default);
1182 vt_compute_drawable_area(vw);
1185 vtbuf_init_early(&vw->vw_buf);
1186 vt_winsize(vd, vw->vw_font, &wsz);
1187 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1189 attr.ta_format = TCHAR_FORMAT(c);
1190 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1191 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1192 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1194 if (vtdbest != NULL) {
1198 vd->vd_flags |= VDF_INITIALIZED;
1203 vtterm_cngetc(struct terminal *tm)
1205 struct vt_window *vw = tm->tm_softc;
1206 struct vt_device *vd = vw->vw_device;
1211 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1212 return (*vw->vw_kbdsq++);
1215 /* Make sure the splash screen is not there. */
1216 if (vd->vd_flags & VDF_SPLASH) {
1218 vd->vd_flags &= ~VDF_SPLASH;
1219 /* Mark screen as invalid to force update */
1220 vd->vd_flags |= VDF_INVALID;
1224 /* Stripped down keyboard handler. */
1225 kbd = kbd_get_keyboard(vd->vd_keyboard);
1229 /* Force keyboard input mode to K_XLATE */
1231 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c);
1233 /* Switch the keyboard to polling to make it work here. */
1234 kbdd_poll(kbd, TRUE);
1235 c = kbdd_read_char(kbd, 0);
1236 kbdd_poll(kbd, FALSE);
1240 if (vw->vw_flags & VWF_SCROLL) {
1241 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1246 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1250 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
1251 if (state & SLKED) {
1252 /* Turn scrolling on. */
1253 vw->vw_flags |= VWF_SCROLL;
1254 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1256 /* Turn scrolling off. */
1257 vt_scroll(vw, 0, VHS_END);
1258 vw->vw_flags &= ~VWF_SCROLL;
1259 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1262 /* XXX: KDB can handle history. */
1263 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1264 vw->vw_kbdsq = "\x1b[A";
1266 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1267 vw->vw_kbdsq = "\x1b[B";
1269 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1270 vw->vw_kbdsq = "\x1b[C";
1272 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1273 vw->vw_kbdsq = "\x1b[D";
1277 /* Force refresh to make scrollback work. */
1279 } else if (KEYFLAGS(c) == 0) {
1280 return (KEYCHAR(c));
1283 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1284 return (*vw->vw_kbdsq++);
1290 vtterm_cngrab(struct terminal *tm)
1292 struct vt_device *vd;
1293 struct vt_window *vw;
1300 vt_window_switch(vw);
1302 kbd = kbd_get_keyboard(vd->vd_keyboard);
1306 if (vw->vw_grabbed++ > 0)
1310 * Make sure the keyboard is accessible even when the kbd device
1311 * driver is disabled.
1315 /* We shall always use the keyboard in the XLATE mode here. */
1316 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1317 vw->vw_kbdmode = K_XLATE;
1318 (void)kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
1320 kbdd_poll(kbd, TRUE);
1324 vtterm_cnungrab(struct terminal *tm)
1326 struct vt_device *vd;
1327 struct vt_window *vw;
1333 kbd = kbd_get_keyboard(vd->vd_keyboard);
1337 if (--vw->vw_grabbed > 0)
1340 kbdd_poll(kbd, FALSE);
1342 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1343 (void)kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
1348 vtterm_opened(struct terminal *tm, int opened)
1350 struct vt_window *vw = tm->tm_softc;
1351 struct vt_device *vd = vw->vw_device;
1354 vd->vd_flags &= ~VDF_SPLASH;
1356 vw->vw_flags |= VWF_OPENED;
1358 vw->vw_flags &= ~VWF_OPENED;
1359 /* TODO: finish ACQ/REL */
1365 vt_set_border(struct vt_window *vw, term_color_t c)
1367 struct vt_device *vd = vw->vw_device;
1369 if (vd->vd_driver->vd_drawrect == NULL)
1373 if (vw->vw_draw_area.tr_begin.tp_row > 0)
1374 vd->vd_driver->vd_drawrect(vd,
1376 vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1380 if (vw->vw_draw_area.tr_begin.tp_col > 0)
1381 vd->vd_driver->vd_drawrect(vd,
1383 vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1387 if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1388 vd->vd_driver->vd_drawrect(vd,
1389 vw->vw_draw_area.tr_end.tp_col - 1, 0,
1390 vd->vd_width - 1, vd->vd_height - 1,
1394 if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1395 vd->vd_driver->vd_drawrect(vd,
1396 0, vw->vw_draw_area.tr_end.tp_row - 1,
1397 vd->vd_width - 1, vd->vd_height - 1,
1404 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1406 struct vt_device *vd = vw->vw_device;
1407 struct terminal *tm = vw->vw_terminal;
1414 * Changing fonts is a little tricky. We must prevent
1415 * simultaneous access to the device, so we must stop
1416 * the display timer and the terminal from accessing.
1417 * We need to switch fonts and grow our screen buffer.
1419 * XXX: Right now the code uses terminal_mute() to
1420 * prevent data from reaching the console driver while
1421 * resizing the screen buffer. This isn't elegant...
1425 if (vw->vw_flags & VWF_BUSY) {
1426 /* Another process is changing the font. */
1430 vw->vw_flags |= VWF_BUSY;
1433 vt_termsize(vd, vf, &size);
1434 vt_winsize(vd, vf, &wsz);
1436 /* Grow the screen buffer and terminal. */
1437 terminal_mute(tm, 1);
1438 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1439 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1440 terminal_mute(tm, 0);
1442 /* Actually apply the font to the current window. */
1444 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1446 * In case vt_change_font called to update size we don't need
1447 * to update font link.
1449 vtfont_unref(vw->vw_font);
1450 vw->vw_font = vtfont_ref(vf);
1454 * Compute the drawable area and move the mouse cursor inside
1455 * it, in case the new area is smaller than the previous one.
1457 vt_compute_drawable_area(vw);
1458 vd->vd_mx = min(vd->vd_mx,
1459 vw->vw_draw_area.tr_end.tp_col -
1460 vw->vw_draw_area.tr_begin.tp_col - 1);
1461 vd->vd_my = min(vd->vd_my,
1462 vw->vw_draw_area.tr_end.tp_row -
1463 vw->vw_draw_area.tr_begin.tp_row - 1);
1465 /* Force a full redraw the next timer tick. */
1466 if (vd->vd_curwindow == vw) {
1467 vt_set_border(vw, TC_BLACK);
1468 vd->vd_flags |= VDF_INVALID;
1469 vt_resume_flush_timer(vw->vw_device, 0);
1471 vw->vw_flags &= ~VWF_BUSY;
1477 vt_proc_alive(struct vt_window *vw)
1481 if (vw->vw_smode.mode != VT_PROCESS)
1485 if ((p = pfind(vw->vw_pid)) != NULL)
1487 if (vw->vw_proc == p)
1490 vw->vw_smode.mode = VT_AUTO;
1491 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1498 signal_vt_rel(struct vt_window *vw)
1501 if (vw->vw_smode.mode != VT_PROCESS)
1503 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1508 vw->vw_flags |= VWF_SWWAIT_REL;
1509 PROC_LOCK(vw->vw_proc);
1510 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1511 PROC_UNLOCK(vw->vw_proc);
1512 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1517 signal_vt_acq(struct vt_window *vw)
1520 if (vw->vw_smode.mode != VT_PROCESS)
1522 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1523 cnavailable(vw->vw_terminal->consdev, FALSE);
1524 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1529 vw->vw_flags |= VWF_SWWAIT_ACQ;
1530 PROC_LOCK(vw->vw_proc);
1531 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1532 PROC_UNLOCK(vw->vw_proc);
1533 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1538 finish_vt_rel(struct vt_window *vw, int release, int *s)
1541 if (vw->vw_flags & VWF_SWWAIT_REL) {
1542 vw->vw_flags &= ~VWF_SWWAIT_REL;
1544 callout_drain(&vw->vw_proc_dead_timer);
1545 vt_late_window_switch(vw->vw_switch_to);
1553 finish_vt_acq(struct vt_window *vw)
1556 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1557 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1563 #ifndef SC_NO_CUTPASTE
1565 vt_mouse_terminput_button(struct vt_device *vd, int button)
1567 struct vt_window *vw;
1569 char mouseb[6] = "\x1B[M";
1572 vw = vd->vd_curwindow;
1575 /* Translate to char position. */
1576 x = vd->vd_mx / vf->vf_width;
1577 y = vd->vd_my / vf->vf_height;
1578 /* Avoid overflow. */
1579 x = MIN(x, 255 - '!');
1580 y = MIN(y, 255 - '!');
1582 mouseb[3] = ' ' + button;
1583 mouseb[4] = '!' + x;
1584 mouseb[5] = '!' + y;
1586 for (i = 0; i < sizeof(mouseb); i++)
1587 terminal_input_char(vw->vw_terminal, mouseb[i]);
1591 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1596 case MOUSE_BUTTON_EVENT:
1598 /* Mouse button pressed. */
1599 if (event & MOUSE_BUTTON1DOWN)
1600 vt_mouse_terminput_button(vd, 0);
1601 if (event & MOUSE_BUTTON2DOWN)
1602 vt_mouse_terminput_button(vd, 1);
1603 if (event & MOUSE_BUTTON3DOWN)
1604 vt_mouse_terminput_button(vd, 2);
1606 /* Mouse button released. */
1607 vt_mouse_terminput_button(vd, 3);
1611 case MOUSE_MOTION_EVENT:
1612 if (mouse->u.data.z < 0) {
1614 sc_mouse_input_button(vd, 64);
1615 } else if (mouse->u.data.z > 0) {
1617 sc_mouse_input_button(vd, 65);
1630 len = VD_PASTEBUFLEN(main_vd);
1631 buf = VD_PASTEBUF(main_vd);
1632 len /= sizeof(term_char_t);
1633 for (i = 0; i < len; i++) {
1636 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1642 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1644 struct vt_device *vd;
1645 struct vt_window *vw;
1651 vw = vd->vd_curwindow;
1655 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1657 * Either the mouse is disabled, or the window is in
1658 * "graphics mode". The graphics mode is usually set by
1659 * an X server, using the KDSETMODE ioctl.
1663 if (vf == NULL) /* Text mode. */
1667 * TODO: add flag about pointer position changed, to not redraw chars
1668 * under mouse pointer when nothing changed.
1671 if (vw->vw_mouse_level > 0)
1672 vt_mouse_terminput(vd, type, x, y, event, cnt);
1676 case MOUSE_MOTION_EVENT:
1681 vt_termsize(vd, vf, &size);
1686 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1687 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1691 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1692 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1693 vd->vd_mx / vf->vf_width,
1694 vd->vd_my / vf->vf_height);
1696 vt_resume_flush_timer(vw->vw_device, 0);
1698 case MOUSE_BUTTON_EVENT:
1706 case MOUSE_BUTTON1DOWN:
1709 mark = VTB_MARK_END;
1711 case 1: /* single click: start cut operation */
1712 mark = VTB_MARK_START;
1714 case 2: /* double click: cut a word */
1715 mark = VTB_MARK_WORD;
1717 case 3: /* triple click: cut a line */
1718 mark = VTB_MARK_ROW;
1722 case VT_MOUSE_PASTEBUTTON:
1731 case VT_MOUSE_EXTENDBUTTON:
1734 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1735 mark = VTB_MARK_EXTEND;
1740 mark = VTB_MARK_EXTEND;
1748 /* Save buttons state. */
1750 vd->vd_mstate |= event;
1752 vd->vd_mstate &= ~event;
1754 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1755 vd->vd_my / vf->vf_height) == 1) {
1757 * We have something marked to copy, so update pointer to
1758 * window with selection.
1760 vt_resume_flush_timer(vw->vw_device, 0);
1766 case VTB_MARK_EXTEND:
1769 /* Other types of mark do not require to copy data. */
1773 /* Get current selection size in bytes. */
1774 len = vtbuf_get_marked_len(&vw->vw_buf);
1778 /* Reallocate buffer only if old one is too small. */
1779 if (len > VD_PASTEBUFSZ(vd)) {
1780 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1782 /* Update buffer size. */
1783 VD_PASTEBUFSZ(vd) = len;
1785 /* Request copy/paste buffer data, no more than `len' */
1786 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1789 VD_PASTEBUFLEN(vd) = len;
1791 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1796 vt_mouse_state(int show)
1798 struct vt_device *vd;
1799 struct vt_window *vw;
1802 vw = vd->vd_curwindow;
1806 vw->vw_flags |= VWF_MOUSE_HIDE;
1809 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1813 /* Mark mouse position as dirty. */
1814 vt_mark_mouse_position_as_dirty(vd);
1815 vt_resume_flush_timer(vw->vw_device, 0);
1820 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1821 int nprot, vm_memattr_t *memattr)
1823 struct vt_window *vw = tm->tm_softc;
1824 struct vt_device *vd = vw->vw_device;
1826 if (vd->vd_driver->vd_fb_mmap)
1827 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1834 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1837 struct vt_window *vw = tm->tm_softc;
1838 struct vt_device *vd = vw->vw_device;
1841 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1842 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1853 cmd = VT_WAITACTIVE;
1879 ival = IOCPARM_IVAL(data);
1880 data = (caddr_t)&ival;
1885 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1886 if (*(int *)data & ~0x7f)
1891 case GIO_DEADKEYMAP:
1892 case PIO_DEADKEYMAP:
1897 case KDSKBSTATE: /* set keyboard state (locks) */
1898 case KDGKBSTATE: /* get keyboard state (locks) */
1899 case KDGETREPEAT: /* get keyboard repeat & delay rates */
1900 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
1901 case KDSETLED: /* set keyboard LED status */
1902 case KDGETLED: /* get keyboard LED status */
1903 case KBADDKBD: /* add/remove keyboard to/from mux */
1908 kbd = kbd_get_keyboard(vd->vd_keyboard);
1910 error = kbdd_ioctl(kbd, cmd, data);
1912 if (error == ENOIOCTL) {
1913 if (cmd == KDGKBTYPE) {
1914 /* always return something? XXX */
1926 kbd = kbd_get_keyboard(vd->vd_keyboard);
1928 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode);
1931 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode);
1932 *(int *)data = mode;
1938 mode = *(int *)data;
1943 vw->vw_kbdmode = mode;
1944 if (vw == vd->vd_curwindow) {
1949 kbd = kbd_get_keyboard(vd->vd_keyboard);
1951 error = kbdd_ioctl(kbd, KDSKBMODE,
1962 case FBIO_GETWINORG: /* get frame buffer window origin */
1963 case FBIO_GETDISPSTART: /* get display start address */
1964 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
1965 case FBIO_BLANK: /* blank display */
1966 if (vd->vd_driver->vd_fb_ioctl)
1967 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
1969 case CONS_BLANKTIME:
1974 *(int *)data = M_CG640x480;
1976 case CONS_BELLTYPE: /* set bell type sound */
1977 if ((*(int *)data) & CONS_QUIET_BELL)
1978 vd->vd_flags |= VDF_QUIET_BELL;
1980 vd->vd_flags &= ~VDF_QUIET_BELL;
1982 case CONS_GETINFO: {
1983 vid_info_t *vi = (vid_info_t *)data;
1985 vi->m_num = vd->vd_curwindow->vw_number + 1;
1986 /* XXX: other fields! */
1990 *(int *)data = 0x200;
1995 case CONS_MOUSECTL: {
1996 mouse_info_t *mouse = (mouse_info_t*)data;
1999 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2000 * should not be applied to individual TTYs, but only to
2003 switch (mouse->operation) {
2005 if (vd->vd_flags & VDF_MOUSECURSOR) {
2006 vd->vd_flags &= ~VDF_MOUSECURSOR;
2007 #ifndef SC_NO_CUTPASTE
2008 vt_mouse_state(VT_MOUSE_HIDE);
2013 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2014 vd->vd_flags |= VDF_MOUSECURSOR;
2015 vd->vd_mx = vd->vd_width / 2;
2016 vd->vd_my = vd->vd_height / 2;
2017 #ifndef SC_NO_CUTPASTE
2018 vt_mouse_state(VT_MOUSE_SHOW);
2029 error = vtfont_load((void *)data, &vf);
2033 error = vt_change_font(vw, vf);
2038 scrmap_t *sm = (scrmap_t *)data;
2040 /* We don't have screen maps, so return a handcrafted one. */
2041 for (i = 0; i < 256; i++)
2047 * FIXME: This implementation is incomplete compared to
2050 switch (*(int *)data) {
2054 vw->vw_flags &= ~VWF_GRAPHICS;
2057 vw->vw_flags |= VWF_GRAPHICS;
2061 case KDENABIO: /* allow io operations */
2062 error = priv_check(td, PRIV_IO);
2065 error = securelevel_gt(td->td_ucred, 0);
2068 #if defined(__i386__)
2069 td->td_frame->tf_eflags |= PSL_IOPL;
2070 #elif defined(__amd64__)
2071 td->td_frame->tf_rflags |= PSL_IOPL;
2074 case KDDISABIO: /* disallow io operations (default) */
2075 #if defined(__i386__)
2076 td->td_frame->tf_eflags &= ~PSL_IOPL;
2077 #elif defined(__amd64__)
2078 td->td_frame->tf_rflags &= ~PSL_IOPL;
2081 case KDMKTONE: /* sound the bell */
2082 vtterm_beep(tm, *(u_int *)data);
2084 case KIOCSOUND: /* make tone (*data) hz */
2087 case CONS_SETKBD: /* set the new keyboard */
2090 if (vd->vd_keyboard != *(int *)data) {
2091 kbd = kbd_get_keyboard(*(int *)data);
2096 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2097 (void *)vd, vt_kbdevent, vd);
2099 if (vd->vd_keyboard != -1) {
2100 kbd_release(kbd, (void *)vd);
2102 kbd = kbd_get_keyboard(i);
2103 vd->vd_keyboard = i;
2105 (void)kbdd_ioctl(kbd, KDSKBMODE,
2106 (caddr_t)&vd->vd_curwindow->vw_kbdmode);
2108 error = EPERM; /* XXX */
2113 case CONS_RELKBD: /* release the current keyboard */
2116 if (vd->vd_keyboard != -1) {
2117 kbd = kbd_get_keyboard(vd->vd_keyboard);
2122 error = kbd_release(kbd, (void *)vd);
2124 vd->vd_keyboard = -1;
2131 win = *(int *)data - 1;
2132 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2134 if ((win > VT_MAXWINDOWS) || (win < 0))
2136 return (vt_proc_window_switch(vd->vd_windows[win]));
2139 *(int *)data = vd->vd_curwindow->vw_number + 1;
2142 *(int *)data = vw->vw_number + 1;
2145 /* TODO: Check current state, switching can be in progress. */
2146 if ((*(int *)data) == 0x01)
2147 vw->vw_flags |= VWF_VTYLOCK;
2148 else if ((*(int *)data) == 0x02)
2149 vw->vw_flags &= ~VWF_VTYLOCK;
2155 for (i = 0; i < VT_MAXWINDOWS; i++) {
2156 vw = vd->vd_windows[i];
2159 if (!(vw->vw_flags & VWF_OPENED)) {
2160 *(int *)data = vw->vw_number + 1;
2170 i = *(unsigned int *)data;
2171 if (i > VT_MAXWINDOWS)
2174 vw = vd->vd_windows[i - 1];
2177 while (vd->vd_curwindow != vw && error == 0)
2178 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2181 case VT_SETMODE: { /* set screen switcher mode */
2182 struct vt_mode *mode;
2185 mode = (struct vt_mode *)data;
2186 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2187 if (vw->vw_smode.mode == VT_PROCESS) {
2188 p1 = pfind(vw->vw_pid);
2189 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2192 DPRINTF(5, "error EPERM\n");
2198 if (mode->mode == VT_AUTO) {
2199 vw->vw_smode.mode = VT_AUTO;
2202 DPRINTF(5, "VT_AUTO, ");
2203 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2204 cnavailable(vw->vw_terminal->consdev, TRUE);
2205 /* were we in the middle of the vty switching process? */
2206 if (finish_vt_rel(vw, TRUE, &s) == 0)
2207 DPRINTF(5, "reset WAIT_REL, ");
2208 if (finish_vt_acq(vw) == 0)
2209 DPRINTF(5, "reset WAIT_ACQ, ");
2211 } else if (mode->mode == VT_PROCESS) {
2212 if (!ISSIGVALID(mode->relsig) ||
2213 !ISSIGVALID(mode->acqsig) ||
2214 !ISSIGVALID(mode->frsig)) {
2215 DPRINTF(5, "error EINVAL\n");
2218 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2219 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2220 vw->vw_proc = td->td_proc;
2221 vw->vw_pid = vw->vw_proc->p_pid;
2222 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2223 cnavailable(vw->vw_terminal->consdev, FALSE);
2225 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2232 case VT_GETMODE: /* get screen switcher mode */
2233 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2236 case VT_RELDISP: /* screen switcher ioctl */
2238 * This must be the current vty which is in the VT_PROCESS
2241 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2245 /* ...and this process is controlling it. */
2246 if (vw->vw_proc != td->td_proc) {
2250 switch(*(int *)data) {
2251 case VT_FALSE: /* user refuses to release screen, abort */
2252 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2253 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2254 SC_DRIVER_NAME, VT_UNIT(vw));
2256 case VT_TRUE: /* user has released screen, go on */
2257 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2258 if (vw->vw_flags & VWF_SWWAIT_REL) {
2259 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2260 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2261 SC_DRIVER_NAME, VT_UNIT(vw));
2266 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2267 if ((error = finish_vt_acq(vw)) == 0)
2268 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2269 SC_DRIVER_NAME, VT_UNIT(vw));
2280 static struct vt_window *
2281 vt_allocate_window(struct vt_device *vd, unsigned int window)
2283 struct vt_window *vw;
2284 struct terminal *tm;
2288 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2290 vw->vw_number = window;
2291 vw->vw_kbdmode = K_XLATE;
2293 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2294 vw->vw_font = vtfont_ref(&vt_font_default);
2295 vt_compute_drawable_area(vw);
2298 vt_termsize(vd, vw->vw_font, &size);
2299 vt_winsize(vd, vw->vw_font, &wsz);
2300 vtbuf_init(&vw->vw_buf, &size);
2302 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2303 terminal_set_winsize(tm, &wsz);
2304 vd->vd_windows[window] = vw;
2305 callout_init(&vw->vw_proc_dead_timer, 0);
2311 vt_upgrade(struct vt_device *vd)
2313 struct vt_window *vw;
2316 if (!vty_enabled(VTY_VT))
2319 for (i = 0; i < VT_MAXWINDOWS; i++) {
2320 vw = vd->vd_windows[i];
2323 vw = vt_allocate_window(vd, i);
2325 if (!(vw->vw_flags & VWF_READY)) {
2326 callout_init(&vw->vw_proc_dead_timer, 0);
2327 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2328 vw->vw_flags |= VWF_READY;
2329 if (vw->vw_flags & VWF_CONSOLE) {
2330 /* For existing console window. */
2331 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2332 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2338 if (vd->vd_curwindow == NULL)
2339 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2341 if (!(vd->vd_flags & VDF_ASYNC)) {
2342 /* Attach keyboard. */
2343 vt_allocate_keyboard(vd);
2345 /* Init 25 Hz timer. */
2346 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2348 /* Start timer when everything ready. */
2349 vd->vd_flags |= VDF_ASYNC;
2350 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2351 vd->vd_timer_armed = 1;
2356 /* Refill settings with new sizes. */
2361 vt_resize(struct vt_device *vd)
2363 struct vt_window *vw;
2366 for (i = 0; i < VT_MAXWINDOWS; i++) {
2367 vw = vd->vd_windows[i];
2369 /* Assign default font to window, if not textmode. */
2370 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2371 vw->vw_font = vtfont_ref(&vt_font_default);
2374 /* Resize terminal windows */
2375 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2376 DPRINTF(100, "%s: vt_change_font() is busy, "
2377 "window %d\n", __func__, i);
2383 vt_allocate(struct vt_driver *drv, void *softc)
2385 struct vt_device *vd;
2387 if (!vty_enabled(VTY_VT))
2390 if (main_vd->vd_driver == NULL) {
2391 main_vd->vd_driver = drv;
2392 printf("VT: initialize with new VT driver \"%s\".\n",
2396 * Check if have rights to replace current driver. For example:
2397 * it is bad idea to replace KMS driver with generic VGA one.
2399 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2400 printf("VT: Driver priority %d too low. Current %d\n ",
2401 drv->vd_priority, main_vd->vd_driver->vd_priority);
2404 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2405 main_vd->vd_driver->vd_name, drv->vd_name);
2409 if (vd->vd_flags & VDF_ASYNC) {
2410 /* Stop vt_flush periodic task. */
2411 vt_suspend_flush_timer(vd);
2413 * Mute current terminal until we done. vt_change_font (called
2414 * from vt_resize) will unmute it.
2416 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2420 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2424 vd->vd_flags &= ~VDF_TEXTMODE;
2426 vd->vd_driver = drv;
2427 vd->vd_softc = softc;
2428 vd->vd_driver->vd_init(vd);
2431 /* Update windows sizes and initialize last items. */
2435 if (vd->vd_flags & VDF_SPLASH)
2439 if (vd->vd_flags & VDF_ASYNC) {
2440 /* Allow to put chars now. */
2441 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2442 /* Rerun timer for screen updates. */
2443 vt_resume_flush_timer(vd, 0);
2447 * Register as console. If it already registered, cnadd() will ignore
2450 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2457 if (vt_suspendswitch == 0)
2459 /* Save current window. */
2460 main_vd->vd_savedwindow = main_vd->vd_curwindow;
2461 /* Ask holding process to free window and switch to console window */
2462 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]);
2469 if (vt_suspendswitch == 0)
2471 /* Switch back to saved window */
2472 if (main_vd->vd_savedwindow != NULL)
2473 vt_proc_window_switch(main_vd->vd_savedwindow);
2474 main_vd->vd_savedwindow = NULL;