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
33 #include <sys/epoll.h>
35 #include <wayland-client.h>
36 #include <wayland-cursor.h>
39 #include "../shared/cairo-util.h"
44 struct window *window;
45 struct widget *widget;
46 struct display *display;
48 struct item *items[16];
50 struct dnd_drag *current_drag;
54 cairo_surface_t *translucent;
55 cairo_surface_t *opaque;
56 int hotspot_x, hotspot_y;
61 int x_offset, y_offset;
63 const char *mime_type;
65 struct wl_surface *drag_surface;
66 struct wl_data_source *data_source;
70 cairo_surface_t *surface;
75 struct dnd_flower_message {
76 int seed, x_offset, y_offset;
80 static const int item_width = 64;
81 static const int item_height = 64;
82 static const int item_padding = 16;
85 item_create(struct display *display, int x, int y, int seed)
90 item = malloc(sizeof *item);
95 gettimeofday(&tv, NULL);
96 item->seed = seed ? seed : tv.tv_usec;
99 const int petal_count = 3 + random() % 5;
100 const double r1 = 20 + random() % 10;
101 const double r2 = 5 + random() % 12;
102 const double u = (10 + random() % 90) / 100.0;
103 const double v = (random() % 90) / 100.0;
107 double t, dt = 2 * M_PI / (petal_count * 2);
108 double x1, y1, x2, y2, x3, y3;
109 struct rectangle rect;
112 rect.width = item_width;
113 rect.height = item_height;
115 display_create_surface(display, NULL, &rect, SURFACE_SHM);
120 cr = cairo_create(item->surface);
121 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
122 cairo_set_source_rgba(cr, 0, 0, 0, 0);
125 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
126 cairo_translate(cr, item_width / 2, item_height / 2);
128 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
129 for (i = 0; i < petal_count; i++, t += dt * 2) {
132 x2 = cos(t + dt) * r2;
133 y2 = sin(t + dt) * r2;
134 x3 = cos(t + 2 * dt) * r1;
135 y3 = sin(t + 2 * dt) * r1;
138 x1 - y1 * u, y1 + x1 * u,
139 x2 + y2 * v, y2 - x2 * v,
143 x2 - y2 * v, y2 + x2 * v,
144 x3 + y3 * u, y3 - x3 * u,
148 cairo_close_path(cr);
150 cairo_set_source_rgba(cr,
151 0.5 + (random() % 50) / 49.0,
152 0.5 + (random() % 50) / 49.0,
153 0.5 + (random() % 50) / 49.0,
154 0.5 + (random() % 100) / 99.0);
156 cairo_fill_preserve(cr);
158 cairo_set_line_width(cr, 1);
159 cairo_set_source_rgba(cr,
160 0.5 + (random() % 50) / 49.0,
161 0.5 + (random() % 50) / 49.0,
162 0.5 + (random() % 50) / 49.0,
163 0.5 + (random() % 100) / 99.0);
172 dnd_redraw_handler(struct widget *widget, void *data)
174 struct dnd *dnd = data;
175 struct rectangle allocation;
177 cairo_surface_t *surface;
180 surface = window_get_surface(dnd->window);
181 cr = cairo_create(surface);
182 widget_get_allocation(dnd->widget, &allocation);
183 cairo_rectangle(cr, allocation.x, allocation.y,
184 allocation.width, allocation.height);
186 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
187 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
190 cairo_rectangle(cr, allocation.x, allocation.y,
191 allocation.width, allocation.height);
193 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
194 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
197 cairo_set_source_surface(cr, dnd->items[i]->surface,
198 dnd->items[i]->x + allocation.x,
199 dnd->items[i]->y + allocation.y);
204 cairo_surface_destroy(surface);
208 keyboard_focus_handler(struct window *window,
209 struct input *device, void *data)
211 struct dnd *dnd = data;
213 window_schedule_redraw(dnd->window);
217 dnd_add_item(struct dnd *dnd, struct item *item)
221 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
222 if (dnd->items[i] == 0) {
223 dnd->items[i] = item;
231 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
234 struct rectangle allocation;
237 widget_get_allocation(dnd->widget, &allocation);
242 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
243 item = dnd->items[i];
245 item->x <= x && x < item->x + item_width &&
246 item->y <= y && y < item->y + item_height)
254 data_source_target(void *data,
255 struct wl_data_source *source, const char *mime_type)
257 struct dnd_drag *dnd_drag = data;
258 struct dnd *dnd = dnd_drag->dnd;
259 cairo_surface_t *surface;
260 struct wl_buffer *buffer;
262 dnd_drag->mime_type = mime_type;
264 surface = dnd_drag->opaque;
266 surface = dnd_drag->translucent;
268 buffer = display_get_buffer_for_surface(dnd->display, surface);
269 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
270 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
271 dnd_drag->width, dnd_drag->height);
272 wl_surface_commit(dnd_drag->drag_surface);
276 data_source_send(void *data, struct wl_data_source *source,
277 const char *mime_type, int32_t fd)
279 struct dnd_flower_message dnd_flower_message;
280 struct dnd_drag *dnd_drag = data;
282 dnd_flower_message.seed = dnd_drag->item->seed;
283 dnd_flower_message.x_offset = dnd_drag->x_offset;
284 dnd_flower_message.y_offset = dnd_drag->y_offset;
286 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
293 data_source_cancelled(void *data, struct wl_data_source *source)
295 struct dnd_drag *dnd_drag = data;
297 /* The 'cancelled' event means that the source is no longer in
298 * use by the drag (or current selection). We need to clean
299 * up the drag object created and the local state. */
301 wl_data_source_destroy(dnd_drag->data_source);
303 /* Destroy the item that has been dragged out */
304 cairo_surface_destroy(dnd_drag->item->surface);
305 free(dnd_drag->item);
307 wl_surface_destroy(dnd_drag->drag_surface);
309 cairo_surface_destroy(dnd_drag->translucent);
310 cairo_surface_destroy(dnd_drag->opaque);
314 static const struct wl_data_source_listener data_source_listener = {
317 data_source_cancelled
320 static cairo_surface_t *
321 create_drag_cursor(struct dnd_drag *dnd_drag,
322 struct item *item, int32_t x, int32_t y, double opacity)
324 struct dnd *dnd = dnd_drag->dnd;
325 cairo_surface_t *surface;
326 struct wl_cursor_image *pointer;
327 struct rectangle rectangle;
328 cairo_pattern_t *pattern;
331 pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
333 fprintf(stderr, "WARNING: grabbing cursor image not found\n");
334 pointer = display_get_pointer_image(dnd->display,
336 assert(pointer && "no cursor image found");
339 rectangle.width = item_width + 2 * pointer->width;
340 rectangle.height = item_height + 2 * pointer->height;
341 surface = display_create_surface(dnd->display, NULL, &rectangle,
344 cr = cairo_create(surface);
345 cairo_translate(cr, pointer->width, pointer->height);
347 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
348 cairo_set_source_rgba(cr, 0, 0, 0, 0);
351 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
352 cairo_set_source_surface(cr, item->surface, 0, 0);
353 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
354 cairo_mask(cr, pattern);
355 cairo_pattern_destroy(pattern);
357 /* FIXME: more cairo-gl brokeness */
358 surface_flush_device(surface);
361 dnd_drag->hotspot_x = pointer->width + x - item->x;
362 dnd_drag->hotspot_y = pointer->height + y - item->y;
363 dnd_drag->width = rectangle.width;
364 dnd_drag->height = rectangle.height;
370 dnd_button_handler(struct widget *widget,
371 struct input *input, uint32_t time,
372 uint32_t button, enum wl_pointer_button_state state,
375 struct dnd *dnd = data;
378 struct rectangle allocation;
379 struct dnd_drag *dnd_drag;
380 struct display *display;
381 struct wl_compositor *compositor;
382 struct wl_buffer *buffer;
385 cairo_surface_t *icon;
387 widget_get_allocation(dnd->widget, &allocation);
388 input_get_position(input, &x, &y);
389 item = dnd_get_item(dnd, x, y);
393 if (item && state == WL_POINTER_BUTTON_STATE_PRESSED) {
394 dnd_drag = malloc(sizeof *dnd_drag);
396 dnd_drag->input = input;
397 dnd_drag->time = time;
398 dnd_drag->item = item;
399 dnd_drag->x_offset = x - item->x;
400 dnd_drag->y_offset = y - item->y;
402 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
403 if (item == dnd->items[i]){
409 display = window_get_display(dnd->window);
410 compositor = display_get_compositor(display);
411 serial = display_get_serial(display);
412 dnd_drag->drag_surface =
413 wl_compositor_create_surface(compositor);
417 if (dnd->self_only) {
418 dnd_drag->data_source = NULL;
420 dnd_drag->data_source =
421 display_create_data_source(dnd->display);
422 wl_data_source_add_listener(dnd_drag->data_source,
423 &data_source_listener,
425 wl_data_source_offer(dnd_drag->data_source,
426 "application/x-wayland-dnd-flower");
427 wl_data_source_offer(dnd_drag->data_source,
428 "text/plain; charset=utf-8");
431 wl_data_device_start_drag(input_get_data_device(input),
432 dnd_drag->data_source,
433 window_get_wl_surface(dnd->window),
434 dnd_drag->drag_surface,
437 input_set_pointer_image(input, CURSOR_DRAGGING);
440 create_drag_cursor(dnd_drag, item, x, y, 1);
441 dnd_drag->translucent =
442 create_drag_cursor(dnd_drag, item, x, y, 0.2);
445 icon = dnd_drag->opaque;
447 icon = dnd_drag->translucent;
449 buffer = display_get_buffer_for_surface(dnd->display, icon);
450 wl_surface_attach(dnd_drag->drag_surface, buffer,
451 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
452 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
453 dnd_drag->width, dnd_drag->height);
454 wl_surface_commit(dnd_drag->drag_surface);
456 dnd->current_drag = dnd_drag;
457 window_schedule_redraw(dnd->window);
462 lookup_cursor(struct dnd *dnd, int x, int y)
466 item = dnd_get_item(dnd, x, y);
470 return CURSOR_LEFT_PTR;
474 dnd_enter_handler(struct widget *widget,
475 struct input *input, float x, float y, void *data)
477 struct dnd *dnd = data;
479 dnd->current_drag = NULL;
481 return lookup_cursor(dnd, x, y);
485 dnd_motion_handler(struct widget *widget,
486 struct input *input, uint32_t time,
487 float x, float y, void *data)
489 return lookup_cursor(data, x, y);
493 dnd_data_handler(struct window *window,
495 float x, float y, const char **types, void *data)
497 struct dnd *dnd = data;
502 if (dnd_get_item(dnd, x, y) || dnd->self_only) {
503 input_accept(input, NULL);
505 input_accept(input, types[0]);
510 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
512 struct dnd *dnd = user_data;
513 struct dnd_flower_message *message = data;
515 struct rectangle allocation;
519 } else if (len != sizeof *message) {
520 fprintf(stderr, "odd message length %zu, expected %zu\n",
521 len, sizeof *message);
525 widget_get_allocation(dnd->widget, &allocation);
526 item = item_create(dnd->display,
527 x - message->x_offset - allocation.x,
528 y - message->y_offset - allocation.y,
531 dnd_add_item(dnd, item);
532 window_schedule_redraw(dnd->window);
536 dnd_drop_handler(struct window *window, struct input *input,
537 int32_t x, int32_t y, void *data)
539 struct dnd *dnd = data;
540 struct dnd_flower_message message;
542 if (dnd_get_item(dnd, x, y)) {
543 fprintf(stderr, "got 'drop', but no target\n");
547 if (!dnd->self_only) {
548 input_receive_drag_data(input,
549 "application/x-wayland-dnd-flower",
550 dnd_receive_func, dnd);
551 } else if (dnd->current_drag) {
552 message.seed = dnd->current_drag->item->seed;
553 message.x_offset = dnd->current_drag->x_offset;
554 message.y_offset = dnd->current_drag->y_offset;
555 dnd_receive_func(&message, sizeof message, x, y, dnd);
556 dnd->current_drag = NULL;
558 fprintf(stderr, "ignoring drop from another client\n");
563 dnd_create(struct display *display)
567 int32_t width, height;
570 dnd = malloc(sizeof *dnd);
573 memset(dnd, 0, sizeof *dnd);
575 dnd->window = window_create(display);
576 dnd->widget = frame_create(dnd->window, dnd);
577 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
579 dnd->display = display;
582 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
583 x = (i % 4) * (item_width + item_padding) + item_padding;
584 y = (i / 4) * (item_height + item_padding) + item_padding;
585 if ((i ^ (i >> 2)) & 1)
586 dnd->items[i] = item_create(display, x, y, 0);
588 dnd->items[i] = NULL;
591 window_set_user_data(dnd->window, dnd);
592 window_set_keyboard_focus_handler(dnd->window,
593 keyboard_focus_handler);
594 window_set_data_handler(dnd->window, dnd_data_handler);
595 window_set_drop_handler(dnd->window, dnd_drop_handler);
597 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
598 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
599 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
600 widget_set_button_handler(dnd->widget, dnd_button_handler);
602 width = 4 * (item_width + item_padding) + item_padding;
603 height = 4 * (item_height + item_padding) + item_padding;
605 frame_set_child_size(dnd->widget, width, height);
611 main(int argc, char *argv[])
617 d = display_create(argc, argv);
619 fprintf(stderr, "failed to create display: %m\n");
625 for (i = 1; i < argc; i++)
626 if (strcmp("--self-only", argv[i]) == 0)