69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package clipboard
|
|
|
|
/*
|
|
#cgo pkg-config: gtk+-3.0
|
|
#include <gtk/gtk.h>
|
|
|
|
typedef struct {
|
|
char *text;
|
|
char **uris;
|
|
int image;
|
|
int initialized;
|
|
} ClipboardSnapshot;
|
|
|
|
static ClipboardSnapshot read_clipboard(void) {
|
|
ClipboardSnapshot result = {0};
|
|
if (!gtk_init_check(NULL, NULL)) return result;
|
|
result.initialized = 1;
|
|
GtkClipboard *clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
|
|
if (!clipboard) { result.initialized = 0; return result; }
|
|
result.text = gtk_clipboard_wait_for_text(clipboard);
|
|
GdkPixbuf *image = gtk_clipboard_wait_for_image(clipboard);
|
|
if (image) {
|
|
result.image = 1;
|
|
g_object_unref(image);
|
|
}
|
|
if (gtk_clipboard_wait_is_uris_available(clipboard)) {
|
|
result.uris = gtk_clipboard_wait_for_uris(clipboard);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static void free_clipboard(ClipboardSnapshot *snapshot) {
|
|
g_free(snapshot->text);
|
|
g_strfreev(snapshot->uris);
|
|
}
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"errors"
|
|
"unsafe"
|
|
)
|
|
|
|
func read_native() (Snapshot, error) {
|
|
native := C.read_clipboard()
|
|
defer C.free_clipboard(&native)
|
|
if native.initialized == 0 {
|
|
return Snapshot{}, errors.New("cannot access the desktop clipboard")
|
|
}
|
|
snapshot := Snapshot{Image: native.image != 0, HasText: native.text != nil}
|
|
if native.text != nil {
|
|
snapshot.Text = C.GoString(native.text)
|
|
}
|
|
if native.uris != nil {
|
|
for offset := uintptr(0); ; offset += unsafe.Sizeof(uintptr(0)) {
|
|
uri := *(**C.char)(unsafe.Add(unsafe.Pointer(native.uris), offset))
|
|
if uri == nil {
|
|
break
|
|
}
|
|
snapshot.URIs = append(snapshot.URIs, C.GoString(uri))
|
|
}
|
|
}
|
|
return snapshot, nil
|
|
}
|