summaryrefslogtreecommitdiff
path: root/src/plugins/platforms/winrt/qwinrtdrag.cpp
blob: 56785072034226a7ec54a5b34e1891528c7a1bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/
#include "qwinrtdrag.h"

#include <QtCore/qglobal.h>
#include <QtCore/QMimeData>
#include <QtCore/QStringList>
#include <QtGui/QGuiApplication>
#include <qpa/qwindowsysteminterface.h>

#include <qfunctions_winrt.h>
#include <private/qeventdispatcher_winrt_p.h>

#include <Windows.ApplicationModel.datatransfer.h>
#include <windows.ui.xaml.h>
#include <windows.foundation.collections.h>
#include <windows.graphics.imaging.h>
#include <windows.storage.streams.h>
#include <functional>
#include <robuffer.h>

using namespace ABI::Windows::ApplicationModel::DataTransfer;
using namespace ABI::Windows::ApplicationModel::DataTransfer::DragDrop;
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::Graphics::Imaging;
using namespace ABI::Windows::Storage;
using namespace ABI::Windows::Storage::Streams;
using namespace ABI::Windows::UI::Xaml;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcQpaMime, "qt.qpa.mime")

ComPtr<IBuffer> createIBufferFromData(const char *data, qint32 size)
{
    static ComPtr<IBufferFactory> bufferFactory;
    HRESULT hr;
    if (!bufferFactory) {
        hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Storage_Streams_Buffer).Get(),
                                    IID_PPV_ARGS(&bufferFactory));
        Q_ASSERT_SUCCEEDED(hr);
    }

    ComPtr<IBuffer> buffer;
    const UINT32 length = UINT32(size);
    hr = bufferFactory->Create(length, &buffer);
    Q_ASSERT_SUCCEEDED(hr);
    hr = buffer->put_Length(length);
    Q_ASSERT_SUCCEEDED(hr);

    ComPtr<Windows::Storage::Streams::IBufferByteAccess> byteArrayAccess;
    hr = buffer.As(&byteArrayAccess);
    Q_ASSERT_SUCCEEDED(hr);

    byte *bytes;
    hr = byteArrayAccess->Buffer(&bytes);
    Q_ASSERT_SUCCEEDED(hr);
    memcpy(bytes, data, length);
    return buffer;
}

class DragThreadTransferData : public QObject
{
    Q_OBJECT
public slots:
    void handleDrag();
public:
    explicit DragThreadTransferData(QObject *parent = nullptr);
    QWindow *window;
    QWinRTInternalMimeData *mime;
    QPoint point;
    Qt::DropActions actions;
    bool dropAction;
    ComPtr<IDragEventArgs> nativeArgs;
    ComPtr<IDragOperationDeferral> deferral;
};

inline QString hStringToQString(const HString &hString)
{
    quint32 l;
    const wchar_t *raw = hString.GetRawBuffer(&l);
    return (QString::fromWCharArray(raw, int(l)));
}

inline HString qStringToHString(const QString &qString)
{
    HString h;
    h.Set(reinterpret_cast<const wchar_t*>(qString.utf16()), uint(qString.size()));
    return h;
}

namespace NativeFormatStrings {
    static ComPtr<IStandardDataFormatsStatics> dataStatics;
    static HSTRING text; // text/plain
    static HSTRING html; // text/html
    static HSTRING storage; // text/uri-list
}

static inline DataPackageOperation translateFromQDragDropActions(const Qt::DropAction action)
{
    switch (action) {
    case Qt::CopyAction:
        return DataPackageOperation_Copy;
    case Qt::MoveAction:
        return DataPackageOperation_Move;
    case Qt::LinkAction:
        return DataPackageOperation_Link;
    case Qt::IgnoreAction:
    default:
        return DataPackageOperation_None;
    }
}

