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
|
/*
* This file is part of mpv.
*
* mpv 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.
*
* mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdatomic.h>
#include <windows.h>
#include <ole2.h>
#include <shellapi.h>
#include <shobjidl.h>
#include "common/msg.h"
#include "common/common.h"
#include "input/input.h"
#include "input/event.h"
#include "osdep/io.h"
#include "osdep/windows_utils.h"
#include "mpv_talloc.h"
#include "droptarget.h"
struct droptarget {
IDropTarget iface;
atomic_int ref_cnt;
struct mp_log *log;
struct input_ctx *input_ctx;
struct mp_vo_opts *opts;
DWORD last_effect;
IDataObject *data_obj;
};
static FORMATETC fmtetc_file = {
.cfFormat = CF_HDROP,
.dwAspect = DVASPECT_CONTENT,
.lindex = -1,
.tymed = TYMED_HGLOBAL,
};
static FORMATETC fmtetc_url = {
.dwAspect = DVASPECT_CONTENT,
.lindex = -1,
.tymed = TYMED_HGLOBAL,
};
static void DropTarget_Destroy(struct droptarget *t)
{
SAFE_RELEASE(t->data_obj);
talloc_free(t);
}
static STDMETHODIMP DropTarget_QueryInterface(IDropTarget *self, REFIID riid,
void **ppvObject)
{
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDropTarget)) {
*ppvObject = self;
IDropTarget_AddRef(self);
return S_OK;
}
*ppvObject = NULL;
return E_NOINTERFACE;
}
static STDMETHODIMP_(ULONG) DropTarget_AddRef(IDropTarget *self)
{
struct droptarget *t = (struct droptarget *)self;
return atomic_fetch_add(&t->ref_cnt, 1) + 1;
}
static STDMETHODIMP_(ULONG) DropTarget_Release(IDropTarget *self)
{
struct droptarget *t = (struct droptarget *)self;
ULONG ref_cnt = atomic_fetch_add(&t->ref_cnt, -1) - 1;
if (ref_cnt == 0)
DropTarget_Destroy(t);
return ref_cnt;
}
static STDMETHODIMP DropTarget_DragEnter(IDropTarget *self,
IDataObject *pDataObj,
DWORD grfKeyState, POINTL pt,
DWORD *pdwEffect)
{
struct droptarget *t = (struct droptarget *)self;
IDataObject_AddRef(pDataObj);
if (FAILED(IDataObject_QueryGetData(pDataObj, &fmtetc_file)) &&
FAILED(IDataObject_QueryGetData(pDataObj, &fmtetc_url)))
{
*pdwEffect = DROPEFFECT_NONE;
}
SAFE_RELEASE(t->data_obj);
t->data_obj = pDataObj;
t->last_effect = *pdwEffect;
return S_OK;
}
static STDMETHODIMP DropTarget_DragOver(IDropTarget *self, DWORD grfKeyState,
POINTL pt, DWORD *pdwEffect)
{
struct droptarget *t = (struct droptarget *)self;
*pdwEffect = t->last_effect;
return S_OK;
}
static STDMETHODIMP DropTarget_DragLeave(IDropTarget *self)
{
struct droptarget *t = (struct droptarget *)self;
SAFE_RELEASE(t->data_obj);
return S_OK;
}
static STDMETHODIMP DropTarget_Drop(IDropTarget *self, IDataObject *pDataObj,
DWORD grfKeyState, POINTL pt,
DWORD *pdwEffect)
{
struct droptarget *t = (struct droptarget *)self;
enum mp_dnd_action action;
if (t->opts->drag_and_drop >= 0) {
action = t->opts->drag_and_drop;
} else {
action = (grfKeyState & MK_SHIFT) ? DND_APPEND : DND_REPLACE;
}
SAFE_RELEASE(t->data_obj);
STGMEDIUM medium;
if (t->opts->drag_and_drop == -2) {
t->last_effect = DROPEFFECT_NONE;
} else if (SUCCEEDED(IDataObject_GetData(pDataObj, &fmtetc_file, &medium))) {
if (GlobalLock(medium.hGlobal)) {
HDROP drop = medium.hGlobal;
UINT files_num = DragQueryFileW(drop, 0xFFFFFFFF, NULL, 0);
char **files = talloc_zero_array(NULL, char*, files_num);
UINT recvd_files = 0;
for (UINT i = 0; i < files_num; i++) {
UINT len = DragQueryFileW(drop, i, NULL, 0);
wchar_t *buf = talloc_array(NULL, wchar_t, len + 1);
if (DragQueryFileW(drop, i, buf, len + 1) == len) {
wchar_t *target = mp_w32_get_shell_link_target(buf);
char *fname = mp_to_utf8(files, target ? target : buf);
files[recvd_files++] = fname;
talloc_free(target);
MP_VERBOSE(t, "received dropped file: %s\n", fname);
} else {
MP_ERR(t, "error getting dropped file name\n");
}
talloc_free(buf);
}
GlobalUnlock(medium.hGlobal);
mp_event_drop_files(t->input_ctx, recvd_files, files, action);
talloc_free(files);
}
ReleaseStgMedium(&medium);
} else if (SUCCEEDED(IDataObject_GetData(pDataObj, &fmtetc_url, &medium))) {
wchar_t *wurl = GlobalLock(medium.hGlobal);
if (wurl) {
char *url = mp_to_utf8(NULL, wurl);
if (mp_event_drop_mime_data(t->input_ctx, "text/uri-list",
bstr0(url), action) > 0)
{
MP_VERBOSE(t, "received dropped URL: %s\n", url);
} else {
MP_ERR(t, "error getting dropped URL\n");
}
talloc_free(url);
GlobalUnlock(medium.hGlobal);
}
ReleaseStgMedium(&medium);
} else {
t->last_effect = DROPEFFECT_NONE;
}
*pdwEffect = t->last_effect;
return S_OK;
}
static IDropTargetVtbl idroptarget_vtbl = {
.QueryInterface = DropTarget_QueryInterface,
.AddRef = DropTarget_AddRef,
.Release = DropTarget_Release,
.DragEnter = DropTarget_DragEnter,
.DragOver = DropTarget_DragOver,
.DragLeave = DropTarget_DragLeave,
.Drop = DropTarget_Drop,
};
IDropTarget *mp_w32_droptarget_create(struct mp_log *log,
struct mp_vo_opts *opts,
struct input_ctx *input_ctx)
{
fmtetc_url.cfFormat = RegisterClipboardFormatW(L"UniformResourceLocatorW");
struct droptarget *dt = talloc(NULL, struct droptarget);
dt->iface.lpVtbl = &idroptarget_vtbl;
atomic_store(&dt->ref_cnt, 0);
dt->last_effect = 0;
dt->data_obj = NULL;
dt->log = mp_log_new(dt, log, "droptarget");
dt->opts = opts;
dt->input_ctx = input_ctx;
return &dt->iface;
}
|