blob: a2067e869940f7a7b861db17c7b21bffd905f227 [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øgsbergac7619f2012-01-09 09:26:38 -050044 struct widget *widget;
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
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040058 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040059};
60
61struct item {
62 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080063 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040064 int x, y;
65};
66
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080067struct dnd_flower_message {
68 int seed, x_offset, y_offset;
69};
70
71
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040072static const int item_width = 64;
73static const int item_height = 64;
74static const int item_padding = 16;
75
76static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080077item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040078{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080079 struct item *item;
80 struct timeval tv;
81
82 item = malloc(sizeof *item);
83 if (item == NULL)
84 return NULL;
85
86
87 gettimeofday(&tv, NULL);
88 item->seed = seed ? seed : tv.tv_usec;
89 srandom(item->seed);
90
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040091 const int petal_count = 3 + random() % 5;
92 const double r1 = 20 + random() % 10;
93 const double r2 = 5 + random() % 12;
94 const double u = (10 + random() % 90) / 100.0;
95 const double v = (random() % 90) / 100.0;
96
97 cairo_t *cr;
98 int i;
99 double t, dt = 2 * M_PI / (petal_count * 2);
100 double x1, y1, x2, y2, x3, y3;
101 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400102
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400103
104 rect.width = item_width;
105 rect.height = item_height;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400106 item->surface = display_create_surface(display, NULL, &rect, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400107
108 item->x = x;
109 item->y = y;
110
111 cr = cairo_create(item->surface);
112 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
113 cairo_set_source_rgba(cr, 0, 0, 0, 0);
114 cairo_paint(cr);
115
116 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
117 cairo_translate(cr, item_width / 2, item_height / 2);
118 t = random();
119 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
120 for (i = 0; i < petal_count; i++, t += dt * 2) {
121 x1 = cos(t) * r1;
122 y1 = sin(t) * r1;
123 x2 = cos(t + dt) * r2;
124 y2 = sin(t + dt) * r2;
125 x3 = cos(t + 2 * dt) * r1;
126 y3 = sin(t + 2 * dt) * r1;
127
128 cairo_curve_to(cr,
129 x1 - y1 * u, y1 + x1 * u,
130 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400131 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400132
133 cairo_curve_to(cr,
134 x2 - y2 * v, y2 + x2 * v,
135 x3 + y3 * u, y3 - x3 * u,
136 x3, y3);
137 }
138
139 cairo_close_path(cr);
140
141 cairo_set_source_rgba(cr,
142 0.5 + (random() % 50) / 49.0,
143 0.5 + (random() % 50) / 49.0,
144 0.5 + (random() % 50) / 49.0,
145 0.5 + (random() % 100) / 99.0);
146
147 cairo_fill_preserve(cr);
148
149 cairo_set_line_width(cr, 1);
150 cairo_set_source_rgba(cr,
151 0.5 + (random() % 50) / 49.0,
152 0.5 + (random() % 50) / 49.0,
153 0.5 + (random() % 50) / 49.0,
154 0.5 + (random() % 100) / 99.0);
155 cairo_stroke(cr);
156
157 cairo_destroy(cr);
158
159 return item;
160}
161
162static void
163dnd_draw(struct dnd *dnd)
164{
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500165 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400166 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500167 cairo_surface_t *surface;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400168 int i;
169
170 window_draw(dnd->window);
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øgsberge164e4e2011-01-21 11:35:05 -0500174 window_get_child_allocation(dnd->window, &allocation);
175 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);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400194 window_flush(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400195}
196
197static void
198redraw_handler(struct window *window, void *data)
199{
200 struct dnd *dnd = data;
201
202 dnd_draw(dnd);
203}
204
205static void
206keyboard_focus_handler(struct window *window,
207 struct input *device, void *data)
208{
209 struct dnd *dnd = data;
210
211 window_schedule_redraw(dnd->window);
212}
213
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800214static int
215dnd_add_item(struct dnd *dnd, struct item *item)
216{
217 int i;
218
219 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
220 if (dnd->items[i] == 0) {
221 dnd->items[i] = item;
222 return i;
223 }
224 }
225 return -1;
226}
227
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400228static struct item *
229dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
230{
231 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500232 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400233 int i;
234
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500235 window_get_child_allocation(dnd->window, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400236
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500237 x -= allocation.x;
238 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400239
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400240 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
241 item = dnd->items[i];
242 if (item &&
243 item->x <= x && x < item->x + item_width &&
244 item->y <= y && y < item->y + item_height)
245 return item;
246 }
247
248 return NULL;
249}
250
251static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400252data_source_target(void *data,
253 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400254{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400255 struct dnd_drag *dnd_drag = data;
256 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400257 cairo_surface_t *surface;
258 struct wl_buffer *buffer;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400259 struct wl_data_device *device;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400260
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400261 device = input_get_data_device(dnd_drag->input);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800262 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400263 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400264 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400265 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400266 surface = dnd_drag->translucent;
267
268 buffer = display_get_buffer_for_surface(dnd->display, surface);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400269 wl_data_device_attach(device, dnd_drag->time, buffer,
270 dnd_drag->hotspot_x, dnd_drag->hotspot_y);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400271}
272
273static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400274data_source_send(void *data, struct wl_data_source *source,
275 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400276{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800277 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400278 struct dnd_drag *dnd_drag = data;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800279
280 dnd_flower_message.seed = dnd_drag->item->seed;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800281 dnd_flower_message.x_offset = dnd_drag->x_offset;
282 dnd_flower_message.y_offset = dnd_drag->y_offset;
283
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800284 write(fd, &dnd_flower_message, sizeof dnd_flower_message);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400285 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400286}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400287
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400288static void
289data_source_cancelled(void *data, struct wl_data_source *source)
290{
291 struct dnd_drag *dnd_drag = data;
292
293 /* The 'cancelled' event means that the source is no longer in
294 * use by the drag (or current selection). We need to clean
295 * up the drag object created and the local state. */
296
297 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800298
299 /* Destroy the item that has been dragged out */
300 cairo_surface_destroy(dnd_drag->item->surface);
301 free(dnd_drag->item);
302
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400303 cairo_surface_destroy(dnd_drag->translucent);
304 cairo_surface_destroy(dnd_drag->opaque);
305 free(dnd_drag);
306}
307
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400308static const struct wl_data_source_listener data_source_listener = {
309 data_source_target,
310 data_source_send,
311 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400312};
313
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400314static cairo_surface_t *
315create_drag_cursor(struct dnd_drag *dnd_drag,
316 struct item *item, int32_t x, int32_t y, double opacity)
317{
318 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400319 cairo_surface_t *surface, *pointer;
320 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
321 struct rectangle rectangle;
322 cairo_pattern_t *pattern;
323 cairo_t *cr;
324
325 pointer = display_get_pointer_surface(dnd->display,
326 POINTER_DRAGGING,
327 &pointer_width,
328 &pointer_height,
329 &hotspot_x,
330 &hotspot_y);
331
332 rectangle.width = item_width + 2 * pointer_width;
333 rectangle.height = item_height + 2 * pointer_height;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400334 surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400335
336 cr = cairo_create(surface);
337 cairo_translate(cr, pointer_width, pointer_height);
338
339 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
340 cairo_set_source_rgba(cr, 0, 0, 0, 0);
341 cairo_paint(cr);
342
343 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
344 cairo_set_source_surface(cr, item->surface, 0, 0);
345 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
346 cairo_mask(cr, pattern);
347 cairo_pattern_destroy(pattern);
348
349 cairo_set_source_surface(cr, pointer,
350 x - item->x - hotspot_x,
351 y - item->y - hotspot_y);
352 cairo_surface_destroy(pointer);
353 cairo_paint(cr);
354 /* FIXME: more cairo-gl brokeness */
Benjamin Franzke47eb8f42011-10-07 09:08:56 +0200355 surface_flush_device(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400356 cairo_destroy(cr);
357
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400358 dnd_drag->hotspot_x = pointer_width + x - item->x;
359 dnd_drag->hotspot_y = pointer_height + y - item->y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400360
361 return surface;
362}
363
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400364static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400365dnd_button_handler(struct window *window,
366 struct input *input, uint32_t time,
367 int button, int state, void *data)
368{
369 struct dnd *dnd = data;
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øgsbergac7619f2012-01-09 09:26:38 -0500435static void
436dnd_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øgsberg47fe08a2011-10-28 12:26:06 -0400440}
441
442static int
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400443dnd_motion_handler(struct window *window,
444 struct input *input, uint32_t time,
445 int32_t x, int32_t y,
446 int32_t sx, int32_t sy, void *data)
447{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400448 return lookup_cursor(data, sx, sy);
449}
450
451static void
452dnd_data_handler(struct window *window,
453 struct input *input, uint32_t time,
454 int32_t x, int32_t y, const char **types, void *data)
455{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400456 struct dnd *dnd = data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400457
458 if (!dnd_get_item(dnd, x, y)) {
459 input_accept(input, time, types[0]);
460 } else {
461 input_accept(input, time, NULL);
462 }
463}
464
465static void
466dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
467{
468 struct dnd *dnd = user_data;
469 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400470 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400471 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400472
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400473 if (len == 0) {
474 return;
475 } else if (len != sizeof *message) {
476 fprintf(stderr, "odd message length %ld, expected %ld\n",
477 len, sizeof *message);
478 return;
479 }
480
481 window_get_child_allocation(dnd->window, &allocation);
482 item = item_create(dnd->display,
483 x - message->x_offset - allocation.x,
484 y - message->y_offset - allocation.y,
485 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400486
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400487 dnd_add_item(dnd, item);
488 window_schedule_redraw(dnd->window);
489}
490
491static void
492dnd_drop_handler(struct window *window, struct input *input,
493 int32_t x, int32_t y, void *data)
494{
495 struct dnd *dnd = data;
496
497 if (dnd_get_item(dnd, x, y)) {
498 fprintf(stderr, "got 'drop', but no target\n");
499 return;
500 }
501
502 input_receive_drag_data(input,
503 "application/x-wayland-dnd-flower",
504 dnd_receive_func, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400505}
506
507static struct dnd *
508dnd_create(struct display *display)
509{
510 struct dnd *dnd;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400511 int i, x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500512 int32_t width, height;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400513
514 dnd = malloc(sizeof *dnd);
515 if (dnd == NULL)
516 return dnd;
517 memset(dnd, 0, sizeof *dnd);
518
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500519 dnd->window = window_create(display, 400, 400);
520 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400521
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400522 dnd->display = display;
523 dnd->key = 100;
524
525 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
526 x = (i % 4) * (item_width + item_padding) + item_padding;
527 y = (i / 4) * (item_height + item_padding) + item_padding;
528 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800529 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400530 else
531 dnd->items[i] = NULL;
532 }
533
534 window_set_user_data(dnd->window, dnd);
535 window_set_redraw_handler(dnd->window, redraw_handler);
536 window_set_keyboard_focus_handler(dnd->window,
537 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400538 window_set_button_handler(dnd->window, dnd_button_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400539 window_set_motion_handler(dnd->window, dnd_motion_handler);
540 window_set_data_handler(dnd->window, dnd_data_handler);
541 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400542
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500543 dnd->widget = window_add_widget(dnd->window, dnd);
544 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
545
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500546 width = 4 * (item_width + item_padding) + item_padding;
547 height = 4 * (item_height + item_padding) + item_padding;
548 window_set_child_size(dnd->window, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400549
550 dnd_draw(dnd);
551
552 return dnd;
553}
554
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400555int
556main(int argc, char *argv[])
557{
558 struct display *d;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400559
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400560 d = display_create(&argc, &argv, NULL);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200561 if (d == NULL) {
562 fprintf(stderr, "failed to create display: %m\n");
563 return -1;
564 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400565
Kristian Høgsberg00439612011-01-25 15:16:01 -0500566 dnd_create(d);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400567
568 display_run(d);
569
570 return 0;
571}