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;
57 const char *mime_type;
59 struct wl_surface *drag_surface;
60 struct wl_data_source *data_source;
64 cairo_surface_t *surface;
69 struct dnd_flower_message {
70 int seed, x_offset, y_offset;
74 static const int item_width = 64;
75 static const int item_height = 64;
76 static const int item_padding = 16;
79 item_create(struct display *display, int x, int y, int seed)
84 item = malloc(sizeof *item);
89 gettimeofday(&tv, NULL);
90 item->seed = seed ? seed : tv.tv_usec;
93 const int petal_count = 3 + random() % 5;
94 const double r1 = 20 + random() % 10;
95 const double r2 = 5 + random() % 12;
96 const double u = (10 + random() % 90) / 100.0;
97 const double v = (random() % 90) / 100.0;
101 double t, dt = 2 * M_PI / (petal_count * 2);
102 double x1, y1, x2, y2, x3, y3;
103 struct rectangle rect;
106 rect.width = item_width;
107 rect.height = item_height;
108 item->surface = display_create_surface(display, NULL, &rect, 0);
113 cr = cairo_create(item->surface);
114 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
115 cairo_set_source_rgba(cr, 0, 0, 0, 0);
118 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
119 cairo_translate(cr, item_width / 2, item_height / 2);
121 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
122 for (i = 0; i < petal_count; i++, t += dt * 2) {
125 x2 = cos(t + dt) * r2;
126 y2 = sin(t + dt) * r2;
127 x3 = cos(t + 2 * dt) * r1;
128 y3 = sin(t + 2 * dt) * r1;
131 x1 - y1 * u, y1 + x1 * u,
132 x2 + y2 * v, y2 - x2 * v,
136 x2 - y2 * v, y2 + x2 * v,
137 x3 + y3 * u, y3 - x3 * u,
141 cairo_close_path(cr);
143 cairo_set_source_rgba(cr,
144 0.5 + (random() % 50) / 49.0,
145 0.5 + (random() % 50) / 49.0,
146 0.5 + (random() % 50) / 49.0,
147 0.5 + (random() % 100) / 99.0);
149 cairo_fill_preserve(cr);
151 cairo_set_line_width(cr, 1);
152 cairo_set_source_rgba(cr,
153 0.5 + (random() % 50) / 49.0,
154 0.5 + (random() % 50) / 49.0,
155 0.5 + (random() % 50) / 49.0,
156 0.5 + (random() % 100) / 99.0);
165 dnd_redraw_handler(struct widget *widget, void *data)
167 struct dnd *dnd = data;
168 struct rectangle allocation;
170 cairo_surface_t *surface;
173 surface = window_get_surface(dnd->window);
174 cr = cairo_create(surface);
175 widget_get_allocation(dnd->widget, &allocation);
176 cairo_rectangle(cr, allocation.x, allocation.y,
177 allocation.width, allocation.height);
179 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
180 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
183 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
184 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
187 cairo_set_source_surface(cr, dnd->items[i]->surface,
188 dnd->items[i]->x + allocation.x,
189 dnd->items[i]->y + allocation.y);
194 cairo_surface_destroy(surface);
198 keyboard_focus_handler(struct window *window,
199 struct input *device, void *data)
201 struct dnd *dnd = data;
203 window_schedule_redraw(dnd->window);
207 dnd_add_item(struct dnd *dnd, struct item *item)
211 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
212 if (dnd->items[i] == 0) {
213 dnd->items[i] = item;
221 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
224 struct rectangle allocation;
227 widget_get_allocation(dnd->widget, &allocation);
232 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
233 item = dnd->items[i];
235 item->x <= x && x < item->x + item_width &&
236 item->y <= y && y < item->y + item_height)
244 data_source_target(void *data,
245 struct wl_data_source *source, const char *mime_type)
247 struct dnd_drag *dnd_drag = data;
248 struct dnd *dnd = dnd_drag->dnd;
249 cairo_surface_t *surface;
250 struct wl_buffer *buffer;
252 dnd_drag->mime_type = mime_type;
254 surface = dnd_drag->opaque;
256 surface = dnd_drag->translucent;
258 buffer = display_get_buffer_for_surface(dnd->display, surface);
259 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
260 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
261 dnd_drag->width, dnd_drag->height);
265 data_source_send(void *data, struct wl_data_source *source,
266 const char *mime_type, int32_t fd)
268 struct dnd_flower_message dnd_flower_message;
269 struct dnd_drag *dnd_drag = data;
271 dnd_flower_message.seed = dnd_drag->item->seed;
272 dnd_flower_message.x_offset = dnd_drag->x_offset;
273 dnd_flower_message.y_offset = dnd_drag->y_offset;
275 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
282 data_source_cancelled(void *data, struct wl_data_source *source)
284 struct dnd_drag *dnd_drag = data;
286 /* The 'cancelled' event means that the source is no longer in
287 * use by the drag (or current selection). We need to clean
288 * up the drag object created and the local state. */
290 wl_data_source_destroy(dnd_drag->data_source);
292 /* Destroy the item that has been dragged out */
293 cairo_surface_destroy(dnd_drag->item->surface);
294 free(dnd_drag->item);
296 wl_surface_destroy(dnd_drag->drag_surface);
298 cairo_surface_destroy(dnd_drag->translucent);
299 cairo_surface_destroy(dnd_drag->opaque);
303 static const struct wl_data_source_listener data_source_listener = {
306 data_source_cancelled
309 static cairo_surface_t *
310 create_drag_cursor(struct dnd_drag *dnd_drag,
311 struct item *item, int32_t x, int32_t y, double opacity)
313 struct dnd *dnd = dnd_drag->dnd;
314 cairo_surface_t *surface, *pointer;
315 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
316 struct rectangle rectangle;
317 cairo_pattern_t *pattern;
320 pointer = display_get_pointer_surface(dnd->display,
327 rectangle.width = item_width + 2 * pointer_width;
328 rectangle.height = item_height + 2 * pointer_height;
329 surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
331 cr = cairo_create(surface);
332 cairo_translate(cr, pointer_width, pointer_height);
334 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
335 cairo_set_source_rgba(cr, 0, 0, 0, 0);
338 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
339 cairo_set_source_surface(cr, item->surface, 0, 0);
340 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
341 cairo_mask(cr, pattern);
342 cairo_pattern_destroy(pattern);
344 cairo_set_source_surface(cr, pointer,
345 x - item->x - hotspot_x,
346 y - item->y - hotspot_y);
347 cairo_surface_destroy(pointer);
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 int button, int state, void *data)
366 struct dnd *dnd = data;
369 struct rectangle allocation;
370 struct dnd_drag *dnd_drag;
371 struct display *display;
372 struct wl_compositor *compositor;
373 struct wl_buffer *buffer;
376 widget_get_allocation(dnd->widget, &allocation);
377 input_get_position(input, &x, &y);
378 item = dnd_get_item(dnd, x, y);
382 if (item && state == 1) {
383 dnd_drag = malloc(sizeof *dnd_drag);
385 dnd_drag->input = input;
386 dnd_drag->time = time;
387 dnd_drag->item = item;
388 dnd_drag->x_offset = x - item->x;
389 dnd_drag->y_offset = y - item->y;
391 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
392 if (item == dnd->items[i]){
398 display = window_get_display(dnd->window);
399 compositor = display_get_compositor(display);
400 dnd_drag->drag_surface =
401 wl_compositor_create_surface(compositor);
403 dnd_drag->data_source =
404 display_create_data_source(dnd->display);
405 wl_data_source_add_listener(dnd_drag->data_source,
406 &data_source_listener,
408 wl_data_source_offer(dnd_drag->data_source,
409 "application/x-wayland-dnd-flower");
410 wl_data_source_offer(dnd_drag->data_source,
411 "text/plain; charset=utf-8");
412 wl_data_device_start_drag(input_get_data_device(input),
413 dnd_drag->data_source,
414 window_get_wl_surface(dnd->window),
415 dnd_drag->drag_surface,
418 input_set_pointer_image(input, time, POINTER_DRAGGING);
421 create_drag_cursor(dnd_drag, item, x, y, 1);
422 dnd_drag->translucent =
423 create_drag_cursor(dnd_drag, item, x, y, 0.2);
425 buffer = display_get_buffer_for_surface(dnd->display, dnd_drag->translucent);
426 wl_surface_attach(dnd_drag->drag_surface, buffer,
427 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
428 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
429 dnd_drag->width, dnd_drag->height);
431 window_schedule_redraw(dnd->window);
436 lookup_cursor(struct dnd *dnd, int x, int y)
440 item = dnd_get_item(dnd, x, y);
442 return POINTER_HAND1;
444 return POINTER_LEFT_PTR;
448 dnd_enter_handler(struct widget *widget,
449 struct input *input, uint32_t time,
450 int32_t x, int32_t y, void *data)
452 return lookup_cursor(data, x, y);
456 dnd_motion_handler(struct widget *widget,
457 struct input *input, uint32_t time,
458 int32_t x, int32_t y, void *data)
460 return lookup_cursor(data, x, y);
464 dnd_data_handler(struct window *window,
465 struct input *input, uint32_t time,
466 int32_t x, int32_t y, const char **types, void *data)
468 struct dnd *dnd = data;
470 if (!dnd_get_item(dnd, x, y)) {
471 input_accept(input, time, types[0]);
473 input_accept(input, time, NULL);
478 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
480 struct dnd *dnd = user_data;
481 struct dnd_flower_message *message = data;
483 struct rectangle allocation;
487 } else if (len != sizeof *message) {
488 fprintf(stderr, "odd message length %ld, expected %ld\n",
489 len, sizeof *message);
493 widget_get_allocation(dnd->widget, &allocation);
494 item = item_create(dnd->display,
495 x - message->x_offset - allocation.x,
496 y - message->y_offset - allocation.y,
499 dnd_add_item(dnd, item);
500 window_schedule_redraw(dnd->window);
504 dnd_drop_handler(struct window *window, struct input *input,
505 int32_t x, int32_t y, void *data)
507 struct dnd *dnd = data;
509 if (dnd_get_item(dnd, x, y)) {
510 fprintf(stderr, "got 'drop', but no target\n");
514 input_receive_drag_data(input,
515 "application/x-wayland-dnd-flower",
516 dnd_receive_func, dnd);
520 dnd_create(struct display *display)
524 int32_t width, height;
527 dnd = malloc(sizeof *dnd);
530 memset(dnd, 0, sizeof *dnd);
532 dnd->window = window_create(display);
533 dnd->widget = frame_create(dnd->window, dnd);
534 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
536 dnd->display = display;
539 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
540 x = (i % 4) * (item_width + item_padding) + item_padding;
541 y = (i / 4) * (item_height + item_padding) + item_padding;
542 if ((i ^ (i >> 2)) & 1)
543 dnd->items[i] = item_create(display, x, y, 0);
545 dnd->items[i] = NULL;
548 window_set_user_data(dnd->window, dnd);
549 window_set_keyboard_focus_handler(dnd->window,
550 keyboard_focus_handler);
551 window_set_data_handler(dnd->window, dnd_data_handler);
552 window_set_drop_handler(dnd->window, dnd_drop_handler);
554 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
555 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
556 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
557 widget_set_button_handler(dnd->widget, dnd_button_handler);
559 width = 4 * (item_width + item_padding) + item_padding;
560 height = 4 * (item_height + item_padding) + item_padding;
562 widget_schedule_resize(dnd->widget, width, height);
568 main(int argc, char *argv[])
572 d = display_create(argc, argv);
574 fprintf(stderr, "failed to create display: %m\n");