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