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 <gdk-pixbuf/gdk-pixbuf.h>
35 #include "wayland-client.h"
36 #include "wayland-glib.h"
40 static const char socket_name[] = "\0wayland";
43 struct window *window;
44 struct display *display;
46 struct item *items[16];
50 cairo_surface_t *translucent;
51 cairo_surface_t *opaque;
52 int hotspot_x, hotspot_y;
59 struct wl_array types;
60 const char *drag_type;
65 cairo_surface_t *surface;
69 static const int item_width = 64;
70 static const int item_height = 64;
71 static const int item_padding = 16;
74 item_create(struct display *display, int x, int y)
76 const int petal_count = 3 + random() % 5;
77 const double r1 = 20 + random() % 10;
78 const double r2 = 5 + random() % 12;
79 const double u = (10 + random() % 90) / 100.0;
80 const double v = (random() % 90) / 100.0;
84 double t, dt = 2 * M_PI / (petal_count * 2);
85 double x1, y1, x2, y2, x3, y3;
86 struct rectangle rect;
89 item = malloc(sizeof *item);
93 rect.width = item_width;
94 rect.height = item_height;
95 item->surface = display_create_surface(display, &rect);
100 cr = cairo_create(item->surface);
101 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
102 cairo_set_source_rgba(cr, 0, 0, 0, 0);
105 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
106 cairo_translate(cr, item_width / 2, item_height / 2);
108 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
109 for (i = 0; i < petal_count; i++, t += dt * 2) {
112 x2 = cos(t + dt) * r2;
113 y2 = sin(t + dt) * r2;
114 x3 = cos(t + 2 * dt) * r1;
115 y3 = sin(t + 2 * dt) * r1;
118 x1 - y1 * u, y1 + x1 * u,
119 x2 + y2 * v, y2 - x2 * v,
123 x2 - y2 * v, y2 + x2 * v,
124 x3 + y3 * u, y3 - x3 * u,
128 cairo_close_path(cr);
130 cairo_set_source_rgba(cr,
131 0.5 + (random() % 50) / 49.0,
132 0.5 + (random() % 50) / 49.0,
133 0.5 + (random() % 50) / 49.0,
134 0.5 + (random() % 100) / 99.0);
136 cairo_fill_preserve(cr);
138 cairo_set_line_width(cr, 1);
139 cairo_set_source_rgba(cr,
140 0.5 + (random() % 50) / 49.0,
141 0.5 + (random() % 50) / 49.0,
142 0.5 + (random() % 50) / 49.0,
143 0.5 + (random() % 100) / 99.0);
152 dnd_draw(struct dnd *dnd)
154 struct rectangle rectangle;
156 cairo_surface_t *wsurface, *surface;
159 window_draw(dnd->window);
161 window_get_child_rectangle(dnd->window, &rectangle);
163 wsurface = window_get_surface(dnd->window);
164 surface = cairo_surface_create_similar(wsurface,
165 CAIRO_CONTENT_COLOR_ALPHA,
169 cr = cairo_create(surface);
170 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
171 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
174 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
175 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
178 cairo_set_source_surface(cr, dnd->items[i]->surface,
179 dnd->items[i]->x, dnd->items[i]->y);
185 window_copy_surface(dnd->window, &rectangle, surface);
186 window_commit(dnd->window, dnd->key);
187 cairo_surface_destroy(surface);
191 redraw_handler(struct window *window, void *data)
193 struct dnd *dnd = data;
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_get_item(struct dnd *dnd, int32_t x, int32_t y)
211 struct rectangle rectangle;
214 window_get_child_rectangle(dnd->window, &rectangle);
219 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
220 item = dnd->items[i];
222 item->x <= x && x < item->x + item_width &&
223 item->y <= y && y < item->y + item_height)
231 drag_target(void *data,
232 struct wl_drag *drag, const char *mime_type)
234 struct dnd_drag *dnd_drag = data;
235 struct dnd *dnd = dnd_drag->dnd;
236 struct wl_input_device *device;
237 cairo_surface_t *surface;
238 struct wl_buffer *buffer;
240 fprintf(stderr, "target %s\n", mime_type);
241 device = input_get_input_device(dnd_drag->input);
243 surface = dnd_drag->opaque;
245 surface = dnd_drag->translucent;
247 buffer = display_get_buffer_for_surface(dnd->display, surface);
248 wl_input_device_attach(device, buffer,
249 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
253 drag_finish(void *data, struct wl_drag *drag, int fd)
255 struct dnd_drag *dnd_drag = data;
256 char text[] = "[drop data]";
258 fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
260 write(fd, text, sizeof text);
263 /* The 'finish' event marks the end of the session on the drag
264 * source side and we need to clean up the drag object created
265 * and the local state. */
266 wl_drag_destroy(drag);
267 cairo_surface_destroy(dnd_drag->translucent);
268 cairo_surface_destroy(dnd_drag->opaque);
272 static const struct wl_drag_listener drag_listener = {
278 drag_offer_offer(void *data,
279 struct wl_drag_offer *offer, const char *type)
281 struct dnd_offer *dnd_offer = data;
284 p = wl_array_add(&dnd_offer->types, sizeof *p);
290 drag_offer_pointer_focus(void *data,
291 struct wl_drag_offer *offer,
292 uint32_t time, struct wl_surface *surface,
293 int32_t x, int32_t y,
294 int32_t surface_x, int32_t surface_y)
296 struct dnd_offer *dnd_offer = data;
297 struct window *window;
300 /* The last event in a dnd session is pointer_focus with a
301 * NULL surface, whether or not we get the drop event. We
302 * need to clean up the dnd_offer proxy and whatever state we
305 fprintf(stderr, "pointer focus NULL, session over\n");
306 wl_array_release(&dnd_offer->types);
308 wl_drag_offer_destroy(offer);
312 fprintf(stderr, "drag pointer focus %p\n", surface);
313 fprintf(stderr, "offered types:\n");
314 end = dnd_offer->types.data + dnd_offer->types.size;
315 for (p = dnd_offer->types.data; p < end; p++)
316 fprintf(stderr, "\%s\n", *p);
318 window = wl_surface_get_user_data(surface);
319 dnd_offer->dnd = window_get_user_data(window);
321 if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
322 wl_drag_offer_accept(offer, time, "text/plain");
323 dnd_offer->drag_type = "text/plain";
325 wl_drag_offer_accept(offer, time, NULL);
326 dnd_offer->drag_type = NULL;
331 drag_offer_motion(void *data,
332 struct wl_drag_offer *offer, uint32_t time,
333 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
335 struct dnd_offer *dnd_offer = data;
336 struct dnd *dnd = dnd_offer->dnd;
338 if (!dnd_get_item(dnd, surface_x, surface_y)) {
339 fprintf(stderr, "drag offer motion %d, %d, accepting\n",
340 surface_x, surface_y);
341 wl_drag_offer_accept(offer, time, "text/plain");
342 dnd_offer->drag_type = "text/plain";
344 fprintf(stderr, "drag offer motion %d, %d, declining\n",
345 surface_x, surface_y);
346 wl_drag_offer_accept(offer, time, NULL);
347 dnd_offer->drag_type = NULL;
352 drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
354 struct dnd_offer *dnd_offer = data;
360 g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
361 fprintf(stderr, "read %d bytes: %s\n", len, buffer);
362 fd = g_io_channel_unix_get_fd(source);
364 g_source_remove(dnd_offer->tag);
366 g_io_channel_unref(source);
372 drag_offer_drop(void *data, struct wl_drag_offer *offer)
374 struct dnd_offer *dnd_offer = data;
378 if (!dnd_offer->drag_type) {
379 fprintf(stderr, "got 'drop', but no target\n");
380 /* FIXME: Should send response so compositor and
381 * source knows it's over. Can't send -1 to indicate
382 * 'no target' though becauses of the way fd passing
383 * currently works. */
387 fprintf(stderr, "got 'drop', sending write end of pipe\n");
390 wl_drag_offer_receive(offer, p[1]);
393 channel = g_io_channel_unix_new(p[0]);
394 dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
395 drop_io_func, dnd_offer);
398 static const struct wl_drag_offer_listener drag_offer_listener = {
400 drag_offer_pointer_focus,
406 drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
408 struct dnd_offer *dnd_offer;
410 dnd_offer = malloc(sizeof *dnd_offer);
411 if (dnd_offer == NULL)
414 wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
415 wl_array_init(&dnd_offer->types);
418 static cairo_surface_t *
419 create_drag_cursor(struct dnd_drag *dnd_drag,
420 struct item *item, int32_t x, int32_t y, double opacity)
422 struct dnd *dnd = dnd_drag->dnd;
423 cairo_surface_t *surface, *pointer;
424 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
425 struct rectangle rectangle;
426 cairo_pattern_t *pattern;
429 pointer = display_get_pointer_surface(dnd->display,
436 rectangle.width = item_width + 2 * pointer_width;
437 rectangle.height = item_height + 2 * pointer_height;
438 surface = display_create_surface(dnd->display, &rectangle);
440 cr = cairo_create(surface);
441 cairo_translate(cr, pointer_width, pointer_height);
443 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
444 cairo_set_source_rgba(cr, 0, 0, 0, 0);
447 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
448 cairo_set_source_surface(cr, item->surface, 0, 0);
449 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
450 cairo_mask(cr, pattern);
451 cairo_pattern_destroy(pattern);
453 cairo_set_source_surface(cr, pointer,
454 x - item->x - hotspot_x,
455 y - item->y - hotspot_y);
456 cairo_surface_destroy(pointer);
458 /* FIXME: more cairo-gl brokeness */
459 display_flush_cairo_device(dnd->display);
462 dnd_drag->hotspot_x = pointer_width + x - item->x;
463 dnd_drag->hotspot_y = pointer_height + y - item->y;
469 dnd_button_handler(struct window *window,
470 struct input *input, uint32_t time,
471 int button, int state, void *data)
473 struct dnd *dnd = data;
476 struct rectangle rectangle;
477 struct dnd_drag *dnd_drag;
479 window_get_child_rectangle(dnd->window, &rectangle);
480 input_get_position(input, &x, &y);
481 item = dnd_get_item(dnd, x, y);
485 if (item && state == 1) {
486 fprintf(stderr, "start drag, item %p\n", item);
488 dnd_drag = malloc(sizeof *dnd_drag);
490 dnd_drag->input = input;
493 create_drag_cursor(dnd_drag, item, x, y, 1);
494 dnd_drag->translucent =
495 create_drag_cursor(dnd_drag, item, x, y, 0.2);
497 window_start_drag(window, input, time,
498 &drag_listener, dnd_drag);
503 dnd_motion_handler(struct window *window,
504 struct input *input, uint32_t time,
505 int32_t x, int32_t y,
506 int32_t sx, int32_t sy, void *data)
508 struct dnd *dnd = data;
511 item = dnd_get_item(dnd, sx, sy);
514 return POINTER_HAND1;
516 return POINTER_LEFT_PTR;
520 dnd_create(struct display *display)
525 struct rectangle rectangle;
527 dnd = malloc(sizeof *dnd);
530 memset(dnd, 0, sizeof *dnd);
532 title = g_strdup_printf("Wayland Drag and Drop Demo");
534 dnd->window = window_create(display, title, 100, 100, 500, 400);
535 dnd->display = display;
538 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
539 x = (i % 4) * (item_width + item_padding) + item_padding;
540 y = (i / 4) * (item_height + item_padding) + item_padding;
541 if ((i ^ (i >> 2)) & 1)
542 dnd->items[i] = item_create(display, x, y);
544 dnd->items[i] = NULL;
547 window_set_user_data(dnd->window, dnd);
548 window_set_redraw_handler(dnd->window, redraw_handler);
549 window_set_keyboard_focus_handler(dnd->window,
550 keyboard_focus_handler);
551 window_set_button_handler(dnd->window,
554 window_set_motion_handler(dnd->window,
557 rectangle.width = 4 * (item_width + item_padding) + item_padding;
558 rectangle.height = 4 * (item_height + item_padding) + item_padding;
559 window_set_child_size(dnd->window, &rectangle);
566 static const GOptionEntry option_entries[] = {
571 main(int argc, char *argv[])
577 gettimeofday(&tv, NULL);
580 d = display_create(&argc, &argv, option_entries);
582 display_set_drag_offer_handler(d, drag_offer_handler);
584 dnd = dnd_create (d);