dnd: fix segfault on grabbing
[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 <assert.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <sys/time.h>
32 #include <cairo.h>
33 #include <sys/epoll.h>
34
35 #include <wayland-client.h>
36 #include <wayland-cursor.h>
37
38 #include "window.h"
39 #include "../shared/cairo-util.h"
40
41 struct dnd_drag;
42
43 struct dnd {
44         struct window *window;
45         struct widget *widget;
46         struct display *display;
47         uint32_t key;
48         struct item *items[16];
49         int self_only;
50         struct dnd_drag *current_drag;
51 };
52
53 struct dnd_drag {
54         cairo_surface_t *translucent;
55         cairo_surface_t *opaque;
56         int hotspot_x, hotspot_y;
57         struct dnd *dnd;
58         struct input *input;
59         uint32_t time;
60         struct item *item;
61         int x_offset, y_offset;
62         int width, height;
63         const char *mime_type;
64
65         struct wl_surface *drag_surface;
66         struct wl_data_source *data_source;
67 };
68
69 struct item {
70         cairo_surface_t *surface;
71         int seed;
72         int x, y;
73 };
74
75 struct dnd_flower_message {
76         int seed, x_offset, y_offset;
77 };
78
79
80 static const int item_width = 64;
81 static const int item_height = 64;
82 static const int item_padding = 16;
83
84 static struct item *
85 item_create(struct display *display, int x, int y, int seed)
86 {
87         struct item *item;
88         struct timeval tv;
89
90         item = malloc(sizeof *item);
91         if (item == NULL)
92                 return NULL;
93         
94         
95         gettimeofday(&tv, NULL);
96         item->seed = seed ? seed : tv.tv_usec;
97         srandom(item->seed);
98         
99         const int petal_count = 3 + random() % 5;
100         const double r1 = 20 + random() % 10;
101         const double r2 = 5 + random() % 12;
102         const double u = (10 + random() % 90) / 100.0;
103         const double v = (random() % 90) / 100.0;
104
105         cairo_t *cr;
106         int i;
107         double t, dt = 2 * M_PI / (petal_count * 2);
108         double x1, y1, x2, y2, x3, y3;
109         struct rectangle rect;
110
111
112         rect.width = item_width;
113         rect.height = item_height;
114         item->surface =
115                 display_create_surface(display, NULL, &rect, SURFACE_SHM);
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_redraw_handler(struct widget *widget, void *data)
173 {
174         struct dnd *dnd = data;
175         struct rectangle allocation;
176         cairo_t *cr;
177         cairo_surface_t *surface;
178         unsigned int i;
179
180         surface = window_get_surface(dnd->window);
181         cr = cairo_create(surface);
182         widget_get_allocation(dnd->widget, &allocation);
183         cairo_rectangle(cr, allocation.x, allocation.y,
184                         allocation.width, allocation.height);
185
186         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
187         cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
188         cairo_fill(cr);
189
190         cairo_rectangle(cr, allocation.x, allocation.y,
191                         allocation.width, allocation.height);
192         cairo_clip(cr);
193         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
194         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
195                 if (!dnd->items[i])
196                         continue;
197                 cairo_set_source_surface(cr, dnd->items[i]->surface,
198                                          dnd->items[i]->x + allocation.x,
199                                          dnd->items[i]->y + allocation.y);
200                 cairo_paint(cr);
201         }
202
203         cairo_destroy(cr);
204         cairo_surface_destroy(surface);
205 }
206
207 static void
208 keyboard_focus_handler(struct window *window,
209                        struct input *device, void *data)
210 {
211         struct dnd *dnd = data;
212
213         window_schedule_redraw(dnd->window);
214 }
215
216 static int
217 dnd_add_item(struct dnd *dnd, struct item *item)
218 {
219         unsigned int i;
220
221         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
222                 if (dnd->items[i] == 0) {
223                         dnd->items[i] = item;
224                         return i;
225                 }
226         }
227         return -1;
228 }
229
230 static struct item *
231 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
232 {
233         struct item *item;
234         struct rectangle allocation;
235         unsigned int i;
236
237         widget_get_allocation(dnd->widget, &allocation);
238
239         x -= allocation.x;
240         y -= allocation.y;
241
242         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
243                 item = dnd->items[i];
244                 if (item &&
245                     item->x <= x && x < item->x + item_width &&
246                     item->y <= y && y < item->y + item_height)
247                         return item;
248         }
249
250         return NULL;
251 }
252
253 static void
254 data_source_target(void *data,
255                    struct wl_data_source *source, const char *mime_type)
256 {
257         struct dnd_drag *dnd_drag = data;
258         struct dnd *dnd = dnd_drag->dnd;
259         cairo_surface_t *surface;
260         struct wl_buffer *buffer;
261
262         dnd_drag->mime_type = mime_type;
263         if (mime_type)
264                 surface = dnd_drag->opaque;
265         else
266                 surface = dnd_drag->translucent;
267
268         buffer = display_get_buffer_for_surface(dnd->display, surface);
269         wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
270         wl_surface_damage(dnd_drag->drag_surface, 0, 0,
271                           dnd_drag->width, dnd_drag->height);
272 }
273
274 static void
275 data_source_send(void *data, struct wl_data_source *source,
276                  const char *mime_type, int32_t fd)
277 {
278         struct dnd_flower_message dnd_flower_message;   
279         struct dnd_drag *dnd_drag = data;
280         
281         dnd_flower_message.seed = dnd_drag->item->seed;
282         dnd_flower_message.x_offset = dnd_drag->x_offset;
283         dnd_flower_message.y_offset = dnd_drag->y_offset;
284
285         if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
286                 abort();
287
288         close(fd);
289 }
290
291 static void
292 data_source_cancelled(void *data, struct wl_data_source *source)
293 {
294         struct dnd_drag *dnd_drag = data;
295
296         /* The 'cancelled' event means that the source is no longer in
297          * use by the drag (or current selection).  We need to clean
298          * up the drag object created and the local state. */
299
300         wl_data_source_destroy(dnd_drag->data_source);
301         
302         /* Destroy the item that has been dragged out */
303         cairo_surface_destroy(dnd_drag->item->surface);
304         free(dnd_drag->item);
305
306         wl_surface_destroy(dnd_drag->drag_surface);
307
308         cairo_surface_destroy(dnd_drag->translucent);
309         cairo_surface_destroy(dnd_drag->opaque);
310         free(dnd_drag);
311 }
312
313 static const struct wl_data_source_listener data_source_listener = {
314         data_source_target,
315         data_source_send,
316         data_source_cancelled
317 };
318
319 static cairo_surface_t *
320 create_drag_cursor(struct dnd_drag *dnd_drag,
321                    struct item *item, int32_t x, int32_t y, double opacity)
322 {
323         struct dnd *dnd = dnd_drag->dnd;
324         cairo_surface_t *surface;
325         struct wl_cursor_image *pointer;
326         struct rectangle rectangle;
327         cairo_pattern_t *pattern;
328         cairo_t *cr;
329
330         pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
331         if (!pointer) {
332                 fprintf(stderr, "WARNING: grabbing cursor image not found\n");
333                 pointer = display_get_pointer_image(dnd->display,
334                                                     CURSOR_LEFT_PTR);
335                 assert(pointer && "no cursor image found");
336         }
337
338         rectangle.width = item_width + 2 * pointer->width;
339         rectangle.height = item_height + 2 * pointer->height;
340         surface = display_create_surface(dnd->display, NULL, &rectangle,
341                                          SURFACE_SHM);
342
343         cr = cairo_create(surface);
344         cairo_translate(cr, pointer->width, pointer->height);
345
346         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
347         cairo_set_source_rgba(cr, 0, 0, 0, 0);
348         cairo_paint(cr);
349
350         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
351         cairo_set_source_surface(cr, item->surface, 0, 0);
352         pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
353         cairo_mask(cr, pattern);
354         cairo_pattern_destroy(pattern);
355
356         /* FIXME: more cairo-gl brokeness */
357         surface_flush_device(surface);
358         cairo_destroy(cr);
359
360         dnd_drag->hotspot_x = pointer->width + x - item->x;
361         dnd_drag->hotspot_y = pointer->height + y - item->y;
362         dnd_drag->width = rectangle.width;
363         dnd_drag->height = rectangle.height;
364
365         return surface;
366 }
367
368 static void
369 dnd_button_handler(struct widget *widget,
370                    struct input *input, uint32_t time,
371                    uint32_t button, enum wl_pointer_button_state state,
372                    void *data)
373 {
374         struct dnd *dnd = data;
375         int32_t x, y;
376         struct item *item;
377         struct rectangle allocation;
378         struct dnd_drag *dnd_drag;
379         struct display *display;
380         struct wl_compositor *compositor;
381         struct wl_buffer *buffer;
382         unsigned int i;
383         uint32_t serial;
384         cairo_surface_t *icon;
385
386         widget_get_allocation(dnd->widget, &allocation);
387         input_get_position(input, &x, &y);
388         item = dnd_get_item(dnd, x, y);
389         x -= allocation.x;
390         y -= allocation.y;
391
392         if (item && state == WL_POINTER_BUTTON_STATE_PRESSED) {
393                 dnd_drag = malloc(sizeof *dnd_drag);
394                 dnd_drag->dnd = dnd;
395                 dnd_drag->input = input;
396                 dnd_drag->time = time;
397                 dnd_drag->item = item;
398                 dnd_drag->x_offset = x - item->x;
399                 dnd_drag->y_offset = y - item->y;
400
401                 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
402                         if (item == dnd->items[i]){
403                                 dnd->items[i] = 0;
404                                 break;
405                         }
406                 }
407
408                 display = window_get_display(dnd->window);
409                 compositor = display_get_compositor(display);
410                 serial = display_get_serial(display);
411                 dnd_drag->drag_surface =
412                         wl_compositor_create_surface(compositor);
413
414                 input_ungrab(input);
415
416                 if (dnd->self_only) {
417                         dnd_drag->data_source = NULL;
418                 } else {
419                         dnd_drag->data_source =
420                                 display_create_data_source(dnd->display);
421                         wl_data_source_add_listener(dnd_drag->data_source,
422                                                     &data_source_listener,
423                                                     dnd_drag);
424                         wl_data_source_offer(dnd_drag->data_source,
425                                              "application/x-wayland-dnd-flower");
426                         wl_data_source_offer(dnd_drag->data_source,
427                                              "text/plain; charset=utf-8");
428                 }
429
430                 wl_data_device_start_drag(input_get_data_device(input),
431                                           dnd_drag->data_source,
432                                           window_get_wl_surface(dnd->window),
433                                           dnd_drag->drag_surface,
434                                           serial);
435
436                 input_set_pointer_image(input, CURSOR_DRAGGING);
437
438                 dnd_drag->opaque =
439                         create_drag_cursor(dnd_drag, item, x, y, 1);
440                 dnd_drag->translucent =
441                         create_drag_cursor(dnd_drag, item, x, y, 0.2);
442
443                 if (dnd->self_only)
444                         icon = dnd_drag->opaque;
445                 else
446                         icon = dnd_drag->translucent;
447
448                 buffer = display_get_buffer_for_surface(dnd->display, icon);
449                 wl_surface_attach(dnd_drag->drag_surface, buffer,
450                                   -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
451                 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
452                                   dnd_drag->width, dnd_drag->height);
453
454                 dnd->current_drag = dnd_drag;
455                 window_schedule_redraw(dnd->window);
456         }
457 }
458
459 static int
460 lookup_cursor(struct dnd *dnd, int x, int y)
461 {
462         struct item *item;
463
464         item = dnd_get_item(dnd, x, y);
465         if (item)
466                 return CURSOR_HAND1;
467         else
468                 return CURSOR_LEFT_PTR;
469 }
470
471 static int
472 dnd_enter_handler(struct widget *widget,
473                   struct input *input, float x, float y, void *data)
474 {
475         struct dnd *dnd = data;
476
477         dnd->current_drag = NULL;
478
479         return lookup_cursor(dnd, x, y);
480 }
481
482 static int
483 dnd_motion_handler(struct widget *widget,
484                    struct input *input, uint32_t time,
485                    float x, float y, void *data)
486 {
487         return lookup_cursor(data, x, y);
488 }
489
490 static void
491 dnd_data_handler(struct window *window,
492                  struct input *input,
493                  float x, float y, const char **types, void *data)
494 {
495         struct dnd *dnd = data;
496
497         if (!types)
498                 return;
499
500         if (dnd_get_item(dnd, x, y) || dnd->self_only) {
501                 input_accept(input, NULL);
502         } else {
503                 input_accept(input, types[0]);
504         }
505 }
506
507 static void
508 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
509 {
510         struct dnd *dnd = user_data;
511         struct dnd_flower_message *message = data;
512         struct item *item;
513         struct rectangle allocation;
514
515         if (len == 0) {
516                 return;
517         } else if (len != sizeof *message) {
518                 fprintf(stderr, "odd message length %ld, expected %ld\n",
519                         len, sizeof *message);
520                 return;
521         }
522                 
523         widget_get_allocation(dnd->widget, &allocation);
524         item = item_create(dnd->display,
525                            x - message->x_offset - allocation.x,
526                            y - message->y_offset - allocation.y,
527                            message->seed);
528
529         dnd_add_item(dnd, item);
530         window_schedule_redraw(dnd->window);
531 }
532
533 static void
534 dnd_drop_handler(struct window *window, struct input *input,
535                  int32_t x, int32_t y, void *data)
536 {
537         struct dnd *dnd = data;
538         struct dnd_flower_message message;
539
540         if (dnd_get_item(dnd, x, y)) {
541                 fprintf(stderr, "got 'drop', but no target\n");
542                 return;
543         }
544
545         if (!dnd->self_only) {
546                 input_receive_drag_data(input,
547                                         "application/x-wayland-dnd-flower",
548                                         dnd_receive_func, dnd);
549         } else if (dnd->current_drag) {
550                 message.seed = dnd->current_drag->item->seed;
551                 message.x_offset = dnd->current_drag->x_offset;
552                 message.y_offset = dnd->current_drag->y_offset;
553                 dnd_receive_func(&message, sizeof message, x, y, dnd);
554                 dnd->current_drag = NULL;
555         } else {
556                 fprintf(stderr, "ignoring drop from another client\n");
557         }
558 }
559
560 static struct dnd *
561 dnd_create(struct display *display)
562 {
563         struct dnd *dnd;
564         int x, y;
565         int32_t width, height;
566         unsigned int i;
567
568         dnd = malloc(sizeof *dnd);
569         if (dnd == NULL)
570                 return dnd;
571         memset(dnd, 0, sizeof *dnd);
572
573         dnd->window = window_create(display);
574         dnd->widget = frame_create(dnd->window, dnd);
575         window_set_title(dnd->window, "Wayland Drag and Drop Demo");
576
577         dnd->display = display;
578         dnd->key = 100;
579
580         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
581                 x = (i % 4) * (item_width + item_padding) + item_padding;
582                 y = (i / 4) * (item_height + item_padding) + item_padding;
583                 if ((i ^ (i >> 2)) & 1)
584                         dnd->items[i] = item_create(display, x, y, 0);
585                 else
586                         dnd->items[i] = NULL;
587         }
588
589         window_set_user_data(dnd->window, dnd);
590         window_set_keyboard_focus_handler(dnd->window,
591                                           keyboard_focus_handler);
592         window_set_data_handler(dnd->window, dnd_data_handler);
593         window_set_drop_handler(dnd->window, dnd_drop_handler);
594
595         widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
596         widget_set_enter_handler(dnd->widget, dnd_enter_handler);
597         widget_set_motion_handler(dnd->widget, dnd_motion_handler);
598         widget_set_button_handler(dnd->widget, dnd_button_handler);
599
600         width = 4 * (item_width + item_padding) + item_padding;
601         height = 4 * (item_height + item_padding) + item_padding;
602
603         frame_set_child_size(dnd->widget, width, height);
604
605         return dnd;
606 }
607
608 int
609 main(int argc, char *argv[])
610 {
611         struct display *d;
612         struct dnd *dnd;
613         int i;
614
615         d = display_create(argc, argv);
616         if (d == NULL) {
617                 fprintf(stderr, "failed to create display: %m\n");
618                 return -1;
619         }
620
621         dnd = dnd_create(d);
622
623         for (i = 1; i < argc; i++)
624                 if (strcmp("--self-only", argv[i]) == 0)
625                         dnd->self_only = 1;
626
627         display_run(d);
628
629         return 0;
630 }