Terminal.cpp 24 KB

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