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