From 07687e2cd856c7ec56f2e8552ae38f4d009f4c78 Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Sat, 10 Feb 2024 21:09:31 +1300 Subject: [PATCH] Return terminal responses from write method Updates the `write()` method to return a String containing any responses from the terminal. For example, these could be responses to a cursor position request such as "\u001b[6n". --- addons/godot_xterm/native/src/terminal.cpp | 19 +++++++++++++------ addons/godot_xterm/native/src/terminal.h | 4 +++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/addons/godot_xterm/native/src/terminal.cpp b/addons/godot_xterm/native/src/terminal.cpp index 007388d..0a9e5d2 100644 --- a/addons/godot_xterm/native/src/terminal.cpp +++ b/addons/godot_xterm/native/src/terminal.cpp @@ -120,7 +120,7 @@ int Terminal::get_max_scrollback() const return max_scrollback; } -void Terminal::write(Variant data) +String Terminal::write(const Variant data) { PackedByteArray bytes; @@ -133,16 +133,16 @@ void Terminal::write(Variant data) bytes = data; break; default: - ERR_FAIL_MSG("Data must be a String or PackedByteArray."); - return; + ERR_FAIL_V_MSG("", "Data must be a String or PackedByteArray."); } - if (bytes.is_empty()) - return; + if (bytes.is_empty()) return ""; + response.clear(); tsm_vte_input(vte, (char *)bytes.ptr(), bytes.size()); - queue_redraw(); + + return response.get_string_from_utf8(); } void Terminal::_notification(int what) @@ -176,6 +176,13 @@ void Terminal::_notification(int what) void Terminal::_write_cb(tsm_vte *vte, const char *u8, size_t len, void *data) { Terminal *term = static_cast(data); + + if (len > 0) { + size_t old_size = term->response.size(); + term->response.resize(old_size + len); + uint8_t *dest = term->response.ptrw() + old_size; + memcpy(dest, u8, len); + } } int Terminal::_draw_cb(struct tsm_screen *con, diff --git a/addons/godot_xterm/native/src/terminal.h b/addons/godot_xterm/native/src/terminal.h index 509c3e9..9787526 100644 --- a/addons/godot_xterm/native/src/terminal.h +++ b/addons/godot_xterm/native/src/terminal.h @@ -71,7 +71,7 @@ namespace godot void set_inverse_mode(const int mode); int get_inverse_mode() const; - void write(Variant data); + String write(const Variant data); protected: static void _bind_methods(); @@ -93,8 +93,10 @@ namespace godot tsm_vte *vte; tsm_age_t framebuffer_age; + PackedByteArray response; static void _write_cb(struct tsm_vte *vte, const char *u8, size_t len, void *data); + static int _draw_cb(struct tsm_screen *con, uint64_t id, const uint32_t *ch, size_t len, unsigned int width, unsigned int posx, unsigned int posy, const struct tsm_screen_attr *attr,