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;
60 struct wl_array types;
61 const char *drag_type;
66 cairo_surface_t *surface;
70 static const int item_width = 64;
71 static const int item_height = 64;
72 static const int item_padding = 16;
75 item_create(struct display *display, int x, int y)
77 const int petal_count = 3 + random() % 5;
78 const double r1 = 20 + random() % 10;
79 const double r2 = 5 + random() % 12;
80 const double u = (10 + random() % 90) / 100.0;
81 const double v = (random() % 90) / 100.0;
85 double t, dt = 2 * M_PI / (petal_count * 2);
86 double x1, y1, x2, y2, x3, y3;
87 struct rectangle rect;
90 item = malloc(sizeof *item);
94 rect.width = item_width;
95 rect.height = item_height;
96 item->surface = display_create_surface(display, &rect);
101 cr = cairo_create(item->surface);
102 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
103 cairo_set_source_rgba(cr, 0, 0, 0, 0);
106 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
107 cairo_translate(cr, item_width / 2, item_height / 2);
109 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
110 for (i = 0; i < petal_count; i++, t += dt * 2) {
113 x2 = cos(t + dt) * r2;
114 y2 = sin(t + dt) * r2;
115 x3 = cos(t + 2 * dt) * r1;
116 y3 = sin(t + 2 * dt) * r1;
119 x1 - y1 * u, y1 + x1 * u,
120 x2 + y2 * v, y2 - x2 * v,
124 x2 - y2 * v, y2 + x2 * v,
125 x3 + y3 * u, y3 - x3 * u,
129 cairo_close_path(cr);
131 cairo_set_source_rgba(cr,
132 0.5 + (random() % 50) / 49.0,
133 0.5 + (random() % 50) / 49.0,
134 0.5 + (random() % 50) / 49.0,
135 0.5 + (random() % 100) / 99.0);
137 cairo_fill_preserve(cr);
139 cairo_set_line_width(cr, 1);
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);
153 dnd_draw(struct dnd *dnd)
155 struct rectangle rectangle;
157 cairo_surface_t *wsurface, *surface;
160 window_draw(dnd->window);
162 window_get_child_rectangle(dnd->window, &rectangle);
164 wsurface = window_get_surface(dnd->window);
165 surface = cairo_surface_create_similar(wsurface,
166 CAIRO_CONTENT_COLOR_ALPHA,
169 cairo_surface_destroy(wsurface);
171 cr = cairo_create(surface);
172 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
173 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
176 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
177 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
180 cairo_set_source_surface(cr, dnd->items[i]->surface,
181 dnd->items[i]->x, dnd->items[i]->y);
187 window_copy_surface(dnd->window, &rectangle, surface);
188 cairo_surface_destroy(surface);
189 window_flush(dnd->window);
193 redraw_handler(struct window *window, void *data)
195 struct dnd *dnd = data;
201 keyboard_focus_handler(struct window *window,
202 struct input *device, void *data)
204 struct dnd *dnd = data;
206 window_schedule_redraw(dnd->window);
210 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
213 struct rectangle rectangle;
216 window_get_child_rectangle(dnd->window, &rectangle);
221 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
222 item = dnd->items[i];
224 item->x <= x && x < item->x + item_width &&
225 item->y <= y && y < item->y + item_height)
233 drag_target(void *data,
234 struct wl_drag *drag, const char *mime_type)
236 struct dnd_drag *dnd_drag = data;
237 struct dnd *dnd = dnd_drag->dnd;
238 struct wl_input_device *device;
239 cairo_surface_t *surface;
240 struct wl_buffer *buffer;
242 fprintf(stderr, "target %s\n", mime_type);
243 device = input_get_input_device(dnd_drag->input);
245 surface = dnd_drag->opaque;
247 surface = dnd_drag->translucent;
249 buffer = display_get_buffer_for_surface(dnd->display, surface);
250 wl_input_device_attach(device, dnd_drag->time, buffer,
251 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
255 drag_finish(void *data, struct wl_drag *drag, int fd)
257 struct dnd_drag *dnd_drag = data;
258 char text[] = "[drop data]";
260 fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
262 write(fd, text, sizeof text);
265 /* The 'finish' event marks the end of the session on the drag
266 * source side and we need to clean up the drag object created
267 * and the local state. */
268 wl_drag_destroy(drag);
269 cairo_surface_destroy(dnd_drag->translucent);
270 cairo_surface_destroy(dnd_drag->opaque);
274 static const struct wl_drag_listener drag_listener = {
280 drag_offer_offer(void *data,
281 struct wl_drag_offer *offer, const char *type)
283 struct dnd_offer *dnd_offer = data;
286 p = wl_array_add(&dnd_offer->types, sizeof *p);
292 drag_offer_pointer_focus(void *data,
293 struct wl_drag_offer *offer,
294 uint32_t time, struct wl_surface *surface,
295 int32_t x, int32_t y,
296 int32_t surface_x, int32_t surface_y)
298 struct dnd_offer *dnd_offer = data;
299 struct window *window;
302 /* The last event in a dnd session is pointer_focus with a
303 * NULL surface, whether or not we get the drop event. We
304 * need to clean up the dnd_offer proxy and whatever state we
307 fprintf(stderr, "pointer focus NULL, session over\n");
308 wl_array_release(&dnd_offer->types);
310 wl_drag_offer_destroy(offer);
314 fprintf(stderr, "drag pointer focus %p\n", surface);
315 fprintf(stderr, "offered types:\n");
316 end = dnd_offer->types.data + dnd_offer->types.size;
317 for (p = dnd_offer->types.data; p < end; p++)
318 fprintf(stderr, "\%s\n", *p);
320 window = wl_surface_get_user_data(surface);
321 dnd_offer->dnd = window_get_user_data(window);
323 if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
324 wl_drag_offer_accept(offer, time, "text/plain");
325 dnd_offer->drag_type = "text/plain";
327 wl_drag_offer_accept(offer, time, NULL);
328 dnd_offer->drag_type = NULL;
333 drag_offer_motion(void *data,
334 struct wl_drag_offer *offer, uint32_t time,
335 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
337 struct dnd_offer *dnd_offer = data;
338 struct dnd *dnd = dnd_offer->dnd;
340 if (!dnd_get_item(dnd, surface_x, surface_y)) {
341 fprintf(stderr, "drag offer motion %d, %d, accepting\n",
342 surface_x, surface_y);
343 wl_drag_offer_accept(offer, time, "text/plain");
344 dnd_offer->drag_type = "text/plain";
346 fprintf(stderr, "drag offer motion %d, %d, declining\n",
347 surface_x, surface_y);
348 wl_drag_offer_accept(offer, time, NULL);
349 dnd_offer->drag_type = NULL;
354 drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
356 struct dnd_offer *dnd_offer = data;
362 g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
363 fprintf(stderr, "read %d bytes: %s\n", len, buffer);
364 fd = g_io_channel_unix_get_fd(source);
366 g_source_remove(dnd_offer->tag);
368 g_io_channel_unref(source);
374 drag_offer_drop(void *data, struct wl_drag_offer *offer)
376 struct dnd_offer *dnd_offer = data;
380 if (!dnd_offer->drag_type) {
381 fprintf(stderr, "got 'drop', but no target\n");
382 /* FIXME: Should send response so compositor and
383 * source knows it's over. Can't send -1 to indicate
384 * 'no target' though becauses of the way fd passing
385 * currently works. */
389 fprintf(stderr, "got 'drop', sending write end of pipe\n");
392 wl_drag_offer_receive(offer, p[1]);
395 channel = g_io_channel_unix_new(p[0]);
396 dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
397 drop_io_func, dnd_offer);
400 static const struct wl_drag_offer_listener drag_offer_listener = {
402 drag_offer_pointer_focus,
408 drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
410 struct dnd_offer *dnd_offer;
412 dnd_offer = malloc(sizeof *dnd_offer);
413 if (dnd_offer == NULL)
416 wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
417 wl_array_init(&dnd_offer->types);
420 static cairo_surface_t *
421 create_drag_cursor(struct dnd_drag *dnd_drag,
422 struct item *item, int32_t x, int32_t y, double opacity)
424 struct dnd *dnd = dnd_drag->dnd;
425 cairo_surface_t *surface, *pointer;
426 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
427 struct rectangle rectangle;
428 cairo_pattern_t *pattern;
431 pointer = display_get_pointer_surface(dnd->display,
438 rectangle.width = item_width + 2 * pointer_width;
439 rectangle.height = item_height + 2 * pointer_height;
440 surface = display_create_surface(dnd->display, &rectangle);
442 cr = cairo_create(surface);
443 cairo_translate(cr, pointer_width, pointer_height);
445 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
446 cairo_set_source_rgba(cr, 0, 0, 0, 0);
449 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
450 cairo_set_source_surface(cr, item->surface, 0, 0);
451 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
452 cairo_mask(cr, pattern);
453 cairo_pattern_destroy(pattern);
455 cairo_set_source_surface(cr, pointer,
456 x - item->x - hotspot_x,
457 y - item->y - hotspot_y);
458 cairo_surface_destroy(pointer);
460 /* FIXME: more cairo-gl brokeness */
461 display_flush_cairo_device(dnd->display);
464 dnd_drag->hotspot_x = pointer_width + x - item->x;
465 dnd_drag->hotspot_y = pointer_height + y - item->y;
471 dnd_button_handler(struct window *window,
472 struct input *input, uint32_t time,
473 int button, int state, void *data)
475 struct dnd *dnd = data;
478 struct rectangle rectangle;
479 struct dnd_drag *dnd_drag;
480 struct wl_drag *drag;
482 window_get_child_rectangle(dnd->window, &rectangle);
483 input_get_position(input, &x, &y);
484 item = dnd_get_item(dnd, x, y);
488 if (item && state == 1) {
489 fprintf(stderr, "start drag, item %p\n", item);
491 dnd_drag = malloc(sizeof *dnd_drag);
493 dnd_drag->input = input;
494 dnd_drag->time = time;
497 create_drag_cursor(dnd_drag, item, x, y, 1);
498 dnd_drag->translucent =
499 create_drag_cursor(dnd_drag, item, x, y, 0.2);
501 drag = window_create_drag(window);
502 wl_drag_offer(drag, "text/plain");
503 wl_drag_offer(drag, "text/html");
504 window_activate_drag(drag, window, input, time);
505 wl_drag_add_listener(drag, &drag_listener, dnd_drag);
510 dnd_motion_handler(struct window *window,
511 struct input *input, uint32_t time,
512 int32_t x, int32_t y,
513 int32_t sx, int32_t sy, void *data)
515 struct dnd *dnd = data;
518 item = dnd_get_item(dnd, sx, sy);
521 return POINTER_HAND1;
523 return POINTER_LEFT_PTR;
527 dnd_create(struct display *display)
532 struct rectangle rectangle;
534 dnd = malloc(sizeof *dnd);
537 memset(dnd, 0, sizeof *dnd);
539 title = g_strdup_printf("Wayland Drag and Drop Demo");
541 dnd->window = window_create(display, title, 100, 100, 500, 400);
542 dnd->display = display;
545 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
546 x = (i % 4) * (item_width + item_padding) + item_padding;
547 y = (i / 4) * (item_height + item_padding) + item_padding;
548 if ((i ^ (i >> 2)) & 1)
549 dnd->items[i] = item_create(display, x, y);
551 dnd->items[i] = NULL;
554 window_set_user_data(dnd->window, dnd);
555 window_set_redraw_handler(dnd->window, redraw_handler);
556 window_set_keyboard_focus_handler(dnd->window,
557 keyboard_focus_handler);
558 window_set_button_handler(dnd->window,
561 window_set_motion_handler(dnd->window,
564 rectangle.width = 4 * (item_width + item_padding) + item_padding;
565 rectangle.height = 4 * (item_height + item_padding) + item_padding;
566 window_set_child_size(dnd->window, &rectangle);
573 static const GOptionEntry option_entries[] = {
578 main(int argc, char *argv[])
584 gettimeofday(&tv, NULL);
587 d = display_create(&argc, &argv, option_entries);
589 fprintf(stderr, "failed to create display: %m\n");
593 display_set_drag_offer_handler(d, drag_offer_handler);
595 dnd = dnd_create (d);