client: Fix shell unstable version check
[profile/ivi/weston-ivi-shell.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 <assert.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <sys/time.h>
32 #include <cairo.h>
33 #include <sys/epoll.h>
34
35 #include <wayland-client.h>
36 #include <wayland-cursor.h>
37
38 #include "window.h"
39 #include "../shared/cairo-util.h"
40
41 struct dnd_drag;
42
43 struct dnd {
44         struct window *window;
45         struct widget *widget;
46         struct display *display;
47         uint32_t key;
48         struct item *items[16];
49         int self_only;
50         struct dnd_drag *current_drag;
51 };
52
53 struct dnd_drag {
54         cairo_surface_t *translucent;
55         cairo_surface_t *opaque;
56         int hotspot_x, hotspot_y;
57         struct dnd *dnd;
58         struct input *input;
59         uint32_t time;
60         struct item *item;
61         int x_offset, y_offset;
62         int width, height;
63         const char *mime_type;
64
65         struct wl_surface *drag_surface;
66         struct wl_data_source *data_source;
67 };
68
69 struct item {
70         cairo_surface_t *surface;
71         int seed;
72         int x, y;
73 };
74
75 struct dnd_flower_message {
76         int seed, x_offset, y_offset;
77 };
78
79
80 static const int item_width = 64;
81 static const int item_height = 64;
82 static const int item_padding = 16;
83
84 static const char flower_mime_type[] = "application/x-wayland-dnd-flower";
85 static const char text_mime_type[] = "text/plain;charset=utf-8";
86
87 static struct item *
88 item_create(struct display *display, int x, int y, int seed)
89 {
90         struct item *item;
91         struct timeval tv;
92
93         item = malloc(sizeof *item);
94         if (item == NULL)
95                 return NULL;
96         
97         
98         gettimeofday(&tv, NULL);
99         item->seed = seed ? seed : tv.tv_usec;
100         srandom(item->seed);
101         
102         const int petal_count = 3 + random() % 5;
103         const double r1 = 20 + random() % 10;
104         const double r2 = 5 + random() % 12;
105         const double u = (10 + random() % 90) / 100.0;
106         const double v = (random() % 90) / 100.0;
107
108         cairo_t *cr;
109         int i;
110         double t, dt = 2 * M_PI / (petal_count * 2);
111         double x1, y1, x2, y2, x3, y3;
112         struct rectangle rect;
113
114
115         rect.width = item_width;
116         rect.height = item_height;
117         item->surface =
118                 display_create_surface(display, NULL, &rect, SURFACE_SHM);
119
120         item->x = x;
121         item->y = y;
122
123         cr = cairo_create(item->surface);
124         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
125         cairo_set_source_rgba(cr, 0, 0, 0, 0);
126         cairo_paint(cr);
127
128         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
129         cairo_translate(cr, item_width / 2, item_height / 2);
130         t = random();
131         cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
132         for (i = 0; i < petal_count; i++, t += dt * 2) {
133                 x1 = cos(t) * r1;
134                 y1 = sin(t) * r1;
135                 x2 = cos(t + dt) * r2;
136                 y2 = sin(t + dt) * r2;
137                 x3 = cos(t + 2 * dt) * r1;
138                 y3 = sin(t + 2 * dt) * r1;
139
140                 cairo_curve_to(cr,
141                                x1 - y1 * u, y1 + x1 * u,
142                                x2 + y2 * v, y2 - x2 * v,
143                                x2, y2);
144
145                 cairo_curve_to(cr,
146                                x2 - y2 * v, y2 + x2 * v,
147                                x3 + y3 * u, y3 - x3 * u,
148                                x3, y3);
149         }
150
151         cairo_close_path(cr);
152
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
159         cairo_fill_preserve(cr);
160
161         cairo_set_line_width(cr, 1);
162         cairo_set_source_rgba(cr,
163                               0.5 + (random() % 50) / 49.0,
164                               0.5 + (random() % 50) / 49.0,
165                               0.5 + (random() % 50) / 49.0,
166                               0.5 + (random() % 100) / 99.0);
167         cairo_stroke(cr);
168
169         cairo_destroy(cr);
170
171         return item;
172 }
173
174 static void
175 dnd_redraw_handler(struct widget *widget, void *data)
176 {
177         struct dnd *dnd = data;
178         struct rectangle allocation;
179         cairo_t *cr;
180         cairo_surface_t *surface;
181         unsigned int i;
182
183         surface = window_get_surface(dnd->window);
184         cr = cairo_create(surface);
185         widget_get_allocation(dnd->widget, &allocation);
186         cairo_rectangle(cr, allocation.x, allocation.y,
187                         allocation.width, allocation.height);
188
189         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
190         cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
191         cairo_fill(cr);
192
193         cairo_rectangle(cr, allocation.x, allocation.y,
194                         allocation.width, allocation.height);
195         cairo_clip(cr);
196         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
197         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
198                 if (!dnd->items[i])
199                         continue;
200                 cairo_set_source_surface(cr, dnd->items[i]->surface,
201                                          dnd->items[i]->x + allocation.x,
202                                          dnd->items[i]->y + allocation.y);
203                 cairo_paint(cr);
204         }
205
206         cairo_destroy(cr);
207         cairo_surface_destroy(surface);
208 }
209
210 static void
211 keyboard_focus_handler(struct window *window,
212                        struct input *device, void *data)
213 {
214         struct dnd *dnd = data;
215
216         window_schedule_redraw(dnd->window);
217 }
218
219 static int
220 dnd_add_item(struct dnd *dnd, struct item *item)
221 {
222         unsigned int i;
223
224         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
225                 if (dnd->items[i] == 0) {
226                         dnd->items[i] = item;
227                         return i;
228                 }
229         }
230         return -1;
231 }
232
233 static struct item *
234 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
235 {
236         struct item *item;
237         struct rectangle allocation;
238         unsigned int i;
239
240         widget_get_allocation(dnd->widget, &allocation);
241
242         x -= allocation.x;
243         y -= allocation.y;
244
245         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
246                 item = dnd->items[i];
247                 if (item &&
248                     item->x <= x && x < item->x + item_width &&
249                     item->y <= y && y < item->y + item_height)
250                         return item;
251         }
252
253         return NULL;
254 }
255
256 static void
257 data_source_target(void *data,
258                    struct wl_data_source *source, const char *mime_type)
259 {
260         struct dnd_drag *dnd_drag = data;
261         struct dnd *dnd = dnd_drag->dnd;
262         cairo_surface_t *surface;
263         struct wl_buffer *buffer;
264
265         dnd_drag->mime_type = mime_type;
266         if (mime_type)
267                 surface = dnd_drag->opaque;
268         else
269                 surface = dnd_drag->translucent;
270
271         buffer = display_get_buffer_for_surface(dnd->display, surface);
272         wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
273         wl_surface_damage(dnd_drag->drag_surface, 0, 0,
274                           dnd_drag->width, dnd_drag->height);
275         wl_surface_commit(dnd_drag->drag_surface);
276 }
277
278 static void
279 data_source_send(void *data, struct wl_data_source *source,
280                  const char *mime_type, int32_t fd)
281 {
282         struct dnd_flower_message dnd_flower_message;   
283         struct dnd_drag *dnd_drag = data;
284         char buffer[128];
285         int n;
286
287         if (strcmp(mime_type, flower_mime_type) == 0) {
288                 dnd_flower_message.seed = dnd_drag->item->seed;
289                 dnd_flower_message.x_offset = dnd_drag->x_offset;
290                 dnd_flower_message.y_offset = dnd_drag->y_offset;
291
292                 if (write(fd, &dnd_flower_message,
293                           sizeof dnd_flower_message) < 0)
294                         abort();
295         } else if (strcmp(mime_type, text_mime_type) == 0) {
296                 n = snprintf(buffer, sizeof buffer, "seed=%d x=%d y=%d\n",
297                              dnd_drag->item->seed,
298                              dnd_drag->x_offset,
299                              dnd_drag->y_offset);
300
301                 if (write(fd, buffer, n) < 0)
302                         abort();
303         }
304
305         close(fd);
306 }
307
308 static void
309 data_source_cancelled(void *data, struct wl_data_source *source)
310 {
311         struct dnd_drag *dnd_drag = data;
312
313         /* The 'cancelled' event means that the source is no longer in
314          * use by the drag (or current selection).  We need to clean
315          * up the drag object created and the local state. */
316
317         wl_data_source_destroy(dnd_drag->data_source);
318         
319         /* Destroy the item that has been dragged out */
320         cairo_surface_destroy(dnd_drag->item->surface);
321         free(dnd_drag->item);
322
323         wl_surface_destroy(dnd_drag->drag_surface);
324
325         cairo_surface_destroy(dnd_drag->translucent);
326         cairo_surface_destroy(dnd_drag->opaque);
327         free(dnd_drag);
328 }
329
330 static const struct wl_data_source_listener data_source_listener = {
331         data_source_target,
332         data_source_send,
333         data_source_cancelled
334 };
335
336 static cairo_surface_t *
337 create_drag_icon(struct dnd_drag *dnd_drag,
338                  struct item *item, int32_t x, int32_t y, double opacity)
339 {
340         struct dnd *dnd = dnd_drag->dnd;
341         cairo_surface_t *surface;
342         struct rectangle rectangle;
343         cairo_pattern_t *pattern;
344         cairo_t *cr;
345
346         rectangle.width = item_width;
347         rectangle.height = item_height;
348         surface = display_create_surface(dnd->display, NULL, &rectangle,
349                                          SURFACE_SHM);
350
351         cr = cairo_create(surface);
352         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
353         cairo_set_source_surface(cr, item->surface, 0, 0);
354         pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
355         cairo_mask(cr, pattern);
356         cairo_pattern_destroy(pattern);
357
358         cairo_destroy(cr);
359
360         dnd_drag->hotspot_x = x - item->x;
361         dnd_drag->hotspot_y = y - item->y;
362         dnd_drag->width = rectangle.width;
363         dnd_drag->height = rectangle.height;
364
365         return surface;
366 }
367
368 static int
369 create_drag_source(struct dnd *dnd,
370                 struct input *input, uint32_t time,
371                 int32_t x, int32_t y)
372 {
373         struct item *item;
374         struct rectangle allocation;
375         struct dnd_drag *dnd_drag;
376         struct display *display;
377         struct wl_compositor *compositor;
378         struct wl_buffer *buffer;
379         unsigned int i;
380         uint32_t serial;
381         cairo_surface_t *icon;
382
383         widget_get_allocation(dnd->widget, &allocation);
384         item = dnd_get_item(dnd, x, y);
385         x -= allocation.x;
386         y -= allocation.y;
387
388         if (item) {
389                 dnd_drag = xmalloc(sizeof *dnd_drag);
390                 dnd_drag->dnd = dnd;
391                 dnd_drag->input = input;
392                 dnd_drag->time = time;
393                 dnd_drag->item = item;
394                 dnd_drag->x_offset = x - item->x;
395                 dnd_drag->y_offset = y - item->y;
396
397                 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
398                         if (item == dnd->items[i]){
399                                 dnd->items[i] = 0;
400                                 break;
401                         }
402                 }
403
404                 display = window_get_display(dnd->window);
405                 compositor = display_get_compositor(display);
406                 serial = display_get_serial(display);
407                 dnd_drag->drag_surface =
408                         wl_compositor_create_surface(compositor);
409
410                 if (dnd->self_only) {
411                         dnd_drag->data_source = NULL;
412                 } else {
413                         dnd_drag->data_source =
414                                 display_create_data_source(dnd->display);
415                         wl_data_source_add_listener(dnd_drag->data_source,
416                                                     &data_source_listener,
417                                                     dnd_drag);
418                         wl_data_source_offer(dnd_drag->data_source,
419                                              flower_mime_type);
420                         wl_data_source_offer(dnd_drag->data_source,
421                                              text_mime_type);
422                 }
423
424                 wl_data_device_start_drag(input_get_data_device(input),
425                                           dnd_drag->data_source,
426                                           window_get_wl_surface(dnd->window),
427                                           dnd_drag->drag_surface,
428                                           serial);
429
430                 dnd_drag->opaque =
431                         create_drag_icon(dnd_drag, item, x, y, 1);
432                 dnd_drag->translucent =
433                         create_drag_icon(dnd_drag, item, x, y, 0.2);
434
435                 if (dnd->self_only)
436                         icon = dnd_drag->opaque;
437                 else
438                         icon = dnd_drag->translucent;
439
440                 buffer = display_get_buffer_for_surface(dnd->display, icon);
441                 wl_surface_attach(dnd_drag->drag_surface, buffer,
442                                   -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
443                 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
444                                   dnd_drag->width, dnd_drag->height);
445                 wl_surface_commit(dnd_drag->drag_surface);
446
447                 dnd->current_drag = dnd_drag;
448                 window_schedule_redraw(dnd->window);
449
450                 return 0;
451         } else
452                 return -1;
453 }
454
455 static void
456 dnd_button_handler(struct widget *widget,
457                    struct input *input, uint32_t time,
458                    uint32_t button, enum wl_pointer_button_state state,
459                    void *data)
460 {
461         struct dnd *dnd = data;
462         int32_t x, y;
463
464         input_get_position(input, &x, &y);
465         if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
466                 input_ungrab(input);
467                 if (create_drag_source(dnd, input, time, x, y) == 0)
468                         input_set_pointer_image(input, CURSOR_DRAGGING);
469         }
470 }
471
472 static void
473 dnd_touch_down_handler(struct widget *widget,
474                 struct input *input, uint32_t serial,
475                 uint32_t time, int32_t id,
476                 float x, float y, void *data)
477 {
478         struct dnd *dnd = data;
479         int32_t int_x, int_y;
480
481         if (id > 0)
482                 return;
483
484         int_x = (int32_t)x;
485         int_y = (int32_t)y;
486         if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
487                 touch_grab(input, 0);
488 }
489
490 static int
491 lookup_cursor(struct dnd *dnd, int x, int y)
492 {
493         struct item *item;
494
495         item = dnd_get_item(dnd, x, y);
496         if (item)
497                 return CURSOR_HAND1;
498         else
499                 return CURSOR_LEFT_PTR;
500 }
501
502 static int
503 dnd_enter_handler(struct widget *widget,
504                   struct input *input, float x, float y, void *data)
505 {
506         struct dnd *dnd = data;
507
508         dnd->current_drag = NULL;
509
510         return lookup_cursor(dnd, x, y);
511 }
512
513 static int
514 dnd_motion_handler(struct widget *widget,
515                    struct input *input, uint32_t time,
516                    float x, float y, void *data)
517 {
518         return lookup_cursor(data, x, y);
519 }
520
521 static void
522 dnd_data_handler(struct window *window,
523                  struct input *input,
524                  float x, float y, const char **types, void *data)
525 {
526         struct dnd *dnd = data;
527         int i, has_flower = 0;
528
529         if (!types)
530                 return;
531         for (i = 0; types[i]; i++)
532                 if (strcmp(types[i], flower_mime_type) == 0)
533                         has_flower = 1;
534
535         if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
536                 input_accept(input, NULL);
537         } else {
538                 input_accept(input, flower_mime_type);
539         }
540 }
541
542 static void
543 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
544 {
545         struct dnd *dnd = user_data;
546         struct dnd_flower_message *message = data;
547         struct item *item;
548         struct rectangle allocation;
549
550         if (len == 0) {
551                 return;
552         } else if (len != sizeof *message) {
553                 fprintf(stderr, "odd message length %zu, expected %zu\n",
554                         len, sizeof *message);
555                 return;
556         }
557                 
558         widget_get_allocation(dnd->widget, &allocation);
559         item = item_create(dnd->display,
560                            x - message->x_offset - allocation.x,
561                            y - message->y_offset - allocation.y,
562                            message->seed);
563
564         dnd_add_item(dnd, item);
565         window_schedule_redraw(dnd->window);
566 }
567
568 static void
569 dnd_drop_handler(struct window *window, struct input *input,
570                  int32_t x, int32_t y, void *data)
571 {
572         struct dnd *dnd = data;
573         struct dnd_flower_message message;
574
575         if (dnd_get_item(dnd, x, y)) {
576                 fprintf(stderr, "got 'drop', but no target\n");
577                 return;
578         }
579
580         if (!dnd->self_only) {
581                 input_receive_drag_data(input,
582                                         flower_mime_type,
583                                         dnd_receive_func, dnd);
584         } else if (dnd->current_drag) {
585                 message.seed = dnd->current_drag->item->seed;
586                 message.x_offset = dnd->current_drag->x_offset;
587                 message.y_offset = dnd->current_drag->y_offset;
588                 dnd_receive_func(&message, sizeof message, x, y, dnd);
589                 dnd->current_drag = NULL;
590         } else {
591                 fprintf(stderr, "ignoring drop from another client\n");
592         }
593 }
594
595 static struct dnd *
596 dnd_create(struct display *display)
597 {
598         struct dnd *dnd;
599         int x, y;
600         int32_t width, height;
601         unsigned int i;
602
603         dnd = xzalloc(sizeof *dnd);
604         dnd->window = window_create(display);
605         dnd->widget = window_frame_create(dnd->window, dnd);
606         window_set_title(dnd->window, "Wayland Drag and Drop Demo");
607
608         dnd->display = display;
609         dnd->key = 100;
610
611         for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
612                 x = (i % 4) * (item_width + item_padding) + item_padding;
613                 y = (i / 4) * (item_height + item_padding) + item_padding;
614                 if ((i ^ (i >> 2)) & 1)
615                         dnd->items[i] = item_create(display, x, y, 0);
616                 else
617                         dnd->items[i] = NULL;
618         }
619
620         window_set_user_data(dnd->window, dnd);
621         window_set_keyboard_focus_handler(dnd->window,
622                                           keyboard_focus_handler);
623         window_set_data_handler(dnd->window, dnd_data_handler);
624         window_set_drop_handler(dnd->window, dnd_drop_handler);
625
626         widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
627         widget_set_enter_handler(dnd->widget, dnd_enter_handler);
628         widget_set_motion_handler(dnd->widget, dnd_motion_handler);
629         widget_set_button_handler(dnd->widget, dnd_button_handler);
630         widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
631
632         width = 4 * (item_width + item_padding) + item_padding;
633         height = 4 * (item_height + item_padding) + item_padding;
634
635         window_frame_set_child_size(dnd->widget, width, height);
636
637         return dnd;
638 }
639
640 int
641 main(int argc, char *argv[])
642 {
643         struct display *d;
644         struct dnd *dnd;
645         int i;
646
647         d = display_create(&argc, argv);
648         if (d == NULL) {
649                 fprintf(stderr, "failed to create display: %m\n");
650                 return -1;
651         }
652
653         dnd = dnd_create(d);
654
655         for (i = 1; i < argc; i++)
656                 if (strcmp("--self-only", argv[i]) == 0)
657                         dnd->self_only = 1;
658
659         display_run(d);
660
661         return 0;
662 }