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"
41 struct window *window;
42 struct widget *widget;
43 struct display *display;
45 struct item *items[16];
49 cairo_surface_t *translucent;
50 cairo_surface_t *opaque;
51 int hotspot_x, hotspot_y;
56 int x_offset, y_offset;
58 const char *mime_type;
60 struct wl_surface *drag_surface;
61 struct wl_data_source *data_source;
65 cairo_surface_t *surface;
70 struct dnd_flower_message {
71 int seed, x_offset, y_offset;
75 static const int item_width = 64;
76 static const int item_height = 64;
77 static const int item_padding = 16;
80 item_create(struct display *display, int x, int y, int seed)
85 item = malloc(sizeof *item);
90 gettimeofday(&tv, NULL);
91 item->seed = seed ? seed : tv.tv_usec;
94 const int petal_count = 3 + random() % 5;
95 const double r1 = 20 + random() % 10;
96 const double r2 = 5 + random() % 12;
97 const double u = (10 + random() % 90) / 100.0;
98 const double v = (random() % 90) / 100.0;
102 double t, dt = 2 * M_PI / (petal_count * 2);
103 double x1, y1, x2, y2, x3, y3;
104 struct rectangle rect;
107 rect.width = item_width;
108 rect.height = item_height;
109 item->surface = display_create_surface(display, NULL, &rect, 0);
114 cr = cairo_create(item->surface);
115 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
116 cairo_set_source_rgba(cr, 0, 0, 0, 0);
119 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
120 cairo_translate(cr, item_width / 2, item_height / 2);
122 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
123 for (i = 0; i < petal_count; i++, t += dt * 2) {
126 x2 = cos(t + dt) * r2;
127 y2 = sin(t + dt) * r2;
128 x3 = cos(t + 2 * dt) * r1;
129 y3 = sin(t + 2 * dt) * r1;
132 x1 - y1 * u, y1 + x1 * u,
133 x2 + y2 * v, y2 - x2 * v,
137 x2 - y2 * v, y2 + x2 * v,
138 x3 + y3 * u, y3 - x3 * u,
142 cairo_close_path(cr);
144 cairo_set_source_rgba(cr,
145 0.5 + (random() % 50) / 49.0,
146 0.5 + (random() % 50) / 49.0,
147 0.5 + (random() % 50) / 49.0,
148 0.5 + (random() % 100) / 99.0);
150 cairo_fill_preserve(cr);
152 cairo_set_line_width(cr, 1);
153 cairo_set_source_rgba(cr,
154 0.5 + (random() % 50) / 49.0,
155 0.5 + (random() % 50) / 49.0,
156 0.5 + (random() % 50) / 49.0,
157 0.5 + (random() % 100) / 99.0);
166 dnd_redraw_handler(struct widget *widget, void *data)
168 struct dnd *dnd = data;
169 struct rectangle allocation;
171 cairo_surface_t *surface;
174 surface = window_get_surface(dnd->window);
175 cr = cairo_create(surface);
176 widget_get_allocation(dnd->widget, &allocation);
177 cairo_rectangle(cr, allocation.x, allocation.y,
178 allocation.width, allocation.height);
180 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
181 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
184 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
185 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
188 cairo_set_source_surface(cr, dnd->items[i]->surface,
189 dnd->items[i]->x + allocation.x,
190 dnd->items[i]->y + allocation.y);
195 cairo_surface_destroy(surface);
199 keyboard_focus_handler(struct window *window,
200 struct input *device, void *data)
202 struct dnd *dnd = data;
204 window_schedule_redraw(dnd->window);
208 dnd_add_item(struct dnd *dnd, struct item *item)
212 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
213 if (dnd->items[i] == 0) {
214 dnd->items[i] = item;
222 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
225 struct rectangle allocation;
228 widget_get_allocation(dnd->widget, &allocation);
233 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
234 item = dnd->items[i];
236 item->x <= x && x < item->x + item_width &&
237 item->y <= y && y < item->y + item_height)
245 data_source_target(void *data,
246 struct wl_data_source *source, const char *mime_type)
248 struct dnd_drag *dnd_drag = data;
249 struct dnd *dnd = dnd_drag->dnd;
250 cairo_surface_t *surface;
251 struct wl_buffer *buffer;
253 dnd_drag->mime_type = mime_type;
255 surface = dnd_drag->opaque;
257 surface = dnd_drag->translucent;
259 buffer = display_get_buffer_for_surface(dnd->display, surface);
260 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
261 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
262 dnd_drag->width, dnd_drag->height);
266 data_source_send(void *data, struct wl_data_source *source,
267 const char *mime_type, int32_t fd)
269 struct dnd_flower_message dnd_flower_message;
270 struct dnd_drag *dnd_drag = data;
272 dnd_flower_message.seed = dnd_drag->item->seed;
273 dnd_flower_message.x_offset = dnd_drag->x_offset;
274 dnd_flower_message.y_offset = dnd_drag->y_offset;
276 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
283 data_source_cancelled(void *data, struct wl_data_source *source)
285 struct dnd_drag *dnd_drag = data;
287 /* The 'cancelled' event means that the source is no longer in
288 * use by the drag (or current selection). We need to clean
289 * up the drag object created and the local state. */
291 wl_data_source_destroy(dnd_drag->data_source);
293 /* Destroy the item that has been dragged out */
294 cairo_surface_destroy(dnd_drag->item->surface);
295 free(dnd_drag->item);
297 wl_surface_destroy(dnd_drag->drag_surface);
299 cairo_surface_destroy(dnd_drag->translucent);
300 cairo_surface_destroy(dnd_drag->opaque);
304 static const struct wl_data_source_listener data_source_listener = {
307 data_source_cancelled
310 static cairo_surface_t *
311 create_drag_cursor(struct dnd_drag *dnd_drag,
312 struct item *item, int32_t x, int32_t y, double opacity)
314 struct dnd *dnd = dnd_drag->dnd;
315 cairo_surface_t *surface;
316 struct wl_cursor_image *pointer;
317 struct rectangle rectangle;
318 cairo_pattern_t *pattern;
321 pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
323 rectangle.width = item_width + 2 * pointer->width;
324 rectangle.height = item_height + 2 * pointer->height;
325 surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
327 cr = cairo_create(surface);
328 cairo_translate(cr, pointer->width, pointer->height);
330 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
331 cairo_set_source_rgba(cr, 0, 0, 0, 0);
334 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
335 cairo_set_source_surface(cr, item->surface, 0, 0);
336 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
337 cairo_mask(cr, pattern);
338 cairo_pattern_destroy(pattern);
340 /* FIXME: more cairo-gl brokeness */
341 surface_flush_device(surface);
344 dnd_drag->hotspot_x = pointer->width + x - item->x;
345 dnd_drag->hotspot_y = pointer->height + y - item->y;
346 dnd_drag->width = rectangle.width;
347 dnd_drag->height = rectangle.height;
353 dnd_button_handler(struct widget *widget,
354 struct input *input, uint32_t time,
355 uint32_t button, uint32_t state, void *data)
357 struct dnd *dnd = data;
360 struct rectangle allocation;
361 struct dnd_drag *dnd_drag;
362 struct display *display;
363 struct wl_compositor *compositor;
364 struct wl_buffer *buffer;
368 widget_get_allocation(dnd->widget, &allocation);
369 input_get_position(input, &x, &y);
370 item = dnd_get_item(dnd, x, y);
374 if (item && state == 1) {
375 dnd_drag = malloc(sizeof *dnd_drag);
377 dnd_drag->input = input;
378 dnd_drag->time = time;
379 dnd_drag->item = item;
380 dnd_drag->x_offset = x - item->x;
381 dnd_drag->y_offset = y - item->y;
383 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
384 if (item == dnd->items[i]){
390 display = window_get_display(dnd->window);
391 compositor = display_get_compositor(display);
392 serial = display_get_serial(display);
393 dnd_drag->drag_surface =
394 wl_compositor_create_surface(compositor);
398 dnd_drag->data_source =
399 display_create_data_source(dnd->display);
400 wl_data_source_add_listener(dnd_drag->data_source,
401 &data_source_listener,
403 wl_data_source_offer(dnd_drag->data_source,
404 "application/x-wayland-dnd-flower");
405 wl_data_source_offer(dnd_drag->data_source,
406 "text/plain; charset=utf-8");
407 wl_data_device_start_drag(input_get_data_device(input),
408 dnd_drag->data_source,
409 window_get_wl_surface(dnd->window),
410 dnd_drag->drag_surface,
413 input_set_pointer_image(input, time, CURSOR_DRAGGING);
416 create_drag_cursor(dnd_drag, item, x, y, 1);
417 dnd_drag->translucent =
418 create_drag_cursor(dnd_drag, item, x, y, 0.2);
420 buffer = display_get_buffer_for_surface(dnd->display, dnd_drag->translucent);
421 wl_surface_attach(dnd_drag->drag_surface, buffer,
422 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
423 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
424 dnd_drag->width, dnd_drag->height);
426 window_schedule_redraw(dnd->window);
431 lookup_cursor(struct dnd *dnd, int x, int y)
435 item = dnd_get_item(dnd, x, y);
439 return CURSOR_LEFT_PTR;
443 dnd_enter_handler(struct widget *widget,
444 struct input *input, float x, float y, void *data)
446 return lookup_cursor(data, x, y);
450 dnd_motion_handler(struct widget *widget,
451 struct input *input, uint32_t time,
452 float x, float y, void *data)
454 return lookup_cursor(data, x, y);
458 dnd_data_handler(struct window *window,
460 float x, float y, const char **types, void *data)
462 struct dnd *dnd = data;
464 if (!dnd_get_item(dnd, x, y)) {
465 input_accept(input, types[0]);
467 input_accept(input, NULL);
472 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
474 struct dnd *dnd = user_data;
475 struct dnd_flower_message *message = data;
477 struct rectangle allocation;
481 } else if (len != sizeof *message) {
482 fprintf(stderr, "odd message length %ld, expected %ld\n",
483 len, sizeof *message);
487 widget_get_allocation(dnd->widget, &allocation);
488 item = item_create(dnd->display,
489 x - message->x_offset - allocation.x,
490 y - message->y_offset - allocation.y,
493 dnd_add_item(dnd, item);
494 window_schedule_redraw(dnd->window);
498 dnd_drop_handler(struct window *window, struct input *input,
499 int32_t x, int32_t y, void *data)
501 struct dnd *dnd = data;
503 if (dnd_get_item(dnd, x, y)) {
504 fprintf(stderr, "got 'drop', but no target\n");
508 input_receive_drag_data(input,
509 "application/x-wayland-dnd-flower",
510 dnd_receive_func, dnd);
514 dnd_create(struct display *display)
518 int32_t width, height;
521 dnd = malloc(sizeof *dnd);
524 memset(dnd, 0, sizeof *dnd);
526 dnd->window = window_create(display);
527 dnd->widget = frame_create(dnd->window, dnd);
528 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
530 dnd->display = display;
533 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
534 x = (i % 4) * (item_width + item_padding) + item_padding;
535 y = (i / 4) * (item_height + item_padding) + item_padding;
536 if ((i ^ (i >> 2)) & 1)
537 dnd->items[i] = item_create(display, x, y, 0);
539 dnd->items[i] = NULL;
542 window_set_user_data(dnd->window, dnd);
543 window_set_keyboard_focus_handler(dnd->window,
544 keyboard_focus_handler);
545 window_set_data_handler(dnd->window, dnd_data_handler);
546 window_set_drop_handler(dnd->window, dnd_drop_handler);
548 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
549 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
550 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
551 widget_set_button_handler(dnd->widget, dnd_button_handler);
553 width = 4 * (item_width + item_padding) + item_padding;
554 height = 4 * (item_height + item_padding) + item_padding;
556 widget_schedule_resize(dnd->widget, width, height);
562 main(int argc, char *argv[])
566 d = display_create(argc, argv);
568 fprintf(stderr, "failed to create display: %m\n");