blob: 5e1696b2952dc9f7fe4985323a205b22d185e3e7 [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;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050041 struct widget *widget;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040042 struct display *display;
43 uint32_t key;
44 struct item *items[16];
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040045};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040046
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040047struct dnd_drag {
48 cairo_surface_t *translucent;
49 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040050 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040051 struct dnd *dnd;
52 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040053 uint32_t time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080054 struct item *item;
55 int x_offset, y_offset;
56 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040057
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020058 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040059 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040060};
61
62struct item {
63 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080064 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040065 int x, y;
66};
67
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080068struct dnd_flower_message {
69 int seed, x_offset, y_offset;
70};
71
72
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040073static const int item_width = 64;
74static const int item_height = 64;
75static const int item_padding = 16;
76
77static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080078item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040079{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080080 struct item *item;
81 struct timeval tv;
82
83 item = malloc(sizeof *item);
84 if (item == NULL)
85 return NULL;
86
87
88 gettimeofday(&tv, NULL);
89 item->seed = seed ? seed : tv.tv_usec;
90 srandom(item->seed);
91
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040092 const int petal_count = 3 + random() % 5;
93 const double r1 = 20 + random() % 10;
94 const double r2 = 5 + random() % 12;
95 const double u = (10 + random() % 90) / 100.0;
96 const double v = (random() % 90) / 100.0;
97
98 cairo_t *cr;
99 int i;
100 double t, dt = 2 * M_PI / (petal_count * 2);
101 double x1, y1, x2, y2, x3, y3;
102 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400103
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400104
105 rect.width = item_width;
106 rect.height = item_height;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400107 item->surface = display_create_surface(display, NULL, &rect, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400108
109 item->x = x;
110 item->y = y;
111
112 cr = cairo_create(item->surface);
113 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
114 cairo_set_source_rgba(cr, 0, 0, 0, 0);
115 cairo_paint(cr);
116
117 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
118 cairo_translate(cr, item_width / 2, item_height / 2);
119 t = random();
120 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
121 for (i = 0; i < petal_count; i++, t += dt * 2) {
122 x1 = cos(t) * r1;
123 y1 = sin(t) * r1;
124 x2 = cos(t + dt) * r2;
125 y2 = sin(t + dt) * r2;
126 x3 = cos(t + 2 * dt) * r1;
127 y3 = sin(t + 2 * dt) * r1;
128
129 cairo_curve_to(cr,
130 x1 - y1 * u, y1 + x1 * u,
131 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400132 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400133
134 cairo_curve_to(cr,
135 x2 - y2 * v, y2 + x2 * v,
136 x3 + y3 * u, y3 - x3 * u,
137 x3, y3);
138 }
139
140 cairo_close_path(cr);
141
142 cairo_set_source_rgba(cr,
143 0.5 + (random() % 50) / 49.0,
144 0.5 + (random() % 50) / 49.0,
145 0.5 + (random() % 50) / 49.0,
146 0.5 + (random() % 100) / 99.0);
147
148 cairo_fill_preserve(cr);
149
150 cairo_set_line_width(cr, 1);
151 cairo_set_source_rgba(cr,
152 0.5 + (random() % 50) / 49.0,
153 0.5 + (random() % 50) / 49.0,
154 0.5 + (random() % 50) / 49.0,
155 0.5 + (random() % 100) / 99.0);
156 cairo_stroke(cr);
157
158 cairo_destroy(cr);
159
160 return item;
161}
162
163static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500164dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400165{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500166 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500167 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400168 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500169 cairo_surface_t *surface;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400170 int i;
171
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500172 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400173 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500174 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500175 cairo_rectangle(cr, allocation.x, allocation.y,
176 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500177
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400178 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
179 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400180 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400181
182 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
183 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
184 if (!dnd->items[i])
185 continue;
186 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400187 dnd->items[i]->x + allocation.x,
188 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400189 cairo_paint(cr);
190 }
191
192 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400193 cairo_surface_destroy(surface);
194}
195
196static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400197keyboard_focus_handler(struct window *window,
198 struct input *device, void *data)
199{
200 struct dnd *dnd = data;
201
202 window_schedule_redraw(dnd->window);
203}
204
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800205static int
206dnd_add_item(struct dnd *dnd, struct item *item)
207{
208 int i;
209
210 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
211 if (dnd->items[i] == 0) {
212 dnd->items[i] = item;
213 return i;
214 }
215 }
216 return -1;
217}
218
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400219static struct item *
220dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
221{
222 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500223 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400224 int i;
225
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500226 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400227
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500228 x -= allocation.x;
229 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400230
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400231 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
232 item = dnd->items[i];
233 if (item &&
234 item->x <= x && x < item->x + item_width &&
235 item->y <= y && y < item->y + item_height)
236 return item;
237 }
238
239 return NULL;
240}
241
242static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400243data_source_target(void *data,
244 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400245{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400246 struct dnd_drag *dnd_drag = data;
247 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400248 cairo_surface_t *surface;
249 struct wl_buffer *buffer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400250 struct wl_data_device *device;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400251
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400252 device = input_get_data_device(dnd_drag->input);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800253 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400254 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400255 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400256 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400257 surface = dnd_drag->translucent;
258
259 buffer = display_get_buffer_for_surface(dnd->display, surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200260 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400261}
262
263static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400264data_source_send(void *data, struct wl_data_source *source,
265 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400266{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800267 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400268 struct dnd_drag *dnd_drag = data;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800269
270 dnd_flower_message.seed = dnd_drag->item->seed;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800271 dnd_flower_message.x_offset = dnd_drag->x_offset;
272 dnd_flower_message.y_offset = dnd_drag->y_offset;
273
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800274 write(fd, &dnd_flower_message, sizeof dnd_flower_message);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400275 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400276}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400277
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400278static void
279data_source_cancelled(void *data, struct wl_data_source *source)
280{
281 struct dnd_drag *dnd_drag = data;
282
283 /* The 'cancelled' event means that the source is no longer in
284 * use by the drag (or current selection). We need to clean
285 * up the drag object created and the local state. */
286
287 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800288
289 /* Destroy the item that has been dragged out */
290 cairo_surface_destroy(dnd_drag->item->surface);
291 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200292
293 wl_surface_destroy(dnd_drag->drag_surface);
294
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400295 cairo_surface_destroy(dnd_drag->translucent);
296 cairo_surface_destroy(dnd_drag->opaque);
297 free(dnd_drag);
298}
299
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400300static const struct wl_data_source_listener data_source_listener = {
301 data_source_target,
302 data_source_send,
303 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400304};
305
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400306static cairo_surface_t *
307create_drag_cursor(struct dnd_drag *dnd_drag,
308 struct item *item, int32_t x, int32_t y, double opacity)
309{
310 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400311 cairo_surface_t *surface, *pointer;
312 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
313 struct rectangle rectangle;
314 cairo_pattern_t *pattern;
315 cairo_t *cr;
316
317 pointer = display_get_pointer_surface(dnd->display,
318 POINTER_DRAGGING,
319 &pointer_width,
320 &pointer_height,
321 &hotspot_x,
322 &hotspot_y);
323
324 rectangle.width = item_width + 2 * pointer_width;
325 rectangle.height = item_height + 2 * pointer_height;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400326 surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400327
328 cr = cairo_create(surface);
329 cairo_translate(cr, pointer_width, pointer_height);
330
331 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
332 cairo_set_source_rgba(cr, 0, 0, 0, 0);
333 cairo_paint(cr);
334
335 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
336 cairo_set_source_surface(cr, item->surface, 0, 0);
337 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
338 cairo_mask(cr, pattern);
339 cairo_pattern_destroy(pattern);
340
341 cairo_set_source_surface(cr, pointer,
342 x - item->x - hotspot_x,
343 y - item->y - hotspot_y);
344 cairo_surface_destroy(pointer);
345 cairo_paint(cr);
346 /* FIXME: more cairo-gl brokeness */
Benjamin Franzke47eb8f42011-10-07 09:08:56 +0200347 surface_flush_device(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400348 cairo_destroy(cr);
349
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400350 dnd_drag->hotspot_x = pointer_width + x - item->x;
351 dnd_drag->hotspot_y = pointer_height + y - item->y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400352
353 return surface;
354}
355
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400356static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500357dnd_button_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400358 struct input *input, uint32_t time,
359 int button, int state, void *data)
360{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500361 struct dnd *dnd = data;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400362 int32_t x, y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400363 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500364 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400365 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200366 struct display *display;
367 struct wl_compositor *compositor;
368 struct wl_buffer *buffer;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800369 int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400370
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500371 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400372 input_get_position(input, &x, &y);
373 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500374 x -= allocation.x;
375 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400376
377 if (item && state == 1) {
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400378 dnd_drag = malloc(sizeof *dnd_drag);
379 dnd_drag->dnd = dnd;
380 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400381 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800382 dnd_drag->item = item;
383 dnd_drag->x_offset = x - item->x;
384 dnd_drag->y_offset = y - item->y;
385
386 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
387 if (item == dnd->items[i]){
388 dnd->items[i] = 0;
389 break;
390 }
391 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400392
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200393 display = window_get_display(dnd->window);
394 compositor = display_get_compositor(display);
395 dnd_drag->drag_surface =
396 wl_compositor_create_surface(compositor);
397
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,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500409 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200410 dnd_drag->drag_surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400411 time);
412
413 input_set_pointer_image(input, time, POINTER_DRAGGING);
414
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400415 dnd_drag->opaque =
416 create_drag_cursor(dnd_drag, item, x, y, 1);
417 dnd_drag->translucent =
418 create_drag_cursor(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400419
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200420 buffer = display_get_buffer_for_surface(dnd->display, dnd_drag->translucent);
421 wl_surface_attach(dnd_drag->drag_surface, buffer,
422 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
423
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800424 window_schedule_redraw(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400425 }
426}
427
428static int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400429lookup_cursor(struct dnd *dnd, int x, int y)
430{
431 struct item *item;
432
433 item = dnd_get_item(dnd, x, y);
434 if (item)
435 return POINTER_HAND1;
436 else
437 return POINTER_LEFT_PTR;
438}
439
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500440static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500441dnd_enter_handler(struct widget *widget,
442 struct input *input, uint32_t time,
443 int32_t x, int32_t y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400444{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500445 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400446}
447
448static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500449dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400450 struct input *input, uint32_t time,
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500451 int32_t x, int32_t y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400452{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500453 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400454}
455
456static void
457dnd_data_handler(struct window *window,
458 struct input *input, uint32_t time,
459 int32_t x, int32_t y, const char **types, void *data)
460{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400461 struct dnd *dnd = data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400462
463 if (!dnd_get_item(dnd, x, y)) {
464 input_accept(input, time, types[0]);
465 } else {
466 input_accept(input, time, NULL);
467 }
468}
469
470static void
471dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
472{
473 struct dnd *dnd = user_data;
474 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400475 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400476 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400477
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400478 if (len == 0) {
479 return;
480 } else if (len != sizeof *message) {
481 fprintf(stderr, "odd message length %ld, expected %ld\n",
482 len, sizeof *message);
483 return;
484 }
485
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500486 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400487 item = item_create(dnd->display,
488 x - message->x_offset - allocation.x,
489 y - message->y_offset - allocation.y,
490 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400491
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400492 dnd_add_item(dnd, item);
493 window_schedule_redraw(dnd->window);
494}
495
496static void
497dnd_drop_handler(struct window *window, struct input *input,
498 int32_t x, int32_t y, void *data)
499{
500 struct dnd *dnd = data;
501
502 if (dnd_get_item(dnd, x, y)) {
503 fprintf(stderr, "got 'drop', but no target\n");
504 return;
505 }
506
507 input_receive_drag_data(input,
508 "application/x-wayland-dnd-flower",
509 dnd_receive_func, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400510}
511
512static struct dnd *
513dnd_create(struct display *display)
514{
515 struct dnd *dnd;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400516 int i, x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500517 int32_t width, height;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400518
519 dnd = malloc(sizeof *dnd);
520 if (dnd == NULL)
521 return dnd;
522 memset(dnd, 0, sizeof *dnd);
523
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500524 dnd->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500525 dnd->widget = frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500526 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400527
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400528 dnd->display = display;
529 dnd->key = 100;
530
531 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
532 x = (i % 4) * (item_width + item_padding) + item_padding;
533 y = (i / 4) * (item_height + item_padding) + item_padding;
534 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800535 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400536 else
537 dnd->items[i] = NULL;
538 }
539
540 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400541 window_set_keyboard_focus_handler(dnd->window,
542 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400543 window_set_data_handler(dnd->window, dnd_data_handler);
544 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400545
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500546 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500547 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
548 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
549 widget_set_button_handler(dnd->widget, dnd_button_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500550
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500551 width = 4 * (item_width + item_padding) + item_padding;
552 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400553
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500554 widget_schedule_resize(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400555
556 return dnd;
557}
558
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400559int
560main(int argc, char *argv[])
561{
562 struct display *d;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400563
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400564 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200565 if (d == NULL) {
566 fprintf(stderr, "failed to create display: %m\n");
567 return -1;
568 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400569
Kristian Høgsberg00439612011-01-25 15:16:01 -0500570 dnd_create(d);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400571
572 display_run(d);
573
574 return 0;
575}