1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QXCBCLIPBOARD_H |
5 | #define QXCBCLIPBOARD_H |
6 | |
7 | #include <qpa/qplatformclipboard.h> |
8 | #include <qxcbobject.h> |
9 | #include <xcb/xcb.h> |
10 | #include <xcb/xfixes.h> |
11 | |
12 | #include <QtCore/qobject.h> |
13 | #include <QtCore/qmap.h> |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | #ifndef QT_NO_CLIPBOARD |
18 | |
19 | class QXcbConnection; |
20 | class QXcbScreen; |
21 | class QXcbClipboard; |
22 | class QXcbClipboardMime; |
23 | |
24 | class QXcbClipboardTransaction : public QObject |
25 | { |
26 | Q_OBJECT |
27 | public: |
28 | QXcbClipboardTransaction(QXcbClipboard *clipboard, xcb_window_t w, xcb_atom_t p, |
29 | QByteArray d, xcb_atom_t t, int f); |
30 | ~QXcbClipboardTransaction(); |
31 | |
32 | bool updateIncrementalProperty(const xcb_property_notify_event_t *event); |
33 | |
34 | protected: |
35 | void timerEvent(QTimerEvent *ev) override; |
36 | |
37 | private: |
38 | QXcbClipboard *m_clipboard; |
39 | xcb_window_t m_window; |
40 | xcb_atom_t m_property; |
41 | QByteArray m_data; |
42 | xcb_atom_t m_target; |
43 | uint8_t m_format; |
44 | uint m_offset = 0; |
45 | int m_abortTimerId = 0; |
46 | }; |
47 | |
48 | class QXcbClipboard : public QXcbObject, public QPlatformClipboard |
49 | { |
50 | public: |
51 | QXcbClipboard(QXcbConnection *connection); |
52 | ~QXcbClipboard(); |
53 | |
54 | QMimeData *mimeData(QClipboard::Mode mode) override; |
55 | void setMimeData(QMimeData *data, QClipboard::Mode mode) override; |
56 | |
57 | bool supportsMode(QClipboard::Mode mode) const override; |
58 | bool ownsMode(QClipboard::Mode mode) const override; |
59 | |
60 | QXcbScreen *screen() const; |
61 | |
62 | xcb_window_t requestor() const; |
63 | void setRequestor(xcb_window_t window); |
64 | |
65 | void handleSelectionRequest(xcb_selection_request_event_t *event); |
66 | void handleSelectionClearRequest(xcb_selection_clear_event_t *event); |
67 | void handleXFixesSelectionRequest(xcb_xfixes_selection_notify_event_t *event); |
68 | |
69 | bool clipboardReadProperty(xcb_window_t win, xcb_atom_t property, bool deleteProperty, QByteArray *buffer, int *size, xcb_atom_t *type, int *format); |
70 | QByteArray clipboardReadIncrementalProperty(xcb_window_t win, xcb_atom_t property, int nbytes, bool nullterm); |
71 | |
72 | QByteArray getDataInFormat(xcb_atom_t modeAtom, xcb_atom_t fmtatom); |
73 | |
74 | bool handlePropertyNotify(const xcb_generic_event_t *event); |
75 | |
76 | QByteArray getSelection(xcb_atom_t selection, xcb_atom_t target, xcb_atom_t property, xcb_timestamp_t t = 0); |
77 | |
78 | int increment() const { return m_maxPropertyRequestDataBytes; } |
79 | int clipboardTimeout() const { return clipboard_timeout; } |
80 | |
81 | void removeTransaction(xcb_window_t window) { m_transactions.remove(key: window); } |
82 | |
83 | private: |
84 | xcb_generic_event_t *waitForClipboardEvent(xcb_window_t window, int type, bool checkManager = false); |
85 | |
86 | xcb_atom_t sendTargetsSelection(QMimeData *d, xcb_window_t window, xcb_atom_t property); |
87 | xcb_atom_t sendSelection(QMimeData *d, xcb_atom_t target, xcb_window_t window, xcb_atom_t property); |
88 | |
89 | xcb_atom_t atomForMode(QClipboard::Mode mode) const; |
90 | QClipboard::Mode modeForAtom(xcb_atom_t atom) const; |
91 | |
92 | // Selection and Clipboard |
93 | QScopedPointer<QXcbClipboardMime> m_xClipboard[2]; |
94 | QMimeData *m_clientClipboard[2]; |
95 | xcb_timestamp_t m_timestamp[2]; |
96 | |
97 | xcb_window_t m_requestor = XCB_NONE; |
98 | |
99 | static const int clipboard_timeout; |
100 | |
101 | int m_maxPropertyRequestDataBytes = 0; |
102 | bool m_clipboard_closing = false; |
103 | xcb_timestamp_t m_incr_receive_time = 0; |
104 | |
105 | using TransactionMap = QMap<xcb_window_t, QXcbClipboardTransaction *>; |
106 | TransactionMap m_transactions; |
107 | }; |
108 | |
109 | #endif // QT_NO_CLIPBOARD |
110 | |
111 | QT_END_NAMESPACE |
112 | |
113 | #endif // QXCBCLIPBOARD_H |
114 | |