blob: febb1ccdb78dbb1a973ebe76b416ca4d584dfd88 [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>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040032#include <sys/epoll.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-client.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040035
36#include "window.h"
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020037#include "cairo-util.h"
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040038
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040039struct dnd {
40 struct window *window;
41 struct display *display;
42 uint32_t key;
43 struct item *items[16];
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040044};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040045
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040046struct dnd_drag {
47 cairo_surface_t *translucent;
48 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040049 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040050 struct dnd *dnd;
51 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040052 uint32_t time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080053 struct item *item;
54 int x_offset, y_offset;
55 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040056
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040057 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040058};
59
60struct item {
61 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080062 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040063 int x, y;
64};
65
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080066struct dnd_flower_message {
67 int seed, x_offset, y_offset;
68};
69
70
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040071static const int item_width = 64;
72static const int item_height = 64;
73static const int item_padding = 16;
74
75static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080076item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040077{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080078 struct item *item;
79 struct timeval tv;
80
81 item = malloc(sizeof *item);
82 if (item == NULL)
83 return NULL;
84
85
86 gettimeofday(&tv, NULL);
87 item->seed = seed ? seed : tv.tv_usec;
88 srandom(item->seed);
89
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040090 const int petal_count = 3 + random() % 5;
91 const double r1 = 20 + random() % 10;
92 const double r2 = 5 + random() % 12;
93 const double u = (10 + random() % 90) / 100.0;
94 const double v = (random() % 90) / 100.0;
95
96 cairo_t *cr;
97 int i;
98 double t, dt = 2 * M_PI / (petal_count * 2);
99 double x1, y1, x2, y2, x3, y3;
100 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400101
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400102
103 rect.width = item_width;
104 rect.height = item_height;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400105 item->surface = display_create_surface(display, NULL, &rect, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400106
107 item->x = x;
108 item->y = y;
109
110 cr = cairo_create(item->surface);
111 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
112 cairo_set_source_rgba(cr, 0, 0, 0, 0);
113 cairo_paint(cr);
114
115 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
116 cairo_translate(cr, item_width / 2, item_height / 2);
117 t = random();
118 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
119 for (i = 0; i < petal_count; i++, t += dt * 2) {
120 x1 = cos(t) * r1;
121 y1 = sin(t) * r1;
122 x2 = cos(t + dt) * r2;
123 y2 = sin(t + dt) * r2;
124 x3 = cos(t + 2 * dt) * r1;
125 y3 = sin(t + 2 * dt) * r1;
126
127 cairo_curve_to(cr,
128 x1 - y1 * u, y1 + x1 * u,
129 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400130 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400131
132 cairo_curve_to(cr,
133 x2 - y2 * v, y2 + x2 * v,
134 x3 + y3 * u, y3 - x3 * u,
135 x3, y3);
136 }
137
138 cairo_close_path(cr);
139
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
146 cairo_fill_preserve(cr);
147
148 cairo_set_line_width(cr, 1);
149 cairo_set_source_rgba(cr,
150 0.5 + (random() % 50) / 49.0,
151 0.5 + (random() % 50) / 49.0,
152 0.5 + (random() % 50) / 49.0,
153 0.5 + (random() % 100) / 99.0);
154 cairo_stroke(cr);
155
156 cairo_destroy(cr);
157
158 return item;
159}
160
161static void
162dnd_draw(struct dnd *dnd)
163{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500164 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400165 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500166 cairo_surface_t *surface;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400167 int i;
168
169 window_draw(dnd->window);
170
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500171 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400172 cr = cairo_create(surface);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500173 window_get_child_allocation(dnd->window, &allocation);
174 cairo_rectangle(cr, allocation.x, allocation.y,
175 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500176
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400177 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
178 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400179 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400180
181 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
182 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
183 if (!dnd->items[i])
184 continue;
185 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400186 dnd->items[i]->x + allocation.x,
187 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400188 cairo_paint(cr);
189 }
190
191 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400192 cairo_surface_destroy(surface);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400193 window_flush(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400194}
195
196static void
197redraw_handler(struct window *window, void *data)
198{
199 struct dnd *dnd = data;
200
201 dnd_draw(dnd);
202}
203
204static void
205keyboard_focus_handler(struct window *window,
206 struct input *device, void *data)
207{
208 struct dnd *dnd = data;
209
210 window_schedule_redraw(dnd->window);
211}
212
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800213static int
214dnd_add_item(struct dnd *dnd, struct item *item)
215{
216 int i;
217
218 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
219 if (dnd->items[i] == 0) {
220 dnd->items[i] = item;
221 return i;
222 }
223 }
224 return -1;
225}
226
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400227static struct item *
228dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
229{
230 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500231 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400232 int i;
233
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500234 window_get_child_allocation(dnd->window, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400235
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500236 x -= allocation.x;
237 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400238
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400239 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
240 item = dnd->items[i];
241 if (item &&
242 item->x <= x && x < item->x + item_width &&
243 item->y <= y && y < item->y + item_height)
244 return item;
245 }
246
247 return NULL;
248}
249
250static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400251data_source_target(void *data,
252 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400253{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400254 struct dnd_drag *dnd_drag = data;
255 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400256 cairo_surface_t *surface;
257 struct wl_buffer *buffer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400258 struct wl_data_device *device;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400259
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400260 device = input_get_data_device(dnd_drag->input);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800261 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400262 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400263 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400264 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400265 surface = dnd_drag->translucent;
266
267 buffer = display_get_buffer_for_surface(dnd->display, surface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400268 wl_data_device_attach(device, dnd_drag->time, buffer,
269 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400270}
271
272static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400273data_source_send(void *data, struct wl_data_source *source,
274 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400275{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800276 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400277 struct dnd_drag *dnd_drag = data;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800278
279 dnd_flower_message.seed = dnd_drag->item->seed;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800280 dnd_flower_message.x_offset = dnd_drag->x_offset;
281 dnd_flower_message.y_offset = dnd_drag->y_offset;
282
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800283 write(fd, &dnd_flower_message, sizeof dnd_flower_message);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400284 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400285}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400286
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400287static void
288data_source_cancelled(void *data, struct wl_data_source *source)
289{
290 struct dnd_drag *dnd_drag = data;
291
292 /* The 'cancelled' event means that the source is no longer in
293 * use by the drag (or current selection). We need to clean
294 * up the drag object created and the local state. */
295
296 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800297
298 /* Destroy the item that has been dragged out */
299 cairo_surface_destroy(dnd_drag->item->surface);
300 free(dnd_drag->item);
301
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400302 cairo_surface_destroy(dnd_drag->translucent);
303 cairo_surface_destroy(dnd_drag->opaque);
304 free(dnd_drag);
305}
306
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400307static const struct wl_data_source_listener data_source_listener = {
308 data_source_target,
309 data_source_send,
310 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400311};
312
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400313static cairo_surface_t *
314create_drag_cursor(struct dnd_drag *dnd_drag,
315 struct item *item, int32_t x, int32_t y, double opacity)
316{
317 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400318 cairo_surface_t *surface, *pointer;
319 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
320 struct rectangle rectangle;
321 cairo_pattern_t *pattern;
322 cairo_t *cr;
323
324 pointer = display_get_pointer_surface(dnd->display,
325 POINTER_DRAGGING,
326 &pointer_width,
327 &pointer_height,
328 &hotspot_x,
329 &hotspot_y);
330
331 rectangle.width = item_width + 2 * pointer_width;
332 rectangle.height = item_height + 2 * pointer_height;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400333 surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400334
335 cr = cairo_create(surface);
336 cairo_translate(cr, pointer_width, pointer_height);
337
338 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
339 cairo_set_source_rgba(cr, 0, 0, 0, 0);
340 cairo_paint(cr);
341
342 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
343 cairo_set_source_surface(cr, item->surface, 0, 0);
344 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
345 cairo_mask(cr, pattern);
346 cairo_pattern_destroy(pattern);
347
348 cairo_set_source_surface(cr, pointer,
349 x - item->x - hotspot_x,
350 y - item->y - hotspot_y);
351 cairo_surface_destroy(pointer);
352 cairo_paint(cr);
353 /* FIXME: more cairo-gl brokeness */
Benjamin Franzke47eb8f42011-10-07 09:08:56 +0200354 surface_flush_device(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400355 cairo_destroy(cr);
356
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400357 dnd_drag->hotspot_x = pointer_width + x - item->x;
358 dnd_drag->hotspot_y = pointer_height + y - item->y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400359
360 return surface;
361}
362
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400363static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500364dnd_button_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400365 struct input *input, uint32_t time,
366 int button, int state, void *data)
367{
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500368 struct window *window = data;
369 struct dnd *dnd = window_get_user_data(window);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400370 int32_t x, y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400371 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500372 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400373 struct dnd_drag *dnd_drag;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800374 int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400375
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500376 window_get_child_allocation(dnd->window, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400377 input_get_position(input, &x, &y);
378 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500379 x -= allocation.x;
380 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400381
382 if (item && state == 1) {
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400383 dnd_drag = malloc(sizeof *dnd_drag);
384 dnd_drag->dnd = dnd;
385 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400386 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800387 dnd_drag->item = item;
388 dnd_drag->x_offset = x - item->x;
389 dnd_drag->y_offset = y - item->y;
390
391 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
392 if (item == dnd->items[i]){
393 dnd->items[i] = 0;
394 break;
395 }
396 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400397
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400398 dnd_drag->data_source =
399 display_create_data_source(dnd->display);
400 wl_data_source_add_listener(dnd_drag->data_source,
401 &data_source_listener,
402 dnd_drag);
403 wl_data_source_offer(dnd_drag->data_source,
404 "application/x-wayland-dnd-flower");
405 wl_data_source_offer(dnd_drag->data_source,
406 "text/plain; charset=utf-8");
407 wl_data_device_start_drag(input_get_data_device(input),
408 dnd_drag->data_source,
409 window_get_wl_surface(window),
410 time);
411
412 input_set_pointer_image(input, time, POINTER_DRAGGING);
413
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400414 dnd_drag->opaque =
415 create_drag_cursor(dnd_drag, item, x, y, 1);
416 dnd_drag->translucent =
417 create_drag_cursor(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400418
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800419 window_schedule_redraw(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400420 }
421}
422
423static int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400424lookup_cursor(struct dnd *dnd, int x, int y)
425{
426 struct item *item;
427
428 item = dnd_get_item(dnd, x, y);
429 if (item)
430 return POINTER_HAND1;
431 else
432 return POINTER_LEFT_PTR;
433}
434
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500435static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500436dnd_enter_handler(struct widget *widget,
437 struct input *input, uint32_t time,
438 int32_t x, int32_t y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400439{
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500440 struct window *window = data;
441 struct dnd *dnd = window_get_user_data(window);
442
443 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400444}
445
446static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500447dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400448 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500449 int32_t x, int32_t y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400450{
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -0500451 struct window *window = data;
452 struct dnd *dnd = window_get_user_data(window);
453
454 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400455}
456
457static void
458dnd_data_handler(struct window *window,
459 struct input *input, uint32_t time,
460 int32_t x, int32_t y, const char **types, void *data)
461{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400462 struct dnd *dnd = data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400463
464 if (!dnd_get_item(dnd, x, y)) {
465 input_accept(input, time, types[0]);
466 } else {
467 input_accept(input, time, NULL);
468 }
469}
470
471static void
472dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
473{
474 struct dnd *dnd = user_data;
475 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400476 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400477 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400478
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400479 if (len == 0) {
480 return;
481 } else if (len != sizeof *message) {
482 fprintf(stderr, "odd message length %ld, expected %ld\n",
483 len, sizeof *message);
484 return;
485 }
486
487 window_get_child_allocation(dnd->window, &allocation);
488 item = item_create(dnd->display,
489 x - message->x_offset - allocation.x,
490 y - message->y_offset - allocation.y,
491 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400492
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400493 dnd_add_item(dnd, item);
494 window_schedule_redraw(dnd->window);
495}
496
497static void
498dnd_drop_handler(struct window *window, struct input *input,
499 int32_t x, int32_t y, void *data)
500{
501 struct dnd *dnd = data;
502
503 if (dnd_get_item(dnd, x, y)) {
504 fprintf(stderr, "got 'drop', but no target\n");
505 return;
506 }
507
508 input_receive_drag_data(input,
509 "application/x-wayland-dnd-flower",
510 dnd_receive_func, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400511}
512
513static struct dnd *
514dnd_create(struct display *display)
515{
516 struct dnd *dnd;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400517 int i, x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500518 int32_t width, height;
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -0500519 struct widget *widget;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400520
521 dnd = malloc(sizeof *dnd);
522 if (dnd == NULL)
523 return dnd;
524 memset(dnd, 0, sizeof *dnd);
525
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500526 dnd->window = window_create(display, 400, 400);
527 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400528
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400529 dnd->display = display;
530 dnd->key = 100;
531
532 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
533 x = (i % 4) * (item_width + item_padding) + item_padding;
534 y = (i / 4) * (item_height + item_padding) + item_padding;
535 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800536 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400537 else
538 dnd->items[i] = NULL;
539 }
540
541 window_set_user_data(dnd->window, dnd);
542 window_set_redraw_handler(dnd->window, redraw_handler);
543 window_set_keyboard_focus_handler(dnd->window,
544 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400545 window_set_data_handler(dnd->window, dnd_data_handler);
546 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400547
Kristian Høgsberg19dd1d72012-01-09 10:42:41 -0500548 widget = window_get_widget(dnd->window);
549 widget_set_enter_handler(widget, dnd_enter_handler);
550 widget_set_motion_handler(widget, dnd_motion_handler);
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500551 widget_set_button_handler(widget, dnd_button_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500552
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500553 width = 4 * (item_width + item_padding) + item_padding;
554 height = 4 * (item_height + item_padding) + item_padding;
555 window_set_child_size(dnd->window, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400556
557 dnd_draw(dnd);
558
559 return dnd;
560}
561
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400562int
563main(int argc, char *argv[])
564{
565 struct display *d;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400566
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400567 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200568 if (d == NULL) {
569 fprintf(stderr, "failed to create display: %m\n");
570 return -1;
571 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400572
Kristian Høgsberg00439612011-01-25 15:16:01 -0500573 dnd_create(d);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400574
575 display_run(d);
576
577 return 0;
578}