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>
35 #include <wayland-cursor.h>
38 #include "../shared/cairo-util.h"
43 struct window *window;
44 struct widget *widget;
45 struct display *display;
47 struct item *items[16];
49 struct dnd_drag *current_drag;
53 cairo_surface_t *translucent;
54 cairo_surface_t *opaque;
55 int hotspot_x, hotspot_y;
60 int x_offset, y_offset;
62 const char *mime_type;
64 struct wl_surface *drag_surface;
65 struct wl_data_source *data_source;
69 cairo_surface_t *surface;
74 struct dnd_flower_message {
75 int seed, x_offset, y_offset;
79 static const int item_width = 64;
80 static const int item_height = 64;
81 static const int item_padding = 16;
84 item_create(struct display *display, int x, int y, int seed)
89 item = malloc(sizeof *item);
94 gettimeofday(&tv, NULL);
95 item->seed = seed ? seed : tv.tv_usec;
98 const int petal_count = 3 + random() % 5;
99 const double r1 = 20 + random() % 10;
100 const double r2 = 5 + random() % 12;
101 const double u = (10 + random() % 90) / 100.0;
102 const double v = (random() % 90) / 100.0;
106 double t, dt = 2 * M_PI / (petal_count * 2);
107 double x1, y1, x2, y2, x3, y3;
108 struct rectangle rect;
111 rect.width = item_width;
112 rect.height = item_height;
114 display_create_surface(display, NULL, &rect, SURFACE_SHM);
119 cr = cairo_create(item->surface);
120 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
121 cairo_set_source_rgba(cr, 0, 0, 0, 0);
124 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
125 cairo_translate(cr, item_width / 2, item_height / 2);
127 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
128 for (i = 0; i < petal_count; i++, t += dt * 2) {
131 x2 = cos(t + dt) * r2;
132 y2 = sin(t + dt) * r2;
133 x3 = cos(t + 2 * dt) * r1;
134 y3 = sin(t + 2 * dt) * r1;
137 x1 - y1 * u, y1 + x1 * u,
138 x2 + y2 * v, y2 - x2 * v,
142 x2 - y2 * v, y2 + x2 * v,
143 x3 + y3 * u, y3 - x3 * u,
147 cairo_close_path(cr);
149 cairo_set_source_rgba(cr,
150 0.5 + (random() % 50) / 49.0,
151 0.5 + (random() % 50) / 49.0,
152 0.5 + (random() % 50) / 49.0,
153 0.5 + (random() % 100) / 99.0);
155 cairo_fill_preserve(cr);
157 cairo_set_line_width(cr, 1);
158 cairo_set_source_rgba(cr,
159 0.5 + (random() % 50) / 49.0,
160 0.5 + (random() % 50) / 49.0,
161 0.5 + (random() % 50) / 49.0,
162 0.5 + (random() % 100) / 99.0);
171 dnd_redraw_handler(struct widget *widget, void *data)
173 struct dnd *dnd = data;
174 struct rectangle allocation;
176 cairo_surface_t *surface;
179 surface = window_get_surface(dnd->window);
180 cr = cairo_create(surface);
181 widget_get_allocation(dnd->widget, &allocation);
182 cairo_rectangle(cr, allocation.x, allocation.y,
183 allocation.width, allocation.height);
185 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
186 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
189 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
190 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
193 cairo_set_source_surface(cr, dnd->items[i]->surface,
194 dnd->items[i]->x + allocation.x,
195 dnd->items[i]->y + allocation.y);
200 cairo_surface_destroy(surface);
204 keyboard_focus_handler(struct window *window,
205 struct input *device, void *data)
207 struct dnd *dnd = data;
209 window_schedule_redraw(dnd->window);
213 dnd_add_item(struct dnd *dnd, struct item *item)
217 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
218 if (dnd->items[i] == 0) {
219 dnd->items[i] = item;
227 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
230 struct rectangle allocation;
233 widget_get_allocation(dnd->widget, &allocation);
238 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
239 item = dnd->items[i];
241 item->x <= x && x < item->x + item_width &&
242 item->y <= y && y < item->y + item_height)
250 data_source_target(void *data,
251 struct wl_data_source *source, const char *mime_type)
253 struct dnd_drag *dnd_drag = data;
254 struct dnd *dnd = dnd_drag->dnd;
255 cairo_surface_t *surface;
256 struct wl_buffer *buffer;
258 dnd_drag->mime_type = mime_type;
260 surface = dnd_drag->opaque;
262 surface = dnd_drag->translucent;
264 buffer = display_get_buffer_for_surface(dnd->display, surface);
265 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
266 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
267 dnd_drag->width, dnd_drag->height);
271 data_source_send(void *data, struct wl_data_source *source,
272 const char *mime_type, int32_t fd)
274 struct dnd_flower_message dnd_flower_message;
275 struct dnd_drag *dnd_drag = data;
277 dnd_flower_message.seed = dnd_drag->item->seed;
278 dnd_flower_message.x_offset = dnd_drag->x_offset;
279 dnd_flower_message.y_offset = dnd_drag->y_offset;
281 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
288 data_source_cancelled(void *data, struct wl_data_source *source)
290 struct dnd_drag *dnd_drag = data;
292 /* The 'cancelled' event means that the source is no longer in
293 * use by the drag (or current selection). We need to clean
294 * up the drag object created and the local state. */
296 wl_data_source_destroy(dnd_drag->data_source);
298 /* Destroy the item that has been dragged out */
299 cairo_surface_destroy(dnd_drag->item->surface);
300 free(dnd_drag->item);
302 wl_surface_destroy(dnd_drag->drag_surface);
304 cairo_surface_destroy(dnd_drag->translucent);
305 cairo_surface_destroy(dnd_drag->opaque);
309 static const struct wl_data_source_listener data_source_listener = {
312 data_source_cancelled
315 static cairo_surface_t *
316 create_drag_cursor(struct dnd_drag *dnd_drag,
317 struct item *item, int32_t x, int32_t y, double opacity)
319 struct dnd *dnd = dnd_drag->dnd;
320 cairo_surface_t *surface;
321 struct wl_cursor_image *pointer;
322 struct rectangle rectangle;
323 cairo_pattern_t *pattern;
326 pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
328 rectangle.width = item_width + 2 * pointer->width;
329 rectangle.height = item_height + 2 * pointer->height;
330 surface = display_create_surface(dnd->display, NULL, &rectangle,
333 cr = cairo_create(surface);
334 cairo_translate(cr, pointer->width, pointer->height);
336 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
337 cairo_set_source_rgba(cr, 0, 0, 0, 0);
340 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
341 cairo_set_source_surface(cr, item->surface, 0, 0);
342 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
343 cairo_mask(cr, pattern);
344 cairo_pattern_destroy(pattern);
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;
352 dnd_drag->width = rectangle.width;
353 dnd_drag->height = rectangle.height;
359 dnd_button_handler(struct widget *widget,
360 struct input *input, uint32_t time,
361 uint32_t button, uint32_t state, void *data)
363 struct dnd *dnd = data;
366 struct rectangle allocation;
367 struct dnd_drag *dnd_drag;
368 struct display *display;
369 struct wl_compositor *compositor;
370 struct wl_buffer *buffer;
373 cairo_surface_t *icon;
375 widget_get_allocation(dnd->widget, &allocation);
376 input_get_position(input, &x, &y);
377 item = dnd_get_item(dnd, x, y);
381 if (item && state == 1) {
382 dnd_drag = malloc(sizeof *dnd_drag);
384 dnd_drag->input = input;
385 dnd_drag->time = time;
386 dnd_drag->item = item;
387 dnd_drag->x_offset = x - item->x;
388 dnd_drag->y_offset = y - item->y;
390 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
391 if (item == dnd->items[i]){
397 display = window_get_display(dnd->window);
398 compositor = display_get_compositor(display);
399 serial = display_get_serial(display);
400 dnd_drag->drag_surface =
401 wl_compositor_create_surface(compositor);
405 if (dnd->self_only) {
406 dnd_drag->data_source = NULL;
408 dnd_drag->data_source =
409 display_create_data_source(dnd->display);
410 wl_data_source_add_listener(dnd_drag->data_source,
411 &data_source_listener,
413 wl_data_source_offer(dnd_drag->data_source,
414 "application/x-wayland-dnd-flower");
415 wl_data_source_offer(dnd_drag->data_source,
416 "text/plain; charset=utf-8");
419 wl_data_device_start_drag(input_get_data_device(input),
420 dnd_drag->data_source,
421 window_get_wl_surface(dnd->window),
422 dnd_drag->drag_surface,
425 input_set_pointer_image(input, time, CURSOR_DRAGGING);
428 create_drag_cursor(dnd_drag, item, x, y, 1);
429 dnd_drag->translucent =
430 create_drag_cursor(dnd_drag, item, x, y, 0.2);
433 icon = dnd_drag->opaque;
435 icon = dnd_drag->translucent;
437 buffer = display_get_buffer_for_surface(dnd->display, icon);
438 wl_surface_attach(dnd_drag->drag_surface, buffer,
439 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
440 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
441 dnd_drag->width, dnd_drag->height);
443 dnd->current_drag = dnd_drag;
444 window_schedule_redraw(dnd->window);
449 lookup_cursor(struct dnd *dnd, int x, int y)
453 item = dnd_get_item(dnd, x, y);
457 return CURSOR_LEFT_PTR;
461 dnd_enter_handler(struct widget *widget,
462 struct input *input, float x, float y, void *data)
464 struct dnd *dnd = data;
466 dnd->current_drag = NULL;
468 return lookup_cursor(dnd, x, y);
472 dnd_motion_handler(struct widget *widget,
473 struct input *input, uint32_t time,
474 float x, float y, void *data)
476 return lookup_cursor(data, x, y);
480 dnd_data_handler(struct window *window,
482 float x, float y, const char **types, void *data)
484 struct dnd *dnd = data;
489 if (dnd_get_item(dnd, x, y) || dnd->self_only) {
490 input_accept(input, NULL);
492 input_accept(input, types[0]);
497 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
499 struct dnd *dnd = user_data;
500 struct dnd_flower_message *message = data;
502 struct rectangle allocation;
506 } else if (len != sizeof *message) {
507 fprintf(stderr, "odd message length %ld, expected %ld\n",
508 len, sizeof *message);
512 widget_get_allocation(dnd->widget, &allocation);
513 item = item_create(dnd->display,
514 x - message->x_offset - allocation.x,
515 y - message->y_offset - allocation.y,
518 dnd_add_item(dnd, item);
519 window_schedule_redraw(dnd->window);
523 dnd_drop_handler(struct window *window, struct input *input,
524 int32_t x, int32_t y, void *data)
526 struct dnd *dnd = data;
527 struct dnd_flower_message message;
529 if (dnd_get_item(dnd, x, y)) {
530 fprintf(stderr, "got 'drop', but no target\n");
534 if (!dnd->self_only) {
535 input_receive_drag_data(input,
536 "application/x-wayland-dnd-flower",
537 dnd_receive_func, dnd);
538 } else if (dnd->current_drag) {
539 message.seed = dnd->current_drag->item->seed;
540 message.x_offset = dnd->current_drag->x_offset;
541 message.y_offset = dnd->current_drag->y_offset;
542 dnd_receive_func(&message, sizeof message, x, y, dnd);
543 dnd->current_drag = NULL;
545 fprintf(stderr, "ignoring drop from another client\n");
550 dnd_create(struct display *display)
554 int32_t width, height;
557 dnd = malloc(sizeof *dnd);
560 memset(dnd, 0, sizeof *dnd);
562 dnd->window = window_create(display);
563 dnd->widget = frame_create(dnd->window, dnd);
564 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
566 dnd->display = display;
569 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
570 x = (i % 4) * (item_width + item_padding) + item_padding;
571 y = (i / 4) * (item_height + item_padding) + item_padding;
572 if ((i ^ (i >> 2)) & 1)
573 dnd->items[i] = item_create(display, x, y, 0);
575 dnd->items[i] = NULL;
578 window_set_user_data(dnd->window, dnd);
579 window_set_keyboard_focus_handler(dnd->window,
580 keyboard_focus_handler);
581 window_set_data_handler(dnd->window, dnd_data_handler);
582 window_set_drop_handler(dnd->window, dnd_drop_handler);
584 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
585 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
586 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
587 widget_set_button_handler(dnd->widget, dnd_button_handler);
589 width = 4 * (item_width + item_padding) + item_padding;
590 height = 4 * (item_height + item_padding) + item_padding;
592 widget_schedule_resize(dnd->widget, width, height);
598 main(int argc, char *argv[])
604 d = display_create(argc, argv);
606 fprintf(stderr, "failed to create display: %m\n");
612 for (i = 1; i < argc; i++)
613 if (strcmp("--self-only", argv[i]) == 0)