Update surface.attach and change surface.map to surface.map_toplevel
[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, &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 rectangle;
173         cairo_t *cr;
174         cairo_surface_t *wsurface, *surface;
175         int i;
176
177         window_draw(dnd->window);
178
179         window_get_child_rectangle(dnd->window, &rectangle);
180
181         wsurface = window_get_surface(dnd->window);
182         surface = cairo_surface_create_similar(wsurface,
183                                                CAIRO_CONTENT_COLOR_ALPHA,
184                                                rectangle.width,
185                                                rectangle.height);
186         cairo_surface_destroy(wsurface);
187
188         cr = cairo_create(surface);
189         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
190         cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
191         cairo_paint(cr);
192
193         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
194         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
195                 if (!dnd->items[i])
196                         continue;
197                 cairo_set_source_surface(cr, dnd->items[i]->surface,
198                                          dnd->items[i]->x, dnd->items[i]->y);
199                 cairo_paint(cr);
200         }
201
202         cairo_destroy(cr);
203
204         window_copy_surface(dnd->window, &rectangle, surface);
205         cairo_surface_destroy(surface);
206         window_flush(dnd->window);
207 }
208
209 static void
210 redraw_handler(struct window *window, void *data)
211 {
212         struct dnd *dnd = data;
213
214         dnd_draw(dnd);
215 }
216
217 static void
218 keyboard_focus_handler(struct window *window,
219                        struct input *device, void *data)
220 {
221         struct dnd *dnd = data;
222
223         window_schedule_redraw(dnd->window);
224 }
225
226 static void 
227 dnd_offer_destroy(struct dnd_offer *dnd_offer)
228 {
229         dnd_offer->refcount--;
230         if (dnd_offer->refcount == 0) {
231                 wl_array_release(&dnd_offer->types);
232                 free(dnd_offer);
233         }
234 }
235
236 static int
237 dnd_add_item(struct dnd *dnd, struct item *item)
238 {
239         int i;
240
241         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
242                 if (dnd->items[i] == 0) {
243                         dnd->items[i] = item;
244                         return i;
245                 }
246         }
247         return -1;
248 }
249
250 static struct item *
251 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
252 {
253         struct item *item;
254         struct rectangle rectangle;
255         int i;
256
257         window_get_child_rectangle(dnd->window, &rectangle);
258
259         x -= rectangle.x;
260         y -= rectangle.y;
261
262         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
263                 item = dnd->items[i];
264                 if (item &&
265                     item->x <= x && x < item->x + item_width &&
266                     item->y <= y && y < item->y + item_height)
267                         return item;
268         }
269
270         return NULL;
271 }
272
273 static void
274 drag_target(void *data,
275             struct wl_drag *drag, const char *mime_type)
276 {
277         struct dnd_drag *dnd_drag = data;
278         struct dnd *dnd = dnd_drag->dnd;
279         struct wl_input_device *device;
280         cairo_surface_t *surface;
281         struct wl_buffer *buffer;
282
283         fprintf(stderr, "target %s\n", mime_type);
284         device = input_get_input_device(dnd_drag->input);
285         dnd_drag->mime_type = mime_type;
286         if (mime_type)
287                 surface = dnd_drag->opaque;
288         else
289                 surface = dnd_drag->translucent;
290
291         buffer = display_get_buffer_for_surface(dnd->display, surface);
292         wl_input_device_attach(device, dnd_drag->time, buffer,
293                                dnd_drag->hotspot_x, dnd_drag->hotspot_y);
294 }
295
296 static void
297 drag_finish(void *data, struct wl_drag *drag, int fd)
298 {
299         struct dnd_drag *dnd_drag = data;
300
301         if (!dnd_drag->mime_type) {
302                 dnd_add_item(dnd_drag->dnd, dnd_drag->item);
303                 window_schedule_redraw(dnd_drag->dnd->window);
304                 return;
305         }
306         
307         struct dnd_flower_message dnd_flower_message;   
308         
309         
310         dnd_flower_message.seed = dnd_drag->item->seed;
311
312         dnd_flower_message.x_offset = dnd_drag->x_offset;
313         dnd_flower_message.y_offset = dnd_drag->y_offset;
314
315         fprintf(stderr, "got 'finish', fd %d, sending dnd_flower_message\n", fd);
316
317         write(fd, &dnd_flower_message, sizeof dnd_flower_message);
318         close(fd);
319
320         /* The 'finish' event marks the end of the session on the drag
321          * source side and we need to clean up the drag object created
322          * and the local state. */
323         wl_drag_destroy(drag);
324         
325         /* Destroy the item that has been dragged out */
326         cairo_surface_destroy(dnd_drag->item->surface);
327         free(dnd_drag->item);
328         
329         cairo_surface_destroy(dnd_drag->translucent);
330         cairo_surface_destroy(dnd_drag->opaque);
331         free(dnd_drag);
332 }
333
334 static void
335 drag_reject(void *data, struct wl_drag *drag)
336 {
337         struct dnd_drag *dnd_drag = data;
338
339         dnd_add_item(dnd_drag->dnd, dnd_drag->item);
340         window_schedule_redraw(dnd_drag->dnd->window);
341 }
342
343 static const struct wl_drag_listener drag_listener = {
344         drag_target,
345         drag_finish,
346         drag_reject
347 };
348
349 static void
350 drag_offer_offer(void *data,
351                  struct wl_drag_offer *offer, const char *type)
352 {
353         struct dnd_offer *dnd_offer = data;
354         char **p;
355
356         p = wl_array_add(&dnd_offer->types, sizeof *p);
357         if (p)
358                 *p = strdup(type);
359 }
360
361 static void
362 drag_offer_pointer_focus(void *data,
363                          struct wl_drag_offer *offer,
364                          uint32_t time, struct wl_surface *surface,
365                          int32_t x, int32_t y,
366                          int32_t surface_x, int32_t surface_y)
367 {
368         struct dnd_offer *dnd_offer = data;
369         struct window *window;
370         char **p, **end;
371
372         /* The last event in a dnd session is pointer_focus with a
373          * NULL surface, whether or not we get the drop event.  We
374          * need to clean up the dnd_offer proxy and whatever state we
375          * allocated. */
376         if (!surface) {
377                 fprintf(stderr, "pointer focus NULL, session over\n");
378                 wl_drag_offer_destroy(offer);
379                 dnd_offer_destroy(dnd_offer);
380                 return;
381         }
382
383         fprintf(stderr, "drag pointer focus %p\n", surface);
384         fprintf(stderr, "offered types:\n");
385         end = dnd_offer->types.data + dnd_offer->types.size;
386         for (p = dnd_offer->types.data; p < end; p++)
387                 fprintf(stderr, "\%s\n", *p);
388
389         window = wl_surface_get_user_data(surface);
390         dnd_offer->dnd = window_get_user_data(window);
391
392         if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
393                 wl_drag_offer_accept(offer, time, "application/x-wayland-dnd-flower");
394                 dnd_offer->drag_type = "application/x-wayland-dnd-flower";
395                 dnd_offer->x = surface_x;
396                 dnd_offer->y = surface_y;
397         } else {
398                 wl_drag_offer_accept(offer, time, NULL);
399                 dnd_offer->drag_type = NULL;
400         }
401 }
402
403 static void
404 drag_offer_motion(void *data,
405                   struct wl_drag_offer *offer, uint32_t time,
406                   int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
407 {
408         struct dnd_offer *dnd_offer = data;
409
410         if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
411                 fprintf(stderr, "drag offer motion %d, %d, accepting\n",
412                         surface_x, surface_y);
413                 wl_drag_offer_accept(offer, time, "application/x-wayland-dnd-flower");
414                 dnd_offer->drag_type = "application/x-wayland-dnd-flower";
415                 dnd_offer->x = surface_x;
416                 dnd_offer->y = surface_y;
417         } else {
418                 fprintf(stderr, "drag offer motion %d, %d, declining\n",
419                         surface_x, surface_y);
420                 wl_drag_offer_accept(offer, time, NULL);
421                 dnd_offer->drag_type = NULL;
422         }
423 }
424
425 static gboolean
426 drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
427 {
428         struct dnd_offer *dnd_offer = data;
429         struct dnd *dnd = dnd_offer->dnd;
430         struct dnd_flower_message dnd_flower_message;
431         int fd;
432         unsigned int len;
433         struct item *item;
434
435         fd = g_io_channel_unix_get_fd(source);
436         len = read(fd, &dnd_flower_message, sizeof dnd_flower_message);
437         fprintf(stderr, "read %d bytes\n", len);
438
439         close(fd);
440         g_source_remove(dnd_offer->tag);
441
442         g_io_channel_unref(source);
443
444         item = item_create(dnd->display,
445                            dnd_offer->x - dnd_flower_message.x_offset - 26,
446                            dnd_offer->y - dnd_flower_message.y_offset - 66,
447                            dnd_flower_message.seed);
448
449         dnd_add_item(dnd, item);
450         window_schedule_redraw(dnd->window);
451
452         dnd_offer_destroy(dnd_offer);
453
454         return TRUE;
455 }
456
457 static void
458 drag_offer_drop(void *data, struct wl_drag_offer *offer)
459 {
460         struct dnd_offer *dnd_offer = data;
461         GIOChannel *channel;
462         int p[2];
463
464         if (!dnd_offer->drag_type) {
465                 fprintf(stderr, "got 'drop', but no target\n");
466                 wl_drag_offer_reject(offer);
467                 return;
468         }
469
470         fprintf(stderr, "got 'drop', sending write end of pipe\n");
471
472         dnd_offer->refcount++;
473         pipe(p);
474         wl_drag_offer_receive(offer, p[1]);
475         close(p[1]);
476
477         channel = g_io_channel_unix_new(p[0]);
478         dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
479                                         drop_io_func, dnd_offer);
480 }
481
482 static const struct wl_drag_offer_listener drag_offer_listener = {
483         drag_offer_offer,
484         drag_offer_pointer_focus,
485         drag_offer_motion,
486         drag_offer_drop,
487 };
488
489 static void
490 drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
491 {
492         struct dnd_offer *dnd_offer;
493
494         dnd_offer = malloc(sizeof *dnd_offer);
495         if (dnd_offer == NULL)
496                 return;
497         
498         dnd_offer->refcount = 1;
499
500         wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
501         wl_array_init(&dnd_offer->types);
502 }
503
504 static cairo_surface_t *
505 create_drag_cursor(struct dnd_drag *dnd_drag,
506                    struct item *item, int32_t x, int32_t y, double opacity)
507 {
508         struct dnd *dnd = dnd_drag->dnd;
509         cairo_surface_t *surface, *pointer;
510         int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
511         struct rectangle rectangle;
512         cairo_pattern_t *pattern;
513         cairo_t *cr;
514
515         pointer = display_get_pointer_surface(dnd->display,
516                                               POINTER_DRAGGING,
517                                               &pointer_width,
518                                               &pointer_height,
519                                               &hotspot_x,
520                                               &hotspot_y);
521
522         rectangle.width = item_width + 2 * pointer_width;
523         rectangle.height = item_height + 2 * pointer_height;
524         surface = display_create_surface(dnd->display, &rectangle);
525
526         cr = cairo_create(surface);
527         cairo_translate(cr, pointer_width, pointer_height);
528
529         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
530         cairo_set_source_rgba(cr, 0, 0, 0, 0);
531         cairo_paint(cr);
532
533         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
534         cairo_set_source_surface(cr, item->surface, 0, 0);
535         pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
536         cairo_mask(cr, pattern);
537         cairo_pattern_destroy(pattern);
538
539         cairo_set_source_surface(cr, pointer,
540                                  x - item->x - hotspot_x,
541                                  y - item->y - hotspot_y);
542         cairo_surface_destroy(pointer);
543         cairo_paint(cr);
544         /* FIXME: more cairo-gl brokeness */
545         display_flush_cairo_device(dnd->display);
546         cairo_destroy(cr);
547
548         dnd_drag->hotspot_x = pointer_width + x - item->x;
549         dnd_drag->hotspot_y = pointer_height + y - item->y;
550
551         return surface;
552 }
553
554 static void
555 dnd_button_handler(struct window *window,
556                    struct input *input, uint32_t time,
557                    int button, int state, void *data)
558 {
559         struct dnd *dnd = data;
560         int32_t x, y;
561         struct item *item;
562         struct rectangle rectangle;
563         struct dnd_drag *dnd_drag;
564         struct wl_drag *drag;
565         int i;
566
567         window_get_child_rectangle(dnd->window, &rectangle);
568         input_get_position(input, &x, &y);
569         item = dnd_get_item(dnd, x, y);
570         x -= rectangle.x;
571         y -= rectangle.y;
572
573         if (item && state == 1) {
574                 fprintf(stderr, "start drag, item %p\n", item);
575
576                 dnd_drag = malloc(sizeof *dnd_drag);
577                 dnd_drag->dnd = dnd;
578                 dnd_drag->input = input;
579                 dnd_drag->time = time;
580                 dnd_drag->item = item;
581                 dnd_drag->x_offset = x - item->x;
582                 dnd_drag->y_offset = y - item->y;
583
584                 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
585                         if (item == dnd->items[i]){
586                                 dnd->items[i] = 0;
587                                 break;
588                         }
589                 }
590
591                 dnd_drag->opaque =
592                         create_drag_cursor(dnd_drag, item, x, y, 1);
593                 dnd_drag->translucent =
594                         create_drag_cursor(dnd_drag, item, x, y, 0.2);
595
596                 drag = window_create_drag(window);
597                 wl_drag_offer(drag, "application/x-wayland-dnd-flower");
598                 window_activate_drag(drag, window, input, time);
599                 wl_drag_add_listener(drag, &drag_listener, dnd_drag);
600                 window_schedule_redraw(dnd->window);
601         }
602 }
603
604 static int
605 dnd_motion_handler(struct window *window,
606                    struct input *input, uint32_t time,
607                    int32_t x, int32_t y,
608                    int32_t sx, int32_t sy, void *data)
609 {
610         struct dnd *dnd = data;
611         struct item *item;
612
613         item = dnd_get_item(dnd, sx, sy);
614
615         if (item)
616                 return POINTER_HAND1;
617         else
618                 return POINTER_LEFT_PTR;
619 }
620
621 static struct dnd *
622 dnd_create(struct display *display)
623 {
624         struct dnd *dnd;
625         gchar *title;
626         int i, x, y;
627         struct rectangle rectangle;
628
629         dnd = malloc(sizeof *dnd);
630         if (dnd == NULL)
631                 return dnd;
632         memset(dnd, 0, sizeof *dnd);
633
634         title = g_strdup_printf("Wayland Drag and Drop Demo");
635
636         dnd->window = window_create(display, title, 500, 400);
637         dnd->display = display;
638         dnd->key = 100;
639
640         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
641                 x = (i % 4) * (item_width + item_padding) + item_padding;
642                 y = (i / 4) * (item_height + item_padding) + item_padding;
643                 if ((i ^ (i >> 2)) & 1)
644                         dnd->items[i] = item_create(display, x, y, 0);
645                 else
646                         dnd->items[i] = NULL;
647         }
648
649         window_set_user_data(dnd->window, dnd);
650         window_set_redraw_handler(dnd->window, redraw_handler);
651         window_set_keyboard_focus_handler(dnd->window,
652                                           keyboard_focus_handler);
653         window_set_button_handler(dnd->window,
654                                   dnd_button_handler);
655
656         window_set_motion_handler(dnd->window,
657                                   dnd_motion_handler);
658
659         rectangle.width = 4 * (item_width + item_padding) + item_padding;
660         rectangle.height = 4 * (item_height + item_padding) + item_padding;
661         window_set_child_size(dnd->window, &rectangle);
662
663         dnd_draw(dnd);
664
665         return dnd;
666 }
667
668 static const GOptionEntry option_entries[] = {
669         { NULL }
670 };
671
672 int
673 main(int argc, char *argv[])
674 {
675         struct display *d;
676         struct dnd *dnd;
677
678         d = display_create(&argc, &argv, option_entries);
679         if (d == NULL) {
680                 fprintf(stderr, "failed to create display: %m\n");
681                 return -1;
682         }
683
684         display_set_drag_offer_handler(d, drag_offer_handler);
685
686         dnd = dnd_create (d);
687
688         display_run(d);
689
690         return 0;
691 }