1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright 2016 Joyent, Inc. 29 */ 30 31 /* 32 * Polled I/O safe ANSI terminal emulator module; 33 * Supporting TERM types 'sun' and 'sun-color, parsing 34 * ANSI x3.64 escape sequences, and the like. (See wscons(7d) 35 * for more information). 36 * 37 * IMPORTANT: 38 * 39 * The functions in this file *must* be able to function in 40 * standalone mode, ie. on a quiesced system. In that state, 41 * access is single threaded, only one CPU is running. 42 * System services are NOT available. 43 * 44 * The following restrictions pertain to every function 45 * in this file: 46 * 47 * - CANNOT use the DDI or LDI interfaces 48 * - CANNOT call system services 49 * - CANNOT use mutexes 50 * - CANNOT wait for interrupts 51 * - CANNOT allocate memory 52 * 53 * All non-static functions in this file which: 54 * - Operates on tems and tem_vt_state 55 * - Not only called from standalone mode, i.e. has 56 * a "calledfrom" argument 57 * should assert this at the beginning: 58 * 59 * ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 60 * called_from == CALLED_FROM_STANDALONE); 61 */ 62 63 #include <sys/types.h> 64 #include <sys/ascii.h> 65 #include <sys/visual_io.h> 66 #include <sys/font.h> 67 #include <sys/tem.h> 68 #include <sys/tem_impl.h> 69 #include <sys/ksynch.h> 70 #include <sys/sysmacros.h> 71 #include <sys/mutex.h> 72 #include <sys/note.h> 73 #include <sys/t_lock.h> 74 75 tem_safe_callbacks_t tem_safe_text_callbacks = { 76 &tem_safe_text_display, 77 &tem_safe_text_copy, 78 &tem_safe_text_cursor, 79 NULL, 80 &tem_safe_text_cls 81 }; 82 tem_safe_callbacks_t tem_safe_pix_callbacks = { 83 &tem_safe_pix_display, 84 &tem_safe_pix_copy, 85 &tem_safe_pix_cursor, 86 &tem_safe_pix_bit2pix, 87 &tem_safe_pix_cls 88 }; 89 90 static void tem_safe_control(struct tem_vt_state *, tem_char_t, 91 cred_t *, enum called_from); 92 static void tem_safe_setparam(struct tem_vt_state *, int, int); 93 static void tem_safe_selgraph(struct tem_vt_state *); 94 static void tem_safe_chkparam(struct tem_vt_state *, tem_char_t, 95 cred_t *, enum called_from); 96 static void tem_safe_getparams(struct tem_vt_state *, tem_char_t, 97 cred_t *, enum called_from); 98 static void tem_safe_outch(struct tem_vt_state *, tem_char_t, 99 cred_t *, enum called_from); 100 static void tem_safe_parse(struct tem_vt_state *, tem_char_t, 101 cred_t *, enum called_from); 102 103 static void tem_safe_new_line(struct tem_vt_state *, 104 cred_t *, enum called_from); 105 static void tem_safe_cr(struct tem_vt_state *); 106 static void tem_safe_lf(struct tem_vt_state *, 107 cred_t *, enum called_from); 108 static void tem_safe_send_data(struct tem_vt_state *, cred_t *, 109 enum called_from); 110 static void tem_safe_cls(struct tem_vt_state *, 111 cred_t *, enum called_from); 112 static void tem_safe_tab(struct tem_vt_state *, 113 cred_t *, enum called_from); 114 static void tem_safe_back_tab(struct tem_vt_state *, 115 cred_t *, enum called_from); 116 static void tem_safe_clear_tabs(struct tem_vt_state *, int); 117 static void tem_safe_set_tab(struct tem_vt_state *); 118 static void tem_safe_mv_cursor(struct tem_vt_state *, int, int, 119 cred_t *, enum called_from); 120 static void tem_safe_shift(struct tem_vt_state *, int, int, 121 cred_t *, enum called_from); 122 static void tem_safe_scroll(struct tem_vt_state *, int, int, 123 int, int, cred_t *, enum called_from); 124 static void tem_safe_clear_chars(struct tem_vt_state *tem, 125 int count, screen_pos_t row, screen_pos_t col, 126 cred_t *credp, enum called_from called_from); 127 static void tem_safe_copy_area(struct tem_vt_state *tem, 128 screen_pos_t s_col, screen_pos_t s_row, 129 screen_pos_t e_col, screen_pos_t e_row, 130 screen_pos_t t_col, screen_pos_t t_row, 131 cred_t *credp, enum called_from called_from); 132 static void tem_safe_image_display(struct tem_vt_state *, uchar_t *, 133 int, int, screen_pos_t, screen_pos_t, 134 cred_t *, enum called_from); 135 static void tem_safe_bell(struct tem_vt_state *tem, 136 enum called_from called_from); 137 static void tem_safe_pix_clear_prom_output(struct tem_vt_state *tem, 138 cred_t *credp, enum called_from called_from); 139 static void tem_safe_get_color(text_color_t *, text_color_t *, term_char_t); 140 141 static void tem_safe_virtual_cls(struct tem_vt_state *, int, screen_pos_t, 142 screen_pos_t); 143 static void tem_safe_virtual_display(struct tem_vt_state *, 144 term_char_t *, int, screen_pos_t, screen_pos_t); 145 static void tem_safe_virtual_copy(struct tem_vt_state *, screen_pos_t, 146 screen_pos_t, screen_pos_t, screen_pos_t, 147 screen_pos_t, screen_pos_t); 148 static void tem_safe_align_cursor(struct tem_vt_state *tem); 149 static void bit_to_pix4(struct tem_vt_state *tem, tem_char_t c, 150 text_color_t fg_color, text_color_t bg_color); 151 static void bit_to_pix8(struct tem_vt_state *tem, tem_char_t c, 152 text_color_t fg_color, text_color_t bg_color); 153 static void bit_to_pix16(struct tem_vt_state *tem, tem_char_t c, 154 text_color_t fg_color, text_color_t bg_color); 155 static void bit_to_pix24(struct tem_vt_state *tem, tem_char_t c, 156 text_color_t fg_color, text_color_t bg_color); 157 static void bit_to_pix32(struct tem_vt_state *tem, tem_char_t c, 158 text_color_t fg_color, text_color_t bg_color); 159 160 #define PIX4TO32(pix4) (uint32_t)( \ 161 cmap4_to_24.red[pix4] << 16 | \ 162 cmap4_to_24.green[pix4] << 8 | \ 163 cmap4_to_24.blue[pix4]) 164 165 #define INVERSE(ch) (ch ^ 0xff) 166 167 #define tem_safe_callback_display (*tems.ts_callbacks->tsc_display) 168 #define tem_safe_callback_copy (*tems.ts_callbacks->tsc_copy) 169 #define tem_safe_callback_cursor (*tems.ts_callbacks->tsc_cursor) 170 #define tem_safe_callback_cls (*tems.ts_callbacks->tsc_cls) 171 #define tem_safe_callback_bit2pix(tem, c) { \ 172 ASSERT(tems.ts_callbacks->tsc_bit2pix != NULL); \ 173 (void) (*tems.ts_callbacks->tsc_bit2pix)((tem), (c));\ 174 } 175 176 void 177 tem_safe_check_first_time( 178 struct tem_vt_state *tem, 179 cred_t *credp, 180 enum called_from called_from) 181 { 182 static int first_time = 1; 183 184 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 185 called_from == CALLED_FROM_STANDALONE); 186 187 /* 188 * Realign the console cursor. We did this in tem_init(). 189 * However, drivers in the console stream may emit additional 190 * messages before we are ready. This causes text overwrite 191 * on the screen. This is a workaround. 192 */ 193 if (!first_time) 194 return; 195 196 first_time = 0; 197 if (tems.ts_display_mode == VIS_TEXT) 198 tem_safe_text_cursor(tem, VIS_GET_CURSOR, credp, called_from); 199 else 200 tem_safe_pix_cursor(tem, VIS_GET_CURSOR, credp, called_from); 201 tem_safe_align_cursor(tem); 202 } 203 204 /* 205 * This entry point handles output requests from restricted contexts like 206 * kmdb, where services like mutexes are not available. This function 207 * is entered when OBP or when a kernel debugger (such as kmdb) 208 * are generating console output. In those cases, power management 209 * concerns are handled by the abort sequence initiation (ie. when 210 * the user hits L1+A or the equivalent to enter OBP or the debugger.). 211 * It is also entered when the kernel is panicing. 212 */ 213 void 214 tem_safe_polled_write( 215 tem_vt_state_t tem_arg, 216 uchar_t *buf, 217 int len) 218 { 219 struct tem_vt_state *tem = (struct tem_vt_state *)tem_arg; 220 221 #ifdef __lock_lint 222 _NOTE(NO_COMPETING_THREADS_NOW) 223 _NOTE(NO_COMPETING_THREADS_AS_SIDE_EFFECT) 224 #endif 225 226 if (!tem->tvs_initialized) { 227 return; 228 } 229 230 tem_safe_check_first_time(tem, kcred, CALLED_FROM_STANDALONE); 231 tem_safe_terminal_emulate(tem, buf, len, NULL, CALLED_FROM_STANDALONE); 232 } 233 234 /* Process partial UTF-8 sequence. */ 235 static void 236 tem_safe_input_partial(struct tem_vt_state *tem, cred_t *credp, 237 enum called_from called_from) 238 { 239 unsigned i; 240 uint8_t c; 241 242 if (tem->tvs_utf8_left == 0) 243 return; 244 245 for (i = 0; i < sizeof (tem->tvs_utf8_partial); i++) { 246 c = (tem->tvs_utf8_partial >> (24 - (i << 3))) & 0xff; 247 if (c != 0) { 248 tem_safe_parse(tem, c, credp, called_from); 249 } 250 } 251 tem->tvs_utf8_left = 0; 252 tem->tvs_utf8_partial = 0; 253 } 254 255 /* 256 * Handle UTF-8 sequences. 257 */ 258 static void 259 tem_safe_input_byte(struct tem_vt_state *tem, uchar_t c, cred_t *credp, 260 enum called_from called_from) 261 { 262 /* 263 * Check for UTF-8 code points. In case of error fall back to 264 * 8-bit code. As we only have 8859-1 fonts for console, this will set 265 * the limits on what chars we actually can display, therefore we 266 * have to return to this code once we have solved the font issue. 267 */ 268 if ((c & 0x80) == 0x00) { 269 /* One-byte sequence. */ 270 tem_safe_input_partial(tem, credp, called_from); 271 tem_safe_parse(tem, c, credp, called_from); 272 return; 273 } 274 if ((c & 0xe0) == 0xc0) { 275 /* Two-byte sequence. */ 276 tem_safe_input_partial(tem, credp, called_from); 277 tem->tvs_utf8_left = 1; 278 tem->tvs_utf8_partial = c; 279 return; 280 } 281 if ((c & 0xf0) == 0xe0) { 282 /* Three-byte sequence. */ 283 tem_safe_input_partial(tem, credp, called_from); 284 tem->tvs_utf8_left = 2; 285 tem->tvs_utf8_partial = c; 286 return; 287 } 288 if ((c & 0xf8) == 0xf0) { 289 /* Four-byte sequence. */ 290 tem_safe_input_partial(tem, credp, called_from); 291 tem->tvs_utf8_left = 3; 292 tem->tvs_utf8_partial = c; 293 return; 294 } 295 if ((c & 0xc0) == 0x80) { 296 /* Invalid state? */ 297 if (tem->tvs_utf8_left == 0) { 298 tem_safe_parse(tem, c, credp, called_from); 299 return; 300 } 301 tem->tvs_utf8_left--; 302 tem->tvs_utf8_partial = (tem->tvs_utf8_partial << 8) | c; 303 if (tem->tvs_utf8_left == 0) { 304 tem_char_t v, u; 305 uint8_t b; 306 307 /* 308 * Transform the sequence of 2 to 4 bytes to 309 * unicode number. 310 */ 311 v = 0; 312 u = tem->tvs_utf8_partial; 313 b = (u >> 24) & 0xff; 314 if (b != 0) { /* Four-byte sequence */ 315 v = b & 0x07; 316 b = (u >> 16) & 0xff; 317 v = (v << 6) | (b & 0x3f); 318 b = (u >> 8) & 0xff; 319 v = (v << 6) | (b & 0x3f); 320 b = u & 0xff; 321 v = (v << 6) | (b & 0x3f); 322 } else if ((b = (u >> 16) & 0xff) != 0) { 323 v = b & 0x0f; /* Three-byte sequence */ 324 b = (u >> 8) & 0xff; 325 v = (v << 6) | (b & 0x3f); 326 b = u & 0xff; 327 v = (v << 6) | (b & 0x3f); 328 } else if ((b = (u >> 8) & 0xff) != 0) { 329 v = b & 0x1f; /* Two-byte sequence */ 330 b = u & 0xff; 331 v = (v << 6) | (b & 0x3f); 332 } 333 334 tem_safe_parse(tem, v, credp, called_from); 335 tem->tvs_utf8_partial = 0; 336 } 337 return; 338 } 339 /* Anything left is illegal in UTF-8 sequence. */ 340 tem_safe_input_partial(tem, credp, called_from); 341 tem_safe_parse(tem, c, credp, called_from); 342 } 343 344 /* 345 * This is the main entry point into the terminal emulator. 346 * 347 * For each data message coming downstream, ANSI assumes that it is composed 348 * of ASCII characters, which are treated as a byte-stream input to the 349 * parsing state machine. All data is parsed immediately -- there is 350 * no enqueing. 351 */ 352 void 353 tem_safe_terminal_emulate( 354 struct tem_vt_state *tem, 355 uchar_t *buf, 356 int len, 357 cred_t *credp, 358 enum called_from called_from) 359 { 360 361 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 362 called_from == CALLED_FROM_STANDALONE); 363 364 if (tem->tvs_isactive) 365 tem_safe_callback_cursor(tem, 366 VIS_HIDE_CURSOR, credp, called_from); 367 368 for (; len > 0; len--, buf++) 369 tem_safe_input_byte(tem, *buf, credp, called_from); 370 371 /* 372 * Send the data we just got to the framebuffer. 373 */ 374 tem_safe_send_data(tem, credp, called_from); 375 376 if (tem->tvs_isactive) 377 tem_safe_callback_cursor(tem, 378 VIS_DISPLAY_CURSOR, credp, called_from); 379 } 380 381 /* 382 * Display an rectangular image on the frame buffer using the 383 * mechanism appropriate for the system state being called 384 * from quiesced or normal (ie. use polled I/O vs. layered ioctls) 385 */ 386 static void 387 tems_safe_display(struct vis_consdisplay *pda, cred_t *credp, 388 enum called_from called_from) 389 { 390 if (called_from == CALLED_FROM_STANDALONE) 391 tems.ts_fb_polledio->display(tems.ts_fb_polledio->arg, pda); 392 else 393 tems_display_layered(pda, credp); 394 } 395 396 /* 397 * Copy a rectangle from one location to another on the frame buffer 398 * using the mechanism appropriate for the system state being called 399 * from, quiesced or normal (ie. use polled I/O vs. layered ioctls) 400 */ 401 void 402 tems_safe_copy(struct vis_conscopy *pca, cred_t *credp, 403 enum called_from called_from) 404 { 405 if (called_from == CALLED_FROM_STANDALONE) 406 tems.ts_fb_polledio->copy(tems.ts_fb_polledio->arg, pca); 407 else 408 tems_copy_layered(pca, credp); 409 } 410 411 /* 412 * Display or hide a rectangular block text cursor of a specificsize 413 * at a specific location on frame buffer* using the mechanism 414 * appropriate for the system state being called from, quisced or 415 * normal (ie. use polled I/O vs. layered ioctls). 416 */ 417 static void 418 tems_safe_cursor(struct vis_conscursor *pca, cred_t *credp, 419 enum called_from called_from) 420 { 421 if (called_from == CALLED_FROM_STANDALONE) 422 tems.ts_fb_polledio->cursor(tems.ts_fb_polledio->arg, pca); 423 else 424 tems_cursor_layered(pca, credp); 425 } 426 427 /* 428 * send the appropriate control message or set state based on the 429 * value of the control character ch 430 */ 431 432 static void 433 tem_safe_control(struct tem_vt_state *tem, tem_char_t ch, cred_t *credp, 434 enum called_from called_from) 435 { 436 tem->tvs_state = A_STATE_START; 437 switch (ch) { 438 case A_BEL: 439 tem_safe_bell(tem, called_from); 440 break; 441 442 case A_BS: 443 tem_safe_mv_cursor(tem, 444 tem->tvs_c_cursor.row, 445 tem->tvs_c_cursor.col - 1, 446 credp, called_from); 447 break; 448 449 case A_HT: 450 tem_safe_tab(tem, credp, called_from); 451 break; 452 453 case A_NL: 454 /* 455 * tem_safe_send_data(tem, credp, called_from); 456 * tem_safe_new_line(tem, credp, called_from); 457 * break; 458 */ 459 460 case A_VT: 461 tem_safe_send_data(tem, credp, called_from); 462 tem_safe_lf(tem, credp, called_from); 463 break; 464 465 case A_FF: 466 tem_safe_send_data(tem, credp, called_from); 467 tem_safe_cls(tem, credp, called_from); 468 break; 469 470 case A_CR: 471 tem_safe_send_data(tem, credp, called_from); 472 tem_safe_cr(tem); 473 break; 474 475 case A_ESC: 476 tem->tvs_state = A_STATE_ESC; 477 break; 478 479 case A_CSI: 480 { 481 int i; 482 tem->tvs_curparam = 0; 483 tem->tvs_paramval = 0; 484 tem->tvs_gotparam = B_FALSE; 485 /* clear the parameters */ 486 for (i = 0; i < TEM_MAXPARAMS; i++) 487 tem->tvs_params[i] = -1; 488 tem->tvs_state = A_STATE_CSI; 489 } 490 break; 491 492 case A_GS: 493 tem_safe_back_tab(tem, credp, called_from); 494 break; 495 496 default: 497 break; 498 } 499 } 500 501 502 /* 503 * if parameters [0..count - 1] are not set, set them to the value 504 * of newparam. 505 */ 506 507 static void 508 tem_safe_setparam(struct tem_vt_state *tem, int count, int newparam) 509 { 510 int i; 511 512 for (i = 0; i < count; i++) { 513 if (tem->tvs_params[i] == -1) 514 tem->tvs_params[i] = newparam; 515 } 516 } 517 518 519 /* 520 * select graphics mode based on the param vals stored in a_params 521 */ 522 static void 523 tem_safe_selgraph(struct tem_vt_state *tem) 524 { 525 int curparam; 526 int count = 0; 527 int param; 528 529 tem->tvs_state = A_STATE_START; 530 531 curparam = tem->tvs_curparam; 532 do { 533 param = tem->tvs_params[count]; 534 535 switch (param) { 536 case -1: 537 case 0: 538 /* reset to initial normal settings */ 539 tem->tvs_fg_color = tems.ts_init_color.fg_color; 540 tem->tvs_bg_color = tems.ts_init_color.bg_color; 541 tem->tvs_flags = tems.ts_init_color.a_flags; 542 break; 543 544 case 1: /* Bold Intense */ 545 tem->tvs_flags |= TEM_ATTR_BOLD; 546 break; 547 548 case 2: /* Faint Intense */ 549 tem->tvs_flags &= ~TEM_ATTR_BOLD; 550 break; 551 552 case 4: /* Underline */ 553 tem->tvs_flags |= TEM_ATTR_UNDERLINE; 554 break; 555 case 5: /* Blink */ 556 tem->tvs_flags |= TEM_ATTR_BLINK; 557 break; 558 559 case 7: /* Reverse video */ 560 if (tem->tvs_flags & TEM_ATTR_SCREEN_REVERSE) { 561 tem->tvs_flags &= ~TEM_ATTR_REVERSE; 562 } else { 563 tem->tvs_flags |= TEM_ATTR_REVERSE; 564 } 565 break; 566 567 case 22: /* Remove Bold */ 568 tem->tvs_flags &= ~TEM_ATTR_BOLD; 569 break; 570 571 case 24: /* Remove Underline */ 572 tem->tvs_flags &= ~TEM_ATTR_UNDERLINE; 573 break; 574 575 case 25: /* Remove Blink */ 576 tem->tvs_flags &= ~TEM_ATTR_BLINK; 577 break; 578 579 case 27: /* Remove Reverse */ 580 if (tem->tvs_flags & TEM_ATTR_SCREEN_REVERSE) { 581 tem->tvs_flags |= TEM_ATTR_REVERSE; 582 } else { 583 tem->tvs_flags &= ~TEM_ATTR_REVERSE; 584 } 585 break; 586 587 case 30: /* black (grey) foreground */ 588 case 31: /* red (light red) foreground */ 589 case 32: /* green (light green) foreground */ 590 case 33: /* brown (yellow) foreground */ 591 case 34: /* blue (light blue) foreground */ 592 case 35: /* magenta (light magenta) foreground */ 593 case 36: /* cyan (light cyan) foreground */ 594 case 37: /* white (bright white) foreground */ 595 tem->tvs_fg_color = param - 30; 596 tem->tvs_flags &= ~TEM_ATTR_BRIGHT_FG; 597 break; 598 599 case 39: 600 /* 601 * Reset the foreground colour and brightness. 602 */ 603 tem->tvs_fg_color = tems.ts_init_color.fg_color; 604 if (tems.ts_init_color.a_flags & TEM_ATTR_BRIGHT_FG) 605 tem->tvs_flags |= TEM_ATTR_BRIGHT_FG; 606 else 607 tem->tvs_flags &= ~TEM_ATTR_BRIGHT_FG; 608 break; 609 610 case 40: /* black (grey) background */ 611 case 41: /* red (light red) background */ 612 case 42: /* green (light green) background */ 613 case 43: /* brown (yellow) background */ 614 case 44: /* blue (light blue) background */ 615 case 45: /* magenta (light magenta) background */ 616 case 46: /* cyan (light cyan) background */ 617 case 47: /* white (bright white) background */ 618 tem->tvs_bg_color = param - 40; 619 tem->tvs_flags &= ~TEM_ATTR_BRIGHT_BG; 620 break; 621 622 case 49: 623 /* 624 * Reset the background colour and brightness. 625 */ 626 tem->tvs_bg_color = tems.ts_init_color.bg_color; 627 if (tems.ts_init_color.a_flags & TEM_ATTR_BRIGHT_BG) 628 tem->tvs_flags |= TEM_ATTR_BRIGHT_BG; 629 else 630 tem->tvs_flags &= ~TEM_ATTR_BRIGHT_BG; 631 break; 632 633 case 90: /* black (grey) foreground */ 634 case 91: /* red (light red) foreground */ 635 case 92: /* green (light green) foreground */ 636 case 93: /* brown (yellow) foreground */ 637 case 94: /* blue (light blue) foreground */ 638 case 95: /* magenta (light magenta) foreground */ 639 case 96: /* cyan (light cyan) foreground */ 640 case 97: /* white (bright white) foreground */ 641 tem->tvs_fg_color = param - 90; 642 tem->tvs_flags |= TEM_ATTR_BRIGHT_FG; 643 break; 644 645 case 100: /* black (grey) background */ 646 case 101: /* red (light red) background */ 647 case 102: /* green (light green) background */ 648 case 103: /* brown (yellow) background */ 649 case 104: /* blue (light blue) background */ 650 case 105: /* magenta (light magenta) background */ 651 case 106: /* cyan (light cyan) background */ 652 case 107: /* white (bright white) background */ 653 tem->tvs_bg_color = param - 100; 654 tem->tvs_flags |= TEM_ATTR_BRIGHT_BG; 655 break; 656 657 default: 658 break; 659 } 660 count++; 661 curparam--; 662 663 } while (curparam > 0); 664 } 665 666 /* 667 * perform the appropriate action for the escape sequence 668 * 669 * General rule: This code does not validate the arguments passed. 670 * It assumes that the next lower level will do so. 671 */ 672 static void 673 tem_safe_chkparam(struct tem_vt_state *tem, tem_char_t ch, cred_t *credp, 674 enum called_from called_from) 675 { 676 int i; 677 int row; 678 int col; 679 680 ASSERT((called_from == CALLED_FROM_STANDALONE) || 681 MUTEX_HELD(&tem->tvs_lock)); 682 683 row = tem->tvs_c_cursor.row; 684 col = tem->tvs_c_cursor.col; 685 686 switch (ch) { 687 688 case 'm': /* select terminal graphics mode */ 689 tem_safe_send_data(tem, credp, called_from); 690 tem_safe_selgraph(tem); 691 break; 692 693 case '@': /* insert char */ 694 tem_safe_setparam(tem, 1, 1); 695 tem_safe_shift(tem, tem->tvs_params[0], TEM_SHIFT_RIGHT, 696 credp, called_from); 697 break; 698 699 case 'A': /* cursor up */ 700 tem_safe_setparam(tem, 1, 1); 701 tem_safe_mv_cursor(tem, row - tem->tvs_params[0], col, 702 credp, called_from); 703 break; 704 705 case 'd': /* VPA - vertical position absolute */ 706 tem_safe_setparam(tem, 1, 1); 707 tem_safe_mv_cursor(tem, tem->tvs_params[0] - 1, col, 708 credp, called_from); 709 break; 710 711 case 'e': /* VPR - vertical position relative */ 712 case 'B': /* cursor down */ 713 tem_safe_setparam(tem, 1, 1); 714 tem_safe_mv_cursor(tem, row + tem->tvs_params[0], col, 715 credp, called_from); 716 break; 717 718 case 'a': /* HPR - horizontal position relative */ 719 case 'C': /* cursor right */ 720 tem_safe_setparam(tem, 1, 1); 721 tem_safe_mv_cursor(tem, row, col + tem->tvs_params[0], 722 credp, called_from); 723 break; 724 725 case '`': /* HPA - horizontal position absolute */ 726 tem_safe_setparam(tem, 1, 1); 727 tem_safe_mv_cursor(tem, row, tem->tvs_params[0] - 1, 728 credp, called_from); 729 break; 730 731 case 'D': /* cursor left */ 732 tem_safe_setparam(tem, 1, 1); 733 tem_safe_mv_cursor(tem, row, col - tem->tvs_params[0], 734 credp, called_from); 735 break; 736 737 case 'E': /* CNL cursor next line */ 738 tem_safe_setparam(tem, 1, 1); 739 tem_safe_mv_cursor(tem, row + tem->tvs_params[0], 0, 740 credp, called_from); 741 break; 742 743 case 'F': /* CPL cursor previous line */ 744 tem_safe_setparam(tem, 1, 1); 745 tem_safe_mv_cursor(tem, row - tem->tvs_params[0], 0, 746 credp, called_from); 747 break; 748 749 case 'G': /* cursor horizontal position */ 750 tem_safe_setparam(tem, 1, 1); 751 tem_safe_mv_cursor(tem, row, tem->tvs_params[0] - 1, 752 credp, called_from); 753 break; 754 755 case 'g': /* clear tabs */ 756 tem_safe_setparam(tem, 1, 0); 757 tem_safe_clear_tabs(tem, tem->tvs_params[0]); 758 break; 759 760 case 'f': /* HVP Horizontal and Vertical Position */ 761 case 'H': /* CUP position cursor */ 762 tem_safe_setparam(tem, 2, 1); 763 tem_safe_mv_cursor(tem, 764 tem->tvs_params[0] - 1, 765 tem->tvs_params[1] - 1, 766 credp, called_from); 767 break; 768 769 case 'I': /* CHT - Cursor Horizontal Tab */ 770 /* Not implemented */ 771 break; 772 773 case 'J': /* ED - Erase in Display */ 774 tem_safe_send_data(tem, credp, called_from); 775 tem_safe_setparam(tem, 1, 0); 776 switch (tem->tvs_params[0]) { 777 case 0: 778 /* erase cursor to end of screen */ 779 /* FIRST erase cursor to end of line */ 780 tem_safe_clear_chars(tem, 781 tems.ts_c_dimension.width - 782 tem->tvs_c_cursor.col, 783 tem->tvs_c_cursor.row, 784 tem->tvs_c_cursor.col, credp, called_from); 785 786 /* THEN erase lines below the cursor */ 787 for (row = tem->tvs_c_cursor.row + 1; 788 row < tems.ts_c_dimension.height; 789 row++) { 790 tem_safe_clear_chars(tem, 791 tems.ts_c_dimension.width, 792 row, 0, credp, called_from); 793 } 794 break; 795 796 case 1: 797 /* erase beginning of screen to cursor */ 798 /* FIRST erase lines above the cursor */ 799 for (row = 0; 800 row < tem->tvs_c_cursor.row; 801 row++) { 802 tem_safe_clear_chars(tem, 803 tems.ts_c_dimension.width, 804 row, 0, credp, called_from); 805 } 806 /* THEN erase beginning of line to cursor */ 807 tem_safe_clear_chars(tem, 808 tem->tvs_c_cursor.col + 1, 809 tem->tvs_c_cursor.row, 810 0, credp, called_from); 811 break; 812 813 case 2: 814 /* erase whole screen */ 815 for (row = 0; 816 row < tems.ts_c_dimension.height; 817 row++) { 818 tem_safe_clear_chars(tem, 819 tems.ts_c_dimension.width, 820 row, 0, credp, called_from); 821 } 822 break; 823 } 824 break; 825 826 case 'K': /* EL - Erase in Line */ 827 tem_safe_send_data(tem, credp, called_from); 828 tem_safe_setparam(tem, 1, 0); 829 switch (tem->tvs_params[0]) { 830 case 0: 831 /* erase cursor to end of line */ 832 tem_safe_clear_chars(tem, 833 (tems.ts_c_dimension.width - 834 tem->tvs_c_cursor.col), 835 tem->tvs_c_cursor.row, 836 tem->tvs_c_cursor.col, 837 credp, called_from); 838 break; 839 840 case 1: 841 /* erase beginning of line to cursor */ 842 tem_safe_clear_chars(tem, 843 tem->tvs_c_cursor.col + 1, 844 tem->tvs_c_cursor.row, 845 0, credp, called_from); 846 break; 847 848 case 2: 849 /* erase whole line */ 850 tem_safe_clear_chars(tem, 851 tems.ts_c_dimension.width, 852 tem->tvs_c_cursor.row, 853 0, credp, called_from); 854 break; 855 } 856 break; 857 858 case 'L': /* insert line */ 859 tem_safe_send_data(tem, credp, called_from); 860 tem_safe_setparam(tem, 1, 1); 861 tem_safe_scroll(tem, 862 tem->tvs_c_cursor.row, 863 tems.ts_c_dimension.height - 1, 864 tem->tvs_params[0], TEM_SCROLL_DOWN, 865 credp, called_from); 866 break; 867 868 case 'M': /* delete line */ 869 tem_safe_send_data(tem, credp, called_from); 870 tem_safe_setparam(tem, 1, 1); 871 tem_safe_scroll(tem, 872 tem->tvs_c_cursor.row, 873 tems.ts_c_dimension.height - 1, 874 tem->tvs_params[0], TEM_SCROLL_UP, 875 credp, called_from); 876 break; 877 878 case 'P': /* DCH - delete char */ 879 tem_safe_setparam(tem, 1, 1); 880 tem_safe_shift(tem, tem->tvs_params[0], TEM_SHIFT_LEFT, 881 credp, called_from); 882 break; 883 884 case 'S': /* scroll up */ 885 tem_safe_send_data(tem, credp, called_from); 886 tem_safe_setparam(tem, 1, 1); 887 tem_safe_scroll(tem, 0, 888 tems.ts_c_dimension.height - 1, 889 tem->tvs_params[0], TEM_SCROLL_UP, 890 credp, called_from); 891 break; 892 893 case 'T': /* scroll down */ 894 tem_safe_send_data(tem, credp, called_from); 895 tem_safe_setparam(tem, 1, 1); 896 tem_safe_scroll(tem, 0, 897 tems.ts_c_dimension.height - 1, 898 tem->tvs_params[0], TEM_SCROLL_DOWN, 899 credp, called_from); 900 break; 901 902 case 'X': /* erase char */ 903 tem_safe_setparam(tem, 1, 1); 904 tem_safe_clear_chars(tem, 905 tem->tvs_params[0], 906 tem->tvs_c_cursor.row, 907 tem->tvs_c_cursor.col, 908 credp, called_from); 909 break; 910 911 case 'Z': /* cursor backward tabulation */ 912 tem_safe_setparam(tem, 1, 1); 913 914 /* 915 * Rule exception - We do sanity checking here. 916 * 917 * Restrict the count to a sane value to keep from 918 * looping for a long time. There can't be more than one 919 * tab stop per column, so use that as a limit. 920 */ 921 if (tem->tvs_params[0] > tems.ts_c_dimension.width) 922 tem->tvs_params[0] = tems.ts_c_dimension.width; 923 924 for (i = 0; i < tem->tvs_params[0]; i++) 925 tem_safe_back_tab(tem, credp, called_from); 926 break; 927 } 928 tem->tvs_state = A_STATE_START; 929 } 930 931 932 /* 933 * Gather the parameters of an ANSI escape sequence 934 */ 935 static void 936 tem_safe_getparams(struct tem_vt_state *tem, tem_char_t ch, 937 cred_t *credp, enum called_from called_from) 938 { 939 ASSERT((called_from == CALLED_FROM_STANDALONE) || 940 MUTEX_HELD(&tem->tvs_lock)); 941 942 if (ch >= '0' && ch <= '9') { 943 tem->tvs_paramval = ((tem->tvs_paramval * 10) + (ch - '0')); 944 tem->tvs_gotparam = B_TRUE; /* Remember got parameter */ 945 return; /* Return immediately */ 946 } else if (tem->tvs_state == A_STATE_CSI_EQUAL || 947 tem->tvs_state == A_STATE_CSI_QMARK) { 948 tem->tvs_state = A_STATE_START; 949 } else { 950 if (tem->tvs_curparam < TEM_MAXPARAMS) { 951 if (tem->tvs_gotparam) { 952 /* get the parameter value */ 953 tem->tvs_params[tem->tvs_curparam] = 954 tem->tvs_paramval; 955 } 956 tem->tvs_curparam++; 957 } 958 959 if (ch == ';') { 960 /* Restart parameter search */ 961 tem->tvs_gotparam = B_FALSE; 962 tem->tvs_paramval = 0; /* No parame value yet */ 963 } else { 964 /* Handle escape sequence */ 965 tem_safe_chkparam(tem, ch, credp, called_from); 966 } 967 } 968 } 969 970 /* 971 * Add character to internal buffer. 972 * When its full, send it to the next layer. 973 */ 974 975 static void 976 tem_safe_outch(struct tem_vt_state *tem, tem_char_t ch, 977 cred_t *credp, enum called_from called_from) 978 { 979 text_color_t fg; 980 text_color_t bg; 981 text_attr_t attr; 982 983 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 984 called_from == CALLED_FROM_STANDALONE); 985 986 /* buffer up the character until later */ 987 tem_safe_get_attr(tem, &fg, &bg, &attr, TEM_ATTR_REVERSE); 988 tem->tvs_outbuf[tem->tvs_outindex].tc_char = ch | TEM_ATTR(attr); 989 tem->tvs_outbuf[tem->tvs_outindex].tc_fg_color = fg; 990 tem->tvs_outbuf[tem->tvs_outindex].tc_bg_color = bg; 991 tem->tvs_outindex++; 992 tem->tvs_c_cursor.col++; 993 if (tem->tvs_c_cursor.col >= tems.ts_c_dimension.width) { 994 tem_safe_send_data(tem, credp, called_from); 995 tem_safe_new_line(tem, credp, called_from); 996 } 997 } 998 999 static void 1000 tem_safe_new_line(struct tem_vt_state *tem, 1001 cred_t *credp, enum called_from called_from) 1002 { 1003 tem_safe_cr(tem); 1004 tem_safe_lf(tem, credp, called_from); 1005 } 1006 1007 static void 1008 tem_safe_cr(struct tem_vt_state *tem) 1009 { 1010 tem->tvs_c_cursor.col = 0; 1011 tem_safe_align_cursor(tem); 1012 } 1013 1014 static void 1015 tem_safe_lf(struct tem_vt_state *tem, 1016 cred_t *credp, enum called_from called_from) 1017 { 1018 int row; 1019 1020 ASSERT((called_from == CALLED_FROM_STANDALONE) || 1021 MUTEX_HELD(&tem->tvs_lock)); 1022 1023 /* 1024 * Sanity checking notes: 1025 * . a_nscroll was validated when it was set. 1026 * . Regardless of that, tem_safe_scroll and tem_safe_mv_cursor 1027 * will prevent anything bad from happening. 1028 */ 1029 row = tem->tvs_c_cursor.row + 1; 1030 1031 if (row >= tems.ts_c_dimension.height) { 1032 if (tem->tvs_nscroll != 0) { 1033 tem_safe_scroll(tem, 0, 1034 tems.ts_c_dimension.height - 1, 1035 tem->tvs_nscroll, TEM_SCROLL_UP, 1036 credp, called_from); 1037 row = tems.ts_c_dimension.height - 1038 tem->tvs_nscroll; 1039 } else { /* no scroll */ 1040 /* 1041 * implement Esc[#r when # is zero. This means no 1042 * scroll but just return cursor to top of screen, 1043 * do not clear screen. 1044 */ 1045 row = 0; 1046 } 1047 } 1048 1049 tem_safe_mv_cursor(tem, row, tem->tvs_c_cursor.col, 1050 credp, called_from); 1051 1052 if (tem->tvs_nscroll == 0) { 1053 /* erase rest of cursor line */ 1054 tem_safe_clear_chars(tem, 1055 tems.ts_c_dimension.width - 1056 tem->tvs_c_cursor.col, 1057 tem->tvs_c_cursor.row, 1058 tem->tvs_c_cursor.col, 1059 credp, called_from); 1060 1061 } 1062 1063 tem_safe_align_cursor(tem); 1064 } 1065 1066 static void 1067 tem_safe_send_data(struct tem_vt_state *tem, cred_t *credp, 1068 enum called_from called_from) 1069 { 1070 ASSERT((called_from == CALLED_FROM_STANDALONE) || 1071 MUTEX_HELD(&tem->tvs_lock)); 1072 1073 if (tem->tvs_outindex == 0) { 1074 tem_safe_align_cursor(tem); 1075 return; 1076 } 1077 1078 tem_safe_virtual_display(tem, 1079 tem->tvs_outbuf, tem->tvs_outindex, 1080 tem->tvs_s_cursor.row, tem->tvs_s_cursor.col); 1081 1082 if (tem->tvs_isactive) { 1083 /* 1084 * Call the primitive to render this data. 1085 */ 1086 tem_safe_callback_display(tem, 1087 tem->tvs_outbuf, tem->tvs_outindex, 1088 tem->tvs_s_cursor.row, tem->tvs_s_cursor.col, 1089 credp, called_from); 1090 } 1091 1092 tem->tvs_outindex = 0; 1093 1094 tem_safe_align_cursor(tem); 1095 } 1096 1097 1098 /* 1099 * We have just done something to the current output point. Reset the start 1100 * point for the buffered data in a_outbuf. There shouldn't be any data 1101 * buffered yet. 1102 */ 1103 static void 1104 tem_safe_align_cursor(struct tem_vt_state *tem) 1105 { 1106 tem->tvs_s_cursor.row = tem->tvs_c_cursor.row; 1107 tem->tvs_s_cursor.col = tem->tvs_c_cursor.col; 1108 } 1109 1110 /* 1111 * State machine parser based on the current state and character input 1112 * major terminations are to control character or normal character 1113 */ 1114 1115 static void 1116 tem_safe_parse(struct tem_vt_state *tem, tem_char_t ch, 1117 cred_t *credp, enum called_from called_from) 1118 { 1119 int i; 1120 1121 ASSERT((called_from == CALLED_FROM_STANDALONE) || 1122 MUTEX_HELD(&tem->tvs_lock)); 1123 1124 if (tem->tvs_state == A_STATE_START) { /* Normal state? */ 1125 if (ch == A_CSI || ch == A_ESC || ch < ' ') { 1126 /* Control */ 1127 tem_safe_control(tem, ch, credp, called_from); 1128 } else { 1129 /* Display */ 1130 tem_safe_outch(tem, ch, credp, called_from); 1131 } 1132 return; 1133 } 1134 1135 /* In <ESC> sequence */ 1136 if (tem->tvs_state != A_STATE_ESC) { /* Need to get parameters? */ 1137 if (tem->tvs_state != A_STATE_CSI) { 1138 tem_safe_getparams(tem, ch, credp, called_from); 1139 return; 1140 } 1141 1142 switch (ch) { 1143 case '?': 1144 tem->tvs_state = A_STATE_CSI_QMARK; 1145 return; 1146 case '=': 1147 tem->tvs_state = A_STATE_CSI_EQUAL; 1148 return; 1149 case 's': 1150 /* 1151 * As defined below, this sequence 1152 * saves the cursor. However, Sun 1153 * defines ESC[s as reset. We resolved 1154 * the conflict by selecting reset as it 1155 * is exported in the termcap file for 1156 * sun-mon, while the "save cursor" 1157 * definition does not exist anywhere in 1158 * /etc/termcap. 1159 * However, having no coherent 1160 * definition of reset, we have not 1161 * implemented it. 1162 */ 1163 1164 /* 1165 * Original code 1166 * tem->tvs_r_cursor.row = tem->tvs_c_cursor.row; 1167 * tem->tvs_r_cursor.col = tem->tvs_c_cursor.col; 1168 * tem->tvs_state = A_STATE_START; 1169 */ 1170 1171 tem->tvs_state = A_STATE_START; 1172 return; 1173 case 'u': 1174 tem_safe_mv_cursor(tem, tem->tvs_r_cursor.row, 1175 tem->tvs_r_cursor.col, credp, called_from); 1176 tem->tvs_state = A_STATE_START; 1177 return; 1178 case 'p': /* sunbow */ 1179 tem_safe_send_data(tem, credp, called_from); 1180 /* 1181 * Don't set anything if we are 1182 * already as we want to be. 1183 */ 1184 if (tem->tvs_flags & TEM_ATTR_SCREEN_REVERSE) { 1185 tem->tvs_flags &= ~TEM_ATTR_SCREEN_REVERSE; 1186 /* 1187 * If we have switched the characters to be the 1188 * inverse from the screen, then switch them as 1189 * well to keep them the inverse of the screen. 1190 */ 1191 if (tem->tvs_flags & TEM_ATTR_REVERSE) 1192 tem->tvs_flags &= ~TEM_ATTR_REVERSE; 1193 else 1194 tem->tvs_flags |= TEM_ATTR_REVERSE; 1195 } 1196 tem_safe_cls(tem, credp, called_from); 1197 tem->tvs_state = A_STATE_START; 1198 return; 1199 case 'q': /* sunwob */ 1200 tem_safe_send_data(tem, credp, called_from); 1201 /* 1202 * Don't set anything if we are 1203 * already where as we want to be. 1204 */ 1205 if (!(tem->tvs_flags & TEM_ATTR_SCREEN_REVERSE)) { 1206 tem->tvs_flags |= TEM_ATTR_SCREEN_REVERSE; 1207 /* 1208 * If we have switched the characters to be the 1209 * inverse from the screen, then switch them as 1210 * well to keep them the inverse of the screen. 1211 */ 1212 if (!(tem->tvs_flags & TEM_ATTR_REVERSE)) 1213 tem->tvs_flags |= TEM_ATTR_REVERSE; 1214 else 1215 tem->tvs_flags &= ~TEM_ATTR_REVERSE; 1216 } 1217 1218 tem_safe_cls(tem, credp, called_from); 1219 tem->tvs_state = A_STATE_START; 1220 return; 1221 case 'r': /* sunscrl */ 1222 /* 1223 * Rule exception: check for validity here. 1224 */ 1225 tem->tvs_nscroll = tem->tvs_paramval; 1226 if (tem->tvs_nscroll > tems.ts_c_dimension.height) 1227 tem->tvs_nscroll = tems.ts_c_dimension.height; 1228 if (tem->tvs_nscroll < 0) 1229 tem->tvs_nscroll = 1; 1230 tem->tvs_state = A_STATE_START; 1231 return; 1232 default: 1233 tem_safe_getparams(tem, ch, credp, called_from); 1234 return; 1235 } 1236 } 1237 1238 /* Previous char was <ESC> */ 1239 if (ch == '[') { 1240 tem->tvs_curparam = 0; 1241 tem->tvs_paramval = 0; 1242 tem->tvs_gotparam = B_FALSE; 1243 /* clear the parameters */ 1244 for (i = 0; i < TEM_MAXPARAMS; i++) 1245 tem->tvs_params[i] = -1; 1246 tem->tvs_state = A_STATE_CSI; 1247 } else if (ch == 'Q') { /* <ESC>Q ? */ 1248 tem->tvs_state = A_STATE_START; 1249 } else if (ch == 'C') { /* <ESC>C ? */ 1250 tem->tvs_state = A_STATE_START; 1251 } else { 1252 tem->tvs_state = A_STATE_START; 1253 if (ch == 'c') { 1254 /* ESC c resets display */ 1255 tem_safe_reset_display(tem, credp, called_from, 1256 B_TRUE, B_TRUE); 1257 } else if (ch == 'H') { 1258 /* ESC H sets a tab */ 1259 tem_safe_set_tab(tem); 1260 } else if (ch == '7') { 1261 /* ESC 7 Save Cursor position */ 1262 tem->tvs_r_cursor.row = tem->tvs_c_cursor.row; 1263 tem->tvs_r_cursor.col = tem->tvs_c_cursor.col; 1264 } else if (ch == '8') { 1265 /* ESC 8 Restore Cursor position */ 1266 tem_safe_mv_cursor(tem, tem->tvs_r_cursor.row, 1267 tem->tvs_r_cursor.col, credp, called_from); 1268 /* check for control chars */ 1269 } else if (ch < ' ') { 1270 tem_safe_control(tem, ch, credp, called_from); 1271 } else { 1272 tem_safe_outch(tem, ch, credp, called_from); 1273 } 1274 } 1275 } 1276 1277 /* ARGSUSED */ 1278 static void 1279 tem_safe_bell(struct tem_vt_state *tem, enum called_from called_from) 1280 { 1281 if (called_from == CALLED_FROM_STANDALONE) 1282 (void) beep_polled(BEEP_CONSOLE); 1283 else 1284 (void) beep(BEEP_CONSOLE); 1285 } 1286 1287 1288 static void 1289 tem_safe_scroll(struct tem_vt_state *tem, int start, int end, int count, 1290 int direction, cred_t *credp, enum called_from called_from) 1291 { 1292 int row; 1293 int lines_affected; 1294 1295 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1296 called_from == CALLED_FROM_STANDALONE); 1297 1298 lines_affected = end - start + 1; 1299 if (count > lines_affected) 1300 count = lines_affected; 1301 if (count <= 0) 1302 return; 1303 1304 switch (direction) { 1305 case TEM_SCROLL_UP: 1306 if (count < lines_affected) { 1307 tem_safe_copy_area(tem, 0, start + count, 1308 tems.ts_c_dimension.width - 1, end, 1309 0, start, credp, called_from); 1310 } 1311 for (row = (end - count) + 1; row <= end; row++) { 1312 tem_safe_clear_chars(tem, tems.ts_c_dimension.width, 1313 row, 0, credp, called_from); 1314 } 1315 break; 1316 1317 case TEM_SCROLL_DOWN: 1318 if (count < lines_affected) { 1319 tem_safe_copy_area(tem, 0, start, 1320 tems.ts_c_dimension.width - 1, 1321 end - count, 0, start + count, 1322 credp, called_from); 1323 } 1324 for (row = start; row < start + count; row++) { 1325 tem_safe_clear_chars(tem, tems.ts_c_dimension.width, 1326 row, 0, credp, called_from); 1327 } 1328 break; 1329 } 1330 } 1331 1332 static int 1333 tem_copy_width(term_char_t *src, term_char_t *dst, int cols) 1334 { 1335 int width = cols - 1; 1336 1337 while (width >= 0) { 1338 /* 1339 * Find difference on line, compare char with its attributes 1340 * and colors. 1341 */ 1342 if (src[width].tc_char != dst[width].tc_char || 1343 src[width].tc_fg_color != dst[width].tc_fg_color || 1344 src[width].tc_bg_color != dst[width].tc_bg_color) { 1345 break; 1346 } 1347 width--; 1348 } 1349 return (width + 1); 1350 } 1351 1352 static void 1353 tem_safe_copy_area(struct tem_vt_state *tem, 1354 screen_pos_t s_col, screen_pos_t s_row, 1355 screen_pos_t e_col, screen_pos_t e_row, 1356 screen_pos_t t_col, screen_pos_t t_row, 1357 cred_t *credp, enum called_from called_from) 1358 { 1359 size_t soffset, toffset; 1360 term_char_t *src, *dst; 1361 int rows; 1362 int cols; 1363 1364 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1365 called_from == CALLED_FROM_STANDALONE); 1366 1367 if (s_col < 0 || s_row < 0 || 1368 e_col < 0 || e_row < 0 || 1369 t_col < 0 || t_row < 0 || 1370 s_col >= tems.ts_c_dimension.width || 1371 e_col >= tems.ts_c_dimension.width || 1372 t_col >= tems.ts_c_dimension.width || 1373 s_row >= tems.ts_c_dimension.height || 1374 e_row >= tems.ts_c_dimension.height || 1375 t_row >= tems.ts_c_dimension.height) 1376 return; 1377 1378 if (s_row > e_row || s_col > e_col) 1379 return; 1380 1381 rows = e_row - s_row + 1; 1382 cols = e_col - s_col + 1; 1383 if (t_row + rows > tems.ts_c_dimension.height || 1384 t_col + cols > tems.ts_c_dimension.width) 1385 return; 1386 1387 soffset = s_col + s_row * tems.ts_c_dimension.width; 1388 toffset = t_col + t_row * tems.ts_c_dimension.width; 1389 src = tem->tvs_screen_buf + soffset; 1390 dst = tem->tvs_screen_buf + toffset; 1391 1392 /* 1393 * Copy line by line. We determine the length by comparing the 1394 * screen content from cached text in tvs_screen_buf. 1395 */ 1396 if (toffset <= soffset) { 1397 for (int i = 0; i < rows; i++) { 1398 int increment = i * tems.ts_c_dimension.width; 1399 int width; 1400 1401 width = tem_copy_width(src + increment, 1402 dst + increment, cols); 1403 1404 tem_safe_virtual_copy(tem, s_col, s_row + i, 1405 e_col - cols + width, s_row + i, 1406 t_col, t_row + i); 1407 1408 if (tem->tvs_isactive) { 1409 tem_safe_callback_copy(tem, s_col, s_row + i, 1410 e_col - cols + width, s_row + i, 1411 t_col, t_row + i, credp, called_from); 1412 } 1413 } 1414 } else { 1415 for (int i = rows - 1; i >= 0; i--) { 1416 int increment = i * tems.ts_c_dimension.width; 1417 int width; 1418 1419 width = tem_copy_width(src + increment, 1420 dst + increment, cols); 1421 1422 tem_safe_virtual_copy(tem, s_col, s_row + i, 1423 e_col - cols + width, s_row + i, 1424 t_col, t_row + i); 1425 1426 if (tem->tvs_isactive) { 1427 tem_safe_callback_copy(tem, s_col, s_row + i, 1428 e_col - cols + width, s_row + i, 1429 t_col, t_row + i, credp, called_from); 1430 } 1431 } 1432 } 1433 } 1434 1435 static void 1436 tem_safe_clear_chars(struct tem_vt_state *tem, int count, screen_pos_t row, 1437 screen_pos_t col, cred_t *credp, enum called_from called_from) 1438 { 1439 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1440 called_from == CALLED_FROM_STANDALONE); 1441 1442 if (row < 0 || row >= tems.ts_c_dimension.height || 1443 col < 0 || col >= tems.ts_c_dimension.width || 1444 count < 0) 1445 return; 1446 1447 /* 1448 * Note that very large values of "count" could cause col+count 1449 * to overflow, so we check "count" independently. 1450 */ 1451 if (count > tems.ts_c_dimension.width || 1452 col + count > tems.ts_c_dimension.width) 1453 count = tems.ts_c_dimension.width - col; 1454 1455 tem_safe_virtual_cls(tem, count, row, col); 1456 1457 if (!tem->tvs_isactive) 1458 return; 1459 1460 tem_safe_callback_cls(tem, count, row, col, credp, called_from); 1461 } 1462 1463 /*ARGSUSED*/ 1464 void 1465 tem_safe_text_display(struct tem_vt_state *tem, term_char_t *string, 1466 int count, screen_pos_t row, screen_pos_t col, 1467 cred_t *credp, enum called_from called_from) 1468 { 1469 struct vis_consdisplay da; 1470 int i; 1471 tem_char_t c; 1472 1473 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1474 called_from == CALLED_FROM_STANDALONE); 1475 1476 da.data = (uint8_t *)&c; 1477 da.width = 1; 1478 da.row = row; 1479 da.col = col; 1480 1481 for (i = 0; i < count; i++) { 1482 tem_safe_get_color(&da.fg_color, &da.bg_color, string[i]); 1483 c = TEM_CHAR(string[i].tc_char); 1484 tems_safe_display(&da, credp, called_from); 1485 da.col++; 1486 } 1487 } 1488 1489 /* 1490 * This function is used to blit a rectangular color image, 1491 * unperturbed on the underlying framebuffer, to render 1492 * icons and pictures. The data is a pixel pattern that 1493 * fills a rectangle bounded to the width and height parameters. 1494 * The color pixel data must to be pre-adjusted by the caller 1495 * for the current video depth. 1496 * 1497 * This function is unused now. 1498 */ 1499 /*ARGSUSED*/ 1500 static void 1501 tem_safe_image_display(struct tem_vt_state *tem, uchar_t *image, 1502 int height, int width, screen_pos_t row, screen_pos_t col, 1503 cred_t *credp, enum called_from called_from) 1504 { 1505 struct vis_consdisplay da; 1506 1507 mutex_enter(&tems.ts_lock); 1508 mutex_enter(&tem->tvs_lock); 1509 1510 da.data = image; 1511 da.width = (screen_size_t)width; 1512 da.height = (screen_size_t)height; 1513 da.row = row; 1514 da.col = col; 1515 1516 tems_safe_display(&da, credp, called_from); 1517 1518 mutex_exit(&tem->tvs_lock); 1519 mutex_exit(&tems.ts_lock); 1520 } 1521 1522 /*ARGSUSED*/ 1523 void 1524 tem_safe_text_copy(struct tem_vt_state *tem, 1525 screen_pos_t s_col, screen_pos_t s_row, 1526 screen_pos_t e_col, screen_pos_t e_row, 1527 screen_pos_t t_col, screen_pos_t t_row, 1528 cred_t *credp, enum called_from called_from) 1529 { 1530 struct vis_conscopy da; 1531 1532 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1533 called_from == CALLED_FROM_STANDALONE); 1534 1535 da.s_row = s_row; 1536 da.s_col = s_col; 1537 da.e_row = e_row; 1538 da.e_col = e_col; 1539 da.t_row = t_row; 1540 da.t_col = t_col; 1541 1542 tems_safe_copy(&da, credp, called_from); 1543 } 1544 1545 void 1546 tem_safe_text_cls(struct tem_vt_state *tem, 1547 int count, screen_pos_t row, screen_pos_t col, cred_t *credp, 1548 enum called_from called_from) 1549 { 1550 text_attr_t attr; 1551 term_char_t c; 1552 int i; 1553 1554 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1555 called_from == CALLED_FROM_STANDALONE); 1556 1557 tem_safe_get_attr(tem, &c.tc_fg_color, &c.tc_bg_color, &attr, 1558 TEM_ATTR_SCREEN_REVERSE); 1559 c.tc_char = TEM_ATTR(attr & ~TEM_ATTR_UNDERLINE) | ' '; 1560 1561 if (count > tems.ts_c_dimension.width || 1562 col + count > tems.ts_c_dimension.width) 1563 count = tems.ts_c_dimension.width - col; 1564 1565 for (i = 0; i < count; i++) 1566 tems.ts_blank_line[i] = c; 1567 1568 tem_safe_text_display(tem, tems.ts_blank_line, count, row, col, 1569 credp, called_from); 1570 } 1571 1572 void 1573 tem_safe_pix_display(struct tem_vt_state *tem, 1574 term_char_t *string, int count, 1575 screen_pos_t row, screen_pos_t col, 1576 cred_t *credp, enum called_from called_from) 1577 { 1578 struct vis_consdisplay da; 1579 int i; 1580 1581 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1582 called_from == CALLED_FROM_STANDALONE); 1583 1584 da.data = (uchar_t *)tem->tvs_pix_data; 1585 da.width = (screen_size_t)tems.ts_font.vf_width; 1586 da.height = (screen_size_t)tems.ts_font.vf_height; 1587 da.row = (row * da.height) + tems.ts_p_offset.y; 1588 da.col = (col * da.width) + tems.ts_p_offset.x; 1589 1590 for (i = 0; i < count; i++) { 1591 tem_safe_callback_bit2pix(tem, string[i]); 1592 tems_safe_display(&da, credp, called_from); 1593 da.col += da.width; 1594 } 1595 } 1596 1597 void 1598 tem_safe_pix_copy(struct tem_vt_state *tem, 1599 screen_pos_t s_col, screen_pos_t s_row, 1600 screen_pos_t e_col, screen_pos_t e_row, 1601 screen_pos_t t_col, screen_pos_t t_row, 1602 cred_t *credp, 1603 enum called_from called_from) 1604 { 1605 struct vis_conscopy ma; 1606 static boolean_t need_clear = B_TRUE; 1607 1608 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1609 called_from == CALLED_FROM_STANDALONE); 1610 1611 if (need_clear && tem->tvs_first_line > 0) { 1612 /* 1613 * Clear OBP output above our kernel console term 1614 * when our kernel console term begins to scroll up, 1615 * we hope it is user friendly. 1616 * (Also see comments on tem_safe_pix_clear_prom_output) 1617 * 1618 * This is only one time call. 1619 */ 1620 tem_safe_pix_clear_prom_output(tem, credp, called_from); 1621 } 1622 need_clear = B_FALSE; 1623 1624 ma.s_row = s_row * tems.ts_font.vf_height + tems.ts_p_offset.y; 1625 ma.e_row = (e_row + 1) * tems.ts_font.vf_height + 1626 tems.ts_p_offset.y - 1; 1627 ma.t_row = t_row * tems.ts_font.vf_height + tems.ts_p_offset.y; 1628 1629 /* 1630 * Check if we're in process of clearing OBP's columns area, 1631 * which only happens when term scrolls up a whole line. 1632 */ 1633 if (tem->tvs_first_line > 0 && t_row < s_row && t_col == 0 && 1634 e_col == tems.ts_c_dimension.width - 1) { 1635 /* 1636 * We need to clear OBP's columns area outside our kernel 1637 * console term. So that we set ma.e_col to entire row here. 1638 */ 1639 ma.s_col = s_col * tems.ts_font.vf_width; 1640 ma.e_col = tems.ts_p_dimension.width - 1; 1641 1642 ma.t_col = t_col * tems.ts_font.vf_width; 1643 } else { 1644 ma.s_col = s_col * tems.ts_font.vf_width + tems.ts_p_offset.x; 1645 ma.e_col = (e_col + 1) * tems.ts_font.vf_width + 1646 tems.ts_p_offset.x - 1; 1647 ma.t_col = t_col * tems.ts_font.vf_width + tems.ts_p_offset.x; 1648 } 1649 1650 tems_safe_copy(&ma, credp, called_from); 1651 1652 if (tem->tvs_first_line > 0 && t_row < s_row) { 1653 /* We have scrolled up (s_row - t_row) rows. */ 1654 tem->tvs_first_line -= (s_row - t_row); 1655 if (tem->tvs_first_line <= 0) { 1656 /* All OBP rows have been cleared. */ 1657 tem->tvs_first_line = 0; 1658 } 1659 } 1660 1661 } 1662 1663 void 1664 tem_safe_pix_bit2pix(struct tem_vt_state *tem, term_char_t c) 1665 { 1666 text_color_t fg, bg; 1667 void (*fp)(struct tem_vt_state *, tem_char_t, 1668 unsigned char, unsigned char); 1669 1670 tem_safe_get_color(&fg, &bg, c); 1671 switch (tems.ts_pdepth) { 1672 case 4: 1673 fp = bit_to_pix4; 1674 break; 1675 case 8: 1676 fp = bit_to_pix8; 1677 break; 1678 case 15: 1679 case 16: 1680 fp = bit_to_pix16; 1681 break; 1682 case 24: 1683 fp = bit_to_pix24; 1684 break; 1685 case 32: 1686 fp = bit_to_pix32; 1687 break; 1688 default: 1689 return; 1690 } 1691 1692 fp(tem, c.tc_char, fg, bg); 1693 } 1694 1695 1696 /* 1697 * This function only clears count of columns in one row 1698 */ 1699 void 1700 tem_safe_pix_cls(struct tem_vt_state *tem, int count, 1701 screen_pos_t row, screen_pos_t col, cred_t *credp, 1702 enum called_from called_from) 1703 { 1704 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1705 called_from == CALLED_FROM_STANDALONE); 1706 1707 tem_safe_pix_cls_range(tem, row, 1, tems.ts_p_offset.y, 1708 col, count, tems.ts_p_offset.x, B_FALSE, credp, called_from); 1709 } 1710 1711 /* 1712 * This function clears OBP output above our kernel console term area 1713 * because OBP's term may have a bigger terminal window than that of 1714 * our kernel console term. So we need to clear OBP output garbage outside 1715 * of our kernel console term at a proper time, which is when the first 1716 * row output of our kernel console term scrolls at the first screen line. 1717 * 1718 * _________________________________ 1719 * | _____________________ | ---> OBP's bigger term window 1720 * | | | | 1721 * |___| | | 1722 * | | | | | 1723 * | | | | | 1724 * |_|_|___________________|_______| 1725 * | | | ---> first line 1726 * | |___________________|---> our kernel console term window 1727 * | 1728 * |---> columns area to be cleared 1729 * 1730 * This function only takes care of the output above our kernel console term, 1731 * and tem_prom_scroll_up takes care of columns area outside of our kernel 1732 * console term. 1733 */ 1734 static void 1735 tem_safe_pix_clear_prom_output(struct tem_vt_state *tem, cred_t *credp, 1736 enum called_from called_from) 1737 { 1738 int nrows, ncols, width, height, offset; 1739 1740 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1741 called_from == CALLED_FROM_STANDALONE); 1742 1743 width = tems.ts_font.vf_width; 1744 height = tems.ts_font.vf_height; 1745 offset = tems.ts_p_offset.y % height; 1746 1747 nrows = tems.ts_p_offset.y / height; 1748 ncols = (tems.ts_p_dimension.width + (width - 1))/ width; 1749 1750 if (nrows > 0) 1751 tem_safe_pix_cls_range(tem, 0, nrows, offset, 0, ncols, 0, 1752 B_FALSE, credp, called_from); 1753 } 1754 1755 /* 1756 * clear the whole screen for pixel mode, just clear the 1757 * physical screen. 1758 */ 1759 void 1760 tem_safe_pix_clear_entire_screen(struct tem_vt_state *tem, cred_t *credp, 1761 enum called_from called_from) 1762 { 1763 struct vis_consclear cl; 1764 text_color_t fg_color; 1765 text_color_t bg_color; 1766 text_attr_t attr; 1767 term_char_t c; 1768 int nrows, ncols, width, height; 1769 1770 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1771 called_from == CALLED_FROM_STANDALONE); 1772 1773 /* call driver first, if error, clear terminal area */ 1774 tem_safe_get_attr(tem, &c.tc_fg_color, &c.tc_bg_color, &attr, 1775 TEM_ATTR_SCREEN_REVERSE); 1776 c.tc_char = TEM_ATTR(attr); 1777 1778 tem_safe_get_color(&fg_color, &bg_color, c); 1779 cl.bg_color = bg_color; 1780 if (tems_cls_layered(&cl, credp) == 0) 1781 return; 1782 1783 width = tems.ts_font.vf_width; 1784 height = tems.ts_font.vf_height; 1785 1786 nrows = (tems.ts_p_dimension.height + (height - 1))/ height; 1787 ncols = (tems.ts_p_dimension.width + (width - 1))/ width; 1788 1789 tem_safe_pix_cls_range(tem, 0, nrows, tems.ts_p_offset.y, 0, ncols, 1790 tems.ts_p_offset.x, B_FALSE, credp, called_from); 1791 1792 /* 1793 * Since the whole screen is cleared, we don't need 1794 * to clear OBP output later. 1795 */ 1796 if (tem->tvs_first_line > 0) 1797 tem->tvs_first_line = 0; 1798 } 1799 1800 /* 1801 * clear the whole screen, including the virtual screen buffer, 1802 * and reset the cursor to start point. 1803 */ 1804 static void 1805 tem_safe_cls(struct tem_vt_state *tem, 1806 cred_t *credp, enum called_from called_from) 1807 { 1808 int row; 1809 1810 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1811 called_from == CALLED_FROM_STANDALONE); 1812 1813 if (tems.ts_display_mode == VIS_TEXT) { 1814 for (row = 0; row < tems.ts_c_dimension.height; row++) { 1815 tem_safe_clear_chars(tem, tems.ts_c_dimension.width, 1816 row, 0, credp, called_from); 1817 } 1818 tem->tvs_c_cursor.row = 0; 1819 tem->tvs_c_cursor.col = 0; 1820 tem_safe_align_cursor(tem); 1821 return; 1822 } 1823 1824 ASSERT(tems.ts_display_mode == VIS_PIXEL); 1825 1826 for (row = 0; row < tems.ts_c_dimension.height; row++) { 1827 tem_safe_virtual_cls(tem, tems.ts_c_dimension.width, row, 0); 1828 } 1829 tem->tvs_c_cursor.row = 0; 1830 tem->tvs_c_cursor.col = 0; 1831 tem_safe_align_cursor(tem); 1832 1833 if (!tem->tvs_isactive) 1834 return; 1835 1836 tem_safe_pix_clear_entire_screen(tem, credp, called_from); 1837 } 1838 1839 static void 1840 tem_safe_back_tab(struct tem_vt_state *tem, 1841 cred_t *credp, enum called_from called_from) 1842 { 1843 int i; 1844 screen_pos_t tabstop; 1845 1846 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1847 called_from == CALLED_FROM_STANDALONE); 1848 1849 tabstop = 0; 1850 1851 for (i = tem->tvs_ntabs - 1; i >= 0; i--) { 1852 if (tem->tvs_tabs[i] < tem->tvs_c_cursor.col) { 1853 tabstop = tem->tvs_tabs[i]; 1854 break; 1855 } 1856 } 1857 1858 tem_safe_mv_cursor(tem, tem->tvs_c_cursor.row, 1859 tabstop, credp, called_from); 1860 } 1861 1862 static void 1863 tem_safe_tab(struct tem_vt_state *tem, 1864 cred_t *credp, enum called_from called_from) 1865 { 1866 int i; 1867 screen_pos_t tabstop; 1868 1869 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1870 called_from == CALLED_FROM_STANDALONE); 1871 1872 tabstop = tems.ts_c_dimension.width - 1; 1873 1874 for (i = 0; i < tem->tvs_ntabs; i++) { 1875 if (tem->tvs_tabs[i] > tem->tvs_c_cursor.col) { 1876 tabstop = tem->tvs_tabs[i]; 1877 break; 1878 } 1879 } 1880 1881 tem_safe_mv_cursor(tem, tem->tvs_c_cursor.row, 1882 tabstop, credp, called_from); 1883 } 1884 1885 static void 1886 tem_safe_set_tab(struct tem_vt_state *tem) 1887 { 1888 int i; 1889 int j; 1890 1891 if (tem->tvs_ntabs == TEM_MAXTAB) 1892 return; 1893 if (tem->tvs_ntabs == 0 || 1894 tem->tvs_tabs[tem->tvs_ntabs] < tem->tvs_c_cursor.col) { 1895 tem->tvs_tabs[tem->tvs_ntabs++] = tem->tvs_c_cursor.col; 1896 return; 1897 } 1898 for (i = 0; i < tem->tvs_ntabs; i++) { 1899 if (tem->tvs_tabs[i] == tem->tvs_c_cursor.col) 1900 return; 1901 if (tem->tvs_tabs[i] > tem->tvs_c_cursor.col) { 1902 for (j = tem->tvs_ntabs - 1; j >= i; j--) 1903 tem->tvs_tabs[j+ 1] = tem->tvs_tabs[j]; 1904 tem->tvs_tabs[i] = tem->tvs_c_cursor.col; 1905 tem->tvs_ntabs++; 1906 return; 1907 } 1908 } 1909 } 1910 1911 static void 1912 tem_safe_clear_tabs(struct tem_vt_state *tem, int action) 1913 { 1914 int i; 1915 int j; 1916 1917 switch (action) { 1918 case 3: /* clear all tabs */ 1919 tem->tvs_ntabs = 0; 1920 break; 1921 case 0: /* clr tab at cursor */ 1922 1923 for (i = 0; i < tem->tvs_ntabs; i++) { 1924 if (tem->tvs_tabs[i] == tem->tvs_c_cursor.col) { 1925 tem->tvs_ntabs--; 1926 for (j = i; j < tem->tvs_ntabs; j++) 1927 tem->tvs_tabs[j] = tem->tvs_tabs[j + 1]; 1928 return; 1929 } 1930 } 1931 break; 1932 } 1933 } 1934 1935 static void 1936 tem_safe_mv_cursor(struct tem_vt_state *tem, int row, int col, 1937 cred_t *credp, enum called_from called_from) 1938 { 1939 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1940 called_from == CALLED_FROM_STANDALONE); 1941 1942 /* 1943 * Sanity check and bounds enforcement. Out of bounds requests are 1944 * clipped to the screen boundaries. This seems to be what SPARC 1945 * does. 1946 */ 1947 if (row < 0) 1948 row = 0; 1949 if (row >= tems.ts_c_dimension.height) 1950 row = tems.ts_c_dimension.height - 1; 1951 if (col < 0) 1952 col = 0; 1953 if (col >= tems.ts_c_dimension.width) 1954 col = tems.ts_c_dimension.width - 1; 1955 1956 tem_safe_send_data(tem, credp, called_from); 1957 tem->tvs_c_cursor.row = (screen_pos_t)row; 1958 tem->tvs_c_cursor.col = (screen_pos_t)col; 1959 tem_safe_align_cursor(tem); 1960 } 1961 1962 /* ARGSUSED */ 1963 void 1964 tem_safe_reset_emulator(struct tem_vt_state *tem, 1965 cred_t *credp, enum called_from called_from, 1966 boolean_t init_color) 1967 { 1968 int j; 1969 1970 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 1971 called_from == CALLED_FROM_STANDALONE); 1972 1973 tem->tvs_c_cursor.row = 0; 1974 tem->tvs_c_cursor.col = 0; 1975 tem->tvs_r_cursor.row = 0; 1976 tem->tvs_r_cursor.col = 0; 1977 tem->tvs_s_cursor.row = 0; 1978 tem->tvs_s_cursor.col = 0; 1979 tem->tvs_outindex = 0; 1980 tem->tvs_state = A_STATE_START; 1981 tem->tvs_gotparam = B_FALSE; 1982 tem->tvs_curparam = 0; 1983 tem->tvs_paramval = 0; 1984 tem->tvs_nscroll = 1; 1985 1986 if (init_color) { 1987 /* use initial settings */ 1988 tem->tvs_fg_color = tems.ts_init_color.fg_color; 1989 tem->tvs_bg_color = tems.ts_init_color.bg_color; 1990 tem->tvs_flags = tems.ts_init_color.a_flags; 1991 } 1992 1993 /* 1994 * set up the initial tab stops 1995 */ 1996 tem->tvs_ntabs = 0; 1997 for (j = 8; j < tems.ts_c_dimension.width; j += 8) 1998 tem->tvs_tabs[tem->tvs_ntabs++] = (screen_pos_t)j; 1999 2000 for (j = 0; j < TEM_MAXPARAMS; j++) 2001 tem->tvs_params[j] = 0; 2002 } 2003 2004 void 2005 tem_safe_reset_display(struct tem_vt_state *tem, 2006 cred_t *credp, enum called_from called_from, 2007 boolean_t clear_txt, boolean_t init_color) 2008 { 2009 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 2010 called_from == CALLED_FROM_STANDALONE); 2011 2012 tem_safe_reset_emulator(tem, credp, called_from, init_color); 2013 2014 if (clear_txt) { 2015 if (tem->tvs_isactive) 2016 tem_safe_callback_cursor(tem, 2017 VIS_HIDE_CURSOR, credp, called_from); 2018 2019 tem_safe_cls(tem, credp, called_from); 2020 2021 if (tem->tvs_isactive) 2022 tem_safe_callback_cursor(tem, 2023 VIS_DISPLAY_CURSOR, credp, called_from); 2024 } 2025 } 2026 2027 static void 2028 tem_safe_shift( 2029 struct tem_vt_state *tem, 2030 int count, 2031 int direction, 2032 cred_t *credp, 2033 enum called_from called_from) 2034 { 2035 int rest_of_line; 2036 2037 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 2038 called_from == CALLED_FROM_STANDALONE); 2039 2040 rest_of_line = tems.ts_c_dimension.width - tem->tvs_c_cursor.col; 2041 if (count > rest_of_line) 2042 count = rest_of_line; 2043 2044 if (count <= 0) 2045 return; 2046 2047 switch (direction) { 2048 case TEM_SHIFT_LEFT: 2049 if (count < rest_of_line) { 2050 tem_safe_copy_area(tem, 2051 tem->tvs_c_cursor.col + count, 2052 tem->tvs_c_cursor.row, 2053 tems.ts_c_dimension.width - 1, 2054 tem->tvs_c_cursor.row, 2055 tem->tvs_c_cursor.col, 2056 tem->tvs_c_cursor.row, 2057 credp, called_from); 2058 } 2059 2060 tem_safe_clear_chars(tem, count, tem->tvs_c_cursor.row, 2061 (tems.ts_c_dimension.width - count), credp, 2062 called_from); 2063 break; 2064 case TEM_SHIFT_RIGHT: 2065 if (count < rest_of_line) { 2066 tem_safe_copy_area(tem, 2067 tem->tvs_c_cursor.col, 2068 tem->tvs_c_cursor.row, 2069 tems.ts_c_dimension.width - count - 1, 2070 tem->tvs_c_cursor.row, 2071 tem->tvs_c_cursor.col + count, 2072 tem->tvs_c_cursor.row, 2073 credp, called_from); 2074 } 2075 2076 tem_safe_clear_chars(tem, count, tem->tvs_c_cursor.row, 2077 tem->tvs_c_cursor.col, credp, called_from); 2078 break; 2079 } 2080 } 2081 2082 void 2083 tem_safe_text_cursor(struct tem_vt_state *tem, short action, 2084 cred_t *credp, enum called_from called_from) 2085 { 2086 struct vis_conscursor ca; 2087 2088 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 2089 called_from == CALLED_FROM_STANDALONE); 2090 2091 ca.row = tem->tvs_c_cursor.row; 2092 ca.col = tem->tvs_c_cursor.col; 2093 ca.action = action; 2094 2095 tems_safe_cursor(&ca, credp, called_from); 2096 2097 if (action == VIS_GET_CURSOR) { 2098 tem->tvs_c_cursor.row = ca.row; 2099 tem->tvs_c_cursor.col = ca.col; 2100 } 2101 } 2102 2103 void 2104 tem_safe_pix_cursor(struct tem_vt_state *tem, short action, 2105 cred_t *credp, enum called_from called_from) 2106 { 2107 struct vis_conscursor ca; 2108 uint32_t color; 2109 text_color_t fg, bg; 2110 term_char_t c; 2111 text_attr_t attr; 2112 2113 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 2114 called_from == CALLED_FROM_STANDALONE); 2115 2116 ca.row = tem->tvs_c_cursor.row * tems.ts_font.vf_height + 2117 tems.ts_p_offset.y; 2118 ca.col = tem->tvs_c_cursor.col * tems.ts_font.vf_width + 2119 tems.ts_p_offset.x; 2120 ca.width = (screen_size_t)tems.ts_font.vf_width; 2121 ca.height = (screen_size_t)tems.ts_font.vf_height; 2122 2123 tem_safe_get_attr(tem, &c.tc_fg_color, &c.tc_bg_color, &attr, 2124 TEM_ATTR_REVERSE); 2125 c.tc_char = TEM_ATTR(attr); 2126 2127 tem_safe_get_color(&fg, &bg, c); 2128 2129 switch (tems.ts_pdepth) { 2130 case 4: 2131 case 8: 2132 ca.fg_color.mono = fg; 2133 ca.bg_color.mono = bg; 2134 break; 2135 case 15: 2136 case 16: 2137 color = tems.ts_color_map(fg); 2138 ca.fg_color.sixteen[0] = (color >> 8) & 0xFF; 2139 ca.fg_color.sixteen[1] = color & 0xFF; 2140 color = tems.ts_color_map(bg); 2141 ca.bg_color.sixteen[0] = (color >> 8) & 0xFF; 2142 ca.bg_color.sixteen[1] = color & 0xFF; 2143 break; 2144 case 24: 2145 case 32: 2146 #ifdef _HAVE_TEM_FIRMWARE 2147 /* Keeping this block to support old binary only drivers */ 2148 if (tem->tvs_flags & TEM_ATTR_REVERSE) { 2149 ca.fg_color.twentyfour[0] = TEM_TEXT_WHITE24_RED; 2150 ca.fg_color.twentyfour[1] = TEM_TEXT_WHITE24_GREEN; 2151 ca.fg_color.twentyfour[2] = TEM_TEXT_WHITE24_BLUE; 2152 2153 ca.bg_color.twentyfour[0] = TEM_TEXT_BLACK24_RED; 2154 ca.bg_color.twentyfour[1] = TEM_TEXT_BLACK24_GREEN; 2155 ca.bg_color.twentyfour[2] = TEM_TEXT_BLACK24_BLUE; 2156 } else { 2157 ca.fg_color.twentyfour[0] = TEM_TEXT_BLACK24_RED; 2158 ca.fg_color.twentyfour[1] = TEM_TEXT_BLACK24_GREEN; 2159 ca.fg_color.twentyfour[2] = TEM_TEXT_BLACK24_BLUE; 2160 2161 ca.bg_color.twentyfour[0] = TEM_TEXT_WHITE24_RED; 2162 ca.bg_color.twentyfour[1] = TEM_TEXT_WHITE24_GREEN; 2163 ca.bg_color.twentyfour[2] = TEM_TEXT_WHITE24_BLUE; 2164 } 2165 #else 2166 color = tems.ts_color_map(fg); 2167 ca.fg_color.twentyfour[0] = (color >> 16) & 0xFF; 2168 ca.fg_color.twentyfour[1] = (color >> 8) & 0xFF; 2169 ca.fg_color.twentyfour[2] = color & 0xFF; 2170 color = tems.ts_color_map(bg); 2171 ca.bg_color.twentyfour[0] = (color >> 16) & 0xFF; 2172 ca.bg_color.twentyfour[1] = (color >> 8) & 0xFF; 2173 ca.bg_color.twentyfour[2] = color & 0xFF; 2174 break; 2175 #endif 2176 } 2177 2178 ca.action = action; 2179 2180 tems_safe_cursor(&ca, credp, called_from); 2181 2182 if (action == VIS_GET_CURSOR) { 2183 tem->tvs_c_cursor.row = 0; 2184 tem->tvs_c_cursor.col = 0; 2185 2186 if (ca.row != 0) { 2187 tem->tvs_c_cursor.row = (ca.row - tems.ts_p_offset.y) / 2188 tems.ts_font.vf_height; 2189 } 2190 if (ca.col != 0) { 2191 tem->tvs_c_cursor.col = (ca.col - tems.ts_p_offset.x) / 2192 tems.ts_font.vf_width; 2193 } 2194 } 2195 } 2196 2197 static void 2198 bit_to_pix4(struct tem_vt_state *tem, tem_char_t c, text_color_t fg_color, 2199 text_color_t bg_color) 2200 { 2201 uint8_t *dest = (uint8_t *)tem->tvs_pix_data; 2202 font_bit_to_pix4(&tems.ts_font, dest, c, fg_color, bg_color); 2203 } 2204 2205 static void 2206 bit_to_pix8(struct tem_vt_state *tem, tem_char_t c, text_color_t fg_color, 2207 text_color_t bg_color) 2208 { 2209 uint8_t *dest = (uint8_t *)tem->tvs_pix_data; 2210 font_bit_to_pix8(&tems.ts_font, dest, c, fg_color, bg_color); 2211 } 2212 2213 static void 2214 bit_to_pix16(struct tem_vt_state *tem, tem_char_t c, text_color_t fg_color4, 2215 text_color_t bg_color4) 2216 { 2217 uint16_t fg_color16, bg_color16; 2218 uint16_t *dest; 2219 2220 ASSERT(fg_color4 < 16 && bg_color4 < 16); 2221 2222 fg_color16 = (uint16_t)tems.ts_color_map(fg_color4); 2223 bg_color16 = (uint16_t)tems.ts_color_map(bg_color4); 2224 2225 dest = (uint16_t *)tem->tvs_pix_data; 2226 font_bit_to_pix16(&tems.ts_font, dest, c, fg_color16, bg_color16); 2227 } 2228 2229 static void 2230 bit_to_pix24(struct tem_vt_state *tem, tem_char_t c, text_color_t fg_color4, 2231 text_color_t bg_color4) 2232 { 2233 uint32_t fg_color32, bg_color32; 2234 uint8_t *dest; 2235 2236 ASSERT(fg_color4 < 16 && bg_color4 < 16); 2237 2238 #ifdef _HAVE_TEM_FIRMWARE 2239 fg_color32 = PIX4TO32(fg_color4); 2240 bg_color32 = PIX4TO32(bg_color4); 2241 #else 2242 fg_color32 = tems.ts_color_map(fg_color4); 2243 bg_color32 = tems.ts_color_map(bg_color4); 2244 #endif 2245 2246 dest = (uint8_t *)tem->tvs_pix_data; 2247 font_bit_to_pix24(&tems.ts_font, dest, c, fg_color32, bg_color32); 2248 } 2249 2250 static void 2251 bit_to_pix32(struct tem_vt_state *tem, tem_char_t c, text_color_t fg_color4, 2252 text_color_t bg_color4) 2253 { 2254 uint32_t fg_color32, bg_color32, *dest; 2255 2256 ASSERT(fg_color4 < 16 && bg_color4 < 16); 2257 2258 #ifdef _HAVE_TEM_FIRMWARE 2259 fg_color32 = PIX4TO32(fg_color4); 2260 bg_color32 = PIX4TO32(bg_color4); 2261 #else 2262 fg_color32 = ((uint32_t)0xFF << 24) | tems.ts_color_map(fg_color4); 2263 bg_color32 = ((uint32_t)0xFF << 24) | tems.ts_color_map(bg_color4); 2264 #endif 2265 2266 dest = (uint32_t *)tem->tvs_pix_data; 2267 font_bit_to_pix32(&tems.ts_font, dest, c, fg_color32, bg_color32); 2268 } 2269 2270 /* 2271 * flag: TEM_ATTR_SCREEN_REVERSE or TEM_ATTR_REVERSE 2272 */ 2273 void 2274 tem_safe_get_attr(struct tem_vt_state *tem, text_color_t *fg, 2275 text_color_t *bg, text_attr_t *attr, uint8_t flag) 2276 { 2277 if (tem->tvs_flags & flag) { 2278 *fg = tem->tvs_bg_color; 2279 *bg = tem->tvs_fg_color; 2280 } else { 2281 *fg = tem->tvs_fg_color; 2282 *bg = tem->tvs_bg_color; 2283 } 2284 2285 if (attr == NULL) 2286 return; 2287 2288 *attr = tem->tvs_flags; 2289 } 2290 2291 static void 2292 tem_safe_get_color(text_color_t *fg, text_color_t *bg, term_char_t c) 2293 { 2294 if (TEM_ATTR_ISSET(c.tc_char, TEM_ATTR_BRIGHT_FG | TEM_ATTR_BOLD)) 2295 *fg = brt_xlate[c.tc_fg_color]; 2296 else 2297 *fg = dim_xlate[c.tc_fg_color]; 2298 2299 if (TEM_ATTR_ISSET(c.tc_char, TEM_ATTR_BRIGHT_BG)) 2300 *bg = brt_xlate[c.tc_bg_color]; 2301 else 2302 *bg = dim_xlate[c.tc_bg_color]; 2303 } 2304 2305 /* 2306 * Clear a rectangle of screen for pixel mode. 2307 * 2308 * arguments: 2309 * row: start row# 2310 * nrows: the number of rows to clear 2311 * offset_y: the offset of height in pixels to begin clear 2312 * col: start col# 2313 * ncols: the number of cols to clear 2314 * offset_x: the offset of width in pixels to begin clear 2315 * scroll_up: whether this function is called during sroll up, 2316 * which is called only once. 2317 */ 2318 void 2319 tem_safe_pix_cls_range(struct tem_vt_state *tem, 2320 screen_pos_t row, int nrows, int offset_y, 2321 screen_pos_t col, int ncols, int offset_x, 2322 boolean_t sroll_up, cred_t *credp, 2323 enum called_from called_from) 2324 { 2325 struct vis_consdisplay da; 2326 int i, j; 2327 int row_add = 0; 2328 term_char_t c; 2329 text_attr_t attr; 2330 2331 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 2332 called_from == CALLED_FROM_STANDALONE); 2333 2334 if (sroll_up) 2335 row_add = tems.ts_c_dimension.height - 1; 2336 2337 da.width = (screen_size_t)tems.ts_font.vf_width; 2338 da.height = (screen_size_t)tems.ts_font.vf_height; 2339 2340 tem_safe_get_attr(tem, &c.tc_fg_color, &c.tc_bg_color, &attr, 2341 TEM_ATTR_SCREEN_REVERSE); 2342 /* Make sure we will not draw underlines */ 2343 c.tc_char = TEM_ATTR(attr & ~TEM_ATTR_UNDERLINE) | ' '; 2344 2345 tem_safe_callback_bit2pix(tem, c); 2346 da.data = (uchar_t *)tem->tvs_pix_data; 2347 2348 for (i = 0; i < nrows; i++, row++) { 2349 da.row = (row + row_add) * da.height + offset_y; 2350 da.col = col * da.width + offset_x; 2351 for (j = 0; j < ncols; j++) { 2352 tems_safe_display(&da, credp, called_from); 2353 da.col += da.width; 2354 } 2355 } 2356 } 2357 2358 /* 2359 * virtual screen operations 2360 */ 2361 static void 2362 tem_safe_virtual_display(struct tem_vt_state *tem, term_char_t *string, 2363 int count, screen_pos_t row, screen_pos_t col) 2364 { 2365 int i, width; 2366 term_char_t *addr; 2367 2368 if (row < 0 || row >= tems.ts_c_dimension.height || 2369 col < 0 || col >= tems.ts_c_dimension.width || 2370 col + count > tems.ts_c_dimension.width) 2371 return; 2372 2373 width = tems.ts_c_dimension.width; 2374 addr = tem->tvs_screen_buf + (row * width + col); 2375 for (i = 0; i < count; i++) { 2376 *addr++ = string[i]; 2377 } 2378 } 2379 2380 static void 2381 i_virtual_copy_tem_chars(term_char_t *base, 2382 screen_pos_t s_col, screen_pos_t s_row, 2383 screen_pos_t e_col, screen_pos_t e_row, 2384 screen_pos_t t_col, screen_pos_t t_row) 2385 { 2386 term_char_t *from; 2387 term_char_t *to; 2388 int cnt; 2389 screen_size_t chars_per_row; 2390 term_char_t *to_row_start; 2391 term_char_t *from_row_start; 2392 screen_size_t rows_to_move; 2393 int cols = tems.ts_c_dimension.width; 2394 2395 chars_per_row = e_col - s_col + 1; 2396 rows_to_move = e_row - s_row + 1; 2397 2398 to_row_start = base + ((t_row * cols) + t_col); 2399 from_row_start = base + ((s_row * cols) + s_col); 2400 2401 if (to_row_start < from_row_start) { 2402 while (rows_to_move-- > 0) { 2403 to = to_row_start; 2404 from = from_row_start; 2405 to_row_start += cols; 2406 from_row_start += cols; 2407 for (cnt = chars_per_row; cnt-- > 0; ) 2408 *to++ = *from++; 2409 } 2410 } else { 2411 /* 2412 * Offset to the end of the region and copy backwards. 2413 */ 2414 cnt = rows_to_move * cols + chars_per_row; 2415 to_row_start += cnt; 2416 from_row_start += cnt; 2417 2418 while (rows_to_move-- > 0) { 2419 to_row_start -= cols; 2420 from_row_start -= cols; 2421 to = to_row_start; 2422 from = from_row_start; 2423 for (cnt = chars_per_row; cnt-- > 0; ) 2424 *--to = *--from; 2425 } 2426 } 2427 } 2428 2429 static void 2430 tem_safe_virtual_copy(struct tem_vt_state *tem, 2431 screen_pos_t s_col, screen_pos_t s_row, 2432 screen_pos_t e_col, screen_pos_t e_row, 2433 screen_pos_t t_col, screen_pos_t t_row) 2434 { 2435 screen_size_t chars_per_row; 2436 screen_size_t rows_to_move; 2437 int rows = tems.ts_c_dimension.height; 2438 int cols = tems.ts_c_dimension.width; 2439 2440 if (s_col < 0 || s_col >= cols || 2441 s_row < 0 || s_row >= rows || 2442 e_col < 0 || e_col >= cols || 2443 e_row < 0 || e_row >= rows || 2444 t_col < 0 || t_col >= cols || 2445 t_row < 0 || t_row >= rows || 2446 s_col > e_col || 2447 s_row > e_row) 2448 return; 2449 2450 chars_per_row = e_col - s_col + 1; 2451 rows_to_move = e_row - s_row + 1; 2452 2453 /* More sanity checks. */ 2454 if (t_row + rows_to_move > rows || 2455 t_col + chars_per_row > cols) 2456 return; 2457 2458 i_virtual_copy_tem_chars(tem->tvs_screen_buf, s_col, s_row, 2459 e_col, e_row, t_col, t_row); 2460 } 2461 2462 static void 2463 tem_safe_virtual_cls(struct tem_vt_state *tem, 2464 int count, screen_pos_t row, screen_pos_t col) 2465 { 2466 int i; 2467 text_attr_t attr; 2468 term_char_t c; 2469 2470 tem_safe_get_attr(tem, &c.tc_fg_color, &c.tc_bg_color, &attr, 2471 TEM_ATTR_SCREEN_REVERSE); 2472 c.tc_char = TEM_ATTR(attr & ~TEM_ATTR_UNDERLINE) | ' '; 2473 2474 for (i = 0; i < tems.ts_c_dimension.width; i++) 2475 tems.ts_blank_line[i] = c; 2476 2477 tem_safe_virtual_display(tem, tems.ts_blank_line, count, row, col); 2478 } 2479 2480 /* 2481 * only blank screen, not clear our screen buffer 2482 */ 2483 void 2484 tem_safe_blank_screen(struct tem_vt_state *tem, cred_t *credp, 2485 enum called_from called_from) 2486 { 2487 int row; 2488 2489 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 2490 called_from == CALLED_FROM_STANDALONE); 2491 2492 if (tems.ts_display_mode == VIS_PIXEL) { 2493 tem_safe_pix_clear_entire_screen(tem, credp, called_from); 2494 return; 2495 } 2496 2497 for (row = 0; row < tems.ts_c_dimension.height; row++) { 2498 tem_safe_callback_cls(tem, 2499 tems.ts_c_dimension.width, 2500 row, 0, credp, called_from); 2501 } 2502 } 2503 2504 /* 2505 * unblank screen with associated tem from its screen buffer 2506 */ 2507 void 2508 tem_safe_unblank_screen(struct tem_vt_state *tem, cred_t *credp, 2509 enum called_from called_from) 2510 { 2511 int row; 2512 2513 ASSERT((MUTEX_HELD(&tems.ts_lock) && MUTEX_HELD(&tem->tvs_lock)) || 2514 called_from == CALLED_FROM_STANDALONE); 2515 2516 if (tems.ts_display_mode == VIS_PIXEL) 2517 tem_safe_pix_clear_entire_screen(tem, credp, called_from); 2518 2519 tem_safe_callback_cursor(tem, VIS_HIDE_CURSOR, credp, called_from); 2520 2521 /* 2522 * Display data in tvs_screen_buf to the actual framebuffer in a 2523 * row by row way. 2524 * When dealing with one row, output data with the same foreground 2525 * and background color all together. 2526 */ 2527 for (row = 0; row < tems.ts_c_dimension.height; row++) { 2528 tem_safe_callback_display(tem, tem->tvs_screen_rows[row], 2529 tems.ts_c_dimension.width, row, 0, credp, called_from); 2530 } 2531 2532 tem_safe_callback_cursor(tem, VIS_DISPLAY_CURSOR, credp, called_from); 2533 } 2534