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 QtGui module 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 QXCBDRAG_H
41#define QXCBDRAG_H
42
43#include <qpa/qplatformdrag.h>
44#include <private/qsimpledrag_p.h>
45#include <qxcbobject.h>
46#include <xcb/xcb.h>
47#include <qpoint.h>
48#include <qrect.h>
49#include <qsharedpointer.h>
50#include <qpointer.h>
51#include <qvector.h>
52#include <qdatetime.h>
53#include <qpixmap.h>
54#include <qbackingstore.h>
55
56#include <QtCore/QDebug>
57
58QT_REQUIRE_CONFIG(draganddrop);
59
60QT_BEGIN_NAMESPACE
61
62class QWindow;
63class QPlatformWindow;
64class QXcbConnection;
65class QXcbWindow;
66class QXcbDropData;
67class QXcbScreen;
68class QDrag;
69class QShapedPixmapWindow;
70
71class QXcbDrag : public QXcbObject, public QBasicDrag
72{
73public:
74 QXcbDrag(QXcbConnection *c);
75 ~QXcbDrag();
76
77 bool eventFilter(QObject *o, QEvent *e) override;
78
79 void startDrag() override;
80 void cancel() override;
81 void move(const QPoint &globalPos, Qt::MouseButtons b, Qt::KeyboardModifiers mods) override;
82 void drop(const QPoint &globalPos, Qt::MouseButtons b, Qt::KeyboardModifiers mods) override;
83 void endDrag() override;
84
85 void handleEnter(QPlatformWindow *window, const xcb_client_message_event_t *event, xcb_window_t proxy = 0);
86 void handlePosition(QPlatformWindow *w, const xcb_client_message_event_t *event);
87 void handleLeave(QPlatformWindow *w, const xcb_client_message_event_t *event);
88 void handleDrop(QPlatformWindow *, const xcb_client_message_event_t *event,
89 Qt::MouseButtons b = nullptr, Qt::KeyboardModifiers mods = nullptr);
90
91 void handleStatus(const xcb_client_message_event_t *event);
92 void handleSelectionRequest(const xcb_selection_request_event_t *event);
93 void handleFinished(const xcb_client_message_event_t *event);
94
95 bool dndEnable(QXcbWindow *win, bool on);
96 bool ownsDragObject() const override;
97
98 void updatePixmap();
99 xcb_timestamp_t targetTime() { return target_time; }
100
101protected:
102 void timerEvent(QTimerEvent* e) override;
103
104 bool findXdndAwareTarget(const QPoint &globalPos, xcb_window_t *target_out);
105
106private:
107 friend class QXcbDropData;
108
109 void init();
110
111 void handle_xdnd_position(QPlatformWindow *w, const xcb_client_message_event_t *event,
112 Qt::MouseButtons b = nullptr, Qt::KeyboardModifiers mods = nullptr);
113 void handle_xdnd_status(const xcb_client_message_event_t *event);
114 void send_leave();
115
116 Qt::DropAction toDropAction(xcb_atom_t atom) const;
117 xcb_atom_t toXdndAction(Qt::DropAction a) const;
118
119 QPointer<QWindow> initiatorWindow;
120 QPointer<QWindow> currentWindow;
121 QPoint currentPosition;
122
123 QXcbDropData *m_dropData;
124 Qt::DropAction accepted_drop_action;
125
126 QWindow *desktop_proxy;
127
128 xcb_atom_t xdnd_dragsource;
129
130 // the types in this drop. 100 is no good, but at least it's big.
131 enum { xdnd_max_type = 100 };
132 QVector<xcb_atom_t> xdnd_types;
133
134 // timestamp from XdndPosition and XdndDroptime for retrieving the data
135 xcb_timestamp_t target_time;
136 xcb_timestamp_t source_time;
137
138 // rectangle in which the answer will be the same
139 QRect source_sameanswer;
140 bool waiting_for_status;
141
142 // helpers for setting executed drop action outside application
143 bool dropped;
144 bool canceled;
145
146 // A window from Unity DnD Manager, which does not respect the XDnD spec
147 xcb_window_t xdndCollectionWindow = XCB_NONE;
148
149 // top-level window we sent position to last.
150 xcb_window_t current_target;
151 // window to send events to (always valid if current_target)
152 xcb_window_t current_proxy_target;
153
154 QXcbVirtualDesktop *current_virtual_desktop;
155
156 // 10 minute timer used to discard old XdndDrop transactions
157 enum { XdndDropTransactionTimeout = 600000 };
158 int cleanup_timer;
159
160 QVector<xcb_atom_t> drag_types;
161
162 struct Transaction
163 {
164 xcb_timestamp_t timestamp;
165 xcb_window_t target;
166 xcb_window_t proxy_target;
167 QPlatformWindow *targetWindow;
168// QWidget *embedding_widget;
169 QPointer<QDrag> drag;
170 QTime time;
171 };
172 friend class QTypeInfo<Transaction>;
173 QVector<Transaction> transactions;
174
175 int transaction_expiry_timer;
176 void restartDropExpiryTimer();
177 int findTransactionByWindow(xcb_window_t window);
178 int findTransactionByTime(xcb_timestamp_t timestamp);
179 xcb_window_t findRealWindow(const QPoint & pos, xcb_window_t w, int md, bool ignoreNonXdndAwareWindows);
180};
181Q_DECLARE_TYPEINFO(QXcbDrag::Transaction, Q_MOVABLE_TYPE);
182
183QT_END_NAMESPACE
184
185#endif
186