From f012b908b77260b89267f1269542923a58539647 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 9 Aug 2023 14:17:03 -0700 Subject: [PATCH] apprt/gtk: use glfw for reading primary clipboard (see comment) --- src/apprt/gtk.zig | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/apprt/gtk.zig b/src/apprt/gtk.zig index acfb6bdc9..e1f7be92f 100644 --- a/src/apprt/gtk.zig +++ b/src/apprt/gtk.zig @@ -15,6 +15,9 @@ pub const c = @cImport({ @cInclude("gtk/gtk.h"); }); +// We need native X11 access to access the primary clipboard. +const glfw_native = glfw.Native(.{ .x11 = true }); + /// Compatibility with gobject < 2.74 const G_CONNECT_DEFAULT = if (@hasDecl(c, "G_CONNECT_DEFAULT")) c.G_CONNECT_DEFAULT @@ -920,9 +923,18 @@ pub const Surface = struct { const clipboard = getClipboard(@ptrCast(self.gl_area), clipboard_type); const content = c.gdk_clipboard_get_content(clipboard) orelse { // On my machine, this NEVER works, so we fallback to glfw's - // implementation... + // implementation... I believe this never works because we need to + // use the async mechanism with GTK but that doesn't play nice + // with what our core expects. log.debug("no GTK clipboard contents, falling back to glfw", .{}); - return glfw.getClipboardString() orelse return glfw.mustGetErrorCode(); + return switch (clipboard_type) { + .standard => glfw.getClipboardString() orelse glfw.mustGetErrorCode(), + .selection => value: { + const raw = glfw_native.getX11SelectionString() orelse + return glfw.mustGetErrorCode(); + break :value std.mem.span(raw); + }, + }; }; c.g_value_unset(&self.clipboard); @@ -940,7 +952,6 @@ pub const Surface = struct { val: [:0]const u8, clipboard_type: apprt.Clipboard, ) !void { - log.warn("SETTING CLIPBOARD: {s}", .{val}); const clipboard = getClipboard(@ptrCast(self.gl_area), clipboard_type); c.gdk_clipboard_set_text(clipboard, val.ptr); }