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_rectangle(cr, allocation.x, allocation.y,
190 allocation.width, allocation.height);
192 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
193 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
196 cairo_set_source_surface(cr, dnd->items[i]->surface,
197 dnd->items[i]->x + allocation.x,
198 dnd->items[i]->y + allocation.y);
203 cairo_surface_destroy(surface);
207 keyboard_focus_handler(struct window *window,
208 struct input *device, void *data)
210 struct dnd *dnd = data;
212 window_schedule_redraw(dnd->window);
216 dnd_add_item(struct dnd *dnd, struct item *item)
220 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
221 if (dnd->items[i] == 0) {
222 dnd->items[i] = item;
230 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
233 struct rectangle allocation;
236 widget_get_allocation(dnd->widget, &allocation);
241 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
242 item = dnd->items[i];
244 item->x <= x && x < item->x + item_width &&
245 item->y <= y && y < item->y + item_height)
253 data_source_target(void *data,
254 struct wl_data_source *source, const char *mime_type)
256 struct dnd_drag *dnd_drag = data;
257 struct dnd *dnd = dnd_drag->dnd;
258 cairo_surface_t *surface;
259 struct wl_buffer *buffer;
261 dnd_drag->mime_type = mime_type;
263 surface = dnd_drag->opaque;
265 surface = dnd_drag->translucent;
267 buffer = display_get_buffer_for_surface(dnd->display, surface);
268 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
269 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
270 dnd_drag->width, dnd_drag->height);
274 data_source_send(void *data, struct wl_data_source *source,
275 const char *mime_type, int32_t fd)
277 struct dnd_flower_message dnd_flower_message;
278 struct dnd_drag *dnd_drag = data;
280 dnd_flower_message.seed = dnd_drag->item->seed;
281 dnd_flower_message.x_offset = dnd_drag->x_offset;
282 dnd_flower_message.y_offset = dnd_drag->y_offset;
284 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
291 data_source_cancelled(void *data, struct wl_data_source *source)
293 struct dnd_drag *dnd_drag = data;
295 /* The 'cancelled' event means that the source is no longer in
296 * use by the drag (or current selection). We need to clean
297 * up the drag object created and the local state. */
299 wl_data_source_destroy(dnd_drag->data_source);
301 /* Destroy the item that has been dragged out */
302 cairo_surface_destroy(dnd_drag->item->surface);
303 free(dnd_drag->item);
305 wl_surface_destroy(dnd_drag->drag_surface);
307 cairo_surface_destroy(dnd_drag->translucent);
308 cairo_surface_destroy(dnd_drag->opaque);
312 static const struct wl_data_source_listener data_source_listener = {
315 data_source_cancelled
318 static cairo_surface_t *
319 create_drag_cursor(struct dnd_drag *dnd_drag,
320 struct item *item, int32_t x, int32_t y, double opacity)
322 struct dnd *dnd = dnd_drag->dnd;
323 cairo_surface_t *surface;
324 struct wl_cursor_image *pointer;
325 struct rectangle rectangle;
326 cairo_pattern_t *pattern;
329 pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
331 rectangle.width = item_width + 2 * pointer->width;
332 rectangle.height = item_height + 2 * pointer->height;
333 surface = display_create_surface(dnd->display, NULL, &rectangle,
336 cr = cairo_create(surface);
337 cairo_translate(cr, pointer->width, pointer->height);
339 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
340 cairo_set_source_rgba(cr, 0, 0, 0, 0);
343 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
344 cairo_set_source_surface(cr, item->surface, 0, 0);
345 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
346 cairo_mask(cr, pattern);
347 cairo_pattern_destroy(pattern);
349 /* FIXME: more cairo-gl brokeness */
350 surface_flush_device(surface);
353 dnd_drag->hotspot_x = pointer->width + x - item->x;
354 dnd_drag->hotspot_y = pointer->height + y - item->y;
355 dnd_drag->width = rectangle.width;
356 dnd_drag->height = rectangle.height;
362 dnd_button_handler(struct widget *widget,
363 struct input *input, uint32_t time,
364 uint32_t button, enum wl_pointer_button_state state,
367 struct dnd *dnd = data;
370 struct rectangle allocation;
371 struct dnd_drag *dnd_drag;
372 struct display *display;
373 struct wl_compositor *compositor;
374 struct wl_buffer *buffer;
377 cairo_surface_t *icon;
379 widget_get_allocation(dnd->widget, &allocation);
380 input_get_position(input, &x, &y);
381 item = dnd_get_item(dnd, x, y);
385 if (item && state == WL_POINTER_BUTTON_STATE_PRESSED) {
386 dnd_drag = malloc(sizeof *dnd_drag);
388 dnd_drag->input = input;
389 dnd_drag->time = time;
390 dnd_drag->item = item;
391 dnd_drag->x_offset = x - item->x;
392 dnd_drag->y_offset = y - item->y;
394 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
395 if (item == dnd->items[i]){
401 display = window_get_display(dnd->window);
402 compositor = display_get_compositor(display);
403 serial = display_get_serial(display);
404 dnd_drag->drag_surface =
405 wl_compositor_create_surface(compositor);
409 if (dnd->self_only) {
410 dnd_drag->data_source = NULL;
412 dnd_drag->data_source =
413 display_create_data_source(dnd->display);
414 wl_data_source_add_listener(dnd_drag->data_source,
415 &data_source_listener,
417 wl_data_source_offer(dnd_drag->data_source,
418 "application/x-wayland-dnd-flower");
419 wl_data_source_offer(dnd_drag->data_source,
420 "text/plain; charset=utf-8");
423 wl_data_device_start_drag(input_get_data_device(input),
424 dnd_drag->data_source,
425 window_get_wl_surface(dnd->window),
426 dnd_drag->drag_surface,
429 input_set_pointer_image(input, CURSOR_DRAGGING);
432 create_drag_cursor(dnd_drag, item, x, y, 1);
433 dnd_drag->translucent =
434 create_drag_cursor(dnd_drag, item, x, y, 0.2);
437 icon = dnd_drag->opaque;
439 icon = dnd_drag->translucent;
441 buffer = display_get_buffer_for_surface(dnd->display, icon);
442 wl_surface_attach(dnd_drag->drag_surface, buffer,
443 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
444 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
445 dnd_drag->width, dnd_drag->height);
447 dnd->current_drag = dnd_drag;
448 window_schedule_redraw(dnd->window);
453 lookup_cursor(struct dnd *dnd, int x, int y)
457 item = dnd_get_item(dnd, x, y);
461 return CURSOR_LEFT_PTR;
465 dnd_enter_handler(struct widget *widget,
466 struct input *input, float x, float y, void *data)
468 struct dnd *dnd = data;
470 dnd->current_drag = NULL;
472 return lookup_cursor(dnd, x, y);
476 dnd_motion_handler(struct widget *widget,
477 struct input *input, uint32_t time,
478 float x, float y, void *data)
480 return lookup_cursor(data, x, y);
484 dnd_data_handler(struct window *window,
486 float x, float y, const char **types, void *data)
488 struct dnd *dnd = data;
493 if (dnd_get_item(dnd, x, y) || dnd->self_only) {
494 input_accept(input, NULL);
496 input_accept(input, types[0]);
501 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
503 struct dnd *dnd = user_data;
504 struct dnd_flower_message *message = data;
506 struct rectangle allocation;
510 } else if (len != sizeof *message) {
511 fprintf(stderr, "odd message length %ld, expected %ld\n",
512 len, sizeof *message);
516 widget_get_allocation(dnd->widget, &allocation);
517 item = item_create(dnd->display,
518 x - message->x_offset - allocation.x,
519 y - message->y_offset - allocation.y,
522 dnd_add_item(dnd, item);
523 window_schedule_redraw(dnd->window);
527 dnd_drop_handler(struct window *window, struct input *input,
528 int32_t x, int32_t y, void *data)
530 struct dnd *dnd = data;
531 struct dnd_flower_message message;
533 if (dnd_get_item(dnd, x, y)) {
534 fprintf(stderr, "got 'drop', but no target\n");
538 if (!dnd->self_only) {
539 input_receive_drag_data(input,
540 "application/x-wayland-dnd-flower",
541 dnd_receive_func, dnd);
542 } else if (dnd->current_drag) {
543 message.seed = dnd->current_drag->item->seed;
544 message.x_offset = dnd->current_drag->x_offset;
545 message.y_offset = dnd->current_drag->y_offset;
546 dnd_receive_func(&message, sizeof message, x, y, dnd);
547 dnd->current_drag = NULL;
549 fprintf(stderr, "ignoring drop from another client\n");
554 dnd_create(struct display *display)
558 int32_t width, height;
561 dnd = malloc(sizeof *dnd);
564 memset(dnd, 0, sizeof *dnd);
566 dnd->window = window_create(display);
567 dnd->widget = frame_create(dnd->window, dnd);
568 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
570 dnd->display = display;
573 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
574 x = (i % 4) * (item_width + item_padding) + item_padding;
575 y = (i / 4) * (item_height + item_padding) + item_padding;
576 if ((i ^ (i >> 2)) & 1)
577 dnd->items[i] = item_create(display, x, y, 0);
579 dnd->items[i] = NULL;
582 window_set_user_data(dnd->window, dnd);
583 window_set_keyboard_focus_handler(dnd->window,
584 keyboard_focus_handler);
585 window_set_data_handler(dnd->window, dnd_data_handler);
586 window_set_drop_handler(dnd->window, dnd_drop_handler);
588 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
589 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
590 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
591 widget_set_button_handler(dnd->widget, dnd_button_handler);
593 width = 4 * (item_width + item_padding) + item_padding;
594 height = 4 * (item_height + item_padding) + item_padding;
596 frame_set_child_size(dnd->widget, width, height);
602 main(int argc, char *argv[])
608 d = display_create(argc, argv);
610 fprintf(stderr, "failed to create display: %m\n");
616 for (i = 1; i < argc; i++)
617 if (strcmp("--self-only", argv[i]) == 0)