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