blob: a463d6f66b286f533f74547816c82ebe7f9744f5 [file] [log] [blame]
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -04001/*
2 * Copyright © 2010 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Philipp Brüschweilerf22d0ec2012-08-13 20:04:54 +020025#include <assert.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040026#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <math.h>
33#include <sys/time.h>
34#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040035#include <sys/epoll.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040036
Pekka Paalanen50719bc2011-11-22 14:18:50 +020037#include <wayland-client.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030038#include <wayland-cursor.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040039
40#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040041#include "../shared/cairo-util.h"
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040042
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030043struct dnd_drag;
44
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040045struct dnd {
46 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050047 struct widget *widget;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040048 struct display *display;
49 uint32_t key;
50 struct item *items[16];
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030051 int self_only;
52 struct dnd_drag *current_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040053};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040054
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040055struct dnd_drag {
56 cairo_surface_t *translucent;
57 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040058 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040059 struct dnd *dnd;
60 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040061 uint32_t time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080062 struct item *item;
63 int x_offset, y_offset;
Kristian Høgsberg679f7162012-03-27 16:44:57 -040064 int width, height;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080065 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040066
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020067 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040068 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040069};
70
71struct item {
72 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080073 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040074 int x, y;
75};
76
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080077struct dnd_flower_message {
78 int seed, x_offset, y_offset;
79};
80
81
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040082static const int item_width = 64;
83static const int item_height = 64;
84static const int item_padding = 16;
85
Kristian Høgsberg938f1022013-09-04 19:36:49 -070086static const char flower_mime_type[] = "application/x-wayland-dnd-flower";
Kristian Høgsberg735bda22013-09-09 15:03:27 -070087static const char text_mime_type[] = "text/plain;charset=utf-8";
Kristian Høgsberg938f1022013-09-04 19:36:49 -070088
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040089static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080090item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040091{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080092 struct item *item;
93 struct timeval tv;
94
95 item = malloc(sizeof *item);
96 if (item == NULL)
97 return NULL;
98
99
100 gettimeofday(&tv, NULL);
101 item->seed = seed ? seed : tv.tv_usec;
102 srandom(item->seed);
103
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400104 const int petal_count = 3 + random() % 5;
105 const double r1 = 20 + random() % 10;
106 const double r2 = 5 + random() % 12;
107 const double u = (10 + random() % 90) / 100.0;
108 const double v = (random() % 90) / 100.0;
109
110 cairo_t *cr;
111 int i;
112 double t, dt = 2 * M_PI / (petal_count * 2);
113 double x1, y1, x2, y2, x3, y3;
114 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400115
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400116
117 rect.width = item_width;
118 rect.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300119 item->surface =
120 display_create_surface(display, NULL, &rect, SURFACE_SHM);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400121
122 item->x = x;
123 item->y = y;
124
125 cr = cairo_create(item->surface);
126 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
127 cairo_set_source_rgba(cr, 0, 0, 0, 0);
128 cairo_paint(cr);
129
130 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
131 cairo_translate(cr, item_width / 2, item_height / 2);
132 t = random();
133 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
134 for (i = 0; i < petal_count; i++, t += dt * 2) {
135 x1 = cos(t) * r1;
136 y1 = sin(t) * r1;
137 x2 = cos(t + dt) * r2;
138 y2 = sin(t + dt) * r2;
139 x3 = cos(t + 2 * dt) * r1;
140 y3 = sin(t + 2 * dt) * r1;
141
142 cairo_curve_to(cr,
143 x1 - y1 * u, y1 + x1 * u,
144 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400145 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400146
147 cairo_curve_to(cr,
148 x2 - y2 * v, y2 + x2 * v,
149 x3 + y3 * u, y3 - x3 * u,
150 x3, y3);
151 }
152
153 cairo_close_path(cr);
154
155 cairo_set_source_rgba(cr,
156 0.5 + (random() % 50) / 49.0,
157 0.5 + (random() % 50) / 49.0,
158 0.5 + (random() % 50) / 49.0,
159 0.5 + (random() % 100) / 99.0);
160
161 cairo_fill_preserve(cr);
162
163 cairo_set_line_width(cr, 1);
164 cairo_set_source_rgba(cr,
165 0.5 + (random() % 50) / 49.0,
166 0.5 + (random() % 50) / 49.0,
167 0.5 + (random() % 50) / 49.0,
168 0.5 + (random() % 100) / 99.0);
169 cairo_stroke(cr);
170
171 cairo_destroy(cr);
172
173 return item;
174}
175
176static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500177dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400178{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500179 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500180 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400181 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500182 cairo_surface_t *surface;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400183 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400184
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500185 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400186 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500187 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500188 cairo_rectangle(cr, allocation.x, allocation.y,
189 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500190
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400191 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
192 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400193 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400194
Kristian Høgsberg0fd49aa2012-07-23 21:32:46 -0400195 cairo_rectangle(cr, allocation.x, allocation.y,
196 allocation.width, allocation.height);
197 cairo_clip(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400198 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
199 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
200 if (!dnd->items[i])
201 continue;
202 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400203 dnd->items[i]->x + allocation.x,
204 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400205 cairo_paint(cr);
206 }
207
208 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400209 cairo_surface_destroy(surface);
210}
211
212static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400213keyboard_focus_handler(struct window *window,
214 struct input *device, void *data)
215{
216 struct dnd *dnd = data;
217
218 window_schedule_redraw(dnd->window);
219}
220
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800221static int
222dnd_add_item(struct dnd *dnd, struct item *item)
223{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400224 unsigned int i;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800225
226 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
227 if (dnd->items[i] == 0) {
228 dnd->items[i] = item;
229 return i;
230 }
231 }
232 return -1;
233}
234
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400235static struct item *
236dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
237{
238 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500239 struct rectangle allocation;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400240 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400241
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500242 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400243
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500244 x -= allocation.x;
245 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400246
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400247 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
248 item = dnd->items[i];
249 if (item &&
250 item->x <= x && x < item->x + item_width &&
251 item->y <= y && y < item->y + item_height)
252 return item;
253 }
254
255 return NULL;
256}
257
258static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400259data_source_target(void *data,
260 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400261{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400262 struct dnd_drag *dnd_drag = data;
263 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400264 cairo_surface_t *surface;
265 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400266
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800267 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400268 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400269 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400270 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400271 surface = dnd_drag->translucent;
272
273 buffer = display_get_buffer_for_surface(dnd->display, surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200274 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400275 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
276 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300277 wl_surface_commit(dnd_drag->drag_surface);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400278}
279
280static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400281data_source_send(void *data, struct wl_data_source *source,
282 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400283{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800284 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400285 struct dnd_drag *dnd_drag = data;
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700286 char buffer[128];
287 int n;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800288
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700289 if (strcmp(mime_type, flower_mime_type) == 0) {
290 dnd_flower_message.seed = dnd_drag->item->seed;
291 dnd_flower_message.x_offset = dnd_drag->x_offset;
292 dnd_flower_message.y_offset = dnd_drag->y_offset;
293
294 if (write(fd, &dnd_flower_message,
295 sizeof dnd_flower_message) < 0)
296 abort();
297 } else if (strcmp(mime_type, text_mime_type) == 0) {
298 n = snprintf(buffer, sizeof buffer, "seed=%d x=%d y=%d\n",
299 dnd_drag->item->seed,
300 dnd_drag->x_offset,
301 dnd_drag->y_offset);
302
303 if (write(fd, buffer, n) < 0)
304 abort();
305 }
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200306
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400307 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400308}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400309
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400310static void
311data_source_cancelled(void *data, struct wl_data_source *source)
312{
313 struct dnd_drag *dnd_drag = data;
314
315 /* The 'cancelled' event means that the source is no longer in
316 * use by the drag (or current selection). We need to clean
317 * up the drag object created and the local state. */
318
319 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800320
321 /* Destroy the item that has been dragged out */
322 cairo_surface_destroy(dnd_drag->item->surface);
323 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200324
325 wl_surface_destroy(dnd_drag->drag_surface);
326
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400327 cairo_surface_destroy(dnd_drag->translucent);
328 cairo_surface_destroy(dnd_drag->opaque);
329 free(dnd_drag);
330}
331
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400332static const struct wl_data_source_listener data_source_listener = {
333 data_source_target,
334 data_source_send,
335 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400336};
337
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400338static cairo_surface_t *
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800339create_drag_icon(struct dnd_drag *dnd_drag,
340 struct item *item, int32_t x, int32_t y, double opacity)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400341{
342 struct dnd *dnd = dnd_drag->dnd;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300343 cairo_surface_t *surface;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400344 struct rectangle rectangle;
345 cairo_pattern_t *pattern;
346 cairo_t *cr;
347
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800348 rectangle.width = item_width;
349 rectangle.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300350 surface = display_create_surface(dnd->display, NULL, &rectangle,
351 SURFACE_SHM);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400352
353 cr = cairo_create(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400354 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400355 cairo_set_source_surface(cr, item->surface, 0, 0);
356 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
357 cairo_mask(cr, pattern);
358 cairo_pattern_destroy(pattern);
359
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400360 cairo_destroy(cr);
361
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800362 dnd_drag->hotspot_x = x - item->x;
363 dnd_drag->hotspot_y = y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400364 dnd_drag->width = rectangle.width;
365 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400366
367 return surface;
368}
369
Xiong Zhang853a7792013-11-25 18:42:50 +0800370static int
371create_drag_source(struct dnd *dnd,
372 struct input *input, uint32_t time,
373 int32_t x, int32_t y)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400374{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400375 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500376 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400377 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200378 struct display *display;
379 struct wl_compositor *compositor;
380 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400381 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400382 uint32_t serial;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300383 cairo_surface_t *icon;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400384
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500385 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400386 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500387 x -= allocation.x;
388 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400389
Xiong Zhang853a7792013-11-25 18:42:50 +0800390 if (item) {
Brian Lovinbc919262013-08-07 15:34:59 -0700391 dnd_drag = xmalloc(sizeof *dnd_drag);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400392 dnd_drag->dnd = dnd;
393 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400394 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800395 dnd_drag->item = item;
396 dnd_drag->x_offset = x - item->x;
397 dnd_drag->y_offset = y - item->y;
398
399 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
400 if (item == dnd->items[i]){
401 dnd->items[i] = 0;
402 break;
403 }
404 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400405
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200406 display = window_get_display(dnd->window);
407 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400408 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200409 dnd_drag->drag_surface =
410 wl_compositor_create_surface(compositor);
411
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300412 if (dnd->self_only) {
413 dnd_drag->data_source = NULL;
414 } else {
415 dnd_drag->data_source =
416 display_create_data_source(dnd->display);
417 wl_data_source_add_listener(dnd_drag->data_source,
418 &data_source_listener,
419 dnd_drag);
420 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700421 flower_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300422 wl_data_source_offer(dnd_drag->data_source,
Kristian Høgsberg735bda22013-09-09 15:03:27 -0700423 text_mime_type);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300424 }
425
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400426 wl_data_device_start_drag(input_get_data_device(input),
427 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500428 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200429 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400430 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400431
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400432 dnd_drag->opaque =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800433 create_drag_icon(dnd_drag, item, x, y, 1);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400434 dnd_drag->translucent =
Kristian Høgsberg617e9a32013-11-22 11:37:16 -0800435 create_drag_icon(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400436
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300437 if (dnd->self_only)
438 icon = dnd_drag->opaque;
439 else
440 icon = dnd_drag->translucent;
441
442 buffer = display_get_buffer_for_surface(dnd->display, icon);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200443 wl_surface_attach(dnd_drag->drag_surface, buffer,
444 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400445 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
446 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300447 wl_surface_commit(dnd_drag->drag_surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200448
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300449 dnd->current_drag = dnd_drag;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800450 window_schedule_redraw(dnd->window);
Xiong Zhang853a7792013-11-25 18:42:50 +0800451
452 return 0;
453 } else
454 return -1;
455}
456
457static void
458dnd_button_handler(struct widget *widget,
459 struct input *input, uint32_t time,
460 uint32_t button, enum wl_pointer_button_state state,
461 void *data)
462{
463 struct dnd *dnd = data;
464 int32_t x, y;
465
466 input_get_position(input, &x, &y);
467 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
468 input_ungrab(input);
469 if (create_drag_source(dnd, input, time, x, y) == 0)
470 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400471 }
472}
473
Xiong Zhang853a7792013-11-25 18:42:50 +0800474static void
475dnd_touch_down_handler(struct widget *widget,
476 struct input *input, uint32_t serial,
477 uint32_t time, int32_t id,
478 float x, float y, void *data)
479{
480 struct dnd *dnd = data;
481 int32_t int_x, int_y;
482
483 if (id > 0)
484 return;
485
486 int_x = (int32_t)x;
487 int_y = (int32_t)y;
Xiong Zhangbf3c1c62013-11-25 18:42:51 +0800488 if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
489 touch_grab(input, 0);
Xiong Zhang853a7792013-11-25 18:42:50 +0800490}
491
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400492static int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400493lookup_cursor(struct dnd *dnd, int x, int y)
494{
495 struct item *item;
496
497 item = dnd_get_item(dnd, x, y);
498 if (item)
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300499 return CURSOR_HAND1;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400500 else
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300501 return CURSOR_LEFT_PTR;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400502}
503
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500504static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500505dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400506 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400507{
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300508 struct dnd *dnd = data;
509
510 dnd->current_drag = NULL;
511
512 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400513}
514
515static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500516dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400517 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400518 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400519{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500520 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400521}
522
523static void
524dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400525 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400526 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400527{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400528 struct dnd *dnd = data;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700529 int i, has_flower = 0;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400530
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300531 if (!types)
532 return;
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700533 for (i = 0; types[i]; i++)
534 if (strcmp(types[i], flower_mime_type) == 0)
535 has_flower = 1;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300536
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700537 if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400538 input_accept(input, NULL);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300539 } else {
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700540 input_accept(input, flower_mime_type);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400541 }
542}
543
544static void
545dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
546{
547 struct dnd *dnd = user_data;
548 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400549 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400550 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400551
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400552 if (len == 0) {
553 return;
554 } else if (len != sizeof *message) {
Damien Lespiau4df7e272012-10-26 01:15:44 +0100555 fprintf(stderr, "odd message length %zu, expected %zu\n",
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400556 len, sizeof *message);
557 return;
558 }
559
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500560 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400561 item = item_create(dnd->display,
562 x - message->x_offset - allocation.x,
563 y - message->y_offset - allocation.y,
564 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400565
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400566 dnd_add_item(dnd, item);
567 window_schedule_redraw(dnd->window);
568}
569
570static void
571dnd_drop_handler(struct window *window, struct input *input,
572 int32_t x, int32_t y, void *data)
573{
574 struct dnd *dnd = data;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300575 struct dnd_flower_message message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400576
577 if (dnd_get_item(dnd, x, y)) {
578 fprintf(stderr, "got 'drop', but no target\n");
579 return;
580 }
581
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300582 if (!dnd->self_only) {
583 input_receive_drag_data(input,
Kristian Høgsberg938f1022013-09-04 19:36:49 -0700584 flower_mime_type,
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300585 dnd_receive_func, dnd);
586 } else if (dnd->current_drag) {
587 message.seed = dnd->current_drag->item->seed;
588 message.x_offset = dnd->current_drag->x_offset;
589 message.y_offset = dnd->current_drag->y_offset;
590 dnd_receive_func(&message, sizeof message, x, y, dnd);
591 dnd->current_drag = NULL;
592 } else {
593 fprintf(stderr, "ignoring drop from another client\n");
594 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400595}
596
597static struct dnd *
598dnd_create(struct display *display)
599{
600 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400601 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500602 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400603 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400604
Peter Huttererf3d62272013-08-08 11:57:05 +1000605 dnd = xzalloc(sizeof *dnd);
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500606 dnd->window = window_create(display);
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500607 dnd->widget = window_frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500608 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400609
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400610 dnd->display = display;
611 dnd->key = 100;
612
613 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
614 x = (i % 4) * (item_width + item_padding) + item_padding;
615 y = (i / 4) * (item_height + item_padding) + item_padding;
616 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800617 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400618 else
619 dnd->items[i] = NULL;
620 }
621
622 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400623 window_set_keyboard_focus_handler(dnd->window,
624 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400625 window_set_data_handler(dnd->window, dnd_data_handler);
626 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400627
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500628 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500629 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
630 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
631 widget_set_button_handler(dnd->widget, dnd_button_handler);
Xiong Zhang853a7792013-11-25 18:42:50 +0800632 widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500633
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500634 width = 4 * (item_width + item_padding) + item_padding;
635 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400636
Jason Ekstrandee7fefc2013-10-13 19:08:38 -0500637 window_frame_set_child_size(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400638
639 return dnd;
640}
641
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400642int
643main(int argc, char *argv[])
644{
645 struct display *d;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300646 struct dnd *dnd;
647 int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400648
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500649 d = display_create(&argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200650 if (d == NULL) {
651 fprintf(stderr, "failed to create display: %m\n");
652 return -1;
653 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400654
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300655 dnd = dnd_create(d);
656
657 for (i = 1; i < argc; i++)
658 if (strcmp("--self-only", argv[i]) == 0)
659 dnd->self_only = 1;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400660
661 display_run(d);
662
663 return 0;
664}