File indexing completed on 2025-02-16 05:09:07

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2018 The Qt Company Ltd.
0004 ** Contact: https://www.qt.io/licensing/
0005 **
0006 ** This file is part of the test suite of the Qt Toolkit.
0007 **
0008 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
0009 ** Commercial License Usage
0010 ** Licensees holding valid commercial Qt licenses may use this file in
0011 ** accordance with the commercial license agreement provided with the
0012 ** Software or, alternatively, in accordance with the terms contained in
0013 ** a written agreement between you and The Qt Company. For licensing terms
0014 ** and conditions see https://www.qt.io/terms-conditions. For further
0015 ** information use the contact form at https://www.qt.io/contact-us.
0016 **
0017 ** GNU General Public License Usage
0018 ** Alternatively, this file may be used under the terms of the GNU
0019 ** General Public License version 3 as published by the Free Software
0020 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
0021 ** included in the packaging of this file. Please review the following
0022 ** information to ensure the GNU General Public License requirements will
0023 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
0024 **
0025 ** $QT_END_LICENSE$
0026 **
0027 ****************************************************************************/
0028 
0029 #include "datadevice.h"
0030 
0031 namespace MockCompositor
0032 {
0033 bool DataDeviceManager::isClean()
0034 {
0035     for (auto *device : std::as_const(m_dataDevices)) {
0036         // The client should not leak selection offers, i.e. if this fails, there is a missing
0037         // data_offer.destroy request
0038         if (!device->m_sentSelectionOffers.empty())
0039             return false;
0040     }
0041     return true;
0042 }
0043 
0044 DataDevice *DataDeviceManager::deviceFor(Seat *seat)
0045 {
0046     Q_ASSERT(seat);
0047     if (auto *device = m_dataDevices.value(seat, nullptr))
0048         return device;
0049 
0050     auto *device = new DataDevice(this, seat);
0051     m_dataDevices[seat] = device;
0052     return device;
0053 }
0054 
0055 void DataDeviceManager::data_device_manager_get_data_device(Resource *resource, uint32_t id, wl_resource *seatResource)
0056 {
0057     auto *seat = fromResource<Seat>(seatResource);
0058     QVERIFY(seat);
0059     auto *device = deviceFor(seat);
0060     device->add(resource->client(), id, resource->version());
0061 }
0062 
0063 DataDevice::~DataDevice()
0064 {
0065     // If the client(s) hasn't deleted the wayland object, just ignore subsequent events
0066     for (auto *r : resourceMap())
0067         wl_resource_set_implementation(r->handle, nullptr, nullptr, nullptr);
0068 }
0069 
0070 DataOffer *DataDevice::sendDataOffer(wl_client *client, const QStringList &mimeTypes)
0071 {
0072     Q_ASSERT(client);
0073     auto *offer = new DataOffer(this, client, m_manager->m_version);
0074     for (auto *resource : resourceMap().values(client))
0075         wl_data_device::send_data_offer(resource->handle, offer->resource()->handle);
0076     for (const auto &mimeType : mimeTypes)
0077         offer->send_offer(mimeType);
0078     return offer;
0079 }
0080 
0081 void DataDevice::sendSelection(DataOffer *offer)
0082 {
0083     auto *client = offer->resource()->client();
0084     for (auto *resource : resourceMap().values(client))
0085         wl_data_device::send_selection(resource->handle, offer->resource()->handle);
0086     m_sentSelectionOffers << offer;
0087 }
0088 
0089 void DataOffer::data_offer_destroy_resource(Resource *resource)
0090 {
0091     Q_UNUSED(resource);
0092     delete this;
0093 }
0094 
0095 void DataOffer::data_offer_receive(Resource *resource, const QString &mime_type, int32_t fd)
0096 {
0097     Q_UNUSED(resource);
0098     emit receive(mime_type, fd);
0099 }
0100 
0101 void DataOffer::data_offer_destroy(QtWaylandServer::wl_data_offer::Resource *resource)
0102 {
0103     bool removed = m_dataDevice->m_sentSelectionOffers.removeOne(this);
0104     QVERIFY(removed);
0105     wl_resource_destroy(resource->handle);
0106 }
0107 
0108 } // namespace MockCompositor