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