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