Add an option parser
[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         struct wl_data_device *device;
251
252         device = input_get_data_device(dnd_drag->input);
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 }
262
263 static void
264 data_source_send(void *data, struct wl_data_source *source,
265                  const char *mime_type, int32_t fd)
266 {
267         struct dnd_flower_message dnd_flower_message;   
268         struct dnd_drag *dnd_drag = data;
269         
270         dnd_flower_message.seed = dnd_drag->item->seed;
271         dnd_flower_message.x_offset = dnd_drag->x_offset;
272         dnd_flower_message.y_offset = dnd_drag->y_offset;
273
274         write(fd, &dnd_flower_message, sizeof dnd_flower_message);
275         close(fd);
276 }
277
278 static void
279 data_source_cancelled(void *data, struct wl_data_source *source)
280 {
281         struct dnd_drag *dnd_drag = data;
282
283         /* The 'cancelled' event means that the source is no longer in
284          * use by the drag (or current selection).  We need to clean
285          * up the drag object created and the local state. */
286
287         wl_data_source_destroy(dnd_drag->data_source);
288         
289         /* Destroy the item that has been dragged out */
290         cairo_surface_destroy(dnd_drag->item->surface);
291         free(dnd_drag->item);
292
293         wl_surface_destroy(dnd_drag->drag_surface);
294
295         cairo_surface_destroy(dnd_drag->translucent);
296         cairo_surface_destroy(dnd_drag->opaque);
297         free(dnd_drag);
298 }
299
300 static const struct wl_data_source_listener data_source_listener = {
301         data_source_target,
302         data_source_send,
303         data_source_cancelled
304 };
305
306 static cairo_surface_t *
307 create_drag_cursor(struct dnd_drag *dnd_drag,
308                    struct item *item, int32_t x, int32_t y, double opacity)
309 {
310         struct dnd *dnd = dnd_drag->dnd;
311         cairo_surface_t *surface, *pointer;
312         int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
313         struct rectangle rectangle;
314         cairo_pattern_t *pattern;
315         cairo_t *cr;
316
317         pointer = display_get_pointer_surface(dnd->display,
318                                               POINTER_DRAGGING,
319                                               &pointer_width,
320                                               &pointer_height,
321                                               &hotspot_x,
322                                               &hotspot_y);
323
324         rectangle.width = item_width + 2 * pointer_width;
325         rectangle.height = item_height + 2 * pointer_height;
326         surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
327
328         cr = cairo_create(surface);
329         cairo_translate(cr, pointer_width, pointer_height);
330
331         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
332         cairo_set_source_rgba(cr, 0, 0, 0, 0);
333         cairo_paint(cr);
334
335         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
336         cairo_set_source_surface(cr, item->surface, 0, 0);
337         pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
338         cairo_mask(cr, pattern);
339         cairo_pattern_destroy(pattern);
340
341         cairo_set_source_surface(cr, pointer,
342                                  x - item->x - hotspot_x,
343                                  y - item->y - hotspot_y);
344         cairo_surface_destroy(pointer);
345         cairo_paint(cr);
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
353         return surface;
354 }
355
356 static void
357 dnd_button_handler(struct widget *widget,
358                    struct input *input, uint32_t time,
359                    int button, int state, void *data)
360 {
361         struct dnd *dnd = data;
362         int32_t x, y;
363         struct item *item;
364         struct rectangle allocation;
365         struct dnd_drag *dnd_drag;
366         struct display *display;
367         struct wl_compositor *compositor;
368         struct wl_buffer *buffer;
369         int i;
370
371         widget_get_allocation(dnd->widget, &allocation);
372         input_get_position(input, &x, &y);
373         item = dnd_get_item(dnd, x, y);
374         x -= allocation.x;
375         y -= allocation.y;
376
377         if (item && state == 1) {
378                 dnd_drag = malloc(sizeof *dnd_drag);
379                 dnd_drag->dnd = dnd;
380                 dnd_drag->input = input;
381                 dnd_drag->time = time;
382                 dnd_drag->item = item;
383                 dnd_drag->x_offset = x - item->x;
384                 dnd_drag->y_offset = y - item->y;
385
386                 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
387                         if (item == dnd->items[i]){
388                                 dnd->items[i] = 0;
389                                 break;
390                         }
391                 }
392
393                 display = window_get_display(dnd->window);
394                 compositor = display_get_compositor(display);
395                 dnd_drag->drag_surface =
396                         wl_compositor_create_surface(compositor);
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                                           time);
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
424                 window_schedule_redraw(dnd->window);
425         }
426 }
427
428 static int
429 lookup_cursor(struct dnd *dnd, int x, int y)
430 {
431         struct item *item;
432
433         item = dnd_get_item(dnd, x, y);
434         if (item)
435                 return POINTER_HAND1;
436         else
437                 return POINTER_LEFT_PTR;
438 }
439
440 static int
441 dnd_enter_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 int
449 dnd_motion_handler(struct widget *widget,
450                    struct input *input, uint32_t time,
451                    int32_t x, int32_t y, void *data)
452 {
453         return lookup_cursor(data, x, y);
454 }
455
456 static void
457 dnd_data_handler(struct window *window,
458                  struct input *input, uint32_t time,
459                  int32_t x, int32_t y, const char **types, void *data)
460 {
461         struct dnd *dnd = data;
462
463         if (!dnd_get_item(dnd, x, y)) {
464                 input_accept(input, time, types[0]);
465         } else {
466                 input_accept(input, time, NULL);
467         }
468 }
469
470 static void
471 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
472 {
473         struct dnd *dnd = user_data;
474         struct dnd_flower_message *message = data;
475         struct item *item;
476         struct rectangle allocation;
477
478         if (len == 0) {
479                 return;
480         } else if (len != sizeof *message) {
481                 fprintf(stderr, "odd message length %ld, expected %ld\n",
482                         len, sizeof *message);
483                 return;
484         }
485                 
486         widget_get_allocation(dnd->widget, &allocation);
487         item = item_create(dnd->display,
488                            x - message->x_offset - allocation.x,
489                            y - message->y_offset - allocation.y,
490                            message->seed);
491
492         dnd_add_item(dnd, item);
493         window_schedule_redraw(dnd->window);
494 }
495
496 static void
497 dnd_drop_handler(struct window *window, struct input *input,
498                  int32_t x, int32_t y, void *data)
499 {
500         struct dnd *dnd = data;
501
502         if (dnd_get_item(dnd, x, y)) {
503                 fprintf(stderr, "got 'drop', but no target\n");
504                 return;
505         }
506
507         input_receive_drag_data(input, 
508                                 "application/x-wayland-dnd-flower",
509                                 dnd_receive_func, dnd);
510 }
511
512 static struct dnd *
513 dnd_create(struct display *display)
514 {
515         struct dnd *dnd;
516         int i, x, y;
517         int32_t width, height;
518
519         dnd = malloc(sizeof *dnd);
520         if (dnd == NULL)
521                 return dnd;
522         memset(dnd, 0, sizeof *dnd);
523
524         dnd->window = window_create(display);
525         dnd->widget = frame_create(dnd->window, dnd);
526         window_set_title(dnd->window, "Wayland Drag and Drop Demo");
527
528         dnd->display = display;
529         dnd->key = 100;
530
531         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
532                 x = (i % 4) * (item_width + item_padding) + item_padding;
533                 y = (i / 4) * (item_height + item_padding) + item_padding;
534                 if ((i ^ (i >> 2)) & 1)
535                         dnd->items[i] = item_create(display, x, y, 0);
536                 else
537                         dnd->items[i] = NULL;
538         }
539
540         window_set_user_data(dnd->window, dnd);
541         window_set_keyboard_focus_handler(dnd->window,
542                                           keyboard_focus_handler);
543         window_set_data_handler(dnd->window, dnd_data_handler);
544         window_set_drop_handler(dnd->window, dnd_drop_handler);
545
546         widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
547         widget_set_enter_handler(dnd->widget, dnd_enter_handler);
548         widget_set_motion_handler(dnd->widget, dnd_motion_handler);
549         widget_set_button_handler(dnd->widget, dnd_button_handler);
550
551         width = 4 * (item_width + item_padding) + item_padding;
552         height = 4 * (item_height + item_padding) + item_padding;
553
554         widget_schedule_resize(dnd->widget, width, height);
555
556         return dnd;
557 }
558
559 int
560 main(int argc, char *argv[])
561 {
562         struct display *d;
563
564         d = display_create(argc, argv);
565         if (d == NULL) {
566                 fprintf(stderr, "failed to create display: %m\n");
567                 return -1;
568         }
569
570         dnd_create(d);
571
572         display_run(d);
573
574         return 0;
575 }