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