forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwl_data_device_manager.cc
432 lines (382 loc) · 16.2 KB
/
wl_data_device_manager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/exo/wayland/wl_data_device_manager.h"
#include <wayland-server-protocol-core.h>
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include "components/exo/data_device.h"
#include "components/exo/data_device_delegate.h"
#include "components/exo/data_offer.h"
#include "components/exo/data_offer_delegate.h"
#include "components/exo/data_source.h"
#include "components/exo/data_source_delegate.h"
#include "components/exo/display.h"
#include "components/exo/wayland/serial_tracker.h"
#include "components/exo/wayland/server_util.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
namespace exo {
namespace wayland {
namespace {
uint32_t WaylandDataDeviceManagerDndAction(DndAction action) {
switch (action) {
case DndAction::kNone:
return WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
case DndAction::kCopy:
return WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
case DndAction::kMove:
return WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;
case DndAction::kAsk:
return WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK;
}
NOTREACHED();
}
uint32_t WaylandDataDeviceManagerDndActions(
const base::flat_set<DndAction>& dnd_actions) {
uint32_t actions = 0;
for (DndAction action : dnd_actions)
actions |= WaylandDataDeviceManagerDndAction(action);
return actions;
}
DndAction DataDeviceManagerDndAction(uint32_t value) {
switch (value) {
case WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE:
return DndAction::kNone;
case WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY:
return DndAction::kCopy;
case WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE:
return DndAction::kMove;
case WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK:
return DndAction::kAsk;
default:
NOTREACHED();
return DndAction::kNone;
}
}
base::flat_set<DndAction> DataDeviceManagerDndActions(uint32_t value) {
base::flat_set<DndAction> actions;
if (value & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY)
actions.insert(DndAction::kCopy);
if (value & WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE)
actions.insert(DndAction::kMove);
if (value & WL_DATA_DEVICE_MANAGER_DND_ACTION_ASK)
actions.insert(DndAction::kAsk);
return actions;
}
////////////////////////////////////////////////////////////////////////////////
// wl_data_source_interface:
class WaylandDataSourceDelegate : public DataSourceDelegate {
public:
explicit WaylandDataSourceDelegate(wl_client* client,
wl_resource* source)
: client_(client),
data_source_resource_(source) {}
// Overridden from DataSourceDelegate:
void OnDataSourceDestroying(DataSource* device) override { delete this; }
bool CanAcceptDataEventsForSurface(Surface* surface) const override {
return surface &&
wl_resource_get_client(GetSurfaceResource(surface)) == client_;
}
void OnTarget(const absl::optional<std::string>& mime_type) override {
wl_data_source_send_target(data_source_resource_,
mime_type ? mime_type->c_str() : nullptr);
wl_client_flush(wl_resource_get_client(data_source_resource_));
}
void OnSend(const std::string& mime_type, base::ScopedFD fd) override {
wl_data_source_send_send(data_source_resource_, mime_type.c_str(),
fd.get());
wl_client_flush(wl_resource_get_client(data_source_resource_));
}
void OnCancelled() override {
wl_data_source_send_cancelled(data_source_resource_);
wl_client_flush(wl_resource_get_client(data_source_resource_));
}
void OnDndDropPerformed() override {
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_);
wl_client_flush(wl_resource_get_client(data_source_resource_));
}
}
void OnDndFinished() override {
if (wl_resource_get_version(data_source_resource_) >=
WL_DATA_SOURCE_DND_FINISHED_SINCE_VERSION) {
wl_data_source_send_dnd_finished(data_source_resource_);
wl_client_flush(wl_resource_get_client(data_source_resource_));
}
}
void OnAction(DndAction dnd_action) override {
if (wl_resource_get_version(data_source_resource_) >=
WL_DATA_SOURCE_ACTION_SINCE_VERSION) {
wl_data_source_send_action(data_source_resource_,
WaylandDataDeviceManagerDndAction(dnd_action));
wl_client_flush(wl_resource_get_client(data_source_resource_));
}
}
private:
wl_client* const client_;
wl_resource* const data_source_resource_;
DISALLOW_COPY_AND_ASSIGN(WaylandDataSourceDelegate);
};
void data_source_offer(wl_client* client,
wl_resource* resource,
const char* mime_type) {
GetUserDataAs<DataSource>(resource)->Offer(mime_type);
}
void data_source_destroy(wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
}
void data_source_set_actions(wl_client* client,
wl_resource* resource,
uint32_t dnd_actions) {
GetUserDataAs<DataSource>(resource)->SetActions(
DataDeviceManagerDndActions(dnd_actions));
}
const struct wl_data_source_interface data_source_implementation = {
data_source_offer, data_source_destroy, data_source_set_actions};
////////////////////////////////////////////////////////////////////////////////
// wl_data_offer_interface:
class WaylandDataOfferDelegate : public DataOfferDelegate {
public:
explicit WaylandDataOfferDelegate(wl_resource* offer)
: data_offer_resource_(offer) {}
// Overridden from DataOfferDelegate:
void OnDataOfferDestroying(DataOffer* device) override { delete this; }
void OnOffer(const std::string& mime_type) override {
wl_data_offer_send_offer(data_offer_resource_, mime_type.c_str());
wl_client_flush(wl_resource_get_client(data_offer_resource_));
}
void OnSourceActions(
const base::flat_set<DndAction>& source_actions) override {
if (wl_resource_get_version(data_offer_resource_) >=
WL_DATA_OFFER_SOURCE_ACTIONS_SINCE_VERSION) {
wl_data_offer_send_source_actions(
data_offer_resource_,
WaylandDataDeviceManagerDndActions(source_actions));
wl_client_flush(wl_resource_get_client(data_offer_resource_));
}
}
void OnAction(DndAction action) override {
if (wl_resource_get_version(data_offer_resource_) >=
WL_DATA_OFFER_ACTION_SINCE_VERSION) {
wl_data_offer_send_action(data_offer_resource_,
WaylandDataDeviceManagerDndAction(action));
wl_client_flush(wl_resource_get_client(data_offer_resource_));
}
}
private:
wl_resource* const data_offer_resource_;
DISALLOW_COPY_AND_ASSIGN(WaylandDataOfferDelegate);
};
void data_offer_accept(wl_client* client,
wl_resource* resource,
uint32_t serial,
const char* mime_type) {
if (mime_type == nullptr) {
GetUserDataAs<DataOffer>(resource)->Accept(nullptr);
return;
}
const std::string mime_type_string(mime_type);
GetUserDataAs<DataOffer>(resource)->Accept(&mime_type_string);
}
void data_offer_receive(wl_client* client,
wl_resource* resource,
const char* mime_type,
int fd) {
GetUserDataAs<DataOffer>(resource)->Receive(mime_type, base::ScopedFD(fd));
}
void data_offer_destroy(wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
}
void data_offer_finish(wl_client* client, wl_resource* resource) {
GetUserDataAs<DataOffer>(resource)->Finish();
}
void data_offer_set_actions(wl_client* client,
wl_resource* resource,
uint32_t dnd_actions,
uint32_t preferred_action) {
GetUserDataAs<DataOffer>(resource)->SetActions(
DataDeviceManagerDndActions(dnd_actions),
DataDeviceManagerDndAction(preferred_action));
}
const struct wl_data_offer_interface data_offer_implementation = {
data_offer_accept, data_offer_receive, data_offer_destroy,
data_offer_finish, data_offer_set_actions};
////////////////////////////////////////////////////////////////////////////////
// wl_data_device_interface:
class WaylandDataDeviceDelegate : public DataDeviceDelegate {
public:
WaylandDataDeviceDelegate(wl_client* client,
wl_resource* device_resource,
SerialTracker* serial_tracker)
: client_(client),
data_device_resource_(device_resource),
serial_tracker_(serial_tracker) {}
// Overridden from DataDeviceDelegate:
void OnDataDeviceDestroying(DataDevice* device) override { delete this; }
bool CanAcceptDataEventsForSurface(Surface* surface) const override {
return surface && GetSurfaceResource(surface) &&
wl_resource_get_client(GetSurfaceResource(surface)) == client_;
}
DataOffer* OnDataOffer() override {
wl_resource* data_offer_resource =
wl_resource_create(client_, &wl_data_offer_interface,
wl_resource_get_version(data_device_resource_), 0);
std::unique_ptr<DataOffer> data_offer = std::make_unique<DataOffer>(
new WaylandDataOfferDelegate(data_offer_resource));
SetDataOfferResource(data_offer.get(), data_offer_resource);
SetImplementation(data_offer_resource, &data_offer_implementation,
std::move(data_offer));
wl_data_device_send_data_offer(data_device_resource_, data_offer_resource);
wl_client_flush(client_);
return GetUserDataAs<DataOffer>(data_offer_resource);
}
void OnEnter(Surface* surface,
const gfx::PointF& point,
const DataOffer& data_offer) override {
wl_data_device_send_enter(
data_device_resource_,
serial_tracker_->GetNextSerial(SerialTracker::EventType::OTHER_EVENT),
GetSurfaceResource(surface), wl_fixed_from_double(point.x()),
wl_fixed_from_double(point.y()), GetDataOfferResource(&data_offer));
wl_client_flush(client_);
}
void OnLeave() override {
wl_data_device_send_leave(data_device_resource_);
wl_client_flush(client_);
}
void OnMotion(base::TimeTicks time_stamp, const gfx::PointF& point) override {
wl_data_device_send_motion(
data_device_resource_, TimeTicksToMilliseconds(time_stamp),
wl_fixed_from_double(point.x()), wl_fixed_from_double(point.y()));
wl_client_flush(client_);
}
void OnDrop() override {
wl_data_device_send_drop(data_device_resource_);
wl_client_flush(client_);
}
void OnSelection(const DataOffer& data_offer) override {
wl_data_device_send_selection(data_device_resource_,
GetDataOfferResource(&data_offer));
wl_client_flush(client_);
}
void StartDrag(DataDevice* data_device,
DataSource* source,
Surface* origin,
Surface* icon,
uint32_t serial) {
absl::optional<wayland::SerialTracker::EventType> event_type =
serial_tracker_->GetEventType(serial);
if (event_type == absl::nullopt) {
LOG(ERROR) << "The serial passed to StartDrag does not exist.";
return;
}
if (event_type == wayland::SerialTracker::EventType::POINTER_BUTTON_DOWN &&
serial_tracker_->GetPointerDownSerial() == serial) {
DCHECK(data_device);
data_device->StartDrag(source, origin, icon,
ui::mojom::DragEventSource::kMouse);
} else if (event_type == wayland::SerialTracker::EventType::TOUCH_DOWN &&
serial_tracker_->GetTouchDownSerial() == serial) {
DCHECK(data_device);
data_device->StartDrag(source, origin, icon,
ui::mojom::DragEventSource::kTouch);
} else {
LOG(ERROR) << "The serial passed to StartDrag does not match its "
"expected types.";
}
}
void SetSelection(DataDevice* data_device,
DataSource* source,
uint32_t serial) {
absl::optional<wayland::SerialTracker::EventType> event_type =
serial_tracker_->GetEventType(serial);
if (event_type == absl::nullopt) {
LOG(ERROR) << "The serial passed to SetSelection does not exist.";
return;
}
DCHECK(data_device);
data_device->SetSelection(source);
}
private:
wl_client* const client_;
wl_resource* const data_device_resource_;
// Owned by Server, which always outlives this delegate.
SerialTracker* const serial_tracker_;
DISALLOW_COPY_AND_ASSIGN(WaylandDataDeviceDelegate);
};
void data_device_start_drag(wl_client* client,
wl_resource* resource,
wl_resource* source_resource,
wl_resource* origin_resource,
wl_resource* icon_resource,
uint32_t serial) {
DataDevice* data_device = GetUserDataAs<DataDevice>(resource);
static_cast<WaylandDataDeviceDelegate*>(data_device->get_delegate())
->StartDrag(
data_device,
source_resource ? GetUserDataAs<DataSource>(source_resource)
: nullptr,
GetUserDataAs<Surface>(origin_resource),
icon_resource ? GetUserDataAs<Surface>(icon_resource) : nullptr,
serial);
}
void data_device_set_selection(wl_client* client,
wl_resource* resource,
wl_resource* source_resource,
uint32_t serial) {
DataDevice* data_device = GetUserDataAs<DataDevice>(resource);
static_cast<WaylandDataDeviceDelegate*>(data_device->get_delegate())
->SetSelection(data_device,
source_resource
? GetUserDataAs<DataSource>(source_resource)
: nullptr,
serial);
}
void data_device_release(wl_client* client, wl_resource* resource) {
wl_resource_destroy(resource);
}
const struct wl_data_device_interface data_device_implementation = {
data_device_start_drag, data_device_set_selection, data_device_release};
////////////////////////////////////////////////////////////////////////////////
// wl_data_device_manager_interface:
void data_device_manager_create_data_source(wl_client* client,
wl_resource* resource,
uint32_t id) {
wl_resource* data_source_resource = wl_resource_create(
client, &wl_data_source_interface, wl_resource_get_version(resource), id);
SetImplementation(data_source_resource, &data_source_implementation,
std::make_unique<DataSource>(new WaylandDataSourceDelegate(
client, data_source_resource)));
}
void data_device_manager_get_data_device(wl_client* client,
wl_resource* resource,
uint32_t id,
wl_resource* seat_resource) {
auto* data = GetUserDataAs<WaylandDataDeviceManager>(resource);
wl_resource* data_device_resource = wl_resource_create(
client, &wl_data_device_interface, wl_resource_get_version(resource), id);
SetImplementation(
data_device_resource, &data_device_implementation,
data->display->CreateDataDevice(new WaylandDataDeviceDelegate(
client, data_device_resource, data->serial_tracker)));
}
const struct wl_data_device_manager_interface
data_device_manager_implementation = {
data_device_manager_create_data_source,
data_device_manager_get_data_device};
} // namespace
void bind_data_device_manager(wl_client* client,
void* data,
uint32_t version,
uint32_t id) {
wl_resource* resource =
wl_resource_create(client, &wl_data_device_manager_interface,
std::min(version, kWlDataDeviceManagerVersion), id);
wl_resource_set_implementation(resource, &data_device_manager_implementation,
data, nullptr);
}
} // namespace wayland
} // namespace exo