f0f6baa4540d9ec994f1fdac500e39ed8dee556b
[profile/ivi/wayland.git] / src / data-device.c
1 /*
2  * Copyright © 2011 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <stdio.h>
27
28 #include "wayland-server.h"
29
30 static void
31 data_offer_accept(struct wl_client *client, struct wl_resource *resource,
32                   uint32_t time, const char *mime_type)
33 {
34         struct wl_data_offer *offer = resource->data;
35
36         /* FIXME: Check that client is currently focused by the input
37          * device that is currently dragging this data source.  Should
38          * this be a wl_data_device request? */
39
40         if (offer->source)
41                 wl_resource_post_event(&offer->source->resource,
42                                        WL_DATA_SOURCE_TARGET, mime_type);
43 }
44
45 static void
46 data_offer_receive(struct wl_client *client, struct wl_resource *resource,
47                    const char *mime_type, int32_t fd)
48 {
49         struct wl_data_offer *offer = resource->data;
50
51         if (offer->source)
52                 wl_resource_post_event(&offer->source->resource,
53                                        WL_DATA_SOURCE_SEND, mime_type, fd);
54
55         close(fd);
56 }
57
58 static void
59 data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
60 {
61         wl_resource_destroy(resource, 0);
62 }
63
64 static void
65 destroy_data_offer(struct wl_resource *resource)
66 {
67         struct wl_data_offer *offer = resource->data;
68
69         wl_list_remove(&offer->source_destroy_listener.link);
70         free(offer);
71 }
72
73 static const struct wl_data_offer_interface data_offer_interface = {
74         data_offer_accept,
75         data_offer_receive,
76         data_offer_destroy,
77 };
78
79 static void
80 destroy_offer_data_source(struct wl_listener *listener,
81                           struct wl_resource *resource, uint32_t time)
82 {
83         struct wl_data_offer *offer;
84
85         offer = container_of(listener, struct wl_data_offer,
86                              source_destroy_listener);
87
88         offer->source = NULL;
89 }
90
91 static void
92 data_source_cancel(struct wl_data_source *source)
93 {
94         wl_resource_post_event(&source->resource, WL_DATA_SOURCE_CANCELLED);
95 }
96
97 static struct wl_resource *
98 wl_data_source_send_offer(struct wl_data_source *source,
99                           struct wl_resource *target)
100 {
101         struct wl_data_offer *offer;
102         char **p, **end;
103
104         offer = malloc(sizeof *offer);
105         if (offer == NULL)
106                 return NULL;
107
108         offer->resource.destroy = destroy_data_offer;
109         offer->resource.object.id = 0;
110         offer->resource.object.interface = &wl_data_offer_interface;
111         offer->resource.object.implementation =
112                 (void (**)(void)) source->offer_interface;
113         offer->resource.data = offer;
114         wl_list_init(&offer->resource.destroy_listener_list);
115
116         offer->source = source;
117         offer->source_destroy_listener.func = destroy_offer_data_source;
118         wl_list_insert(&source->resource.destroy_listener_list,
119                        &offer->source_destroy_listener.link);
120
121         wl_client_add_resource(target->client, &offer->resource);
122
123         wl_resource_post_event(target,
124                                WL_DATA_DEVICE_DATA_OFFER, &offer->resource);
125
126         end = source->mime_types.data + source->mime_types.size;
127         for (p = source->mime_types.data; p < end; p++)
128                 wl_resource_post_event(&offer->resource,
129                                        WL_DATA_OFFER_OFFER, *p);
130
131         return &offer->resource;
132 }
133
134 static void
135 data_source_offer(struct wl_client *client,
136                   struct wl_resource *resource,
137                   const char *type)
138 {
139         struct wl_data_source *source = resource->data;
140         char **p;
141
142         p = wl_array_add(&source->mime_types, sizeof *p);
143         if (p)
144                 *p = strdup(type);
145         if (!p || !*p)
146                 wl_resource_post_no_memory(resource);
147 }
148
149 static void
150 data_source_destroy(struct wl_client *client, struct wl_resource *resource)
151 {
152         wl_resource_destroy(resource, 0);
153 }
154
155 static struct wl_data_source_interface data_source_interface = {
156         data_source_offer,
157         data_source_destroy
158 };
159
160 static struct wl_resource *
161 find_resource(struct wl_list *list, struct wl_client *client)
162 {
163         struct wl_resource *r;
164
165         wl_list_for_each(r, list, link) {
166                 if (r->client == client)
167                         return r;
168         }
169
170         return NULL;
171 }
172
173 static void
174 destroy_drag_focus(struct wl_listener *listener,
175                    struct wl_resource *resource, uint32_t time)
176 {
177         struct wl_input_device *device =
178                 container_of(listener, struct wl_input_device,
179                              drag_focus_listener);
180
181         device->drag_focus_resource = NULL;
182 }
183
184 static void
185 drag_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
186                 struct wl_surface *surface, int32_t x, int32_t y)
187 {
188         struct wl_input_device *device =
189                 container_of(grab, struct wl_input_device, drag_grab);
190         struct wl_resource *resource, *offer;
191
192         if (device->drag_focus_resource) {
193                 wl_resource_post_event(device->drag_focus_resource,
194                                        WL_DATA_DEVICE_LEAVE);
195                 wl_list_remove(&device->drag_focus_listener.link);
196                 device->drag_focus_resource = NULL;
197                 device->drag_focus = NULL;
198         }
199
200         if (surface)
201                 resource = find_resource(&device->drag_resource_list, 
202                                          surface->resource.client);
203         if (surface && resource) {
204                 offer = wl_data_source_send_offer(device->drag_data_source,
205                                                   resource);
206
207                 wl_resource_post_event(resource,
208                                        WL_DATA_DEVICE_ENTER,
209                                        time, surface, x, y, offer);
210
211                 device->drag_focus = surface;
212                 device->drag_focus_listener.func = destroy_drag_focus;
213                 wl_list_insert(resource->destroy_listener_list.prev,
214                                &device->drag_focus_listener.link);
215                 device->drag_focus_resource = resource;
216                 grab->focus = surface;
217         }
218 }
219
220 static void
221 drag_grab_motion(struct wl_pointer_grab *grab,
222                  uint32_t time, int32_t x, int32_t y)
223 {
224         struct wl_input_device *device =
225                 container_of(grab, struct wl_input_device, drag_grab);
226
227         if (device->drag_focus_resource)
228                 wl_resource_post_event(device->drag_focus_resource,
229                                        WL_DATA_DEVICE_MOTION, time, x, y);
230 }
231
232 static void
233 drag_grab_button(struct wl_pointer_grab *grab,
234                  uint32_t time, int32_t button, int32_t state)
235 {
236         struct wl_input_device *device =
237                 container_of(grab, struct wl_input_device, drag_grab);
238
239         if (device->drag_focus_resource &&
240             device->grab_button == button && state == 0)
241                 wl_resource_post_event(device->drag_focus_resource,
242                                        WL_DATA_DEVICE_DROP);
243
244         if (device->button_count == 0 && state == 0) {
245                 wl_input_device_end_pointer_grab(device, time);
246
247                 if (device->drag_surface) {
248                         struct wl_resource *surface_resource =
249                                 &device->drag_surface->resource;
250                         struct wl_surface_interface *implementation =
251                                 (struct wl_surface_interface *)
252                                 surface_resource->object.implementation;
253
254                         implementation->attach(surface_resource->client,
255                                                surface_resource, NULL, 0, 0);
256                         wl_list_remove(&device->drag_icon_listener.link);
257                 }
258
259                 device->drag_data_source = NULL;
260                 device->drag_surface = NULL;
261         }
262 }
263
264 static const struct wl_pointer_grab_interface drag_grab_interface = {
265         drag_grab_focus,
266         drag_grab_motion,
267         drag_grab_button,
268 };
269
270 static void
271 destroy_data_device_icon(struct wl_listener *listener,
272                          struct wl_resource *resource, uint32_t time)
273 {
274         struct wl_input_device *device;
275
276         device = container_of(listener, struct wl_input_device,
277                               drag_icon_listener);
278
279         device->drag_surface = NULL;
280 }
281
282 static void
283 data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
284                        struct wl_resource *source_resource,
285                        struct wl_resource *origin_resource,
286                        struct wl_resource *icon_resource, uint32_t time)
287 {
288         struct wl_input_device *device = resource->data;
289
290         /* FIXME: Check that client has implicit grab on the origin
291          * surface that matches the given time. */
292
293         /* FIXME: Check that the data source type array isn't empty. */
294
295         device->drag_grab.interface = &drag_grab_interface;
296         device->drag_data_source = source_resource->data;
297
298         if (icon_resource) {
299                 device->drag_surface = icon_resource->data;
300                 device->drag_icon_listener.func = destroy_data_device_icon;
301                 wl_list_insert(icon_resource->destroy_listener_list.prev,
302                                &device->drag_icon_listener.link);
303         }
304
305         wl_input_device_start_pointer_grab(device, &device->drag_grab, time);
306 }
307
308 static void
309 destroy_selection_data_source(struct wl_listener *listener,
310                               struct wl_resource *resource, uint32_t time)
311 {
312         struct wl_input_device *device =
313                 container_of(listener, struct wl_input_device,
314                              selection_data_source_listener);
315         struct wl_resource *data_device, *focus;
316
317         device->selection_data_source = NULL;
318
319         focus = device->keyboard_focus_resource;
320         if (focus) {
321                 data_device = find_resource(&device->drag_resource_list,
322                                             focus->client);
323                 wl_resource_post_event(data_device,
324                                        WL_DATA_DEVICE_SELECTION, NULL);
325         }
326 }
327
328 WL_EXPORT void
329 wl_input_device_set_selection(struct wl_input_device *device,
330                               struct wl_data_source *source, uint32_t time)
331 {
332         struct wl_resource *data_device, *focus, *offer;
333         struct wl_selection_listener *listener, *next;
334
335         if (device->selection_data_source) {
336                 device->selection_data_source->cancel(device->selection_data_source);
337                 wl_list_remove(&device->selection_data_source_listener.link);
338                 device->selection_data_source = NULL;
339         }
340
341         device->selection_data_source = source;
342
343         focus = device->keyboard_focus_resource;
344         if (focus) {
345                 data_device = find_resource(&device->drag_resource_list,
346                                             focus->client);
347                 if (data_device) {
348                         offer = wl_data_source_send_offer(device->selection_data_source,
349                                                           data_device);
350                         wl_resource_post_event(data_device,
351                                                WL_DATA_DEVICE_SELECTION,
352                                                offer);
353                 }
354         }
355
356         wl_list_for_each_safe(listener, next,
357                               &device->selection_listener_list, link)
358                 listener->func(listener, device);
359
360         device->selection_data_source_listener.func =
361                 destroy_selection_data_source;
362         wl_list_insert(source->resource.destroy_listener_list.prev,
363                        &device->selection_data_source_listener.link);
364 }
365
366 static void
367 data_device_set_selection(struct wl_client *client,
368                           struct wl_resource *resource,
369                           struct wl_resource *source_resource, uint32_t time)
370 {
371         if (!source_resource)
372                 return;
373
374         wl_input_device_set_selection(resource->data,
375                                       source_resource->data, time);
376 }
377
378 static const struct wl_data_device_interface data_device_interface = {
379         data_device_start_drag,
380         data_device_set_selection,
381 };
382
383 static void
384 destroy_data_source(struct wl_resource *resource)
385 {
386         struct wl_data_source *source =
387                 container_of(resource, struct wl_data_source, resource);
388         char **p, **end;
389
390         end = source->mime_types.data + source->mime_types.size;
391         for (p = source->mime_types.data; p < end; p++)
392                 free(*p);
393
394         wl_array_release(&source->mime_types);
395
396         source->resource.object.id = 0;
397 }
398
399 static void
400 create_data_source(struct wl_client *client,
401                    struct wl_resource *resource, uint32_t id)
402 {
403         struct wl_data_source *source;
404
405         source = malloc(sizeof *source);
406         if (source == NULL) {
407                 wl_resource_post_no_memory(resource);
408                 return;
409         }
410
411         source->resource.destroy = destroy_data_source;
412         source->resource.object.id = id;
413         source->resource.object.interface = &wl_data_source_interface;
414         source->resource.object.implementation =
415                 (void (**)(void)) &data_source_interface;
416         source->resource.data = source;
417         wl_list_init(&source->resource.destroy_listener_list);
418
419         source->offer_interface = &data_offer_interface;
420         source->cancel = data_source_cancel;
421
422         wl_array_init(&source->mime_types);
423         wl_client_add_resource(client, &source->resource);
424 }
425
426 static void unbind_data_device(struct wl_resource *resource)
427 {
428         wl_list_remove(&resource->link);
429         free(resource);
430 }
431
432 static void
433 get_data_device(struct wl_client *client,
434                 struct wl_resource *manager_resource,
435                 uint32_t id, struct wl_resource *input_device)
436 {
437         struct wl_input_device *device = input_device->data;
438         struct wl_resource *resource;
439
440         resource =
441                 wl_client_add_object(client, &wl_data_device_interface,
442                                      &data_device_interface, id, device);
443                                      
444         wl_list_insert(&device->drag_resource_list, &resource->link);
445         resource->destroy = unbind_data_device;
446 }
447
448 static const struct wl_data_device_manager_interface manager_interface = {
449         create_data_source,
450         get_data_device
451 };
452
453 static void
454 bind_manager(struct wl_client *client,
455              void *data, uint32_t version, uint32_t id)
456 {
457         wl_client_add_object(client, &wl_data_device_manager_interface,
458                              &manager_interface, id, NULL);
459 }
460
461 WL_EXPORT void
462 wl_data_device_set_keyboard_focus(struct wl_input_device *device)
463 {
464         struct wl_resource *data_device, *focus, *offer;
465         struct wl_data_source *source;
466
467         focus = device->keyboard_focus_resource;
468         if (!focus)
469                 return;
470
471         data_device = find_resource(&device->drag_resource_list,
472                                     focus->client);
473         if (!data_device)
474                 return;
475
476         source = device->selection_data_source;
477         if (source) {
478                 offer = wl_data_source_send_offer(source, data_device);
479                 wl_resource_post_event(data_device,
480                                        WL_DATA_DEVICE_SELECTION, offer);
481         }
482 }
483
484 WL_EXPORT int
485 wl_data_device_manager_init(struct wl_display *display)
486 {
487         if (wl_display_add_global(display,
488                                   &wl_data_device_manager_interface,
489                                   NULL, bind_manager) == NULL)
490                 return -1;
491
492         return 0;
493 }