static inline Qt::DropActions translateToQDragDropActions(const DataPackageOperation op)
{
    Qt::DropActions actions = Qt::IgnoreAction;
    // None needs to be interpreted as the sender being able to handle
    // anything and let the receiver decide
    if (op == DataPackageOperation_None)
        actions = Qt::LinkAction | Qt::CopyAction | Qt::MoveAction;
    if (op & DataPackageOperation_Link)
        actions |= Qt::LinkAction;
    if (op & DataPackageOperation_Copy)
        actions |= Qt::CopyAction;
    if (op & DataPackageOperation_Move)
        actions |= Qt::MoveAction;
    return actions;
}

QWinRTInternalMimeData::QWinRTInternalMimeData()
    : QInternalMimeData()
{
    qCDebug(lcQpaMime) << __FUNCTION__;
    if (!NativeFormatStrings::dataStatics) {
        HRESULT hr;
        hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_ApplicationModel_DataTransfer_StandardDataFormats).Get(),
                                    IID_PPV_ARGS(&NativeFormatStrings::dataStatics));
        Q_ASSERT_SUCCEEDED(hr);
        hr = NativeFormatStrings::dataStatics->get_Text(&NativeFormatStrings::text);
        Q_ASSERT_SUCCEEDED(hr);
        hr = NativeFormatStrings::dataStatics->get_Html(&NativeFormatStrings::html);
        Q_ASSERT_SUCCEEDED(hr);
        hr = NativeFormatStrings::dataStatics->get_StorageItems(&NativeFormatStrings::storage);
        Q_ASSERT_SUCCEEDED(hr);
    }
}

bool QWinRTInternalMimeData::hasFormat_sys(const QString &mimetype) const
{
    qCDebug(lcQpaMime) << __FUNCTION__ << mimetype;

    if (!dataView)
        return false;

    return formats_sys().contains(mimetype);
}

QStringList QWinRTInternalMimeData::formats_sys() const
{
    qCDebug(lcQpaMime) << __FUNCTION__;

    if (!dataView)
        return QStringList();

    if (!formats.isEmpty())
        return formats;

    HRESULT hr;
    hr = QEventDispatcherWinRT::runOnXamlThread([this]() {
        boolean contains;
        HRESULT hr;
        hr = dataView->Contains(NativeFormatStrings::text, &contains);
        if (SUCCEEDED(hr) && contains)
            formats.append(QLatin1String("text/plain"));

        hr = dataView->Contains(NativeFormatStrings::html, &contains);
        if (SUCCEEDED(hr) && contains)
            formats.append(QLatin1String("text/html"));

        hr = dataView->Contains(NativeFormatStrings::storage, &contains);
        if (SUCCEEDED(hr) && contains)
            formats.append(QLatin1String("text/uri-list"));

        // We need to add any additional format as well, for legacy windows
        // reasons, but also in case someone adds custom formats.
        ComPtr<IVectorView<HSTRING>> availableFormats;
        hr = dataView->get_AvailableFormats(&availableFormats);
        RETURN_OK_IF_FAILED("Could not query available formats.");

        quint32 size;
        hr = availableFormats->get_Size(&size);
        RETURN_OK_IF_FAILED("Could not query format vector size.");
        for (quint32 i = 0; i < size; ++i) {
            HString str;
            hr = availableFormats->GetAt(i, str.GetAddressOf());
            if (FAILED(hr))
                continue;
            formats.append(hStringToQString(str));
        }
        return S_OK;
    });
    Q_ASSERT_SUCCEEDED(hr);

    return formats;
}

