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 vd = vw->vw_device; 284 curvw = vd->vd_curwindow; 285 286 if (curvw->vw_flags & VWF_VTYLOCK) 287 return (EBUSY); 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 x, y, off_x, off_y; 1175 1176 if (vd->vd_driver->vd_drawrect == NULL) 1177 return (ENOTSUP); 1178 1179 x = vd->vd_width - 1; 1180 y = vd->vd_height - 1; 1181 off_x = vd->vd_offset.tp_col; 1182 off_y = vd->vd_offset.tp_row; 1183 1184 /* Top bar. */ 1185 if (off_y > 0) 1186 vd->vd_driver->vd_drawrect(vd, 0, 0, x, off_y - 1, 1, c); 1187 /* Left bar. */ 1188 if (off_x > 0) 1189 vd->vd_driver->vd_drawrect(vd, 0, off_y, off_x - 1, y - off_y, 1190 1, c); 1191 /* Right bar. May be 1 pixel wider than necessary due to rounding. */ 1192 vd->vd_driver->vd_drawrect(vd, x - off_x, off_y, x, y - off_y, 1, c); 1193 /* Bottom bar. May be 1 mixel taller than necessary due to rounding. */ 1194 vd->vd_driver->vd_drawrect(vd, 0, y - off_y, x, y, 1, c); 1195 1196 return (0); 1197 } 1198 1199 static int 1200 vt_proc_alive(struct vt_window *vw) 1201 { 1202 struct proc *p; 1203 1204 if (vw->vw_smode.mode != VT_PROCESS) 1205 return (FALSE); 1206 1207 if (vw->vw_proc) { 1208 if ((p = pfind(vw->vw_pid)) != NULL) 1209 PROC_UNLOCK(p); 1210 if (vw->vw_proc == p) 1211 return (TRUE); 1212 vw->vw_proc = NULL; 1213 vw->vw_smode.mode = VT_AUTO; 1214 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid); 1215 vw->vw_pid = 0; 1216 } 1217 return (FALSE); 1218 } 1219 1220 static int 1221 signal_vt_rel(struct vt_window *vw) 1222 { 1223 1224 if (vw->vw_smode.mode != VT_PROCESS) 1225 return (FALSE); 1226 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 1227 vw->vw_proc = NULL; 1228 vw->vw_pid = 0; 1229 return (TRUE); 1230 } 1231 vw->vw_flags |= VWF_SWWAIT_REL; 1232 PROC_LOCK(vw->vw_proc); 1233 kern_psignal(vw->vw_proc, vw->vw_smode.relsig); 1234 PROC_UNLOCK(vw->vw_proc); 1235 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid); 1236 return (TRUE); 1237 } 1238 1239 static int 1240 signal_vt_acq(struct vt_window *vw) 1241 { 1242 1243 if (vw->vw_smode.mode != VT_PROCESS) 1244 return (FALSE); 1245 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1246 cnavailable(vw->vw_terminal->consdev, FALSE); 1247 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 1248 vw->vw_proc = NULL; 1249 vw->vw_pid = 0; 1250 return (TRUE); 1251 } 1252 vw->vw_flags |= VWF_SWWAIT_ACQ; 1253 PROC_LOCK(vw->vw_proc); 1254 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig); 1255 PROC_UNLOCK(vw->vw_proc); 1256 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid); 1257 return (TRUE); 1258 } 1259 1260 static int 1261 finish_vt_rel(struct vt_window *vw, int release, int *s) 1262 { 1263 1264 if (vw->vw_flags & VWF_SWWAIT_REL) { 1265 vw->vw_flags &= ~VWF_SWWAIT_REL; 1266 if (release) { 1267 callout_drain(&vw->vw_proc_dead_timer); 1268 vt_late_window_switch(vw->vw_switch_to); 1269 } 1270 return (0); 1271 } 1272 return (EINVAL); 1273 } 1274 1275 static int 1276 finish_vt_acq(struct vt_window *vw) 1277 { 1278 1279 if (vw->vw_flags & VWF_SWWAIT_ACQ) { 1280 vw->vw_flags &= ~VWF_SWWAIT_ACQ; 1281 return (0); 1282 } 1283 return (EINVAL); 1284 } 1285 1286 #ifndef SC_NO_CUTPASTE 1287 static void 1288 vt_mouse_terminput_button(struct vt_device *vd, int button) 1289 { 1290 struct vt_window *vw; 1291 struct vt_font *vf; 1292 char mouseb[6] = "\x1B[M"; 1293 int i, x, y; 1294 1295 vw = vd->vd_curwindow; 1296 vf = vw->vw_font; 1297 1298 /* Translate to char position. */ 1299 x = vd->vd_mx / vf->vf_width; 1300 y = vd->vd_my / vf->vf_height; 1301 /* Avoid overflow. */ 1302 x = MIN(x, 255 - '!'); 1303 y = MIN(y, 255 - '!'); 1304 1305 mouseb[3] = ' ' + button; 1306 mouseb[4] = '!' + x; 1307 mouseb[5] = '!' + y; 1308 1309 for (i = 0; i < sizeof(mouseb); i++ ) 1310 terminal_input_char(vw->vw_terminal, mouseb[i]); 1311 } 1312 1313 static void 1314 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event, 1315 int cnt) 1316 { 1317 1318 switch (type) { 1319 case MOUSE_BUTTON_EVENT: 1320 if (cnt > 0) { 1321 /* Mouse button pressed. */ 1322 if (event & MOUSE_BUTTON1DOWN) 1323 vt_mouse_terminput_button(vd, 0); 1324 if (event & MOUSE_BUTTON2DOWN) 1325 vt_mouse_terminput_button(vd, 1); 1326 if (event & MOUSE_BUTTON3DOWN) 1327 vt_mouse_terminput_button(vd, 2); 1328 } else { 1329 /* Mouse button released. */ 1330 vt_mouse_terminput_button(vd, 3); 1331 } 1332 break; 1333 #ifdef notyet 1334 case MOUSE_MOTION_EVENT: 1335 if (mouse->u.data.z < 0) { 1336 /* Scroll up. */ 1337 sc_mouse_input_button(vd, 64); 1338 } else if (mouse->u.data.z > 0) { 1339 /* Scroll down. */ 1340 sc_mouse_input_button(vd, 65); 1341 } 1342 break; 1343 #endif 1344 } 1345 } 1346 1347 void 1348 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel) 1349 { 1350 struct vt_device *vd; 1351 struct vt_window *vw; 1352 struct vt_font *vf; 1353 term_pos_t size; 1354 term_char_t *buf; 1355 int i, len, mark; 1356 1357 vd = main_vd; 1358 vw = vd->vd_curwindow; 1359 vf = vw->vw_font; 1360 mark = 0; 1361 1362 if (vw->vw_flags & VWF_MOUSE_HIDE) 1363 return; /* Mouse disabled. */ 1364 1365 if (vf == NULL) /* Text mode. */ 1366 return; 1367 1368 /* 1369 * TODO: add flag about pointer position changed, to not redraw chars 1370 * under mouse pointer when nothing changed. 1371 */ 1372 1373 if (vw->vw_mouse_level > 0) 1374 vt_mouse_terminput(vd, type, x, y, event, cnt); 1375 1376 switch (type) { 1377 case MOUSE_ACTION: 1378 case MOUSE_MOTION_EVENT: 1379 /* Movement */ 1380 x += vd->vd_mx; 1381 y += vd->vd_my; 1382 1383 vt_termsize(vd, vf, &size); 1384 1385 /* Apply limits. */ 1386 x = MAX(x, 0); 1387 y = MAX(y, 0); 1388 x = MIN(x, (size.tp_col * vf->vf_width) - 1); 1389 y = MIN(y, (size.tp_row * vf->vf_height) - 1); 1390 1391 vd->vd_mx = x; 1392 vd->vd_my = y; 1393 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) && 1394 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE, 1395 vd->vd_mx / vf->vf_width, 1396 vd->vd_my / vf->vf_height) == 1)) { 1397 1398 /* 1399 * We have something marked to copy, so update pointer 1400 * to window with selection. 1401 */ 1402 vd->vd_markedwin = vw; 1403 } 1404 return; /* Done */ 1405 case MOUSE_BUTTON_EVENT: 1406 /* Buttons */ 1407 break; 1408 default: 1409 return; /* Done */ 1410 } 1411 1412 switch (event) { 1413 case MOUSE_BUTTON1DOWN: 1414 switch (cnt % 4) { 1415 case 0: /* up */ 1416 mark = VTB_MARK_END; 1417 break; 1418 case 1: /* single click: start cut operation */ 1419 mark = VTB_MARK_START; 1420 break; 1421 case 2: /* double click: cut a word */ 1422 mark = VTB_MARK_WORD; 1423 break; 1424 case 3: /* triple click: cut a line */ 1425 mark = VTB_MARK_ROW; 1426 break; 1427 } 1428 break; 1429 case VT_MOUSE_PASTEBUTTON: 1430 switch (cnt) { 1431 case 0: /* up */ 1432 break; 1433 default: 1434 if (vd->vd_markedwin == NULL) 1435 return; 1436 /* Get current selecton size in bytes. */ 1437 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf); 1438 if (len <= 0) 1439 return; 1440 1441 buf = malloc(len, M_VT, M_WAITOK | M_ZERO); 1442 /* Request cupy/paste buffer data, no more than `len' */ 1443 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf, 1444 len); 1445 1446 len /= sizeof(term_char_t); 1447 for (i = 0; i < len; i++ ) { 1448 if (buf[i] == '\0') 1449 continue; 1450 terminal_input_char(vw->vw_terminal, buf[i]); 1451 } 1452 1453 /* Done, so cleanup. */ 1454 free(buf, M_VT); 1455 break; 1456 } 1457 return; /* Done */ 1458 case VT_MOUSE_EXTENDBUTTON: 1459 switch (cnt) { 1460 case 0: /* up */ 1461 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN)) 1462 mark = VTB_MARK_EXTEND; 1463 else 1464 mark = 0; 1465 break; 1466 default: 1467 mark = VTB_MARK_EXTEND; 1468 break; 1469 } 1470 break; 1471 default: 1472 return; /* Done */ 1473 } 1474 1475 /* Save buttons state. */ 1476 if (cnt > 0) 1477 vd->vd_mstate |= event; 1478 else 1479 vd->vd_mstate &= ~event; 1480 1481 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width, 1482 vd->vd_my / vf->vf_height) == 1) { 1483 /* 1484 * We have something marked to copy, so update pointer to 1485 * window with selection. 1486 */ 1487 vd->vd_markedwin = vw; 1488 } 1489 } 1490 1491 void 1492 vt_mouse_state(int show) 1493 { 1494 struct vt_device *vd; 1495 struct vt_window *vw; 1496 1497 vd = main_vd; 1498 vw = vd->vd_curwindow; 1499 1500 switch (show) { 1501 case VT_MOUSE_HIDE: 1502 vw->vw_flags |= VWF_MOUSE_HIDE; 1503 break; 1504 case VT_MOUSE_SHOW: 1505 vw->vw_flags &= ~VWF_MOUSE_HIDE; 1506 break; 1507 } 1508 } 1509 #endif 1510 1511 static int 1512 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr, 1513 int nprot, vm_memattr_t *memattr) 1514 { 1515 struct vt_window *vw = tm->tm_softc; 1516 struct vt_device *vd = vw->vw_device; 1517 1518 if (vd->vd_driver->vd_fb_mmap) 1519 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot, 1520 memattr)); 1521 1522 return (ENXIO); 1523 } 1524 1525 static int 1526 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, 1527 struct thread *td) 1528 { 1529 struct vt_window *vw = tm->tm_softc; 1530 struct vt_device *vd = vw->vw_device; 1531 keyboard_t *kbd; 1532 int error, i, s; 1533 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1534 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1535 int ival; 1536 1537 switch (cmd) { 1538 case _IO('v', 4): 1539 cmd = VT_RELDISP; 1540 break; 1541 case _IO('v', 5): 1542 cmd = VT_ACTIVATE; 1543 break; 1544 case _IO('v', 6): 1545 cmd = VT_WAITACTIVE; 1546 break; 1547 case _IO('K', 20): 1548 cmd = KDSKBSTATE; 1549 break; 1550 case _IO('K', 67): 1551 cmd = KDSETRAD; 1552 break; 1553 case _IO('K', 7): 1554 cmd = KDSKBMODE; 1555 break; 1556 case _IO('K', 8): 1557 cmd = KDMKTONE; 1558 break; 1559 case _IO('K', 63): 1560 cmd = KIOCSOUND; 1561 break; 1562 case _IO('K', 66): 1563 cmd = KDSETLED; 1564 break; 1565 case _IO('c', 110): 1566 cmd = CONS_SETKBD; 1567 break; 1568 default: 1569 goto skip_thunk; 1570 } 1571 ival = IOCPARM_IVAL(data); 1572 data = (caddr_t)&ival; 1573 skip_thunk: 1574 #endif 1575 1576 switch (cmd) { 1577 case KDSETRAD: /* set keyboard repeat & delay rates (old) */ 1578 if (*(int *)data & ~0x7f) 1579 return (EINVAL); 1580 case GIO_KEYMAP: 1581 case PIO_KEYMAP: 1582 case GIO_DEADKEYMAP: 1583 case PIO_DEADKEYMAP: 1584 case GETFKEY: 1585 case SETFKEY: 1586 case KDGKBINFO: 1587 case KDGKBTYPE: 1588 case KDSKBSTATE: /* set keyboard state (locks) */ 1589 case KDGKBSTATE: /* get keyboard state (locks) */ 1590 case KDGETREPEAT: /* get keyboard repeat & delay rates */ 1591 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ 1592 case KDSETLED: /* set keyboard LED status */ 1593 case KDGETLED: /* get keyboard LED status */ 1594 case KBADDKBD: /* add/remove keyboard to/from mux */ 1595 case KBRELKBD: { 1596 error = 0; 1597 1598 mtx_lock(&Giant); 1599 kbd = kbd_get_keyboard(vd->vd_keyboard); 1600 if (kbd != NULL) 1601 error = kbdd_ioctl(kbd, cmd, data); 1602 mtx_unlock(&Giant); 1603 if (error == ENOIOCTL) { 1604 if (cmd == KDGKBTYPE) { 1605 /* always return something? XXX */ 1606 *(int *)data = 0; 1607 } else { 1608 return (ENODEV); 1609 } 1610 } 1611 return (error); 1612 } 1613 case KDGKBMODE: { 1614 int mode = -1; 1615 1616 mtx_lock(&Giant); 1617 kbd = kbd_get_keyboard(vd->vd_keyboard); 1618 if (kbd != NULL) { 1619 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode); 1620 } 1621 mtx_unlock(&Giant); 1622 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode); 1623 *(int *)data = mode; 1624 return (0); 1625 } 1626 case KDSKBMODE: { 1627 int mode; 1628 1629 mode = *(int *)data; 1630 switch (mode) { 1631 case K_XLATE: 1632 case K_RAW: 1633 case K_CODE: 1634 vw->vw_kbdmode = mode; 1635 if (vw == vd->vd_curwindow) { 1636 keyboard_t *kbd; 1637 error = 0; 1638 1639 mtx_lock(&Giant); 1640 kbd = kbd_get_keyboard(vd->vd_keyboard); 1641 if (kbd != NULL) { 1642 error = kbdd_ioctl(kbd, KDSKBMODE, 1643 (void *)&mode); 1644 } 1645 mtx_unlock(&Giant); 1646 } 1647 return (0); 1648 default: 1649 return (EINVAL); 1650 } 1651 } 1652 case FBIOGTYPE: 1653 case FBIO_GETWINORG: /* get frame buffer window origin */ 1654 case FBIO_GETDISPSTART: /* get display start address */ 1655 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */ 1656 case FBIO_BLANK: /* blank display */ 1657 if (vd->vd_driver->vd_fb_ioctl) 1658 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td)); 1659 break; 1660 case CONS_BLANKTIME: 1661 /* XXX */ 1662 return (0); 1663 case CONS_GET: 1664 /* XXX */ 1665 *(int *)data = M_CG640x480; 1666 return (0); 1667 case CONS_BELLTYPE: /* set bell type sound */ 1668 if ((*(int *)data) & CONS_QUIET_BELL) 1669 vd->vd_flags |= VDF_QUIET_BELL; 1670 else 1671 vd->vd_flags &= ~VDF_QUIET_BELL; 1672 return (0); 1673 case CONS_GETINFO: { 1674 vid_info_t *vi = (vid_info_t *)data; 1675 1676 vi->m_num = vd->vd_curwindow->vw_number + 1; 1677 /* XXX: other fields! */ 1678 return (0); 1679 } 1680 case CONS_GETVERS: 1681 *(int *)data = 0x200; 1682 return (0); 1683 case CONS_MODEINFO: 1684 /* XXX */ 1685 return (0); 1686 case CONS_MOUSECTL: { 1687 mouse_info_t *mouse = (mouse_info_t*)data; 1688 1689 /* 1690 * This has no effect on vt(4). We don't draw any mouse 1691 * cursor. Just ignore MOUSE_HIDE and MOUSE_SHOW to 1692 * prevent excessive errors. All the other commands 1693 * should not be applied to individual TTYs, but only to 1694 * consolectl. 1695 */ 1696 switch (mouse->operation) { 1697 case MOUSE_HIDE: 1698 vd->vd_flags &= ~VDF_MOUSECURSOR; 1699 return (0); 1700 case MOUSE_SHOW: 1701 vd->vd_mx = vd->vd_width / 2; 1702 vd->vd_my = vd->vd_height / 2; 1703 vd->vd_flags |= VDF_MOUSECURSOR; 1704 return (0); 1705 default: 1706 return (EINVAL); 1707 } 1708 } 1709 case PIO_VFONT: { 1710 struct vt_font *vf; 1711 1712 error = vtfont_load((void *)data, &vf); 1713 if (error != 0) 1714 return (error); 1715 1716 error = vt_change_font(vw, vf); 1717 if (error == 0) { 1718 /* XXX: replace 0 with current bg color. */ 1719 vt_set_border(vw, vf, 0); 1720 } 1721 vtfont_unref(vf); 1722 return (error); 1723 } 1724 case GIO_SCRNMAP: { 1725 scrmap_t *sm = (scrmap_t *)data; 1726 int i; 1727 1728 /* We don't have screen maps, so return a handcrafted one. */ 1729 for (i = 0; i < 256; i++) 1730 sm->scrmap[i] = i; 1731 return (0); 1732 } 1733 case KDSETMODE: 1734 /* XXX */ 1735 return (0); 1736 case KDENABIO: /* allow io operations */ 1737 error = priv_check(td, PRIV_IO); 1738 if (error != 0) 1739 return (error); 1740 error = securelevel_gt(td->td_ucred, 0); 1741 if (error != 0) 1742 return (error); 1743 #if defined(__i386__) 1744 td->td_frame->tf_eflags |= PSL_IOPL; 1745 #elif defined(__amd64__) 1746 td->td_frame->tf_rflags |= PSL_IOPL; 1747 #endif 1748 return (0); 1749 case KDDISABIO: /* disallow io operations (default) */ 1750 #if defined(__i386__) 1751 td->td_frame->tf_eflags &= ~PSL_IOPL; 1752 #elif defined(__amd64__) 1753 td->td_frame->tf_rflags &= ~PSL_IOPL; 1754 #endif 1755 return (0); 1756 case KDMKTONE: /* sound the bell */ 1757 vtterm_beep(tm, *(u_int *)data); 1758 return (0); 1759 case KIOCSOUND: /* make tone (*data) hz */ 1760 /* TODO */ 1761 return (0); 1762 case CONS_SETKBD: /* set the new keyboard */ 1763 mtx_lock(&Giant); 1764 error = 0; 1765 if (vd->vd_keyboard != *(int *)data) { 1766 kbd = kbd_get_keyboard(*(int *)data); 1767 if (kbd == NULL) { 1768 mtx_unlock(&Giant); 1769 return (EINVAL); 1770 } 1771 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, 1772 (void *)&vd->vd_keyboard, vt_kbdevent, vd); 1773 if (i >= 0) { 1774 if (vd->vd_keyboard != -1) { 1775 kbd_release(kbd, 1776 (void *)&vd->vd_keyboard); 1777 } 1778 kbd = kbd_get_keyboard(i); 1779 vd->vd_keyboard = i; 1780 1781 (void)kbdd_ioctl(kbd, KDSKBMODE, 1782 (caddr_t)&vd->vd_curwindow->vw_kbdmode); 1783 } else { 1784 error = EPERM; /* XXX */ 1785 } 1786 } 1787 mtx_unlock(&Giant); 1788 return (error); 1789 case CONS_RELKBD: /* release the current keyboard */ 1790 mtx_lock(&Giant); 1791 error = 0; 1792 if (vd->vd_keyboard != -1) { 1793 kbd = kbd_get_keyboard(vd->vd_keyboard); 1794 if (kbd == NULL) { 1795 mtx_unlock(&Giant); 1796 return (EINVAL); 1797 } 1798 error = kbd_release(kbd, (void *)&vd->vd_keyboard); 1799 if (error == 0) { 1800 vd->vd_keyboard = -1; 1801 } 1802 } 1803 mtx_unlock(&Giant); 1804 return (error); 1805 case VT_ACTIVATE: { 1806 int win; 1807 win = *(int *)data - 1; 1808 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME, 1809 VT_UNIT(vw), win); 1810 if ((win > VT_MAXWINDOWS) || (win < 0)) 1811 return (EINVAL); 1812 return (vt_proc_window_switch(vd->vd_windows[win])); 1813 } 1814 case VT_GETACTIVE: 1815 *(int *)data = vd->vd_curwindow->vw_number + 1; 1816 return (0); 1817 case VT_GETINDEX: 1818 *(int *)data = vw->vw_number + 1; 1819 return (0); 1820 case VT_LOCKSWITCH: 1821 /* TODO: Check current state, switching can be in progress. */ 1822 if ((*(int *)data) == 0x01) 1823 vw->vw_flags |= VWF_VTYLOCK; 1824 else if ((*(int *)data) == 0x02) 1825 vw->vw_flags &= ~VWF_VTYLOCK; 1826 else 1827 return (EINVAL); 1828 return (0); 1829 case VT_OPENQRY: 1830 VT_LOCK(vd); 1831 for (i = 0; i < VT_MAXWINDOWS; i++) { 1832 vw = vd->vd_windows[i]; 1833 if (vw == NULL) 1834 continue; 1835 if (!(vw->vw_flags & VWF_OPENED)) { 1836 *(int *)data = vw->vw_number + 1; 1837 VT_UNLOCK(vd); 1838 return (0); 1839 } 1840 } 1841 VT_UNLOCK(vd); 1842 return (EINVAL); 1843 case VT_WAITACTIVE: 1844 error = 0; 1845 1846 i = *(unsigned int *)data; 1847 if (i > VT_MAXWINDOWS) 1848 return (EINVAL); 1849 if (i != 0) 1850 vw = vd->vd_windows[i - 1]; 1851 1852 VT_LOCK(vd); 1853 while (vd->vd_curwindow != vw && error == 0) 1854 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 1855 VT_UNLOCK(vd); 1856 return (error); 1857 case VT_SETMODE: { /* set screen switcher mode */ 1858 struct vt_mode *mode; 1859 struct proc *p1; 1860 1861 mode = (struct vt_mode *)data; 1862 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw)); 1863 if (vw->vw_smode.mode == VT_PROCESS) { 1864 p1 = pfind(vw->vw_pid); 1865 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) { 1866 if (p1) 1867 PROC_UNLOCK(p1); 1868 DPRINTF(5, "error EPERM\n"); 1869 return (EPERM); 1870 } 1871 if (p1) 1872 PROC_UNLOCK(p1); 1873 } 1874 if (mode->mode == VT_AUTO) { 1875 vw->vw_smode.mode = VT_AUTO; 1876 vw->vw_proc = NULL; 1877 vw->vw_pid = 0; 1878 DPRINTF(5, "VT_AUTO, "); 1879 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1880 cnavailable(vw->vw_terminal->consdev, TRUE); 1881 /* were we in the middle of the vty switching process? */ 1882 if (finish_vt_rel(vw, TRUE, &s) == 0) 1883 DPRINTF(5, "reset WAIT_REL, "); 1884 if (finish_vt_acq(vw) == 0) 1885 DPRINTF(5, "reset WAIT_ACQ, "); 1886 return (0); 1887 } else if (mode->mode == VT_PROCESS) { 1888 if (!ISSIGVALID(mode->relsig) || 1889 !ISSIGVALID(mode->acqsig) || 1890 !ISSIGVALID(mode->frsig)) { 1891 DPRINTF(5, "error EINVAL\n"); 1892 return (EINVAL); 1893 } 1894 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid); 1895 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode)); 1896 vw->vw_proc = td->td_proc; 1897 vw->vw_pid = vw->vw_proc->p_pid; 1898 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1899 cnavailable(vw->vw_terminal->consdev, FALSE); 1900 } else { 1901 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n", 1902 mode->mode); 1903 return (EINVAL); 1904 } 1905 DPRINTF(5, "\n"); 1906 return (0); 1907 } 1908 case VT_GETMODE: /* get screen switcher mode */ 1909 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode)); 1910 return (0); 1911 1912 case VT_RELDISP: /* screen switcher ioctl */ 1913 /* 1914 * This must be the current vty which is in the VT_PROCESS 1915 * switching mode... 1916 */ 1917 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode != 1918 VT_PROCESS)) { 1919 return (EINVAL); 1920 } 1921 /* ...and this process is controlling it. */ 1922 if (vw->vw_proc != td->td_proc) { 1923 return (EPERM); 1924 } 1925 error = EINVAL; 1926 switch(*(int *)data) { 1927 case VT_FALSE: /* user refuses to release screen, abort */ 1928 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0) 1929 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n", 1930 SC_DRIVER_NAME, VT_UNIT(vw)); 1931 break; 1932 case VT_TRUE: /* user has released screen, go on */ 1933 /* finish_vt_rel(..., TRUE, ...) should not be locked */ 1934 if (vw->vw_flags & VWF_SWWAIT_REL) { 1935 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0) 1936 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n", 1937 SC_DRIVER_NAME, VT_UNIT(vw)); 1938 } else { 1939 error = EINVAL; 1940 } 1941 return (error); 1942 case VT_ACKACQ: /* acquire acknowledged, switch completed */ 1943 if ((error = finish_vt_acq(vw)) == 0) 1944 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n", 1945 SC_DRIVER_NAME, VT_UNIT(vw)); 1946 break; 1947 default: 1948 break; 1949 } 1950 return (error); 1951 } 1952 1953 return (ENOIOCTL); 1954 } 1955 1956 static struct vt_window * 1957 vt_allocate_window(struct vt_device *vd, unsigned int window) 1958 { 1959 struct vt_window *vw; 1960 struct terminal *tm; 1961 term_pos_t size; 1962 struct winsize wsz; 1963 1964 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO); 1965 vw->vw_device = vd; 1966 vw->vw_number = window; 1967 vw->vw_kbdmode = K_XLATE; 1968 1969 if (!(vd->vd_flags & VDF_TEXTMODE)) 1970 vw->vw_font = vtfont_ref(&vt_font_default); 1971 1972 vt_termsize(vd, vw->vw_font, &size); 1973 vt_winsize(vd, vw->vw_font, &wsz); 1974 vtbuf_init(&vw->vw_buf, &size); 1975 1976 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw); 1977 terminal_set_winsize(tm, &wsz); 1978 vd->vd_windows[window] = vw; 1979 callout_init(&vw->vw_proc_dead_timer, 0); 1980 1981 return (vw); 1982 } 1983 1984 void 1985 vt_upgrade(struct vt_device *vd) 1986 { 1987 struct vt_window *vw; 1988 unsigned int i; 1989 1990 /* Device didn't pass vd_init() or already upgraded. */ 1991 if (vd->vd_flags & (VDF_ASYNC|VDF_DEAD)) { 1992 /* Refill settings with new sizes anyway. */ 1993 vt_resize(vd); 1994 return; 1995 } 1996 vd->vd_flags |= VDF_ASYNC; 1997 1998 for (i = 0; i < VT_MAXWINDOWS; i++) { 1999 vw = vd->vd_windows[i]; 2000 if (vw == NULL) { 2001 /* New window. */ 2002 vw = vt_allocate_window(vd, i); 2003 } else if (vw->vw_flags & VWF_CONSOLE) { 2004 /* For existing console window. */ 2005 callout_init(&vw->vw_proc_dead_timer, 0); 2006 } 2007 if (i == VT_CONSWINDOW) { 2008 /* Console window. */ 2009 EVENTHANDLER_REGISTER(shutdown_pre_sync, 2010 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT); 2011 } 2012 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw)); 2013 2014 } 2015 VT_LOCK(vd); 2016 if (vd->vd_curwindow == NULL) 2017 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW]; 2018 2019 /* Attach keyboard. */ 2020 vt_allocate_keyboard(vd); 2021 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard); 2022 2023 /* Init 25 Hz timer. */ 2024 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0); 2025 2026 /* Start timer when everything ready. */ 2027 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd); 2028 VT_UNLOCK(vd); 2029 2030 /* Refill settings with new sizes. */ 2031 vt_resize(vd); 2032 } 2033 2034 static void 2035 vt_resize(struct vt_device *vd) 2036 { 2037 struct vt_window *vw; 2038 int i; 2039 2040 for (i = 0; i < VT_MAXWINDOWS; i++) { 2041 vw = vd->vd_windows[i]; 2042 VT_LOCK(vd); 2043 /* Assign default font to window, if not textmode. */ 2044 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL) 2045 vw->vw_font = vtfont_ref(&vt_font_default); 2046 VT_UNLOCK(vd); 2047 /* Resize terminal windows */ 2048 vt_change_font(vw, vw->vw_font); 2049 } 2050 } 2051 2052 void 2053 vt_allocate(struct vt_driver *drv, void *softc) 2054 { 2055 struct vt_device *vd; 2056 struct winsize wsz; 2057 2058 if (main_vd == NULL) { 2059 main_vd = malloc(sizeof *vd, M_VT, M_WAITOK|M_ZERO); 2060 printf("VT: initialize with new VT driver \"%s\".\n", 2061 drv->vd_name); 2062 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF); 2063 cv_init(&main_vd->vd_winswitch, "vtwswt"); 2064 2065 } else { 2066 /* 2067 * Check if have rights to replace current driver. For example: 2068 * it is bad idea to replace KMS driver with generic VGA one. 2069 */ 2070 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) { 2071 printf("VT: Driver priority %d too low. Current %d\n ", 2072 drv->vd_priority, main_vd->vd_driver->vd_priority); 2073 return; 2074 } 2075 printf("VT: Replacing driver \"%s\" with new \"%s\".\n", 2076 main_vd->vd_driver->vd_name, drv->vd_name); 2077 } 2078 vd = main_vd; 2079 VT_LOCK(vd); 2080 if (drv->vd_maskbitbltchr == NULL) 2081 drv->vd_maskbitbltchr = drv->vd_bitbltchr; 2082 2083 if (vd->vd_flags & VDF_ASYNC) { 2084 /* Stop vt_flush periodic task. */ 2085 callout_drain(&vd->vd_timer); 2086 /* 2087 * Mute current terminal until we done. vt_change_font (called 2088 * from vt_resize) will unmute it. 2089 */ 2090 terminal_mute(vd->vd_curwindow->vw_terminal, 1); 2091 } 2092 2093 /* 2094 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will 2095 * set it. 2096 */ 2097 vd->vd_flags &= ~VDF_TEXTMODE; 2098 2099 vd->vd_driver = drv; 2100 vd->vd_softc = softc; 2101 vd->vd_driver->vd_init(vd); 2102 VT_UNLOCK(vd); 2103 2104 vt_upgrade(vd); 2105 2106 #ifdef DEV_SPLASH 2107 if (vd->vd_flags & VDF_SPLASH) 2108 vtterm_splash(vd); 2109 #endif 2110 2111 if (vd->vd_flags & VDF_ASYNC) { 2112 terminal_mute(vd->vd_curwindow->vw_terminal, 0); 2113 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ); 2114 } 2115 2116 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal); 2117 2118 /* Update console window sizes to actual. */ 2119 vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz); 2120 terminal_set_winsize(vd->vd_windows[VT_CONSWINDOW]->vw_terminal, &wsz); 2121 } 2122 2123 void 2124 vt_suspend() 2125 { 2126 2127 if (vt_suspendswitch == 0) 2128 return; 2129 /* Save current window. */ 2130 main_vd->vd_savedwindow = main_vd->vd_curwindow; 2131 /* Ask holding process to free window and switch to console window */ 2132 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]); 2133 } 2134 2135 void 2136 vt_resume() 2137 { 2138 2139 if (vt_suspendswitch == 0) 2140 return; 2141 /* Switch back to saved window */ 2142 if (main_vd->vd_savedwindow != NULL) 2143 vt_proc_window_switch(main_vd->vd_savedwindow); 2144 main_vd->vd_savedwindow = NULL; 2145 } 2146