TerminalWidget.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. #include "TerminalWidget.h"
  2. #include "XtermColors.h"
  3. #include <AK/StdLibExtras.h>
  4. #include <AK/String.h>
  5. #include <AK/StringBuilder.h>
  6. #include <Kernel/KeyCode.h>
  7. #include <LibDraw/Font.h>
  8. #include <LibGUI/GAction.h>
  9. #include <LibGUI/GApplication.h>
  10. #include <LibGUI/GClipboard.h>
  11. #include <LibGUI/GPainter.h>
  12. #include <LibGUI/GScrollBar.h>
  13. #include <LibGUI/GWindow.h>
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/ioctl.h>
  19. #include <unistd.h>
  20. //#define TERMINAL_DEBUG
  21. void TerminalWidget::set_pty_master_fd(int fd)
  22. {
  23. m_ptm_fd = fd;
  24. if (m_ptm_fd == -1) {
  25. m_notifier = nullptr;
  26. return;
  27. }
  28. m_notifier = CNotifier::construct(m_ptm_fd, CNotifier::Read);
  29. m_notifier->on_ready_to_read = [this] {
  30. u8 buffer[BUFSIZ];
  31. ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer));
  32. if (nread < 0) {
  33. dbgprintf("Terminal read error: %s\n", strerror(errno));
  34. perror("read(ptm)");
  35. GApplication::the().quit(1);
  36. return;
  37. }
  38. if (nread == 0) {
  39. dbgprintf("Terminal: EOF on master pty, firing on_command_exit hook.\n");
  40. if (on_command_exit)
  41. on_command_exit();
  42. int rc = close(m_ptm_fd);
  43. if (rc < 0) {
  44. perror("close");
  45. }
  46. set_pty_master_fd(-1);
  47. return;
  48. }
  49. for (ssize_t i = 0; i < nread; ++i)
  50. m_terminal.on_char(buffer[i]);
  51. flush_dirty_lines();
  52. };
  53. }
  54. TerminalWidget::TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtr<CConfigFile> config)
  55. : m_terminal(*this)
  56. , m_automatic_size_policy(automatic_size_policy)
  57. , m_config(move(config))
  58. {
  59. set_pty_master_fd(ptm_fd);
  60. m_cursor_blink_timer = CTimer::construct();
  61. m_visual_beep_timer = CTimer::construct();
  62. set_frame_shape(FrameShape::Container);
  63. set_frame_shadow(FrameShadow::Sunken);
  64. set_frame_thickness(2);
  65. m_scrollbar = GScrollBar::construct(Orientation::Vertical, this);
  66. m_scrollbar->set_relative_rect(0, 0, 16, 0);
  67. m_scrollbar->on_change = [this](int) {
  68. force_repaint();
  69. };
  70. dbgprintf("Terminal: Load config file from %s\n", m_config->file_name().characters());
  71. m_cursor_blink_timer->set_interval(m_config->read_num_entry("Text",
  72. "CursorBlinkInterval",
  73. 500));
  74. m_cursor_blink_timer->on_timeout = [this] {
  75. m_cursor_blink_state = !m_cursor_blink_state;
  76. update_cursor();
  77. };
  78. auto font_entry = m_config->read_entry("Text", "Font", "default");
  79. if (font_entry == "default")
  80. set_font(Font::default_fixed_width_font());
  81. else
  82. set_font(Font::load_from_file(font_entry));
  83. m_line_height = font().glyph_height() + m_line_spacing;
  84. m_terminal.set_size(m_config->read_num_entry("Window", "Width", 80), m_config->read_num_entry("Window", "Height", 25));
  85. m_copy_action = GAction::create("Copy", { Mod_Ctrl | Mod_Shift, Key_C }, GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [this](auto&) {
  86. copy();
  87. });
  88. m_paste_action = GAction::create("Paste", { Mod_Ctrl | Mod_Shift, Key_V }, GraphicsBitmap::load_from_file("/res/icons/paste16.png"), [this](auto&) {
  89. paste();
  90. });
  91. }
  92. TerminalWidget::~TerminalWidget()
  93. {
  94. }
  95. static inline Color lookup_color(unsigned color)
  96. {
  97. return Color::from_rgb(xterm_colors[color]);
  98. }
  99. Rect TerminalWidget::glyph_rect(u16 row, u16 column)
  100. {
  101. int y = row * m_line_height;
  102. int x = column * font().glyph_width('x');
  103. return { x + frame_thickness() + m_inset, y + frame_thickness() + m_inset, font().glyph_width('x'), font().glyph_height() };
  104. }
  105. Rect TerminalWidget::row_rect(u16 row)
  106. {
  107. int y = row * m_line_height;
  108. Rect rect = { frame_thickness() + m_inset, y + frame_thickness() + m_inset, font().glyph_width('x') * m_terminal.columns(), font().glyph_height() };
  109. rect.inflate(0, m_line_spacing);
  110. return rect;
  111. }
  112. void TerminalWidget::set_logical_focus(bool focus)
  113. {
  114. m_has_logical_focus = focus;
  115. if (!m_has_logical_focus) {
  116. m_cursor_blink_timer->stop();
  117. } else {
  118. m_cursor_blink_state = true;
  119. m_cursor_blink_timer->start();
  120. }
  121. invalidate_cursor();
  122. update();
  123. }
  124. void TerminalWidget::focusin_event(CEvent& event)
  125. {
  126. set_logical_focus(true);
  127. return GFrame::focusin_event(event);
  128. }
  129. void TerminalWidget::focusout_event(CEvent& event)
  130. {
  131. set_logical_focus(false);
  132. return GFrame::focusout_event(event);
  133. }
  134. void TerminalWidget::event(CEvent& event)
  135. {
  136. if (event.type() == GEvent::WindowBecameActive)
  137. set_logical_focus(true);
  138. else if (event.type() == GEvent::WindowBecameInactive)
  139. set_logical_focus(false);
  140. return GFrame::event(event);
  141. }
  142. void TerminalWidget::keydown_event(GKeyEvent& event)
  143. {
  144. if (m_ptm_fd == -1) {
  145. event.ignore();
  146. return GFrame::keydown_event(event);
  147. }
  148. // Reset timer so cursor doesn't blink while typing.
  149. m_cursor_blink_timer->stop();
  150. m_cursor_blink_state = true;
  151. m_cursor_blink_timer->start();
  152. switch (event.key()) {
  153. case KeyCode::Key_Up:
  154. write(m_ptm_fd, "\033[A", 3);
  155. return;
  156. case KeyCode::Key_Down:
  157. write(m_ptm_fd, "\033[B", 3);
  158. return;
  159. case KeyCode::Key_Right:
  160. write(m_ptm_fd, "\033[C", 3);
  161. return;
  162. case KeyCode::Key_Left:
  163. write(m_ptm_fd, "\033[D", 3);
  164. return;
  165. case KeyCode::Key_Insert:
  166. write(m_ptm_fd, "\033[2~", 4);
  167. return;
  168. case KeyCode::Key_Delete:
  169. write(m_ptm_fd, "\033[3~", 4);
  170. return;
  171. case KeyCode::Key_Home:
  172. write(m_ptm_fd, "\033[H", 3);
  173. return;
  174. case KeyCode::Key_End:
  175. write(m_ptm_fd, "\033[F", 3);
  176. return;
  177. case KeyCode::Key_PageUp:
  178. if (event.modifiers() == Mod_Shift) {
  179. m_scrollbar->set_value(m_scrollbar->value() - m_terminal.rows());
  180. return;
  181. }
  182. write(m_ptm_fd, "\033[5~", 4);
  183. return;
  184. case KeyCode::Key_PageDown:
  185. if (event.modifiers() == Mod_Shift) {
  186. m_scrollbar->set_value(m_scrollbar->value() + m_terminal.rows());
  187. return;
  188. }
  189. write(m_ptm_fd, "\033[6~", 4);
  190. return;
  191. default:
  192. break;
  193. }
  194. // Key event was not one of the above special cases,
  195. // attempt to treat it as a character...
  196. char ch = !event.text().is_empty() ? event.text()[0] : 0;
  197. if (ch) {
  198. if (event.ctrl()) {
  199. if (ch >= 'a' && ch <= 'z') {
  200. ch = ch - 'a' + 1;
  201. } else if (ch == '\\') {
  202. ch = 0x1c;
  203. }
  204. }
  205. // ALT modifier sends escape prefix
  206. if (event.alt())
  207. write(m_ptm_fd, "\033", 1);
  208. //Clear the selection if we type in/behind it
  209. auto future_cursor_column = (event.key() == KeyCode::Key_Backspace) ? m_terminal.cursor_column() - 1 : m_terminal.cursor_column();
  210. auto min_selection_row = min(m_selection_start.row(), m_selection_end.row());
  211. auto max_selection_row = max(m_selection_start.row(), m_selection_end.row());
  212. if (future_cursor_column <= last_selection_column_on_row(m_terminal.cursor_row()) && m_terminal.cursor_row() >= min_selection_row && m_terminal.cursor_row() <= max_selection_row) {
  213. m_selection_end = {};
  214. update();
  215. }
  216. write(m_ptm_fd, &ch, 1);
  217. }
  218. m_scrollbar->set_value(m_scrollbar->max());
  219. }
  220. void TerminalWidget::paint_event(GPaintEvent& event)
  221. {
  222. GFrame::paint_event(event);
  223. GPainter painter(*this);
  224. painter.add_clip_rect(event.rect());
  225. Rect terminal_buffer_rect(frame_inner_rect().top_left(), { frame_inner_rect().width() - m_scrollbar->width(), frame_inner_rect().height() });
  226. painter.add_clip_rect(terminal_buffer_rect);
  227. if (m_visual_beep_timer->is_active())
  228. painter.fill_rect(frame_inner_rect(), Color::Red);
  229. else
  230. painter.fill_rect(frame_inner_rect(), Color(Color::Black).with_alpha(m_opacity));
  231. invalidate_cursor();
  232. int rows_from_history = 0;
  233. int first_row_from_history = 0;
  234. int row_with_cursor = m_terminal.cursor_row();
  235. if (m_scrollbar->value() != m_scrollbar->max()) {
  236. rows_from_history = min((int)m_terminal.rows(), m_scrollbar->max() - m_scrollbar->value());
  237. first_row_from_history = m_terminal.history().size() - (m_scrollbar->max() - m_scrollbar->value());
  238. row_with_cursor = m_terminal.cursor_row() + rows_from_history;
  239. }
  240. auto line_for_visual_row = [&](u16 row) -> const VT::Terminal::Line& {
  241. if (row < rows_from_history)
  242. return m_terminal.history().at(first_row_from_history + row);
  243. return m_terminal.line(row - rows_from_history);
  244. };
  245. for (u16 row = 0; row < m_terminal.rows(); ++row) {
  246. auto row_rect = this->row_rect(row);
  247. if (!event.rect().contains(row_rect))
  248. continue;
  249. auto& line = line_for_visual_row(row);
  250. bool has_only_one_background_color = line.has_only_one_background_color();
  251. if (m_visual_beep_timer->is_active())
  252. painter.fill_rect(row_rect, Color::Red);
  253. else if (has_only_one_background_color)
  254. painter.fill_rect(row_rect, lookup_color(line.attributes[0].background_color).with_alpha(m_opacity));
  255. // The terminal insists on thinking characters and
  256. // bytes are the same thing. We want to still draw
  257. // emojis in *some* way, but it won't be completely
  258. // perfect. So what we do is we make multi-byte
  259. // characters take up multiple columns, and render
  260. // the character itself in the center of the columns
  261. // its bytes take up as far as the terminal is concerned.
  262. Utf8View utf8_view { line.text() };
  263. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  264. u32 codepoint = *it;
  265. int this_char_column = utf8_view.byte_offset_of(it);
  266. AK::Utf8CodepointIterator it_copy = it;
  267. int next_char_column = utf8_view.byte_offset_of(++it_copy);
  268. // Columns from this_char_column up until next_char_column
  269. // are logically taken up by this (possibly multi-byte)
  270. // character. Iterate over these columns and draw background
  271. // for each one of them separately.
  272. bool should_reverse_fill_for_cursor_or_selection = false;
  273. VT::Attribute attribute;
  274. for (u16 column = this_char_column; column < next_char_column; ++column) {
  275. should_reverse_fill_for_cursor_or_selection |= m_cursor_blink_state
  276. && m_has_logical_focus
  277. && row == row_with_cursor
  278. && column == m_terminal.cursor_column();
  279. should_reverse_fill_for_cursor_or_selection |= selection_contains({ row, column });
  280. attribute = line.attributes[column];
  281. auto character_rect = glyph_rect(row, column);
  282. auto cell_rect = character_rect.inflated(0, m_line_spacing);
  283. if (!has_only_one_background_color || should_reverse_fill_for_cursor_or_selection) {
  284. painter.fill_rect(cell_rect, lookup_color(should_reverse_fill_for_cursor_or_selection ? attribute.foreground_color : attribute.background_color).with_alpha(m_opacity));
  285. }
  286. if (attribute.flags & VT::Attribute::Underline)
  287. painter.draw_line(cell_rect.bottom_left(), cell_rect.bottom_right(), lookup_color(should_reverse_fill_for_cursor_or_selection ? attribute.background_color : attribute.foreground_color));
  288. }
  289. if (codepoint == ' ')
  290. continue;
  291. auto character_rect = glyph_rect(row, this_char_column);
  292. auto num_columns = next_char_column - this_char_column;
  293. character_rect.move_by((num_columns - 1) * font().glyph_width('x') / 2, 0);
  294. painter.draw_glyph_or_emoji(
  295. character_rect.location(),
  296. codepoint,
  297. attribute.flags & VT::Attribute::Bold ? Font::default_bold_fixed_width_font() : font(),
  298. lookup_color(should_reverse_fill_for_cursor_or_selection ? attribute.background_color : attribute.foreground_color));
  299. }
  300. }
  301. if (!m_has_logical_focus && row_with_cursor < m_terminal.rows()) {
  302. auto& cursor_line = line_for_visual_row(row_with_cursor);
  303. if (m_terminal.cursor_row() < (m_terminal.rows() - rows_from_history)) {
  304. auto cell_rect = glyph_rect(row_with_cursor, m_terminal.cursor_column()).inflated(0, m_line_spacing);
  305. painter.draw_rect(cell_rect, lookup_color(cursor_line.attributes[m_terminal.cursor_column()].foreground_color));
  306. }
  307. }
  308. }
  309. void TerminalWidget::set_window_title(const StringView& title)
  310. {
  311. if (on_title_change)
  312. on_title_change(title);
  313. }
  314. void TerminalWidget::invalidate_cursor()
  315. {
  316. m_terminal.invalidate_cursor();
  317. }
  318. void TerminalWidget::flush_dirty_lines()
  319. {
  320. // FIXME: Update smarter when scrolled
  321. if (m_terminal.m_need_full_flush || m_scrollbar->value() != m_scrollbar->max()) {
  322. update();
  323. m_terminal.m_need_full_flush = false;
  324. return;
  325. }
  326. Rect rect;
  327. for (int i = 0; i < m_terminal.rows(); ++i) {
  328. if (m_terminal.line(i).dirty) {
  329. rect = rect.united(row_rect(i));
  330. m_terminal.line(i).dirty = false;
  331. }
  332. }
  333. update(rect);
  334. }
  335. void TerminalWidget::force_repaint()
  336. {
  337. m_needs_background_fill = true;
  338. update();
  339. }
  340. void TerminalWidget::resize_event(GResizeEvent& event)
  341. {
  342. auto base_size = compute_base_size();
  343. int new_columns = (event.size().width() - base_size.width()) / font().glyph_width('x');
  344. int new_rows = (event.size().height() - base_size.height()) / m_line_height;
  345. m_terminal.set_size(new_columns, new_rows);
  346. Rect scrollbar_rect = {
  347. event.size().width() - m_scrollbar->width() - frame_thickness(),
  348. frame_thickness(),
  349. m_scrollbar->width(),
  350. event.size().height() - frame_thickness() * 2,
  351. };
  352. m_scrollbar->set_relative_rect(scrollbar_rect);
  353. }
  354. Size TerminalWidget::compute_base_size() const
  355. {
  356. int base_width = frame_thickness() * 2 + m_inset * 2 + m_scrollbar->width();
  357. int base_height = frame_thickness() * 2 + m_inset * 2;
  358. return { base_width, base_height };
  359. }
  360. void TerminalWidget::apply_size_increments_to_window(GWindow& window)
  361. {
  362. window.set_size_increment({ font().glyph_width('x'), m_line_height });
  363. window.set_base_size(compute_base_size());
  364. }
  365. void TerminalWidget::update_cursor()
  366. {
  367. invalidate_cursor();
  368. flush_dirty_lines();
  369. }
  370. void TerminalWidget::set_opacity(u8 new_opacity)
  371. {
  372. if (m_opacity == new_opacity)
  373. return;
  374. window()->set_has_alpha_channel(new_opacity < 255);
  375. m_opacity = new_opacity;
  376. force_repaint();
  377. }
  378. VT::Position TerminalWidget::normalized_selection_start() const
  379. {
  380. if (m_selection_start < m_selection_end)
  381. return m_selection_start;
  382. return m_selection_end;
  383. }
  384. VT::Position TerminalWidget::normalized_selection_end() const
  385. {
  386. if (m_selection_start < m_selection_end)
  387. return m_selection_end;
  388. return m_selection_start;
  389. }
  390. bool TerminalWidget::has_selection() const
  391. {
  392. return m_selection_start.is_valid() && m_selection_end.is_valid();
  393. }
  394. bool TerminalWidget::selection_contains(const VT::Position& position) const
  395. {
  396. if (!has_selection())
  397. return false;
  398. return position >= normalized_selection_start() && position <= normalized_selection_end();
  399. }
  400. VT::Position TerminalWidget::buffer_position_at(const Point& position) const
  401. {
  402. auto adjusted_position = position.translated(-(frame_thickness() + m_inset), -(frame_thickness() + m_inset));
  403. int row = adjusted_position.y() / m_line_height;
  404. int column = adjusted_position.x() / font().glyph_width('x');
  405. if (row < 0)
  406. row = 0;
  407. if (column < 0)
  408. column = 0;
  409. if (row >= m_terminal.rows())
  410. row = m_terminal.rows() - 1;
  411. if (column >= m_terminal.columns())
  412. column = m_terminal.columns() - 1;
  413. return { row, column };
  414. }
  415. void TerminalWidget::doubleclick_event(GMouseEvent& event)
  416. {
  417. if (event.button() == GMouseButton::Left) {
  418. m_triple_click_timer.start();
  419. auto position = buffer_position_at(event.position());
  420. auto& line = m_terminal.line(position.row());
  421. bool want_whitespace = line.characters[position.column()] == ' ';
  422. int start_column = 0;
  423. int end_column = 0;
  424. for (int column = position.column(); column >= 0 && (line.characters[column] == ' ') == want_whitespace; --column) {
  425. start_column = column;
  426. }
  427. for (int column = position.column(); column < m_terminal.columns() && (line.characters[column] == ' ') == want_whitespace; ++column) {
  428. end_column = column;
  429. }
  430. m_selection_start = { position.row(), start_column };
  431. m_selection_end = { position.row(), end_column };
  432. if (has_selection())
  433. GClipboard::the().set_data(selected_text());
  434. }
  435. GFrame::doubleclick_event(event);
  436. }
  437. void TerminalWidget::paste()
  438. {
  439. if (m_ptm_fd == -1)
  440. return;
  441. auto text = GClipboard::the().data();
  442. if (text.is_empty())
  443. return;
  444. int nwritten = write(m_ptm_fd, text.characters(), text.length());
  445. if (nwritten < 0) {
  446. perror("write");
  447. ASSERT_NOT_REACHED();
  448. }
  449. }
  450. void TerminalWidget::copy()
  451. {
  452. if (has_selection())
  453. GClipboard::the().set_data(selected_text());
  454. }
  455. void TerminalWidget::mousedown_event(GMouseEvent& event)
  456. {
  457. if (event.button() == GMouseButton::Left) {
  458. if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
  459. int start_column = 0;
  460. int end_column = m_terminal.columns() - 1;
  461. auto position = buffer_position_at(event.position());
  462. m_selection_start = { position.row(), start_column };
  463. m_selection_end = { position.row(), end_column };
  464. } else {
  465. m_selection_start = buffer_position_at(event.position());
  466. m_selection_end = {};
  467. }
  468. update();
  469. }
  470. }
  471. void TerminalWidget::mousemove_event(GMouseEvent& event)
  472. {
  473. if (!(event.buttons() & GMouseButton::Left))
  474. return;
  475. auto old_selection_end = m_selection_end;
  476. m_selection_end = buffer_position_at(event.position());
  477. if (old_selection_end != m_selection_end)
  478. update();
  479. }
  480. void TerminalWidget::mouseup_event(GMouseEvent& event)
  481. {
  482. if (event.button() != GMouseButton::Left)
  483. return;
  484. if (!has_selection())
  485. return;
  486. GClipboard::the().set_data(selected_text());
  487. }
  488. void TerminalWidget::mousewheel_event(GMouseEvent& event)
  489. {
  490. if (!is_scrollable())
  491. return;
  492. m_scrollbar->set_value(m_scrollbar->value() + event.wheel_delta());
  493. GFrame::mousewheel_event(event);
  494. }
  495. bool TerminalWidget::is_scrollable() const
  496. {
  497. return m_scrollbar->is_scrollable();
  498. }
  499. String TerminalWidget::selected_text() const
  500. {
  501. StringBuilder builder;
  502. auto start = normalized_selection_start();
  503. auto end = normalized_selection_end();
  504. for (int row = start.row(); row <= end.row(); ++row) {
  505. int first_column = first_selection_column_on_row(row);
  506. int last_column = last_selection_column_on_row(row);
  507. for (int column = first_column; column <= last_column; ++column) {
  508. auto& line = m_terminal.line(row);
  509. if (line.attributes[column].is_untouched()) {
  510. builder.append('\n');
  511. break;
  512. }
  513. builder.append(line.characters[column]);
  514. if (column == line.m_length - 1) {
  515. builder.append('\n');
  516. }
  517. }
  518. }
  519. return builder.to_string();
  520. }
  521. int TerminalWidget::first_selection_column_on_row(int row) const
  522. {
  523. return row == normalized_selection_start().row() ? normalized_selection_start().column() : 0;
  524. }
  525. int TerminalWidget::last_selection_column_on_row(int row) const
  526. {
  527. return row == normalized_selection_end().row() ? normalized_selection_end().column() : m_terminal.columns() - 1;
  528. }
  529. void TerminalWidget::terminal_history_changed()
  530. {
  531. bool was_max = m_scrollbar->value() == m_scrollbar->max();
  532. m_scrollbar->set_max(m_terminal.history().size());
  533. if (was_max)
  534. m_scrollbar->set_value(m_scrollbar->max());
  535. m_scrollbar->update();
  536. }
  537. void TerminalWidget::terminal_did_resize(u16 columns, u16 rows)
  538. {
  539. m_pixel_width = (frame_thickness() * 2) + (m_inset * 2) + (columns * font().glyph_width('x'));
  540. m_pixel_height = (frame_thickness() * 2) + (m_inset * 2) + (rows * (font().glyph_height() + m_line_spacing)) - m_line_spacing;
  541. if (m_automatic_size_policy) {
  542. set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  543. set_preferred_size(m_pixel_width, m_pixel_height);
  544. }
  545. m_needs_background_fill = true;
  546. force_repaint();
  547. winsize ws;
  548. ws.ws_row = rows;
  549. ws.ws_col = columns;
  550. if (m_ptm_fd != -1) {
  551. int rc = ioctl(m_ptm_fd, TIOCSWINSZ, &ws);
  552. ASSERT(rc == 0);
  553. }
  554. }
  555. void TerminalWidget::beep()
  556. {
  557. if (m_should_beep) {
  558. sysbeep();
  559. return;
  560. }
  561. m_visual_beep_timer->restart(200);
  562. m_visual_beep_timer->set_single_shot(true);
  563. m_visual_beep_timer->on_timeout = [this] {
  564. force_repaint();
  565. };
  566. force_repaint();
  567. }