1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWaylandCompositor module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #ifndef WLDATADEVICEMANAGER_H
31 #define WLDATADEVICEMANAGER_H
32 
33 //
34 //  W A R N I N G
35 //  -------------
36 //
37 // This file is not part of the Qt API.  It exists purely as an
38 // implementation detail.  This header file may change from version to
39 // version without notice, or even be removed.
40 //
41 // We mean it.
42 //
43 
44 #include <QtCore/QList>
45 #include <QtCore/QMap>
46 #include <QtGui/QClipboard>
47 #include <QtCore/QMimeData>
48 
49 #include <QtWaylandCompositor/QWaylandCompositor>
50 
51 #include <QtWaylandCompositor/private/qwayland-server-wayland.h>
52 #include <QtWaylandCompositor/private/qtwaylandcompositorglobal_p.h>
53 
54 QT_REQUIRE_CONFIG(wayland_datadevice);
55 
56 QT_BEGIN_NAMESPACE
57 
58 class QSocketNotifier;
59 
60 namespace QtWayland {
61 
62 class DataDevice;
63 class DataSource;
64 
65 class DataDeviceManager : public QObject, public QtWaylandServer::wl_data_device_manager
66 {
67     Q_OBJECT
68 
69 public:
70     DataDeviceManager(QWaylandCompositor *compositor);
71 
72     void setCurrentSelectionSource(DataSource *source);
73     DataSource *currentSelectionSource();
74 
75     struct wl_display *display() const;
76 
77     void sourceDestroyed(DataSource *source);
78 
79     void overrideSelection(const QMimeData &mimeData);
80     bool offerFromCompositorToClient(wl_resource *clientDataDeviceResource);
81     void offerRetainedSelection(wl_resource *clientDataDeviceResource);
82 
83 protected:
84     void data_device_manager_create_data_source(Resource *resource, uint32_t id) override;
85     void data_device_manager_get_data_device(Resource *resource, uint32_t id, struct ::wl_resource *seat) override;
86 
87 private Q_SLOTS:
88     void readFromClient(int fd);
89 
90 private:
91     void retain();
92     void finishReadFromClient(bool exhausted = false);
93 
94     QWaylandCompositor *m_compositor = nullptr;
95     QList<DataDevice *> m_data_device_list;
96 
97     DataSource *m_current_selection_source = nullptr;
98 
99     QMimeData m_retainedData;
100     QSocketNotifier *m_retainedReadNotifier = nullptr;
101     QList<QSocketNotifier *> m_obsoleteRetainedReadNotifiers;
102     int m_retainedReadIndex = 0;
103     QByteArray m_retainedReadBuf;
104 
105     bool m_compositorOwnsSelection = false;
106 
107 
108     static void comp_accept(struct wl_client *client,
109                             struct wl_resource *resource,
110                             uint32_t time,
111                             const char *type);
112     static void comp_receive(struct wl_client *client,
113                              struct wl_resource *resource,
114                              const char *mime_type,
115                              int32_t fd);
116     static void comp_destroy(struct wl_client *client,
117                              struct wl_resource *resource);
118 
119     static const struct wl_data_offer_interface compositor_offer_interface;
120 };
121 
122 }
123 
124 QT_END_NAMESPACE
125 
126 #endif // WLDATADEVICEMANAGER_H
127