Terminal.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. #include "Terminal.h"
  2. #include "XtermColors.h"
  3. #include <AK/AKString.h>
  4. #include <AK/StringBuilder.h>
  5. #include <SharedGraphics/Font.h>
  6. #include <SharedGraphics/Painter.h>
  7. #include <AK/StdLibExtras.h>
  8. #include <LibC/stdlib.h>
  9. #include <LibC/unistd.h>
  10. #include <LibC/stdio.h>
  11. #include <LibC/gui.h>
  12. //#define TERMINAL_DEBUG
  13. void Terminal::create_window()
  14. {
  15. m_pixel_width = m_columns * font().glyph_width() + m_inset * 2;
  16. m_pixel_height = (m_rows * (font().glyph_height() + m_line_spacing)) + (m_inset * 2) - m_line_spacing;
  17. GUI_WindowParameters params;
  18. params.rect = { { 300, 300 }, { m_pixel_width, m_pixel_height } };
  19. params.background_color = 0x000000;
  20. strcpy(params.title, "Terminal");
  21. m_window_id = gui_create_window(&params);
  22. ASSERT(m_window_id > 0);
  23. if (m_window_id < 0) {
  24. perror("gui_create_window");
  25. exit(1);
  26. }
  27. // NOTE: We never release the backing store.
  28. GUI_WindowBackingStoreInfo info;
  29. int rc = gui_get_window_backing_store(m_window_id, &info);
  30. if (rc < 0) {
  31. perror("gui_get_window_backing_store");
  32. exit(1);
  33. }
  34. m_backing = GraphicsBitmap::create_wrapper(info.size, info.pixels);
  35. dbgprintf("(Terminal:%d) window backing %ux%u @ %p\n", getpid(), info.size.width, info.size.height, info.pixels);
  36. }
  37. Terminal::Terminal()
  38. : m_font(Font::default_font())
  39. {
  40. m_line_height = font().glyph_height() + m_line_spacing;
  41. set_size(80, 25);
  42. m_horizontal_tabs = static_cast<byte*>(malloc(columns()));
  43. for (unsigned i = 0; i < columns(); ++i)
  44. m_horizontal_tabs[i] = (i % 8) == 0;
  45. // Rightmost column is always last tab on line.
  46. m_horizontal_tabs[columns() - 1] = 1;
  47. m_lines = new Line*[rows()];
  48. for (size_t i = 0; i < rows(); ++i)
  49. m_lines[i] = new Line(columns());
  50. }
  51. Terminal::Line::Line(word columns)
  52. : length(columns)
  53. {
  54. characters = new byte[length];
  55. attributes = new Attribute[length];
  56. did_paint = false;
  57. memset(characters, ' ', length);
  58. }
  59. Terminal::Line::~Line()
  60. {
  61. delete [] characters;
  62. delete [] attributes;
  63. }
  64. void Terminal::Line::clear(Attribute attribute)
  65. {
  66. if (dirty) {
  67. memset(characters, ' ', length);
  68. for (word i = 0 ; i < length; ++i)
  69. attributes[i] = attribute;
  70. return;
  71. }
  72. for (unsigned i = 0 ; i < length; ++i) {
  73. if (characters[i] != ' ')
  74. dirty = true;
  75. characters[i] = ' ';
  76. }
  77. for (unsigned i = 0 ; i < length; ++i) {
  78. if (attributes[i] != attribute)
  79. dirty = true;
  80. attributes[i] = attribute;
  81. }
  82. }
  83. Terminal::~Terminal()
  84. {
  85. for (size_t i = 0; i < m_rows; ++i)
  86. delete m_lines[i];
  87. delete [] m_lines;
  88. free(m_horizontal_tabs);
  89. }
  90. void Terminal::clear()
  91. {
  92. for (size_t i = 0; i < rows(); ++i)
  93. line(i).clear(m_current_attribute);
  94. set_cursor(0, 0);
  95. }
  96. inline bool is_valid_parameter_character(byte ch)
  97. {
  98. return ch >= 0x30 && ch <= 0x3f;
  99. }
  100. inline bool is_valid_intermediate_character(byte ch)
  101. {
  102. return ch >= 0x20 && ch <= 0x2f;
  103. }
  104. inline bool is_valid_final_character(byte ch)
  105. {
  106. return ch >= 0x40 && ch <= 0x7e;
  107. }
  108. unsigned parse_uint(const String& str, bool& ok)
  109. {
  110. unsigned value = 0;
  111. for (size_t i = 0; i < str.length(); ++i) {
  112. if (str[i] < '0' || str[i] > '9') {
  113. ok = false;
  114. return 0;
  115. }
  116. value = value * 10;
  117. value += str[i] - '0';
  118. }
  119. ok = true;
  120. return value;
  121. }
  122. static inline Color lookup_color(unsigned color)
  123. {
  124. return xterm_colors[color];
  125. }
  126. void Terminal::escape$m(const Vector<unsigned>& params)
  127. {
  128. if (params.size() == 3 && params[1] == 5) {
  129. if (params[0] == 38) {
  130. m_current_attribute.foreground_color = params[2];
  131. return;
  132. } else if (params[0] == 48) {
  133. m_current_attribute.background_color = params[2];
  134. return;
  135. }
  136. }
  137. for (auto param : params) {
  138. switch (param) {
  139. case 0:
  140. // Reset
  141. m_current_attribute.reset();
  142. break;
  143. case 1:
  144. // Bold
  145. //m_current_attribute.bold = true;
  146. break;
  147. case 30:
  148. case 31:
  149. case 32:
  150. case 33:
  151. case 34:
  152. case 35:
  153. case 36:
  154. case 37:
  155. // Foreground color
  156. m_current_attribute.foreground_color = param - 30;
  157. break;
  158. case 40:
  159. case 41:
  160. case 42:
  161. case 43:
  162. case 44:
  163. case 45:
  164. case 46:
  165. case 47:
  166. // Background color
  167. m_current_attribute.background_color = param - 40;
  168. break;
  169. }
  170. }
  171. }
  172. void Terminal::escape$s(const Vector<unsigned>&)
  173. {
  174. m_saved_cursor_row = m_cursor_row;
  175. m_saved_cursor_column = m_cursor_column;
  176. }
  177. void Terminal::escape$u(const Vector<unsigned>&)
  178. {
  179. set_cursor(m_saved_cursor_row, m_saved_cursor_column);
  180. }
  181. void Terminal::escape$H(const Vector<unsigned>& params)
  182. {
  183. unsigned row = 1;
  184. unsigned col = 1;
  185. if (params.size() >= 1)
  186. row = params[0];
  187. if (params.size() >= 2)
  188. col = params[1];
  189. set_cursor(row - 1, col - 1);
  190. }
  191. void Terminal::escape$A(const Vector<unsigned>& params)
  192. {
  193. int num = 1;
  194. if (params.size() >= 1)
  195. num = params[0];
  196. if (num == 0)
  197. num = 1;
  198. int new_row = (int)m_cursor_row - num;
  199. if (new_row < 0)
  200. new_row = 0;
  201. set_cursor(new_row, m_cursor_column);
  202. }
  203. void Terminal::escape$B(const Vector<unsigned>& params)
  204. {
  205. int num = 1;
  206. if (params.size() >= 1)
  207. num = params[0];
  208. if (num == 0)
  209. num = 1;
  210. int new_row = (int)m_cursor_row + num;
  211. if (new_row >= m_rows)
  212. new_row = m_rows - 1;
  213. set_cursor(new_row, m_cursor_column);
  214. }
  215. void Terminal::escape$C(const Vector<unsigned>& params)
  216. {
  217. int num = 1;
  218. if (params.size() >= 1)
  219. num = params[0];
  220. if (num == 0)
  221. num = 1;
  222. int new_column = (int)m_cursor_column + num;
  223. if (new_column >= m_columns)
  224. new_column = m_columns - 1;
  225. set_cursor(m_cursor_row, new_column);
  226. }
  227. void Terminal::escape$D(const Vector<unsigned>& params)
  228. {
  229. int num = 1;
  230. if (params.size() >= 1)
  231. num = params[0];
  232. if (num == 0)
  233. num = 1;
  234. int new_column = (int)m_cursor_column - num;
  235. if (new_column < 0)
  236. new_column = 0;
  237. set_cursor(m_cursor_row, new_column);
  238. }
  239. void Terminal::escape$K(const Vector<unsigned>& params)
  240. {
  241. int mode = 0;
  242. if (params.size() >= 1)
  243. mode = params[0];
  244. switch (mode) {
  245. case 0:
  246. // Clear from cursor to end of line.
  247. for (int i = m_cursor_column; i < m_columns; ++i) {
  248. put_character_at(m_cursor_row, i, ' ');
  249. }
  250. break;
  251. case 1:
  252. // FIXME: Clear from cursor to beginning of screen.
  253. unimplemented_escape();
  254. break;
  255. case 2:
  256. unimplemented_escape();
  257. break;
  258. default:
  259. unimplemented_escape();
  260. break;
  261. }
  262. }
  263. void Terminal::escape$J(const Vector<unsigned>& params)
  264. {
  265. int mode = 0;
  266. if (params.size() >= 1)
  267. mode = params[0];
  268. switch (mode) {
  269. case 0:
  270. // Clear from cursor to end of screen.
  271. for (int i = m_cursor_column; i < m_columns; ++i) {
  272. put_character_at(m_cursor_row, i, ' ');
  273. }
  274. for (int row = m_cursor_row + 1; row < m_rows; ++row) {
  275. for (int column = 0; column < m_columns; ++column) {
  276. put_character_at(row, column, ' ');
  277. }
  278. }
  279. break;
  280. case 1:
  281. // FIXME: Clear from cursor to beginning of screen.
  282. unimplemented_escape();
  283. break;
  284. case 2:
  285. clear();
  286. break;
  287. case 3:
  288. // FIXME: <esc>[3J should also clear the scrollback buffer.
  289. clear();
  290. break;
  291. default:
  292. unimplemented_escape();
  293. break;
  294. }
  295. }
  296. void Terminal::escape$M(const Vector<unsigned>& params)
  297. {
  298. int count = 1;
  299. if (params.size() >= 1)
  300. count = params[0];
  301. if (count == 1 && m_cursor_row == 0) {
  302. scroll_up();
  303. return;
  304. }
  305. int max_count = m_rows - m_cursor_row;
  306. count = min(count, max_count);
  307. dbgprintf("Delete %d line(s) starting from %d\n", count, m_cursor_row);
  308. // FIXME: Implement.
  309. ASSERT_NOT_REACHED();
  310. }
  311. void Terminal::execute_xterm_command()
  312. {
  313. m_final = '@';
  314. bool ok;
  315. unsigned value = parse_uint(String((const char*)m_xterm_param1.data(), m_xterm_param1.size()), ok);
  316. if (ok) {
  317. switch (value) {
  318. case 0:
  319. case 1:
  320. case 2:
  321. set_window_title(String((const char*)m_xterm_param2.data(), m_xterm_param2.size()));
  322. break;
  323. default:
  324. unimplemented_xterm_escape();
  325. break;
  326. }
  327. }
  328. m_xterm_param1.clear_with_capacity();
  329. m_xterm_param2.clear_with_capacity();
  330. }
  331. void Terminal::execute_escape_sequence(byte final)
  332. {
  333. m_final = final;
  334. auto paramparts = String((const char*)m_parameters.data(), m_parameters.size()).split(';');
  335. Vector<unsigned> params;
  336. for (auto& parampart : paramparts) {
  337. bool ok;
  338. unsigned value = parse_uint(parampart, ok);
  339. if (!ok) {
  340. m_parameters.clear_with_capacity();
  341. m_intermediates.clear_with_capacity();
  342. // FIXME: Should we do something else?
  343. return;
  344. }
  345. params.append(value);
  346. }
  347. switch (final) {
  348. case 'A': escape$A(params); break;
  349. case 'B': escape$B(params); break;
  350. case 'C': escape$C(params); break;
  351. case 'D': escape$D(params); break;
  352. case 'H': escape$H(params); break;
  353. case 'J': escape$J(params); break;
  354. case 'K': escape$K(params); break;
  355. case 'M': escape$M(params); break;
  356. case 'm': escape$m(params); break;
  357. case 's': escape$s(params); break;
  358. case 'u': escape$u(params); break;
  359. default:
  360. dbgprintf("Terminal::execute_escape_sequence: Unhandled final '%c'\n", final);
  361. break;
  362. }
  363. m_parameters.clear_with_capacity();
  364. m_intermediates.clear_with_capacity();
  365. }
  366. void Terminal::newline()
  367. {
  368. word new_row = m_cursor_row;
  369. if (m_cursor_row == (rows() - 1)) {
  370. scroll_up();
  371. } else {
  372. ++new_row;
  373. }
  374. set_cursor(new_row, 0);
  375. }
  376. void Terminal::scroll_up()
  377. {
  378. // NOTE: We have to invalidate the cursor first.
  379. invalidate_cursor();
  380. delete m_lines[0];
  381. for (word row = 1; row < rows(); ++row)
  382. m_lines[row - 1] = m_lines[row];
  383. m_lines[m_rows - 1] = new Line(m_columns);
  384. ++m_rows_to_scroll_backing_store;
  385. }
  386. void Terminal::set_cursor(unsigned a_row, unsigned a_column)
  387. {
  388. unsigned row = min(a_row, m_rows - 1u);
  389. unsigned column = min(a_column, m_columns - 1u);
  390. if (row == m_cursor_row && column == m_cursor_column)
  391. return;
  392. ASSERT(row < rows());
  393. ASSERT(column < columns());
  394. invalidate_cursor();
  395. m_cursor_row = row;
  396. m_cursor_column = column;
  397. if (column != columns() - 1)
  398. m_stomp = false;
  399. invalidate_cursor();
  400. }
  401. void Terminal::put_character_at(unsigned row, unsigned column, byte ch)
  402. {
  403. ASSERT(row < rows());
  404. ASSERT(column < columns());
  405. auto& line = this->line(row);
  406. if ((line.characters[column] == ch) && (line.attributes[column] == m_current_attribute))
  407. return;
  408. line.characters[column] = ch;
  409. line.attributes[column] = m_current_attribute;
  410. line.dirty = true;
  411. }
  412. void Terminal::on_char(byte ch)
  413. {
  414. #ifdef TERMINAL_DEBUG
  415. dbgprintf("Terminal::on_char: %b (%c), fg=%u, bg=%u\n", ch, ch, m_current_attribute.foreground_color, m_current_attribute.background_color);
  416. #endif
  417. switch (m_escape_state) {
  418. case ExpectBracket:
  419. if (ch == '[')
  420. m_escape_state = ExpectParameter;
  421. else if (ch == ']')
  422. m_escape_state = ExpectXtermParameter1;
  423. else
  424. m_escape_state = Normal;
  425. return;
  426. case ExpectXtermParameter1:
  427. if (ch != ';') {
  428. m_xterm_param1.append(ch);
  429. return;
  430. }
  431. m_escape_state = ExpectXtermParameter2;
  432. return;
  433. case ExpectXtermParameter2:
  434. if (ch != '\007') {
  435. m_xterm_param2.append(ch);
  436. return;
  437. }
  438. m_escape_state = ExpectXtermFinal;
  439. // fall through
  440. case ExpectXtermFinal:
  441. m_escape_state = Normal;
  442. if (ch == '\007')
  443. execute_xterm_command();
  444. return;
  445. case ExpectParameter:
  446. if (is_valid_parameter_character(ch)) {
  447. m_parameters.append(ch);
  448. return;
  449. }
  450. m_escape_state = ExpectIntermediate;
  451. // fall through
  452. case ExpectIntermediate:
  453. if (is_valid_intermediate_character(ch)) {
  454. m_intermediates.append(ch);
  455. return;
  456. }
  457. m_escape_state = ExpectFinal;
  458. // fall through
  459. case ExpectFinal:
  460. if (is_valid_final_character(ch)) {
  461. m_escape_state = Normal;
  462. execute_escape_sequence(ch);
  463. return;
  464. }
  465. m_escape_state = Normal;
  466. return;
  467. case Normal:
  468. break;
  469. }
  470. switch (ch) {
  471. case '\0':
  472. return;
  473. case '\033':
  474. m_escape_state = ExpectBracket;
  475. return;
  476. case 8: // Backspace
  477. if (m_cursor_column) {
  478. set_cursor(m_cursor_row, m_cursor_column - 1);
  479. put_character_at(m_cursor_row, m_cursor_column, ' ');
  480. return;
  481. }
  482. return;
  483. case '\a':
  484. // FIXME: Bell!
  485. return;
  486. case '\t': {
  487. for (unsigned i = m_cursor_column; i < columns(); ++i) {
  488. if (m_horizontal_tabs[i]) {
  489. set_cursor(m_cursor_row, i);
  490. return;
  491. }
  492. }
  493. return;
  494. }
  495. case '\r':
  496. set_cursor(m_cursor_row, 0);
  497. return;
  498. case '\n':
  499. newline();
  500. return;
  501. }
  502. auto new_column = m_cursor_column + 1;
  503. if (new_column < columns()) {
  504. put_character_at(m_cursor_row, m_cursor_column, ch);
  505. set_cursor(m_cursor_row, new_column);
  506. } else {
  507. if (m_stomp) {
  508. m_stomp = false;
  509. newline();
  510. put_character_at(m_cursor_row, m_cursor_column, ch);
  511. set_cursor(m_cursor_row, 1);
  512. } else {
  513. // Curious: We wait once on the right-hand side
  514. m_stomp = true;
  515. put_character_at(m_cursor_row, m_cursor_column, ch);
  516. }
  517. }
  518. }
  519. void Terminal::inject_string(const String& str)
  520. {
  521. for (size_t i = 0; i < str.length(); ++i)
  522. on_char(str[i]);
  523. }
  524. void Terminal::unimplemented_escape()
  525. {
  526. StringBuilder builder;
  527. builder.appendf("((Unimplemented escape: %c", m_final);
  528. if (!m_parameters.is_empty()) {
  529. builder.append(" parameters:");
  530. for (size_t i = 0; i < m_parameters.size(); ++i)
  531. builder.append((char)m_parameters[i]);
  532. }
  533. if (!m_intermediates.is_empty()) {
  534. builder.append(" intermediates:");
  535. for (size_t i = 0; i < m_intermediates.size(); ++i)
  536. builder.append((char)m_intermediates[i]);
  537. }
  538. builder.append("))");
  539. inject_string(builder.to_string());
  540. }
  541. void Terminal::unimplemented_xterm_escape()
  542. {
  543. auto message = String::format("((Unimplemented xterm escape: %c))\n", m_final);
  544. inject_string(message);
  545. }
  546. void Terminal::set_size(word columns, word rows)
  547. {
  548. m_columns = columns;
  549. m_rows = rows;
  550. }
  551. Rect Terminal::glyph_rect(word row, word column)
  552. {
  553. int y = row * m_line_height;
  554. int x = column * font().glyph_width();
  555. return { x + m_inset, y + m_inset, font().glyph_width(), font().glyph_height() };
  556. }
  557. Rect Terminal::row_rect(word row)
  558. {
  559. int y = row * m_line_height;
  560. Rect rect = { m_inset, y + m_inset, font().glyph_width() * m_columns, font().glyph_height() };
  561. rect.inflate(0, m_line_spacing);
  562. return rect;
  563. }
  564. bool Terminal::Line::has_only_one_background_color() const
  565. {
  566. if (!length)
  567. return true;
  568. // FIXME: Cache this result?
  569. auto color = attributes[0].background_color;
  570. for (size_t i = 1; i < length; ++i) {
  571. if (attributes[i].background_color != color)
  572. return false;
  573. }
  574. return true;
  575. }
  576. void Terminal::paint()
  577. {
  578. Rect rect { 0, 0, m_pixel_width, m_pixel_height };
  579. Painter painter(*m_backing);
  580. for (size_t i = 0; i < rows(); ++i)
  581. line(i).did_paint = false;
  582. if (m_rows_to_scroll_backing_store && m_rows_to_scroll_backing_store < m_rows) {
  583. int first_scanline = m_inset;
  584. int second_scanline = m_inset + (m_rows_to_scroll_backing_store * m_line_height);
  585. int num_rows_to_memcpy = m_rows - m_rows_to_scroll_backing_store;
  586. int scanlines_to_copy = (num_rows_to_memcpy * m_line_height) - m_line_spacing;
  587. fast_dword_copy(
  588. m_backing->scanline(first_scanline),
  589. m_backing->scanline(second_scanline),
  590. scanlines_to_copy * m_pixel_width
  591. );
  592. m_need_full_invalidation = true;
  593. line(max(0, m_cursor_row - m_rows_to_scroll_backing_store)).dirty = true;
  594. }
  595. m_rows_to_scroll_backing_store = 0;
  596. invalidate_cursor();
  597. for (word row = 0; row < m_rows; ++row) {
  598. auto& line = this->line(row);
  599. if (!line.dirty)
  600. continue;
  601. line.dirty = false;
  602. bool has_only_one_background_color = line.has_only_one_background_color();
  603. if (has_only_one_background_color) {
  604. painter.fill_rect(row_rect(row), lookup_color(line.attributes[0].background_color));
  605. }
  606. for (word column = 0; column < m_columns; ++column) {
  607. bool should_reverse_fill_for_cursor = m_in_active_window && row == m_cursor_row && column == m_cursor_column;
  608. auto& attribute = line.attributes[column];
  609. line.did_paint = true;
  610. char ch = line.characters[column];
  611. auto character_rect = glyph_rect(row, column);
  612. if (!has_only_one_background_color || should_reverse_fill_for_cursor) {
  613. auto cell_rect = character_rect;
  614. cell_rect.inflate(0, m_line_spacing);
  615. painter.fill_rect(cell_rect, lookup_color(should_reverse_fill_for_cursor ? attribute.foreground_color : attribute.background_color));
  616. }
  617. if (ch == ' ')
  618. continue;
  619. painter.draw_glyph(character_rect.location(), ch, lookup_color(should_reverse_fill_for_cursor ? attribute.background_color : attribute.foreground_color));
  620. }
  621. }
  622. if (!m_in_active_window) {
  623. auto cursor_rect = glyph_rect(m_cursor_row, m_cursor_column);
  624. painter.draw_rect(cursor_rect, lookup_color(line(m_cursor_row).attributes[m_cursor_column].foreground_color));
  625. }
  626. line(m_cursor_row).did_paint = true;
  627. if (m_belling) {
  628. m_need_full_invalidation = true;
  629. painter.draw_rect(rect, Color::Red);
  630. }
  631. if (m_need_full_invalidation) {
  632. did_paint();
  633. m_need_full_invalidation = false;
  634. return;
  635. }
  636. Rect painted_rect;
  637. for (int i = 0; i < m_rows; ++i) {
  638. if (line(i).did_paint)
  639. painted_rect = painted_rect.united(row_rect(i));
  640. }
  641. did_paint(painted_rect);
  642. }
  643. void Terminal::did_paint(const Rect& a_rect)
  644. {
  645. GUI_Rect rect = a_rect;
  646. int rc = gui_notify_paint_finished(m_window_id, a_rect.is_null() ? nullptr : &rect);
  647. if (rc < 0) {
  648. perror("gui_notify_paint_finished");
  649. exit(1);
  650. }
  651. }
  652. void Terminal::update()
  653. {
  654. Rect rect;
  655. for (int i = 0; i < m_rows; ++i) {
  656. if (line(i).did_paint)
  657. rect = rect.united(row_rect(i));
  658. }
  659. GUI_Rect gui_rect = rect;
  660. int rc = gui_invalidate_window(m_window_id, rect.is_null() ? nullptr : &gui_rect);
  661. if (rc < 0) {
  662. perror("gui_invalidate_window");
  663. exit(1);
  664. }
  665. }
  666. void Terminal::set_window_title(const String& title)
  667. {
  668. int rc = gui_set_window_title(m_window_id, title.characters(), title.length());
  669. if (rc < 0) {
  670. perror("gui_set_window_title");
  671. exit(1);
  672. }
  673. }
  674. void Terminal::set_in_active_window(bool b)
  675. {
  676. if (m_in_active_window == b)
  677. return;
  678. m_in_active_window = b;
  679. invalidate_cursor();
  680. update();
  681. }
  682. void Terminal::invalidate_cursor()
  683. {
  684. line(m_cursor_row).dirty = true;
  685. }