blob: e893d36400f8ec37951555e5ce37d53b7873ae07 [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
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Philipp Brüschweilerf22d0ec2012-08-13 20:04:54 +020025#include <assert.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <math.h>
33#include <sys/time.h>
34#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040035#include <sys/epoll.h>
Derek Foremanba0f33d2014-11-20 15:32:40 -060036#include <stdbool.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040037
Pekka Paalanen50719bc2011-11-22 14:18:50 +020038#include <wayland-client.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030039#include <wayland-cursor.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040040
41#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040042#include "../shared/cairo-util.h"
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040043
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030044struct dnd_drag;
45
Derek Foremanba0f33d2014-11-20 15:32:40 -060046struct pointer {
47 struct input *input;
48 bool dragging;
49 struct wl_list link;
50};
51
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040052struct dnd {
53 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050054 struct widget *widget;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040055 struct display *display;
56 uint32_t key;
57 struct item *items[16];
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030058 int self_only;
59 struct dnd_drag *current_drag;
Derek Foremanba0f33d2014-11-20 15:32:40 -060060 struct wl_list pointers;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040061};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040062
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040063struct dnd_drag {
64 cairo_surface_t *translucent;
65 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040066 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040067 struct dnd *dnd;
68 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040069 uint32_t time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080070 struct item *item;
71 int x_offset, y_offset;
Kristian Høgsberg679f7162012-03-27 16:44:57 -040072 int width, height;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080073 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040074
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020075 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040076 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040077};
78
79struct item {
80 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080081 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040082 int x, y;
83};
84
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080085struct dnd_flower_message {
86 int seed, x_offset, y_offset;
87};
88
89
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040090static const int item_width = 64;
91static const int item_height = 64;
92static const int item_padding = 16;
93
Kristian Høgsberg938f1022013-09-04 19:36:49 -070094static const char flower_mime_type[] = "application/x-wayland-dnd-flower";
Kristian Høgsberg735bda22013-09-09 15:03:27 -070095static const char text_mime_type[] = "text/plain;charset=utf-8";
Kristian Høgsberg938f1022013-09-04 19:36:49 -070096
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040097static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080098item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040099{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800100 struct item *item;
101 struct timeval tv;
102
103 item = malloc(sizeof *item);
104 if (item == NULL)
105 return NULL;
106
107
108 gettimeofday(&tv, NULL);
109 item->seed = seed ? seed : tv.tv_usec;
110 srandom(item->seed);
111
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400112 const int petal_count = 3 + random() % 5;
113 const double r1 = 20 + random() % 10;
114 const double r2 = 5 + random() % 12;
115 const double u = (10 + random() % 90) / 100.0;
116 const double v = (random() % 90) / 100.0;
117
118 cairo_t *cr;
119 int i;
120 double t, dt = 2 * M_PI / (petal_count * 2);
121 double x1, y1, x2, y2, x3, y3;
122 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400123
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400124
125 rect.width = item_width;
126 rect.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300127 item->surface =
128 display_create_surface(display, NULL, &rect, SURFACE_SHM);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400129
130 item->x = x;
131 item->y = y;
132
133 cr = cairo_create(item->surface);
134 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
135 cairo_set_source_rgba(cr, 0, 0, 0, 0);
136 cairo_paint(cr);
137
138 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
139 cairo_translate(cr, item_width / 2, item_height / 2);
140 t = random();
141 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
142 for (i = 0; i < petal_count; i++, t += dt * 2) {
143 x1 = cos(t) * r1;
144 y1 = sin(t) * r1;
145 x2 = cos(t + dt) * r2;
146 y2 = sin(t + dt) * r2;
147 x3 = cos(t + 2 * dt) * r1;
148 y3 = sin(t + 2 * dt) * r1;
149
150 cairo_curve_to(cr,
151 x1 - y1 * u, y1 + x1 * u,
152 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400153 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400154
155 cairo_curve_to(cr,
156 x2 - y2 * v, y2 + x2 * v,
157 x3 + y3 * u, y3 - x3 * u,
158 x3, y3);
159 }
160
161 cairo_close_path(cr);
162
163 cairo_set_source_rgba(cr,
164 0.5 + (random() % 50) / 49.0,
165 0.5 + (random() % 50) / 49.0,
166 0.5 + (random() % 50) / 49.0,
167 0.5 + (random() % 100) / 99.0);
168
169 cairo_fill_preserve(cr);
170
171 cairo_set_line_width(cr, 1);
172 cairo_set_source_rgba(cr,
173 0.5 + (random() % 50) / 49.0,
174 0.5 + (random() % 50) / 49.0,
175 0.5 + (random() % 50) / 49.0,
176 0.5 + (random() % 100) / 99.0);
177 cairo_stroke(cr);
178
179 cairo_destroy(cr);
180
181 return item;
182}
183
184static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500185dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400186{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500187 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500188 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400189 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500190 cairo_surface_t *surface;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400191 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400192
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500193 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400194 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500195 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500196 cairo_rectangle(cr, allocation.x, allocation.y,
197 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500198
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400199 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
200 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400201 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400202
Kristian Høgsberg0fd49aa2012-07-23 21:32:46 -0400203 cairo_rectangle(cr, allocation.x, allocation.y,
204 allocation.width, allocation.height);
205 cairo_clip(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400206 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
207 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
208 if (!dnd->items[i])
209 continue;
210 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400211 dnd->items[i]->x + allocation.x,
212 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400213 cairo_paint(cr);
214 }
215
216 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400217 cairo_surface_destroy(surface);
218}
219
220static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400221keyboard_focus_handler(struct window *window,
222 struct input *device, void *data)
223{
224 struct dnd *dnd = data;
225
226 window_schedule_redraw(dnd->window);
227}
228
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800229static int
230dnd_add_item(struct dnd *dnd, struct item *item)
231{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400232 unsigned int i;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800233
234 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
235 if (dnd->items[i] == 0) {
236 dnd->items[i] = item;
237 return i;
238 }
239 }
240 return -1;
241}
242
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400243static struct item *
244dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
245{
246 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500247 struct rectangle allocation;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400248 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400249
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500250 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400251
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500252 x -= allocation.x;
253 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400254
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400255 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
256 item = dnd->items[i];
257 if (item &&
258 item->x <= x && x < item->x + item_width &&
259 item->y <= y && y < item->y + item_height)
260 return item;
261 }
262
263 return NULL;
264}
265
266static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400267data_source_target(void *data,
268 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400269{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400270 struct dnd_drag *dnd_drag = data;
271 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400272 cairo_surface_t *surface;
273 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400274
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800275 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400276 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400277 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400278 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400279 surface = dnd_drag->translucent;
280
281 buffer = display_get_buffer_for_surface(dnd->display, surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200282 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400283 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
284 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300285 wl_surface_commit(dnd_drag->drag_surface);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400286}
287
288static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400289data_source_send(void *data, struct wl_data_source *source,
290 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400291{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800292 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400293 struct dnd_drag *dnd_drag = data;
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700294 char buffer[128];
295 int n;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800296
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700297 if (strcmp(mime_type, flower_mime_type) == 0) {
298 dnd_flower_message.seed = dnd_drag->item->seed;
299 dnd_flower_message.x_offset = dnd_drag->x_offset;
300 dnd_flower_message.y_offset = dnd_drag->y_offset;
301
302 if (write(fd, &dnd_flower_message,
303 sizeof dnd_flower_message) < 0)
304 abort();
305 } else if (strcmp(mime_type, text_mime_type) == 0) {
306 n = snprintf(buffer, sizeof buffer, "seed=%d x=%d y=%d\n",
307 dnd_drag->item->seed,
308 dnd_drag->x_offset,
309 dnd_drag->y_offset);
310
311 if (write(fd, buffer, n) < 0)
312 abort();
313 }
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200314
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400315 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400316}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400317
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400318static void
319data_source_cancelled(void *data, struct wl_data_source *source)
320{
321 struct dnd_drag *dnd_drag = data;
322
323 /* The 'cancelled' event means that the source is no longer in
324 * use by the drag (or current selection). We need to clean
325 * up the drag object created and the local state. */
326
327 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800328
329 /* Destroy the item that has been dragged out */
330 cairo_surface_destroy(dnd_drag->item->surface);
331 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200332
333 wl_surface_destroy(dnd_drag->drag_surface);
334
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400335 cairo_surface_destroy(dnd_drag->translucent);
336 cairo_surface_destroy(dnd_drag->opaque);
337 free(dnd_drag);
338}
339
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400340static const struct wl_data_source_listener data_source_listener = {
341 data_source_target,
342 data_source_send,
343 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400344};
345
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400346static cairo_surface_t *
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800347create_drag_icon(struct dnd_drag *dnd_drag,
348 struct item *item, int32_t x, int32_t y, double opacity)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400349{
350 struct dnd *dnd = dnd_drag->dnd;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300351 cairo_surface_t *surface;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400352 struct rectangle rectangle;
353 cairo_pattern_t *pattern;
354 cairo_t *cr;
355
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800356 rectangle.width = item_width;
357 rectangle.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300358 surface = display_create_surface(dnd->display, NULL, &rectangle,
359 SURFACE_SHM);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400360
361 cr = cairo_create(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400362 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400363 cairo_set_source_surface(cr, item->surface, 0, 0);
364 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
365 cairo_mask(cr, pattern);
366 cairo_pattern_destroy(pattern);
367
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400368 cairo_destroy(cr);
369
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800370 dnd_drag->hotspot_x = x - item->x;
371 dnd_drag->hotspot_y = y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400372 dnd_drag->width = rectangle.width;
373 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400374
375 return surface;
376}
377
Xiong Zhang853a7792013-11-25 18:42:50 +0800378static int
379create_drag_source(struct dnd *dnd,
380 struct input *input, uint32_t time,
381 int32_t x, int32_t y)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400382{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400383 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500384 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400385 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200386 struct display *display;
387 struct wl_compositor *compositor;
388 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400389 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400390 uint32_t serial;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300391 cairo_surface_t *icon;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400392
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500393 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400394 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500395 x -= allocation.x;
396 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400397
Xiong Zhang853a7792013-11-25 18:42:50 +0800398 if (item) {
Brian Lovinbc919262013-08-07 15:34:59 -0700399 dnd_drag = xmalloc(sizeof *dnd_drag);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400400 dnd_drag->dnd = dnd;
401 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400402 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800403 dnd_drag->item = item;
404 dnd_drag->x_offset = x - item->x;
405 dnd_drag->y_offset = y - item->y;
406
407 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
408 if (item == dnd->items[i]){
409 dnd->items[i] = 0;
410 break;
411 }
412 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400413
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200414 display = window_get_display(dnd->window);
415 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400416 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200417 dnd_drag->drag_surface =
418 wl_compositor_create_surface(compositor);
419
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300420 if (dnd->self_only) {
421 dnd_drag->data_source = NULL;
422 } else {
423 dnd_drag->data_source =
424 display_create_data_source(dnd->display);
425 wl_data_source_add_listener(dnd_drag->data_source,
426 &data_source_listener,
427 dnd_drag);
428 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700429 flower_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300430 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700431 text_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300432 }
433
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400434 wl_data_device_start_drag(input_get_data_device(input),
435 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500436 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200437 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400438 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400439
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400440 dnd_drag->opaque =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800441 create_drag_icon(dnd_drag, item, x, y, 1);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400442 dnd_drag->translucent =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800443 create_drag_icon(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400444
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300445 if (dnd->self_only)
446 icon = dnd_drag->opaque;
447 else
448 icon = dnd_drag->translucent;
449
450 buffer = display_get_buffer_for_surface(dnd->display, icon);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200451 wl_surface_attach(dnd_drag->drag_surface, buffer,
452 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400453 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
454 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300455 wl_surface_commit(dnd_drag->drag_surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200456
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300457 dnd->current_drag = dnd_drag;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800458 window_schedule_redraw(dnd->window);
Xiong Zhang853a7792013-11-25 18:42:50 +0800459
460 return 0;
461 } else
462 return -1;
463}
464
Derek Foremanba0f33d2014-11-20 15:32:40 -0600465static int
466lookup_cursor(struct dnd *dnd, int x, int y)
467{
468 struct item *item;
469
470 item = dnd_get_item(dnd, x, y);
471 if (item)
472 return CURSOR_HAND1;
473 else
474 return CURSOR_LEFT_PTR;
475}
476
477/* Update all the mouse pointers in the window appropriately.
478 * Optionally, skip one (which will be the current pointer just
479 * about to start a drag). This is done here to save a scan
480 * through the pointer list.
481 */
482static void
483update_pointer_images_except(struct dnd *dnd, struct input *except)
484{
485 struct pointer *pointer;
486 int32_t x, y;
487
488 wl_list_for_each(pointer, &dnd->pointers, link) {
489 if (pointer->input == except) {
490 pointer->dragging = true;
491 continue;
492 }
493 input_get_position(pointer->input, &x, &y);
494 input_set_pointer_image(pointer->input,
495 lookup_cursor(dnd, x, y));
496 }
497}
498
Xiong Zhang853a7792013-11-25 18:42:50 +0800499static void
500dnd_button_handler(struct widget *widget,
501 struct input *input, uint32_t time,
502 uint32_t button, enum wl_pointer_button_state state,
503 void *data)
504{
505 struct dnd *dnd = data;
506 int32_t x, y;
507
508 input_get_position(input, &x, &y);
509 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
510 input_ungrab(input);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600511 if (create_drag_source(dnd, input, time, x, y) == 0) {
Xiong Zhang853a7792013-11-25 18:42:50 +0800512 input_set_pointer_image(input, CURSOR_DRAGGING);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600513 update_pointer_images_except(dnd, input);
514 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400515 }
516}
517
Xiong Zhang853a7792013-11-25 18:42:50 +0800518static void
519dnd_touch_down_handler(struct widget *widget,
520 struct input *input, uint32_t serial,
521 uint32_t time, int32_t id,
522 float x, float y, void *data)
523{
524 struct dnd *dnd = data;
525 int32_t int_x, int_y;
526
527 if (id > 0)
528 return;
529
530 int_x = (int32_t)x;
531 int_y = (int32_t)y;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800532 if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
533 touch_grab(input, 0);
Xiong Zhang853a7792013-11-25 18:42:50 +0800534}
535
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400536static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500537dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400538 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400539{
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300540 struct dnd *dnd = data;
Derek Foremanba0f33d2014-11-20 15:32:40 -0600541 struct pointer *new_pointer = malloc(sizeof *new_pointer);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300542
543 dnd->current_drag = NULL;
544
Derek Foremanba0f33d2014-11-20 15:32:40 -0600545 if (new_pointer) {
546 new_pointer->input = input;
547 new_pointer->dragging = false;
548 wl_list_insert(dnd->pointers.prev, &new_pointer->link);
549 }
550
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300551 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400552}
553
Derek Foremanba0f33d2014-11-20 15:32:40 -0600554static void
555dnd_leave_handler(struct widget *widget,
556 struct input *input, void *data)
557{
558 struct dnd *dnd = data;
559 struct pointer *pointer, *tmp;
560
561 wl_list_for_each_safe(pointer, tmp, &dnd->pointers, link)
562 if (pointer->input == input) {
563 wl_list_remove(&pointer->link);
564 free(pointer);
565 }
566}
567
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400568static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500569dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400570 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400571 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400572{
Derek Foremanba0f33d2014-11-20 15:32:40 -0600573 struct dnd *dnd = data;
574 struct pointer *pointer;
575
576 wl_list_for_each(pointer, &dnd->pointers, link)
577 if (pointer->input == input) {
578 if (pointer->dragging)
579 return CURSOR_DRAGGING;
580 break;
581 }
582
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500583 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400584}
585
586static void
587dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400588 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400589 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400590{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400591 struct dnd *dnd = data;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700592 int i, has_flower = 0;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400593
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300594 if (!types)
595 return;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700596 for (i = 0; types[i]; i++)
597 if (strcmp(types[i], flower_mime_type) == 0)
598 has_flower = 1;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300599
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700600 if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400601 input_accept(input, NULL);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300602 } else {
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700603 input_accept(input, flower_mime_type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400604 }
605}
606
607static void
608dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
609{
610 struct dnd *dnd = user_data;
611 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400612 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400613 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400614
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400615 if (len == 0) {
616 return;
617 } else if (len != sizeof *message) {
Damien Lespiau4df7e272012-10-26 01:15:44 +0100618 fprintf(stderr, "odd message length %zu, expected %zu\n",
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400619 len, sizeof *message);
620 return;
621 }
622
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500623 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400624 item = item_create(dnd->display,
625 x - message->x_offset - allocation.x,
626 y - message->y_offset - allocation.y,
627 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400628
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400629 dnd_add_item(dnd, item);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600630 update_pointer_images_except(dnd, NULL);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400631 window_schedule_redraw(dnd->window);
632}
633
634static void
635dnd_drop_handler(struct window *window, struct input *input,
636 int32_t x, int32_t y, void *data)
637{
638 struct dnd *dnd = data;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300639 struct dnd_flower_message message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400640
641 if (dnd_get_item(dnd, x, y)) {
642 fprintf(stderr, "got 'drop', but no target\n");
643 return;
644 }
645
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300646 if (!dnd->self_only) {
647 input_receive_drag_data(input,
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700648 flower_mime_type,
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300649 dnd_receive_func, dnd);
650 } else if (dnd->current_drag) {
651 message.seed = dnd->current_drag->item->seed;
652 message.x_offset = dnd->current_drag->x_offset;
653 message.y_offset = dnd->current_drag->y_offset;
654 dnd_receive_func(&message, sizeof message, x, y, dnd);
655 dnd->current_drag = NULL;
656 } else {
657 fprintf(stderr, "ignoring drop from another client\n");
658 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400659}
660
661static struct dnd *
662dnd_create(struct display *display)
663{
664 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400665 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500666 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400667 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400668
Peter Huttererf3d62272013-08-08 11:57:05 +1000669 dnd = xzalloc(sizeof *dnd);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500670 dnd->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500671 dnd->widget = window_frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500672 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400673
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400674 dnd->display = display;
675 dnd->key = 100;
676
Derek Foremanba0f33d2014-11-20 15:32:40 -0600677 wl_list_init(&dnd->pointers);
678
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400679 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
680 x = (i % 4) * (item_width + item_padding) + item_padding;
681 y = (i / 4) * (item_height + item_padding) + item_padding;
682 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800683 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400684 else
685 dnd->items[i] = NULL;
686 }
687
688 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400689 window_set_keyboard_focus_handler(dnd->window,
690 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400691 window_set_data_handler(dnd->window, dnd_data_handler);
692 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400693
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500694 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500695 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600696 widget_set_leave_handler(dnd->widget, dnd_leave_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500697 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
698 widget_set_button_handler(dnd->widget, dnd_button_handler);
Xiong Zhang853a7792013-11-25 18:42:50 +0800699 widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500700
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500701 width = 4 * (item_width + item_padding) + item_padding;
702 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400703
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500704 window_frame_set_child_size(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400705
706 return dnd;
707}
708
vivek31732f72014-05-15 18:58:16 +0530709static void
710dnd_destroy(struct dnd *dnd)
711{
712 widget_destroy(dnd->widget);
713 window_destroy(dnd->window);
714 free(dnd);
715}
716
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400717int
718main(int argc, char *argv[])
719{
720 struct display *d;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300721 struct dnd *dnd;
Bill Spitzak6fd10c62014-08-08 12:59:57 -0700722 int self_only = 0;
723
724 if (argc == 2 && !strcmp(argv[1], "--self-only"))
725 self_only = 1;
726 else if (argc > 1) {
727 printf("Usage: %s [OPTIONS]\n --self-only\n", argv[0]);
728 return 1;
729 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400730
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500731 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200732 if (d == NULL) {
733 fprintf(stderr, "failed to create display: %m\n");
734 return -1;
735 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400736
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300737 dnd = dnd_create(d);
Bill Spitzak6fd10c62014-08-08 12:59:57 -0700738 if (self_only)
739 dnd->self_only = 1;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400740
741 display_run(d);
742
vivek31732f72014-05-15 18:58:16 +0530743 dnd_destroy(dnd);
744 display_destroy(d);
745
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400746 return 0;
747}