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,
170 cr = cairo_create(surface);
171 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
172 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
175 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
176 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
179 cairo_set_source_surface(cr, dnd->items[i]->surface,
180 dnd->items[i]->x, dnd->items[i]->y);
186 window_copy_surface(dnd->window, &rectangle, surface);
187 cairo_surface_destroy(surface);
188 window_flush(dnd->window);
192 redraw_handler(struct window *window, void *data)
194 struct dnd *dnd = data;
200 keyboard_focus_handler(struct window *window,
201 struct input *device, void *data)
203 struct dnd *dnd = data;
205 window_schedule_redraw(dnd->window);
209 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
212 struct rectangle rectangle;
215 window_get_child_rectangle(dnd->window, &rectangle);
220 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
221 item = dnd->items[i];
223 item->x <= x && x < item->x + item_width &&
224 item->y <= y && y < item->y + item_height)
232 drag_target(void *data,
233 struct wl_drag *drag, const char *mime_type)
235 struct dnd_drag *dnd_drag = data;
236 struct dnd *dnd = dnd_drag->dnd;
237 struct wl_input_device *device;
238 cairo_surface_t *surface;
239 struct wl_buffer *buffer;
241 fprintf(stderr, "target %s\n", mime_type);
242 device = input_get_input_device(dnd_drag->input);
244 surface = dnd_drag->opaque;
246 surface = dnd_drag->translucent;
248 buffer = display_get_buffer_for_surface(dnd->display, surface);
249 wl_input_device_attach(device, dnd_drag->time, buffer,
250 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
254 drag_finish(void *data, struct wl_drag *drag, int fd)
256 struct dnd_drag *dnd_drag = data;
257 char text[] = "[drop data]";
259 fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
261 write(fd, text, sizeof text);
264 /* The 'finish' event marks the end of the session on the drag
265 * source side and we need to clean up the drag object created
266 * and the local state. */
267 wl_drag_destroy(drag);
268 cairo_surface_destroy(dnd_drag->translucent);
269 cairo_surface_destroy(dnd_drag->opaque);
273 static const struct wl_drag_listener drag_listener = {
279 drag_offer_offer(void *data,
280 struct wl_drag_offer *offer, const char *type)
282 struct dnd_offer *dnd_offer = data;
285 p = wl_array_add(&dnd_offer->types, sizeof *p);
291 drag_offer_pointer_focus(void *data,
292 struct wl_drag_offer *offer,
293 uint32_t time, struct wl_surface *surface,
294 int32_t x, int32_t y,
295 int32_t surface_x, int32_t surface_y)
297 struct dnd_offer *dnd_offer = data;
298 struct window *window;
301 /* The last event in a dnd session is pointer_focus with a
302 * NULL surface, whether or not we get the drop event. We
303 * need to clean up the dnd_offer proxy and whatever state we
306 fprintf(stderr, "pointer focus NULL, session over\n");
307 wl_array_release(&dnd_offer->types);
309 wl_drag_offer_destroy(offer);
313 fprintf(stderr, "drag pointer focus %p\n", surface);
314 fprintf(stderr, "offered types:\n");
315 end = dnd_offer->types.data + dnd_offer->types.size;
316 for (p = dnd_offer->types.data; p < end; p++)
317 fprintf(stderr, "\%s\n", *p);
319 window = wl_surface_get_user_data(surface);
320 dnd_offer->dnd = window_get_user_data(window);
322 if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
323 wl_drag_offer_accept(offer, time, "text/plain");
324 dnd_offer->drag_type = "text/plain";
326 wl_drag_offer_accept(offer, time, NULL);
327 dnd_offer->drag_type = NULL;
332 drag_offer_motion(void *data,
333 struct wl_drag_offer *offer, uint32_t time,
334 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
336 struct dnd_offer *dnd_offer = data;
337 struct dnd *dnd = dnd_offer->dnd;
339 if (!dnd_get_item(dnd, surface_x, surface_y)) {
340 fprintf(stderr, "drag offer motion %d, %d, accepting\n",
341 surface_x, surface_y);
342 wl_drag_offer_accept(offer, time, "text/plain");
343 dnd_offer->drag_type = "text/plain";
345 fprintf(stderr, "drag offer motion %d, %d, declining\n",
346 surface_x, surface_y);
347 wl_drag_offer_accept(offer, time, NULL);
348 dnd_offer->drag_type = NULL;
353 drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
355 struct dnd_offer *dnd_offer = data;
361 g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
362 fprintf(stderr, "read %d bytes: %s\n", len, buffer);
363 fd = g_io_channel_unix_get_fd(source);
365 g_source_remove(dnd_offer->tag);
367 g_io_channel_unref(source);
373 drag_offer_drop(void *data, struct wl_drag_offer *offer)
375 struct dnd_offer *dnd_offer = data;
379 if (!dnd_offer->drag_type) {
380 fprintf(stderr, "got 'drop', but no target\n");
381 /* FIXME: Should send response so compositor and
382 * source knows it's over. Can't send -1 to indicate
383 * 'no target' though becauses of the way fd passing
384 * currently works. */
388 fprintf(stderr, "got 'drop', sending write end of pipe\n");
391 wl_drag_offer_receive(offer, p[1]);
394 channel = g_io_channel_unix_new(p[0]);
395 dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
396 drop_io_func, dnd_offer);
399 static const struct wl_drag_offer_listener drag_offer_listener = {
401 drag_offer_pointer_focus,
407 drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
409 struct dnd_offer *dnd_offer;
411 dnd_offer = malloc(sizeof *dnd_offer);
412 if (dnd_offer == NULL)
415 wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
416 wl_array_init(&dnd_offer->types);
419 static cairo_surface_t *
420 create_drag_cursor(struct dnd_drag *dnd_drag,
421 struct item *item, int32_t x, int32_t y, double opacity)
423 struct dnd *dnd = dnd_drag->dnd;
424 cairo_surface_t *surface, *pointer;
425 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
426 struct rectangle rectangle;
427 cairo_pattern_t *pattern;
430 pointer = display_get_pointer_surface(dnd->display,
437 rectangle.width = item_width + 2 * pointer_width;
438 rectangle.height = item_height + 2 * pointer_height;
439 surface = display_create_surface(dnd->display, &rectangle);
441 cr = cairo_create(surface);
442 cairo_translate(cr, pointer_width, pointer_height);
444 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
445 cairo_set_source_rgba(cr, 0, 0, 0, 0);
448 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
449 cairo_set_source_surface(cr, item->surface, 0, 0);
450 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
451 cairo_mask(cr, pattern);
452 cairo_pattern_destroy(pattern);
454 cairo_set_source_surface(cr, pointer,
455 x - item->x - hotspot_x,
456 y - item->y - hotspot_y);
457 cairo_surface_destroy(pointer);
459 /* FIXME: more cairo-gl brokeness */
460 display_flush_cairo_device(dnd->display);
463 dnd_drag->hotspot_x = pointer_width + x - item->x;
464 dnd_drag->hotspot_y = pointer_height + y - item->y;
470 dnd_button_handler(struct window *window,
471 struct input *input, uint32_t time,
472 int button, int state, void *data)
474 struct dnd *dnd = data;
477 struct rectangle rectangle;
478 struct dnd_drag *dnd_drag;
480 window_get_child_rectangle(dnd->window, &rectangle);
481 input_get_position(input, &x, &y);
482 item = dnd_get_item(dnd, x, y);
486 if (item && state == 1) {
487 fprintf(stderr, "start drag, item %p\n", item);
489 dnd_drag = malloc(sizeof *dnd_drag);
491 dnd_drag->input = input;
492 dnd_drag->time = time;
495 create_drag_cursor(dnd_drag, item, x, y, 1);
496 dnd_drag->translucent =
497 create_drag_cursor(dnd_drag, item, x, y, 0.2);
499 window_start_drag(window, input, time,
500 &drag_listener, dnd_drag);
505 dnd_motion_handler(struct window *window,
506 struct input *input, uint32_t time,
507 int32_t x, int32_t y,
508 int32_t sx, int32_t sy, void *data)
510 struct dnd *dnd = data;
513 item = dnd_get_item(dnd, sx, sy);
516 return POINTER_HAND1;
518 return POINTER_LEFT_PTR;
522 dnd_create(struct display *display)
527 struct rectangle rectangle;
529 dnd = malloc(sizeof *dnd);
532 memset(dnd, 0, sizeof *dnd);
534 title = g_strdup_printf("Wayland Drag and Drop Demo");
536 dnd->window = window_create(display, title, 100, 100, 500, 400);
537 dnd->display = display;
540 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
541 x = (i % 4) * (item_width + item_padding) + item_padding;
542 y = (i / 4) * (item_height + item_padding) + item_padding;
543 if ((i ^ (i >> 2)) & 1)
544 dnd->items[i] = item_create(display, x, y);
546 dnd->items[i] = NULL;
549 window_set_user_data(dnd->window, dnd);
550 window_set_redraw_handler(dnd->window, redraw_handler);
551 window_set_keyboard_focus_handler(dnd->window,
552 keyboard_focus_handler);
553 window_set_button_handler(dnd->window,
556 window_set_motion_handler(dnd->window,
559 rectangle.width = 4 * (item_width + item_padding) + item_padding;
560 rectangle.height = 4 * (item_height + item_padding) + item_padding;
561 window_set_child_size(dnd->window, &rectangle);
568 static const GOptionEntry option_entries[] = {
573 main(int argc, char *argv[])
579 gettimeofday(&tv, NULL);
582 d = display_create(&argc, &argv, option_entries);
584 display_set_drag_offer_handler(d, drag_offer_handler);
586 dnd = dnd_create (d);