data-device.c 36.8 KB
Newer Older
1
2
3
/*
 * Copyright © 2011 Kristian Høgsberg
 *
4
5
6
7
8
9
10
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
11
 *
12
13
14
15
16
17
18
19
20
21
22
23
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
24
25
 */

26
27
#include "config.h"

28
29
30
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
31
#include <stdint.h>
32
#include <stdio.h>
33
#include <assert.h>
34

35
#include <libweston/libweston.h>
36
#include "shared/helpers.h"
37
#include "shared/timespec-util.h"
38

39
40
struct weston_drag {
	struct wl_client *client;
41
	struct weston_data_source *data_source;
42
	struct wl_listener data_source_listener;
43
	struct weston_view *focus;
44
45
	struct wl_resource *focus_resource;
	struct wl_listener focus_listener;
46
	struct weston_view *icon;
47
	struct wl_listener icon_destroy_listener;
48
	int32_t dx, dy;
49
	struct weston_keyboard_grab keyboard_grab;
50
51
};

52
53
54
55
56
57
58
59
60
61
struct weston_pointer_drag {
	struct weston_drag  base;
	struct weston_pointer_grab grab;
};

struct weston_touch_drag {
	struct weston_drag base;
	struct weston_touch_grab grab;
};

62
63
64
65
#define ALL_ACTIONS (WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY | \
		     WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE | \
		     WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)

66
67
68
69
static void
data_offer_accept(struct wl_client *client, struct wl_resource *resource,
		  uint32_t serial, const char *mime_type)
{
70
	struct weston_data_offer *offer = wl_resource_get_user_data(resource);
71

72
73
74
75
	/* Protect against untimely calls from older data offers */
	if (!offer->source || offer != offer->source->offer)
		return;

76
77
78
79
	/* FIXME: Check that client is currently focused by the input
	 * device that is currently dragging this data source.  Should
	 * this be a wl_data_device request? */

80
81
	offer->source->accept(offer->source, serial, mime_type);
	offer->source->accepted = mime_type != NULL;
82
83
84
85
86
87
}

static void
data_offer_receive(struct wl_client *client, struct wl_resource *resource,
		   const char *mime_type, int32_t fd)
{
88
	struct weston_data_offer *offer = wl_resource_get_user_data(resource);
89

90
	if (offer->source && offer == offer->source->offer)
91
92
93
94
95
96
97
98
99
100
101
		offer->source->send(offer->source, mime_type, fd);
	else
		close(fd);
}

static void
data_offer_destroy(struct wl_client *client, struct wl_resource *resource)
{
	wl_resource_destroy(resource);
}

102
103
104
static void
data_source_notify_finish(struct weston_data_source *source)
{
105
106
107
	if (!source->actions_set)
		return;

108
109
110
111
112
113
114
	if (source->offer->in_ask &&
	    wl_resource_get_version(source->resource) >=
	    WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
		wl_data_source_send_action(source->resource,
					   source->current_dnd_action);
	}

115
116
117
118
119
120
121
122
	if (wl_resource_get_version(source->resource) >=
	    WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
		wl_data_source_send_dnd_finished(source->resource);
	}

	source->offer = NULL;
}

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
static uint32_t
data_offer_choose_action(struct weston_data_offer *offer)
{
	uint32_t available_actions, preferred_action = 0;
	uint32_t source_actions, offer_actions;

	if (wl_resource_get_version(offer->resource) >=
	    WL_DATA_OFFER_ACTION_SINCE_VERSION) {
		offer_actions = offer->dnd_actions;
		preferred_action = offer->preferred_dnd_action;
	} else {
		offer_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
	}

	if (wl_resource_get_version(offer->source->resource) >=
	    WL_DATA_SOURCE_ACTION_SINCE_VERSION)
		source_actions = offer->source->dnd_actions;
	else
		source_actions = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;

	available_actions = offer_actions & source_actions;

	if (!available_actions)
		return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;

148
149
150
151
	if (offer->source->seat &&
	    offer->source->compositor_action & available_actions)
		return offer->source->compositor_action;

152
153
154
155
156
157
158
159
160
161
162
163
164
	/* If the dest side has a preferred DnD action, use it */
	if ((preferred_action & available_actions) != 0)
		return preferred_action;

	/* Use the first found action, in bit order */
	return 1 << (ffs(available_actions) - 1);
}

static void
data_offer_update_action(struct weston_data_offer *offer)
{
	uint32_t action;

165
	if (!offer->source)
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
		return;

	action = data_offer_choose_action(offer);

	if (offer->source->current_dnd_action == action)
		return;

	offer->source->current_dnd_action = action;

	if (offer->in_ask)
		return;

	if (wl_resource_get_version(offer->source->resource) >=
	    WL_DATA_SOURCE_ACTION_SINCE_VERSION)
		wl_data_source_send_action(offer->source->resource, action);

	if (wl_resource_get_version(offer->resource) >=
	    WL_DATA_OFFER_ACTION_SINCE_VERSION)
		wl_data_offer_send_action(offer->resource, action);
}

