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