blob: d32655deabf97c7eb02e6aa8f8c46aff049c9002 [file] [log] [blame]
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -04001/*
2 * Copyright © 2010 Kristian Høgsberg
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040022 */
23
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010024#include "config.h"
25
Philipp Brüschweilerf22d0ec2012-08-13 20:04:54 +020026#include <assert.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <math.h>
34#include <sys/time.h>
35#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040036#include <sys/epoll.h>
Derek Foremanba0f33d2014-11-20 15:32:40 -060037#include <stdbool.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040038
Pekka Paalanen50719bc2011-11-22 14:18:50 +020039#include <wayland-client.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030040#include <wayland-cursor.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040041
42#include "window.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070043#include "shared/cairo-util.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070044#include "shared/helpers.h"
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040045
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030046struct dnd_drag;
47
Derek Foremanba0f33d2014-11-20 15:32:40 -060048struct pointer {
49 struct input *input;
50 bool dragging;
51 struct wl_list link;
52};
53
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040054struct dnd {
55 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050056 struct widget *widget;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040057 struct display *display;
58 uint32_t key;
59 struct item *items[16];
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030060 int self_only;
61 struct dnd_drag *current_drag;
Derek Foremanba0f33d2014-11-20 15:32:40 -060062 struct wl_list pointers;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040063};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040064
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040065struct dnd_drag {
66 cairo_surface_t *translucent;
67 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040068 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040069 struct dnd *dnd;
70 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040071 uint32_t time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080072 struct item *item;
73 int x_offset, y_offset;
Kristian Høgsberg679f7162012-03-27 16:44:57 -040074 int width, height;
Carlos Garnacho9c931792016-01-18 23:52:12 +010075 uint32_t dnd_action;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080076 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040077
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020078 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040079 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040080};
81
82struct item {
83 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080084 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040085 int x, y;
86};
87
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080088struct dnd_flower_message {
89 int seed, x_offset, y_offset;
90};
91
92
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040093static const int item_width = 64;
94static const int item_height = 64;
95static const int item_padding = 16;
96
Kristian Høgsberg938f1022013-09-04 19:36:49 -070097static const char flower_mime_type[] = "application/x-wayland-dnd-flower";
Kristian Høgsberg735bda22013-09-09 15:03:27 -070098static const char text_mime_type[] = "text/plain;charset=utf-8";
Kristian Høgsberg938f1022013-09-04 19:36:49 -070099
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400100static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800101item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400102{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800103 struct item *item;
104 struct timeval tv;
105
106 item = malloc(sizeof *item);
107 if (item == NULL)
108 return NULL;
Michael Vetter2a18a522015-05-15 17:17:47 +0200109
110
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800111 gettimeofday(&tv, NULL);
112 item->seed = seed ? seed : tv.tv_usec;
113 srandom(item->seed);
Michael Vetter2a18a522015-05-15 17:17:47 +0200114
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400115 const int petal_count = 3 + random() % 5;
116 const double r1 = 20 + random() % 10;
117 const double r2 = 5 + random() % 12;
118 const double u = (10 + random() % 90) / 100.0;
119 const double v = (random() % 90) / 100.0;
120
121 cairo_t *cr;
122 int i;
123 double t, dt = 2 * M_PI / (petal_count * 2);
124 double x1, y1, x2, y2, x3, y3;
125 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400126
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400127
128 rect.width = item_width;
129 rect.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300130 item->surface =
131 display_create_surface(display, NULL, &rect, SURFACE_SHM);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400132
133 item->x = x;
134 item->y = y;
135
136 cr = cairo_create(item->surface);
137 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
138 cairo_set_source_rgba(cr, 0, 0, 0, 0);
139 cairo_paint(cr);
140
141 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
142 cairo_translate(cr, item_width / 2, item_height / 2);
143 t = random();
144 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
145 for (i = 0; i < petal_count; i++, t += dt * 2) {
146 x1 = cos(t) * r1;
147 y1 = sin(t) * r1;
148 x2 = cos(t + dt) * r2;
149 y2 = sin(t + dt) * r2;
150 x3 = cos(t + 2 * dt) * r1;
151 y3 = sin(t + 2 * dt) * r1;
152
153 cairo_curve_to(cr,
154 x1 - y1 * u, y1 + x1 * u,
155 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400156 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400157
158 cairo_curve_to(cr,
159 x2 - y2 * v, y2 + x2 * v,
160 x3 + y3 * u, y3 - x3 * u,
161 x3, y3);
162 }
163
164 cairo_close_path(cr);
165
166 cairo_set_source_rgba(cr,
167 0.5 + (random() % 50) / 49.0,
168 0.5 + (random() % 50) / 49.0,
169 0.5 + (random() % 50) / 49.0,
170 0.5 + (random() % 100) / 99.0);
171
172 cairo_fill_preserve(cr);
173
174 cairo_set_line_width(cr, 1);
175 cairo_set_source_rgba(cr,
176 0.5 + (random() % 50) / 49.0,
177 0.5 + (random() % 50) / 49.0,
178 0.5 + (random() % 50) / 49.0,
179 0.5 + (random() % 100) / 99.0);
180 cairo_stroke(cr);
181
182 cairo_destroy(cr);
183
184 return item;
185}
186
187static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500188dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400189{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500190 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500191 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400192 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500193 cairo_surface_t *surface;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400194 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400195
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500196 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400197 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500198 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500199 cairo_rectangle(cr, allocation.x, allocation.y,
200 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500201
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400202 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
203 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400204 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400205
Kristian Høgsberg0fd49aa2012-07-23 21:32:46 -0400206 cairo_rectangle(cr, allocation.x, allocation.y,
207 allocation.width, allocation.height);
208 cairo_clip(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400209 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
210 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
211 if (!dnd->items[i])
212 continue;
213 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400214 dnd->items[i]->x + allocation.x,
215 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400216 cairo_paint(cr);
217 }
218
219 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400220 cairo_surface_destroy(surface);
221}
222
223static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400224keyboard_focus_handler(struct window *window,
225 struct input *device, void *data)
226{
227 struct dnd *dnd = data;
228
229 window_schedule_redraw(dnd->window);
230}
231
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800232static int
233dnd_add_item(struct dnd *dnd, struct item *item)
234{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400235 unsigned int i;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800236
237 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
238 if (dnd->items[i] == 0) {
239 dnd->items[i] = item;
240 return i;
241 }
242 }
243 return -1;
244}
245
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400246static struct item *
247dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
248{
249 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500250 struct rectangle allocation;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400251 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400252
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500253 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400254
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500255 x -= allocation.x;
256 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400257
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400258 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
259 item = dnd->items[i];
260 if (item &&
261 item->x <= x && x < item->x + item_width &&
262 item->y <= y && y < item->y + item_height)
263 return item;
264 }
265
266 return NULL;
267}
268
269static void
Carlos Garnacho9c931792016-01-18 23:52:12 +0100270dnd_drag_update_surface(struct dnd_drag *dnd_drag)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400271{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400272 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400273 cairo_surface_t *surface;
274 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400275
Carlos Garnacho9c931792016-01-18 23:52:12 +0100276 if (dnd_drag->mime_type && dnd_drag->dnd_action)
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
Carlos Garnacho9c931792016-01-18 23:52:12 +0100289data_source_target(void *data,
290 struct wl_data_source *source, const char *mime_type)
291{
292 struct dnd_drag *dnd_drag = data;
293
294 dnd_drag->mime_type = mime_type;
295 dnd_drag_update_surface(dnd_drag);
296}
297
298static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400299data_source_send(void *data, struct wl_data_source *source,
300 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400301{
Michael Vetter2a18a522015-05-15 17:17:47 +0200302 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400303 struct dnd_drag *dnd_drag = data;
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700304 char buffer[128];
305 int n;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800306
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700307 if (strcmp(mime_type, flower_mime_type) == 0) {
308 dnd_flower_message.seed = dnd_drag->item->seed;
309 dnd_flower_message.x_offset = dnd_drag->x_offset;
310 dnd_flower_message.y_offset = dnd_drag->y_offset;
311
312 if (write(fd, &dnd_flower_message,
313 sizeof dnd_flower_message) < 0)
314 abort();
315 } else if (strcmp(mime_type, text_mime_type) == 0) {
316 n = snprintf(buffer, sizeof buffer, "seed=%d x=%d y=%d\n",
317 dnd_drag->item->seed,
318 dnd_drag->x_offset,
319 dnd_drag->y_offset);
320
321 if (write(fd, buffer, n) < 0)
322 abort();
323 }
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200324
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400325 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400326}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400327
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400328static void
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100329dnd_drag_destroy(struct dnd_drag *dnd_drag)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400330{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400331 wl_data_source_destroy(dnd_drag->data_source);
Michael Vetter2a18a522015-05-15 17:17:47 +0200332
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800333 /* Destroy the item that has been dragged out */
334 cairo_surface_destroy(dnd_drag->item->surface);
335 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200336
337 wl_surface_destroy(dnd_drag->drag_surface);
338
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400339 cairo_surface_destroy(dnd_drag->translucent);
340 cairo_surface_destroy(dnd_drag->opaque);
341 free(dnd_drag);
342}
343
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100344static void
345data_source_cancelled(void *data, struct wl_data_source *source)
346{
347 struct dnd_drag *dnd_drag = data;
348
349 /* The 'cancelled' event means that the source is no longer in
350 * use by the drag (or current selection). We need to clean
351 * up the drag object created and the local state. */
352 dnd_drag_destroy(dnd_drag);
353}
354
355static void
356data_source_dnd_drop_performed(void *data, struct wl_data_source *source)
357{
358}
359
360static void
361data_source_dnd_finished(void *data, struct wl_data_source *source)
362{
363 struct dnd_drag *dnd_drag = data;
364
365 /* The operation is already finished, we can destroy all
366 * related data.
367 */
368 dnd_drag_destroy(dnd_drag);
369}
370
Carlos Garnacho9c931792016-01-18 23:52:12 +0100371static void
372data_source_action(void *data, struct wl_data_source *source, uint32_t dnd_action)
373{
374 struct dnd_drag *dnd_drag = data;
375
376 dnd_drag->dnd_action = dnd_action;
377 dnd_drag_update_surface(dnd_drag);
378}
379
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400380static const struct wl_data_source_listener data_source_listener = {
381 data_source_target,
382 data_source_send,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100383 data_source_cancelled,
384 data_source_dnd_drop_performed,
385 data_source_dnd_finished,
Carlos Garnacho9c931792016-01-18 23:52:12 +0100386 data_source_action,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400387};
388
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400389static cairo_surface_t *
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800390create_drag_icon(struct dnd_drag *dnd_drag,
391 struct item *item, int32_t x, int32_t y, double opacity)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400392{
393 struct dnd *dnd = dnd_drag->dnd;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300394 cairo_surface_t *surface;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400395 struct rectangle rectangle;
396 cairo_pattern_t *pattern;
397 cairo_t *cr;
398
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800399 rectangle.width = item_width;
400 rectangle.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300401 surface = display_create_surface(dnd->display, NULL, &rectangle,
402 SURFACE_SHM);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400403
404 cr = cairo_create(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400405 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400406 cairo_set_source_surface(cr, item->surface, 0, 0);
407 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
408 cairo_mask(cr, pattern);
409 cairo_pattern_destroy(pattern);
410
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400411 cairo_destroy(cr);
412
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800413 dnd_drag->hotspot_x = x - item->x;
414 dnd_drag->hotspot_y = y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400415 dnd_drag->width = rectangle.width;
416 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400417
418 return surface;
419}
420
Xiong Zhang853a7792013-11-25 18:42:50 +0800421static int
422create_drag_source(struct dnd *dnd,
423 struct input *input, uint32_t time,
424 int32_t x, int32_t y)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400425{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400426 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500427 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400428 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200429 struct display *display;
430 struct wl_compositor *compositor;
431 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400432 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400433 uint32_t serial;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300434 cairo_surface_t *icon;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400435
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500436 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400437 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500438 x -= allocation.x;
439 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400440
Xiong Zhang853a7792013-11-25 18:42:50 +0800441 if (item) {
Brian Lovinbc919262013-08-07 15:34:59 -0700442 dnd_drag = xmalloc(sizeof *dnd_drag);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400443 dnd_drag->dnd = dnd;
444 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400445 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800446 dnd_drag->item = item;
447 dnd_drag->x_offset = x - item->x;
448 dnd_drag->y_offset = y - item->y;
Carlos Garnacho9c931792016-01-18 23:52:12 +0100449 dnd_drag->dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
450 dnd_drag->mime_type = NULL;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800451
452 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
453 if (item == dnd->items[i]){
454 dnd->items[i] = 0;
455 break;
456 }
457 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400458
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200459 display = window_get_display(dnd->window);
460 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400461 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200462 dnd_drag->drag_surface =
463 wl_compositor_create_surface(compositor);
464
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300465 if (dnd->self_only) {
466 dnd_drag->data_source = NULL;
467 } else {
468 dnd_drag->data_source =
469 display_create_data_source(dnd->display);
470 wl_data_source_add_listener(dnd_drag->data_source,
471 &data_source_listener,
472 dnd_drag);
473 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700474 flower_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300475 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700476 text_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300477 }
478
Carlos Garnacho9c931792016-01-18 23:52:12 +0100479 if (display_get_data_device_manager_version(display) >=
480 WL_DATA_SOURCE_SET_ACTIONS_SINCE_VERSION) {
481 wl_data_source_set_actions(dnd_drag->data_source,
482 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE);
483 }
484
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400485 wl_data_device_start_drag(input_get_data_device(input),
486 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500487 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200488 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400489 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400490
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400491 dnd_drag->opaque =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800492 create_drag_icon(dnd_drag, item, x, y, 1);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400493 dnd_drag->translucent =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800494 create_drag_icon(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400495
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300496 if (dnd->self_only)
497 icon = dnd_drag->opaque;
498 else
499 icon = dnd_drag->translucent;
500
501 buffer = display_get_buffer_for_surface(dnd->display, icon);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200502 wl_surface_attach(dnd_drag->drag_surface, buffer,
503 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400504 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
505 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300506 wl_surface_commit(dnd_drag->drag_surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200507
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300508 dnd->current_drag = dnd_drag;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800509 window_schedule_redraw(dnd->window);
Xiong Zhang853a7792013-11-25 18:42:50 +0800510
511 return 0;
512 } else
513 return -1;
514}
515
Derek Foremanba0f33d2014-11-20 15:32:40 -0600516static int
517lookup_cursor(struct dnd *dnd, int x, int y)
518{
519 struct item *item;
520
521 item = dnd_get_item(dnd, x, y);
522 if (item)
523 return CURSOR_HAND1;
524 else
525 return CURSOR_LEFT_PTR;
526}
527
528/* Update all the mouse pointers in the window appropriately.
529 * Optionally, skip one (which will be the current pointer just
530 * about to start a drag). This is done here to save a scan
531 * through the pointer list.
532 */
533static void
534update_pointer_images_except(struct dnd *dnd, struct input *except)
535{
536 struct pointer *pointer;
537 int32_t x, y;
538
539 wl_list_for_each(pointer, &dnd->pointers, link) {
540 if (pointer->input == except) {
541 pointer->dragging = true;
542 continue;
543 }
544 input_get_position(pointer->input, &x, &y);
545 input_set_pointer_image(pointer->input,
546 lookup_cursor(dnd, x, y));
547 }
548}
549
Xiong Zhang853a7792013-11-25 18:42:50 +0800550static void
551dnd_button_handler(struct widget *widget,
552 struct input *input, uint32_t time,
553 uint32_t button, enum wl_pointer_button_state state,
554 void *data)
555{
556 struct dnd *dnd = data;
557 int32_t x, y;
558
559 input_get_position(input, &x, &y);
560 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
561 input_ungrab(input);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600562 if (create_drag_source(dnd, input, time, x, y) == 0) {
Xiong Zhang853a7792013-11-25 18:42:50 +0800563 input_set_pointer_image(input, CURSOR_DRAGGING);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600564 update_pointer_images_except(dnd, input);
565 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400566 }
567}
568
Xiong Zhang853a7792013-11-25 18:42:50 +0800569static void
570dnd_touch_down_handler(struct widget *widget,
571 struct input *input, uint32_t serial,
572 uint32_t time, int32_t id,
573 float x, float y, void *data)
574{
575 struct dnd *dnd = data;
576 int32_t int_x, int_y;
577
578 if (id > 0)
579 return;
580
581 int_x = (int32_t)x;
582 int_y = (int32_t)y;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800583 if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
584 touch_grab(input, 0);
Xiong Zhang853a7792013-11-25 18:42:50 +0800585}
586
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400587static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500588dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400589 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400590{
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300591 struct dnd *dnd = data;
Derek Foremanba0f33d2014-11-20 15:32:40 -0600592 struct pointer *new_pointer = malloc(sizeof *new_pointer);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300593
594 dnd->current_drag = NULL;
595
Derek Foremanba0f33d2014-11-20 15:32:40 -0600596 if (new_pointer) {
597 new_pointer->input = input;
598 new_pointer->dragging = false;
599 wl_list_insert(dnd->pointers.prev, &new_pointer->link);
600 }
601
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300602 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400603}
604
Derek Foremanba0f33d2014-11-20 15:32:40 -0600605static void
606dnd_leave_handler(struct widget *widget,
607 struct input *input, void *data)
608{
609 struct dnd *dnd = data;
610 struct pointer *pointer, *tmp;
611
612 wl_list_for_each_safe(pointer, tmp, &dnd->pointers, link)
613 if (pointer->input == input) {
614 wl_list_remove(&pointer->link);
615 free(pointer);
616 }
617}
618
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400619static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500620dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400621 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400622 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400623{
Derek Foremanba0f33d2014-11-20 15:32:40 -0600624 struct dnd *dnd = data;
625 struct pointer *pointer;
626
627 wl_list_for_each(pointer, &dnd->pointers, link)
628 if (pointer->input == input) {
629 if (pointer->dragging)
630 return CURSOR_DRAGGING;
631 break;
632 }
633
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500634 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400635}
636
637static void
638dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400639 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400640 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400641{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400642 struct dnd *dnd = data;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700643 int i, has_flower = 0;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400644
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300645 if (!types)
646 return;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700647 for (i = 0; types[i]; i++)
648 if (strcmp(types[i], flower_mime_type) == 0)
649 has_flower = 1;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300650
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700651 if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400652 input_accept(input, NULL);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300653 } else {
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700654 input_accept(input, flower_mime_type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400655 }
656}
657
658static void
659dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
660{
661 struct dnd *dnd = user_data;
662 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400663 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400664 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400665
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400666 if (len == 0) {
667 return;
668 } else if (len != sizeof *message) {
Damien Lespiau4df7e272012-10-26 01:15:44 +0100669 fprintf(stderr, "odd message length %zu, expected %zu\n",
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400670 len, sizeof *message);
671 return;
672 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200673
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500674 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400675 item = item_create(dnd->display,
676 x - message->x_offset - allocation.x,
677 y - message->y_offset - allocation.y,
678 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400679
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400680 dnd_add_item(dnd, item);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600681 update_pointer_images_except(dnd, NULL);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400682 window_schedule_redraw(dnd->window);
683}
684
685static void
686dnd_drop_handler(struct window *window, struct input *input,
687 int32_t x, int32_t y, void *data)
688{
689 struct dnd *dnd = data;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300690 struct dnd_flower_message message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400691
692 if (dnd_get_item(dnd, x, y)) {
693 fprintf(stderr, "got 'drop', but no target\n");
694 return;
695 }
696
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300697 if (!dnd->self_only) {
698 input_receive_drag_data(input,
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700699 flower_mime_type,
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300700 dnd_receive_func, dnd);
701 } else if (dnd->current_drag) {
702 message.seed = dnd->current_drag->item->seed;
703 message.x_offset = dnd->current_drag->x_offset;
704 message.y_offset = dnd->current_drag->y_offset;
705 dnd_receive_func(&message, sizeof message, x, y, dnd);
706 dnd->current_drag = NULL;
707 } else {
708 fprintf(stderr, "ignoring drop from another client\n");
709 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400710}
711
712static struct dnd *
713dnd_create(struct display *display)
714{
715 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400716 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500717 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400718 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400719
Peter Huttererf3d62272013-08-08 11:57:05 +1000720 dnd = xzalloc(sizeof *dnd);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500721 dnd->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500722 dnd->widget = window_frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500723 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400724
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400725 dnd->display = display;
726 dnd->key = 100;
727
Derek Foremanba0f33d2014-11-20 15:32:40 -0600728 wl_list_init(&dnd->pointers);
729
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400730 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
731 x = (i % 4) * (item_width + item_padding) + item_padding;
732 y = (i / 4) * (item_height + item_padding) + item_padding;
733 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800734 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400735 else
736 dnd->items[i] = NULL;
737 }
738
739 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400740 window_set_keyboard_focus_handler(dnd->window,
741 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400742 window_set_data_handler(dnd->window, dnd_data_handler);
743 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400744
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500745 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500746 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600747 widget_set_leave_handler(dnd->widget, dnd_leave_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500748 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
749 widget_set_button_handler(dnd->widget, dnd_button_handler);
Xiong Zhang853a7792013-11-25 18:42:50 +0800750 widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500751
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500752 width = 4 * (item_width + item_padding) + item_padding;
753 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400754
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500755 window_frame_set_child_size(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400756
757 return dnd;
758}
759
vivek31732f72014-05-15 18:58:16 +0530760static void
761dnd_destroy(struct dnd *dnd)
762{
763 widget_destroy(dnd->widget);
764 window_destroy(dnd->window);
765 free(dnd);
766}
767
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400768int
769main(int argc, char *argv[])
770{
771 struct display *d;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300772 struct dnd *dnd;
Bill Spitzak6fd10c62014-08-08 12:59:57 -0700773 int self_only = 0;
774
775 if (argc == 2 && !strcmp(argv[1], "--self-only"))
776 self_only = 1;
777 else if (argc > 1) {
778 printf("Usage: %s [OPTIONS]\n --self-only\n", argv[0]);
779 return 1;
780 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400781
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500782 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200783 if (d == NULL) {
784 fprintf(stderr, "failed to create display: %m\n");
785 return -1;
786 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400787
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300788 dnd = dnd_create(d);
Bill Spitzak6fd10c62014-08-08 12:59:57 -0700789 if (self_only)
790 dnd->self_only = 1;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400791
792 display_run(d);
793
vivek31732f72014-05-15 18:58:16 +0530794 dnd_destroy(dnd);
795 display_destroy(d);
796
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400797 return 0;
798}