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];
48 struct wl_buffer *buffer;
49 int hotspot_x, hotspot_y;
51 const char *drag_type;
55 cairo_surface_t *surface;
59 static const int item_width = 64;
60 static const int item_height = 64;
61 static const int item_padding = 16;
64 item_create(struct display *display, int x, int y)
66 const int petal_count = 3 + random() % 5;
67 const double r1 = 20 + random() % 10;
68 const double r2 = 5 + random() % 12;
69 const double u = (10 + random() % 90) / 100.0;
70 const double v = (random() % 90) / 100.0;
74 double t, dt = 2 * M_PI / (petal_count * 2);
75 double x1, y1, x2, y2, x3, y3;
76 struct rectangle rect;
79 item = malloc(sizeof *item);
83 rect.width = item_width;
84 rect.height = item_height;
85 item->surface = display_create_surface(display, &rect);
90 cr = cairo_create(item->surface);
91 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
92 cairo_set_source_rgba(cr, 0, 0, 0, 0);
95 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
96 cairo_translate(cr, item_width / 2, item_height / 2);
98 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
99 for (i = 0; i < petal_count; i++, t += dt * 2) {
102 x2 = cos(t + dt) * r2;
103 y2 = sin(t + dt) * r2;
104 x3 = cos(t + 2 * dt) * r1;
105 y3 = sin(t + 2 * dt) * r1;
108 x1 - y1 * u, y1 + x1 * u,
109 x2 + y2 * v, y2 - x2 * v,
113 x2 - y2 * v, y2 + x2 * v,
114 x3 + y3 * u, y3 - x3 * u,
118 cairo_close_path(cr);
120 cairo_set_source_rgba(cr,
121 0.5 + (random() % 50) / 49.0,
122 0.5 + (random() % 50) / 49.0,
123 0.5 + (random() % 50) / 49.0,
124 0.5 + (random() % 100) / 99.0);
126 cairo_fill_preserve(cr);
128 cairo_set_line_width(cr, 1);
129 cairo_set_source_rgba(cr,
130 0.5 + (random() % 50) / 49.0,
131 0.5 + (random() % 50) / 49.0,
132 0.5 + (random() % 50) / 49.0,
133 0.5 + (random() % 100) / 99.0);
142 dnd_draw(struct dnd *dnd)
144 struct rectangle rectangle;
146 cairo_surface_t *wsurface, *surface;
149 window_draw(dnd->window);
151 window_get_child_rectangle(dnd->window, &rectangle);
153 wsurface = window_get_surface(dnd->window);
154 surface = cairo_surface_create_similar(wsurface,
155 CAIRO_CONTENT_COLOR_ALPHA,
159 cr = cairo_create(surface);
160 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
161 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
164 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
165 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
168 cairo_set_source_surface(cr, dnd->items[i]->surface,
169 dnd->items[i]->x, dnd->items[i]->y);
175 window_copy_surface(dnd->window, &rectangle, surface);
176 window_commit(dnd->window, dnd->key);
177 cairo_surface_destroy(surface);
181 redraw_handler(struct window *window, void *data)
183 struct dnd *dnd = data;
189 keyboard_focus_handler(struct window *window,
190 struct input *device, void *data)
192 struct dnd *dnd = data;
194 window_schedule_redraw(dnd->window);
198 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
203 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
204 item = dnd->items[i];
206 item->x <= x && x < item->x + item_width &&
207 item->y <= y && y < item->y + item_height)
215 drag_handle_device(void *data,
216 struct wl_drag *drag, struct wl_input_device *device)
221 drag_pointer_focus(void *data,
222 struct wl_drag *drag,
223 uint32_t time, struct wl_surface *surface,
224 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
226 struct dnd *dnd = data;
228 /* FIXME: We need the offered types before we get the
229 * pointer_focus event so we know which one we want and can
230 * send the accept request back. */
232 fprintf(stderr, "drag pointer focus %p\n", surface);
235 wl_drag_accept(drag, "text/plain");
236 dnd->drag_type = "text/plain";
238 dnd->drag_type = NULL;
243 drag_offer(void *data,
244 struct wl_drag *drag, const char *type)
246 fprintf(stderr, "drag offer %s\n", type);
250 drag_motion(void *data,
251 struct wl_drag *drag,
253 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
255 struct dnd *dnd = data;
257 /* FIXME: Need to correlate this with the offer event.
258 * Problem is, we don't know when we've seen that last offer
259 * event, and we might need to look at all of them before we
260 * can decide which one to go with. */
261 wl_drag_accept(drag, "text/plain");
262 dnd->drag_type = "text/plain";
266 drag_target(void *data,
267 struct wl_drag *drag, const char *mime_type)
269 struct dnd *dnd = data;
271 struct wl_input_device *device;
273 input = wl_drag_get_user_data(drag);
274 device = input_get_input_device(input);
275 wl_input_device_attach(device, dnd->buffer,
276 dnd->hotspot_x, dnd->hotspot_y);
280 drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
282 struct dnd *dnd = data;
288 g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
289 fprintf(stderr, "read %d bytes: %s\n", len, buffer);
290 fd = g_io_channel_unix_get_fd(source);
292 g_source_remove(dnd->tag);
294 g_io_channel_unref(source);
300 drag_drop(void *data, struct wl_drag *drag)
302 struct dnd *dnd = data;
306 if (!dnd->drag_type) {
307 fprintf(stderr, "got 'drop', but no target\n");
311 fprintf(stderr, "got 'drop', sending write end of pipe\n");
314 wl_drag_receive(drag, p[1]);
317 channel = g_io_channel_unix_new(p[0]);
318 dnd->tag = g_io_add_watch(channel, G_IO_IN, drop_io_func, dnd);
322 drag_finish(void *data, struct wl_drag *drag, int fd)
324 char text[] = "[drop data]";
326 fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
328 write(fd, text, sizeof text);
332 static const struct wl_drag_listener drag_listener = {
343 dnd_button_handler(struct window *window,
344 struct input *input, uint32_t time,
345 int button, int state, void *data)
347 struct dnd *dnd = data;
348 int32_t x, y, hotspot_x, hotspot_y, pointer_width, pointer_height;
349 struct rectangle rectangle;
351 cairo_surface_t *surface, *pointer;
354 input_get_position(input, &x, &y);
355 window_get_child_rectangle(dnd->window, &rectangle);
359 item = dnd_get_item(dnd, x, y);
361 if (item && state == 1) {
362 fprintf(stderr, "start drag, item %p\n", item);
364 pointer = display_get_pointer_surface(dnd->display,
371 rectangle.width = item_width + 2 * pointer_width;
372 rectangle.height = item_height + 2 * pointer_height;
373 surface = display_create_surface(dnd->display, &rectangle);
375 cr = cairo_create(surface);
376 cairo_translate(cr, pointer_width, pointer_height);
378 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
379 cairo_set_source_rgba(cr, 0, 0, 0, 0);
382 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
383 cairo_set_source_surface(cr, item->surface, 0, 0);
386 cairo_set_source_surface(cr, pointer,
387 x - item->x - hotspot_x,
388 y - item->y - hotspot_y);
389 cairo_surface_destroy(pointer);
393 dnd->buffer = display_get_buffer_for_surface(dnd->display,
395 dnd->hotspot_x = pointer_width + x - item->x;
396 dnd->hotspot_y = pointer_height + y - item->y;
398 window_start_drag(window, input, time);
400 /* FIXME: We leak the surface because we can't free it
401 * until the server has referenced it. */
406 dnd_motion_handler(struct window *window,
407 struct input *input, uint32_t time,
408 int32_t x, int32_t y,
409 int32_t sx, int32_t sy, void *data)
411 struct dnd *dnd = data;
413 struct rectangle rectangle;
415 window_get_child_rectangle(dnd->window, &rectangle);
416 item = dnd_get_item(dnd, sx - rectangle.x, sy - rectangle.y);
419 return POINTER_HAND1;
421 return POINTER_LEFT_PTR;
425 dnd_create(struct display *display)
430 struct rectangle rectangle;
432 dnd = malloc(sizeof *dnd);
435 memset(dnd, 0, sizeof *dnd);
437 title = g_strdup_printf("Wayland Drag and Drop Demo");
439 dnd->window = window_create(display, title, 100, 100, 500, 400);
440 dnd->display = display;
443 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
444 x = (i % 4) * (item_width + item_padding) + item_padding;
445 y = (i / 4) * (item_height + item_padding) + item_padding;
446 if ((i ^ (i >> 2)) & 1)
447 dnd->items[i] = item_create(display, x, y);
449 dnd->items[i] = NULL;
452 window_set_user_data(dnd->window, dnd);
453 window_set_redraw_handler(dnd->window, redraw_handler);
454 window_set_keyboard_focus_handler(dnd->window,
455 keyboard_focus_handler);
456 window_set_button_handler(dnd->window,
459 window_set_motion_handler(dnd->window,
462 rectangle.width = 4 * (item_width + item_padding) + item_padding;
463 rectangle.height = 4 * (item_height + item_padding) + item_padding;
464 window_set_child_size(dnd->window, &rectangle);
466 display_add_drag_listener(display, &drag_listener, dnd);
473 static const GOptionEntry option_entries[] = {
478 main(int argc, char *argv[])
484 gettimeofday(&tv, NULL);
487 d = display_create(&argc, &argv, option_entries);
489 dnd = dnd_create (d);