1 /* $NetBSD: wsdisplay_vcons.c,v 1.16 2008/04/28 20:24:01 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2005, 2006 Michael Lorenz 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.16 2008/04/28 20:24:01 martin Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/buf.h> 36 #include <sys/device.h> 37 #include <sys/ioctl.h> 38 #include <sys/malloc.h> 39 #include <sys/mman.h> 40 #include <sys/tty.h> 41 #include <sys/conf.h> 42 #include <sys/proc.h> 43 #include <sys/kthread.h> 44 #include <sys/tprintf.h> 45 46 #include <dev/wscons/wsdisplayvar.h> 47 #include <dev/wscons/wsconsio.h> 48 #include <dev/wsfont/wsfont.h> 49 #include <dev/rasops/rasops.h> 50 51 #include <dev/wscons/wsdisplay_vconsvar.h> 52 53 #include "opt_wsemul.h" 54 #include "opt_wsdisplay_compat.h" 55 #include "opt_vcons.h" 56 57 static void vcons_dummy_init_screen(void *, struct vcons_screen *, int, 58 long *); 59 60 static int vcons_ioctl(void *, void *, u_long, void *, int, struct lwp *); 61 static int vcons_alloc_screen(void *, const struct wsscreen_descr *, void **, 62 int *, int *, long *); 63 static void vcons_free_screen(void *, void *); 64 static int vcons_show_screen(void *, void *, int, void (*)(void *, int, int), 65 void *); 66 67 #ifdef WSDISPLAY_SCROLLSUPPORT 68 static void vcons_scroll(void *, void *, int); 69 static void vcons_do_scroll(struct vcons_screen *); 70 #endif 71 72 static void vcons_do_switch(void *); 73 74 /* methods that work only on text buffers */ 75 static void vcons_copycols_buffer(void *, int, int, int, int); 76 static void vcons_erasecols_buffer(void *, int, int, int, long); 77 static void vcons_copyrows_buffer(void *, int, int, int); 78 static void vcons_eraserows_buffer(void *, int, int, long); 79 static void vcons_putchar_buffer(void *, int, int, u_int, long); 80 81 /* 82 * actual wrapper methods which call both the _buffer ones above and the 83 * driver supplied ones to do the drawing 84 */ 85 static void vcons_copycols(void *, int, int, int, int); 86 static void vcons_erasecols(void *, int, int, int, long); 87 static void vcons_copyrows(void *, int, int, int); 88 static void vcons_eraserows(void *, int, int, long); 89 static void vcons_putchar(void *, int, int, u_int, long); 90 static void vcons_cursor(void *, int, int, int); 91 92 /* support for reading/writing text buffers. For wsmoused */ 93 static int vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *); 94 static int vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *); 95 96 static void vcons_lock(struct vcons_screen *); 97 static void vcons_unlock(struct vcons_screen *); 98 99 #ifdef VCONS_SWITCH_ASYNC 100 static void vcons_kthread(void *); 101 #endif 102 103 int 104 vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def, 105 struct wsdisplay_accessops *ao) 106 { 107 108 /* zero out everything so we can rely on untouched fields being 0 */ 109 memset(vd, 0, sizeof(struct vcons_data)); 110 111 vd->cookie = cookie; 112 113 vd->init_screen = vcons_dummy_init_screen; 114 vd->show_screen_cb = NULL; 115 116 /* keep a copy of the accessops that we replace below with our 117 * own wrappers */ 118 vd->ioctl = ao->ioctl; 119 120 /* configure the accessops */ 121 ao->ioctl = vcons_ioctl; 122 ao->alloc_screen = vcons_alloc_screen; 123 ao->free_screen = vcons_free_screen; 124 ao->show_screen = vcons_show_screen; 125 #ifdef WSDISPLAY_SCROLLSUPPORT 126 ao->scroll = vcons_scroll; 127 #endif 128 129 LIST_INIT(&vd->screens); 130 vd->active = NULL; 131 vd->wanted = NULL; 132 vd->currenttype = def; 133 callout_init(&vd->switch_callout, 0); 134 callout_setfunc(&vd->switch_callout, vcons_do_switch, vd); 135 136 /* 137 * a lock to serialize access to the framebuffer. 138 * when switching screens we need to make sure there's no rasops 139 * operation in progress 140 */ 141 #ifdef DIAGNOSTIC 142 vd->switch_poll_count = 0; 143 #endif 144 #ifdef VCONS_SWITCH_ASYNC 145 kthread_create(PRI_NONE, 0, NULL, vcons_kthread, vd, 146 &vd->redraw_thread, "vcons_draw"); 147 #endif 148 return 0; 149 } 150 151 static void 152 vcons_lock(struct vcons_screen *scr) 153 { 154 #ifdef VCONS_PARANOIA 155 int s; 156 157 s = splhigh(); 158 #endif 159 SCREEN_BUSY(scr); 160 #ifdef VCONS_PARANOIA 161 splx(s); 162 #endif 163 } 164 165 static void 166 vcons_unlock(struct vcons_screen *scr) 167 { 168 #ifdef VCONS_PARANOIA 169 int s; 170 171 s = splhigh(); 172 #endif 173 SCREEN_IDLE(scr); 174 #ifdef VCONS_PARANOIA 175 splx(s); 176 #endif 177 #ifdef VCONS_SWITCH_ASYNC 178 wakeup(&scr->scr_vd->done_drawing); 179 #endif 180 } 181 182 static void 183 vcons_dummy_init_screen(void *cookie, 184 struct vcons_screen *scr, int exists, 185 long *defattr) 186 { 187 188 /* 189 * default init_screen() method. 190 * Needs to be overwritten so we bitch and whine in case anyone ends 191 * up in here. 192 */ 193 printf("vcons_init_screen: dummy function called. Your driver is " 194 "supposed to supply a replacement for proper operation\n"); 195 } 196 197 int 198 vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr, 199 int existing, long *defattr) 200 { 201 struct rasops_info *ri = &scr->scr_ri; 202 int cnt, i; 203 204 scr->scr_cookie = vd->cookie; 205 scr->scr_vd = scr->scr_origvd = vd; 206 scr->scr_busy = 0; 207 208 /* 209 * call the driver-supplied init_screen function which is expected 210 * to set up rasops_info, override cursor() and probably others 211 */ 212 vd->init_screen(vd->cookie, scr, existing, defattr); 213 214 /* 215 * save the non virtual console aware rasops and replace them with 216 * our wrappers 217 */ 218 vd->eraserows = ri->ri_ops.eraserows; 219 vd->copyrows = ri->ri_ops.copyrows; 220 vd->erasecols = ri->ri_ops.erasecols; 221 vd->copycols = ri->ri_ops.copycols; 222 vd->putchar = ri->ri_ops.putchar; 223 vd->cursor = ri->ri_ops.cursor; 224 225 ri->ri_ops.eraserows = vcons_eraserows; 226 ri->ri_ops.copyrows = vcons_copyrows; 227 ri->ri_ops.erasecols = vcons_erasecols; 228 ri->ri_ops.copycols = vcons_copycols; 229 ri->ri_ops.putchar = vcons_putchar; 230 ri->ri_ops.cursor = vcons_cursor; 231 ri->ri_hw = scr; 232 233 /* 234 * we allocate both chars and attributes in one chunk, attributes first 235 * because they have the (potentially) bigger alignment 236 */ 237 #ifdef WSDISPLAY_SCROLLSUPPORT 238 cnt = (ri->ri_rows + WSDISPLAY_SCROLLBACK_LINES) * ri->ri_cols; 239 scr->scr_lines_in_buffer = WSDISPLAY_SCROLLBACK_LINES; 240 scr->scr_current_line = 0; 241 scr->scr_line_wanted = 0; 242 scr->scr_offset_to_zero = ri->ri_cols * WSDISPLAY_SCROLLBACK_LINES; 243 scr->scr_current_offset = scr->scr_offset_to_zero; 244 #else 245 cnt = ri->ri_rows * ri->ri_cols; 246 #endif 247 scr->scr_attrs = (long *)malloc(cnt * (sizeof(long) + 248 sizeof(uint16_t)), M_DEVBUF, M_WAITOK); 249 if (scr->scr_attrs == NULL) 250 return ENOMEM; 251 252 scr->scr_chars = (uint16_t *)&scr->scr_attrs[cnt]; 253 254 ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr); 255 scr->scr_defattr = *defattr; 256 257 /* 258 * fill the attribute buffer with *defattr, chars with 0x20 259 * since we don't know if the driver tries to mimic firmware output or 260 * reset everything we do nothing to VRAM here, any driver that feels 261 * the need to clear screen or something will have to do it on its own 262 * Additional screens will start out in the background anyway so 263 * cleaning or not only really affects the initial console screen 264 */ 265 for (i = 0; i < cnt; i++) { 266 scr->scr_attrs[i] = *defattr; 267 scr->scr_chars[i] = 0x20; 268 } 269 270 if(vd->active == NULL) { 271 vd->active = scr; 272 SCREEN_VISIBLE(scr); 273 } 274 275 if (existing) { 276 SCREEN_VISIBLE(scr); 277 vd->active = scr; 278 } else { 279 SCREEN_INVISIBLE(scr); 280 } 281 282 LIST_INSERT_HEAD(&vd->screens, scr, next); 283 return 0; 284 } 285 286 static void 287 vcons_do_switch(void *arg) 288 { 289 struct vcons_data *vd = arg; 290 struct vcons_screen *scr, *oldscr; 291 292 scr = vd->wanted; 293 if (!scr) { 294 printf("vcons_switch_screen: disappeared\n"); 295 vd->switch_cb(vd->switch_cb_arg, EIO, 0); 296 return; 297 } 298 oldscr = vd->active; /* can be NULL! */ 299 300 /* 301 * if there's an old, visible screen we mark it invisible and wait 302 * until it's not busy so we can safely switch 303 */ 304 if (oldscr != NULL) { 305 SCREEN_INVISIBLE(oldscr); 306 if (SCREEN_IS_BUSY(oldscr)) { 307 callout_schedule(&vd->switch_callout, 1); 308 #ifdef DIAGNOSTIC 309 /* bitch if we wait too long */ 310 vd->switch_poll_count++; 311 if (vd->switch_poll_count > 100) { 312 panic("vcons: screen still busy"); 313 } 314 #endif 315 return; 316 } 317 /* invisible screen -> no visible cursor image */ 318 oldscr->scr_ri.ri_flg &= ~RI_CURSOR; 319 #ifdef DIAGNOSTIC 320 vd->switch_poll_count = 0; 321 #endif 322 } 323 324 if (scr == oldscr) 325 return; 326 327 #ifdef DIAGNOSTIC 328 if (SCREEN_IS_VISIBLE(scr)) 329 printf("vcons_switch_screen: already active"); 330 #endif 331 332 #ifdef notyet 333 if (vd->currenttype != type) { 334 vcons_set_screentype(vd, type); 335 vd->currenttype = type; 336 } 337 #endif 338 339 SCREEN_VISIBLE(scr); 340 vd->active = scr; 341 vd->wanted = NULL; 342 343 if (vd->show_screen_cb != NULL) 344 vd->show_screen_cb(scr); 345 346 if ((scr->scr_flags & VCONS_NO_REDRAW) == 0) 347 vcons_redraw_screen(scr); 348 349 if (vd->switch_cb) 350 vd->switch_cb(vd->switch_cb_arg, 0, 0); 351 } 352 353 void 354 vcons_redraw_screen(struct vcons_screen *scr) 355 { 356 uint16_t *charptr = scr->scr_chars; 357 long *attrptr = scr->scr_attrs; 358 struct rasops_info *ri = &scr->scr_ri; 359 int i, j, offset; 360 361 vcons_lock(scr); 362 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) { 363 364 /* 365 * only clear the screen when RI_FULLCLEAR is set since we're 366 * going to overwrite every single character cell anyway 367 */ 368 if (ri->ri_flg & RI_FULLCLEAR) { 369 scr->scr_vd->eraserows(ri, 0, ri->ri_rows, 370 scr->scr_defattr); 371 } 372 373 /* redraw the screen */ 374 #ifdef WSDISPLAY_SCROLLSUPPORT 375 offset = scr->scr_current_offset; 376 #else 377 offset = 0; 378 #endif 379 for (i = 0; i < ri->ri_rows; i++) { 380 for (j = 0; j < ri->ri_cols; j++) { 381 /* 382 * no need to use the wrapper function - we 383 * don't change any characters or attributes 384 * and we already made sure the screen we're 385 * working on is visible 386 */ 387 scr->scr_vd->putchar(ri, i, j, 388 charptr[offset], attrptr[offset]); 389 offset++; 390 } 391 } 392 ri->ri_flg &= ~RI_CURSOR; 393 scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol); 394 } 395 vcons_unlock(scr); 396 } 397 398 static int 399 vcons_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, 400 struct lwp *l) 401 { 402 struct vcons_data *vd = v; 403 int error; 404 405 switch (cmd) { 406 case WSDISPLAYIO_GETWSCHAR: 407 error = vcons_getwschar((struct vcons_screen *)vs, 408 (struct wsdisplay_char *)data); 409 break; 410 411 case WSDISPLAYIO_PUTWSCHAR: 412 error = vcons_putwschar((struct vcons_screen *)vs, 413 (struct wsdisplay_char *)data); 414 break; 415 416 default: 417 if (vd->ioctl != NULL) 418 error = (*vd->ioctl)(v, vs, cmd, data, flag, l); 419 else 420 error = EPASSTHROUGH; 421 } 422 423 return error; 424 } 425 426 static int 427 vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep, 428 int *curxp, int *curyp, long *defattrp) 429 { 430 struct vcons_data *vd = v; 431 struct vcons_screen *scr; 432 int ret; 433 434 scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO); 435 if (scr == NULL) 436 return ENOMEM; 437 438 scr->scr_flags = 0; 439 scr->scr_status = 0; 440 scr->scr_busy = 0; 441 scr->scr_type = type; 442 443 ret = vcons_init_screen(vd, scr, 0, defattrp); 444 if (ret != 0) { 445 free(scr, M_DEVBUF); 446 return ret; 447 } 448 449 if (vd->active == NULL) { 450 SCREEN_VISIBLE(scr); 451 vd->active = scr; 452 vd->currenttype = type; 453 } 454 455 *cookiep = scr; 456 *curxp = scr->scr_ri.ri_ccol; 457 *curyp = scr->scr_ri.ri_crow; 458 return 0; 459 } 460 461 static void 462 vcons_free_screen(void *v, void *cookie) 463 { 464 struct vcons_data *vd = v; 465 struct vcons_screen *scr = cookie; 466 467 vcons_lock(scr); 468 /* there should be no rasops activity here */ 469 470 LIST_REMOVE(scr, next); 471 472 if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) { 473 free(scr->scr_attrs, M_DEVBUF); 474 free(scr, M_DEVBUF); 475 } else { 476 /* 477 * maybe we should just restore the old rasops_info methods 478 * and free the character/attribute buffer here? 479 */ 480 #ifdef VCONS_DEBUG 481 panic("vcons_free_screen: console"); 482 #else 483 printf("vcons_free_screen: console\n"); 484 #endif 485 } 486 487 if (vd->active == scr) 488 vd->active = NULL; 489 } 490 491 static int 492 vcons_show_screen(void *v, void *cookie, int waitok, 493 void (*cb)(void *, int, int), void *cb_arg) 494 { 495 struct vcons_data *vd = v; 496 struct vcons_screen *scr; 497 498 scr = cookie; 499 if (scr == vd->active) 500 return 0; 501 502 vd->wanted = scr; 503 vd->switch_cb = cb; 504 vd->switch_cb_arg = cb_arg; 505 #ifdef VCONS_SWITCH_ASYNC 506 wakeup(&vd->start_drawing); 507 return EAGAIN; 508 #else 509 if (cb) { 510 callout_schedule(&vd->switch_callout, 0); 511 return EAGAIN; 512 } 513 514 vcons_do_switch(vd); 515 return 0; 516 #endif 517 } 518 519 /* wrappers for rasops_info methods */ 520 521 static void 522 vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols) 523 { 524 struct rasops_info *ri = cookie; 525 struct vcons_screen *scr = ri->ri_hw; 526 int from = srccol + row * ri->ri_cols; 527 int to = dstcol + row * ri->ri_cols; 528 529 #ifdef WSDISPLAY_SCROLLSUPPORT 530 int offset; 531 offset = scr->scr_offset_to_zero; 532 533 memmove(&scr->scr_attrs[offset + to], &scr->scr_attrs[offset + from], 534 ncols * sizeof(long)); 535 memmove(&scr->scr_chars[offset + to], &scr->scr_chars[offset + from], 536 ncols * sizeof(uint16_t)); 537 #else 538 memmove(&scr->scr_attrs[to], &scr->scr_attrs[from], 539 ncols * sizeof(long)); 540 memmove(&scr->scr_chars[to], &scr->scr_chars[from], 541 ncols * sizeof(uint16_t)); 542 #endif 543 } 544 545 static void 546 vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols) 547 { 548 struct rasops_info *ri = cookie; 549 struct vcons_screen *scr = ri->ri_hw; 550 551 vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols); 552 553 vcons_lock(scr); 554 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) { 555 scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols); 556 } 557 vcons_unlock(scr); 558 } 559 560 static void 561 vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr) 562 { 563 struct rasops_info *ri = cookie; 564 struct vcons_screen *scr = ri->ri_hw; 565 int start = startcol + row * ri->ri_cols; 566 int end = start + ncols, i; 567 568 #ifdef WSDISPLAY_SCROLLSUPPORT 569 int offset; 570 offset = scr->scr_offset_to_zero; 571 572 for (i = start; i < end; i++) { 573 scr->scr_attrs[offset + i] = fillattr; 574 scr->scr_chars[offset + i] = 0x20; 575 } 576 #else 577 for (i = start; i < end; i++) { 578 scr->scr_attrs[i] = fillattr; 579 scr->scr_chars[i] = 0x20; 580 } 581 #endif 582 } 583 584 static void 585 vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr) 586 { 587 struct rasops_info *ri = cookie; 588 struct vcons_screen *scr = ri->ri_hw; 589 590 vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr); 591 592 vcons_lock(scr); 593 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) { 594 scr->scr_vd->erasecols(cookie, row, startcol, ncols, 595 fillattr); 596 } 597 vcons_unlock(scr); 598 } 599 600 static void 601 vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows) 602 { 603 struct rasops_info *ri = cookie; 604 struct vcons_screen *scr = ri->ri_hw; 605 int from, to, len; 606 607 #ifdef WSDISPLAY_SCROLLSUPPORT 608 int offset; 609 offset = scr->scr_offset_to_zero; 610 611 /* do we need to scroll the back buffer? */ 612 if (dstrow == 0) { 613 from = ri->ri_cols * srcrow; 614 to = ri->ri_cols * dstrow; 615 616 memmove(&scr->scr_attrs[to], &scr->scr_attrs[from], 617 scr->scr_offset_to_zero * sizeof(long)); 618 memmove(&scr->scr_chars[to], &scr->scr_chars[from], 619 scr->scr_offset_to_zero * sizeof(uint16_t)); 620 } 621 from = ri->ri_cols * srcrow + offset; 622 to = ri->ri_cols * dstrow + offset; 623 len = ri->ri_cols * nrows; 624 625 #else 626 from = ri->ri_cols * srcrow; 627 to = ri->ri_cols * dstrow; 628 len = ri->ri_cols * nrows; 629 #endif 630 memmove(&scr->scr_attrs[to], &scr->scr_attrs[from], 631 len * sizeof(long)); 632 memmove(&scr->scr_chars[to], &scr->scr_chars[from], 633 len * sizeof(uint16_t)); 634 } 635 636 static void 637 vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows) 638 { 639 struct rasops_info *ri = cookie; 640 struct vcons_screen *scr = ri->ri_hw; 641 642 vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows); 643 644 vcons_lock(scr); 645 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) { 646 scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows); 647 } 648 vcons_unlock(scr); 649 } 650 651 static void 652 vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr) 653 { 654 struct rasops_info *ri = cookie; 655 struct vcons_screen *scr = ri->ri_hw; 656 int start, end, i; 657 658 #ifdef WSDISPLAY_SCROLLSUPPORT 659 int offset; 660 offset = scr->scr_offset_to_zero; 661 662 start = ri->ri_cols * row + offset; 663 end = ri->ri_cols * (row + nrows) + offset; 664 #else 665 start = ri->ri_cols * row; 666 end = ri->ri_cols * (row + nrows); 667 #endif 668 669 for (i = start; i < end; i++) { 670 scr->scr_attrs[i] = fillattr; 671 scr->scr_chars[i] = 0x20; 672 } 673 } 674 675 static void 676 vcons_eraserows(void *cookie, int row, int nrows, long fillattr) 677 { 678 struct rasops_info *ri = cookie; 679 struct vcons_screen *scr = ri->ri_hw; 680 681 vcons_eraserows_buffer(cookie, row, nrows, fillattr); 682 683 vcons_lock(scr); 684 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) { 685 scr->scr_vd->eraserows(cookie, row, nrows, fillattr); 686 } 687 vcons_unlock(scr); 688 } 689 690 static void 691 vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr) 692 { 693 struct rasops_info *ri = cookie; 694 struct vcons_screen *scr = ri->ri_hw; 695 int pos; 696 697 #ifdef WSDISPLAY_SCROLLSUPPORT 698 int offset; 699 offset = scr->scr_offset_to_zero; 700 701 if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) && 702 (col < ri->ri_cols)) { 703 pos = col + row * ri->ri_cols; 704 scr->scr_attrs[pos + offset] = attr; 705 scr->scr_chars[pos + offset] = c; 706 } 707 #else 708 if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) && 709 (col < ri->ri_cols)) { 710 pos = col + row * ri->ri_cols; 711 scr->scr_attrs[pos] = attr; 712 scr->scr_chars[pos] = c; 713 } 714 #endif 715 } 716 717 static void 718 vcons_putchar(void *cookie, int row, int col, u_int c, long attr) 719 { 720 struct rasops_info *ri = cookie; 721 struct vcons_screen *scr = ri->ri_hw; 722 723 vcons_putchar_buffer(cookie, row, col, c, attr); 724 725 vcons_lock(scr); 726 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) { 727 scr->scr_vd->putchar(cookie, row, col, c, attr); 728 } 729 vcons_unlock(scr); 730 } 731 732 static void 733 vcons_cursor(void *cookie, int on, int row, int col) 734 { 735 struct rasops_info *ri = cookie; 736 struct vcons_screen *scr = ri->ri_hw; 737 738 vcons_lock(scr); 739 if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) { 740 scr->scr_vd->cursor(cookie, on, row, col); 741 } else { 742 scr->scr_ri.ri_crow = row; 743 scr->scr_ri.ri_ccol = col; 744 } 745 vcons_unlock(scr); 746 } 747 748 /* methods to read/write characters via ioctl() */ 749 750 static int 751 vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc) 752 { 753 long attr; 754 struct rasops_info *ri; 755 756 KASSERT(scr != NULL && wsc != NULL); 757 758 ri = &scr->scr_ri; 759 760 if (__predict_false((unsigned int)wsc->col > ri->ri_cols || 761 (unsigned int)wsc->row > ri->ri_rows)) 762 return (EINVAL); 763 764 if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) && 765 (wsc->col < ri->ri_cols)) { 766 767 ri->ri_ops.allocattr(ri, wsc->foreground, wsc->background, 768 wsc->flags, &attr); 769 vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr); 770 #ifdef VCONS_DEBUG 771 printf("vcons_putwschar(%d, %d, %x, %lx\n", wsc->row, wsc->col, 772 wsc->letter, attr); 773 #endif 774 return 0; 775 } else 776 return EINVAL; 777 } 778 779 static int 780 vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc) 781 { 782 int offset; 783 long attr; 784 struct rasops_info *ri; 785 786 KASSERT(scr != NULL && wsc != NULL); 787 788 ri = &scr->scr_ri; 789 790 if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) && 791 (wsc->col < ri->ri_cols)) { 792 793 offset = ri->ri_cols * wsc->row + wsc->col; 794 #ifdef WSDISPLAY_SCROLLSUPPORT 795 offset += scr->scr_offset_to_zero; 796 #endif 797 wsc->letter = scr->scr_chars[offset]; 798 attr = scr->scr_attrs[offset]; 799 800 /* 801 * this is ugly. We need to break up an attribute into colours and 802 * flags but there's no rasops method to do that so we must rely on 803 * the 'canonical' encoding. 804 */ 805 #ifdef VCONS_DEBUG 806 printf("vcons_getwschar: %d, %d, %x, %lx\n", wsc->row, 807 wsc->col, wsc->letter, attr); 808 #endif 809 wsc->foreground = (attr >> 24) & 0xff; 810 wsc->background = (attr >> 16) & 0xff; 811 wsc->flags = attr & 0xff; 812 return 0; 813 } else 814 return EINVAL; 815 } 816 817 #ifdef WSDISPLAY_SCROLLSUPPORT 818 819 static void 820 vcons_scroll(void *cookie, void *vs, int where) 821 { 822 struct vcons_screen *scr = vs; 823 824 if (where == 0) { 825 scr->scr_line_wanted = 0; 826 } else { 827 scr->scr_line_wanted = scr->scr_line_wanted - where; 828 if (scr->scr_line_wanted < 0) 829 scr->scr_line_wanted = 0; 830 if (scr->scr_line_wanted > scr->scr_lines_in_buffer) 831 scr->scr_line_wanted = scr->scr_lines_in_buffer; 832 } 833 834 if (scr->scr_line_wanted != scr->scr_current_line) { 835 836 vcons_do_scroll(scr); 837 } 838 } 839 840 static void 841 vcons_do_scroll(struct vcons_screen *scr) 842 { 843 int dist, from, to, num; 844 int r_offset, r_start; 845 int i, j; 846 847 if (scr->scr_line_wanted == scr->scr_current_line) 848 return; 849 dist = scr->scr_line_wanted - scr->scr_current_line; 850 scr->scr_current_line = scr->scr_line_wanted; 851 scr->scr_current_offset = scr->scr_ri.ri_cols * 852 (scr->scr_lines_in_buffer - scr->scr_current_line); 853 if (abs(dist) >= scr->scr_ri.ri_rows) { 854 vcons_redraw_screen(scr); 855 return; 856 } 857 /* scroll and redraw only what we really have to */ 858 if (dist > 0) { 859 /* we scroll down */ 860 from = 0; 861 to = dist; 862 num = scr->scr_ri.ri_rows - dist; 863 /* now the redraw parameters */ 864 r_offset = scr->scr_current_offset; 865 r_start = 0; 866 } else { 867 /* scrolling up */ 868 to = 0; 869 from = -dist; 870 num = scr->scr_ri.ri_rows + dist; 871 r_offset = scr->scr_current_offset + num * scr->scr_ri.ri_cols; 872 r_start = num; 873 } 874 scr->scr_vd->copyrows(scr, from, to, num); 875 for (i = 0; i < abs(dist); i++) { 876 for (j = 0; j < scr->scr_ri.ri_cols; j++) { 877 scr->scr_vd->putchar(scr, i + r_start, j, 878 scr->scr_chars[r_offset], 879 scr->scr_attrs[r_offset]); 880 r_offset++; 881 } 882 } 883 884 if (scr->scr_line_wanted == 0) { 885 /* this was a reset - need to draw the cursor */ 886 scr->scr_ri.ri_flg &= ~RI_CURSOR; 887 scr->scr_vd->cursor(scr, 1, scr->scr_ri.ri_crow, 888 scr->scr_ri.ri_ccol); 889 } 890 } 891 892 #endif /* WSDISPLAY_SCROLLSUPPORT */ 893 894 /* async drawing using a kernel thread */ 895 896 #ifdef VCONS_SWITCH_ASYNC 897 static void 898 vcons_kthread(void *cookie) 899 { 900 struct vcons_data *vd = cookie; 901 struct vcons_screen *scr; 902 int sec = hz; 903 904 while (1) { 905 906 tsleep(&vd->start_drawing, 0, "vc_idle", sec); 907 if ((vd->wanted != vd->active) && (vd->wanted != NULL)) { 908 /* 909 * we need to switch screens 910 * so first we mark the active screen as invisible 911 * and wait until it's idle 912 */ 913 scr = vd->wanted; 914 SCREEN_INVISIBLE(vd->active); 915 while (SCREEN_IS_BUSY(vd->active)) { 916 917 tsleep(&vd->done_drawing, 0, "vc_wait", sec); 918 } 919 /* 920 * now we mark the wanted screen busy so nobody 921 * messes around while we redraw it 922 */ 923 vd->active = scr; 924 vd->wanted = NULL; 925 SCREEN_VISIBLE(scr); 926 927 if (vd->show_screen_cb != NULL) 928 vd->show_screen_cb(scr); 929 930 if ((scr->scr_flags & VCONS_NO_REDRAW) == 0) 931 vcons_redraw_screen(scr); 932 933 if (vd->switch_cb) 934 vd->switch_cb(vd->switch_cb_arg, 0, 0); 935 } 936 } 937 } 938 #endif /* VCONS_SWITCH_ASYNC */ 939