blob: 4aad070ca35f11d228da0eda78e4a5ba8719cb00 [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
Philipp Brüschweilerf22d0ec2012-08-13 20:04:54 +020023#include <assert.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040024#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <sys/time.h>
32#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040033#include <sys/epoll.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040034
Pekka Paalanen50719bc2011-11-22 14:18:50 +020035#include <wayland-client.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030036#include <wayland-cursor.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040037
38#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040039#include "../shared/cairo-util.h"
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040040
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030041struct dnd_drag;
42
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040043struct dnd {
44 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050045 struct widget *widget;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040046 struct display *display;
47 uint32_t key;
48 struct item *items[16];
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030049 int self_only;
50 struct dnd_drag *current_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040051};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040052
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040053struct dnd_drag {
54 cairo_surface_t *translucent;
55 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040056 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040057 struct dnd *dnd;
58 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040059 uint32_t time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080060 struct item *item;
61 int x_offset, y_offset;
Kristian Høgsberg679f7162012-03-27 16:44:57 -040062 int width, height;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080063 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040064
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020065 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040066 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040067};
68
69struct item {
70 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080071 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040072 int x, y;
73};
74
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080075struct dnd_flower_message {
76 int seed, x_offset, y_offset;
77};
78
79
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040080static const int item_width = 64;
81static const int item_height = 64;
82static const int item_padding = 16;
83
84static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080085item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040086{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080087 struct item *item;
88 struct timeval tv;
89
90 item = malloc(sizeof *item);
91 if (item == NULL)
92 return NULL;
93
94
95 gettimeofday(&tv, NULL);
96 item->seed = seed ? seed : tv.tv_usec;
97 srandom(item->seed);
98
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040099 const int petal_count = 3 + random() % 5;
100 const double r1 = 20 + random() % 10;
101 const double r2 = 5 + random() % 12;
102 const double u = (10 + random() % 90) / 100.0;
103 const double v = (random() % 90) / 100.0;
104
105 cairo_t *cr;
106 int i;
107 double t, dt = 2 * M_PI / (petal_count * 2);
108 double x1, y1, x2, y2, x3, y3;
109 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400110
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400111
112 rect.width = item_width;
113 rect.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300114 item->surface =
115 display_create_surface(display, NULL, &rect, SURFACE_SHM);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400116
117 item->x = x;
118 item->y = y;
119
120 cr = cairo_create(item->surface);
121 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
122 cairo_set_source_rgba(cr, 0, 0, 0, 0);
123 cairo_paint(cr);
124
125 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
126 cairo_translate(cr, item_width / 2, item_height / 2);
127 t = random();
128 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
129 for (i = 0; i < petal_count; i++, t += dt * 2) {
130 x1 = cos(t) * r1;
131 y1 = sin(t) * r1;
132 x2 = cos(t + dt) * r2;
133 y2 = sin(t + dt) * r2;
134 x3 = cos(t + 2 * dt) * r1;
135 y3 = sin(t + 2 * dt) * r1;
136
137 cairo_curve_to(cr,
138 x1 - y1 * u, y1 + x1 * u,
139 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400140 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400141
142 cairo_curve_to(cr,
143 x2 - y2 * v, y2 + x2 * v,
144 x3 + y3 * u, y3 - x3 * u,
145 x3, y3);
146 }
147
148 cairo_close_path(cr);
149
150 cairo_set_source_rgba(cr,
151 0.5 + (random() % 50) / 49.0,
152 0.5 + (random() % 50) / 49.0,
153 0.5 + (random() % 50) / 49.0,
154 0.5 + (random() % 100) / 99.0);
155
156 cairo_fill_preserve(cr);
157
158 cairo_set_line_width(cr, 1);
159 cairo_set_source_rgba(cr,
160 0.5 + (random() % 50) / 49.0,
161 0.5 + (random() % 50) / 49.0,
162 0.5 + (random() % 50) / 49.0,
163 0.5 + (random() % 100) / 99.0);
164 cairo_stroke(cr);
165
166 cairo_destroy(cr);
167
168 return item;
169}
170
171static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500172dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400173{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500174 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500175 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400176 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500177 cairo_surface_t *surface;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400178 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400179
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500180 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400181 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500182 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500183 cairo_rectangle(cr, allocation.x, allocation.y,
184 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500185
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400186 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
187 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400188 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400189
Kristian Høgsberg0fd49aa2012-07-23 21:32:46 -0400190 cairo_rectangle(cr, allocation.x, allocation.y,
191 allocation.width, allocation.height);
192 cairo_clip(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400193 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
194 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
195 if (!dnd->items[i])
196 continue;
197 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400198 dnd->items[i]->x + allocation.x,
199 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400200 cairo_paint(cr);
201 }
202
203 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400204 cairo_surface_destroy(surface);
205}
206
207static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400208keyboard_focus_handler(struct window *window,
209 struct input *device, void *data)
210{
211 struct dnd *dnd = data;
212
213 window_schedule_redraw(dnd->window);
214}
215
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800216static int
217dnd_add_item(struct dnd *dnd, struct item *item)
218{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400219 unsigned int i;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800220
221 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
222 if (dnd->items[i] == 0) {
223 dnd->items[i] = item;
224 return i;
225 }
226 }
227 return -1;
228}
229
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400230static struct item *
231dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
232{
233 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500234 struct rectangle allocation;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400235 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400236
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500237 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400238
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500239 x -= allocation.x;
240 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400241
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400242 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
243 item = dnd->items[i];
244 if (item &&
245 item->x <= x && x < item->x + item_width &&
246 item->y <= y && y < item->y + item_height)
247 return item;
248 }
249
250 return NULL;
251}
252
253static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400254data_source_target(void *data,
255 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400256{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400257 struct dnd_drag *dnd_drag = data;
258 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400259 cairo_surface_t *surface;
260 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400261
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800262 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400263 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400264 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400265 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400266 surface = dnd_drag->translucent;
267
268 buffer = display_get_buffer_for_surface(dnd->display, surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200269 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400270 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
271 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300272 wl_surface_commit(dnd_drag->drag_surface);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400273}
274
275static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400276data_source_send(void *data, struct wl_data_source *source,
277 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400278{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800279 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400280 struct dnd_drag *dnd_drag = data;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800281
282 dnd_flower_message.seed = dnd_drag->item->seed;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800283 dnd_flower_message.x_offset = dnd_drag->x_offset;
284 dnd_flower_message.y_offset = dnd_drag->y_offset;
285
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200286 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
287 abort();
288
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400289 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400290}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400291
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400292static void
293data_source_cancelled(void *data, struct wl_data_source *source)
294{
295 struct dnd_drag *dnd_drag = data;
296
297 /* The 'cancelled' event means that the source is no longer in
298 * use by the drag (or current selection). We need to clean
299 * up the drag object created and the local state. */
300
301 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800302
303 /* Destroy the item that has been dragged out */
304 cairo_surface_destroy(dnd_drag->item->surface);
305 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200306
307 wl_surface_destroy(dnd_drag->drag_surface);
308
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400309 cairo_surface_destroy(dnd_drag->translucent);
310 cairo_surface_destroy(dnd_drag->opaque);
311 free(dnd_drag);
312}
313
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400314static const struct wl_data_source_listener data_source_listener = {
315 data_source_target,
316 data_source_send,
317 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400318};
319
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400320static cairo_surface_t *
321create_drag_cursor(struct dnd_drag *dnd_drag,
322 struct item *item, int32_t x, int32_t y, double opacity)
323{
324 struct dnd *dnd = dnd_drag->dnd;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300325 cairo_surface_t *surface;
326 struct wl_cursor_image *pointer;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400327 struct rectangle rectangle;
328 cairo_pattern_t *pattern;
329 cairo_t *cr;
330
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300331 pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
Philipp Brüschweilerf22d0ec2012-08-13 20:04:54 +0200332 if (!pointer) {
333 fprintf(stderr, "WARNING: grabbing cursor image not found\n");
334 pointer = display_get_pointer_image(dnd->display,
335 CURSOR_LEFT_PTR);
336 assert(pointer && "no cursor image found");
337 }
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400338
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300339 rectangle.width = item_width + 2 * pointer->width;
340 rectangle.height = item_height + 2 * pointer->height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300341 surface = display_create_surface(dnd->display, NULL, &rectangle,
342 SURFACE_SHM);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400343
344 cr = cairo_create(surface);
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300345 cairo_translate(cr, pointer->width, pointer->height);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400346
347 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
348 cairo_set_source_rgba(cr, 0, 0, 0, 0);
349 cairo_paint(cr);
350
351 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
352 cairo_set_source_surface(cr, item->surface, 0, 0);
353 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
354 cairo_mask(cr, pattern);
355 cairo_pattern_destroy(pattern);
356
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400357 /* FIXME: more cairo-gl brokeness */
Benjamin Franzke47eb8f42011-10-07 09:08:56 +0200358 surface_flush_device(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400359 cairo_destroy(cr);
360
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300361 dnd_drag->hotspot_x = pointer->width + x - item->x;
362 dnd_drag->hotspot_y = pointer->height + y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400363 dnd_drag->width = rectangle.width;
364 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400365
366 return surface;
367}
368
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400369static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500370dnd_button_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400371 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100372 uint32_t button, enum wl_pointer_button_state state,
373 void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400374{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500375 struct dnd *dnd = data;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400376 int32_t x, y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400377 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500378 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400379 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200380 struct display *display;
381 struct wl_compositor *compositor;
382 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400383 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400384 uint32_t serial;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300385 cairo_surface_t *icon;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400386
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500387 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400388 input_get_position(input, &x, &y);
389 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500390 x -= allocation.x;
391 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400392
Daniel Stone4dbadb12012-05-30 16:31:51 +0100393 if (item && state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400394 dnd_drag = malloc(sizeof *dnd_drag);
395 dnd_drag->dnd = dnd;
396 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400397 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800398 dnd_drag->item = item;
399 dnd_drag->x_offset = x - item->x;
400 dnd_drag->y_offset = y - item->y;
401
402 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
403 if (item == dnd->items[i]){
404 dnd->items[i] = 0;
405 break;
406 }
407 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400408
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200409 display = window_get_display(dnd->window);
410 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400411 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200412 dnd_drag->drag_surface =
413 wl_compositor_create_surface(compositor);
414
Kristian Høgsberga6c8b002012-04-13 12:55:45 -0400415 input_ungrab(input);
416
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300417 if (dnd->self_only) {
418 dnd_drag->data_source = NULL;
419 } else {
420 dnd_drag->data_source =
421 display_create_data_source(dnd->display);
422 wl_data_source_add_listener(dnd_drag->data_source,
423 &data_source_listener,
424 dnd_drag);
425 wl_data_source_offer(dnd_drag->data_source,
426 "application/x-wayland-dnd-flower");
427 wl_data_source_offer(dnd_drag->data_source,
428 "text/plain; charset=utf-8");
429 }
430
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400431 wl_data_device_start_drag(input_get_data_device(input),
432 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500433 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200434 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400435 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400436
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -0400437 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400438
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400439 dnd_drag->opaque =
440 create_drag_cursor(dnd_drag, item, x, y, 1);
441 dnd_drag->translucent =
442 create_drag_cursor(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400443
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300444 if (dnd->self_only)
445 icon = dnd_drag->opaque;
446 else
447 icon = dnd_drag->translucent;
448
449 buffer = display_get_buffer_for_surface(dnd->display, icon);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200450 wl_surface_attach(dnd_drag->drag_surface, buffer,
451 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400452 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
453 dnd_drag->width, dnd_drag->height);
Pekka Paalanenc9e00c02012-10-10 12:49:24 +0300454 wl_surface_commit(dnd_drag->drag_surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200455
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300456 dnd->current_drag = dnd_drag;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800457 window_schedule_redraw(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400458 }
459}
460
461static int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400462lookup_cursor(struct dnd *dnd, int x, int y)
463{
464 struct item *item;
465
466 item = dnd_get_item(dnd, x, y);
467 if (item)
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300468 return CURSOR_HAND1;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400469 else
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300470 return CURSOR_LEFT_PTR;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400471}
472
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500473static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500474dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400475 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400476{
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300477 struct dnd *dnd = data;
478
479 dnd->current_drag = NULL;
480
481 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400482}
483
484static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500485dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400486 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400487 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400488{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500489 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400490}
491
492static void
493dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400494 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400495 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400496{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400497 struct dnd *dnd = data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400498
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300499 if (!types)
500 return;
501
502 if (dnd_get_item(dnd, x, y) || dnd->self_only) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400503 input_accept(input, NULL);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300504 } else {
505 input_accept(input, types[0]);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400506 }
507}
508
509static void
510dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
511{
512 struct dnd *dnd = user_data;
513 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400514 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400515 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400516
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400517 if (len == 0) {
518 return;
519 } else if (len != sizeof *message) {
Damien Lespiau4df7e272012-10-26 01:15:44 +0100520 fprintf(stderr, "odd message length %zu, expected %zu\n",
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400521 len, sizeof *message);
522 return;
523 }
524
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500525 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400526 item = item_create(dnd->display,
527 x - message->x_offset - allocation.x,
528 y - message->y_offset - allocation.y,
529 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400530
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400531 dnd_add_item(dnd, item);
532 window_schedule_redraw(dnd->window);
533}
534
535static void
536dnd_drop_handler(struct window *window, struct input *input,
537 int32_t x, int32_t y, void *data)
538{
539 struct dnd *dnd = data;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300540 struct dnd_flower_message message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400541
542 if (dnd_get_item(dnd, x, y)) {
543 fprintf(stderr, "got 'drop', but no target\n");
544 return;
545 }
546
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300547 if (!dnd->self_only) {
548 input_receive_drag_data(input,
549 "application/x-wayland-dnd-flower",
550 dnd_receive_func, dnd);
551 } else if (dnd->current_drag) {
552 message.seed = dnd->current_drag->item->seed;
553 message.x_offset = dnd->current_drag->x_offset;
554 message.y_offset = dnd->current_drag->y_offset;
555 dnd_receive_func(&message, sizeof message, x, y, dnd);
556 dnd->current_drag = NULL;
557 } else {
558 fprintf(stderr, "ignoring drop from another client\n");
559 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400560}
561
562static struct dnd *
563dnd_create(struct display *display)
564{
565 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400566 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500567 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400568 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400569
570 dnd = malloc(sizeof *dnd);
571 if (dnd == NULL)
572 return dnd;
573 memset(dnd, 0, sizeof *dnd);
574
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500575 dnd->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500576 dnd->widget = frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500577 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400578
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400579 dnd->display = display;
580 dnd->key = 100;
581
582 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
583 x = (i % 4) * (item_width + item_padding) + item_padding;
584 y = (i / 4) * (item_height + item_padding) + item_padding;
585 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800586 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400587 else
588 dnd->items[i] = NULL;
589 }
590
591 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400592 window_set_keyboard_focus_handler(dnd->window,
593 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400594 window_set_data_handler(dnd->window, dnd_data_handler);
595 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400596
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500597 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500598 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
599 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
600 widget_set_button_handler(dnd->widget, dnd_button_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500601
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500602 width = 4 * (item_width + item_padding) + item_padding;
603 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400604
Kristian Høgsberga1627922012-06-20 17:30:03 -0400605 frame_set_child_size(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400606
607 return dnd;
608}
609
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400610int
611main(int argc, char *argv[])
612{
613 struct display *d;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300614 struct dnd *dnd;
615 int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400616
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400617 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200618 if (d == NULL) {
619 fprintf(stderr, "failed to create display: %m\n");
620 return -1;
621 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400622
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300623 dnd = dnd_create(d);
624
625 for (i = 1; i < argc; i++)
626 if (strcmp("--self-only", argv[i]) == 0)
627 dnd->self_only = 1;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400628
629 display_run(d);
630
631 return 0;
632}