1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QXCBCLIPBOARD_H
41 #define QXCBCLIPBOARD_H
42 
43 #include <qpa/qplatformclipboard.h>
44 #include <qxcbobject.h>
45 #include <xcb/xcb.h>
46 #include <xcb/xfixes.h>
47 
48 #include <QtCore/QObject>
49 
50 QT_BEGIN_NAMESPACE
51 
52 #ifndef QT_NO_CLIPBOARD
53 
54 class QXcbConnection;
55 class QXcbScreen;
56 class QXcbClipboard;
57 class QXcbClipboardMime;
58 
59 class QXcbClipboardTransaction : public QObject
60 {
61     Q_OBJECT
62 public:
63     QXcbClipboardTransaction(QXcbClipboard *clipboard, xcb_window_t w, xcb_atom_t p,
64                            QByteArray d, xcb_atom_t t, int f);
65     ~QXcbClipboardTransaction();
66 
67     bool updateIncrementalProperty(const xcb_property_notify_event_t *event);
68 
69 protected:
70     void timerEvent(QTimerEvent *ev) override;
71 
72 private:
73     QXcbClipboard *m_clipboard;
74     xcb_window_t m_window;
75     xcb_atom_t m_property;
76     QByteArray m_data;
77     xcb_atom_t m_target;
78     uint8_t m_format;
79     uint m_offset = 0;
80     int m_abortTimerId = 0;
81 };
82 
83 class QXcbClipboard : public QXcbObject, public QPlatformClipboard
84 {
85 public:
86     QXcbClipboard(QXcbConnection *connection);
87     ~QXcbClipboard();
88 
89     QMimeData *mimeData(QClipboard::Mode mode) override;
90     void setMimeData(QMimeData *data, QClipboard::Mode mode) override;
91 
92     bool supportsMode(QClipboard::Mode mode) const override;
93     bool ownsMode(QClipboard::Mode mode) const override;
94 
95     QXcbScreen *screen() const;
96 
97     xcb_window_t requestor() const;
98     void setRequestor(xcb_window_t window);
99 
100     xcb_window_t owner() const;
101 
102     void handleSelectionRequest(xcb_selection_request_event_t *event);
103     void handleSelectionClearRequest(xcb_selection_clear_event_t *event);
104     void handleXFixesSelectionRequest(xcb_xfixes_selection_notify_event_t *event);
105 
106     bool clipboardReadProperty(xcb_window_t win, xcb_atom_t property, bool deleteProperty, QByteArray *buffer, int *size, xcb_atom_t *type, int *format);
107     QByteArray clipboardReadIncrementalProperty(xcb_window_t win, xcb_atom_t property, int nbytes, bool nullterm);
108 
109     QByteArray getDataInFormat(xcb_atom_t modeAtom, xcb_atom_t fmtatom);
110 
111     bool handlePropertyNotify(const xcb_generic_event_t *event);
112 
113     xcb_window_t getSelectionOwner(xcb_atom_t atom) const;
114     QByteArray getSelection(xcb_atom_t selection, xcb_atom_t target, xcb_atom_t property, xcb_timestamp_t t = 0);
115 
increment()116     int increment() const { return m_maxPropertyRequestDataBytes; }
clipboardTimeout()117     int clipboardTimeout() const { return clipboard_timeout; }
118 
removeTransaction(xcb_window_t window)119     void removeTransaction(xcb_window_t window) { m_transactions.remove(window); }
120 
121 private:
122     xcb_generic_event_t *waitForClipboardEvent(xcb_window_t window, int type, bool checkManager = false);
123 
124     xcb_atom_t sendTargetsSelection(QMimeData *d, xcb_window_t window, xcb_atom_t property);
125     xcb_atom_t sendSelection(QMimeData *d, xcb_atom_t target, xcb_window_t window, xcb_atom_t property);
126 
127     xcb_atom_t atomForMode(QClipboard::Mode mode) const;
128     QClipboard::Mode modeForAtom(xcb_atom_t atom) const;
129 
130     // Selection and Clipboard
131     QScopedPointer<QXcbClipboardMime> m_xClipboard[2];
132     QMimeData *m_clientClipboard[2];
133     xcb_timestamp_t m_timestamp[2];
134 
135     xcb_window_t m_requestor = XCB_NONE;
136     xcb_window_t m_owner = XCB_NONE;
137 
138     static const int clipboard_timeout;
139 
140     int m_maxPropertyRequestDataBytes = 0;
141     bool m_clipboard_closing = false;
142     xcb_timestamp_t m_incr_receive_time = 0;
143 
144     using TransactionMap = QMap<xcb_window_t, QXcbClipboardTransaction *>;
145     TransactionMap m_transactions;
146 };
147 
148 #endif // QT_NO_CLIPBOARD
149 
150 QT_END_NAMESPACE
151 
152 #endif // QXCBCLIPBOARD_H
153