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];
44 struct widget *widget;
48 cairo_surface_t *translucent;
49 cairo_surface_t *opaque;
50 int hotspot_x, hotspot_y;
55 int x_offset, y_offset;
56 const char *mime_type;
58 struct wl_data_source *data_source;
62 cairo_surface_t *surface;
67 struct dnd_flower_message {
68 int seed, x_offset, y_offset;
72 static const int item_width = 64;
73 static const int item_height = 64;
74 static const int item_padding = 16;
77 item_create(struct display *display, int x, int y, int seed)
82 item = malloc(sizeof *item);
87 gettimeofday(&tv, NULL);
88 item->seed = seed ? seed : tv.tv_usec;
91 const int petal_count = 3 + random() % 5;
92 const double r1 = 20 + random() % 10;
93 const double r2 = 5 + random() % 12;
94 const double u = (10 + random() % 90) / 100.0;
95 const double v = (random() % 90) / 100.0;
99 double t, dt = 2 * M_PI / (petal_count * 2);
100 double x1, y1, x2, y2, x3, y3;
101 struct rectangle rect;
104 rect.width = item_width;
105 rect.height = item_height;
106 item->surface = display_create_surface(display, NULL, &rect, 0);
111 cr = cairo_create(item->surface);
112 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
113 cairo_set_source_rgba(cr, 0, 0, 0, 0);
116 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
117 cairo_translate(cr, item_width / 2, item_height / 2);
119 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
120 for (i = 0; i < petal_count; i++, t += dt * 2) {
123 x2 = cos(t + dt) * r2;
124 y2 = sin(t + dt) * r2;
125 x3 = cos(t + 2 * dt) * r1;
126 y3 = sin(t + 2 * dt) * r1;
129 x1 - y1 * u, y1 + x1 * u,
130 x2 + y2 * v, y2 - x2 * v,
134 x2 - y2 * v, y2 + x2 * v,
135 x3 + y3 * u, y3 - x3 * u,
139 cairo_close_path(cr);
141 cairo_set_source_rgba(cr,
142 0.5 + (random() % 50) / 49.0,
143 0.5 + (random() % 50) / 49.0,
144 0.5 + (random() % 50) / 49.0,
145 0.5 + (random() % 100) / 99.0);
147 cairo_fill_preserve(cr);
149 cairo_set_line_width(cr, 1);
150 cairo_set_source_rgba(cr,
151 0.5 + (random() % 50) / 49.0,
152 0.5 + (random() % 50) / 49.0,
153 0.5 + (random() % 50) / 49.0,
154 0.5 + (random() % 100) / 99.0);
163 dnd_draw(struct dnd *dnd)
165 struct rectangle allocation;
167 cairo_surface_t *surface;
170 window_draw(dnd->window);
172 surface = window_get_surface(dnd->window);
173 cr = cairo_create(surface);
174 window_get_child_allocation(dnd->window, &allocation);
175 cairo_rectangle(cr, allocation.x, allocation.y,
176 allocation.width, allocation.height);
178 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
179 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
182 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
183 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
186 cairo_set_source_surface(cr, dnd->items[i]->surface,
187 dnd->items[i]->x + allocation.x,
188 dnd->items[i]->y + allocation.y);
193 cairo_surface_destroy(surface);
194 window_flush(dnd->window);
198 redraw_handler(struct window *window, void *data)
200 struct dnd *dnd = data;
206 keyboard_focus_handler(struct window *window,
207 struct input *device, void *data)
209 struct dnd *dnd = data;
211 window_schedule_redraw(dnd->window);
215 dnd_add_item(struct dnd *dnd, struct item *item)
219 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
220 if (dnd->items[i] == 0) {
221 dnd->items[i] = item;
229 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
232 struct rectangle allocation;
235 window_get_child_allocation(dnd->window, &allocation);
240 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
241 item = dnd->items[i];
243 item->x <= x && x < item->x + item_width &&
244 item->y <= y && y < item->y + item_height)
252 data_source_target(void *data,
253 struct wl_data_source *source, const char *mime_type)
255 struct dnd_drag *dnd_drag = data;
256 struct dnd *dnd = dnd_drag->dnd;
257 cairo_surface_t *surface;
258 struct wl_buffer *buffer;
259 struct wl_data_device *device;
261 device = input_get_data_device(dnd_drag->input);
262 dnd_drag->mime_type = mime_type;
264 surface = dnd_drag->opaque;
266 surface = dnd_drag->translucent;
268 buffer = display_get_buffer_for_surface(dnd->display, surface);
269 wl_data_device_attach(device, dnd_drag->time, buffer,
270 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
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 write(fd, &dnd_flower_message, sizeof dnd_flower_message);
289 data_source_cancelled(void *data, struct wl_data_source *source)
291 struct dnd_drag *dnd_drag = data;
293 /* The 'cancelled' event means that the source is no longer in
294 * use by the drag (or current selection). We need to clean
295 * up the drag object created and the local state. */
297 wl_data_source_destroy(dnd_drag->data_source);
299 /* Destroy the item that has been dragged out */
300 cairo_surface_destroy(dnd_drag->item->surface);
301 free(dnd_drag->item);
303 cairo_surface_destroy(dnd_drag->translucent);
304 cairo_surface_destroy(dnd_drag->opaque);
308 static const struct wl_data_source_listener data_source_listener = {
311 data_source_cancelled
314 static cairo_surface_t *
315 create_drag_cursor(struct dnd_drag *dnd_drag,
316 struct item *item, int32_t x, int32_t y, double opacity)
318 struct dnd *dnd = dnd_drag->dnd;
319 cairo_surface_t *surface, *pointer;
320 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
321 struct rectangle rectangle;
322 cairo_pattern_t *pattern;
325 pointer = display_get_pointer_surface(dnd->display,
332 rectangle.width = item_width + 2 * pointer_width;
333 rectangle.height = item_height + 2 * pointer_height;
334 surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
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 cairo_set_source_surface(cr, pointer,
350 x - item->x - hotspot_x,
351 y - item->y - hotspot_y);
352 cairo_surface_destroy(pointer);
354 /* FIXME: more cairo-gl brokeness */
355 surface_flush_device(surface);
358 dnd_drag->hotspot_x = pointer_width + x - item->x;
359 dnd_drag->hotspot_y = pointer_height + y - item->y;
365 dnd_button_handler(struct window *window,
366 struct input *input, uint32_t time,
367 int button, int state, void *data)
369 struct dnd *dnd = data;
372 struct rectangle allocation;
373 struct dnd_drag *dnd_drag;
376 window_get_child_allocation(dnd->window, &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 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(window),
412 input_set_pointer_image(input, time, POINTER_DRAGGING);
415 create_drag_cursor(dnd_drag, item, x, y, 1);
416 dnd_drag->translucent =
417 create_drag_cursor(dnd_drag, item, x, y, 0.2);
419 window_schedule_redraw(dnd->window);
424 lookup_cursor(struct dnd *dnd, int x, int y)
428 item = dnd_get_item(dnd, x, y);
430 return POINTER_HAND1;
432 return POINTER_LEFT_PTR;
436 dnd_enter_handler(struct widget *widget,
437 struct input *input, uint32_t time,
438 int32_t x, int32_t y, void *data)
443 dnd_motion_handler(struct window *window,
444 struct input *input, uint32_t time,
445 int32_t x, int32_t y,
446 int32_t sx, int32_t sy, void *data)
448 return lookup_cursor(data, sx, sy);
452 dnd_data_handler(struct window *window,
453 struct input *input, uint32_t time,
454 int32_t x, int32_t y, const char **types, void *data)
456 struct dnd *dnd = data;
458 if (!dnd_get_item(dnd, x, y)) {
459 input_accept(input, time, types[0]);
461 input_accept(input, time, NULL);
466 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
468 struct dnd *dnd = user_data;
469 struct dnd_flower_message *message = data;
471 struct rectangle allocation;
475 } else if (len != sizeof *message) {
476 fprintf(stderr, "odd message length %ld, expected %ld\n",
477 len, sizeof *message);
481 window_get_child_allocation(dnd->window, &allocation);
482 item = item_create(dnd->display,
483 x - message->x_offset - allocation.x,
484 y - message->y_offset - allocation.y,
487 dnd_add_item(dnd, item);
488 window_schedule_redraw(dnd->window);
492 dnd_drop_handler(struct window *window, struct input *input,
493 int32_t x, int32_t y, void *data)
495 struct dnd *dnd = data;
497 if (dnd_get_item(dnd, x, y)) {
498 fprintf(stderr, "got 'drop', but no target\n");
502 input_receive_drag_data(input,
503 "application/x-wayland-dnd-flower",
504 dnd_receive_func, dnd);
508 dnd_create(struct display *display)
512 int32_t width, height;
514 dnd = malloc(sizeof *dnd);
517 memset(dnd, 0, sizeof *dnd);
519 dnd->window = window_create(display, 400, 400);
520 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
522 dnd->display = display;
525 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
526 x = (i % 4) * (item_width + item_padding) + item_padding;
527 y = (i / 4) * (item_height + item_padding) + item_padding;
528 if ((i ^ (i >> 2)) & 1)
529 dnd->items[i] = item_create(display, x, y, 0);
531 dnd->items[i] = NULL;
534 window_set_user_data(dnd->window, dnd);
535 window_set_redraw_handler(dnd->window, redraw_handler);
536 window_set_keyboard_focus_handler(dnd->window,
537 keyboard_focus_handler);
538 window_set_button_handler(dnd->window, dnd_button_handler);
539 window_set_motion_handler(dnd->window, dnd_motion_handler);
540 window_set_data_handler(dnd->window, dnd_data_handler);
541 window_set_drop_handler(dnd->window, dnd_drop_handler);
543 dnd->widget = window_add_widget(dnd->window, dnd);
544 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
546 width = 4 * (item_width + item_padding) + item_padding;
547 height = 4 * (item_height + item_padding) + item_padding;
548 window_set_child_size(dnd->window, width, height);
556 main(int argc, char *argv[])
560 d = display_create(&argc, &argv, NULL);
562 fprintf(stderr, "failed to create display: %m\n");