1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009, 2013 The FreeBSD Foundation 5 * 6 * This software was developed by Ed Schouten under sponsorship from the 7 * FreeBSD Foundation. 8 * 9 * Portions of this software were developed by Oleksandr Rybalko 10 * under sponsorship from the FreeBSD Foundation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #include <sys/param.h> 36 #include <sys/consio.h> 37 #include <sys/devctl.h> 38 #include <sys/eventhandler.h> 39 #include <sys/fbio.h> 40 #include <sys/font.h> 41 #include <sys/kbio.h> 42 #include <sys/kdb.h> 43 #include <sys/kernel.h> 44 #include <sys/linker.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/mutex.h> 48 #include <sys/splash.h> 49 #include <sys/power.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/random.h> 53 #include <sys/reboot.h> 54 #include <sys/sbuf.h> 55 #include <sys/systm.h> 56 #include <sys/terminal.h> 57 58 #include <dev/kbd/kbdreg.h> 59 #include <dev/vt/vt.h> 60 61 #if defined(__i386__) || defined(__amd64__) 62 #include <machine/psl.h> 63 #include <machine/frame.h> 64 #endif 65 66 static int vtterm_cngrab_noswitch(struct vt_device *, struct vt_window *); 67 static int vtterm_cnungrab_noswitch(struct vt_device *, struct vt_window *); 68 69 static tc_bell_t vtterm_bell; 70 static tc_cursor_t vtterm_cursor; 71 static tc_putchar_t vtterm_putchar; 72 static tc_fill_t vtterm_fill; 73 static tc_copy_t vtterm_copy; 74 static tc_pre_input_t vtterm_pre_input; 75 static tc_post_input_t vtterm_post_input; 76 static tc_param_t vtterm_param; 77 static tc_done_t vtterm_done; 78 79 static tc_cnprobe_t vtterm_cnprobe; 80 static tc_cngetc_t vtterm_cngetc; 81 82 static tc_cngrab_t vtterm_cngrab; 83 static tc_cnungrab_t vtterm_cnungrab; 84 85 static tc_opened_t vtterm_opened; 86 static tc_ioctl_t vtterm_ioctl; 87 static tc_mmap_t vtterm_mmap; 88 89 static const struct terminal_class vt_termclass = { 90 .tc_bell = vtterm_bell, 91 .tc_cursor = vtterm_cursor, 92 .tc_putchar = vtterm_putchar, 93 .tc_fill = vtterm_fill, 94 .tc_copy = vtterm_copy, 95 .tc_pre_input = vtterm_pre_input, 96 .tc_post_input = vtterm_post_input, 97 .tc_param = vtterm_param, 98 .tc_done = vtterm_done, 99 100 .tc_cnprobe = vtterm_cnprobe, 101 .tc_cngetc = vtterm_cngetc, 102 103 .tc_cngrab = vtterm_cngrab, 104 .tc_cnungrab = vtterm_cnungrab, 105 106 .tc_opened = vtterm_opened, 107 .tc_ioctl = vtterm_ioctl, 108 .tc_mmap = vtterm_mmap, 109 }; 110 111 /* 112 * Use a constant timer of 25 Hz to redraw the screen. 113 * 114 * XXX: In theory we should only fire up the timer when there is really 115 * activity. Unfortunately we cannot always start timers. We really 116 * don't want to process kernel messages synchronously, because it 117 * really slows down the system. 118 */ 119 #define VT_TIMERFREQ 25 120 121 /* Bell pitch/duration. */ 122 #define VT_BELLDURATION (SBT_1S / 20) 123 #define VT_BELLPITCH (1193182 / 800) /* Approx 1491Hz */ 124 125 #define VT_UNIT(vw) ((vw)->vw_device->vd_unit * VT_MAXWINDOWS + \ 126 (vw)->vw_number) 127 128 static SYSCTL_NODE(_kern, OID_AUTO, vt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 129 "vt(9) parameters"); 130 static VT_SYSCTL_INT(enable_altgr, 1, "Enable AltGr key (Do not assume R.Alt as Alt)"); 131 static VT_SYSCTL_INT(enable_bell, 0, "Enable bell"); 132 static VT_SYSCTL_INT(debug, 0, "vt(9) debug level"); 133 static VT_SYSCTL_INT(deadtimer, 15, "Time to wait busy process in VT_PROCESS mode"); 134 static VT_SYSCTL_INT(suspendswitch, 1, "Switch to VT0 before suspend"); 135 136 /* Allow to disable some keyboard combinations. */ 137 static VT_SYSCTL_INT(kbd_halt, 1, "Enable halt keyboard combination. " 138 "See kbdmap(5) to configure."); 139 static VT_SYSCTL_INT(kbd_poweroff, 1, "Enable Power Off keyboard combination. " 140 "See kbdmap(5) to configure."); 141 static VT_SYSCTL_INT(kbd_reboot, 1, "Enable reboot keyboard combination. " 142 "See kbdmap(5) to configure (typically Ctrl-Alt-Delete)."); 143 static VT_SYSCTL_INT(kbd_debug, 1, "Enable key combination to enter debugger. " 144 "See kbdmap(5) to configure (typically Ctrl-Alt-Esc)."); 145 static VT_SYSCTL_INT(kbd_panic, 0, "Enable request to panic. " 146 "See kbdmap(5) to configure."); 147 148 /* Used internally, not a tunable. */ 149 int vt_draw_logo_cpus; 150 VT_SYSCTL_INT(splash_cpu, 0, "Show logo CPUs during boot"); 151 VT_SYSCTL_INT(splash_ncpu, 0, "Override number of logos displayed " 152 "(0 = do not override)"); 153 VT_SYSCTL_INT(splash_cpu_style, 2, "Draw logo style " 154 "(0 = Alternate beastie, 1 = Beastie, 2 = Orb)"); 155 VT_SYSCTL_INT(splash_cpu_duration, 10, "Hide logos after (seconds)"); 156 157 static unsigned int vt_unit = 0; 158 static MALLOC_DEFINE(M_VT, "vt", "vt device"); 159 struct vt_device *main_vd = &vt_consdev; 160 161 /* Boot logo. */ 162 extern unsigned int vt_logo_width; 163 extern unsigned int vt_logo_height; 164 extern unsigned int vt_logo_depth; 165 extern unsigned char vt_logo_image[]; 166 #ifndef DEV_SPLASH 167 #define vtterm_draw_cpu_logos(...) do {} while (0) 168 const unsigned int vt_logo_sprite_height; 169 #endif 170 171 /* 172 * Console font. vt_font_loader will be filled with font data passed 173 * by loader. If there is no font passed by boot loader, we use built in 174 * default. 175 */ 176 extern struct vt_font vt_font_default; 177 static struct vt_font vt_font_loader; 178 static struct vt_font *vt_font_assigned = &vt_font_default; 179 180 #ifndef SC_NO_CUTPASTE 181 extern struct vt_mouse_cursor vt_default_mouse_pointer; 182 #endif 183 184 static int signal_vt_rel(struct vt_window *); 185 static int signal_vt_acq(struct vt_window *); 186 static int finish_vt_rel(struct vt_window *, int, int *); 187 static int finish_vt_acq(struct vt_window *); 188 static int vt_window_switch(struct vt_window *); 189 static int vt_late_window_switch(struct vt_window *); 190 static int vt_proc_alive(struct vt_window *); 191 static void vt_resize(struct vt_device *); 192 static void vt_update_static(void *); 193 #ifndef SC_NO_CUTPASTE 194 static void vt_mouse_paste(void); 195 #endif 196 static void vt_suspend_handler(void *priv); 197 static void vt_resume_handler(void *priv); 198 199 SET_DECLARE(vt_drv_set, struct vt_driver); 200 201 #define _VTDEFH MAX(100, PIXEL_HEIGHT(VT_FB_MAX_HEIGHT)) 202 #define _VTDEFW MAX(200, PIXEL_WIDTH(VT_FB_MAX_WIDTH)) 203 204 static struct terminal vt_consterm; 205 static struct vt_window vt_conswindow; 206 static term_char_t vt_consdrawn[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 207 static term_color_t vt_consdrawnfg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 208 static term_color_t vt_consdrawnbg[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 209 static bool vt_cons_pos_to_flush[PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * PIXEL_WIDTH(VT_FB_MAX_WIDTH)]; 210 struct vt_device vt_consdev = { 211 .vd_driver = NULL, 212 .vd_softc = NULL, 213 .vd_prev_driver = NULL, 214 .vd_prev_softc = NULL, 215 .vd_flags = VDF_INVALID, 216 .vd_windows = { [VT_CONSWINDOW] = &vt_conswindow, }, 217 .vd_curwindow = &vt_conswindow, 218 .vd_kbstate = 0, 219 220 #ifndef SC_NO_CUTPASTE 221 .vd_pastebuf = { 222 .vpb_buf = NULL, 223 .vpb_bufsz = 0, 224 .vpb_len = 0 225 }, 226 .vd_mcursor = &vt_default_mouse_pointer, 227 .vd_mcursor_fg = TC_WHITE, 228 .vd_mcursor_bg = TC_BLACK, 229 #endif 230 231 .vd_drawn = vt_consdrawn, 232 .vd_drawnfg = vt_consdrawnfg, 233 .vd_drawnbg = vt_consdrawnbg, 234 235 .vd_pos_to_flush = vt_cons_pos_to_flush, 236 }; 237 static term_char_t vt_constextbuf[(_VTDEFW) * (VBF_DEFAULT_HISTORY_SIZE)]; 238 static term_char_t *vt_constextbufrows[VBF_DEFAULT_HISTORY_SIZE]; 239 static struct vt_window vt_conswindow = { 240 .vw_number = VT_CONSWINDOW, 241 .vw_flags = VWF_CONSOLE, 242 .vw_buf = { 243 .vb_buffer = &vt_constextbuf[0], 244 .vb_rows = &vt_constextbufrows[0], 245 .vb_history_size = VBF_DEFAULT_HISTORY_SIZE, 246 .vb_curroffset = 0, 247 .vb_roffset = 0, 248 .vb_flags = VBF_STATIC, 249 .vb_mark_start = {.tp_row = 0, .tp_col = 0,}, 250 .vb_mark_end = {.tp_row = 0, .tp_col = 0,}, 251 .vb_scr_size = { 252 .tp_row = _VTDEFH, 253 .tp_col = _VTDEFW, 254 }, 255 }, 256 .vw_device = &vt_consdev, 257 .vw_terminal = &vt_consterm, 258 .vw_kbdmode = K_XLATE, 259 .vw_grabbed = 0, 260 .vw_bell_pitch = VT_BELLPITCH, 261 .vw_bell_duration = VT_BELLDURATION, 262 }; 263 264 /* Add to set of consoles. */ 265 TERMINAL_DECLARE_EARLY(vt_consterm, vt_termclass, &vt_conswindow); 266 267 /* 268 * Right after kmem is done to allow early drivers to use locking and allocate 269 * memory. 270 */ 271 SYSINIT(vt_update_static, SI_SUB_KMEM, SI_ORDER_ANY, vt_update_static, 272 &vt_consdev); 273 /* Delay until all devices attached, to not waste time. */ 274 SYSINIT(vt_early_cons, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, vt_upgrade, 275 &vt_consdev); 276 277 static bool inside_vt_flush = false; 278 static bool inside_vt_window_switch = false; 279 280 /* Initialize locks/mem depended members. */ 281 static void 282 vt_update_static(void *dummy) 283 { 284 285 if (!vty_enabled(VTY_VT)) 286 return; 287 if (main_vd->vd_driver != NULL) 288 printf("VT(%s): %s %ux%u\n", main_vd->vd_driver->vd_name, 289 (main_vd->vd_flags & VDF_TEXTMODE) ? "text" : "resolution", 290 main_vd->vd_width, main_vd->vd_height); 291 else 292 printf("VT: init without driver.\n"); 293 294 mtx_init(&main_vd->vd_lock, "vtdev", NULL, MTX_DEF); 295 mtx_init(&main_vd->vd_flush_lock, "vtdev flush", NULL, MTX_DEF); 296 cv_init(&main_vd->vd_winswitch, "vtwswt"); 297 } 298 299 static void 300 vt_schedule_flush(struct vt_device *vd, int ms) 301 { 302 303 if (ms <= 0) 304 /* Default to initial value. */ 305 ms = 1000 / VT_TIMERFREQ; 306 307 callout_schedule(&vd->vd_timer, hz / (1000 / ms)); 308 } 309 310 void 311 vt_resume_flush_timer(struct vt_window *vw, int ms) 312 { 313 struct vt_device *vd = vw->vw_device; 314 315 if (vd->vd_curwindow != vw) 316 return; 317 318 if (!(vd->vd_flags & VDF_ASYNC) || 319 !atomic_cmpset_int(&vd->vd_timer_armed, 0, 1)) 320 return; 321 322 vt_schedule_flush(vd, ms); 323 } 324 325 static void 326 vt_suspend_flush_timer(struct vt_device *vd) 327 { 328 /* 329 * As long as this function is called locked, callout_stop() 330 * has the same effect like callout_drain() with regard to 331 * preventing the callback function from executing. 332 */ 333 VT_LOCK_ASSERT(vd, MA_OWNED); 334 335 if (!(vd->vd_flags & VDF_ASYNC) || 336 !atomic_cmpset_int(&vd->vd_timer_armed, 1, 0)) 337 return; 338 339 callout_stop(&vd->vd_timer); 340 } 341 342 static void 343 vt_switch_timer(void *arg, int pending) 344 { 345 346 (void)vt_late_window_switch((struct vt_window *)arg); 347 } 348 349 static int 350 vt_save_kbd_mode(struct vt_window *vw, keyboard_t *kbd) 351 { 352 int mode, ret; 353 354 mode = 0; 355 ret = kbdd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode); 356 if (ret == ENOIOCTL) 357 ret = ENODEV; 358 if (ret != 0) 359 return (ret); 360 361 vw->vw_kbdmode = mode; 362 363 return (0); 364 } 365 366 static int 367 vt_update_kbd_mode(struct vt_window *vw, keyboard_t *kbd) 368 { 369 int ret; 370 371 ret = kbdd_ioctl(kbd, KDSKBMODE, (caddr_t)&vw->vw_kbdmode); 372 if (ret == ENOIOCTL) 373 ret = ENODEV; 374 375 return (ret); 376 } 377 378 static int 379 vt_save_kbd_state(struct vt_window *vw, keyboard_t *kbd) 380 { 381 int state, ret; 382 383 state = 0; 384 ret = kbdd_ioctl(kbd, KDGKBSTATE, (caddr_t)&state); 385 if (ret == ENOIOCTL) 386 ret = ENODEV; 387 if (ret != 0) 388 return (ret); 389 390 vw->vw_kbdstate &= ~LOCK_MASK; 391 vw->vw_kbdstate |= state & LOCK_MASK; 392 393 return (0); 394 } 395 396 static int 397 vt_update_kbd_state(struct vt_window *vw, keyboard_t *kbd) 398 { 399 int state, ret; 400 401 state = vw->vw_kbdstate & LOCK_MASK; 402 ret = kbdd_ioctl(kbd, KDSKBSTATE, (caddr_t)&state); 403 if (ret == ENOIOCTL) 404 ret = ENODEV; 405 406 return (ret); 407 } 408 409 static int 410 vt_save_kbd_leds(struct vt_window *vw, keyboard_t *kbd) 411 { 412 int leds, ret; 413 414 leds = 0; 415 ret = kbdd_ioctl(kbd, KDGETLED, (caddr_t)&leds); 416 if (ret == ENOIOCTL) 417 ret = ENODEV; 418 if (ret != 0) 419 return (ret); 420 421 vw->vw_kbdstate &= ~LED_MASK; 422 vw->vw_kbdstate |= leds & LED_MASK; 423 424 return (0); 425 } 426 427 static int 428 vt_update_kbd_leds(struct vt_window *vw, keyboard_t *kbd) 429 { 430 int leds, ret; 431 432 leds = vw->vw_kbdstate & LED_MASK; 433 ret = kbdd_ioctl(kbd, KDSETLED, (caddr_t)&leds); 434 if (ret == ENOIOCTL) 435 ret = ENODEV; 436 437 return (ret); 438 } 439 440 static int 441 vt_window_preswitch(struct vt_window *vw, struct vt_window *curvw) 442 { 443 444 DPRINTF(40, "%s\n", __func__); 445 curvw->vw_switch_to = vw; 446 /* Set timer to allow switch in case when process hang. */ 447 taskqueue_enqueue_timeout(taskqueue_thread, &vw->vw_timeout_task_dead, hz * vt_deadtimer); 448 /* Notify process about vt switch attempt. */ 449 DPRINTF(30, "%s: Notify process.\n", __func__); 450 signal_vt_rel(curvw); 451 452 return (0); 453 } 454 455 static int 456 vt_window_postswitch(struct vt_window *vw) 457 { 458 459 signal_vt_acq(vw); 460 return (0); 461 } 462 463 /* vt_late_window_switch will do VT switching for regular case. */ 464 static int 465 vt_late_window_switch(struct vt_window *vw) 466 { 467 struct vt_window *curvw; 468 int ret; 469 470 taskqueue_cancel_timeout(taskqueue_thread, &vw->vw_timeout_task_dead, NULL); 471 472 ret = vt_window_switch(vw); 473 if (ret != 0) { 474 /* 475 * If the switch hasn't happened, then return the VT 476 * to the current owner, if any. 477 */ 478 curvw = vw->vw_device->vd_curwindow; 479 if (curvw->vw_smode.mode == VT_PROCESS) 480 (void)vt_window_postswitch(curvw); 481 return (ret); 482 } 483 484 /* Notify owner process about terminal availability. */ 485 if (vw->vw_smode.mode == VT_PROCESS) { 486 ret = vt_window_postswitch(vw); 487 } 488 return (ret); 489 } 490 491 /* Switch window. */ 492 static int 493 vt_proc_window_switch(struct vt_window *vw) 494 { 495 struct vt_window *curvw; 496 struct vt_device *vd; 497 int ret; 498 499 /* Prevent switching to NULL */ 500 if (vw == NULL) { 501 DPRINTF(30, "%s: Cannot switch: vw is NULL.", __func__); 502 return (EINVAL); 503 } 504 vd = vw->vw_device; 505 curvw = vd->vd_curwindow; 506 507 /* Check if virtual terminal is locked */ 508 if (curvw->vw_flags & VWF_VTYLOCK) 509 return (EBUSY); 510 511 /* Check if switch already in progress */ 512 if (curvw->vw_flags & VWF_SWWAIT_REL) { 513 /* Check if switching to same window */ 514 if (curvw->vw_switch_to == vw) { 515 DPRINTF(30, "%s: Switch in progress to same vw.", __func__); 516 return (0); /* success */ 517 } 518 DPRINTF(30, "%s: Switch in progress to different vw.", __func__); 519 return (EBUSY); 520 } 521 522 /* Avoid switching to already selected window */ 523 if (vw == curvw) { 524 DPRINTF(30, "%s: Cannot switch: vw == curvw.", __func__); 525 return (0); /* success */ 526 } 527 528 /* 529 * Early check for an attempt to switch to a non-functional VT. 530 * The same check is done in vt_window_switch(), but it's better 531 * to fail as early as possible to avoid needless pre-switch 532 * actions. 533 */ 534 VT_LOCK(vd); 535 if ((vw->vw_flags & (VWF_OPENED|VWF_CONSOLE)) == 0) { 536 VT_UNLOCK(vd); 537 return (EINVAL); 538 } 539 VT_UNLOCK(vd); 540 541 /* Ask current process permission to switch away. */ 542 if (curvw->vw_smode.mode == VT_PROCESS) { 543 DPRINTF(30, "%s: VT_PROCESS ", __func__); 544 if (vt_proc_alive(curvw) == FALSE) { 545 DPRINTF(30, "Dead. Cleaning."); 546 /* Dead */ 547 } else { 548 DPRINTF(30, "%s: Signaling process.\n", __func__); 549 /* Alive, try to ask him. */ 550 ret = vt_window_preswitch(vw, curvw); 551 /* Wait for process answer or timeout. */ 552 return (ret); 553 } 554 DPRINTF(30, "\n"); 555 } 556 557 ret = vt_late_window_switch(vw); 558 return (ret); 559 } 560 561 /* Switch window ignoring process locking. */ 562 static int 563 vt_window_switch(struct vt_window *vw) 564 { 565 struct vt_device *vd = vw->vw_device; 566 struct vt_window *curvw = vd->vd_curwindow; 567 keyboard_t *kbd; 568 569 if (inside_vt_window_switch && KERNEL_PANICKED()) 570 return (0); 571 572 inside_vt_window_switch = true; 573 574 if (kdb_active) { 575 /* 576 * When grabbing the console for the debugger, avoid 577 * locks as that can result in deadlock. While this 578 * could use try locks, that wouldn't really make a 579 * difference as there are sufficient barriers in 580 * debugger entry/exit to be equivalent to 581 * successfully try-locking here. 582 */ 583 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) { 584 inside_vt_window_switch = false; 585 return (EINVAL); 586 } 587 588 vd->vd_curwindow = vw; 589 vd->vd_flags |= VDF_INVALID; 590 if (vd->vd_driver->vd_postswitch) 591 vd->vd_driver->vd_postswitch(vd); 592 inside_vt_window_switch = false; 593 return (0); 594 } 595 596 VT_LOCK(vd); 597 if (curvw == vw) { 598 /* 599 * Nothing to do, except ensure the driver has the opportunity to 600 * switch to console mode when panicking, making sure the panic 601 * is readable (even when a GUI was using ttyv0). 602 */ 603 if ((kdb_active || KERNEL_PANICKED()) && 604 vd->vd_driver->vd_postswitch) 605 vd->vd_driver->vd_postswitch(vd); 606 inside_vt_window_switch = false; 607 VT_UNLOCK(vd); 608 return (0); 609 } 610 if (!(vw->vw_flags & (VWF_OPENED|VWF_CONSOLE))) { 611 inside_vt_window_switch = false; 612 VT_UNLOCK(vd); 613 return (EINVAL); 614 } 615 616 vt_suspend_flush_timer(vd); 617 618 vd->vd_curwindow = vw; 619 vd->vd_flags |= VDF_INVALID; 620 cv_broadcast(&vd->vd_winswitch); 621 VT_UNLOCK(vd); 622 623 if (vd->vd_driver->vd_postswitch) 624 vd->vd_driver->vd_postswitch(vd); 625 626 vt_resume_flush_timer(vw, 0); 627 628 /* Restore per-window keyboard mode. */ 629 mtx_lock(&Giant); 630 if ((kbd = vd->vd_keyboard) != NULL) { 631 if (curvw->vw_kbdmode == K_XLATE) 632 vt_save_kbd_state(curvw, kbd); 633 634 vt_update_kbd_mode(vw, kbd); 635 vt_update_kbd_state(vw, kbd); 636 } 637 mtx_unlock(&Giant); 638 DPRINTF(10, "%s(ttyv%d) done\n", __func__, vw->vw_number); 639 640 inside_vt_window_switch = false; 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 void 1351 vt_flush_to_buffer(struct vt_device *vd, 1352 const struct vt_window *vw, const term_rect_t *area) 1353 { 1354 unsigned int col, row; 1355 term_char_t c; 1356 term_color_t fg, bg; 1357 size_t z; 1358 1359 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) { 1360 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col; 1361 ++col) { 1362 z = row * PIXEL_WIDTH(VT_FB_MAX_WIDTH) + col; 1363 if (z >= PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * 1364 PIXEL_WIDTH(VT_FB_MAX_WIDTH)) 1365 continue; 1366 1367 c = VTBUF_GET_FIELD(&vw->vw_buf, row, col); 1368 vt_determine_colors(c, 1369 VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg); 1370 1371 if (vd->vd_drawn && (vd->vd_drawn[z] == c) && 1372 vd->vd_drawnfg && (vd->vd_drawnfg[z] == fg) && 1373 vd->vd_drawnbg && (vd->vd_drawnbg[z] == bg)) { 1374 vd->vd_pos_to_flush[z] = false; 1375 continue; 1376 } 1377 1378 vd->vd_pos_to_flush[z] = true; 1379 1380 if (vd->vd_drawn) 1381 vd->vd_drawn[z] = c; 1382 if (vd->vd_drawnfg) 1383 vd->vd_drawnfg[z] = fg; 1384 if (vd->vd_drawnbg) 1385 vd->vd_drawnbg[z] = bg; 1386 } 1387 } 1388 } 1389 1390 static void 1391 vt_bitblt_buffer(struct vt_device *vd, const struct vt_window *vw, 1392 const term_rect_t *area) 1393 { 1394 unsigned int col, row, x, y; 1395 struct vt_font *vf; 1396 term_char_t c; 1397 term_color_t fg, bg; 1398 const uint8_t *pattern; 1399 size_t z; 1400 1401 vf = vw->vw_font; 1402 1403 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) { 1404 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col; 1405 ++col) { 1406 x = col * vf->vf_width + 1407 vw->vw_draw_area.tr_begin.tp_col; 1408 y = row * vf->vf_height + 1409 vw->vw_draw_area.tr_begin.tp_row; 1410 1411 z = row * PIXEL_WIDTH(VT_FB_MAX_WIDTH) + col; 1412 if (z >= PIXEL_HEIGHT(VT_FB_MAX_HEIGHT) * 1413 PIXEL_WIDTH(VT_FB_MAX_WIDTH)) 1414 continue; 1415 if (!vd->vd_pos_to_flush[z]) 1416 continue; 1417 1418 c = vd->vd_drawn[z]; 1419 fg = vd->vd_drawnfg[z]; 1420 bg = vd->vd_drawnbg[z]; 1421 1422 pattern = vtfont_lookup(vf, c); 1423 vd->vd_driver->vd_bitblt_bmp(vd, vw, 1424 pattern, NULL, vf->vf_width, vf->vf_height, 1425 x, y, fg, bg); 1426 } 1427 } 1428 1429 #ifndef SC_NO_CUTPASTE 1430 if (!vd->vd_mshown) 1431 return; 1432 1433 term_rect_t drawn_area; 1434 1435 drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width; 1436 drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height; 1437 drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width; 1438 drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height; 1439 1440 if (vt_is_cursor_in_area(vd, &drawn_area)) { 1441 vd->vd_driver->vd_bitblt_bmp(vd, vw, 1442 vd->vd_mcursor->map, vd->vd_mcursor->mask, 1443 vd->vd_mcursor->width, vd->vd_mcursor->height, 1444 vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col, 1445 vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row, 1446 vd->vd_mcursor_fg, vd->vd_mcursor_bg); 1447 } 1448 #endif 1449 } 1450 1451 static void 1452 vt_draw_decorations(struct vt_device *vd) 1453 { 1454 struct vt_window *vw; 1455 const teken_attr_t *a; 1456 1457 vw = vd->vd_curwindow; 1458 1459 a = teken_get_curattr(&vw->vw_terminal->tm_emulator); 1460 vt_set_border(vd, &vw->vw_draw_area, a->ta_bgcolor); 1461 1462 if (vt_draw_logo_cpus) 1463 vtterm_draw_cpu_logos(vd); 1464 } 1465 1466 static int 1467 vt_flush(struct vt_device *vd) 1468 { 1469 struct vt_window *vw; 1470 struct vt_font *vf; 1471 term_rect_t tarea; 1472 #ifndef SC_NO_CUTPASTE 1473 int cursor_was_shown, cursor_moved; 1474 #endif 1475 bool needs_refresh; 1476 1477 if (inside_vt_flush && KERNEL_PANICKED()) 1478 return (0); 1479 1480 vw = vd->vd_curwindow; 1481 if (vw == NULL) 1482 return (0); 1483 1484 if (vd->vd_flags & VDF_SPLASH || vw->vw_flags & VWF_BUSY) 1485 return (0); 1486 1487 vf = vw->vw_font; 1488 if (((vd->vd_flags & VDF_TEXTMODE) == 0) && (vf == NULL)) 1489 return (0); 1490 1491 VT_FLUSH_LOCK(vd); 1492 1493 vtbuf_lock(&vw->vw_buf); 1494 inside_vt_flush = true; 1495 1496 #ifndef SC_NO_CUTPASTE 1497 cursor_was_shown = vd->vd_mshown; 1498 cursor_moved = (vd->vd_mx != vd->vd_mx_drawn || 1499 vd->vd_my != vd->vd_my_drawn); 1500 1501 /* Check if the cursor should be displayed or not. */ 1502 if ((vd->vd_flags & VDF_MOUSECURSOR) && /* Mouse support enabled. */ 1503 !(vw->vw_flags & VWF_MOUSE_HIDE) && /* Cursor displayed. */ 1504 !kdb_active && !KERNEL_PANICKED()) { /* DDB inactive. */ 1505 vd->vd_mshown = 1; 1506 } else { 1507 vd->vd_mshown = 0; 1508 } 1509 1510 /* 1511 * If the cursor changed display state or moved, we must mark 1512 * the old position as dirty, so that it's erased. 1513 */ 1514 if (cursor_was_shown != vd->vd_mshown || 1515 (vd->vd_mshown && cursor_moved)) 1516 vt_mark_mouse_position_as_dirty(vd, true); 1517 1518 /* 1519 * Save position of the mouse cursor. It's used by backends to 1520 * know where to draw the cursor and during the next refresh to 1521 * erase the previous position. 1522 */ 1523 vd->vd_mx_drawn = vd->vd_mx; 1524 vd->vd_my_drawn = vd->vd_my; 1525 1526 /* 1527 * If the cursor is displayed and has moved since last refresh, 1528 * mark the new position as dirty. 1529 */ 1530 if (vd->vd_mshown && cursor_moved) 1531 vt_mark_mouse_position_as_dirty(vd, true); 1532 #endif 1533 1534 vtbuf_undirty(&vw->vw_buf, &tarea); 1535 1536 /* Force a full redraw when the screen contents might be invalid. */ 1537 needs_refresh = false; 1538 if (vd->vd_flags & (VDF_INVALID | VDF_SUSPENDED)) { 1539 needs_refresh = true; 1540 vd->vd_flags &= ~VDF_INVALID; 1541 1542 vt_termrect(vd, vf, &tarea); 1543 if (vd->vd_driver->vd_invalidate_text) 1544 vd->vd_driver->vd_invalidate_text(vd, &tarea); 1545 } 1546 1547 if (tarea.tr_begin.tp_col < tarea.tr_end.tp_col) { 1548 if (vd->vd_driver->vd_bitblt_after_vtbuf_unlock) { 1549 /* 1550 * When `vd_bitblt_after_vtbuf_unlock` is set to true, 1551 * we first remember the characters to redraw. They are 1552 * already copied to the `vd_drawn` arrays. 1553 * 1554 * We then unlock vt_buf and proceed with the actual 1555 * drawing using the backend driver. 1556 */ 1557 vt_flush_to_buffer(vd, vw, &tarea); 1558 vtbuf_unlock(&vw->vw_buf); 1559 vt_bitblt_buffer(vd, vw, &tarea); 1560 1561 if (needs_refresh) 1562 vt_draw_decorations(vd); 1563 1564 /* 1565 * We can reset `inside_vt_flush` after unlocking vtbuf 1566 * here because we also hold vt_flush_lock in this code 1567 * path. 1568 */ 1569 inside_vt_flush = false; 1570 } else { 1571 /* 1572 * When `vd_bitblt_after_vtbuf_unlock` is false, we use 1573 * the backend's `vd_bitblt_text` callback directly. 1574 */ 1575 vd->vd_driver->vd_bitblt_text(vd, vw, &tarea); 1576 1577 if (needs_refresh) 1578 vt_draw_decorations(vd); 1579 1580 inside_vt_flush = false; 1581 vtbuf_unlock(&vw->vw_buf); 1582 } 1583 1584 VT_FLUSH_UNLOCK(vd); 1585 1586 return (1); 1587 } 1588 1589 inside_vt_flush = false; 1590 vtbuf_unlock(&vw->vw_buf); 1591 1592 VT_FLUSH_UNLOCK(vd); 1593 1594 return (0); 1595 } 1596 1597 static void 1598 vt_timer(void *arg) 1599 { 1600 struct vt_device *vd; 1601 int changed; 1602 1603 vd = arg; 1604 /* Update screen if required. */ 1605 changed = vt_flush(vd); 1606 1607 /* Schedule for next update. */ 1608 if (changed) 1609 vt_schedule_flush(vd, 0); 1610 else 1611 vd->vd_timer_armed = 0; 1612 } 1613 1614 static void 1615 vtterm_pre_input(struct terminal *tm) 1616 { 1617 struct vt_window *vw = tm->tm_softc; 1618 1619 if (inside_vt_flush && KERNEL_PANICKED()) 1620 return; 1621 1622 vtbuf_lock(&vw->vw_buf); 1623 } 1624 1625 static void 1626 vtterm_post_input(struct terminal *tm) 1627 { 1628 struct vt_window *vw = tm->tm_softc; 1629 1630 if (inside_vt_flush && KERNEL_PANICKED()) 1631 return; 1632 1633 vtbuf_unlock(&vw->vw_buf); 1634 vt_resume_flush_timer(vw, 0); 1635 } 1636 1637 static void 1638 vtterm_done(struct terminal *tm) 1639 { 1640 struct vt_window *vw = tm->tm_softc; 1641 struct vt_device *vd = vw->vw_device; 1642 1643 if (kdb_active || KERNEL_PANICKED()) { 1644 /* Switch to the debugger. */ 1645 if (vd->vd_curwindow != vw) { 1646 vd->vd_curwindow = vw; 1647 vd->vd_flags |= VDF_INVALID; 1648 if (vd->vd_driver->vd_postswitch) 1649 vd->vd_driver->vd_postswitch(vd); 1650 } 1651 vd->vd_flags &= ~VDF_SPLASH; 1652 vt_flush(vd); 1653 } else if (!(vd->vd_flags & VDF_ASYNC)) { 1654 vt_flush(vd); 1655 } 1656 } 1657 1658 #ifdef DEV_SPLASH 1659 static void 1660 vtterm_splash(struct vt_device *vd) 1661 { 1662 caddr_t kmdp; 1663 struct splash_info *si; 1664 uintptr_t image; 1665 vt_axis_t top, left; 1666 1667 kmdp = preload_search_by_type("elf kernel"); 1668 if (kmdp == NULL) 1669 kmdp = preload_search_by_type("elf64 kernel"); 1670 si = MD_FETCH(kmdp, MODINFOMD_SPLASH, struct splash_info *); 1671 if (!(vd->vd_flags & VDF_TEXTMODE) && (boothowto & RB_MUTE)) { 1672 if (si == NULL) { 1673 top = (vd->vd_height - vt_logo_height) / 2; 1674 left = (vd->vd_width - vt_logo_width) / 2; 1675 vd->vd_driver->vd_bitblt_bmp(vd, vd->vd_curwindow, 1676 vt_logo_image, NULL, vt_logo_width, vt_logo_height, 1677 left, top, TC_WHITE, TC_BLACK); 1678 } else { 1679 if (si->si_depth != 4) 1680 return; 1681 printf("SPLASH: width: %d height: %d depth: %d\n", si->si_width, si->si_height, si->si_depth); 1682 image = (uintptr_t)si + sizeof(struct splash_info); 1683 image = roundup2(image, 8); 1684 top = (vd->vd_height - si->si_height) / 2; 1685 left = (vd->vd_width - si->si_width) / 2; 1686 vd->vd_driver->vd_bitblt_argb(vd, vd->vd_curwindow, 1687 (unsigned char *)image, si->si_width, si->si_height, 1688 left, top); 1689 } 1690 vd->vd_flags |= VDF_SPLASH; 1691 } 1692 } 1693 #endif 1694 1695 static struct vt_font * 1696 parse_font_info_static(struct font_info *fi) 1697 { 1698 struct vt_font *vfp; 1699 uintptr_t ptr; 1700 uint32_t checksum; 1701 1702 if (fi == NULL) 1703 return (NULL); 1704 1705 ptr = (uintptr_t)fi; 1706 /* 1707 * Compute and verify checksum. The total sum of all the fields 1708 * must be 0. 1709 */ 1710 checksum = fi->fi_width; 1711 checksum += fi->fi_height; 1712 checksum += fi->fi_bitmap_size; 1713 for (unsigned i = 0; i < VFNT_MAPS; i++) 1714 checksum += fi->fi_map_count[i]; 1715 1716 if (checksum + fi->fi_checksum != 0) 1717 return (NULL); 1718 1719 ptr += sizeof(struct font_info); 1720 ptr = roundup2(ptr, 8); 1721 1722 vfp = &vt_font_loader; 1723 vfp->vf_height = fi->fi_height; 1724 vfp->vf_width = fi->fi_width; 1725 /* This is default font, set refcount 1 to disable removal. */ 1726 vfp->vf_refcount = 1; 1727 for (unsigned i = 0; i < VFNT_MAPS; i++) { 1728 if (fi->fi_map_count[i] == 0) 1729 continue; 1730 vfp->vf_map_count[i] = fi->fi_map_count[i]; 1731 vfp->vf_map[i] = (vfnt_map_t *)ptr; 1732 ptr += (fi->fi_map_count[i] * sizeof(vfnt_map_t)); 1733 ptr = roundup2(ptr, 8); 1734 } 1735 vfp->vf_bytes = (uint8_t *)ptr; 1736 return (vfp); 1737 } 1738 1739 /* 1740 * Set up default font with allocated data structures. 1741 * However, we can not set refcount here, because it is already set and 1742 * incremented in vtterm_cnprobe() to avoid being released by font load from 1743 * userland. 1744 */ 1745 static struct vt_font * 1746 parse_font_info(struct font_info *fi) 1747 { 1748 struct vt_font *vfp; 1749 uintptr_t ptr; 1750 uint32_t checksum; 1751 size_t size; 1752 1753 if (fi == NULL) 1754 return (NULL); 1755 1756 ptr = (uintptr_t)fi; 1757 /* 1758 * Compute and verify checksum. The total sum of all the fields 1759 * must be 0. 1760 */ 1761 checksum = fi->fi_width; 1762 checksum += fi->fi_height; 1763 checksum += fi->fi_bitmap_size; 1764 for (unsigned i = 0; i < VFNT_MAPS; i++) 1765 checksum += fi->fi_map_count[i]; 1766 1767 if (checksum + fi->fi_checksum != 0) 1768 return (NULL); 1769 1770 ptr += sizeof(struct font_info); 1771 ptr = roundup2(ptr, 8); 1772 1773 vfp = &vt_font_loader; 1774 vfp->vf_height = fi->fi_height; 1775 vfp->vf_width = fi->fi_width; 1776 for (unsigned i = 0; i < VFNT_MAPS; i++) { 1777 if (fi->fi_map_count[i] == 0) 1778 continue; 1779 vfp->vf_map_count[i] = fi->fi_map_count[i]; 1780 size = fi->fi_map_count[i] * sizeof(vfnt_map_t); 1781 vfp->vf_map[i] = malloc(size, M_VT, M_WAITOK | M_ZERO); 1782 bcopy((vfnt_map_t *)ptr, vfp->vf_map[i], size); 1783 ptr += size; 1784 ptr = roundup2(ptr, 8); 1785 } 1786 vfp->vf_bytes = malloc(fi->fi_bitmap_size, M_VT, M_WAITOK | M_ZERO); 1787 bcopy((uint8_t *)ptr, vfp->vf_bytes, fi->fi_bitmap_size); 1788 return (vfp); 1789 } 1790 1791 static void 1792 vt_init_font(void *arg) 1793 { 1794 caddr_t kmdp; 1795 struct font_info *fi; 1796 struct vt_font *font; 1797 1798 kmdp = preload_search_by_type("elf kernel"); 1799 if (kmdp == NULL) 1800 kmdp = preload_search_by_type("elf64 kernel"); 1801 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *); 1802 1803 font = parse_font_info(fi); 1804 if (font != NULL) 1805 vt_font_assigned = font; 1806 } 1807 1808 SYSINIT(vt_init_font, SI_SUB_KMEM, SI_ORDER_ANY, vt_init_font, &vt_consdev); 1809 1810 static void 1811 vt_init_font_static(void) 1812 { 1813 caddr_t kmdp; 1814 struct font_info *fi; 1815 struct vt_font *font; 1816 1817 kmdp = preload_search_by_type("elf kernel"); 1818 if (kmdp == NULL) 1819 kmdp = preload_search_by_type("elf64 kernel"); 1820 fi = MD_FETCH(kmdp, MODINFOMD_FONT, struct font_info *); 1821 1822 font = parse_font_info_static(fi); 1823 if (font != NULL) 1824 vt_font_assigned = font; 1825 } 1826 1827 static void 1828 vtterm_cnprobe(struct terminal *tm, struct consdev *cp) 1829 { 1830 struct vt_driver *vtd, **vtdlist, *vtdbest = NULL; 1831 struct vt_window *vw = tm->tm_softc; 1832 struct vt_device *vd = vw->vw_device; 1833 struct winsize wsz; 1834 const term_attr_t *a; 1835 1836 if (!vty_enabled(VTY_VT)) 1837 return; 1838 1839 if (vd->vd_flags & VDF_INITIALIZED) 1840 /* Initialization already done. */ 1841 return; 1842 1843 SET_FOREACH(vtdlist, vt_drv_set) { 1844 vtd = *vtdlist; 1845 if (vtd->vd_probe == NULL) 1846 continue; 1847 if (vtd->vd_probe(vd) == CN_DEAD) 1848 continue; 1849 if ((vtdbest == NULL) || 1850 (vtd->vd_priority > vtdbest->vd_priority)) 1851 vtdbest = vtd; 1852 } 1853 if (vtdbest == NULL) { 1854 cp->cn_pri = CN_DEAD; 1855 vd->vd_flags |= VDF_DEAD; 1856 } else { 1857 vd->vd_driver = vtdbest; 1858 cp->cn_pri = vd->vd_driver->vd_init(vd); 1859 } 1860 1861 /* Check if driver's vt_init return CN_DEAD. */ 1862 if (cp->cn_pri == CN_DEAD) { 1863 vd->vd_flags |= VDF_DEAD; 1864 } 1865 1866 /* Initialize any early-boot keyboard drivers */ 1867 kbd_configure(KB_CONF_PROBE_ONLY); 1868 1869 vd->vd_unit = atomic_fetchadd_int(&vt_unit, 1); 1870 vd->vd_windows[VT_CONSWINDOW] = vw; 1871 sprintf(cp->cn_name, "ttyv%r", VT_UNIT(vw)); 1872 1873 vt_init_font_static(); 1874 1875 /* Attach default font if not in TEXTMODE. */ 1876 if ((vd->vd_flags & VDF_TEXTMODE) == 0) { 1877 vw->vw_font = vtfont_ref(vt_font_assigned); 1878 vt_compute_drawable_area(vw); 1879 } 1880 1881 /* 1882 * The original screen size was faked (_VTDEFW x _VTDEFH). Now 1883 * that we have the real viewable size, fix it in the static 1884 * buffer. 1885 */ 1886 if (vd->vd_width != 0 && vd->vd_height != 0) 1887 vt_termsize(vd, vw->vw_font, &vw->vw_buf.vb_scr_size); 1888 1889 /* We need to access terminal attributes from vtbuf */ 1890 vw->vw_buf.vb_terminal = tm; 1891 vtbuf_init_early(&vw->vw_buf); 1892 vt_winsize(vd, vw->vw_font, &wsz); 1893 a = teken_get_curattr(&tm->tm_emulator); 1894 terminal_set_winsize_blank(tm, &wsz, 1, a); 1895 1896 if (vtdbest != NULL) { 1897 #ifdef DEV_SPLASH 1898 if (!vt_splash_cpu) 1899 vtterm_splash(vd); 1900 #endif 1901 vd->vd_flags |= VDF_INITIALIZED; 1902 } 1903 } 1904 1905 static int 1906 vtterm_cngetc(struct terminal *tm) 1907 { 1908 struct vt_window *vw = tm->tm_softc; 1909 struct vt_device *vd = vw->vw_device; 1910 keyboard_t *kbd; 1911 u_int c; 1912 1913 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1914 return (*vw->vw_kbdsq++); 1915 1916 /* Make sure the splash screen is not there. */ 1917 if (vd->vd_flags & VDF_SPLASH) { 1918 /* Remove splash */ 1919 vd->vd_flags &= ~VDF_SPLASH; 1920 /* Mark screen as invalid to force update */ 1921 vd->vd_flags |= VDF_INVALID; 1922 vt_flush(vd); 1923 } 1924 1925 /* Stripped down keyboard handler. */ 1926 if ((kbd = vd->vd_keyboard) == NULL) 1927 return (-1); 1928 1929 /* Force keyboard input mode to K_XLATE */ 1930 vw->vw_kbdmode = K_XLATE; 1931 vt_update_kbd_mode(vw, kbd); 1932 1933 /* Switch the keyboard to polling to make it work here. */ 1934 kbdd_poll(kbd, TRUE); 1935 c = kbdd_read_char(kbd, 0); 1936 kbdd_poll(kbd, FALSE); 1937 if (c & RELKEY) 1938 return (-1); 1939 1940 if (vw->vw_flags & VWF_SCROLL) { 1941 vt_scrollmode_kbdevent(vw, c, 1/* Console mode */); 1942 vt_flush(vd); 1943 return (-1); 1944 } 1945 1946 /* Stripped down handling of vt_kbdevent(), without locking, etc. */ 1947 if (c & SPCLKEY) { 1948 switch (c) { 1949 case SPCLKEY | SLK: 1950 vt_save_kbd_state(vw, kbd); 1951 if (vw->vw_kbdstate & SLKED) { 1952 /* Turn scrolling on. */ 1953 vw->vw_flags |= VWF_SCROLL; 1954 VTBUF_SLCK_ENABLE(&vw->vw_buf); 1955 } else { 1956 /* Turn scrolling off. */ 1957 vt_scroll(vw, 0, VHS_END); 1958 vw->vw_flags &= ~VWF_SCROLL; 1959 VTBUF_SLCK_DISABLE(&vw->vw_buf); 1960 } 1961 break; 1962 /* XXX: KDB can handle history. */ 1963 case SPCLKEY | FKEY | F(50): /* Arrow up. */ 1964 vw->vw_kbdsq = "\x1b[A"; 1965 break; 1966 case SPCLKEY | FKEY | F(58): /* Arrow down. */ 1967 vw->vw_kbdsq = "\x1b[B"; 1968 break; 1969 case SPCLKEY | FKEY | F(55): /* Arrow right. */ 1970 vw->vw_kbdsq = "\x1b[C"; 1971 break; 1972 case SPCLKEY | FKEY | F(53): /* Arrow left. */ 1973 vw->vw_kbdsq = "\x1b[D"; 1974 break; 1975 } 1976 1977 /* Force refresh to make scrollback work. */ 1978 vt_flush(vd); 1979 } else if (KEYFLAGS(c) == 0) { 1980 return (KEYCHAR(c)); 1981 } 1982 1983 if (vw->vw_kbdsq && *vw->vw_kbdsq) 1984 return (*vw->vw_kbdsq++); 1985 1986 return (-1); 1987 } 1988 1989 /* 1990 * These two do most of what we want to do in vtterm_cnungrab, but without 1991 * actually switching windows. This is necessary for, e.g., 1992 * vt_allocate_keyboard() to get the current keyboard into the state it needs to 1993 * be in without damaging the device's window state. 1994 * 1995 * Both return the current grab count, though it's only used in vtterm_cnungrab. 1996 */ 1997 static int 1998 vtterm_cngrab_noswitch(struct vt_device *vd, struct vt_window *vw) 1999 { 2000 keyboard_t *kbd; 2001 2002 if (vw->vw_grabbed++ > 0) 2003 return (vw->vw_grabbed); 2004 2005 if ((kbd = vd->vd_keyboard) == NULL) 2006 return (1); 2007 2008 /* 2009 * Make sure the keyboard is accessible even when the kbd device 2010 * driver is disabled. 2011 */ 2012 kbdd_enable(kbd); 2013 2014 /* We shall always use the keyboard in the XLATE mode here. */ 2015 vw->vw_prev_kbdmode = vw->vw_kbdmode; 2016 vw->vw_kbdmode = K_XLATE; 2017 vt_update_kbd_mode(vw, kbd); 2018 2019 kbdd_poll(kbd, TRUE); 2020 return (1); 2021 } 2022 2023 static int 2024 vtterm_cnungrab_noswitch(struct vt_device *vd, struct vt_window *vw) 2025 { 2026 keyboard_t *kbd; 2027 2028 if (--vw->vw_grabbed > 0) 2029 return (vw->vw_grabbed); 2030 2031 if ((kbd = vd->vd_keyboard) == NULL) 2032 return (0); 2033 2034 kbdd_poll(kbd, FALSE); 2035 2036 vw->vw_kbdmode = vw->vw_prev_kbdmode; 2037 vt_update_kbd_mode(vw, kbd); 2038 kbdd_disable(kbd); 2039 return (0); 2040 } 2041 2042 static void 2043 vtterm_cngrab(struct terminal *tm) 2044 { 2045 struct vt_device *vd; 2046 struct vt_window *vw; 2047 2048 vw = tm->tm_softc; 2049 vd = vw->vw_device; 2050 2051 /* To be restored after we ungrab. */ 2052 if (vd->vd_grabwindow == NULL) 2053 vd->vd_grabwindow = vd->vd_curwindow; 2054 2055 if (!cold) 2056 vt_window_switch(vw); 2057 2058 vtterm_cngrab_noswitch(vd, vw); 2059 } 2060 2061 static void 2062 vtterm_cnungrab(struct terminal *tm) 2063 { 2064 struct vt_device *vd; 2065 struct vt_window *vw, *grabwindow; 2066 2067 vw = tm->tm_softc; 2068 vd = vw->vw_device; 2069 2070 MPASS(vd->vd_grabwindow != NULL); 2071 if (vtterm_cnungrab_noswitch(vd, vw) != 0) 2072 return; 2073 2074 /* 2075 * We set `vd_grabwindow` to NULL before calling vt_window_switch() 2076 * because it allows the underlying vt(4) backend to distinguish a 2077 * "grab" from an "ungrab" of the console. 2078 * 2079 * This is used by `vt_drmfb` in drm-kmod to call either 2080 * fb_debug_enter() or fb_debug_leave() appropriately. 2081 */ 2082 grabwindow = vd->vd_grabwindow; 2083 vd->vd_grabwindow = NULL; 2084 2085 if (!cold) 2086 vt_window_switch(grabwindow); 2087 } 2088 2089 static void 2090 vtterm_opened(struct terminal *tm, int opened) 2091 { 2092 struct vt_window *vw = tm->tm_softc; 2093 struct vt_device *vd = vw->vw_device; 2094 2095 VT_LOCK(vd); 2096 vd->vd_flags &= ~VDF_SPLASH; 2097 if (opened) 2098 vw->vw_flags |= VWF_OPENED; 2099 else { 2100 vw->vw_flags &= ~VWF_OPENED; 2101 /* TODO: finish ACQ/REL */ 2102 } 2103 VT_UNLOCK(vd); 2104 } 2105 2106 static int 2107 vt_change_font(struct vt_window *vw, struct vt_font *vf) 2108 { 2109 struct vt_device *vd = vw->vw_device; 2110 struct terminal *tm = vw->vw_terminal; 2111 term_pos_t size; 2112 struct winsize wsz; 2113 2114 /* 2115 * Changing fonts. 2116 * 2117 * Changing fonts is a little tricky. We must prevent 2118 * simultaneous access to the device, so we must stop 2119 * the display timer and the terminal from accessing. 2120 * We need to switch fonts and grow our screen buffer. 2121 * 2122 * XXX: Right now the code uses terminal_mute() to 2123 * prevent data from reaching the console driver while 2124 * resizing the screen buffer. This isn't elegant... 2125 */ 2126 2127 VT_LOCK(vd); 2128 if (vw->vw_flags & VWF_BUSY) { 2129 /* Another process is changing the font. */ 2130 VT_UNLOCK(vd); 2131 return (EBUSY); 2132 } 2133 vw->vw_flags |= VWF_BUSY; 2134 VT_UNLOCK(vd); 2135 2136 vt_termsize(vd, vf, &size); 2137 vt_winsize(vd, vf, &wsz); 2138 2139 /* Grow the screen buffer and terminal. */ 2140 terminal_mute(tm, 1); 2141 vtbuf_grow(&vw->vw_buf, &size, vw->vw_buf.vb_history_size); 2142 terminal_set_winsize_blank(tm, &wsz, 0, NULL); 2143 terminal_set_cursor(tm, &vw->vw_buf.vb_cursor); 2144 terminal_mute(tm, 0); 2145 2146 /* Actually apply the font to the current window. */ 2147 VT_LOCK(vd); 2148 if (vw->vw_font != vf && vw->vw_font != NULL && vf != NULL) { 2149 /* 2150 * In case vt_change_font called to update size we don't need 2151 * to update font link. 2152 */ 2153 vtfont_unref(vw->vw_font); 2154 vw->vw_font = vtfont_ref(vf); 2155 } 2156 2157 /* 2158 * Compute the drawable area and move the mouse cursor inside 2159 * it, in case the new area is smaller than the previous one. 2160 */ 2161 vt_compute_drawable_area(vw); 2162 vd->vd_mx = min(vd->vd_mx, 2163 vw->vw_draw_area.tr_end.tp_col - 2164 vw->vw_draw_area.tr_begin.tp_col - 1); 2165 vd->vd_my = min(vd->vd_my, 2166 vw->vw_draw_area.tr_end.tp_row - 2167 vw->vw_draw_area.tr_begin.tp_row - 1); 2168 2169 /* Force a full redraw the next timer tick. */ 2170 if (vd->vd_curwindow == vw) { 2171 vd->vd_flags |= VDF_INVALID; 2172 vt_resume_flush_timer(vw, 0); 2173 } 2174 vw->vw_flags &= ~VWF_BUSY; 2175 VT_UNLOCK(vd); 2176 return (0); 2177 } 2178 2179 static int 2180 vt_proc_alive(struct vt_window *vw) 2181 { 2182 struct proc *p; 2183 2184 if (vw->vw_smode.mode != VT_PROCESS) 2185 return (FALSE); 2186 2187 if (vw->vw_proc) { 2188 if ((p = pfind(vw->vw_pid)) != NULL) 2189 PROC_UNLOCK(p); 2190 if (vw->vw_proc == p) 2191 return (TRUE); 2192 vw->vw_proc = NULL; 2193 vw->vw_smode.mode = VT_AUTO; 2194 DPRINTF(1, "vt controlling process %d died\n", vw->vw_pid); 2195 vw->vw_pid = 0; 2196 } 2197 return (FALSE); 2198 } 2199 2200 static int 2201 signal_vt_rel(struct vt_window *vw) 2202 { 2203 2204 if (vw->vw_smode.mode != VT_PROCESS) 2205 return (FALSE); 2206 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 2207 vw->vw_proc = NULL; 2208 vw->vw_pid = 0; 2209 return (TRUE); 2210 } 2211 vw->vw_flags |= VWF_SWWAIT_REL; 2212 PROC_LOCK(vw->vw_proc); 2213 kern_psignal(vw->vw_proc, vw->vw_smode.relsig); 2214 PROC_UNLOCK(vw->vw_proc); 2215 DPRINTF(1, "sending relsig to %d\n", vw->vw_pid); 2216 return (TRUE); 2217 } 2218 2219 static int 2220 signal_vt_acq(struct vt_window *vw) 2221 { 2222 2223 if (vw->vw_smode.mode != VT_PROCESS) 2224 return (FALSE); 2225 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 2226 cnavailable(vw->vw_terminal->consdev, FALSE); 2227 if (vw->vw_proc == NULL || vt_proc_alive(vw) == FALSE) { 2228 vw->vw_proc = NULL; 2229 vw->vw_pid = 0; 2230 return (TRUE); 2231 } 2232 vw->vw_flags |= VWF_SWWAIT_ACQ; 2233 PROC_LOCK(vw->vw_proc); 2234 kern_psignal(vw->vw_proc, vw->vw_smode.acqsig); 2235 PROC_UNLOCK(vw->vw_proc); 2236 DPRINTF(1, "sending acqsig to %d\n", vw->vw_pid); 2237 return (TRUE); 2238 } 2239 2240 static int 2241 finish_vt_rel(struct vt_window *vw, int release, int *s) 2242 { 2243 2244 if (vw->vw_flags & VWF_SWWAIT_REL) { 2245 vw->vw_flags &= ~VWF_SWWAIT_REL; 2246 if (release) { 2247 taskqueue_drain_timeout(taskqueue_thread, &vw->vw_timeout_task_dead); 2248 (void)vt_late_window_switch(vw->vw_switch_to); 2249 } 2250 return (0); 2251 } 2252 return (EINVAL); 2253 } 2254 2255 static int 2256 finish_vt_acq(struct vt_window *vw) 2257 { 2258 2259 if (vw->vw_flags & VWF_SWWAIT_ACQ) { 2260 vw->vw_flags &= ~VWF_SWWAIT_ACQ; 2261 return (0); 2262 } 2263 return (EINVAL); 2264 } 2265 2266 #ifndef SC_NO_CUTPASTE 2267 static void 2268 vt_mouse_terminput_button(struct vt_device *vd, int button) 2269 { 2270 struct vt_window *vw; 2271 struct vt_font *vf; 2272 char mouseb[6] = "\x1B[M"; 2273 int i, x, y; 2274 2275 vw = vd->vd_curwindow; 2276 vf = vw->vw_font; 2277 2278 /* Translate to char position. */ 2279 x = vd->vd_mx / vf->vf_width; 2280 y = vd->vd_my / vf->vf_height; 2281 /* Avoid overflow. */ 2282 x = MIN(x, 255 - '!'); 2283 y = MIN(y, 255 - '!'); 2284 2285 mouseb[3] = ' ' + button; 2286 mouseb[4] = '!' + x; 2287 mouseb[5] = '!' + y; 2288 2289 for (i = 0; i < sizeof(mouseb); i++) 2290 terminal_input_char(vw->vw_terminal, mouseb[i]); 2291 } 2292 2293 static void 2294 vt_mouse_terminput(struct vt_device *vd, int type, int x, int y, int event, 2295 int cnt) 2296 { 2297 2298 switch (type) { 2299 case MOUSE_BUTTON_EVENT: 2300 if (cnt > 0) { 2301 /* Mouse button pressed. */ 2302 if (event & MOUSE_BUTTON1DOWN) 2303 vt_mouse_terminput_button(vd, 0); 2304 if (event & MOUSE_BUTTON2DOWN) 2305 vt_mouse_terminput_button(vd, 1); 2306 if (event & MOUSE_BUTTON3DOWN) 2307 vt_mouse_terminput_button(vd, 2); 2308 } else { 2309 /* Mouse button released. */ 2310 vt_mouse_terminput_button(vd, 3); 2311 } 2312 break; 2313 #ifdef notyet 2314 case MOUSE_MOTION_EVENT: 2315 if (mouse->u.data.z < 0) { 2316 /* Scroll up. */ 2317 sc_mouse_input_button(vd, 64); 2318 } else if (mouse->u.data.z > 0) { 2319 /* Scroll down. */ 2320 sc_mouse_input_button(vd, 65); 2321 } 2322 break; 2323 #endif 2324 } 2325 } 2326 2327 static void 2328 vt_mouse_paste(void) 2329 { 2330 term_char_t *buf; 2331 int i, len; 2332 2333 len = VD_PASTEBUFLEN(main_vd); 2334 buf = VD_PASTEBUF(main_vd); 2335 len /= sizeof(term_char_t); 2336 for (i = 0; i < len; i++) { 2337 if (TCHAR_CHARACTER(buf[i]) == '\0') 2338 continue; 2339 terminal_input_char(main_vd->vd_curwindow->vw_terminal, 2340 buf[i]); 2341 } 2342 } 2343 2344 void 2345 vt_mouse_event(int type, int x, int y, int event, int cnt, int mlevel) 2346 { 2347 struct vt_device *vd; 2348 struct vt_window *vw; 2349 struct vt_font *vf; 2350 term_pos_t size; 2351 int len, mark; 2352 2353 vd = main_vd; 2354 vw = vd->vd_curwindow; 2355 vf = vw->vw_font; 2356 2357 if (vw->vw_flags & (VWF_MOUSE_HIDE | VWF_GRAPHICS)) 2358 /* 2359 * Either the mouse is disabled, or the window is in 2360 * "graphics mode". The graphics mode is usually set by 2361 * an X server, using the KDSETMODE ioctl. 2362 */ 2363 return; 2364 2365 if (vf == NULL) /* Text mode. */ 2366 return; 2367 2368 /* 2369 * TODO: add flag about pointer position changed, to not redraw chars 2370 * under mouse pointer when nothing changed. 2371 */ 2372 2373 if (vw->vw_mouse_level > 0) 2374 vt_mouse_terminput(vd, type, x, y, event, cnt); 2375 2376 switch (type) { 2377 case MOUSE_ACTION: 2378 case MOUSE_MOTION_EVENT: 2379 /* Movement */ 2380 x += vd->vd_mx; 2381 y += vd->vd_my; 2382 2383 vt_termsize(vd, vf, &size); 2384 2385 /* Apply limits. */ 2386 x = MAX(x, 0); 2387 y = MAX(y, 0); 2388 x = MIN(x, (size.tp_col * vf->vf_width) - 1); 2389 y = MIN(y, (size.tp_row * vf->vf_height) - 1); 2390 2391 vd->vd_mx = x; 2392 vd->vd_my = y; 2393 if (vd->vd_mstate & (MOUSE_BUTTON1DOWN | VT_MOUSE_EXTENDBUTTON)) 2394 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_MOVE, 2395 vd->vd_mx / vf->vf_width, 2396 vd->vd_my / vf->vf_height); 2397 2398 vt_resume_flush_timer(vw, 0); 2399 return; /* Done */ 2400 case MOUSE_BUTTON_EVENT: 2401 /* Buttons */ 2402 break; 2403 default: 2404 return; /* Done */ 2405 } 2406 2407 switch (event) { 2408 case MOUSE_BUTTON1DOWN: 2409 switch (cnt % 4) { 2410 case 0: /* up */ 2411 mark = VTB_MARK_END; 2412 break; 2413 case 1: /* single click: start cut operation */ 2414 mark = VTB_MARK_START; 2415 break; 2416 case 2: /* double click: cut a word */ 2417 mark = VTB_MARK_WORD; 2418 break; 2419 default: /* triple click: cut a line */ 2420 mark = VTB_MARK_ROW; 2421 break; 2422 } 2423 break; 2424 case VT_MOUSE_PASTEBUTTON: 2425 switch (cnt) { 2426 case 0: /* up */ 2427 break; 2428 default: 2429 vt_mouse_paste(); 2430 /* clear paste buffer selection after paste */ 2431 vtbuf_set_mark(&vw->vw_buf, VTB_MARK_START, 2432 vd->vd_mx / vf->vf_width, 2433 vd->vd_my / vf->vf_height); 2434 break; 2435 } 2436 return; /* Done */ 2437 case VT_MOUSE_EXTENDBUTTON: 2438 switch (cnt) { 2439 case 0: /* up */ 2440 if (!(vd->vd_mstate & MOUSE_BUTTON1DOWN)) 2441 mark = VTB_MARK_EXTEND; 2442 else 2443 mark = VTB_MARK_NONE; 2444 break; 2445 default: 2446 mark = VTB_MARK_EXTEND; 2447 break; 2448 } 2449 break; 2450 default: 2451 return; /* Done */ 2452 } 2453 2454 /* Save buttons state. */ 2455 if (cnt > 0) 2456 vd->vd_mstate |= event; 2457 else 2458 vd->vd_mstate &= ~event; 2459 2460 if (vtbuf_set_mark(&vw->vw_buf, mark, vd->vd_mx / vf->vf_width, 2461 vd->vd_my / vf->vf_height) == 1) { 2462 /* 2463 * We have something marked to copy, so update pointer to 2464 * window with selection. 2465 */ 2466 vt_resume_flush_timer(vw, 0); 2467 2468 switch (mark) { 2469 case VTB_MARK_END: 2470 case VTB_MARK_WORD: 2471 case VTB_MARK_ROW: 2472 case VTB_MARK_EXTEND: 2473 break; 2474 default: 2475 /* Other types of mark do not require to copy data. */ 2476 return; 2477 } 2478 2479 /* Get current selection size in bytes. */ 2480 len = vtbuf_get_marked_len(&vw->vw_buf); 2481 if (len <= 0) 2482 return; 2483 2484 /* Reallocate buffer only if old one is too small. */ 2485 if (len > VD_PASTEBUFSZ(vd)) { 2486 VD_PASTEBUF(vd) = realloc(VD_PASTEBUF(vd), len, M_VT, 2487 M_WAITOK | M_ZERO); 2488 /* Update buffer size. */ 2489 VD_PASTEBUFSZ(vd) = len; 2490 } 2491 /* Request copy/paste buffer data, no more than `len' */ 2492 vtbuf_extract_marked(&vw->vw_buf, VD_PASTEBUF(vd), len, mark); 2493 2494 VD_PASTEBUFLEN(vd) = len; 2495 2496 /* XXX VD_PASTEBUF(vd) have to be freed on shutdown/unload. */ 2497 } 2498 } 2499 2500 void 2501 vt_mouse_state(int show) 2502 { 2503 struct vt_device *vd; 2504 struct vt_window *vw; 2505 2506 vd = main_vd; 2507 vw = vd->vd_curwindow; 2508 2509 switch (show) { 2510 case VT_MOUSE_HIDE: 2511 vw->vw_flags |= VWF_MOUSE_HIDE; 2512 break; 2513 case VT_MOUSE_SHOW: 2514 vw->vw_flags &= ~VWF_MOUSE_HIDE; 2515 break; 2516 } 2517 2518 /* Mark mouse position as dirty. */ 2519 vt_mark_mouse_position_as_dirty(vd, false); 2520 vt_resume_flush_timer(vw, 0); 2521 } 2522 #endif 2523 2524 static int 2525 vtterm_mmap(struct terminal *tm, vm_ooffset_t offset, vm_paddr_t * paddr, 2526 int nprot, vm_memattr_t *memattr) 2527 { 2528 struct vt_window *vw = tm->tm_softc; 2529 struct vt_device *vd = vw->vw_device; 2530 2531 if (vd->vd_driver->vd_fb_mmap) 2532 return (vd->vd_driver->vd_fb_mmap(vd, offset, paddr, nprot, 2533 memattr)); 2534 2535 return (ENXIO); 2536 } 2537 2538 static int 2539 vtterm_ioctl(struct terminal *tm, u_long cmd, caddr_t data, 2540 struct thread *td) 2541 { 2542 struct vt_window *vw = tm->tm_softc; 2543 struct vt_device *vd = vw->vw_device; 2544 keyboard_t *kbd; 2545 int error, i, s; 2546 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \ 2547 defined(COMPAT_FREEBSD4) || defined(COMPAT_43) 2548 int ival; 2549 2550 switch (cmd) { 2551 case _IO('v', 4): 2552 cmd = VT_RELDISP; 2553 break; 2554 case _IO('v', 5): 2555 cmd = VT_ACTIVATE; 2556 break; 2557 case _IO('v', 6): 2558 cmd = VT_WAITACTIVE; 2559 break; 2560 case _IO('K', 20): 2561 cmd = KDSKBSTATE; 2562 break; 2563 case _IO('K', 67): 2564 cmd = KDSETRAD; 2565 break; 2566 case _IO('K', 7): 2567 cmd = KDSKBMODE; 2568 break; 2569 case _IO('K', 8): 2570 cmd = KDMKTONE; 2571 break; 2572 case _IO('K', 10): 2573 cmd = KDSETMODE; 2574 break; 2575 case _IO('K', 13): 2576 cmd = KDSBORDER; 2577 break; 2578 case _IO('K', 63): 2579 cmd = KIOCSOUND; 2580 break; 2581 case _IO('K', 66): 2582 cmd = KDSETLED; 2583 break; 2584 case _IO('c', 104): 2585 cmd = CONS_SETWINORG; 2586 break; 2587 case _IO('c', 110): 2588 cmd = CONS_SETKBD; 2589 break; 2590 default: 2591 goto skip_thunk; 2592 } 2593 ival = IOCPARM_IVAL(data); 2594 data = (caddr_t)&ival; 2595 skip_thunk: 2596 #endif 2597 2598 switch (cmd) { 2599 case KDSETRAD: /* set keyboard repeat & delay rates (old) */ 2600 if (*(int *)data & ~0x7f) 2601 return (EINVAL); 2602 /* FALLTHROUGH */ 2603 case GIO_KEYMAP: 2604 case PIO_KEYMAP: 2605 case GIO_DEADKEYMAP: 2606 case PIO_DEADKEYMAP: 2607 #ifdef COMPAT_FREEBSD13 2608 case OGIO_DEADKEYMAP: 2609 case OPIO_DEADKEYMAP: 2610 #endif /* COMPAT_FREEBSD13 */ 2611 case GETFKEY: 2612 case SETFKEY: 2613 case KDGKBINFO: 2614 case KDGKBTYPE: 2615 case KDGETREPEAT: /* get keyboard repeat & delay rates */ 2616 case KDSETREPEAT: /* set keyboard repeat & delay rates (new) */ 2617 case KBADDKBD: /* add keyboard to mux */ 2618 case KBRELKBD: { /* release keyboard from mux */ 2619 error = 0; 2620 2621 mtx_lock(&Giant); 2622 if ((kbd = vd->vd_keyboard) != NULL) 2623 error = kbdd_ioctl(kbd, cmd, data); 2624 mtx_unlock(&Giant); 2625 if (error == ENOIOCTL) { 2626 if (cmd == KDGKBTYPE) { 2627 /* always return something? XXX */ 2628 *(int *)data = 0; 2629 } else { 2630 return (ENODEV); 2631 } 2632 } 2633 return (error); 2634 } 2635 case KDGKBSTATE: { /* get keyboard state (locks) */ 2636 error = 0; 2637 2638 if (vw == vd->vd_curwindow) { 2639 mtx_lock(&Giant); 2640 if ((kbd = vd->vd_keyboard) != NULL) 2641 error = vt_save_kbd_state(vw, kbd); 2642 mtx_unlock(&Giant); 2643 2644 if (error != 0) 2645 return (error); 2646 } 2647 2648 *(int *)data = vw->vw_kbdstate & LOCK_MASK; 2649 2650 return (error); 2651 } 2652 case KDSKBSTATE: { /* set keyboard state (locks) */ 2653 int state; 2654 2655 state = *(int *)data; 2656 if (state & ~LOCK_MASK) 2657 return (EINVAL); 2658 2659 vw->vw_kbdstate &= ~LOCK_MASK; 2660 vw->vw_kbdstate |= state; 2661 2662 error = 0; 2663 if (vw == vd->vd_curwindow) { 2664 mtx_lock(&Giant); 2665 if ((kbd = vd->vd_keyboard) != NULL) 2666 error = vt_update_kbd_state(vw, kbd); 2667 mtx_unlock(&Giant); 2668 } 2669 2670 return (error); 2671 } 2672 case KDGETLED: { /* get keyboard LED status */ 2673 error = 0; 2674 2675 if (vw == vd->vd_curwindow) { 2676 mtx_lock(&Giant); 2677 if ((kbd = vd->vd_keyboard) != NULL) 2678 error = vt_save_kbd_leds(vw, kbd); 2679 mtx_unlock(&Giant); 2680 2681 if (error != 0) 2682 return (error); 2683 } 2684 2685 *(int *)data = vw->vw_kbdstate & LED_MASK; 2686 2687 return (error); 2688 } 2689 case KDSETLED: { /* set keyboard LED status */ 2690 int leds; 2691 2692 leds = *(int *)data; 2693 if (leds & ~LED_MASK) 2694 return (EINVAL); 2695 2696 vw->vw_kbdstate &= ~LED_MASK; 2697 vw->vw_kbdstate |= leds; 2698 2699 error = 0; 2700 if (vw == vd->vd_curwindow) { 2701 mtx_lock(&Giant); 2702 if ((kbd = vd->vd_keyboard) != NULL) 2703 error = vt_update_kbd_leds(vw, kbd); 2704 mtx_unlock(&Giant); 2705 } 2706 2707 return (error); 2708 } 2709 case KDGETMODE: 2710 *(int *)data = (vw->vw_flags & VWF_GRAPHICS) ? 2711 KD_GRAPHICS : KD_TEXT; 2712 return (0); 2713 case KDGKBMODE: { 2714 error = 0; 2715 2716 if (vw == vd->vd_curwindow) { 2717 mtx_lock(&Giant); 2718 if ((kbd = vd->vd_keyboard) != NULL) 2719 error = vt_save_kbd_mode(vw, kbd); 2720 mtx_unlock(&Giant); 2721 2722 if (error != 0) 2723 return (error); 2724 } 2725 2726 *(int *)data = vw->vw_kbdmode; 2727 2728 return (error); 2729 } 2730 case KDSKBMODE: { 2731 int mode; 2732 2733 mode = *(int *)data; 2734 switch (mode) { 2735 case K_XLATE: 2736 case K_RAW: 2737 case K_CODE: 2738 vw->vw_kbdmode = mode; 2739 2740 error = 0; 2741 if (vw == vd->vd_curwindow) { 2742 mtx_lock(&Giant); 2743 if ((kbd = vd->vd_keyboard) != NULL) 2744 error = vt_update_kbd_mode(vw, kbd); 2745 mtx_unlock(&Giant); 2746 } 2747 2748 return (error); 2749 default: 2750 return (EINVAL); 2751 } 2752 } 2753 case FBIOGTYPE: 2754 case FBIO_GETWINORG: /* get frame buffer window origin */ 2755 case FBIO_GETDISPSTART: /* get display start address */ 2756 case FBIO_GETLINEWIDTH: /* get scan line width in bytes */ 2757 case FBIO_BLANK: /* blank display */ 2758 case FBIO_GETRGBOFFS: /* get RGB offsets */ 2759 if (vd->vd_driver->vd_fb_ioctl) 2760 return (vd->vd_driver->vd_fb_ioctl(vd, cmd, data, td)); 2761 break; 2762 case CONS_BLANKTIME: 2763 /* XXX */ 2764 return (0); 2765 case CONS_HISTORY: 2766 if (*(int *)data < 0) 2767 return EINVAL; 2768 if (*(int *)data != vw->vw_buf.vb_history_size) 2769 vtbuf_sethistory_size(&vw->vw_buf, *(int *)data); 2770 return (0); 2771 case CONS_CLRHIST: 2772 vtbuf_clearhistory(&vw->vw_buf); 2773 /* 2774 * Invalidate the entire visible window; it is not guaranteed 2775 * that this operation will be immediately followed by a scroll 2776 * event, so it would otherwise be possible for prior artifacts 2777 * to remain visible. 2778 */ 2779 VT_LOCK(vd); 2780 if (vw == vd->vd_curwindow) { 2781 vd->vd_flags |= VDF_INVALID; 2782 vt_resume_flush_timer(vw, 0); 2783 } 2784 VT_UNLOCK(vd); 2785 return (0); 2786 case CONS_GET: 2787 /* XXX */ 2788 *(int *)data = M_CG640x480; 2789 return (0); 2790 case CONS_BELLTYPE: /* set bell type sound */ 2791 if ((*(int *)data) & CONS_QUIET_BELL) 2792 vd->vd_flags |= VDF_QUIET_BELL; 2793 else 2794 vd->vd_flags &= ~VDF_QUIET_BELL; 2795 return (0); 2796 case CONS_GETINFO: { 2797 vid_info_t *vi = (vid_info_t *)data; 2798 if (vi->size != sizeof(struct vid_info)) 2799 return (EINVAL); 2800 2801 if (vw == vd->vd_curwindow) { 2802 mtx_lock(&Giant); 2803 if ((kbd = vd->vd_keyboard) != NULL) 2804 vt_save_kbd_state(vw, kbd); 2805 mtx_unlock(&Giant); 2806 } 2807 2808 vi->m_num = vd->vd_curwindow->vw_number + 1; 2809 vi->mk_keylock = vw->vw_kbdstate & LOCK_MASK; 2810 /* XXX: other fields! */ 2811 return (0); 2812 } 2813 case CONS_GETVERS: 2814 *(int *)data = 0x200; 2815 return (0); 2816 case CONS_MODEINFO: 2817 /* XXX */ 2818 return (0); 2819 case CONS_MOUSECTL: { 2820 mouse_info_t *mouse = (mouse_info_t*)data; 2821 2822 /* 2823 * All the commands except MOUSE_SHOW nd MOUSE_HIDE 2824 * should not be applied to individual TTYs, but only to 2825 * consolectl. 2826 */ 2827 switch (mouse->operation) { 2828 case MOUSE_HIDE: 2829 if (vd->vd_flags & VDF_MOUSECURSOR) { 2830 vd->vd_flags &= ~VDF_MOUSECURSOR; 2831 #ifndef SC_NO_CUTPASTE 2832 vt_mouse_state(VT_MOUSE_HIDE); 2833 #endif 2834 } 2835 return (0); 2836 case MOUSE_SHOW: 2837 if (!(vd->vd_flags & VDF_MOUSECURSOR)) { 2838 vd->vd_flags |= VDF_MOUSECURSOR; 2839 vd->vd_mx = vd->vd_width / 2; 2840 vd->vd_my = vd->vd_height / 2; 2841 #ifndef SC_NO_CUTPASTE 2842 vt_mouse_state(VT_MOUSE_SHOW); 2843 #endif 2844 } 2845 return (0); 2846 default: 2847 return (EINVAL); 2848 } 2849 } 2850 case PIO_VFONT: { 2851 struct vt_font *vf; 2852 2853 if (vd->vd_flags & VDF_TEXTMODE) 2854 return (ENOTSUP); 2855 2856 error = vtfont_load((void *)data, &vf); 2857 if (error != 0) 2858 return (error); 2859 2860 error = vt_change_font(vw, vf); 2861 vtfont_unref(vf); 2862 return (error); 2863 } 2864 case PIO_VFONT_DEFAULT: { 2865 /* Reset to default font. */ 2866 error = vt_change_font(vw, vt_font_assigned); 2867 return (error); 2868 } 2869 case GIO_SCRNMAP: { 2870 scrmap_t *sm = (scrmap_t *)data; 2871 2872 /* We don't have screen maps, so return a handcrafted one. */ 2873 for (i = 0; i < 256; i++) 2874 sm->scrmap[i] = i; 2875 return (0); 2876 } 2877 case KDSETMODE: 2878 /* 2879 * FIXME: This implementation is incomplete compared to 2880 * syscons. 2881 */ 2882 switch (*(int *)data) { 2883 case KD_TEXT: 2884 case KD_TEXT1: 2885 case KD_PIXEL: 2886 vw->vw_flags &= ~VWF_GRAPHICS; 2887 break; 2888 case KD_GRAPHICS: 2889 vw->vw_flags |= VWF_GRAPHICS; 2890 break; 2891 } 2892 return (0); 2893 case KDENABIO: /* allow io operations */ 2894 error = priv_check(td, PRIV_IO); 2895 if (error != 0) 2896 return (error); 2897 error = securelevel_gt(td->td_ucred, 0); 2898 if (error != 0) 2899 return (error); 2900 #if defined(__i386__) 2901 td->td_frame->tf_eflags |= PSL_IOPL; 2902 #elif defined(__amd64__) 2903 td->td_frame->tf_rflags |= PSL_IOPL; 2904 #endif 2905 return (0); 2906 case KDDISABIO: /* disallow io operations (default) */ 2907 #if defined(__i386__) 2908 td->td_frame->tf_eflags &= ~PSL_IOPL; 2909 #elif defined(__amd64__) 2910 td->td_frame->tf_rflags &= ~PSL_IOPL; 2911 #endif 2912 return (0); 2913 case KDMKTONE: /* sound the bell */ 2914 vtterm_beep(tm, *(u_int *)data); 2915 return (0); 2916 case KIOCSOUND: /* make tone (*data) hz */ 2917 /* TODO */ 2918 return (0); 2919 case CONS_SETKBD: /* set the new keyboard */ 2920 mtx_lock(&Giant); 2921 error = 0; 2922 if (vd->vd_keyboard == NULL || 2923 vd->vd_keyboard->kb_index != *(int *)data) { 2924 kbd = kbd_get_keyboard(*(int *)data); 2925 if (kbd == NULL) { 2926 mtx_unlock(&Giant); 2927 return (EINVAL); 2928 } 2929 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, 2930 (void *)vd, vt_kbdevent, vd); 2931 if (i >= 0) { 2932 if ((kbd = vd->vd_keyboard) != NULL) { 2933 vt_save_kbd_state(vd->vd_curwindow, kbd); 2934 kbd_release(kbd, (void *)vd); 2935 } 2936 kbd = vd->vd_keyboard = kbd_get_keyboard(i); 2937 2938 vt_update_kbd_mode(vd->vd_curwindow, kbd); 2939 vt_update_kbd_state(vd->vd_curwindow, kbd); 2940 } else { 2941 error = EPERM; /* XXX */ 2942 } 2943 } 2944 mtx_unlock(&Giant); 2945 return (error); 2946 case CONS_RELKBD: /* release the current keyboard */ 2947 mtx_lock(&Giant); 2948 error = 0; 2949 if ((kbd = vd->vd_keyboard) != NULL) { 2950 vt_save_kbd_state(vd->vd_curwindow, kbd); 2951 error = kbd_release(kbd, (void *)vd); 2952 if (error == 0) { 2953 vd->vd_keyboard = NULL; 2954 } 2955 } 2956 mtx_unlock(&Giant); 2957 return (error); 2958 case VT_ACTIVATE: { 2959 int win; 2960 win = *(int *)data - 1; 2961 DPRINTF(5, "%s%d: VT_ACTIVATE ttyv%d ", SC_DRIVER_NAME, 2962 VT_UNIT(vw), win); 2963 if ((win >= VT_MAXWINDOWS) || (win < 0)) 2964 return (EINVAL); 2965 return (vt_proc_window_switch(vd->vd_windows[win])); 2966 } 2967 case VT_GETACTIVE: 2968 *(int *)data = vd->vd_curwindow->vw_number + 1; 2969 return (0); 2970 case VT_GETINDEX: 2971 *(int *)data = vw->vw_number + 1; 2972 return (0); 2973 case VT_LOCKSWITCH: 2974 /* TODO: Check current state, switching can be in progress. */ 2975 if ((*(int *)data) == 0x01) 2976 vw->vw_flags |= VWF_VTYLOCK; 2977 else if ((*(int *)data) == 0x02) 2978 vw->vw_flags &= ~VWF_VTYLOCK; 2979 else 2980 return (EINVAL); 2981 return (0); 2982 case VT_OPENQRY: 2983 VT_LOCK(vd); 2984 for (i = 0; i < VT_MAXWINDOWS; i++) { 2985 vw = vd->vd_windows[i]; 2986 if (vw == NULL) 2987 continue; 2988 if (!(vw->vw_flags & VWF_OPENED)) { 2989 *(int *)data = vw->vw_number + 1; 2990 VT_UNLOCK(vd); 2991 return (0); 2992 } 2993 } 2994 VT_UNLOCK(vd); 2995 return (EINVAL); 2996 case VT_WAITACTIVE: { 2997 unsigned int idx; 2998 2999 error = 0; 3000 3001 idx = *(unsigned int *)data; 3002 if (idx > VT_MAXWINDOWS) 3003 return (EINVAL); 3004 if (idx > 0) 3005 vw = vd->vd_windows[idx - 1]; 3006 3007 VT_LOCK(vd); 3008 while (vd->vd_curwindow != vw && error == 0) 3009 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 3010 VT_UNLOCK(vd); 3011 return (error); 3012 } 3013 case VT_SETMODE: { /* set screen switcher mode */ 3014 struct vt_mode *mode; 3015 struct proc *p1; 3016 3017 mode = (struct vt_mode *)data; 3018 DPRINTF(5, "%s%d: VT_SETMODE ", SC_DRIVER_NAME, VT_UNIT(vw)); 3019 if (vw->vw_smode.mode == VT_PROCESS) { 3020 p1 = pfind(vw->vw_pid); 3021 if (vw->vw_proc == p1 && vw->vw_proc != td->td_proc) { 3022 if (p1) 3023 PROC_UNLOCK(p1); 3024 DPRINTF(5, "error EPERM\n"); 3025 return (EPERM); 3026 } 3027 if (p1) 3028 PROC_UNLOCK(p1); 3029 } 3030 if (mode->mode == VT_AUTO) { 3031 vw->vw_smode.mode = VT_AUTO; 3032 vw->vw_proc = NULL; 3033 vw->vw_pid = 0; 3034 DPRINTF(5, "VT_AUTO, "); 3035 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 3036 cnavailable(vw->vw_terminal->consdev, TRUE); 3037 /* were we in the middle of the vty switching process? */ 3038 if (finish_vt_rel(vw, TRUE, &s) == 0) 3039 DPRINTF(5, "reset WAIT_REL, "); 3040 if (finish_vt_acq(vw) == 0) 3041 DPRINTF(5, "reset WAIT_ACQ, "); 3042 return (0); 3043 } else if (mode->mode == VT_PROCESS) { 3044 if (!ISSIGVALID(mode->relsig) || 3045 !ISSIGVALID(mode->acqsig) || 3046 !ISSIGVALID(mode->frsig)) { 3047 DPRINTF(5, "error EINVAL\n"); 3048 return (EINVAL); 3049 } 3050 DPRINTF(5, "VT_PROCESS %d, ", td->td_proc->p_pid); 3051 bcopy(data, &vw->vw_smode, sizeof(struct vt_mode)); 3052 vw->vw_proc = td->td_proc; 3053 vw->vw_pid = vw->vw_proc->p_pid; 3054 if (vw == vw->vw_device->vd_windows[VT_CONSWINDOW]) 3055 cnavailable(vw->vw_terminal->consdev, FALSE); 3056 } else { 3057 DPRINTF(5, "VT_SETMODE failed, unknown mode %d\n", 3058 mode->mode); 3059 return (EINVAL); 3060 } 3061 DPRINTF(5, "\n"); 3062 return (0); 3063 } 3064 case VT_GETMODE: /* get screen switcher mode */ 3065 bcopy(&vw->vw_smode, data, sizeof(struct vt_mode)); 3066 return (0); 3067 3068 case VT_RELDISP: /* screen switcher ioctl */ 3069 /* 3070 * This must be the current vty which is in the VT_PROCESS 3071 * switching mode... 3072 */ 3073 if ((vw != vd->vd_curwindow) || (vw->vw_smode.mode != 3074 VT_PROCESS)) { 3075 return (EINVAL); 3076 } 3077 /* ...and this process is controlling it. */ 3078 if (vw->vw_proc != td->td_proc) { 3079 return (EPERM); 3080 } 3081 error = EINVAL; 3082 switch(*(int *)data) { 3083 case VT_FALSE: /* user refuses to release screen, abort */ 3084 if ((error = finish_vt_rel(vw, FALSE, &s)) == 0) 3085 DPRINTF(5, "%s%d: VT_RELDISP: VT_FALSE\n", 3086 SC_DRIVER_NAME, VT_UNIT(vw)); 3087 break; 3088 case VT_TRUE: /* user has released screen, go on */ 3089 /* finish_vt_rel(..., TRUE, ...) should not be locked */ 3090 if (vw->vw_flags & VWF_SWWAIT_REL) { 3091 if ((error = finish_vt_rel(vw, TRUE, &s)) == 0) 3092 DPRINTF(5, "%s%d: VT_RELDISP: VT_TRUE\n", 3093 SC_DRIVER_NAME, VT_UNIT(vw)); 3094 } else { 3095 error = EINVAL; 3096 } 3097 return (error); 3098 case VT_ACKACQ: /* acquire acknowledged, switch completed */ 3099 if ((error = finish_vt_acq(vw)) == 0) 3100 DPRINTF(5, "%s%d: VT_RELDISP: VT_ACKACQ\n", 3101 SC_DRIVER_NAME, VT_UNIT(vw)); 3102 break; 3103 default: 3104 break; 3105 } 3106 return (error); 3107 } 3108 3109 return (ENOIOCTL); 3110 } 3111 3112 static struct vt_window * 3113 vt_allocate_window(struct vt_device *vd, unsigned int window) 3114 { 3115 struct vt_window *vw; 3116 struct terminal *tm; 3117 term_pos_t size; 3118 struct winsize wsz; 3119 3120 vw = malloc(sizeof *vw, M_VT, M_WAITOK|M_ZERO); 3121 vw->vw_device = vd; 3122 vw->vw_number = window; 3123 vw->vw_kbdmode = K_XLATE; 3124 3125 if ((vd->vd_flags & VDF_TEXTMODE) == 0) { 3126 vw->vw_font = vtfont_ref(vt_font_assigned); 3127 vt_compute_drawable_area(vw); 3128 } 3129 3130 vt_termsize(vd, vw->vw_font, &size); 3131 vt_winsize(vd, vw->vw_font, &wsz); 3132 tm = vw->vw_terminal = terminal_alloc(&vt_termclass, vw); 3133 vw->vw_buf.vb_terminal = tm; /* must be set before vtbuf_init() */ 3134 vtbuf_init(&vw->vw_buf, &size); 3135 3136 terminal_set_winsize(tm, &wsz); 3137 vd->vd_windows[window] = vw; 3138 TIMEOUT_TASK_INIT(taskqueue_thread, &vw->vw_timeout_task_dead, 0, &vt_switch_timer, vw); 3139 3140 return (vw); 3141 } 3142 3143 void 3144 vt_upgrade(struct vt_device *vd) 3145 { 3146 struct vt_window *vw; 3147 unsigned int i; 3148 int register_handlers; 3149 3150 if (!vty_enabled(VTY_VT)) 3151 return; 3152 if (main_vd->vd_driver == NULL) 3153 return; 3154 3155 for (i = 0; i < VT_MAXWINDOWS; i++) { 3156 vw = vd->vd_windows[i]; 3157 if (vw == NULL) { 3158 /* New window. */ 3159 vw = vt_allocate_window(vd, i); 3160 } 3161 if (!(vw->vw_flags & VWF_READY)) { 3162 TIMEOUT_TASK_INIT(taskqueue_thread, &vw->vw_timeout_task_dead, 0, &vt_switch_timer, vw); 3163 terminal_maketty(vw->vw_terminal, "v%r", VT_UNIT(vw)); 3164 vw->vw_flags |= VWF_READY; 3165 if (vw->vw_flags & VWF_CONSOLE) { 3166 /* For existing console window. */ 3167 EVENTHANDLER_REGISTER(shutdown_pre_sync, 3168 vt_window_switch, vw, SHUTDOWN_PRI_DEFAULT); 3169 } 3170 } 3171 } 3172 VT_LOCK(vd); 3173 if (vd->vd_curwindow == NULL) 3174 vd->vd_curwindow = vd->vd_windows[VT_CONSWINDOW]; 3175 3176 register_handlers = 0; 3177 if (!(vd->vd_flags & VDF_ASYNC)) { 3178 /* Attach keyboard. */ 3179 vt_allocate_keyboard(vd); 3180 3181 /* Init 25 Hz timer. */ 3182 callout_init_mtx(&vd->vd_timer, &vd->vd_lock, 0); 3183 3184 /* 3185 * Start timer when everything ready. 3186 * Note that the operations here are purposefully ordered. 3187 * We need to ensure vd_timer_armed is non-zero before we set 3188 * the VDF_ASYNC flag. That prevents this function from 3189 * racing with vt_resume_flush_timer() to update the 3190 * callout structure. 3191 */ 3192 atomic_add_acq_int(&vd->vd_timer_armed, 1); 3193 vd->vd_flags |= VDF_ASYNC; 3194 callout_reset(&vd->vd_timer, hz / VT_TIMERFREQ, vt_timer, vd); 3195 register_handlers = 1; 3196 } 3197 3198 VT_UNLOCK(vd); 3199 3200 /* Refill settings with new sizes. */ 3201 vt_resize(vd); 3202 3203 if (register_handlers) { 3204 /* Register suspend/resume handlers. */ 3205 EVENTHANDLER_REGISTER(power_suspend_early, vt_suspend_handler, 3206 vd, EVENTHANDLER_PRI_ANY); 3207 EVENTHANDLER_REGISTER(power_resume, vt_resume_handler, vd, 3208 EVENTHANDLER_PRI_ANY); 3209 } 3210 } 3211 3212 static void 3213 vt_resize(struct vt_device *vd) 3214 { 3215 struct vt_window *vw; 3216 int i; 3217 3218 for (i = 0; i < VT_MAXWINDOWS; i++) { 3219 vw = vd->vd_windows[i]; 3220 VT_LOCK(vd); 3221 /* Assign default font to window, if not textmode. */ 3222 if (!(vd->vd_flags & VDF_TEXTMODE) && vw->vw_font == NULL) 3223 vw->vw_font = vtfont_ref(vt_font_assigned); 3224 VT_UNLOCK(vd); 3225 3226 /* Resize terminal windows */ 3227 while (vt_change_font(vw, vw->vw_font) == EBUSY) { 3228 DPRINTF(100, "%s: vt_change_font() is busy, " 3229 "window %d\n", __func__, i); 3230 } 3231 } 3232 } 3233 3234 static void 3235 vt_replace_backend(const struct vt_driver *drv, void *softc) 3236 { 3237 struct vt_device *vd; 3238 3239 vd = main_vd; 3240 3241 if (vd->vd_flags & VDF_ASYNC) { 3242 /* Stop vt_flush periodic task. */ 3243 VT_LOCK(vd); 3244 vt_suspend_flush_timer(vd); 3245 VT_UNLOCK(vd); 3246 /* 3247 * Mute current terminal until we done. vt_change_font (called 3248 * from vt_resize) will unmute it. 3249 */ 3250 terminal_mute(vd->vd_curwindow->vw_terminal, 1); 3251 } 3252 3253 /* 3254 * Reset VDF_TEXTMODE flag, driver who require that flag (vt_vga) will 3255 * set it. 3256 */ 3257 VT_LOCK(vd); 3258 vd->vd_flags &= ~VDF_TEXTMODE; 3259 3260 if (drv != NULL) { 3261 /* 3262 * We want to upgrade from the current driver to the 3263 * given driver. 3264 */ 3265 3266 vd->vd_prev_driver = vd->vd_driver; 3267 vd->vd_prev_softc = vd->vd_softc; 3268 vd->vd_driver = drv; 3269 vd->vd_softc = softc; 3270 3271 vd->vd_driver->vd_init(vd); 3272 } else if (vd->vd_prev_driver != NULL && vd->vd_prev_softc != NULL) { 3273 /* 3274 * No driver given: we want to downgrade to the previous 3275 * driver. 3276 */ 3277 const struct vt_driver *old_drv; 3278 void *old_softc; 3279 3280 old_drv = vd->vd_driver; 3281 old_softc = vd->vd_softc; 3282 3283 vd->vd_driver = vd->vd_prev_driver; 3284 vd->vd_softc = vd->vd_prev_softc; 3285 vd->vd_prev_driver = NULL; 3286 vd->vd_prev_softc = NULL; 3287 3288 vd->vd_flags |= VDF_DOWNGRADE; 3289 3290 vd->vd_driver->vd_init(vd); 3291 3292 if (old_drv->vd_fini) 3293 old_drv->vd_fini(vd, old_softc); 3294 3295 vd->vd_flags &= ~VDF_DOWNGRADE; 3296 } 3297 3298 VT_UNLOCK(vd); 3299 3300 /* Update windows sizes and initialize last items. */ 3301 vt_upgrade(vd); 3302 3303 /* 3304 * Give a chance to the new backend to run the post-switch code, for 3305 * instance to refresh the screen. 3306 */ 3307 if (vd->vd_driver->vd_postswitch) 3308 vd->vd_driver->vd_postswitch(vd); 3309 3310 #ifdef DEV_SPLASH 3311 if (vd->vd_flags & VDF_SPLASH) 3312 vtterm_splash(vd); 3313 #endif 3314 3315 if (vd->vd_flags & VDF_ASYNC) { 3316 /* Allow to put chars now. */ 3317 terminal_mute(vd->vd_curwindow->vw_terminal, 0); 3318 /* Rerun timer for screen updates. */ 3319 vt_resume_flush_timer(vd->vd_curwindow, 0); 3320 } 3321 3322 /* 3323 * Register as console. If it already registered, cnadd() will ignore 3324 * it. 3325 */ 3326 termcn_cnregister(vd->vd_windows[VT_CONSWINDOW]->vw_terminal); 3327 } 3328 3329 static void 3330 vt_suspend_handler(void *priv) 3331 { 3332 struct vt_device *vd; 3333 3334 vd = priv; 3335 vd->vd_flags |= VDF_SUSPENDED; 3336 if (vd->vd_driver != NULL && vd->vd_driver->vd_suspend != NULL) 3337 vd->vd_driver->vd_suspend(vd); 3338 } 3339 3340 static void 3341 vt_resume_handler(void *priv) 3342 { 3343 struct vt_device *vd; 3344 3345 vd = priv; 3346 if (vd->vd_driver != NULL && vd->vd_driver->vd_resume != NULL) 3347 vd->vd_driver->vd_resume(vd); 3348 vd->vd_flags &= ~VDF_SUSPENDED; 3349 } 3350 3351 int 3352 vt_allocate(const struct vt_driver *drv, void *softc) 3353 { 3354 3355 if (!vty_enabled(VTY_VT)) 3356 return (EINVAL); 3357 3358 if (main_vd->vd_driver == NULL) { 3359 main_vd->vd_driver = drv; 3360 printf("VT: initialize with new VT driver \"%s\".\n", 3361 drv->vd_name); 3362 } else { 3363 /* 3364 * Check if have rights to replace current driver. For example: 3365 * it is bad idea to replace KMS driver with generic VGA one. 3366 */ 3367 if (drv->vd_priority <= main_vd->vd_driver->vd_priority) { 3368 printf("VT: Driver priority %d too low. Current %d\n ", 3369 drv->vd_priority, main_vd->vd_driver->vd_priority); 3370 return (EEXIST); 3371 } 3372 printf("VT: Replacing driver \"%s\" with new \"%s\".\n", 3373 main_vd->vd_driver->vd_name, drv->vd_name); 3374 } 3375 3376 vt_replace_backend(drv, softc); 3377 3378 return (0); 3379 } 3380 3381 int 3382 vt_deallocate(const struct vt_driver *drv, void *softc) 3383 { 3384 3385 if (!vty_enabled(VTY_VT)) 3386 return (EINVAL); 3387 3388 if (main_vd->vd_prev_driver == NULL || 3389 main_vd->vd_driver != drv || 3390 main_vd->vd_softc != softc) 3391 return (EPERM); 3392 3393 printf("VT: Switching back from \"%s\" to \"%s\".\n", 3394 main_vd->vd_driver->vd_name, main_vd->vd_prev_driver->vd_name); 3395 3396 vt_replace_backend(NULL, NULL); 3397 3398 return (0); 3399 } 3400 3401 void 3402 vt_suspend(struct vt_device *vd) 3403 { 3404 int error; 3405 3406 if (vt_suspendswitch == 0) 3407 return; 3408 /* Save current window. */ 3409 vd->vd_savedwindow = vd->vd_curwindow; 3410 /* Ask holding process to free window and switch to console window */ 3411 vt_proc_window_switch(vd->vd_windows[VT_CONSWINDOW]); 3412 3413 /* Wait for the window switch to complete. */ 3414 error = 0; 3415 VT_LOCK(vd); 3416 while (vd->vd_curwindow != vd->vd_windows[VT_CONSWINDOW] && error == 0) 3417 error = cv_wait_sig(&vd->vd_winswitch, &vd->vd_lock); 3418 VT_UNLOCK(vd); 3419 } 3420 3421 void 3422 vt_resume(struct vt_device *vd) 3423 { 3424 3425 if (vt_suspendswitch == 0) 3426 return; 3427 /* Switch back to saved window, if any */ 3428 vt_proc_window_switch(vd->vd_savedwindow); 3429 vd->vd_savedwindow = NULL; 3430 } 3431