static void
data_offer_set_actions(struct wl_client *client,
		       struct wl_resource *resource,
		       uint32_t dnd_actions, uint32_t preferred_action)
{
	struct weston_data_offer *offer = wl_resource_get_user_data(resource);

	if (dnd_actions & ~ALL_ACTIONS) {
		wl_resource_post_error(offer->resource,
				       WL_DATA_OFFER_ERROR_INVALID_ACTION_MASK,
				       "invalid action mask %x", dnd_actions);
		return;
	}

	if (preferred_action &&
	    (!(preferred_action & dnd_actions) ||
	     __builtin_popcount(preferred_action) > 1)) {
		wl_resource_post_error(offer->resource,
				       WL_DATA_OFFER_ERROR_INVALID_ACTION,
				       "invalid action %x", preferred_action);
		return;
	}

	offer->dnd_actions = dnd_actions;
	offer->preferred_dnd_action = preferred_action;
	data_offer_update_action(offer);
}

215
216
217
218
219
220
221
222
static void
data_offer_finish(struct wl_client *client, struct wl_resource *resource)
{
	struct weston_data_offer *offer = wl_resource_get_user_data(resource);

	if (!offer->source || offer->source->offer != offer)
		return;

223
224
225
226
227
228
229
	if (offer->source->set_selection) {
		wl_resource_post_error(offer->resource,
				       WL_DATA_OFFER_ERROR_INVALID_FINISH,
				       "finish only valid for drag n drop");
		return;
	}

230
231
232
233
234
235
236
237
238
239
240
	/* Disallow finish while we have a grab driving drag-and-drop, or
	 * if the negotiation is not at the right stage
	 */
	if (offer->source->seat ||
	    !offer->source->accepted) {
		wl_resource_post_error(offer->resource,
				       WL_DATA_OFFER_ERROR_INVALID_FINISH,
				       "premature finish request");
		return;
	}

241
242
243
244
245
246
247
248
249
250
251
	switch (offer->source->current_dnd_action) {
	case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE:
	case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK:
		wl_resource_post_error(offer->resource,
				       WL_DATA_OFFER_ERROR_INVALID_OFFER,
				       "offer finished with an invalid action");
		return;
	default:
		break;
	}

252
253
254
	data_source_notify_finish(offer->source);
}

255
256
257
258
static const struct wl_data_offer_interface data_offer_interface = {
	data_offer_accept,
	data_offer_receive,
	data_offer_destroy,
259
	data_offer_finish,
260
	data_offer_set_actions,
261
262
263
264
265
};

static void
destroy_data_offer(struct wl_resource *resource)
{
266
	struct weston_data_offer *offer = wl_resource_get_user_data(resource);
267

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	if (!offer->source)
		goto out;

	wl_list_remove(&offer->source_destroy_listener.link);

	if (offer->source->offer != offer)
		goto out;

	/* If the drag destination has version < 3, wl_data_offer.finish
	 * won't be called, so do this here as a safety net, because
	 * we still want the version >=3 drag source to be happy.
	 */
	if (wl_resource_get_version(offer->resource) <
	    WL_DATA_OFFER_ACTION_SINCE_VERSION) {
		data_source_notify_finish(offer->source);
283
284
	} else if (offer->source->resource &&
		   wl_resource_get_version(offer->source->resource) >=
285
286
287
288
289
290
		   WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
		wl_data_source_send_cancelled(offer->source->resource);
	}

	offer->source->offer = NULL;
out:
291
292
293
294
295
296
	free(offer);
}

static void
destroy_offer_data_source(struct wl_listener *listener, void *data)
{
297
	struct weston_data_offer *offer;
298

299
	offer = container_of(listener, struct weston_data_offer,
300
301
302
303
304
			     source_destroy_listener);

	offer->source = NULL;
}

305
static struct weston_data_offer *
306
307
weston_data_source_send_offer(struct weston_data_source *source,
			      struct wl_resource *target)
308
{
309
	struct weston_data_offer *offer;
310
311
312
313
314
315
	char **p;

	offer = malloc(sizeof *offer);
	if (offer == NULL)
		return NULL;

316
317
	offer->resource =
		wl_resource_create(wl_resource_get_client(target),
318
319
				   &wl_data_offer_interface,
				   wl_resource_get_version(target), 0);
320
321
322
323
324
	if (offer->resource == NULL) {
		free(offer);
		return NULL;
	}

325
326
	wl_resource_set_implementation(offer->resource, &data_offer_interface,
				       offer, destroy_data_offer);
327

328
329
330
	offer->in_ask = false;
	offer->dnd_actions = 0;
	offer->preferred_dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
331
332
	offer->source = source;
	offer->source_destroy_listener.notify = destroy_offer_data_source;
333
	wl_signal_add(&source->destroy_signal,
334
335
		      &offer->source_destroy_listener);

336
	wl_data_device_send_data_offer(target, offer->resource);
337
338

	wl_array_for_each(p, &source->mime_types)
339
		wl_data_offer_send_offer(offer->resource, *p);
340

341
342
343
	source->offer = offer;
	source->accepted = false;

344
	return offer;
345
346
347
348
349
350
351
}

