1 /*- 2 * Copyright (c) 2009, 2013 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Ed Schouten under sponsorship from the 6 * FreeBSD Foundation. 7 * 8 * Portions of this software were developed by Oleksandr Rybalko 9 * under sponsorship from the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_compat.h" 37 38 #include <sys/param.h> 39 #include <sys/consio.h> 40 #include <sys/eventhandler.h> 41 #include <sys/fbio.h> 42 #include <sys/kbio.h> 43 #include <sys/kdb.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/mutex.h> 48 #include <sys/priv.h> 49 #include <sys/proc.h> 50 #include <sys/reboot.h> 51 #include <sys/systm.h> 52 #include <sys/terminal.h> 53 54 #include <dev/kbd/kbdreg.h> 55 #include <dev/vt/vt.h> 56 57 #if defined(__i386__) || defined(__amd64__) 58 #include <machine/psl.h> 59 #include <machine/frame.h> 60 #endif 61 62 static tc_bell_t vtterm_bell; 63 static tc_cursor_t vtterm_cursor; 64 static tc_putchar_t vtterm_putchar; 65 static tc_fill_t vtterm_fill; 66 static tc_copy_t vtterm_copy; 67 static tc_param_t vtterm_param; 68 static tc_done_t vtterm_done; 69 70 static tc_cnprobe_t vtterm_cnprobe; 71 static tc_cngetc_t vtterm_cngetc; 72 73 static tc_opened_t vtterm_opened; 74 static tc_ioctl_t vtterm_ioctl; 75 static tc_mmap_t vtterm_mmap; 76 77 const struct terminal_class vt_termclass = { 78 .tc_bell = vtterm_bell, 79 .tc_cursor = vtterm_cursor, 80 .tc_putchar = vtterm_putchar, 81 .tc_fill = vtterm_fill, 82 .tc_copy = vtterm_copy, 83 .tc_param = vtterm_param, 84 .tc_done = vtterm_done, 85 86 .tc_cnprobe = vtterm_cnprobe, 87 .tc_cngetc = vtterm_cngetc, 88 89 .tc_opened = vtterm_opened, 90 .tc_ioctl = vtterm_ioctl, 91 .tc_mmap = vtterm_mmap, 92 }; 93 94 /* 95 * Use a constant timer of 25 Hz to redraw the screen. 96 * 97 * XXX: In theory we should only fire up the timer when there is really 98 * activity. Unfortunately we cannot always start timers. We really 99 * don't want to process kernel messages synchronously, because it 100 * really slows down the system. 101 */ 102 #define VT_TIMERFREQ 25 103 104 /* Bell pitch/duration. */ 105 #define VT_BELLDURATION ((5 * hz + 99) / 100) 106 #define VT_BELLPITCH 800 107 108 #define VT_LOCK(vd) mtx_lock(&(vd)->vd_lock) 109 #define VT_UNLOCK(vd) mtx_unlock(&(vd)->vd_lock) 110 111 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \ 112 (vw)->vw_number) 113 114 /* XXX while syscons is here. */ 115 int sc_txtmouse_no_retrace_wait; 116 117 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD, 0, "vt(9) parameters"); 118 VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)"); 119 VT_SYSCTL_INT(debug, 0, "vt(9) debug level"); 120 VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode"); 121 VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend"); 122 123 static unsigned int vt_unit = 0; 124 static MALLOC_DEFINE(M_VT, "vt", "vt device"); 125 struct vt_device *main_vd = NULL; 126 127 /* Boot logo. */ 128 extern unsigned int vt_logo_width; 129 extern unsigned int vt_logo_height; 130 extern unsigned int vt_logo_depth; 131 extern unsigned char vt_logo_image[]; 132 133 /* Font. */ 134 extern struct vt_font vt_font_default; 135 #ifndef SC_NO_CUTPASTE 136 extern struct mouse_cursor vt_default_mouse_pointer; 137 #endif 138 139 static int signal_vt_rel(struct vt_window *); 140 static int signal_vt_acq(struct vt_window *); 141 static int finish_vt_rel(struct vt_window *, int, int *); 142 static int finish_vt_acq(struct vt_window *); 143 static int vt_window_switch(struct vt_window *); 144 static int vt_late_window_switch(struct vt_window *); 145 static int vt_proc_alive(struct vt_window *); 146 static void vt_resize(struct vt_device *); 147 static void vt_update_static(void *); 148 149 SET_DECLARE(vt_drv_set, struct vt_driver); 150 151 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_DEFAULT_HEIGHT)) 152 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_DEFAULT_WIDTH)) 153 154 static struct terminal vt_consterm; 155 static struct vt_window vt_conswindow; 156 static struct vt_device vt_consdev = { 157 .vd_driver = NULL, 158 .vd_softc = NULL, 159 .vd_flags = VDF_INVALID, 160 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, }, 161 .vd_curwindow = &vt_conswindow, 162 .vd_markedwin = NULL, 163 .vd_kbstate = 0, 164 }; 165 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)]; 166 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE]; 167 static struct vt_window vt_conswindow = { 168 .vw_number = VT_CONSWINDOW, 169 .vw_flags = VWF_CONSOLE, 170 .vw_buf = { 171 .vb_buffer = vt_constextbuf, 172 .vb_rows = vt_constextbufrows, 173 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE, 174 .vb_curroffset = 0, 175 .vb_roffset = 0, 176 .vb_flags = VBF_STATIC, 177 .vb_mark_start = {.tp_row = 0, .tp_col = 0,}, 178 .vb_mark_end = {.tp_row = 0, .tp_col = 0,}, 179 .vb_scr_size = { 180 .tp_row = _VTDEFH, 181 .tp_col = _VTDEFW, 182 }, 183 }, 184 .vw_device = &vt_consdev, 185 .vw_terminal = &vt_consterm, 186 .vw_kbdmode = K_XLATE, 187 }; 188 static struct terminal vt_consterm = { 189 .tm_class = &vt_termclass, 190 .tm_softc = &vt_conswindow, 191 .tm_flags = TF_CONS, 192 }; 193 static struct consdev vt_consterm_consdev = { 194 .cn_ops = &termcn_cnops, 195 .cn_arg = &vt_consterm, 196 .cn_name = "ttyv0", 197 }; 198 199 /* Add to set of consoles. */ 200 DATA_SET(cons_set, vt_consterm_consdev); 201 202 /* 203 * Right after kmem is done to allow early drivers to use locking and allocate 204 * memory. 205 */ 206 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static, 207 &vt_consdev); 208 /* Delay until all devices attached, to not waste time. */ 209 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade, 210 &vt_consdev); 211 212 /* Initialize locks/mem depended members. */ 213 static void 214 vt_update_static(void *dummy) 215 { 216 217 if (main_vd != NULL) { 218 printf("VT: running with driver \"%s\".\n", 219 main_vd->vd_driver->vd_name); 220 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF); 221 cv_init(&main_vd->vd_winswitch, "vtwswt"); 222 } 223 } 224 225 static void 226 vt_switch_timer(void *arg) 227 { 228 229 vt_late_window_switch((struct vt_window *)arg); 230 } 231 232 static int 233 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw) 234 { 235 236 DPRINTF(40, "%s\n", __func__); 237 curvw->vw_switch_to = vw; 238 /* Set timer to allow switch in case when process hang. */ 239 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer, 240 vt_switch_timer, (void *)vw); 241 /* Notify process about vt switch attempt. */ 242 DPRINTF(30, "%s: Notify process.\n", __func__); 243 signal_vt_rel(curvw); 244 245 return (0); 246 } 247 248 static int 249 vt_window_postswitch(struct vt_window *vw) 250 { 251 252 signal_vt_acq(vw); 253 return (0); 254 } 255 256 /* vt_late_window_switch will done VT switching for regular case. */ 257 static int 258 vt_late_window_switch(struct vt_window *vw) 259 { 260 int ret; 261 262 callout_stop(&vw->vw_proc_dead_timer); 263 264 ret = vt_window_switch(vw); 265 if (ret) 266 return (ret); 267 268 /* Notify owner process about terminal availability. */ 269 if (vw->vw_smode.mode == VT_PROCESS) { 270 ret = vt_window_postswitch(vw); 271 } 272 return (ret); 273 } 274 275 /* Switch window. */ 276 static int 277 vt_proc_window_switch(struct vt_window *vw) 278 { 279 struct vt_window *curvw; 280 struct vt_device *vd; 281 int ret; 282 283 if (vw->vw_flags & VWF_VTYLOCK) 284 return (EBUSY); 285 286 vd = vw->vw_device; 287 curvw = vd->vd_curwindow; 288 289 /* Ask current process permitions to switch away. */ 290 if (curvw->vw_smode.mode == VT_PROCESS) { 291 DPRINTF(30, "%s: VT_PROCESS ", __func__); 292 if (vt_proc_alive(curvw) == FALSE) { 293 DPRINTF(30, "Dead. Cleaning."); 294 /* Dead */ 295 } else { 296 DPRINTF(30, "%s: Signaling process.\n", __func__); 297 /* Alive, try to ask him. */ 298 ret = vt_window_preswitch(vw, curvw); 299 /* Wait for process answer or timeout. */ 300 return (ret); 301 } 302 DPRINTF(30, "\n"); 303 } 304 305 ret = vt_late_window_switch(vw); 306 return (ret); 307 } 308 309 /* Switch window ignoring process locking. */ 310 static int 311 vt_window_switch(struct vt_window *vw) 312 { 313 struct vt_device *vd = vw->vw_device; 314 struct vt_window *curvw = vd->vd_curwindow; 315 keyboard_t *kbd; 316 317 VT_LOCK(vd); 318 if (curvw == vw) { 319 /* Nothing to do. */ 320 VT_UNLOCK(vd); 321 return (0); 322 } 323 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) { 324 VT_UNLOCK(vd); 325 return (EINVAL); 326 } 327 328 vd->vd_curwindow = vw; 329 vd->vd_flags |= VDF_INVALID; 330 cv_broadcast(&vd->vd_winswitch); 331 VT_UNLOCK(vd); 332 333 if (vd->vd_driver->vd_postswitch) 334 vd->vd_driver->vd_postswitch(vd); 335 336 /* Restore per-window keyboard mode. */ 337 mtx_lock(&Giant); 338 kbd = kbd_get_keyboard(vd->vd_keyboard); 339 if (kbd != NULL) { 340 kbdd_ioctl(kbd, KDSKBMODE, (void *)&vw->vw_kbdmode); 341 } 342 mtx_unlock(&Giant); 343 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number); 344 345 return (0); 346 } 347 348 static inline void 349 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size) 350 { 351 352 size->tp_row = vd->vd_height; 353 size->tp_col = vd->vd_width; 354 if (vf != NULL) { 355 size->tp_row /= vf->vf_height; 356 size->tp_col /= vf->vf_width; 357 } 358 } 359 360 static inline void 361 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size) 362 { 363 364 size->ws_row = size->ws_ypixel = vd->vd_height; 365 size->ws_col = size->ws_xpixel = vd->vd_width; 366 if (vf != NULL) { 367 size->ws_row /= vf->vf_height; 368 size->ws_col /= vf->vf_width; 369 } 370 } 371 372 static void 373 vt_scroll(struct vt_window *vw, int offset, int whence) 374 { 375 int diff; 376 term_pos_t size; 377 378 if ((vw->vw_flags & VWF_SCROLL) == 0) 379 return; 380 381 vt_termsize(vw->vw_device, vw->vw_font, &size); 382 383 diff = vthistory_seek(&vw->vw_buf, offset, whence); 384 /* 385 * Offset changed, please update Nth lines on sceen. 386 * +N - Nth lines at top; 387 * -N - Nth lines at bottom. 388 */ 389 390 if (diff < -size.tp_row || diff > size.tp_row) { 391 vw->vw_device->vd_flags |= VDF_INVALID; 392 return; 393 } 394 vw->vw_device->vd_flags |= VDF_INVALID; /*XXX*/ 395 } 396 397 static int 398 vt_machine_kbdevent(int c) 399 { 400 401 switch (c) { 402 case SPCLKEY | DBG: 403 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger"); 404 return (1); 405 case SPCLKEY | RBT: 406 /* XXX: Make this configurable! */ 407 shutdown_nice(0); 408 return (1); 409 case SPCLKEY | HALT: 410 shutdown_nice(RB_HALT); 411 return (1); 412 case SPCLKEY | PDWN: 413 shutdown_nice(RB_HALT|RB_POWEROFF); 414 return (1); 415 }; 416 417 return (0); 418 } 419 420 static void 421 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console) 422 { 423 struct vt_device *vd; 424 term_pos_t size; 425 426 vd = vw->vw_device; 427 /* Only special keys handled in ScrollLock mode */ 428 if ((c & SPCLKEY) == 0) 429 return; 430 431 c &= ~SPCLKEY; 432 433 if (console == 0) { 434 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) { 435 vw = vd->vd_windows[c - F_SCR]; 436 if (vw != NULL) 437 vt_proc_window_switch(vw); 438 return; 439 } 440 VT_LOCK(vd); 441 } 442 443 switch (c) { 444 case SLK: { 445 /* Turn scrolling off. */ 446 vt_scroll(vw, 0, VHS_END); 447 VTBUF_SLCK_DISABLE(&vw->vw_buf); 448 vw->vw_flags &= ~VWF_SCROLL; 449 break; 450 } 451 case FKEY | F(49): /* Home key. */ 452 vt_scroll(vw, 0, VHS_SET); 453 break; 454 case FKEY | F(50): /* Arrow up. */ 455 vt_scroll(vw, -1, VHS_CUR); 456 break; 457 case FKEY | F(51): /* Page up. */ 458 vt_termsize(vd, vw->vw_font, &size); 459 vt_scroll(vw, -size.tp_row, VHS_CUR); 460 break; 461 case FKEY | F(57): /* End key. */ 462 vt_scroll(vw, 0, VHS_END); 463 break; 464 case FKEY | F(58): /* Arrow down. */ 465 vt_scroll(vw, 1, VHS_CUR); 466 break; 467 case FKEY | F(59): /* Page down. */ 468 vt_termsize(vd, vw->vw_font, &size); 469 vt_scroll(vw, size.tp_row, VHS_CUR); 470 break; 471 } 472 473 if (console == 0) 474 VT_UNLOCK(vd); 475 } 476 477 static int 478 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c) 479 { 480 struct vt_window *vw = vd->vd_curwindow; 481 int state = 0; 482 483 #if VT_ALT_TO_ESC_HACK 484 if (c & RELKEY) { 485 switch (c & ~RELKEY) { 486 case (SPCLKEY | RALT): 487 if (vt_enable_altgr != 0) 488 break; 489 case (SPCLKEY | LALT): 490 vd->vd_kbstate &= ~ALKED; 491 } 492 /* Other keys ignored for RELKEY event. */ 493 return (0); 494 } else { 495 switch (c & ~RELKEY) { 496 case (SPCLKEY | RALT): 497 if (vt_enable_altgr != 0) 498 break; 499 case (SPCLKEY | LALT): 500 vd->vd_kbstate |= ALKED; 501 } 502 } 503 #else 504 if (c & RELKEY) 505 /* Other keys ignored for RELKEY event. */ 506 return (0); 507 #endif 508 509 if (vt_machine_kbdevent(c)) 510 return (0); 511 512 if (vw->vw_flags & VWF_SCROLL) { 513 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */); 514 /* Scroll mode keys handled, nothing to do more. */ 515 return (0); 516 } 517 518 if (c & SPCLKEY) { 519 c &= ~SPCLKEY; 520 521 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) { 522 vw = vd->vd_windows[c - F_SCR]; 523 if (vw != NULL) 524 vt_proc_window_switch(vw); 525 return (0); 526 } 527 528 switch (c) { 529 case SLK: { 530 531 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state); 532 VT_LOCK(vd); 533 if (state & SLKED) { 534 /* Turn scrolling on. */ 535 vw->vw_flags |= VWF_SCROLL; 536 VTBUF_SLCK_ENABLE(&vw->vw_buf); 537 } else { 538 /* Turn scrolling off. */ 539 vw->vw_flags &= ~VWF_SCROLL; 540 VTBUF_SLCK_DISABLE(&vw->vw_buf); 541 vt_scroll(vw, 0, VHS_END); 542 } 543 VT_UNLOCK(vd); 544 break; 545 } 546 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3): 547 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6): 548 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9): 549 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12): 550 /* F1 through F12 keys. */ 551 terminal_input_special(vw->vw_terminal, 552 TKEY_F1 + c - (FKEY | F(1))); 553 break; 554 case FKEY | F(49): /* Home key. */ 555 terminal_input_special(vw->vw_terminal, TKEY_HOME); 556 break; 557 case FKEY | F(50): /* Arrow up. */ 558 terminal_input_special(vw->vw_terminal, TKEY_UP); 559 break; 560 case FKEY | F(51): /* Page up. */ 561 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP); 562 break; 563 case FKEY | F(53): /* Arrow left. */ 564 terminal_input_special(vw->vw_terminal, TKEY_LEFT); 565 break; 566 case FKEY | F(55): /* Arrow right. */ 567 terminal_input_special(vw->vw_terminal, TKEY_RIGHT); 568 break; 569 case FKEY | F(57): /* End key. */ 570 terminal_input_special(vw->vw_terminal, TKEY_END); 571 break; 572 case FKEY | F(58): /* Arrow down. */ 573 terminal_input_special(vw->vw_terminal, TKEY_DOWN); 574 break; 575 case FKEY | F(59): /* Page down. */ 576 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN); 577 break; 578 case FKEY | F(60): /* Insert key. */ 579 terminal_input_special(vw->vw_terminal, TKEY_INSERT); 580 break; 581 case FKEY | F(61): /* Delete key. */ 582 terminal_input_special(vw->vw_terminal, TKEY_DELETE); 583 break; 584 } 585 } else if (KEYFLAGS(c) == 0) { 586 /* Don't do UTF-8 conversion when doing raw mode. */ 587 if (vw->vw_kbdmode == K_XLATE) { 588 #if VT_ALT_TO_ESC_HACK 589 if (vd->vd_kbstate & ALKED) { 590 /* 591 * Prepend ESC sequence if one of ALT keys down. 592 */ 593 terminal_input_char(vw->vw_terminal, 0x1b); 594 } 595 #endif 596 597 terminal_input_char(vw->vw_terminal, KEYCHAR(c)); 598 } else 599 terminal_input_raw(vw->vw_terminal, c); 600 } 601 return (0); 602 } 603 604 static int 605 vt_kbdevent(keyboard_t *kbd, int event, void *arg) 606 { 607 struct vt_device *vd = arg; 608 int c; 609 610 switch (event) { 611 case KBDIO_KEYINPUT: 612 break; 613 case KBDIO_UNLOADING: 614 mtx_lock(&Giant); 615 vd->vd_keyboard = -1; 616 kbd_release(kbd, (void *)&vd->vd_keyboard); 617 mtx_unlock(&Giant); 618 return (0); 619 default: 620 return (EINVAL); 621 } 622 623 while ((c = kbdd_read_char(kbd, 0)) != NOKEY) 624 vt_processkey(kbd, vd, c); 625 626 return (0); 627 } 628 629 static int 630 vt_allocate_keyboard(struct vt_device *vd) 631 { 632 int idx0, idx; 633 keyboard_t *k0, *k; 634 keyboard_info_t ki; 635 636 idx0 = kbd_allocate("kbdmux", -1, (void *)&vd->vd_keyboard, 637 vt_kbdevent, vd); 638 /* XXX: kb_token lost */ 639 vd->vd_keyboard = idx0; 640 if (idx0 != -1) { 641 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0); 642 k0 = kbd_get_keyboard(idx0); 643 644 for (idx = kbd_find_keyboard2("*", -1, 0); 645 idx != -1; 646 idx = kbd_find_keyboard2("*", -1, idx + 1)) { 647 k = kbd_get_keyboard(idx); 648 649 if (idx == idx0 || KBD_IS_BUSY(k)) 650 continue; 651 652 bzero(&ki, sizeof(ki)); 653 strcpy(ki.kb_name, k->kb_name); 654 ki.kb_unit = k->kb_unit; 655 656 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki); 657 } 658 } else { 659 DPRINTF(20, "%s: no kbdmux allocated\n", __func__); 660 idx0 = kbd_allocate("*", -1, (void *)&vd->vd_keyboard, 661 vt_kbdevent, vd); 662 } 663 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard); 664 665 return (idx0); 666 } 667 668 static void 669 vtterm_bell(struct terminal *tm) 670 { 671 struct vt_window *vw = tm->tm_softc; 672 struct vt_device *vd = vw->vw_device; 673 674 if (vd->vd_flags & VDF_QUIET_BELL) 675 return; 676 677 sysbeep(1193182 / VT_BELLPITCH, VT_BELLDURATION); 678 } 679 680 static void 681 vtterm_beep(struct terminal *tm, u_int param) 682 { 683 u_int freq, period; 684 685 if ((param == 0) || ((param & 0xffff) == 0)) { 686 vtterm_bell(tm); 687 return; 688 } 689 690 period = ((param >> 16) & 0xffff) * hz / 1000; 691 freq = 1193182 / (param & 0xffff); 692 693 sysbeep(freq, period); 694 } 695 696 static void 697 vtterm_cursor(struct terminal *tm, const term_pos_t *p) 698 { 699 struct vt_window *vw = tm->tm_softc; 700 701 vtbuf_cursor_position(&vw->vw_buf, p); 702 } 703 704 static void 705 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c) 706 { 707 struct vt_window *vw = tm->tm_softc; 708 709 vtbuf_putchar(&vw->vw_buf, p, c); 710 } 711 712 static void 713 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c) 714 { 715 struct vt_window *vw = tm->tm_softc; 716 717 vtbuf_fill_locked(&vw->vw_buf, r, c); 718 } 719 720 static void 721 vtterm_copy(struct terminal *tm, const term_rect_t *r, 722 const term_pos_t *p) 723 { 724 struct vt_window *vw = tm->tm_softc; 725 726 vtbuf_copy(&vw->vw_buf, r, p); 727 } 728 729 static void 730 vtterm_param(struct terminal *tm, int cmd, unsigned int arg) 731 { 732 struct vt_window *vw = tm->tm_softc; 733 734 switch (cmd) { 735 case TP_SHOWCURSOR: 736 vtbuf_cursor_visibility(&vw->vw_buf, arg); 737 break; 738 case TP_MOUSE: 739 vw->vw_mouse_level = arg; 740 break; 741 } 742 } 743 744 static inline void 745 vt_determine_colors(term_char_t c, int cursor, 746 term_color_t *fg, term_color_t *bg) 747 { 748 term_color_t tmp; 749 int invert; 750 751 invert = 0; 752 753 *fg = TCHAR_FGCOLOR(c); 754 if (TCHAR_FORMAT(c) & TF_BOLD) 755 *fg = TCOLOR_LIGHT(*fg); 756 *bg = TCHAR_BGCOLOR(c); 757 758 if (TCHAR_FORMAT(c) & TF_REVERSE) 759 invert ^= 1; 760 if (cursor) 761 invert ^= 1; 762 763 if (invert) { 764 tmp = *fg; 765 *fg = *bg; 766 *bg = tmp; 767 } 768 } 769 770 static void 771 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c, 772 int iscursor, unsigned int row, unsigned int col) 773 { 774 term_color_t fg, bg; 775 776 vt_determine_colors(c, iscursor, &fg, &bg); 777 778 if (vf != NULL) { 779 const uint8_t *src; 780 vt_axis_t top, left; 781 782 src = vtfont_lookup(vf, c); 783 784 /* 785 * Align the terminal to the centre of the screen. 786 * Fonts may not always be able to fill the entire 787 * screen. 788 */ 789 top = row * vf->vf_height + vd->vd_offset.tp_row; 790 left = col * vf->vf_width + vd->vd_offset.tp_col; 791 792 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left, 793 vf->vf_width, vf->vf_height, fg, bg); 794 } else { 795 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c), 796 row, col, fg, bg); 797 } 798 } 799 800 static void 801 vt_flush(struct vt_device *vd) 802 { 803 struct vt_window *vw; 804 struct vt_font *vf; 805 struct vt_bufmask tmask; 806 unsigned int row, col; 807 term_rect_t tarea; 808 term_pos_t size; 809 term_char_t *r; 810 #ifndef SC_NO_CUTPASTE 811 struct mouse_cursor *m; 812 int bpl, h, w; 813 #endif 814 815 vw = vd->vd_curwindow; 816 if (vw == NULL) 817 return; 818 vf = vw->vw_font; 819 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL)) 820 return; 821 822 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY) 823 return; 824 825 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask); 826 vt_termsize(vd, vf, &size); 827 828 /* Force a full redraw when the screen contents are invalid. */ 829 if (vd->vd_flags & VDF_INVALID) { 830 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0; 831 tarea.tr_end = size; 832 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY; 833 834 vd->vd_flags &= ~VDF_INVALID; 835 } 836 837 #ifndef SC_NO_CUTPASTE 838 if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) { 839 /* Mark last mouse position as dirty to erase. */ 840 vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx, 841 vd->vd_mdirtyy); 842 } 843 #endif 844 845 for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) { 846 if (!VTBUF_DIRTYROW(&tmask, row)) 847 continue; 848 r = VTBUF_GET_ROW(&vw->vw_buf, row); 849 for (col = tarea.tr_begin.tp_col; 850 col < tarea.tr_end.tp_col; col++) { 851 if (!VTBUF_DIRTYCOL(&tmask, col)) 852 continue; 853 854 vt_bitblt_char(vd, vf, r[col], 855 VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col); 856 } 857 } 858 859 #ifndef SC_NO_CUTPASTE 860 /* Mouse disabled. */ 861 if (vw->vw_flags & VWF_MOUSE_HIDE) 862 return; 863 864 /* No mouse for DDB. */ 865 if (kdb_active || panicstr != NULL) 866 return; 867 868 if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) == 869 VDF_MOUSECURSOR) { 870 m = &vt_default_mouse_pointer; 871 bpl = (m->w + 7) >> 3; /* Bytes per source line. */ 872 w = m->w; 873 h = m->h; 874 875 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width)) 876 w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1; 877 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height)) 878 h = (size.tp_row * vf->vf_height) - vd->vd_my - 1; 879 880 vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl, 881 vd->vd_offset.tp_row + vd->vd_my, 882 vd->vd_offset.tp_col + vd->vd_mx, 883 w, h, TC_WHITE, TC_BLACK); 884 /* Save point of last mouse cursor to erase it later. */ 885 vd->vd_mdirtyx = vd->vd_mx / vf->vf_width; 886 vd->vd_mdirtyy = vd->vd_my / vf->vf_height; 887 } 888 #endif 889 } 890 891 static void 892 vt_timer(void *arg) 893 { 894 struct vt_device *vd; 895 896 vd = arg; 897 /* Update screen if required. */ 898 vt_flush(vd); 899 900 /* Schedule for next update. */ 901 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ); 902 } 903 904 static void 905 vtterm_done(struct terminal *tm) 906 { 907 struct vt_window *vw = tm->tm_softc; 908 struct vt_device *vd = vw->vw_device; 909 910 if (kdb_active || panicstr != NULL) { 911 /* Switch to the debugger. */ 912 if (vd->vd_curwindow != vw) { 913 vd->vd_curwindow = vw; 914 vd->vd_flags |= VDF_INVALID; 915 if (vd->vd_driver->vd_postswitch) 916 vd->vd_driver->vd_postswitch(vd); 917 } 918 vd->vd_flags &= ~VDF_SPLASH; 919 vt_flush(vd); 920 } else if (!(vd->vd_flags & VDF_ASYNC)) { 921 vt_flush(vd); 922 } 923 } 924 925 #ifdef DEV_SPLASH 926 static void 927 vtterm_splash(struct vt_device *vd) 928 { 929 vt_axis_t top, left; 930 931 /* Display a nice boot splash. */ 932 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) { 933 934 top = (vd->vd_height - vt_logo_height) / 2; 935 left = (vd->vd_width - vt_logo_width) / 2; 936 switch (vt_logo_depth) { 937 case 1: 938 /* XXX: Unhardcode colors! */ 939 vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0, 940 top, left, vt_logo_width, vt_logo_height, 0xf, 0x0); 941 } 942 vd->vd_flags |= VDF_SPLASH; 943 } 944 } 945 #endif 946 947 948 static void 949 vtterm_cnprobe(struct terminal *tm, struct consdev *cp) 950 { 951 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL; 952 struct vt_window *vw = tm->tm_softc; 953 struct vt_device *vd = vw->vw_device; 954 struct winsize wsz; 955 956 if (vd->vd_flags & VDF_INITIALIZED) 957 /* Initialization already done. */ 958 return; 959 960 SET_FOREACH(vtdlist, vt_drv_set) { 961 vtd = *vtdlist; 962 if (vtd->vd_probe == NULL) 963 continue; 964 if (vtd->vd_probe(vd) == CN_DEAD) 965 continue; 966 if ((vtdbest == NULL) || 967 (vtd->vd_priority > vtdbest->vd_priority)) 968 vtdbest = vtd; 969 } 970 if (vtdbest == NULL) { 971 cp->cn_pri = CN_DEAD; 972 vd->vd_flags |= VDF_DEAD; 973 return; 974 } 975 976 vd->vd_driver = vtdbest; 977 978 cp->cn_pri = vd->vd_driver->vd_init(vd); 979 if (cp->cn_pri == CN_DEAD) { 980 vd->vd_flags |= VDF_DEAD; 981 return; 982 } 983 984 /* Initialize any early-boot keyboard drivers */ 985 kbd_configure(KB_CONF_PROBE_ONLY); 986 987 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1); 988 vd->vd_windows[VT_CONSWINDOW] = vw; 989 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw)); 990 991 if (!(vd->vd_flags & VDF_TEXTMODE)) 992 vw->vw_font = vtfont_ref(&vt_font_default); 993 994 vtbuf_init_early(&vw->vw_buf); 995 vt_winsize(vd, vw->vw_font, &wsz); 996 terminal_set_winsize(tm, &wsz); 997 998 #ifdef DEV_SPLASH 999 vtterm_splash(vd); 1000 #endif 1001 1002 vd->vd_flags |= VDF_INITIALIZED; 1003 main_vd = vd; 1004 } 1005 1006 static int 1007 vtterm_cngetc(struct terminal *tm) 1008 { 1009 struct vt_window *vw = tm->tm_softc; 1010 struct vt_device *vd = vw->vw_device; 1011 keyboard_t *kbd; 1012 int state; 1013 u_int c; 1014 1015 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1016 return (*vw->vw_kbdsq++); 1017 1018 state = 0; 1019 /* Make sure the splash screen is not there. */ 1020 if (vd->vd_flags & VDF_SPLASH) { 1021 /* Remove splash */ 1022 vd->vd_flags &= ~VDF_SPLASH; 1023 /* Mark screen as invalid to force update */ 1024 vd->vd_flags |= VDF_INVALID; 1025 vt_flush(vd); 1026 } 1027 1028 /* Stripped down keyboard handler. */ 1029 kbd = kbd_get_keyboard(vd->vd_keyboard); 1030 if (kbd == NULL) 1031 return (-1); 1032 1033 /* Force keyboard input mode to K_XLATE */ 1034 c = K_XLATE; 1035 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c); 1036 1037 /* Switch the keyboard to polling to make it work here. */ 1038 kbdd_poll(kbd, TRUE); 1039 c = kbdd_read_char(kbd, 0); 1040 kbdd_poll(kbd, FALSE); 1041 if (c & RELKEY) 1042 return (-1); 1043 1044 if (vw->vw_flags & VWF_SCROLL) { 1045 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */); 1046 vt_flush(vd); 1047 return (-1); 1048 } 1049 1050 /* Stripped down handling of vt_kbdevent(), without locking, etc. */ 1051 if (c & SPCLKEY) { 1052 switch (c) { 1053 case SPCLKEY | SLK: 1054 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state); 1055 if (state & SLKED) { 1056 /* Turn scrolling on. */ 1057 vw->vw_flags |= VWF_SCROLL; 1058 VTBUF_SLCK_ENABLE(&vw->vw_buf); 1059 } else { 1060 /* Turn scrolling off. */ 1061 vt_scroll(vw, 0, VHS_END); 1062 vw->vw_flags &= ~VWF_SCROLL; 1063 VTBUF_SLCK_DISABLE(&vw->vw_buf); 1064 } 1065 break; 1066 /* XXX: KDB can handle history. */ 1067 case SPCLKEY | FKEY | F(50): /* Arrow up. */ 1068 vw->vw_kbdsq = "\x1b[A"; 1069 break; 1070 case SPCLKEY | FKEY | F(58): /* Arrow down. */ 1071 vw->vw_kbdsq = "\x1b[B"; 1072 break; 1073 case SPCLKEY | FKEY | F(55): /* Arrow right. */ 1074 vw->vw_kbdsq = "\x1b[C"; 1075 break; 1076 case SPCLKEY | FKEY | F(53): /* Arrow left. */ 1077 vw->vw_kbdsq = "\x1b[D"; 1078 break; 1079 } 1080 1081 /* Force refresh to make scrollback work. */ 1082 vt_flush(vd); 1083 } else if (KEYFLAGS(c) == 0) { 1084 return (KEYCHAR(c)); 1085 } 1086 1087 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1088 return (*vw->vw_kbdsq++); 1089 1090 return (-1); 1091 } 1092 1093 static void 1094 vtterm_opened(struct terminal *tm, int opened) 1095 { 1096 struct vt_window *vw = tm->tm_softc; 1097 struct vt_device *vd = vw->vw_device; 1098 1099 VT_LOCK(vd); 1100 vd->vd_flags &= ~VDF_SPLASH; 1101 if (opened) 1102 vw->vw_flags |= VWF_OPENED; 1103 else { 1104 vw->vw_flags &= ~VWF_OPENED; 1105 /* TODO: finish ACQ/REL */ 1106 } 1107 VT_UNLOCK(vd); 1108 } 1109 1110 static int 1111 vt_change_font(struct vt_window *vw, struct vt_font *vf) 1112 { 1113 struct vt_device *vd = vw->vw_device; 1114 struct terminal *tm = vw->vw_terminal; 1115 term_pos_t size; 1116 struct winsize wsz; 1117 1118 /* 1119 * Changing fonts. 1120 * 1121 * Changing fonts is a little tricky. We must prevent 1122 * simultaneous access to the device, so we must stop 1123 * the display timer and the terminal from accessing. 1124 * We need to switch fonts and grow our screen buffer. 1125 * 1126 * XXX: Right now the code uses terminal_mute() to 1127 * prevent data from reaching the console driver while 1128 * resizing the screen buffer. This isn't elegant... 1129 */ 1130 1131 VT_LOCK(vd); 1132 if (vw->vw_flags & VWF_BUSY) { 1133 /* Another process is changing the font. */ 1134 VT_UNLOCK(vd); 1135 return (EBUSY); 1136 } 1137 if (vw->vw_font == NULL) { 1138 /* Our device doesn't need fonts. */ 1139 VT_UNLOCK(vd); 1140 return (ENOTTY); 1141 } 1142 vw->vw_flags |= VWF_BUSY; 1143 VT_UNLOCK(vd); 1144 1145 vt_termsize(vd, vf, &size); 1146 vt_winsize(vd, vf, &wsz); 1147 /* Save offset to font aligned area. */ 1148 vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2; 1149 vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2; 1150 1151 /* Grow the screen buffer and terminal. */ 1152 terminal_mute(tm, 1); 1153 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size); 1154 terminal_set_winsize_blank(tm, &wsz, 0); 1155 terminal_mute(tm, 0); 1156 1157 /* Actually apply the font to the current window. */ 1158 VT_LOCK(vd); 1159 vtfont_unref(vw->vw_font); 1160 vw->vw_font = vtfont_ref(vf); 1161 1162 /* Force a full redraw the next timer tick. */ 1163 if (vd->vd_curwindow == vw) 1164 vd->vd_flags |= VDF_INVALID; 1165 vw->vw_flags &= ~VWF_BUSY; 1166 VT_UNLOCK(vd); 1167 return (0); 1168 } 1169 1170 static int 1171 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c) 1172 { 1173 struct vt_device *vd = vw->vw_device; 1174 int l, r, t, b, w, h; 1175 1176 if (vd->vd_driver->vd_drawrect == NULL) 1177 return (ENOTSUP); 1178 1179 w = vd->vd_width - 1; 1180 h = vd->vd_height - 1; 1181 l = vd->vd_offset.tp_col - 1; 1182 r = w - l; 1183 t = vd->vd_offset.tp_row - 1; 1184 b = h - t; 1185 1186 vd->vd_driver->vd_drawrect(vd, 0, 0, w, t, 1, c); /* Top bar. */ 1187 vd->vd_driver->vd_drawrect(vd, 0, t, l, b, 1, c); /* Left bar. */ 1188 vd->vd_driver->vd_drawrect(vd, r, t, w, b, 1, c); /* Right bar. */ 1189 vd->vd_driver->vd_drawrect(vd, 0, b, w, h, 1, c); /* Bottom bar. */ 1190 1191 return (0); 1192 } 1193 1194 static int 1195 vt_proc_alive(struct vt_window *vw) 1196 { 1197 struct proc *p; 1198 1199 if (vw->vw_smode.mode != VT_PROCESS) 1200 return (FALSE); 1201 1202 if (vw->vw_proc) { 1203 if ((p = pfind(vw->vw_pid)) != NULL) 1204 PROC_UNLOCK(p); 1205 if (vw->vw_proc == p) 1206 return (TRUE); 1207 vw->vw_proc = NULL; 1208 vw->vw_smode.mode = VT_AUTO; 1209 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid); 1210 vw->vw_pid = 0; 1211 } 1212 return (FALSE); 1213 } 1214 1215 static int 1216 signal_vt_rel(struct vt_window *vw) 1217 { 1218 1219 if (vw->vw_smode.mode != VT_PROCESS) 1220 return (FALSE); 1221 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 1222 vw->vw_proc = NULL; 1223 vw->vw_pid = 0; 1224 return (TRUE); 1225 } 1226 vw->vw_flags |= VWF_SWWAIT_REL; 1227 PROC_LOCK(vw->vw_proc); 1228 kern_psignal(vw->vw_proc, vw->vw_smode.relsig); 1229 PROC_UNLOCK(vw->vw_proc); 1230 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid); 1231 return (TRUE); 1232 } 1233 1234 static int 1235 signal_vt_acq(struct vt_window *vw) 1236 { 1237 1238 if (vw->vw_smode.mode != VT_PROCESS) 1239 return (FALSE); 1240 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1241 cnavailable(vw->vw_terminal->consdev, FALSE); 1242 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 1243 vw->vw_proc = NULL; 1244 vw->vw_pid = 0; 1245 return (TRUE); 1246 } 1247 vw->vw_flags |= VWF_SWWAIT_ACQ; 1248 PROC_LOCK(vw->vw_proc); 1249 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig); 1250 PROC_UNLOCK(vw->vw_proc); 1251 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid); 1252 return (TRUE); 1253 } 1254 1255 static int 1256 finish_vt_rel(struct vt_window *vw, int release, int *s) 1257 { 1258 1259 if (vw->vw_flags & VWF_SWWAIT_REL) { 1260 vw->vw_flags &= ~VWF_SWWAIT_REL; 1261 if (release) { 1262 callout_drain(&vw->vw_proc_dead_timer); 1263 vt_late_window_switch(vw->vw_switch_to); 1264 } 1265 return (0); 1266 } 1267 return (EINVAL); 1268 } 1269 1270 static int 1271 finish_vt_acq(struct vt_window *vw) 1272 { 1273 1274 if (vw->vw_flags & VWF_SWWAIT_ACQ) { 1275 vw->vw_flags &= ~VWF_SWWAIT_ACQ; 1276 return (0); 1277 } 1278 return (EINVAL); 1279 } 1280 1281 #ifndef SC_NO_CUTPASTE 1282 static void 1283 vt_mouse_terminput_button(struct vt_device *vd, int button) 1284 { 1285 struct vt_window *vw; 1286 struct vt_font *vf; 1287 char mouseb[6] = "\x1B[M"; 1288 int i, x, y; 1289 1290 vw = vd->vd_curwindow; 1291 vf = vw->vw_font; 1292 1293 /* Translate to char position. */ 1294 x = vd->vd_mx / vf->vf_width; 1295 y = vd->vd_my / vf->vf_height; 1296 /* Avoid overflow. */ 1297 x = MIN(x, 255 - '!'); 1298 y = MIN(y, 255 - '!'); 1299 1300 mouseb[3] = ' ' + button; 1301 mouseb[4] = '!' + x; 1302 mouseb[5] = '!' + y; 1303 1304 for (i = 0; i < sizeof(mouseb); i++ ) 1305 terminal_input_char(vw->vw_terminal, mouseb[i]); 1306 } 1307 1308 static void 1309 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event, 1310 int cnt) 1311 { 1312 1313 switch (type) { 1314 case MOUSE_BUTTON_EVENT: 1315 if (cnt > 0) { 1316 /* Mouse button pressed. */ 1317 if (event & MOUSE_BUTTON1DOWN) 1318 vt_mouse_terminput_button(vd, 0); 1319 if (event & MOUSE_BUTTON2DOWN) 1320 vt_mouse_terminput_button(vd, 1); 1321 if (event & MOUSE_BUTTON3DOWN) 1322 vt_mouse_terminput_button(vd, 2); 1323 } else { 1324 /* Mouse button released. */ 1325 vt_mouse_terminput_button(vd, 3); 1326 } 1327 break; 1328 #ifdef notyet 1329 case MOUSE_MOTION_EVENT: 1330 if (mouse->u.data.z < 0) { 1331 /* Scroll up. */ 1332 sc_mouse_input_button(vd, 64); 1333 } else if (mouse->u.data.z > 0) { 1334 /* Scroll down. */ 1335 sc_mouse_input_button(vd, 65); 1336 } 1337 break; 1338 #endif 1339 } 1340 } 1341 1342 void 1343 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel) 1344 { 1345 struct vt_device *vd; 1346 struct vt_window *vw; 1347 struct vt_font *vf; 1348 term_pos_t size; 1349 term_char_t *buf; 1350 int i, len, mark; 1351 1352 vd = main_vd; 1353 vw = vd->vd_curwindow; 1354 vf = vw->vw_font; 1355 mark = 0; 1356 1357 if (vw->vw_flags & VWF_MOUSE_HIDE) 1358 return; /* Mouse disabled. */ 1359 1360 if (vf == NULL) /* Text mode. */ 1361 return; 1362 1363 /* 1364 * TODO: add flag about pointer position changed, to not redraw chars 1365 * under mouse pointer when nothing changed. 1366 */ 1367 1368 if (vw->vw_mouse_level > 0) 1369 vt_mouse_terminput(vd, type, x, y, event, cnt); 1370 1371 switch (type) { 1372 case MOUSE_ACTION: 1373 case MOUSE_MOTION_EVENT: 1374 /* Movement */ 1375 x += vd->vd_mx; 1376 y += vd->vd_my; 1377 1378 vt_termsize(vd, vf, &size); 1379 1380 /* Apply limits. */ 1381 x = MAX(x, 0); 1382 y = MAX(y, 0); 1383 x = MIN(x, (size.tp_col * vf->vf_width) - 1); 1384 y = MIN(y, (size.tp_row * vf->vf_height) - 1); 1385 1386 vd->vd_mx = x; 1387 vd->vd_my = y; 1388 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) && 1389 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE, 1390 vd->vd_mx / vf->vf_width, 1391 vd->vd_my / vf->vf_height) == 1)) { 1392 1393 /* 1394 * We have something marked to copy, so update pointer 1395 * to window with selection. 1396 */ 1397 vd->vd_markedwin = vw; 1398 } 1399 return; /* Done */ 1400 case MOUSE_BUTTON_EVENT: 1401 /* Buttons */ 1402 break; 1403 default: 1404 return; /* Done */ 1405 } 1406 1407 switch (event) { 1408 case MOUSE_BUTTON1DOWN: 1409 switch (cnt % 4) { 1410 case 0: /* up */ 1411 mark = VTB_MARK_END; 1412 break; 1413 case 1: /* single click: start cut operation */ 1414 mark = VTB_MARK_START; 1415 break; 1416 case 2: /* double click: cut a word */ 1417 mark = VTB_MARK_WORD; 1418 break; 1419 case 3: /* triple click: cut a line */ 1420 mark = VTB_MARK_ROW; 1421 break; 1422 } 1423 break; 1424 case VT_MOUSE_PASTEBUTTON: 1425 switch (cnt) { 1426 case 0: /* up */ 1427 break; 1428 default: 1429 if (vd->vd_markedwin == NULL) 1430 return; 1431 /* Get current selecton size in bytes. */ 1432 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf); 1433 if (len <= 0) 1434 return; 1435 1436 buf = malloc(len, M_VT, M_WAITOK | M_ZERO); 1437 /* Request cupy/paste buffer data, no more than `len' */ 1438 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf, 1439 len); 1440 1441 len /= sizeof(term_char_t); 1442 for (i = 0; i < len; i++ ) { 1443 if (buf[i] == '\0') 1444 continue; 1445 terminal_input_char(vw->vw_terminal, buf[i]); 1446 } 1447 1448 /* Done, so cleanup. */ 1449 free(buf, M_VT); 1450 break; 1451 } 1452 return; /* Done */ 1453 case VT_MOUSE_EXTENDBUTTON: 1454 switch (cnt) { 1455 case 0: /* up */ 1456 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN)) 1457 mark = VTB_MARK_EXTEND; 1458 else 1459 mark = 0; 1460 break; 1461 default: 1462 mark = VTB_MARK_EXTEND; 1463 break; 1464 } 1465 break; 1466 default: 1467 return; /* Done */ 1468 } 1469 1470 /* Save buttons state. */ 1471 if (cnt > 0) 1472 vd->vd_mstate |= event; 1473 else 1474 vd->vd_mstate &= ~event; 1475 1476 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width, 1477 vd->vd_my / vf->vf_height) == 1) { 1478 /* 1479 * We have something marked to copy, so update pointer to 1480 * window with selection. 1481 */ 1482 vd->vd_markedwin = vw; 1483 } 1484 } 1485 1486 void 1487 vt_mouse_state(int show) 1488 { 1489 struct vt_device *vd; 1490 struct vt_window *vw; 1491 1492 vd = main_vd; 1493 vw = vd->vd_curwindow; 1494 1495 switch (show) { 1496 case VT_MOUSE_HIDE: 1497 vw->vw_flags |= VWF_MOUSE_HIDE; 1498 break; 1499 case VT_MOUSE_SHOW: 1500 vw->vw_flags &= ~VWF_MOUSE_HIDE; 1501 break; 1502 } 1503 } 1504 #endif 1505 1506 static int 1507 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr, 1508 int nprot, vm_memattr_t *memattr) 1509 { 1510 struct vt_window *vw = tm->tm_softc; 1511 struct vt_device *vd = vw->vw_device; 1512 1513 if (vd->vd_driver->vd_fb_mmap) 1514 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot, 1515 memattr)); 1516 1517 return (ENXIO); 1518 } 1519 1520 static int 1521 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, 1522 struct thread *td) 1523 { 1524 struct vt_window *vw = tm->tm_softc; 1525 struct vt_device *vd = vw->vw_device; 1526 keyboard_t *kbd; 1527 int error, i, s; 1528 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1529 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1530 int ival; 1531 1532 switch (cmd) { 1533 case _IO('v', 4): 1534 cmd = VT_RELDISP; 1535 break; 1536 case _IO('v', 5): 1537 cmd = VT_ACTIVATE; 1538 break; 1539 case _IO('v', 6): 1540 cmd = VT_WAITACTIVE; 1541 break; 1542 case _IO('K', 20): 1543 cmd = KDSKBSTATE; 1544 break; 1545 case _IO('K', 67): 1546 cmd = KDSETRAD; 1547 break; 1548 case _IO('K', 7): 1549 cmd = KDSKBMODE; 1550 break; 1551 case _IO('K', 8): 1552 cmd = KDMKTONE; 1553 break; 1554 case _IO('K', 63): 1555 cmd = KIOCSOUND; 1556 break; 1557 case _IO('K', 66): 1558 cmd = KDSETLED; 1559 break; 1560 case _IO('c', 110): 1561 cmd = CONS_SETKBD; 1562 break; 1563 default: 1564 goto skip_thunk; 1565 } 1566 ival = IOCPARM_IVAL(data); 1567 data = (caddr_t)&ival; 1568 skip_thunk: 1569 #endif 1570 1571 switch (cmd) { 1572 case KDSETRAD: /* set keyboard repeat & delay rates (old) */ 1573 if (*(int *)data & ~0x7f) 1574 return (EINVAL); 1575 case GIO_KEYMAP: 1576 case PIO_KEYMAP: 1577 case GIO_DEADKEYMAP: 1578 case PIO_DEADKEYMAP: 1579 case GETFKEY: 1580 case SETFKEY: 1581 case KDGKBINFO: 1582 case KDGKBTYPE: 1583 case KDSKBSTATE: /* set keyboard state (locks) */ 1584 case KDGKBSTATE: /* get keyboard state (locks) */ 1585 case KDGETREPEAT: /* get keyboard repeat & delay rates */ 1586 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ 1587 case KDSETLED: /* set keyboard LED status */ 1588 case KDGETLED: /* get keyboard LED status */ 1589 case KBADDKBD: /* add/remove keyboard to/from mux */ 1590 case KBRELKBD: { 1591 error = 0; 1592 1593 mtx_lock(&Giant); 1594 kbd = kbd_get_keyboard(vd->vd_keyboard); 1595 if (kbd != NULL) 1596 error = kbdd_ioctl(kbd, cmd, data); 1597 mtx_unlock(&Giant); 1598 if (error == ENOIOCTL) { 1599 if (cmd == KDGKBTYPE) { 1600 /* always return something? XXX */ 1601 *(int *)data = 0; 1602 } else { 1603 return (ENODEV); 1604 } 1605 } 1606 return (error); 1607 } 1608 case KDGKBMODE: { 1609 int mode = -1; 1610 1611 mtx_lock(&Giant); 1612 kbd = kbd_get_keyboard(vd->vd_keyboard); 1613 if (kbd != NULL) { 1614 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode); 1615 } 1616 mtx_unlock(&Giant); 1617 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode); 1618 *(int *)data = mode; 1619 return (0); 1620 } 1621 case KDSKBMODE: { 1622 int mode; 1623 1624 mode = *(int *)data; 1625 switch (mode) { 1626 case K_XLATE: 1627 case K_RAW: 1628 case K_CODE: 1629 vw->vw_kbdmode = mode; 1630 if (vw == vd->vd_curwindow) { 1631 keyboard_t *kbd; 1632 error = 0; 1633 1634 mtx_lock(&Giant); 1635 kbd = kbd_get_keyboard(vd->vd_keyboard); 1636 if (kbd != NULL) { 1637 error = kbdd_ioctl(kbd, KDSKBMODE, 1638 (void *)&mode); 1639 } 1640 mtx_unlock(&Giant); 1641 } 1642 return (0); 1643 default: 1644 return (EINVAL); 1645 } 1646 } 1647 case FBIOGTYPE: 1648 case FBIO_GETWINORG: /* get frame buffer window origin */ 1649 case FBIO_GETDISPSTART: /* get display start address */ 1650 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */ 1651 case FBIO_BLANK: /* blank display */ 1652 if (vd->vd_driver->vd_fb_ioctl) 1653 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td)); 1654 break; 1655 case CONS_BLANKTIME: 1656 /* XXX */ 1657 return (0); 1658 case CONS_GET: 1659 /* XXX */ 1660 *(int *)data = M_CG640x480; 1661 return (0); 1662 case CONS_BELLTYPE: /* set bell type sound */ 1663 if ((*(int *)data) & CONS_QUIET_BELL) 1664 vd->vd_flags |= VDF_QUIET_BELL; 1665 else 1666 vd->vd_flags &= ~VDF_QUIET_BELL; 1667 return (0); 1668 case CONS_GETINFO: { 1669 vid_info_t *vi = (vid_info_t *)data; 1670 1671 vi->m_num = vd->vd_curwindow->vw_number + 1; 1672 /* XXX: other fields! */ 1673 return (0); 1674 } 1675 case CONS_GETVERS: 1676 *(int *)data = 0x200; 1677 return (0); 1678 case CONS_MODEINFO: 1679 /* XXX */ 1680 return (0); 1681 case CONS_MOUSECTL: { 1682 mouse_info_t *mouse = (mouse_info_t*)data; 1683 1684 /* 1685 * This has no effect on vt(4). We don't draw any mouse 1686 * cursor. Just ignore MOUSE_HIDE and MOUSE_SHOW to 1687 * prevent excessive errors. All the other commands 1688 * should not be applied to individual TTYs, but only to 1689 * consolectl. 1690 */ 1691 switch (mouse->operation) { 1692 case MOUSE_HIDE: 1693 vd->vd_flags &= ~VDF_MOUSECURSOR; 1694 return (0); 1695 case MOUSE_SHOW: 1696 vd->vd_mx = vd->vd_width / 2; 1697 vd->vd_my = vd->vd_height / 2; 1698 vd->vd_flags |= VDF_MOUSECURSOR; 1699 return (0); 1700 default: 1701 return (EINVAL); 1702 } 1703 } 1704 case PIO_VFONT: { 1705 struct vt_font *vf; 1706 1707 error = vtfont_load((void *)data, &vf); 1708 if (error != 0) 1709 return (error); 1710 1711 error = vt_change_font(vw, vf); 1712 if (error == 0) { 1713 /* XXX: replace 0 with current bg color. */ 1714 vt_set_border(vw, vf, 0); 1715 } 1716 vtfont_unref(vf); 1717 return (error); 1718 } 1719 case GIO_SCRNMAP: { 1720 scrmap_t *sm = (scrmap_t *)data; 1721 int i; 1722 1723 /* We don't have screen maps, so return a handcrafted one. */ 1724 for (i = 0; i < 256; i++) 1725 sm->scrmap[i] = i; 1726 return (0); 1727 } 1728 case KDSETMODE: 1729 /* XXX */ 1730 return (0); 1731 case KDENABIO: /* allow io operations */ 1732 error = priv_check(td, PRIV_IO); 1733 if (error != 0) 1734 return (error); 1735 error = securelevel_gt(td->td_ucred, 0); 1736 if (error != 0) 1737 return (error); 1738 #if defined(__i386__) 1739 td->td_frame->tf_eflags |= PSL_IOPL; 1740 #elif defined(__amd64__) 1741 td->td_frame->tf_rflags |= PSL_IOPL; 1742 #endif 1743 return (0); 1744 case KDDISABIO: /* disallow io operations (default) */ 1745 #if defined(__i386__) 1746 td->td_frame->tf_eflags &= ~PSL_IOPL; 1747 #elif defined(__amd64__) 1748 td->td_frame->tf_rflags &= ~PSL_IOPL; 1749 #endif 1750 return (0); 1751 case KDMKTONE: /* sound the bell */ 1752 vtterm_beep(tm, *(u_int *)data); 1753 return (0); 1754 case KIOCSOUND: /* make tone (*data) hz */ 1755 /* TODO */ 1756 return (0); 1757 case CONS_SETKBD: /* set the new keyboard */ 1758 mtx_lock(&Giant); 1759 error = 0; 1760 if (vd->vd_keyboard != *(int *)data) { 1761 kbd = kbd_get_keyboard(*(int *)data); 1762 if (kbd == NULL) { 1763 mtx_unlock(&Giant); 1764 return (EINVAL); 1765 } 1766 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, 1767 (void *)&vd->vd_keyboard, vt_kbdevent, vd); 1768 if (i >= 0) { 1769 if (vd->vd_keyboard != -1) { 1770 kbd_release(kbd, 1771 (void *)&vd->vd_keyboard); 1772 } 1773 kbd = kbd_get_keyboard(i); 1774 vd->vd_keyboard = i; 1775 1776 (void)kbdd_ioctl(kbd, KDSKBMODE, 1777 (caddr_t)&vd->vd_curwindow->vw_kbdmode); 1778 } else { 1779 error = EPERM; /* XXX */ 1780 } 1781 } 1782 mtx_unlock(&Giant); 1783 return (error); 1784 case CONS_RELKBD: /* release the current keyboard */ 1785 mtx_lock(&Giant); 1786 error = 0; 1787 if (vd->vd_keyboard != -1) { 1788 kbd = kbd_get_keyboard(vd->vd_keyboard); 1789 if (kbd == NULL) { 1790 mtx_unlock(&Giant); 1791 return (EINVAL); 1792 } 1793 error = kbd_release(kbd, (void *)&vd->vd_keyboard); 1794 if (error == 0) { 1795 vd->vd_keyboard = -1; 1796 } 1797 } 1798 mtx_unlock(&Giant); 1799 return (error); 1800 case VT_ACTIVATE: { 1801 int win; 1802 win = *(int *)data - 1; 1803 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME, 1804 VT_UNIT(vw), win); 1805 if ((win > VT_MAXWINDOWS) || (win < 0)) 1806 return (EINVAL); 1807 return (vt_proc_window_switch(vd->vd_windows[win])); 1808 } 1809 case VT_GETACTIVE: 1810 *(int *)data = vd->vd_curwindow->vw_number + 1; 1811 return (0); 1812 case VT_GETINDEX: 1813 *(int *)data = vw->vw_number + 1; 1814 return (0); 1815 case VT_LOCKSWITCH: 1816 /* TODO: Check current state, switching can be in progress. */ 1817 if ((*(int *)data) & 0x01) 1818 vw->vw_flags |= VWF_VTYLOCK; 1819 else 1820 vw->vw_flags &= ~VWF_VTYLOCK; 1821 return (0); 1822 case VT_OPENQRY: 1823 VT_LOCK(vd); 1824 for (i = 0; i < VT_MAXWINDOWS; i++) { 1825 vw = vd->vd_windows[i]; 1826 if (vw == NULL) 1827 continue; 1828 if (!(vw->vw_flags & VWF_OPENED)) { 1829 *(int *)data = vw->vw_number + 1; 1830 VT_UNLOCK(vd); 1831 return (0); 1832 } 1833 } 1834 VT_UNLOCK(vd); 1835 return (EINVAL); 1836 case VT_WAITACTIVE: 1837 error = 0; 1838 1839 i = *(unsigned int *)data; 1840 if (i > VT_MAXWINDOWS) 1841 return (EINVAL); 1842 if (i != 0) 1843 vw = vd->vd_windows[i - 1]; 1844 1845 VT_LOCK(vd); 1846 while (vd->vd_curwindow != vw && error == 0) 1847 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 1848 VT_UNLOCK(vd); 1849 return (error); 1850 case VT_SETMODE: { /* set screen switcher mode */ 1851 struct vt_mode *mode; 1852 struct proc *p1; 1853 1854 mode = (struct vt_mode *)data; 1855 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw)); 1856 if (vw->vw_smode.mode == VT_PROCESS) { 1857 p1 = pfind(vw->vw_pid); 1858 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) { 1859 if (p1) 1860 PROC_UNLOCK(p1); 1861 DPRINTF(5, "error EPERM\n"); 1862 return (EPERM); 1863 } 1864 if (p1) 1865 PROC_UNLOCK(p1); 1866 } 1867 if (mode->mode == VT_AUTO) { 1868 vw->vw_smode.mode = VT_AUTO; 1869 vw->vw_proc = NULL; 1870 vw->vw_pid = 0; 1871 DPRINTF(5, "VT_AUTO, "); 1872 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1873 cnavailable(vw->vw_terminal->consdev, TRUE); 1874 /* were we in the middle of the vty switching process? */ 1875 if (finish_vt_rel(vw, TRUE, &s) == 0) 1876 DPRINTF(5, "reset WAIT_REL, "); 1877 if (finish_vt_acq(vw) == 0) 1878 DPRINTF(5, "reset WAIT_ACQ, "); 1879 return (0); 1880 } else if (mode->mode == VT_PROCESS) { 1881 if (!ISSIGVALID(mode->relsig) || 1882 !ISSIGVALID(mode->acqsig) || 1883 !ISSIGVALID(mode->frsig)) { 1884 DPRINTF(5, "error EINVAL\n"); 1885 return (EINVAL); 1886 } 1887 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid); 1888 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode)); 1889 vw->vw_proc = td->td_proc; 1890 vw->vw_pid = vw->vw_proc->p_pid; 1891 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1892 cnavailable(vw->vw_terminal->consdev, FALSE); 1893 } else { 1894 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n", 1895 mode->mode); 1896 return (EINVAL); 1897 } 1898 DPRINTF(5, "\n"); 1899 return (0); 1900 } 1901 case VT_GETMODE: /* get screen switcher mode */ 1902 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode)); 1903 return (0); 1904 1905 case VT_RELDISP: /* screen switcher ioctl */ 1906 /* 1907 * This must be the current vty which is in the VT_PROCESS 1908 * switching mode... 1909 */ 1910 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode != 1911 VT_PROCESS)) { 1912 return (EINVAL); 1913 } 1914 /* ...and this process is controlling it. */ 1915 if (vw->vw_proc != td->td_proc) { 1916 return (EPERM); 1917 } 1918 error = EINVAL; 1919 switch(*(int *)data) { 1920 case VT_FALSE: /* user refuses to release screen, abort */ 1921 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0) 1922 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n", 1923 SC_DRIVER_NAME, VT_UNIT(vw)); 1924 break; 1925 case VT_TRUE: /* user has released screen, go on */ 1926 /* finish_vt_rel(..., TRUE, ...) should not be locked */ 1927 if (vw->vw_flags & VWF_SWWAIT_REL) { 1928 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0) 1929 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n", 1930 SC_DRIVER_NAME, VT_UNIT(vw)); 1931 } else { 1932 error = EINVAL; 1933 } 1934 return (error); 1935 case VT_ACKACQ: /* acquire acknowledged, switch completed */ 1936 if ((error = finish_vt_acq(vw)) == 0) 1937 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n", 1938 SC_DRIVER_NAME, VT_UNIT(vw)); 1939 break; 1940 default: 1941 break; 1942 } 1943 return (error); 1944 } 1945 1946 return (ENOIOCTL); 1947 } 1948 1949 static struct vt_window * 1950 vt_allocate_window(struct vt_device *vd, unsigned int window) 1951 { 1952 struct vt_window *vw; 1953 struct terminal *tm; 1954 term_pos_t size; 1955 struct winsize wsz; 1956 1957 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO); 1958 vw->vw_device = vd; 1959 vw->vw_number = window; 1960 vw->vw_kbdmode = K_XLATE; 1961 1962 if (!(vd->vd_flags & VDF_TEXTMODE)) 1963 vw->vw_font = vtfont_ref(&vt_font_default); 1964 1965 vt_termsize(vd, vw->vw_font, &size); 1966 vt_winsize(vd, vw->vw_font, &wsz); 1967 vtbuf_init(&vw->vw_buf, &size); 1968 1969 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw); 1970 terminal_set_winsize(tm, &wsz); 1971 vd->vd_windows[window] = vw; 1972 callout_init(&vw->vw_proc_dead_timer, 0); 1973 1974 return (vw); 1975 } 1976 1977 void 1978 vt_upgrade(struct vt_device *vd) 1979 { 1980 struct vt_window *vw; 1981 unsigned int i; 1982 1983 /* Device didn't pass vd_init() or already upgraded. */ 1984 if (vd->vd_flags & (VDF_ASYNC|VDF_DEAD)) 1985 return; 1986 vd->vd_flags |= VDF_ASYNC; 1987 1988 for (i = 0; i < VT_MAXWINDOWS; i++) { 1989 vw = vd->vd_windows[i]; 1990 if (vw == NULL) { 1991 /* New window. */ 1992 vw = vt_allocate_window(vd, i); 1993 } else if (vw->vw_flags & VWF_CONSOLE) { 1994 /* For existing console window. */ 1995 callout_init(&vw->vw_proc_dead_timer, 0); 1996 } 1997 if (i == VT_CONSWINDOW) { 1998 /* Console window. */ 1999 EVENTHANDLER_REGISTER(shutdown_pre_sync, 2000 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT); 2001 } 2002 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw)); 2003 2004 } 2005 VT_LOCK(vd); 2006 if (vd->vd_curwindow == NULL) 2007 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW]; 2008 2009 /* Attach keyboard. */ 2010 vt_allocate_keyboard(vd); 2011 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard); 2012 2013 /* Init 25 Hz timer. */ 2014 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0); 2015 2016 /* Start timer when everything ready. */ 2017 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd); 2018 VT_UNLOCK(vd); 2019 2020 /* Refill settings with new sizes. */ 2021 vt_resize(vd); 2022 2023 } 2024 2025 static void 2026 vt_resize(struct vt_device *vd) 2027 { 2028 struct vt_window *vw; 2029 int i; 2030 2031 for (i = 0; i < VT_MAXWINDOWS; i++) { 2032 vw = vd->vd_windows[i]; 2033 VT_LOCK(vd); 2034 /* Assign default font to window, if not textmode. */ 2035 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL) 2036 vw->vw_font = vtfont_ref(&vt_font_default); 2037 VT_UNLOCK(vd); 2038 /* Resize terminal windows */ 2039 vt_change_font(vw, vw->vw_font); 2040 } 2041 } 2042 2043 void 2044 vt_allocate(struct vt_driver *drv, void *softc) 2045 { 2046 struct vt_device *vd; 2047 struct winsize wsz; 2048 2049 if (main_vd == NULL) { 2050 main_vd = malloc(sizeof *vd, M_VT, M_WAITOK|M_ZERO); 2051 printf("VT: initialize with new VT driver \"%s\".\n", 2052 drv->vd_name); 2053 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF); 2054 cv_init(&main_vd->vd_winswitch, "vtwswt"); 2055 2056 } else { 2057 /* 2058 * Check if have rights to replace current driver. For example: 2059 * it is bad idea to replace KMS driver with generic VGA one. 2060 */ 2061 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) { 2062 printf("VT: Driver priority %d too low. Current %d\n ", 2063 drv->vd_priority, main_vd->vd_driver->vd_priority); 2064 return; 2065 } 2066 printf("VT: Replacing driver \"%s\" with new \"%s\".\n", 2067 main_vd->vd_driver->vd_name, drv->vd_name); 2068 } 2069 vd = main_vd; 2070 VT_LOCK(vd); 2071 if (drv->vd_maskbitbltchr == NULL) 2072 drv->vd_maskbitbltchr = drv->vd_bitbltchr; 2073 2074 if (vd->vd_flags & VDF_ASYNC) { 2075 /* Stop vt_flush periodic task. */ 2076 callout_drain(&vd->vd_timer); 2077 /* 2078 * Mute current terminal until we done. vt_change_font (called 2079 * from vt_resize) will unmute it. 2080 */ 2081 terminal_mute(vd->vd_curwindow->vw_terminal, 1); 2082 } 2083 2084 /* 2085 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will 2086 * set it. 2087 */ 2088 vd->vd_flags &= ~VDF_TEXTMODE; 2089 2090 vd->vd_driver = drv; 2091 vd->vd_softc = softc; 2092 vd->vd_driver->vd_init(vd); 2093 VT_UNLOCK(vd); 2094 2095 vt_upgrade(vd); 2096 2097 #ifdef DEV_SPLASH 2098 if (vd->vd_flags & VDF_SPLASH) 2099 vtterm_splash(vd); 2100 #endif 2101 2102 if (vd->vd_flags & VDF_ASYNC) { 2103 terminal_mute(vd->vd_curwindow->vw_terminal, 0); 2104 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ); 2105 } 2106 2107 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal); 2108 2109 /* Update console window sizes to actual. */ 2110 vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz); 2111 terminal_set_winsize(vd->vd_windows[VT_CONSWINDOW]->vw_terminal, &wsz); 2112 } 2113 2114 void 2115 vt_suspend() 2116 { 2117 2118 if (vt_suspendswitch == 0) 2119 return; 2120 /* Save current window. */ 2121 main_vd->vd_savedwindow = main_vd->vd_curwindow; 2122 /* Ask holding process to free window and switch to console window */ 2123 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]); 2124 } 2125 2126 void 2127 vt_resume() 2128 { 2129 2130 if (vt_suspendswitch == 0) 2131 return; 2132 /* Switch back to saved window */ 2133 if (main_vd->vd_savedwindow != NULL) 2134 vt_proc_window_switch(main_vd->vd_savedwindow); 2135 main_vd->vd_savedwindow = NULL; 2136 } 2137