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 display *display;
43 struct item *items[16];
47 cairo_surface_t *translucent;
48 cairo_surface_t *opaque;
49 int hotspot_x, hotspot_y;
54 int x_offset, y_offset;
55 const char *mime_type;
57 struct wl_data_source *data_source;
61 cairo_surface_t *surface;
66 struct dnd_flower_message {
67 int seed, x_offset, y_offset;
71 static const int item_width = 64;
72 static const int item_height = 64;
73 static const int item_padding = 16;
76 item_create(struct display *display, int x, int y, int seed)
81 item = malloc(sizeof *item);
86 gettimeofday(&tv, NULL);
87 item->seed = seed ? seed : tv.tv_usec;
90 const int petal_count = 3 + random() % 5;
91 const double r1 = 20 + random() % 10;
92 const double r2 = 5 + random() % 12;
93 const double u = (10 + random() % 90) / 100.0;
94 const double v = (random() % 90) / 100.0;
98 double t, dt = 2 * M_PI / (petal_count * 2);
99 double x1, y1, x2, y2, x3, y3;
100 struct rectangle rect;
103 rect.width = item_width;
104 rect.height = item_height;
105 item->surface = display_create_surface(display, NULL, &rect, 0);
110 cr = cairo_create(item->surface);
111 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
112 cairo_set_source_rgba(cr, 0, 0, 0, 0);
115 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
116 cairo_translate(cr, item_width / 2, item_height / 2);
118 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
119 for (i = 0; i < petal_count; i++, t += dt * 2) {
122 x2 = cos(t + dt) * r2;
123 y2 = sin(t + dt) * r2;
124 x3 = cos(t + 2 * dt) * r1;
125 y3 = sin(t + 2 * dt) * r1;
128 x1 - y1 * u, y1 + x1 * u,
129 x2 + y2 * v, y2 - x2 * v,
133 x2 - y2 * v, y2 + x2 * v,
134 x3 + y3 * u, y3 - x3 * u,
138 cairo_close_path(cr);
140 cairo_set_source_rgba(cr,
141 0.5 + (random() % 50) / 49.0,
142 0.5 + (random() % 50) / 49.0,
143 0.5 + (random() % 50) / 49.0,
144 0.5 + (random() % 100) / 99.0);
146 cairo_fill_preserve(cr);
148 cairo_set_line_width(cr, 1);
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);
162 dnd_draw(struct dnd *dnd)
164 struct rectangle allocation;
166 cairo_surface_t *surface;
169 window_draw(dnd->window);
171 surface = window_get_surface(dnd->window);
172 cr = cairo_create(surface);
173 window_get_child_allocation(dnd->window, &allocation);
174 cairo_rectangle(cr, allocation.x, allocation.y,
175 allocation.width, allocation.height);
177 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
178 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
181 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
182 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
185 cairo_set_source_surface(cr, dnd->items[i]->surface,
186 dnd->items[i]->x + allocation.x,
187 dnd->items[i]->y + allocation.y);
192 cairo_surface_destroy(surface);
193 window_flush(dnd->window);
197 redraw_handler(struct window *window, void *data)
199 struct dnd *dnd = data;
205 keyboard_focus_handler(struct window *window,
206 struct input *device, void *data)
208 struct dnd *dnd = data;
210 window_schedule_redraw(dnd->window);
214 dnd_add_item(struct dnd *dnd, struct item *item)
218 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
219 if (dnd->items[i] == 0) {
220 dnd->items[i] = item;
228 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
231 struct rectangle allocation;
234 window_get_child_allocation(dnd->window, &allocation);
239 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
240 item = dnd->items[i];
242 item->x <= x && x < item->x + item_width &&
243 item->y <= y && y < item->y + item_height)
251 data_source_target(void *data,
252 struct wl_data_source *source, const char *mime_type)
254 struct dnd_drag *dnd_drag = data;
255 struct dnd *dnd = dnd_drag->dnd;
256 cairo_surface_t *surface;
257 struct wl_buffer *buffer;
258 struct wl_data_device *device;
260 device = input_get_data_device(dnd_drag->input);
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_data_device_attach(device, dnd_drag->time, buffer,
269 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
273 data_source_send(void *data, struct wl_data_source *source,
274 const char *mime_type, int32_t fd)
276 struct dnd_flower_message dnd_flower_message;
277 struct dnd_drag *dnd_drag = data;
279 dnd_flower_message.seed = dnd_drag->item->seed;
280 dnd_flower_message.x_offset = dnd_drag->x_offset;
281 dnd_flower_message.y_offset = dnd_drag->y_offset;
283 write(fd, &dnd_flower_message, sizeof dnd_flower_message);
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 cairo_surface_destroy(dnd_drag->translucent);
303 cairo_surface_destroy(dnd_drag->opaque);
307 static const struct wl_data_source_listener data_source_listener = {
310 data_source_cancelled
313 static cairo_surface_t *
314 create_drag_cursor(struct dnd_drag *dnd_drag,
315 struct item *item, int32_t x, int32_t y, double opacity)
317 struct dnd *dnd = dnd_drag->dnd;
318 cairo_surface_t *surface, *pointer;
319 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
320 struct rectangle rectangle;
321 cairo_pattern_t *pattern;
324 pointer = display_get_pointer_surface(dnd->display,
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, 0);
335 cr = cairo_create(surface);
336 cairo_translate(cr, pointer_width, pointer_height);
338 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
339 cairo_set_source_rgba(cr, 0, 0, 0, 0);
342 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
343 cairo_set_source_surface(cr, item->surface, 0, 0);
344 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
345 cairo_mask(cr, pattern);
346 cairo_pattern_destroy(pattern);
348 cairo_set_source_surface(cr, pointer,
349 x - item->x - hotspot_x,
350 y - item->y - hotspot_y);
351 cairo_surface_destroy(pointer);
353 /* FIXME: more cairo-gl brokeness */
354 surface_flush_device(surface);
357 dnd_drag->hotspot_x = pointer_width + x - item->x;
358 dnd_drag->hotspot_y = pointer_height + y - item->y;
364 dnd_button_handler(struct window *window,
365 struct input *input, uint32_t time,
366 int button, int state, void *data)
368 struct dnd *dnd = data;
371 struct rectangle allocation;
372 struct dnd_drag *dnd_drag;
375 window_get_child_allocation(dnd->window, &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 dnd_drag->data_source =
398 display_create_data_source(dnd->display);
399 wl_data_source_add_listener(dnd_drag->data_source,
400 &data_source_listener,
402 wl_data_source_offer(dnd_drag->data_source,
403 "application/x-wayland-dnd-flower");
404 wl_data_source_offer(dnd_drag->data_source,
405 "text/plain; charset=utf-8");
406 wl_data_device_start_drag(input_get_data_device(input),
407 dnd_drag->data_source,
408 window_get_wl_surface(window),
411 input_set_pointer_image(input, time, POINTER_DRAGGING);
414 create_drag_cursor(dnd_drag, item, x, y, 1);
415 dnd_drag->translucent =
416 create_drag_cursor(dnd_drag, item, x, y, 0.2);
418 window_schedule_redraw(dnd->window);
423 lookup_cursor(struct dnd *dnd, int x, int y)
427 item = dnd_get_item(dnd, x, y);
429 return POINTER_HAND1;
431 return POINTER_LEFT_PTR;
435 dnd_enter_handler(struct widget *widget,
436 struct input *input, uint32_t time,
437 int32_t x, int32_t y, void *data)
442 dnd_motion_handler(struct widget *widget,
443 struct input *input, uint32_t time,
444 int32_t x, int32_t y, void *data)
446 struct window *window = data;
447 struct dnd *dnd = window_get_user_data(window);
449 return lookup_cursor(dnd, x, y);
453 dnd_data_handler(struct window *window,
454 struct input *input, uint32_t time,
455 int32_t x, int32_t y, const char **types, void *data)
457 struct dnd *dnd = data;
459 if (!dnd_get_item(dnd, x, y)) {
460 input_accept(input, time, types[0]);
462 input_accept(input, time, NULL);
467 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
469 struct dnd *dnd = user_data;
470 struct dnd_flower_message *message = data;
472 struct rectangle allocation;
476 } else if (len != sizeof *message) {
477 fprintf(stderr, "odd message length %ld, expected %ld\n",
478 len, sizeof *message);
482 window_get_child_allocation(dnd->window, &allocation);
483 item = item_create(dnd->display,
484 x - message->x_offset - allocation.x,
485 y - message->y_offset - allocation.y,
488 dnd_add_item(dnd, item);
489 window_schedule_redraw(dnd->window);
493 dnd_drop_handler(struct window *window, struct input *input,
494 int32_t x, int32_t y, void *data)
496 struct dnd *dnd = data;
498 if (dnd_get_item(dnd, x, y)) {
499 fprintf(stderr, "got 'drop', but no target\n");
503 input_receive_drag_data(input,
504 "application/x-wayland-dnd-flower",
505 dnd_receive_func, dnd);
509 dnd_create(struct display *display)
513 int32_t width, height;
514 struct widget *widget;
516 dnd = malloc(sizeof *dnd);
519 memset(dnd, 0, sizeof *dnd);
521 dnd->window = window_create(display, 400, 400);
522 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
524 dnd->display = display;
527 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
528 x = (i % 4) * (item_width + item_padding) + item_padding;
529 y = (i / 4) * (item_height + item_padding) + item_padding;
530 if ((i ^ (i >> 2)) & 1)
531 dnd->items[i] = item_create(display, x, y, 0);
533 dnd->items[i] = NULL;
536 window_set_user_data(dnd->window, dnd);
537 window_set_redraw_handler(dnd->window, redraw_handler);
538 window_set_keyboard_focus_handler(dnd->window,
539 keyboard_focus_handler);
540 window_set_button_handler(dnd->window, dnd_button_handler);
541 window_set_data_handler(dnd->window, dnd_data_handler);
542 window_set_drop_handler(dnd->window, dnd_drop_handler);
544 widget = window_get_widget(dnd->window);
545 widget_set_enter_handler(widget, dnd_enter_handler);
546 widget_set_motion_handler(widget, dnd_motion_handler);
548 width = 4 * (item_width + item_padding) + item_padding;
549 height = 4 * (item_height + item_padding) + item_padding;
550 window_set_child_size(dnd->window, width, height);
558 main(int argc, char *argv[])
562 d = display_create(&argc, &argv, NULL);
564 fprintf(stderr, "failed to create display: %m\n");