static void
data_source_offer(struct wl_client *client,
		  struct wl_resource *resource,
		  const char *type)
{
352
353
	struct weston_data_source *source =
		wl_resource_get_user_data(resource);
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
	char **p;

	p = wl_array_add(&source->mime_types, sizeof *p);
	if (p)
		*p = strdup(type);
	if (!p || !*p)
		wl_resource_post_no_memory(resource);
}

static void
data_source_destroy(struct wl_client *client, struct wl_resource *resource)
{
	wl_resource_destroy(resource);
}

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
static void
data_source_set_actions(struct wl_client *client,
			struct wl_resource *resource,
			uint32_t dnd_actions)
{
	struct weston_data_source *source =
		wl_resource_get_user_data(resource);

	if (source->actions_set) {
		wl_resource_post_error(source->resource,
				       WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
				       "cannot set actions more than once");
		return;
	}

	if (dnd_actions & ~ALL_ACTIONS) {
		wl_resource_post_error(source->resource,
				       WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
				       "invalid action mask %x", dnd_actions);
		return;
	}

	if (source->seat) {
		wl_resource_post_error(source->resource,
				       WL_DATA_SOURCE_ERROR_INVALID_ACTION_MASK,
				       "invalid action change after "
				       "wl_data_device.start_drag");
		return;
	}

	source->dnd_actions = dnd_actions;
	source->actions_set = true;
}

403
404
static struct wl_data_source_interface data_source_interface = {
	data_source_offer,
405
406
	data_source_destroy,
	data_source_set_actions
407
408
};

409
static void
410
drag_surface_configure(struct weston_drag *drag,
411
412
413
414
		       struct weston_pointer *pointer,
		       struct weston_touch *touch,
		       struct weston_surface *es,
		       int32_t sx, int32_t sy)
415
{
416
	struct weston_layer_entry *list;
417
	float fx, fy;
418

419
420
	assert((pointer != NULL && touch == NULL) ||
			(pointer == NULL && touch != NULL));
421

422
	if (!weston_surface_is_mapped(es) && es->buffer_ref.buffer) {
423
424
		if (pointer && pointer->sprite &&
			weston_view_is_mapped(pointer->sprite))
425
			list = &pointer->sprite->layer_link;
426
		else
427
			list = &es->compositor->cursor_layer.view_list;
428

429
430
		weston_layer_entry_remove(&drag->icon->layer_link);
		weston_layer_entry_insert(list, &drag->icon->layer_link);
431
		weston_view_update_transform(drag->icon);
432
		pixman_region32_clear(&es->pending.input);
433
434
		es->is_mapped = true;
		drag->icon->is_mapped = true;
435
	}
436

437
438
	drag->dx += sx;
	drag->dy += sy;
439

440
441
442
443
444
445
446
447
448
	/* init to 0 for avoiding a compile warning */
	fx = fy = 0;
	if (pointer) {
		fx = wl_fixed_to_double(pointer->x) + drag->dx;
		fy = wl_fixed_to_double(pointer->y) + drag->dy;
	} else if (touch) {
		fx = wl_fixed_to_double(touch->grab_x) + drag->dx;
		fy = wl_fixed_to_double(touch->grab_y) + drag->dy;
	}
449
	weston_view_set_position(drag->icon, fx, fy);
450
451
}

452
453
454
455
456
457
458
static int
pointer_drag_surface_get_label(struct weston_surface *surface,
			       char *buf, size_t len)
{
	return snprintf(buf, len, "pointer drag icon");
}

459
static void
460
pointer_drag_surface_committed(struct weston_surface *es,
461
			       int32_t sx, int32_t sy)
462
{
463
	struct weston_pointer_drag *drag = es->committed_private;
464
465
	struct weston_pointer *pointer = drag->grab.pointer;

466
	assert(es->committed == pointer_drag_surface_committed);
467

468
	drag_surface_configure(&drag->base, pointer, NULL, es, sx, sy);
469
470
}

471
472
473
474
475
476
477
static int
touch_drag_surface_get_label(struct weston_surface *surface,
			     char *buf, size_t len)
{
	return snprintf(buf, len, "touch drag icon");
}

478
static void
479
touch_drag_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
480
{
481
	struct weston_touch_drag *drag = es->committed_private;
482
483
	struct weston_touch *touch = drag->grab.touch;

484
	assert(es->committed == touch_drag_surface_committed);
485

486
	drag_surface_configure(&drag->base, NULL, touch, es, sx, sy);
487
488
}

