1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include "qxcbconnection.h"
45 #include "qxcbclipboard.h"
47 #include "qxcbwindow.h"
48 #include "qxcbscreen.h"
50 #include <private/qdnd_p.h>
53 #include <qguiapplication.h>
57 #include <QtGui/qwindowsysteminterface.h>
59 #include <QtPlatformSupport/private/qshapedpixmapdndwindow_p.h>
60 #include <QtPlatformSupport/private/qsimpledrag_p.h>
64 #ifndef QT_NO_DRAGANDDROP
70 #define DEBUG if(0) qDebug
74 #define DNDDEBUG qDebug()
76 #define DNDDEBUG if(0) qDebug()
79 const int xdnd_version = 5;
81 static inline xcb_window_t xcb_window(QWindow *w)
83 return static_cast<QXcbWindow *>(w->handle())->xcb_window();
87 static xcb_window_t xdndProxy(QXcbConnection *c, xcb_window_t w)
89 xcb_window_t proxy = XCB_NONE;
91 xcb_get_property_cookie_t cookie = Q_XCB_CALL2(xcb_get_property(c->xcb_connection(), false, w, c->atom(QXcbAtom::XdndProxy),
92 XCB_ATOM_WINDOW, 0, 1), c);
93 xcb_get_property_reply_t *reply = xcb_get_property_reply(c->xcb_connection(), cookie, 0);
95 if (reply && reply->type == XCB_ATOM_WINDOW)
96 proxy = *((xcb_window_t *)xcb_get_property_value(reply));
99 if (proxy == XCB_NONE)
102 // exists and is real?
103 cookie = Q_XCB_CALL2(xcb_get_property(c->xcb_connection(), false, proxy, c->atom(QXcbAtom::XdndProxy),
104 XCB_ATOM_WINDOW, 0, 1), c);
105 reply = xcb_get_property_reply(c->xcb_connection(), cookie, 0);
107 if (reply && reply->type == XCB_ATOM_WINDOW) {
108 xcb_window_t p = *((xcb_window_t *)xcb_get_property_value(reply));
120 class QXcbDropData : public QXcbMime
123 QXcbDropData(QXcbDrag *d);
127 bool hasFormat_sys(const QString &mimeType) const;
128 QStringList formats_sys() const;
129 QVariant retrieveData_sys(const QString &mimeType, QVariant::Type type) const;
131 QVariant xdndObtainData(const QByteArray &format, QVariant::Type requestedType) const;
137 QXcbDrag::QXcbDrag(QXcbConnection *c) : QXcbObject(c)
139 dropData = new QXcbDropData(this);
144 transaction_expiry_timer = -1;
147 QXcbDrag::~QXcbDrag()
152 void QXcbDrag::init()
154 currentWindow.clear();
156 accepted_drop_action = Qt::IgnoreAction;
158 xdnd_dragsource = XCB_NONE;
160 waiting_for_status = false;
161 current_target = XCB_NONE;
162 current_proxy_target = XCB_NONE;
164 source_time = XCB_CURRENT_TIME;
165 target_time = XCB_CURRENT_TIME;
171 QMimeData *QXcbDrag::platformDropData()
176 void QXcbDrag::startDrag()
178 // #fixme enableEventFilter();
182 heartbeat = startTimer(200);
185 xcb_set_selection_owner(xcb_connection(), connection()->clipboard()->owner(),
186 atom(QXcbAtom::XdndSelection), connection()->time());
188 QStringList fmts = QXcbMime::formatsHelper(drag()->mimeData());
189 for (int i = 0; i < fmts.size(); ++i) {
190 QList<xcb_atom_t> atoms = QXcbMime::mimeAtomsForFormat(connection(), fmts.at(i));
191 for (int j = 0; j < atoms.size(); ++j) {
192 if (!drag_types.contains(atoms.at(j)))
193 drag_types.append(atoms.at(j));
196 if (drag_types.size() > 3)
197 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, connection()->clipboard()->owner(),
198 atom(QXcbAtom::XdndTypelist),
199 XCB_ATOM_ATOM, 32, drag_types.size(), (const void *)drag_types.constData());
200 QBasicDrag::startDrag();
203 void QXcbDrag::endDrag()
205 if (heartbeat != -1) {
206 killTimer(heartbeat);
209 QBasicDrag::endDrag();
212 static xcb_translate_coordinates_reply_t *
213 translateCoordinates(QXcbConnection *c, xcb_window_t from, xcb_window_t to, int x, int y)
215 xcb_translate_coordinates_cookie_t cookie =
216 xcb_translate_coordinates(c->xcb_connection(), from, to, x, y);
217 return xcb_translate_coordinates_reply(c->xcb_connection(), cookie, 0);
221 bool windowInteractsWithPosition(xcb_connection_t *connection, const QPoint & pos, xcb_window_t w, xcb_shape_sk_t shapeType)
223 bool interacts = false;
224 xcb_shape_get_rectangles_reply_t *reply = xcb_shape_get_rectangles_reply(connection, xcb_shape_get_rectangles(connection, w, shapeType), NULL);
226 xcb_rectangle_t *rectangles = xcb_shape_get_rectangles_rectangles(reply);
228 const int nRectangles = xcb_shape_get_rectangles_rectangles_length(reply);
229 for (int i = 0; !interacts && i < nRectangles; ++i) {
230 interacts = QRect(rectangles[i].x, rectangles[i].y, rectangles[i].width, rectangles[i].height).contains(pos);
239 xcb_window_t QXcbDrag::findRealWindow(const QPoint & pos, xcb_window_t w, int md, bool ignoreNonXdndAwareWindows)
241 if (w == shapedPixmapWindow()->handle()->winId())
245 xcb_get_window_attributes_cookie_t cookie = xcb_get_window_attributes(xcb_connection(), w);
246 xcb_get_window_attributes_reply_t *reply = xcb_get_window_attributes_reply(xcb_connection(), cookie, 0);
250 if (reply->map_state != XCB_MAP_STATE_VIEWABLE)
253 xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(xcb_connection(), w);
254 xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(xcb_connection(), gcookie, 0);
255 if (reply && QRect(greply->x, greply->y, greply->width, greply->height).contains(pos)) {
256 bool windowContainsMouse = !ignoreNonXdndAwareWindows;
258 xcb_get_property_cookie_t cookie =
259 Q_XCB_CALL(xcb_get_property(xcb_connection(), false, w, connection()->atom(QXcbAtom::XdndAware),
260 XCB_GET_PROPERTY_TYPE_ANY, 0, 0));
261 xcb_get_property_reply_t *reply = xcb_get_property_reply(xcb_connection(), cookie, 0);
263 bool isAware = reply && reply->type != XCB_NONE;
266 const QPoint relPos = pos - QPoint(greply->x, greply->y);
267 // When ShapeInput and ShapeBounding are not set they return a single rectangle with the geometry of the window, this is why we
268 // need to check both here so that in the case one is set and the other is not we still get the correct result.
269 if (connection()->hasInputShape())
270 windowContainsMouse = windowInteractsWithPosition(xcb_connection(), relPos, w, XCB_SHAPE_SK_INPUT);
271 if (windowContainsMouse && connection()->hasXShape())
272 windowContainsMouse = windowInteractsWithPosition(xcb_connection(), relPos, w, XCB_SHAPE_SK_BOUNDING);
273 if (!connection()->hasInputShape() && !connection()->hasXShape())
274 windowContainsMouse = true;
275 if (windowContainsMouse)
280 xcb_query_tree_cookie_t cookie = xcb_query_tree (xcb_connection(), w);
281 xcb_query_tree_reply_t *reply = xcb_query_tree_reply(xcb_connection(), cookie, 0);
285 int nc = xcb_query_tree_children_length(reply);
286 xcb_window_t *c = xcb_query_tree_children(reply);
289 for (uint i = nc; !r && i--;)
290 r = findRealWindow(pos - QPoint(greply->x, greply->y), c[i], md-1, ignoreNonXdndAwareWindows);
296 // We didn't find a client window! Just use the
300 if (!windowContainsMouse)
309 void QXcbDrag::move(const QMouseEvent *me)
311 QBasicDrag::move(me);
312 QPoint globalPos = me->globalPos();
314 if (source_sameanswer.contains(globalPos) && source_sameanswer.isValid())
317 const QList<QXcbScreen *> &screens = connection()->screens();
318 QXcbScreen *screen = screens.at(connection()->primaryScreen());
319 for (int i = 0; i < screens.size(); ++i) {
320 if (screens.at(i)->geometry().contains(globalPos)) {
321 screen = screens.at(i);
325 if (screen != current_screen) {
326 // ### need to recreate the shaped pixmap window?
327 // int screen = QCursor::x11Screen();
328 // if ((qt_xdnd_current_screen == -1 && screen != X11->defaultScreen) || (screen != qt_xdnd_current_screen)) {
329 // // recreate the pixmap on the new screen...
330 // delete xdnd_data.deco;
331 // QWidget* parent = object->source()->window()->x11Info().screen() == screen
332 // ? object->source()->window() : QApplication::desktop()->screen(screen);
333 // xdnd_data.deco = new QShapedPixmapWidget(parent);
334 // if (!QWidget::mouseGrabber()) {
336 // xdnd_data.deco->grabMouse();
339 // xdnd_data.deco->move(QCursor::pos() - xdnd_data.deco->pm_hot);
340 current_screen = screen;
344 // qt_xdnd_current_screen = screen;
345 xcb_window_t rootwin = current_screen->root();
346 xcb_translate_coordinates_reply_t *translate =
347 ::translateCoordinates(connection(), rootwin, rootwin, globalPos.x(), globalPos.y());
351 xcb_window_t target = translate->child;
352 int lx = translate->dst_x;
353 int ly = translate->dst_y;
356 if (target && target != rootwin) {
357 xcb_window_t src = rootwin;
358 while (target != 0) {
359 DNDDEBUG << "checking target for XdndAware" << target << lx << ly;
361 // translate coordinates
362 translate = ::translateCoordinates(connection(), src, target, lx, ly);
367 lx = translate->dst_x;
368 ly = translate->dst_y;
370 xcb_window_t child = translate->child;
373 // check if it has XdndAware
374 xcb_get_property_cookie_t cookie = Q_XCB_CALL(xcb_get_property(xcb_connection(), false, target,
375 atom(QXcbAtom::XdndAware), XCB_GET_PROPERTY_TYPE_ANY, 0, 0));
376 xcb_get_property_reply_t *reply = xcb_get_property_reply(xcb_connection(), cookie, 0);
377 bool aware = reply && reply->type != XCB_NONE;
380 DNDDEBUG << "Found XdndAware on " << target;
387 if (!target || target == shapedPixmapWindow()->handle()->winId()) {
388 DNDDEBUG << "need to find real window";
389 target = findRealWindow(globalPos, rootwin, 6, true);
391 target = findRealWindow(globalPos, rootwin, 6, false);
392 DNDDEBUG << "real window found" << target;
398 w = connection()->platformWindowFromId(target);
399 if (w && (w->window()->windowType() == Qt::Desktop) /*&& !w->acceptDrops()*/)
406 xcb_window_t proxy_target = xdndProxy(connection(), target);
408 proxy_target = target;
409 int target_version = 1;
412 xcb_get_property_cookie_t cookie = xcb_get_property(xcb_connection(), false, target,
413 atom(QXcbAtom::XdndAware), XCB_GET_PROPERTY_TYPE_ANY, 0, 1);
414 xcb_get_property_reply_t *reply = xcb_get_property_reply(xcb_connection(), cookie, 0);
415 if (!reply || reply->type == XCB_NONE)
417 target_version = xcb_get_property_value_length(reply) == 1 ? *(uint32_t *)xcb_get_property_value(reply) : 1;
418 if (target_version > xdnd_version)
419 target_version = xdnd_version;
424 if (target != current_target) {
428 current_target = target;
429 current_proxy_target = proxy_target;
431 int flags = target_version << 24;
432 if (drag_types.size() > 3)
435 xcb_client_message_event_t enter;
436 enter.response_type = XCB_CLIENT_MESSAGE;
437 enter.window = target;
439 enter.type = atom(QXcbAtom::XdndEnter);
440 enter.data.data32[0] = connection()->clipboard()->owner();
441 enter.data.data32[1] = flags;
442 enter.data.data32[2] = drag_types.size()>0 ? drag_types.at(0) : 0;
443 enter.data.data32[3] = drag_types.size()>1 ? drag_types.at(1) : 0;
444 enter.data.data32[4] = drag_types.size()>2 ? drag_types.at(2) : 0;
445 // provisionally set the rectangle to 5x5 pixels...
446 source_sameanswer = QRect(globalPos.x() - 2, globalPos.y() -2 , 5, 5);
448 DEBUG() << "sending Xdnd enter source=" << enter.data.data32[0];
450 handleEnter(w->window(), &enter);
452 xcb_send_event(xcb_connection(), false, proxy_target, XCB_EVENT_MASK_NO_EVENT, (const char *)&enter);
453 waiting_for_status = false;
457 if (waiting_for_status)
461 waiting_for_status = true;
463 xcb_client_message_event_t move;
464 move.response_type = XCB_CLIENT_MESSAGE;
465 move.window = target;
467 move.type = atom(QXcbAtom::XdndPosition);
468 move.data.data32[0] = connection()->clipboard()->owner();
469 move.data.data32[1] = 0; // flags
470 move.data.data32[2] = (globalPos.x() << 16) + globalPos.y();
471 move.data.data32[3] = connection()->time();
472 move.data.data32[4] = toXdndAction(defaultAction(currentDrag()->supportedActions(), QGuiApplication::keyboardModifiers()));
473 DEBUG() << "sending Xdnd position source=" << move.data.data32[0] << "target=" << move.window;
475 source_time = connection()->time();
478 handle_xdnd_position(w->window(), &move);
480 xcb_send_event(xcb_connection(), false, proxy_target, XCB_EVENT_MASK_NO_EVENT, (const char *)&move);
484 void QXcbDrag::drop(const QMouseEvent *event)
486 QBasicDrag::drop(event);
490 xcb_client_message_event_t drop;
491 drop.response_type = XCB_CLIENT_MESSAGE;
492 drop.window = current_target;
494 drop.type = atom(QXcbAtom::XdndDrop);
495 drop.data.data32[0] = connection()->clipboard()->owner();
496 drop.data.data32[1] = 0; // flags
497 drop.data.data32[2] = connection()->time();
499 drop.data.data32[3] = 0;
500 drop.data.data32[4] = currentDrag()->supportedActions();
502 QXcbWindow *w = connection()->platformWindowFromId(current_proxy_target);
504 if (w && (w->window()->windowType() == Qt::Desktop) /*&& !w->acceptDrops()*/)
509 connection()->time(),
511 current_proxy_target,
512 (w ? w->window() : 0),
513 // current_embedding_widget,
516 transactions.append(t);
517 restartDropExpiryTimer();
520 handleDrop(w->window(), &drop);
522 xcb_send_event(xcb_connection(), false, current_proxy_target, XCB_EVENT_MASK_NO_EVENT, (const char *)&drop);
526 current_proxy_target = 0;
528 // current_embedding_widget = 0;
529 // #fixme resetDndState(false);
532 Qt::DropAction QXcbDrag::toDropAction(xcb_atom_t a) const
534 if (a == atom(QXcbAtom::XdndActionCopy) || a == 0)
535 return Qt::CopyAction;
536 if (a == atom(QXcbAtom::XdndActionLink))
537 return Qt::LinkAction;
538 if (a == atom(QXcbAtom::XdndActionMove))
539 return Qt::MoveAction;
540 return Qt::CopyAction;
543 xcb_atom_t QXcbDrag::toXdndAction(Qt::DropAction a) const
547 return atom(QXcbAtom::XdndActionCopy);
549 return atom(QXcbAtom::XdndActionLink);
551 case Qt::TargetMoveAction:
552 return atom(QXcbAtom::XdndActionMove);
553 case Qt::IgnoreAction:
556 return atom(QXcbAtom::XdndActionCopy);
560 // timer used to discard old XdndDrop transactions
561 enum { XdndDropTransactionTimeout = 5000 }; // 5 seconds
563 void QXcbDrag::restartDropExpiryTimer()
565 if (transaction_expiry_timer != -1)
566 killTimer(transaction_expiry_timer);
567 transaction_expiry_timer = startTimer(XdndDropTransactionTimeout);
570 int QXcbDrag::findTransactionByWindow(xcb_window_t window)
573 for (int i = 0; i < transactions.count(); ++i) {
574 const Transaction &t = transactions.at(i);
575 if (t.target == window || t.proxy_target == window) {
583 int QXcbDrag::findTransactionByTime(xcb_timestamp_t timestamp)
586 for (int i = 0; i < transactions.count(); ++i) {
587 const Transaction &t = transactions.at(i);
588 if (t.timestamp == timestamp) {
598 // find an ancestor with XdndAware on it
599 static Window findXdndAwareParent(Window window)
603 // check if window has XdndAware
607 unsigned char *data = 0;
608 if (XGetWindowProperty(X11->display, window, ATOM(XdndAware), 0, 0, False,
609 AnyPropertyType, &type, &f,&n,&a,&data) == Success) {
618 // try window's parent
623 if (!XQueryTree(X11->display, window, &root, &parent, &children, &unused))
635 // for embedding only
636 static QWidget* current_embedding_widget = 0;
637 static xcb_client_message_event_t last_enter_event;
640 static bool checkEmbedded(QWidget* w, const XEvent* xe)
645 if (current_embedding_widget != 0 && current_embedding_widget != w) {
646 current_target = ((QExtraWidget*)current_embedding_widget)->extraData()->xDndProxy;
647 current_proxy_target = current_target;
648 qt_xdnd_send_leave();
650 current_proxy_target = 0;
651 current_embedding_widget = 0;
654 QWExtra* extra = ((QExtraWidget*)w)->extraData();
655 if (extra && extra->xDndProxy != 0) {
657 if (current_embedding_widget != w) {
659 last_enter_event.xany.window = extra->xDndProxy;
660 XSendEvent(X11->display, extra->xDndProxy, False, NoEventMask, &last_enter_event);
661 current_embedding_widget = w;
664 ((XEvent*)xe)->xany.window = extra->xDndProxy;
665 XSendEvent(X11->display, extra->xDndProxy, False, NoEventMask, (XEvent*)xe);
666 if (currentWindow != w) {
671 current_embedding_widget = 0;
677 void QXcbDrag::handleEnter(QWindow *window, const xcb_client_message_event_t *event)
680 DEBUG() << "handleEnter" << window;
683 // motifdnd_active = false;
684 // last_enter_event.xclient = xe->xclient;
686 int version = (int)(event->data.data32[1] >> 24);
687 if (version > xdnd_version)
690 xdnd_dragsource = event->data.data32[0];
692 if (event->data.data32[1] & 1) {
693 // get the types from XdndTypeList
694 xcb_get_property_cookie_t cookie = xcb_get_property(xcb_connection(), false, xdnd_dragsource,
695 atom(QXcbAtom::XdndTypelist), XCB_ATOM_ATOM,
697 xcb_get_property_reply_t *reply = xcb_get_property_reply(xcb_connection(), cookie, 0);
698 if (reply && reply->type != XCB_NONE && reply->format == 32) {
699 int length = xcb_get_property_value_length(reply) / 4;
700 if (length > xdnd_max_type)
701 length = xdnd_max_type;
703 xcb_atom_t *atoms = (xcb_atom_t *)xcb_get_property_value(reply);
704 for (int i = 0; i < length; ++i)
705 xdnd_types.append(atoms[i]);
709 // get the types from the message
710 for(int i = 2; i < 5; i++) {
711 if (event->data.data32[i])
712 xdnd_types.append(event->data.data32[i]);
715 for(int i = 0; i < xdnd_types.length(); ++i)
716 DEBUG() << " " << connection()->atomName(xdnd_types.at(i));
719 void QXcbDrag::handle_xdnd_position(QWindow *w, const xcb_client_message_event_t *e)
721 QPoint p((e->data.data32[2] & 0xffff0000) >> 16, e->data.data32[2] & 0x0000ffff);
723 QRect geometry = w->geometry();
725 p -= geometry.topLeft();
727 if (!w || (w->windowType() == Qt::Desktop))
730 if (e->data.data32[0] != xdnd_dragsource) {
731 DEBUG("xdnd drag position from unexpected source (%x not %x)", e->data.data32[0], xdnd_dragsource);
738 // timestamp from the source
739 if (e->data.data32[3] != XCB_NONE) {
740 target_time = e->data.data32[3];
743 QMimeData *dropData = 0;
744 Qt::DropActions supported_actions = Qt::IgnoreAction;
746 dropData = currentDrag()->mimeData();
747 supported_actions = currentDrag()->supportedActions();
749 dropData = platformDropData();
750 supported_actions = Qt::DropActions(toDropAction(e->data.data32[4]));
753 QPlatformDragQtResponse qt_response = QWindowSystemInterface::handleDrag(w,dropData,p,supported_actions);
754 QRect answerRect(p + geometry.topLeft(), QSize(1,1));
755 answerRect = qt_response.answerRect().translated(geometry.topLeft()).intersected(geometry);
757 xcb_client_message_event_t response;
758 response.response_type = XCB_CLIENT_MESSAGE;
759 response.window = xdnd_dragsource;
760 response.format = 32;
761 response.type = atom(QXcbAtom::XdndStatus);
762 response.data.data32[0] = xcb_window(w);
763 response.data.data32[1] = qt_response.isAccepted(); // flags
764 response.data.data32[2] = 0; // x, y
765 response.data.data32[3] = 0; // w, h
766 response.data.data32[4] = toXdndAction(qt_response.acceptedAction()); // action
770 if (answerRect.left() < 0)
771 answerRect.setLeft(0);
772 if (answerRect.right() > 4096)
773 answerRect.setRight(4096);
774 if (answerRect.top() < 0)
775 answerRect.setTop(0);
776 if (answerRect.bottom() > 4096)
777 answerRect.setBottom(4096);
778 if (answerRect.width() < 0)
779 answerRect.setWidth(0);
780 if (answerRect.height() < 0)
781 answerRect.setHeight(0);
783 response.data.data32[4] = toXdndAction(qt_response.acceptedAction());
786 target_time = XCB_CURRENT_TIME;
788 if (xdnd_dragsource == connection()->clipboard()->owner())
789 handle_xdnd_status(&response);
791 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, xdnd_dragsource,
792 XCB_EVENT_MASK_NO_EVENT, (const char *)&response));
797 class ClientMessageScanner {
799 ClientMessageScanner(xcb_atom_t a) : atom(a) {}
801 bool checkEvent(xcb_generic_event_t *event) const {
804 if ((event->response_type & 0x7f) != XCB_CLIENT_MESSAGE)
806 return ((xcb_client_message_event_t *)event)->type == atom;
811 void QXcbDrag::handlePosition(QWindow * w, const xcb_client_message_event_t *event)
813 xcb_client_message_event_t *lastEvent = const_cast<xcb_client_message_event_t *>(event);
814 xcb_generic_event_t *nextEvent;
815 ClientMessageScanner scanner(atom(QXcbAtom::XdndPosition));
816 while ((nextEvent = connection()->checkEvent(scanner))) {
817 if (lastEvent != event)
819 lastEvent = (xcb_client_message_event_t *)nextEvent;
822 handle_xdnd_position(w, lastEvent);
823 if (lastEvent != event)
827 void QXcbDrag::handle_xdnd_status(const xcb_client_message_event_t *event)
829 DEBUG("xdndHandleStatus");
830 waiting_for_status = false;
831 // ignore late status messages
832 if (event->data.data32[0] && event->data.data32[0] != current_proxy_target)
835 const bool dropPossible = event->data.data32[1];
836 setCanDrop(dropPossible);
839 accepted_drop_action = toDropAction(event->data.data32[4]);
840 updateCursor(accepted_drop_action);
842 updateCursor(Qt::IgnoreAction);
845 if ((event->data.data32[1] & 2) == 0) {
846 QPoint p((event->data.data32[2] & 0xffff0000) >> 16, event->data.data32[2] & 0x0000ffff);
847 QSize s((event->data.data32[3] & 0xffff0000) >> 16, event->data.data32[3] & 0x0000ffff);
848 source_sameanswer = QRect(p, s);
850 source_sameanswer = QRect();
854 void QXcbDrag::handleStatus(const xcb_client_message_event_t *event)
856 if (event->window != connection()->clipboard()->owner())
859 xcb_client_message_event_t *lastEvent = const_cast<xcb_client_message_event_t *>(event);
860 xcb_generic_event_t *nextEvent;
861 ClientMessageScanner scanner(atom(QXcbAtom::XdndStatus));
862 while ((nextEvent = connection()->checkEvent(scanner))) {
863 if (lastEvent != event)
865 lastEvent = (xcb_client_message_event_t *)nextEvent;
868 handle_xdnd_status(lastEvent);
869 if (lastEvent != event)
871 DEBUG("xdndHandleStatus end");
874 void QXcbDrag::handleLeave(QWindow *w, const xcb_client_message_event_t *event)
877 if (!currentWindow || w != currentWindow.data())
881 // if (checkEmbedded(current_embedding_widget, event)) {
882 // current_embedding_widget = 0;
883 // currentWindow.clear();
887 if (event->data.data32[0] != xdnd_dragsource) {
888 // This often happens - leave other-process window quickly
889 DEBUG("xdnd drag leave from unexpected source (%x not %x", event->data.data32[0], xdnd_dragsource);
892 QWindowSystemInterface::handleDrag(w,0,QPoint(),Qt::IgnoreAction);
893 updateAction(Qt::IgnoreAction);
897 currentWindow.clear();
900 void QXcbDrag::send_leave()
906 xcb_client_message_event_t leave;
907 leave.response_type = XCB_CLIENT_MESSAGE;
908 leave.window = current_target;
910 leave.type = atom(QXcbAtom::XdndLeave);
911 leave.data.data32[0] = connection()->clipboard()->owner();
912 leave.data.data32[1] = 0; // flags
913 leave.data.data32[2] = 0; // x, y
914 leave.data.data32[3] = 0; // w, h
915 leave.data.data32[4] = 0; // just null
917 QXcbWindow *w = connection()->platformWindowFromId(current_proxy_target);
919 if (w && (w->window()->windowType() == Qt::Desktop) /*&& !w->acceptDrops()*/)
923 handleLeave(w->window(), (const xcb_client_message_event_t *)&leave);
925 xcb_send_event(xcb_connection(), false,current_proxy_target,
926 XCB_EVENT_MASK_NO_EVENT, (const char *)&leave);
929 current_proxy_target = 0;
930 source_time = XCB_CURRENT_TIME;
931 waiting_for_status = false;
934 void QXcbDrag::handleDrop(QWindow *, const xcb_client_message_event_t *event)
936 DEBUG("xdndHandleDrop");
937 if (!currentWindow) {
942 const uint32_t *l = event->data.data32;
946 if (l[0] != xdnd_dragsource) {
947 DEBUG("xdnd drop from unexpected source (%x not %x", l[0], xdnd_dragsource);
951 // update the "user time" from the timestamp in the event.
953 target_time = /*X11->userTime =*/ l[2];
955 // this could be a same-application drop, just proxied due to
956 // some XEMBEDding, so try to find the real QMimeData used
957 // based on the timestamp for this drop.
958 Qt::DropActions supported_drop_actions(l[4]);
959 QMimeData *dropData = 0;
961 dropData = currentDrag()->mimeData();
963 dropData = platformDropData();
969 // int at = findXdndDropTransactionByTime(target_time);
971 // dropData = QDragManager::dragPrivate(X11->dndDropTransactions.at(at).object)->data;
972 // if we can't find it, then use the data in the drag manager
974 QPlatformDropQtResponse response = QWindowSystemInterface::handleDrop(currentWindow.data(),dropData,currentPosition,supported_drop_actions);
975 setExecutedDropAction(response.acceptedAction());
977 xcb_client_message_event_t finished;
978 finished.response_type = XCB_CLIENT_MESSAGE;
979 finished.window = xdnd_dragsource;
980 finished.format = 32;
981 finished.type = atom(QXcbAtom::XdndFinished);
982 finished.data.data32[0] = currentWindow ? xcb_window(currentWindow.data()) : XCB_NONE;
983 finished.data.data32[1] = response.isAccepted(); // flags
984 finished.data.data32[2] = toXdndAction(response.acceptedAction());
985 Q_XCB_CALL(xcb_send_event(xcb_connection(), false, xdnd_dragsource,
986 XCB_EVENT_MASK_NO_EVENT, (char *)&finished));
989 currentWindow.clear();
990 waiting_for_status = false;
993 target_time = XCB_CURRENT_TIME;
997 void QXcbDrag::handleFinished(const xcb_client_message_event_t *event)
999 DEBUG("xdndHandleFinished");
1000 if (event->window != connection()->clipboard()->owner())
1003 const unsigned long *l = (const unsigned long *)event->data.data32;
1005 DNDDEBUG << "xdndHandleFinished, l[0]" << l[0]
1006 << "current_target" << current_target
1007 << "qt_xdnd_current_proxy_targe" << current_proxy_target;
1010 int at = findTransactionByWindow(l[0]);
1012 restartDropExpiryTimer();
1014 Transaction t = transactions.takeAt(at);
1015 // QDragManager *manager = QDragManager::self();
1017 // Window target = current_target;
1018 // Window proxy_target = current_proxy_target;
1019 // QWidget *embedding_widget = current_embedding_widget;
1020 // QDrag *currentObject = manager->object;
1022 // current_target = t.target;
1023 // current_proxy_target = t.proxy_target;
1024 // current_embedding_widget = t.embedding_widget;
1025 // manager->object = t.object;
1028 // (void) checkEmbedded(currentWindow, xe);
1030 // current_embedding_widget = 0;
1031 // current_target = 0;
1032 // current_proxy_target = 0;
1035 t.drag->deleteLater();
1037 // current_target = target;
1038 // current_proxy_target = proxy_target;
1039 // current_embedding_widget = embedding_widget;
1040 // manager->object = currentObject;
1043 waiting_for_status = false;
1047 void QXcbDrag::timerEvent(QTimerEvent* e)
1049 if (e->timerId() == heartbeat && source_sameanswer.isNull()) {
1050 QPointF pos = QCursor::pos();
1051 QMouseEvent me(QEvent::MouseMove, pos, pos, pos, Qt::LeftButton,
1052 QGuiApplication::mouseButtons(), QGuiApplication::keyboardModifiers());
1054 } else if (e->timerId() == transaction_expiry_timer) {
1055 for (int i = 0; i < transactions.count(); ++i) {
1056 const Transaction &t = transactions.at(i);
1057 if (t.targetWindow) {
1058 // dnd within the same process, don't delete these
1061 t.drag->deleteLater();
1062 transactions.removeAt(i--);
1065 killTimer(transaction_expiry_timer);
1066 transaction_expiry_timer = -1;
1070 void QXcbDrag::cancel()
1072 DEBUG("QXcbDrag::cancel");
1073 QBasicDrag::cancel();
1079 void QXcbDrag::handleSelectionRequest(const xcb_selection_request_event_t *event)
1081 xcb_selection_notify_event_t notify;
1082 notify.response_type = XCB_SELECTION_NOTIFY;
1083 notify.requestor = event->requestor;
1084 notify.selection = event->selection;
1085 notify.target = XCB_NONE;
1086 notify.property = XCB_NONE;
1087 notify.time = event->time;
1089 // which transaction do we use? (note: -2 means use current manager->object)
1092 // figure out which data the requestor is really interested in
1093 if (currentDrag() && event->time == source_time) {
1094 // requestor wants the current drag data
1097 // if someone has requested data in response to XdndDrop, find the corresponding transaction. the
1098 // spec says to call XConvertSelection() using the timestamp from the XdndDrop
1099 at = findTransactionByTime(event->time);
1101 // no dice, perhaps the client was nice enough to use the same window id in XConvertSelection()
1102 // that we sent the XdndDrop event to.
1103 at = findTransactionByWindow(event->requestor);
1105 // if (at == -1 && event->time == XCB_CURRENT_TIME) {
1106 // // previous Qt versions always requested the data on a child of the target window
1107 // // using CurrentTime... but it could be asking for either drop data or the current drag's data
1108 // Window target = findXdndAwareParent(event->requestor);
1110 // if (current_target && current_target == target)
1113 // at = findXdndDropTransactionByWindow(target);
1118 QDrag *transactionDrag = 0;
1120 restartDropExpiryTimer();
1122 transactionDrag = transactions.at(at).drag;
1124 if (transactionDrag) {
1125 xcb_atom_t atomFormat = event->target;
1128 if (QXcbMime::mimeDataForAtom(connection(), event->target, transactionDrag->mimeData(),
1129 &data, &atomFormat, &dataFormat)) {
1130 int dataSize = data.size() / (dataFormat / 8);
1131 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, event->requestor, event->property,
1132 atomFormat, dataFormat, dataSize, (const void *)data.constData());
1133 notify.property = event->property;
1134 notify.target = atomFormat;
1138 xcb_send_event(xcb_connection(), false, event->requestor, XCB_EVENT_MASK_NO_EVENT, (const char *)¬ify);
1142 bool QXcbDrag::dndEnable(QXcbWindow *w, bool on)
1144 DNDDEBUG << "xdndEnable" << w << on;
1146 QXcbWindow *xdnd_widget = 0;
1147 if ((w->window()->windowType() == Qt::Desktop)) {
1148 if (desktop_proxy) // *WE* already have one.
1151 xcb_grab_server(xcb_connection());
1153 // As per Xdnd4, use XdndProxy
1154 xcb_window_t proxy_id = xdndProxy(connection(), w->xcb_window());
1157 desktop_proxy = new QWindow;
1158 xdnd_widget = static_cast<QXcbWindow *>(desktop_proxy->handle());
1159 proxy_id = xdnd_widget->xcb_window();
1160 xcb_atom_t xdnd_proxy = atom(QXcbAtom::XdndProxy);
1161 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, w->xcb_window(), xdnd_proxy,
1162 XCB_ATOM_WINDOW, 32, 1, &proxy_id);
1163 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, proxy_id, xdnd_proxy,
1164 XCB_ATOM_WINDOW, 32, 1, &proxy_id);
1167 xcb_ungrab_server(xcb_connection());
1172 DNDDEBUG << "setting XdndAware for" << xdnd_widget << xdnd_widget->xcb_window();
1173 xcb_atom_t atm = xdnd_version;
1174 xcb_change_property(xcb_connection(), XCB_PROP_MODE_REPLACE, xdnd_widget->xcb_window(),
1175 atom(QXcbAtom::XdndAware), XCB_ATOM_ATOM, 32, 1, &atm);
1181 if ((w->window()->windowType() == Qt::Desktop)) {
1182 xcb_delete_property(xcb_connection(), w->xcb_window(), atom(QXcbAtom::XdndProxy));
1183 delete desktop_proxy;
1186 DNDDEBUG << "not deleting XDndAware";
1192 QXcbDropData::QXcbDropData(QXcbDrag *d)
1198 QXcbDropData::~QXcbDropData()
1202 QVariant QXcbDropData::retrieveData_sys(const QString &mimetype, QVariant::Type requestedType) const
1204 QByteArray mime = mimetype.toLatin1();
1205 QVariant data = /*X11->motifdnd_active
1206 ? X11->motifdndObtainData(mime)
1207 :*/ xdndObtainData(mime, requestedType);
1211 QVariant QXcbDropData::xdndObtainData(const QByteArray &format, QVariant::Type requestedType) const
1215 QXcbConnection *c = drag->connection();
1216 QXcbWindow *xcb_window = c->platformWindowFromId(drag->xdnd_dragsource);
1217 if (xcb_window && drag->currentDrag() && xcb_window->window()->windowType() != Qt::Desktop) {
1218 QMimeData *data = drag->currentDrag()->mimeData();
1219 if (data->hasFormat(QLatin1String(format)))
1220 result = data->data(QLatin1String(format));
1224 QList<xcb_atom_t> atoms = drag->xdnd_types;
1225 QByteArray encoding;
1226 xcb_atom_t a = mimeAtomForFormat(c, QLatin1String(format), requestedType, atoms, &encoding);
1230 if (c->clipboard()->getSelectionOwner(drag->atom(QXcbAtom::XdndSelection)) == XCB_NONE)
1231 return result; // should never happen?
1233 xcb_atom_t xdnd_selection = c->atom(QXcbAtom::XdndSelection);
1234 result = c->clipboard()->getSelection(xdnd_selection, a, xdnd_selection);
1236 return mimeConvertToFormat(c, a, result, QLatin1String(format), requestedType, encoding);
1240 bool QXcbDropData::hasFormat_sys(const QString &format) const
1242 return formats().contains(format);
1245 QStringList QXcbDropData::formats_sys() const
1247 QStringList formats;
1248 // if (X11->motifdnd_active) {
1251 // while (!(fmt = X11->motifdndFormat(i)).isEmpty()) {
1252 // formats.append(QLatin1String(fmt));
1256 for (int i = 0; i < drag->xdnd_types.size(); ++i) {
1257 QString f = mimeAtomToString(drag->connection(), drag->xdnd_types.at(i));
1258 if (!formats.contains(f))
1265 #endif // QT_NO_DRAGANDDROP