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;
251 dnd_drag->mime_type = mime_type;
253 surface = dnd_drag->opaque;
255 surface = dnd_drag->translucent;
257 buffer = display_get_buffer_for_surface(dnd->display, surface);
258 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
262 data_source_send(void *data, struct wl_data_source *source,
263 const char *mime_type, int32_t fd)
265 struct dnd_flower_message dnd_flower_message;
266 struct dnd_drag *dnd_drag = data;
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;
272 write(fd, &dnd_flower_message, sizeof dnd_flower_message);
277 data_source_cancelled(void *data, struct wl_data_source *source)
279 struct dnd_drag *dnd_drag = data;
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. */
285 wl_data_source_destroy(dnd_drag->data_source);
287 /* Destroy the item that has been dragged out */
288 cairo_surface_destroy(dnd_drag->item->surface);
289 free(dnd_drag->item);
291 wl_surface_destroy(dnd_drag->drag_surface);
293 cairo_surface_destroy(dnd_drag->translucent);
294 cairo_surface_destroy(dnd_drag->opaque);
298 static const struct wl_data_source_listener data_source_listener = {
301 data_source_cancelled
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)
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;
315 pointer = display_get_pointer_surface(dnd->display,
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);
326 cr = cairo_create(surface);
327 cairo_translate(cr, pointer_width, pointer_height);
329 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
330 cairo_set_source_rgba(cr, 0, 0, 0, 0);
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);
339 cairo_set_source_surface(cr, pointer,
340 x - item->x - hotspot_x,
341 y - item->y - hotspot_y);
342 cairo_surface_destroy(pointer);
344 /* FIXME: more cairo-gl brokeness */
345 surface_flush_device(surface);
348 dnd_drag->hotspot_x = pointer_width + x - item->x;
349 dnd_drag->hotspot_y = pointer_height + y - item->y;
355 dnd_button_handler(struct widget *widget,
356 struct input *input, uint32_t time,
357 int button, int state, void *data)
359 struct dnd *dnd = data;
362 struct rectangle allocation;
363 struct dnd_drag *dnd_drag;
364 struct display *display;
365 struct wl_compositor *compositor;
366 struct wl_buffer *buffer;
369 widget_get_allocation(dnd->widget, &allocation);
370 input_get_position(input, &x, &y);
371 item = dnd_get_item(dnd, x, y);
375 if (item && state == 1) {
376 dnd_drag = malloc(sizeof *dnd_drag);
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;
384 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
385 if (item == dnd->items[i]){
391 display = window_get_display(dnd->window);
392 compositor = display_get_compositor(display);
393 dnd_drag->drag_surface =
394 wl_compositor_create_surface(compositor);
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,
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,
411 input_set_pointer_image(input, time, POINTER_DRAGGING);
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);
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);
422 window_schedule_redraw(dnd->window);
427 lookup_cursor(struct dnd *dnd, int x, int y)
431 item = dnd_get_item(dnd, x, y);
433 return POINTER_HAND1;
435 return POINTER_LEFT_PTR;
439 dnd_enter_handler(struct widget *widget,
440 struct input *input, uint32_t time,
441 int32_t x, int32_t y, void *data)
443 return lookup_cursor(data, x, y);
447 dnd_motion_handler(struct widget *widget,
448 struct input *input, uint32_t time,
449 int32_t x, int32_t y, void *data)
451 return lookup_cursor(data, x, y);
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)
459 struct dnd *dnd = data;
461 if (!dnd_get_item(dnd, x, y)) {
462 input_accept(input, time, types[0]);
464 input_accept(input, time, NULL);
469 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
471 struct dnd *dnd = user_data;
472 struct dnd_flower_message *message = data;
474 struct rectangle allocation;
478 } else if (len != sizeof *message) {
479 fprintf(stderr, "odd message length %ld, expected %ld\n",
480 len, sizeof *message);
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,
490 dnd_add_item(dnd, item);
491 window_schedule_redraw(dnd->window);
495 dnd_drop_handler(struct window *window, struct input *input,
496 int32_t x, int32_t y, void *data)
498 struct dnd *dnd = data;
500 if (dnd_get_item(dnd, x, y)) {
501 fprintf(stderr, "got 'drop', but no target\n");
505 input_receive_drag_data(input,
506 "application/x-wayland-dnd-flower",
507 dnd_receive_func, dnd);
511 dnd_create(struct display *display)
515 int32_t width, height;
517 dnd = malloc(sizeof *dnd);
520 memset(dnd, 0, sizeof *dnd);
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");
526 dnd->display = display;
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);
535 dnd->items[i] = NULL;
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);
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);
549 width = 4 * (item_width + item_padding) + item_padding;
550 height = 4 * (item_height + item_padding) + item_padding;
552 widget_schedule_resize(dnd->widget, width, height);
558 main(int argc, char *argv[])
562 d = display_create(argc, argv);
564 fprintf(stderr, "failed to create display: %m\n");