489
490
491
static void
destroy_drag_focus(struct wl_listener *listener, void *data)
{
492
493
	struct weston_drag *drag =
		container_of(listener, struct weston_drag, focus_listener);
494

495
	drag->focus_resource = NULL;
496
497
498
}

static void
499
500
501
502
weston_drag_set_focus(struct weston_drag *drag,
			struct weston_seat *seat,
			struct weston_view *view,
			wl_fixed_t sx, wl_fixed_t sy)
503
{
504
	struct wl_resource *resource, *offer_resource = NULL;
505
	struct wl_display *display = seat->compositor->wl_display;
506
	struct weston_data_offer *offer;
507
508
	uint32_t serial;

509
510
511
512
513
	if (drag->focus && view && drag->focus->surface == view->surface) {
		drag->focus = view;
		return;
	}

514
515
516
517
518
	if (drag->focus_resource) {
		wl_data_device_send_leave(drag->focus_resource);
		wl_list_remove(&drag->focus_listener.link);
		drag->focus_resource = NULL;
		drag->focus = NULL;
519
520
	}

521
	if (!view || !view->surface->resource)
522
523
		return;

524
	if (!drag->data_source &&
525
	    wl_resource_get_client(view->surface->resource) != drag->client)
526
527
		return;

528
529
530
531
532
533
534
535
536
	if (drag->data_source &&
	    drag->data_source->offer) {
		/* Unlink the offer from the source */
		offer = drag->data_source->offer;
		offer->source = NULL;
		drag->data_source->offer = NULL;
		wl_list_remove(&offer->source_destroy_listener.link);
	}

537
	resource = wl_resource_find_for_client(&seat->drag_resource_list,
538
					       wl_resource_get_client(view->surface->resource));
539
540
541
542
543
	if (!resource)
		return;

	serial = wl_display_next_serial(display);

544
	if (drag->data_source) {
545
		drag->data_source->accepted = false;
546
547
		offer = weston_data_source_send_offer(drag->data_source, resource);
		if (offer == NULL)
548
			return;
549

550
551
552
		data_offer_update_action(offer);

		offer_resource = offer->resource;
553
554
555
556
557
		if (wl_resource_get_version (offer_resource) >=
		    WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
			wl_data_offer_send_source_actions (offer_resource,
			                                   drag->data_source->dnd_actions);
		}
558
	}
559

560
	wl_data_device_send_enter(resource, serial, view->surface->resource,
561
				  sx, sy, offer_resource);
562

563
	drag->focus = view;
564
	drag->focus_listener.notify = destroy_drag_focus;
565
	wl_resource_add_destroy_listener(resource, &drag->focus_listener);
566
	drag->focus_resource = resource;
567
568
}

569
570
571
static void
drag_grab_focus(struct weston_pointer_grab *grab)
{
572
573
	struct weston_pointer_drag *drag =
		container_of(grab, struct weston_pointer_drag, grab);
574
	struct weston_pointer *pointer = grab->pointer;
575
	struct weston_view *view;
576
577
	wl_fixed_t sx, sy;

578
579
580
	view = weston_compositor_pick_view(pointer->seat->compositor,
					   pointer->x, pointer->y,
					   &sx, &sy);
581
582
	if (drag->base.focus != view)
		weston_drag_set_focus(&drag->base, pointer->seat, view, sx, sy);
583
584
}

585
static void
586
587
drag_grab_motion(struct weston_pointer_grab *grab,
		 const struct timespec *time,
588
		 struct weston_pointer_motion_event *event)
589
{
590
591
	struct weston_pointer_drag *drag =
		container_of(grab, struct weston_pointer_drag, grab);
592
	struct weston_pointer *pointer = drag->grab.pointer;
593
	float fx, fy;
594
	wl_fixed_t sx, sy;
595
	uint32_t msecs;
596

597
	weston_pointer_move(pointer, event);
598

599
600
601
602
603
	if (drag->base.icon) {
		fx = wl_fixed_to_double(pointer->x) + drag->base.dx;
		fy = wl_fixed_to_double(pointer->y) + drag->base.dy;
		weston_view_set_position(drag->base.icon, fx, fy);
		weston_view_schedule_repaint(drag->base.icon);
604
	}
605

606
	if (drag->base.focus_resource) {
607
		msecs = timespec_to_msec(time);
608
		weston_view_from_global_fixed(drag->base.focus,
609
610
					      pointer->x, pointer->y,
					      &sx, &sy);
611

612
		wl_data_device_send_motion(drag->base.focus_resource, msecs, sx, sy);
613
	}
614
615
616
}

static void
617
618
data_device_end_drag_grab(struct weston_drag *drag,
		struct weston_seat *seat)
619
{
620
	if (drag->icon) {
621
622
		if (weston_view_is_mapped(drag->icon))
			weston_view_unmap(drag->icon);
623

624
		drag->icon->surface->committed = NULL;
625
		weston_surface_set_label_func(drag->icon->surface, NULL);
626
		pixman_region32_clear(&drag->icon->surface->pending.input);
627
		wl_list_remove(&drag->icon_destroy_listener.link);
628
		weston_view_destroy(drag->icon);
629
	}
630

631
632
	weston_drag_set_focus(drag, seat, NULL, 0, 0);
}
633

