Terminal.cpp 24 KB

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