Add reject round trip to dnd protocol
[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 void
275 drag_reject(void *data, struct wl_drag *drag)
276 {
277         fprintf(stderr, "drop rejected\n");
278 }
279
280 static const struct wl_drag_listener drag_listener = {
281         drag_target,
282         drag_finish,
283         drag_reject
284 };
285
286 static void
287 drag_offer_offer(void *data,
288                  struct wl_drag_offer *offer, const char *type)
289 {
290         struct dnd_offer *dnd_offer = data;
291         char **p;
292
293         p = wl_array_add(&dnd_offer->types, sizeof *p);
294         if (p)
295                 *p = strdup(type);
296 }
297
298 static void
299 drag_offer_pointer_focus(void *data,
300                          struct wl_drag_offer *offer,
301                          uint32_t time, struct wl_surface *surface,
302                          int32_t x, int32_t y,
303                          int32_t surface_x, int32_t surface_y)
304 {
305         struct dnd_offer *dnd_offer = data;
306         struct window *window;
307         char **p, **end;
308
309         /* The last event in a dnd session is pointer_focus with a
310          * NULL surface, whether or not we get the drop event.  We
311          * need to clean up the dnd_offer proxy and whatever state we
312          * allocated. */
313         if (!surface) {
314                 fprintf(stderr, "pointer focus NULL, session over\n");
315                 wl_array_release(&dnd_offer->types);
316                 free(dnd_offer);
317                 wl_drag_offer_destroy(offer);
318                 return;
319         }
320
321         fprintf(stderr, "drag pointer focus %p\n", surface);
322         fprintf(stderr, "offered types:\n");
323         end = dnd_offer->types.data + dnd_offer->types.size;
324         for (p = dnd_offer->types.data; p < end; p++)
325                 fprintf(stderr, "\%s\n", *p);
326
327         window = wl_surface_get_user_data(surface);
328         dnd_offer->dnd = window_get_user_data(window);
329
330         if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
331                 wl_drag_offer_accept(offer, time, "text/plain");
332                 dnd_offer->drag_type = "text/plain";
333         } else {
334                 wl_drag_offer_accept(offer, time, NULL);
335                 dnd_offer->drag_type = NULL;
336         }
337 }
338
339 static void
340 drag_offer_motion(void *data,
341                   struct wl_drag_offer *offer, uint32_t time,
342                   int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
343 {
344         struct dnd_offer *dnd_offer = data;
345         struct dnd *dnd = dnd_offer->dnd;
346
347         if (!dnd_get_item(dnd, surface_x, surface_y)) {
348                 fprintf(stderr, "drag offer motion %d, %d, accepting\n",
349                         surface_x, surface_y);
350                 wl_drag_offer_accept(offer, time, "text/plain");
351                 dnd_offer->drag_type = "text/plain";
352         } else {
353                 fprintf(stderr, "drag offer motion %d, %d, declining\n",
354                         surface_x, surface_y);
355                 wl_drag_offer_accept(offer, time, NULL);
356                 dnd_offer->drag_type = NULL;
357         }
358 }
359
360 static gboolean
361 drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
362 {
363         struct dnd_offer *dnd_offer = data;
364         char buffer[256];
365         int fd;
366         unsigned int len;
367         GError *err = NULL;
368
369         g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
370         fprintf(stderr, "read %d bytes: %s\n", len, buffer);
371         fd = g_io_channel_unix_get_fd(source);
372         close(fd);
373         g_source_remove(dnd_offer->tag);
374
375         g_io_channel_unref(source);
376
377         return TRUE;
378 }
379
380 static void
381 drag_offer_drop(void *data, struct wl_drag_offer *offer)
382 {
383         struct dnd_offer *dnd_offer = data;
384         GIOChannel *channel;
385         int p[2];
386
387         if (!dnd_offer->drag_type) {
388                 fprintf(stderr, "got 'drop', but no target\n");
389                 wl_drag_offer_reject(offer);
390                 return;
391         }
392
393         fprintf(stderr, "got 'drop', sending write end of pipe\n");
394
395         pipe(p);
396         wl_drag_offer_receive(offer, p[1]);
397         close(p[1]);
398
399         channel = g_io_channel_unix_new(p[0]);
400         dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
401                                         drop_io_func, dnd_offer);
402 }
403
404 static const struct wl_drag_offer_listener drag_offer_listener = {
405         drag_offer_offer,
406         drag_offer_pointer_focus,
407         drag_offer_motion,
408         drag_offer_drop,
409 };
410
411 static void
412 drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
413 {
414         struct dnd_offer *dnd_offer;
415
416         dnd_offer = malloc(sizeof *dnd_offer);
417         if (dnd_offer == NULL)
418                 return;
419
420         wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
421         wl_array_init(&dnd_offer->types);
422 }
423
424 static cairo_surface_t *
425 create_drag_cursor(struct dnd_drag *dnd_drag,
426                    struct item *item, int32_t x, int32_t y, double opacity)
427 {
428         struct dnd *dnd = dnd_drag->dnd;
429         cairo_surface_t *surface, *pointer;
430         int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
431         struct rectangle rectangle;
432         cairo_pattern_t *pattern;
433         cairo_t *cr;
434
435         pointer = display_get_pointer_surface(dnd->display,
436                                               POINTER_DRAGGING,
437                                               &pointer_width,
438                                               &pointer_height,
439                                               &hotspot_x,
440                                               &hotspot_y);
441
442         rectangle.width = item_width + 2 * pointer_width;
443         rectangle.height = item_height + 2 * pointer_height;
444         surface = display_create_surface(dnd->display, &rectangle);
445
446         cr = cairo_create(surface);
447         cairo_translate(cr, pointer_width, pointer_height);
448
449         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
450         cairo_set_source_rgba(cr, 0, 0, 0, 0);
451         cairo_paint(cr);
452
453         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
454         cairo_set_source_surface(cr, item->surface, 0, 0);
455         pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
456         cairo_mask(cr, pattern);
457         cairo_pattern_destroy(pattern);
458
459         cairo_set_source_surface(cr, pointer,
460                                  x - item->x - hotspot_x,
461                                  y - item->y - hotspot_y);
462         cairo_surface_destroy(pointer);
463         cairo_paint(cr);
464         /* FIXME: more cairo-gl brokeness */
465         display_flush_cairo_device(dnd->display);
466         cairo_destroy(cr);
467
468         dnd_drag->hotspot_x = pointer_width + x - item->x;
469         dnd_drag->hotspot_y = pointer_height + y - item->y;
470
471         return surface;
472 }
473
474 static void
475 dnd_button_handler(struct window *window,
476                    struct input *input, uint32_t time,
477                    int button, int state, void *data)
478 {
479         struct dnd *dnd = data;
480         int32_t x, y;
481         struct item *item;
482         struct rectangle rectangle;
483         struct dnd_drag *dnd_drag;
484         struct wl_drag *drag;
485
486         window_get_child_rectangle(dnd->window, &rectangle);
487         input_get_position(input, &x, &y);
488         item = dnd_get_item(dnd, x, y);
489         x -= rectangle.x;
490         y -= rectangle.y;
491
492         if (item && state == 1) {
493                 fprintf(stderr, "start drag, item %p\n", item);
494
495                 dnd_drag = malloc(sizeof *dnd_drag);
496                 dnd_drag->dnd = dnd;
497                 dnd_drag->input = input;
498                 dnd_drag->time = time;
499
500                 dnd_drag->opaque =
501                         create_drag_cursor(dnd_drag, item, x, y, 1);
502                 dnd_drag->translucent =
503                         create_drag_cursor(dnd_drag, item, x, y, 0.2);
504
505                 drag = window_create_drag(window);
506                 wl_drag_offer(drag, "text/plain");
507                 wl_drag_offer(drag, "text/html");
508                 window_activate_drag(drag, window, input, time);
509                 wl_drag_add_listener(drag, &drag_listener, dnd_drag);
510         }
511 }
512
513 static int
514 dnd_motion_handler(struct window *window,
515                    struct input *input, uint32_t time,
516                    int32_t x, int32_t y,
517                    int32_t sx, int32_t sy, void *data)
518 {
519         struct dnd *dnd = data;
520         struct item *item;
521
522         item = dnd_get_item(dnd, sx, sy);
523
524         if (item)
525                 return POINTER_HAND1;
526         else
527                 return POINTER_LEFT_PTR;
528 }
529
530 static struct dnd *
531 dnd_create(struct display *display)
532 {
533         struct dnd *dnd;
534         gchar *title;
535         int i, x, y;
536         struct rectangle rectangle;
537
538         dnd = malloc(sizeof *dnd);
539         if (dnd == NULL)
540                 return dnd;
541         memset(dnd, 0, sizeof *dnd);
542
543         title = g_strdup_printf("Wayland Drag and Drop Demo");
544
545         dnd->window = window_create(display, title, 100, 100, 500, 400);
546         dnd->display = display;
547         dnd->key = 100;
548
549         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
550                 x = (i % 4) * (item_width + item_padding) + item_padding;
551                 y = (i / 4) * (item_height + item_padding) + item_padding;
552                 if ((i ^ (i >> 2)) & 1)
553                         dnd->items[i] = item_create(display, x, y);
554                 else
555                         dnd->items[i] = NULL;
556         }
557
558         window_set_user_data(dnd->window, dnd);
559         window_set_redraw_handler(dnd->window, redraw_handler);
560         window_set_keyboard_focus_handler(dnd->window,
561                                           keyboard_focus_handler);
562         window_set_button_handler(dnd->window,
563                                   dnd_button_handler);
564
565         window_set_motion_handler(dnd->window,
566                                   dnd_motion_handler);
567
568         rectangle.width = 4 * (item_width + item_padding) + item_padding;
569         rectangle.height = 4 * (item_height + item_padding) + item_padding;
570         window_set_child_size(dnd->window, &rectangle);
571
572         dnd_draw(dnd);
573
574         return dnd;
575 }
576
577 static const GOptionEntry option_entries[] = {
578         { NULL }
579 };
580
581 int
582 main(int argc, char *argv[])
583 {
584         struct display *d;
585         struct dnd *dnd;
586         struct timeval tv;
587
588         gettimeofday(&tv, NULL);
589         srandom(tv.tv_usec);
590
591         d = display_create(&argc, &argv, option_entries);
592         if (d == NULL) {
593                 fprintf(stderr, "failed to create display: %m\n");
594                 return -1;
595         }
596
597         display_set_drag_offer_handler(d, drag_offer_handler);
598
599         dnd = dnd_create (d);
600
601         display_run(d);
602
603         return 0;
604 }