-
Notifications
You must be signed in to change notification settings - Fork 39
/
clipboard.c
90 lines (73 loc) · 2.21 KB
/
clipboard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "clipboard.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "pixbuf.h"
#include "util.h"
#define gtk_clipboard_t GtkClipboard
#define gdk_pixbuf_t GdkPixbuf
static gboolean send_pixbuf_to_wl_copy(gdk_pixbuf_t *pixbuf) {
pid_t clipboard_process = 0;
int pipefd[2];
int status;
ssize_t written;
gsize size;
gchar *buffer = NULL;
GError *error = NULL;
if (pipe(pipefd) < 0) {
g_warning("unable to pipe for copy process to work");
return false;
}
clipboard_process = fork();
if (clipboard_process == -1) {
g_warning("unable to fork process for copy");
return false;
}
if (clipboard_process == 0) {
close(pipefd[1]);
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]);
execlp("wl-copy", "wl-copy", "-t", "image/png", NULL);
g_warning(
"Unable to copy contents to clipboard. Please make sure you have "
"`wl-clipboard`, `xclip`, or `xsel` installed.");
exit(1);
}
close(pipefd[0]);
gdk_pixbuf_save_to_buffer(pixbuf, &buffer, &size, "png", &error, NULL);
if (error != NULL) {
g_critical("unable to save pixbuf to buffer for copy: %s", error->message);
g_error_free(error);
return false;
}
written = write(pipefd[1], buffer, size);
if (written == -1) {
g_warning("unable to write to pipe fd for copy");
g_free(buffer);
return false;
}
close(pipefd[1]);
g_free(buffer);
waitpid(clipboard_process, &status, 0);
if (WIFEXITED(status)) {
return WEXITSTATUS(status) == 0; // Make sure the child exited properly
}
return false;
}
static void send_pixbuf_to_gdk_clipboard(gdk_pixbuf_t *pixbuf) {
gtk_clipboard_t *clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
gtk_clipboard_set_image(clipboard, pixbuf);
gtk_clipboard_store(clipboard); // Does not work for Wayland gdk backend
}
bool clipboard_copy_drawing_area_to_selection(struct swappy_state *state) {
gdk_pixbuf_t *pixbuf = pixbuf_get_from_state(state);
// Try `wl-copy` first and fall back to gtk function. See README.md.
if (!send_pixbuf_to_wl_copy(pixbuf)) {
send_pixbuf_to_gdk_clipboard(pixbuf);
}
g_object_unref(pixbuf);
if (state->config->early_exit) {
gtk_main_quit();
}
return true;
}