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