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
35 #include <sys/epoll.h>
37 #include <wayland-client.h>
38 #include <wayland-cursor.h>
41 #include "../shared/cairo-util.h"
46 struct window *window;
47 struct widget *widget;
48 struct display *display;
50 struct item *items[16];
52 struct dnd_drag *current_drag;
56 cairo_surface_t *translucent;
57 cairo_surface_t *opaque;
58 int hotspot_x, hotspot_y;
63 int x_offset, y_offset;
65 const char *mime_type;
67 struct wl_surface *drag_surface;
68 struct wl_data_source *data_source;
72 cairo_surface_t *surface;
77 struct dnd_flower_message {
78 int seed, x_offset, y_offset;
82 static const int item_width = 64;
83 static const int item_height = 64;
84 static const int item_padding = 16;
86 static const char flower_mime_type[] = "application/x-wayland-dnd-flower";
87 static const char text_mime_type[] = "text/plain;charset=utf-8";
90 item_create(struct display *display, int x, int y, int seed)
95 item = malloc(sizeof *item);
100 gettimeofday(&tv, NULL);
101 item->seed = seed ? seed : tv.tv_usec;
104 const int petal_count = 3 + random() % 5;
105 const double r1 = 20 + random() % 10;
106 const double r2 = 5 + random() % 12;
107 const double u = (10 + random() % 90) / 100.0;
108 const double v = (random() % 90) / 100.0;
112 double t, dt = 2 * M_PI / (petal_count * 2);
113 double x1, y1, x2, y2, x3, y3;
114 struct rectangle rect;
117 rect.width = item_width;
118 rect.height = item_height;
120 display_create_surface(display, NULL, &rect, SURFACE_SHM);
125 cr = cairo_create(item->surface);
126 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
127 cairo_set_source_rgba(cr, 0, 0, 0, 0);
130 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
131 cairo_translate(cr, item_width / 2, item_height / 2);
133 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
134 for (i = 0; i < petal_count; i++, t += dt * 2) {
137 x2 = cos(t + dt) * r2;
138 y2 = sin(t + dt) * r2;
139 x3 = cos(t + 2 * dt) * r1;
140 y3 = sin(t + 2 * dt) * r1;
143 x1 - y1 * u, y1 + x1 * u,
144 x2 + y2 * v, y2 - x2 * v,
148 x2 - y2 * v, y2 + x2 * v,
149 x3 + y3 * u, y3 - x3 * u,
153 cairo_close_path(cr);
155 cairo_set_source_rgba(cr,
156 0.5 + (random() % 50) / 49.0,
157 0.5 + (random() % 50) / 49.0,
158 0.5 + (random() % 50) / 49.0,
159 0.5 + (random() % 100) / 99.0);
161 cairo_fill_preserve(cr);
163 cairo_set_line_width(cr, 1);
164 cairo_set_source_rgba(cr,
165 0.5 + (random() % 50) / 49.0,
166 0.5 + (random() % 50) / 49.0,
167 0.5 + (random() % 50) / 49.0,
168 0.5 + (random() % 100) / 99.0);
177 dnd_redraw_handler(struct widget *widget, void *data)
179 struct dnd *dnd = data;
180 struct rectangle allocation;
182 cairo_surface_t *surface;
185 surface = window_get_surface(dnd->window);
186 cr = cairo_create(surface);
187 widget_get_allocation(dnd->widget, &allocation);
188 cairo_rectangle(cr, allocation.x, allocation.y,
189 allocation.width, allocation.height);
191 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
192 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
195 cairo_rectangle(cr, allocation.x, allocation.y,
196 allocation.width, allocation.height);
198 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
199 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
202 cairo_set_source_surface(cr, dnd->items[i]->surface,
203 dnd->items[i]->x + allocation.x,
204 dnd->items[i]->y + allocation.y);
209 cairo_surface_destroy(surface);
213 keyboard_focus_handler(struct window *window,
214 struct input *device, void *data)
216 struct dnd *dnd = data;
218 window_schedule_redraw(dnd->window);
222 dnd_add_item(struct dnd *dnd, struct item *item)
226 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
227 if (dnd->items[i] == 0) {
228 dnd->items[i] = item;
236 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
239 struct rectangle allocation;
242 widget_get_allocation(dnd->widget, &allocation);
247 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
248 item = dnd->items[i];
250 item->x <= x && x < item->x + item_width &&
251 item->y <= y && y < item->y + item_height)
259 data_source_target(void *data,
260 struct wl_data_source *source, const char *mime_type)
262 struct dnd_drag *dnd_drag = data;
263 struct dnd *dnd = dnd_drag->dnd;
264 cairo_surface_t *surface;
265 struct wl_buffer *buffer;
267 dnd_drag->mime_type = mime_type;
269 surface = dnd_drag->opaque;
271 surface = dnd_drag->translucent;
273 buffer = display_get_buffer_for_surface(dnd->display, surface);
274 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
275 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
276 dnd_drag->width, dnd_drag->height);
277 wl_surface_commit(dnd_drag->drag_surface);
281 data_source_send(void *data, struct wl_data_source *source,
282 const char *mime_type, int32_t fd)
284 struct dnd_flower_message dnd_flower_message;
285 struct dnd_drag *dnd_drag = data;
289 if (strcmp(mime_type, flower_mime_type) == 0) {
290 dnd_flower_message.seed = dnd_drag->item->seed;
291 dnd_flower_message.x_offset = dnd_drag->x_offset;
292 dnd_flower_message.y_offset = dnd_drag->y_offset;
294 if (write(fd, &dnd_flower_message,
295 sizeof dnd_flower_message) < 0)
297 } else if (strcmp(mime_type, text_mime_type) == 0) {
298 n = snprintf(buffer, sizeof buffer, "seed=%d x=%d y=%d\n",
299 dnd_drag->item->seed,
303 if (write(fd, buffer, n) < 0)
311 data_source_cancelled(void *data, struct wl_data_source *source)
313 struct dnd_drag *dnd_drag = data;
315 /* The 'cancelled' event means that the source is no longer in
316 * use by the drag (or current selection). We need to clean
317 * up the drag object created and the local state. */
319 wl_data_source_destroy(dnd_drag->data_source);
321 /* Destroy the item that has been dragged out */
322 cairo_surface_destroy(dnd_drag->item->surface);
323 free(dnd_drag->item);
325 wl_surface_destroy(dnd_drag->drag_surface);
327 cairo_surface_destroy(dnd_drag->translucent);
328 cairo_surface_destroy(dnd_drag->opaque);
332 static const struct wl_data_source_listener data_source_listener = {
335 data_source_cancelled
338 static cairo_surface_t *
339 create_drag_icon(struct dnd_drag *dnd_drag,
340 struct item *item, int32_t x, int32_t y, double opacity)
342 struct dnd *dnd = dnd_drag->dnd;
343 cairo_surface_t *surface;
344 struct rectangle rectangle;
345 cairo_pattern_t *pattern;
348 rectangle.width = item_width;
349 rectangle.height = item_height;
350 surface = display_create_surface(dnd->display, NULL, &rectangle,
353 cr = cairo_create(surface);
354 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
355 cairo_set_source_surface(cr, item->surface, 0, 0);
356 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
357 cairo_mask(cr, pattern);
358 cairo_pattern_destroy(pattern);
362 dnd_drag->hotspot_x = x - item->x;
363 dnd_drag->hotspot_y = y - item->y;
364 dnd_drag->width = rectangle.width;
365 dnd_drag->height = rectangle.height;
371 create_drag_source(struct dnd *dnd,
372 struct input *input, uint32_t time,
373 int32_t x, int32_t y)
376 struct rectangle allocation;
377 struct dnd_drag *dnd_drag;
378 struct display *display;
379 struct wl_compositor *compositor;
380 struct wl_buffer *buffer;
383 cairo_surface_t *icon;
385 widget_get_allocation(dnd->widget, &allocation);
386 item = dnd_get_item(dnd, x, y);
391 dnd_drag = xmalloc(sizeof *dnd_drag);
393 dnd_drag->input = input;
394 dnd_drag->time = time;
395 dnd_drag->item = item;
396 dnd_drag->x_offset = x - item->x;
397 dnd_drag->y_offset = y - item->y;
399 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
400 if (item == dnd->items[i]){
406 display = window_get_display(dnd->window);
407 compositor = display_get_compositor(display);
408 serial = display_get_serial(display);
409 dnd_drag->drag_surface =
410 wl_compositor_create_surface(compositor);
412 if (dnd->self_only) {
413 dnd_drag->data_source = NULL;
415 dnd_drag->data_source =
416 display_create_data_source(dnd->display);
417 wl_data_source_add_listener(dnd_drag->data_source,
418 &data_source_listener,
420 wl_data_source_offer(dnd_drag->data_source,
422 wl_data_source_offer(dnd_drag->data_source,
426 wl_data_device_start_drag(input_get_data_device(input),
427 dnd_drag->data_source,
428 window_get_wl_surface(dnd->window),
429 dnd_drag->drag_surface,
433 create_drag_icon(dnd_drag, item, x, y, 1);
434 dnd_drag->translucent =
435 create_drag_icon(dnd_drag, item, x, y, 0.2);
438 icon = dnd_drag->opaque;
440 icon = dnd_drag->translucent;
442 buffer = display_get_buffer_for_surface(dnd->display, icon);
443 wl_surface_attach(dnd_drag->drag_surface, buffer,
444 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
445 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
446 dnd_drag->width, dnd_drag->height);
447 wl_surface_commit(dnd_drag->drag_surface);
449 dnd->current_drag = dnd_drag;
450 window_schedule_redraw(dnd->window);
458 dnd_button_handler(struct widget *widget,
459 struct input *input, uint32_t time,
460 uint32_t button, enum wl_pointer_button_state state,
463 struct dnd *dnd = data;
466 input_get_position(input, &x, &y);
467 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
469 if (create_drag_source(dnd, input, time, x, y) == 0)
470 input_set_pointer_image(input, CURSOR_DRAGGING);
475 dnd_touch_down_handler(struct widget *widget,
476 struct input *input, uint32_t serial,
477 uint32_t time, int32_t id,
478 float x, float y, void *data)
480 struct dnd *dnd = data;
481 int32_t int_x, int_y;
488 if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
489 touch_grab(input, 0);
493 lookup_cursor(struct dnd *dnd, int x, int y)
497 item = dnd_get_item(dnd, x, y);
501 return CURSOR_LEFT_PTR;
505 dnd_enter_handler(struct widget *widget,
506 struct input *input, float x, float y, void *data)
508 struct dnd *dnd = data;
510 dnd->current_drag = NULL;
512 return lookup_cursor(dnd, x, y);
516 dnd_motion_handler(struct widget *widget,
517 struct input *input, uint32_t time,
518 float x, float y, void *data)
520 return lookup_cursor(data, x, y);
524 dnd_data_handler(struct window *window,
526 float x, float y, const char **types, void *data)
528 struct dnd *dnd = data;
529 int i, has_flower = 0;
533 for (i = 0; types[i]; i++)
534 if (strcmp(types[i], flower_mime_type) == 0)
537 if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
538 input_accept(input, NULL);
540 input_accept(input, flower_mime_type);
545 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
547 struct dnd *dnd = user_data;
548 struct dnd_flower_message *message = data;
550 struct rectangle allocation;
554 } else if (len != sizeof *message) {
555 fprintf(stderr, "odd message length %zu, expected %zu\n",
556 len, sizeof *message);
560 widget_get_allocation(dnd->widget, &allocation);
561 item = item_create(dnd->display,
562 x - message->x_offset - allocation.x,
563 y - message->y_offset - allocation.y,
566 dnd_add_item(dnd, item);
567 window_schedule_redraw(dnd->window);
571 dnd_drop_handler(struct window *window, struct input *input,
572 int32_t x, int32_t y, void *data)
574 struct dnd *dnd = data;
575 struct dnd_flower_message message;
577 if (dnd_get_item(dnd, x, y)) {
578 fprintf(stderr, "got 'drop', but no target\n");
582 if (!dnd->self_only) {
583 input_receive_drag_data(input,
585 dnd_receive_func, dnd);
586 } else if (dnd->current_drag) {
587 message.seed = dnd->current_drag->item->seed;
588 message.x_offset = dnd->current_drag->x_offset;
589 message.y_offset = dnd->current_drag->y_offset;
590 dnd_receive_func(&message, sizeof message, x, y, dnd);
591 dnd->current_drag = NULL;
593 fprintf(stderr, "ignoring drop from another client\n");
598 dnd_create(struct display *display)
602 int32_t width, height;
605 dnd = xzalloc(sizeof *dnd);
606 dnd->window = window_create(display);
607 dnd->widget = window_frame_create(dnd->window, dnd);
608 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
610 dnd->display = display;
613 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
614 x = (i % 4) * (item_width + item_padding) + item_padding;
615 y = (i / 4) * (item_height + item_padding) + item_padding;
616 if ((i ^ (i >> 2)) & 1)
617 dnd->items[i] = item_create(display, x, y, 0);
619 dnd->items[i] = NULL;
622 window_set_user_data(dnd->window, dnd);
623 window_set_keyboard_focus_handler(dnd->window,
624 keyboard_focus_handler);
625 window_set_data_handler(dnd->window, dnd_data_handler);
626 window_set_drop_handler(dnd->window, dnd_drop_handler);
628 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
629 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
630 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
631 widget_set_button_handler(dnd->widget, dnd_button_handler);
632 widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
634 width = 4 * (item_width + item_padding) + item_padding;
635 height = 4 * (item_height + item_padding) + item_padding;
637 window_frame_set_child_size(dnd->widget, width, height);
643 dnd_destroy(struct dnd *dnd)
645 widget_destroy(dnd->widget);
646 window_destroy(dnd->window);
651 main(int argc, char *argv[])
657 d = display_create(&argc, argv);
659 fprintf(stderr, "failed to create display: %m\n");
665 for (i = 1; i < argc; i++)
666 if (strcmp("--self-only", argv[i]) == 0)