db794fdaa83d129dcd0fd5c10149c33f14c0e3b0
[profile/ivi/weston.git] / clients / dnd.c
1 /*
2  * Copyright © 2010 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 <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <sys/time.h>
31 #include <cairo.h>
32 #include <sys/epoll.h>
33 #include <gdk-pixbuf/gdk-pixbuf.h>
34
35 #include <wayland-client.h>
36
37 #include "window.h"
38 #include "cairo-util.h"
39
40 struct dnd {
41         struct window *window;
42         struct display *display;
43         uint32_t key;
44         struct item *items[16];
45 };
46
47 struct dnd_drag {
48         cairo_surface_t *translucent;
49         cairo_surface_t *opaque;
50         int hotspot_x, hotspot_y;
51         struct dnd *dnd;
52         struct input *input;
53         uint32_t time;
54         struct item *item;
55         int x_offset, y_offset;
56         const char *mime_type;
57 };
58
59 struct dnd_offer {
60         int refcount;
61         struct dnd *dnd;
62         struct wl_array types;
63         struct task io_task;
64         const char *drag_type;
65         uint32_t tag;
66         int x, y;
67         int fd;
68 };
69
70 struct item {
71         cairo_surface_t *surface;
72         int seed;
73         int x, y;
74 };
75
76 struct dnd_flower_message {
77         int seed, x_offset, y_offset;
78 };
79
80
81 static const int item_width = 64;
82 static const int item_height = 64;
83 static const int item_padding = 16;
84
85 static struct item *
86 item_create(struct display *display, int x, int y, int seed)
87 {
88         struct item *item;
89         struct timeval tv;
90
91         item = malloc(sizeof *item);
92         if (item == NULL)
93                 return NULL;
94         
95         
96         gettimeofday(&tv, NULL);
97         item->seed = seed ? seed : tv.tv_usec;
98         srandom(item->seed);
99         
100         const int petal_count = 3 + random() % 5;
101         const double r1 = 20 + random() % 10;
102         const double r2 = 5 + random() % 12;
103         const double u = (10 + random() % 90) / 100.0;
104         const double v = (random() % 90) / 100.0;
105
106         cairo_t *cr;
107         int i;
108         double t, dt = 2 * M_PI / (petal_count * 2);
109         double x1, y1, x2, y2, x3, y3;
110         struct rectangle rect;
111
112
113         rect.width = item_width;
114         rect.height = item_height;
115         item->surface = display_create_surface(display, NULL, &rect, 0);
116
117         item->x = x;
118         item->y = y;
119
120         cr = cairo_create(item->surface);
121         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
122         cairo_set_source_rgba(cr, 0, 0, 0, 0);
123         cairo_paint(cr);
124
125         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
126         cairo_translate(cr, item_width / 2, item_height / 2);
127         t = random();
128         cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
129         for (i = 0; i < petal_count; i++, t += dt * 2) {
130                 x1 = cos(t) * r1;
131                 y1 = sin(t) * r1;
132                 x2 = cos(t + dt) * r2;
133                 y2 = sin(t + dt) * r2;
134                 x3 = cos(t + 2 * dt) * r1;
135                 y3 = sin(t + 2 * dt) * r1;
136
137                 cairo_curve_to(cr,
138                                x1 - y1 * u, y1 + x1 * u,
139                                x2 + y2 * v, y2 - x2 * v,
140                                x2, y2);
141
142                 cairo_curve_to(cr,
143                                x2 - y2 * v, y2 + x2 * v,
144                                x3 + y3 * u, y3 - x3 * u,
145                                x3, y3);
146         }
147
148         cairo_close_path(cr);
149
150         cairo_set_source_rgba(cr,
151                               0.5 + (random() % 50) / 49.0,
152                               0.5 + (random() % 50) / 49.0,
153                               0.5 + (random() % 50) / 49.0,
154                               0.5 + (random() % 100) / 99.0);
155
156         cairo_fill_preserve(cr);
157
158         cairo_set_line_width(cr, 1);
159         cairo_set_source_rgba(cr,
160                               0.5 + (random() % 50) / 49.0,
161                               0.5 + (random() % 50) / 49.0,
162                               0.5 + (random() % 50) / 49.0,
163                               0.5 + (random() % 100) / 99.0);
164         cairo_stroke(cr);
165
166         cairo_destroy(cr);
167
168         return item;
169 }
170
171 static void
172 dnd_draw(struct dnd *dnd)
173 {
174         struct rectangle allocation;
175         cairo_t *cr;
176         cairo_surface_t *surface;
177         int i;
178
179         window_draw(dnd->window);
180
181         surface = window_get_surface(dnd->window);
182         cr = cairo_create(surface);
183         window_get_child_allocation(dnd->window, &allocation);
184         cairo_rectangle(cr, allocation.x, allocation.y,
185                         allocation.width, allocation.height);
186         cairo_clip(cr);
187         cairo_push_group(cr);
188
189         cairo_translate(cr, allocation.x, allocation.y);
190         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
191         cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
192         cairo_paint(cr);
193
194         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
195         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
196                 if (!dnd->items[i])
197                         continue;
198                 cairo_set_source_surface(cr, dnd->items[i]->surface,
199                                          dnd->items[i]->x, dnd->items[i]->y);
200                 cairo_paint(cr);
201         }
202
203         cairo_pop_group_to_source(cr);
204         cairo_paint(cr);
205         cairo_destroy(cr);
206         cairo_surface_destroy(surface);
207         window_flush(dnd->window);
208 }
209
210 static void
211 redraw_handler(struct window *window, void *data)
212 {
213         struct dnd *dnd = data;
214
215         dnd_draw(dnd);
216 }
217
218 static void
219 keyboard_focus_handler(struct window *window,
220                        struct input *device, void *data)
221 {
222         struct dnd *dnd = data;
223
224         window_schedule_redraw(dnd->window);
225 }
226
227 static void 
228 dnd_offer_destroy(struct dnd_offer *dnd_offer)
229 {
230         dnd_offer->refcount--;
231         if (dnd_offer->refcount == 0) {
232                 wl_array_release(&dnd_offer->types);
233                 free(dnd_offer);
234         }
235 }
236
237 static int
238 dnd_add_item(struct dnd *dnd, struct item *item)
239 {
240         int i;
241
242         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
243                 if (dnd->items[i] == 0) {
244                         dnd->items[i] = item;
245                         return i;
246                 }
247         }
248         return -1;
249 }
250
251 static struct item *
252 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
253 {
254         struct item *item;
255         struct rectangle allocation;
256         int i;
257
258         window_get_child_allocation(dnd->window, &allocation);
259
260         x -= allocation.x;
261         y -= allocation.y;
262
263         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
264                 item = dnd->items[i];
265                 if (item &&
266                     item->x <= x && x < item->x + item_width &&
267                     item->y <= y && y < item->y + item_height)
268                         return item;
269         }
270
271         return NULL;
272 }
273
274 static void
275 drag_target(void *data,
276             struct wl_drag *drag, const char *mime_type)
277 {
278         struct dnd_drag *dnd_drag = data;
279         struct dnd *dnd = dnd_drag->dnd;
280         struct wl_input_device *device;
281         cairo_surface_t *surface;
282         struct wl_buffer *buffer;
283
284         fprintf(stderr, "target %s\n", mime_type);
285         device = input_get_input_device(dnd_drag->input);
286         dnd_drag->mime_type = mime_type;
287         if (mime_type)
288                 surface = dnd_drag->opaque;
289         else
290                 surface = dnd_drag->translucent;
291
292         buffer = display_get_buffer_for_surface(dnd->display, surface);
293         wl_input_device_attach(device, dnd_drag->time, buffer,
294                                dnd_drag->hotspot_x, dnd_drag->hotspot_y);
295 }
296
297 static void
298 drag_finish(void *data, struct wl_drag *drag, int fd)
299 {
300         struct dnd_drag *dnd_drag = data;
301
302         if (!dnd_drag->mime_type) {
303                 dnd_add_item(dnd_drag->dnd, dnd_drag->item);
304                 window_schedule_redraw(dnd_drag->dnd->window);
305                 return;
306         }
307         
308         struct dnd_flower_message dnd_flower_message;   
309         
310         
311         dnd_flower_message.seed = dnd_drag->item->seed;
312
313         dnd_flower_message.x_offset = dnd_drag->x_offset;
314         dnd_flower_message.y_offset = dnd_drag->y_offset;
315
316         fprintf(stderr, "got 'finish', fd %d, sending dnd_flower_message\n", fd);
317
318         write(fd, &dnd_flower_message, sizeof dnd_flower_message);
319         close(fd);
320
321         /* The 'finish' event marks the end of the session on the drag
322          * source side and we need to clean up the drag object created
323          * and the local state. */
324         wl_drag_destroy(drag);
325         
326         /* Destroy the item that has been dragged out */
327         cairo_surface_destroy(dnd_drag->item->surface);
328         free(dnd_drag->item);
329         
330         cairo_surface_destroy(dnd_drag->translucent);
331         cairo_surface_destroy(dnd_drag->opaque);
332         free(dnd_drag);
333 }
334
335 static void
336 drag_reject(void *data, struct wl_drag *drag)
337 {
338         struct dnd_drag *dnd_drag = data;
339
340         dnd_add_item(dnd_drag->dnd, dnd_drag->item);
341         window_schedule_redraw(dnd_drag->dnd->window);
342 }
343
344 static const struct wl_drag_listener drag_listener = {
345         drag_target,
346         drag_finish,
347         drag_reject
348 };
349
350 static void
351 drag_offer_offer(void *data,
352                  struct wl_drag_offer *offer, const char *type)
353 {
354         struct dnd_offer *dnd_offer = data;
355         char **p;
356
357         p = wl_array_add(&dnd_offer->types, sizeof *p);
358         if (p)
359                 *p = strdup(type);
360 }
361
362 static void
363 drag_offer_pointer_focus(void *data,
364                          struct wl_drag_offer *offer,
365                          uint32_t time, struct wl_surface *surface,
366                          int32_t x, int32_t y,
367                          int32_t surface_x, int32_t surface_y)
368 {
369         struct dnd_offer *dnd_offer = data;
370         struct window *window;
371         char **p, **end;
372
373         /* The last event in a dnd session is pointer_focus with a
374          * NULL surface, whether or not we get the drop event.  We
375          * need to clean up the dnd_offer proxy and whatever state we
376          * allocated. */
377         if (!surface) {
378                 fprintf(stderr, "pointer focus NULL, session over\n");
379                 wl_drag_offer_destroy(offer);
380                 dnd_offer_destroy(dnd_offer);
381                 return;
382         }
383
384         fprintf(stderr, "drag pointer focus %p\n", surface);
385         fprintf(stderr, "offered types:\n");
386         end = dnd_offer->types.data + dnd_offer->types.size;
387         for (p = dnd_offer->types.data; p < end; p++)
388                 fprintf(stderr, "\%s\n", *p);
389
390         window = wl_surface_get_user_data(surface);
391         dnd_offer->dnd = window_get_user_data(window);
392
393         if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
394                 wl_drag_offer_accept(offer, time, "application/x-wayland-dnd-flower");
395                 dnd_offer->drag_type = "application/x-wayland-dnd-flower";
396                 dnd_offer->x = surface_x;
397                 dnd_offer->y = surface_y;
398         } else {
399                 wl_drag_offer_accept(offer, time, NULL);
400                 dnd_offer->drag_type = NULL;
401         }
402 }
403
404 static void
405 drag_offer_motion(void *data,
406                   struct wl_drag_offer *offer, uint32_t time,
407                   int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
408 {
409         struct dnd_offer *dnd_offer = data;
410
411         if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
412                 fprintf(stderr, "drag offer motion %d, %d, accepting\n",
413                         surface_x, surface_y);
414                 wl_drag_offer_accept(offer, time, "application/x-wayland-dnd-flower");
415                 dnd_offer->drag_type = "application/x-wayland-dnd-flower";
416                 dnd_offer->x = surface_x;
417                 dnd_offer->y = surface_y;
418         } else {
419                 fprintf(stderr, "drag offer motion %d, %d, declining\n",
420                         surface_x, surface_y);
421                 wl_drag_offer_accept(offer, time, NULL);
422                 dnd_offer->drag_type = NULL;
423         }
424 }
425
426 static void
427 drop_io_func(struct task *task, uint32_t events)
428 {
429         struct dnd_offer *dnd_offer =
430                 container_of(task, struct dnd_offer, io_task);
431         struct dnd *dnd = dnd_offer->dnd;
432         struct dnd_flower_message dnd_flower_message;
433         unsigned int len;
434         struct item *item;
435
436         len = read(dnd_offer->fd,
437                    &dnd_flower_message, sizeof dnd_flower_message);
438         fprintf(stderr, "read %d bytes\n", len);
439
440         close(dnd_offer->fd);
441
442         item = item_create(dnd->display,
443                            dnd_offer->x - dnd_flower_message.x_offset - 26,
444                            dnd_offer->y - dnd_flower_message.y_offset - 66,
445                            dnd_flower_message.seed);
446
447         dnd_add_item(dnd, item);
448         window_schedule_redraw(dnd->window);
449
450         dnd_offer_destroy(dnd_offer);
451 }
452
453 static void
454 drag_offer_drop(void *data, struct wl_drag_offer *offer)
455 {
456         struct dnd_offer *dnd_offer = data;
457         int p[2];
458
459         if (!dnd_offer->drag_type) {
460                 fprintf(stderr, "got 'drop', but no target\n");
461                 wl_drag_offer_reject(offer);
462                 return;
463         }
464
465         fprintf(stderr, "got 'drop', sending write end of pipe\n");
466
467         dnd_offer->refcount++;
468         pipe(p);
469         wl_drag_offer_receive(offer, p[1]);
470         close(p[1]);
471
472         dnd_offer->io_task.run = drop_io_func;
473         dnd_offer->fd = p[0];
474         display_watch_fd(dnd_offer->dnd->display,
475                          p[0], EPOLLIN, &dnd_offer->io_task);
476 }
477
478 static const struct wl_drag_offer_listener drag_offer_listener = {
479         drag_offer_offer,
480         drag_offer_pointer_focus,
481         drag_offer_motion,
482         drag_offer_drop,
483 };
484
485 static void
486 global_handler(struct wl_display *display, uint32_t id,
487                const char *interface, uint32_t version, void *data)
488 {
489         struct wl_drag_offer *offer;
490         struct dnd_offer *dnd_offer;
491
492         if (strcmp(interface, "wl_drag_offer") != 0)
493                 return;
494
495         offer = wl_display_bind(display, id, &wl_drag_offer_interface);
496
497         dnd_offer = malloc(sizeof *dnd_offer);
498         if (dnd_offer == NULL)
499                 return;
500
501         dnd_offer->refcount = 1;
502
503         wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
504         wl_array_init(&dnd_offer->types);
505 }
506
507 static cairo_surface_t *
508 create_drag_cursor(struct dnd_drag *dnd_drag,
509                    struct item *item, int32_t x, int32_t y, double opacity)
510 {
511         struct dnd *dnd = dnd_drag->dnd;
512         cairo_surface_t *surface, *pointer;
513         int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
514         struct rectangle rectangle;
515         cairo_pattern_t *pattern;
516         cairo_t *cr;
517
518         pointer = display_get_pointer_surface(dnd->display,
519                                               POINTER_DRAGGING,
520                                               &pointer_width,
521                                               &pointer_height,
522                                               &hotspot_x,
523                                               &hotspot_y);
524
525         rectangle.width = item_width + 2 * pointer_width;
526         rectangle.height = item_height + 2 * pointer_height;
527         surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
528
529         cr = cairo_create(surface);
530         cairo_translate(cr, pointer_width, pointer_height);
531
532         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
533         cairo_set_source_rgba(cr, 0, 0, 0, 0);
534         cairo_paint(cr);
535
536         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
537         cairo_set_source_surface(cr, item->surface, 0, 0);
538         pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
539         cairo_mask(cr, pattern);
540         cairo_pattern_destroy(pattern);
541
542         cairo_set_source_surface(cr, pointer,
543                                  x - item->x - hotspot_x,
544                                  y - item->y - hotspot_y);
545         cairo_surface_destroy(pointer);
546         cairo_paint(cr);
547         /* FIXME: more cairo-gl brokeness */
548         surface_flush_device(surface);
549         cairo_destroy(cr);
550
551         dnd_drag->hotspot_x = pointer_width + x - item->x;
552         dnd_drag->hotspot_y = pointer_height + y - item->y;
553
554         return surface;
555 }
556
557 static void
558 dnd_button_handler(struct window *window,
559                    struct input *input, uint32_t time,
560                    int button, int state, void *data)
561 {
562         struct dnd *dnd = data;
563         int32_t x, y;
564         struct item *item;
565         struct rectangle allocation;
566         struct dnd_drag *dnd_drag;
567         struct wl_drag *drag;
568         int i;
569
570         window_get_child_allocation(dnd->window, &allocation);
571         input_get_position(input, &x, &y);
572         item = dnd_get_item(dnd, x, y);
573         x -= allocation.x;
574         y -= allocation.y;
575
576         if (item && state == 1) {
577                 fprintf(stderr, "start drag, item %p\n", item);
578
579                 dnd_drag = malloc(sizeof *dnd_drag);
580                 dnd_drag->dnd = dnd;
581                 dnd_drag->input = input;
582                 dnd_drag->time = time;
583                 dnd_drag->item = item;
584                 dnd_drag->x_offset = x - item->x;
585                 dnd_drag->y_offset = y - item->y;
586
587                 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
588                         if (item == dnd->items[i]){
589                                 dnd->items[i] = 0;
590                                 break;
591                         }
592                 }
593
594                 dnd_drag->opaque =
595                         create_drag_cursor(dnd_drag, item, x, y, 1);
596                 dnd_drag->translucent =
597                         create_drag_cursor(dnd_drag, item, x, y, 0.2);
598
599                 drag = window_create_drag(window);
600                 wl_drag_offer(drag, "application/x-wayland-dnd-flower");
601                 window_activate_drag(drag, window, input, time);
602                 wl_drag_add_listener(drag, &drag_listener, dnd_drag);
603                 window_schedule_redraw(dnd->window);
604         }
605 }
606
607 static int
608 dnd_motion_handler(struct window *window,
609                    struct input *input, uint32_t time,
610                    int32_t x, int32_t y,
611                    int32_t sx, int32_t sy, void *data)
612 {
613         struct dnd *dnd = data;
614         struct item *item;
615
616         item = dnd_get_item(dnd, sx, sy);
617
618         if (item)
619                 return POINTER_HAND1;
620         else
621                 return POINTER_LEFT_PTR;
622 }
623
624 static struct dnd *
625 dnd_create(struct display *display)
626 {
627         struct dnd *dnd;
628         int i, x, y;
629         int32_t width, height;
630
631         dnd = malloc(sizeof *dnd);
632         if (dnd == NULL)
633                 return dnd;
634         memset(dnd, 0, sizeof *dnd);
635
636         dnd->window = window_create(display, 400, 400);
637         window_set_title(dnd->window, "Wayland Drag and Drop Demo");
638
639         dnd->display = display;
640         dnd->key = 100;
641
642         wl_display_add_global_listener(display_get_display(display),
643                                        global_handler, dnd);
644
645         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
646                 x = (i % 4) * (item_width + item_padding) + item_padding;
647                 y = (i / 4) * (item_height + item_padding) + item_padding;
648                 if ((i ^ (i >> 2)) & 1)
649                         dnd->items[i] = item_create(display, x, y, 0);
650                 else
651                         dnd->items[i] = NULL;
652         }
653
654         window_set_user_data(dnd->window, dnd);
655         window_set_redraw_handler(dnd->window, redraw_handler);
656         window_set_keyboard_focus_handler(dnd->window,
657                                           keyboard_focus_handler);
658         window_set_button_handler(dnd->window,
659                                   dnd_button_handler);
660
661         window_set_motion_handler(dnd->window,
662                                   dnd_motion_handler);
663
664         width = 4 * (item_width + item_padding) + item_padding;
665         height = 4 * (item_height + item_padding) + item_padding;
666         window_set_child_size(dnd->window, width, height);
667
668         dnd_draw(dnd);
669
670         return dnd;
671 }
672
673 static const GOptionEntry option_entries[] = {
674         { NULL }
675 };
676
677 int
678 main(int argc, char *argv[])
679 {
680         struct display *d;
681
682         d = display_create(&argc, &argv, option_entries);
683         if (d == NULL) {
684                 fprintf(stderr, "failed to create display: %m\n");
685                 return -1;
686         }
687
688         dnd_create(d);
689
690         display_run(d);
691
692         return 0;
693 }