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