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