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