Serenity Operating System
at portability 735 lines 25 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "TerminalWidget.h" 28#include "XtermColors.h" 29#include <AK/StdLibExtras.h> 30#include <AK/String.h> 31#include <AK/StringBuilder.h> 32#include <AK/Utf8View.h> 33#include <Kernel/KeyCode.h> 34#include <LibCore/MimeData.h> 35#include <LibGUI/Action.h> 36#include <LibGUI/Application.h> 37#include <LibGUI/Clipboard.h> 38#include <LibGUI/Menu.h> 39#include <LibGUI/Painter.h> 40#include <LibGUI/ScrollBar.h> 41#include <LibGUI/Window.h> 42#include <LibGfx/Font.h> 43#include <errno.h> 44#include <stdio.h> 45#include <stdlib.h> 46#include <string.h> 47#include <sys/ioctl.h> 48#include <unistd.h> 49 50//#define TERMINAL_DEBUG 51 52void TerminalWidget::set_pty_master_fd(int fd) 53{ 54 m_ptm_fd = fd; 55 if (m_ptm_fd == -1) { 56 m_notifier = nullptr; 57 return; 58 } 59 m_notifier = Core::Notifier::construct(m_ptm_fd, Core::Notifier::Read); 60 m_notifier->on_ready_to_read = [this] { 61 u8 buffer[BUFSIZ]; 62 ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer)); 63 if (nread < 0) { 64 dbgprintf("Terminal read error: %s\n", strerror(errno)); 65 perror("read(ptm)"); 66 GUI::Application::the().quit(1); 67 return; 68 } 69 if (nread == 0) { 70 dbgprintf("Terminal: EOF on master pty, firing on_command_exit hook.\n"); 71 if (on_command_exit) 72 on_command_exit(); 73 int rc = close(m_ptm_fd); 74 if (rc < 0) { 75 perror("close"); 76 } 77 set_pty_master_fd(-1); 78 return; 79 } 80 for (ssize_t i = 0; i < nread; ++i) 81 m_terminal.on_char(buffer[i]); 82 flush_dirty_lines(); 83 }; 84} 85 86TerminalWidget::TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtr<Core::ConfigFile> config) 87 : m_terminal(*this) 88 , m_automatic_size_policy(automatic_size_policy) 89 , m_config(move(config)) 90{ 91 set_pty_master_fd(ptm_fd); 92 m_cursor_blink_timer = add<Core::Timer>(); 93 m_visual_beep_timer = add<Core::Timer>(); 94 95 m_scrollbar = add<GUI::ScrollBar>(Orientation::Vertical); 96 m_scrollbar->set_relative_rect(0, 0, 16, 0); 97 m_scrollbar->on_change = [this](int) { 98 force_repaint(); 99 }; 100 101 dbgprintf("Terminal: Load config file from %s\n", m_config->file_name().characters()); 102 m_cursor_blink_timer->set_interval(m_config->read_num_entry("Text", 103 "CursorBlinkInterval", 104 500)); 105 m_cursor_blink_timer->on_timeout = [this] { 106 m_cursor_blink_state = !m_cursor_blink_state; 107 update_cursor(); 108 }; 109 110 auto font_entry = m_config->read_entry("Text", "Font", "default"); 111 if (font_entry == "default") 112 set_font(Gfx::Font::default_fixed_width_font()); 113 else 114 set_font(Gfx::Font::load_from_file(font_entry)); 115 116 m_line_height = font().glyph_height() + m_line_spacing; 117 118 m_terminal.set_size(m_config->read_num_entry("Window", "Width", 80), m_config->read_num_entry("Window", "Height", 25)); 119 120 m_copy_action = GUI::Action::create("Copy", { Mod_Ctrl | Mod_Shift, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [this](auto&) { 121 copy(); 122 }); 123 124 m_paste_action = GUI::Action::create("Paste", { Mod_Ctrl | Mod_Shift, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/paste16.png"), [this](auto&) { 125 paste(); 126 }); 127} 128 129TerminalWidget::~TerminalWidget() 130{ 131} 132 133static inline Color lookup_color(unsigned color) 134{ 135 return Color::from_rgb(xterm_colors[color]); 136} 137 138Gfx::Rect TerminalWidget::glyph_rect(u16 row, u16 column) 139{ 140 int y = row * m_line_height; 141 int x = column * font().glyph_width('x'); 142 return { x + frame_thickness() + m_inset, y + frame_thickness() + m_inset, font().glyph_width('x'), font().glyph_height() }; 143} 144 145Gfx::Rect TerminalWidget::row_rect(u16 row) 146{ 147 int y = row * m_line_height; 148 Gfx::Rect rect = { frame_thickness() + m_inset, y + frame_thickness() + m_inset, font().glyph_width('x') * m_terminal.columns(), font().glyph_height() }; 149 rect.inflate(0, m_line_spacing); 150 return rect; 151} 152 153void TerminalWidget::set_logical_focus(bool focus) 154{ 155 m_has_logical_focus = focus; 156 if (!m_has_logical_focus) { 157 m_cursor_blink_timer->stop(); 158 } else { 159 m_cursor_blink_state = true; 160 m_cursor_blink_timer->start(); 161 } 162 invalidate_cursor(); 163 update(); 164} 165 166void TerminalWidget::focusin_event(Core::Event& event) 167{ 168 set_logical_focus(true); 169 return GUI::Frame::focusin_event(event); 170} 171 172void TerminalWidget::focusout_event(Core::Event& event) 173{ 174 set_logical_focus(false); 175 return GUI::Frame::focusout_event(event); 176} 177 178void TerminalWidget::event(Core::Event& event) 179{ 180 if (event.type() == GUI::Event::WindowBecameActive) 181 set_logical_focus(true); 182 else if (event.type() == GUI::Event::WindowBecameInactive) 183 set_logical_focus(false); 184 return GUI::Frame::event(event); 185} 186 187void TerminalWidget::keydown_event(GUI::KeyEvent& event) 188{ 189 if (m_ptm_fd == -1) { 190 event.ignore(); 191 return GUI::Frame::keydown_event(event); 192 } 193 194 // Reset timer so cursor doesn't blink while typing. 195 m_cursor_blink_timer->stop(); 196 m_cursor_blink_state = true; 197 m_cursor_blink_timer->start(); 198 199 switch (event.key()) { 200 case KeyCode::Key_Up: 201 write(m_ptm_fd, "\033[A", 3); 202 return; 203 case KeyCode::Key_Down: 204 write(m_ptm_fd, "\033[B", 3); 205 return; 206 case KeyCode::Key_Right: 207 write(m_ptm_fd, "\033[C", 3); 208 return; 209 case KeyCode::Key_Left: 210 write(m_ptm_fd, "\033[D", 3); 211 return; 212 case KeyCode::Key_Insert: 213 write(m_ptm_fd, "\033[2~", 4); 214 return; 215 case KeyCode::Key_Delete: 216 write(m_ptm_fd, "\033[3~", 4); 217 return; 218 case KeyCode::Key_Home: 219 write(m_ptm_fd, "\033[H", 3); 220 return; 221 case KeyCode::Key_End: 222 write(m_ptm_fd, "\033[F", 3); 223 return; 224 case KeyCode::Key_PageUp: 225 if (event.modifiers() == Mod_Shift) { 226 m_scrollbar->set_value(m_scrollbar->value() - m_terminal.rows()); 227 return; 228 } 229 write(m_ptm_fd, "\033[5~", 4); 230 return; 231 case KeyCode::Key_PageDown: 232 if (event.modifiers() == Mod_Shift) { 233 m_scrollbar->set_value(m_scrollbar->value() + m_terminal.rows()); 234 return; 235 } 236 write(m_ptm_fd, "\033[6~", 4); 237 return; 238 default: 239 break; 240 } 241 242 // Key event was not one of the above special cases, 243 // attempt to treat it as a character... 244 char ch = !event.text().is_empty() ? event.text()[0] : 0; 245 if (ch) { 246 if (event.ctrl()) { 247 if (ch >= 'a' && ch <= 'z') { 248 ch = ch - 'a' + 1; 249 } else if (ch == '\\') { 250 ch = 0x1c; 251 } 252 } 253 254 // ALT modifier sends escape prefix 255 if (event.alt()) 256 write(m_ptm_fd, "\033", 1); 257 258 //Clear the selection if we type in/behind it 259 auto future_cursor_column = (event.key() == KeyCode::Key_Backspace) ? m_terminal.cursor_column() - 1 : m_terminal.cursor_column(); 260 auto min_selection_row = min(m_selection_start.row(), m_selection_end.row()); 261 auto max_selection_row = max(m_selection_start.row(), m_selection_end.row()); 262 263 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) { 264 m_selection_end = {}; 265 update(); 266 } 267 268 write(m_ptm_fd, &ch, 1); 269 } 270 271 if (event.key() != Key_Control && event.key() != Key_Alt && event.key() != Key_Shift) 272 m_scrollbar->set_value(m_scrollbar->max()); 273} 274 275void TerminalWidget::paint_event(GUI::PaintEvent& event) 276{ 277 GUI::Frame::paint_event(event); 278 279 GUI::Painter painter(*this); 280 281 painter.add_clip_rect(event.rect()); 282 283 Gfx::Rect terminal_buffer_rect(frame_inner_rect().top_left(), { frame_inner_rect().width() - m_scrollbar->width(), frame_inner_rect().height() }); 284 painter.add_clip_rect(terminal_buffer_rect); 285 286 if (m_visual_beep_timer->is_active()) 287 painter.clear_rect(frame_inner_rect(), Color::Red); 288 else 289 painter.clear_rect(frame_inner_rect(), Color(Color::Black).with_alpha(m_opacity)); 290 invalidate_cursor(); 291 292 int rows_from_history = 0; 293 int first_row_from_history = 0; 294 int row_with_cursor = m_terminal.cursor_row(); 295 if (m_scrollbar->value() != m_scrollbar->max()) { 296 rows_from_history = min((int)m_terminal.rows(), m_scrollbar->max() - m_scrollbar->value()); 297 first_row_from_history = m_terminal.history().size() - (m_scrollbar->max() - m_scrollbar->value()); 298 row_with_cursor = m_terminal.cursor_row() + rows_from_history; 299 } 300 301 auto line_for_visual_row = [&](u16 row) -> const VT::Terminal::Line& { 302 if (row < rows_from_history) 303 return m_terminal.history().at(first_row_from_history + row); 304 return m_terminal.line(row - rows_from_history); 305 }; 306 307 for (u16 row = 0; row < m_terminal.rows(); ++row) { 308 auto row_rect = this->row_rect(row); 309 if (!event.rect().contains(row_rect)) 310 continue; 311 auto& line = line_for_visual_row(row); 312 bool has_only_one_background_color = line.has_only_one_background_color(); 313 if (m_visual_beep_timer->is_active()) 314 painter.clear_rect(row_rect, Color::Red); 315 else if (has_only_one_background_color) 316 painter.clear_rect(row_rect, lookup_color(line.attributes[0].background_color).with_alpha(m_opacity)); 317 318 // The terminal insists on thinking characters and 319 // bytes are the same thing. We want to still draw 320 // emojis in *some* way, but it won't be completely 321 // perfect. So what we do is we make multi-byte 322 // characters take up multiple columns, and render 323 // the character itself in the center of the columns 324 // its bytes take up as far as the terminal is concerned. 325 326 Utf8View utf8_view { line.text() }; 327 328 for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) { 329 u32 codepoint = *it; 330 int this_char_column = utf8_view.byte_offset_of(it); 331 AK::Utf8CodepointIterator it_copy = it; 332 int next_char_column = utf8_view.byte_offset_of(++it_copy); 333 334 // Columns from this_char_column up until next_char_column 335 // are logically taken up by this (possibly multi-byte) 336 // character. Iterate over these columns and draw background 337 // for each one of them separately. 338 339 bool should_reverse_fill_for_cursor_or_selection = false; 340 VT::Attribute attribute; 341 342 for (u16 column = this_char_column; column < next_char_column; ++column) { 343 should_reverse_fill_for_cursor_or_selection |= m_cursor_blink_state 344 && m_has_logical_focus 345 && row == row_with_cursor 346 && column == m_terminal.cursor_column(); 347 should_reverse_fill_for_cursor_or_selection |= selection_contains({ row, column }); 348 attribute = line.attributes[column]; 349 auto character_rect = glyph_rect(row, column); 350 auto cell_rect = character_rect.inflated(0, m_line_spacing); 351 if (!has_only_one_background_color || should_reverse_fill_for_cursor_or_selection) { 352 painter.clear_rect(cell_rect, lookup_color(should_reverse_fill_for_cursor_or_selection ? attribute.foreground_color : attribute.background_color).with_alpha(m_opacity)); 353 } 354 if (attribute.flags & VT::Attribute::Underline) 355 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)); 356 } 357 358 if (codepoint == ' ') 359 continue; 360 361 auto character_rect = glyph_rect(row, this_char_column); 362 auto num_columns = next_char_column - this_char_column; 363 character_rect.move_by((num_columns - 1) * font().glyph_width('x') / 2, 0); 364 painter.draw_glyph_or_emoji( 365 character_rect.location(), 366 codepoint, 367 attribute.flags & VT::Attribute::Bold ? bold_font() : font(), 368 lookup_color(should_reverse_fill_for_cursor_or_selection ? attribute.background_color : attribute.foreground_color)); 369 } 370 } 371 372 if (!m_has_logical_focus && row_with_cursor < m_terminal.rows()) { 373 auto& cursor_line = line_for_visual_row(row_with_cursor); 374 if (m_terminal.cursor_row() < (m_terminal.rows() - rows_from_history)) { 375 auto cell_rect = glyph_rect(row_with_cursor, m_terminal.cursor_column()).inflated(0, m_line_spacing); 376 painter.draw_rect(cell_rect, lookup_color(cursor_line.attributes[m_terminal.cursor_column()].foreground_color)); 377 } 378 } 379} 380 381void TerminalWidget::set_window_title(const StringView& title) 382{ 383 if (on_title_change) 384 on_title_change(title); 385} 386 387void TerminalWidget::invalidate_cursor() 388{ 389 m_terminal.invalidate_cursor(); 390} 391 392void TerminalWidget::flush_dirty_lines() 393{ 394 // FIXME: Update smarter when scrolled 395 if (m_terminal.m_need_full_flush || m_scrollbar->value() != m_scrollbar->max()) { 396 update(); 397 m_terminal.m_need_full_flush = false; 398 return; 399 } 400 Gfx::Rect rect; 401 for (int i = 0; i < m_terminal.rows(); ++i) { 402 if (m_terminal.line(i).dirty) { 403 rect = rect.united(row_rect(i)); 404 m_terminal.line(i).dirty = false; 405 } 406 } 407 update(rect); 408} 409 410void TerminalWidget::force_repaint() 411{ 412 m_needs_background_fill = true; 413 update(); 414} 415 416void TerminalWidget::resize_event(GUI::ResizeEvent& event) 417{ 418 relayout(event.size()); 419} 420 421void TerminalWidget::relayout(const Gfx::Size& size) 422{ 423 if (!m_scrollbar) 424 return; 425 426 auto base_size = compute_base_size(); 427 int new_columns = (size.width() - base_size.width()) / font().glyph_width('x'); 428 int new_rows = (size.height() - base_size.height()) / m_line_height; 429 m_terminal.set_size(new_columns, new_rows); 430 431 Gfx::Rect scrollbar_rect = { 432 size.width() - m_scrollbar->width() - frame_thickness(), 433 frame_thickness(), 434 m_scrollbar->width(), 435 size.height() - frame_thickness() * 2, 436 }; 437 m_scrollbar->set_relative_rect(scrollbar_rect); 438} 439 440Gfx::Size TerminalWidget::compute_base_size() const 441{ 442 int base_width = frame_thickness() * 2 + m_inset * 2 + m_scrollbar->width(); 443 int base_height = frame_thickness() * 2 + m_inset * 2; 444 return { base_width, base_height }; 445} 446 447void TerminalWidget::apply_size_increments_to_window(GUI::Window& window) 448{ 449 window.set_size_increment({ font().glyph_width('x'), m_line_height }); 450 window.set_base_size(compute_base_size()); 451} 452 453void TerminalWidget::update_cursor() 454{ 455 invalidate_cursor(); 456 flush_dirty_lines(); 457} 458 459void TerminalWidget::set_opacity(u8 new_opacity) 460{ 461 if (m_opacity == new_opacity) 462 return; 463 464 window()->set_has_alpha_channel(new_opacity < 255); 465 m_opacity = new_opacity; 466 force_repaint(); 467} 468 469VT::Position TerminalWidget::normalized_selection_start() const 470{ 471 if (m_selection_start < m_selection_end) 472 return m_selection_start; 473 return m_selection_end; 474} 475 476VT::Position TerminalWidget::normalized_selection_end() const 477{ 478 if (m_selection_start < m_selection_end) 479 return m_selection_end; 480 return m_selection_start; 481} 482 483bool TerminalWidget::has_selection() const 484{ 485 return m_selection_start.is_valid() && m_selection_end.is_valid(); 486} 487 488bool TerminalWidget::selection_contains(const VT::Position& position) const 489{ 490 if (!has_selection()) 491 return false; 492 493 return position >= normalized_selection_start() && position <= normalized_selection_end(); 494} 495 496VT::Position TerminalWidget::buffer_position_at(const Gfx::Point& position) const 497{ 498 auto adjusted_position = position.translated(-(frame_thickness() + m_inset), -(frame_thickness() + m_inset)); 499 int row = adjusted_position.y() / m_line_height; 500 int column = adjusted_position.x() / font().glyph_width('x'); 501 if (row < 0) 502 row = 0; 503 if (column < 0) 504 column = 0; 505 if (row >= m_terminal.rows()) 506 row = m_terminal.rows() - 1; 507 if (column >= m_terminal.columns()) 508 column = m_terminal.columns() - 1; 509 return { row, column }; 510} 511 512void TerminalWidget::doubleclick_event(GUI::MouseEvent& event) 513{ 514 if (event.button() == GUI::MouseButton::Left) { 515 m_triple_click_timer.start(); 516 517 auto position = buffer_position_at(event.position()); 518 auto& line = m_terminal.line(position.row()); 519 bool want_whitespace = line.characters[position.column()] == ' '; 520 521 int start_column = 0; 522 int end_column = 0; 523 524 for (int column = position.column(); column >= 0 && (line.characters[column] == ' ') == want_whitespace; --column) { 525 start_column = column; 526 } 527 528 for (int column = position.column(); column < m_terminal.columns() && (line.characters[column] == ' ') == want_whitespace; ++column) { 529 end_column = column; 530 } 531 532 m_selection_start = { position.row(), start_column }; 533 m_selection_end = { position.row(), end_column }; 534 } 535 GUI::Frame::doubleclick_event(event); 536} 537 538void TerminalWidget::paste() 539{ 540 if (m_ptm_fd == -1) 541 return; 542 auto text = GUI::Clipboard::the().data(); 543 if (text.is_empty()) 544 return; 545 int nwritten = write(m_ptm_fd, text.characters(), text.length()); 546 if (nwritten < 0) { 547 perror("write"); 548 ASSERT_NOT_REACHED(); 549 } 550} 551 552void TerminalWidget::copy() 553{ 554 if (has_selection()) 555 GUI::Clipboard::the().set_data(selected_text()); 556} 557 558void TerminalWidget::mousedown_event(GUI::MouseEvent& event) 559{ 560 if (event.button() == GUI::MouseButton::Left) { 561 if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) { 562 int start_column = 0; 563 int end_column = m_terminal.columns() - 1; 564 565 auto position = buffer_position_at(event.position()); 566 m_selection_start = { position.row(), start_column }; 567 m_selection_end = { position.row(), end_column }; 568 } else { 569 m_selection_start = buffer_position_at(event.position()); 570 m_selection_end = {}; 571 } 572 update(); 573 } 574} 575 576void TerminalWidget::mousemove_event(GUI::MouseEvent& event) 577{ 578 if (!(event.buttons() & GUI::MouseButton::Left)) 579 return; 580 581 auto old_selection_end = m_selection_end; 582 m_selection_end = buffer_position_at(event.position()); 583 if (old_selection_end != m_selection_end) 584 update(); 585} 586 587void TerminalWidget::mousewheel_event(GUI::MouseEvent& event) 588{ 589 if (!is_scrollable()) 590 return; 591 m_scrollbar->set_value(m_scrollbar->value() + event.wheel_delta()); 592 GUI::Frame::mousewheel_event(event); 593} 594 595bool TerminalWidget::is_scrollable() const 596{ 597 return m_scrollbar->is_scrollable(); 598} 599 600String TerminalWidget::selected_text() const 601{ 602 StringBuilder builder; 603 auto start = normalized_selection_start(); 604 auto end = normalized_selection_end(); 605 606 for (int row = start.row(); row <= end.row(); ++row) { 607 int first_column = first_selection_column_on_row(row); 608 int last_column = last_selection_column_on_row(row); 609 for (int column = first_column; column <= last_column; ++column) { 610 auto& line = m_terminal.line(row); 611 if (line.attributes[column].is_untouched()) { 612 builder.append('\n'); 613 break; 614 } 615 builder.append(line.characters[column]); 616 if (column == line.m_length - 1) { 617 builder.append('\n'); 618 } 619 } 620 } 621 622 return builder.to_string(); 623} 624 625int TerminalWidget::first_selection_column_on_row(int row) const 626{ 627 return row == normalized_selection_start().row() ? normalized_selection_start().column() : 0; 628} 629 630int TerminalWidget::last_selection_column_on_row(int row) const 631{ 632 return row == normalized_selection_end().row() ? normalized_selection_end().column() : m_terminal.columns() - 1; 633} 634 635void TerminalWidget::terminal_history_changed() 636{ 637 bool was_max = m_scrollbar->value() == m_scrollbar->max(); 638 m_scrollbar->set_max(m_terminal.history().size()); 639 if (was_max) 640 m_scrollbar->set_value(m_scrollbar->max()); 641 m_scrollbar->update(); 642} 643 644void TerminalWidget::terminal_did_resize(u16 columns, u16 rows) 645{ 646 m_pixel_width = (frame_thickness() * 2) + (m_inset * 2) + (columns * font().glyph_width('x')) + m_scrollbar->width(); 647 m_pixel_height = (frame_thickness() * 2) + (m_inset * 2) + (rows * (font().glyph_height() + m_line_spacing)); 648 649 if (m_automatic_size_policy) { 650 set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); 651 set_preferred_size(m_pixel_width, m_pixel_height); 652 } 653 654 m_needs_background_fill = true; 655 force_repaint(); 656 657 winsize ws; 658 ws.ws_row = rows; 659 ws.ws_col = columns; 660 if (m_ptm_fd != -1) { 661 int rc = ioctl(m_ptm_fd, TIOCSWINSZ, &ws); 662 ASSERT(rc == 0); 663 } 664} 665 666void TerminalWidget::beep() 667{ 668 if (m_should_beep) { 669 sysbeep(); 670 return; 671 } 672 m_visual_beep_timer->restart(200); 673 m_visual_beep_timer->set_single_shot(true); 674 m_visual_beep_timer->on_timeout = [this] { 675 force_repaint(); 676 }; 677 force_repaint(); 678} 679 680void TerminalWidget::emit_char(u8 ch) 681{ 682 if (write(m_ptm_fd, &ch, 1) < 0) { 683 perror("emit_char: write"); 684 } 685} 686 687void TerminalWidget::context_menu_event(GUI::ContextMenuEvent& event) 688{ 689 if (!m_context_menu) { 690 m_context_menu = GUI::Menu::construct(); 691 m_context_menu->add_action(copy_action()); 692 m_context_menu->add_action(paste_action()); 693 } 694 m_context_menu->popup(event.screen_position()); 695} 696 697void TerminalWidget::drop_event(GUI::DropEvent& event) 698{ 699 if (event.mime_data().has_text()) { 700 event.accept(); 701 auto text = event.mime_data().text(); 702 write(m_ptm_fd, text.characters(), text.length()); 703 } else if (event.mime_data().has_urls()) { 704 event.accept(); 705 auto urls = event.mime_data().urls(); 706 bool first = true; 707 for (auto& url : event.mime_data().urls()) { 708 if (!first) { 709 write(m_ptm_fd, " ", 1); 710 first = false; 711 } 712 if (url.protocol() == "file") 713 write(m_ptm_fd, url.path().characters(), url.path().length()); 714 else 715 write(m_ptm_fd, url.to_string().characters(), url.to_string().length()); 716 } 717 } 718} 719 720void TerminalWidget::did_change_font() 721{ 722 GUI::Frame::did_change_font(); 723 m_line_height = font().glyph_height() + m_line_spacing; 724 725 // TODO: try to find a bold version of the new font (e.g. CsillaThin7x10 -> CsillaBold7x10) 726 const Gfx::Font& bold_font = Gfx::Font::default_bold_fixed_width_font(); 727 728 if (bold_font.glyph_height() == font().glyph_height() && bold_font.glyph_width(' ') == font().glyph_width(' ')) 729 m_bold_font = &bold_font; 730 else 731 m_bold_font = font(); 732 733 if (!size().is_empty()) 734 relayout(size()); 735}