blob: 21a7e6993a5623143bc6b859a23904129776713a [file] [log] [blame]
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
Daniel Stonec228e232013-05-22 18:03:19 +030023#include "config.h"
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -040024
25#include <stdlib.h>
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -040026#include <string.h>
27#include <linux/input.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <sys/uio.h>
31
32#include "compositor.h"
33
34struct clipboard_source {
35 struct wl_data_source base;
36 struct wl_array contents;
37 struct clipboard *clipboard;
38 struct wl_event_source *event_source;
39 uint32_t serial;
40 int refcount;
41};
42
43struct clipboard {
44 struct weston_seat *seat;
45 struct wl_listener selection_listener;
46 struct wl_listener destroy_listener;
47 struct clipboard_source *source;
48};
49
50static void clipboard_client_create(struct clipboard_source *source, int fd);
51
52static void
53clipboard_source_unref(struct clipboard_source *source)
54{
55 char **s;
56
57 source->refcount--;
58 if (source->refcount > 0)
59 return;
60
61 if (source->event_source)
62 wl_event_source_remove(source->event_source);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -050063 wl_signal_emit(&source->base.destroy_signal,
64 &source->base);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -040065 s = source->base.mime_types.data;
66 free(*s);
67 wl_array_release(&source->base.mime_types);
68 wl_array_release(&source->contents);
69 free(source);
70}
71
72static int
73clipboard_source_data(int fd, uint32_t mask, void *data)
74{
75 struct clipboard_source *source = data;
76 struct clipboard *clipboard = source->clipboard;
77 char *p;
78 int len, size;
79
80 if (source->contents.alloc - source->contents.size < 1024) {
81 wl_array_add(&source->contents, 1024);
82 source->contents.size -= 1024;
83 }
84
85 p = source->contents.data + source->contents.size;
86 size = source->contents.alloc - source->contents.size;
87 len = read(fd, p, size);
88 if (len == 0) {
89 wl_event_source_remove(source->event_source);
90 source->event_source = NULL;
91 } else if (len < 0) {
92 clipboard_source_unref(source);
93 clipboard->source = NULL;
94 } else {
95 source->contents.size += len;
96 }
97
98 return 1;
99}
100
101static void
102clipboard_source_accept(struct wl_data_source *source,
103 uint32_t time, const char *mime_type)
104{
105}
106
107static void
108clipboard_source_send(struct wl_data_source *base,
109 const char *mime_type, int32_t fd)
110{
111 struct clipboard_source *source =
112 container_of(base, struct clipboard_source, base);
113 char **s;
114
115 s = source->base.mime_types.data;
116 if (strcmp(mime_type, s[0]) == 0)
117 clipboard_client_create(source, fd);
118 else
119 close(fd);
120}
121
122static void
123clipboard_source_cancel(struct wl_data_source *source)
124{
125}
126
127static struct clipboard_source *
128clipboard_source_create(struct clipboard *clipboard,
129 const char *mime_type, uint32_t serial, int fd)
130{
131 struct wl_display *display = clipboard->seat->compositor->wl_display;
132 struct wl_event_loop *loop = wl_display_get_event_loop(display);
133 struct clipboard_source *source;
134 char **s;
135
136 source = malloc(sizeof *source);
137 wl_array_init(&source->contents);
138 wl_array_init(&source->base.mime_types);
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500139 source->base.resource = NULL;
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400140 source->base.accept = clipboard_source_accept;
141 source->base.send = clipboard_source_send;
142 source->base.cancel = clipboard_source_cancel;
Jason Ekstrand8a4a9eb2013-06-14 10:07:55 -0500143 wl_signal_init(&source->base.destroy_signal);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400144 source->refcount = 1;
145 source->clipboard = clipboard;
146 source->serial = serial;
147
148 s = wl_array_add(&source->base.mime_types, sizeof *s);
149 *s = strdup(mime_type);
150
151 source->event_source =
152 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
153 clipboard_source_data, source);
154
155 return source;
156}
157
158struct clipboard_client {
159 struct wl_event_source *event_source;
160 size_t offset;
161 struct clipboard_source *source;
162};
163
164static int
165clipboard_client_data(int fd, uint32_t mask, void *data)
166{
167 struct clipboard_client *client = data;
168 char *p;
169 size_t size;
170 int len;
171
172 size = client->source->contents.size;
173 p = client->source->contents.data;
174 len = write(fd, p + client->offset, size - client->offset);
175 if (len > 0)
176 client->offset += len;
177
178 if (client->offset == size || len <= 0) {
179 close(fd);
180 wl_event_source_remove(client->event_source);
181 clipboard_source_unref(client->source);
182 free(client);
183 }
184
185 return 1;
186}
187
188static void
189clipboard_client_create(struct clipboard_source *source, int fd)
190{
191 struct weston_seat *seat = source->clipboard->seat;
192 struct clipboard_client *client;
193 struct wl_event_loop *loop =
194 wl_display_get_event_loop(seat->compositor->wl_display);
195
196 client = malloc(sizeof *client);
197
198 client->offset = 0;
199 client->source = source;
200 source->refcount++;
201 client->event_source =
202 wl_event_loop_add_fd(loop, fd, WL_EVENT_WRITABLE,
203 clipboard_client_data, client);
204}
205
206static void
207clipboard_set_selection(struct wl_listener *listener, void *data)
208{
209 struct clipboard *clipboard =
210 container_of(listener, struct clipboard, selection_listener);
211 struct weston_seat *seat = data;
Kristian Høgsberge3148752013-05-06 23:19:49 -0400212 struct wl_data_source *source = seat->selection_data_source;
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400213 const char **mime_types;
214 int p[2];
215
216 if (source == NULL) {
217 if (clipboard->source)
Kristian Høgsberge3148752013-05-06 23:19:49 -0400218 weston_seat_set_selection(seat,
219 &clipboard->source->base,
220 clipboard->source->serial);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400221 return;
222 } else if (source->accept == clipboard_source_accept) {
223 /* Callback for our data source. */
224 return;
225 }
226
227 if (clipboard->source)
228 clipboard_source_unref(clipboard->source);
229
230 clipboard->source = NULL;
231
232 mime_types = source->mime_types.data;
233
234 if (pipe2(p, O_CLOEXEC) == -1)
235 return;
236
237 source->send(source, mime_types[0], p[1]);
238
239 clipboard->source =
240 clipboard_source_create(clipboard, mime_types[0],
Kristian Høgsberge3148752013-05-06 23:19:49 -0400241 seat->selection_serial, p[0]);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400242 if (clipboard->source == NULL)
243 return;
244}
245
Rafal Mielniczuk96ddcb22012-07-11 18:48:12 +0200246static void
247clipboard_destroy(struct wl_listener *listener, void *data)
248{
249 struct clipboard *clipboard =
250 container_of(listener, struct clipboard, destroy_listener);
251
252 wl_list_remove(&clipboard->selection_listener.link);
253
254 free(clipboard);
255}
256
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400257struct clipboard *
258clipboard_create(struct weston_seat *seat)
259{
260 struct clipboard *clipboard;
261
262 clipboard = malloc(sizeof *clipboard);
263 if (clipboard == NULL)
264 return NULL;
Rafal Mielniczuk340a4342012-06-30 18:33:34 +0200265 memset(clipboard, 0, sizeof *clipboard);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400266
267 clipboard->seat = seat;
268 clipboard->selection_listener.notify = clipboard_set_selection;
Rafal Mielniczuk96ddcb22012-07-11 18:48:12 +0200269 clipboard->destroy_listener.notify = clipboard_destroy;
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400270
Kristian Høgsberge3148752013-05-06 23:19:49 -0400271 wl_signal_add(&seat->selection_signal,
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400272 &clipboard->selection_listener);
Kristian Høgsberg49124542013-05-06 22:27:40 -0400273 wl_signal_add(&seat->destroy_signal,
Rafal Mielniczuk96ddcb22012-07-11 18:48:12 +0200274 &clipboard->destroy_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -0400275
276 return clipboard;
277}