Terminal.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. #include "Terminal.h"
  2. #include "XtermColors.h"
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8. #include <AK/AKString.h>
  9. #include <AK/StringBuilder.h>
  10. #include <SharedGraphics/Font.h>
  11. #include <LibGUI/GPainter.h>
  12. #include <AK/StdLibExtras.h>
  13. #include <LibGUI/GApplication.h>
  14. #include <LibGUI/GWindow.h>
  15. #include <Kernel/KeyCode.h>
  16. #include <sys/ioctl.h>
  17. //#define TERMINAL_DEBUG
  18. Terminal::Terminal(int ptm_fd)
  19. : m_ptm_fd(ptm_fd)
  20. , m_notifier(ptm_fd, CNotifier::Read)
  21. {
  22. m_cursor_blink_timer.set_interval(500);
  23. m_cursor_blink_timer.on_timeout = [this] {
  24. m_cursor_blink_state = !m_cursor_blink_state;
  25. update_cursor();
  26. };
  27. set_font(Font::default_fixed_width_font());
  28. m_notifier.on_ready_to_read = [this]{
  29. byte buffer[BUFSIZ];
  30. ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer));
  31. if (nread < 0) {
  32. dbgprintf("Terminal read error: %s\n", strerror(errno));
  33. perror("read(ptm)");
  34. GApplication::the().quit(1);
  35. return;
  36. }
  37. if (nread == 0) {
  38. dbgprintf("Terminal: EOF on master pty, closing.\n");
  39. GApplication::the().quit(0);
  40. return;
  41. }
  42. for (ssize_t i = 0; i < nread; ++i)
  43. on_char(buffer[i]);
  44. flush_dirty_lines();
  45. };
  46. m_line_height = font().glyph_height() + m_line_spacing;
  47. set_size(80, 25);
  48. }
  49. Terminal::Line::Line(word columns)
  50. : length(columns)
  51. {
  52. characters = new byte[length];
  53. attributes = new Attribute[length];
  54. memset(characters, ' ', length);
  55. }
  56. Terminal::Line::~Line()
  57. {
  58. delete [] characters;
  59. delete [] attributes;
  60. }
  61. void Terminal::Line::clear(Attribute attribute)
  62. {
  63. if (dirty) {
  64. memset(characters, ' ', length);
  65. for (word i = 0 ; i < length; ++i)
  66. attributes[i] = attribute;
  67. return;
  68. }
  69. for (unsigned i = 0 ; i < length; ++i) {
  70. if (characters[i] != ' ')
  71. dirty = true;
  72. characters[i] = ' ';
  73. }
  74. for (unsigned i = 0 ; i < length; ++i) {
  75. if (attributes[i] != attribute)
  76. dirty = true;
  77. attributes[i] = attribute;
  78. }
  79. }
  80. Terminal::~Terminal()
  81. {
  82. for (int i = 0; i < m_rows; ++i)
  83. delete m_lines[i];
  84. delete [] m_lines;
  85. free(m_horizontal_tabs);
  86. }
  87. void Terminal::clear()
  88. {
  89. for (size_t i = 0; i < rows(); ++i)
  90. line(i).clear(m_current_attribute);
  91. set_cursor(0, 0);
  92. }
  93. inline bool is_valid_parameter_character(byte ch)
  94. {
  95. return ch >= 0x30 && ch <= 0x3f;
  96. }
  97. inline bool is_valid_intermediate_character(byte ch)
  98. {
  99. return ch >= 0x20 && ch <= 0x2f;
  100. }
  101. inline bool is_valid_final_character(byte ch)
  102. {
  103. return ch >= 0x40 && ch <= 0x7e;
  104. }
  105. unsigned parse_uint(const String& str, bool& ok)
  106. {
  107. unsigned value = 0;
  108. for (int i = 0; i < str.length(); ++i) {
  109. if (str[i] < '0' || str[i] > '9') {
  110. ok = false;
  111. return 0;
  112. }
  113. value = value * 10;
  114. value += str[i] - '0';
  115. }
  116. ok = true;
  117. return value;
  118. }
  119. static inline Color lookup_color(unsigned color)
  120. {
  121. return Color::from_rgb(xterm_colors[color]);
  122. }
  123. void Terminal::escape$m(const ParamVector& params)
  124. {
  125. if (params.size() == 3 && params[1] == 5) {
  126. if (params[0] == 38) {
  127. m_current_attribute.foreground_color = params[2];
  128. return;
  129. } else if (params[0] == 48) {
  130. m_current_attribute.background_color = params[2];
  131. return;
  132. }
  133. }
  134. for (auto param : params) {
  135. switch (param) {
  136. case 0:
  137. // Reset
  138. m_current_attribute.reset();
  139. break;
  140. case 1:
  141. // Bold
  142. //m_current_attribute.bold = true;
  143. break;
  144. case 30:
  145. case 31:
  146. case 32:
  147. case 33:
  148. case 34:
  149. case 35:
  150. case 36:
  151. case 37:
  152. // Foreground color
  153. m_current_attribute.foreground_color = param - 30;
  154. break;
  155. case 40:
  156. case 41:
  157. case 42:
  158. case 43:
  159. case 44:
  160. case 45:
  161. case 46:
  162. case 47:
  163. // Background color
  164. m_current_attribute.background_color = param - 40;
  165. break;
  166. }
  167. }
  168. }
  169. void Terminal::escape$s(const ParamVector&)
  170. {
  171. m_saved_cursor_row = m_cursor_row;
  172. m_saved_cursor_column = m_cursor_column;
  173. }
  174. void Terminal::escape$u(const ParamVector&)
  175. {
  176. set_cursor(m_saved_cursor_row, m_saved_cursor_column);
  177. }
  178. void Terminal::escape$t(const ParamVector& params)
  179. {
  180. if (params.size() < 1)
  181. return;
  182. dbgprintf("FIXME: escape$t: Ps: %u\n", params[0]);
  183. }
  184. void Terminal::escape$r(const ParamVector& params)
  185. {
  186. unsigned top = 1;
  187. unsigned bottom = m_rows;
  188. if (params.size() >= 1)
  189. top = params[0];
  190. if (params.size() >= 2)
  191. bottom = params[1];
  192. dbgprintf("FIXME: escape$r: Set scrolling region: %u-%u\n", top, bottom);
  193. }
  194. void Terminal::escape$H(const ParamVector& params)
  195. {
  196. unsigned row = 1;
  197. unsigned col = 1;
  198. if (params.size() >= 1)
  199. row = params[0];
  200. if (params.size() >= 2)
  201. col = params[1];
  202. set_cursor(row - 1, col - 1);
  203. }
  204. void Terminal::escape$A(const ParamVector& params)
  205. {
  206. int num = 1;
  207. if (params.size() >= 1)
  208. num = params[0];
  209. if (num == 0)
  210. num = 1;
  211. int new_row = (int)m_cursor_row - num;
  212. if (new_row < 0)
  213. new_row = 0;
  214. set_cursor(new_row, m_cursor_column);
  215. }
  216. void Terminal::escape$B(const ParamVector& params)
  217. {
  218. int num = 1;
  219. if (params.size() >= 1)
  220. num = params[0];
  221. if (num == 0)
  222. num = 1;
  223. int new_row = (int)m_cursor_row + num;
  224. if (new_row >= m_rows)
  225. new_row = m_rows - 1;
  226. set_cursor(new_row, m_cursor_column);
  227. }
  228. void Terminal::escape$C(const ParamVector& params)
  229. {
  230. int num = 1;
  231. if (params.size() >= 1)
  232. num = params[0];
  233. if (num == 0)
  234. num = 1;
  235. int new_column = (int)m_cursor_column + num;
  236. if (new_column >= m_columns)
  237. new_column = m_columns - 1;
  238. set_cursor(m_cursor_row, new_column);
  239. }
  240. void Terminal::escape$D(const ParamVector& params)
  241. {
  242. int num = 1;
  243. if (params.size() >= 1)
  244. num = params[0];
  245. if (num == 0)
  246. num = 1;
  247. int new_column = (int)m_cursor_column - num;
  248. if (new_column < 0)
  249. new_column = 0;
  250. set_cursor(m_cursor_row, new_column);
  251. }
  252. void Terminal::escape$G(const ParamVector& params)
  253. {
  254. int new_column = 1;
  255. if (params.size() >= 1)
  256. new_column = params[0] - 1;
  257. if (new_column < 0)
  258. new_column = 0;
  259. set_cursor(m_cursor_row, new_column);
  260. }
  261. void Terminal::escape$d(const ParamVector& params)
  262. {
  263. int new_row = 1;
  264. if (params.size() >= 1)
  265. new_row = params[0] - 1;
  266. if (new_row < 0)
  267. new_row = 0;
  268. set_cursor(new_row, m_cursor_column);
  269. }
  270. void Terminal::escape$X(const ParamVector& params)
  271. {
  272. // Erase characters (without moving cursor)
  273. int num = 1;
  274. if (params.size() >= 1)
  275. num = params[0];
  276. if (num == 0)
  277. num = 1;
  278. // Clear from cursor to end of line.
  279. for (int i = m_cursor_column; i < num; ++i) {
  280. put_character_at(m_cursor_row, i, ' ');
  281. }
  282. }
  283. void Terminal::escape$K(const ParamVector& params)
  284. {
  285. int mode = 0;
  286. if (params.size() >= 1)
  287. mode = params[0];
  288. switch (mode) {
  289. case 0:
  290. // Clear from cursor to end of line.
  291. for (int i = m_cursor_column; i < m_columns; ++i) {
  292. put_character_at(m_cursor_row, i, ' ');
  293. }
  294. break;
  295. case 1:
  296. // Clear from cursor to beginning of line.
  297. for (int i = 0; i < m_cursor_column; ++i) {
  298. put_character_at(m_cursor_row, i, ' ');
  299. }
  300. break;
  301. case 2:
  302. unimplemented_escape();
  303. break;
  304. default:
  305. unimplemented_escape();
  306. break;
  307. }
  308. }
  309. void Terminal::escape$J(const ParamVector& params)
  310. {
  311. int mode = 0;
  312. if (params.size() >= 1)
  313. mode = params[0];
  314. switch (mode) {
  315. case 0:
  316. // Clear from cursor to end of screen.
  317. for (int i = m_cursor_column; i < m_columns; ++i) {
  318. put_character_at(m_cursor_row, i, ' ');
  319. }
  320. for (int row = m_cursor_row + 1; row < m_rows; ++row) {
  321. for (int column = 0; column < m_columns; ++column) {
  322. put_character_at(row, column, ' ');
  323. }
  324. }
  325. break;
  326. case 1:
  327. // FIXME: Clear from cursor to beginning of screen.
  328. unimplemented_escape();
  329. break;
  330. case 2:
  331. clear();
  332. break;
  333. case 3:
  334. // FIXME: <esc>[3J should also clear the scrollback buffer.
  335. clear();
  336. break;
  337. default:
  338. unimplemented_escape();
  339. break;
  340. }
  341. }
  342. void Terminal::escape$M(const ParamVector& params)
  343. {
  344. int count = 1;
  345. if (params.size() >= 1)
  346. count = params[0];
  347. if (count == 1 && m_cursor_row == 0) {
  348. scroll_up();
  349. return;
  350. }
  351. int max_count = m_rows - m_cursor_row;
  352. count = min(count, max_count);
  353. dbgprintf("Delete %d line(s) starting from %d\n", count, m_cursor_row);
  354. // FIXME: Implement.
  355. ASSERT_NOT_REACHED();
  356. }
  357. void Terminal::execute_xterm_command()
  358. {
  359. m_final = '@';
  360. bool ok;
  361. unsigned value = parse_uint(String((const char*)m_xterm_param1.data(), m_xterm_param1.size()), ok);
  362. if (ok) {
  363. switch (value) {
  364. case 0:
  365. case 1:
  366. case 2:
  367. set_window_title(String((const char*)m_xterm_param2.data(), m_xterm_param2.size()));
  368. break;
  369. default:
  370. unimplemented_xterm_escape();
  371. break;
  372. }
  373. }
  374. m_xterm_param1.clear_with_capacity();
  375. m_xterm_param2.clear_with_capacity();
  376. }
  377. void Terminal::execute_escape_sequence(byte final)
  378. {
  379. m_final = final;
  380. auto paramparts = String((const char*)m_parameters.data(), m_parameters.size()).split(';');
  381. ParamVector params;
  382. for (auto& parampart : paramparts) {
  383. bool ok;
  384. unsigned value = parse_uint(parampart, ok);
  385. if (!ok) {
  386. m_parameters.clear_with_capacity();
  387. m_intermediates.clear_with_capacity();
  388. // FIXME: Should we do something else?
  389. return;
  390. }
  391. params.append(value);
  392. }
  393. switch (final) {
  394. case 'A': escape$A(params); break;
  395. case 'B': escape$B(params); break;
  396. case 'C': escape$C(params); break;
  397. case 'D': escape$D(params); break;
  398. case 'H': escape$H(params); break;
  399. case 'J': escape$J(params); break;
  400. case 'K': escape$K(params); break;
  401. case 'M': escape$M(params); break;
  402. case 'G': escape$G(params); break;
  403. case 'X': escape$X(params); break;
  404. case 'd': escape$d(params); break;
  405. case 'm': escape$m(params); break;
  406. case 's': escape$s(params); break;
  407. case 'u': escape$u(params); break;
  408. case 't': escape$t(params); break;
  409. case 'r': escape$r(params); break;
  410. default:
  411. dbgprintf("Terminal::execute_escape_sequence: Unhandled final '%c'\n", final);
  412. break;
  413. }
  414. m_parameters.clear_with_capacity();
  415. m_intermediates.clear_with_capacity();
  416. }
  417. void Terminal::newline()
  418. {
  419. word new_row = m_cursor_row;
  420. if (m_cursor_row == (rows() - 1)) {
  421. scroll_up();
  422. } else {
  423. ++new_row;
  424. }
  425. set_cursor(new_row, 0);
  426. }
  427. void Terminal::scroll_up()
  428. {
  429. // NOTE: We have to invalidate the cursor first.
  430. invalidate_cursor();
  431. delete m_lines[0];
  432. for (word row = 1; row < rows(); ++row)
  433. m_lines[row - 1] = m_lines[row];
  434. m_lines[m_rows - 1] = new Line(m_columns);
  435. ++m_rows_to_scroll_backing_store;
  436. m_need_full_flush = true;
  437. }
  438. void Terminal::set_cursor(unsigned a_row, unsigned a_column)
  439. {
  440. unsigned row = min(a_row, m_rows - 1u);
  441. unsigned column = min(a_column, m_columns - 1u);
  442. if (row == m_cursor_row && column == m_cursor_column)
  443. return;
  444. ASSERT(row < rows());
  445. ASSERT(column < columns());
  446. invalidate_cursor();
  447. m_cursor_row = row;
  448. m_cursor_column = column;
  449. if (column != columns() - 1)
  450. m_stomp = false;
  451. invalidate_cursor();
  452. }
  453. void Terminal::put_character_at(unsigned row, unsigned column, byte ch)
  454. {
  455. ASSERT(row < rows());
  456. ASSERT(column < columns());
  457. auto& line = this->line(row);
  458. if ((line.characters[column] == ch) && (line.attributes[column] == m_current_attribute))
  459. return;
  460. line.characters[column] = ch;
  461. line.attributes[column] = m_current_attribute;
  462. line.dirty = true;
  463. }
  464. void Terminal::on_char(byte ch)
  465. {
  466. #ifdef TERMINAL_DEBUG
  467. dbgprintf("Terminal::on_char: %b (%c), fg=%u, bg=%u\n", ch, ch, m_current_attribute.foreground_color, m_current_attribute.background_color);
  468. #endif
  469. switch (m_escape_state) {
  470. case ExpectBracket:
  471. if (ch == '[')
  472. m_escape_state = ExpectParameter;
  473. else if (ch == '(') {
  474. m_swallow_current = true;
  475. m_escape_state = ExpectParameter;
  476. } else if (ch == ']')
  477. m_escape_state = ExpectXtermParameter1;
  478. else
  479. m_escape_state = Normal;
  480. return;
  481. case ExpectXtermParameter1:
  482. if (ch != ';') {
  483. m_xterm_param1.append(ch);
  484. return;
  485. }
  486. m_escape_state = ExpectXtermParameter2;
  487. return;
  488. case ExpectXtermParameter2:
  489. if (ch != '\007') {
  490. m_xterm_param2.append(ch);
  491. return;
  492. }
  493. m_escape_state = ExpectXtermFinal;
  494. [[fallthrough]];
  495. case ExpectXtermFinal:
  496. m_escape_state = Normal;
  497. if (ch == '\007')
  498. execute_xterm_command();
  499. return;
  500. case ExpectParameter:
  501. if (is_valid_parameter_character(ch)) {
  502. m_parameters.append(ch);
  503. return;
  504. }
  505. m_escape_state = ExpectIntermediate;
  506. [[fallthrough]];
  507. case ExpectIntermediate:
  508. if (is_valid_intermediate_character(ch)) {
  509. m_intermediates.append(ch);
  510. return;
  511. }
  512. m_escape_state = ExpectFinal;
  513. [[fallthrough]];
  514. case ExpectFinal:
  515. if (is_valid_final_character(ch)) {
  516. m_escape_state = Normal;
  517. if (!m_swallow_current)
  518. execute_escape_sequence(ch);
  519. m_swallow_current = false;
  520. return;
  521. }
  522. m_escape_state = Normal;
  523. m_swallow_current = false;
  524. return;
  525. case Normal:
  526. break;
  527. }
  528. switch (ch) {
  529. case '\0':
  530. return;
  531. case '\033':
  532. m_escape_state = ExpectBracket;
  533. m_swallow_current = false;
  534. return;
  535. case 8: // Backspace
  536. if (m_cursor_column) {
  537. set_cursor(m_cursor_row, m_cursor_column - 1);
  538. put_character_at(m_cursor_row, m_cursor_column, ' ');
  539. return;
  540. }
  541. return;
  542. case '\a':
  543. // FIXME: Bell!
  544. return;
  545. case '\t': {
  546. for (unsigned i = m_cursor_column; i < columns(); ++i) {
  547. if (m_horizontal_tabs[i]) {
  548. set_cursor(m_cursor_row, i);
  549. return;
  550. }
  551. }
  552. return;
  553. }
  554. case '\r':
  555. set_cursor(m_cursor_row, 0);
  556. return;
  557. case '\n':
  558. newline();
  559. return;
  560. }
  561. auto new_column = m_cursor_column + 1;
  562. if (new_column < columns()) {
  563. put_character_at(m_cursor_row, m_cursor_column, ch);
  564. set_cursor(m_cursor_row, new_column);
  565. } else {
  566. if (m_stomp) {
  567. m_stomp = false;
  568. newline();
  569. put_character_at(m_cursor_row, m_cursor_column, ch);
  570. set_cursor(m_cursor_row, 1);
  571. } else {
  572. // Curious: We wait once on the right-hand side
  573. m_stomp = true;
  574. put_character_at(m_cursor_row, m_cursor_column, ch);
  575. }
  576. }
  577. }
  578. void Terminal::inject_string(const String& str)
  579. {
  580. for (int i = 0; i < str.length(); ++i)
  581. on_char(str[i]);
  582. }
  583. void Terminal::unimplemented_escape()
  584. {
  585. StringBuilder builder;
  586. builder.appendf("((Unimplemented escape: %c", m_final);
  587. if (!m_parameters.is_empty()) {
  588. builder.append(" parameters:");
  589. for (int i = 0; i < m_parameters.size(); ++i)
  590. builder.append((char)m_parameters[i]);
  591. }
  592. if (!m_intermediates.is_empty()) {
  593. builder.append(" intermediates:");
  594. for (int i = 0; i < m_intermediates.size(); ++i)
  595. builder.append((char)m_intermediates[i]);
  596. }
  597. builder.append("))");
  598. inject_string(builder.to_string());
  599. }
  600. void Terminal::unimplemented_xterm_escape()
  601. {
  602. auto message = String::format("((Unimplemented xterm escape: %c))\n", m_final);
  603. inject_string(message);
  604. }
  605. void Terminal::set_size(word columns, word rows)
  606. {
  607. if (columns == m_columns && rows == m_rows)
  608. return;
  609. if (m_lines) {
  610. for (size_t i = 0; i < m_rows; ++i)
  611. delete m_lines[i];
  612. delete m_lines;
  613. }
  614. m_columns = columns;
  615. m_rows = rows;
  616. m_cursor_row = 0;
  617. m_cursor_column = 0;
  618. m_saved_cursor_row = 0;
  619. m_saved_cursor_column = 0;
  620. if (m_horizontal_tabs)
  621. free(m_horizontal_tabs);
  622. m_horizontal_tabs = static_cast<byte*>(malloc(columns));
  623. for (unsigned i = 0; i < columns; ++i)
  624. m_horizontal_tabs[i] = (i % 8) == 0;
  625. // Rightmost column is always last tab on line.
  626. m_horizontal_tabs[columns - 1] = 1;
  627. m_lines = new Line*[rows];
  628. for (size_t i = 0; i < rows; ++i)
  629. m_lines[i] = new Line(columns);
  630. m_pixel_width = m_columns * font().glyph_width('x') + m_inset * 2;
  631. m_pixel_height = (m_rows * (font().glyph_height() + m_line_spacing)) + (m_inset * 2) - m_line_spacing;
  632. set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  633. set_preferred_size({ m_pixel_width, m_pixel_height });
  634. m_rows_to_scroll_backing_store = 0;
  635. m_needs_background_fill = true;
  636. force_repaint();
  637. winsize ws;
  638. ws.ws_row = rows;
  639. ws.ws_col = columns;
  640. int rc = ioctl(m_ptm_fd, TIOCSWINSZ, &ws);
  641. ASSERT(rc == 0);
  642. }
  643. Rect Terminal::glyph_rect(word row, word column)
  644. {
  645. int y = row * m_line_height;
  646. int x = column * font().glyph_width('x');
  647. return { x + m_inset, y + m_inset, font().glyph_width('x'), font().glyph_height() };
  648. }
  649. Rect Terminal::row_rect(word row)
  650. {
  651. int y = row * m_line_height;
  652. Rect rect = { m_inset, y + m_inset, font().glyph_width('x') * m_columns, font().glyph_height() };
  653. rect.inflate(0, m_line_spacing);
  654. return rect;
  655. }
  656. bool Terminal::Line::has_only_one_background_color() const
  657. {
  658. if (!length)
  659. return true;
  660. // FIXME: Cache this result?
  661. auto color = attributes[0].background_color;
  662. for (size_t i = 1; i < length; ++i) {
  663. if (attributes[i].background_color != color)
  664. return false;
  665. }
  666. return true;
  667. }
  668. void Terminal::event(CEvent& event)
  669. {
  670. if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
  671. m_in_active_window = event.type() == GEvent::WindowBecameActive;
  672. if (!m_in_active_window) {
  673. m_cursor_blink_timer.stop();
  674. } else {
  675. m_cursor_blink_state = true;
  676. m_cursor_blink_timer.start();
  677. }
  678. invalidate_cursor();
  679. update();
  680. }
  681. return GWidget::event(event);
  682. }
  683. void Terminal::keydown_event(GKeyEvent& event)
  684. {
  685. char ch = !event.text().is_empty() ? event.text()[0] : 0;
  686. if (event.ctrl()) {
  687. if (ch >= 'a' && ch <= 'z') {
  688. ch = ch - 'a' + 1;
  689. } else if (ch == '\\') {
  690. ch = 0x1c;
  691. }
  692. }
  693. switch (event.key()) {
  694. case KeyCode::Key_Up:
  695. write(m_ptm_fd, "\033[A", 3);
  696. break;
  697. case KeyCode::Key_Down:
  698. write(m_ptm_fd, "\033[B", 3);
  699. break;
  700. case KeyCode::Key_Right:
  701. write(m_ptm_fd, "\033[C", 3);
  702. break;
  703. case KeyCode::Key_Left:
  704. write(m_ptm_fd, "\033[D", 3);
  705. break;
  706. default:
  707. write(m_ptm_fd, &ch, 1);
  708. break;
  709. }
  710. }
  711. void Terminal::paint_event(GPaintEvent&)
  712. {
  713. GPainter painter(*this);
  714. if (m_needs_background_fill) {
  715. m_needs_background_fill = false;
  716. painter.fill_rect(rect(), Color(Color::Black).with_alpha(255 * m_opacity));
  717. }
  718. if (m_rows_to_scroll_backing_store && m_rows_to_scroll_backing_store < m_rows) {
  719. int first_scanline = m_inset;
  720. int second_scanline = m_inset + (m_rows_to_scroll_backing_store * m_line_height);
  721. int num_rows_to_memcpy = m_rows - m_rows_to_scroll_backing_store;
  722. int scanlines_to_copy = (num_rows_to_memcpy * m_line_height) - m_line_spacing;
  723. fast_dword_copy(
  724. painter.target()->scanline(first_scanline),
  725. painter.target()->scanline(second_scanline),
  726. scanlines_to_copy * m_pixel_width
  727. );
  728. line(max(0, m_cursor_row - m_rows_to_scroll_backing_store)).dirty = true;
  729. }
  730. m_rows_to_scroll_backing_store = 0;
  731. invalidate_cursor();
  732. for (word row = 0; row < m_rows; ++row) {
  733. auto& line = this->line(row);
  734. if (!line.dirty)
  735. continue;
  736. line.dirty = false;
  737. bool has_only_one_background_color = line.has_only_one_background_color();
  738. if (has_only_one_background_color) {
  739. painter.fill_rect(row_rect(row), lookup_color(line.attributes[0].background_color).with_alpha(255 * m_opacity));
  740. }
  741. for (word column = 0; column < m_columns; ++column) {
  742. bool should_reverse_fill_for_cursor = m_cursor_blink_state && m_in_active_window && row == m_cursor_row && column == m_cursor_column;
  743. auto& attribute = line.attributes[column];
  744. char ch = line.characters[column];
  745. auto character_rect = glyph_rect(row, column);
  746. if (!has_only_one_background_color || should_reverse_fill_for_cursor) {
  747. auto cell_rect = character_rect.inflated(0, m_line_spacing);
  748. painter.fill_rect(cell_rect, lookup_color(should_reverse_fill_for_cursor ? attribute.foreground_color : attribute.background_color).with_alpha(255 * m_opacity));
  749. }
  750. if (ch == ' ')
  751. continue;
  752. painter.draw_glyph(character_rect.location(), ch, lookup_color(should_reverse_fill_for_cursor ? attribute.background_color : attribute.foreground_color));
  753. }
  754. }
  755. if (!m_in_active_window) {
  756. auto cell_rect = glyph_rect(m_cursor_row, m_cursor_column).inflated(0, m_line_spacing);
  757. painter.draw_rect(cell_rect, lookup_color(line(m_cursor_row).attributes[m_cursor_column].foreground_color));
  758. }
  759. if (m_belling)
  760. painter.draw_rect(rect(), Color::Red);
  761. }
  762. void Terminal::set_window_title(String&& title)
  763. {
  764. auto* w = window();
  765. if (!w)
  766. return;
  767. w->set_title(move(title));
  768. }
  769. void Terminal::invalidate_cursor()
  770. {
  771. line(m_cursor_row).dirty = true;
  772. }
  773. void Terminal::flush_dirty_lines()
  774. {
  775. if (m_need_full_flush) {
  776. update();
  777. m_need_full_flush = false;
  778. return;
  779. }
  780. Rect rect;
  781. for (int i = 0; i < m_rows; ++i) {
  782. if (line(i).dirty)
  783. rect = rect.united(row_rect(i));
  784. }
  785. update(rect);
  786. }
  787. void Terminal::force_repaint()
  788. {
  789. for (int i = 0; i < m_rows; ++i)
  790. line(i).dirty = true;
  791. update();
  792. }
  793. void Terminal::resize_event(GResizeEvent& event)
  794. {
  795. int new_columns = event.size().width() / font().glyph_width('x');
  796. int new_rows = event.size().height() / m_line_height;
  797. set_size(new_columns, new_rows);
  798. }
  799. void Terminal::apply_size_increments_to_window(GWindow& window)
  800. {
  801. window.set_size_increment({ font().glyph_width('x'), m_line_height });
  802. window.set_base_size({ m_inset * 2, m_inset * 2});
  803. }
  804. void Terminal::update_cursor()
  805. {
  806. invalidate_cursor();
  807. flush_dirty_lines();
  808. }