blob: de1ca4731bf769a3191206cfa0634346955de31e [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>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030035#include <wayland-cursor.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040036
37#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040038#include "../shared/cairo-util.h"
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040039
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040040struct dnd {
41 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050042 struct widget *widget;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040043 struct display *display;
44 uint32_t key;
45 struct item *items[16];
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040046};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040047
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040048struct dnd_drag {
49 cairo_surface_t *translucent;
50 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040051 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040052 struct dnd *dnd;
53 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040054 uint32_t time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080055 struct item *item;
56 int x_offset, y_offset;
Kristian Høgsberg679f7162012-03-27 16:44:57 -040057 int width, height;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080058 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040059
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020060 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040061 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040062};
63
64struct item {
65 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080066 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040067 int x, y;
68};
69
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080070struct dnd_flower_message {
71 int seed, x_offset, y_offset;
72};
73
74
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040075static const int item_width = 64;
76static const int item_height = 64;
77static const int item_padding = 16;
78
79static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080080item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040081{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080082 struct item *item;
83 struct timeval tv;
84
85 item = malloc(sizeof *item);
86 if (item == NULL)
87 return NULL;
88
89
90 gettimeofday(&tv, NULL);
91 item->seed = seed ? seed : tv.tv_usec;
92 srandom(item->seed);
93
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040094 const int petal_count = 3 + random() % 5;
95 const double r1 = 20 + random() % 10;
96 const double r2 = 5 + random() % 12;
97 const double u = (10 + random() % 90) / 100.0;
98 const double v = (random() % 90) / 100.0;
99
100 cairo_t *cr;
101 int i;
102 double t, dt = 2 * M_PI / (petal_count * 2);
103 double x1, y1, x2, y2, x3, y3;
104 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400105
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400106
107 rect.width = item_width;
108 rect.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300109 item->surface =
110 display_create_surface(display, NULL, &rect, SURFACE_SHM);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400111
112 item->x = x;
113 item->y = y;
114
115 cr = cairo_create(item->surface);
116 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
117 cairo_set_source_rgba(cr, 0, 0, 0, 0);
118 cairo_paint(cr);
119
120 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
121 cairo_translate(cr, item_width / 2, item_height / 2);
122 t = random();
123 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
124 for (i = 0; i < petal_count; i++, t += dt * 2) {
125 x1 = cos(t) * r1;
126 y1 = sin(t) * r1;
127 x2 = cos(t + dt) * r2;
128 y2 = sin(t + dt) * r2;
129 x3 = cos(t + 2 * dt) * r1;
130 y3 = sin(t + 2 * dt) * r1;
131
132 cairo_curve_to(cr,
133 x1 - y1 * u, y1 + x1 * u,
134 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400135 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400136
137 cairo_curve_to(cr,
138 x2 - y2 * v, y2 + x2 * v,
139 x3 + y3 * u, y3 - x3 * u,
140 x3, y3);
141 }
142
143 cairo_close_path(cr);
144
145 cairo_set_source_rgba(cr,
146 0.5 + (random() % 50) / 49.0,
147 0.5 + (random() % 50) / 49.0,
148 0.5 + (random() % 50) / 49.0,
149 0.5 + (random() % 100) / 99.0);
150
151 cairo_fill_preserve(cr);
152
153 cairo_set_line_width(cr, 1);
154 cairo_set_source_rgba(cr,
155 0.5 + (random() % 50) / 49.0,
156 0.5 + (random() % 50) / 49.0,
157 0.5 + (random() % 50) / 49.0,
158 0.5 + (random() % 100) / 99.0);
159 cairo_stroke(cr);
160
161 cairo_destroy(cr);
162
163 return item;
164}
165
166static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500167dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400168{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500169 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500170 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400171 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500172 cairo_surface_t *surface;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400173 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400174
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500175 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400176 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500177 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500178 cairo_rectangle(cr, allocation.x, allocation.y,
179 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500180
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400181 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
182 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400183 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400184
185 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
186 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
187 if (!dnd->items[i])
188 continue;
189 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400190 dnd->items[i]->x + allocation.x,
191 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400192 cairo_paint(cr);
193 }
194
195 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400196 cairo_surface_destroy(surface);
197}
198
199static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400200keyboard_focus_handler(struct window *window,
201 struct input *device, void *data)
202{
203 struct dnd *dnd = data;
204
205 window_schedule_redraw(dnd->window);
206}
207
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800208static int
209dnd_add_item(struct dnd *dnd, struct item *item)
210{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400211 unsigned int i;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800212
213 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
214 if (dnd->items[i] == 0) {
215 dnd->items[i] = item;
216 return i;
217 }
218 }
219 return -1;
220}
221
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400222static struct item *
223dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
224{
225 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500226 struct rectangle allocation;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400227 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400228
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500229 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400230
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500231 x -= allocation.x;
232 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400233
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400234 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
235 item = dnd->items[i];
236 if (item &&
237 item->x <= x && x < item->x + item_width &&
238 item->y <= y && y < item->y + item_height)
239 return item;
240 }
241
242 return NULL;
243}
244
245static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400246data_source_target(void *data,
247 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400248{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400249 struct dnd_drag *dnd_drag = data;
250 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400251 cairo_surface_t *surface;
252 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400253
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800254 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400255 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400256 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400257 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400258 surface = dnd_drag->translucent;
259
260 buffer = display_get_buffer_for_surface(dnd->display, surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200261 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400262 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
263 dnd_drag->width, dnd_drag->height);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400264}
265
266static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400267data_source_send(void *data, struct wl_data_source *source,
268 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400269{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800270 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400271 struct dnd_drag *dnd_drag = data;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800272
273 dnd_flower_message.seed = dnd_drag->item->seed;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800274 dnd_flower_message.x_offset = dnd_drag->x_offset;
275 dnd_flower_message.y_offset = dnd_drag->y_offset;
276
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200277 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
278 abort();
279
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400280 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400281}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400282
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400283static void
284data_source_cancelled(void *data, struct wl_data_source *source)
285{
286 struct dnd_drag *dnd_drag = data;
287
288 /* The 'cancelled' event means that the source is no longer in
289 * use by the drag (or current selection). We need to clean
290 * up the drag object created and the local state. */
291
292 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800293
294 /* Destroy the item that has been dragged out */
295 cairo_surface_destroy(dnd_drag->item->surface);
296 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200297
298 wl_surface_destroy(dnd_drag->drag_surface);
299
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400300 cairo_surface_destroy(dnd_drag->translucent);
301 cairo_surface_destroy(dnd_drag->opaque);
302 free(dnd_drag);
303}
304
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400305static const struct wl_data_source_listener data_source_listener = {
306 data_source_target,
307 data_source_send,
308 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400309};
310
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400311static cairo_surface_t *
312create_drag_cursor(struct dnd_drag *dnd_drag,
313 struct item *item, int32_t x, int32_t y, double opacity)
314{
315 struct dnd *dnd = dnd_drag->dnd;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300316 cairo_surface_t *surface;
317 struct wl_cursor_image *pointer;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400318 struct rectangle rectangle;
319 cairo_pattern_t *pattern;
320 cairo_t *cr;
321
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300322 pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400323
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300324 rectangle.width = item_width + 2 * pointer->width;
325 rectangle.height = item_height + 2 * pointer->height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300326 surface = display_create_surface(dnd->display, NULL, &rectangle,
327 SURFACE_SHM);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400328
329 cr = cairo_create(surface);
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300330 cairo_translate(cr, pointer->width, pointer->height);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400331
332 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
333 cairo_set_source_rgba(cr, 0, 0, 0, 0);
334 cairo_paint(cr);
335
336 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
337 cairo_set_source_surface(cr, item->surface, 0, 0);
338 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
339 cairo_mask(cr, pattern);
340 cairo_pattern_destroy(pattern);
341
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400342 /* FIXME: more cairo-gl brokeness */
Benjamin Franzke47eb8f42011-10-07 09:08:56 +0200343 surface_flush_device(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400344 cairo_destroy(cr);
345
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300346 dnd_drag->hotspot_x = pointer->width + x - item->x;
347 dnd_drag->hotspot_y = pointer->height + y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400348 dnd_drag->width = rectangle.width;
349 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400350
351 return surface;
352}
353
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400354static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500355dnd_button_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400356 struct input *input, uint32_t time,
Daniel Stone5d663712012-05-04 11:21:55 +0100357 uint32_t button, uint32_t state, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400358{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500359 struct dnd *dnd = data;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400360 int32_t x, y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400361 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500362 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400363 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200364 struct display *display;
365 struct wl_compositor *compositor;
366 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400367 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400368 uint32_t serial;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400369
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500370 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400371 input_get_position(input, &x, &y);
372 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500373 x -= allocation.x;
374 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400375
376 if (item && state == 1) {
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400377 dnd_drag = malloc(sizeof *dnd_drag);
378 dnd_drag->dnd = dnd;
379 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400380 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800381 dnd_drag->item = item;
382 dnd_drag->x_offset = x - item->x;
383 dnd_drag->y_offset = y - item->y;
384
385 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
386 if (item == dnd->items[i]){
387 dnd->items[i] = 0;
388 break;
389 }
390 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400391
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200392 display = window_get_display(dnd->window);
393 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400394 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200395 dnd_drag->drag_surface =
396 wl_compositor_create_surface(compositor);
397
Kristian Høgsberga6c8b002012-04-13 12:55:45 -0400398 input_ungrab(input);
399
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400400 dnd_drag->data_source =
401 display_create_data_source(dnd->display);
402 wl_data_source_add_listener(dnd_drag->data_source,
403 &data_source_listener,
404 dnd_drag);
405 wl_data_source_offer(dnd_drag->data_source,
406 "application/x-wayland-dnd-flower");
407 wl_data_source_offer(dnd_drag->data_source,
408 "text/plain; charset=utf-8");
409 wl_data_device_start_drag(input_get_data_device(input),
410 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500411 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200412 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400413 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400414
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300415 input_set_pointer_image(input, time, CURSOR_DRAGGING);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400416
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400417 dnd_drag->opaque =
418 create_drag_cursor(dnd_drag, item, x, y, 1);
419 dnd_drag->translucent =
420 create_drag_cursor(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400421
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200422 buffer = display_get_buffer_for_surface(dnd->display, dnd_drag->translucent);
423 wl_surface_attach(dnd_drag->drag_surface, buffer,
424 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400425 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
426 dnd_drag->width, dnd_drag->height);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200427
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800428 window_schedule_redraw(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400429 }
430}
431
432static int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400433lookup_cursor(struct dnd *dnd, int x, int y)
434{
435 struct item *item;
436
437 item = dnd_get_item(dnd, x, y);
438 if (item)
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300439 return CURSOR_HAND1;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400440 else
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300441 return CURSOR_LEFT_PTR;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400442}
443
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500444static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500445dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400446 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400447{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500448 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400449}
450
451static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500452dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400453 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400454 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400455{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500456 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400457}
458
459static void
460dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400461 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400462 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400463{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400464 struct dnd *dnd = data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400465
466 if (!dnd_get_item(dnd, x, y)) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400467 input_accept(input, types[0]);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400468 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400469 input_accept(input, NULL);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400470 }
471}
472
473static void
474dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
475{
476 struct dnd *dnd = user_data;
477 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400478 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400479 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400480
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400481 if (len == 0) {
482 return;
483 } else if (len != sizeof *message) {
484 fprintf(stderr, "odd message length %ld, expected %ld\n",
485 len, sizeof *message);
486 return;
487 }
488
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500489 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400490 item = item_create(dnd->display,
491 x - message->x_offset - allocation.x,
492 y - message->y_offset - allocation.y,
493 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400494
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400495 dnd_add_item(dnd, item);
496 window_schedule_redraw(dnd->window);
497}
498
499static void
500dnd_drop_handler(struct window *window, struct input *input,
501 int32_t x, int32_t y, void *data)
502{
503 struct dnd *dnd = data;
504
505 if (dnd_get_item(dnd, x, y)) {
506 fprintf(stderr, "got 'drop', but no target\n");
507 return;
508 }
509
510 input_receive_drag_data(input,
511 "application/x-wayland-dnd-flower",
512 dnd_receive_func, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400513}
514
515static struct dnd *
516dnd_create(struct display *display)
517{
518 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400519 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500520 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400521 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400522
523 dnd = malloc(sizeof *dnd);
524 if (dnd == NULL)
525 return dnd;
526 memset(dnd, 0, sizeof *dnd);
527
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500528 dnd->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500529 dnd->widget = frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500530 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400531
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400532 dnd->display = display;
533 dnd->key = 100;
534
535 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
536 x = (i % 4) * (item_width + item_padding) + item_padding;
537 y = (i / 4) * (item_height + item_padding) + item_padding;
538 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800539 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400540 else
541 dnd->items[i] = NULL;
542 }
543
544 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400545 window_set_keyboard_focus_handler(dnd->window,
546 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400547 window_set_data_handler(dnd->window, dnd_data_handler);
548 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400549
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500550 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500551 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
552 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
553 widget_set_button_handler(dnd->widget, dnd_button_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500554
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500555 width = 4 * (item_width + item_padding) + item_padding;
556 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400557
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500558 widget_schedule_resize(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400559
560 return dnd;
561}
562
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400563int
564main(int argc, char *argv[])
565{
566 struct display *d;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400567
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400568 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200569 if (d == NULL) {
570 fprintf(stderr, "failed to create display: %m\n");
571 return -1;
572 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400573
Kristian Høgsberg00439612011-01-25 15:16:01 -0500574 dnd_create(d);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400575
576 display_run(d);
577
578 return 0;
579}