2 * Copyright (c) 2009, 2013 The FreeBSD Foundation
5 * This software was developed by Ed Schouten under sponsorship from the
8 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_compat.h"
38 #include <sys/param.h>
39 #include <sys/consio.h>
40 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/power.h>
51 #include <sys/reboot.h>
52 #include <sys/systm.h>
53 #include <sys/terminal.h>
55 #include <dev/kbd/kbdreg.h>
56 #include <dev/vt/vt.h>
58 #if defined(__i386__) || defined(__amd64__)
59 #include <machine/psl.h>
60 #include <machine/frame.h>
63 static tc_bell_t vtterm_bell;
64 static tc_cursor_t vtterm_cursor;
65 static tc_putchar_t vtterm_putchar;
66 static tc_fill_t vtterm_fill;
67 static tc_copy_t vtterm_copy;
68 static tc_param_t vtterm_param;
69 static tc_done_t vtterm_done;
71 static tc_cnprobe_t vtterm_cnprobe;
72 static tc_cngetc_t vtterm_cngetc;
74 static tc_cngrab_t vtterm_cngrab;
75 static tc_cnungrab_t vtterm_cnungrab;
77 static tc_opened_t vtterm_opened;
78 static tc_ioctl_t vtterm_ioctl;
79 static tc_mmap_t vtterm_mmap;
81 const struct terminal_class vt_termclass = {
82 .tc_bell = vtterm_bell,
83 .tc_cursor = vtterm_cursor,
84 .tc_putchar = vtterm_putchar,
85 .tc_fill = vtterm_fill,
86 .tc_copy = vtterm_copy,
87 .tc_param = vtterm_param,
88 .tc_done = vtterm_done,
90 .tc_cnprobe = vtterm_cnprobe,
91 .tc_cngetc = vtterm_cngetc,
93 .tc_cngrab = vtterm_cngrab,
94 .tc_cnungrab = vtterm_cnungrab,
96 .tc_opened = vtterm_opened,
97 .tc_ioctl = vtterm_ioctl,
98 .tc_mmap = vtterm_mmap,
102 * Use a constant timer of 25 Hz to redraw the screen.
104 * XXX: In theory we should only fire up the timer when there is really
105 * activity. Unfortunately we cannot always start timers. We really
106 * don't want to process kernel messages synchronously, because it
107 * really slows down the system.
109 #define VT_TIMERFREQ 25
111 /* Bell pitch/duration. */
112 #define VT_BELLDURATION ((5 * hz + 99) / 100)
113 #define VT_BELLPITCH 800
115 #define VT_LOCK(vd) mtx_lock(&(vd)->vd_lock)
116 #define VT_UNLOCK(vd) mtx_unlock(&(vd)->vd_lock)
117 #define VT_LOCK_ASSERT(vd, what) mtx_assert(&(vd)->vd_lock, what)
119 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \
122 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters");
123 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)");
124 VT_SYSCTL_INT(enable_bell, 1, "Enable bell");
125 VT_SYSCTL_INT(debug, 0, "vt(9) debug level");
126 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode");
127 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend");
129 /* Allow to disable some keyboard combinations. */
130 VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. "
131 "See kbdmap(5) to configure.");
132 VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. "
133 "See kbdmap(5) to configure.");
134 VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. "
135 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete).");
136 VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. "
137 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc).");
138 VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. "
139 "See kbdmap(5) to configure.");
141 static struct vt_device vt_consdev;
142 static unsigned int vt_unit = 0;
143 static MALLOC_DEFINE(M_VT, "vt", "vt device");
144 struct vt_device *main_vd = &vt_consdev;
147 extern unsigned int vt_logo_width;
148 extern unsigned int vt_logo_height;
149 extern unsigned int vt_logo_depth;
150 extern unsigned char vt_logo_image[];
153 extern struct vt_font vt_font_default;
154 #ifndef SC_NO_CUTPASTE
155 extern struct vt_mouse_cursor vt_default_mouse_pointer;
158 static int signal_vt_rel(struct vt_window *);
159 static int signal_vt_acq(struct vt_window *);
160 static int finish_vt_rel(struct vt_window *, int, int *);
161 static int finish_vt_acq(struct vt_window *);
162 static int vt_window_switch(struct vt_window *);
163 static int vt_late_window_switch(struct vt_window *);
164 static int vt_proc_alive(struct vt_window *);
165 static void vt_resize(struct vt_device *);
166 static void vt_update_static(void *);
167 #ifndef SC_NO_CUTPASTE
168 static void vt_mouse_paste(void);
170 static void vt_suspend_handler(void *priv);
171 static void vt_resume_handler(void *priv);
173 SET_DECLARE(vt_drv_set, struct vt_driver);
175 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT))
176 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH))
178 static struct terminal vt_consterm;
179 static struct vt_window vt_conswindow;
180 static struct vt_device vt_consdev = {
183 .vd_flags = VDF_INVALID,
184 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, },
185 .vd_curwindow = &vt_conswindow,
188 #ifndef SC_NO_CUTPASTE
194 .vd_mcursor = &vt_default_mouse_pointer,
195 .vd_mcursor_fg = TC_WHITE,
196 .vd_mcursor_bg = TC_BLACK,
199 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)];
200 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE];
201 static struct vt_window vt_conswindow = {
202 .vw_number = VT_CONSWINDOW,
203 .vw_flags = VWF_CONSOLE,
205 .vb_buffer = &vt_constextbuf[0],
206 .vb_rows = &vt_constextbufrows[0],
207 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE,
210 .vb_flags = VBF_STATIC,
211 .vb_mark_start = {.tp_row = 0, .tp_col = 0,},
212 .vb_mark_end = {.tp_row = 0, .tp_col = 0,},
218 .vw_device = &vt_consdev,
219 .vw_terminal = &vt_consterm,
220 .vw_kbdmode = K_XLATE,
223 static struct terminal vt_consterm = {
224 .tm_class = &vt_termclass,
225 .tm_softc = &vt_conswindow,
228 static struct consdev vt_consterm_consdev = {
229 .cn_ops = &termcn_cnops,
230 .cn_arg = &vt_consterm,
234 /* Add to set of consoles. */
235 DATA_SET(cons_set, vt_consterm_consdev);
238 * Right after kmem is done to allow early drivers to use locking and allocate
241 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static,
243 /* Delay until all devices attached, to not waste time. */
244 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade,
247 /* Initialize locks/mem depended members. */
249 vt_update_static(void *dummy)
252 if (!vty_enabled(VTY_VT))
254 if (main_vd->vd_driver != NULL)
255 printf("VT: running with driver \"%s\".\n",
256 main_vd->vd_driver->vd_name);
258 printf("VT: init without driver.\n");
260 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF);
261 cv_init(&main_vd->vd_winswitch, "vtwswt");
265 vt_schedule_flush(struct vt_device *vd, int ms)
269 /* Default to initial value. */
270 ms = 1000 / VT_TIMERFREQ;
272 callout_schedule(&vd->vd_timer, hz / (1000 / ms));
276 vt_resume_flush_timer(struct vt_device *vd, int ms)
279 if (!(vd->vd_flags & VDF_ASYNC) ||
280 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1))
283 vt_schedule_flush(vd, ms);
287 vt_suspend_flush_timer(struct vt_device *vd)
290 * As long as this function is called locked, callout_stop()
291 * has the same effect like callout_drain() with regard to
292 * preventing the callback function from executing.
294 VT_LOCK_ASSERT(vd, MA_OWNED);
296 if (!(vd->vd_flags & VDF_ASYNC) ||
297 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0))
300 callout_stop(&vd->vd_timer);
304 vt_switch_timer(void *arg)
307 vt_late_window_switch((struct vt_window *)arg);
311 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
316 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode);
322 vw->vw_kbdmode = mode;
328 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd)
332 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode);
340 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd)
345 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state);
351 vw->vw_kbdstate &= ~LOCK_MASK;
352 vw->vw_kbdstate |= state & LOCK_MASK;
358 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd)
362 state = vw->vw_kbdstate & LOCK_MASK;
363 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state);
371 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
376 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds);
382 vw->vw_kbdstate &= ~LED_MASK;
383 vw->vw_kbdstate |= leds & LED_MASK;
389 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd)
393 leds = vw->vw_kbdstate & LED_MASK;
394 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds);
402 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw)
405 DPRINTF(40, "%s\n", __func__);
406 curvw->vw_switch_to = vw;
407 /* Set timer to allow switch in case when process hang. */
408 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer,
409 vt_switch_timer, (void *)vw);
410 /* Notify process about vt switch attempt. */
411 DPRINTF(30, "%s: Notify process.\n", __func__);
412 signal_vt_rel(curvw);
418 vt_window_postswitch(struct vt_window *vw)
425 /* vt_late_window_switch will done VT switching for regular case. */
427 vt_late_window_switch(struct vt_window *vw)
431 callout_stop(&vw->vw_proc_dead_timer);
433 ret = vt_window_switch(vw);
437 /* Notify owner process about terminal availability. */
438 if (vw->vw_smode.mode == VT_PROCESS) {
439 ret = vt_window_postswitch(vw);
446 vt_proc_window_switch(struct vt_window *vw)
448 struct vt_window *curvw;
449 struct vt_device *vd;
453 curvw = vd->vd_curwindow;
455 if (curvw->vw_flags & VWF_VTYLOCK)
458 /* Ask current process permission to switch away. */
459 if (curvw->vw_smode.mode == VT_PROCESS) {
460 DPRINTF(30, "%s: VT_PROCESS ", __func__);
461 if (vt_proc_alive(curvw) == FALSE) {
462 DPRINTF(30, "Dead. Cleaning.");
465 DPRINTF(30, "%s: Signaling process.\n", __func__);
466 /* Alive, try to ask him. */
467 ret = vt_window_preswitch(vw, curvw);
468 /* Wait for process answer or timeout. */
474 ret = vt_late_window_switch(vw);
478 /* Switch window ignoring process locking. */
480 vt_window_switch(struct vt_window *vw)
482 struct vt_device *vd = vw->vw_device;
483 struct vt_window *curvw = vd->vd_curwindow;
492 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) {
497 vt_suspend_flush_timer(vd);
499 vd->vd_curwindow = vw;
500 vd->vd_flags |= VDF_INVALID;
501 cv_broadcast(&vd->vd_winswitch);
504 if (vd->vd_driver->vd_postswitch)
505 vd->vd_driver->vd_postswitch(vd);
507 vt_resume_flush_timer(vd, 0);
509 /* Restore per-window keyboard mode. */
511 kbd = kbd_get_keyboard(vd->vd_keyboard);
513 if (curvw->vw_kbdmode == K_XLATE)
514 vt_save_kbd_state(curvw, kbd);
516 vt_update_kbd_mode(vw, kbd);
517 vt_update_kbd_state(vw, kbd);
520 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number);
526 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size)
529 size->tp_row = vd->vd_height;
530 size->tp_col = vd->vd_width;
532 size->tp_row /= vf->vf_height;
533 size->tp_col /= vf->vf_width;
538 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size)
541 size->ws_row = size->ws_ypixel = vd->vd_height;
542 size->ws_col = size->ws_xpixel = vd->vd_width;
544 size->ws_row /= vf->vf_height;
545 size->ws_col /= vf->vf_width;
550 vt_compute_drawable_area(struct vt_window *vw)
552 struct vt_device *vd;
557 if (vw->vw_font == NULL) {
558 vw->vw_draw_area.tr_begin.tp_col = 0;
559 vw->vw_draw_area.tr_begin.tp_row = 0;
560 vw->vw_draw_area.tr_end.tp_col = vd->vd_width;
561 vw->vw_draw_area.tr_end.tp_row = vd->vd_height;
568 * Compute the drawable area, so that the text is centered on
572 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2;
573 vw->vw_draw_area.tr_begin.tp_row = (vd->vd_height % vf->vf_height) / 2;
574 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col +
575 vd->vd_width / vf->vf_width * vf->vf_width;
576 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row +
577 vd->vd_height / vf->vf_height * vf->vf_height;
581 vt_scroll(struct vt_window *vw, int offset, int whence)
586 if ((vw->vw_flags & VWF_SCROLL) == 0)
589 vt_termsize(vw->vw_device, vw->vw_font, &size);
591 diff = vthistory_seek(&vw->vw_buf, offset, whence);
593 vw->vw_device->vd_flags |= VDF_INVALID;
594 vt_resume_flush_timer(vw->vw_device, 0);
598 vt_machine_kbdevent(int c)
602 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */
604 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger");
606 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */
608 shutdown_nice(RB_HALT);
610 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */
611 #ifndef SC_NO_CUTPASTE
612 /* Insert text from cut-paste buffer. */
616 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */
618 shutdown_nice(RB_HALT|RB_POWEROFF);
620 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */
622 * Request to immediate panic if sysctl
623 * kern.vt.enable_panic_key allow it.
626 panic("Forced by the panic key");
628 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */
630 shutdown_nice(RB_AUTOBOOT);
632 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */
633 /* Force activatation/deactivation of the screen saver. */
636 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */
637 /* Put machine into Stand-By mode. */
638 power_pm_suspend(POWER_SLEEP_STATE_STANDBY);
640 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */
641 /* Suspend machine. */
642 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND);
650 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console)
652 struct vt_device *vd;
656 /* Only special keys handled in ScrollLock mode */
657 if ((c & SPCLKEY) == 0)
663 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
664 vw = vd->vd_windows[c - F_SCR];
666 vt_proc_window_switch(vw);
674 /* Turn scrolling off. */
675 vt_scroll(vw, 0, VHS_END);
676 VTBUF_SLCK_DISABLE(&vw->vw_buf);
677 vw->vw_flags &= ~VWF_SCROLL;
680 case FKEY | F(49): /* Home key. */
681 vt_scroll(vw, 0, VHS_SET);
683 case FKEY | F(50): /* Arrow up. */
684 vt_scroll(vw, -1, VHS_CUR);
686 case FKEY | F(51): /* Page up. */
687 vt_termsize(vd, vw->vw_font, &size);
688 vt_scroll(vw, -size.tp_row, VHS_CUR);
690 case FKEY | F(57): /* End key. */
691 vt_scroll(vw, 0, VHS_END);
693 case FKEY | F(58): /* Arrow down. */
694 vt_scroll(vw, 1, VHS_CUR);
696 case FKEY | F(59): /* Page down. */
697 vt_termsize(vd, vw->vw_font, &size);
698 vt_scroll(vw, size.tp_row, VHS_CUR);
707 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c)
709 struct vt_window *vw = vd->vd_curwindow;
711 #if VT_ALT_TO_ESC_HACK
713 switch (c & ~RELKEY) {
714 case (SPCLKEY | RALT):
715 if (vt_enable_altgr != 0)
717 case (SPCLKEY | LALT):
718 vd->vd_kbstate &= ~ALKED;
720 /* Other keys ignored for RELKEY event. */
723 switch (c & ~RELKEY) {
724 case (SPCLKEY | RALT):
725 if (vt_enable_altgr != 0)
727 case (SPCLKEY | LALT):
728 vd->vd_kbstate |= ALKED;
733 /* Other keys ignored for RELKEY event. */
737 if (vt_machine_kbdevent(c))
740 if (vw->vw_flags & VWF_SCROLL) {
741 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */);
742 /* Scroll mode keys handled, nothing to do more. */
749 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) {
750 vw = vd->vd_windows[c - F_SCR];
752 vt_proc_window_switch(vw);
758 /* Switch to next VT. */
759 c = (vw->vw_number + 1) % VT_MAXWINDOWS;
760 vw = vd->vd_windows[c];
762 vt_proc_window_switch(vw);
765 /* Switch to previous VT. */
766 c = (vw->vw_number - 1) % VT_MAXWINDOWS;
767 vw = vd->vd_windows[c];
769 vt_proc_window_switch(vw);
772 vt_save_kbd_state(vw, kbd);
774 if (vw->vw_kbdstate & SLKED) {
775 /* Turn scrolling on. */
776 vw->vw_flags |= VWF_SCROLL;
777 VTBUF_SLCK_ENABLE(&vw->vw_buf);
779 /* Turn scrolling off. */
780 vw->vw_flags &= ~VWF_SCROLL;
781 VTBUF_SLCK_DISABLE(&vw->vw_buf);
782 vt_scroll(vw, 0, VHS_END);
787 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3):
788 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6):
789 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9):
790 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
791 /* F1 through F12 keys. */
792 terminal_input_special(vw->vw_terminal,
793 TKEY_F1 + c - (FKEY | F(1)));
795 case FKEY | F(49): /* Home key. */
796 terminal_input_special(vw->vw_terminal, TKEY_HOME);
798 case FKEY | F(50): /* Arrow up. */
799 terminal_input_special(vw->vw_terminal, TKEY_UP);
801 case FKEY | F(51): /* Page up. */
802 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP);
804 case FKEY | F(53): /* Arrow left. */
805 terminal_input_special(vw->vw_terminal, TKEY_LEFT);
807 case FKEY | F(55): /* Arrow right. */
808 terminal_input_special(vw->vw_terminal, TKEY_RIGHT);
810 case FKEY | F(57): /* End key. */
811 terminal_input_special(vw->vw_terminal, TKEY_END);
813 case FKEY | F(58): /* Arrow down. */
814 terminal_input_special(vw->vw_terminal, TKEY_DOWN);
816 case FKEY | F(59): /* Page down. */
817 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN);
819 case FKEY | F(60): /* Insert key. */
820 terminal_input_special(vw->vw_terminal, TKEY_INSERT);
822 case FKEY | F(61): /* Delete key. */
823 terminal_input_special(vw->vw_terminal, TKEY_DELETE);
826 } else if (KEYFLAGS(c) == 0) {
827 /* Don't do UTF-8 conversion when doing raw mode. */
828 if (vw->vw_kbdmode == K_XLATE) {
829 #if VT_ALT_TO_ESC_HACK
830 if (vd->vd_kbstate & ALKED) {
832 * Prepend ESC sequence if one of ALT keys down.
834 terminal_input_char(vw->vw_terminal, 0x1b);
838 kdb_alt_break(c, &vd->vd_altbrk);
840 terminal_input_char(vw->vw_terminal, KEYCHAR(c));
842 terminal_input_raw(vw->vw_terminal, c);
848 vt_kbdevent(keyboard_t *kbd, int event, void *arg)
850 struct vt_device *vd = arg;
856 case KBDIO_UNLOADING:
858 vd->vd_keyboard = -1;
859 kbd_release(kbd, (void *)vd);
866 while ((c = kbdd_read_char(kbd, 0)) != NOKEY)
867 vt_processkey(kbd, vd, c);
873 vt_allocate_keyboard(struct vt_device *vd)
879 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd);
881 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0);
882 k0 = kbd_get_keyboard(idx0);
884 for (idx = kbd_find_keyboard2("*", -1, 0);
886 idx = kbd_find_keyboard2("*", -1, idx + 1)) {
887 k = kbd_get_keyboard(idx);
889 if (idx == idx0 || KBD_IS_BUSY(k))
892 bzero(&ki, sizeof(ki));
893 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name));
894 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0';
895 ki.kb_unit = k->kb_unit;
897 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki);
900 DPRINTF(20, "%s: no kbdmux allocated\n", __func__);
901 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd);
903 DPRINTF(10, "%s: No keyboard found.\n", __func__);
907 vd->vd_keyboard = idx0;
908 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard);
914 vtterm_bell(struct terminal *tm)
916 struct vt_window *vw = tm->tm_softc;
917 struct vt_device *vd = vw->vw_device;
922 if (vd->vd_flags & VDF_QUIET_BELL)
925 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION);
929 vtterm_beep(struct terminal *tm, u_int param)
936 if ((param == 0) || ((param & 0xffff) == 0)) {
941 period = ((param >> 16) & 0xffff) * hz / 1000;
942 freq = 1193182 / (param & 0xffff);
944 sysbeep(freq, period);
948 vtterm_cursor(struct terminal *tm, const term_pos_t *p)
950 struct vt_window *vw = tm->tm_softc;
952 vtbuf_cursor_position(&vw->vw_buf, p);
953 vt_resume_flush_timer(vw->vw_device, 0);
957 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c)
959 struct vt_window *vw = tm->tm_softc;
961 vtbuf_putchar(&vw->vw_buf, p, c);
962 vt_resume_flush_timer(vw->vw_device, 0);
966 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c)
968 struct vt_window *vw = tm->tm_softc;
970 vtbuf_fill_locked(&vw->vw_buf, r, c);
971 vt_resume_flush_timer(vw->vw_device, 0);
975 vtterm_copy(struct terminal *tm, const term_rect_t *r,
978 struct vt_window *vw = tm->tm_softc;
980 vtbuf_copy(&vw->vw_buf, r, p);
981 vt_resume_flush_timer(vw->vw_device, 0);
985 vtterm_param(struct terminal *tm, int cmd, unsigned int arg)
987 struct vt_window *vw = tm->tm_softc;
991 vtbuf_cursor_visibility(&vw->vw_buf, arg);
992 vt_resume_flush_timer(vw->vw_device, 0);
995 vw->vw_mouse_level = arg;
1001 vt_determine_colors(term_char_t c, int cursor,
1002 term_color_t *fg, term_color_t *bg)
1009 *fg = TCHAR_FGCOLOR(c);
1010 if (TCHAR_FORMAT(c) & TF_BOLD)
1011 *fg = TCOLOR_LIGHT(*fg);
1012 *bg = TCHAR_BGCOLOR(c);
1014 if (TCHAR_FORMAT(c) & TF_REVERSE)
1026 #ifndef SC_NO_CUTPASTE
1028 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area)
1030 unsigned int mx, my, x1, y1, x2, y2;
1033 * We use the cursor position saved during the current refresh,
1034 * in case the cursor moved since.
1036 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col;
1037 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row;
1039 x1 = area->tr_begin.tp_col;
1040 y1 = area->tr_begin.tp_row;
1041 x2 = area->tr_end.tp_col;
1042 y2 = area->tr_end.tp_row;
1044 if (((mx >= x1 && x2 - 1 >= mx) ||
1045 (mx < x1 && mx + vd->vd_mcursor->width >= x1)) &&
1046 ((my >= y1 && y2 - 1 >= my) ||
1047 (my < y1 && my + vd->vd_mcursor->height >= y1)))
1054 vt_mark_mouse_position_as_dirty(struct vt_device *vd)
1057 struct vt_window *vw;
1061 vw = vd->vd_curwindow;
1064 x = vd->vd_mx_drawn;
1065 y = vd->vd_my_drawn;
1068 area.tr_begin.tp_col = x / vf->vf_width;
1069 area.tr_begin.tp_row = y / vf->vf_height;
1070 area.tr_end.tp_col =
1071 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1;
1072 area.tr_end.tp_row =
1073 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1;
1076 * No font loaded (ie. vt_vga operating in textmode).
1078 * FIXME: This fake area needs to be revisited once the
1079 * mouse cursor is supported in vt_vga's textmode.
1081 area.tr_begin.tp_col = x;
1082 area.tr_begin.tp_row = y;
1083 area.tr_end.tp_col = x + 2;
1084 area.tr_end.tp_row = y + 2;
1087 vtbuf_dirty(&vw->vw_buf, &area);
1092 vt_flush(struct vt_device *vd)
1094 struct vt_window *vw;
1098 #ifndef SC_NO_CUTPASTE
1099 int cursor_was_shown, cursor_moved;
1102 vw = vd->vd_curwindow;
1106 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY)
1110 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL))
1113 #ifndef SC_NO_CUTPASTE
1114 cursor_was_shown = vd->vd_mshown;
1115 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn ||
1116 vd->vd_my != vd->vd_my_drawn);
1118 /* Check if the cursor should be displayed or not. */
1119 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */
1120 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */
1121 !kdb_active && panicstr == NULL) { /* DDB inactive. */
1128 * If the cursor changed display state or moved, we must mark
1129 * the old position as dirty, so that it's erased.
1131 if (cursor_was_shown != vd->vd_mshown ||
1132 (vd->vd_mshown && cursor_moved))
1133 vt_mark_mouse_position_as_dirty(vd);
1136 * Save position of the mouse cursor. It's used by backends to
1137 * know where to draw the cursor and during the next refresh to
1138 * erase the previous position.
1140 vd->vd_mx_drawn = vd->vd_mx;
1141 vd->vd_my_drawn = vd->vd_my;
1144 * If the cursor is displayed and has moved since last refresh,
1145 * mark the new position as dirty.
1147 if (vd->vd_mshown && cursor_moved)
1148 vt_mark_mouse_position_as_dirty(vd);
1151 vtbuf_undirty(&vw->vw_buf, &tarea);
1152 vt_termsize(vd, vf, &size);
1154 /* Force a full redraw when the screen contents are invalid. */
1155 if (vd->vd_flags & VDF_INVALID) {
1156 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0;
1157 tarea.tr_end = size;
1159 vd->vd_flags &= ~VDF_INVALID;
1162 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) {
1163 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea);
1173 struct vt_device *vd;
1177 /* Update screen if required. */
1178 changed = vt_flush(vd);
1180 /* Schedule for next update. */
1182 vt_schedule_flush(vd, 0);
1184 vd->vd_timer_armed = 0;
1188 vtterm_done(struct terminal *tm)
1190 struct vt_window *vw = tm->tm_softc;
1191 struct vt_device *vd = vw->vw_device;
1193 if (kdb_active || panicstr != NULL) {
1194 /* Switch to the debugger. */
1195 if (vd->vd_curwindow != vw) {
1196 vd->vd_curwindow = vw;
1197 vd->vd_flags |= VDF_INVALID;
1198 if (vd->vd_driver->vd_postswitch)
1199 vd->vd_driver->vd_postswitch(vd);
1201 vd->vd_flags &= ~VDF_SPLASH;
1203 } else if (!(vd->vd_flags & VDF_ASYNC)) {
1210 vtterm_splash(struct vt_device *vd)
1212 vt_axis_t top, left;
1214 /* Display a nice boot splash. */
1215 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) {
1217 top = (vd->vd_height - vt_logo_height) / 2;
1218 left = (vd->vd_width - vt_logo_width) / 2;
1219 switch (vt_logo_depth) {
1221 /* XXX: Unhardcode colors! */
1222 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow,
1223 vt_logo_image, NULL, vt_logo_width, vt_logo_height,
1224 left, top, TC_WHITE, TC_BLACK);
1226 vd->vd_flags |= VDF_SPLASH;
1233 vtterm_cnprobe(struct terminal *tm, struct consdev *cp)
1235 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL;
1236 struct vt_window *vw = tm->tm_softc;
1237 struct vt_device *vd = vw->vw_device;
1242 if (!vty_enabled(VTY_VT))
1245 if (vd->vd_flags & VDF_INITIALIZED)
1246 /* Initialization already done. */
1249 SET_FOREACH(vtdlist, vt_drv_set) {
1251 if (vtd->vd_probe == NULL)
1253 if (vtd->vd_probe(vd) == CN_DEAD)
1255 if ((vtdbest == NULL) ||
1256 (vtd->vd_priority > vtdbest->vd_priority))
1259 if (vtdbest == NULL) {
1260 cp->cn_pri = CN_DEAD;
1261 vd->vd_flags |= VDF_DEAD;
1263 vd->vd_driver = vtdbest;
1264 cp->cn_pri = vd->vd_driver->vd_init(vd);
1267 /* Check if driver's vt_init return CN_DEAD. */
1268 if (cp->cn_pri == CN_DEAD) {
1269 vd->vd_flags |= VDF_DEAD;
1272 /* Initialize any early-boot keyboard drivers */
1273 kbd_configure(KB_CONF_PROBE_ONLY);
1275 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1);
1276 vd->vd_windows[VT_CONSWINDOW] = vw;
1277 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw));
1279 /* Attach default font if not in TEXTMODE. */
1280 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
1281 vw->vw_font = vtfont_ref(&vt_font_default);
1282 vt_compute_drawable_area(vw);
1286 * The original screen size was faked (_VTDEFW x _VTDEFH). Now
1287 * that we have the real viewable size, fix it in the static
1290 if (vd->vd_width != 0 && vd->vd_height != 0)
1291 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size);
1293 vtbuf_init_early(&vw->vw_buf);
1294 vt_winsize(vd, vw->vw_font, &wsz);
1295 c = (boothowto & RB_MUTE) == 0 ? TERMINAL_KERN_ATTR :
1297 attr.ta_format = TCHAR_FORMAT(c);
1298 attr.ta_fgcolor = TCHAR_FGCOLOR(c);
1299 attr.ta_bgcolor = TCHAR_BGCOLOR(c);
1300 terminal_set_winsize_blank(tm, &wsz, 1, &attr);
1302 if (vtdbest != NULL) {
1306 vd->vd_flags |= VDF_INITIALIZED;
1311 vtterm_cngetc(struct terminal *tm)
1313 struct vt_window *vw = tm->tm_softc;
1314 struct vt_device *vd = vw->vw_device;
1318 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1319 return (*vw->vw_kbdsq++);
1321 /* Make sure the splash screen is not there. */
1322 if (vd->vd_flags & VDF_SPLASH) {
1324 vd->vd_flags &= ~VDF_SPLASH;
1325 /* Mark screen as invalid to force update */
1326 vd->vd_flags |= VDF_INVALID;
1330 /* Stripped down keyboard handler. */
1331 kbd = kbd_get_keyboard(vd->vd_keyboard);
1335 /* Force keyboard input mode to K_XLATE */
1336 vw->vw_kbdmode = K_XLATE;
1337 vt_update_kbd_mode(vw, kbd);
1339 /* Switch the keyboard to polling to make it work here. */
1340 kbdd_poll(kbd, TRUE);
1341 c = kbdd_read_char(kbd, 0);
1342 kbdd_poll(kbd, FALSE);
1346 if (vw->vw_flags & VWF_SCROLL) {
1347 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */);
1352 /* Stripped down handling of vt_kbdevent(), without locking, etc. */
1356 vt_save_kbd_state(vw, kbd);
1357 if (vw->vw_kbdstate & SLKED) {
1358 /* Turn scrolling on. */
1359 vw->vw_flags |= VWF_SCROLL;
1360 VTBUF_SLCK_ENABLE(&vw->vw_buf);
1362 /* Turn scrolling off. */
1363 vt_scroll(vw, 0, VHS_END);
1364 vw->vw_flags &= ~VWF_SCROLL;
1365 VTBUF_SLCK_DISABLE(&vw->vw_buf);
1368 /* XXX: KDB can handle history. */
1369 case SPCLKEY | FKEY | F(50): /* Arrow up. */
1370 vw->vw_kbdsq = "\x1b[A";
1372 case SPCLKEY | FKEY | F(58): /* Arrow down. */
1373 vw->vw_kbdsq = "\x1b[B";
1375 case SPCLKEY | FKEY | F(55): /* Arrow right. */
1376 vw->vw_kbdsq = "\x1b[C";
1378 case SPCLKEY | FKEY | F(53): /* Arrow left. */
1379 vw->vw_kbdsq = "\x1b[D";
1383 /* Force refresh to make scrollback work. */
1385 } else if (KEYFLAGS(c) == 0) {
1386 return (KEYCHAR(c));
1389 if (vw->vw_kbdsq && *vw->vw_kbdsq)
1390 return (*vw->vw_kbdsq++);
1396 vtterm_cngrab(struct terminal *tm)
1398 struct vt_device *vd;
1399 struct vt_window *vw;
1406 vt_window_switch(vw);
1408 kbd = kbd_get_keyboard(vd->vd_keyboard);
1412 if (vw->vw_grabbed++ > 0)
1416 * Make sure the keyboard is accessible even when the kbd device
1417 * driver is disabled.
1421 /* We shall always use the keyboard in the XLATE mode here. */
1422 vw->vw_prev_kbdmode = vw->vw_kbdmode;
1423 vw->vw_kbdmode = K_XLATE;
1424 vt_update_kbd_mode(vw, kbd);
1426 kbdd_poll(kbd, TRUE);
1430 vtterm_cnungrab(struct terminal *tm)
1432 struct vt_device *vd;
1433 struct vt_window *vw;
1439 kbd = kbd_get_keyboard(vd->vd_keyboard);
1443 if (--vw->vw_grabbed > 0)
1446 kbdd_poll(kbd, FALSE);
1448 vw->vw_kbdmode = vw->vw_prev_kbdmode;
1449 vt_update_kbd_mode(vw, kbd);
1454 vtterm_opened(struct terminal *tm, int opened)
1456 struct vt_window *vw = tm->tm_softc;
1457 struct vt_device *vd = vw->vw_device;
1460 vd->vd_flags &= ~VDF_SPLASH;
1462 vw->vw_flags |= VWF_OPENED;
1464 vw->vw_flags &= ~VWF_OPENED;
1465 /* TODO: finish ACQ/REL */
1471 vt_set_border(struct vt_window *vw, term_color_t c)
1473 struct vt_device *vd = vw->vw_device;
1475 if (vd->vd_driver->vd_drawrect == NULL)
1479 if (vw->vw_draw_area.tr_begin.tp_row > 0)
1480 vd->vd_driver->vd_drawrect(vd,
1482 vd->vd_width - 1, vw->vw_draw_area.tr_begin.tp_row - 1,
1486 if (vw->vw_draw_area.tr_begin.tp_col > 0)
1487 vd->vd_driver->vd_drawrect(vd,
1489 vw->vw_draw_area.tr_begin.tp_col - 1, vd->vd_height - 1,
1493 if (vw->vw_draw_area.tr_end.tp_col < vd->vd_width)
1494 vd->vd_driver->vd_drawrect(vd,
1495 vw->vw_draw_area.tr_end.tp_col - 1, 0,
1496 vd->vd_width - 1, vd->vd_height - 1,
1500 if (vw->vw_draw_area.tr_end.tp_row < vd->vd_height)
1501 vd->vd_driver->vd_drawrect(vd,
1502 0, vw->vw_draw_area.tr_end.tp_row - 1,
1503 vd->vd_width - 1, vd->vd_height - 1,
1510 vt_change_font(struct vt_window *vw, struct vt_font *vf)
1512 struct vt_device *vd = vw->vw_device;
1513 struct terminal *tm = vw->vw_terminal;
1520 * Changing fonts is a little tricky. We must prevent
1521 * simultaneous access to the device, so we must stop
1522 * the display timer and the terminal from accessing.
1523 * We need to switch fonts and grow our screen buffer.
1525 * XXX: Right now the code uses terminal_mute() to
1526 * prevent data from reaching the console driver while
1527 * resizing the screen buffer. This isn't elegant...
1531 if (vw->vw_flags & VWF_BUSY) {
1532 /* Another process is changing the font. */
1536 vw->vw_flags |= VWF_BUSY;
1539 vt_termsize(vd, vf, &size);
1540 vt_winsize(vd, vf, &wsz);
1542 /* Grow the screen buffer and terminal. */
1543 terminal_mute(tm, 1);
1544 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size);
1545 terminal_set_winsize_blank(tm, &wsz, 0, NULL);
1546 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor);
1547 terminal_mute(tm, 0);
1549 /* Actually apply the font to the current window. */
1551 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) {
1553 * In case vt_change_font called to update size we don't need
1554 * to update font link.
1556 vtfont_unref(vw->vw_font);
1557 vw->vw_font = vtfont_ref(vf);
1561 * Compute the drawable area and move the mouse cursor inside
1562 * it, in case the new area is smaller than the previous one.
1564 vt_compute_drawable_area(vw);
1565 vd->vd_mx = min(vd->vd_mx,
1566 vw->vw_draw_area.tr_end.tp_col -
1567 vw->vw_draw_area.tr_begin.tp_col - 1);
1568 vd->vd_my = min(vd->vd_my,
1569 vw->vw_draw_area.tr_end.tp_row -
1570 vw->vw_draw_area.tr_begin.tp_row - 1);
1572 /* Force a full redraw the next timer tick. */
1573 if (vd->vd_curwindow == vw) {
1574 vt_set_border(vw, TC_BLACK);
1575 vd->vd_flags |= VDF_INVALID;
1576 vt_resume_flush_timer(vw->vw_device, 0);
1578 vw->vw_flags &= ~VWF_BUSY;
1584 vt_proc_alive(struct vt_window *vw)
1588 if (vw->vw_smode.mode != VT_PROCESS)
1592 if ((p = pfind(vw->vw_pid)) != NULL)
1594 if (vw->vw_proc == p)
1597 vw->vw_smode.mode = VT_AUTO;
1598 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid);
1605 signal_vt_rel(struct vt_window *vw)
1608 if (vw->vw_smode.mode != VT_PROCESS)
1610 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1615 vw->vw_flags |= VWF_SWWAIT_REL;
1616 PROC_LOCK(vw->vw_proc);
1617 kern_psignal(vw->vw_proc, vw->vw_smode.relsig);
1618 PROC_UNLOCK(vw->vw_proc);
1619 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid);
1624 signal_vt_acq(struct vt_window *vw)
1627 if (vw->vw_smode.mode != VT_PROCESS)
1629 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
1630 cnavailable(vw->vw_terminal->consdev, FALSE);
1631 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) {
1636 vw->vw_flags |= VWF_SWWAIT_ACQ;
1637 PROC_LOCK(vw->vw_proc);
1638 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig);
1639 PROC_UNLOCK(vw->vw_proc);
1640 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid);
1645 finish_vt_rel(struct vt_window *vw, int release, int *s)
1648 if (vw->vw_flags & VWF_SWWAIT_REL) {
1649 vw->vw_flags &= ~VWF_SWWAIT_REL;
1651 callout_drain(&vw->vw_proc_dead_timer);
1652 vt_late_window_switch(vw->vw_switch_to);
1660 finish_vt_acq(struct vt_window *vw)
1663 if (vw->vw_flags & VWF_SWWAIT_ACQ) {
1664 vw->vw_flags &= ~VWF_SWWAIT_ACQ;
1670 #ifndef SC_NO_CUTPASTE
1672 vt_mouse_terminput_button(struct vt_device *vd, int button)
1674 struct vt_window *vw;
1676 char mouseb[6] = "\x1B[M";
1679 vw = vd->vd_curwindow;
1682 /* Translate to char position. */
1683 x = vd->vd_mx / vf->vf_width;
1684 y = vd->vd_my / vf->vf_height;
1685 /* Avoid overflow. */
1686 x = MIN(x, 255 - '!');
1687 y = MIN(y, 255 - '!');
1689 mouseb[3] = ' ' + button;
1690 mouseb[4] = '!' + x;
1691 mouseb[5] = '!' + y;
1693 for (i = 0; i < sizeof(mouseb); i++)
1694 terminal_input_char(vw->vw_terminal, mouseb[i]);
1698 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event,
1703 case MOUSE_BUTTON_EVENT:
1705 /* Mouse button pressed. */
1706 if (event & MOUSE_BUTTON1DOWN)
1707 vt_mouse_terminput_button(vd, 0);
1708 if (event & MOUSE_BUTTON2DOWN)
1709 vt_mouse_terminput_button(vd, 1);
1710 if (event & MOUSE_BUTTON3DOWN)
1711 vt_mouse_terminput_button(vd, 2);
1713 /* Mouse button released. */
1714 vt_mouse_terminput_button(vd, 3);
1718 case MOUSE_MOTION_EVENT:
1719 if (mouse->u.data.z < 0) {
1721 sc_mouse_input_button(vd, 64);
1722 } else if (mouse->u.data.z > 0) {
1724 sc_mouse_input_button(vd, 65);
1737 len = VD_PASTEBUFLEN(main_vd);
1738 buf = VD_PASTEBUF(main_vd);
1739 len /= sizeof(term_char_t);
1740 for (i = 0; i < len; i++) {
1743 terminal_input_char(main_vd->vd_curwindow->vw_terminal,
1749 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel)
1751 struct vt_device *vd;
1752 struct vt_window *vw;
1758 vw = vd->vd_curwindow;
1762 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS))
1764 * Either the mouse is disabled, or the window is in
1765 * "graphics mode". The graphics mode is usually set by
1766 * an X server, using the KDSETMODE ioctl.
1770 if (vf == NULL) /* Text mode. */
1774 * TODO: add flag about pointer position changed, to not redraw chars
1775 * under mouse pointer when nothing changed.
1778 if (vw->vw_mouse_level > 0)
1779 vt_mouse_terminput(vd, type, x, y, event, cnt);
1783 case MOUSE_MOTION_EVENT:
1788 vt_termsize(vd, vf, &size);
1793 x = MIN(x, (size.tp_col * vf->vf_width) - 1);
1794 y = MIN(y, (size.tp_row * vf->vf_height) - 1);
1798 if (vd->vd_mstate & MOUSE_BUTTON1DOWN)
1799 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE,
1800 vd->vd_mx / vf->vf_width,
1801 vd->vd_my / vf->vf_height);
1803 vt_resume_flush_timer(vw->vw_device, 0);
1805 case MOUSE_BUTTON_EVENT:
1813 case MOUSE_BUTTON1DOWN:
1816 mark = VTB_MARK_END;
1818 case 1: /* single click: start cut operation */
1819 mark = VTB_MARK_START;
1821 case 2: /* double click: cut a word */
1822 mark = VTB_MARK_WORD;
1824 case 3: /* triple click: cut a line */
1825 mark = VTB_MARK_ROW;
1829 case VT_MOUSE_PASTEBUTTON:
1838 case VT_MOUSE_EXTENDBUTTON:
1841 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN))
1842 mark = VTB_MARK_EXTEND;
1847 mark = VTB_MARK_EXTEND;
1855 /* Save buttons state. */
1857 vd->vd_mstate |= event;
1859 vd->vd_mstate &= ~event;
1861 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width,
1862 vd->vd_my / vf->vf_height) == 1) {
1864 * We have something marked to copy, so update pointer to
1865 * window with selection.
1867 vt_resume_flush_timer(vw->vw_device, 0);
1873 case VTB_MARK_EXTEND:
1876 /* Other types of mark do not require to copy data. */
1880 /* Get current selection size in bytes. */
1881 len = vtbuf_get_marked_len(&vw->vw_buf);
1885 /* Reallocate buffer only if old one is too small. */
1886 if (len > VD_PASTEBUFSZ(vd)) {
1887 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT,
1889 /* Update buffer size. */
1890 VD_PASTEBUFSZ(vd) = len;
1892 /* Request copy/paste buffer data, no more than `len' */
1893 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd),
1896 VD_PASTEBUFLEN(vd) = len;
1898 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */
1903 vt_mouse_state(int show)
1905 struct vt_device *vd;
1906 struct vt_window *vw;
1909 vw = vd->vd_curwindow;
1913 vw->vw_flags |= VWF_MOUSE_HIDE;
1916 vw->vw_flags &= ~VWF_MOUSE_HIDE;
1920 /* Mark mouse position as dirty. */
1921 vt_mark_mouse_position_as_dirty(vd);
1922 vt_resume_flush_timer(vw->vw_device, 0);
1927 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr,
1928 int nprot, vm_memattr_t *memattr)
1930 struct vt_window *vw = tm->tm_softc;
1931 struct vt_device *vd = vw->vw_device;
1933 if (vd->vd_driver->vd_fb_mmap)
1934 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot,
1941 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data,
1944 struct vt_window *vw = tm->tm_softc;
1945 struct vt_device *vd = vw->vw_device;
1948 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1949 defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
1960 cmd = VT_WAITACTIVE;
1986 ival = IOCPARM_IVAL(data);
1987 data = (caddr_t)&ival;
1992 case KDSETRAD: /* set keyboard repeat & delay rates (old) */
1993 if (*(int *)data & ~0x7f)
1998 case GIO_DEADKEYMAP:
1999 case PIO_DEADKEYMAP:
2004 case KDGETREPEAT: /* get keyboard repeat & delay rates */
2005 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */
2006 case KBADDKBD: /* add/remove keyboard to/from mux */
2011 kbd = kbd_get_keyboard(vd->vd_keyboard);
2013 error = kbdd_ioctl(kbd, cmd, data);
2015 if (error == ENOIOCTL) {
2016 if (cmd == KDGKBTYPE) {
2017 /* always return something? XXX */
2025 case KDGKBSTATE: { /* get keyboard state (locks) */
2028 if (vw == vd->vd_curwindow) {
2030 kbd = kbd_get_keyboard(vd->vd_keyboard);
2032 error = vt_save_kbd_state(vw, kbd);
2039 *(int *)data = vw->vw_kbdstate & LOCK_MASK;
2043 case KDSKBSTATE: { /* set keyboard state (locks) */
2046 state = *(int *)data;
2047 if (state & ~LOCK_MASK)
2050 vw->vw_kbdstate &= ~LOCK_MASK;
2051 vw->vw_kbdstate |= state;
2054 if (vw == vd->vd_curwindow) {
2056 kbd = kbd_get_keyboard(vd->vd_keyboard);
2058 error = vt_update_kbd_state(vw, kbd);
2064 case KDGETLED: { /* get keyboard LED status */
2067 if (vw == vd->vd_curwindow) {
2069 kbd = kbd_get_keyboard(vd->vd_keyboard);
2071 error = vt_save_kbd_leds(vw, kbd);
2078 *(int *)data = vw->vw_kbdstate & LED_MASK;
2082 case KDSETLED: { /* set keyboard LED status */
2085 leds = *(int *)data;
2086 if (leds & ~LED_MASK)
2089 vw->vw_kbdstate &= ~LED_MASK;
2090 vw->vw_kbdstate |= leds;
2093 if (vw == vd->vd_curwindow) {
2095 kbd = kbd_get_keyboard(vd->vd_keyboard);
2097 error = vt_update_kbd_leds(vw, kbd);
2106 if (vw == vd->vd_curwindow) {
2108 kbd = kbd_get_keyboard(vd->vd_keyboard);
2110 error = vt_save_kbd_mode(vw, kbd);
2117 *(int *)data = vw->vw_kbdmode;
2124 mode = *(int *)data;
2129 vw->vw_kbdmode = mode;
2132 if (vw == vd->vd_curwindow) {
2134 kbd = kbd_get_keyboard(vd->vd_keyboard);
2136 error = vt_update_kbd_mode(vw, kbd);
2146 case FBIO_GETWINORG: /* get frame buffer window origin */
2147 case FBIO_GETDISPSTART: /* get display start address */
2148 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */
2149 case FBIO_BLANK: /* blank display */
2150 if (vd->vd_driver->vd_fb_ioctl)
2151 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td));
2153 case CONS_BLANKTIME:
2158 *(int *)data = M_CG640x480;
2160 case CONS_BELLTYPE: /* set bell type sound */
2161 if ((*(int *)data) & CONS_QUIET_BELL)
2162 vd->vd_flags |= VDF_QUIET_BELL;
2164 vd->vd_flags &= ~VDF_QUIET_BELL;
2166 case CONS_GETINFO: {
2167 vid_info_t *vi = (vid_info_t *)data;
2168 if (vi->size != sizeof(struct vid_info))
2171 if (vw == vd->vd_curwindow) {
2172 kbd = kbd_get_keyboard(vd->vd_keyboard);
2174 vt_save_kbd_state(vw, kbd);
2177 vi->m_num = vd->vd_curwindow->vw_number + 1;
2178 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK;
2179 /* XXX: other fields! */
2183 *(int *)data = 0x200;
2188 case CONS_MOUSECTL: {
2189 mouse_info_t *mouse = (mouse_info_t*)data;
2192 * All the commands except MOUSE_SHOW nd MOUSE_HIDE
2193 * should not be applied to individual TTYs, but only to
2196 switch (mouse->operation) {
2198 if (vd->vd_flags & VDF_MOUSECURSOR) {
2199 vd->vd_flags &= ~VDF_MOUSECURSOR;
2200 #ifndef SC_NO_CUTPASTE
2201 vt_mouse_state(VT_MOUSE_HIDE);
2206 if (!(vd->vd_flags & VDF_MOUSECURSOR)) {
2207 vd->vd_flags |= VDF_MOUSECURSOR;
2208 vd->vd_mx = vd->vd_width / 2;
2209 vd->vd_my = vd->vd_height / 2;
2210 #ifndef SC_NO_CUTPASTE
2211 vt_mouse_state(VT_MOUSE_SHOW);
2222 if (vd->vd_flags & VDF_TEXTMODE)
2225 error = vtfont_load((void *)data, &vf);
2229 error = vt_change_font(vw, vf);
2233 case PIO_VFONT_DEFAULT: {
2234 /* Reset to default font. */
2235 error = vt_change_font(vw, &vt_font_default);
2239 scrmap_t *sm = (scrmap_t *)data;
2241 /* We don't have screen maps, so return a handcrafted one. */
2242 for (i = 0; i < 256; i++)
2248 * FIXME: This implementation is incomplete compared to
2251 switch (*(int *)data) {
2255 vw->vw_flags &= ~VWF_GRAPHICS;
2258 vw->vw_flags |= VWF_GRAPHICS;
2262 case KDENABIO: /* allow io operations */
2263 error = priv_check(td, PRIV_IO);
2266 error = securelevel_gt(td->td_ucred, 0);
2269 #if defined(__i386__)
2270 td->td_frame->tf_eflags |= PSL_IOPL;
2271 #elif defined(__amd64__)
2272 td->td_frame->tf_rflags |= PSL_IOPL;
2275 case KDDISABIO: /* disallow io operations (default) */
2276 #if defined(__i386__)
2277 td->td_frame->tf_eflags &= ~PSL_IOPL;
2278 #elif defined(__amd64__)
2279 td->td_frame->tf_rflags &= ~PSL_IOPL;
2282 case KDMKTONE: /* sound the bell */
2283 vtterm_beep(tm, *(u_int *)data);
2285 case KIOCSOUND: /* make tone (*data) hz */
2288 case CONS_SETKBD: /* set the new keyboard */
2291 if (vd->vd_keyboard != *(int *)data) {
2292 kbd = kbd_get_keyboard(*(int *)data);
2297 i = kbd_allocate(kbd->kb_name, kbd->kb_unit,
2298 (void *)vd, vt_kbdevent, vd);
2300 if (vd->vd_keyboard != -1) {
2301 vt_save_kbd_state(vd->vd_curwindow, kbd);
2302 kbd_release(kbd, (void *)vd);
2304 kbd = kbd_get_keyboard(i);
2305 vd->vd_keyboard = i;
2307 vt_update_kbd_mode(vd->vd_curwindow, kbd);
2308 vt_update_kbd_state(vd->vd_curwindow, kbd);
2310 error = EPERM; /* XXX */
2315 case CONS_RELKBD: /* release the current keyboard */
2318 if (vd->vd_keyboard != -1) {
2319 kbd = kbd_get_keyboard(vd->vd_keyboard);
2324 vt_save_kbd_state(vd->vd_curwindow, kbd);
2325 error = kbd_release(kbd, (void *)vd);
2327 vd->vd_keyboard = -1;
2334 win = *(int *)data - 1;
2335 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME,
2337 if ((win >= VT_MAXWINDOWS) || (win < 0))
2339 return (vt_proc_window_switch(vd->vd_windows[win]));
2342 *(int *)data = vd->vd_curwindow->vw_number + 1;
2345 *(int *)data = vw->vw_number + 1;
2348 /* TODO: Check current state, switching can be in progress. */
2349 if ((*(int *)data) == 0x01)
2350 vw->vw_flags |= VWF_VTYLOCK;
2351 else if ((*(int *)data) == 0x02)
2352 vw->vw_flags &= ~VWF_VTYLOCK;
2358 for (i = 0; i < VT_MAXWINDOWS; i++) {
2359 vw = vd->vd_windows[i];
2362 if (!(vw->vw_flags & VWF_OPENED)) {
2363 *(int *)data = vw->vw_number + 1;
2370 case VT_WAITACTIVE: {
2375 idx = *(unsigned int *)data;
2376 if (idx > VT_MAXWINDOWS)
2379 vw = vd->vd_windows[idx - 1];
2382 while (vd->vd_curwindow != vw && error == 0)
2383 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2387 case VT_SETMODE: { /* set screen switcher mode */
2388 struct vt_mode *mode;
2391 mode = (struct vt_mode *)data;
2392 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw));
2393 if (vw->vw_smode.mode == VT_PROCESS) {
2394 p1 = pfind(vw->vw_pid);
2395 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) {
2398 DPRINTF(5, "error EPERM\n");
2404 if (mode->mode == VT_AUTO) {
2405 vw->vw_smode.mode = VT_AUTO;
2408 DPRINTF(5, "VT_AUTO, ");
2409 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2410 cnavailable(vw->vw_terminal->consdev, TRUE);
2411 /* were we in the middle of the vty switching process? */
2412 if (finish_vt_rel(vw, TRUE, &s) == 0)
2413 DPRINTF(5, "reset WAIT_REL, ");
2414 if (finish_vt_acq(vw) == 0)
2415 DPRINTF(5, "reset WAIT_ACQ, ");
2417 } else if (mode->mode == VT_PROCESS) {
2418 if (!ISSIGVALID(mode->relsig) ||
2419 !ISSIGVALID(mode->acqsig) ||
2420 !ISSIGVALID(mode->frsig)) {
2421 DPRINTF(5, "error EINVAL\n");
2424 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid);
2425 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode));
2426 vw->vw_proc = td->td_proc;
2427 vw->vw_pid = vw->vw_proc->p_pid;
2428 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW])
2429 cnavailable(vw->vw_terminal->consdev, FALSE);
2431 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n",
2438 case VT_GETMODE: /* get screen switcher mode */
2439 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode));
2442 case VT_RELDISP: /* screen switcher ioctl */
2444 * This must be the current vty which is in the VT_PROCESS
2447 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode !=
2451 /* ...and this process is controlling it. */
2452 if (vw->vw_proc != td->td_proc) {
2456 switch(*(int *)data) {
2457 case VT_FALSE: /* user refuses to release screen, abort */
2458 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0)
2459 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n",
2460 SC_DRIVER_NAME, VT_UNIT(vw));
2462 case VT_TRUE: /* user has released screen, go on */
2463 /* finish_vt_rel(..., TRUE, ...) should not be locked */
2464 if (vw->vw_flags & VWF_SWWAIT_REL) {
2465 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0)
2466 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n",
2467 SC_DRIVER_NAME, VT_UNIT(vw));
2472 case VT_ACKACQ: /* acquire acknowledged, switch completed */
2473 if ((error = finish_vt_acq(vw)) == 0)
2474 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n",
2475 SC_DRIVER_NAME, VT_UNIT(vw));
2486 static struct vt_window *
2487 vt_allocate_window(struct vt_device *vd, unsigned int window)
2489 struct vt_window *vw;
2490 struct terminal *tm;
2494 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO);
2496 vw->vw_number = window;
2497 vw->vw_kbdmode = K_XLATE;
2499 if ((vd->vd_flags & VDF_TEXTMODE) == 0) {
2500 vw->vw_font = vtfont_ref(&vt_font_default);
2501 vt_compute_drawable_area(vw);
2504 vt_termsize(vd, vw->vw_font, &size);
2505 vt_winsize(vd, vw->vw_font, &wsz);
2506 vtbuf_init(&vw->vw_buf, &size);
2508 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw);
2509 terminal_set_winsize(tm, &wsz);
2510 vd->vd_windows[window] = vw;
2511 callout_init(&vw->vw_proc_dead_timer, 0);
2517 vt_upgrade(struct vt_device *vd)
2519 struct vt_window *vw;
2521 int register_handlers;
2523 if (!vty_enabled(VTY_VT))
2525 if (main_vd->vd_driver == NULL)
2528 for (i = 0; i < VT_MAXWINDOWS; i++) {
2529 vw = vd->vd_windows[i];
2532 vw = vt_allocate_window(vd, i);
2534 if (!(vw->vw_flags & VWF_READY)) {
2535 callout_init(&vw->vw_proc_dead_timer, 0);
2536 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw));
2537 vw->vw_flags |= VWF_READY;
2538 if (vw->vw_flags & VWF_CONSOLE) {
2539 /* For existing console window. */
2540 EVENTHANDLER_REGISTER(shutdown_pre_sync,
2541 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT);
2547 if (vd->vd_curwindow == NULL)
2548 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW];
2550 register_handlers = 0;
2551 if (!(vd->vd_flags & VDF_ASYNC)) {
2552 /* Attach keyboard. */
2553 vt_allocate_keyboard(vd);
2555 /* Init 25 Hz timer. */
2556 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0);
2558 /* Start timer when everything ready. */
2559 vd->vd_flags |= VDF_ASYNC;
2560 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd);
2561 vd->vd_timer_armed = 1;
2562 register_handlers = 1;
2567 /* Refill settings with new sizes. */
2570 if (register_handlers) {
2571 /* Register suspend/resume handlers. */
2572 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler,
2573 vd, EVENTHANDLER_PRI_ANY);
2574 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd,
2575 EVENTHANDLER_PRI_ANY);
2580 vt_resize(struct vt_device *vd)
2582 struct vt_window *vw;
2585 for (i = 0; i < VT_MAXWINDOWS; i++) {
2586 vw = vd->vd_windows[i];
2588 /* Assign default font to window, if not textmode. */
2589 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL)
2590 vw->vw_font = vtfont_ref(&vt_font_default);
2593 /* Resize terminal windows */
2594 while (vt_change_font(vw, vw->vw_font) == EBUSY) {
2595 DPRINTF(100, "%s: vt_change_font() is busy, "
2596 "window %d\n", __func__, i);
2602 vt_allocate(struct vt_driver *drv, void *softc)
2604 struct vt_device *vd;
2606 if (!vty_enabled(VTY_VT))
2609 if (main_vd->vd_driver == NULL) {
2610 main_vd->vd_driver = drv;
2611 printf("VT: initialize with new VT driver \"%s\".\n",
2615 * Check if have rights to replace current driver. For example:
2616 * it is bad idea to replace KMS driver with generic VGA one.
2618 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) {
2619 printf("VT: Driver priority %d too low. Current %d\n ",
2620 drv->vd_priority, main_vd->vd_driver->vd_priority);
2623 printf("VT: Replacing driver \"%s\" with new \"%s\".\n",
2624 main_vd->vd_driver->vd_name, drv->vd_name);
2628 if (vd->vd_flags & VDF_ASYNC) {
2629 /* Stop vt_flush periodic task. */
2631 vt_suspend_flush_timer(vd);
2634 * Mute current terminal until we done. vt_change_font (called
2635 * from vt_resize) will unmute it.
2637 terminal_mute(vd->vd_curwindow->vw_terminal, 1);
2641 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will
2645 vd->vd_flags &= ~VDF_TEXTMODE;
2647 vd->vd_driver = drv;
2648 vd->vd_softc = softc;
2649 vd->vd_driver->vd_init(vd);
2652 /* Update windows sizes and initialize last items. */
2656 if (vd->vd_flags & VDF_SPLASH)
2660 if (vd->vd_flags & VDF_ASYNC) {
2661 /* Allow to put chars now. */
2662 terminal_mute(vd->vd_curwindow->vw_terminal, 0);
2663 /* Rerun timer for screen updates. */
2664 vt_resume_flush_timer(vd, 0);
2668 * Register as console. If it already registered, cnadd() will ignore
2671 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal);
2675 vt_suspend_handler(void *priv)
2677 struct vt_device *vd;
2680 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL)
2681 vd->vd_driver->vd_suspend(vd);
2685 vt_resume_handler(void *priv)
2687 struct vt_device *vd;
2690 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL)
2691 vd->vd_driver->vd_resume(vd);
2695 vt_suspend(struct vt_device *vd)
2699 if (vt_suspendswitch == 0)
2701 /* Save current window. */
2702 vd->vd_savedwindow = vd->vd_curwindow;
2703 /* Ask holding process to free window and switch to console window */
2704 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]);
2706 /* Wait for the window switch to complete. */
2709 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0)
2710 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock);
2715 vt_resume(struct vt_device *vd)
2718 if (vt_suspendswitch == 0)
2720 /* Switch back to saved window */
2721 if (vd->vd_savedwindow != NULL)
2722 vt_proc_window_switch(vd->vd_savedwindow);
2723 vd->vd_savedwindow = NULL;