diff --git a/addons/godot_xterm/native/src/pipe.cpp b/addons/godot_xterm/native/src/pipe.cpp index 89e36b4..17e90f0 100644 --- a/addons/godot_xterm/native/src/pipe.cpp +++ b/addons/godot_xterm/native/src/pipe.cpp @@ -63,9 +63,10 @@ void Pipe::close() { uv_run(uv_default_loop(), UV_RUN_NOWAIT); } -godot_error Pipe::write(String p_data) { - char *s = p_data.alloc_c_string(); - ULONG len = strlen(s); +godot_error Pipe::write(PoolByteArray p_data) { + ULONG len = p_data.size(); + char *s = (char *)malloc(len); + memcpy(s, p_data.read().ptr(), len); uv_buf_t bufs[1]; bufs[0].base = s; @@ -113,7 +114,7 @@ void _read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) { pipe->emit_signal("data_received", data); } -void _write_cb(uv_write_t *req, int status) {} +void _write_cb(uv_write_t *req, int status) { std::free(req->data); } void _alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { buf->base = (char *)malloc(suggested_size); diff --git a/addons/godot_xterm/native/src/pipe.h b/addons/godot_xterm/native/src/pipe.h index 0b7c09a..590bb45 100644 --- a/addons/godot_xterm/native/src/pipe.h +++ b/addons/godot_xterm/native/src/pipe.h @@ -26,7 +26,7 @@ public: void close(); int get_status(); - godot_error write(String p_data); + godot_error write(PoolByteArray p_data); void pause(); void resume(); diff --git a/addons/godot_xterm/nodes/pty/pty.gd b/addons/godot_xterm/nodes/pty/pty.gd index e3f414e..b5f6646 100644 --- a/addons/godot_xterm/nodes/pty/pty.gd +++ b/addons/godot_xterm/nodes/pty/pty.gd @@ -174,7 +174,9 @@ func write(data) -> void: _write(data) -func _write(data: String) -> void: +func _write(data) -> void: + assert(data is PoolByteArray or data is String) + data = data if data is PoolByteArray else data.to_utf8() if _pipe: _pipe.write(data) diff --git a/addons/godot_xterm/nodes/terminal/terminal.gd b/addons/godot_xterm/nodes/terminal/terminal.gd index f42d9c1..c0ecbb6 100644 --- a/addons/godot_xterm/nodes/terminal/terminal.gd +++ b/addons/godot_xterm/nodes/terminal/terminal.gd @@ -29,8 +29,12 @@ export (UpdateMode) var update_mode = UpdateMode.AUTO setget set_update_mode var cols = 2 var rows = 2 +# Mouse + # If true, text in the terminal will be copied to the clipboard when selected. export (bool) var copy_on_selection +# If true mouse events will be emitted by the signal when clicking on the Terminal. +export (bool) var mouse_tracking := true # Bell # If muted, the "bell" signal will not be emitted when the bell "\u0007" character @@ -160,10 +164,34 @@ func _refresh(): func _gui_input(event): - _native_terminal._gui_input(event) + #_native_terminal._gui_input(event) if event is InputEventKey: - _native_terminal.sb_reset() # Return to bottom of scrollback buffer if we scrolled up. + pass + _native_terminal._gui_input(event) + #_native_terminal.sb_reset() # Return to bottom of scrollback buffer if we scrolled up. + + # Mouse tracking. + # Reference: https://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking + if mouse_tracking: + if event is InputEventMouseButton: + var b: int + if event.pressed: + if event.button_index == BUTTON_LEFT: + b = 0 + if event.button_index == BUTTON_RIGHT: + b = 1 + if event.button_index == BUTTON_MIDDLE: + b = 2 + else: + b = 3 + + # Xterm coords start at 1,1 for the top/leftmost cell. + var position: Vector2 = _mouse_to_cell(get_local_mouse_position()) + Vector2.ONE + var e = "\u001b[M".to_ascii() + print(_native_terminal.cell_size) + e = PoolByteArray([0x1b, 0x5b, 0x4d, b + 32, position.x + 32, position.y + 32]) + emit_signal("data_sent", e) _handle_mouse_wheel(event) _handle_selection(event)