blob: af8dc1cff4e08615bf84fe1838abff17e0af5e85 [file] [log] [blame]
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -04001/*
2 * Copyright © 2010 Kristian Høgsberg
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <sys/time.h>
31#include <cairo.h>
32#include <glib.h>
33#include <gdk-pixbuf/gdk-pixbuf.h>
34
35#include "wayland-client.h"
36#include "wayland-glib.h"
37
38#include "window.h"
39
40static const char socket_name[] = "\0wayland";
41
42struct dnd {
43 struct window *window;
44 struct display *display;
45 uint32_t key;
46 struct item *items[16];
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040047};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040048
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040049struct dnd_drag {
50 cairo_surface_t *translucent;
51 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040052 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040053 struct dnd *dnd;
54 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040055 uint32_t time;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040056};
57
58struct dnd_offer {
59 struct dnd *dnd;
60 struct wl_array types;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -040061 const char *drag_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040062 uint32_t tag;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040063};
64
65struct item {
66 cairo_surface_t *surface;
67 int x, y;
68};
69
70static const int item_width = 64;
71static const int item_height = 64;
72static const int item_padding = 16;
73
74static struct item *
75item_create(struct display *display, int x, int y)
76{
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;
82
83 cairo_t *cr;
84 int i;
85 double t, dt = 2 * M_PI / (petal_count * 2);
86 double x1, y1, x2, y2, x3, y3;
87 struct rectangle rect;
88 struct item *item;
89
90 item = malloc(sizeof *item);
91 if (item == NULL)
92 return NULL;
93
94 rect.width = item_width;
95 rect.height = item_height;
96 item->surface = display_create_surface(display, &rect);
97
98 item->x = x;
99 item->y = y;
100
101 cr = cairo_create(item->surface);
102 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
103 cairo_set_source_rgba(cr, 0, 0, 0, 0);
104 cairo_paint(cr);
105
106 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
107 cairo_translate(cr, item_width / 2, item_height / 2);
108 t = random();
109 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
110 for (i = 0; i < petal_count; i++, t += dt * 2) {
111 x1 = cos(t) * r1;
112 y1 = sin(t) * r1;
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;
117
118 cairo_curve_to(cr,
119 x1 - y1 * u, y1 + x1 * u,
120 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400121 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400122
123 cairo_curve_to(cr,
124 x2 - y2 * v, y2 + x2 * v,
125 x3 + y3 * u, y3 - x3 * u,
126 x3, y3);
127 }
128
129 cairo_close_path(cr);
130
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);
136
137 cairo_fill_preserve(cr);
138
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);
145 cairo_stroke(cr);
146
147 cairo_destroy(cr);
148
149 return item;
150}
151
152static void
153dnd_draw(struct dnd *dnd)
154{
155 struct rectangle rectangle;
156 cairo_t *cr;
157 cairo_surface_t *wsurface, *surface;
158 int i;
159
160 window_draw(dnd->window);
161
162 window_get_child_rectangle(dnd->window, &rectangle);
163
164 wsurface = window_get_surface(dnd->window);
165 surface = cairo_surface_create_similar(wsurface,
166 CAIRO_CONTENT_COLOR_ALPHA,
167 rectangle.width,
168 rectangle.height);
169
170 cr = cairo_create(surface);
171 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
172 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
173 cairo_paint(cr);
174
175 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
176 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
177 if (!dnd->items[i])
178 continue;
179 cairo_set_source_surface(cr, dnd->items[i]->surface,
180 dnd->items[i]->x, dnd->items[i]->y);
181 cairo_paint(cr);
182 }
183
184 cairo_destroy(cr);
185
186 window_copy_surface(dnd->window, &rectangle, surface);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400187 cairo_surface_destroy(surface);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400188 window_flush(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400189}
190
191static void
192redraw_handler(struct window *window, void *data)
193{
194 struct dnd *dnd = data;
195
196 dnd_draw(dnd);
197}
198
199static void
200keyboard_focus_handler(struct window *window,
201 struct input *device, void *data)
202{
203 struct dnd *dnd = data;
204
205 window_schedule_redraw(dnd->window);
206}
207
208static struct item *
209dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
210{
211 struct item *item;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400212 struct rectangle rectangle;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400213 int i;
214
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400215 window_get_child_rectangle(dnd->window, &rectangle);
216
217 x -= rectangle.x;
218 y -= rectangle.y;
219
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400220 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
221 item = dnd->items[i];
222 if (item &&
223 item->x <= x && x < item->x + item_width &&
224 item->y <= y && y < item->y + item_height)
225 return item;
226 }
227
228 return NULL;
229}
230
231static void
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400232drag_target(void *data,
233 struct wl_drag *drag, const char *mime_type)
234{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400235 struct dnd_drag *dnd_drag = data;
236 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400237 struct wl_input_device *device;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400238 cairo_surface_t *surface;
239 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400240
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400241 fprintf(stderr, "target %s\n", mime_type);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400242 device = input_get_input_device(dnd_drag->input);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400243 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400244 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400245 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400246 surface = dnd_drag->translucent;
247
248 buffer = display_get_buffer_for_surface(dnd->display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400249 wl_input_device_attach(device, dnd_drag->time, buffer,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400250 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
251}
252
253static void
254drag_finish(void *data, struct wl_drag *drag, int fd)
255{
256 struct dnd_drag *dnd_drag = data;
257 char text[] = "[drop data]";
258
259 fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
260
261 write(fd, text, sizeof text);
262 close(fd);
263
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);
270 free(dnd_drag);
271}
272
273static const struct wl_drag_listener drag_listener = {
274 drag_target,
275 drag_finish
276};
277
278static void
279drag_offer_offer(void *data,
280 struct wl_drag_offer *offer, const char *type)
281{
282 struct dnd_offer *dnd_offer = data;
283 char **p;
284
285 p = wl_array_add(&dnd_offer->types, sizeof *p);
286 if (p)
287 *p = strdup(type);
288}
289
290static void
291drag_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)
296{
297 struct dnd_offer *dnd_offer = data;
298 struct window *window;
299 char **p, **end;
300
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
304 * allocated. */
305 if (!surface) {
306 fprintf(stderr, "pointer focus NULL, session over\n");
307 wl_array_release(&dnd_offer->types);
308 free(dnd_offer);
309 wl_drag_offer_destroy(offer);
310 return;
311 }
312
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);
318
319 window = wl_surface_get_user_data(surface);
320 dnd_offer->dnd = window_get_user_data(window);
321
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";
325 } else {
326 wl_drag_offer_accept(offer, time, NULL);
327 dnd_offer->drag_type = NULL;
328 }
329}
330
331static void
332drag_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)
335{
336 struct dnd_offer *dnd_offer = data;
337 struct dnd *dnd = dnd_offer->dnd;
338
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";
344 } else {
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;
349 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400350}
351
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400352static gboolean
353drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400354{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400355 struct dnd_offer *dnd_offer = data;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400356 char buffer[256];
357 int fd;
358 unsigned int len;
359 GError *err = NULL;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400360
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400361 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);
364 close(fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400365 g_source_remove(dnd_offer->tag);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400366
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400367 g_io_channel_unref(source);
368
369 return TRUE;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400370}
371
372static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400373drag_offer_drop(void *data, struct wl_drag_offer *offer)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400374{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400375 struct dnd_offer *dnd_offer = data;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400376 GIOChannel *channel;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400377 int p[2];
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400378
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400379 if (!dnd_offer->drag_type) {
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400380 fprintf(stderr, "got 'drop', but no target\n");
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400381 /* 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. */
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400385 return;
386 }
387
388 fprintf(stderr, "got 'drop', sending write end of pipe\n");
389
390 pipe(p);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400391 wl_drag_offer_receive(offer, p[1]);
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400392 close(p[1]);
393
394 channel = g_io_channel_unix_new(p[0]);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400395 dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
396 drop_io_func, dnd_offer);
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400397}
398
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400399static const struct wl_drag_offer_listener drag_offer_listener = {
400 drag_offer_offer,
401 drag_offer_pointer_focus,
402 drag_offer_motion,
403 drag_offer_drop,
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400404};
405
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400406static void
407drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400408{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400409 struct dnd_offer *dnd_offer;
410
411 dnd_offer = malloc(sizeof *dnd_offer);
412 if (dnd_offer == NULL)
413 return;
414
415 wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
416 wl_array_init(&dnd_offer->types);
417}
418
419static cairo_surface_t *
420create_drag_cursor(struct dnd_drag *dnd_drag,
421 struct item *item, int32_t x, int32_t y, double opacity)
422{
423 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400424 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;
428 cairo_t *cr;
429
430 pointer = display_get_pointer_surface(dnd->display,
431 POINTER_DRAGGING,
432 &pointer_width,
433 &pointer_height,
434 &hotspot_x,
435 &hotspot_y);
436
437 rectangle.width = item_width + 2 * pointer_width;
438 rectangle.height = item_height + 2 * pointer_height;
439 surface = display_create_surface(dnd->display, &rectangle);
440
441 cr = cairo_create(surface);
442 cairo_translate(cr, pointer_width, pointer_height);
443
444 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
445 cairo_set_source_rgba(cr, 0, 0, 0, 0);
446 cairo_paint(cr);
447
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);
453
454 cairo_set_source_surface(cr, pointer,
455 x - item->x - hotspot_x,
456 y - item->y - hotspot_y);
457 cairo_surface_destroy(pointer);
458 cairo_paint(cr);
459 /* FIXME: more cairo-gl brokeness */
460 display_flush_cairo_device(dnd->display);
461 cairo_destroy(cr);
462
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400463 dnd_drag->hotspot_x = pointer_width + x - item->x;
464 dnd_drag->hotspot_y = pointer_height + y - item->y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400465
466 return surface;
467}
468
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400469static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400470dnd_button_handler(struct window *window,
471 struct input *input, uint32_t time,
472 int button, int state, void *data)
473{
474 struct dnd *dnd = data;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400475 int32_t x, y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400476 struct item *item;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400477 struct rectangle rectangle;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400478 struct dnd_drag *dnd_drag;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400479
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400480 window_get_child_rectangle(dnd->window, &rectangle);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400481 input_get_position(input, &x, &y);
482 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400483 x -= rectangle.x;
484 y -= rectangle.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400485
486 if (item && state == 1) {
487 fprintf(stderr, "start drag, item %p\n", item);
488
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400489 dnd_drag = malloc(sizeof *dnd_drag);
490 dnd_drag->dnd = dnd;
491 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400492 dnd_drag->time = time;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400493
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400494 dnd_drag->opaque =
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);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400498
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400499 window_start_drag(window, input, time,
500 &drag_listener, dnd_drag);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400501 }
502}
503
504static int
505dnd_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)
509{
510 struct dnd *dnd = data;
511 struct item *item;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400512
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400513 item = dnd_get_item(dnd, sx, sy);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400514
515 if (item)
516 return POINTER_HAND1;
517 else
518 return POINTER_LEFT_PTR;
519}
520
521static struct dnd *
522dnd_create(struct display *display)
523{
524 struct dnd *dnd;
525 gchar *title;
526 int i, x, y;
527 struct rectangle rectangle;
528
529 dnd = malloc(sizeof *dnd);
530 if (dnd == NULL)
531 return dnd;
532 memset(dnd, 0, sizeof *dnd);
533
534 title = g_strdup_printf("Wayland Drag and Drop Demo");
535
536 dnd->window = window_create(display, title, 100, 100, 500, 400);
537 dnd->display = display;
538 dnd->key = 100;
539
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);
545 else
546 dnd->items[i] = NULL;
547 }
548
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,
554 dnd_button_handler);
555
556 window_set_motion_handler(dnd->window,
557 dnd_motion_handler);
558
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);
562
563 dnd_draw(dnd);
564
565 return dnd;
566}
567
568static const GOptionEntry option_entries[] = {
569 { NULL }
570};
571
572int
573main(int argc, char *argv[])
574{
575 struct display *d;
576 struct dnd *dnd;
577 struct timeval tv;
578
579 gettimeofday(&tv, NULL);
580 srandom(tv.tv_usec);
581
582 d = display_create(&argc, &argv, option_entries);
583
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400584 display_set_drag_offer_handler(d, drag_offer_handler);
585
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400586 dnd = dnd_create (d);
587
588 display_run(d);
589
590 return 0;
591}