blob: 45adc8a3cad9b20cd364fa50f2c096c209be3551 [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;
Kristian Høgsberg679f7162012-03-27 16:44:57 -040056 int width, height;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080057 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040058
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020059 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040060 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040061};
62
63struct item {
64 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080065 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040066 int x, y;
67};
68
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080069struct dnd_flower_message {
70 int seed, x_offset, y_offset;
71};
72
73
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040074static const int item_width = 64;
75static const int item_height = 64;
76static const int item_padding = 16;
77
78static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080079item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040080{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080081 struct item *item;
82 struct timeval tv;
83
84 item = malloc(sizeof *item);
85 if (item == NULL)
86 return NULL;
87
88
89 gettimeofday(&tv, NULL);
90 item->seed = seed ? seed : tv.tv_usec;
91 srandom(item->seed);
92
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040093 const int petal_count = 3 + random() % 5;
94 const double r1 = 20 + random() % 10;
95 const double r2 = 5 + random() % 12;
96 const double u = (10 + random() % 90) / 100.0;
97 const double v = (random() % 90) / 100.0;
98
99 cairo_t *cr;
100 int i;
101 double t, dt = 2 * M_PI / (petal_count * 2);
102 double x1, y1, x2, y2, x3, y3;
103 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400104
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400105
106 rect.width = item_width;
107 rect.height = item_height;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400108 item->surface = display_create_surface(display, NULL, &rect, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400109
110 item->x = x;
111 item->y = y;
112
113 cr = cairo_create(item->surface);
114 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
115 cairo_set_source_rgba(cr, 0, 0, 0, 0);
116 cairo_paint(cr);
117
118 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
119 cairo_translate(cr, item_width / 2, item_height / 2);
120 t = random();
121 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
122 for (i = 0; i < petal_count; i++, t += dt * 2) {
123 x1 = cos(t) * r1;
124 y1 = sin(t) * r1;
125 x2 = cos(t + dt) * r2;
126 y2 = sin(t + dt) * r2;
127 x3 = cos(t + 2 * dt) * r1;
128 y3 = sin(t + 2 * dt) * r1;
129
130 cairo_curve_to(cr,
131 x1 - y1 * u, y1 + x1 * u,
132 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400133 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400134
135 cairo_curve_to(cr,
136 x2 - y2 * v, y2 + x2 * v,
137 x3 + y3 * u, y3 - x3 * u,
138 x3, y3);
139 }
140
141 cairo_close_path(cr);
142
143 cairo_set_source_rgba(cr,
144 0.5 + (random() % 50) / 49.0,
145 0.5 + (random() % 50) / 49.0,
146 0.5 + (random() % 50) / 49.0,
147 0.5 + (random() % 100) / 99.0);
148
149 cairo_fill_preserve(cr);
150
151 cairo_set_line_width(cr, 1);
152 cairo_set_source_rgba(cr,
153 0.5 + (random() % 50) / 49.0,
154 0.5 + (random() % 50) / 49.0,
155 0.5 + (random() % 50) / 49.0,
156 0.5 + (random() % 100) / 99.0);
157 cairo_stroke(cr);
158
159 cairo_destroy(cr);
160
161 return item;
162}
163
164static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500165dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400166{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500167 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500168 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400169 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500170 cairo_surface_t *surface;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400171 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400172
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500173 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400174 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500175 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500176 cairo_rectangle(cr, allocation.x, allocation.y,
177 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500178
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400179 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
180 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400181 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400182
183 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
184 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
185 if (!dnd->items[i])
186 continue;
187 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400188 dnd->items[i]->x + allocation.x,
189 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400190 cairo_paint(cr);
191 }
192
193 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400194 cairo_surface_destroy(surface);
195}
196
197static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400198keyboard_focus_handler(struct window *window,
199 struct input *device, void *data)
200{
201 struct dnd *dnd = data;
202
203 window_schedule_redraw(dnd->window);
204}
205
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800206static int
207dnd_add_item(struct dnd *dnd, struct item *item)
208{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400209 unsigned int i;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800210
211 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
212 if (dnd->items[i] == 0) {
213 dnd->items[i] = item;
214 return i;
215 }
216 }
217 return -1;
218}
219
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400220static struct item *
221dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
222{
223 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500224 struct rectangle allocation;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400225 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400226
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500227 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400228
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500229 x -= allocation.x;
230 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400231
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400232 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
233 item = dnd->items[i];
234 if (item &&
235 item->x <= x && x < item->x + item_width &&
236 item->y <= y && y < item->y + item_height)
237 return item;
238 }
239
240 return NULL;
241}
242
243static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400244data_source_target(void *data,
245 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400246{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400247 struct dnd_drag *dnd_drag = data;
248 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400249 cairo_surface_t *surface;
250 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400251
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800252 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400253 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400254 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400255 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400256 surface = dnd_drag->translucent;
257
258 buffer = display_get_buffer_for_surface(dnd->display, surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200259 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400260 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
261 dnd_drag->width, dnd_drag->height);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400262}
263
264static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400265data_source_send(void *data, struct wl_data_source *source,
266 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400267{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800268 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400269 struct dnd_drag *dnd_drag = data;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800270
271 dnd_flower_message.seed = dnd_drag->item->seed;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800272 dnd_flower_message.x_offset = dnd_drag->x_offset;
273 dnd_flower_message.y_offset = dnd_drag->y_offset;
274
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200275 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
276 abort();
277
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400278 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400279}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400280
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400281static void
282data_source_cancelled(void *data, struct wl_data_source *source)
283{
284 struct dnd_drag *dnd_drag = data;
285
286 /* The 'cancelled' event means that the source is no longer in
287 * use by the drag (or current selection). We need to clean
288 * up the drag object created and the local state. */
289
290 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800291
292 /* Destroy the item that has been dragged out */
293 cairo_surface_destroy(dnd_drag->item->surface);
294 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200295
296 wl_surface_destroy(dnd_drag->drag_surface);
297
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400298 cairo_surface_destroy(dnd_drag->translucent);
299 cairo_surface_destroy(dnd_drag->opaque);
300 free(dnd_drag);
301}
302
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400303static const struct wl_data_source_listener data_source_listener = {
304 data_source_target,
305 data_source_send,
306 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400307};
308
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400309static cairo_surface_t *
310create_drag_cursor(struct dnd_drag *dnd_drag,
311 struct item *item, int32_t x, int32_t y, double opacity)
312{
313 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400314 cairo_surface_t *surface, *pointer;
315 int32_t pointer_width, pointer_height, hotspot_x, hotspot_y;
316 struct rectangle rectangle;
317 cairo_pattern_t *pattern;
318 cairo_t *cr;
319
320 pointer = display_get_pointer_surface(dnd->display,
321 POINTER_DRAGGING,
322 &pointer_width,
323 &pointer_height,
324 &hotspot_x,
325 &hotspot_y);
326
327 rectangle.width = item_width + 2 * pointer_width;
328 rectangle.height = item_height + 2 * pointer_height;
Kristian Høgsberg3be87d12011-05-13 13:45:17 -0400329 surface = display_create_surface(dnd->display, NULL, &rectangle, 0);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400330
331 cr = cairo_create(surface);
332 cairo_translate(cr, pointer_width, pointer_height);
333
334 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
335 cairo_set_source_rgba(cr, 0, 0, 0, 0);
336 cairo_paint(cr);
337
338 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
339 cairo_set_source_surface(cr, item->surface, 0, 0);
340 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
341 cairo_mask(cr, pattern);
342 cairo_pattern_destroy(pattern);
343
344 cairo_set_source_surface(cr, pointer,
345 x - item->x - hotspot_x,
346 y - item->y - hotspot_y);
347 cairo_surface_destroy(pointer);
348 cairo_paint(cr);
349 /* FIXME: more cairo-gl brokeness */
Benjamin Franzke47eb8f42011-10-07 09:08:56 +0200350 surface_flush_device(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400351 cairo_destroy(cr);
352
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400353 dnd_drag->hotspot_x = pointer_width + x - item->x;
354 dnd_drag->hotspot_y = pointer_height + y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400355 dnd_drag->width = rectangle.width;
356 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400357
358 return surface;
359}
360
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400361static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500362dnd_button_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400363 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +0100364 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400365{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500366 struct dnd *dnd = data;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400367 int32_t x, y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400368 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500369 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400370 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200371 struct display *display;
372 struct wl_compositor *compositor;
373 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400374 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400375 uint32_t serial;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400376
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500377 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400378 input_get_position(input, &x, &y);
379 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500380 x -= allocation.x;
381 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400382
383 if (item && state == 1) {
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400384 dnd_drag = malloc(sizeof *dnd_drag);
385 dnd_drag->dnd = dnd;
386 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400387 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800388 dnd_drag->item = item;
389 dnd_drag->x_offset = x - item->x;
390 dnd_drag->y_offset = y - item->y;
391
392 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
393 if (item == dnd->items[i]){
394 dnd->items[i] = 0;
395 break;
396 }
397 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400398
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200399 display = window_get_display(dnd->window);
400 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400401 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200402 dnd_drag->drag_surface =
403 wl_compositor_create_surface(compositor);
404
Kristian Høgsberga6c8b002012-04-13 12:55:45 -0400405 input_ungrab(input);
406
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400407 dnd_drag->data_source =
408 display_create_data_source(dnd->display);
409 wl_data_source_add_listener(dnd_drag->data_source,
410 &data_source_listener,
411 dnd_drag);
412 wl_data_source_offer(dnd_drag->data_source,
413 "application/x-wayland-dnd-flower");
414 wl_data_source_offer(dnd_drag->data_source,
415 "text/plain; charset=utf-8");
416 wl_data_device_start_drag(input_get_data_device(input),
417 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500418 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200419 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400420 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400421
422 input_set_pointer_image(input, time, POINTER_DRAGGING);
423
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400424 dnd_drag->opaque =
425 create_drag_cursor(dnd_drag, item, x, y, 1);
426 dnd_drag->translucent =
427 create_drag_cursor(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400428
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200429 buffer = display_get_buffer_for_surface(dnd->display, dnd_drag->translucent);
430 wl_surface_attach(dnd_drag->drag_surface, buffer,
431 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400432 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
433 dnd_drag->width, dnd_drag->height);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200434
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800435 window_schedule_redraw(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400436 }
437}
438
439static int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400440lookup_cursor(struct dnd *dnd, int x, int y)
441{
442 struct item *item;
443
444 item = dnd_get_item(dnd, x, y);
445 if (item)
446 return POINTER_HAND1;
447 else
448 return POINTER_LEFT_PTR;
449}
450
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500451static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500452dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400453 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400454{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500455 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400456}
457
458static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500459dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400460 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400461 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400462{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500463 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400464}
465
466static void
467dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400468 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400469 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400470{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400471 struct dnd *dnd = data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400472
473 if (!dnd_get_item(dnd, x, y)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400474 input_accept(input, types[0]);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400475 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400476 input_accept(input, NULL);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400477 }
478}
479
480static void
481dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
482{
483 struct dnd *dnd = user_data;
484 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400485 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400486 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400487
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400488 if (len == 0) {
489 return;
490 } else if (len != sizeof *message) {
491 fprintf(stderr, "odd message length %ld, expected %ld\n",
492 len, sizeof *message);
493 return;
494 }
495
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500496 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400497 item = item_create(dnd->display,
498 x - message->x_offset - allocation.x,
499 y - message->y_offset - allocation.y,
500 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400501
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400502 dnd_add_item(dnd, item);
503 window_schedule_redraw(dnd->window);
504}
505
506static void
507dnd_drop_handler(struct window *window, struct input *input,
508 int32_t x, int32_t y, void *data)
509{
510 struct dnd *dnd = data;
511
512 if (dnd_get_item(dnd, x, y)) {
513 fprintf(stderr, "got 'drop', but no target\n");
514 return;
515 }
516
517 input_receive_drag_data(input,
518 "application/x-wayland-dnd-flower",
519 dnd_receive_func, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400520}
521
522static struct dnd *
523dnd_create(struct display *display)
524{
525 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400526 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500527 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400528 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400529
530 dnd = malloc(sizeof *dnd);
531 if (dnd == NULL)
532 return dnd;
533 memset(dnd, 0, sizeof *dnd);
534
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500535 dnd->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500536 dnd->widget = frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500537 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400538
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400539 dnd->display = display;
540 dnd->key = 100;
541
542 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
543 x = (i % 4) * (item_width + item_padding) + item_padding;
544 y = (i / 4) * (item_height + item_padding) + item_padding;
545 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800546 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400547 else
548 dnd->items[i] = NULL;
549 }
550
551 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400552 window_set_keyboard_focus_handler(dnd->window,
553 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400554 window_set_data_handler(dnd->window, dnd_data_handler);
555 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400556
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500557 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500558 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
559 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
560 widget_set_button_handler(dnd->widget, dnd_button_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500561
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500562 width = 4 * (item_width + item_padding) + item_padding;
563 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400564
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500565 widget_schedule_resize(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400566
567 return dnd;
568}
569
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400570int
571main(int argc, char *argv[])
572{
573 struct display *d;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400574
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400575 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200576 if (d == NULL) {
577 fprintf(stderr, "failed to create display: %m\n");
578 return -1;
579 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400580
Kristian Høgsberg00439612011-01-25 15:16:01 -0500581 dnd_create(d);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400582
583 display_run(d);
584
585 return 0;
586}