<commit_before>/*
 * Copyright (C) 2016  <NAME> (PCMan) <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/*
 * Note:
 * This is a workaround for the following Qt5 bugs.
 *
 * #49947: Drop events have broken mimeData()->urls() and text/uri-list.
 * #47981: Qt5.4 regression: Dropping text/urilist over browser windows stop working.
 *
 * Related LXQt bug: https://github.com/lxde/lxqt/issues/688
 *
 * This workaround is not 100% reliable, but it should work most of the time.
 * In theory, when there are multiple drag and drops at nearly the same time and
 * you are using a remote X11 instance via a slow network connection, this workaround
 * might break. However, that should be a really rare corner case.
 *
 * How this fix works:
 * 1. Hook QApplication to filter raw X11 events
 * 2. Intercept SelectionRequest events sent from XDnd target window.
 * 3. Check if the data requested have the type "text/uri-list" or "x-moz-url"
 * 4. Bypass the broken Qt5 code and send the mime data to the target with our own code.
 *
 * The mime data is obtained during the most recent mouse button release event.
 * This can be incorrect in some corner cases, but it is still a simple and
 * good enough approximation that returns the correct data most of the time.
 * Anyway, a workarond is just a workaround. Ask Qt developers to fix their bugs.
 */

#ifndef XDNDWORKAROUND_H
#define XDNDWORKAROUND_H

#include <QtGlobal>

#include <QObject>
#include <QAbstractNativeEventFilter>
#include <xcb/xcb.h>
#include <QByteArray>

class QDrag;

class XdndWorkaround : public QAbstractNativeEventFilter {
public:
    explicit XdndWorkaround();
    ~XdndWorkaround();
    bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) override;
    static QByteArray atomName(xcb_atom_t atom);
    static xcb_atom_t internAtom(const char* name, int len = -1);
    static QByteArray windowProperty(xcb_window_t window, xcb_atom_t propAtom, xcb_atom_t typeAtom, int len);
    static void setWindowProperty(xcb_window_t window, xcb_atom_t propAtom, xcb_atom_t typeAtom, void* data, int len, int format = 8);

private:
    bool clientMessage(xcb_client_message_event_t* event);
    bool selectionNotify(xcb_selection_notify_event_t* event);

// This part is for Qt >= 5.4 only
#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
private:
    bool selectionRequest(xcb_selection_request_event_t* event);
    bool genericEvent(xcb_ge_generic_event_t* event);
    // _QBasicDrag* xcbDrag() const;
    void buttonRelease();

    QDrag* lastDrag_;
    // xinput related
    bool xinput2Enabled_;
    int xinputOpCode_;
    int xinputEventBase_;
    int xinputErrorBase_;
#endif // Qt >= 5.4
};

#endif // XDNDWORKAROUND_H<commit_msg>Prevent a potential crash in xdndworkaround<commit_after>/*
 * Copyright (C) 2016  <NAME> (PCMan) <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/*
 * Note:
 * This is a workaround for the following Qt5 bugs.
 *
 * #49947: Drop events have broken mimeData()->urls() and text/uri-list.
 * #47981: Qt5.4 regression: Dropping text/urilist over browser windows stop working.
 *
 * Related LXQt bug: https://github.com/lxde/lxqt/issues/688
 *
 * This workaround is not 100% reliable, but it should work most of the time.
 * In theory, when there are multiple drag and drops at nearly the same time and
 * you are using a remote X11 instance via a slow network connection, this workaround
 * might break. However, that should be a really rare corner case.
 *
 * How this fix works:
 * 1. Hook QApplication to filter raw X11 events
 * 2. Intercept SelectionRequest events sent from XDnd target window.
 * 3. Check if the data requested have the type "text/uri-list" or "x-moz-url"
 * 4. Bypass the broken Qt5 code and send the mime data to the target with our own code.
 *
 * The mime data is obtained during the most recent mouse button release event.
 * This can be incorrect in some corner cases, but it is still a simple and
 * good enough approximation that returns the correct data most of the time.
 * Anyway, a workarond is just a workaround. Ask Qt developers to fix their bugs.
 */

#ifndef XDNDWORKAROUND_H
#define XDNDWORKAROUND_H

#include <QtGlobal>

#include <QObject>
#include <QAbstractNativeEventFilter>
#include <xcb/xcb.h>
#include <QByteArray>
#include <QPointer>

class QDrag;

class XdndWorkaround : public QAbstractNativeEventFilter {
public:
    explicit XdndWorkaround();
    ~XdndWorkaround();
    bool nativeEventFilter(const QByteArray& eventType, void* message, long* result) override;
    static QByteArray atomName(xcb_atom_t atom);
    static xcb_atom_t internAtom(const char* name, int len = -1);
    static QByteArray windowProperty(xcb_window_t window, xcb_atom_t propAtom, xcb_atom_t typeAtom, int len);
    static void setWindowProperty(xcb_window_t window, xcb_atom_t propAtom, xcb_atom_t typeAtom, void* data, int len, int format = 8);

private:
    bool clientMessage(xcb_client_message_event_t* event);
    bool selectionNotify(xcb_selection_notify_event_t* event);

// This part is for Qt >= 5.4 only
#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
private:
    bool selectionRequest(xcb_selection_request_event_t* event);
    bool genericEvent(xcb_ge_generic_event_t* event);
    // _QBasicDrag* xcbDrag() const;
    void buttonRelease();

    QPointer<QDrag> lastDrag_;
    // xinput related
    bool xinput2Enabled_;
    int xinputOpCode_;
    int xinputEventBase_;
    int xinputErrorBase_;
#endif // Qt >= 5.4
};

#endif // XDNDWORKAROUND_H
{'fraction_non_alphanumeric': 0.07620147207389234, 'fraction_numerical': 0.015875306682060903, 'mean_word_length': 4.103092783505154, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '74414', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2042, 'n_words': 870}