1 /* 2 * Copyright 2013, Haiku, Inc. All rights reserved. 3 * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de. 4 * Distributed under the terms of the MIT License. 5 * 6 * Authors: 7 * Ingo Weinhold, ingo_weinhold@gmx.de 8 * Siarzhuk Zharski, zharik@gmx.li 9 */ 10 11 #include "BasicTerminalBuffer.h" 12 13 #include <alloca.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <fcntl.h> 17 #include <string.h> 18 19 #include <algorithm> 20 21 #include <StackOrHeapArray.h> 22 #include <String.h> 23 24 #include "TermConst.h" 25 #include "TerminalCharClassifier.h" 26 #include "TerminalLine.h" 27 28 29 static const UTF8Char kSpaceChar(' '); 30 31 // Soft size limits for the terminal buffer. The constants defined in 32 // TermConst.h are rather for the Terminal in general (i.e. the GUI). 33 static const int32 kMinRowCount = 2; 34 static const int32 kMaxRowCount = 1024; 35 static const int32 kMinColumnCount = 4; 36 static const int32 kMaxColumnCount = 1024; 37 38 39 #define ALLOC_LINE_ON_STACK(width) \ 40 ((TerminalLine*)alloca(sizeof(TerminalLine) \ 41 + sizeof(TerminalCell) * ((width) - 1))) 42 43 44 static inline int32 45 restrict_value(int32 value, int32 min, int32 max) 46 { 47 return value < min ? min : (value > max ? max : value); 48 } 49 50 51 // #pragma mark - private inline methods 52 53 54 inline int32 55 BasicTerminalBuffer::_LineIndex(int32 index) const 56 { 57 return (index + fScreenOffset) % fHeight; 58 } 59 60 61 inline TerminalLine* 62 BasicTerminalBuffer::_LineAt(int32 index) const 63 { 64 return fScreen[_LineIndex(index)]; 65 } 66 67 68 inline TerminalLine* 69 BasicTerminalBuffer::_HistoryLineAt(int32 index, TerminalLine* lineBuffer) const 70 { 71 if (index >= fHeight) 72 return NULL; 73 74 if (index < 0 && fHistory != NULL) 75 return fHistory->GetTerminalLineAt(-index - 1, lineBuffer); 76 77 return _LineAt(index + fHeight); 78 } 79 80 81 inline void 82 BasicTerminalBuffer::_Invalidate(int32 top, int32 bottom) 83 { 84 //debug_printf("%p->BasicTerminalBuffer::_Invalidate(%ld, %ld)\n", this, top, bottom); 85 fDirtyInfo.ExtendDirtyRegion(top, bottom); 86 87 if (!fDirtyInfo.messageSent) { 88 NotifyListener(); 89 fDirtyInfo.messageSent = true; 90 } 91 } 92 93 94 inline void 95 BasicTerminalBuffer::_CursorChanged() 96 { 97 if (!fDirtyInfo.messageSent) { 98 NotifyListener(); 99 fDirtyInfo.messageSent = true; 100 } 101 } 102 103 104 // #pragma mark - public methods 105 106 107 BasicTerminalBuffer::BasicTerminalBuffer() 108 : 109 fWidth(0), 110 fHeight(0), 111 fScrollTop(0), 112 fScrollBottom(0), 113 fScreen(NULL), 114 fScreenOffset(0), 115 fHistory(NULL), 116 fAttributes(), 117 fSoftWrappedCursor(false), 118 fOverwriteMode(false), 119 fAlternateScreenActive(false), 120 fOriginMode(false), 121 fSavedOriginMode(false), 122 fTabStops(NULL), 123 fEncoding(M_UTF8), 124 fCaptureFile(-1), 125 fLast() 126 { 127 } 128 129 130 BasicTerminalBuffer::~BasicTerminalBuffer() 131 { 132 delete fHistory; 133 _FreeLines(fScreen, fHeight); 134 delete[] fTabStops; 135 136 if (fCaptureFile >= 0) 137 close(fCaptureFile); 138 } 139 140 141 status_t 142 BasicTerminalBuffer::Init(int32 width, int32 height, int32 historySize) 143 { 144 status_t error; 145 146 fWidth = width; 147 fHeight = height; 148 149 fScrollTop = 0; 150 fScrollBottom = fHeight - 1; 151 152 fCursor.x = 0; 153 fCursor.y = 0; 154 fSoftWrappedCursor = false; 155 156 fScreenOffset = 0; 157 158 fOverwriteMode = true; 159 fAlternateScreenActive = false; 160 fOriginMode = fSavedOriginMode = false; 161 162 fScreen = _AllocateLines(width, height); 163 if (fScreen == NULL) 164 return B_NO_MEMORY; 165 166 if (historySize > 0) { 167 fHistory = new(std::nothrow) HistoryBuffer; 168 if (fHistory == NULL) 169 return B_NO_MEMORY; 170 171 error = fHistory->Init(width, historySize); 172 if (error != B_OK) 173 return error; 174 } 175 176 error = _ResetTabStops(fWidth); 177 if (error != B_OK) 178 return error; 179 180 for (int32 i = 0; i < fHeight; i++) 181 fScreen[i]->Clear(); 182 183 fDirtyInfo.Reset(); 184 185 return B_OK; 186 } 187 188 189 status_t 190 BasicTerminalBuffer::ResizeTo(int32 width, int32 height) 191 { 192 return ResizeTo(width, height, fHistory != NULL ? fHistory->Capacity() : 0); 193 } 194 195 196 status_t 197 BasicTerminalBuffer::ResizeTo(int32 width, int32 height, int32 historyCapacity) 198 { 199 if (height < kMinRowCount || height > kMaxRowCount 200 || width < kMinColumnCount || width > kMaxColumnCount) { 201 return B_BAD_VALUE; 202 } 203 204 if (width == fWidth && height == fHeight) 205 return SetHistoryCapacity(historyCapacity); 206 207 if (fAlternateScreenActive) 208 return _ResizeSimple(width, height, historyCapacity); 209 210 return _ResizeRewrap(width, height, historyCapacity); 211 } 212 213 214 status_t 215 BasicTerminalBuffer::SetHistoryCapacity(int32 historyCapacity) 216 { 217 return _ResizeHistory(fWidth, historyCapacity); 218 } 219 220 221 void 222 BasicTerminalBuffer::Clear(bool resetCursor) 223 { 224 fSoftWrappedCursor = false; 225 fScreenOffset = 0; 226 _ClearLines(0, fHeight - 1); 227 228 if (resetCursor) 229 fCursor.SetTo(0, 0); 230 231 if (fHistory != NULL) 232 fHistory->Clear(); 233 234 fDirtyInfo.linesScrolled = 0; 235 _Invalidate(0, fHeight - 1); 236 } 237 238 239 void 240 BasicTerminalBuffer::SynchronizeWith(const BasicTerminalBuffer* other, 241 int32 offset, int32 dirtyTop, int32 dirtyBottom) 242 { 243 //debug_printf("BasicTerminalBuffer::SynchronizeWith(%p, %ld, %ld - %ld)\n", 244 //other, offset, dirtyTop, dirtyBottom); 245 246 // intersect the visible region with the dirty region 247 int32 first = 0; 248 int32 last = fHeight - 1; 249 dirtyTop -= offset; 250 dirtyBottom -= offset; 251 252 if (first > dirtyBottom || dirtyTop > last) 253 return; 254 255 if (first < dirtyTop) 256 first = dirtyTop; 257 if (last > dirtyBottom) 258 last = dirtyBottom; 259 260 // update the dirty lines 261 //debug_printf(" updating: %ld - %ld\n", first, last); 262 for (int32 i = first; i <= last; i++) { 263 TerminalLine* destLine = _LineAt(i); 264 TerminalLine* sourceLine = other->_HistoryLineAt(i + offset, destLine); 265 if (sourceLine != NULL) { 266 if (sourceLine != destLine) { 267 destLine->length = sourceLine->length; 268 destLine->attributes = sourceLine->attributes; 269 destLine->softBreak = sourceLine->softBreak; 270 if (destLine->length > 0) { 271 memcpy(destLine->cells, sourceLine->cells, 272 fWidth * sizeof(TerminalCell)); 273 } 274 } else { 275 // The source line was a history line and has been copied 276 // directly into destLine. 277 } 278 } else 279 destLine->Clear(fAttributes, fWidth); 280 } 281 } 282 283 284 bool 285 BasicTerminalBuffer::IsFullWidthChar(int32 row, int32 column) const 286 { 287 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 288 TerminalLine* line = _HistoryLineAt(row, lineBuffer); 289 return line != NULL && column > 0 && column < line->length 290 && line->cells[column - 1].attributes.IsWidth(); 291 } 292 293 294 int 295 BasicTerminalBuffer::GetChar(int32 row, int32 column, UTF8Char& character, 296 Attributes& attributes) const 297 { 298 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 299 TerminalLine* line = _HistoryLineAt(row, lineBuffer); 300 if (line == NULL) 301 return NO_CHAR; 302 303 if (column < 0 || column >= line->length) 304 return NO_CHAR; 305 306 if (column > 0 && line->cells[column - 1].attributes.IsWidth()) 307 return IN_STRING; 308 309 TerminalCell& cell = line->cells[column]; 310 character = cell.character; 311 attributes = cell.attributes; 312 return A_CHAR; 313 } 314 315 316 void 317 BasicTerminalBuffer::GetCellAttributes(int32 row, int32 column, 318 Attributes& attributes, uint32& count) const 319 { 320 count = 0; 321 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 322 TerminalLine* line = _HistoryLineAt(row, lineBuffer); 323 if (line == NULL || column < 0) 324 return; 325 326 int32 c = column; 327 for (; c < fWidth; c++) { 328 TerminalCell& cell = line->cells[c]; 329 if (c > column && attributes != cell.attributes) 330 break; 331 attributes = cell.attributes; 332 } 333 count = c - column; 334 } 335 336 337 int32 338 BasicTerminalBuffer::GetString(int32 row, int32 firstColumn, int32 lastColumn, 339 char* buffer, Attributes& attributes) const 340 { 341 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 342 TerminalLine* line = _HistoryLineAt(row, lineBuffer); 343 if (line == NULL) 344 return 0; 345 346 if (lastColumn >= line->length) 347 lastColumn = line->length - 1; 348 349 int32 column = firstColumn; 350 if (column <= lastColumn) 351 attributes = line->cells[column].attributes; 352 353 for (; column <= lastColumn; column++) { 354 TerminalCell& cell = line->cells[column]; 355 if (cell.attributes != attributes) 356 break; 357 358 int32 bytes = cell.character.ByteCount(); 359 for (int32 i = 0; i < bytes; i++) 360 *buffer++ = cell.character.bytes[i]; 361 } 362 363 *buffer = '\0'; 364 365 return column - firstColumn; 366 } 367 368 369 void 370 BasicTerminalBuffer::GetStringFromRegion(BString& string, const TermPos& start, 371 const TermPos& end) const 372 { 373 //debug_printf("BasicTerminalBuffer::GetStringFromRegion((%ld, %ld), (%ld, %ld))\n", 374 //start.x, start.y, end.x, end.y); 375 if (start >= end) 376 return; 377 378 TermPos pos(start); 379 380 if (IsFullWidthChar(pos.y, pos.x)) 381 pos.x--; 382 383 // get all but the last line 384 while (pos.y < end.y) { 385 TerminalLine* line = _GetPartialLineString(string, pos.y, pos.x, 386 fWidth); 387 if (line != NULL && !line->softBreak) 388 string.Append('\n', 1); 389 pos.x = 0; 390 pos.y++; 391 } 392 393 // get the last line, if not empty 394 if (end.x > 0) 395 _GetPartialLineString(string, end.y, pos.x, end.x); 396 } 397 398 399 bool 400 BasicTerminalBuffer::FindWord(const TermPos& pos, 401 TerminalCharClassifier* classifier, bool findNonWords, TermPos& _start, 402 TermPos& _end) const 403 { 404 int32 x = pos.x; 405 int32 y = pos.y; 406 407 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 408 TerminalLine* line = _HistoryLineAt(y, lineBuffer); 409 if (line == NULL || x < 0 || x >= fWidth) 410 return false; 411 412 if (x >= line->length) { 413 // beyond the end of the line -- select all space 414 if (!findNonWords) 415 return false; 416 417 _start.SetTo(line->length, y); 418 _end.SetTo(fWidth, y); 419 return true; 420 } 421 422 if (x > 0 && line->cells[x - 1].attributes.IsWidth()) 423 x--; 424 425 // get the char type at the given position 426 int type = classifier->Classify(line->cells[x].character); 427 428 // check whether we are supposed to find words only 429 if (type != CHAR_TYPE_WORD_CHAR && !findNonWords) 430 return false; 431 432 // find the beginning 433 TermPos start(x, y); 434 TermPos end(x + (line->cells[x].attributes.IsWidth() 435 ? FULL_WIDTH : HALF_WIDTH), y); 436 for (;;) { 437 TermPos previousPos = start; 438 if (!_PreviousLinePos(lineBuffer, line, previousPos) 439 || classifier->Classify(line->cells[previousPos.x].character) 440 != type) { 441 break; 442 } 443 444 start = previousPos; 445 } 446 447 // find the end 448 line = _HistoryLineAt(end.y, lineBuffer); 449 450 for (;;) { 451 TermPos nextPos = end; 452 if (!_NormalizeLinePos(lineBuffer, line, nextPos)) 453 break; 454 455 if (classifier->Classify(line->cells[nextPos.x].character) != type) 456 break; 457 458 nextPos.x += line->cells[nextPos.x].attributes.IsWidth() 459 ? FULL_WIDTH : HALF_WIDTH; 460 end = nextPos; 461 } 462 463 _start = start; 464 _end = end; 465 return true; 466 } 467 468 469 bool 470 BasicTerminalBuffer::PreviousLinePos(TermPos& pos) const 471 { 472 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 473 TerminalLine* line = _HistoryLineAt(pos.y, lineBuffer); 474 if (line == NULL || pos.x < 0 || pos.x >= fWidth) 475 return false; 476 477 return _PreviousLinePos(lineBuffer, line, pos); 478 } 479 480 481 bool 482 BasicTerminalBuffer::NextLinePos(TermPos& pos, bool normalize) const 483 { 484 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 485 TerminalLine* line = _HistoryLineAt(pos.y, lineBuffer); 486 if (line == NULL || pos.x < 0 || pos.x > fWidth) 487 return false; 488 489 if (!_NormalizeLinePos(lineBuffer, line, pos)) 490 return false; 491 492 pos.x += line->cells[pos.x].attributes.IsWidth() ? FULL_WIDTH : HALF_WIDTH; 493 return !normalize || _NormalizeLinePos(lineBuffer, line, pos); 494 } 495 496 497 int32 498 BasicTerminalBuffer::LineLength(int32 index) const 499 { 500 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 501 TerminalLine* line = _HistoryLineAt(index, lineBuffer); 502 return line != NULL ? line->length : 0; 503 } 504 505 506 void 507 BasicTerminalBuffer::GetLineColor(int32 index, Attributes& attr) const 508 { 509 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 510 TerminalLine* line = _HistoryLineAt(index, lineBuffer); 511 if (line != NULL) 512 attr = line->attributes; 513 else 514 attr.Reset(); 515 } 516 517 518 bool 519 BasicTerminalBuffer::Find(const char* _pattern, const TermPos& start, 520 bool forward, bool caseSensitive, bool matchWord, TermPos& _matchStart, 521 TermPos& _matchEnd) const 522 { 523 //debug_printf("BasicTerminalBuffer::Find(\"%s\", (%ld, %ld), forward: %d, case: %d, " 524 //"word: %d)\n", _pattern, start.x, start.y, forward, caseSensitive, matchWord); 525 // normalize pos, so that _NextChar() and _PreviousChar() are happy 526 TermPos pos(start); 527 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 528 TerminalLine* line = _HistoryLineAt(pos.y, lineBuffer); 529 if (line != NULL) { 530 if (forward) { 531 while (line != NULL && pos.x >= line->length && line->softBreak) { 532 pos.x = 0; 533 pos.y++; 534 line = _HistoryLineAt(pos.y, lineBuffer); 535 } 536 } else { 537 if (pos.x > line->length) 538 pos.x = line->length; 539 } 540 } 541 542 int32 patternByteLen = strlen(_pattern); 543 544 // convert pattern to UTF8Char array 545 BStackOrHeapArray<UTF8Char, 64> pattern(patternByteLen); 546 if (!pattern.IsValid()) 547 return false; 548 int32 patternLen = 0; 549 while (*_pattern != '\0') { 550 int32 charLen = UTF8Char::ByteCount(*_pattern); 551 if (charLen > 0) { 552 pattern[patternLen].SetTo(_pattern, charLen); 553 554 // if not case sensitive, convert to lower case 555 if (!caseSensitive && charLen == 1) 556 pattern[patternLen] = pattern[patternLen].ToLower(); 557 558 patternLen++; 559 _pattern += charLen; 560 } else 561 _pattern++; 562 } 563 //debug_printf(" pattern byte len: %ld, pattern len: %ld\n", patternByteLen, patternLen); 564 565 if (patternLen == 0) 566 return false; 567 568 // reverse pattern, if searching backward 569 if (!forward) { 570 for (int32 i = 0; i < patternLen / 2; i++) 571 std::swap(pattern[i], pattern[patternLen - i - 1]); 572 } 573 574 // search loop 575 int32 matchIndex = 0; 576 TermPos matchStart; 577 while (true) { 578 //debug_printf(" (%ld, %ld): matchIndex: %ld\n", pos.x, pos.y, matchIndex); 579 TermPos previousPos(pos); 580 UTF8Char c; 581 if (!(forward ? _NextChar(pos, c) : _PreviousChar(pos, c))) 582 return false; 583 584 if (caseSensitive ? (c == pattern[matchIndex]) 585 : (c.ToLower() == pattern[matchIndex])) { 586 if (matchIndex == 0) 587 matchStart = previousPos; 588 589 matchIndex++; 590 591 if (matchIndex == patternLen) { 592 //debug_printf(" match!\n"); 593 // compute the match range 594 TermPos matchEnd(pos); 595 if (!forward) 596 std::swap(matchStart, matchEnd); 597 598 // check word match 599 if (matchWord) { 600 TermPos tempPos(matchStart); 601 if ((_PreviousChar(tempPos, c) && !c.IsSpace()) 602 || (_NextChar(tempPos = matchEnd, c) && !c.IsSpace())) { 603 //debug_printf(" but no word match!\n"); 604 continue; 605 } 606 } 607 608 _matchStart = matchStart; 609 _matchEnd = matchEnd; 610 //debug_printf(" -> (%ld, %ld) - (%ld, %ld)\n", matchStart.x, matchStart.y, 611 //matchEnd.x, matchEnd.y); 612 return true; 613 } 614 } else if (matchIndex > 0) { 615 // continue after the position where we started matching 616 pos = matchStart; 617 if (forward) 618 _NextChar(pos, c); 619 else 620 _PreviousChar(pos, c); 621 matchIndex = 0; 622 } 623 } 624 } 625 626 627 void 628 BasicTerminalBuffer::InsertChar(UTF8Char c) 629 { 630 //debug_printf("BasicTerminalBuffer::InsertChar('%.*s' (%d), %#lx)\n", 631 //(int)c.ByteCount(), c.bytes, c.bytes[0], attributes); 632 fLast = c; 633 int32 width = c.IsFullWidth() ? FULL_WIDTH : HALF_WIDTH; 634 635 if (fSoftWrappedCursor || (fCursor.x + width) > fWidth) 636 _SoftBreakLine(); 637 else 638 _PadLineToCursor(); 639 640 fSoftWrappedCursor = false; 641 642 if (!fOverwriteMode) 643 _InsertGap(width); 644 645 TerminalLine* line = _LineAt(fCursor.y); 646 line->cells[fCursor.x].character = c; 647 line->cells[fCursor.x].attributes = fAttributes; 648 line->cells[fCursor.x].attributes.state |= (width == FULL_WIDTH ? A_WIDTH : 0); 649 650 if (line->length < fCursor.x + width) 651 line->length = fCursor.x + width; 652 653 _Invalidate(fCursor.y, fCursor.y); 654 655 fCursor.x += width; 656 657 // TODO: Deal correctly with full-width chars! We must take care not to 658 // overwrite half of a full-width char. This holds also for other methods. 659 660 if (fCursor.x == fWidth) { 661 fCursor.x -= width; 662 fSoftWrappedCursor = true; 663 } 664 } 665 666 667 void 668 BasicTerminalBuffer::FillScreen(UTF8Char c, Attributes &attributes) 669 { 670 uint32 width = HALF_WIDTH; 671 if (c.IsFullWidth()) { 672 attributes |= A_WIDTH; 673 width = FULL_WIDTH; 674 } 675 676 fSoftWrappedCursor = false; 677 678 for (int32 y = 0; y < fHeight; y++) { 679 TerminalLine *line = _LineAt(y); 680 for (int32 x = 0; x < fWidth / (int32)width; x++) { 681 line->cells[x].character = c; 682 line->cells[x].attributes = attributes; 683 } 684 line->length = fWidth / width; 685 } 686 687 _Invalidate(0, fHeight - 1); 688 } 689 690 691 void 692 BasicTerminalBuffer::InsertCR() 693 { 694 TerminalLine* line = _LineAt(fCursor.y); 695 696 line->attributes = fAttributes; 697 line->softBreak = false; 698 fSoftWrappedCursor = false; 699 fCursor.x = 0; 700 _Invalidate(fCursor.y, fCursor.y); 701 _CursorChanged(); 702 } 703 704 705 void 706 BasicTerminalBuffer::InsertLF() 707 { 708 fSoftWrappedCursor = false; 709 710 // If we're at the end of the scroll region, scroll. Otherwise just advance 711 // the cursor. 712 if (fCursor.y == fScrollBottom) { 713 _Scroll(fScrollTop, fScrollBottom, 1); 714 } else { 715 if (fCursor.y < fHeight - 1) 716 fCursor.y++; 717 _CursorChanged(); 718 } 719 } 720 721 722 void 723 BasicTerminalBuffer::InsertRI() 724 { 725 fSoftWrappedCursor = false; 726 727 // If we're at the beginning of the scroll region, scroll. Otherwise just 728 // reverse the cursor. 729 if (fCursor.y == fScrollTop) { 730 _Scroll(fScrollTop, fScrollBottom, -1); 731 } else { 732 if (fCursor.y > 0) 733 fCursor.y--; 734 _CursorChanged(); 735 } 736 } 737 738 739 void 740 BasicTerminalBuffer::InsertTab() 741 { 742 int32 x; 743 744 fSoftWrappedCursor = false; 745 746 // Find the next tab stop 747 for (x = fCursor.x + 1; x < fWidth && !fTabStops[x]; x++) 748 ; 749 // Ensure x stayx within the line bounds 750 x = restrict_value(x, 0, fWidth - 1); 751 752 if (x != fCursor.x) { 753 TerminalLine* line = _LineAt(fCursor.y); 754 for (int32 i = fCursor.x; i <= x; i++) { 755 if (line->length <= i) { 756 line->cells[i].character = ' '; 757 line->cells[i].attributes = fAttributes; 758 } 759 } 760 fCursor.x = x; 761 if (line->length < fCursor.x) 762 line->length = fCursor.x; 763 _CursorChanged(); 764 } 765 } 766 767 768 void 769 BasicTerminalBuffer::InsertCursorBackTab(int32 numTabs) 770 { 771 int32 x = fCursor.x - 1; 772 773 fSoftWrappedCursor = false; 774 775 // Find the next tab stop 776 while (numTabs-- > 0) 777 for (; x >=0 && !fTabStops[x]; x--) 778 ; 779 // Ensure x stays within the line bounds 780 x = restrict_value(x, 0, fWidth - 1); 781 782 if (x != fCursor.x) { 783 fCursor.x = x; 784 _CursorChanged(); 785 } 786 } 787 788 789 void 790 BasicTerminalBuffer::InsertLines(int32 numLines) 791 { 792 if (fCursor.y >= fScrollTop && fCursor.y < fScrollBottom) { 793 fSoftWrappedCursor = false; 794 _Scroll(fCursor.y, fScrollBottom, -numLines); 795 } 796 } 797 798 799 void 800 BasicTerminalBuffer::SetInsertMode(int flag) 801 { 802 fOverwriteMode = flag == MODE_OVER; 803 } 804 805 806 void 807 BasicTerminalBuffer::InsertSpace(int32 num) 808 { 809 // TODO: Deal with full-width chars! 810 if (fCursor.x + num > fWidth) 811 num = fWidth - fCursor.x; 812 813 if (num > 0) { 814 fSoftWrappedCursor = false; 815 _PadLineToCursor(); 816 _InsertGap(num); 817 818 TerminalLine* line = _LineAt(fCursor.y); 819 for (int32 i = fCursor.x; i < fCursor.x + num; i++) { 820 line->cells[i].character = kSpaceChar; 821 line->cells[i].attributes = line->cells[fCursor.x - 1].attributes; 822 } 823 line->attributes = fAttributes; 824 825 _Invalidate(fCursor.y, fCursor.y); 826 } 827 } 828 829 830 void 831 BasicTerminalBuffer::EraseCharsFrom(int32 first, int32 numChars) 832 { 833 TerminalLine* line = _LineAt(fCursor.y); 834 835 int32 end = min_c(first + numChars, fWidth); 836 for (int32 i = first; i < end; i++) 837 line->cells[i].attributes = fAttributes; 838 839 line->attributes = fAttributes; 840 841 fSoftWrappedCursor = false; 842 843 end = min_c(first + numChars, line->length); 844 if (first > 0 && line->cells[first - 1].attributes.IsWidth()) 845 first--; 846 if (end > 0 && line->cells[end - 1].attributes.IsWidth()) 847 end++; 848 849 for (int32 i = first; i < end; i++) { 850 line->cells[i].character = kSpaceChar; 851 line->cells[i].attributes = fAttributes; 852 } 853 854 _Invalidate(fCursor.y, fCursor.y); 855 } 856 857 858 void 859 BasicTerminalBuffer::EraseAbove() 860 { 861 // Clear the preceding lines. 862 if (fCursor.y > 0) 863 _ClearLines(0, fCursor.y - 1); 864 865 fSoftWrappedCursor = false; 866 867 // Delete the chars on the cursor line before (and including) the cursor. 868 TerminalLine* line = _LineAt(fCursor.y); 869 if (fCursor.x < line->length) { 870 int32 to = fCursor.x; 871 if (line->cells[fCursor.x].attributes.IsWidth()) 872 to++; 873 for (int32 i = 0; i <= to; i++) { 874 line->cells[i].attributes = fAttributes; 875 line->cells[i].character = kSpaceChar; 876 } 877 } else 878 line->Clear(fAttributes, fWidth); 879 880 _Invalidate(fCursor.y, fCursor.y); 881 } 882 883 884 void 885 BasicTerminalBuffer::EraseBelow() 886 { 887 fSoftWrappedCursor = false; 888 889 // Clear the following lines. 890 if (fCursor.y < fHeight - 1) 891 _ClearLines(fCursor.y + 1, fHeight - 1); 892 893 // Delete the chars on the cursor line after (and including) the cursor. 894 DeleteColumns(); 895 } 896 897 898 void 899 BasicTerminalBuffer::EraseAll() 900 { 901 fSoftWrappedCursor = false; 902 _Scroll(0, fHeight - 1, fHeight); 903 } 904 905 906 void 907 BasicTerminalBuffer::DeleteChars(int32 numChars) 908 { 909 fSoftWrappedCursor = false; 910 911 TerminalLine* line = _LineAt(fCursor.y); 912 if (fCursor.x < line->length) { 913 if (fCursor.x + numChars < line->length) { 914 int32 left = line->length - fCursor.x - numChars; 915 memmove(line->cells + fCursor.x, line->cells + fCursor.x + numChars, 916 left * sizeof(TerminalCell)); 917 line->length = fCursor.x + left; 918 // process BCE on freed tail cells 919 for (int i = 0; i < numChars; i++) 920 line->cells[fCursor.x + left + i].attributes = fAttributes; 921 } else { 922 // process BCE on freed tail cells 923 for (int i = 0; i < line->length - fCursor.x; i++) 924 line->cells[fCursor.x + i].attributes = fAttributes; 925 // remove all remaining chars 926 line->length = fCursor.x; 927 } 928 929 _Invalidate(fCursor.y, fCursor.y); 930 } 931 } 932 933 934 void 935 BasicTerminalBuffer::DeleteColumnsFrom(int32 first) 936 { 937 fSoftWrappedCursor = false; 938 939 TerminalLine* line = _LineAt(fCursor.y); 940 941 for (int32 i = first; i < fWidth; i++) 942 line->cells[i].attributes = fAttributes; 943 944 if (first <= line->length) { 945 line->length = first; 946 line->attributes = fAttributes; 947 } 948 _Invalidate(fCursor.y, fCursor.y); 949 } 950 951 952 void 953 BasicTerminalBuffer::DeleteLines(int32 numLines) 954 { 955 if (fCursor.y >= fScrollTop && fCursor.y <= fScrollBottom) { 956 fSoftWrappedCursor = false; 957 _Scroll(fCursor.y, fScrollBottom, numLines); 958 } 959 } 960 961 962 void 963 BasicTerminalBuffer::SaveCursor() 964 { 965 fSavedCursors.push(fCursor); 966 } 967 968 969 void 970 BasicTerminalBuffer::RestoreCursor() 971 { 972 if (fSavedCursors.size() == 0) 973 return; 974 975 _SetCursor(fSavedCursors.top().x, fSavedCursors.top().y, true); 976 fSavedCursors.pop(); 977 } 978 979 980 void 981 BasicTerminalBuffer::SetScrollRegion(int32 top, int32 bottom) 982 { 983 fScrollTop = restrict_value(top, 0, fHeight - 1); 984 fScrollBottom = restrict_value(bottom, fScrollTop, fHeight - 1); 985 986 // also sets the cursor position 987 _SetCursor(0, 0, false); 988 } 989 990 991 void 992 BasicTerminalBuffer::SetOriginMode(bool enabled) 993 { 994 fOriginMode = enabled; 995 _SetCursor(0, 0, false); 996 } 997 998 999 void 1000 BasicTerminalBuffer::SaveOriginMode() 1001 { 1002 fSavedOriginMode = fOriginMode; 1003 } 1004 1005 1006 void 1007 BasicTerminalBuffer::RestoreOriginMode() 1008 { 1009 fOriginMode = fSavedOriginMode; 1010 } 1011 1012 1013 void 1014 BasicTerminalBuffer::SetTabStop(int32 x) 1015 { 1016 x = restrict_value(x, 0, fWidth - 1); 1017 fTabStops[x] = true; 1018 } 1019 1020 1021 void 1022 BasicTerminalBuffer::ClearTabStop(int32 x) 1023 { 1024 x = restrict_value(x, 0, fWidth - 1); 1025 fTabStops[x] = false; 1026 } 1027 1028 1029 void 1030 BasicTerminalBuffer::ClearAllTabStops() 1031 { 1032 for (int32 i = 0; i < fWidth; i++) 1033 fTabStops[i] = false; 1034 } 1035 1036 1037 void 1038 BasicTerminalBuffer::NotifyListener() 1039 { 1040 // Implemented by derived classes. 1041 } 1042 1043 1044 // #pragma mark - private methods 1045 1046 1047 void 1048 BasicTerminalBuffer::_SetCursor(int32 x, int32 y, bool absolute) 1049 { 1050 //debug_printf("BasicTerminalBuffer::_SetCursor(%d, %d)\n", x, y); 1051 fSoftWrappedCursor = false; 1052 1053 x = restrict_value(x, 0, fWidth - 1); 1054 if (fOriginMode && !absolute) { 1055 y += fScrollTop; 1056 y = restrict_value(y, fScrollTop, fScrollBottom); 1057 } else { 1058 y = restrict_value(y, 0, fHeight - 1); 1059 } 1060 1061 if (x != fCursor.x || y != fCursor.y) { 1062 fCursor.x = x; 1063 fCursor.y = y; 1064 _CursorChanged(); 1065 } 1066 } 1067 1068 1069 void 1070 BasicTerminalBuffer::_InvalidateAll() 1071 { 1072 fDirtyInfo.invalidateAll = true; 1073 1074 if (!fDirtyInfo.messageSent) { 1075 NotifyListener(); 1076 fDirtyInfo.messageSent = true; 1077 } 1078 } 1079 1080 1081 /* static */ TerminalLine** 1082 BasicTerminalBuffer::_AllocateLines(int32 width, int32 count) 1083 { 1084 TerminalLine** lines = (TerminalLine**)malloc(sizeof(TerminalLine*) * count); 1085 if (lines == NULL) 1086 return NULL; 1087 1088 for (int32 i = 0; i < count; i++) { 1089 const int32 size = sizeof(TerminalLine) 1090 + sizeof(TerminalCell) * (width - 1); 1091 lines[i] = (TerminalLine*)malloc(size); 1092 if (lines[i] == NULL) { 1093 _FreeLines(lines, i); 1094 return NULL; 1095 } 1096 lines[i]->Clear(width); 1097 } 1098 1099 return lines; 1100 } 1101 1102 1103 /* static */ void 1104 BasicTerminalBuffer::_FreeLines(TerminalLine** lines, int32 count) 1105 { 1106 if (lines != NULL) { 1107 for (int32 i = 0; i < count; i++) 1108 free(lines[i]); 1109 1110 free(lines); 1111 } 1112 } 1113 1114 1115 void 1116 BasicTerminalBuffer::_ClearLines(int32 first, int32 last) 1117 { 1118 int32 firstCleared = -1; 1119 int32 lastCleared = -1; 1120 1121 for (int32 i = first; i <= last; i++) { 1122 TerminalLine* line = _LineAt(i); 1123 if (line->length > 0) { 1124 if (firstCleared == -1) 1125 firstCleared = i; 1126 lastCleared = i; 1127 } 1128 1129 line->Clear(fAttributes, fWidth); 1130 } 1131 1132 if (firstCleared >= 0) 1133 _Invalidate(firstCleared, lastCleared); 1134 } 1135 1136 1137 status_t 1138 BasicTerminalBuffer::_ResizeHistory(int32 width, int32 historyCapacity) 1139 { 1140 if (width == fWidth && historyCapacity == HistoryCapacity()) 1141 return B_OK; 1142 1143 if (historyCapacity <= 0) { 1144 // new history capacity is 0 -- delete the old history object 1145 delete fHistory; 1146 fHistory = NULL; 1147 1148 return B_OK; 1149 } 1150 1151 HistoryBuffer* history = new(std::nothrow) HistoryBuffer; 1152 if (history == NULL) 1153 return B_NO_MEMORY; 1154 1155 status_t error = history->Init(width, historyCapacity); 1156 if (error != B_OK) { 1157 delete history; 1158 return error; 1159 } 1160 1161 // Transfer the lines from the old history to the new one. 1162 if (fHistory != NULL) { 1163 int32 historySize = min_c(HistorySize(), historyCapacity); 1164 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 1165 for (int32 i = historySize - 1; i >= 0; i--) { 1166 TerminalLine* line = fHistory->GetTerminalLineAt(i, lineBuffer); 1167 if (line->length > width) 1168 _TruncateLine(line, width); 1169 history->AddLine(line); 1170 } 1171 } 1172 1173 delete fHistory; 1174 fHistory = history; 1175 1176 return B_OK; 1177 } 1178 1179 1180 status_t 1181 BasicTerminalBuffer::_ResizeSimple(int32 width, int32 height, 1182 int32 historyCapacity) 1183 { 1184 //debug_printf("BasicTerminalBuffer::_ResizeSimple(): (%ld, %ld) -> " 1185 //"(%ld, %ld)\n", fWidth, fHeight, width, height); 1186 if (width == fWidth && height == fHeight) 1187 return B_OK; 1188 1189 if (width != fWidth || historyCapacity != HistoryCapacity()) { 1190 status_t error = _ResizeHistory(width, historyCapacity); 1191 if (error != B_OK) 1192 return error; 1193 } 1194 1195 TerminalLine** lines = _AllocateLines(width, height); 1196 if (lines == NULL) 1197 return B_NO_MEMORY; 1198 // NOTE: If width or history capacity changed, the object will be in 1199 // an invalid state, since the history will already use the new values. 1200 1201 int32 endLine = min_c(fHeight, height); 1202 int32 firstLine = 0; 1203 1204 if (height < fHeight) { 1205 if (endLine <= fCursor.y) { 1206 endLine = fCursor.y + 1; 1207 firstLine = endLine - height; 1208 } 1209 1210 // push the first lines to the history 1211 if (fHistory != NULL) { 1212 for (int32 i = 0; i < firstLine; i++) { 1213 TerminalLine* line = _LineAt(i); 1214 if (width < fWidth) 1215 _TruncateLine(line, width); 1216 fHistory->AddLine(line); 1217 } 1218 } 1219 } 1220 1221 // copy the lines we keep 1222 for (int32 i = firstLine; i < endLine; i++) { 1223 TerminalLine* sourceLine = _LineAt(i); 1224 TerminalLine* destLine = lines[i - firstLine]; 1225 if (width < fWidth) 1226 _TruncateLine(sourceLine, width); 1227 memcpy(destLine, sourceLine, (int32)sizeof(TerminalLine) 1228 + (sourceLine->length - 1) * (int32)sizeof(TerminalCell)); 1229 } 1230 1231 // clear the remaining lines 1232 for (int32 i = endLine - firstLine; i < height; i++) 1233 lines[i]->Clear(fAttributes, width); 1234 1235 _FreeLines(fScreen, fHeight); 1236 fScreen = lines; 1237 1238 if (fWidth != width) { 1239 status_t error = _ResetTabStops(width); 1240 if (error != B_OK) 1241 return error; 1242 } 1243 1244 fWidth = width; 1245 fHeight = height; 1246 1247 fScrollTop = 0; 1248 fScrollBottom = fHeight - 1; 1249 fOriginMode = fSavedOriginMode = false; 1250 1251 fScreenOffset = 0; 1252 1253 if (fCursor.x > width) 1254 fCursor.x = width; 1255 fCursor.y -= firstLine; 1256 fSoftWrappedCursor = false; 1257 1258 return B_OK; 1259 } 1260 1261 1262 status_t 1263 BasicTerminalBuffer::_ResizeRewrap(int32 width, int32 height, 1264 int32 historyCapacity) 1265 { 1266 //debug_printf("BasicTerminalBuffer::_ResizeRewrap(): (%ld, %ld, history: %ld) -> " 1267 //"(%ld, %ld, history: %ld)\n", fWidth, fHeight, HistoryCapacity(), width, height, 1268 //historyCapacity); 1269 1270 // The width stays the same. _ResizeSimple() does exactly what we need. 1271 if (width == fWidth) 1272 return _ResizeSimple(width, height, historyCapacity); 1273 1274 // The width changes. We have to allocate a new line array, a new history 1275 // and re-wrap all lines. 1276 1277 TerminalLine** screen = _AllocateLines(width, height); 1278 if (screen == NULL) 1279 return B_NO_MEMORY; 1280 1281 HistoryBuffer* history = NULL; 1282 1283 if (historyCapacity > 0) { 1284 history = new(std::nothrow) HistoryBuffer; 1285 if (history == NULL) { 1286 _FreeLines(screen, height); 1287 return B_NO_MEMORY; 1288 } 1289 1290 status_t error = history->Init(width, historyCapacity); 1291 if (error != B_OK) { 1292 _FreeLines(screen, height); 1293 delete history; 1294 return error; 1295 } 1296 } 1297 1298 int32 historySize = HistorySize(); 1299 int32 totalLines = historySize + fHeight; 1300 1301 // re-wrap 1302 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 1303 TermPos cursor; 1304 int32 destIndex = 0; 1305 int32 sourceIndex = 0; 1306 int32 sourceX = 0; 1307 int32 destTotalLines = 0; 1308 int32 destScreenOffset = 0; 1309 int32 maxDestTotalLines = INT_MAX; 1310 bool newDestLine = true; 1311 bool cursorSeen = false; 1312 TerminalLine* sourceLine = _HistoryLineAt(-historySize, lineBuffer); 1313 1314 while (sourceIndex < totalLines) { 1315 TerminalLine* destLine = screen[destIndex]; 1316 1317 if (newDestLine) { 1318 // Clear a new dest line before using it. If we're about to 1319 // overwrite an previously written line, we push it to the 1320 // history first, though. 1321 if (history != NULL && destTotalLines >= height) 1322 history->AddLine(screen[destIndex]); 1323 destLine->Clear(fAttributes, width); 1324 newDestLine = false; 1325 } 1326 1327 int32 sourceLeft = sourceLine->length - sourceX; 1328 int32 destLeft = width - destLine->length; 1329 //debug_printf(" source: %ld, left: %ld, dest: %ld, left: %ld\n", 1330 //sourceIndex, sourceLeft, destIndex, destLeft); 1331 1332 if (sourceIndex == historySize && sourceX == 0) { 1333 destScreenOffset = destTotalLines; 1334 if (destLeft == 0 && sourceLeft > 0) 1335 destScreenOffset++; 1336 maxDestTotalLines = destScreenOffset + height; 1337 //debug_printf(" destScreenOffset: %ld\n", destScreenOffset); 1338 } 1339 1340 int32 toCopy = min_c(sourceLeft, destLeft); 1341 // If the last cell to copy is the first cell of a 1342 // full-width char, don't copy it yet. 1343 if (toCopy > 0 && sourceLine->cells[sourceX + toCopy - 1].attributes.IsWidth()) { 1344 //debug_printf(" -> last char is full-width -- don't copy it\n"); 1345 toCopy--; 1346 } 1347 1348 // translate the cursor position 1349 if (fCursor.y + historySize == sourceIndex 1350 && fCursor.x >= sourceX 1351 && (fCursor.x < sourceX + toCopy 1352 || (destLeft >= sourceLeft 1353 && sourceX + sourceLeft <= fCursor.x))) { 1354 cursor.x = destLine->length + fCursor.x - sourceX; 1355 cursor.y = destTotalLines; 1356 1357 if (cursor.x >= width) { 1358 // The cursor was in free space after the official end 1359 // of line. 1360 cursor.x = width - 1; 1361 } 1362 //debug_printf(" cursor: (%ld, %ld)\n", cursor.x, cursor.y); 1363 1364 cursorSeen = true; 1365 } 1366 1367 if (toCopy > 0) { 1368 memcpy(destLine->cells + destLine->length, 1369 sourceLine->cells + sourceX, toCopy * sizeof(TerminalCell)); 1370 destLine->length += toCopy; 1371 } 1372 1373 destLine->attributes = sourceLine->attributes; 1374 1375 bool nextDestLine = false; 1376 if (toCopy == sourceLeft) { 1377 if (!sourceLine->softBreak) 1378 nextDestLine = true; 1379 sourceIndex++; 1380 sourceX = 0; 1381 sourceLine = _HistoryLineAt(sourceIndex - historySize, 1382 lineBuffer); 1383 } else { 1384 destLine->softBreak = true; 1385 nextDestLine = true; 1386 sourceX += toCopy; 1387 } 1388 1389 if (nextDestLine) { 1390 destIndex = (destIndex + 1) % height; 1391 destTotalLines++; 1392 newDestLine = true; 1393 if (cursorSeen && destTotalLines >= maxDestTotalLines) 1394 break; 1395 } 1396 } 1397 1398 // If the last source line had a soft break, the last dest line 1399 // won't have been counted yet. 1400 if (!newDestLine) { 1401 destIndex = (destIndex + 1) % height; 1402 destTotalLines++; 1403 } 1404 1405 //debug_printf(" total lines: %ld -> %ld\n", totalLines, destTotalLines); 1406 1407 if (destTotalLines - destScreenOffset > height) 1408 destScreenOffset = destTotalLines - height; 1409 1410 cursor.y -= destScreenOffset; 1411 1412 // When there are less lines (starting with the screen offset) than 1413 // there's room in the screen, clear the remaining screen lines. 1414 for (int32 i = destTotalLines; i < destScreenOffset + height; i++) { 1415 // Move the line we're going to clear to the history, if that's a 1416 // line we've written earlier. 1417 TerminalLine* line = screen[i % height]; 1418 if (history != NULL && i >= height) 1419 history->AddLine(line); 1420 line->Clear(fAttributes, width); 1421 } 1422 1423 // Update the values 1424 _FreeLines(fScreen, fHeight); 1425 delete fHistory; 1426 1427 fScreen = screen; 1428 fHistory = history; 1429 1430 if (fWidth != width) { 1431 status_t error = _ResetTabStops(width); 1432 if (error != B_OK) 1433 return error; 1434 } 1435 1436 //debug_printf(" cursor: (%ld, %ld) -> (%ld, %ld)\n", fCursor.x, fCursor.y, 1437 //cursor.x, cursor.y); 1438 fCursor.x = cursor.x; 1439 fCursor.y = cursor.y; 1440 fSoftWrappedCursor = false; 1441 //debug_printf(" screen offset: %ld -> %ld\n", fScreenOffset, destScreenOffset % height); 1442 fScreenOffset = destScreenOffset % height; 1443 //debug_printf(" height %ld -> %ld\n", fHeight, height); 1444 //debug_printf(" width %ld -> %ld\n", fWidth, width); 1445 fHeight = height; 1446 fWidth = width; 1447 1448 fScrollTop = 0; 1449 fScrollBottom = fHeight - 1; 1450 fOriginMode = fSavedOriginMode = false; 1451 1452 return B_OK; 1453 } 1454 1455 1456 status_t 1457 BasicTerminalBuffer::_ResetTabStops(int32 width) 1458 { 1459 if (fTabStops != NULL) 1460 delete[] fTabStops; 1461 1462 fTabStops = new(std::nothrow) bool[width]; 1463 if (fTabStops == NULL) 1464 return B_NO_MEMORY; 1465 1466 for (int32 i = 0; i < width; i++) 1467 fTabStops[i] = (i % TAB_WIDTH) == 0; 1468 return B_OK; 1469 } 1470 1471 1472 void 1473 BasicTerminalBuffer::_Scroll(int32 top, int32 bottom, int32 numLines) 1474 { 1475 if (numLines == 0) 1476 return; 1477 1478 if (numLines > 0) { 1479 // scroll text up 1480 if (top == 0) { 1481 // The lines scrolled out of the screen range are transferred to 1482 // the history. 1483 1484 // add the lines to the history 1485 if (fHistory != NULL) { 1486 int32 toHistory = min_c(numLines, bottom - top + 1); 1487 for (int32 i = 0; i < toHistory; i++) 1488 fHistory->AddLine(_LineAt(i)); 1489 1490 if (toHistory < numLines) 1491 fHistory->AddEmptyLines(numLines - toHistory); 1492 } 1493 1494 if (numLines >= bottom - top + 1) { 1495 // all lines are scrolled out of range -- just clear them 1496 _ClearLines(top, bottom); 1497 } else if (bottom == fHeight - 1) { 1498 // full screen scroll -- update the screen offset and clear new 1499 // lines 1500 fScreenOffset = (fScreenOffset + numLines) % fHeight; 1501 for (int32 i = bottom - numLines + 1; i <= bottom; i++) 1502 _LineAt(i)->Clear(fAttributes, fWidth); 1503 } else { 1504 // Partial screen scroll. We move the screen offset anyway, but 1505 // have to move the unscrolled lines to their new location. 1506 // TODO: It may be more efficient to actually move the scrolled 1507 // lines only (might depend on the number of scrolled/unscrolled 1508 // lines). 1509 for (int32 i = fHeight - 1; i > bottom; i--) { 1510 std::swap(fScreen[_LineIndex(i)], 1511 fScreen[_LineIndex(i + numLines)]); 1512 } 1513 1514 // update the screen offset and clear the new lines 1515 fScreenOffset = (fScreenOffset + numLines) % fHeight; 1516 for (int32 i = bottom - numLines + 1; i <= bottom; i++) 1517 _LineAt(i)->Clear(fAttributes, fWidth); 1518 } 1519 1520 // scroll/extend dirty range 1521 1522 if (fDirtyInfo.dirtyTop != INT_MAX) { 1523 // If the top or bottom of the dirty region are above the 1524 // bottom of the scroll region, we have to scroll them up. 1525 if (fDirtyInfo.dirtyTop <= bottom) { 1526 fDirtyInfo.dirtyTop -= numLines; 1527 if (fDirtyInfo.dirtyBottom <= bottom) 1528 fDirtyInfo.dirtyBottom -= numLines; 1529 } 1530 1531 // numLines above the bottom become dirty 1532 _Invalidate(bottom - numLines + 1, bottom); 1533 } 1534 1535 fDirtyInfo.linesScrolled += numLines; 1536 1537 // invalidate new empty lines 1538 _Invalidate(bottom + 1 - numLines, bottom); 1539 1540 // In case only part of the screen was scrolled, we invalidate also 1541 // the lines below the scroll region. Those remain unchanged, but 1542 // we can't convey that they have not been scrolled via 1543 // TerminalBufferDirtyInfo. So we need to force the view to sync 1544 // them again. 1545 if (bottom < fHeight - 1) 1546 _Invalidate(bottom + 1, fHeight - 1); 1547 } else if (numLines >= bottom - top + 1) { 1548 // all lines are completely scrolled out of range -- just clear 1549 // them 1550 _ClearLines(top, bottom); 1551 } else { 1552 // partial scroll -- clear the lines scrolled out of range and move 1553 // the other ones 1554 for (int32 i = top + numLines; i <= bottom; i++) { 1555 int32 lineToDrop = _LineIndex(i - numLines); 1556 int32 lineToKeep = _LineIndex(i); 1557 fScreen[lineToDrop]->Clear(fAttributes, fWidth); 1558 std::swap(fScreen[lineToDrop], fScreen[lineToKeep]); 1559 } 1560 // clear any lines between the two swapped ranges above 1561 for (int32 i = bottom - numLines + 1; i < top + numLines; i++) 1562 _LineAt(i)->Clear(fAttributes, fWidth); 1563 1564 _Invalidate(top, bottom); 1565 } 1566 } else { 1567 // scroll text down 1568 numLines = -numLines; 1569 1570 if (numLines >= bottom - top + 1) { 1571 // all lines are completely scrolled out of range -- just clear 1572 // them 1573 _ClearLines(top, bottom); 1574 } else { 1575 // partial scroll -- clear the lines scrolled out of range and move 1576 // the other ones 1577 // TODO: When scrolling the whole screen, we could just update fScreenOffset and 1578 // clear the respective lines. 1579 for (int32 i = bottom - numLines; i >= top; i--) { 1580 int32 lineToKeep = _LineIndex(i); 1581 int32 lineToDrop = _LineIndex(i + numLines); 1582 fScreen[lineToDrop]->Clear(fAttributes, fWidth); 1583 std::swap(fScreen[lineToDrop], fScreen[lineToKeep]); 1584 } 1585 // clear any lines between the two swapped ranges above 1586 for (int32 i = bottom - numLines + 1; i < top + numLines; i++) 1587 _LineAt(i)->Clear(fAttributes, fWidth); 1588 1589 _Invalidate(top, bottom); 1590 } 1591 } 1592 } 1593 1594 1595 void 1596 BasicTerminalBuffer::_SoftBreakLine() 1597 { 1598 TerminalLine* line = _LineAt(fCursor.y); 1599 line->softBreak = true; 1600 1601 fCursor.x = 0; 1602 if (fCursor.y == fScrollBottom) 1603 _Scroll(fScrollTop, fScrollBottom, 1); 1604 else 1605 fCursor.y++; 1606 } 1607 1608 1609 void 1610 BasicTerminalBuffer::_PadLineToCursor() 1611 { 1612 TerminalLine* line = _LineAt(fCursor.y); 1613 if (line->length < fCursor.x) 1614 for (int32 i = line->length; i < fCursor.x; i++) 1615 line->cells[i].character = kSpaceChar; 1616 } 1617 1618 1619 /*static*/ void 1620 BasicTerminalBuffer::_TruncateLine(TerminalLine* line, int32 length) 1621 { 1622 if (line->length <= length) 1623 return; 1624 1625 if (length > 0 && line->cells[length - 1].attributes.IsWidth()) 1626 length--; 1627 1628 line->length = length; 1629 } 1630 1631 1632 void 1633 BasicTerminalBuffer::_InsertGap(int32 width) 1634 { 1635 // ASSERT(fCursor.x + width <= fWidth) 1636 TerminalLine* line = _LineAt(fCursor.y); 1637 1638 int32 toMove = min_c(line->length - fCursor.x, fWidth - fCursor.x - width); 1639 if (toMove > 0) { 1640 memmove(line->cells + fCursor.x + width, 1641 line->cells + fCursor.x, toMove * sizeof(TerminalCell)); 1642 } 1643 1644 line->length = min_c(line->length + width, fWidth); 1645 } 1646 1647 1648 /*! \a endColumn is not inclusive. 1649 */ 1650 TerminalLine* 1651 BasicTerminalBuffer::_GetPartialLineString(BString& string, int32 row, 1652 int32 startColumn, int32 endColumn) const 1653 { 1654 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 1655 TerminalLine* line = _HistoryLineAt(row, lineBuffer); 1656 if (line == NULL) 1657 return NULL; 1658 1659 if (endColumn > line->length) 1660 endColumn = line->length; 1661 1662 for (int32 x = startColumn; x < endColumn; x++) { 1663 const TerminalCell& cell = line->cells[x]; 1664 string.Append(cell.character.bytes, cell.character.ByteCount()); 1665 1666 if (cell.attributes.IsWidth()) 1667 x++; 1668 } 1669 1670 return line; 1671 } 1672 1673 1674 /*! Decrement \a pos and return the char at that location. 1675 */ 1676 bool 1677 BasicTerminalBuffer::_PreviousChar(TermPos& pos, UTF8Char& c) const 1678 { 1679 pos.x--; 1680 1681 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 1682 TerminalLine* line = _HistoryLineAt(pos.y, lineBuffer); 1683 1684 while (true) { 1685 if (pos.x < 0) { 1686 pos.y--; 1687 line = _HistoryLineAt(pos.y, lineBuffer); 1688 if (line == NULL) 1689 return false; 1690 1691 pos.x = line->length; 1692 if (line->softBreak) { 1693 pos.x--; 1694 } else { 1695 c = '\n'; 1696 return true; 1697 } 1698 } else { 1699 c = line->cells[pos.x].character; 1700 return true; 1701 } 1702 } 1703 } 1704 1705 1706 /*! Return the char at \a pos and increment it. 1707 */ 1708 bool 1709 BasicTerminalBuffer::_NextChar(TermPos& pos, UTF8Char& c) const 1710 { 1711 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 1712 TerminalLine* line = _HistoryLineAt(pos.y, lineBuffer); 1713 if (line == NULL) 1714 return false; 1715 1716 if (pos.x >= line->length) { 1717 c = '\n'; 1718 pos.x = 0; 1719 pos.y++; 1720 return true; 1721 } 1722 1723 c = line->cells[pos.x].character; 1724 1725 pos.x++; 1726 while (line != NULL && pos.x >= line->length && line->softBreak) { 1727 pos.x = 0; 1728 pos.y++; 1729 line = _HistoryLineAt(pos.y, lineBuffer); 1730 } 1731 1732 return true; 1733 } 1734 1735 1736 bool 1737 BasicTerminalBuffer::_PreviousLinePos(TerminalLine* lineBuffer, 1738 TerminalLine*& line, TermPos& pos) const 1739 { 1740 if (--pos.x < 0) { 1741 // Hit the beginning of the line -- continue at the end of the 1742 // previous line, if it soft-breaks. 1743 pos.y--; 1744 if ((line = _HistoryLineAt(pos.y, lineBuffer)) == NULL 1745 || !line->softBreak || line->length == 0) { 1746 return false; 1747 } 1748 pos.x = line->length - 1; 1749 } 1750 if (pos.x > 0 && line->cells[pos.x - 1].attributes.IsWidth()) 1751 pos.x--; 1752 1753 return true; 1754 } 1755 1756 1757 bool 1758 BasicTerminalBuffer::_NormalizeLinePos(TerminalLine* lineBuffer, 1759 TerminalLine*& line, TermPos& pos) const 1760 { 1761 if (pos.x < line->length) 1762 return true; 1763 1764 // Hit the end of the line -- if it soft-breaks continue with the 1765 // next line. 1766 if (!line->softBreak) 1767 return false; 1768 1769 pos.y++; 1770 pos.x = 0; 1771 line = _HistoryLineAt(pos.y, lineBuffer); 1772 return line != NULL; 1773 } 1774 1775 1776 #ifdef USE_DEBUG_SNAPSHOTS 1777 1778 void 1779 BasicTerminalBuffer::MakeLinesSnapshots(time_t timeStamp, const char* fileName) 1780 { 1781 BString str("/var/log/"); 1782 struct tm* ts = gmtime(&timeStamp); 1783 str << ts->tm_hour << ts->tm_min << ts->tm_sec; 1784 str << fileName; 1785 FILE* fileOut = fopen(str.String(), "w"); 1786 1787 bool dumpHistory = false; 1788 do { 1789 if (dumpHistory && fHistory == NULL) { 1790 fprintf(fileOut, "> History is empty <\n"); 1791 break; 1792 } 1793 1794 int countLines = dumpHistory ? fHistory->Size() : fHeight; 1795 fprintf(fileOut, "> %s lines dump begin <\n", 1796 dumpHistory ? "History" : "Terminal"); 1797 1798 TerminalLine* lineBuffer = ALLOC_LINE_ON_STACK(fWidth); 1799 for (int i = 0; i < countLines; i++) { 1800 TerminalLine* line = dumpHistory 1801 ? fHistory->GetTerminalLineAt(i, lineBuffer) 1802 : fScreen[_LineIndex(i)]; 1803 1804 if (line == NULL) { 1805 fprintf(fileOut, "line: %d is NULL!!!\n", i); 1806 continue; 1807 } 1808 1809 fprintf(fileOut, "%02" B_PRId16 ":%02" B_PRId16 ":%08" B_PRIx32 ":\n", 1810 i, line->length, line->attributes.state); 1811 for (int j = 0; j < line->length; j++) 1812 if (line->cells[j].character.bytes[0] != 0) 1813 fwrite(line->cells[j].character.bytes, 1, 1814 line->cells[j].character.ByteCount(), fileOut); 1815 1816 fprintf(fileOut, "\n"); 1817 for (int s = 28; s >= 0; s -= 4) { 1818 for (int j = 0; j < fWidth; j++) 1819 fprintf(fileOut, "%01" B_PRIx32, 1820 (line->cells[j].attributes.state >> s) & 0x0F); 1821 1822 fprintf(fileOut, "\n"); 1823 } 1824 1825 fprintf(fileOut, "\n"); 1826 } 1827 1828 fprintf(fileOut, "> %s lines dump finished <\n", 1829 dumpHistory ? "History" : "Terminal"); 1830 1831 dumpHistory = !dumpHistory; 1832 } while (dumpHistory); 1833 1834 fclose(fileOut); 1835 } 1836 1837 1838 void 1839 BasicTerminalBuffer::StartStopDebugCapture() 1840 { 1841 if (fCaptureFile >= 0) { 1842 close(fCaptureFile); 1843 fCaptureFile = -1; 1844 return; 1845 } 1846 1847 time_t timeStamp = time(NULL); 1848 BString str("/var/log/"); 1849 struct tm* ts = gmtime(&timeStamp); 1850 str << ts->tm_hour << ts->tm_min << ts->tm_sec; 1851 str << ".Capture.log"; 1852 fCaptureFile = open(str.String(), O_CREAT | O_WRONLY, 1853 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 1854 } 1855 1856 1857 void 1858 BasicTerminalBuffer::CaptureChar(char ch) 1859 { 1860 if (fCaptureFile >= 0) 1861 write(fCaptureFile, &ch, 1); 1862 } 1863 1864 1865 void 1866 BasicTerminalBuffer::InsertLastChar() 1867 { 1868 InsertChar(fLast); 1869 } 1870 1871 #endif 1872