From 7f239310281e84057e66d866cae95d7b8ec43ac7 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 16 Sep 2023 13:29:22 +0200 Subject: [PATCH] terminal: Draw directly onto the window canvas --- terminal/TerminalWidget.cpp | 13 +++++-------- terminal/TerminalWidget.h | 2 +- terminal/main.cpp | 5 ++--- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/terminal/TerminalWidget.cpp b/terminal/TerminalWidget.cpp index 9709115b..e87b8e4d 100644 --- a/terminal/TerminalWidget.cpp +++ b/terminal/TerminalWidget.cpp @@ -46,10 +46,8 @@ Result TerminalWidget::init(char* const* args) m_font = ui::Font::default_font(); m_bold_font = ui::Font::default_bold_font(); - m_terminal_canvas = ui::Canvas { .width = m_rect.width, - .height = m_rect.height, - .stride = m_rect.width, - .ptr = (u8*)TRY(calloc_impl(m_rect.width, m_rect.height * sizeof(u32), false)) }; + m_terminal_canvas = ui::App::the().main_window()->canvas(); + m_terminal_canvas.fill(ui::BLACK); signal(SIGCHLD, sigchld_handler); @@ -213,13 +211,12 @@ Result TerminalWidget::handle_key_event(const ui::KeyEventReque return ui::EventResult::DidHandle; } -Result TerminalWidget::draw(ui::Canvas& canvas) +Result TerminalWidget::draw(ui::Canvas&) { - canvas.fill((u32*)m_terminal_canvas.ptr, m_terminal_canvas.stride); return {}; } -Result TerminalWidget::process() +Result TerminalWidget::process() { char buffer[BUFSIZ]; ssize_t nread = read(m_pty, buffer, BUFSIZ); @@ -238,7 +235,7 @@ Result TerminalWidget::process() if (should_update_cursor || nread > 0) ui::App::the().main_window()->draw(); - return {}; + return nread == 0; } bool TerminalWidget::tick_cursor() diff --git a/terminal/TerminalWidget.h b/terminal/TerminalWidget.h index d29cb3ad..694761d8 100644 --- a/terminal/TerminalWidget.h +++ b/terminal/TerminalWidget.h @@ -16,7 +16,7 @@ class TerminalWidget : public ui::Widget Result draw(ui::Canvas& canvas) override; - Result process(); + Result process(); void quit(); diff --git a/terminal/main.cpp b/terminal/main.cpp index 96e1d698..48e438b9 100644 --- a/terminal/main.cpp +++ b/terminal/main.cpp @@ -11,7 +11,6 @@ Result luna_main(int argc, char** argv) auto* window = TRY(ui::Window::create(ui::Rect { 150, 150, 640, 400 })); app.set_main_window(window); - window->set_background(ui::BLACK); window->set_title("Terminal"); TerminalWidget terminal; @@ -24,8 +23,8 @@ Result luna_main(int argc, char** argv) while (app.process_events()) { - TRY(terminal.process()); - usleep(10000); + bool should_sleep = TRY(terminal.process()); + if (should_sleep) usleep(10000); } terminal.quit();