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