634
635
636
637
static void
data_device_end_pointer_drag_grab(struct weston_pointer_drag *drag)
{
	struct weston_pointer *pointer = drag->grab.pointer;
638
	struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
639

640
641
	data_device_end_drag_grab(&drag->base, pointer->seat);
	weston_pointer_end_grab(pointer);
642
	weston_keyboard_end_grab(keyboard);
643
	free(drag);
644
645
646
}

static void
647
drag_grab_button(struct weston_pointer_grab *grab,
648
649
		 const struct timespec *time,
		 uint32_t button, uint32_t state_w)
650
{
651
652
	struct weston_pointer_drag *drag =
		container_of(grab, struct weston_pointer_drag, grab);
653
	struct weston_pointer *pointer = drag->grab.pointer;
654
	enum wl_pointer_button_state state = state_w;
655
	struct weston_data_source *data_source = drag->base.data_source;
656

657
	if (data_source &&
658
	    pointer->grab_button == button &&
659
660
	    state == WL_POINTER_BUTTON_STATE_RELEASED) {
		if (drag->base.focus_resource &&
661
662
		    data_source->accepted &&
		    data_source->current_dnd_action) {
663
664
665
666
667
668
			wl_data_device_send_drop(drag->base.focus_resource);

			if (wl_resource_get_version(data_source->resource) >=
			    WL_DATA_SOURCE_DND_DROP_PERFORMED_SINCE_VERSION)
				wl_data_source_send_dnd_drop_performed(data_source->resource);

669
670
671
672
			data_source->offer->in_ask =
				data_source->current_dnd_action ==
				WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;

673
674
675
676
677
678
			data_source->seat = NULL;
		} else if (wl_resource_get_version(data_source->resource) >=
			   WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
			wl_data_source_send_cancelled(data_source->resource);
		}
	}
679

680
	if (pointer->button_count == 0 &&
681
	    state == WL_POINTER_BUTTON_STATE_RELEASED) {
682
683
684
		if (drag->base.data_source)
			wl_list_remove(&drag->base.data_source_listener.link);
		data_device_end_pointer_drag_grab(drag);
685
686
687
	}
}

688
689
static void
drag_grab_axis(struct weston_pointer_grab *grab,
690
691
	       const struct timespec *time,
	       struct weston_pointer_axis_event *event)
692
693
694
{
}

695
696
697
698
699
700
701
702
703
704
static void
drag_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
{
}

static void
drag_grab_frame(struct weston_pointer_grab *grab)
{
}

705
706
707
static void
drag_grab_cancel(struct weston_pointer_grab *grab)
{
708
709
	struct weston_pointer_drag *drag =
		container_of(grab, struct weston_pointer_drag, grab);
710

711
712
	if (drag->base.data_source)
		wl_list_remove(&drag->base.data_source_listener.link);
713

714
	data_device_end_pointer_drag_grab(drag);
715
716
}

717
static const struct weston_pointer_grab_interface pointer_drag_grab_interface = {
718
719
720
	drag_grab_focus,
	drag_grab_motion,
	drag_grab_button,
721
	drag_grab_axis,
722
723
	drag_grab_axis_source,
	drag_grab_frame,
724
	drag_grab_cancel,
725
726
727
};

static void
728
729
730
drag_grab_touch_down(struct weston_touch_grab *grab,
		     const struct timespec *time, int touch_id,
		     wl_fixed_t sx, wl_fixed_t sy)
731
{
732
733
734
735
736
737
}

static void
data_device_end_touch_drag_grab(struct weston_touch_drag *drag)
{
	struct weston_touch *touch = drag->grab.touch;
738
	struct weston_keyboard *keyboard = drag->base.keyboard_grab.keyboard;
739
740
741

	data_device_end_drag_grab(&drag->base, touch->seat);
	weston_touch_end_grab(touch);
742
	weston_keyboard_end_grab(keyboard);
743
744
745
746
747
	free(drag);
}

static void
drag_grab_touch_up(struct weston_touch_grab *grab,
748
		   const struct timespec *time, int touch_id)
749
750
751
752
753
754
755
756
757
758
759
760
761
762
{
	struct weston_touch_drag *touch_drag =
		container_of(grab, struct weston_touch_drag, grab);
	struct weston_touch *touch = grab->touch;

	if (touch_id != touch->grab_touch_id)
		return;

	if (touch_drag->base.focus_resource)
		wl_data_device_send_drop(touch_drag->base.focus_resource);
	if (touch_drag->base.data_source)
		wl_list_remove(&touch_drag->base.data_source_listener.link);
	data_device_end_touch_drag_grab(touch_drag);
}
763

