Files
pipelines/app/linux/runner/clipboard_files.cc
T
alan 8138cdc3d9 Linux desktop: optimized release build, installer, Linux gaps
Build: split Dart debug info out of libapp.so (13.96 -> 11.27 MB, symbols
kept in the cache) and fix icon tree shaking, which Flutter 3.44's Linux
CMake backend silently disables by quoting the define; the build image
patches its SDK copy (MaterialIcons 1.6 MB -> 25 KB). Impeller cannot be
enabled in a Linux release build and was slower in a profile-build bench.

Install: `./install --linux --install` installs the bundle into
<prefix>/lib/app-linux with com.pipelines.app.desktop (the GTK/Wayland
app id), its icon and a ~/.local/bin/pipelines-app link, through
scripts/install-linux-app.sh.

Linux gaps: notifications through the freedesktop daemon; HTML artifacts
explain the missing web view instead of throwing; no Share for files;
a GTK clipboard reader for pasting files and screenshots (Ctrl+V and
Omarchy's Super+V); GTK header bar only on GNOME; window icon.

Bench: native fixture frame log, wheel/mark steps and
tool/perf/linux_desktop_bench.py (private Xvfb); results in
app/notes/linux-desktop-optimized.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:54:18 +00:00

136 lines
5.3 KiB
C++

#include "clipboard_files.h"
#include <gio/gio.h>
#include <gtk/gtk.h>
namespace {
FlMethodChannel* channel = nullptr;
// The MIME type of [path] from its name and contents, or "".
gchar* mime_of(const gchar* path, const guchar* data, gsize length) {
gboolean uncertain = FALSE;
g_autofree gchar* type =
g_content_type_guess(path, data, MIN(length, (gsize)4096), &uncertain);
gchar* mime = type == nullptr ? nullptr : g_content_type_get_mime_type(type);
return mime == nullptr ? g_strdup("") : mime;
}
FlValue* file_entry(const gchar* name, const gchar* mime, const guchar* data,
gsize length) {
FlValue* entry = fl_value_new_map();
fl_value_set_string_take(entry, "name", fl_value_new_string(name));
fl_value_set_string_take(entry, "mime", fl_value_new_string(mime));
fl_value_set_string_take(entry, "bytes",
fl_value_new_uint8_list(data, length));
return entry;
}
// Adds each copied local file; folders and unreadable files become errors.
gboolean add_uris(GtkClipboard* clipboard, FlValue* files, FlValue* errors) {
g_auto(GStrv) uris = gtk_clipboard_wait_for_uris(clipboard);
if (uris == nullptr || uris[0] == nullptr) return FALSE;
gboolean any = FALSE;
for (gchar** uri = uris; *uri != nullptr; uri++) {
g_autofree gchar* path = g_filename_from_uri(*uri, nullptr, nullptr);
if (path == nullptr) continue;
any = TRUE;
g_autofree gchar* name = g_path_get_basename(path);
if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
g_autofree gchar* message =
g_strdup_printf("%s is a folder; paste its files instead.", name);
fl_value_append_take(errors, fl_value_new_string(message));
continue;
}
g_autofree gchar* contents = nullptr;
gsize length = 0;
g_autoptr(GError) error = nullptr;
if (!g_file_get_contents(path, &contents, &length, &error)) {
g_autofree gchar* message =
g_strdup_printf("Could not read %s: %s", name, error->message);
fl_value_append_take(errors, fl_value_new_string(message));
continue;
}
g_autofree gchar* mime =
mime_of(path, reinterpret_cast<const guchar*>(contents), length);
fl_value_append_take(
files, file_entry(name, mime,
reinterpret_cast<const guchar*>(contents), length));
}
return any;
}
// The clipboard's own PNG (a screenshot tool's), else any image GTK can read
// converted to PNG.
void add_image(GtkClipboard* clipboard, FlValue* files) {
g_autoptr(GtkSelectionData) png = gtk_clipboard_wait_for_contents(
clipboard, gdk_atom_intern_static_string("image/png"));
if (png != nullptr && gtk_selection_data_get_length(png) > 0) {
fl_value_append_take(
files, file_entry("", "image/png", gtk_selection_data_get_data(png),
gtk_selection_data_get_length(png)));
return;
}
g_autoptr(GdkPixbuf) image = gtk_clipboard_wait_for_image(clipboard);
if (image == nullptr) return;
g_autofree gchar* buffer = nullptr;
gsize length = 0;
if (gdk_pixbuf_save_to_buffer(image, &buffer, &length, "png", nullptr,
nullptr)) {
fl_value_append_take(
files, file_entry("", "image/png",
reinterpret_cast<const guchar*>(buffer), length));
}
}
FlValue* read_clipboard() {
GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
FlValue* files = fl_value_new_list();
FlValue* errors = fl_value_new_list();
FlValue* types = fl_value_new_list();
GdkAtom* targets = nullptr;
gint count = 0;
if (gtk_clipboard_wait_for_targets(clipboard, &targets, &count)) {
for (gint i = 0; i < count; i++) {
g_autofree gchar* name = gdk_atom_name(targets[i]);
fl_value_append_take(types, fl_value_new_string(name));
}
g_free(targets);
}
if (!add_uris(clipboard, files, errors)) add_image(clipboard, files);
FlValue* reply = fl_value_new_map();
fl_value_set_string_take(reply, "files", files);
fl_value_set_string_take(reply, "errors", errors);
fl_value_set_string_take(reply, "types", types);
return reply;
}
void method_call_cb(FlMethodChannel* channel, FlMethodCall* call,
gpointer user_data) {
g_autoptr(FlMethodResponse) response = nullptr;
if (g_strcmp0(fl_method_call_get_name(call), "read") == 0) {
g_autoptr(FlValue) reply = read_clipboard();
response = FL_METHOD_RESPONSE(fl_method_success_response_new(reply));
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
}
g_autoptr(GError) error = nullptr;
if (!fl_method_call_respond(call, response, &error)) {
g_warning("Failed to answer a clipboard read: %s", error->message);
}
}
} // namespace
void clipboard_files_register(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "ClipboardFiles");
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
g_clear_object(&channel);
channel = fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
"pipelines/clipboard_files",
FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(channel, method_call_cb, nullptr,
nullptr);
}