blob: dedf353a89df631395ea94b23ed08d38d50ec72f [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);
Kristian Høgsberg012a0072010-10-26 00:02:20 -0400169 cairo_surface_destroy(wsurface);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400170
171 cr = cairo_create(surface);
172 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
173 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
174 cairo_paint(cr);
175
176 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
177 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
178 if (!dnd->items[i])
179 continue;
180 cairo_set_source_surface(cr, dnd->items[i]->surface,
181 dnd->items[i]->x, dnd->items[i]->y);
182 cairo_paint(cr);
183 }
184
185 cairo_destroy(cr);
186
187 window_copy_surface(dnd->window, &rectangle, surface);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400188 cairo_surface_destroy(surface);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400189 window_flush(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400190}
191
192static void
193redraw_handler(struct window *window, void *data)
194{
195 struct dnd *dnd = data;
196
197 dnd_draw(dnd);
198}
199
200static void
201keyboard_focus_handler(struct window *window,
202 struct input *device, void *data)
203{
204 struct dnd *dnd = data;
205
206 window_schedule_redraw(dnd->window);
207}
208
209static struct item *
210dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
211{
212 struct item *item;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400213 struct rectangle rectangle;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400214 int i;
215
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400216 window_get_child_rectangle(dnd->window, &rectangle);
217
218 x -= rectangle.x;
219 y -= rectangle.y;
220
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400221 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
222 item = dnd->items[i];
223 if (item &&
224 item->x <= x && x < item->x + item_width &&
225 item->y <= y && y < item->y + item_height)
226 return item;
227 }
228
229 return NULL;
230}
231
232static void
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400233drag_target(void *data,
234 struct wl_drag *drag, const char *mime_type)
235{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400236 struct dnd_drag *dnd_drag = data;
237 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400238 struct wl_input_device *device;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400239 cairo_surface_t *surface;
240 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400241
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400242 fprintf(stderr, "target %s\n", mime_type);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400243 device = input_get_input_device(dnd_drag->input);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400244 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400245 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400246 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400247 surface = dnd_drag->translucent;
248
249 buffer = display_get_buffer_for_surface(dnd->display, surface);
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400250 wl_input_device_attach(device, dnd_drag->time, buffer,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400251 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
252}
253
254static void
255drag_finish(void *data, struct wl_drag *drag, int fd)
256{
257 struct dnd_drag *dnd_drag = data;
258 char text[] = "[drop data]";
259
260 fprintf(stderr, "got 'finish', fd %d, sending message\n", fd);
261
262 write(fd, text, sizeof text);
263 close(fd);
264
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);
271 free(dnd_drag);
272}
273
274static const struct wl_drag_listener drag_listener = {
275 drag_target,
276 drag_finish
277};
278
279static void
280drag_offer_offer(void *data,
281 struct wl_drag_offer *offer, const char *type)
282{
283 struct dnd_offer *dnd_offer = data;
284 char **p;
285
286 p = wl_array_add(&dnd_offer->types, sizeof *p);
287 if (p)
288 *p = strdup(type);
289}
290
291static void
292drag_offer_pointer_focus(void *data,
293 struct wl_drag_offer *offer,
294 uint32_t time, struct wl_surface *surface,
295 int32_t x, int32_t y,
296 int32_t surface_x, int32_t surface_y)
297{
298 struct dnd_offer *dnd_offer = data;
299 struct window *window;
300 char **p, **end;
301
302 /* The last event in a dnd session is pointer_focus with a
303 * NULL surface, whether or not we get the drop event. We
304 * need to clean up the dnd_offer proxy and whatever state we
305 * allocated. */
306 if (!surface) {
307 fprintf(stderr, "pointer focus NULL, session over\n");
308 wl_array_release(&dnd_offer->types);
309 free(dnd_offer);
310 wl_drag_offer_destroy(offer);
311 return;
312 }
313
314 fprintf(stderr, "drag pointer focus %p\n", surface);
315 fprintf(stderr, "offered types:\n");
316 end = dnd_offer->types.data + dnd_offer->types.size;
317 for (p = dnd_offer->types.data; p < end; p++)
318 fprintf(stderr, "\%s\n", *p);
319
320 window = wl_surface_get_user_data(surface);
321 dnd_offer->dnd = window_get_user_data(window);
322
323 if (!dnd_get_item(dnd_offer->dnd, surface_x, surface_y)) {
324 wl_drag_offer_accept(offer, time, "text/plain");
325 dnd_offer->drag_type = "text/plain";
326 } else {
327 wl_drag_offer_accept(offer, time, NULL);
328 dnd_offer->drag_type = NULL;
329 }
330}
331
332static void
333drag_offer_motion(void *data,
334 struct wl_drag_offer *offer, uint32_t time,
335 int32_t x, int32_t y, int32_t surface_x, int32_t surface_y)
336{
337 struct dnd_offer *dnd_offer = data;
338 struct dnd *dnd = dnd_offer->dnd;
339
340 if (!dnd_get_item(dnd, surface_x, surface_y)) {
341 fprintf(stderr, "drag offer motion %d, %d, accepting\n",
342 surface_x, surface_y);
343 wl_drag_offer_accept(offer, time, "text/plain");
344 dnd_offer->drag_type = "text/plain";
345 } else {
346 fprintf(stderr, "drag offer motion %d, %d, declining\n",
347 surface_x, surface_y);
348 wl_drag_offer_accept(offer, time, NULL);
349 dnd_offer->drag_type = NULL;
350 }
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400351}
352
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400353static gboolean
354drop_io_func(GIOChannel *source, GIOCondition condition, gpointer data)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400355{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400356 struct dnd_offer *dnd_offer = data;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400357 char buffer[256];
358 int fd;
359 unsigned int len;
360 GError *err = NULL;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400361
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400362 g_io_channel_read_chars(source, buffer, sizeof buffer, &len, &err);
363 fprintf(stderr, "read %d bytes: %s\n", len, buffer);
364 fd = g_io_channel_unix_get_fd(source);
365 close(fd);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400366 g_source_remove(dnd_offer->tag);
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400367
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400368 g_io_channel_unref(source);
369
370 return TRUE;
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400371}
372
373static void
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400374drag_offer_drop(void *data, struct wl_drag_offer *offer)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400375{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400376 struct dnd_offer *dnd_offer = data;
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400377 GIOChannel *channel;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400378 int p[2];
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400379
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400380 if (!dnd_offer->drag_type) {
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400381 fprintf(stderr, "got 'drop', but no target\n");
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400382 /* FIXME: Should send response so compositor and
383 * source knows it's over. Can't send -1 to indicate
384 * 'no target' though becauses of the way fd passing
385 * currently works. */
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400386 return;
387 }
388
389 fprintf(stderr, "got 'drop', sending write end of pipe\n");
390
391 pipe(p);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400392 wl_drag_offer_receive(offer, p[1]);
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400393 close(p[1]);
394
395 channel = g_io_channel_unix_new(p[0]);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400396 dnd_offer->tag = g_io_add_watch(channel, G_IO_IN,
397 drop_io_func, dnd_offer);
Kristian Høgsberg4eb53602010-08-27 20:29:56 -0400398}
399
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400400static const struct wl_drag_offer_listener drag_offer_listener = {
401 drag_offer_offer,
402 drag_offer_pointer_focus,
403 drag_offer_motion,
404 drag_offer_drop,
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400405};
406
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400407static void
408drag_offer_handler(struct wl_drag_offer *offer, struct display *display)
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400409{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400410 struct dnd_offer *dnd_offer;
411
412 dnd_offer = malloc(sizeof *dnd_offer);
413 if (dnd_offer == NULL)
414 return;
415
416 wl_drag_offer_add_listener(offer, &drag_offer_listener, dnd_offer);
417 wl_array_init(&dnd_offer->types);
418}
419
420static cairo_surface_t *
421create_drag_cursor(struct dnd_drag *dnd_drag,
422 struct item *item, int32_t x, int32_t y, double opacity)
423{
424 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400425 cairo_surface_t *surface, *pointer;
426 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
427 struct rectangle rectangle;
428 cairo_pattern_t *pattern;
429 cairo_t *cr;
430
431 pointer = display_get_pointer_surface(dnd->display,
432 POINTER_DRAGGING,
433 &pointer_width,
434 &pointer_height,
435 &hotspot_x,
436 &hotspot_y);
437
438 rectangle.width = item_width + 2 * pointer_width;
439 rectangle.height = item_height + 2 * pointer_height;
440 surface = display_create_surface(dnd->display, &rectangle);
441
442 cr = cairo_create(surface);
443 cairo_translate(cr, pointer_width, pointer_height);
444
445 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
446 cairo_set_source_rgba(cr, 0, 0, 0, 0);
447 cairo_paint(cr);
448
449 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
450 cairo_set_source_surface(cr, item->surface, 0, 0);
451 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
452 cairo_mask(cr, pattern);
453 cairo_pattern_destroy(pattern);
454
455 cairo_set_source_surface(cr, pointer,
456 x - item->x - hotspot_x,
457 y - item->y - hotspot_y);
458 cairo_surface_destroy(pointer);
459 cairo_paint(cr);
460 /* FIXME: more cairo-gl brokeness */
461 display_flush_cairo_device(dnd->display);
462 cairo_destroy(cr);
463
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400464 dnd_drag->hotspot_x = pointer_width + x - item->x;
465 dnd_drag->hotspot_y = pointer_height + y - item->y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400466
467 return surface;
468}
469
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400470static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400471dnd_button_handler(struct window *window,
472 struct input *input, uint32_t time,
473 int button, int state, void *data)
474{
475 struct dnd *dnd = data;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400476 int32_t x, y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400477 struct item *item;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400478 struct rectangle rectangle;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400479 struct dnd_drag *dnd_drag;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400480
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400481 window_get_child_rectangle(dnd->window, &rectangle);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400482 input_get_position(input, &x, &y);
483 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400484 x -= rectangle.x;
485 y -= rectangle.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400486
487 if (item && state == 1) {
488 fprintf(stderr, "start drag, item %p\n", item);
489
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400490 dnd_drag = malloc(sizeof *dnd_drag);
491 dnd_drag->dnd = dnd;
492 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400493 dnd_drag->time = time;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400494
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400495 dnd_drag->opaque =
496 create_drag_cursor(dnd_drag, item, x, y, 1);
497 dnd_drag->translucent =
498 create_drag_cursor(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400499
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400500 window_start_drag(window, input, time,
501 &drag_listener, dnd_drag);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400502 }
503}
504
505static int
506dnd_motion_handler(struct window *window,
507 struct input *input, uint32_t time,
508 int32_t x, int32_t y,
509 int32_t sx, int32_t sy, void *data)
510{
511 struct dnd *dnd = data;
512 struct item *item;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400513
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400514 item = dnd_get_item(dnd, sx, sy);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400515
516 if (item)
517 return POINTER_HAND1;
518 else
519 return POINTER_LEFT_PTR;
520}
521
522static struct dnd *
523dnd_create(struct display *display)
524{
525 struct dnd *dnd;
526 gchar *title;
527 int i, x, y;
528 struct rectangle rectangle;
529
530 dnd = malloc(sizeof *dnd);
531 if (dnd == NULL)
532 return dnd;
533 memset(dnd, 0, sizeof *dnd);
534
535 title = g_strdup_printf("Wayland Drag and Drop Demo");
536
537 dnd->window = window_create(display, title, 100, 100, 500, 400);
538 dnd->display = display;
539 dnd->key = 100;
540
541 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
542 x = (i % 4) * (item_width + item_padding) + item_padding;
543 y = (i / 4) * (item_height + item_padding) + item_padding;
544 if ((i ^ (i >> 2)) & 1)
545 dnd->items[i] = item_create(display, x, y);
546 else
547 dnd->items[i] = NULL;
548 }
549
550 window_set_user_data(dnd->window, dnd);
551 window_set_redraw_handler(dnd->window, redraw_handler);
552 window_set_keyboard_focus_handler(dnd->window,
553 keyboard_focus_handler);
554 window_set_button_handler(dnd->window,
555 dnd_button_handler);
556
557 window_set_motion_handler(dnd->window,
558 dnd_motion_handler);
559
560 rectangle.width = 4 * (item_width + item_padding) + item_padding;
561 rectangle.height = 4 * (item_height + item_padding) + item_padding;
562 window_set_child_size(dnd->window, &rectangle);
563
564 dnd_draw(dnd);
565
566 return dnd;
567}
568
569static const GOptionEntry option_entries[] = {
570 { NULL }
571};
572
573int
574main(int argc, char *argv[])
575{
576 struct display *d;
577 struct dnd *dnd;
578 struct timeval tv;
579
580 gettimeofday(&tv, NULL);
581 srandom(tv.tv_usec);
582
583 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200584 if (d == NULL) {
585 fprintf(stderr, "failed to create display: %m\n");
586 return -1;
587 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400588
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400589 display_set_drag_offer_handler(d, drag_offer_handler);
590
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400591 dnd = dnd_create (d);
592
593 display_run(d);
594
595 return 0;
596}