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