764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
static void
drag_grab_touch_focus(struct weston_touch_drag *drag)
{
	struct weston_touch *touch = drag->grab.touch;
	struct weston_view *view;
	wl_fixed_t view_x, view_y;

	view = weston_compositor_pick_view(touch->seat->compositor,
				touch->grab_x, touch->grab_y,
				&view_x, &view_y);
	if (drag->base.focus != view)
		weston_drag_set_focus(&drag->base, touch->seat,
				view, view_x, view_y);
}

static void
780
781
782
drag_grab_touch_motion(struct weston_touch_grab *grab,
		       const struct timespec *time,
		       int touch_id, wl_fixed_t x, wl_fixed_t y)
783
784
785
786
787
788
{
	struct weston_touch_drag *touch_drag =
		container_of(grab, struct weston_touch_drag, grab);
	struct weston_touch *touch = grab->touch;
	wl_fixed_t view_x, view_y;
	float fx, fy;
789
	uint32_t msecs;
790
791
792
793
794
795
796
797
798
799
800
801
802

	if (touch_id != touch->grab_touch_id)
		return;

	drag_grab_touch_focus(touch_drag);
	if (touch_drag->base.icon) {
		fx = wl_fixed_to_double(touch->grab_x) + touch_drag->base.dx;
		fy = wl_fixed_to_double(touch->grab_y) + touch_drag->base.dy;
		weston_view_set_position(touch_drag->base.icon, fx, fy);
		weston_view_schedule_repaint(touch_drag->base.icon);
	}

	if (touch_drag->base.focus_resource) {
803
		msecs = timespec_to_msec(time);
804
805
806
		weston_view_from_global_fixed(touch_drag->base.focus,
					touch->grab_x, touch->grab_y,
					&view_x, &view_y);
807
808
		wl_data_device_send_motion(touch_drag->base.focus_resource,
					   msecs, view_x, view_y);
809
810
811
	}
}

812
813
814
815
816
static void
drag_grab_touch_frame(struct weston_touch_grab *grab)
{
}

817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
static void
drag_grab_touch_cancel(struct weston_touch_grab *grab)
{
	struct weston_touch_drag *touch_drag =
		container_of(grab, struct weston_touch_drag, grab);

	if (touch_drag->base.data_source)
		wl_list_remove(&touch_drag->base.data_source_listener.link);
	data_device_end_touch_drag_grab(touch_drag);
}

static const struct weston_touch_grab_interface touch_drag_grab_interface = {
	drag_grab_touch_down,
	drag_grab_touch_up,
	drag_grab_touch_motion,
832
	drag_grab_touch_frame,
833
834
835
	drag_grab_touch_cancel
};

836
837
static void
drag_grab_keyboard_key(struct weston_keyboard_grab *grab,
838
		       const struct timespec *time, uint32_t key, uint32_t state)
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
{
}

static void
drag_grab_keyboard_modifiers(struct weston_keyboard_grab *grab,
			     uint32_t serial, uint32_t mods_depressed,
			     uint32_t mods_latched,
			     uint32_t mods_locked, uint32_t group)
{
	struct weston_keyboard *keyboard = grab->keyboard;
	struct weston_drag *drag =
		container_of(grab, struct weston_drag, keyboard_grab);
	uint32_t compositor_action;

	if (mods_depressed & (1 << keyboard->xkb_info->shift_mod))
		compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
	else if (mods_depressed & (1 << keyboard->xkb_info->ctrl_mod))
		compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
	else
		compositor_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;

	drag->data_source->compositor_action = compositor_action;

	if (drag->data_source->offer)
		data_offer_update_action(drag->data_source->offer);
}

static void
drag_grab_keyboard_cancel(struct weston_keyboard_grab *grab)
{
	struct weston_drag *drag =
		container_of(grab, struct weston_drag, keyboard_grab);
	struct weston_pointer *pointer = grab->keyboard->seat->pointer_state;
	struct weston_touch *touch = grab->keyboard->seat->touch_state;

	if (pointer && pointer->grab->interface == &pointer_drag_grab_interface) {
		struct weston_touch_drag *touch_drag =
			(struct weston_touch_drag *) drag;
		drag_grab_touch_cancel(&touch_drag->grab);
	} else if (touch && touch->grab->interface == &touch_drag_grab_interface) {
		struct weston_pointer_drag *pointer_drag =
			(struct weston_pointer_drag *) drag;
		drag_grab_cancel(&pointer_drag->grab);
	}
}

static const struct weston_keyboard_grab_interface keyboard_drag_grab_interface = {
	drag_grab_keyboard_key,
	drag_grab_keyboard_modifiers,
	drag_grab_keyboard_cancel
};

891
892
893
894
895
896
897
static void
destroy_pointer_data_device_source(struct wl_listener *listener, void *data)
{
	struct weston_pointer_drag *drag = container_of(listener,
			struct weston_pointer_drag, base.data_source_listener);

	data_device_end_pointer_drag_grab(drag);
898
899
}