QVariant QWinRTInternalMimeData::retrieveData_sys(const QString &mimetype, QVariant::Type preferredType) const
{
    qCDebug(lcQpaMime) << __FUNCTION__ << mimetype << preferredType;

    if (!dataView || !formats.contains(mimetype))
        return QVariant();

    QVariant result;
    HRESULT hr;
    if (mimetype == QLatin1String("text/plain")) {
        hr = QEventDispatcherWinRT::runOnXamlThread([this, &result]() {
            HRESULT hr;
            ComPtr<IAsyncOperation<HSTRING>> op;
            HString res;
            hr = dataView->GetTextAsync(&op);
            Q_ASSERT_SUCCEEDED(hr);
            hr = QWinRTFunctions::await(op, res.GetAddressOf());
            Q_ASSERT_SUCCEEDED(hr);
            result.setValue(hStringToQString(res));
            return S_OK;
        });
    } else if (mimetype == QLatin1String("text/uri-list")) {
        hr = QEventDispatcherWinRT::runOnXamlThread([this, &result]() {
            HRESULT hr;
            ComPtr<IAsyncOperation<IVectorView<IStorageItem*>*>> op;
            hr = dataView->GetStorageItemsAsync(&op);
            Q_ASSERT_SUCCEEDED(hr);
            ComPtr<IVectorView<IStorageItem*>> nativeItems;
            hr = QWinRTFunctions::await(op, nativeItems.GetAddressOf());
            Q_ASSERT_SUCCEEDED(hr);
            QList<QVariant> items;
            quint32 count;
            hr = nativeItems->get_Size(&count);
            for (quint32 i = 0; i < count; ++i) {
                ComPtr<IStorageItem> item;
                hr = nativeItems->GetAt(i, &item);
                Q_ASSERT_SUCCEEDED(hr);
                HString path;
                hr = item->get_Path(path.GetAddressOf());
                Q_ASSERT_SUCCEEDED(hr);
                items.append(QUrl::fromLocalFile(hStringToQString(path)));
            }
            result.setValue(items);
            return S_OK;
        });
    } else if (mimetype == QLatin1String("text/html")) {
        hr = QEventDispatcherWinRT::runOnXamlThread([this, &result]() {
            HRESULT hr;
            ComPtr<IAsyncOperation<HSTRING>> op;
            HString res;
            hr = dataView->GetHtmlFormatAsync(&op);
            Q_ASSERT_SUCCEEDED(hr);
            hr = QWinRTFunctions::await(op, res.GetAddressOf());
            Q_ASSERT_SUCCEEDED(hr);
            result.setValue(hStringToQString(res));
            return S_OK;
        });
    } else {
        // Asking for custom data
        hr = QEventDispatcherWinRT::runOnXamlThread([this, &result, mimetype]() {
            HRESULT hr;
            ComPtr<IAsyncOperation<IInspectable*>> op;
            ComPtr<IInspectable> res;
            HString type;
            type.Set(reinterpret_cast<const wchar_t*>(mimetype.utf16()), uint(mimetype.size()));
            hr = dataView->GetDataAsync(type.Get(), &op);
            RETURN_OK_IF_FAILED("Could not query custom drag data.");
            hr = QWinRTFunctions::await(op, res.GetAddressOf());
            if (FAILED(hr) || !res) {
                qCDebug(lcQpaMime) << "Custom drop data operation returned no results or failed.";
                return S_OK;
            }

            // Test for properties
            ComPtr<IPropertyValue> propertyValue;
            hr = res.As(&propertyValue);
            if (SUCCEEDED(hr)) {
                // We need to check which type of custom data we are receiving
                PropertyType propertyType;
                propertyValue->get_Type(&propertyType);
                switch (propertyType) {
                case PropertyType_UInt8: {
                    quint8 v;
                    hr = propertyValue->GetUInt8(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_Int16: {
                    qint16 v;
                    hr = propertyValue->GetInt16(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_UInt16: {
                    quint16 v;
                    hr = propertyValue->GetUInt16(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_Int32: {
                    qint32 v;
                    hr = propertyValue->GetInt32(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_UInt32: {
                    quint32 v;
                    hr = propertyValue->GetUInt32(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_Int64: {
                    qint64 v;
                    hr = propertyValue->GetInt64(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_UInt64: {
                    quint64 v;
                    hr = propertyValue->GetUInt64(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_Single: {
                    float v;
                    hr = propertyValue->GetSingle(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_Double: {
                    double v;
                    hr = propertyValue->GetDouble(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_Char16: {
                    wchar_t v;
                    hr = propertyValue->GetChar16(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(QString::fromWCharArray(&v, 1));
                    return S_OK;
                }
                case PropertyType_Boolean: {
                    boolean v;
                    hr = propertyValue->GetBoolean(&v);
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(v);
                    return S_OK;
                }
                case PropertyType_String: {
                    HString stringProperty;
                    hr = propertyValue->GetString(stringProperty.GetAddressOf());
                    Q_ASSERT_SUCCEEDED(hr);
                    result.setValue(hStringToQString(stringProperty));
                    return S_OK;
                }
                default:
                    qCDebug(lcQpaMime) << "Unknown property type dropped:" << propertyType;
                }
                return S_OK;
            }

            // Custom data can be read via input streams
            ComPtr<IRandomAccessStream> randomAccessStream;
            hr = res.As(&randomAccessStream);
            if (SUCCEEDED(hr)) {
                UINT64 size;
                hr = randomAccessStream->get_Size(&size);
                Q_ASSERT_SUCCEEDED(hr);
                ComPtr<IInputStream> stream;
                hr = randomAccessStream.As(&stream);
                Q_ASSERT_SUCCEEDED(hr);

                ComPtr<IBufferFactory> bufferFactory;
                hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Storage_Streams_Buffer).Get(),
                                            IID_PPV_ARGS(&bufferFactory));
                Q_ASSERT_SUCCEEDED(hr);

                UINT32 length = UINT32(qBound(quint64(0), quint64(size), quint64(UINT_MAX)));
                ComPtr<IBuffer> buffer;
                hr = bufferFactory->Create(length, &buffer);
                Q_ASSERT_SUCCEEDED(hr);

                ComPtr<IAsyncOperationWithProgress<IBuffer *, UINT32>> readOp;
                hr = stream->ReadAsync(buffer.Get(), length, InputStreamOptions_None, &readOp);

                ComPtr<IBuffer> effectiveBuffer;
                hr = QWinRTFunctions::await(readOp, effectiveBuffer.GetAddressOf());

                hr = effectiveBuffer->get_Length(&length);

                ComPtr<Windows::Storage::Streams::IBufferByteAccess> byteArrayAccess;
                hr = effectiveBuffer.As(&byteArrayAccess);

                byte *bytes;
                hr = byteArrayAccess->Buffer(&bytes);
                QByteArray array((char *)bytes, int(length));
                result.setValue(array);
                return S_OK;
            }

            HSTRING runtimeClass;
            hr = res->GetRuntimeClassName(&runtimeClass);
            Q_ASSERT_SUCCEEDED(hr);
            HString converted;
            converted.Set(runtimeClass);
            qCDebug(lcQpaMime) << "Unknown drop data type received (" << hStringToQString(converted)
                               << "). Ignoring...";
            return S_OK;
        });
    }
    return result;
}

void QWinRTInternalMimeData::setDataView(const Microsoft::WRL::ComPtr<IDataPackageView> &d)
{
    dataView = d;
    formats.clear();
}

static HRESULT qt_drag_enter(IInspectable *sender, ABI::Windows::UI::Xaml::IDragEventArgs *e)
{
    QWinRTDrag::instance()->handleNativeDragEvent(sender, e);
    return S_OK;
}

static HRESULT qt_drag_over(IInspectable *sender, ABI::Windows::UI::Xaml::IDragEventArgs *e)
{
    QWinRTDrag::instance()->handleNativeDragEvent(sender, e);
    return S_OK;
}

static HRESULT qt_drag_leave(IInspectable *sender, ABI::Windows::UI::Xaml::IDragEventArgs *e)
{
    // Qt internally checks for new drags and auto sends leave events
    // Also there is no QPA function for handling leave
    Q_UNUSED(sender);
    Q_UNUSED(e);
    return S_OK;
}

static HRESULT qt_drop(IInspectable *sender, ABI::Windows::UI::Xaml::IDragEventArgs *e)
{
    QWinRTDrag::instance()->handleNativeDragEvent(sender, e, true);
    return S_OK;
}

#define Q_DECLARE_DRAGHANDLER(name,func) \
class QtDragEventHandler##name : public IDragEventHandler \
{ \
public: \
    virtual ~QtDragEventHandler##name() {\
    }\
    STDMETHODIMP Invoke(IInspectable *sender, \
                        ABI::Windows::UI::Xaml::IDragEventArgs *e) \
    { \
        return qt_##func(sender, e);\
    } \
 \
    STDMETHODIMP \
    QueryInterface(REFIID riid, void FAR* FAR* ppvObj) \
    { \
        if (riid == IID_IUnknown || riid == IID_IDragEventHandler) { \
            *ppvObj = this; \
            AddRef(); \
            return NOERROR; \
        } \
        *ppvObj = NULL; \
        return ResultFromScode(E_NOINTERFACE); \
    } \
 \
    STDMETHODIMP_(ULONG) \
    AddRef(void) \
    { \
        return ++m_refs; \
    } \
 \
    STDMETHODIMP_(ULONG) \
    Release(void) \
    { \
        if (--m_refs == 0) { \
            delete this; \
            return 0; \
        } \
        return m_refs; \
    } \
private: \
ULONG m_refs{0}; \
};

Q_DECLARE_DRAGHANDLER(Enter, drag_enter)
Q_DECLARE_DRAGHANDLER(Over, drag_over)
Q_DECLARE_DRAGHANDLER(Leave, drag_leave)
Q_DECLARE_DRAGHANDLER(Drop, drop)

#define Q_INST_DRAGHANDLER(name) QtDragEventHandler##name()

Q_GLOBAL_STATIC(QWinRTDrag, gDrag);

extern ComPtr<ABI::Windows::UI::Input::IPointerPoint> qt_winrt_lastPointerPoint; // qwinrtscreen.cpp

QWinRTDrag::QWinRTDrag()
    : QPlatformDrag()
    , m_dragTarget(nullptr)
{
    qCDebug(lcQpaMime) << __FUNCTION__;
    m_enter = new Q_INST_DRAGHANDLER(Enter);
    m_over = new Q_INST_DRAGHANDLER(Over);
    m_leave = new Q_INST_DRAGHANDLER(Leave);
    m_drop = new Q_INST_DRAGHANDLER(Drop);
    m_mimeData = new QWinRTInternalMimeData;
}

QWinRTDrag::~QWinRTDrag()
{
    qCDebug(lcQpaMime) << __FUNCTION__;
    delete m_enter;
    delete m_over;
    delete m_leave;
    delete m_drop;
    delete m_mimeData;
}

QWinRTDrag *QWinRTDrag::instance()
{
    return gDrag;
}

inline HRESULT resetUiElementDrag(ComPtr<IUIElement3> &elem3, EventRegistrationToken startingToken)
{
    return QEventDispatcherWinRT::runOnXamlThread([elem3, startingToken]() {
        HRESULT hr;
        hr = elem3->put_CanDrag(false);
        Q_ASSERT_SUCCEEDED(hr);
        hr = elem3->remove_DragStarting(startingToken);
        Q_ASSERT_SUCCEEDED(hr);
        return S_OK;
    });
}

Qt::DropAction QWinRTDrag::drag(QDrag *drag)
{
    qCDebug(lcQpaMime) << __FUNCTION__ << drag;

    if (!qt_winrt_lastPointerPoint) {
        Q_ASSERT_X(qt_winrt_lastPointerPoint, Q_FUNC_INFO, "No pointerpoint known");
        return Qt::IgnoreAction;
    }

    ComPtr<IUIElement3> elem3;
    HRESULT hr = m_ui.As(&elem3);
    Q_ASSERT_SUCCEEDED(hr);

    ComPtr<IAsyncOperation<ABI::Windows::ApplicationModel::DataTransfer::DataPackageOperation>> op;
    EventRegistrationToken startingToken;

    hr = QEventDispatcherWinRT::runOnXamlThread([drag, &op, &hr, elem3, &startingToken]() {

        hr = elem3->put_CanDrag(true);
        Q_ASSERT_SUCCEEDED(hr);

        auto startingCallback = Callback<ITypedEventHandler<UIElement*, DragStartingEventArgs*>> ([drag](IInspectable *, IDragStartingEventArgs *args) {
            qCDebug(lcQpaMime) << "Drag starting" << args;

            ComPtr<IDataPackage> dataPackage;
            HRESULT hr;
            hr = args->get_Data(dataPackage.GetAddressOf());
            Q_ASSERT_SUCCEEDED(hr);
            Qt::DropAction action = drag->defaultAction();
            hr = dataPackage->put_RequestedOperation(translateFromQDragDropActions(action));
            Q_ASSERT_SUCCEEDED(hr);

#ifndef QT_WINRT_LIMITED_DRAGANDDROP
            ComPtr<IDragStartingEventArgs2> args2;
            hr = args->QueryInterface(IID_PPV_ARGS(&args2));
            Q_ASSERT_SUCCEEDED(hr);

            Qt::DropActions actions = drag->supportedActions();
            DataPackageOperation allowedOperations = DataPackageOperation_None;
            if (actions & Qt::CopyAction)
                allowedOperations |= DataPackageOperation_Copy;
            if (actions & Qt::MoveAction)
                allowedOperations |= DataPackageOperation_Move;
            if (actions & Qt::LinkAction)
                allowedOperations |= DataPackageOperation_Link;
            hr = args2->put_AllowedOperations(allowedOperations);
            Q_ASSERT_SUCCEEDED(hr);
#endif // QT_WINRT_LIMITED_DRAGANDDROP
            QMimeData *mimeData = drag->mimeData();
            if (mimeData->hasText()) {
                hr = dataPackage->SetText(qStringToHString(mimeData->text()).Get());
                Q_ASSERT_SUCCEEDED(hr);
            }
            if (mimeData->hasHtml()) {
                hr = dataPackage->SetHtmlFormat(qStringToHString(mimeData->html()).Get());
                Q_ASSERT_SUCCEEDED(hr);
            }
            // ### TODO: Missing: weblink, image

            if (!drag->pixmap().isNull()) {
                const QImage image2 = drag->pixmap().toImage();
                const QImage image = image2.convertToFormat(QImage::Format_ARGB32);
                if (!image.isNull()) {
                    // Create IBuffer containing image
                    ComPtr<IBuffer> imageBuffer
                            = createIBufferFromData(reinterpret_cast<const char*>(image.bits()), int(image.sizeInBytes()));

                    ComPtr<ISoftwareBitmapFactory> bitmapFactory;
                    hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Graphics_Imaging_SoftwareBitmap).Get(),
                                                IID_PPV_ARGS(&bitmapFactory));
                    Q_ASSERT_SUCCEEDED(hr);

                    ComPtr<ISoftwareBitmap> bitmap;
                    hr = bitmapFactory->Create(BitmapPixelFormat_Rgba8, image.width(), image.height(), &bitmap);
                    Q_ASSERT_SUCCEEDED(hr);

                    hr = bitmap->CopyFromBuffer(imageBuffer.Get());
                    Q_ASSERT_SUCCEEDED(hr);

                    ComPtr<IDragUI> dragUi;
                    hr = args->get_DragUI(dragUi.GetAddressOf());
                    Q_ASSERT_SUCCEEDED(hr);

                    hr = dragUi->SetContentFromSoftwareBitmap(bitmap.Get());
                    Q_ASSERT_SUCCEEDED(hr);
                }
            }

            const QStringList formats = mimeData->formats();
            for (auto item : formats) {
                QByteArray data = mimeData->data(item);

                ComPtr<IBuffer> buffer = createIBufferFromData(data.constData(), data.size());

                // We cannot push the buffer to the data package as the result on
                // recipient side is different from native events. It still sends a
                // buffer, but that potentially cannot be parsed. Hence we need to create
                // a IRandomAccessStream which gets forwarded as is to the drop side.
                ComPtr<IRandomAccessStream> ras;
                hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_Storage_Streams_InMemoryRandomAccessStream).Get(), &ras);
                Q_ASSERT_SUCCEEDED(hr);

                hr = ras->put_Size(UINT64(data.size()));
                ComPtr<IOutputStream> outputStream;
                hr = ras->GetOutputStreamAt(0, &outputStream);
                Q_ASSERT_SUCCEEDED(hr);

                ComPtr<IAsyncOperationWithProgress<UINT32,UINT32>> writeOp;
                hr = outputStream->WriteAsync(buffer.Get(), &writeOp);
                Q_ASSERT_SUCCEEDED(hr);

                UINT32 result;
                hr = QWinRTFunctions::await(writeOp, &result);
                Q_ASSERT_SUCCEEDED(hr);

                unsigned char flushResult;
                ComPtr<IAsyncOperation<bool>> flushOp;
                hr = outputStream->FlushAsync(&flushOp);
                Q_ASSERT_SUCCEEDED(hr);

                hr = QWinRTFunctions::await(flushOp, &flushResult);
                Q_ASSERT_SUCCEEDED(hr);

                hr = dataPackage->SetData(qStringToHString(item).Get(), ras.Get());
                Q_ASSERT_SUCCEEDED(hr);
            }
            return S_OK;
        });

        hr = elem3->add_DragStarting(startingCallback.Get(), &startingToken);
        Q_ASSERT_SUCCEEDED(hr);

        hr = elem3->StartDragAsync(qt_winrt_lastPointerPoint.Get(), &op);
        Q_ASSERT_SUCCEEDED(hr);

        return hr;
    });
    if (!op || FAILED(hr)) {
        qCDebug(lcQpaMime) << "Drag failed:" << hr;
        hr = resetUiElementDrag(elem3, startingToken);
        Q_ASSERT_SUCCEEDED(hr);
        return Qt::IgnoreAction;
    }

    DataPackageOperation nativeOperationType;
    // Do not yield, as that can cause deadlocks when dropping inside the same app
    hr = QWinRTFunctions::await(op, &nativeOperationType, QWinRTFunctions::ProcessThreadEvents);
    Q_ASSERT_SUCCEEDED(hr);

    hr = resetUiElementDrag(elem3, startingToken);
    Q_ASSERT_SUCCEEDED(hr);

    Qt::DropAction resultAction;
    switch (nativeOperationType) {
    case DataPackageOperation_Link:
        resultAction = Qt::LinkAction;
        break;
    case DataPackageOperation_Copy:
        resultAction = Qt::CopyAction;
        break;
    case DataPackageOperation_Move:
        resultAction = Qt::MoveAction;
        break;
    case DataPackageOperation_None:
    default:
        resultAction = Qt::IgnoreAction;
        break;
    }

    return resultAction;
}

void QWinRTDrag::setDropTarget(QWindow *target)
{
    qCDebug(lcQpaMime) << __FUNCTION__ << target;
    m_dragTarget = target;
}

void QWinRTDrag::setUiElement(ComPtr<ABI::Windows::UI::Xaml::IUIElement> &element)
{
    qCDebug(lcQpaMime) << __FUNCTION__;
    m_ui = element;
    // We set the element to always accept drops and then evaluate during
    // runtime
    HRESULT hr = element->put_AllowDrop(TRUE);
    EventRegistrationToken tok;
    hr = element->add_DragEnter(m_enter, &tok);
    RETURN_VOID_IF_FAILED("Failed to add DragEnter handler.");
    hr = element->add_DragOver(m_over, &tok);
    RETURN_VOID_IF_FAILED("Failed to add DragOver handler.");
    hr = element->add_DragLeave(m_leave, &tok);
    RETURN_VOID_IF_FAILED("Failed to add DragLeave handler.");
    hr = element->add_Drop(m_drop, &tok);
    RETURN_VOID_IF_FAILED("Failed to add Drop handler.");
}

void QWinRTDrag::handleNativeDragEvent(IInspectable *sender, ABI::Windows::UI::Xaml::IDragEventArgs *e, bool drop)
{
    Q_UNUSED(sender);

    if (!m_dragTarget)
        return;

    HRESULT hr;
    Point relativePoint;
    hr = e->GetPosition(m_ui.Get(), &relativePoint);
    RETURN_VOID_IF_FAILED("Could not query drag position.");
    const QPoint p(int(relativePoint.X), int(relativePoint.Y));

    ComPtr<IDragEventArgs2> e2;
    hr = e->QueryInterface(IID_PPV_ARGS(&e2));
    RETURN_VOID_IF_FAILED("Could not convert drag event args");

    DragDropModifiers modifiers;
    hr = e2->get_Modifiers(&modifiers);

#ifndef QT_WINRT_LIMITED_DRAGANDDROP
    ComPtr<IDragEventArgs3> e3;
    hr = e->QueryInterface(IID_PPV_ARGS(&e3));
    Q_ASSERT_SUCCEEDED(hr);

    DataPackageOperation dataOp;
    hr = e3->get_AllowedOperations(&dataOp);
    if (FAILED(hr))
        qCDebug(lcQpaMime) << __FUNCTION__ << "Could not query drag operations";

    const Qt::DropActions actions = translateToQDragDropActions(dataOp);
#else // !QT_WINRT_LIMITED_DRAGANDDROP
    const Qt::DropActions actions = Qt::LinkAction | Qt::CopyAction | Qt::MoveAction;;
#endif // !QT_WINRT_LIMITED_DRAGANDDROP

    ComPtr<IDataPackageView> dataView;
    hr = e2->get_DataView(&dataView);
    Q_ASSERT_SUCCEEDED(hr);

    m_mimeData->setDataView(dataView);

    // We use deferral as we need to jump to the Qt thread to handle
    // the drag event
    ComPtr<IDragOperationDeferral> deferral;
    hr = e2->GetDeferral(&deferral);
    Q_ASSERT_SUCCEEDED(hr);

    DragThreadTransferData *transferData = new DragThreadTransferData;
    transferData->moveToThread(qGuiApp->thread());
    transferData->window = m_dragTarget;
    transferData->point = p;
    transferData->mime = m_mimeData;
    transferData->actions = actions;
    transferData->dropAction = drop;
    transferData->nativeArgs = e;
    transferData->deferral = deferral;
    QMetaObject::invokeMethod(transferData, "handleDrag", Qt::QueuedConnection);
}

DragThreadTransferData::DragThreadTransferData(QObject *parent)
    : QObject(parent)
    , dropAction(false)
{
}

void DragThreadTransferData::handleDrag()
{
    bool accepted = false;
    Qt::DropAction acceptedAction;
    if (dropAction) {
        QPlatformDropQtResponse response = QWindowSystemInterface::handleDrop(window, mime, point, actions);
        accepted = response.isAccepted();
        acceptedAction = response.acceptedAction();
    } else {
        QPlatformDragQtResponse response = QWindowSystemInterface::handleDrag(window, mime, point, actions);
        accepted = response.isAccepted();
        acceptedAction = response.acceptedAction();
    }

    HRESULT hr;
    hr = QEventDispatcherWinRT::runOnXamlThread([accepted, acceptedAction, this]() {
        HRESULT hr;
        hr = nativeArgs->put_Handled(accepted);
        if (acceptedAction != Qt::IgnoreAction) {
            ComPtr<IDragEventArgs2> e2;
            hr = nativeArgs.As(&e2);
            if (SUCCEEDED(hr))
                hr = e2->put_AcceptedOperation(translateFromQDragDropActions(acceptedAction));
        }
        deferral->Complete();
        return S_OK;
    });
    Q_ASSERT_SUCCEEDED(hr);
    deleteLater();
}

QT_END_NAMESPACE

#include "qwinrtdrag.moc"