1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009, 2013 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by Ed Schouten under sponsorship from the 8 * FreeBSD Foundation. 9 * 10 * Portions of this software were developed by Oleksandr Rybalko 11 * under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/consio.h> 40 #include <sys/devctl.h> 41 #include <sys/eventhandler.h> 42 #include <sys/fbio.h> 43 #include <sys/font.h> 44 #include <sys/kbio.h> 45 #include <sys/kdb.h> 46 #include <sys/kernel.h> 47 #include <sys/linker.h> 48 #include <sys/lock.h> 49 #include <sys/malloc.h> 50 #include <sys/mutex.h> 51 #include <sys/power.h> 52 #include <sys/priv.h> 53 #include <sys/proc.h> 54 #include <sys/random.h> 55 #include <sys/reboot.h> 56 #include <sys/sbuf.h> 57 #include <sys/systm.h> 58 #include <sys/terminal.h> 59 60 #include <dev/kbd/kbdreg.h> 61 #include <dev/vt/vt.h> 62 63 #if defined(__i386__) || defined(__amd64__) 64 #include <machine/psl.h> 65 #include <machine/frame.h> 66 #endif 67 68 static int vtterm_cngrab_noswitch(struct vt_device *, struct vt_window *); 69 static int vtterm_cnungrab_noswitch(struct vt_device *, struct vt_window *); 70 71 static tc_bell_t vtterm_bell; 72 static tc_cursor_t vtterm_cursor; 73 static tc_putchar_t vtterm_putchar; 74 static tc_fill_t vtterm_fill; 75 static tc_copy_t vtterm_copy; 76 static tc_pre_input_t vtterm_pre_input; 77 static tc_post_input_t vtterm_post_input; 78 static tc_param_t vtterm_param; 79 static tc_done_t vtterm_done; 80 81 static tc_cnprobe_t vtterm_cnprobe; 82 static tc_cngetc_t vtterm_cngetc; 83 84 static tc_cngrab_t vtterm_cngrab; 85 static tc_cnungrab_t vtterm_cnungrab; 86 87 static tc_opened_t vtterm_opened; 88 static tc_ioctl_t vtterm_ioctl; 89 static tc_mmap_t vtterm_mmap; 90 91 const struct terminal_class vt_termclass = { 92 .tc_bell = vtterm_bell, 93 .tc_cursor = vtterm_cursor, 94 .tc_putchar = vtterm_putchar, 95 .tc_fill = vtterm_fill, 96 .tc_copy = vtterm_copy, 97 .tc_pre_input = vtterm_pre_input, 98 .tc_post_input = vtterm_post_input, 99 .tc_param = vtterm_param, 100 .tc_done = vtterm_done, 101 102 .tc_cnprobe = vtterm_cnprobe, 103 .tc_cngetc = vtterm_cngetc, 104 105 .tc_cngrab = vtterm_cngrab, 106 .tc_cnungrab = vtterm_cnungrab, 107 108 .tc_opened = vtterm_opened, 109 .tc_ioctl = vtterm_ioctl, 110 .tc_mmap = vtterm_mmap, 111 }; 112 113 /* 114 * Use a constant timer of 25 Hz to redraw the screen. 115 * 116 * XXX: In theory we should only fire up the timer when there is really 117 * activity. Unfortunately we cannot always start timers. We really 118 * don't want to process kernel messages synchronously, because it 119 * really slows down the system. 120 */ 121 #define VT_TIMERFREQ 25 122 123 /* Bell pitch/duration. */ 124 #define VT_BELLDURATION (SBT_1S / 20) 125 #define VT_BELLPITCH (1193182 / 800) /* Approx 1491Hz */ 126 127 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \ 128 (vw)->vw_number) 129 130 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 131 "vt(9) parameters"); 132 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)"); 133 static VT_SYSCTL_INT(enable_bell, 1, "Enable bell"); 134 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level"); 135 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode"); 136 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend"); 137 138 /* Allow to disable some keyboard combinations. */ 139 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. " 140 "See kbdmap(5) to configure."); 141 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. " 142 "See kbdmap(5) to configure."); 143 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. " 144 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete)."); 145 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. " 146 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc)."); 147 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. " 148 "See kbdmap(5) to configure."); 149 150 /* Used internally, not a tunable. */ 151 int vt_draw_logo_cpus; 152 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot"); 153 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed " 154 "(0 = do not override)"); 155 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style " 156 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)"); 157 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)"); 158 159 static unsigned int vt_unit = 0; 160 static MALLOC_DEFINE(M_VT, "vt", "vt device"); 161 struct vt_device *main_vd = &vt_consdev; 162 163 /* Boot logo. */ 164 extern unsigned int vt_logo_width; 165 extern unsigned int vt_logo_height; 166 extern unsigned int vt_logo_depth; 167 extern unsigned char vt_logo_image[]; 168 #ifndef DEV_SPLASH 169 #define vtterm_draw_cpu_logos(...) do {} while (0) 170 const unsigned int vt_logo_sprite_height; 171 #endif 172 173 /* 174 * Console font. vt_font_loader will be filled with font data passed 175 * by loader. If there is no font passed by boot loader, we use built in 176 * default. 177 */ 178 extern struct vt_font vt_font_default; 179 static struct vt_font vt_font_loader; 180 static struct vt_font *vt_font_assigned = &vt_font_default; 181 182 #ifndef SC_NO_CUTPASTE 183 extern struct vt_mouse_cursor vt_default_mouse_pointer; 184 #endif 185 186 static int signal_vt_rel(struct vt_window *); 187 static int signal_vt_acq(struct vt_window *); 188 static int finish_vt_rel(struct vt_window *, int, int *); 189 static int finish_vt_acq(struct vt_window *); 190 static int vt_window_switch(struct vt_window *); 191 static int vt_late_window_switch(struct vt_window *); 192 static int vt_proc_alive(struct vt_window *); 193 static void vt_resize(struct vt_device *); 194 static void vt_update_static(void *); 195 #ifndef SC_NO_CUTPASTE 196 static void vt_mouse_paste(void); 197 #endif 198 static void vt_suspend_handler(void *priv); 199 static void vt_resume_handler(void *priv); 200 201 SET_DECLARE(vt_drv_set, struct vt_driver); 202 203 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)) 204 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH)) 205 206 struct terminal vt_consterm; 207 static struct vt_window vt_conswindow; 208 #ifndef SC_NO_CONSDRAWN 209 static term_char_t vt_consdrawn[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 210 static term_color_t vt_consdrawnfg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 211 static term_color_t vt_consdrawnbg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 212 #endif 213 struct vt_device vt_consdev = { 214 .vd_driver = NULL, 215 .vd_softc = NULL, 216 .vd_prev_driver = NULL, 217 .vd_prev_softc = NULL, 218 .vd_flags = VDF_INVALID, 219 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, }, 220 .vd_curwindow = &vt_conswindow, 221 .vd_kbstate = 0, 222 223 #ifndef SC_NO_CUTPASTE 224 .vd_pastebuf = { 225 .vpb_buf = NULL, 226 .vpb_bufsz = 0, 227 .vpb_len = 0 228 }, 229 .vd_mcursor = &vt_default_mouse_pointer, 230 .vd_mcursor_fg = TC_WHITE, 231 .vd_mcursor_bg = TC_BLACK, 232 #endif 233 234 #ifndef SC_NO_CONSDRAWN 235 .vd_drawn = vt_consdrawn, 236 .vd_drawnfg = vt_consdrawnfg, 237 .vd_drawnbg = vt_consdrawnbg, 238 #endif 239 }; 240 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)]; 241 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE]; 242 static struct vt_window vt_conswindow = { 243 .vw_number = VT_CONSWINDOW, 244 .vw_flags = VWF_CONSOLE, 245 .vw_buf = { 246 .vb_buffer = &vt_constextbuf[0], 247 .vb_rows = &vt_constextbufrows[0], 248 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE, 249 .vb_curroffset = 0, 250 .vb_roffset = 0, 251 .vb_flags = VBF_STATIC, 252 .vb_mark_start = {.tp_row = 0, .tp_col = 0,}, 253 .vb_mark_end = {.tp_row = 0, .tp_col = 0,}, 254 .vb_scr_size = { 255 .tp_row = _VTDEFH, 256 .tp_col = _VTDEFW, 257 }, 258 }, 259 .vw_device = &vt_consdev, 260 .vw_terminal = &vt_consterm, 261 .vw_kbdmode = K_XLATE, 262 .vw_grabbed = 0, 263 .vw_bell_pitch = VT_BELLPITCH, 264 .vw_bell_duration = VT_BELLDURATION, 265 }; 266 struct terminal vt_consterm = { 267 .tm_class = &vt_termclass, 268 .tm_softc = &vt_conswindow, 269 .tm_flags = TF_CONS, 270 }; 271 static struct consdev vt_consterm_consdev = { 272 .cn_ops = &termcn_cnops, 273 .cn_arg = &vt_consterm, 274 .cn_name = "ttyv0", 275 }; 276 277 /* Add to set of consoles. */ 278 DATA_SET(cons_set, vt_consterm_consdev); 279 280 /* 281 * Right after kmem is done to allow early drivers to use locking and allocate 282 * memory. 283 */ 284 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static, 285 &vt_consdev); 286 /* Delay until all devices attached, to not waste time. */ 287 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade, 288 &vt_consdev); 289 290 /* Initialize locks/mem depended members. */ 291 static void 292 vt_update_static(void *dummy) 293 { 294 295 if (!vty_enabled(VTY_VT)) 296 return; 297 if (main_vd->vd_driver != NULL) 298 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name, 299 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution", 300 main_vd->vd_width, main_vd->vd_height); 301 else 302 printf("VT: init without driver.\n"); 303 304 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF); 305 cv_init(&main_vd->vd_winswitch, "vtwswt"); 306 } 307 308 static void 309 vt_schedule_flush(struct vt_device *vd, int ms) 310 { 311 312 if (ms <= 0) 313 /* Default to initial value. */ 314 ms = 1000 / VT_TIMERFREQ; 315 316 callout_schedule(&vd->vd_timer, hz / (1000 / ms)); 317 } 318 319 void 320 vt_resume_flush_timer(struct vt_window *vw, int ms) 321 { 322 struct vt_device *vd = vw->vw_device; 323 324 if (vd->vd_curwindow != vw) 325 return; 326 327 if (!(vd->vd_flags & VDF_ASYNC) || 328 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1)) 329 return; 330 331 vt_schedule_flush(vd, ms); 332 } 333 334 static void 335 vt_suspend_flush_timer(struct vt_device *vd) 336 { 337 /* 338 * As long as this function is called locked, callout_stop() 339 * has the same effect like callout_drain() with regard to 340 * preventing the callback function from executing. 341 */ 342 VT_LOCK_ASSERT(vd, MA_OWNED); 343 344 if (!(vd->vd_flags & VDF_ASYNC) || 345 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0)) 346 return; 347 348 callout_stop(&vd->vd_timer); 349 } 350 351 static void 352 vt_switch_timer(void *arg) 353 { 354 355 (void)vt_late_window_switch((struct vt_window *)arg); 356 } 357 358 static int 359 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd) 360 { 361 int mode, ret; 362 363 mode = 0; 364 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode); 365 if (ret == ENOIOCTL) 366 ret = ENODEV; 367 if (ret != 0) 368 return (ret); 369 370 vw->vw_kbdmode = mode; 371 372 return (0); 373 } 374 375 static int 376 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd) 377 { 378 int ret; 379 380 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode); 381 if (ret == ENOIOCTL) 382 ret = ENODEV; 383 384 return (ret); 385 } 386 387 static int 388 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd) 389 { 390 int state, ret; 391 392 state = 0; 393 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state); 394 if (ret == ENOIOCTL) 395 ret = ENODEV; 396 if (ret != 0) 397 return (ret); 398 399 vw->vw_kbdstate &= ~LOCK_MASK; 400 vw->vw_kbdstate |= state & LOCK_MASK; 401 402 return (0); 403 } 404 405 static int 406 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd) 407 { 408 int state, ret; 409 410 state = vw->vw_kbdstate & LOCK_MASK; 411 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state); 412 if (ret == ENOIOCTL) 413 ret = ENODEV; 414 415 return (ret); 416 } 417 418 static int 419 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd) 420 { 421 int leds, ret; 422 423 leds = 0; 424 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds); 425 if (ret == ENOIOCTL) 426 ret = ENODEV; 427 if (ret != 0) 428 return (ret); 429 430 vw->vw_kbdstate &= ~LED_MASK; 431 vw->vw_kbdstate |= leds & LED_MASK; 432 433 return (0); 434 } 435 436 static int 437 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd) 438 { 439 int leds, ret; 440 441 leds = vw->vw_kbdstate & LED_MASK; 442 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds); 443 if (ret == ENOIOCTL) 444 ret = ENODEV; 445 446 return (ret); 447 } 448 449 static int 450 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw) 451 { 452 453 DPRINTF(40, "%s\n", __func__); 454 curvw->vw_switch_to = vw; 455 /* Set timer to allow switch in case when process hang. */ 456 callout_reset(&vw->vw_proc_dead_timer, hz * vt_deadtimer, 457 vt_switch_timer, (void *)vw); 458 /* Notify process about vt switch attempt. */ 459 DPRINTF(30, "%s: Notify process.\n", __func__); 460 signal_vt_rel(curvw); 461 462 return (0); 463 } 464 465 static int 466 vt_window_postswitch(struct vt_window *vw) 467 { 468 469 signal_vt_acq(vw); 470 return (0); 471 } 472 473 /* vt_late_window_switch will do VT switching for regular case. */ 474 static int 475 vt_late_window_switch(struct vt_window *vw) 476 { 477 struct vt_window *curvw; 478 int ret; 479 480 callout_stop(&vw->vw_proc_dead_timer); 481 482 ret = vt_window_switch(vw); 483 if (ret != 0) { 484 /* 485 * If the switch hasn't happened, then return the VT 486 * to the current owner, if any. 487 */ 488 curvw = vw->vw_device->vd_curwindow; 489 if (curvw->vw_smode.mode == VT_PROCESS) 490 (void)vt_window_postswitch(curvw); 491 return (ret); 492 } 493 494 /* Notify owner process about terminal availability. */ 495 if (vw->vw_smode.mode == VT_PROCESS) { 496 ret = vt_window_postswitch(vw); 497 } 498 return (ret); 499 } 500 501 /* Switch window. */ 502 static int 503 vt_proc_window_switch(struct vt_window *vw) 504 { 505 struct vt_window *curvw; 506 struct vt_device *vd; 507 int ret; 508 509 /* Prevent switching to NULL */ 510 if (vw == NULL) { 511 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__); 512 return (EINVAL); 513 } 514 vd = vw->vw_device; 515 curvw = vd->vd_curwindow; 516 517 /* Check if virtual terminal is locked */ 518 if (curvw->vw_flags & VWF_VTYLOCK) 519 return (EBUSY); 520 521 /* Check if switch already in progress */ 522 if (curvw->vw_flags & VWF_SWWAIT_REL) { 523 /* Check if switching to same window */ 524 if (curvw->vw_switch_to == vw) { 525 DPRINTF(30, "%s: Switch in progress to same vw.", __func__); 526 return (0); /* success */ 527 } 528 DPRINTF(30, "%s: Switch in progress to different vw.", __func__); 529 return (EBUSY); 530 } 531 532 /* Avoid switching to already selected window */ 533 if (vw == curvw) { 534 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__); 535 return (0); /* success */ 536 } 537 538 /* 539 * Early check for an attempt to switch to a non-functional VT. 540 * The same check is done in vt_window_switch(), but it's better 541 * to fail as early as possible to avoid needless pre-switch 542 * actions. 543 */ 544 VT_LOCK(vd); 545 if ((vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)) == 0) { 546 VT_UNLOCK(vd); 547 return (EINVAL); 548 } 549 VT_UNLOCK(vd); 550 551 /* Ask current process permission to switch away. */ 552 if (curvw->vw_smode.mode == VT_PROCESS) { 553 DPRINTF(30, "%s: VT_PROCESS ", __func__); 554 if (vt_proc_alive(curvw) == FALSE) { 555 DPRINTF(30, "Dead. Cleaning."); 556 /* Dead */ 557 } else { 558 DPRINTF(30, "%s: Signaling process.\n", __func__); 559 /* Alive, try to ask him. */ 560 ret = vt_window_preswitch(vw, curvw); 561 /* Wait for process answer or timeout. */ 562 return (ret); 563 } 564 DPRINTF(30, "\n"); 565 } 566 567 ret = vt_late_window_switch(vw); 568 return (ret); 569 } 570 571 /* Switch window ignoring process locking. */ 572 static int 573 vt_window_switch(struct vt_window *vw) 574 { 575 struct vt_device *vd = vw->vw_device; 576 struct vt_window *curvw = vd->vd_curwindow; 577 keyboard_t *kbd; 578 579 if (kdb_active) { 580 /* 581 * When grabbing the console for the debugger, avoid 582 * locks as that can result in deadlock. While this 583 * could use try locks, that wouldn't really make a 584 * difference as there are sufficient barriers in 585 * debugger entry/exit to be equivalent to 586 * successfully try-locking here. 587 */ 588 if (curvw == vw) 589 return (0); 590 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) 591 return (EINVAL); 592 593 vd->vd_curwindow = vw; 594 vd->vd_flags |= VDF_INVALID; 595 if (vd->vd_driver->vd_postswitch) 596 vd->vd_driver->vd_postswitch(vd); 597 return (0); 598 } 599 600 VT_LOCK(vd); 601 if (curvw == vw) { 602 /* 603 * Nothing to do, except ensure the driver has the opportunity to 604 * switch to console mode when panicking, making sure the panic 605 * is readable (even when a GUI was using ttyv0). 606 */ 607 if ((kdb_active || panicstr) && vd->vd_driver->vd_postswitch) 608 vd->vd_driver->vd_postswitch(vd); 609 VT_UNLOCK(vd); 610 return (0); 611 } 612 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) { 613 VT_UNLOCK(vd); 614 return (EINVAL); 615 } 616 617 vt_suspend_flush_timer(vd); 618 619 vd->vd_curwindow = vw; 620 vd->vd_flags |= VDF_INVALID; 621 cv_broadcast(&vd->vd_winswitch); 622 VT_UNLOCK(vd); 623 624 if (vd->vd_driver->vd_postswitch) 625 vd->vd_driver->vd_postswitch(vd); 626 627 vt_resume_flush_timer(vw, 0); 628 629 /* Restore per-window keyboard mode. */ 630 mtx_lock(&Giant); 631 if ((kbd = vd->vd_keyboard) != NULL) { 632 if (curvw->vw_kbdmode == K_XLATE) 633 vt_save_kbd_state(curvw, kbd); 634 635 vt_update_kbd_mode(vw, kbd); 636 vt_update_kbd_state(vw, kbd); 637 } 638 mtx_unlock(&Giant); 639 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number); 640 641 return (0); 642 } 643 644 void 645 vt_termsize(struct vt_device *vd, struct vt_font *vf, term_pos_t *size) 646 { 647 648 size->tp_row = vd->vd_height; 649 if (vt_draw_logo_cpus) 650 size->tp_row -= vt_logo_sprite_height; 651 size->tp_col = vd->vd_width; 652 if (vf != NULL) { 653 size->tp_row = MIN(size->tp_row / vf->vf_height, 654 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)); 655 size->tp_col = MIN(size->tp_col / vf->vf_width, 656 PIXEL_WIDTH(VT_FB_MAX_WIDTH)); 657 } 658 } 659 660 static inline void 661 vt_termrect(struct vt_device *vd, struct vt_font *vf, term_rect_t *rect) 662 { 663 664 rect->tr_begin.tp_row = rect->tr_begin.tp_col = 0; 665 if (vt_draw_logo_cpus) 666 rect->tr_begin.tp_row = vt_logo_sprite_height; 667 668 rect->tr_end.tp_row = vd->vd_height; 669 rect->tr_end.tp_col = vd->vd_width; 670 671 if (vf != NULL) { 672 rect->tr_begin.tp_row = 673 howmany(rect->tr_begin.tp_row, vf->vf_height); 674 675 rect->tr_end.tp_row = MIN(rect->tr_end.tp_row / vf->vf_height, 676 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)); 677 rect->tr_end.tp_col = MIN(rect->tr_end.tp_col / vf->vf_width, 678 PIXEL_WIDTH(VT_FB_MAX_WIDTH)); 679 } 680 } 681 682 void 683 vt_winsize(struct vt_device *vd, struct vt_font *vf, struct winsize *size) 684 { 685 686 size->ws_ypixel = vd->vd_height; 687 if (vt_draw_logo_cpus) 688 size->ws_ypixel -= vt_logo_sprite_height; 689 size->ws_row = size->ws_ypixel; 690 size->ws_col = size->ws_xpixel = vd->vd_width; 691 if (vf != NULL) { 692 size->ws_row = MIN(size->ws_row / vf->vf_height, 693 PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)); 694 size->ws_col = MIN(size->ws_col / vf->vf_width, 695 PIXEL_WIDTH(VT_FB_MAX_WIDTH)); 696 } 697 } 698 699 void 700 vt_compute_drawable_area(struct vt_window *vw) 701 { 702 struct vt_device *vd; 703 struct vt_font *vf; 704 vt_axis_t height; 705 706 vd = vw->vw_device; 707 708 if (vw->vw_font == NULL) { 709 vw->vw_draw_area.tr_begin.tp_col = 0; 710 vw->vw_draw_area.tr_begin.tp_row = 0; 711 if (vt_draw_logo_cpus) 712 vw->vw_draw_area.tr_begin.tp_row = vt_logo_sprite_height; 713 vw->vw_draw_area.tr_end.tp_col = vd->vd_width; 714 vw->vw_draw_area.tr_end.tp_row = vd->vd_height; 715 return; 716 } 717 718 vf = vw->vw_font; 719 720 /* 721 * Compute the drawable area, so that the text is centered on 722 * the screen. 723 */ 724 725 height = vd->vd_height; 726 if (vt_draw_logo_cpus) 727 height -= vt_logo_sprite_height; 728 vw->vw_draw_area.tr_begin.tp_col = (vd->vd_width % vf->vf_width) / 2; 729 vw->vw_draw_area.tr_begin.tp_row = (height % vf->vf_height) / 2; 730 if (vt_draw_logo_cpus) 731 vw->vw_draw_area.tr_begin.tp_row += vt_logo_sprite_height; 732 vw->vw_draw_area.tr_end.tp_col = vw->vw_draw_area.tr_begin.tp_col + 733 rounddown(vd->vd_width, vf->vf_width); 734 vw->vw_draw_area.tr_end.tp_row = vw->vw_draw_area.tr_begin.tp_row + 735 rounddown(height, vf->vf_height); 736 } 737 738 static void 739 vt_scroll(struct vt_window *vw, int offset, int whence) 740 { 741 int diff; 742 term_pos_t size; 743 744 if ((vw->vw_flags & VWF_SCROLL) == 0) 745 return; 746 747 vt_termsize(vw->vw_device, vw->vw_font, &size); 748 749 diff = vthistory_seek(&vw->vw_buf, offset, whence); 750 if (diff) 751 vw->vw_device->vd_flags |= VDF_INVALID; 752 vt_resume_flush_timer(vw, 0); 753 } 754 755 static int 756 vt_machine_kbdevent(struct vt_device *vd, int c) 757 { 758 759 switch (c) { 760 case SPCLKEY | DBG: /* kbdmap(5) keyword `debug`. */ 761 if (vt_kbd_debug) { 762 kdb_enter(KDB_WHY_BREAK, "manual escape to debugger"); 763 #if VT_ALT_TO_ESC_HACK 764 /* 765 * There's an unfortunate conflict between SPCLKEY|DBG 766 * and VT_ALT_TO_ESC_HACK. Just assume they didn't mean 767 * it if we got to here. 768 */ 769 vd->vd_kbstate &= ~ALKED; 770 #endif 771 } 772 return (1); 773 case SPCLKEY | HALT: /* kbdmap(5) keyword `halt`. */ 774 if (vt_kbd_halt) 775 shutdown_nice(RB_HALT); 776 return (1); 777 case SPCLKEY | PASTE: /* kbdmap(5) keyword `paste`. */ 778 #ifndef SC_NO_CUTPASTE 779 /* Insert text from cut-paste buffer. */ 780 vt_mouse_paste(); 781 #endif 782 break; 783 case SPCLKEY | PDWN: /* kbdmap(5) keyword `pdwn`. */ 784 if (vt_kbd_poweroff) 785 shutdown_nice(RB_HALT|RB_POWEROFF); 786 return (1); 787 case SPCLKEY | PNC: /* kbdmap(5) keyword `panic`. */ 788 /* 789 * Request to immediate panic if sysctl 790 * kern.vt.enable_panic_key allow it. 791 */ 792 if (vt_kbd_panic) 793 panic("Forced by the panic key"); 794 return (1); 795 case SPCLKEY | RBT: /* kbdmap(5) keyword `boot`. */ 796 if (vt_kbd_reboot) 797 shutdown_nice(RB_AUTOBOOT); 798 return (1); 799 case SPCLKEY | SPSC: /* kbdmap(5) keyword `spsc`. */ 800 /* Force activatation/deactivation of the screen saver. */ 801 /* TODO */ 802 return (1); 803 case SPCLKEY | STBY: /* XXX Not present in kbdcontrol parser. */ 804 /* Put machine into Stand-By mode. */ 805 power_pm_suspend(POWER_SLEEP_STATE_STANDBY); 806 return (1); 807 case SPCLKEY | SUSP: /* kbdmap(5) keyword `susp`. */ 808 /* Suspend machine. */ 809 power_pm_suspend(POWER_SLEEP_STATE_SUSPEND); 810 return (1); 811 } 812 813 return (0); 814 } 815 816 static void 817 vt_scrollmode_kbdevent(struct vt_window *vw, int c, int console) 818 { 819 struct vt_device *vd; 820 term_pos_t size; 821 822 vd = vw->vw_device; 823 /* Only special keys handled in ScrollLock mode */ 824 if ((c & SPCLKEY) == 0) 825 return; 826 827 c &= ~SPCLKEY; 828 829 if (console == 0) { 830 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) { 831 vw = vd->vd_windows[c - F_SCR]; 832 vt_proc_window_switch(vw); 833 return; 834 } 835 VT_LOCK(vd); 836 } 837 838 switch (c) { 839 case SLK: { 840 /* Turn scrolling off. */ 841 vt_scroll(vw, 0, VHS_END); 842 VTBUF_SLCK_DISABLE(&vw->vw_buf); 843 vw->vw_flags &= ~VWF_SCROLL; 844 break; 845 } 846 case FKEY | F(49): /* Home key. */ 847 vt_scroll(vw, 0, VHS_SET); 848 break; 849 case FKEY | F(50): /* Arrow up. */ 850 vt_scroll(vw, -1, VHS_CUR); 851 break; 852 case FKEY | F(51): /* Page up. */ 853 vt_termsize(vd, vw->vw_font, &size); 854 vt_scroll(vw, -size.tp_row, VHS_CUR); 855 break; 856 case FKEY | F(57): /* End key. */ 857 vt_scroll(vw, 0, VHS_END); 858 break; 859 case FKEY | F(58): /* Arrow down. */ 860 vt_scroll(vw, 1, VHS_CUR); 861 break; 862 case FKEY | F(59): /* Page down. */ 863 vt_termsize(vd, vw->vw_font, &size); 864 vt_scroll(vw, size.tp_row, VHS_CUR); 865 break; 866 } 867 868 if (console == 0) 869 VT_UNLOCK(vd); 870 } 871 872 static int 873 vt_processkey(keyboard_t *kbd, struct vt_device *vd, int c) 874 { 875 struct vt_window *vw = vd->vd_curwindow; 876 877 random_harvest_queue(&c, sizeof(c), RANDOM_KEYBOARD); 878 #if VT_ALT_TO_ESC_HACK 879 if (c & RELKEY) { 880 switch (c & ~RELKEY) { 881 case (SPCLKEY | RALT): 882 if (vt_enable_altgr != 0) 883 break; 884 case (SPCLKEY | LALT): 885 vd->vd_kbstate &= ~ALKED; 886 } 887 /* Other keys ignored for RELKEY event. */ 888 return (0); 889 } else { 890 switch (c & ~RELKEY) { 891 case (SPCLKEY | RALT): 892 if (vt_enable_altgr != 0) 893 break; 894 case (SPCLKEY | LALT): 895 vd->vd_kbstate |= ALKED; 896 } 897 } 898 #else 899 if (c & RELKEY) 900 /* Other keys ignored for RELKEY event. */ 901 return (0); 902 #endif 903 904 if (vt_machine_kbdevent(vd, c)) 905 return (0); 906 907 if (vw->vw_flags & VWF_SCROLL) { 908 vt_scrollmode_kbdevent(vw, c, 0/* Not a console */); 909 /* Scroll mode keys handled, nothing to do more. */ 910 return (0); 911 } 912 913 if (c & SPCLKEY) { 914 c &= ~SPCLKEY; 915 916 if (c >= F_SCR && c <= MIN(L_SCR, F_SCR + VT_MAXWINDOWS - 1)) { 917 vw = vd->vd_windows[c - F_SCR]; 918 vt_proc_window_switch(vw); 919 return (0); 920 } 921 922 switch (c) { 923 case NEXT: 924 /* Switch to next VT. */ 925 c = (vw->vw_number + 1) % VT_MAXWINDOWS; 926 vw = vd->vd_windows[c]; 927 vt_proc_window_switch(vw); 928 return (0); 929 case PREV: 930 /* Switch to previous VT. */ 931 c = (vw->vw_number + VT_MAXWINDOWS - 1) % VT_MAXWINDOWS; 932 vw = vd->vd_windows[c]; 933 vt_proc_window_switch(vw); 934 return (0); 935 case SLK: { 936 vt_save_kbd_state(vw, kbd); 937 VT_LOCK(vd); 938 if (vw->vw_kbdstate & SLKED) { 939 /* Turn scrolling on. */ 940 vw->vw_flags |= VWF_SCROLL; 941 VTBUF_SLCK_ENABLE(&vw->vw_buf); 942 } else { 943 /* Turn scrolling off. */ 944 vw->vw_flags &= ~VWF_SCROLL; 945 VTBUF_SLCK_DISABLE(&vw->vw_buf); 946 vt_scroll(vw, 0, VHS_END); 947 } 948 VT_UNLOCK(vd); 949 break; 950 } 951 case FKEY | F(1): case FKEY | F(2): case FKEY | F(3): 952 case FKEY | F(4): case FKEY | F(5): case FKEY | F(6): 953 case FKEY | F(7): case FKEY | F(8): case FKEY | F(9): 954 case FKEY | F(10): case FKEY | F(11): case FKEY | F(12): 955 /* F1 through F12 keys. */ 956 terminal_input_special(vw->vw_terminal, 957 TKEY_F1 + c - (FKEY | F(1))); 958 break; 959 case FKEY | F(49): /* Home key. */ 960 terminal_input_special(vw->vw_terminal, TKEY_HOME); 961 break; 962 case FKEY | F(50): /* Arrow up. */ 963 terminal_input_special(vw->vw_terminal, TKEY_UP); 964 break; 965 case FKEY | F(51): /* Page up. */ 966 terminal_input_special(vw->vw_terminal, TKEY_PAGE_UP); 967 break; 968 case FKEY | F(53): /* Arrow left. */ 969 terminal_input_special(vw->vw_terminal, TKEY_LEFT); 970 break; 971 case FKEY | F(55): /* Arrow right. */ 972 terminal_input_special(vw->vw_terminal, TKEY_RIGHT); 973 break; 974 case FKEY | F(57): /* End key. */ 975 terminal_input_special(vw->vw_terminal, TKEY_END); 976 break; 977 case FKEY | F(58): /* Arrow down. */ 978 terminal_input_special(vw->vw_terminal, TKEY_DOWN); 979 break; 980 case FKEY | F(59): /* Page down. */ 981 terminal_input_special(vw->vw_terminal, TKEY_PAGE_DOWN); 982 break; 983 case FKEY | F(60): /* Insert key. */ 984 terminal_input_special(vw->vw_terminal, TKEY_INSERT); 985 break; 986 case FKEY | F(61): /* Delete key. */ 987 terminal_input_special(vw->vw_terminal, TKEY_DELETE); 988 break; 989 } 990 } else if (KEYFLAGS(c) == 0) { 991 /* Don't do UTF-8 conversion when doing raw mode. */ 992 if (vw->vw_kbdmode == K_XLATE) { 993 #if VT_ALT_TO_ESC_HACK 994 if (vd->vd_kbstate & ALKED) { 995 /* 996 * Prepend ESC sequence if one of ALT keys down. 997 */ 998 terminal_input_char(vw->vw_terminal, 0x1b); 999 } 1000 #endif 1001 #if defined(KDB) 1002 kdb_alt_break(c, &vd->vd_altbrk); 1003 #endif 1004 terminal_input_char(vw->vw_terminal, KEYCHAR(c)); 1005 } else 1006 terminal_input_raw(vw->vw_terminal, c); 1007 } 1008 return (0); 1009 } 1010 1011 static int 1012 vt_kbdevent(keyboard_t *kbd, int event, void *arg) 1013 { 1014 struct vt_device *vd = arg; 1015 int c; 1016 1017 switch (event) { 1018 case KBDIO_KEYINPUT: 1019 break; 1020 case KBDIO_UNLOADING: 1021 mtx_lock(&Giant); 1022 vd->vd_keyboard = NULL; 1023 kbd_release(kbd, (void *)vd); 1024 mtx_unlock(&Giant); 1025 return (0); 1026 default: 1027 return (EINVAL); 1028 } 1029 1030 while ((c = kbdd_read_char(kbd, 0)) != NOKEY) 1031 vt_processkey(kbd, vd, c); 1032 1033 return (0); 1034 } 1035 1036 static int 1037 vt_allocate_keyboard(struct vt_device *vd) 1038 { 1039 int grabbed, i, idx0, idx; 1040 keyboard_t *k0, *k; 1041 keyboard_info_t ki; 1042 1043 /* 1044 * If vt_upgrade() happens while the console is grabbed, we are 1045 * potentially going to switch keyboard devices while the keyboard is in 1046 * use. Unwind the grabbing of the current keyboard first, then we will 1047 * re-grab the new keyboard below, before we return. 1048 */ 1049 if (vd->vd_curwindow == &vt_conswindow) { 1050 grabbed = vd->vd_curwindow->vw_grabbed; 1051 for (i = 0; i < grabbed; ++i) 1052 vtterm_cnungrab_noswitch(vd, vd->vd_curwindow); 1053 } 1054 1055 idx0 = kbd_allocate("kbdmux", -1, vd, vt_kbdevent, vd); 1056 if (idx0 >= 0) { 1057 DPRINTF(20, "%s: kbdmux allocated, idx = %d\n", __func__, idx0); 1058 k0 = kbd_get_keyboard(idx0); 1059 1060 for (idx = kbd_find_keyboard2("*", -1, 0); 1061 idx != -1; 1062 idx = kbd_find_keyboard2("*", -1, idx + 1)) { 1063 k = kbd_get_keyboard(idx); 1064 1065 if (idx == idx0 || KBD_IS_BUSY(k)) 1066 continue; 1067 1068 bzero(&ki, sizeof(ki)); 1069 strncpy(ki.kb_name, k->kb_name, sizeof(ki.kb_name)); 1070 ki.kb_name[sizeof(ki.kb_name) - 1] = '\0'; 1071 ki.kb_unit = k->kb_unit; 1072 1073 kbdd_ioctl(k0, KBADDKBD, (caddr_t) &ki); 1074 } 1075 } else { 1076 DPRINTF(20, "%s: no kbdmux allocated\n", __func__); 1077 idx0 = kbd_allocate("*", -1, vd, vt_kbdevent, vd); 1078 if (idx0 < 0) { 1079 DPRINTF(10, "%s: No keyboard found.\n", __func__); 1080 return (-1); 1081 } 1082 k0 = kbd_get_keyboard(idx0); 1083 } 1084 vd->vd_keyboard = k0; 1085 DPRINTF(20, "%s: vd_keyboard = %d\n", __func__, 1086 vd->vd_keyboard->kb_index); 1087 1088 if (vd->vd_curwindow == &vt_conswindow) { 1089 for (i = 0; i < grabbed; ++i) 1090 vtterm_cngrab_noswitch(vd, vd->vd_curwindow); 1091 } 1092 1093 return (idx0); 1094 } 1095 1096 #define DEVCTL_LEN 64 1097 static void 1098 vtterm_devctl(bool enabled, bool hushed, int hz, sbintime_t duration) 1099 { 1100 struct sbuf sb; 1101 char *buf; 1102 1103 buf = malloc(DEVCTL_LEN, M_VT, M_NOWAIT); 1104 if (buf == NULL) 1105 return; 1106 sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN); 1107 sbuf_printf(&sb, "enabled=%s hushed=%s hz=%d duration_ms=%d", 1108 enabled ? "true" : "false", hushed ? "true" : "false", 1109 hz, (int)(duration / SBT_1MS)); 1110 sbuf_finish(&sb); 1111 if (sbuf_error(&sb) == 0) 1112 devctl_notify("VT", "BELL", "RING", sbuf_data(&sb)); 1113 sbuf_delete(&sb); 1114 free(buf, M_VT); 1115 } 1116 1117 static void 1118 vtterm_bell(struct terminal *tm) 1119 { 1120 struct vt_window *vw = tm->tm_softc; 1121 struct vt_device *vd = vw->vw_device; 1122 1123 vtterm_devctl(vt_enable_bell, vd->vd_flags & VDF_QUIET_BELL, 1124 vw->vw_bell_pitch, vw->vw_bell_duration); 1125 1126 if (!vt_enable_bell) 1127 return; 1128 1129 if (vd->vd_flags & VDF_QUIET_BELL) 1130 return; 1131 1132 if (vw->vw_bell_pitch == 0 || 1133 vw->vw_bell_duration == 0) 1134 return; 1135 1136 sysbeep(vw->vw_bell_pitch, vw->vw_bell_duration); 1137 } 1138 1139 static void 1140 vtterm_beep(struct terminal *tm, u_int param) 1141 { 1142 u_int freq; 1143 sbintime_t period; 1144 struct vt_window *vw = tm->tm_softc; 1145 struct vt_device *vd = vw->vw_device; 1146 1147 if ((param == 0) || ((param & 0xffff) == 0)) { 1148 vtterm_bell(tm); 1149 return; 1150 } 1151 1152 period = ((param >> 16) & 0xffff) * SBT_1MS; 1153 freq = 1193182 / (param & 0xffff); 1154 1155 vtterm_devctl(vt_enable_bell, vd->vd_flags & VDF_QUIET_BELL, 1156 freq, period); 1157 1158 if (!vt_enable_bell) 1159 return; 1160 1161 sysbeep(freq, period); 1162 } 1163 1164 static void 1165 vtterm_cursor(struct terminal *tm, const term_pos_t *p) 1166 { 1167 struct vt_window *vw = tm->tm_softc; 1168 1169 vtbuf_cursor_position(&vw->vw_buf, p); 1170 } 1171 1172 static void 1173 vtterm_putchar(struct terminal *tm, const term_pos_t *p, term_char_t c) 1174 { 1175 struct vt_window *vw = tm->tm_softc; 1176 1177 vtbuf_putchar(&vw->vw_buf, p, c); 1178 } 1179 1180 static void 1181 vtterm_fill(struct terminal *tm, const term_rect_t *r, term_char_t c) 1182 { 1183 struct vt_window *vw = tm->tm_softc; 1184 1185 vtbuf_fill(&vw->vw_buf, r, c); 1186 } 1187 1188 static void 1189 vtterm_copy(struct terminal *tm, const term_rect_t *r, 1190 const term_pos_t *p) 1191 { 1192 struct vt_window *vw = tm->tm_softc; 1193 1194 vtbuf_copy(&vw->vw_buf, r, p); 1195 } 1196 1197 static void 1198 vtterm_param(struct terminal *tm, int cmd, unsigned int arg) 1199 { 1200 struct vt_window *vw = tm->tm_softc; 1201 1202 switch (cmd) { 1203 case TP_SETLOCALCURSOR: 1204 /* 1205 * 0 means normal (usually block), 1 means hidden, and 1206 * 2 means blinking (always block) for compatibility with 1207 * syscons. We don't support any changes except hiding, 1208 * so must map 2 to 0. 1209 */ 1210 arg = (arg == 1) ? 0 : 1; 1211 /* FALLTHROUGH */ 1212 case TP_SHOWCURSOR: 1213 vtbuf_cursor_visibility(&vw->vw_buf, arg); 1214 vt_resume_flush_timer(vw, 0); 1215 break; 1216 case TP_MOUSE: 1217 vw->vw_mouse_level = arg; 1218 break; 1219 case TP_SETBELLPD: 1220 vw->vw_bell_pitch = TP_SETBELLPD_PITCH(arg); 1221 vw->vw_bell_duration = 1222 TICKS_2_MSEC(TP_SETBELLPD_DURATION(arg)) * SBT_1MS; 1223 break; 1224 } 1225 } 1226 1227 void 1228 vt_determine_colors(term_char_t c, int cursor, 1229 term_color_t *fg, term_color_t *bg) 1230 { 1231 term_color_t tmp; 1232 int invert; 1233 1234 invert = 0; 1235 1236 *fg = TCHAR_FGCOLOR(c); 1237 if (TCHAR_FORMAT(c) & TF_BOLD) 1238 *fg = TCOLOR_LIGHT(*fg); 1239 *bg = TCHAR_BGCOLOR(c); 1240 if (TCHAR_FORMAT(c) & TF_BLINK) 1241 *bg = TCOLOR_LIGHT(*bg); 1242 1243 if (TCHAR_FORMAT(c) & TF_REVERSE) 1244 invert ^= 1; 1245 if (cursor) 1246 invert ^= 1; 1247 1248 if (invert) { 1249 tmp = *fg; 1250 *fg = *bg; 1251 *bg = tmp; 1252 } 1253 } 1254 1255 #ifndef SC_NO_CUTPASTE 1256 int 1257 vt_is_cursor_in_area(const struct vt_device *vd, const term_rect_t *area) 1258 { 1259 unsigned int mx, my; 1260 1261 /* 1262 * We use the cursor position saved during the current refresh, 1263 * in case the cursor moved since. 1264 */ 1265 mx = vd->vd_mx_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_col; 1266 my = vd->vd_my_drawn + vd->vd_curwindow->vw_draw_area.tr_begin.tp_row; 1267 1268 if (mx >= area->tr_end.tp_col || 1269 mx + vd->vd_mcursor->width <= area->tr_begin.tp_col || 1270 my >= area->tr_end.tp_row || 1271 my + vd->vd_mcursor->height <= area->tr_begin.tp_row) 1272 return (0); 1273 return (1); 1274 } 1275 1276 static void 1277 vt_mark_mouse_position_as_dirty(struct vt_device *vd, int locked) 1278 { 1279 term_rect_t area; 1280 struct vt_window *vw; 1281 struct vt_font *vf; 1282 int x, y; 1283 1284 vw = vd->vd_curwindow; 1285 vf = vw->vw_font; 1286 1287 x = vd->vd_mx_drawn; 1288 y = vd->vd_my_drawn; 1289 1290 if (vf != NULL) { 1291 area.tr_begin.tp_col = x / vf->vf_width; 1292 area.tr_begin.tp_row = y / vf->vf_height; 1293 area.tr_end.tp_col = 1294 ((x + vd->vd_mcursor->width) / vf->vf_width) + 1; 1295 area.tr_end.tp_row = 1296 ((y + vd->vd_mcursor->height) / vf->vf_height) + 1; 1297 } else { 1298 /* 1299 * No font loaded (ie. vt_vga operating in textmode). 1300 * 1301 * FIXME: This fake area needs to be revisited once the 1302 * mouse cursor is supported in vt_vga's textmode. 1303 */ 1304 area.tr_begin.tp_col = x; 1305 area.tr_begin.tp_row = y; 1306 area.tr_end.tp_col = x + 2; 1307 area.tr_end.tp_row = y + 2; 1308 } 1309 1310 if (!locked) 1311 vtbuf_lock(&vw->vw_buf); 1312 if (vd->vd_driver->vd_invalidate_text) 1313 vd->vd_driver->vd_invalidate_text(vd, &area); 1314 vtbuf_dirty(&vw->vw_buf, &area); 1315 if (!locked) 1316 vtbuf_unlock(&vw->vw_buf); 1317 } 1318 #endif 1319 1320 static void 1321 vt_set_border(struct vt_device *vd, const term_rect_t *area, 1322 term_color_t c) 1323 { 1324 vd_drawrect_t *drawrect = vd->vd_driver->vd_drawrect; 1325 1326 if (drawrect == NULL) 1327 return; 1328 1329 /* Top bar */ 1330 if (area->tr_begin.tp_row > 0) 1331 drawrect(vd, 0, 0, vd->vd_width - 1, 1332 area->tr_begin.tp_row - 1, 1, c); 1333 1334 /* Left bar */ 1335 if (area->tr_begin.tp_col > 0) 1336 drawrect(vd, 0, area->tr_begin.tp_row, 1337 area->tr_begin.tp_col - 1, area->tr_end.tp_row - 1, 1, c); 1338 1339 /* Right bar */ 1340 if (area->tr_end.tp_col < vd->vd_width) 1341 drawrect(vd, area->tr_end.tp_col, area->tr_begin.tp_row, 1342 vd->vd_width - 1, area->tr_end.tp_row - 1, 1, c); 1343 1344 /* Bottom bar */ 1345 if (area->tr_end.tp_row < vd->vd_height) 1346 drawrect(vd, 0, area->tr_end.tp_row, vd->vd_width - 1, 1347 vd->vd_height - 1, 1, c); 1348 } 1349 1350 static int 1351 vt_flush(struct vt_device *vd) 1352 { 1353 struct vt_window *vw; 1354 struct vt_font *vf; 1355 term_rect_t tarea; 1356 #ifndef SC_NO_CUTPASTE 1357 int cursor_was_shown, cursor_moved; 1358 #endif 1359 1360 vw = vd->vd_curwindow; 1361 if (vw == NULL) 1362 return (0); 1363 1364 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY) 1365 return (0); 1366 1367 vf = vw->vw_font; 1368 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL)) 1369 return (0); 1370 1371 vtbuf_lock(&vw->vw_buf); 1372 1373 #ifndef SC_NO_CUTPASTE 1374 cursor_was_shown = vd->vd_mshown; 1375 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn || 1376 vd->vd_my != vd->vd_my_drawn); 1377 1378 /* Check if the cursor should be displayed or not. */ 1379 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */ 1380 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */ 1381 !kdb_active && !KERNEL_PANICKED()) { /* DDB inactive. */ 1382 vd->vd_mshown = 1; 1383 } else { 1384 vd->vd_mshown = 0; 1385 } 1386 1387 /* 1388 * If the cursor changed display state or moved, we must mark 1389 * the old position as dirty, so that it's erased. 1390 */ 1391 if (cursor_was_shown != vd->vd_mshown || 1392 (vd->vd_mshown && cursor_moved)) 1393 vt_mark_mouse_position_as_dirty(vd, true); 1394 1395 /* 1396 * Save position of the mouse cursor. It's used by backends to 1397 * know where to draw the cursor and during the next refresh to 1398 * erase the previous position. 1399 */ 1400 vd->vd_mx_drawn = vd->vd_mx; 1401 vd->vd_my_drawn = vd->vd_my; 1402 1403 /* 1404 * If the cursor is displayed and has moved since last refresh, 1405 * mark the new position as dirty. 1406 */ 1407 if (vd->vd_mshown && cursor_moved) 1408 vt_mark_mouse_position_as_dirty(vd, true); 1409 #endif 1410 1411 vtbuf_undirty(&vw->vw_buf, &tarea); 1412 1413 /* Force a full redraw when the screen contents might be invalid. */ 1414 if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) { 1415 const teken_attr_t *a; 1416 1417 vd->vd_flags &= ~VDF_INVALID; 1418 1419 a = teken_get_curattr(&vw->vw_terminal->tm_emulator); 1420 vt_set_border(vd, &vw->vw_draw_area, a->ta_bgcolor); 1421 vt_termrect(vd, vf, &tarea); 1422 if (vd->vd_driver->vd_invalidate_text) 1423 vd->vd_driver->vd_invalidate_text(vd, &tarea); 1424 if (vt_draw_logo_cpus) 1425 vtterm_draw_cpu_logos(vd); 1426 } 1427 1428 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) { 1429 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea); 1430 vtbuf_unlock(&vw->vw_buf); 1431 return (1); 1432 } 1433 1434 vtbuf_unlock(&vw->vw_buf); 1435 return (0); 1436 } 1437 1438 static void 1439 vt_timer(void *arg) 1440 { 1441 struct vt_device *vd; 1442 int changed; 1443 1444 vd = arg; 1445 /* Update screen if required. */ 1446 changed = vt_flush(vd); 1447 1448 /* Schedule for next update. */ 1449 if (changed) 1450 vt_schedule_flush(vd, 0); 1451 else 1452 vd->vd_timer_armed = 0; 1453 } 1454 1455 static void 1456 vtterm_pre_input(struct terminal *tm) 1457 { 1458 struct vt_window *vw = tm->tm_softc; 1459 1460 vtbuf_lock(&vw->vw_buf); 1461 } 1462 1463 static void 1464 vtterm_post_input(struct terminal *tm) 1465 { 1466 struct vt_window *vw = tm->tm_softc; 1467 1468 vtbuf_unlock(&vw->vw_buf); 1469 vt_resume_flush_timer(vw, 0); 1470 } 1471 1472 static void 1473 vtterm_done(struct terminal *tm) 1474 { 1475 struct vt_window *vw = tm->tm_softc; 1476 struct vt_device *vd = vw->vw_device; 1477 1478 if (kdb_active || KERNEL_PANICKED()) { 1479 /* Switch to the debugger. */ 1480 if (vd->vd_curwindow != vw) { 1481 vd->vd_curwindow = vw; 1482 vd->vd_flags |= VDF_INVALID; 1483 if (vd->vd_driver->vd_postswitch) 1484 vd->vd_driver->vd_postswitch(vd); 1485 } 1486 vd->vd_flags &= ~VDF_SPLASH; 1487 vt_flush(vd); 1488 } else if (!(vd->vd_flags & VDF_ASYNC)) { 1489 vt_flush(vd); 1490 } 1491 } 1492 1493 #ifdef DEV_SPLASH 1494 static void 1495 vtterm_splash(struct vt_device *vd) 1496 { 1497 vt_axis_t top, left; 1498 1499 /* Display a nice boot splash. */ 1500 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) { 1501 top = (vd->vd_height - vt_logo_height) / 2; 1502 left = (vd->vd_width - vt_logo_width) / 2; 1503 switch (vt_logo_depth) { 1504 case 1: 1505 /* XXX: Unhardcode colors! */ 1506 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow, 1507 vt_logo_image, NULL, vt_logo_width, vt_logo_height, 1508 left, top, TC_WHITE, TC_BLACK); 1509 } 1510 vd->vd_flags |= VDF_SPLASH; 1511 } 1512 } 1513 #endif 1514 1515 static struct vt_font * 1516 parse_font_info_static(struct font_info *fi) 1517 { 1518 struct vt_font *vfp; 1519 uintptr_t ptr; 1520 uint32_t checksum; 1521 1522 if (fi == NULL) 1523 return (NULL); 1524 1525 ptr = (uintptr_t)fi; 1526 /* 1527 * Compute and verify checksum. The total sum of all the fields 1528 * must be 0. 1529 */ 1530 checksum = fi->fi_width; 1531 checksum += fi->fi_height; 1532 checksum += fi->fi_bitmap_size; 1533 for (unsigned i = 0; i < VFNT_MAPS; i++) 1534 checksum += fi->fi_map_count[i]; 1535 1536 if (checksum + fi->fi_checksum != 0) 1537 return (NULL); 1538 1539 ptr += sizeof(struct font_info); 1540 ptr = roundup2(ptr, 8); 1541 1542 vfp = &vt_font_loader; 1543 vfp->vf_height = fi->fi_height; 1544 vfp->vf_width = fi->fi_width; 1545 /* This is default font, set refcount 1 to disable removal. */ 1546 vfp->vf_refcount = 1; 1547 for (unsigned i = 0; i < VFNT_MAPS; i++) { 1548 if (fi->fi_map_count[i] == 0) 1549 continue; 1550 vfp->vf_map_count[i] = fi->fi_map_count[i]; 1551 vfp->vf_map[i] = (vfnt_map_t *)ptr; 1552 ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t)); 1553 ptr = roundup2(ptr, 8); 1554 } 1555 vfp->vf_bytes = (uint8_t *)ptr; 1556 return (vfp); 1557 } 1558 1559 /* 1560 * Set up default font with allocated data structures. 1561 * However, we can not set refcount here, because it is already set and 1562 * incremented in vtterm_cnprobe() to avoid being released by font load from 1563 * userland. 1564 */ 1565 static struct vt_font * 1566 parse_font_info(struct font_info *fi) 1567 { 1568 struct vt_font *vfp; 1569 uintptr_t ptr; 1570 uint32_t checksum; 1571 size_t size; 1572 1573 if (fi == NULL) 1574 return (NULL); 1575 1576 ptr = (uintptr_t)fi; 1577 /* 1578 * Compute and verify checksum. The total sum of all the fields 1579 * must be 0. 1580 */ 1581 checksum = fi->fi_width; 1582 checksum += fi->fi_height; 1583 checksum += fi->fi_bitmap_size; 1584 for (unsigned i = 0; i < VFNT_MAPS; i++) 1585 checksum += fi->fi_map_count[i]; 1586 1587 if (checksum + fi->fi_checksum != 0) 1588 return (NULL); 1589 1590 ptr += sizeof(struct font_info); 1591 ptr = roundup2(ptr, 8); 1592 1593 vfp = &vt_font_loader; 1594 vfp->vf_height = fi->fi_height; 1595 vfp->vf_width = fi->fi_width; 1596 for (unsigned i = 0; i < VFNT_MAPS; i++) { 1597 if (fi->fi_map_count[i] == 0) 1598 continue; 1599 vfp->vf_map_count[i] = fi->fi_map_count[i]; 1600 size = fi->fi_map_count[i] * sizeof(vfnt_map_t); 1601 vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO); 1602 bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size); 1603 ptr += size; 1604 ptr = roundup2(ptr, 8); 1605 } 1606 vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO); 1607 bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size); 1608 return (vfp); 1609 } 1610 1611 static void 1612 vt_init_font(void *arg) 1613 { 1614 caddr_t kmdp; 1615 struct font_info *fi; 1616 struct vt_font *font; 1617 1618 kmdp = preload_search_by_type("elf kernel"); 1619 if (kmdp == NULL) 1620 kmdp = preload_search_by_type("elf64 kernel"); 1621 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *); 1622 1623 font = parse_font_info(fi); 1624 if (font != NULL) 1625 vt_font_assigned = font; 1626 } 1627 1628 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev); 1629 1630 static void 1631 vt_init_font_static(void) 1632 { 1633 caddr_t kmdp; 1634 struct font_info *fi; 1635 struct vt_font *font; 1636 1637 kmdp = preload_search_by_type("elf kernel"); 1638 if (kmdp == NULL) 1639 kmdp = preload_search_by_type("elf64 kernel"); 1640 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *); 1641 1642 font = parse_font_info_static(fi); 1643 if (font != NULL) 1644 vt_font_assigned = font; 1645 } 1646 1647 static void 1648 vtterm_cnprobe(struct terminal *tm, struct consdev *cp) 1649 { 1650 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL; 1651 struct vt_window *vw = tm->tm_softc; 1652 struct vt_device *vd = vw->vw_device; 1653 struct winsize wsz; 1654 const term_attr_t *a; 1655 1656 if (!vty_enabled(VTY_VT)) 1657 return; 1658 1659 if (vd->vd_flags & VDF_INITIALIZED) 1660 /* Initialization already done. */ 1661 return; 1662 1663 SET_FOREACH(vtdlist, vt_drv_set) { 1664 vtd = *vtdlist; 1665 if (vtd->vd_probe == NULL) 1666 continue; 1667 if (vtd->vd_probe(vd) == CN_DEAD) 1668 continue; 1669 if ((vtdbest == NULL) || 1670 (vtd->vd_priority > vtdbest->vd_priority)) 1671 vtdbest = vtd; 1672 } 1673 if (vtdbest == NULL) { 1674 cp->cn_pri = CN_DEAD; 1675 vd->vd_flags |= VDF_DEAD; 1676 } else { 1677 vd->vd_driver = vtdbest; 1678 cp->cn_pri = vd->vd_driver->vd_init(vd); 1679 } 1680 1681 /* Check if driver's vt_init return CN_DEAD. */ 1682 if (cp->cn_pri == CN_DEAD) { 1683 vd->vd_flags |= VDF_DEAD; 1684 } 1685 1686 /* Initialize any early-boot keyboard drivers */ 1687 kbd_configure(KB_CONF_PROBE_ONLY); 1688 1689 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1); 1690 vd->vd_windows[VT_CONSWINDOW] = vw; 1691 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw)); 1692 1693 vt_init_font_static(); 1694 1695 /* Attach default font if not in TEXTMODE. */ 1696 if ((vd->vd_flags & VDF_TEXTMODE) == 0) { 1697 vw->vw_font = vtfont_ref(vt_font_assigned); 1698 vt_compute_drawable_area(vw); 1699 } 1700 1701 /* 1702 * The original screen size was faked (_VTDEFW x _VTDEFH). Now 1703 * that we have the real viewable size, fix it in the static 1704 * buffer. 1705 */ 1706 if (vd->vd_width != 0 && vd->vd_height != 0) 1707 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size); 1708 1709 /* We need to access terminal attributes from vtbuf */ 1710 vw->vw_buf.vb_terminal = tm; 1711 vtbuf_init_early(&vw->vw_buf); 1712 vt_winsize(vd, vw->vw_font, &wsz); 1713 a = teken_get_curattr(&tm->tm_emulator); 1714 terminal_set_winsize_blank(tm, &wsz, 1, a); 1715 1716 if (vtdbest != NULL) { 1717 #ifdef DEV_SPLASH 1718 if (!vt_splash_cpu) 1719 vtterm_splash(vd); 1720 #endif 1721 vd->vd_flags |= VDF_INITIALIZED; 1722 } 1723 } 1724 1725 static int 1726 vtterm_cngetc(struct terminal *tm) 1727 { 1728 struct vt_window *vw = tm->tm_softc; 1729 struct vt_device *vd = vw->vw_device; 1730 keyboard_t *kbd; 1731 u_int c; 1732 1733 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1734 return (*vw->vw_kbdsq++); 1735 1736 /* Make sure the splash screen is not there. */ 1737 if (vd->vd_flags & VDF_SPLASH) { 1738 /* Remove splash */ 1739 vd->vd_flags &= ~VDF_SPLASH; 1740 /* Mark screen as invalid to force update */ 1741 vd->vd_flags |= VDF_INVALID; 1742 vt_flush(vd); 1743 } 1744 1745 /* Stripped down keyboard handler. */ 1746 if ((kbd = vd->vd_keyboard) == NULL) 1747 return (-1); 1748 1749 /* Force keyboard input mode to K_XLATE */ 1750 vw->vw_kbdmode = K_XLATE; 1751 vt_update_kbd_mode(vw, kbd); 1752 1753 /* Switch the keyboard to polling to make it work here. */ 1754 kbdd_poll(kbd, TRUE); 1755 c = kbdd_read_char(kbd, 0); 1756 kbdd_poll(kbd, FALSE); 1757 if (c & RELKEY) 1758 return (-1); 1759 1760 if (vw->vw_flags & VWF_SCROLL) { 1761 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */); 1762 vt_flush(vd); 1763 return (-1); 1764 } 1765 1766 /* Stripped down handling of vt_kbdevent(), without locking, etc. */ 1767 if (c & SPCLKEY) { 1768 switch (c) { 1769 case SPCLKEY | SLK: 1770 vt_save_kbd_state(vw, kbd); 1771 if (vw->vw_kbdstate & SLKED) { 1772 /* Turn scrolling on. */ 1773 vw->vw_flags |= VWF_SCROLL; 1774 VTBUF_SLCK_ENABLE(&vw->vw_buf); 1775 } else { 1776 /* Turn scrolling off. */ 1777 vt_scroll(vw, 0, VHS_END); 1778 vw->vw_flags &= ~VWF_SCROLL; 1779 VTBUF_SLCK_DISABLE(&vw->vw_buf); 1780 } 1781 break; 1782 /* XXX: KDB can handle history. */ 1783 case SPCLKEY | FKEY | F(50): /* Arrow up. */ 1784 vw->vw_kbdsq = "\x1b[A"; 1785 break; 1786 case SPCLKEY | FKEY | F(58): /* Arrow down. */ 1787 vw->vw_kbdsq = "\x1b[B"; 1788 break; 1789 case SPCLKEY | FKEY | F(55): /* Arrow right. */ 1790 vw->vw_kbdsq = "\x1b[C"; 1791 break; 1792 case SPCLKEY | FKEY | F(53): /* Arrow left. */ 1793 vw->vw_kbdsq = "\x1b[D"; 1794 break; 1795 } 1796 1797 /* Force refresh to make scrollback work. */ 1798 vt_flush(vd); 1799 } else if (KEYFLAGS(c) == 0) { 1800 return (KEYCHAR(c)); 1801 } 1802 1803 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1804 return (*vw->vw_kbdsq++); 1805 1806 return (-1); 1807 } 1808 1809 /* 1810 * These two do most of what we want to do in vtterm_cnungrab, but without 1811 * actually switching windows. This is necessary for, e.g., 1812 * vt_allocate_keyboard() to get the current keyboard into the state it needs to 1813 * be in without damaging the device's window state. 1814 * 1815 * Both return the current grab count, though it's only used in vtterm_cnungrab. 1816 */ 1817 static int 1818 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw) 1819 { 1820 keyboard_t *kbd; 1821 1822 if (vw->vw_grabbed++ > 0) 1823 return (vw->vw_grabbed); 1824 1825 if ((kbd = vd->vd_keyboard) == NULL) 1826 return (1); 1827 1828 /* 1829 * Make sure the keyboard is accessible even when the kbd device 1830 * driver is disabled. 1831 */ 1832 kbdd_enable(kbd); 1833 1834 /* We shall always use the keyboard in the XLATE mode here. */ 1835 vw->vw_prev_kbdmode = vw->vw_kbdmode; 1836 vw->vw_kbdmode = K_XLATE; 1837 vt_update_kbd_mode(vw, kbd); 1838 1839 kbdd_poll(kbd, TRUE); 1840 return (1); 1841 } 1842 1843 static int 1844 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw) 1845 { 1846 keyboard_t *kbd; 1847 1848 if (--vw->vw_grabbed > 0) 1849 return (vw->vw_grabbed); 1850 1851 if ((kbd = vd->vd_keyboard) == NULL) 1852 return (0); 1853 1854 kbdd_poll(kbd, FALSE); 1855 1856 vw->vw_kbdmode = vw->vw_prev_kbdmode; 1857 vt_update_kbd_mode(vw, kbd); 1858 kbdd_disable(kbd); 1859 return (0); 1860 } 1861 1862 static void 1863 vtterm_cngrab(struct terminal *tm) 1864 { 1865 struct vt_device *vd; 1866 struct vt_window *vw; 1867 1868 vw = tm->tm_softc; 1869 vd = vw->vw_device; 1870 1871 /* To be restored after we ungrab. */ 1872 if (vd->vd_grabwindow == NULL) 1873 vd->vd_grabwindow = vd->vd_curwindow; 1874 1875 if (!cold) 1876 vt_window_switch(vw); 1877 1878 vtterm_cngrab_noswitch(vd, vw); 1879 } 1880 1881 static void 1882 vtterm_cnungrab(struct terminal *tm) 1883 { 1884 struct vt_device *vd; 1885 struct vt_window *vw; 1886 1887 vw = tm->tm_softc; 1888 vd = vw->vw_device; 1889 1890 MPASS(vd->vd_grabwindow != NULL); 1891 if (vtterm_cnungrab_noswitch(vd, vw) != 0) 1892 return; 1893 1894 if (!cold && vd->vd_grabwindow != vw) 1895 vt_window_switch(vd->vd_grabwindow); 1896 1897 vd->vd_grabwindow = NULL; 1898 } 1899 1900 static void 1901 vtterm_opened(struct terminal *tm, int opened) 1902 { 1903 struct vt_window *vw = tm->tm_softc; 1904 struct vt_device *vd = vw->vw_device; 1905 1906 VT_LOCK(vd); 1907 vd->vd_flags &= ~VDF_SPLASH; 1908 if (opened) 1909 vw->vw_flags |= VWF_OPENED; 1910 else { 1911 vw->vw_flags &= ~VWF_OPENED; 1912 /* TODO: finish ACQ/REL */ 1913 } 1914 VT_UNLOCK(vd); 1915 } 1916 1917 static int 1918 vt_change_font(struct vt_window *vw, struct vt_font *vf) 1919 { 1920 struct vt_device *vd = vw->vw_device; 1921 struct terminal *tm = vw->vw_terminal; 1922 term_pos_t size; 1923 struct winsize wsz; 1924 1925 /* 1926 * Changing fonts. 1927 * 1928 * Changing fonts is a little tricky. We must prevent 1929 * simultaneous access to the device, so we must stop 1930 * the display timer and the terminal from accessing. 1931 * We need to switch fonts and grow our screen buffer. 1932 * 1933 * XXX: Right now the code uses terminal_mute() to 1934 * prevent data from reaching the console driver while 1935 * resizing the screen buffer. This isn't elegant... 1936 */ 1937 1938 VT_LOCK(vd); 1939 if (vw->vw_flags & VWF_BUSY) { 1940 /* Another process is changing the font. */ 1941 VT_UNLOCK(vd); 1942 return (EBUSY); 1943 } 1944 vw->vw_flags |= VWF_BUSY; 1945 VT_UNLOCK(vd); 1946 1947 vt_termsize(vd, vf, &size); 1948 vt_winsize(vd, vf, &wsz); 1949 1950 /* Grow the screen buffer and terminal. */ 1951 terminal_mute(tm, 1); 1952 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size); 1953 terminal_set_winsize_blank(tm, &wsz, 0, NULL); 1954 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor); 1955 terminal_mute(tm, 0); 1956 1957 /* Actually apply the font to the current window. */ 1958 VT_LOCK(vd); 1959 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) { 1960 /* 1961 * In case vt_change_font called to update size we don't need 1962 * to update font link. 1963 */ 1964 vtfont_unref(vw->vw_font); 1965 vw->vw_font = vtfont_ref(vf); 1966 } 1967 1968 /* 1969 * Compute the drawable area and move the mouse cursor inside 1970 * it, in case the new area is smaller than the previous one. 1971 */ 1972 vt_compute_drawable_area(vw); 1973 vd->vd_mx = min(vd->vd_mx, 1974 vw->vw_draw_area.tr_end.tp_col - 1975 vw->vw_draw_area.tr_begin.tp_col - 1); 1976 vd->vd_my = min(vd->vd_my, 1977 vw->vw_draw_area.tr_end.tp_row - 1978 vw->vw_draw_area.tr_begin.tp_row - 1); 1979 1980 /* Force a full redraw the next timer tick. */ 1981 if (vd->vd_curwindow == vw) { 1982 vd->vd_flags |= VDF_INVALID; 1983 vt_resume_flush_timer(vw, 0); 1984 } 1985 vw->vw_flags &= ~VWF_BUSY; 1986 VT_UNLOCK(vd); 1987 return (0); 1988 } 1989 1990 static int 1991 vt_proc_alive(struct vt_window *vw) 1992 { 1993 struct proc *p; 1994 1995 if (vw->vw_smode.mode != VT_PROCESS) 1996 return (FALSE); 1997 1998 if (vw->vw_proc) { 1999 if ((p = pfind(vw->vw_pid)) != NULL) 2000 PROC_UNLOCK(p); 2001 if (vw->vw_proc == p) 2002 return (TRUE); 2003 vw->vw_proc = NULL; 2004 vw->vw_smode.mode = VT_AUTO; 2005 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid); 2006 vw->vw_pid = 0; 2007 } 2008 return (FALSE); 2009 } 2010 2011 static int 2012 signal_vt_rel(struct vt_window *vw) 2013 { 2014 2015 if (vw->vw_smode.mode != VT_PROCESS) 2016 return (FALSE); 2017 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 2018 vw->vw_proc = NULL; 2019 vw->vw_pid = 0; 2020 return (TRUE); 2021 } 2022 vw->vw_flags |= VWF_SWWAIT_REL; 2023 PROC_LOCK(vw->vw_proc); 2024 kern_psignal(vw->vw_proc, vw->vw_smode.relsig); 2025 PROC_UNLOCK(vw->vw_proc); 2026 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid); 2027 return (TRUE); 2028 } 2029 2030 static int 2031 signal_vt_acq(struct vt_window *vw) 2032 { 2033 2034 if (vw->vw_smode.mode != VT_PROCESS) 2035 return (FALSE); 2036 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 2037 cnavailable(vw->vw_terminal->consdev, FALSE); 2038 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 2039 vw->vw_proc = NULL; 2040 vw->vw_pid = 0; 2041 return (TRUE); 2042 } 2043 vw->vw_flags |= VWF_SWWAIT_ACQ; 2044 PROC_LOCK(vw->vw_proc); 2045 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig); 2046 PROC_UNLOCK(vw->vw_proc); 2047 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid); 2048 return (TRUE); 2049 } 2050 2051 static int 2052 finish_vt_rel(struct vt_window *vw, int release, int *s) 2053 { 2054 2055 if (vw->vw_flags & VWF_SWWAIT_REL) { 2056 vw->vw_flags &= ~VWF_SWWAIT_REL; 2057 if (release) { 2058 callout_drain(&vw->vw_proc_dead_timer); 2059 (void)vt_late_window_switch(vw->vw_switch_to); 2060 } 2061 return (0); 2062 } 2063 return (EINVAL); 2064 } 2065 2066 static int 2067 finish_vt_acq(struct vt_window *vw) 2068 { 2069 2070 if (vw->vw_flags & VWF_SWWAIT_ACQ) { 2071 vw->vw_flags &= ~VWF_SWWAIT_ACQ; 2072 return (0); 2073 } 2074 return (EINVAL); 2075 } 2076 2077 #ifndef SC_NO_CUTPASTE 2078 static void 2079 vt_mouse_terminput_button(struct vt_device *vd, int button) 2080 { 2081 struct vt_window *vw; 2082 struct vt_font *vf; 2083 char mouseb[6] = "\x1B[M"; 2084 int i, x, y; 2085 2086 vw = vd->vd_curwindow; 2087 vf = vw->vw_font; 2088 2089 /* Translate to char position. */ 2090 x = vd->vd_mx / vf->vf_width; 2091 y = vd->vd_my / vf->vf_height; 2092 /* Avoid overflow. */ 2093 x = MIN(x, 255 - '!'); 2094 y = MIN(y, 255 - '!'); 2095 2096 mouseb[3] = ' ' + button; 2097 mouseb[4] = '!' + x; 2098 mouseb[5] = '!' + y; 2099 2100 for (i = 0; i < sizeof(mouseb); i++) 2101 terminal_input_char(vw->vw_terminal, mouseb[i]); 2102 } 2103 2104 static void 2105 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event, 2106 int cnt) 2107 { 2108 2109 switch (type) { 2110 case MOUSE_BUTTON_EVENT: 2111 if (cnt > 0) { 2112 /* Mouse button pressed. */ 2113 if (event & MOUSE_BUTTON1DOWN) 2114 vt_mouse_terminput_button(vd, 0); 2115 if (event & MOUSE_BUTTON2DOWN) 2116 vt_mouse_terminput_button(vd, 1); 2117 if (event & MOUSE_BUTTON3DOWN) 2118 vt_mouse_terminput_button(vd, 2); 2119 } else { 2120 /* Mouse button released. */ 2121 vt_mouse_terminput_button(vd, 3); 2122 } 2123 break; 2124 #ifdef notyet 2125 case MOUSE_MOTION_EVENT: 2126 if (mouse->u.data.z < 0) { 2127 /* Scroll up. */ 2128 sc_mouse_input_button(vd, 64); 2129 } else if (mouse->u.data.z > 0) { 2130 /* Scroll down. */ 2131 sc_mouse_input_button(vd, 65); 2132 } 2133 break; 2134 #endif 2135 } 2136 } 2137 2138 static void 2139 vt_mouse_paste() 2140 { 2141 term_char_t *buf; 2142 int i, len; 2143 2144 len = VD_PASTEBUFLEN(main_vd); 2145 buf = VD_PASTEBUF(main_vd); 2146 len /= sizeof(term_char_t); 2147 for (i = 0; i < len; i++) { 2148 if (buf[i] == '\0') 2149 continue; 2150 terminal_input_char(main_vd->vd_curwindow->vw_terminal, 2151 buf[i]); 2152 } 2153 } 2154 2155 void 2156 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel) 2157 { 2158 struct vt_device *vd; 2159 struct vt_window *vw; 2160 struct vt_font *vf; 2161 term_pos_t size; 2162 int len, mark; 2163 2164 vd = main_vd; 2165 vw = vd->vd_curwindow; 2166 vf = vw->vw_font; 2167 mark = 0; 2168 2169 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS)) 2170 /* 2171 * Either the mouse is disabled, or the window is in 2172 * "graphics mode". The graphics mode is usually set by 2173 * an X server, using the KDSETMODE ioctl. 2174 */ 2175 return; 2176 2177 if (vf == NULL) /* Text mode. */ 2178 return; 2179 2180 /* 2181 * TODO: add flag about pointer position changed, to not redraw chars 2182 * under mouse pointer when nothing changed. 2183 */ 2184 2185 if (vw->vw_mouse_level > 0) 2186 vt_mouse_terminput(vd, type, x, y, event, cnt); 2187 2188 switch (type) { 2189 case MOUSE_ACTION: 2190 case MOUSE_MOTION_EVENT: 2191 /* Movement */ 2192 x += vd->vd_mx; 2193 y += vd->vd_my; 2194 2195 vt_termsize(vd, vf, &size); 2196 2197 /* Apply limits. */ 2198 x = MAX(x, 0); 2199 y = MAX(y, 0); 2200 x = MIN(x, (size.tp_col * vf->vf_width) - 1); 2201 y = MIN(y, (size.tp_row * vf->vf_height) - 1); 2202 2203 vd->vd_mx = x; 2204 vd->vd_my = y; 2205 if (vd->vd_mstate & MOUSE_BUTTON1DOWN) 2206 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE, 2207 vd->vd_mx / vf->vf_width, 2208 vd->vd_my / vf->vf_height); 2209 2210 vt_resume_flush_timer(vw, 0); 2211 return; /* Done */ 2212 case MOUSE_BUTTON_EVENT: 2213 /* Buttons */ 2214 break; 2215 default: 2216 return; /* Done */ 2217 } 2218 2219 switch (event) { 2220 case MOUSE_BUTTON1DOWN: 2221 switch (cnt % 4) { 2222 case 0: /* up */ 2223 mark = VTB_MARK_END; 2224 break; 2225 case 1: /* single click: start cut operation */ 2226 mark = VTB_MARK_START; 2227 break; 2228 case 2: /* double click: cut a word */ 2229 mark = VTB_MARK_WORD; 2230 break; 2231 case 3: /* triple click: cut a line */ 2232 mark = VTB_MARK_ROW; 2233 break; 2234 } 2235 break; 2236 case VT_MOUSE_PASTEBUTTON: 2237 switch (cnt) { 2238 case 0: /* up */ 2239 break; 2240 default: 2241 vt_mouse_paste(); 2242 break; 2243 } 2244 return; /* Done */ 2245 case VT_MOUSE_EXTENDBUTTON: 2246 switch (cnt) { 2247 case 0: /* up */ 2248 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN)) 2249 mark = VTB_MARK_EXTEND; 2250 else 2251 mark = 0; 2252 break; 2253 default: 2254 mark = VTB_MARK_EXTEND; 2255 break; 2256 } 2257 break; 2258 default: 2259 return; /* Done */ 2260 } 2261 2262 /* Save buttons state. */ 2263 if (cnt > 0) 2264 vd->vd_mstate |= event; 2265 else 2266 vd->vd_mstate &= ~event; 2267 2268 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width, 2269 vd->vd_my / vf->vf_height) == 1) { 2270 /* 2271 * We have something marked to copy, so update pointer to 2272 * window with selection. 2273 */ 2274 vt_resume_flush_timer(vw, 0); 2275 2276 switch (mark) { 2277 case VTB_MARK_END: 2278 case VTB_MARK_WORD: 2279 case VTB_MARK_ROW: 2280 case VTB_MARK_EXTEND: 2281 break; 2282 default: 2283 /* Other types of mark do not require to copy data. */ 2284 return; 2285 } 2286 2287 /* Get current selection size in bytes. */ 2288 len = vtbuf_get_marked_len(&vw->vw_buf); 2289 if (len <= 0) 2290 return; 2291 2292 /* Reallocate buffer only if old one is too small. */ 2293 if (len > VD_PASTEBUFSZ(vd)) { 2294 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT, 2295 M_WAITOK | M_ZERO); 2296 /* Update buffer size. */ 2297 VD_PASTEBUFSZ(vd) = len; 2298 } 2299 /* Request copy/paste buffer data, no more than `len' */ 2300 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd), 2301 VD_PASTEBUFSZ(vd)); 2302 2303 VD_PASTEBUFLEN(vd) = len; 2304 2305 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */ 2306 } 2307 } 2308 2309 void 2310 vt_mouse_state(int show) 2311 { 2312 struct vt_device *vd; 2313 struct vt_window *vw; 2314 2315 vd = main_vd; 2316 vw = vd->vd_curwindow; 2317 2318 switch (show) { 2319 case VT_MOUSE_HIDE: 2320 vw->vw_flags |= VWF_MOUSE_HIDE; 2321 break; 2322 case VT_MOUSE_SHOW: 2323 vw->vw_flags &= ~VWF_MOUSE_HIDE; 2324 break; 2325 } 2326 2327 /* Mark mouse position as dirty. */ 2328 vt_mark_mouse_position_as_dirty(vd, false); 2329 vt_resume_flush_timer(vw, 0); 2330 } 2331 #endif 2332 2333 static int 2334 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr, 2335 int nprot, vm_memattr_t *memattr) 2336 { 2337 struct vt_window *vw = tm->tm_softc; 2338 struct vt_device *vd = vw->vw_device; 2339 2340 if (vd->vd_driver->vd_fb_mmap) 2341 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot, 2342 memattr)); 2343 2344 return (ENXIO); 2345 } 2346 2347 static int 2348 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, 2349 struct thread *td) 2350 { 2351 struct vt_window *vw = tm->tm_softc; 2352 struct vt_device *vd = vw->vw_device; 2353 keyboard_t *kbd; 2354 int error, i, s; 2355 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 2356 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 2357 int ival; 2358 2359 switch (cmd) { 2360 case _IO('v', 4): 2361 cmd = VT_RELDISP; 2362 break; 2363 case _IO('v', 5): 2364 cmd = VT_ACTIVATE; 2365 break; 2366 case _IO('v', 6): 2367 cmd = VT_WAITACTIVE; 2368 break; 2369 case _IO('K', 20): 2370 cmd = KDSKBSTATE; 2371 break; 2372 case _IO('K', 67): 2373 cmd = KDSETRAD; 2374 break; 2375 case _IO('K', 7): 2376 cmd = KDSKBMODE; 2377 break; 2378 case _IO('K', 8): 2379 cmd = KDMKTONE; 2380 break; 2381 case _IO('K', 10): 2382 cmd = KDSETMODE; 2383 break; 2384 case _IO('K', 13): 2385 cmd = KDSBORDER; 2386 break; 2387 case _IO('K', 63): 2388 cmd = KIOCSOUND; 2389 break; 2390 case _IO('K', 66): 2391 cmd = KDSETLED; 2392 break; 2393 case _IO('c', 104): 2394 cmd = CONS_SETWINORG; 2395 break; 2396 case _IO('c', 110): 2397 cmd = CONS_SETKBD; 2398 break; 2399 default: 2400 goto skip_thunk; 2401 } 2402 ival = IOCPARM_IVAL(data); 2403 data = (caddr_t)&ival; 2404 skip_thunk: 2405 #endif 2406 2407 switch (cmd) { 2408 case KDSETRAD: /* set keyboard repeat & delay rates (old) */ 2409 if (*(int *)data & ~0x7f) 2410 return (EINVAL); 2411 /* FALLTHROUGH */ 2412 case GIO_KEYMAP: 2413 case PIO_KEYMAP: 2414 case GIO_DEADKEYMAP: 2415 case PIO_DEADKEYMAP: 2416 case GETFKEY: 2417 case SETFKEY: 2418 case KDGKBINFO: 2419 case KDGKBTYPE: 2420 case KDGETREPEAT: /* get keyboard repeat & delay rates */ 2421 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ 2422 case KBADDKBD: /* add/remove keyboard to/from mux */ 2423 case KBRELKBD: { 2424 error = 0; 2425 2426 mtx_lock(&Giant); 2427 if ((kbd = vd->vd_keyboard) != NULL) 2428 error = kbdd_ioctl(kbd, cmd, data); 2429 mtx_unlock(&Giant); 2430 if (error == ENOIOCTL) { 2431 if (cmd == KDGKBTYPE) { 2432 /* always return something? XXX */ 2433 *(int *)data = 0; 2434 } else { 2435 return (ENODEV); 2436 } 2437 } 2438 return (error); 2439 } 2440 case KDGKBSTATE: { /* get keyboard state (locks) */ 2441 error = 0; 2442 2443 if (vw == vd->vd_curwindow) { 2444 mtx_lock(&Giant); 2445 if ((kbd = vd->vd_keyboard) != NULL) 2446 error = vt_save_kbd_state(vw, kbd); 2447 mtx_unlock(&Giant); 2448 2449 if (error != 0) 2450 return (error); 2451 } 2452 2453 *(int *)data = vw->vw_kbdstate & LOCK_MASK; 2454 2455 return (error); 2456 } 2457 case KDSKBSTATE: { /* set keyboard state (locks) */ 2458 int state; 2459 2460 state = *(int *)data; 2461 if (state & ~LOCK_MASK) 2462 return (EINVAL); 2463 2464 vw->vw_kbdstate &= ~LOCK_MASK; 2465 vw->vw_kbdstate |= state; 2466 2467 error = 0; 2468 if (vw == vd->vd_curwindow) { 2469 mtx_lock(&Giant); 2470 if ((kbd = vd->vd_keyboard) != NULL) 2471 error = vt_update_kbd_state(vw, kbd); 2472 mtx_unlock(&Giant); 2473 } 2474 2475 return (error); 2476 } 2477 case KDGETLED: { /* get keyboard LED status */ 2478 error = 0; 2479 2480 if (vw == vd->vd_curwindow) { 2481 mtx_lock(&Giant); 2482 if ((kbd = vd->vd_keyboard) != NULL) 2483 error = vt_save_kbd_leds(vw, kbd); 2484 mtx_unlock(&Giant); 2485 2486 if (error != 0) 2487 return (error); 2488 } 2489 2490 *(int *)data = vw->vw_kbdstate & LED_MASK; 2491 2492 return (error); 2493 } 2494 case KDSETLED: { /* set keyboard LED status */ 2495 int leds; 2496 2497 leds = *(int *)data; 2498 if (leds & ~LED_MASK) 2499 return (EINVAL); 2500 2501 vw->vw_kbdstate &= ~LED_MASK; 2502 vw->vw_kbdstate |= leds; 2503 2504 error = 0; 2505 if (vw == vd->vd_curwindow) { 2506 mtx_lock(&Giant); 2507 if ((kbd = vd->vd_keyboard) != NULL) 2508 error = vt_update_kbd_leds(vw, kbd); 2509 mtx_unlock(&Giant); 2510 } 2511 2512 return (error); 2513 } 2514 case KDGETMODE: 2515 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ? 2516 KD_GRAPHICS : KD_TEXT; 2517 return (0); 2518 case KDGKBMODE: { 2519 error = 0; 2520 2521 if (vw == vd->vd_curwindow) { 2522 mtx_lock(&Giant); 2523 if ((kbd = vd->vd_keyboard) != NULL) 2524 error = vt_save_kbd_mode(vw, kbd); 2525 mtx_unlock(&Giant); 2526 2527 if (error != 0) 2528 return (error); 2529 } 2530 2531 *(int *)data = vw->vw_kbdmode; 2532 2533 return (error); 2534 } 2535 case KDSKBMODE: { 2536 int mode; 2537 2538 mode = *(int *)data; 2539 switch (mode) { 2540 case K_XLATE: 2541 case K_RAW: 2542 case K_CODE: 2543 vw->vw_kbdmode = mode; 2544 2545 error = 0; 2546 if (vw == vd->vd_curwindow) { 2547 mtx_lock(&Giant); 2548 if ((kbd = vd->vd_keyboard) != NULL) 2549 error = vt_update_kbd_mode(vw, kbd); 2550 mtx_unlock(&Giant); 2551 } 2552 2553 return (error); 2554 default: 2555 return (EINVAL); 2556 } 2557 } 2558 case FBIOGTYPE: 2559 case FBIO_GETWINORG: /* get frame buffer window origin */ 2560 case FBIO_GETDISPSTART: /* get display start address */ 2561 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */ 2562 case FBIO_BLANK: /* blank display */ 2563 if (vd->vd_driver->vd_fb_ioctl) 2564 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td)); 2565 break; 2566 case CONS_BLANKTIME: 2567 /* XXX */ 2568 return (0); 2569 case CONS_HISTORY: 2570 if (*(int *)data < 0) 2571 return EINVAL; 2572 if (*(int *)data != vw->vw_buf.vb_history_size) 2573 vtbuf_sethistory_size(&vw->vw_buf, *(int *)data); 2574 return (0); 2575 case CONS_CLRHIST: 2576 vtbuf_clearhistory(&vw->vw_buf); 2577 /* 2578 * Invalidate the entire visible window; it is not guaranteed 2579 * that this operation will be immediately followed by a scroll 2580 * event, so it would otherwise be possible for prior artifacts 2581 * to remain visible. 2582 */ 2583 VT_LOCK(vd); 2584 if (vw == vd->vd_curwindow) { 2585 vd->vd_flags |= VDF_INVALID; 2586 vt_resume_flush_timer(vw, 0); 2587 } 2588 VT_UNLOCK(vd); 2589 return (0); 2590 case CONS_GET: 2591 /* XXX */ 2592 *(int *)data = M_CG640x480; 2593 return (0); 2594 case CONS_BELLTYPE: /* set bell type sound */ 2595 if ((*(int *)data) & CONS_QUIET_BELL) 2596 vd->vd_flags |= VDF_QUIET_BELL; 2597 else 2598 vd->vd_flags &= ~VDF_QUIET_BELL; 2599 return (0); 2600 case CONS_GETINFO: { 2601 vid_info_t *vi = (vid_info_t *)data; 2602 if (vi->size != sizeof(struct vid_info)) 2603 return (EINVAL); 2604 2605 if (vw == vd->vd_curwindow) { 2606 mtx_lock(&Giant); 2607 if ((kbd = vd->vd_keyboard) != NULL) 2608 vt_save_kbd_state(vw, kbd); 2609 mtx_unlock(&Giant); 2610 } 2611 2612 vi->m_num = vd->vd_curwindow->vw_number + 1; 2613 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK; 2614 /* XXX: other fields! */ 2615 return (0); 2616 } 2617 case CONS_GETVERS: 2618 *(int *)data = 0x200; 2619 return (0); 2620 case CONS_MODEINFO: 2621 /* XXX */ 2622 return (0); 2623 case CONS_MOUSECTL: { 2624 mouse_info_t *mouse = (mouse_info_t*)data; 2625 2626 /* 2627 * All the commands except MOUSE_SHOW nd MOUSE_HIDE 2628 * should not be applied to individual TTYs, but only to 2629 * consolectl. 2630 */ 2631 switch (mouse->operation) { 2632 case MOUSE_HIDE: 2633 if (vd->vd_flags & VDF_MOUSECURSOR) { 2634 vd->vd_flags &= ~VDF_MOUSECURSOR; 2635 #ifndef SC_NO_CUTPASTE 2636 vt_mouse_state(VT_MOUSE_HIDE); 2637 #endif 2638 } 2639 return (0); 2640 case MOUSE_SHOW: 2641 if (!(vd->vd_flags & VDF_MOUSECURSOR)) { 2642 vd->vd_flags |= VDF_MOUSECURSOR; 2643 vd->vd_mx = vd->vd_width / 2; 2644 vd->vd_my = vd->vd_height / 2; 2645 #ifndef SC_NO_CUTPASTE 2646 vt_mouse_state(VT_MOUSE_SHOW); 2647 #endif 2648 } 2649 return (0); 2650 default: 2651 return (EINVAL); 2652 } 2653 } 2654 case PIO_VFONT: { 2655 struct vt_font *vf; 2656 2657 if (vd->vd_flags & VDF_TEXTMODE) 2658 return (ENOTSUP); 2659 2660 error = vtfont_load((void *)data, &vf); 2661 if (error != 0) 2662 return (error); 2663 2664 error = vt_change_font(vw, vf); 2665 vtfont_unref(vf); 2666 return (error); 2667 } 2668 case PIO_VFONT_DEFAULT: { 2669 /* Reset to default font. */ 2670 error = vt_change_font(vw, vt_font_assigned); 2671 return (error); 2672 } 2673 case GIO_SCRNMAP: { 2674 scrmap_t *sm = (scrmap_t *)data; 2675 2676 /* We don't have screen maps, so return a handcrafted one. */ 2677 for (i = 0; i < 256; i++) 2678 sm->scrmap[i] = i; 2679 return (0); 2680 } 2681 case KDSETMODE: 2682 /* 2683 * FIXME: This implementation is incomplete compared to 2684 * syscons. 2685 */ 2686 switch (*(int *)data) { 2687 case KD_TEXT: 2688 case KD_TEXT1: 2689 case KD_PIXEL: 2690 vw->vw_flags &= ~VWF_GRAPHICS; 2691 break; 2692 case KD_GRAPHICS: 2693 vw->vw_flags |= VWF_GRAPHICS; 2694 break; 2695 } 2696 return (0); 2697 case KDENABIO: /* allow io operations */ 2698 error = priv_check(td, PRIV_IO); 2699 if (error != 0) 2700 return (error); 2701 error = securelevel_gt(td->td_ucred, 0); 2702 if (error != 0) 2703 return (error); 2704 #if defined(__i386__) 2705 td->td_frame->tf_eflags |= PSL_IOPL; 2706 #elif defined(__amd64__) 2707 td->td_frame->tf_rflags |= PSL_IOPL; 2708 #endif 2709 return (0); 2710 case KDDISABIO: /* disallow io operations (default) */ 2711 #if defined(__i386__) 2712 td->td_frame->tf_eflags &= ~PSL_IOPL; 2713 #elif defined(__amd64__) 2714 td->td_frame->tf_rflags &= ~PSL_IOPL; 2715 #endif 2716 return (0); 2717 case KDMKTONE: /* sound the bell */ 2718 vtterm_beep(tm, *(u_int *)data); 2719 return (0); 2720 case KIOCSOUND: /* make tone (*data) hz */ 2721 /* TODO */ 2722 return (0); 2723 case CONS_SETKBD: /* set the new keyboard */ 2724 mtx_lock(&Giant); 2725 error = 0; 2726 if (vd->vd_keyboard == NULL || 2727 vd->vd_keyboard->kb_index != *(int *)data) { 2728 kbd = kbd_get_keyboard(*(int *)data); 2729 if (kbd == NULL) { 2730 mtx_unlock(&Giant); 2731 return (EINVAL); 2732 } 2733 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, 2734 (void *)vd, vt_kbdevent, vd); 2735 if (i >= 0) { 2736 if ((kbd = vd->vd_keyboard) != NULL) { 2737 vt_save_kbd_state(vd->vd_curwindow, kbd); 2738 kbd_release(kbd, (void *)vd); 2739 } 2740 kbd = vd->vd_keyboard = kbd_get_keyboard(i); 2741 2742 vt_update_kbd_mode(vd->vd_curwindow, kbd); 2743 vt_update_kbd_state(vd->vd_curwindow, kbd); 2744 } else { 2745 error = EPERM; /* XXX */ 2746 } 2747 } 2748 mtx_unlock(&Giant); 2749 return (error); 2750 case CONS_RELKBD: /* release the current keyboard */ 2751 mtx_lock(&Giant); 2752 error = 0; 2753 if ((kbd = vd->vd_keyboard) != NULL) { 2754 vt_save_kbd_state(vd->vd_curwindow, kbd); 2755 error = kbd_release(kbd, (void *)vd); 2756 if (error == 0) { 2757 vd->vd_keyboard = NULL; 2758 } 2759 } 2760 mtx_unlock(&Giant); 2761 return (error); 2762 case VT_ACTIVATE: { 2763 int win; 2764 win = *(int *)data - 1; 2765 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME, 2766 VT_UNIT(vw), win); 2767 if ((win >= VT_MAXWINDOWS) || (win < 0)) 2768 return (EINVAL); 2769 return (vt_proc_window_switch(vd->vd_windows[win])); 2770 } 2771 case VT_GETACTIVE: 2772 *(int *)data = vd->vd_curwindow->vw_number + 1; 2773 return (0); 2774 case VT_GETINDEX: 2775 *(int *)data = vw->vw_number + 1; 2776 return (0); 2777 case VT_LOCKSWITCH: 2778 /* TODO: Check current state, switching can be in progress. */ 2779 if ((*(int *)data) == 0x01) 2780 vw->vw_flags |= VWF_VTYLOCK; 2781 else if ((*(int *)data) == 0x02) 2782 vw->vw_flags &= ~VWF_VTYLOCK; 2783 else 2784 return (EINVAL); 2785 return (0); 2786 case VT_OPENQRY: 2787 VT_LOCK(vd); 2788 for (i = 0; i < VT_MAXWINDOWS; i++) { 2789 vw = vd->vd_windows[i]; 2790 if (vw == NULL) 2791 continue; 2792 if (!(vw->vw_flags & VWF_OPENED)) { 2793 *(int *)data = vw->vw_number + 1; 2794 VT_UNLOCK(vd); 2795 return (0); 2796 } 2797 } 2798 VT_UNLOCK(vd); 2799 return (EINVAL); 2800 case VT_WAITACTIVE: { 2801 unsigned int idx; 2802 2803 error = 0; 2804 2805 idx = *(unsigned int *)data; 2806 if (idx > VT_MAXWINDOWS) 2807 return (EINVAL); 2808 if (idx > 0) 2809 vw = vd->vd_windows[idx - 1]; 2810 2811 VT_LOCK(vd); 2812 while (vd->vd_curwindow != vw && error == 0) 2813 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 2814 VT_UNLOCK(vd); 2815 return (error); 2816 } 2817 case VT_SETMODE: { /* set screen switcher mode */ 2818 struct vt_mode *mode; 2819 struct proc *p1; 2820 2821 mode = (struct vt_mode *)data; 2822 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw)); 2823 if (vw->vw_smode.mode == VT_PROCESS) { 2824 p1 = pfind(vw->vw_pid); 2825 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) { 2826 if (p1) 2827 PROC_UNLOCK(p1); 2828 DPRINTF(5, "error EPERM\n"); 2829 return (EPERM); 2830 } 2831 if (p1) 2832 PROC_UNLOCK(p1); 2833 } 2834 if (mode->mode == VT_AUTO) { 2835 vw->vw_smode.mode = VT_AUTO; 2836 vw->vw_proc = NULL; 2837 vw->vw_pid = 0; 2838 DPRINTF(5, "VT_AUTO, "); 2839 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 2840 cnavailable(vw->vw_terminal->consdev, TRUE); 2841 /* were we in the middle of the vty switching process? */ 2842 if (finish_vt_rel(vw, TRUE, &s) == 0) 2843 DPRINTF(5, "reset WAIT_REL, "); 2844 if (finish_vt_acq(vw) == 0) 2845 DPRINTF(5, "reset WAIT_ACQ, "); 2846 return (0); 2847 } else if (mode->mode == VT_PROCESS) { 2848 if (!ISSIGVALID(mode->relsig) || 2849 !ISSIGVALID(mode->acqsig) || 2850 !ISSIGVALID(mode->frsig)) { 2851 DPRINTF(5, "error EINVAL\n"); 2852 return (EINVAL); 2853 } 2854 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid); 2855 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode)); 2856 vw->vw_proc = td->td_proc; 2857 vw->vw_pid = vw->vw_proc->p_pid; 2858 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 2859 cnavailable(vw->vw_terminal->consdev, FALSE); 2860 } else { 2861 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n", 2862 mode->mode); 2863 return (EINVAL); 2864 } 2865 DPRINTF(5, "\n"); 2866 return (0); 2867 } 2868 case VT_GETMODE: /* get screen switcher mode */ 2869 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode)); 2870 return (0); 2871 2872 case VT_RELDISP: /* screen switcher ioctl */ 2873 /* 2874 * This must be the current vty which is in the VT_PROCESS 2875 * switching mode... 2876 */ 2877 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode != 2878 VT_PROCESS)) { 2879 return (EINVAL); 2880 } 2881 /* ...and this process is controlling it. */ 2882 if (vw->vw_proc != td->td_proc) { 2883 return (EPERM); 2884 } 2885 error = EINVAL; 2886 switch(*(int *)data) { 2887 case VT_FALSE: /* user refuses to release screen, abort */ 2888 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0) 2889 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n", 2890 SC_DRIVER_NAME, VT_UNIT(vw)); 2891 break; 2892 case VT_TRUE: /* user has released screen, go on */ 2893 /* finish_vt_rel(..., TRUE, ...) should not be locked */ 2894 if (vw->vw_flags & VWF_SWWAIT_REL) { 2895 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0) 2896 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n", 2897 SC_DRIVER_NAME, VT_UNIT(vw)); 2898 } else { 2899 error = EINVAL; 2900 } 2901 return (error); 2902 case VT_ACKACQ: /* acquire acknowledged, switch completed */ 2903 if ((error = finish_vt_acq(vw)) == 0) 2904 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n", 2905 SC_DRIVER_NAME, VT_UNIT(vw)); 2906 break; 2907 default: 2908 break; 2909 } 2910 return (error); 2911 } 2912 2913 return (ENOIOCTL); 2914 } 2915 2916 static struct vt_window * 2917 vt_allocate_window(struct vt_device *vd, unsigned int window) 2918 { 2919 struct vt_window *vw; 2920 struct terminal *tm; 2921 term_pos_t size; 2922 struct winsize wsz; 2923 2924 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO); 2925 vw->vw_device = vd; 2926 vw->vw_number = window; 2927 vw->vw_kbdmode = K_XLATE; 2928 2929 if ((vd->vd_flags & VDF_TEXTMODE) == 0) { 2930 vw->vw_font = vtfont_ref(vt_font_assigned); 2931 vt_compute_drawable_area(vw); 2932 } 2933 2934 vt_termsize(vd, vw->vw_font, &size); 2935 vt_winsize(vd, vw->vw_font, &wsz); 2936 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw); 2937 vw->vw_buf.vb_terminal = tm; /* must be set before vtbuf_init() */ 2938 vtbuf_init(&vw->vw_buf, &size); 2939 2940 terminal_set_winsize(tm, &wsz); 2941 vd->vd_windows[window] = vw; 2942 callout_init(&vw->vw_proc_dead_timer, 1); 2943 2944 return (vw); 2945 } 2946 2947 void 2948 vt_upgrade(struct vt_device *vd) 2949 { 2950 struct vt_window *vw; 2951 unsigned int i; 2952 int register_handlers; 2953 2954 if (!vty_enabled(VTY_VT)) 2955 return; 2956 if (main_vd->vd_driver == NULL) 2957 return; 2958 2959 for (i = 0; i < VT_MAXWINDOWS; i++) { 2960 vw = vd->vd_windows[i]; 2961 if (vw == NULL) { 2962 /* New window. */ 2963 vw = vt_allocate_window(vd, i); 2964 } 2965 if (!(vw->vw_flags & VWF_READY)) { 2966 callout_init(&vw->vw_proc_dead_timer, 1); 2967 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw)); 2968 vw->vw_flags |= VWF_READY; 2969 if (vw->vw_flags & VWF_CONSOLE) { 2970 /* For existing console window. */ 2971 EVENTHANDLER_REGISTER(shutdown_pre_sync, 2972 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT); 2973 } 2974 } 2975 } 2976 VT_LOCK(vd); 2977 if (vd->vd_curwindow == NULL) 2978 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW]; 2979 2980 register_handlers = 0; 2981 if (!(vd->vd_flags & VDF_ASYNC)) { 2982 /* Attach keyboard. */ 2983 vt_allocate_keyboard(vd); 2984 2985 /* Init 25 Hz timer. */ 2986 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0); 2987 2988 /* 2989 * Start timer when everything ready. 2990 * Note that the operations here are purposefully ordered. 2991 * We need to ensure vd_timer_armed is non-zero before we set 2992 * the VDF_ASYNC flag. That prevents this function from 2993 * racing with vt_resume_flush_timer() to update the 2994 * callout structure. 2995 */ 2996 atomic_add_acq_int(&vd->vd_timer_armed, 1); 2997 vd->vd_flags |= VDF_ASYNC; 2998 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd); 2999 register_handlers = 1; 3000 } 3001 3002 VT_UNLOCK(vd); 3003 3004 /* Refill settings with new sizes. */ 3005 vt_resize(vd); 3006 3007 if (register_handlers) { 3008 /* Register suspend/resume handlers. */ 3009 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler, 3010 vd, EVENTHANDLER_PRI_ANY); 3011 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd, 3012 EVENTHANDLER_PRI_ANY); 3013 } 3014 } 3015 3016 static void 3017 vt_resize(struct vt_device *vd) 3018 { 3019 struct vt_window *vw; 3020 int i; 3021 3022 for (i = 0; i < VT_MAXWINDOWS; i++) { 3023 vw = vd->vd_windows[i]; 3024 VT_LOCK(vd); 3025 /* Assign default font to window, if not textmode. */ 3026 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL) 3027 vw->vw_font = vtfont_ref(vt_font_assigned); 3028 VT_UNLOCK(vd); 3029 3030 /* Resize terminal windows */ 3031 while (vt_change_font(vw, vw->vw_font) == EBUSY) { 3032 DPRINTF(100, "%s: vt_change_font() is busy, " 3033 "window %d\n", __func__, i); 3034 } 3035 } 3036 } 3037 3038 static void 3039 vt_replace_backend(const struct vt_driver *drv, void *softc) 3040 { 3041 struct vt_device *vd; 3042 3043 vd = main_vd; 3044 3045 if (vd->vd_flags & VDF_ASYNC) { 3046 /* Stop vt_flush periodic task. */ 3047 VT_LOCK(vd); 3048 vt_suspend_flush_timer(vd); 3049 VT_UNLOCK(vd); 3050 /* 3051 * Mute current terminal until we done. vt_change_font (called 3052 * from vt_resize) will unmute it. 3053 */ 3054 terminal_mute(vd->vd_curwindow->vw_terminal, 1); 3055 } 3056 3057 /* 3058 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will 3059 * set it. 3060 */ 3061 VT_LOCK(vd); 3062 vd->vd_flags &= ~VDF_TEXTMODE; 3063 3064 if (drv != NULL) { 3065 /* 3066 * We want to upgrade from the current driver to the 3067 * given driver. 3068 */ 3069 3070 vd->vd_prev_driver = vd->vd_driver; 3071 vd->vd_prev_softc = vd->vd_softc; 3072 vd->vd_driver = drv; 3073 vd->vd_softc = softc; 3074 3075 vd->vd_driver->vd_init(vd); 3076 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) { 3077 /* 3078 * No driver given: we want to downgrade to the previous 3079 * driver. 3080 */ 3081 const struct vt_driver *old_drv; 3082 void *old_softc; 3083 3084 old_drv = vd->vd_driver; 3085 old_softc = vd->vd_softc; 3086 3087 vd->vd_driver = vd->vd_prev_driver; 3088 vd->vd_softc = vd->vd_prev_softc; 3089 vd->vd_prev_driver = NULL; 3090 vd->vd_prev_softc = NULL; 3091 3092 vd->vd_flags |= VDF_DOWNGRADE; 3093 3094 vd->vd_driver->vd_init(vd); 3095 3096 if (old_drv->vd_fini) 3097 old_drv->vd_fini(vd, old_softc); 3098 3099 vd->vd_flags &= ~VDF_DOWNGRADE; 3100 } 3101 3102 VT_UNLOCK(vd); 3103 3104 /* Update windows sizes and initialize last items. */ 3105 vt_upgrade(vd); 3106 3107 #ifdef DEV_SPLASH 3108 if (vd->vd_flags & VDF_SPLASH) 3109 vtterm_splash(vd); 3110 #endif 3111 3112 if (vd->vd_flags & VDF_ASYNC) { 3113 /* Allow to put chars now. */ 3114 terminal_mute(vd->vd_curwindow->vw_terminal, 0); 3115 /* Rerun timer for screen updates. */ 3116 vt_resume_flush_timer(vd->vd_curwindow, 0); 3117 } 3118 3119 /* 3120 * Register as console. If it already registered, cnadd() will ignore 3121 * it. 3122 */ 3123 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal); 3124 } 3125 3126 static void 3127 vt_suspend_handler(void *priv) 3128 { 3129 struct vt_device *vd; 3130 3131 vd = priv; 3132 vd->vd_flags |= VDF_SUSPENDED; 3133 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL) 3134 vd->vd_driver->vd_suspend(vd); 3135 } 3136 3137 static void 3138 vt_resume_handler(void *priv) 3139 { 3140 struct vt_device *vd; 3141 3142 vd = priv; 3143 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL) 3144 vd->vd_driver->vd_resume(vd); 3145 vd->vd_flags &= ~VDF_SUSPENDED; 3146 } 3147 3148 void 3149 vt_allocate(const struct vt_driver *drv, void *softc) 3150 { 3151 3152 if (!vty_enabled(VTY_VT)) 3153 return; 3154 3155 if (main_vd->vd_driver == NULL) { 3156 main_vd->vd_driver = drv; 3157 printf("VT: initialize with new VT driver \"%s\".\n", 3158 drv->vd_name); 3159 } else { 3160 /* 3161 * Check if have rights to replace current driver. For example: 3162 * it is bad idea to replace KMS driver with generic VGA one. 3163 */ 3164 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) { 3165 printf("VT: Driver priority %d too low. Current %d\n ", 3166 drv->vd_priority, main_vd->vd_driver->vd_priority); 3167 return; 3168 } 3169 printf("VT: Replacing driver \"%s\" with new \"%s\".\n", 3170 main_vd->vd_driver->vd_name, drv->vd_name); 3171 } 3172 3173 vt_replace_backend(drv, softc); 3174 } 3175 3176 void 3177 vt_deallocate(const struct vt_driver *drv, void *softc) 3178 { 3179 3180 if (!vty_enabled(VTY_VT)) 3181 return; 3182 3183 if (main_vd->vd_prev_driver == NULL || 3184 main_vd->vd_driver != drv || 3185 main_vd->vd_softc != softc) 3186 return; 3187 3188 printf("VT: Switching back from \"%s\" to \"%s\".\n", 3189 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name); 3190 3191 vt_replace_backend(NULL, NULL); 3192 } 3193 3194 void 3195 vt_suspend(struct vt_device *vd) 3196 { 3197 int error; 3198 3199 if (vt_suspendswitch == 0) 3200 return; 3201 /* Save current window. */ 3202 vd->vd_savedwindow = vd->vd_curwindow; 3203 /* Ask holding process to free window and switch to console window */ 3204 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]); 3205 3206 /* Wait for the window switch to complete. */ 3207 error = 0; 3208 VT_LOCK(vd); 3209 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0) 3210 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 3211 VT_UNLOCK(vd); 3212 } 3213 3214 void 3215 vt_resume(struct vt_device *vd) 3216 { 3217 3218 if (vt_suspendswitch == 0) 3219 return; 3220 /* Switch back to saved window, if any */ 3221 vt_proc_window_switch(vd->vd_savedwindow); 3222 vd->vd_savedwindow = NULL; 3223 } 3224