blob: 6f55e9540fb4a9446eea2e3febfa4d1ee4eccdf3 [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
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <sys/time.h>
31#include <cairo.h>
Kristian Høgsberg3a696272011-09-14 17:33:48 -040032#include <sys/epoll.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-client.h>
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +030035#include <wayland-cursor.h>
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040036
37#include "window.h"
Kristian Høgsberg5a315bc2012-05-15 22:33:43 -040038#include "../shared/cairo-util.h"
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040039
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030040struct dnd_drag;
41
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040042struct dnd {
43 struct window *window;
Kristian Høgsberg75bc6672012-01-10 09:43:58 -050044 struct widget *widget;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040045 struct display *display;
46 uint32_t key;
47 struct item *items[16];
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +030048 int self_only;
49 struct dnd_drag *current_drag;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040050};
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040051
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040052struct dnd_drag {
53 cairo_surface_t *translucent;
54 cairo_surface_t *opaque;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -040055 int hotspot_x, hotspot_y;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040056 struct dnd *dnd;
57 struct input *input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -040058 uint32_t time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080059 struct item *item;
60 int x_offset, y_offset;
Kristian Høgsberg679f7162012-03-27 16:44:57 -040061 int width, height;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080062 const char *mime_type;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -040063
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +020064 struct wl_surface *drag_surface;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -040065 struct wl_data_source *data_source;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040066};
67
68struct item {
69 cairo_surface_t *surface;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080070 int seed;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040071 int x, y;
72};
73
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080074struct dnd_flower_message {
75 int seed, x_offset, y_offset;
76};
77
78
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040079static const int item_width = 64;
80static const int item_height = 64;
81static const int item_padding = 16;
82
83static struct item *
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080084item_create(struct display *display, int x, int y, int seed)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040085{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -080086 struct item *item;
87 struct timeval tv;
88
89 item = malloc(sizeof *item);
90 if (item == NULL)
91 return NULL;
92
93
94 gettimeofday(&tv, NULL);
95 item->seed = seed ? seed : tv.tv_usec;
96 srandom(item->seed);
97
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -040098 const int petal_count = 3 + random() % 5;
99 const double r1 = 20 + random() % 10;
100 const double r2 = 5 + random() % 12;
101 const double u = (10 + random() % 90) / 100.0;
102 const double v = (random() % 90) / 100.0;
103
104 cairo_t *cr;
105 int i;
106 double t, dt = 2 * M_PI / (petal_count * 2);
107 double x1, y1, x2, y2, x3, y3;
108 struct rectangle rect;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400109
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400110
111 rect.width = item_width;
112 rect.height = item_height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300113 item->surface =
114 display_create_surface(display, NULL, &rect, SURFACE_SHM);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400115
116 item->x = x;
117 item->y = y;
118
119 cr = cairo_create(item->surface);
120 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
121 cairo_set_source_rgba(cr, 0, 0, 0, 0);
122 cairo_paint(cr);
123
124 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
125 cairo_translate(cr, item_width / 2, item_height / 2);
126 t = random();
127 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
128 for (i = 0; i < petal_count; i++, t += dt * 2) {
129 x1 = cos(t) * r1;
130 y1 = sin(t) * r1;
131 x2 = cos(t + dt) * r2;
132 y2 = sin(t + dt) * r2;
133 x3 = cos(t + 2 * dt) * r1;
134 y3 = sin(t + 2 * dt) * r1;
135
136 cairo_curve_to(cr,
137 x1 - y1 * u, y1 + x1 * u,
138 x2 + y2 * v, y2 - x2 * v,
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400139 x2, y2);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400140
141 cairo_curve_to(cr,
142 x2 - y2 * v, y2 + x2 * v,
143 x3 + y3 * u, y3 - x3 * u,
144 x3, y3);
145 }
146
147 cairo_close_path(cr);
148
149 cairo_set_source_rgba(cr,
150 0.5 + (random() % 50) / 49.0,
151 0.5 + (random() % 50) / 49.0,
152 0.5 + (random() % 50) / 49.0,
153 0.5 + (random() % 100) / 99.0);
154
155 cairo_fill_preserve(cr);
156
157 cairo_set_line_width(cr, 1);
158 cairo_set_source_rgba(cr,
159 0.5 + (random() % 50) / 49.0,
160 0.5 + (random() % 50) / 49.0,
161 0.5 + (random() % 50) / 49.0,
162 0.5 + (random() % 100) / 99.0);
163 cairo_stroke(cr);
164
165 cairo_destroy(cr);
166
167 return item;
168}
169
170static void
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500171dnd_redraw_handler(struct widget *widget, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400172{
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500173 struct dnd *dnd = data;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500174 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400175 cairo_t *cr;
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500176 cairo_surface_t *surface;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400177 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400178
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500179 surface = window_get_surface(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400180 cr = cairo_create(surface);
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500181 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500182 cairo_rectangle(cr, allocation.x, allocation.y,
183 allocation.width, allocation.height);
Kristian Høgsberge164e4e2011-01-21 11:35:05 -0500184
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400185 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
186 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400187 cairo_fill(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400188
Kristian Høgsberg0fd49aa2012-07-23 21:32:46 -0400189 cairo_rectangle(cr, allocation.x, allocation.y,
190 allocation.width, allocation.height);
191 cairo_clip(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400192 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
193 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
194 if (!dnd->items[i])
195 continue;
196 cairo_set_source_surface(cr, dnd->items[i]->surface,
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400197 dnd->items[i]->x + allocation.x,
198 dnd->items[i]->y + allocation.y);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400199 cairo_paint(cr);
200 }
201
202 cairo_destroy(cr);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400203 cairo_surface_destroy(surface);
204}
205
206static void
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400207keyboard_focus_handler(struct window *window,
208 struct input *device, void *data)
209{
210 struct dnd *dnd = data;
211
212 window_schedule_redraw(dnd->window);
213}
214
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800215static int
216dnd_add_item(struct dnd *dnd, struct item *item)
217{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400218 unsigned int i;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800219
220 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
221 if (dnd->items[i] == 0) {
222 dnd->items[i] = item;
223 return i;
224 }
225 }
226 return -1;
227}
228
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400229static struct item *
230dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
231{
232 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500233 struct rectangle allocation;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400234 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400235
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500236 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400237
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500238 x -= allocation.x;
239 y -= allocation.y;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400240
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400241 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
242 item = dnd->items[i];
243 if (item &&
244 item->x <= x && x < item->x + item_width &&
245 item->y <= y && y < item->y + item_height)
246 return item;
247 }
248
249 return NULL;
250}
251
252static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400253data_source_target(void *data,
254 struct wl_data_source *source, const char *mime_type)
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400255{
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400256 struct dnd_drag *dnd_drag = data;
257 struct dnd *dnd = dnd_drag->dnd;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400258 cairo_surface_t *surface;
259 struct wl_buffer *buffer;
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400260
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800261 dnd_drag->mime_type = mime_type;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400262 if (mime_type)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400263 surface = dnd_drag->opaque;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400264 else
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400265 surface = dnd_drag->translucent;
266
267 buffer = display_get_buffer_for_surface(dnd->display, surface);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200268 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400269 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
270 dnd_drag->width, dnd_drag->height);
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400271}
272
273static void
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400274data_source_send(void *data, struct wl_data_source *source,
275 const char *mime_type, int32_t fd)
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400276{
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800277 struct dnd_flower_message dnd_flower_message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400278 struct dnd_drag *dnd_drag = data;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800279
280 dnd_flower_message.seed = dnd_drag->item->seed;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800281 dnd_flower_message.x_offset = dnd_drag->x_offset;
282 dnd_flower_message.y_offset = dnd_drag->y_offset;
283
Jonas Ådahl3685c3a2012-03-30 23:10:27 +0200284 if (write(fd, &dnd_flower_message, sizeof dnd_flower_message) < 0)
285 abort();
286
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400287 close(fd);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400288}
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400289
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400290static void
291data_source_cancelled(void *data, struct wl_data_source *source)
292{
293 struct dnd_drag *dnd_drag = data;
294
295 /* The 'cancelled' event means that the source is no longer in
296 * use by the drag (or current selection). We need to clean
297 * up the drag object created and the local state. */
298
299 wl_data_source_destroy(dnd_drag->data_source);
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800300
301 /* Destroy the item that has been dragged out */
302 cairo_surface_destroy(dnd_drag->item->surface);
303 free(dnd_drag->item);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200304
305 wl_surface_destroy(dnd_drag->drag_surface);
306
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400307 cairo_surface_destroy(dnd_drag->translucent);
308 cairo_surface_destroy(dnd_drag->opaque);
309 free(dnd_drag);
310}
311
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400312static const struct wl_data_source_listener data_source_listener = {
313 data_source_target,
314 data_source_send,
315 data_source_cancelled
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400316};
317
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400318static cairo_surface_t *
319create_drag_cursor(struct dnd_drag *dnd_drag,
320 struct item *item, int32_t x, int32_t y, double opacity)
321{
322 struct dnd *dnd = dnd_drag->dnd;
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300323 cairo_surface_t *surface;
324 struct wl_cursor_image *pointer;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400325 struct rectangle rectangle;
326 cairo_pattern_t *pattern;
327 cairo_t *cr;
328
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300329 pointer = display_get_pointer_image(dnd->display, CURSOR_DRAGGING);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400330
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300331 rectangle.width = item_width + 2 * pointer->width;
332 rectangle.height = item_height + 2 * pointer->height;
Ander Conselvan de Oliveira210eb9d2012-05-25 16:03:06 +0300333 surface = display_create_surface(dnd->display, NULL, &rectangle,
334 SURFACE_SHM);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400335
336 cr = cairo_create(surface);
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300337 cairo_translate(cr, pointer->width, pointer->height);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400338
339 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
340 cairo_set_source_rgba(cr, 0, 0, 0, 0);
341 cairo_paint(cr);
342
343 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
344 cairo_set_source_surface(cr, item->surface, 0, 0);
345 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
346 cairo_mask(cr, pattern);
347 cairo_pattern_destroy(pattern);
348
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400349 /* FIXME: more cairo-gl brokeness */
Benjamin Franzke47eb8f42011-10-07 09:08:56 +0200350 surface_flush_device(surface);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400351 cairo_destroy(cr);
352
Ander Conselvan de Oliveira1042dc12012-05-22 15:39:42 +0300353 dnd_drag->hotspot_x = pointer->width + x - item->x;
354 dnd_drag->hotspot_y = pointer->height + y - item->y;
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400355 dnd_drag->width = rectangle.width;
356 dnd_drag->height = rectangle.height;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400357
358 return surface;
359}
360
Kristian Høgsberg506e20e2010-08-19 17:26:02 -0400361static void
Kristian Høgsberga8a0db32012-01-09 11:12:05 -0500362dnd_button_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400363 struct input *input, uint32_t time,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100364 uint32_t button, enum wl_pointer_button_state state,
365 void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400366{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500367 struct dnd *dnd = data;
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400368 int32_t x, y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400369 struct item *item;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500370 struct rectangle allocation;
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400371 struct dnd_drag *dnd_drag;
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200372 struct display *display;
373 struct wl_compositor *compositor;
374 struct wl_buffer *buffer;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400375 unsigned int i;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400376 uint32_t serial;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300377 cairo_surface_t *icon;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400378
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500379 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberge968f9c2010-08-27 22:18:00 -0400380 input_get_position(input, &x, &y);
381 item = dnd_get_item(dnd, x, y);
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500382 x -= allocation.x;
383 y -= allocation.y;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400384
Daniel Stone4dbadb12012-05-30 16:31:51 +0100385 if (item && state == WL_POINTER_BUTTON_STATE_PRESSED) {
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400386 dnd_drag = malloc(sizeof *dnd_drag);
387 dnd_drag->dnd = dnd;
388 dnd_drag->input = input;
Kristian Høgsbergce457ba2010-09-14 15:39:45 -0400389 dnd_drag->time = time;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800390 dnd_drag->item = item;
391 dnd_drag->x_offset = x - item->x;
392 dnd_drag->y_offset = y - item->y;
393
394 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
395 if (item == dnd->items[i]){
396 dnd->items[i] = 0;
397 break;
398 }
399 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400400
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200401 display = window_get_display(dnd->window);
402 compositor = display_get_compositor(display);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400403 serial = display_get_serial(display);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200404 dnd_drag->drag_surface =
405 wl_compositor_create_surface(compositor);
406
Kristian Høgsberga6c8b002012-04-13 12:55:45 -0400407 input_ungrab(input);
408
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300409 if (dnd->self_only) {
410 dnd_drag->data_source = NULL;
411 } else {
412 dnd_drag->data_source =
413 display_create_data_source(dnd->display);
414 wl_data_source_add_listener(dnd_drag->data_source,
415 &data_source_listener,
416 dnd_drag);
417 wl_data_source_offer(dnd_drag->data_source,
418 "application/x-wayland-dnd-flower");
419 wl_data_source_offer(dnd_drag->data_source,
420 "text/plain; charset=utf-8");
421 }
422
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400423 wl_data_device_start_drag(input_get_data_device(input),
424 dnd_drag->data_source,
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500425 window_get_wl_surface(dnd->window),
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200426 dnd_drag->drag_surface,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400427 serial);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400428
Kristian Høgsberg5a4e9ff2012-06-04 16:04:07 -0400429 input_set_pointer_image(input, CURSOR_DRAGGING);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400430
Kristian Høgsberge9d37bd2010-09-02 20:22:42 -0400431 dnd_drag->opaque =
432 create_drag_cursor(dnd_drag, item, x, y, 1);
433 dnd_drag->translucent =
434 create_drag_cursor(dnd_drag, item, x, y, 0.2);
Kristian Høgsberg1d7ffd32010-08-25 16:34:05 -0400435
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300436 if (dnd->self_only)
437 icon = dnd_drag->opaque;
438 else
439 icon = dnd_drag->translucent;
440
441 buffer = display_get_buffer_for_surface(dnd->display, icon);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200442 wl_surface_attach(dnd_drag->drag_surface, buffer,
443 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
Kristian Høgsberg679f7162012-03-27 16:44:57 -0400444 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
445 dnd_drag->width, dnd_drag->height);
Ander Conselvan de Oliveirae47c3a32012-02-15 17:02:58 +0200446
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300447 dnd->current_drag = dnd_drag;
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800448 window_schedule_redraw(dnd->window);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400449 }
450}
451
452static int
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400453lookup_cursor(struct dnd *dnd, int x, int y)
454{
455 struct item *item;
456
457 item = dnd_get_item(dnd, x, y);
458 if (item)
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300459 return CURSOR_HAND1;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400460 else
Ander Conselvan de Oliveiradc8c8fc2012-05-25 16:01:41 +0300461 return CURSOR_LEFT_PTR;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400462}
463
Kristian Høgsbergbb901fa2012-01-09 11:22:32 -0500464static int
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500465dnd_enter_handler(struct widget *widget,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400466 struct input *input, float x, float y, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400467{
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300468 struct dnd *dnd = data;
469
470 dnd->current_drag = NULL;
471
472 return lookup_cursor(dnd, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400473}
474
475static int
Kristian Høgsberg5f190ef2012-01-09 09:44:45 -0500476dnd_motion_handler(struct widget *widget,
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400477 struct input *input, uint32_t time,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400478 float x, float y, void *data)
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400479{
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500480 return lookup_cursor(data, x, y);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400481}
482
483static void
484dnd_data_handler(struct window *window,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400485 struct input *input,
Kristian Høgsberg80680c72012-05-10 12:21:37 -0400486 float x, float y, const char **types, void *data)
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400487{
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400488 struct dnd *dnd = data;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400489
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300490 if (!types)
491 return;
492
493 if (dnd_get_item(dnd, x, y) || dnd->self_only) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400494 input_accept(input, NULL);
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300495 } else {
496 input_accept(input, types[0]);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400497 }
498}
499
500static void
501dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
502{
503 struct dnd *dnd = user_data;
504 struct dnd_flower_message *message = data;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400505 struct item *item;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400506 struct rectangle allocation;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400507
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400508 if (len == 0) {
509 return;
510 } else if (len != sizeof *message) {
511 fprintf(stderr, "odd message length %ld, expected %ld\n",
512 len, sizeof *message);
513 return;
514 }
515
Kristian Høgsbergbb977002012-01-10 19:11:42 -0500516 widget_get_allocation(dnd->widget, &allocation);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400517 item = item_create(dnd->display,
518 x - message->x_offset - allocation.x,
519 y - message->y_offset - allocation.y,
520 message->seed);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400521
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400522 dnd_add_item(dnd, item);
523 window_schedule_redraw(dnd->window);
524}
525
526static void
527dnd_drop_handler(struct window *window, struct input *input,
528 int32_t x, int32_t y, void *data)
529{
530 struct dnd *dnd = data;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300531 struct dnd_flower_message message;
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400532
533 if (dnd_get_item(dnd, x, y)) {
534 fprintf(stderr, "got 'drop', but no target\n");
535 return;
536 }
537
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300538 if (!dnd->self_only) {
539 input_receive_drag_data(input,
540 "application/x-wayland-dnd-flower",
541 dnd_receive_func, dnd);
542 } else if (dnd->current_drag) {
543 message.seed = dnd->current_drag->item->seed;
544 message.x_offset = dnd->current_drag->x_offset;
545 message.y_offset = dnd->current_drag->y_offset;
546 dnd_receive_func(&message, sizeof message, x, y, dnd);
547 dnd->current_drag = NULL;
548 } else {
549 fprintf(stderr, "ignoring drop from another client\n");
550 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400551}
552
553static struct dnd *
554dnd_create(struct display *display)
555{
556 struct dnd *dnd;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400557 int x, y;
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500558 int32_t width, height;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400559 unsigned int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400560
561 dnd = malloc(sizeof *dnd);
562 if (dnd == NULL)
563 return dnd;
564 memset(dnd, 0, sizeof *dnd);
565
Kristian Høgsberg009ac0a2012-01-31 15:24:48 -0500566 dnd->window = window_create(display);
Kristian Høgsberg29af3eb2012-01-10 22:41:05 -0500567 dnd->widget = frame_create(dnd->window, dnd);
Kristian Høgsberg248c1b62011-01-21 18:03:15 -0500568 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400569
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400570 dnd->display = display;
571 dnd->key = 100;
572
573 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
574 x = (i % 4) * (item_width + item_padding) + item_padding;
575 y = (i / 4) * (item_height + item_padding) + item_padding;
576 if ((i ^ (i >> 2)) & 1)
Joel Teichroeb0c007ae2010-11-30 10:22:16 -0800577 dnd->items[i] = item_create(display, x, y, 0);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400578 else
579 dnd->items[i] = NULL;
580 }
581
582 window_set_user_data(dnd->window, dnd);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400583 window_set_keyboard_focus_handler(dnd->window,
584 keyboard_focus_handler);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -0400585 window_set_data_handler(dnd->window, dnd_data_handler);
586 window_set_drop_handler(dnd->window, dnd_drop_handler);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400587
Kristian Høgsbergb67e94b2012-01-10 12:23:19 -0500588 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
Kristian Høgsberg75bc6672012-01-10 09:43:58 -0500589 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
590 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
591 widget_set_button_handler(dnd->widget, dnd_button_handler);
Kristian Høgsbergac7619f2012-01-09 09:26:38 -0500592
Kristian Høgsbergda846ca2011-01-11 10:00:52 -0500593 width = 4 * (item_width + item_padding) + item_padding;
594 height = 4 * (item_height + item_padding) + item_padding;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400595
Kristian Høgsberga1627922012-06-20 17:30:03 -0400596 frame_set_child_size(dnd->widget, width, height);
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400597
598 return dnd;
599}
600
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400601int
602main(int argc, char *argv[])
603{
604 struct display *d;
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300605 struct dnd *dnd;
606 int i;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400607
Kristian Høgsbergbcacef12012-03-11 21:05:57 -0400608 d = display_create(argc, argv);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200609 if (d == NULL) {
610 fprintf(stderr, "failed to create display: %m\n");
611 return -1;
612 }
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400613
Ander Conselvan de Oliveira08bcf142012-05-29 10:58:27 +0300614 dnd = dnd_create(d);
615
616 for (i = 1; i < argc; i++)
617 if (strcmp("--self-only", argv[i]) == 0)
618 dnd->self_only = 1;
Kristian Høgsbergb8cc24e2010-08-18 20:31:06 -0400619
620 display_run(d);
621
622 return 0;
623}