1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the Qt Compositor.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #include "wldatadevicemanager.h"
43 #include "wldatadevice.h"
44 #include "wldatasource.h"
45 #include "wlinputdevice.h"
46 #include "wlcompositor.h"
47 #include "wldataoffer.h"
48 #include "wlsurface.h"
49 #include "qwaylandmimehelper.h"
51 #include <QtCore/QDebug>
52 #include <QtCore/QSocketNotifier>
54 #include <QtCore/private/qcore_unix_p.h>
55 #include <QtCore/QFile>
59 DataDeviceManager::DataDeviceManager(Compositor *compositor)
60 : m_compositor(compositor)
61 , m_current_selection_source(0)
62 , m_retainedReadNotifier(0)
63 , m_compositorOwnsSelection(false)
65 wl_display_add_global(compositor->wl_display(), &wl_data_device_manager_interface, this, DataDeviceManager::bind_func_drag);
68 void DataDeviceManager::setCurrentSelectionSource(DataSource *source)
70 if (m_current_selection_source
71 && m_current_selection_source->time() > source->time()) {
72 qDebug() << "Trying to set older selection";
76 m_compositorOwnsSelection = false;
78 finishReadFromClient();
80 m_current_selection_source = source;
81 source->setManager(this);
83 // When retained selection is enabled, the compositor will query all the data from the client.
84 // This makes it possible to
85 // 1. supply the selection after the offering client is gone
86 // 2. make it possible for the compositor to participate in copy-paste
87 // The downside is decreased performance, therefore this mode has to be enabled
88 // explicitly in the compositors.
89 if (m_compositor->wantsRetainedSelection()) {
90 m_retainedData.clear();
91 m_retainedReadIndex = 0;
96 void DataDeviceManager::sourceDestroyed(DataSource *source)
99 if (m_current_selection_source == source)
100 finishReadFromClient();
103 void DataDeviceManager::retain()
105 QList<QByteArray> offers = m_current_selection_source->offerList();
106 finishReadFromClient();
107 if (m_retainedReadIndex >= offers.count()) {
108 m_compositor->feedRetainedSelectionData(&m_retainedData);
111 QByteArray mimeType = offers.at(m_retainedReadIndex);
112 m_retainedReadBuf.clear();
114 if (pipe(fd) == -1) {
115 qWarning("Clipboard: Failed to create pipe");
118 fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL, 0) | O_NONBLOCK);
119 m_current_selection_source->postSendEvent(mimeType, fd[1]);
121 m_retainedReadNotifier = new QSocketNotifier(fd[0], QSocketNotifier::Read, this);
122 connect(m_retainedReadNotifier, SIGNAL(activated(int)), SLOT(readFromClient(int)));
125 void DataDeviceManager::finishReadFromClient(bool exhausted)
128 if (m_retainedReadNotifier) {
130 int fd = m_retainedReadNotifier->socket();
131 delete m_retainedReadNotifier;
134 // Do not close the handle or destroy the read notifier here
135 // or else clients may SIGPIPE.
136 m_obsoleteRetainedReadNotifiers.append(m_retainedReadNotifier);
138 m_retainedReadNotifier = 0;
142 void DataDeviceManager::readFromClient(int fd)
144 static char buf[4096];
145 int obsCount = m_obsoleteRetainedReadNotifiers.count();
146 for (int i = 0; i < obsCount; ++i) {
147 QSocketNotifier *sn = m_obsoleteRetainedReadNotifiers.at(i);
148 if (sn->socket() == fd) {
149 // Read and drop the data, stopping to read and closing the handle
150 // is not yet safe because that could kill the client with SIGPIPE
151 // when it still tries to write.
154 n = QT_READ(fd, buf, sizeof buf);
156 if (n != -1 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
157 m_obsoleteRetainedReadNotifiers.removeAt(i);
164 int n = QT_READ(fd, buf, sizeof buf);
166 if (n != -1 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
167 finishReadFromClient(true);
168 QList<QByteArray> offers = m_current_selection_source->offerList();
169 QString mimeType = QString::fromLatin1(offers.at(m_retainedReadIndex));
170 m_retainedData.setData(mimeType, m_retainedReadBuf);
171 ++m_retainedReadIndex;
175 m_retainedReadBuf.append(buf, n);
179 DataSource *DataDeviceManager::currentSelectionSource()
181 return m_current_selection_source;
184 struct wl_display *DataDeviceManager::display() const
186 return m_compositor->wl_display();
189 void DataDeviceManager::bind_func_drag(struct wl_client *client, void *data, uint32_t version, uint32_t id)
192 wl_client_add_object(client,&wl_data_device_manager_interface,&drag_interface,id,data);
195 void DataDeviceManager::bind_func_data(struct wl_client *client, void *data, uint32_t version, uint32_t id)
203 void DataDeviceManager::get_data_device(struct wl_client *client,
204 struct wl_resource *data_device_manager_resource,
206 struct wl_resource *input_device_resource)
208 DataDeviceManager *data_device_manager = static_cast<DataDeviceManager *>(data_device_manager_resource->data);
209 InputDevice *input_device = resolve<InputDevice>(input_device_resource);
210 input_device->clientRequestedDataDevice(data_device_manager,client,id);
213 void DataDeviceManager::create_data_source(struct wl_client *client,
214 struct wl_resource *data_device_manager_resource,
217 Q_UNUSED(data_device_manager_resource);
218 new DataSource(client,id, Compositor::currentTimeMsecs());
221 struct wl_data_device_manager_interface DataDeviceManager::drag_interface = {
222 DataDeviceManager::create_data_source,
223 DataDeviceManager::get_data_device
226 void DataDeviceManager::overrideSelection(const QMimeData &mimeData)
228 QStringList formats = mimeData.formats();
229 if (formats.isEmpty())
232 m_retainedData.clear();
233 foreach (const QString &format, formats)
234 m_retainedData.setData(format, mimeData.data(format));
236 m_compositor->feedRetainedSelectionData(&m_retainedData);
238 m_compositorOwnsSelection = true;
240 InputDevice *dev = m_compositor->defaultInputDevice();
241 Surface *focusSurface = dev->keyboardFocus();
243 offerFromCompositorToClient(
244 dev->dataDevice(focusSurface->base()->resource.client)->dataDeviceResource());
247 bool DataDeviceManager::offerFromCompositorToClient(wl_resource *clientDataDeviceResource)
249 if (!m_compositorOwnsSelection)
252 wl_client *client = clientDataDeviceResource->client;
253 //qDebug("compositor offers %d types to %p", m_retainedData.formats().count(), client);
255 struct wl_resource *selectionOffer =
256 wl_client_new_object(client, &wl_data_offer_interface, &compositor_offer_interface, this);
257 wl_data_device_send_data_offer(clientDataDeviceResource, selectionOffer);
258 foreach (const QString &format, m_retainedData.formats()) {
259 QByteArray ba = format.toLatin1();
260 wl_data_offer_send_offer(selectionOffer, ba.constData());
262 wl_data_device_send_selection(clientDataDeviceResource, selectionOffer);
267 void DataDeviceManager::offerRetainedSelection(wl_resource *clientDataDeviceResource)
269 if (m_retainedData.formats().isEmpty())
272 m_compositorOwnsSelection = true;
273 offerFromCompositorToClient(clientDataDeviceResource);
276 void DataDeviceManager::comp_accept(wl_client *, wl_resource *, uint32_t, const char *)
280 void DataDeviceManager::comp_receive(wl_client *client, wl_resource *resource, const char *mime_type, int32_t fd)
283 DataDeviceManager *self = static_cast<DataDeviceManager *>(resource->data);
284 //qDebug("client %p wants data for type %s from compositor", client, mime_type);
285 QByteArray content = QWaylandMimeHelper::getByteArray(&self->m_retainedData, QString::fromLatin1(mime_type));
286 if (!content.isEmpty()) {
288 if (f.open(fd, QIODevice::WriteOnly))
294 void DataDeviceManager::comp_destroy(wl_client *, wl_resource *)
298 const struct wl_data_offer_interface DataDeviceManager::compositor_offer_interface = {
299 DataDeviceManager::comp_accept,
300 DataDeviceManager::comp_receive,
301 DataDeviceManager::comp_destroy