blob: 974429b0c2000f7a02daaae5fa740385d8bacef9 [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;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080075 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040076
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020077 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040078 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040079};
80
81struct item {
82 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080083 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040084 int x, y;
85};
86
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080087struct dnd_flower_message {
88 int seed, x_offset, y_offset;
89};
90
91
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040092static const int item_width = 64;
93static const int item_height = 64;
94static const int item_padding = 16;
95
Kristian Høgsberg938f1022013-09-04 19:36:49 -070096static const char flower_mime_type[] = "application/x-wayland-dnd-flower";
Kristian Høgsberg735bda22013-09-09 15:03:27 -070097static const char text_mime_type[] = "text/plain;charset=utf-8";
Kristian Høgsberg938f1022013-09-04 19:36:49 -070098
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040099static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800100item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400101{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800102 struct item *item;
103 struct timeval tv;
104
105 item = malloc(sizeof *item);
106 if (item == NULL)
107 return NULL;
Michael Vetter2a18a522015-05-15 17:17:47 +0200108
109
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800110 gettimeofday(&tv, NULL);
111 item->seed = seed ? seed : tv.tv_usec;
112 srandom(item->seed);
Michael Vetter2a18a522015-05-15 17:17:47 +0200113
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400114 const int petal_count = 3 + random() % 5;
115 const double r1 = 20 + random() % 10;
116 const double r2 = 5 + random() % 12;
117 const double u = (10 + random() % 90) / 100.0;
118 const double v = (random() % 90) / 100.0;
119
120 cairo_t *cr;
121 int i;
122 double t, dt = 2 * M_PI / (petal_count * 2);
123 double x1, y1, x2, y2, x3, y3;
124 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400125
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400126
127 rect.width = item_width;
128 rect.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300129 item->surface =
130 display_create_surface(display, NULL, &rect, SURFACE_SHM);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400131
132 item->x = x;
133 item->y = y;
134
135 cr = cairo_create(item->surface);
136 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
137 cairo_set_source_rgba(cr, 0, 0, 0, 0);
138 cairo_paint(cr);
139
140 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
141 cairo_translate(cr, item_width / 2, item_height / 2);
142 t = random();
143 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
144 for (i = 0; i < petal_count; i++, t += dt * 2) {
145 x1 = cos(t) * r1;
146 y1 = sin(t) * r1;
147 x2 = cos(t + dt) * r2;
148 y2 = sin(t + dt) * r2;
149 x3 = cos(t + 2 * dt) * r1;
150 y3 = sin(t + 2 * dt) * r1;
151
152 cairo_curve_to(cr,
153 x1 - y1 * u, y1 + x1 * u,
154 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400155 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400156
157 cairo_curve_to(cr,
158 x2 - y2 * v, y2 + x2 * v,
159 x3 + y3 * u, y3 - x3 * u,
160 x3, y3);
161 }
162
163 cairo_close_path(cr);
164
165 cairo_set_source_rgba(cr,
166 0.5 + (random() % 50) / 49.0,
167 0.5 + (random() % 50) / 49.0,
168 0.5 + (random() % 50) / 49.0,
169 0.5 + (random() % 100) / 99.0);
170
171 cairo_fill_preserve(cr);
172
173 cairo_set_line_width(cr, 1);
174 cairo_set_source_rgba(cr,
175 0.5 + (random() % 50) / 49.0,
176 0.5 + (random() % 50) / 49.0,
177 0.5 + (random() % 50) / 49.0,
178 0.5 + (random() % 100) / 99.0);
179 cairo_stroke(cr);
180
181 cairo_destroy(cr);
182
183 return item;
184}
185
186static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500187dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400188{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500189 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500190 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400191 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500192 cairo_surface_t *surface;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400193 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400194
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500195 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400196 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500197 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500198 cairo_rectangle(cr, allocation.x, allocation.y,
199 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500200
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400201 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
202 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400203 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400204
Kristian Høgsberg0fd49aa2012-07-23 21:32:46 -0400205 cairo_rectangle(cr, allocation.x, allocation.y,
206 allocation.width, allocation.height);
207 cairo_clip(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400208 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
209 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
210 if (!dnd->items[i])
211 continue;
212 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400213 dnd->items[i]->x + allocation.x,
214 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400215 cairo_paint(cr);
216 }
217
218 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400219 cairo_surface_destroy(surface);
220}
221
222static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400223keyboard_focus_handler(struct window *window,
224 struct input *device, void *data)
225{
226 struct dnd *dnd = data;
227
228 window_schedule_redraw(dnd->window);
229}
230
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800231static int
232dnd_add_item(struct dnd *dnd, struct item *item)
233{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400234 unsigned int i;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800235
236 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
237 if (dnd->items[i] == 0) {
238 dnd->items[i] = item;
239 return i;
240 }
241 }
242 return -1;
243}
244
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400245static struct item *
246dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
247{
248 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500249 struct rectangle allocation;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400250 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400251
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500252 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400253
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500254 x -= allocation.x;
255 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400256
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400257 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
258 item = dnd->items[i];
259 if (item &&
260 item->x <= x && x < item->x + item_width &&
261 item->y <= y && y < item->y + item_height)
262 return item;
263 }
264
265 return NULL;
266}
267
268static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400269data_source_target(void *data,
270 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400271{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400272 struct dnd_drag *dnd_drag = data;
273 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400274 cairo_surface_t *surface;
275 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400276
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800277 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400278 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400279 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400280 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400281 surface = dnd_drag->translucent;
282
283 buffer = display_get_buffer_for_surface(dnd->display, surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200284 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400285 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
286 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300287 wl_surface_commit(dnd_drag->drag_surface);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400288}
289
290static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400291data_source_send(void *data, struct wl_data_source *source,
292 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400293{
Michael Vetter2a18a522015-05-15 17:17:47 +0200294 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400295 struct dnd_drag *dnd_drag = data;
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700296 char buffer[128];
297 int n;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800298
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700299 if (strcmp(mime_type, flower_mime_type) == 0) {
300 dnd_flower_message.seed = dnd_drag->item->seed;
301 dnd_flower_message.x_offset = dnd_drag->x_offset;
302 dnd_flower_message.y_offset = dnd_drag->y_offset;
303
304 if (write(fd, &dnd_flower_message,
305 sizeof dnd_flower_message) < 0)
306 abort();
307 } else if (strcmp(mime_type, text_mime_type) == 0) {
308 n = snprintf(buffer, sizeof buffer, "seed=%d x=%d y=%d\n",
309 dnd_drag->item->seed,
310 dnd_drag->x_offset,
311 dnd_drag->y_offset);
312
313 if (write(fd, buffer, n) < 0)
314 abort();
315 }
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200316
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400317 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400318}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400319
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400320static void
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100321dnd_drag_destroy(struct dnd_drag *dnd_drag)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400322{
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400323 wl_data_source_destroy(dnd_drag->data_source);
Michael Vetter2a18a522015-05-15 17:17:47 +0200324
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800325 /* Destroy the item that has been dragged out */
326 cairo_surface_destroy(dnd_drag->item->surface);
327 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200328
329 wl_surface_destroy(dnd_drag->drag_surface);
330
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400331 cairo_surface_destroy(dnd_drag->translucent);
332 cairo_surface_destroy(dnd_drag->opaque);
333 free(dnd_drag);
334}
335
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100336static void
337data_source_cancelled(void *data, struct wl_data_source *source)
338{
339 struct dnd_drag *dnd_drag = data;
340
341 /* The 'cancelled' event means that the source is no longer in
342 * use by the drag (or current selection). We need to clean
343 * up the drag object created and the local state. */
344 dnd_drag_destroy(dnd_drag);
345}
346
347static void
348data_source_dnd_drop_performed(void *data, struct wl_data_source *source)
349{
350}
351
352static void
353data_source_dnd_finished(void *data, struct wl_data_source *source)
354{
355 struct dnd_drag *dnd_drag = data;
356
357 /* The operation is already finished, we can destroy all
358 * related data.
359 */
360 dnd_drag_destroy(dnd_drag);
361}
362
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400363static const struct wl_data_source_listener data_source_listener = {
364 data_source_target,
365 data_source_send,
Carlos Garnacho78d4bf92016-01-15 21:14:23 +0100366 data_source_cancelled,
367 data_source_dnd_drop_performed,
368 data_source_dnd_finished,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400369};
370
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400371static cairo_surface_t *
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800372create_drag_icon(struct dnd_drag *dnd_drag,
373 struct item *item, int32_t x, int32_t y, double opacity)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400374{
375 struct dnd *dnd = dnd_drag->dnd;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300376 cairo_surface_t *surface;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400377 struct rectangle rectangle;
378 cairo_pattern_t *pattern;
379 cairo_t *cr;
380
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800381 rectangle.width = item_width;
382 rectangle.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300383 surface = display_create_surface(dnd->display, NULL, &rectangle,
384 SURFACE_SHM);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400385
386 cr = cairo_create(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400387 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400388 cairo_set_source_surface(cr, item->surface, 0, 0);
389 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
390 cairo_mask(cr, pattern);
391 cairo_pattern_destroy(pattern);
392
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400393 cairo_destroy(cr);
394
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800395 dnd_drag->hotspot_x = x - item->x;
396 dnd_drag->hotspot_y = y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400397 dnd_drag->width = rectangle.width;
398 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400399
400 return surface;
401}
402
Xiong Zhang853a7792013-11-25 18:42:50 +0800403static int
404create_drag_source(struct dnd *dnd,
405 struct input *input, uint32_t time,
406 int32_t x, int32_t y)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400407{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400408 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500409 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400410 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200411 struct display *display;
412 struct wl_compositor *compositor;
413 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400414 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400415 uint32_t serial;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300416 cairo_surface_t *icon;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400417
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500418 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400419 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500420 x -= allocation.x;
421 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400422
Xiong Zhang853a7792013-11-25 18:42:50 +0800423 if (item) {
Brian Lovinbc919262013-08-07 15:34:59 -0700424 dnd_drag = xmalloc(sizeof *dnd_drag);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400425 dnd_drag->dnd = dnd;
426 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400427 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800428 dnd_drag->item = item;
429 dnd_drag->x_offset = x - item->x;
430 dnd_drag->y_offset = y - item->y;
431
432 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
433 if (item == dnd->items[i]){
434 dnd->items[i] = 0;
435 break;
436 }
437 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400438
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200439 display = window_get_display(dnd->window);
440 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400441 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200442 dnd_drag->drag_surface =
443 wl_compositor_create_surface(compositor);
444
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300445 if (dnd->self_only) {
446 dnd_drag->data_source = NULL;
447 } else {
448 dnd_drag->data_source =
449 display_create_data_source(dnd->display);
450 wl_data_source_add_listener(dnd_drag->data_source,
451 &data_source_listener,
452 dnd_drag);
453 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700454 flower_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300455 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700456 text_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300457 }
458
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400459 wl_data_device_start_drag(input_get_data_device(input),
460 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500461 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200462 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400463 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400464
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400465 dnd_drag->opaque =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800466 create_drag_icon(dnd_drag, item, x, y, 1);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400467 dnd_drag->translucent =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800468 create_drag_icon(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400469
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300470 if (dnd->self_only)
471 icon = dnd_drag->opaque;
472 else
473 icon = dnd_drag->translucent;
474
475 buffer = display_get_buffer_for_surface(dnd->display, icon);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200476 wl_surface_attach(dnd_drag->drag_surface, buffer,
477 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400478 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
479 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300480 wl_surface_commit(dnd_drag->drag_surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200481
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300482 dnd->current_drag = dnd_drag;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800483 window_schedule_redraw(dnd->window);
Xiong Zhang853a7792013-11-25 18:42:50 +0800484
485 return 0;
486 } else
487 return -1;
488}
489
Derek Foremanba0f33d2014-11-20 15:32:40 -0600490static int
491lookup_cursor(struct dnd *dnd, int x, int y)
492{
493 struct item *item;
494
495 item = dnd_get_item(dnd, x, y);
496 if (item)
497 return CURSOR_HAND1;
498 else
499 return CURSOR_LEFT_PTR;
500}
501
502/* Update all the mouse pointers in the window appropriately.
503 * Optionally, skip one (which will be the current pointer just
504 * about to start a drag). This is done here to save a scan
505 * through the pointer list.
506 */
507static void
508update_pointer_images_except(struct dnd *dnd, struct input *except)
509{
510 struct pointer *pointer;
511 int32_t x, y;
512
513 wl_list_for_each(pointer, &dnd->pointers, link) {
514 if (pointer->input == except) {
515 pointer->dragging = true;
516 continue;
517 }
518 input_get_position(pointer->input, &x, &y);
519 input_set_pointer_image(pointer->input,
520 lookup_cursor(dnd, x, y));
521 }
522}
523
Xiong Zhang853a7792013-11-25 18:42:50 +0800524static void
525dnd_button_handler(struct widget *widget,
526 struct input *input, uint32_t time,
527 uint32_t button, enum wl_pointer_button_state state,
528 void *data)
529{
530 struct dnd *dnd = data;
531 int32_t x, y;
532
533 input_get_position(input, &x, &y);
534 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
535 input_ungrab(input);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600536 if (create_drag_source(dnd, input, time, x, y) == 0) {
Xiong Zhang853a7792013-11-25 18:42:50 +0800537 input_set_pointer_image(input, CURSOR_DRAGGING);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600538 update_pointer_images_except(dnd, input);
539 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400540 }
541}
542
Xiong Zhang853a7792013-11-25 18:42:50 +0800543static void
544dnd_touch_down_handler(struct widget *widget,
545 struct input *input, uint32_t serial,
546 uint32_t time, int32_t id,
547 float x, float y, void *data)
548{
549 struct dnd *dnd = data;
550 int32_t int_x, int_y;
551
552 if (id > 0)
553 return;
554
555 int_x = (int32_t)x;
556 int_y = (int32_t)y;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800557 if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
558 touch_grab(input, 0);
Xiong Zhang853a7792013-11-25 18:42:50 +0800559}
560
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400561static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500562dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400563 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400564{
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300565 struct dnd *dnd = data;
Derek Foremanba0f33d2014-11-20 15:32:40 -0600566 struct pointer *new_pointer = malloc(sizeof *new_pointer);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300567
568 dnd->current_drag = NULL;
569
Derek Foremanba0f33d2014-11-20 15:32:40 -0600570 if (new_pointer) {
571 new_pointer->input = input;
572 new_pointer->dragging = false;
573 wl_list_insert(dnd->pointers.prev, &new_pointer->link);
574 }
575
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300576 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400577}
578
Derek Foremanba0f33d2014-11-20 15:32:40 -0600579static void
580dnd_leave_handler(struct widget *widget,
581 struct input *input, void *data)
582{
583 struct dnd *dnd = data;
584 struct pointer *pointer, *tmp;
585
586 wl_list_for_each_safe(pointer, tmp, &dnd->pointers, link)
587 if (pointer->input == input) {
588 wl_list_remove(&pointer->link);
589 free(pointer);
590 }
591}
592
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400593static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500594dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400595 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400596 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400597{
Derek Foremanba0f33d2014-11-20 15:32:40 -0600598 struct dnd *dnd = data;
599 struct pointer *pointer;
600
601 wl_list_for_each(pointer, &dnd->pointers, link)
602 if (pointer->input == input) {
603 if (pointer->dragging)
604 return CURSOR_DRAGGING;
605 break;
606 }
607
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500608 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400609}
610
611static void
612dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400613 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400614 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400615{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400616 struct dnd *dnd = data;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700617 int i, has_flower = 0;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400618
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300619 if (!types)
620 return;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700621 for (i = 0; types[i]; i++)
622 if (strcmp(types[i], flower_mime_type) == 0)
623 has_flower = 1;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300624
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700625 if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400626 input_accept(input, NULL);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300627 } else {
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700628 input_accept(input, flower_mime_type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400629 }
630}
631
632static void
633dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
634{
635 struct dnd *dnd = user_data;
636 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400637 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400638 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400639
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400640 if (len == 0) {
641 return;
642 } else if (len != sizeof *message) {
Damien Lespiau4df7e272012-10-26 01:15:44 +0100643 fprintf(stderr, "odd message length %zu, expected %zu\n",
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400644 len, sizeof *message);
645 return;
646 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200647
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500648 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400649 item = item_create(dnd->display,
650 x - message->x_offset - allocation.x,
651 y - message->y_offset - allocation.y,
652 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400653
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400654 dnd_add_item(dnd, item);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600655 update_pointer_images_except(dnd, NULL);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400656 window_schedule_redraw(dnd->window);
657}
658
659static void
660dnd_drop_handler(struct window *window, struct input *input,
661 int32_t x, int32_t y, void *data)
662{
663 struct dnd *dnd = data;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300664 struct dnd_flower_message message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400665
666 if (dnd_get_item(dnd, x, y)) {
667 fprintf(stderr, "got 'drop', but no target\n");
668 return;
669 }
670
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300671 if (!dnd->self_only) {
672 input_receive_drag_data(input,
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700673 flower_mime_type,
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300674 dnd_receive_func, dnd);
675 } else if (dnd->current_drag) {
676 message.seed = dnd->current_drag->item->seed;
677 message.x_offset = dnd->current_drag->x_offset;
678 message.y_offset = dnd->current_drag->y_offset;
679 dnd_receive_func(&message, sizeof message, x, y, dnd);
680 dnd->current_drag = NULL;
681 } else {
682 fprintf(stderr, "ignoring drop from another client\n");
683 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400684}
685
686static struct dnd *
687dnd_create(struct display *display)
688{
689 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400690 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500691 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400692 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400693
Peter Huttererf3d62272013-08-08 11:57:05 +1000694 dnd = xzalloc(sizeof *dnd);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500695 dnd->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500696 dnd->widget = window_frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500697 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400698
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400699 dnd->display = display;
700 dnd->key = 100;
701
Derek Foremanba0f33d2014-11-20 15:32:40 -0600702 wl_list_init(&dnd->pointers);
703
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400704 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
705 x = (i % 4) * (item_width + item_padding) + item_padding;
706 y = (i / 4) * (item_height + item_padding) + item_padding;
707 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800708 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400709 else
710 dnd->items[i] = NULL;
711 }
712
713 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400714 window_set_keyboard_focus_handler(dnd->window,
715 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400716 window_set_data_handler(dnd->window, dnd_data_handler);
717 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400718
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500719 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500720 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600721 widget_set_leave_handler(dnd->widget, dnd_leave_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500722 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
723 widget_set_button_handler(dnd->widget, dnd_button_handler);
Xiong Zhang853a7792013-11-25 18:42:50 +0800724 widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500725
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500726 width = 4 * (item_width + item_padding) + item_padding;
727 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400728
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500729 window_frame_set_child_size(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400730
731 return dnd;
732}
733
vivek31732f72014-05-15 18:58:16 +0530734static void
735dnd_destroy(struct dnd *dnd)
736{
737 widget_destroy(dnd->widget);
738 window_destroy(dnd->window);
739 free(dnd);
740}
741
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400742int
743main(int argc, char *argv[])
744{
745 struct display *d;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300746 struct dnd *dnd;
Bill Spitzak6fd10c62014-08-08 12:59:57 -0700747 int self_only = 0;
748
749 if (argc == 2 && !strcmp(argv[1], "--self-only"))
750 self_only = 1;
751 else if (argc > 1) {
752 printf("Usage: %s [OPTIONS]\n --self-only\n", argv[0]);
753 return 1;
754 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400755
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500756 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200757 if (d == NULL) {
758 fprintf(stderr, "failed to create display: %m\n");
759 return -1;
760 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400761
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300762 dnd = dnd_create(d);
Bill Spitzak6fd10c62014-08-08 12:59:57 -0700763 if (self_only)
764 dnd->self_only = 1;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400765
766 display_run(d);
767
vivek31732f72014-05-15 18:58:16 +0530768 dnd_destroy(dnd);
769 display_destroy(d);
770
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400771 return 0;
772}