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