From e471228f6b508218cb55afb4d7965e02a44f8bb0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 3 Oct 2023 22:40:56 -0700 Subject: [PATCH 1/4] macos: surfaceview supports dropping file URLs, just logs for now --- macos/Sources/Ghostty/SurfaceView.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index de8a434a4..77f63c7e3 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -129,6 +129,16 @@ extension Ghostty { // I don't know how older macOS versions behave but Ghostty only // supports back to macOS 12 so its moot. } + .onDrop(of: [.fileURL], isTargeted: nil) { providers in + providers.forEach { provider in + _ = provider.loadObject(ofClass: URL.self) { url, _ in + guard let url = url else { return } + AppDelegate.logger.warning("OPEN url=\(url.path)") + } + } + + return true + } } .ghosttySurfaceView(surfaceView) From 3046fb6dd7f32a8c15540f2f58c269d5a0eeaa67 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 4 Oct 2023 07:50:05 -0700 Subject: [PATCH 2/4] core: textCallback to send text directly to the terminal --- src/Surface.zig | 9 +++++++++ src/apprt/embedded.zig | 14 +------------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 70f4d9405..872e8fe1d 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -1020,6 +1020,15 @@ pub fn keyCallback( return true; } +/// Sends text as-is to the terminal without triggering any keyboard +/// protocol. This will treat the input text as if it was pasted +/// from the clipboard so the same logic will be applied. Namely, +/// if bracketed mode is on this will do a bracketed paste. Otherwise, +/// this will filter newlines to '\r'. +pub fn textCallback(self: *Surface, text: []const u8) !void { + try self.completeClipboardPaste(text); +} + pub fn focusCallback(self: *Surface, focused: bool) !void { // Notify our render thread of the new state _ = self.renderer_thread.mailbox.push(.{ diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index bffc8a95b..28d54286e 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -686,19 +686,7 @@ pub const Surface = struct { return; }; - // For a char callback we just construct a key event with invalid - // keys but with text. This should result in the text being sent - // as-is. - _ = self.core_surface.keyCallback(.{ - .action = .press, - .key = .invalid, - .physical_key = .invalid, - .mods = .{}, - .consumed_mods = .{}, - .composing = false, - .utf8 = buf[0..len], - .unshifted_codepoint = 0, - }) catch |err| { + _ = self.core_surface.textCallback(buf[0..len]) catch |err| { log.err("error in key callback err={}", .{err}); return; }; From ee832aa3614478da516359dd96c9bb2781c94780 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 4 Oct 2023 07:56:56 -0700 Subject: [PATCH 3/4] apprt/embedded: ghostty_surface_text function, remove _char --- include/ghostty.h | 2 +- macos/Sources/Ghostty/SurfaceView.swift | 5 +++-- src/apprt/embedded.zig | 25 +++++++++++-------------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index 826e919af..5d85d8d51 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -388,7 +388,7 @@ void ghostty_surface_set_content_scale(ghostty_surface_t, double, double); void ghostty_surface_set_focus(ghostty_surface_t, bool); void ghostty_surface_set_size(ghostty_surface_t, uint32_t, uint32_t); void ghostty_surface_key(ghostty_surface_t, ghostty_input_action_e, uint32_t, ghostty_input_mods_e); -void ghostty_surface_char(ghostty_surface_t, uint32_t); +void ghostty_surface_text(ghostty_surface_t, const char *, uintptr_t); void ghostty_surface_mouse_button(ghostty_surface_t, ghostty_input_mouse_state_e, ghostty_input_mouse_button_e, ghostty_input_mods_e); void ghostty_surface_mouse_pos(ghostty_surface_t, double, double); void ghostty_surface_mouse_scroll(ghostty_surface_t, double, double, ghostty_input_scroll_mods_t); diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index 77f63c7e3..d187c9394 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -836,8 +836,9 @@ extension Ghostty { return } - for codepoint in chars.unicodeScalars { - ghostty_surface_char(surface, codepoint.value) + let len = chars.utf8CString.count + chars.withCString { ptr in + ghostty_surface_text(surface, ptr, UInt(len)) } } diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 28d54286e..d8f6bf316 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -678,15 +678,8 @@ pub const Surface = struct { } } - pub fn charCallback(self: *Surface, cp_: u32) void { - const cp = std.math.cast(u21, cp_) orelse return; - var buf: [4]u8 = undefined; - const len = std.unicode.utf8Encode(cp, &buf) catch |err| { - log.err("error encoding codepoint={} err={}", .{ cp, err }); - return; - }; - - _ = self.core_surface.textCallback(buf[0..len]) catch |err| { + pub fn textCallback(self: *Surface, text: []const u8) void { + _ = self.core_surface.textCallback(text) catch |err| { log.err("error in key callback err={}", .{err}); return; }; @@ -946,11 +939,15 @@ pub const CAPI = struct { }; } - /// Send for a unicode character. This is used for IME input. This - /// should only be sent for characters that are not the result of - /// key events. - export fn ghostty_surface_char(surface: *Surface, codepoint: u32) void { - surface.charCallback(codepoint); + /// Send raw text to the terminal. This is treated like a paste + /// so this isn't useful for sending escape sequences. For that, + /// individual key input should be used. + export fn ghostty_surface_text( + surface: *Surface, + ptr: [*]const u8, + len: usize, + ) void { + surface.textCallback(ptr[0..len]); } /// Tell the surface that it needs to schedule a render From 2aacf6e130ea4335290470c91c5530b85542b2dd Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 4 Oct 2023 08:01:13 -0700 Subject: [PATCH 4/4] macos: support dropping file urls onto surfaces --- macos/Sources/Ghostty/SurfaceView.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index d187c9394..7a5bbb081 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -133,7 +133,12 @@ extension Ghostty { providers.forEach { provider in _ = provider.loadObject(ofClass: URL.self) { url, _ in guard let url = url else { return } - AppDelegate.logger.warning("OPEN url=\(url.path)") + DispatchQueue.main.async { + surfaceView.insertText( + url.path, + replacementRange: NSMakeRange(0, 0) + ) + } } }