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_cursor(struct terminal *tm, const term_pos_t *p) 682 { 683 struct vt_window *vw = tm->tm_softc; 684 685 vtbuf_cursor_position(&vw->vw_buf, p); 686 } 687 688 static void 689 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c) 690 { 691 struct vt_window *vw = tm->tm_softc; 692 693 vtbuf_putchar(&vw->vw_buf, p, c); 694 } 695 696 static void 697 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c) 698 { 699 struct vt_window *vw = tm->tm_softc; 700 701 vtbuf_fill_locked(&vw->vw_buf, r, c); 702 } 703 704 static void 705 vtterm_copy(struct terminal *tm, const term_rect_t *r, 706 const term_pos_t *p) 707 { 708 struct vt_window *vw = tm->tm_softc; 709 710 vtbuf_copy(&vw->vw_buf, r, p); 711 } 712 713 static void 714 vtterm_param(struct terminal *tm, int cmd, unsigned int arg) 715 { 716 struct vt_window *vw = tm->tm_softc; 717 718 switch (cmd) { 719 case TP_SHOWCURSOR: 720 vtbuf_cursor_visibility(&vw->vw_buf, arg); 721 break; 722 case TP_MOUSE: 723 vw->vw_mouse_level = arg; 724 break; 725 } 726 } 727 728 static inline void 729 vt_determine_colors(term_char_t c, int cursor, 730 term_color_t *fg, term_color_t *bg) 731 { 732 term_color_t tmp; 733 int invert; 734 735 invert = 0; 736 737 *fg = TCHAR_FGCOLOR(c); 738 if (TCHAR_FORMAT(c) & TF_BOLD) 739 *fg = TCOLOR_LIGHT(*fg); 740 *bg = TCHAR_BGCOLOR(c); 741 742 if (TCHAR_FORMAT(c) & TF_REVERSE) 743 invert ^= 1; 744 if (cursor) 745 invert ^= 1; 746 747 if (invert) { 748 tmp = *fg; 749 *fg = *bg; 750 *bg = tmp; 751 } 752 } 753 754 static void 755 vt_bitblt_char(struct vt_device *vd, struct vt_font *vf, term_char_t c, 756 int iscursor, unsigned int row, unsigned int col) 757 { 758 term_color_t fg, bg; 759 760 vt_determine_colors(c, iscursor, &fg, &bg); 761 762 if (vf != NULL) { 763 const uint8_t *src; 764 vt_axis_t top, left; 765 766 src = vtfont_lookup(vf, c); 767 768 /* 769 * Align the terminal to the centre of the screen. 770 * Fonts may not always be able to fill the entire 771 * screen. 772 */ 773 top = row * vf->vf_height + vd->vd_offset.tp_row; 774 left = col * vf->vf_width + vd->vd_offset.tp_col; 775 776 vd->vd_driver->vd_bitbltchr(vd, src, NULL, 0, top, left, 777 vf->vf_width, vf->vf_height, fg, bg); 778 } else { 779 vd->vd_driver->vd_putchar(vd, TCHAR_CHARACTER(c), 780 row, col, fg, bg); 781 } 782 } 783 784 static void 785 vt_flush(struct vt_device *vd) 786 { 787 struct vt_window *vw; 788 struct vt_font *vf; 789 struct vt_bufmask tmask; 790 unsigned int row, col; 791 term_rect_t tarea; 792 term_pos_t size; 793 term_char_t *r; 794 #ifndef SC_NO_CUTPASTE 795 struct mouse_cursor *m; 796 int bpl, h, w; 797 #endif 798 799 vw = vd->vd_curwindow; 800 if (vw == NULL) 801 return; 802 vf = vw->vw_font; 803 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL)) 804 return; 805 806 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY) 807 return; 808 809 vtbuf_undirty(&vw->vw_buf, &tarea, &tmask); 810 vt_termsize(vd, vf, &size); 811 812 /* Force a full redraw when the screen contents are invalid. */ 813 if (vd->vd_flags & VDF_INVALID) { 814 tarea.tr_begin.tp_row = tarea.tr_begin.tp_col = 0; 815 tarea.tr_end = size; 816 tmask.vbm_row = tmask.vbm_col = VBM_DIRTY; 817 818 vd->vd_flags &= ~VDF_INVALID; 819 } 820 821 #ifndef SC_NO_CUTPASTE 822 if ((vw->vw_flags & VWF_MOUSE_HIDE) == 0) { 823 /* Mark last mouse position as dirty to erase. */ 824 vtbuf_mouse_cursor_position(&vw->vw_buf, vd->vd_mdirtyx, 825 vd->vd_mdirtyy); 826 } 827 #endif 828 829 for (row = tarea.tr_begin.tp_row; row < tarea.tr_end.tp_row; row++) { 830 if (!VTBUF_DIRTYROW(&tmask, row)) 831 continue; 832 r = VTBUF_GET_ROW(&vw->vw_buf, row); 833 for (col = tarea.tr_begin.tp_col; 834 col < tarea.tr_end.tp_col; col++) { 835 if (!VTBUF_DIRTYCOL(&tmask, col)) 836 continue; 837 838 vt_bitblt_char(vd, vf, r[col], 839 VTBUF_ISCURSOR(&vw->vw_buf, row, col), row, col); 840 } 841 } 842 843 #ifndef SC_NO_CUTPASTE 844 /* Mouse disabled. */ 845 if (vw->vw_flags & VWF_MOUSE_HIDE) 846 return; 847 848 /* No mouse for DDB. */ 849 if (kdb_active || panicstr != NULL) 850 return; 851 852 if ((vd->vd_flags & (VDF_MOUSECURSOR|VDF_TEXTMODE)) == 853 VDF_MOUSECURSOR) { 854 m = &vt_default_mouse_pointer; 855 bpl = (m->w + 7) >> 3; /* Bytes per source line. */ 856 w = m->w; 857 h = m->h; 858 859 if ((vd->vd_mx + m->w) > (size.tp_col * vf->vf_width)) 860 w = (size.tp_col * vf->vf_width) - vd->vd_mx - 1; 861 if ((vd->vd_my + m->h) > (size.tp_row * vf->vf_height)) 862 h = (size.tp_row * vf->vf_height) - vd->vd_my - 1; 863 864 vd->vd_driver->vd_maskbitbltchr(vd, m->map, m->mask, bpl, 865 vd->vd_offset.tp_row + vd->vd_my, 866 vd->vd_offset.tp_col + vd->vd_mx, 867 w, h, TC_WHITE, TC_BLACK); 868 /* Save point of last mouse cursor to erase it later. */ 869 vd->vd_mdirtyx = vd->vd_mx / vf->vf_width; 870 vd->vd_mdirtyy = vd->vd_my / vf->vf_height; 871 } 872 #endif 873 } 874 875 static void 876 vt_timer(void *arg) 877 { 878 struct vt_device *vd; 879 880 vd = arg; 881 /* Update screen if required. */ 882 vt_flush(vd); 883 884 /* Schedule for next update. */ 885 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ); 886 } 887 888 static void 889 vtterm_done(struct terminal *tm) 890 { 891 struct vt_window *vw = tm->tm_softc; 892 struct vt_device *vd = vw->vw_device; 893 894 if (kdb_active || panicstr != NULL) { 895 /* Switch to the debugger. */ 896 if (vd->vd_curwindow != vw) { 897 vd->vd_curwindow = vw; 898 vd->vd_flags |= VDF_INVALID; 899 if (vd->vd_driver->vd_postswitch) 900 vd->vd_driver->vd_postswitch(vd); 901 } 902 vd->vd_flags &= ~VDF_SPLASH; 903 vt_flush(vd); 904 } else if (!(vd->vd_flags & VDF_ASYNC)) { 905 vt_flush(vd); 906 } 907 } 908 909 #ifdef DEV_SPLASH 910 static void 911 vtterm_splash(struct vt_device *vd) 912 { 913 vt_axis_t top, left; 914 915 /* Display a nice boot splash. */ 916 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) { 917 918 top = (vd->vd_height - vt_logo_height) / 2; 919 left = (vd->vd_width - vt_logo_width) / 2; 920 switch (vt_logo_depth) { 921 case 1: 922 /* XXX: Unhardcode colors! */ 923 vd->vd_driver->vd_bitbltchr(vd, vt_logo_image, NULL, 0, 924 top, left, vt_logo_width, vt_logo_height, 0xf, 0x0); 925 } 926 vd->vd_flags |= VDF_SPLASH; 927 } 928 } 929 #endif 930 931 932 static void 933 vtterm_cnprobe(struct terminal *tm, struct consdev *cp) 934 { 935 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL; 936 struct vt_window *vw = tm->tm_softc; 937 struct vt_device *vd = vw->vw_device; 938 struct winsize wsz; 939 940 if (vd->vd_flags & VDF_INITIALIZED) 941 /* Initialization already done. */ 942 return; 943 944 SET_FOREACH(vtdlist, vt_drv_set) { 945 vtd = *vtdlist; 946 if (vtd->vd_probe == NULL) 947 continue; 948 if (vtd->vd_probe(vd) == CN_DEAD) 949 continue; 950 if ((vtdbest == NULL) || 951 (vtd->vd_priority > vtdbest->vd_priority)) 952 vtdbest = vtd; 953 } 954 if (vtdbest == NULL) { 955 cp->cn_pri = CN_DEAD; 956 vd->vd_flags |= VDF_DEAD; 957 return; 958 } 959 960 vd->vd_driver = vtdbest; 961 962 cp->cn_pri = vd->vd_driver->vd_init(vd); 963 if (cp->cn_pri == CN_DEAD) { 964 vd->vd_flags |= VDF_DEAD; 965 return; 966 } 967 968 /* Initialize any early-boot keyboard drivers */ 969 kbd_configure(KB_CONF_PROBE_ONLY); 970 971 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1); 972 vd->vd_windows[VT_CONSWINDOW] = vw; 973 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw)); 974 975 if (!(vd->vd_flags & VDF_TEXTMODE)) 976 vw->vw_font = vtfont_ref(&vt_font_default); 977 978 vtbuf_init_early(&vw->vw_buf); 979 vt_winsize(vd, vw->vw_font, &wsz); 980 terminal_set_winsize(tm, &wsz); 981 982 #ifdef DEV_SPLASH 983 vtterm_splash(vd); 984 #endif 985 986 vd->vd_flags |= VDF_INITIALIZED; 987 main_vd = vd; 988 } 989 990 static int 991 vtterm_cngetc(struct terminal *tm) 992 { 993 struct vt_window *vw = tm->tm_softc; 994 struct vt_device *vd = vw->vw_device; 995 keyboard_t *kbd; 996 int state; 997 u_int c; 998 999 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1000 return (*vw->vw_kbdsq++); 1001 1002 state = 0; 1003 /* Make sure the splash screen is not there. */ 1004 if (vd->vd_flags & VDF_SPLASH) { 1005 /* Remove splash */ 1006 vd->vd_flags &= ~VDF_SPLASH; 1007 /* Mark screen as invalid to force update */ 1008 vd->vd_flags |= VDF_INVALID; 1009 vt_flush(vd); 1010 } 1011 1012 /* Stripped down keyboard handler. */ 1013 kbd = kbd_get_keyboard(vd->vd_keyboard); 1014 if (kbd == NULL) 1015 return (-1); 1016 1017 /* Force keyboard input mode to K_XLATE */ 1018 c = K_XLATE; 1019 kbdd_ioctl(kbd, KDSKBMODE, (void *)&c); 1020 1021 /* Switch the keyboard to polling to make it work here. */ 1022 kbdd_poll(kbd, TRUE); 1023 c = kbdd_read_char(kbd, 0); 1024 kbdd_poll(kbd, FALSE); 1025 if (c & RELKEY) 1026 return (-1); 1027 1028 if (vw->vw_flags & VWF_SCROLL) { 1029 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */); 1030 vt_flush(vd); 1031 return (-1); 1032 } 1033 1034 /* Stripped down handling of vt_kbdevent(), without locking, etc. */ 1035 if (c & SPCLKEY) { 1036 switch (c) { 1037 case SPCLKEY | SLK: 1038 kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state); 1039 if (state & SLKED) { 1040 /* Turn scrolling on. */ 1041 vw->vw_flags |= VWF_SCROLL; 1042 VTBUF_SLCK_ENABLE(&vw->vw_buf); 1043 } else { 1044 /* Turn scrolling off. */ 1045 vt_scroll(vw, 0, VHS_END); 1046 vw->vw_flags &= ~VWF_SCROLL; 1047 VTBUF_SLCK_DISABLE(&vw->vw_buf); 1048 } 1049 break; 1050 /* XXX: KDB can handle history. */ 1051 case SPCLKEY | FKEY | F(50): /* Arrow up. */ 1052 vw->vw_kbdsq = "\x1b[A"; 1053 break; 1054 case SPCLKEY | FKEY | F(58): /* Arrow down. */ 1055 vw->vw_kbdsq = "\x1b[B"; 1056 break; 1057 case SPCLKEY | FKEY | F(55): /* Arrow right. */ 1058 vw->vw_kbdsq = "\x1b[C"; 1059 break; 1060 case SPCLKEY | FKEY | F(53): /* Arrow left. */ 1061 vw->vw_kbdsq = "\x1b[D"; 1062 break; 1063 } 1064 1065 /* Force refresh to make scrollback work. */ 1066 vt_flush(vd); 1067 } else if (KEYFLAGS(c) == 0) { 1068 return (KEYCHAR(c)); 1069 } 1070 1071 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1072 return (*vw->vw_kbdsq++); 1073 1074 return (-1); 1075 } 1076 1077 static void 1078 vtterm_opened(struct terminal *tm, int opened) 1079 { 1080 struct vt_window *vw = tm->tm_softc; 1081 struct vt_device *vd = vw->vw_device; 1082 1083 VT_LOCK(vd); 1084 vd->vd_flags &= ~VDF_SPLASH; 1085 if (opened) 1086 vw->vw_flags |= VWF_OPENED; 1087 else { 1088 vw->vw_flags &= ~VWF_OPENED; 1089 /* TODO: finish ACQ/REL */ 1090 } 1091 VT_UNLOCK(vd); 1092 } 1093 1094 static int 1095 vt_change_font(struct vt_window *vw, struct vt_font *vf) 1096 { 1097 struct vt_device *vd = vw->vw_device; 1098 struct terminal *tm = vw->vw_terminal; 1099 term_pos_t size; 1100 struct winsize wsz; 1101 1102 /* 1103 * Changing fonts. 1104 * 1105 * Changing fonts is a little tricky. We must prevent 1106 * simultaneous access to the device, so we must stop 1107 * the display timer and the terminal from accessing. 1108 * We need to switch fonts and grow our screen buffer. 1109 * 1110 * XXX: Right now the code uses terminal_mute() to 1111 * prevent data from reaching the console driver while 1112 * resizing the screen buffer. This isn't elegant... 1113 */ 1114 1115 VT_LOCK(vd); 1116 if (vw->vw_flags & VWF_BUSY) { 1117 /* Another process is changing the font. */ 1118 VT_UNLOCK(vd); 1119 return (EBUSY); 1120 } 1121 if (vw->vw_font == NULL) { 1122 /* Our device doesn't need fonts. */ 1123 VT_UNLOCK(vd); 1124 return (ENOTTY); 1125 } 1126 vw->vw_flags |= VWF_BUSY; 1127 VT_UNLOCK(vd); 1128 1129 vt_termsize(vd, vf, &size); 1130 vt_winsize(vd, vf, &wsz); 1131 /* Save offset to font aligned area. */ 1132 vd->vd_offset.tp_col = (vd->vd_width % vf->vf_width) / 2; 1133 vd->vd_offset.tp_row = (vd->vd_height % vf->vf_height) / 2; 1134 1135 /* Grow the screen buffer and terminal. */ 1136 terminal_mute(tm, 1); 1137 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size); 1138 terminal_set_winsize_blank(tm, &wsz, 0); 1139 terminal_mute(tm, 0); 1140 1141 /* Actually apply the font to the current window. */ 1142 VT_LOCK(vd); 1143 vtfont_unref(vw->vw_font); 1144 vw->vw_font = vtfont_ref(vf); 1145 1146 /* Force a full redraw the next timer tick. */ 1147 if (vd->vd_curwindow == vw) 1148 vd->vd_flags |= VDF_INVALID; 1149 vw->vw_flags &= ~VWF_BUSY; 1150 VT_UNLOCK(vd); 1151 return (0); 1152 } 1153 1154 static int 1155 vt_set_border(struct vt_window *vw, struct vt_font *vf, term_color_t c) 1156 { 1157 struct vt_device *vd = vw->vw_device; 1158 int l, r, t, b, w, h; 1159 1160 if (vd->vd_driver->vd_drawrect == NULL) 1161 return (ENOTSUP); 1162 1163 w = vd->vd_width - 1; 1164 h = vd->vd_height - 1; 1165 l = vd->vd_offset.tp_col - 1; 1166 r = w - l; 1167 t = vd->vd_offset.tp_row - 1; 1168 b = h - t; 1169 1170 vd->vd_driver->vd_drawrect(vd, 0, 0, w, t, 1, c); /* Top bar. */ 1171 vd->vd_driver->vd_drawrect(vd, 0, t, l, b, 1, c); /* Left bar. */ 1172 vd->vd_driver->vd_drawrect(vd, r, t, w, b, 1, c); /* Right bar. */ 1173 vd->vd_driver->vd_drawrect(vd, 0, b, w, h, 1, c); /* Bottom bar. */ 1174 1175 return (0); 1176 } 1177 1178 static int 1179 vt_proc_alive(struct vt_window *vw) 1180 { 1181 struct proc *p; 1182 1183 if (vw->vw_smode.mode != VT_PROCESS) 1184 return (FALSE); 1185 1186 if (vw->vw_proc) { 1187 if ((p = pfind(vw->vw_pid)) != NULL) 1188 PROC_UNLOCK(p); 1189 if (vw->vw_proc == p) 1190 return (TRUE); 1191 vw->vw_proc = NULL; 1192 vw->vw_smode.mode = VT_AUTO; 1193 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid); 1194 vw->vw_pid = 0; 1195 } 1196 return (FALSE); 1197 } 1198 1199 static int 1200 signal_vt_rel(struct vt_window *vw) 1201 { 1202 1203 if (vw->vw_smode.mode != VT_PROCESS) 1204 return (FALSE); 1205 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 1206 vw->vw_proc = NULL; 1207 vw->vw_pid = 0; 1208 return (TRUE); 1209 } 1210 vw->vw_flags |= VWF_SWWAIT_REL; 1211 PROC_LOCK(vw->vw_proc); 1212 kern_psignal(vw->vw_proc, vw->vw_smode.relsig); 1213 PROC_UNLOCK(vw->vw_proc); 1214 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid); 1215 return (TRUE); 1216 } 1217 1218 static int 1219 signal_vt_acq(struct vt_window *vw) 1220 { 1221 1222 if (vw->vw_smode.mode != VT_PROCESS) 1223 return (FALSE); 1224 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1225 cnavailable(vw->vw_terminal->consdev, 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_ACQ; 1232 PROC_LOCK(vw->vw_proc); 1233 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig); 1234 PROC_UNLOCK(vw->vw_proc); 1235 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid); 1236 return (TRUE); 1237 } 1238 1239 static int 1240 finish_vt_rel(struct vt_window *vw, int release, int *s) 1241 { 1242 1243 if (vw->vw_flags & VWF_SWWAIT_REL) { 1244 vw->vw_flags &= ~VWF_SWWAIT_REL; 1245 if (release) { 1246 callout_drain(&vw->vw_proc_dead_timer); 1247 vt_late_window_switch(vw->vw_switch_to); 1248 } 1249 return (0); 1250 } 1251 return (EINVAL); 1252 } 1253 1254 static int 1255 finish_vt_acq(struct vt_window *vw) 1256 { 1257 1258 if (vw->vw_flags & VWF_SWWAIT_ACQ) { 1259 vw->vw_flags &= ~VWF_SWWAIT_ACQ; 1260 return (0); 1261 } 1262 return (EINVAL); 1263 } 1264 1265 #ifndef SC_NO_CUTPASTE 1266 static void 1267 vt_mouse_terminput_button(struct vt_device *vd, int button) 1268 { 1269 struct vt_window *vw; 1270 struct vt_font *vf; 1271 char mouseb[6] = "\x1B[M"; 1272 int i, x, y; 1273 1274 vw = vd->vd_curwindow; 1275 vf = vw->vw_font; 1276 1277 /* Translate to char position. */ 1278 x = vd->vd_mx / vf->vf_width; 1279 y = vd->vd_my / vf->vf_height; 1280 /* Avoid overflow. */ 1281 x = MIN(x, 255 - '!'); 1282 y = MIN(y, 255 - '!'); 1283 1284 mouseb[3] = ' ' + button; 1285 mouseb[4] = '!' + x; 1286 mouseb[5] = '!' + y; 1287 1288 for (i = 0; i < sizeof(mouseb); i++ ) 1289 terminal_input_char(vw->vw_terminal, mouseb[i]); 1290 } 1291 1292 static void 1293 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event, 1294 int cnt) 1295 { 1296 1297 switch (type) { 1298 case MOUSE_BUTTON_EVENT: 1299 if (cnt > 0) { 1300 /* Mouse button pressed. */ 1301 if (event & MOUSE_BUTTON1DOWN) 1302 vt_mouse_terminput_button(vd, 0); 1303 if (event & MOUSE_BUTTON2DOWN) 1304 vt_mouse_terminput_button(vd, 1); 1305 if (event & MOUSE_BUTTON3DOWN) 1306 vt_mouse_terminput_button(vd, 2); 1307 } else { 1308 /* Mouse button released. */ 1309 vt_mouse_terminput_button(vd, 3); 1310 } 1311 break; 1312 #ifdef notyet 1313 case MOUSE_MOTION_EVENT: 1314 if (mouse->u.data.z < 0) { 1315 /* Scroll up. */ 1316 sc_mouse_input_button(vd, 64); 1317 } else if (mouse->u.data.z > 0) { 1318 /* Scroll down. */ 1319 sc_mouse_input_button(vd, 65); 1320 } 1321 break; 1322 #endif 1323 } 1324 } 1325 1326 void 1327 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel) 1328 { 1329 struct vt_device *vd; 1330 struct vt_window *vw; 1331 struct vt_font *vf; 1332 term_pos_t size; 1333 term_char_t *buf; 1334 int i, len, mark; 1335 1336 vd = main_vd; 1337 vw = vd->vd_curwindow; 1338 vf = vw->vw_font; 1339 mark = 0; 1340 1341 if (vw->vw_flags & VWF_MOUSE_HIDE) 1342 return; /* Mouse disabled. */ 1343 1344 if (vf == NULL) /* Text mode. */ 1345 return; 1346 1347 /* 1348 * TODO: add flag about pointer position changed, to not redraw chars 1349 * under mouse pointer when nothing changed. 1350 */ 1351 1352 if (vw->vw_mouse_level > 0) 1353 vt_mouse_terminput(vd, type, x, y, event, cnt); 1354 1355 switch (type) { 1356 case MOUSE_ACTION: 1357 case MOUSE_MOTION_EVENT: 1358 /* Movement */ 1359 x += vd->vd_mx; 1360 y += vd->vd_my; 1361 1362 vt_termsize(vd, vf, &size); 1363 1364 /* Apply limits. */ 1365 x = MAX(x, 0); 1366 y = MAX(y, 0); 1367 x = MIN(x, (size.tp_col * vf->vf_width) - 1); 1368 y = MIN(y, (size.tp_row * vf->vf_height) - 1); 1369 1370 vd->vd_mx = x; 1371 vd->vd_my = y; 1372 if ((vd->vd_mstate & MOUSE_BUTTON1DOWN) && 1373 (vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE, 1374 vd->vd_mx / vf->vf_width, 1375 vd->vd_my / vf->vf_height) == 1)) { 1376 1377 /* 1378 * We have something marked to copy, so update pointer 1379 * to window with selection. 1380 */ 1381 vd->vd_markedwin = vw; 1382 } 1383 return; /* Done */ 1384 case MOUSE_BUTTON_EVENT: 1385 /* Buttons */ 1386 break; 1387 default: 1388 return; /* Done */ 1389 } 1390 1391 switch (event) { 1392 case MOUSE_BUTTON1DOWN: 1393 switch (cnt % 4) { 1394 case 0: /* up */ 1395 mark = VTB_MARK_END; 1396 break; 1397 case 1: /* single click: start cut operation */ 1398 mark = VTB_MARK_START; 1399 break; 1400 case 2: /* double click: cut a word */ 1401 mark = VTB_MARK_WORD; 1402 break; 1403 case 3: /* triple click: cut a line */ 1404 mark = VTB_MARK_ROW; 1405 break; 1406 } 1407 break; 1408 case VT_MOUSE_PASTEBUTTON: 1409 switch (cnt) { 1410 case 0: /* up */ 1411 break; 1412 default: 1413 if (vd->vd_markedwin == NULL) 1414 return; 1415 /* Get current selecton size in bytes. */ 1416 len = vtbuf_get_marked_len(&vd->vd_markedwin->vw_buf); 1417 if (len <= 0) 1418 return; 1419 1420 buf = malloc(len, M_VT, M_WAITOK | M_ZERO); 1421 /* Request cupy/paste buffer data, no more than `len' */ 1422 vtbuf_extract_marked(&vd->vd_markedwin->vw_buf, buf, 1423 len); 1424 1425 len /= sizeof(term_char_t); 1426 for (i = 0; i < len; i++ ) { 1427 if (buf[i] == '\0') 1428 continue; 1429 terminal_input_char(vw->vw_terminal, buf[i]); 1430 } 1431 1432 /* Done, so cleanup. */ 1433 free(buf, M_VT); 1434 break; 1435 } 1436 return; /* Done */ 1437 case VT_MOUSE_EXTENDBUTTON: 1438 switch (cnt) { 1439 case 0: /* up */ 1440 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN)) 1441 mark = VTB_MARK_EXTEND; 1442 else 1443 mark = 0; 1444 break; 1445 default: 1446 mark = VTB_MARK_EXTEND; 1447 break; 1448 } 1449 break; 1450 default: 1451 return; /* Done */ 1452 } 1453 1454 /* Save buttons state. */ 1455 if (cnt > 0) 1456 vd->vd_mstate |= event; 1457 else 1458 vd->vd_mstate &= ~event; 1459 1460 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width, 1461 vd->vd_my / vf->vf_height) == 1) { 1462 /* 1463 * We have something marked to copy, so update pointer to 1464 * window with selection. 1465 */ 1466 vd->vd_markedwin = vw; 1467 } 1468 } 1469 1470 void 1471 vt_mouse_state(int show) 1472 { 1473 struct vt_device *vd; 1474 struct vt_window *vw; 1475 1476 vd = main_vd; 1477 vw = vd->vd_curwindow; 1478 1479 switch (show) { 1480 case VT_MOUSE_HIDE: 1481 vw->vw_flags |= VWF_MOUSE_HIDE; 1482 break; 1483 case VT_MOUSE_SHOW: 1484 vw->vw_flags &= ~VWF_MOUSE_HIDE; 1485 break; 1486 } 1487 } 1488 #endif 1489 1490 static int 1491 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr, 1492 int nprot, vm_memattr_t *memattr) 1493 { 1494 struct vt_window *vw = tm->tm_softc; 1495 struct vt_device *vd = vw->vw_device; 1496 1497 if (vd->vd_driver->vd_fb_mmap) 1498 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot, 1499 memattr)); 1500 1501 return (ENXIO); 1502 } 1503 1504 static int 1505 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, 1506 struct thread *td) 1507 { 1508 struct vt_window *vw = tm->tm_softc; 1509 struct vt_device *vd = vw->vw_device; 1510 keyboard_t *kbd; 1511 int error, i, s; 1512 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 1513 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 1514 int ival; 1515 1516 switch (cmd) { 1517 case _IO('v', 4): 1518 cmd = VT_RELDISP; 1519 break; 1520 case _IO('v', 5): 1521 cmd = VT_ACTIVATE; 1522 break; 1523 case _IO('v', 6): 1524 cmd = VT_WAITACTIVE; 1525 break; 1526 case _IO('K', 20): 1527 cmd = KDSKBSTATE; 1528 break; 1529 case _IO('K', 67): 1530 cmd = KDSETRAD; 1531 break; 1532 case _IO('K', 7): 1533 cmd = KDSKBMODE; 1534 break; 1535 case _IO('K', 8): 1536 cmd = KDMKTONE; 1537 break; 1538 case _IO('K', 63): 1539 cmd = KIOCSOUND; 1540 break; 1541 case _IO('K', 66): 1542 cmd = KDSETLED; 1543 break; 1544 case _IO('c', 110): 1545 cmd = CONS_SETKBD; 1546 break; 1547 default: 1548 goto skip_thunk; 1549 } 1550 ival = IOCPARM_IVAL(data); 1551 data = (caddr_t)&ival; 1552 skip_thunk: 1553 #endif 1554 1555 switch (cmd) { 1556 case KDSETRAD: /* set keyboard repeat & delay rates (old) */ 1557 if (*(int *)data & ~0x7f) 1558 return (EINVAL); 1559 case GIO_KEYMAP: 1560 case PIO_KEYMAP: 1561 case GIO_DEADKEYMAP: 1562 case PIO_DEADKEYMAP: 1563 case GETFKEY: 1564 case SETFKEY: 1565 case KDGKBINFO: 1566 case KDGKBTYPE: 1567 case KDSKBSTATE: /* set keyboard state (locks) */ 1568 case KDGKBSTATE: /* get keyboard state (locks) */ 1569 case KDGETREPEAT: /* get keyboard repeat & delay rates */ 1570 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ 1571 case KDSETLED: /* set keyboard LED status */ 1572 case KDGETLED: /* get keyboard LED status */ 1573 case KBADDKBD: /* add/remove keyboard to/from mux */ 1574 case KBRELKBD: { 1575 error = 0; 1576 1577 mtx_lock(&Giant); 1578 kbd = kbd_get_keyboard(vd->vd_keyboard); 1579 if (kbd != NULL) 1580 error = kbdd_ioctl(kbd, cmd, data); 1581 mtx_unlock(&Giant); 1582 if (error == ENOIOCTL) { 1583 if (cmd == KDGKBTYPE) { 1584 /* always return something? XXX */ 1585 *(int *)data = 0; 1586 } else { 1587 return (ENODEV); 1588 } 1589 } 1590 return (error); 1591 } 1592 case KDGKBMODE: { 1593 int mode = -1; 1594 1595 mtx_lock(&Giant); 1596 kbd = kbd_get_keyboard(vd->vd_keyboard); 1597 if (kbd != NULL) { 1598 kbdd_ioctl(kbd, KDGKBMODE, (void *)&mode); 1599 } 1600 mtx_unlock(&Giant); 1601 DPRINTF(20, "mode %d, vw_kbdmode %d\n", mode, vw->vw_kbdmode); 1602 *(int *)data = mode; 1603 return (0); 1604 } 1605 case KDSKBMODE: { 1606 int mode; 1607 1608 mode = *(int *)data; 1609 switch (mode) { 1610 case K_XLATE: 1611 case K_RAW: 1612 case K_CODE: 1613 vw->vw_kbdmode = mode; 1614 if (vw == vd->vd_curwindow) { 1615 keyboard_t *kbd; 1616 error = 0; 1617 1618 mtx_lock(&Giant); 1619 kbd = kbd_get_keyboard(vd->vd_keyboard); 1620 if (kbd != NULL) { 1621 error = kbdd_ioctl(kbd, KDSKBMODE, 1622 (void *)&mode); 1623 } 1624 mtx_unlock(&Giant); 1625 } 1626 return (0); 1627 default: 1628 return (EINVAL); 1629 } 1630 } 1631 case FBIOGTYPE: 1632 case FBIO_GETWINORG: /* get frame buffer window origin */ 1633 case FBIO_GETDISPSTART: /* get display start address */ 1634 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */ 1635 case FBIO_BLANK: /* blank display */ 1636 if (vd->vd_driver->vd_fb_ioctl) 1637 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td)); 1638 break; 1639 case CONS_BLANKTIME: 1640 /* XXX */ 1641 return (0); 1642 case CONS_GET: 1643 /* XXX */ 1644 *(int *)data = M_CG640x480; 1645 return (0); 1646 case CONS_BELLTYPE: /* set bell type sound */ 1647 if ((*(int *)data) & CONS_QUIET_BELL) 1648 vd->vd_flags |= VDF_QUIET_BELL; 1649 else 1650 vd->vd_flags &= ~VDF_QUIET_BELL; 1651 return (0); 1652 case CONS_GETINFO: { 1653 vid_info_t *vi = (vid_info_t *)data; 1654 1655 vi->m_num = vd->vd_curwindow->vw_number + 1; 1656 /* XXX: other fields! */ 1657 return (0); 1658 } 1659 case CONS_GETVERS: 1660 *(int *)data = 0x200; 1661 return (0); 1662 case CONS_MODEINFO: 1663 /* XXX */ 1664 return (0); 1665 case CONS_MOUSECTL: { 1666 mouse_info_t *mouse = (mouse_info_t*)data; 1667 1668 /* 1669 * This has no effect on vt(4). We don't draw any mouse 1670 * cursor. Just ignore MOUSE_HIDE and MOUSE_SHOW to 1671 * prevent excessive errors. All the other commands 1672 * should not be applied to individual TTYs, but only to 1673 * consolectl. 1674 */ 1675 switch (mouse->operation) { 1676 case MOUSE_HIDE: 1677 vd->vd_flags &= ~VDF_MOUSECURSOR; 1678 return (0); 1679 case MOUSE_SHOW: 1680 vd->vd_mx = vd->vd_width / 2; 1681 vd->vd_my = vd->vd_height / 2; 1682 vd->vd_flags |= VDF_MOUSECURSOR; 1683 return (0); 1684 default: 1685 return (EINVAL); 1686 } 1687 } 1688 case PIO_VFONT: { 1689 struct vt_font *vf; 1690 1691 error = vtfont_load((void *)data, &vf); 1692 if (error != 0) 1693 return (error); 1694 1695 error = vt_change_font(vw, vf); 1696 if (error == 0) { 1697 /* XXX: replace 0 with current bg color. */ 1698 vt_set_border(vw, vf, 0); 1699 } 1700 vtfont_unref(vf); 1701 return (error); 1702 } 1703 case GIO_SCRNMAP: { 1704 scrmap_t *sm = (scrmap_t *)data; 1705 int i; 1706 1707 /* We don't have screen maps, so return a handcrafted one. */ 1708 for (i = 0; i < 256; i++) 1709 sm->scrmap[i] = i; 1710 return (0); 1711 } 1712 case KDSETMODE: 1713 /* XXX */ 1714 return (0); 1715 case KDENABIO: /* allow io operations */ 1716 error = priv_check(td, PRIV_IO); 1717 if (error != 0) 1718 return (error); 1719 error = securelevel_gt(td->td_ucred, 0); 1720 if (error != 0) 1721 return (error); 1722 #if defined(__i386__) 1723 td->td_frame->tf_eflags |= PSL_IOPL; 1724 #elif defined(__amd64__) 1725 td->td_frame->tf_rflags |= PSL_IOPL; 1726 #endif 1727 return (0); 1728 case KDDISABIO: /* disallow io operations (default) */ 1729 #if defined(__i386__) 1730 td->td_frame->tf_eflags &= ~PSL_IOPL; 1731 #elif defined(__amd64__) 1732 td->td_frame->tf_rflags &= ~PSL_IOPL; 1733 #endif 1734 return (0); 1735 case KDMKTONE: /* sound the bell */ 1736 /* TODO */ 1737 return (0); 1738 case KIOCSOUND: /* make tone (*data) hz */ 1739 /* TODO */ 1740 return (0); 1741 case CONS_SETKBD: /* set the new keyboard */ 1742 mtx_lock(&Giant); 1743 error = 0; 1744 if (vd->vd_keyboard != *(int *)data) { 1745 kbd = kbd_get_keyboard(*(int *)data); 1746 if (kbd == NULL) { 1747 mtx_unlock(&Giant); 1748 return (EINVAL); 1749 } 1750 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, 1751 (void *)&vd->vd_keyboard, vt_kbdevent, vd); 1752 if (i >= 0) { 1753 if (vd->vd_keyboard != -1) { 1754 kbd_release(kbd, 1755 (void *)&vd->vd_keyboard); 1756 } 1757 kbd = kbd_get_keyboard(i); 1758 vd->vd_keyboard = i; 1759 1760 (void)kbdd_ioctl(kbd, KDSKBMODE, 1761 (caddr_t)&vd->vd_curwindow->vw_kbdmode); 1762 } else { 1763 error = EPERM; /* XXX */ 1764 } 1765 } 1766 mtx_unlock(&Giant); 1767 return (error); 1768 case CONS_RELKBD: /* release the current keyboard */ 1769 mtx_lock(&Giant); 1770 error = 0; 1771 if (vd->vd_keyboard != -1) { 1772 kbd = kbd_get_keyboard(vd->vd_keyboard); 1773 if (kbd == NULL) { 1774 mtx_unlock(&Giant); 1775 return (EINVAL); 1776 } 1777 error = kbd_release(kbd, (void *)&vd->vd_keyboard); 1778 if (error == 0) { 1779 vd->vd_keyboard = -1; 1780 } 1781 } 1782 mtx_unlock(&Giant); 1783 return (error); 1784 case VT_ACTIVATE: { 1785 int win; 1786 win = *(int *)data - 1; 1787 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME, 1788 VT_UNIT(vw), win); 1789 if ((win > VT_MAXWINDOWS) || (win < 0)) 1790 return (EINVAL); 1791 return (vt_proc_window_switch(vd->vd_windows[win])); 1792 } 1793 case VT_GETACTIVE: 1794 *(int *)data = vd->vd_curwindow->vw_number + 1; 1795 return (0); 1796 case VT_GETINDEX: 1797 *(int *)data = vw->vw_number + 1; 1798 return (0); 1799 case VT_LOCKSWITCH: 1800 /* TODO: Check current state, switching can be in progress. */ 1801 if ((*(int *)data) & 0x01) 1802 vw->vw_flags |= VWF_VTYLOCK; 1803 else 1804 vw->vw_flags &= ~VWF_VTYLOCK; 1805 return (0); 1806 case VT_OPENQRY: 1807 VT_LOCK(vd); 1808 for (i = 0; i < VT_MAXWINDOWS; i++) { 1809 vw = vd->vd_windows[i]; 1810 if (vw == NULL) 1811 continue; 1812 if (!(vw->vw_flags & VWF_OPENED)) { 1813 *(int *)data = vw->vw_number + 1; 1814 VT_UNLOCK(vd); 1815 return (0); 1816 } 1817 } 1818 VT_UNLOCK(vd); 1819 return (EINVAL); 1820 case VT_WAITACTIVE: 1821 error = 0; 1822 1823 i = *(unsigned int *)data; 1824 if (i > VT_MAXWINDOWS) 1825 return (EINVAL); 1826 if (i != 0) 1827 vw = vd->vd_windows[i - 1]; 1828 1829 VT_LOCK(vd); 1830 while (vd->vd_curwindow != vw && error == 0) 1831 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 1832 VT_UNLOCK(vd); 1833 return (error); 1834 case VT_SETMODE: { /* set screen switcher mode */ 1835 struct vt_mode *mode; 1836 struct proc *p1; 1837 1838 mode = (struct vt_mode *)data; 1839 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw)); 1840 if (vw->vw_smode.mode == VT_PROCESS) { 1841 p1 = pfind(vw->vw_pid); 1842 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) { 1843 if (p1) 1844 PROC_UNLOCK(p1); 1845 DPRINTF(5, "error EPERM\n"); 1846 return (EPERM); 1847 } 1848 if (p1) 1849 PROC_UNLOCK(p1); 1850 } 1851 if (mode->mode == VT_AUTO) { 1852 vw->vw_smode.mode = VT_AUTO; 1853 vw->vw_proc = NULL; 1854 vw->vw_pid = 0; 1855 DPRINTF(5, "VT_AUTO, "); 1856 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1857 cnavailable(vw->vw_terminal->consdev, TRUE); 1858 /* were we in the middle of the vty switching process? */ 1859 if (finish_vt_rel(vw, TRUE, &s) == 0) 1860 DPRINTF(5, "reset WAIT_REL, "); 1861 if (finish_vt_acq(vw) == 0) 1862 DPRINTF(5, "reset WAIT_ACQ, "); 1863 return (0); 1864 } else if (mode->mode == VT_PROCESS) { 1865 if (!ISSIGVALID(mode->relsig) || 1866 !ISSIGVALID(mode->acqsig) || 1867 !ISSIGVALID(mode->frsig)) { 1868 DPRINTF(5, "error EINVAL\n"); 1869 return (EINVAL); 1870 } 1871 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid); 1872 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode)); 1873 vw->vw_proc = td->td_proc; 1874 vw->vw_pid = vw->vw_proc->p_pid; 1875 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 1876 cnavailable(vw->vw_terminal->consdev, FALSE); 1877 } else { 1878 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n", 1879 mode->mode); 1880 return (EINVAL); 1881 } 1882 DPRINTF(5, "\n"); 1883 return (0); 1884 } 1885 case VT_GETMODE: /* get screen switcher mode */ 1886 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode)); 1887 return (0); 1888 1889 case VT_RELDISP: /* screen switcher ioctl */ 1890 /* 1891 * This must be the current vty which is in the VT_PROCESS 1892 * switching mode... 1893 */ 1894 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode != 1895 VT_PROCESS)) { 1896 return (EINVAL); 1897 } 1898 /* ...and this process is controlling it. */ 1899 if (vw->vw_proc != td->td_proc) { 1900 return (EPERM); 1901 } 1902 error = EINVAL; 1903 switch(*(int *)data) { 1904 case VT_FALSE: /* user refuses to release screen, abort */ 1905 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0) 1906 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n", 1907 SC_DRIVER_NAME, VT_UNIT(vw)); 1908 break; 1909 case VT_TRUE: /* user has released screen, go on */ 1910 /* finish_vt_rel(..., TRUE, ...) should not be locked */ 1911 if (vw->vw_flags & VWF_SWWAIT_REL) { 1912 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0) 1913 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n", 1914 SC_DRIVER_NAME, VT_UNIT(vw)); 1915 } else { 1916 error = EINVAL; 1917 } 1918 return (error); 1919 case VT_ACKACQ: /* acquire acknowledged, switch completed */ 1920 if ((error = finish_vt_acq(vw)) == 0) 1921 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n", 1922 SC_DRIVER_NAME, VT_UNIT(vw)); 1923 break; 1924 default: 1925 break; 1926 } 1927 return (error); 1928 } 1929 1930 return (ENOIOCTL); 1931 } 1932 1933 static struct vt_window * 1934 vt_allocate_window(struct vt_device *vd, unsigned int window) 1935 { 1936 struct vt_window *vw; 1937 struct terminal *tm; 1938 term_pos_t size; 1939 struct winsize wsz; 1940 1941 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO); 1942 vw->vw_device = vd; 1943 vw->vw_number = window; 1944 vw->vw_kbdmode = K_XLATE; 1945 1946 if (!(vd->vd_flags & VDF_TEXTMODE)) 1947 vw->vw_font = vtfont_ref(&vt_font_default); 1948 1949 vt_termsize(vd, vw->vw_font, &size); 1950 vt_winsize(vd, vw->vw_font, &wsz); 1951 vtbuf_init(&vw->vw_buf, &size); 1952 1953 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw); 1954 terminal_set_winsize(tm, &wsz); 1955 vd->vd_windows[window] = vw; 1956 callout_init(&vw->vw_proc_dead_timer, 0); 1957 1958 return (vw); 1959 } 1960 1961 void 1962 vt_upgrade(struct vt_device *vd) 1963 { 1964 struct vt_window *vw; 1965 unsigned int i; 1966 1967 /* Device didn't pass vd_init() or already upgraded. */ 1968 if (vd->vd_flags & (VDF_ASYNC|VDF_DEAD)) 1969 return; 1970 vd->vd_flags |= VDF_ASYNC; 1971 1972 for (i = 0; i < VT_MAXWINDOWS; i++) { 1973 vw = vd->vd_windows[i]; 1974 if (vw == NULL) { 1975 /* New window. */ 1976 vw = vt_allocate_window(vd, i); 1977 } else if (vw->vw_flags & VWF_CONSOLE) { 1978 /* For existing console window. */ 1979 callout_init(&vw->vw_proc_dead_timer, 0); 1980 } 1981 if (i == VT_CONSWINDOW) { 1982 /* Console window. */ 1983 EVENTHANDLER_REGISTER(shutdown_pre_sync, 1984 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT); 1985 } 1986 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw)); 1987 1988 } 1989 VT_LOCK(vd); 1990 if (vd->vd_curwindow == NULL) 1991 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW]; 1992 1993 /* Attach keyboard. */ 1994 vt_allocate_keyboard(vd); 1995 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, vd->vd_keyboard); 1996 1997 /* Init 25 Hz timer. */ 1998 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0); 1999 2000 /* Start timer when everything ready. */ 2001 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd); 2002 VT_UNLOCK(vd); 2003 } 2004 2005 static void 2006 vt_resize(struct vt_device *vd) 2007 { 2008 struct vt_window *vw; 2009 int i; 2010 2011 for (i = 0; i < VT_MAXWINDOWS; i++) { 2012 vw = vd->vd_windows[i]; 2013 VT_LOCK(vd); 2014 /* Assign default font to window, if not textmode. */ 2015 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL) 2016 vw->vw_font = vtfont_ref(&vt_font_default); 2017 VT_UNLOCK(vd); 2018 /* Resize terminal windows */ 2019 vt_change_font(vw, vw->vw_font); 2020 } 2021 } 2022 2023 void 2024 vt_allocate(struct vt_driver *drv, void *softc) 2025 { 2026 struct vt_device *vd; 2027 struct winsize wsz; 2028 2029 if (main_vd == NULL) { 2030 main_vd = malloc(sizeof *vd, M_VT, M_WAITOK|M_ZERO); 2031 printf("VT: initialize with new VT driver \"%s\".\n", 2032 drv->vd_name); 2033 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF); 2034 cv_init(&main_vd->vd_winswitch, "vtwswt"); 2035 2036 } else { 2037 /* 2038 * Check if have rights to replace current driver. For example: 2039 * it is bad idea to replace KMS driver with generic VGA one. 2040 */ 2041 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) { 2042 printf("VT: Driver priority %d too low. Current %d\n ", 2043 drv->vd_priority, main_vd->vd_driver->vd_priority); 2044 return; 2045 } 2046 printf("VT: Replacing driver \"%s\" with new \"%s\".\n", 2047 main_vd->vd_driver->vd_name, drv->vd_name); 2048 } 2049 vd = main_vd; 2050 VT_LOCK(vd); 2051 if (drv->vd_maskbitbltchr == NULL) 2052 drv->vd_maskbitbltchr = drv->vd_bitbltchr; 2053 2054 if (vd->vd_flags & VDF_ASYNC) { 2055 /* Stop vt_flush periodic task. */ 2056 callout_drain(&vd->vd_timer); 2057 /* 2058 * Mute current terminal until we done. vt_change_font (called 2059 * from vt_resize) will unmute it. 2060 */ 2061 terminal_mute(vd->vd_curwindow->vw_terminal, 1); 2062 } 2063 2064 /* 2065 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will 2066 * set it. 2067 */ 2068 vd->vd_flags &= ~VDF_TEXTMODE; 2069 2070 vd->vd_driver = drv; 2071 vd->vd_softc = softc; 2072 vd->vd_driver->vd_init(vd); 2073 VT_UNLOCK(vd); 2074 2075 vt_upgrade(vd); 2076 2077 /* Refill settings with new sizes. */ 2078 vt_resize(vd); 2079 2080 #ifdef DEV_SPLASH 2081 if (vd->vd_flags & VDF_SPLASH) 2082 vtterm_splash(vd); 2083 #endif 2084 2085 if (vd->vd_flags & VDF_ASYNC) { 2086 terminal_mute(vd->vd_curwindow->vw_terminal, 0); 2087 callout_schedule(&vd->vd_timer, hz / VT_TIMERFREQ); 2088 } 2089 2090 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal); 2091 2092 /* Update console window sizes to actual. */ 2093 vt_winsize(vd, vd->vd_windows[VT_CONSWINDOW]->vw_font, &wsz); 2094 terminal_set_winsize(vd->vd_windows[VT_CONSWINDOW]->vw_terminal, &wsz); 2095 } 2096 2097 void 2098 vt_suspend() 2099 { 2100 2101 if (vt_suspendswitch == 0) 2102 return; 2103 /* Save current window. */ 2104 main_vd->vd_savedwindow = main_vd->vd_curwindow; 2105 /* Ask holding process to free window and switch to console window */ 2106 vt_proc_window_switch(main_vd->vd_windows[VT_CONSWINDOW]); 2107 } 2108 2109 void 2110 vt_resume() 2111 { 2112 2113 if (vt_suspendswitch == 0) 2114 return; 2115 /* Switch back to saved window */ 2116 if (main_vd->vd_savedwindow != NULL) 2117 vt_proc_window_switch(main_vd->vd_savedwindow); 2118 main_vd->vd_savedwindow = NULL; 2119 } 2120