2017-03-25 12:30:54 +00:00
|
|
|
/*
|
|
|
|
* 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 <windows.h>
|
|
|
|
#include <ole2.h>
|
|
|
|
#include <shobjidl.h>
|
|
|
|
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "input/input.h"
|
|
|
|
#include "input/event.h"
|
|
|
|
#include "osdep/atomic.h"
|
|
|
|
#include "osdep/io.h"
|
2017-03-25 13:13:51 +00:00
|
|
|
#include "osdep/windows_utils.h"
|
2017-03-25 12:30:54 +00:00
|
|
|
#include "mpv_talloc.h"
|
|
|
|
|
|
|
|
#include "droptarget.h"
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
struct droptarget {
|
2017-03-25 12:30:54 +00:00
|
|
|
IDropTarget iface;
|
2017-03-25 13:13:51 +00:00
|
|
|
atomic_int ref_cnt;
|
2017-03-25 12:30:54 +00:00
|
|
|
struct mp_log *log;
|
|
|
|
struct input_ctx *input_ctx;
|
2023-06-12 23:42:49 +00:00
|
|
|
struct mp_vo_opts *opts;
|
2017-03-25 13:13:51 +00:00
|
|
|
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)
|
2017-03-25 12:30:54 +00:00
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
SAFE_RELEASE(t->data_obj);
|
|
|
|
talloc_free(t);
|
2017-03-25 12:30:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
static STDMETHODIMP DropTarget_QueryInterface(IDropTarget *self, REFIID riid,
|
|
|
|
void **ppvObject)
|
2017-03-25 12:30:54 +00:00
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDropTarget)) {
|
|
|
|
*ppvObject = self;
|
|
|
|
IDropTarget_AddRef(self);
|
|
|
|
return S_OK;
|
2017-03-25 12:30:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
*ppvObject = NULL;
|
|
|
|
return E_NOINTERFACE;
|
2017-03-25 12:30:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
static STDMETHODIMP_(ULONG) DropTarget_AddRef(IDropTarget *self)
|
2017-03-25 12:30:54 +00:00
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
struct droptarget *t = (struct droptarget *)self;
|
|
|
|
return atomic_fetch_add(&t->ref_cnt, 1) + 1;
|
2017-03-25 12:30:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
static STDMETHODIMP_(ULONG) DropTarget_Release(IDropTarget *self)
|
2017-03-25 12:30:54 +00:00
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
struct droptarget *t = (struct droptarget *)self;
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
ULONG ref_cnt = atomic_fetch_add(&t->ref_cnt, -1) - 1;
|
|
|
|
if (ref_cnt == 0)
|
2017-03-25 12:30:54 +00:00
|
|
|
DropTarget_Destroy(t);
|
2017-03-25 13:13:51 +00:00
|
|
|
return ref_cnt;
|
2017-03-25 12:30:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
static STDMETHODIMP DropTarget_DragEnter(IDropTarget *self,
|
|
|
|
IDataObject *pDataObj,
|
|
|
|
DWORD grfKeyState, POINTL pt,
|
|
|
|
DWORD *pdwEffect)
|
2017-03-25 12:30:54 +00:00
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
struct droptarget *t = (struct droptarget *)self;
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
IDataObject_AddRef(pDataObj);
|
|
|
|
if (FAILED(IDataObject_QueryGetData(pDataObj, &fmtetc_file)) &&
|
|
|
|
FAILED(IDataObject_QueryGetData(pDataObj, &fmtetc_url)))
|
|
|
|
{
|
2017-03-25 12:30:54 +00:00
|
|
|
*pdwEffect = DROPEFFECT_NONE;
|
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
SAFE_RELEASE(t->data_obj);
|
|
|
|
t->data_obj = pDataObj;
|
|
|
|
t->last_effect = *pdwEffect;
|
2017-03-25 12:30:54 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
static STDMETHODIMP DropTarget_DragOver(IDropTarget *self, DWORD grfKeyState,
|
|
|
|
POINTL pt, DWORD *pdwEffect)
|
2017-03-25 12:30:54 +00:00
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
struct droptarget *t = (struct droptarget *)self;
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
*pdwEffect = t->last_effect;
|
2017-03-25 12:30:54 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
static STDMETHODIMP DropTarget_DragLeave(IDropTarget *self)
|
2017-03-25 12:30:54 +00:00
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
struct droptarget *t = (struct droptarget *)self;
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
SAFE_RELEASE(t->data_obj);
|
2017-03-25 12:30:54 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
static STDMETHODIMP DropTarget_Drop(IDropTarget *self, IDataObject *pDataObj,
|
|
|
|
DWORD grfKeyState, POINTL pt,
|
|
|
|
DWORD *pdwEffect)
|
2017-03-25 12:30:54 +00:00
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
struct droptarget *t = (struct droptarget *)self;
|
2023-06-12 23:42:49 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
SAFE_RELEASE(t->data_obj);
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
STGMEDIUM medium;
|
2023-06-12 23:42:49 +00:00
|
|
|
if (t->opts->drag_and_drop == -2) {
|
|
|
|
t->last_effect = DROPEFFECT_NONE;
|
|
|
|
} else if (SUCCEEDED(IDataObject_GetData(pDataObj, &fmtetc_file, &medium))) {
|
2017-03-25 13:13:51 +00:00
|
|
|
if (GlobalLock(medium.hGlobal)) {
|
|
|
|
HDROP drop = medium.hGlobal;
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
UINT files_num = DragQueryFileW(drop, 0xFFFFFFFF, NULL, 0);
|
|
|
|
char **files = talloc_zero_array(NULL, char*, files_num);
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
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);
|
2017-03-25 12:30:54 +00:00
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
if (DragQueryFileW(drop, i, buf, len + 1) == len) {
|
|
|
|
char *fname = mp_to_utf8(files, buf);
|
|
|
|
files[recvd_files++] = fname;
|
2017-03-25 12:30:54 +00:00
|
|
|
|
|
|
|
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);
|
2017-03-25 13:13:51 +00:00
|
|
|
mp_event_drop_files(t->input_ctx, recvd_files, files, action);
|
2017-03-25 12:30:54 +00:00
|
|
|
talloc_free(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReleaseStgMedium(&medium);
|
2017-03-25 13:13:51 +00:00
|
|
|
} else if (SUCCEEDED(IDataObject_GetData(pDataObj, &fmtetc_url, &medium))) {
|
|
|
|
wchar_t *wurl = GlobalLock(medium.hGlobal);
|
|
|
|
if (wurl) {
|
2017-03-25 12:30:54 +00:00
|
|
|
char *url = mp_to_utf8(NULL, wurl);
|
|
|
|
if (mp_event_drop_mime_data(t->input_ctx, "text/uri-list",
|
2017-03-25 13:13:51 +00:00
|
|
|
bstr0(url), action) > 0)
|
|
|
|
{
|
2017-03-25 12:30:54 +00:00
|
|
|
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);
|
2017-03-25 13:13:51 +00:00
|
|
|
} else {
|
|
|
|
t->last_effect = DROPEFFECT_NONE;
|
2017-03-25 12:30:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
*pdwEffect = t->last_effect;
|
2017-03-25 12:30:54 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-25 13:13:51 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2017-03-25 12:30:54 +00:00
|
|
|
IDropTarget *mp_w32_droptarget_create(struct mp_log *log,
|
2023-06-12 23:42:49 +00:00
|
|
|
struct mp_vo_opts *opts,
|
2017-03-25 12:30:54 +00:00
|
|
|
struct input_ctx *input_ctx)
|
|
|
|
{
|
2017-03-25 13:13:51 +00:00
|
|
|
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");
|
2023-06-12 23:42:49 +00:00
|
|
|
dt->opts = opts;
|
2017-03-25 13:13:51 +00:00
|
|
|
dt->input_ctx = input_ctx;
|
|
|
|
|
|
|
|
return &dt->iface;
|
2017-03-25 12:30:54 +00:00
|
|
|
}
|