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);
275 drag_reject(void *data, struct wl_drag *drag)
277 fprintf(stderr, "drop rejected\n");
280 static const struct wl_drag_listener drag_listener = {
287 drag_offer_offer(void *data,
288 struct wl_drag_offer *offer, const char *type)
290 struct dnd_offer *dnd_offer = data;
293 p = wl_array_add(&dnd_offer->types, sizeof *p);
299 drag_offer_pointer_focus(void *data,
300 struct wl_drag_offer *offer,
301 uint32_t time, struct wl_surface *surface,
302 int32_t x, int32_t y,
303 int32_t surface_x, int32_t surface_y)
305 struct dnd_offer *dnd_offer = data;
306 struct window *window;
309 /* The last event in a dnd session is pointer_focus with a
310 * NULL surface, whether or not we get the drop event. We
311 * need to clean up the dnd_offer proxy and whatever state we
314 fprintf(stderr, "pointer focus NULL, session over\n");
315 wl_array_release(&dnd_offer->types);
317 wl_drag_offer_destroy(offer);
321 fprintf(stderr, "drag pointer focus %p\n", surface);
322 fprintf(stderr, "offered types:\n");
323 end = dnd_offer->types.data + dnd_offer->types.size;
324 for (p = dnd_offer->types.data; p < end; p++)
325 fprintf(stderr, "\%s\n", *p);
327 window = wl_surface_get_user_data(surface);
328 dnd_offer->dnd = window_get_user_data(window);
330 if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
331 wl_drag_offer_accept(offer, time, "text/plain");
332 dnd_offer->drag_type = "text/plain";
334 wl_drag_offer_accept(offer, time, NULL);
335 dnd_offer->drag_type = NULL;
340 drag_offer_motion(void *data,
341 struct wl_drag_offer *offer, uint32_t time,
342 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
344 struct dnd_offer *dnd_offer = data;
345 struct dnd *dnd = dnd_offer->dnd;
347 if (!dnd_get_item(dnd, surface_x, surface_y)) {
348 fprintf(stderr, "drag offer motion %d, %d, accepting\n",
349 surface_x, surface_y);
350 wl_drag_offer_accept(offer, time, "text/plain");
351 dnd_offer->drag_type = "text/plain";
353 fprintf(stderr, "drag offer motion %d, %d, declining\n",
354 surface_x, surface_y);
355 wl_drag_offer_accept(offer, time, NULL);
356 dnd_offer->drag_type = NULL;
361 drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
363 struct dnd_offer *dnd_offer = data;
369 g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
370 fprintf(stderr, "read %d bytes: %s\n", len, buffer);
371 fd = g_io_channel_unix_get_fd(source);
373 g_source_remove(dnd_offer->tag);
375 g_io_channel_unref(source);
381 drag_offer_drop(void *data, struct wl_drag_offer *offer)
383 struct dnd_offer *dnd_offer = data;
387 if (!dnd_offer->drag_type) {
388 fprintf(stderr, "got 'drop', but no target\n");
389 wl_drag_offer_reject(offer);
393 fprintf(stderr, "got 'drop', sending write end of pipe\n");
396 wl_drag_offer_receive(offer, p[1]);
399 channel = g_io_channel_unix_new(p[0]);
400 dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
401 drop_io_func, dnd_offer);
404 static const struct wl_drag_offer_listener drag_offer_listener = {
406 drag_offer_pointer_focus,
412 drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
414 struct dnd_offer *dnd_offer;
416 dnd_offer = malloc(sizeof *dnd_offer);
417 if (dnd_offer == NULL)
420 wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
421 wl_array_init(&dnd_offer->types);
424 static cairo_surface_t *
425 create_drag_cursor(struct dnd_drag *dnd_drag,
426 struct item *item, int32_t x, int32_t y, double opacity)
428 struct dnd *dnd = dnd_drag->dnd;
429 cairo_surface_t *surface, *pointer;
430 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
431 struct rectangle rectangle;
432 cairo_pattern_t *pattern;
435 pointer = display_get_pointer_surface(dnd->display,
442 rectangle.width = item_width + 2 * pointer_width;
443 rectangle.height = item_height + 2 * pointer_height;
444 surface = display_create_surface(dnd->display, &rectangle);
446 cr = cairo_create(surface);
447 cairo_translate(cr, pointer_width, pointer_height);
449 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
450 cairo_set_source_rgba(cr, 0, 0, 0, 0);
453 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
454 cairo_set_source_surface(cr, item->surface, 0, 0);
455 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
456 cairo_mask(cr, pattern);
457 cairo_pattern_destroy(pattern);
459 cairo_set_source_surface(cr, pointer,
460 x - item->x - hotspot_x,
461 y - item->y - hotspot_y);
462 cairo_surface_destroy(pointer);
464 /* FIXME: more cairo-gl brokeness */
465 display_flush_cairo_device(dnd->display);
468 dnd_drag->hotspot_x = pointer_width + x - item->x;
469 dnd_drag->hotspot_y = pointer_height + y - item->y;
475 dnd_button_handler(struct window *window,
476 struct input *input, uint32_t time,
477 int button, int state, void *data)
479 struct dnd *dnd = data;
482 struct rectangle rectangle;
483 struct dnd_drag *dnd_drag;
484 struct wl_drag *drag;
486 window_get_child_rectangle(dnd->window, &rectangle);
487 input_get_position(input, &x, &y);
488 item = dnd_get_item(dnd, x, y);
492 if (item && state == 1) {
493 fprintf(stderr, "start drag, item %p\n", item);
495 dnd_drag = malloc(sizeof *dnd_drag);
497 dnd_drag->input = input;
498 dnd_drag->time = time;
501 create_drag_cursor(dnd_drag, item, x, y, 1);
502 dnd_drag->translucent =
503 create_drag_cursor(dnd_drag, item, x, y, 0.2);
505 drag = window_create_drag(window);
506 wl_drag_offer(drag, "text/plain");
507 wl_drag_offer(drag, "text/html");
508 window_activate_drag(drag, window, input, time);
509 wl_drag_add_listener(drag, &drag_listener, dnd_drag);
514 dnd_motion_handler(struct window *window,
515 struct input *input, uint32_t time,
516 int32_t x, int32_t y,
517 int32_t sx, int32_t sy, void *data)
519 struct dnd *dnd = data;
522 item = dnd_get_item(dnd, sx, sy);
525 return POINTER_HAND1;
527 return POINTER_LEFT_PTR;
531 dnd_create(struct display *display)
536 struct rectangle rectangle;
538 dnd = malloc(sizeof *dnd);
541 memset(dnd, 0, sizeof *dnd);
543 title = g_strdup_printf("Wayland Drag and Drop Demo");
545 dnd->window = window_create(display, title, 100, 100, 500, 400);
546 dnd->display = display;
549 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
550 x = (i % 4) * (item_width + item_padding) + item_padding;
551 y = (i / 4) * (item_height + item_padding) + item_padding;
552 if ((i ^ (i >> 2)) & 1)
553 dnd->items[i] = item_create(display, x, y);
555 dnd->items[i] = NULL;
558 window_set_user_data(dnd->window, dnd);
559 window_set_redraw_handler(dnd->window, redraw_handler);
560 window_set_keyboard_focus_handler(dnd->window,
561 keyboard_focus_handler);
562 window_set_button_handler(dnd->window,
565 window_set_motion_handler(dnd->window,
568 rectangle.width = 4 * (item_width + item_padding) + item_padding;
569 rectangle.height = 4 * (item_height + item_padding) + item_padding;
570 window_set_child_size(dnd->window, &rectangle);
577 static const GOptionEntry option_entries[] = {
582 main(int argc, char *argv[])
588 gettimeofday(&tv, NULL);
591 d = display_create(&argc, &argv, option_entries);
593 fprintf(stderr, "failed to create display: %m\n");
597 display_set_drag_offer_handler(d, drag_offer_handler);
599 dnd = dnd_create (d);