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