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