window.c: Drop timestamp argument to input_set_pointer_image()
[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_set_operator(cr, CAIRO_OPERATOR_OVER);
190         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
191                 if (!dnd->items[i])
192                         continue;
193                 cairo_set_source_surface(cr, dnd->items[i]->surface,
194                                          dnd->items[i]->x + allocation.x,
195                                          dnd->items[i]->y + allocation.y);
196                 cairo_paint(cr);
197         }
198
199         cairo_destroy(cr);
200         cairo_surface_destroy(surface);
201 }
202
203 static void
204 keyboard_focus_handler(struct window *window,
205                        struct input *device, void *data)
206 {
207         struct dnd *dnd = data;
208
209         window_schedule_redraw(dnd->window);
210 }
211
212 static int
213 dnd_add_item(struct dnd *dnd, struct item *item)
214 {
215         unsigned int i;
216
217         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
218                 if (dnd->items[i] == 0) {
219                         dnd->items[i] = item;
220                         return i;
221                 }
222         }
223         return -1;
224 }
225
226 static struct item *
227 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
228 {
229         struct item *item;
230         struct rectangle allocation;
231         unsigned int i;
232
233         widget_get_allocation(dnd->widget, &allocation);
234
235         x -= allocation.x;
236         y -= allocation.y;
237
238         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
239                 item = dnd->items[i];
240                 if (item &&
241                     item->x <= x && x < item->x + item_width &&
242                     item->y <= y && y < item->y + item_height)
243                         return item;
244         }
245
246         return NULL;
247 }
248
249 static void
250 data_source_target(void *data,
251                    struct wl_data_source *source, const char *mime_type)
252 {
253         struct dnd_drag *dnd_drag = data;
254         struct dnd *dnd = dnd_drag->dnd;
255         cairo_surface_t *surface;
256         struct wl_buffer *buffer;
257
258         dnd_drag->mime_type = mime_type;
259         if (mime_type)
260                 surface = dnd_drag->opaque;
261         else
262                 surface = dnd_drag->translucent;
263
264         buffer = display_get_buffer_for_surface(dnd->display, surface);
265         wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
266         wl_surface_damage(dnd_drag->drag_surface, 0, 0,
267                           dnd_drag->width, dnd_drag->height);
268 }
269
270 static void
271 data_source_send(void *data, struct wl_data_source *source,
272                  const char *mime_type, int32_t fd)
273 {
274         struct dnd_flower_message dnd_flower_message;   
275         struct dnd_drag *dnd_drag = data;
276         
277         dnd_flower_message.seed = dnd_drag->item->seed;
278         dnd_flower_message.x_offset = dnd_drag->x_offset;
279         dnd_flower_message.y_offset = dnd_drag->y_offset;
280
281         if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
282                 abort();
283
284         close(fd);
285 }
286
287 static void
288 data_source_cancelled(void *data, struct wl_data_source *source)
289 {
290         struct dnd_drag *dnd_drag = data;
291
292         /* The 'cancelled' event means that the source is no longer in
293          * use by the drag (or current selection).  We need to clean
294          * up the drag object created and the local state. */
295
296         wl_data_source_destroy(dnd_drag->data_source);
297         
298         /* Destroy the item that has been dragged out */
299         cairo_surface_destroy(dnd_drag->item->surface);
300         free(dnd_drag->item);
301
302         wl_surface_destroy(dnd_drag->drag_surface);
303
304         cairo_surface_destroy(dnd_drag->translucent);
305         cairo_surface_destroy(dnd_drag->opaque);
306         free(dnd_drag);
307 }
308
309 static const struct wl_data_source_listener data_source_listener = {
310         data_source_target,
311         data_source_send,
312         data_source_cancelled
313 };
314
315 static cairo_surface_t *
316 create_drag_cursor(struct dnd_drag *dnd_drag,
317                    struct item *item, int32_t x, int32_t y, double opacity)
318 {
319         struct dnd *dnd = dnd_drag->dnd;
320         cairo_surface_t *surface;
321         struct wl_cursor_image *pointer;
322         struct rectangle rectangle;
323         cairo_pattern_t *pattern;
324         cairo_t *cr;
325
326         pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
327
328         rectangle.width = item_width + 2 * pointer->width;
329         rectangle.height = item_height + 2 * pointer->height;
330         surface = display_create_surface(dnd->display, NULL, &rectangle,
331                                          SURFACE_SHM);
332
333         cr = cairo_create(surface);
334         cairo_translate(cr, pointer->width, pointer->height);
335
336         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
337         cairo_set_source_rgba(cr, 0, 0, 0, 0);
338         cairo_paint(cr);
339
340         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
341         cairo_set_source_surface(cr, item->surface, 0, 0);
342         pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
343         cairo_mask(cr, pattern);
344         cairo_pattern_destroy(pattern);
345
346         /* FIXME: more cairo-gl brokeness */
347         surface_flush_device(surface);
348         cairo_destroy(cr);
349
350         dnd_drag->hotspot_x = pointer->width + x - item->x;
351         dnd_drag->hotspot_y = pointer->height + y - item->y;
352         dnd_drag->width = rectangle.width;
353         dnd_drag->height = rectangle.height;
354
355         return surface;
356 }
357
358 static void
359 dnd_button_handler(struct widget *widget,
360                    struct input *input, uint32_t time,
361                    uint32_t button, enum wl_pointer_button_state state,
362                    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         unsigned int i;
373         uint32_t serial;
374         cairo_surface_t *icon;
375
376         widget_get_allocation(dnd->widget, &allocation);
377         input_get_position(input, &x, &y);
378         item = dnd_get_item(dnd, x, y);
379         x -= allocation.x;
380         y -= allocation.y;
381
382         if (item && state == WL_POINTER_BUTTON_STATE_PRESSED) {
383                 dnd_drag = malloc(sizeof *dnd_drag);
384                 dnd_drag->dnd = dnd;
385                 dnd_drag->input = input;
386                 dnd_drag->time = time;
387                 dnd_drag->item = item;
388                 dnd_drag->x_offset = x - item->x;
389                 dnd_drag->y_offset = y - item->y;
390
391                 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
392                         if (item == dnd->items[i]){
393                                 dnd->items[i] = 0;
394                                 break;
395                         }
396                 }
397
398                 display = window_get_display(dnd->window);
399                 compositor = display_get_compositor(display);
400                 serial = display_get_serial(display);
401                 dnd_drag->drag_surface =
402                         wl_compositor_create_surface(compositor);
403
404                 input_ungrab(input);
405
406                 if (dnd->self_only) {
407                         dnd_drag->data_source = NULL;
408                 } else {
409                         dnd_drag->data_source =
410                                 display_create_data_source(dnd->display);
411                         wl_data_source_add_listener(dnd_drag->data_source,
412                                                     &data_source_listener,
413                                                     dnd_drag);
414                         wl_data_source_offer(dnd_drag->data_source,
415                                              "application/x-wayland-dnd-flower");
416                         wl_data_source_offer(dnd_drag->data_source,
417                                              "text/plain; charset=utf-8");
418                 }
419
420                 wl_data_device_start_drag(input_get_data_device(input),
421                                           dnd_drag->data_source,
422                                           window_get_wl_surface(dnd->window),
423                                           dnd_drag->drag_surface,
424                                           serial);
425
426                 input_set_pointer_image(input, CURSOR_DRAGGING);
427
428                 dnd_drag->opaque =
429                         create_drag_cursor(dnd_drag, item, x, y, 1);
430                 dnd_drag->translucent =
431                         create_drag_cursor(dnd_drag, item, x, y, 0.2);
432
433                 if (dnd->self_only)
434                         icon = dnd_drag->opaque;
435                 else
436                         icon = dnd_drag->translucent;
437
438                 buffer = display_get_buffer_for_surface(dnd->display, icon);
439                 wl_surface_attach(dnd_drag->drag_surface, buffer,
440                                   -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
441                 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
442                                   dnd_drag->width, dnd_drag->height);
443
444                 dnd->current_drag = dnd_drag;
445                 window_schedule_redraw(dnd->window);
446         }
447 }
448
449 static int
450 lookup_cursor(struct dnd *dnd, int x, int y)
451 {
452         struct item *item;
453
454         item = dnd_get_item(dnd, x, y);
455         if (item)
456                 return CURSOR_HAND1;
457         else
458                 return CURSOR_LEFT_PTR;
459 }
460
461 static int
462 dnd_enter_handler(struct widget *widget,
463                   struct input *input, float x, float y, void *data)
464 {
465         struct dnd *dnd = data;
466
467         dnd->current_drag = NULL;
468
469         return lookup_cursor(dnd, x, y);
470 }
471
472 static int
473 dnd_motion_handler(struct widget *widget,
474                    struct input *input, uint32_t time,
475                    float x, float y, void *data)
476 {
477         return lookup_cursor(data, x, y);
478 }
479
480 static void
481 dnd_data_handler(struct window *window,
482                  struct input *input,
483                  float x, float y, const char **types, void *data)
484 {
485         struct dnd *dnd = data;
486
487         if (!types)
488                 return;
489
490         if (dnd_get_item(dnd, x, y) || dnd->self_only) {
491                 input_accept(input, NULL);
492         } else {
493                 input_accept(input, types[0]);
494         }
495 }
496
497 static void
498 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
499 {
500         struct dnd *dnd = user_data;
501         struct dnd_flower_message *message = data;
502         struct item *item;
503         struct rectangle allocation;
504
505         if (len == 0) {
506                 return;
507         } else if (len != sizeof *message) {
508                 fprintf(stderr, "odd message length %ld, expected %ld\n",
509                         len, sizeof *message);
510                 return;
511         }
512                 
513         widget_get_allocation(dnd->widget, &allocation);
514         item = item_create(dnd->display,
515                            x - message->x_offset - allocation.x,
516                            y - message->y_offset - allocation.y,
517                            message->seed);
518
519         dnd_add_item(dnd, item);
520         window_schedule_redraw(dnd->window);
521 }
522
523 static void
524 dnd_drop_handler(struct window *window, struct input *input,
525                  int32_t x, int32_t y, void *data)
526 {
527         struct dnd *dnd = data;
528         struct dnd_flower_message message;
529
530         if (dnd_get_item(dnd, x, y)) {
531                 fprintf(stderr, "got 'drop', but no target\n");
532                 return;
533         }
534
535         if (!dnd->self_only) {
536                 input_receive_drag_data(input,
537                                         "application/x-wayland-dnd-flower",
538                                         dnd_receive_func, dnd);
539         } else if (dnd->current_drag) {
540                 message.seed = dnd->current_drag->item->seed;
541                 message.x_offset = dnd->current_drag->x_offset;
542                 message.y_offset = dnd->current_drag->y_offset;
543                 dnd_receive_func(&message, sizeof message, x, y, dnd);
544                 dnd->current_drag = NULL;
545         } else {
546                 fprintf(stderr, "ignoring drop from another client\n");
547         }
548 }
549
550 static struct dnd *
551 dnd_create(struct display *display)
552 {
553         struct dnd *dnd;
554         int x, y;
555         int32_t width, height;
556         unsigned int i;
557
558         dnd = malloc(sizeof *dnd);
559         if (dnd == NULL)
560                 return dnd;
561         memset(dnd, 0, sizeof *dnd);
562
563         dnd->window = window_create(display);
564         dnd->widget = frame_create(dnd->window, dnd);
565         window_set_title(dnd->window, "Wayland Drag and Drop Demo");
566
567         dnd->display = display;
568         dnd->key = 100;
569
570         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
571                 x = (i % 4) * (item_width + item_padding) + item_padding;
572                 y = (i / 4) * (item_height + item_padding) + item_padding;
573                 if ((i ^ (i >> 2)) & 1)
574                         dnd->items[i] = item_create(display, x, y, 0);
575                 else
576                         dnd->items[i] = NULL;
577         }
578
579         window_set_user_data(dnd->window, dnd);
580         window_set_keyboard_focus_handler(dnd->window,
581                                           keyboard_focus_handler);
582         window_set_data_handler(dnd->window, dnd_data_handler);
583         window_set_drop_handler(dnd->window, dnd_drop_handler);
584
585         widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
586         widget_set_enter_handler(dnd->widget, dnd_enter_handler);
587         widget_set_motion_handler(dnd->widget, dnd_motion_handler);
588         widget_set_button_handler(dnd->widget, dnd_button_handler);
589
590         width = 4 * (item_width + item_padding) + item_padding;
591         height = 4 * (item_height + item_padding) + item_padding;
592
593         widget_schedule_resize(dnd->widget, width, height);
594
595         return dnd;
596 }
597
598 int
599 main(int argc, char *argv[])
600 {
601         struct display *d;
602         struct dnd *dnd;
603         int i;
604
605         d = display_create(argc, argv);
606         if (d == NULL) {
607                 fprintf(stderr, "failed to create display: %m\n");
608                 return -1;
609         }
610
611         dnd = dnd_create(d);
612
613         for (i = 1; i < argc; i++)
614                 if (strcmp("--self-only", argv[i]) == 0)
615                         dnd->self_only = 1;
616
617         display_run(d);
618
619         return 0;
620 }