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;
84 static const char flower_mime_type[] = "application/x-wayland-dnd-flower";
85 static const char text_mime_type[] = "text/plain;charset=utf-8";
88 item_create(struct display *display, int x, int y, int seed)
93 item = malloc(sizeof *item);
98 gettimeofday(&tv, NULL);
99 item->seed = seed ? seed : tv.tv_usec;
102 const int petal_count = 3 + random() % 5;
103 const double r1 = 20 + random() % 10;
104 const double r2 = 5 + random() % 12;
105 const double u = (10 + random() % 90) / 100.0;
106 const double v = (random() % 90) / 100.0;
110 double t, dt = 2 * M_PI / (petal_count * 2);
111 double x1, y1, x2, y2, x3, y3;
112 struct rectangle rect;
115 rect.width = item_width;
116 rect.height = item_height;
118 display_create_surface(display, NULL, &rect, SURFACE_SHM);
123 cr = cairo_create(item->surface);
124 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
125 cairo_set_source_rgba(cr, 0, 0, 0, 0);
128 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
129 cairo_translate(cr, item_width / 2, item_height / 2);
131 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
132 for (i = 0; i < petal_count; i++, t += dt * 2) {
135 x2 = cos(t + dt) * r2;
136 y2 = sin(t + dt) * r2;
137 x3 = cos(t + 2 * dt) * r1;
138 y3 = sin(t + 2 * dt) * r1;
141 x1 - y1 * u, y1 + x1 * u,
142 x2 + y2 * v, y2 - x2 * v,
146 x2 - y2 * v, y2 + x2 * v,
147 x3 + y3 * u, y3 - x3 * u,
151 cairo_close_path(cr);
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);
159 cairo_fill_preserve(cr);
161 cairo_set_line_width(cr, 1);
162 cairo_set_source_rgba(cr,
163 0.5 + (random() % 50) / 49.0,
164 0.5 + (random() % 50) / 49.0,
165 0.5 + (random() % 50) / 49.0,
166 0.5 + (random() % 100) / 99.0);
175 dnd_redraw_handler(struct widget *widget, void *data)
177 struct dnd *dnd = data;
178 struct rectangle allocation;
180 cairo_surface_t *surface;
183 surface = window_get_surface(dnd->window);
184 cr = cairo_create(surface);
185 widget_get_allocation(dnd->widget, &allocation);
186 cairo_rectangle(cr, allocation.x, allocation.y,
187 allocation.width, allocation.height);
189 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
190 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
193 cairo_rectangle(cr, allocation.x, allocation.y,
194 allocation.width, allocation.height);
196 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
197 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
200 cairo_set_source_surface(cr, dnd->items[i]->surface,
201 dnd->items[i]->x + allocation.x,
202 dnd->items[i]->y + allocation.y);
207 cairo_surface_destroy(surface);
211 keyboard_focus_handler(struct window *window,
212 struct input *device, void *data)
214 struct dnd *dnd = data;
216 window_schedule_redraw(dnd->window);
220 dnd_add_item(struct dnd *dnd, struct item *item)
224 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
225 if (dnd->items[i] == 0) {
226 dnd->items[i] = item;
234 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
237 struct rectangle allocation;
240 widget_get_allocation(dnd->widget, &allocation);
245 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
246 item = dnd->items[i];
248 item->x <= x && x < item->x + item_width &&
249 item->y <= y && y < item->y + item_height)
257 data_source_target(void *data,
258 struct wl_data_source *source, const char *mime_type)
260 struct dnd_drag *dnd_drag = data;
261 struct dnd *dnd = dnd_drag->dnd;
262 cairo_surface_t *surface;
263 struct wl_buffer *buffer;
265 dnd_drag->mime_type = mime_type;
267 surface = dnd_drag->opaque;
269 surface = dnd_drag->translucent;
271 buffer = display_get_buffer_for_surface(dnd->display, surface);
272 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
273 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
274 dnd_drag->width, dnd_drag->height);
275 wl_surface_commit(dnd_drag->drag_surface);
279 data_source_send(void *data, struct wl_data_source *source,
280 const char *mime_type, int32_t fd)
282 struct dnd_flower_message dnd_flower_message;
283 struct dnd_drag *dnd_drag = data;
287 if (strcmp(mime_type, flower_mime_type) == 0) {
288 dnd_flower_message.seed = dnd_drag->item->seed;
289 dnd_flower_message.x_offset = dnd_drag->x_offset;
290 dnd_flower_message.y_offset = dnd_drag->y_offset;
292 if (write(fd, &dnd_flower_message,
293 sizeof dnd_flower_message) < 0)
295 } else if (strcmp(mime_type, text_mime_type) == 0) {
296 n = snprintf(buffer, sizeof buffer, "seed=%d x=%d y=%d\n",
297 dnd_drag->item->seed,
301 if (write(fd, buffer, n) < 0)
309 data_source_cancelled(void *data, struct wl_data_source *source)
311 struct dnd_drag *dnd_drag = data;
313 /* The 'cancelled' event means that the source is no longer in
314 * use by the drag (or current selection). We need to clean
315 * up the drag object created and the local state. */
317 wl_data_source_destroy(dnd_drag->data_source);
319 /* Destroy the item that has been dragged out */
320 cairo_surface_destroy(dnd_drag->item->surface);
321 free(dnd_drag->item);
323 wl_surface_destroy(dnd_drag->drag_surface);
325 cairo_surface_destroy(dnd_drag->translucent);
326 cairo_surface_destroy(dnd_drag->opaque);
330 static const struct wl_data_source_listener data_source_listener = {
333 data_source_cancelled
336 static cairo_surface_t *
337 create_drag_icon(struct dnd_drag *dnd_drag,
338 struct item *item, int32_t x, int32_t y, double opacity)
340 struct dnd *dnd = dnd_drag->dnd;
341 cairo_surface_t *surface;
342 struct rectangle rectangle;
343 cairo_pattern_t *pattern;
346 rectangle.width = item_width;
347 rectangle.height = item_height;
348 surface = display_create_surface(dnd->display, NULL, &rectangle,
351 cr = cairo_create(surface);
352 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
353 cairo_set_source_surface(cr, item->surface, 0, 0);
354 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
355 cairo_mask(cr, pattern);
356 cairo_pattern_destroy(pattern);
360 dnd_drag->hotspot_x = x - item->x;
361 dnd_drag->hotspot_y = y - item->y;
362 dnd_drag->width = rectangle.width;
363 dnd_drag->height = rectangle.height;
369 create_drag_source(struct dnd *dnd,
370 struct input *input, uint32_t time,
371 int32_t x, int32_t y)
374 struct rectangle allocation;
375 struct dnd_drag *dnd_drag;
376 struct display *display;
377 struct wl_compositor *compositor;
378 struct wl_buffer *buffer;
381 cairo_surface_t *icon;
383 widget_get_allocation(dnd->widget, &allocation);
384 item = dnd_get_item(dnd, x, y);
389 dnd_drag = xmalloc(sizeof *dnd_drag);
391 dnd_drag->input = input;
392 dnd_drag->time = time;
393 dnd_drag->item = item;
394 dnd_drag->x_offset = x - item->x;
395 dnd_drag->y_offset = y - item->y;
397 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
398 if (item == dnd->items[i]){
404 display = window_get_display(dnd->window);
405 compositor = display_get_compositor(display);
406 serial = display_get_serial(display);
407 dnd_drag->drag_surface =
408 wl_compositor_create_surface(compositor);
410 if (dnd->self_only) {
411 dnd_drag->data_source = NULL;
413 dnd_drag->data_source =
414 display_create_data_source(dnd->display);
415 wl_data_source_add_listener(dnd_drag->data_source,
416 &data_source_listener,
418 wl_data_source_offer(dnd_drag->data_source,
420 wl_data_source_offer(dnd_drag->data_source,
424 wl_data_device_start_drag(input_get_data_device(input),
425 dnd_drag->data_source,
426 window_get_wl_surface(dnd->window),
427 dnd_drag->drag_surface,
431 create_drag_icon(dnd_drag, item, x, y, 1);
432 dnd_drag->translucent =
433 create_drag_icon(dnd_drag, item, x, y, 0.2);
436 icon = dnd_drag->opaque;
438 icon = dnd_drag->translucent;
440 buffer = display_get_buffer_for_surface(dnd->display, icon);
441 wl_surface_attach(dnd_drag->drag_surface, buffer,
442 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
443 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
444 dnd_drag->width, dnd_drag->height);
445 wl_surface_commit(dnd_drag->drag_surface);
447 dnd->current_drag = dnd_drag;
448 window_schedule_redraw(dnd->window);
456 dnd_button_handler(struct widget *widget,
457 struct input *input, uint32_t time,
458 uint32_t button, enum wl_pointer_button_state state,
461 struct dnd *dnd = data;
464 input_get_position(input, &x, &y);
465 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
467 if (create_drag_source(dnd, input, time, x, y) == 0)
468 input_set_pointer_image(input, CURSOR_DRAGGING);
473 dnd_touch_down_handler(struct widget *widget,
474 struct input *input, uint32_t serial,
475 uint32_t time, int32_t id,
476 float x, float y, void *data)
478 struct dnd *dnd = data;
479 int32_t int_x, int_y;
486 if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
487 touch_grab(input, 0);
491 lookup_cursor(struct dnd *dnd, int x, int y)
495 item = dnd_get_item(dnd, x, y);
499 return CURSOR_LEFT_PTR;
503 dnd_enter_handler(struct widget *widget,
504 struct input *input, float x, float y, void *data)
506 struct dnd *dnd = data;
508 dnd->current_drag = NULL;
510 return lookup_cursor(dnd, x, y);
514 dnd_motion_handler(struct widget *widget,
515 struct input *input, uint32_t time,
516 float x, float y, void *data)
518 return lookup_cursor(data, x, y);
522 dnd_data_handler(struct window *window,
524 float x, float y, const char **types, void *data)
526 struct dnd *dnd = data;
527 int i, has_flower = 0;
531 for (i = 0; types[i]; i++)
532 if (strcmp(types[i], flower_mime_type) == 0)
535 if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
536 input_accept(input, NULL);
538 input_accept(input, flower_mime_type);
543 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
545 struct dnd *dnd = user_data;
546 struct dnd_flower_message *message = data;
548 struct rectangle allocation;
552 } else if (len != sizeof *message) {
553 fprintf(stderr, "odd message length %zu, expected %zu\n",
554 len, sizeof *message);
558 widget_get_allocation(dnd->widget, &allocation);
559 item = item_create(dnd->display,
560 x - message->x_offset - allocation.x,
561 y - message->y_offset - allocation.y,
564 dnd_add_item(dnd, item);
565 window_schedule_redraw(dnd->window);
569 dnd_drop_handler(struct window *window, struct input *input,
570 int32_t x, int32_t y, void *data)
572 struct dnd *dnd = data;
573 struct dnd_flower_message message;
575 if (dnd_get_item(dnd, x, y)) {
576 fprintf(stderr, "got 'drop', but no target\n");
580 if (!dnd->self_only) {
581 input_receive_drag_data(input,
583 dnd_receive_func, dnd);
584 } else if (dnd->current_drag) {
585 message.seed = dnd->current_drag->item->seed;
586 message.x_offset = dnd->current_drag->x_offset;
587 message.y_offset = dnd->current_drag->y_offset;
588 dnd_receive_func(&message, sizeof message, x, y, dnd);
589 dnd->current_drag = NULL;
591 fprintf(stderr, "ignoring drop from another client\n");
596 dnd_create(struct display *display)
600 int32_t width, height;
603 dnd = xzalloc(sizeof *dnd);
604 dnd->window = window_create(display);
605 dnd->widget = window_frame_create(dnd->window, dnd);
606 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
608 dnd->display = display;
611 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
612 x = (i % 4) * (item_width + item_padding) + item_padding;
613 y = (i / 4) * (item_height + item_padding) + item_padding;
614 if ((i ^ (i >> 2)) & 1)
615 dnd->items[i] = item_create(display, x, y, 0);
617 dnd->items[i] = NULL;
620 window_set_user_data(dnd->window, dnd);
621 window_set_keyboard_focus_handler(dnd->window,
622 keyboard_focus_handler);
623 window_set_data_handler(dnd->window, dnd_data_handler);
624 window_set_drop_handler(dnd->window, dnd_drop_handler);
626 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
627 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
628 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
629 widget_set_button_handler(dnd->widget, dnd_button_handler);
630 widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
632 width = 4 * (item_width + item_padding) + item_padding;
633 height = 4 * (item_height + item_padding) + item_padding;
635 window_frame_set_child_size(dnd->widget, width, height);
641 main(int argc, char *argv[])
647 d = display_create(&argc, argv);
649 fprintf(stderr, "failed to create display: %m\n");
655 for (i = 1; i < argc; i++)
656 if (strcmp("--self-only", argv[i]) == 0)