server: use the event sending wrappers
[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_data_source_send_target(&offer->source->resource,
42                                            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_data_source_send_send(&offer->source->resource,
53                                          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_data_source_send_cancelled(&source->resource);
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_data_device_send_data_offer(target, &offer->resource);
124
125         end = source->mime_types.data + source->mime_types.size;
126         for (p = source->mime_types.data; p < end; p++)
127                 wl_data_offer_send_offer(&offer->resource, *p);
128
129         return &offer->resource;
130 }
131
132 static void
133 data_source_offer(struct wl_client *client,
134                   struct wl_resource *resource,
135                   const char *type)
136 {
137         struct wl_data_source *source = resource->data;
138         char **p;
139
140         p = wl_array_add(&source->mime_types, sizeof *p);
141         if (p)
142                 *p = strdup(type);
143         if (!p || !*p)
144                 wl_resource_post_no_memory(resource);
145 }
146
147 static void
148 data_source_destroy(struct wl_client *client, struct wl_resource *resource)
149 {
150         wl_resource_destroy(resource, 0);
151 }
152
153 static struct wl_data_source_interface data_source_interface = {
154         data_source_offer,
155         data_source_destroy
156 };
157
158 static struct wl_resource *
159 find_resource(struct wl_list *list, struct wl_client *client)
160 {
161         struct wl_resource *r;
162
163         wl_list_for_each(r, list, link) {
164                 if (r->client == client)
165                         return r;
166         }
167
168         return NULL;
169 }
170
171 static void
172 destroy_drag_focus(struct wl_listener *listener,
173                    struct wl_resource *resource, uint32_t time)
174 {
175         struct wl_input_device *device =
176                 container_of(listener, struct wl_input_device,
177                              drag_focus_listener);
178
179         device->drag_focus_resource = NULL;
180 }
181
182 static void
183 drag_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
184                 struct wl_surface *surface, int32_t x, int32_t y)
185 {
186         struct wl_input_device *device =
187                 container_of(grab, struct wl_input_device, drag_grab);
188         struct wl_resource *resource, *offer;
189
190         if (device->drag_focus_resource) {
191                 wl_data_device_send_leave(device->drag_focus_resource);
192                 wl_list_remove(&device->drag_focus_listener.link);
193                 device->drag_focus_resource = NULL;
194                 device->drag_focus = NULL;
195         }
196
197         if (surface)
198                 resource = find_resource(&device->drag_resource_list, 
199                                          surface->resource.client);
200         if (surface && resource) {
201                 offer = wl_data_source_send_offer(device->drag_data_source,
202                                                   resource);
203
204                 wl_data_device_send_enter(resource, time, surface,
205                                           x, y, offer);
206
207                 device->drag_focus = surface;
208                 device->drag_focus_listener.func = destroy_drag_focus;
209                 wl_list_insert(resource->destroy_listener_list.prev,
210                                &device->drag_focus_listener.link);
211                 device->drag_focus_resource = resource;
212                 grab->focus = surface;
213         }
214 }
215
216 static void
217 drag_grab_motion(struct wl_pointer_grab *grab,
218                  uint32_t time, int32_t x, int32_t y)
219 {
220         struct wl_input_device *device =
221                 container_of(grab, struct wl_input_device, drag_grab);
222
223         if (device->drag_focus_resource)
224                 wl_data_device_send_motion(device->drag_focus_resource,
225                                            time, x, y);
226 }
227
228 static void
229 drag_grab_button(struct wl_pointer_grab *grab,
230                  uint32_t time, int32_t button, int32_t state)
231 {
232         struct wl_input_device *device =
233                 container_of(grab, struct wl_input_device, drag_grab);
234
235         if (device->drag_focus_resource &&
236             device->grab_button == button && state == 0)
237                 wl_data_device_send_drop(device->drag_focus_resource);
238
239         if (device->button_count == 0 && state == 0) {
240                 wl_input_device_end_pointer_grab(device, time);
241
242                 if (device->drag_surface) {
243                         struct wl_resource *surface_resource =
244                                 &device->drag_surface->resource;
245                         struct wl_surface_interface *implementation =
246                                 (struct wl_surface_interface *)
247                                 surface_resource->object.implementation;
248
249                         implementation->attach(surface_resource->client,
250                                                surface_resource, NULL, 0, 0);
251                         wl_list_remove(&device->drag_icon_listener.link);
252                 }
253
254                 device->drag_data_source = NULL;
255                 device->drag_surface = NULL;
256         }
257 }
258
259 static const struct wl_pointer_grab_interface drag_grab_interface = {
260         drag_grab_focus,
261         drag_grab_motion,
262         drag_grab_button,
263 };
264
265 static void
266 destroy_data_device_icon(struct wl_listener *listener,
267                          struct wl_resource *resource, uint32_t time)
268 {
269         struct wl_input_device *device;
270
271         device = container_of(listener, struct wl_input_device,
272                               drag_icon_listener);
273
274         device->drag_surface = NULL;
275 }
276
277 static void
278 data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
279                        struct wl_resource *source_resource,
280                        struct wl_resource *origin_resource,
281                        struct wl_resource *icon_resource, uint32_t time)
282 {
283         struct wl_input_device *device = resource->data;
284
285         /* FIXME: Check that client has implicit grab on the origin
286          * surface that matches the given time. */
287
288         /* FIXME: Check that the data source type array isn't empty. */
289
290         device->drag_grab.interface = &drag_grab_interface;
291         device->drag_data_source = source_resource->data;
292
293         if (icon_resource) {
294                 device->drag_surface = icon_resource->data;
295                 device->drag_icon_listener.func = destroy_data_device_icon;
296                 wl_list_insert(icon_resource->destroy_listener_list.prev,
297                                &device->drag_icon_listener.link);
298         }
299
300         wl_input_device_start_pointer_grab(device, &device->drag_grab, time);
301 }
302
303 static void
304 destroy_selection_data_source(struct wl_listener *listener,
305                               struct wl_resource *resource, uint32_t time)
306 {
307         struct wl_input_device *device =
308                 container_of(listener, struct wl_input_device,
309                              selection_data_source_listener);
310         struct wl_resource *data_device, *focus;
311
312         device->selection_data_source = NULL;
313
314         focus = device->keyboard_focus_resource;
315         if (focus) {
316                 data_device = find_resource(&device->drag_resource_list,
317                                             focus->client);
318                 wl_data_device_send_selection(data_device, NULL);
319         }
320 }
321
322 WL_EXPORT void
323 wl_input_device_set_selection(struct wl_input_device *device,
324                               struct wl_data_source *source, uint32_t time)
325 {
326         struct wl_resource *data_device, *focus, *offer;
327         struct wl_selection_listener *listener, *next;
328
329         if (device->selection_data_source) {
330                 device->selection_data_source->cancel(device->selection_data_source);
331                 wl_list_remove(&device->selection_data_source_listener.link);
332                 device->selection_data_source = NULL;
333         }
334
335         device->selection_data_source = source;
336
337         focus = device->keyboard_focus_resource;
338         if (focus) {
339                 data_device = find_resource(&device->drag_resource_list,
340                                             focus->client);
341                 if (data_device) {
342                         offer = wl_data_source_send_offer(device->selection_data_source,
343                                                           data_device);
344                         wl_data_device_send_selection(data_device, offer);
345                 }
346         }
347
348         wl_list_for_each_safe(listener, next,
349                               &device->selection_listener_list, link)
350                 listener->func(listener, device);
351
352         device->selection_data_source_listener.func =
353                 destroy_selection_data_source;
354         wl_list_insert(source->resource.destroy_listener_list.prev,
355                        &device->selection_data_source_listener.link);
356 }
357
358 static void
359 data_device_set_selection(struct wl_client *client,
360                           struct wl_resource *resource,
361                           struct wl_resource *source_resource, uint32_t time)
362 {
363         if (!source_resource)
364                 return;
365
366         wl_input_device_set_selection(resource->data,
367                                       source_resource->data, time);
368 }
369
370 static const struct wl_data_device_interface data_device_interface = {
371         data_device_start_drag,
372         data_device_set_selection,
373 };
374
375 static void
376 destroy_data_source(struct wl_resource *resource)
377 {
378         struct wl_data_source *source =
379                 container_of(resource, struct wl_data_source, resource);
380         char **p, **end;
381
382         end = source->mime_types.data + source->mime_types.size;
383         for (p = source->mime_types.data; p < end; p++)
384                 free(*p);
385
386         wl_array_release(&source->mime_types);
387
388         source->resource.object.id = 0;
389 }
390
391 static void
392 create_data_source(struct wl_client *client,
393                    struct wl_resource *resource, uint32_t id)
394 {
395         struct wl_data_source *source;
396
397         source = malloc(sizeof *source);
398         if (source == NULL) {
399                 wl_resource_post_no_memory(resource);
400                 return;
401         }
402
403         source->resource.destroy = destroy_data_source;
404         source->resource.object.id = id;
405         source->resource.object.interface = &wl_data_source_interface;
406         source->resource.object.implementation =
407                 (void (**)(void)) &data_source_interface;
408         source->resource.data = source;
409         wl_list_init(&source->resource.destroy_listener_list);
410
411         source->offer_interface = &data_offer_interface;
412         source->cancel = data_source_cancel;
413
414         wl_array_init(&source->mime_types);
415         wl_client_add_resource(client, &source->resource);
416 }
417
418 static void unbind_data_device(struct wl_resource *resource)
419 {
420         wl_list_remove(&resource->link);
421         free(resource);
422 }
423
424 static void
425 get_data_device(struct wl_client *client,
426                 struct wl_resource *manager_resource,
427                 uint32_t id, struct wl_resource *input_device)
428 {
429         struct wl_input_device *device = input_device->data;
430         struct wl_resource *resource;
431
432         resource =
433                 wl_client_add_object(client, &wl_data_device_interface,
434                                      &data_device_interface, id, device);
435                                      
436         wl_list_insert(&device->drag_resource_list, &resource->link);
437         resource->destroy = unbind_data_device;
438 }
439
440 static const struct wl_data_device_manager_interface manager_interface = {
441         create_data_source,
442         get_data_device
443 };
444
445 static void
446 bind_manager(struct wl_client *client,
447              void *data, uint32_t version, uint32_t id)
448 {
449         wl_client_add_object(client, &wl_data_device_manager_interface,
450                              &manager_interface, id, NULL);
451 }
452
453 WL_EXPORT void
454 wl_data_device_set_keyboard_focus(struct wl_input_device *device)
455 {
456         struct wl_resource *data_device, *focus, *offer;
457         struct wl_data_source *source;
458
459         focus = device->keyboard_focus_resource;
460         if (!focus)
461                 return;
462
463         data_device = find_resource(&device->drag_resource_list,
464                                     focus->client);
465         if (!data_device)
466                 return;
467
468         source = device->selection_data_source;
469         if (source) {
470                 offer = wl_data_source_send_offer(source, data_device);
471                 wl_data_device_send_selection(data_device, offer);
472         }
473 }
474
475 WL_EXPORT int
476 wl_data_device_manager_init(struct wl_display *display)
477 {
478         if (wl_display_add_global(display,
479                                   &wl_data_device_manager_interface,
480                                   NULL, bind_manager) == NULL)
481                 return -1;
482
483         return 0;
484 }