blob: e6a0c399f8d0fcd9a2bec59b283d7a54b5b729f3 [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
321data_source_cancelled(void *data, struct wl_data_source *source)
322{
323 struct dnd_drag *dnd_drag = data;
324
325 /* The 'cancelled' event means that the source is no longer in
326 * use by the drag (or current selection). We need to clean
327 * up the drag object created and the local state. */
328
329 wl_data_source_destroy(dnd_drag->data_source);
Michael Vetter2a18a522015-05-15 17:17:47 +0200330
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800331 /* Destroy the item that has been dragged out */
332 cairo_surface_destroy(dnd_drag->item->surface);
333 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200334
335 wl_surface_destroy(dnd_drag->drag_surface);
336
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400337 cairo_surface_destroy(dnd_drag->translucent);
338 cairo_surface_destroy(dnd_drag->opaque);
339 free(dnd_drag);
340}
341
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400342static const struct wl_data_source_listener data_source_listener = {
343 data_source_target,
344 data_source_send,
345 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400346};
347
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400348static cairo_surface_t *
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800349create_drag_icon(struct dnd_drag *dnd_drag,
350 struct item *item, int32_t x, int32_t y, double opacity)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400351{
352 struct dnd *dnd = dnd_drag->dnd;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300353 cairo_surface_t *surface;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400354 struct rectangle rectangle;
355 cairo_pattern_t *pattern;
356 cairo_t *cr;
357
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800358 rectangle.width = item_width;
359 rectangle.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300360 surface = display_create_surface(dnd->display, NULL, &rectangle,
361 SURFACE_SHM);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400362
363 cr = cairo_create(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400364 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400365 cairo_set_source_surface(cr, item->surface, 0, 0);
366 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
367 cairo_mask(cr, pattern);
368 cairo_pattern_destroy(pattern);
369
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400370 cairo_destroy(cr);
371
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800372 dnd_drag->hotspot_x = x - item->x;
373 dnd_drag->hotspot_y = y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400374 dnd_drag->width = rectangle.width;
375 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400376
377 return surface;
378}
379
Xiong Zhang853a7792013-11-25 18:42:50 +0800380static int
381create_drag_source(struct dnd *dnd,
382 struct input *input, uint32_t time,
383 int32_t x, int32_t y)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400384{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400385 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500386 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400387 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200388 struct display *display;
389 struct wl_compositor *compositor;
390 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400391 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400392 uint32_t serial;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300393 cairo_surface_t *icon;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400394
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500395 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400396 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500397 x -= allocation.x;
398 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400399
Xiong Zhang853a7792013-11-25 18:42:50 +0800400 if (item) {
Brian Lovinbc919262013-08-07 15:34:59 -0700401 dnd_drag = xmalloc(sizeof *dnd_drag);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400402 dnd_drag->dnd = dnd;
403 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400404 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800405 dnd_drag->item = item;
406 dnd_drag->x_offset = x - item->x;
407 dnd_drag->y_offset = y - item->y;
408
409 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
410 if (item == dnd->items[i]){
411 dnd->items[i] = 0;
412 break;
413 }
414 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400415
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200416 display = window_get_display(dnd->window);
417 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400418 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200419 dnd_drag->drag_surface =
420 wl_compositor_create_surface(compositor);
421
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300422 if (dnd->self_only) {
423 dnd_drag->data_source = NULL;
424 } else {
425 dnd_drag->data_source =
426 display_create_data_source(dnd->display);
427 wl_data_source_add_listener(dnd_drag->data_source,
428 &data_source_listener,
429 dnd_drag);
430 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700431 flower_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300432 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700433 text_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300434 }
435
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400436 wl_data_device_start_drag(input_get_data_device(input),
437 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500438 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200439 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400440 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400441
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400442 dnd_drag->opaque =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800443 create_drag_icon(dnd_drag, item, x, y, 1);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400444 dnd_drag->translucent =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800445 create_drag_icon(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400446
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300447 if (dnd->self_only)
448 icon = dnd_drag->opaque;
449 else
450 icon = dnd_drag->translucent;
451
452 buffer = display_get_buffer_for_surface(dnd->display, icon);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200453 wl_surface_attach(dnd_drag->drag_surface, buffer,
454 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400455 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
456 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300457 wl_surface_commit(dnd_drag->drag_surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200458
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300459 dnd->current_drag = dnd_drag;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800460 window_schedule_redraw(dnd->window);
Xiong Zhang853a7792013-11-25 18:42:50 +0800461
462 return 0;
463 } else
464 return -1;
465}
466
Derek Foremanba0f33d2014-11-20 15:32:40 -0600467static int
468lookup_cursor(struct dnd *dnd, int x, int y)
469{
470 struct item *item;
471
472 item = dnd_get_item(dnd, x, y);
473 if (item)
474 return CURSOR_HAND1;
475 else
476 return CURSOR_LEFT_PTR;
477}
478
479/* Update all the mouse pointers in the window appropriately.
480 * Optionally, skip one (which will be the current pointer just
481 * about to start a drag). This is done here to save a scan
482 * through the pointer list.
483 */
484static void
485update_pointer_images_except(struct dnd *dnd, struct input *except)
486{
487 struct pointer *pointer;
488 int32_t x, y;
489
490 wl_list_for_each(pointer, &dnd->pointers, link) {
491 if (pointer->input == except) {
492 pointer->dragging = true;
493 continue;
494 }
495 input_get_position(pointer->input, &x, &y);
496 input_set_pointer_image(pointer->input,
497 lookup_cursor(dnd, x, y));
498 }
499}
500
Xiong Zhang853a7792013-11-25 18:42:50 +0800501static void
502dnd_button_handler(struct widget *widget,
503 struct input *input, uint32_t time,
504 uint32_t button, enum wl_pointer_button_state state,
505 void *data)
506{
507 struct dnd *dnd = data;
508 int32_t x, y;
509
510 input_get_position(input, &x, &y);
511 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
512 input_ungrab(input);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600513 if (create_drag_source(dnd, input, time, x, y) == 0) {
Xiong Zhang853a7792013-11-25 18:42:50 +0800514 input_set_pointer_image(input, CURSOR_DRAGGING);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600515 update_pointer_images_except(dnd, input);
516 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400517 }
518}
519
Xiong Zhang853a7792013-11-25 18:42:50 +0800520static void
521dnd_touch_down_handler(struct widget *widget,
522 struct input *input, uint32_t serial,
523 uint32_t time, int32_t id,
524 float x, float y, void *data)
525{
526 struct dnd *dnd = data;
527 int32_t int_x, int_y;
528
529 if (id > 0)
530 return;
531
532 int_x = (int32_t)x;
533 int_y = (int32_t)y;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800534 if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
535 touch_grab(input, 0);
Xiong Zhang853a7792013-11-25 18:42:50 +0800536}
537
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400538static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500539dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400540 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400541{
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300542 struct dnd *dnd = data;
Derek Foremanba0f33d2014-11-20 15:32:40 -0600543 struct pointer *new_pointer = malloc(sizeof *new_pointer);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300544
545 dnd->current_drag = NULL;
546
Derek Foremanba0f33d2014-11-20 15:32:40 -0600547 if (new_pointer) {
548 new_pointer->input = input;
549 new_pointer->dragging = false;
550 wl_list_insert(dnd->pointers.prev, &new_pointer->link);
551 }
552
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300553 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400554}
555
Derek Foremanba0f33d2014-11-20 15:32:40 -0600556static void
557dnd_leave_handler(struct widget *widget,
558 struct input *input, void *data)
559{
560 struct dnd *dnd = data;
561 struct pointer *pointer, *tmp;
562
563 wl_list_for_each_safe(pointer, tmp, &dnd->pointers, link)
564 if (pointer->input == input) {
565 wl_list_remove(&pointer->link);
566 free(pointer);
567 }
568}
569
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400570static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500571dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400572 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400573 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400574{
Derek Foremanba0f33d2014-11-20 15:32:40 -0600575 struct dnd *dnd = data;
576 struct pointer *pointer;
577
578 wl_list_for_each(pointer, &dnd->pointers, link)
579 if (pointer->input == input) {
580 if (pointer->dragging)
581 return CURSOR_DRAGGING;
582 break;
583 }
584
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500585 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400586}
587
588static void
589dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400590 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400591 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400592{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400593 struct dnd *dnd = data;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700594 int i, has_flower = 0;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400595
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300596 if (!types)
597 return;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700598 for (i = 0; types[i]; i++)
599 if (strcmp(types[i], flower_mime_type) == 0)
600 has_flower = 1;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300601
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700602 if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400603 input_accept(input, NULL);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300604 } else {
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700605 input_accept(input, flower_mime_type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400606 }
607}
608
609static void
610dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
611{
612 struct dnd *dnd = user_data;
613 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400614 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400615 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400616
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400617 if (len == 0) {
618 return;
619 } else if (len != sizeof *message) {
Damien Lespiau4df7e272012-10-26 01:15:44 +0100620 fprintf(stderr, "odd message length %zu, expected %zu\n",
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400621 len, sizeof *message);
622 return;
623 }
Michael Vetter2a18a522015-05-15 17:17:47 +0200624
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500625 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400626 item = item_create(dnd->display,
627 x - message->x_offset - allocation.x,
628 y - message->y_offset - allocation.y,
629 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400630
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400631 dnd_add_item(dnd, item);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600632 update_pointer_images_except(dnd, NULL);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400633 window_schedule_redraw(dnd->window);
634}
635
636static void
637dnd_drop_handler(struct window *window, struct input *input,
638 int32_t x, int32_t y, void *data)
639{
640 struct dnd *dnd = data;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300641 struct dnd_flower_message message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400642
643 if (dnd_get_item(dnd, x, y)) {
644 fprintf(stderr, "got 'drop', but no target\n");
645 return;
646 }
647
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300648 if (!dnd->self_only) {
649 input_receive_drag_data(input,
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700650 flower_mime_type,
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300651 dnd_receive_func, dnd);
652 } else if (dnd->current_drag) {
653 message.seed = dnd->current_drag->item->seed;
654 message.x_offset = dnd->current_drag->x_offset;
655 message.y_offset = dnd->current_drag->y_offset;
656 dnd_receive_func(&message, sizeof message, x, y, dnd);
657 dnd->current_drag = NULL;
658 } else {
659 fprintf(stderr, "ignoring drop from another client\n");
660 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400661}
662
663static struct dnd *
664dnd_create(struct display *display)
665{
666 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400667 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500668 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400669 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400670
Peter Huttererf3d62272013-08-08 11:57:05 +1000671 dnd = xzalloc(sizeof *dnd);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500672 dnd->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500673 dnd->widget = window_frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500674 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400675
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400676 dnd->display = display;
677 dnd->key = 100;
678
Derek Foremanba0f33d2014-11-20 15:32:40 -0600679 wl_list_init(&dnd->pointers);
680
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400681 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
682 x = (i % 4) * (item_width + item_padding) + item_padding;
683 y = (i / 4) * (item_height + item_padding) + item_padding;
684 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800685 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400686 else
687 dnd->items[i] = NULL;
688 }
689
690 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400691 window_set_keyboard_focus_handler(dnd->window,
692 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400693 window_set_data_handler(dnd->window, dnd_data_handler);
694 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400695
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500696 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500697 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
Derek Foremanba0f33d2014-11-20 15:32:40 -0600698 widget_set_leave_handler(dnd->widget, dnd_leave_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500699 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
700 widget_set_button_handler(dnd->widget, dnd_button_handler);
Xiong Zhang853a7792013-11-25 18:42:50 +0800701 widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500702
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500703 width = 4 * (item_width + item_padding) + item_padding;
704 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400705
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500706 window_frame_set_child_size(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400707
708 return dnd;
709}
710
vivek31732f72014-05-15 18:58:16 +0530711static void
712dnd_destroy(struct dnd *dnd)
713{
714 widget_destroy(dnd->widget);
715 window_destroy(dnd->window);
716 free(dnd);
717}
718
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400719int
720main(int argc, char *argv[])
721{
722 struct display *d;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300723 struct dnd *dnd;
Bill Spitzak6fd10c62014-08-08 12:59:57 -0700724 int self_only = 0;
725
726 if (argc == 2 && !strcmp(argv[1], "--self-only"))
727 self_only = 1;
728 else if (argc > 1) {
729 printf("Usage: %s [OPTIONS]\n --self-only\n", argv[0]);
730 return 1;
731 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400732
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500733 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200734 if (d == NULL) {
735 fprintf(stderr, "failed to create display: %m\n");
736 return -1;
737 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400738
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300739 dnd = dnd_create(d);
Bill Spitzak6fd10c62014-08-08 12:59:57 -0700740 if (self_only)
741 dnd->self_only = 1;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400742
743 display_run(d);
744
vivek31732f72014-05-15 18:58:16 +0530745 dnd_destroy(dnd);
746 display_destroy(d);
747
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400748 return 0;
749}