900
static void
901
handle_drag_icon_destroy(struct wl_listener *listener, void *data)
902
{
903
	struct weston_drag *drag = container_of(listener, struct weston_drag,
904
						icon_destroy_listener);
905

906
	drag->icon = NULL;
907
908
}

909
WL_EXPORT int
910
weston_pointer_start_drag(struct weston_pointer *pointer,
911
912
913
914
		       struct weston_data_source *source,
		       struct weston_surface *icon,
		       struct wl_client *client)
{
915
	struct weston_pointer_drag *drag;
916
917
	struct weston_keyboard *keyboard =
		weston_seat_get_keyboard(pointer->seat);
918
919
920
921
922

	drag = zalloc(sizeof *drag);
	if (drag == NULL)
		return -1;

923
	drag->grab.interface = &pointer_drag_grab_interface;
924
	drag->base.keyboard_grab.interface = &keyboard_drag_grab_interface;
925
926
	drag->base.client = client;
	drag->base.data_source = source;
927
928

	if (icon) {
929
930
		drag->base.icon = weston_view_create(icon);
		if (drag->base.icon == NULL) {
931
932
933
934
			free(drag);
			return -1;
		}

935
		drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
936
		wl_signal_add(&icon->destroy_signal,
937
			      &drag->base.icon_destroy_listener);
938

939
940
		icon->committed = pointer_drag_surface_committed;
		icon->committed_private = drag;
941
942
		weston_surface_set_label_func(icon,
					pointer_drag_surface_get_label);
943
	} else {
944
		drag->base.icon = NULL;
945
946
947
	}

	if (source) {
948
		drag->base.data_source_listener.notify = destroy_pointer_data_device_source;
949
		wl_signal_add(&source->destroy_signal,
950
			      &drag->base.data_source_listener);
951
952
	}

953
	weston_pointer_clear_focus(pointer);
954
955
	weston_keyboard_set_focus(keyboard, NULL);

956
	weston_pointer_start_grab(pointer, &drag->grab);
957
	weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977

	return 0;
}

static void
destroy_touch_data_device_source(struct wl_listener *listener, void *data)
{
	struct weston_touch_drag *drag = container_of(listener,
			struct weston_touch_drag, base.data_source_listener);

	data_device_end_touch_drag_grab(drag);
}

WL_EXPORT int
weston_touch_start_drag(struct weston_touch *touch,
		       struct weston_data_source *source,
		       struct weston_surface *icon,
		       struct wl_client *client)
{
	struct weston_touch_drag *drag;
978
979
	struct weston_keyboard *keyboard =
		weston_seat_get_keyboard(touch->seat);
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999

	drag = zalloc(sizeof *drag);
	if (drag == NULL)
		return -1;

	drag->grab.interface = &touch_drag_grab_interface;
	drag->base.client = client;
	drag->base.data_source = source;

	if (icon) {
		drag->base.icon = weston_view_create(icon);
		if (drag->base.icon == NULL) {
			free(drag);
			return -1;
		}

		drag->base.icon_destroy_listener.notify = handle_drag_icon_destroy;
		wl_signal_add(&icon->destroy_signal,
			      &drag->base.icon_destroy_listener);

1000
1001
		icon->committed = touch_drag_surface_committed;
		icon->committed_private = drag;
1002
1003
		weston_surface_set_label_func(icon,
					touch_drag_surface_get_label);
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
	} else {
		drag->base.icon = NULL;
	}

	if (source) {
		drag->base.data_source_listener.notify = destroy_touch_data_device_source;
		wl_signal_add(&source->destroy_signal,
			      &drag->base.data_source_listener);
	}

1014
1015
	weston_keyboard_set_focus(keyboard, NULL);

1016
	weston_touch_start_grab(touch, &drag->grab);
1017
	weston_keyboard_start_grab(keyboard, &drag->base.keyboard_grab);
1018
1019

	drag_grab_touch_focus(drag);
1020
1021

	return 0;
1022
1023
}

1024
1025
1026
1027
1028
1029
static void
data_device_start_drag(struct wl_client *client, struct wl_resource *resource,
		       struct wl_resource *source_resource,
		       struct wl_resource *origin_resource,
		       struct wl_resource *icon_resource, uint32_t serial)
{
1030
	struct weston_seat *seat = wl_resource_get_user_data(resource);
1031
1032
	struct weston_pointer *pointer = weston_seat_get_pointer(seat);
	struct weston_touch *touch = weston_seat_get_touch(seat);
1033
	struct weston_surface *origin = wl_resource_get_user_data(origin_resource);
1034
	struct weston_data_source *source = NULL;
1035
	struct weston_surface *icon = NULL;
1036
	int is_pointer_grab, is_touch_grab;
1037
	int32_t ret = 0;
1038

1039
1040
1041
1042
1043
	is_pointer_grab = pointer &&
			  pointer->button_count == 1 &&
			  pointer->grab_serial == serial &&
			  pointer->focus &&
			  pointer->focus->surface == origin;
1044

1045
1046
1047
1048
1049
	is_touch_grab = touch &&
			touch->num_tp == 1 &&
			touch->grab_serial == serial &&
			touch->focus &&
			touch->focus->surface == origin;
1050
1051

