-
Notifications
You must be signed in to change notification settings - Fork 1
/
waycopy.c
120 lines (91 loc) · 2.8 KB
/
waycopy.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#define _POSIX_C_SOURCE 200809L
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client.h>
#include <unistd.h>
#include "protocol/wlr-data-control-unstable-v1-client-protocol.h"
#include "common.h"
const char *usagestr = "usage: %s [-f] [-p] [-s seat] [-t mimetype]\n";
struct wl_registry *registry;
int temp;
bool running = true;
void
data_source_send(void *data, struct zwlr_data_control_source_v1 *source, const char *mime_type, int32_t fd)
{
lseek(temp, SEEK_SET, 0);
copyfd(fd, temp);
close(fd);
}
void
data_source_cancelled(void *data, struct zwlr_data_control_source_v1 *source)
{
running = 0;
}
static const struct zwlr_data_control_source_v1_listener data_source_listener = {
.send = data_source_send,
.cancelled = data_source_cancelled,
};
const char *const tempname = "/waycopy-buffer-XXXXXX";
int
main(int argc, char *argv[])
{
argv0 = argv[0];
parseopts("hfps:t:", argc, argv);
char path[PATH_MAX] = {0};
char *ptr = getenv("TMPDIR");
if (ptr == NULL)
strcpy(path, "/tmp");
else {
if (strlen(ptr) > PATH_MAX - strlen(tempname))
die("TMPDIR has too long of a path");
strcpy(path, ptr);
}
strncat(path, tempname, PATH_MAX - 1);
temp = mkstemp(path);
if (temp == -1)
die("failed to create temporary file for copy buffer");
if (unlink(path) == -1)
die("failed to remove temporary file");
copyfd(temp, STDIN_FILENO);
close(STDIN_FILENO);
struct wl_display *const display = wl_display_connect(NULL);
if (display == NULL)
die("failed to connect to display");
registry = wl_display_get_registry(display);
if (registry == NULL)
die("failed to get registry");
wl_registry_add_listener(registry, ®istry_listener, NULL);
wl_display_roundtrip(display);
if (options.seat)
wl_display_roundtrip(display);
if (seat == NULL)
die("failed to bind to seat interface");
if (data_control_manager == NULL)
die("failed to bind to data_control_manager interface");
struct zwlr_data_control_device_v1 *device = zwlr_data_control_manager_v1_get_data_device(data_control_manager, seat);
if (device == NULL)
die("data device is null");
struct zwlr_data_control_source_v1 *source = zwlr_data_control_manager_v1_create_data_source(data_control_manager);
if (source == NULL)
die("source is null");
zwlr_data_control_source_v1_offer(source, options.type);
zwlr_data_control_source_v1_add_listener(source, &data_source_listener, NULL);
if (options.primary)
zwlr_data_control_device_v1_set_primary_selection(device, source);
else
zwlr_data_control_device_v1_set_selection(device, source);
if (!options.foreground) {
pid_t pid = fork();
if (pid == -1)
die("failed to fork");
if (pid != 0) {
close(STDERR_FILENO);
return 0;
}
}
while (wl_display_dispatch(display) != -1 && running);
return running;
}