2 * Copyright © 2010 Kristian Høgsberg
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.
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
32 #include <sys/epoll.h>
34 #include <wayland-client.h>
37 #include "cairo-util.h"
40 struct window *window;
41 struct widget *widget;
42 struct display *display;
44 struct item *items[16];
48 cairo_surface_t *translucent;
49 cairo_surface_t *opaque;
50 int hotspot_x, hotspot_y;
55 int x_offset, y_offset;
56 const char *mime_type;
58 struct wl_surface *drag_surface;
59 struct wl_data_source *data_source;
63 cairo_surface_t *surface;
68 struct dnd_flower_message {
69 int seed, x_offset, y_offset;
73 static const int item_width = 64;
74 static const int item_height = 64;
75 static const int item_padding = 16;
78 item_create(struct display *display, int x, int y, int seed)
83 item = malloc(sizeof *item);
88 gettimeofday(&tv, NULL);
89 item->seed = seed ? seed : tv.tv_usec;
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;
100 double t, dt = 2 * M_PI / (petal_count * 2);
101 double x1, y1, x2, y2, x3, y3;
102 struct rectangle rect;
105 rect.width = item_width;
106 rect.height = item_height;
107 item->surface = display_create_surface(display, NULL, &rect, 0);
112 cr = cairo_create(item->surface);
113 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
114 cairo_set_source_rgba(cr, 0, 0, 0, 0);
117 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
118 cairo_translate(cr, item_width / 2, item_height / 2);
120 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
121 for (i = 0; i < petal_count; i++, t += dt * 2) {
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;
130 x1 - y1 * u, y1 + x1 * u,
131 x2 + y2 * v, y2 - x2 * v,
135 x2 - y2 * v, y2 + x2 * v,
136 x3 + y3 * u, y3 - x3 * u,
140 cairo_close_path(cr);
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);
148 cairo_fill_preserve(cr);
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);
164 dnd_redraw_handler(struct widget *widget, void *data)
166 struct dnd *dnd = data;
167 struct rectangle allocation;
169 cairo_surface_t *surface;
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);
178 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
179 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
182 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
183 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
186 cairo_set_source_surface(cr, dnd->items[i]->surface,
187 dnd->items[i]->x + allocation.x,
188 dnd->items[i]->y + allocation.y);
193 cairo_surface_destroy(surface);
197 keyboard_focus_handler(struct window *window,
198 struct input *device, void *data)
200 struct dnd *dnd = data;
202 window_schedule_redraw(dnd->window);
206 dnd_add_item(struct dnd *dnd, struct item *item)
210 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
211 if (dnd->items[i] == 0) {
212 dnd->items[i] = item;
220 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
223 struct rectangle allocation;
226 widget_get_allocation(dnd->widget, &allocation);
231 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
232 item = dnd->items[i];
234 item->x <= x && x < item->x + item_width &&
235 item->y <= y && y < item->y + item_height)
243 data_source_target(void *data,
244 struct wl_data_source *source, const char *mime_type)
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;
252 device = input_get_data_device(dnd_drag->input);
253 dnd_drag->mime_type = mime_type;
255 surface = dnd_drag->opaque;
257 surface = dnd_drag->translucent;
259 buffer = display_get_buffer_for_surface(dnd->display, surface);
260 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
264 data_source_send(void *data, struct wl_data_source *source,
265 const char *mime_type, int32_t fd)
267 struct dnd_flower_message dnd_flower_message;
268 struct dnd_drag *dnd_drag = data;
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;
274 write(fd, &dnd_flower_message, sizeof dnd_flower_message);
279 data_source_cancelled(void *data, struct wl_data_source *source)
281 struct dnd_drag *dnd_drag = data;
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. */
287 wl_data_source_destroy(dnd_drag->data_source);
289 /* Destroy the item that has been dragged out */
290 cairo_surface_destroy(dnd_drag->item->surface);
291 free(dnd_drag->item);
293 wl_surface_destroy(dnd_drag->drag_surface);
295 cairo_surface_destroy(dnd_drag->translucent);
296 cairo_surface_destroy(dnd_drag->opaque);
300 static const struct wl_data_source_listener data_source_listener = {
303 data_source_cancelled
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)
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;
317 pointer = display_get_pointer_surface(dnd->display,
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);
328 cr = cairo_create(surface);
329 cairo_translate(cr, pointer_width, pointer_height);
331 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
332 cairo_set_source_rgba(cr, 0, 0, 0, 0);
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);
341 cairo_set_source_surface(cr, pointer,
342 x - item->x - hotspot_x,
343 y - item->y - hotspot_y);
344 cairo_surface_destroy(pointer);
346 /* FIXME: more cairo-gl brokeness */
347 surface_flush_device(surface);
350 dnd_drag->hotspot_x = pointer_width + x - item->x;
351 dnd_drag->hotspot_y = pointer_height + y - item->y;
357 dnd_button_handler(struct widget *widget,
358 struct input *input, uint32_t time,
359 int button, int state, void *data)
361 struct dnd *dnd = data;
364 struct rectangle allocation;
365 struct dnd_drag *dnd_drag;
366 struct display *display;
367 struct wl_compositor *compositor;
368 struct wl_buffer *buffer;
371 widget_get_allocation(dnd->widget, &allocation);
372 input_get_position(input, &x, &y);
373 item = dnd_get_item(dnd, x, y);
377 if (item && state == 1) {
378 dnd_drag = malloc(sizeof *dnd_drag);
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;
386 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
387 if (item == dnd->items[i]){
393 display = window_get_display(dnd->window);
394 compositor = display_get_compositor(display);
395 dnd_drag->drag_surface =
396 wl_compositor_create_surface(compositor);
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,
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,
413 input_set_pointer_image(input, time, POINTER_DRAGGING);
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);
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);
424 window_schedule_redraw(dnd->window);
429 lookup_cursor(struct dnd *dnd, int x, int y)
433 item = dnd_get_item(dnd, x, y);
435 return POINTER_HAND1;
437 return POINTER_LEFT_PTR;
441 dnd_enter_handler(struct widget *widget,
442 struct input *input, uint32_t time,
443 int32_t x, int32_t y, void *data)
445 return lookup_cursor(data, x, y);
449 dnd_motion_handler(struct widget *widget,
450 struct input *input, uint32_t time,
451 int32_t x, int32_t y, void *data)
453 return lookup_cursor(data, x, y);
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)
461 struct dnd *dnd = data;
463 if (!dnd_get_item(dnd, x, y)) {
464 input_accept(input, time, types[0]);
466 input_accept(input, time, NULL);
471 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
473 struct dnd *dnd = user_data;
474 struct dnd_flower_message *message = data;
476 struct rectangle allocation;
480 } else if (len != sizeof *message) {
481 fprintf(stderr, "odd message length %ld, expected %ld\n",
482 len, sizeof *message);
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,
492 dnd_add_item(dnd, item);
493 window_schedule_redraw(dnd->window);
497 dnd_drop_handler(struct window *window, struct input *input,
498 int32_t x, int32_t y, void *data)
500 struct dnd *dnd = data;
502 if (dnd_get_item(dnd, x, y)) {
503 fprintf(stderr, "got 'drop', but no target\n");
507 input_receive_drag_data(input,
508 "application/x-wayland-dnd-flower",
509 dnd_receive_func, dnd);
513 dnd_create(struct display *display)
517 int32_t width, height;
519 dnd = malloc(sizeof *dnd);
522 memset(dnd, 0, sizeof *dnd);
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");
528 dnd->display = display;
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);
537 dnd->items[i] = NULL;
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);
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);
551 width = 4 * (item_width + item_padding) + item_padding;
552 height = 4 * (item_height + item_padding) + item_padding;
554 widget_schedule_resize(dnd->widget, width, height);
560 main(int argc, char *argv[])
564 d = display_create(argc, argv);
566 fprintf(stderr, "failed to create display: %m\n");