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