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/qplatformdrag.h> |
7 | #include <private/qsimpledrag_p.h> |
8 | #include <xcb/xcb.h> |
9 | #include <qbackingstore.h> |
10 | #include <qdatetime.h> |
11 | #include <qlist.h> |
12 | #include <qpixmap.h> |
13 | #include <qpoint.h> |
14 | #include <qpointer.h> |
15 | #include <qrect.h> |
16 | #include <qxcbobject.h> |
17 | |
18 | #include <QtCore/QDebug> |
19 | |
20 | QT_REQUIRE_CONFIG(draganddrop); |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | class QWindow; |
25 | class QPlatformWindow; |
26 | class QXcbConnection; |
27 | class QXcbWindow; |
28 | class QXcbDropData; |
29 | class QXcbScreen; |
30 | class QDrag; |
31 | class QShapedPixmapWindow; |
32 | |
33 | class QXcbDrag : public QXcbObject, public QBasicDrag, public QXcbWindowEventListener |
34 | { |
35 | public: |
36 | QXcbDrag(QXcbConnection *c); |
37 | ~QXcbDrag(); |
38 | |
39 | bool eventFilter(QObject *o, QEvent *e) override; |
40 | |
41 | void startDrag() override; |
42 | void cancel() override; |
43 | void move(const QPoint &globalPos, Qt::MouseButtons b, Qt::KeyboardModifiers mods) override; |
44 | void drop(const QPoint &globalPos, Qt::MouseButtons b, Qt::KeyboardModifiers mods) override; |
45 | void endDrag() override; |
46 | |
47 | Qt::DropAction defaultAction(Qt::DropActions possibleActions, Qt::KeyboardModifiers modifiers) const override; |
48 | |
49 | void handlePropertyNotifyEvent(const xcb_property_notify_event_t *event) override; |
50 | |
51 | void handleEnter(QPlatformWindow *window, const xcb_client_message_event_t *event, xcb_window_t proxy = 0); |
52 | void handlePosition(QPlatformWindow *w, const xcb_client_message_event_t *event); |
53 | void handleLeave(QPlatformWindow *w, const xcb_client_message_event_t *event); |
54 | void handleDrop(QPlatformWindow *, const xcb_client_message_event_t *event, |
55 | Qt::MouseButtons b = { }, Qt::KeyboardModifiers mods = { }); |
56 | |
57 | void handleStatus(const xcb_client_message_event_t *event); |
58 | void handleSelectionRequest(const xcb_selection_request_event_t *event); |
59 | void handleFinished(const xcb_client_message_event_t *event); |
60 | |
61 | bool dndEnable(QXcbWindow *win, bool on); |
62 | bool ownsDragObject() const override; |
63 | |
64 | void updatePixmap(); |
65 | xcb_timestamp_t targetTime() { return target_time; } |
66 | |
67 | protected: |
68 | void timerEvent(QTimerEvent* e) override; |
69 | |
70 | bool findXdndAwareTarget(const QPoint &globalPos, xcb_window_t *target_out); |
71 | |
72 | private: |
73 | friend class QXcbDropData; |
74 | |
75 | void init(); |
76 | |
77 | void handle_xdnd_position(QPlatformWindow *w, const xcb_client_message_event_t *event, |
78 | Qt::MouseButtons b = { }, Qt::KeyboardModifiers mods = { }); |
79 | void handle_xdnd_status(const xcb_client_message_event_t *event); |
80 | void send_leave(); |
81 | |
82 | Qt::DropAction toDropAction(xcb_atom_t atom) const; |
83 | Qt::DropActions toDropActions(const QList<xcb_atom_t> &atoms) const; |
84 | xcb_atom_t toXdndAction(Qt::DropAction a) const; |
85 | |
86 | void readActionList(); |
87 | void setActionList(Qt::DropAction requestedAction, Qt::DropActions supportedActions); |
88 | void startListeningForActionListChanges(); |
89 | void stopListeningForActionListChanges(); |
90 | |
91 | QPointer<QWindow> initiatorWindow; |
92 | QPointer<QWindow> currentWindow; |
93 | QPoint currentPosition; |
94 | |
95 | QXcbDropData *m_dropData; |
96 | Qt::DropAction accepted_drop_action; |
97 | |
98 | QWindow *desktop_proxy; |
99 | |
100 | xcb_atom_t xdnd_dragsource; |
101 | |
102 | // the types in this drop. 100 is no good, but at least it's big. |
103 | enum { xdnd_max_type = 100 }; |
104 | QList<xcb_atom_t> xdnd_types; |
105 | |
106 | // timestamp from XdndPosition and XdndDroptime for retrieving the data |
107 | xcb_timestamp_t target_time; |
108 | xcb_timestamp_t source_time; |
109 | |
110 | // rectangle in which the answer will be the same |
111 | QRect source_sameanswer; |
112 | bool waiting_for_status; |
113 | |
114 | // helpers for setting executed drop action outside application |
115 | bool dropped; |
116 | bool canceled; |
117 | |
118 | // A window from Unity DnD Manager, which does not respect the XDnD spec |
119 | xcb_window_t xdndCollectionWindow = XCB_NONE; |
120 | |
121 | // top-level window we sent position to last. |
122 | xcb_window_t current_target; |
123 | // window to send events to (always valid if current_target) |
124 | xcb_window_t current_proxy_target; |
125 | |
126 | QXcbVirtualDesktop *current_virtual_desktop; |
127 | |
128 | // 10 minute timer used to discard old XdndDrop transactions |
129 | static constexpr std::chrono::minutes XdndDropTransactionTimeout{10}; |
130 | int cleanup_timer; |
131 | |
132 | QList<xcb_atom_t> drag_types; |
133 | |
134 | QList<xcb_atom_t> current_actions; |
135 | QList<xcb_atom_t> drop_actions; |
136 | |
137 | struct Transaction |
138 | { |
139 | xcb_timestamp_t timestamp; |
140 | xcb_window_t target; |
141 | xcb_window_t proxy_target; |
142 | QPlatformWindow *targetWindow; |
143 | // QWidget *embedding_widget; |
144 | QPointer<QDrag> drag; |
145 | QTime time; |
146 | }; |
147 | friend class QTypeInfo<Transaction>; |
148 | QList<Transaction> transactions; |
149 | |
150 | int transaction_expiry_timer; |
151 | void restartDropExpiryTimer(); |
152 | int findTransactionByWindow(xcb_window_t window); |
153 | int findTransactionByTime(xcb_timestamp_t timestamp); |
154 | xcb_window_t findRealWindow(const QPoint & pos, xcb_window_t w, int md, bool ignoreNonXdndAwareWindows); |
155 | }; |
156 | Q_DECLARE_TYPEINFO(QXcbDrag::Transaction, Q_RELOCATABLE_TYPE); |
157 | |
158 | QT_END_NAMESPACE |
159 | |