	if (!is_pointer_grab && !is_touch_grab)
1052
		return;
1053
1054
1055

	/* FIXME: Check that the data source type array isn't empty. */

1056
1057
	if (source_resource)
		source = wl_resource_get_user_data(source_resource);
1058
	if (icon_resource)
1059
		icon = wl_resource_get_user_data(icon_resource);
1060
1061
1062
1063
1064
1065

	if (icon) {
		if (weston_surface_set_role(icon, "wl_data_device-icon",
					    resource,
					    WL_DATA_DEVICE_ERROR_ROLE) < 0)
			return;
1066
1067
	}

1068
	if (is_pointer_grab)
1069
		ret = weston_pointer_start_drag(pointer, source, icon, client);
1070
	else if (is_touch_grab)
1071
		ret = weston_touch_start_drag(touch, source, icon, client);
1072
1073

	if (ret < 0)
1074
		wl_resource_post_no_memory(resource);
1075
1076
	else
		source->seat = seat;
1077
1078
1079
1080
1081
}

static void
destroy_selection_data_source(struct wl_listener *listener, void *data)
{
1082
1083
	struct weston_seat *seat = container_of(listener, struct weston_seat,
						selection_data_source_listener);
1084
	struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1085
	struct wl_resource *data_device;
1086
	struct weston_surface *focus = NULL;
1087
1088
1089

	seat->selection_data_source = NULL;

1090
1091
	if (keyboard)
		focus = keyboard->focus;
1092
	if (focus && focus->resource) {
1093
		data_device = wl_resource_find_for_client(&seat->drag_resource_list,
1094
							  wl_resource_get_client(focus->resource));
1095
1096
1097
1098
1099
1100
1101
		if (data_device)
			wl_data_device_send_selection(data_device, NULL);
	}

	wl_signal_emit(&seat->selection_signal, seat);
}

1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
/** \brief Send the selection to the specified client
 *
 * This function creates a new wl_data_offer if there is a wl_data_source
 * currently set as the selection and sends it to the specified client,
 * followed by the wl_data_device.selection() event.
 * If there is no current selection the wl_data_device.selection() event
 * will carry a NULL wl_data_offer.
 *
 * If the client does not have a wl_data_device for the specified seat
 * nothing will be done.
 *
 * \param seat The seat owning the wl_data_device used to send the events.
 * \param client The client to which to send the selection.
 */
WL_EXPORT void
weston_seat_send_selection(struct weston_seat *seat, struct wl_client *client)
{
1119
1120
	struct weston_data_offer *offer;
	struct wl_resource *data_device;
1121

1122
1123
1124
1125
1126
1127
	wl_resource_for_each(data_device, &seat->drag_resource_list) {
		if (wl_resource_get_client(data_device) != client)
		    continue;

		if (seat->selection_data_source) {
			offer = weston_data_source_send_offer(seat->selection_data_source,
1128
1129
							      data_device);
			wl_data_device_send_selection(data_device, offer->resource);
1130
1131
1132
		} else {
			wl_data_device_send_selection(data_device, NULL);
		}
1133
1134
1135
	}
}

1136
WL_EXPORT void
1137
weston_seat_set_selection(struct weston_seat *seat,
1138
			  struct weston_data_source *source, uint32_t serial)
1139
{
1140
	struct weston_surface *focus = NULL;
1141
	struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154

	if (seat->selection_data_source &&
	    seat->selection_serial - serial < UINT32_MAX / 2)
		return;

	if (seat->selection_data_source) {
		seat->selection_data_source->cancel(seat->selection_data_source);
		wl_list_remove(&seat->selection_data_source_listener.link);
		seat->selection_data_source = NULL;
	}

	seat->selection_data_source = source;
	seat->selection_serial = serial;
1155
	source->set_selection = true;
1156

1157
1158
	if (keyboard)
		focus = keyboard->focus;
1159
	if (focus && focus->resource) {
1160
		weston_seat_send_selection(seat, wl_resource_get_client(focus->resource));
1161
1162
1163
1164
1165
1166
1167
	}

	wl_signal_emit(&seat->selection_signal, seat);

	if (source) {
		seat->selection_data_source_listener.notify =
			destroy_selection_data_source;
1168
		wl_signal_add(&source->destroy_signal,
1169
1170
1171
1172
1173
1174
1175
1176
1177
			      &seat->selection_data_source_listener);
	}
}

static void
data_device_set_selection(struct wl_client *client,
			  struct wl_resource *resource,
			  struct wl_resource *source_resource, uint32_t serial)
{
1178
	struct weston_seat *seat = wl_resource_get_user_data(resource);