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;
53 cairo_surface_t *surface;
57 static const int item_width = 64;
58 static const int item_height = 64;
59 static const int item_padding = 16;
62 item_create(struct display *display, int x, int y)
64 const int petal_count = 3 + random() % 5;
65 const double r1 = 20 + random() % 10;
66 const double r2 = 5 + random() % 12;
67 const double u = (10 + random() % 90) / 100.0;
68 const double v = (random() % 90) / 100.0;
72 double t, dt = 2 * M_PI / (petal_count * 2);
73 double x1, y1, x2, y2, x3, y3;
74 struct rectangle rect;
77 item = malloc(sizeof *item);
81 rect.width = item_width;
82 rect.height = item_height;
83 item->surface = display_create_surface(display, &rect);
88 cr = cairo_create(item->surface);
89 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
90 cairo_set_source_rgba(cr, 0, 0, 0, 0);
93 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
94 cairo_translate(cr, item_width / 2, item_height / 2);
96 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
97 for (i = 0; i < petal_count; i++, t += dt * 2) {
100 x2 = cos(t + dt) * r2;
101 y2 = sin(t + dt) * r2;
102 x3 = cos(t + 2 * dt) * r1;
103 y3 = sin(t + 2 * dt) * r1;
106 x1 - y1 * u, y1 + x1 * u,
107 x2 + y2 * v, y2 - x2 * v,
111 x2 - y2 * v, y2 + x2 * v,
112 x3 + y3 * u, y3 - x3 * u,
116 cairo_close_path(cr);
118 cairo_set_source_rgba(cr,
119 0.5 + (random() % 50) / 49.0,
120 0.5 + (random() % 50) / 49.0,
121 0.5 + (random() % 50) / 49.0,
122 0.5 + (random() % 100) / 99.0);
124 cairo_fill_preserve(cr);
126 cairo_set_line_width(cr, 1);
127 cairo_set_source_rgba(cr,
128 0.5 + (random() % 50) / 49.0,
129 0.5 + (random() % 50) / 49.0,
130 0.5 + (random() % 50) / 49.0,
131 0.5 + (random() % 100) / 99.0);
140 dnd_draw(struct dnd *dnd)
142 struct rectangle rectangle;
144 cairo_surface_t *wsurface, *surface;
147 window_draw(dnd->window);
149 window_get_child_rectangle(dnd->window, &rectangle);
151 wsurface = window_get_surface(dnd->window);
152 surface = cairo_surface_create_similar(wsurface,
153 CAIRO_CONTENT_COLOR_ALPHA,
157 cr = cairo_create(surface);
158 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
159 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
162 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
163 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
166 cairo_set_source_surface(cr, dnd->items[i]->surface,
167 dnd->items[i]->x, dnd->items[i]->y);
173 window_copy_surface(dnd->window, &rectangle, surface);
174 window_commit(dnd->window, dnd->key);
175 cairo_surface_destroy(surface);
179 redraw_handler(struct window *window, void *data)
181 struct dnd *dnd = data;
187 keyboard_focus_handler(struct window *window,
188 struct input *device, void *data)
190 struct dnd *dnd = data;
192 window_schedule_redraw(dnd->window);
196 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
201 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
202 item = dnd->items[i];
204 item->x <= x && x < item->x + item_width &&
205 item->y <= y && y < item->y + item_height)
213 drag_handle_device(void *data,
214 struct wl_drag *drag, struct wl_input_device *device)
219 drag_pointer_focus(void *data,
220 struct wl_drag *drag,
221 uint32_t time, struct wl_surface *surface,
222 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
224 fprintf(stderr, "drag pointer focus %p\n", surface);
226 wl_drag_accept(drag, "text/plain");
230 drag_offer(void *data,
231 struct wl_drag *drag, const char *type)
233 fprintf(stderr, "drag offer %s\n", type);
237 drag_motion(void *data,
238 struct wl_drag *drag,
240 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
242 fprintf(stderr, "drag motion %d,%d\n", surface_x, surface_y);
244 /* FIXME: Need to correlate this with the offer event.
245 * Problem is, we don't know when we've seen that last offer
246 * event, and we might need to look at all of them before we
247 * can decide which one to go with. */
248 wl_drag_accept(drag, "text/plain");
252 drag_target(void *data,
253 struct wl_drag *drag, const char *mime_type)
255 struct dnd *dnd = data;
257 struct wl_input_device *device;
259 fprintf(stderr, "target %s\n", mime_type);
261 input = wl_drag_get_user_data(drag);
262 device = input_get_input_device(input);
263 wl_input_device_attach(device, dnd->buffer,
264 dnd->hotspot_x, dnd->hotspot_y);
268 drag_finish(void *data, struct wl_drag *drag)
270 fprintf(stderr, "drag finish\n");
272 char text[] = "[drop data]";
275 a.size = sizeof text;
277 wl_drag_send(drag, &a);
281 drag_data(void *data,
282 struct wl_drag *drag, struct wl_array *contents)
284 fprintf(stderr, "drag drop, data %s\n", (char *) contents->data);
287 static const struct wl_drag_listener drag_listener = {
298 dnd_button_handler(struct window *window,
299 struct input *input, uint32_t time,
300 int button, int state, void *data)
302 struct dnd *dnd = data;
303 int32_t x, y, hotspot_x, hotspot_y, pointer_width, pointer_height;
304 struct rectangle rectangle;
306 cairo_surface_t *surface, *pointer;
309 input_get_position(input, &x, &y);
310 window_get_child_rectangle(dnd->window, &rectangle);
314 item = dnd_get_item(dnd, x, y);
316 if (item && state == 1) {
317 fprintf(stderr, "start drag, item %p\n", item);
319 pointer = display_get_pointer_surface(dnd->display,
326 rectangle.width = item_width + 2 * pointer_width;
327 rectangle.height = item_height + 2 * pointer_height;
328 surface = display_create_surface(dnd->display, &rectangle);
330 cr = cairo_create(surface);
331 cairo_translate(cr, pointer_width, pointer_height);
333 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
334 cairo_set_source_rgba(cr, 0, 0, 0, 0);
337 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
338 cairo_set_source_surface(cr, item->surface, 0, 0);
341 cairo_set_source_surface(cr, pointer,
342 x - item->x - hotspot_x,
343 y - item->y - hotspot_y);
344 cairo_surface_destroy(pointer);
348 dnd->buffer = display_get_buffer_for_surface(dnd->display,
350 dnd->hotspot_x = pointer_width + x - item->x;
351 dnd->hotspot_y = pointer_height + y - item->y;
353 window_start_drag(window, input, time);
355 /* FIXME: We leak the surface because we can't free it
356 * until the server has referenced it. */
361 dnd_motion_handler(struct window *window,
362 struct input *input, uint32_t time,
363 int32_t x, int32_t y,
364 int32_t sx, int32_t sy, void *data)
366 struct dnd *dnd = data;
368 struct rectangle rectangle;
370 window_get_child_rectangle(dnd->window, &rectangle);
371 item = dnd_get_item(dnd, sx - rectangle.x, sy - rectangle.y);
374 return POINTER_HAND1;
376 return POINTER_LEFT_PTR;
380 dnd_create(struct display *display)
385 struct rectangle rectangle;
387 dnd = malloc(sizeof *dnd);
390 memset(dnd, 0, sizeof *dnd);
392 title = g_strdup_printf("Wayland Drag and Drop Demo");
394 dnd->window = window_create(display, title, 100, 100, 500, 400);
395 dnd->display = display;
398 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
399 x = (i % 4) * (item_width + item_padding) + item_padding;
400 y = (i / 4) * (item_height + item_padding) + item_padding;
401 if ((i ^ (i >> 2)) & 1)
402 dnd->items[i] = item_create(display, x, y);
404 dnd->items[i] = NULL;
407 window_set_user_data(dnd->window, dnd);
408 window_set_redraw_handler(dnd->window, redraw_handler);
409 window_set_keyboard_focus_handler(dnd->window,
410 keyboard_focus_handler);
411 window_set_button_handler(dnd->window,
414 window_set_motion_handler(dnd->window,
417 rectangle.width = 4 * (item_width + item_padding) + item_padding;
418 rectangle.height = 4 * (item_height + item_padding) + item_padding;
419 window_set_child_size(dnd->window, &rectangle);
421 display_add_drag_listener(display, &drag_listener, dnd);
428 static const GOptionEntry option_entries[] = {
433 main(int argc, char *argv[])
439 gettimeofday(&tv, NULL);
442 d = display_create(&argc, &argv, option_entries);
444 dnd = dnd_create (d);