1 /*
2 * Copyright © 2010 Kristian Høgsberg
3 *
4 * 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:
10 *
11 * 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.
22 */
23
24 #include "config.h"
25
26 #include <assert.h>
27 #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>
36 #include <sys/epoll.h>
37 #include <stdbool.h>
38 #include <errno.h>
39
40 #include <wayland-client.h>
41 #include <wayland-cursor.h>
42
43 #include "window.h"
44 #include "shared/cairo-util.h"
45 #include "shared/helpers.h"
46 #include "shared/xalloc.h"
47
48 struct dnd_drag;
49
50 struct pointer {
51 struct input *input;
52 bool dragging;
53 struct wl_list link;
54 };
55
56 struct dnd {
57 struct window *window;
58 struct widget *widget;
59 struct display *display;
60 uint32_t key;
61 struct item *items[16];
62 int self_only;
63 struct dnd_drag *current_drag;
64 struct wl_list pointers;
65 };
66
67 struct dnd_drag {
68 cairo_surface_t *translucent;
69 cairo_surface_t *opaque;
70 int hotspot_x, hotspot_y;
71 struct dnd *dnd;
72 struct input *input;
73 uint32_t time;
74 struct item *item;
75 int x_offset, y_offset;
76 int width, height;
77 uint32_t dnd_action;
78 const char *mime_type;
79
80 struct wl_surface *drag_surface;
81 struct wl_data_source *data_source;
82 };
83
84 struct item {
85 cairo_surface_t *surface;
86 int seed;
87 int x, y;
88 };
89
90 struct dnd_flower_message {
91 int seed, x_offset, y_offset;
92 };
93
94
95 static const int item_width = 64;
96 static const int item_height = 64;
97 static const int item_padding = 16;
98
99 static const char flower_mime_type[] = "application/x-wayland-dnd-flower";
100 static const char text_mime_type[] = "text/plain;charset=utf-8";
101
102 static struct item *
item_create(struct display * display,int x,int y,int seed)103 item_create(struct display *display, int x, int y, int seed)
104 {
105 struct item *item;
106 struct timeval tv;
107
108 item = malloc(sizeof *item);
109 if (item == NULL)
110 return NULL;
111
112
113 gettimeofday(&tv, NULL);
114 item->seed = seed ? seed : tv.tv_usec;
115 srandom(item->seed);
116
117 const int petal_count = 3 + random() % 5;
118 const double r1 = 20 + random() % 10;
119 const double r2 = 5 + random() % 12;
120 const double u = (10 + random() % 90) / 100.0;
121 const double v = (random() % 90) / 100.0;
122
123 cairo_t *cr;
124 int i;
125 double t, dt = 2 * M_PI / (petal_count * 2);
126 double x1, y1, x2, y2, x3, y3;
127 struct rectangle rect;
128
129
130 rect.width = item_width;
131 rect.height = item_height;
132 item->surface =
133 display_create_surface(display, NULL, &rect, SURFACE_SHM);
134
135 item->x = x;
136 item->y = y;
137
138 cr = cairo_create(item->surface);
139 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
140 cairo_set_source_rgba(cr, 0, 0, 0, 0);
141 cairo_paint(cr);
142
143 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
144 cairo_translate(cr, item_width / 2, item_height / 2);
145 t = random();
146 cairo_move_to(cr, cos(t) * r1, sin(t) * r1);
147 for (i = 0; i < petal_count; i++, t += dt * 2) {
148 x1 = cos(t) * r1;
149 y1 = sin(t) * r1;
150 x2 = cos(t + dt) * r2;
151 y2 = sin(t + dt) * r2;
152 x3 = cos(t + 2 * dt) * r1;
153 y3 = sin(t + 2 * dt) * r1;
154
155 cairo_curve_to(cr,
156 x1 - y1 * u, y1 + x1 * u,
157 x2 + y2 * v, y2 - x2 * v,
158 x2, y2);
159
160 cairo_curve_to(cr,
161 x2 - y2 * v, y2 + x2 * v,
162 x3 + y3 * u, y3 - x3 * u,
163 x3, y3);
164 }
165
166 cairo_close_path(cr);
167
168 cairo_set_source_rgba(cr,
169 0.5 + (random() % 50) / 49.0,
170 0.5 + (random() % 50) / 49.0,
171 0.5 + (random() % 50) / 49.0,
172 0.5 + (random() % 100) / 99.0);
173
174 cairo_fill_preserve(cr);
175
176 cairo_set_line_width(cr, 1);
177 cairo_set_source_rgba(cr,
178 0.5 + (random() % 50) / 49.0,
179 0.5 + (random() % 50) / 49.0,
180 0.5 + (random() % 50) / 49.0,
181 0.5 + (random() % 100) / 99.0);
182 cairo_stroke(cr);
183
184 cairo_destroy(cr);
185
186 return item;
187 }
188
189 static void
dnd_redraw_handler(struct widget * widget,void * data)190 dnd_redraw_handler(struct widget *widget, void *data)
191 {
192 struct dnd *dnd = data;
193 struct rectangle allocation;
194 cairo_t *cr;
195 cairo_surface_t *surface, *item_surface;
196 unsigned int i;
197
198 surface = window_get_surface(dnd->window);
199 cr = cairo_create(surface);
200 widget_get_allocation(dnd->widget, &allocation);
201 cairo_rectangle(cr, allocation.x, allocation.y,
202 allocation.width, allocation.height);
203
204 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
205 cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
206 cairo_fill(cr);
207
208 cairo_rectangle(cr, allocation.x, allocation.y,
209 allocation.width, allocation.height);
210 cairo_clip(cr);
211 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
212 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
213 if (!dnd->items[i])
214 continue;
215
216 if (dnd->current_drag && dnd->items[i] == dnd->current_drag->item)
217 item_surface = dnd->current_drag->translucent;
218 else
219 item_surface = dnd->items[i]->surface;
220
221 cairo_set_source_surface(cr, item_surface,
222 dnd->items[i]->x + allocation.x,
223 dnd->items[i]->y + allocation.y);
224 cairo_paint(cr);
225 }
226
227 cairo_destroy(cr);
228 cairo_surface_destroy(surface);
229 }
230
231 static void
keyboard_focus_handler(struct window * window,struct input * device,void * data)232 keyboard_focus_handler(struct window *window,
233 struct input *device, void *data)
234 {
235 struct dnd *dnd = data;
236
237 window_schedule_redraw(dnd->window);
238 }
239
240 static int
dnd_add_item(struct dnd * dnd,struct item * item)241 dnd_add_item(struct dnd *dnd, struct item *item)
242 {
243 unsigned int i;
244
245 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
246 if (dnd->items[i] == 0) {
247 dnd->items[i] = item;
248 return i;
249 }
250 }
251 return -1;
252 }
253
254 static struct item *
dnd_get_item(struct dnd * dnd,int32_t x,int32_t y)255 dnd_get_item(struct dnd *dnd, int32_t x, int32_t y)
256 {
257 struct item *item;
258 struct rectangle allocation;
259 unsigned int i;
260
261 widget_get_allocation(dnd->widget, &allocation);
262
263 x -= allocation.x;
264 y -= allocation.y;
265
266 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
267 item = dnd->items[i];
268 if (item &&
269 item->x <= x && x < item->x + item_width &&
270 item->y <= y && y < item->y + item_height)
271 return item;
272 }
273
274 return NULL;
275 }
276
277 static int
lookup_dnd_cursor(uint32_t dnd_action)278 lookup_dnd_cursor(uint32_t dnd_action)
279 {
280 if (dnd_action == WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE)
281 return CURSOR_DND_MOVE;
282 else if (dnd_action == WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY)
283 return CURSOR_DND_COPY;
284
285 return CURSOR_DND_FORBIDDEN;
286 }
287
288 static void
dnd_drag_update_cursor(struct dnd_drag * dnd_drag)289 dnd_drag_update_cursor(struct dnd_drag *dnd_drag)
290 {
291 int cursor;
292
293 if (dnd_drag->mime_type == NULL)
294 cursor = CURSOR_DND_FORBIDDEN;
295 else
296 cursor = lookup_dnd_cursor(dnd_drag->dnd_action);
297
298 input_set_pointer_image(dnd_drag->input, cursor);
299 }
300
301 static void
dnd_drag_update_surface(struct dnd_drag * dnd_drag)302 dnd_drag_update_surface(struct dnd_drag *dnd_drag)
303 {
304 struct dnd *dnd = dnd_drag->dnd;
305 cairo_surface_t *surface;
306 struct wl_buffer *buffer;
307
308 if (dnd_drag->mime_type && dnd_drag->dnd_action)
309 surface = dnd_drag->opaque;
310 else
311 surface = dnd_drag->translucent;
312
313 buffer = display_get_buffer_for_surface(dnd->display, surface);
314 wl_surface_attach(dnd_drag->drag_surface, buffer, 0, 0);
315 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
316 dnd_drag->width, dnd_drag->height);
317 wl_surface_commit(dnd_drag->drag_surface);
318 }
319
320 static void
data_source_target(void * data,struct wl_data_source * source,const char * mime_type)321 data_source_target(void *data,
322 struct wl_data_source *source, const char *mime_type)
323 {
324 struct dnd_drag *dnd_drag = data;
325
326 dnd_drag->mime_type = mime_type;
327 dnd_drag_update_surface(dnd_drag);
328 dnd_drag_update_cursor(dnd_drag);
329 }
330
331 static void
data_source_send(void * data,struct wl_data_source * source,const char * mime_type,int32_t fd)332 data_source_send(void *data, struct wl_data_source *source,
333 const char *mime_type, int32_t fd)
334 {
335 struct dnd_flower_message dnd_flower_message;
336 struct dnd_drag *dnd_drag = data;
337 char buffer[128];
338 int n;
339
340 if (strcmp(mime_type, flower_mime_type) == 0) {
341 dnd_flower_message.seed = dnd_drag->item->seed;
342 dnd_flower_message.x_offset = dnd_drag->x_offset;
343 dnd_flower_message.y_offset = dnd_drag->y_offset;
344
345 if (write(fd, &dnd_flower_message,
346 sizeof dnd_flower_message) < 0)
347 abort();
348 } else if (strcmp(mime_type, text_mime_type) == 0) {
349 n = snprintf(buffer, sizeof buffer, "seed=%d x=%d y=%d\n",
350 dnd_drag->item->seed,
351 dnd_drag->x_offset,
352 dnd_drag->y_offset);
353
354 if (write(fd, buffer, n) < 0)
355 abort();
356 }
357
358 close(fd);
359 }
360
361 static void
dnd_drag_destroy(struct dnd_drag * dnd_drag,bool delete_item)362 dnd_drag_destroy(struct dnd_drag *dnd_drag, bool delete_item)
363 {
364 struct dnd *dnd = dnd_drag->dnd;
365 unsigned int i;
366
367 wl_data_source_destroy(dnd_drag->data_source);
368
369 if (delete_item) {
370 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
371 if (dnd_drag->item == dnd->items[i]) {
372 dnd->items[i] = NULL;
373 break;
374 }
375 }
376
377 /* Destroy the item that has been dragged out */
378 cairo_surface_destroy(dnd_drag->item->surface);
379 free(dnd_drag->item);
380 }
381
382 dnd->current_drag = NULL;
383
384 wl_surface_destroy(dnd_drag->drag_surface);
385
386 cairo_surface_destroy(dnd_drag->translucent);
387 cairo_surface_destroy(dnd_drag->opaque);
388 free(dnd_drag);
389 }
390
391 static void
data_source_cancelled(void * data,struct wl_data_source * source)392 data_source_cancelled(void *data, struct wl_data_source *source)
393 {
394 struct dnd_drag *dnd_drag = data;
395 struct dnd *dnd = dnd_drag->dnd;
396
397 /* The 'cancelled' event means that the source is no longer in
398 * use by the drag (or current selection). We need to clean
399 * up the drag object created and the local state. */
400 dnd_drag_destroy(dnd_drag, false);
401 window_schedule_redraw(dnd->window);
402 }
403
404 static void
data_source_dnd_drop_performed(void * data,struct wl_data_source * source)405 data_source_dnd_drop_performed(void *data, struct wl_data_source *source)
406 {
407 }
408
409 static void
data_source_dnd_finished(void * data,struct wl_data_source * source)410 data_source_dnd_finished(void *data, struct wl_data_source *source)
411 {
412 struct dnd_drag *dnd_drag = data;
413 struct dnd *dnd = dnd_drag->dnd;
414 bool delete_item;
415
416 delete_item =
417 dnd_drag->dnd_action == WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
418
419 /* The operation is already finished, we can destroy all
420 * related data.
421 */
422 dnd_drag_destroy(dnd_drag, delete_item);
423 window_schedule_redraw(dnd->window);
424 }
425
426 static void
data_source_action(void * data,struct wl_data_source * source,uint32_t dnd_action)427 data_source_action(void *data, struct wl_data_source *source, uint32_t dnd_action)
428 {
429 struct dnd_drag *dnd_drag = data;
430
431 dnd_drag->dnd_action = dnd_action;
432 dnd_drag_update_surface(dnd_drag);
433 dnd_drag_update_cursor(dnd_drag);
434 }
435
436 static const struct wl_data_source_listener data_source_listener = {
437 data_source_target,
438 data_source_send,
439 data_source_cancelled,
440 data_source_dnd_drop_performed,
441 data_source_dnd_finished,
442 data_source_action,
443 };
444
445 static cairo_surface_t *
create_drag_icon(struct dnd_drag * dnd_drag,struct item * item,int32_t x,int32_t y,double opacity)446 create_drag_icon(struct dnd_drag *dnd_drag,
447 struct item *item, int32_t x, int32_t y, double opacity)
448 {
449 struct dnd *dnd = dnd_drag->dnd;
450 cairo_surface_t *surface;
451 struct rectangle rectangle;
452 cairo_pattern_t *pattern;
453 cairo_t *cr;
454
455 rectangle.width = item_width;
456 rectangle.height = item_height;
457 surface = display_create_surface(dnd->display, NULL, &rectangle,
458 SURFACE_SHM);
459
460 cr = cairo_create(surface);
461 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
462 cairo_set_source_surface(cr, item->surface, 0, 0);
463 pattern = cairo_pattern_create_rgba(0, 0, 0, opacity);
464 cairo_mask(cr, pattern);
465 cairo_pattern_destroy(pattern);
466
467 cairo_destroy(cr);
468
469 dnd_drag->hotspot_x = x - item->x;
470 dnd_drag->hotspot_y = y - item->y;
471 dnd_drag->width = rectangle.width;
472 dnd_drag->height = rectangle.height;
473
474 return surface;
475 }
476
477 static int
create_drag_source(struct dnd * dnd,struct input * input,uint32_t time,int32_t x,int32_t y)478 create_drag_source(struct dnd *dnd,
479 struct input *input, uint32_t time,
480 int32_t x, int32_t y)
481 {
482 struct item *item;
483 struct rectangle allocation;
484 struct dnd_drag *dnd_drag;
485 struct display *display;
486 struct wl_compositor *compositor;
487 struct wl_buffer *buffer;
488 unsigned int i;
489 uint32_t serial;
490 cairo_surface_t *icon;
491 uint32_t actions;
492
493 widget_get_allocation(dnd->widget, &allocation);
494 item = dnd_get_item(dnd, x, y);
495 x -= allocation.x;
496 y -= allocation.y;
497
498 if (item) {
499 dnd_drag = xmalloc(sizeof *dnd_drag);
500 dnd_drag->dnd = dnd;
501 dnd_drag->input = input;
502 dnd_drag->time = time;
503 dnd_drag->item = item;
504 dnd_drag->x_offset = x - item->x;
505 dnd_drag->y_offset = y - item->y;
506 dnd_drag->dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
507 dnd_drag->mime_type = NULL;
508
509 actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE |
510 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
511
512 display = window_get_display(dnd->window);
513 compositor = display_get_compositor(display);
514 serial = display_get_serial(display);
515 dnd_drag->drag_surface =
516 wl_compositor_create_surface(compositor);
517
518 if (display_get_data_device_manager_version(display) <
519 WL_DATA_SOURCE_SET_ACTIONS_SINCE_VERSION) {
520 /* Data sources version < 3 will not get action
521 * nor dnd_finished events, as we can't honor
522 * the "move" action at the time of finishing
523 * drag-and-drop, do it preemptively here.
524 */
525 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
526 if (item == dnd->items[i]){
527 dnd->items[i] = NULL;
528 break;
529 }
530 }
531 }
532
533 if (dnd->self_only) {
534 dnd_drag->data_source = NULL;
535 } else {
536 dnd_drag->data_source =
537 display_create_data_source(dnd->display);
538 if (!dnd_drag->data_source) {
539 fprintf(stderr, "No data device manager\n");
540 abort();
541 }
542 wl_data_source_add_listener(dnd_drag->data_source,
543 &data_source_listener,
544 dnd_drag);
545 wl_data_source_offer(dnd_drag->data_source,
546 flower_mime_type);
547 wl_data_source_offer(dnd_drag->data_source,
548 text_mime_type);
549 }
550
551 if (display_get_data_device_manager_version(display) >=
552 WL_DATA_SOURCE_SET_ACTIONS_SINCE_VERSION) {
553 wl_data_source_set_actions(dnd_drag->data_source, actions);
554 }
555
556 wl_data_device_start_drag(input_get_data_device(input),
557 dnd_drag->data_source,
558 window_get_wl_surface(dnd->window),
559 dnd_drag->drag_surface,
560 serial);
561
562 dnd_drag->opaque =
563 create_drag_icon(dnd_drag, item, x, y, 1);
564 dnd_drag->translucent =
565 create_drag_icon(dnd_drag, item, x, y, 0.2);
566
567 if (dnd->self_only)
568 icon = dnd_drag->opaque;
569 else
570 icon = dnd_drag->translucent;
571
572 buffer = display_get_buffer_for_surface(dnd->display, icon);
573 wl_surface_attach(dnd_drag->drag_surface, buffer,
574 -dnd_drag->hotspot_x, -dnd_drag->hotspot_y);
575 wl_surface_damage(dnd_drag->drag_surface, 0, 0,
576 dnd_drag->width, dnd_drag->height);
577 wl_surface_commit(dnd_drag->drag_surface);
578
579 dnd->current_drag = dnd_drag;
580 window_schedule_redraw(dnd->window);
581
582 return 0;
583 } else
584 return -1;
585 }
586
587 static int
lookup_cursor(struct dnd * dnd,int x,int y)588 lookup_cursor(struct dnd *dnd, int x, int y)
589 {
590 struct item *item;
591
592 item = dnd_get_item(dnd, x, y);
593 if (item)
594 return CURSOR_HAND1;
595 else
596 return CURSOR_LEFT_PTR;
597 }
598
599 /* Update all the mouse pointers in the window appropriately.
600 * Optionally, skip one (which will be the current pointer just
601 * about to start a drag). This is done here to save a scan
602 * through the pointer list.
603 */
604 static void
update_pointer_images_except(struct dnd * dnd,struct input * except)605 update_pointer_images_except(struct dnd *dnd, struct input *except)
606 {
607 struct pointer *pointer;
608 int32_t x, y;
609
610 wl_list_for_each(pointer, &dnd->pointers, link) {
611 if (pointer->input == except) {
612 pointer->dragging = true;
613 continue;
614 }
615 input_get_position(pointer->input, &x, &y);
616 input_set_pointer_image(pointer->input,
617 lookup_cursor(dnd, x, y));
618 }
619 }
620
621 static void
dnd_button_handler(struct widget * widget,struct input * input,uint32_t time,uint32_t button,enum wl_pointer_button_state state,void * data)622 dnd_button_handler(struct widget *widget,
623 struct input *input, uint32_t time,
624 uint32_t button, enum wl_pointer_button_state state,
625 void *data)
626 {
627 struct dnd *dnd = data;
628 int32_t x, y;
629
630 input_get_position(input, &x, &y);
631 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
632 input_ungrab(input);
633 if (create_drag_source(dnd, input, time, x, y) == 0) {
634 input_set_pointer_image(input, CURSOR_DRAGGING);
635 update_pointer_images_except(dnd, input);
636 }
637 }
638 }
639
640 static void
dnd_touch_down_handler(struct widget * widget,struct input * input,uint32_t serial,uint32_t time,int32_t id,float x,float y,void * data)641 dnd_touch_down_handler(struct widget *widget,
642 struct input *input, uint32_t serial,
643 uint32_t time, int32_t id,
644 float x, float y, void *data)
645 {
646 struct dnd *dnd = data;
647 int32_t int_x, int_y;
648
649 if (id > 0)
650 return;
651
652 int_x = (int32_t)x;
653 int_y = (int32_t)y;
654 if (create_drag_source(dnd, input, time, int_x, int_y) == 0)
655 touch_grab(input, 0);
656 }
657
658 static int
dnd_enter_handler(struct widget * widget,struct input * input,float x,float y,void * data)659 dnd_enter_handler(struct widget *widget,
660 struct input *input, float x, float y, void *data)
661 {
662 struct dnd *dnd = data;
663 struct pointer *new_pointer = malloc(sizeof *new_pointer);
664
665 if (new_pointer) {
666 new_pointer->input = input;
667 new_pointer->dragging = false;
668 wl_list_insert(dnd->pointers.prev, &new_pointer->link);
669 }
670
671 return lookup_cursor(dnd, x, y);
672 }
673
674 static void
dnd_leave_handler(struct widget * widget,struct input * input,void * data)675 dnd_leave_handler(struct widget *widget,
676 struct input *input, void *data)
677 {
678 struct dnd *dnd = data;
679 struct pointer *pointer, *tmp;
680
681 wl_list_for_each_safe(pointer, tmp, &dnd->pointers, link)
682 if (pointer->input == input) {
683 wl_list_remove(&pointer->link);
684 free(pointer);
685 }
686 }
687
688 static int
dnd_motion_handler(struct widget * widget,struct input * input,uint32_t time,float x,float y,void * data)689 dnd_motion_handler(struct widget *widget,
690 struct input *input, uint32_t time,
691 float x, float y, void *data)
692 {
693 struct dnd *dnd = data;
694 struct pointer *pointer;
695
696 wl_list_for_each(pointer, &dnd->pointers, link)
697 if (pointer->input == input) {
698 if (pointer->dragging)
699 return CURSOR_DRAGGING;
700 break;
701 }
702
703 return lookup_cursor(data, x, y);
704 }
705
706 static void
dnd_data_handler(struct window * window,struct input * input,float x,float y,const char ** types,void * data)707 dnd_data_handler(struct window *window,
708 struct input *input,
709 float x, float y, const char **types, void *data)
710 {
711 struct dnd *dnd = data;
712 int i, has_flower = 0;
713
714 if (!types)
715 return;
716 for (i = 0; types[i]; i++)
717 if (strcmp(types[i], flower_mime_type) == 0)
718 has_flower = 1;
719
720 if (dnd_get_item(dnd, x, y) || dnd->self_only || !has_flower) {
721 input_accept(input, NULL);
722 } else {
723 input_accept(input, flower_mime_type);
724 }
725 }
726
727 static void
dnd_receive_func(void * data,size_t len,int32_t x,int32_t y,void * user_data)728 dnd_receive_func(void *data, size_t len, int32_t x, int32_t y, void *user_data)
729 {
730 struct dnd *dnd = user_data;
731 struct dnd_flower_message *message = data;
732 struct item *item;
733 struct rectangle allocation;
734
735 if (len == 0) {
736 return;
737 } else if (len != sizeof *message) {
738 fprintf(stderr, "odd message length %zu, expected %zu\n",
739 len, sizeof *message);
740 return;
741 }
742
743 widget_get_allocation(dnd->widget, &allocation);
744 item = item_create(dnd->display,
745 x - message->x_offset - allocation.x,
746 y - message->y_offset - allocation.y,
747 message->seed);
748
749 dnd_add_item(dnd, item);
750 update_pointer_images_except(dnd, NULL);
751 window_schedule_redraw(dnd->window);
752 }
753
754 static void
dnd_drop_handler(struct window * window,struct input * input,int32_t x,int32_t y,void * data)755 dnd_drop_handler(struct window *window, struct input *input,
756 int32_t x, int32_t y, void *data)
757 {
758 struct dnd *dnd = data;
759 struct dnd_flower_message message;
760
761 if (dnd_get_item(dnd, x, y)) {
762 fprintf(stderr, "got 'drop', but no target\n");
763 return;
764 }
765
766 if (!dnd->self_only) {
767 input_receive_drag_data(input,
768 flower_mime_type,
769 dnd_receive_func, dnd);
770 } else if (dnd->current_drag) {
771 message.seed = dnd->current_drag->item->seed;
772 message.x_offset = dnd->current_drag->x_offset;
773 message.y_offset = dnd->current_drag->y_offset;
774 dnd_receive_func(&message, sizeof message, x, y, dnd);
775 } else {
776 fprintf(stderr, "ignoring drop from another client\n");
777 }
778 }
779
780 static struct dnd *
dnd_create(struct display * display)781 dnd_create(struct display *display)
782 {
783 struct dnd *dnd;
784 int x, y;
785 int32_t width, height;
786 unsigned int i;
787
788 dnd = xzalloc(sizeof *dnd);
789 dnd->window = window_create(display);
790 dnd->widget = window_frame_create(dnd->window, dnd);
791 window_set_title(dnd->window, "Wayland Drag and Drop Demo");
792
793 dnd->display = display;
794 dnd->key = 100;
795
796 wl_list_init(&dnd->pointers);
797
798 for (i = 0; i < ARRAY_LENGTH(dnd->items); i++) {
799 x = (i % 4) * (item_width + item_padding) + item_padding;
800 y = (i / 4) * (item_height + item_padding) + item_padding;
801 if ((i ^ (i >> 2)) & 1)
802 dnd->items[i] = item_create(display, x, y, 0);
803 else
804 dnd->items[i] = NULL;
805 }
806
807 window_set_user_data(dnd->window, dnd);
808 window_set_keyboard_focus_handler(dnd->window,
809 keyboard_focus_handler);
810 window_set_data_handler(dnd->window, dnd_data_handler);
811 window_set_drop_handler(dnd->window, dnd_drop_handler);
812
813 widget_set_redraw_handler(dnd->widget, dnd_redraw_handler);
814 widget_set_enter_handler(dnd->widget, dnd_enter_handler);
815 widget_set_leave_handler(dnd->widget, dnd_leave_handler);
816 widget_set_motion_handler(dnd->widget, dnd_motion_handler);
817 widget_set_button_handler(dnd->widget, dnd_button_handler);
818 widget_set_touch_down_handler(dnd->widget, dnd_touch_down_handler);
819
820 width = 4 * (item_width + item_padding) + item_padding;
821 height = 4 * (item_height + item_padding) + item_padding;
822
823 window_frame_set_child_size(dnd->widget, width, height);
824
825 return dnd;
826 }
827
828 static void
dnd_destroy(struct dnd * dnd)829 dnd_destroy(struct dnd *dnd)
830 {
831 widget_destroy(dnd->widget);
832 window_destroy(dnd->window);
833 free(dnd);
834 }
835
836 int
main(int argc,char * argv[])837 main(int argc, char *argv[])
838 {
839 struct display *d;
840 struct dnd *dnd;
841 int self_only = 0;
842
843 if (argc == 2 && !strcmp(argv[1], "--self-only"))
844 self_only = 1;
845 else if (argc > 1) {
846 printf("Usage: %s [OPTIONS]\n --self-only\n", argv[0]);
847 return 1;
848 }
849
850 d = display_create(&argc, argv);
851 if (d == NULL) {
852 fprintf(stderr, "failed to create display: %s\n",
853 strerror(errno));
854 return -1;
855 }
856
857 dnd = dnd_create(d);
858 if (self_only)
859 dnd->self_only = 1;
860
861 display_run(d);
862
863 dnd_destroy(dnd);
864 display_destroy(d);
865
866 return 0;
867 }
868