oleaut32/tests: Use common wine_dbgstr_guid implementation from test.h.
[wine.git] / dlls / winex11.drv / xdnd.c
blob5d3749559558d7eb1237bd9428ecca4940048882
1 /*
2 * XDND handler code
4 * Copyright 2003 Ulrich Czekalla
5 * Copyright 2007 Damjan Jovanovic
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdarg.h>
30 #include <stdio.h>
32 #define NONAMELESSUNION
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
39 #define COBJMACROS
40 #include "x11drv.h"
41 #include "shlobj.h" /* DROPFILES */
42 #include "oleidl.h"
43 #include "objidl.h"
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
47 #include "wine/list.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(xdnd);
51 /* Maximum wait time for selection notify */
52 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
53 #define SELECTION_WAIT 1000 /* us */
55 typedef struct tagXDNDDATA
57 int cf_win;
58 Atom cf_xdnd;
59 void *data;
60 unsigned int size;
61 struct list entry;
62 } XDNDDATA, *LPXDNDDATA;
64 static struct list xdndData = LIST_INIT(xdndData);
65 static POINT XDNDxy = { 0, 0 };
66 static IDataObject XDNDDataObject;
67 static BOOL XDNDAccepted = FALSE;
68 static DWORD XDNDDropEffect = DROPEFFECT_NONE;
69 /* the last window the mouse was over */
70 static HWND XDNDLastTargetWnd;
71 /* might be an ancestor of XDNDLastTargetWnd */
72 static HWND XDNDLastDropTargetWnd;
74 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len);
75 static int X11DRV_XDND_DeconstructTextURIList(int property, void* data, int len);
76 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len);
77 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len);
78 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len);
79 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
80 Atom *types, unsigned long *count);
81 static void X11DRV_XDND_SendDropFiles(HWND hwnd);
82 static void X11DRV_XDND_FreeDragDropOp(void);
83 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len);
84 static WCHAR* X11DRV_XDND_URIToDOS(char *encodedURI);
86 static CRITICAL_SECTION xdnd_cs;
87 static CRITICAL_SECTION_DEBUG critsect_debug =
89 0, 0, &xdnd_cs,
90 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
91 0, 0, { (DWORD_PTR)(__FILE__ ": xdnd_cs") }
93 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
96 /* Based on functions in dlls/ole32/ole2.c */
97 static HANDLE get_droptarget_local_handle(HWND hwnd)
99 static const WCHAR prop_marshalleddroptarget[] =
100 {'W','i','n','e','M','a','r','s','h','a','l','l','e','d','D','r','o','p','T','a','r','g','e','t',0};
101 HANDLE handle;
102 HANDLE local_handle = 0;
104 handle = GetPropW(hwnd, prop_marshalleddroptarget);
105 if (handle)
107 DWORD pid;
108 HANDLE process;
110 GetWindowThreadProcessId(hwnd, &pid);
111 process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid);
112 if (process)
114 DuplicateHandle(process, handle, GetCurrentProcess(), &local_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
115 CloseHandle(process);
118 return local_handle;
121 static HRESULT create_stream_from_map(HANDLE map, IStream **stream)
123 HRESULT hr = E_OUTOFMEMORY;
124 HGLOBAL hmem;
125 void *data;
126 MEMORY_BASIC_INFORMATION info;
128 data = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
129 if(!data) return hr;
131 VirtualQuery(data, &info, sizeof(info));
132 TRACE("size %d\n", (int)info.RegionSize);
134 hmem = GlobalAlloc(GMEM_MOVEABLE, info.RegionSize);
135 if(hmem)
137 memcpy(GlobalLock(hmem), data, info.RegionSize);
138 GlobalUnlock(hmem);
139 hr = CreateStreamOnHGlobal(hmem, TRUE, stream);
141 UnmapViewOfFile(data);
142 return hr;
145 static IDropTarget* get_droptarget_pointer(HWND hwnd)
147 IDropTarget *droptarget = NULL;
148 HANDLE map;
149 IStream *stream;
151 map = get_droptarget_local_handle(hwnd);
152 if(!map) return NULL;
154 if(SUCCEEDED(create_stream_from_map(map, &stream)))
156 CoUnmarshalInterface(stream, &IID_IDropTarget, (void**)&droptarget);
157 IStream_Release(stream);
159 CloseHandle(map);
160 return droptarget;
163 /**************************************************************************
164 * X11DRV_XDND_XdndActionToDROPEFFECT
166 static DWORD X11DRV_XDND_XdndActionToDROPEFFECT(long action)
168 /* In Windows, nothing but the given effects is allowed.
169 * In X the given action is just a hint, and you can always
170 * XdndActionCopy and XdndActionPrivate, so be more permissive. */
171 if (action == x11drv_atom(XdndActionCopy))
172 return DROPEFFECT_COPY;
173 else if (action == x11drv_atom(XdndActionMove))
174 return DROPEFFECT_MOVE | DROPEFFECT_COPY;
175 else if (action == x11drv_atom(XdndActionLink))
176 return DROPEFFECT_LINK | DROPEFFECT_COPY;
177 else if (action == x11drv_atom(XdndActionAsk))
178 /* FIXME: should we somehow ask the user what to do here? */
179 return DROPEFFECT_COPY | DROPEFFECT_MOVE | DROPEFFECT_LINK;
180 FIXME("unknown action %ld, assuming DROPEFFECT_COPY\n", action);
181 return DROPEFFECT_COPY;
184 /**************************************************************************
185 * X11DRV_XDND_DROPEFFECTToXdndAction
187 static long X11DRV_XDND_DROPEFFECTToXdndAction(DWORD effect)
189 if (effect == DROPEFFECT_COPY)
190 return x11drv_atom(XdndActionCopy);
191 else if (effect == DROPEFFECT_MOVE)
192 return x11drv_atom(XdndActionMove);
193 else if (effect == DROPEFFECT_LINK)
194 return x11drv_atom(XdndActionLink);
195 FIXME("unknown drop effect %u, assuming XdndActionCopy\n", effect);
196 return x11drv_atom(XdndActionCopy);
199 /**************************************************************************
200 * X11DRV_XDND_EnterEvent
202 * Handle an XdndEnter event.
204 void X11DRV_XDND_EnterEvent( HWND hWnd, XClientMessageEvent *event )
206 int version;
207 Atom *xdndtypes;
208 unsigned long count = 0;
210 version = (event->data.l[1] & 0xFF000000) >> 24;
211 TRACE("ver(%d) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
212 version, (event->data.l[1] & 1),
213 event->data.l[0], event->data.l[1], event->data.l[2],
214 event->data.l[3], event->data.l[4]);
216 if (version > WINE_XDND_VERSION)
218 TRACE("Ignores unsupported version\n");
219 return;
222 XDNDAccepted = FALSE;
224 /* If the source supports more than 3 data types we retrieve
225 * the entire list. */
226 if (event->data.l[1] & 1)
228 Atom acttype;
229 int actfmt;
230 unsigned long bytesret;
232 /* Request supported formats from source window */
233 XGetWindowProperty(event->display, event->data.l[0], x11drv_atom(XdndTypeList),
234 0, 65535, FALSE, AnyPropertyType, &acttype, &actfmt, &count,
235 &bytesret, (unsigned char**)&xdndtypes);
237 else
239 count = 3;
240 xdndtypes = (Atom*) &event->data.l[2];
243 if (TRACE_ON(xdnd))
245 unsigned int i;
247 for (i = 0; i < count; i++)
249 if (xdndtypes[i] != 0)
251 char * pn = XGetAtomName(event->display, xdndtypes[i]);
252 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
253 XFree(pn);
258 /* Do a one-time data read and cache results */
259 X11DRV_XDND_ResolveProperty(event->display, event->window,
260 event->data.l[1], xdndtypes, &count);
262 if (event->data.l[1] & 1)
263 XFree(xdndtypes);
266 /**************************************************************************
267 * X11DRV_XDND_PositionEvent
269 * Handle an XdndPosition event.
271 void X11DRV_XDND_PositionEvent( HWND hWnd, XClientMessageEvent *event )
273 XClientMessageEvent e;
274 int accept = 0; /* Assume we're not accepting */
275 IDropTarget *dropTarget = NULL;
276 DWORD effect;
277 POINTL pointl;
278 HWND targetWindow;
279 HRESULT hr;
281 XDNDxy = root_to_virtual_screen( event->data.l[2] >> 16, event->data.l[2] & 0xFFFF );
282 targetWindow = WindowFromPoint(XDNDxy);
284 pointl.x = XDNDxy.x;
285 pointl.y = XDNDxy.y;
286 effect = X11DRV_XDND_XdndActionToDROPEFFECT(event->data.l[4]);
288 if (!XDNDAccepted || XDNDLastTargetWnd != targetWindow)
290 /* Notify OLE of DragEnter. Result determines if we accept */
291 HWND dropTargetWindow;
293 if (XDNDLastDropTargetWnd)
295 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
296 if (dropTarget)
298 hr = IDropTarget_DragLeave(dropTarget);
299 if (FAILED(hr))
300 WARN("IDropTarget_DragLeave failed, error 0x%08X\n", hr);
301 IDropTarget_Release(dropTarget);
304 dropTargetWindow = targetWindow;
307 dropTarget = get_droptarget_pointer(dropTargetWindow);
308 } while (dropTarget == NULL && (dropTargetWindow = GetParent(dropTargetWindow)) != NULL);
309 XDNDLastTargetWnd = targetWindow;
310 XDNDLastDropTargetWnd = dropTargetWindow;
311 if (dropTarget)
313 hr = IDropTarget_DragEnter(dropTarget, &XDNDDataObject,
314 MK_LBUTTON, pointl, &effect);
315 if (SUCCEEDED(hr))
317 if (effect != DROPEFFECT_NONE)
319 XDNDAccepted = TRUE;
320 TRACE("the application accepted the drop\n");
322 else
323 TRACE("the application refused the drop\n");
325 else
326 WARN("IDropTarget_DragEnter failed, error 0x%08X\n", hr);
327 IDropTarget_Release(dropTarget);
330 if (XDNDAccepted && XDNDLastTargetWnd == targetWindow)
332 /* If drag accepted notify OLE of DragOver */
333 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
334 if (dropTarget)
336 hr = IDropTarget_DragOver(dropTarget, MK_LBUTTON, pointl, &effect);
337 if (SUCCEEDED(hr))
338 XDNDDropEffect = effect;
339 else
340 WARN("IDropTarget_DragOver failed, error 0x%08X\n", hr);
341 IDropTarget_Release(dropTarget);
345 if (XDNDAccepted)
346 accept = 1;
347 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
348 accept = 1;
350 TRACE("action req: %ld accept(%d) at x(%d),y(%d)\n",
351 event->data.l[4], accept, XDNDxy.x, XDNDxy.y);
354 * Let source know if we're accepting the drop by
355 * sending a status message.
357 e.type = ClientMessage;
358 e.display = event->display;
359 e.window = event->data.l[0];
360 e.message_type = x11drv_atom(XdndStatus);
361 e.format = 32;
362 e.data.l[0] = event->window;
363 e.data.l[1] = accept;
364 e.data.l[2] = 0; /* Empty Rect */
365 e.data.l[3] = 0; /* Empty Rect */
366 if (accept)
367 e.data.l[4] = X11DRV_XDND_DROPEFFECTToXdndAction(effect);
368 else
369 e.data.l[4] = None;
370 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
373 /**************************************************************************
374 * X11DRV_XDND_DropEvent
376 * Handle an XdndDrop event.
378 void X11DRV_XDND_DropEvent( HWND hWnd, XClientMessageEvent *event )
380 XClientMessageEvent e;
381 IDropTarget *dropTarget;
383 TRACE("\n");
385 /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
386 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
387 X11DRV_XDND_SendDropFiles( hWnd );
389 /* Notify OLE of Drop */
390 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
391 if (dropTarget)
393 HRESULT hr;
394 POINTL pointl;
395 DWORD effect = XDNDDropEffect;
397 pointl.x = XDNDxy.x;
398 pointl.y = XDNDxy.y;
399 hr = IDropTarget_Drop(dropTarget, &XDNDDataObject, MK_LBUTTON,
400 pointl, &effect);
401 if (SUCCEEDED(hr))
403 if (effect != DROPEFFECT_NONE)
404 TRACE("drop succeeded\n");
405 else
406 TRACE("the application refused the drop\n");
408 else
409 WARN("drop failed, error 0x%08X\n", hr);
410 IDropTarget_Release(dropTarget);
413 X11DRV_XDND_FreeDragDropOp();
415 /* Tell the target we are finished. */
416 memset(&e, 0, sizeof(e));
417 e.type = ClientMessage;
418 e.display = event->display;
419 e.window = event->data.l[0];
420 e.message_type = x11drv_atom(XdndFinished);
421 e.format = 32;
422 e.data.l[0] = event->window;
423 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
426 /**************************************************************************
427 * X11DRV_XDND_LeaveEvent
429 * Handle an XdndLeave event.
431 void X11DRV_XDND_LeaveEvent( HWND hWnd, XClientMessageEvent *event )
433 IDropTarget *dropTarget;
435 TRACE("DND Operation canceled\n");
437 /* Notify OLE of DragLeave */
438 dropTarget = get_droptarget_pointer(XDNDLastDropTargetWnd);
439 if (dropTarget)
441 HRESULT hr = IDropTarget_DragLeave(dropTarget);
442 if (FAILED(hr))
443 WARN("IDropTarget_DragLeave failed, error 0x%08X\n", hr);
444 IDropTarget_Release(dropTarget);
447 X11DRV_XDND_FreeDragDropOp();
451 /**************************************************************************
452 * X11DRV_XDND_ResolveProperty
454 * Resolve all MIME types to windows clipboard formats. All data is cached.
456 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
457 Atom *types, unsigned long *count)
459 unsigned int i, j;
460 BOOL res;
461 XEvent xe;
462 Atom acttype;
463 int actfmt;
464 unsigned long bytesret, icount;
465 int entries = 0;
466 unsigned char* data = NULL;
467 XDNDDATA *current, *next;
468 BOOL haveHDROP = FALSE;
470 TRACE("count(%ld)\n", *count);
472 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
474 for (i = 0; i < *count; i++)
476 TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
478 if (types[i] == 0)
479 continue;
481 XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
482 x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
485 * Wait for SelectionNotify
487 for (j = 0; j < SELECTION_RETRIES; j++)
489 res = XCheckTypedWindowEvent(display, xwin, SelectionNotify, &xe);
490 if (res && xe.xselection.selection == x11drv_atom(XdndSelection)) break;
492 usleep(SELECTION_WAIT);
495 if (xe.xselection.property == None)
496 continue;
498 XGetWindowProperty(display, xwin, x11drv_atom(XdndTarget), 0, 65535, FALSE,
499 AnyPropertyType, &acttype, &actfmt, &icount, &bytesret, &data);
501 entries += X11DRV_XDND_MapFormat(types[i], data, get_property_size( actfmt, icount ));
502 XFree(data);
505 /* On Windows when there is a CF_HDROP, there are no other CF_ formats.
506 * foobar2000 relies on this (spaces -> %20's without it).
508 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
510 if (current->cf_win == CF_HDROP)
512 haveHDROP = TRUE;
513 break;
516 if (haveHDROP)
518 LIST_FOR_EACH_ENTRY_SAFE(current, next, &xdndData, XDNDDATA, entry)
520 if (current->cf_win != CF_HDROP && current->cf_win < CF_MAX)
522 list_remove(&current->entry);
523 HeapFree(GetProcessHeap(), 0, current->data);
524 HeapFree(GetProcessHeap(), 0, current);
525 --entries;
530 *count = entries;
534 /**************************************************************************
535 * X11DRV_XDND_InsertXDNDData
537 * Cache available XDND property
539 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len)
541 LPXDNDDATA current = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
543 if (current)
545 EnterCriticalSection(&xdnd_cs);
546 current->cf_xdnd = property;
547 current->cf_win = format;
548 current->data = data;
549 current->size = len;
550 list_add_tail(&xdndData, &current->entry);
551 LeaveCriticalSection(&xdnd_cs);
556 /**************************************************************************
557 * X11DRV_XDND_MapFormat
559 * Map XDND MIME format to windows clipboard format.
561 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len)
563 void* xdata;
564 int count = 0;
566 TRACE("%d: %s\n", property, data);
568 /* Always include the raw type */
569 xdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
570 memcpy(xdata, data, len);
571 X11DRV_XDND_InsertXDNDData(property, property, xdata, len);
572 count++;
574 if (property == x11drv_atom(text_uri_list))
575 count += X11DRV_XDND_DeconstructTextURIList(property, data, len);
576 else if (property == x11drv_atom(text_plain))
577 count += X11DRV_XDND_DeconstructTextPlain(property, data, len);
578 else if (property == x11drv_atom(text_html))
579 count += X11DRV_XDND_DeconstructTextHTML(property, data, len);
581 return count;
585 /**************************************************************************
586 * X11DRV_XDND_DeconstructTextURIList
588 * Interpret text/uri-list data and add records to <dndfmt> linked list
590 static int X11DRV_XDND_DeconstructTextURIList(int property, void* data, int len)
592 char *uriList = data;
593 char *uri;
594 WCHAR *path;
596 WCHAR *out = NULL;
597 int size = 0;
598 int capacity = 4096;
600 int count = 0;
601 int start = 0;
602 int end = 0;
604 out = HeapAlloc(GetProcessHeap(), 0, capacity * sizeof(WCHAR));
605 if (out == NULL)
606 return 0;
608 while (end < len)
610 while (end < len && uriList[end] != '\r')
611 ++end;
612 if (end < (len - 1) && uriList[end+1] != '\n')
614 WARN("URI list line doesn't end in \\r\\n\n");
615 break;
618 uri = HeapAlloc(GetProcessHeap(), 0, end - start + 1);
619 if (uri == NULL)
620 break;
621 lstrcpynA(uri, &uriList[start], end - start + 1);
622 path = X11DRV_XDND_URIToDOS(uri);
623 TRACE("converted URI %s to DOS path %s\n", debugstr_a(uri), debugstr_w(path));
624 HeapFree(GetProcessHeap(), 0, uri);
626 if (path)
628 int pathSize = strlenW(path) + 1;
629 if (pathSize > capacity-size)
631 capacity = 2*capacity + pathSize;
632 out = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, out, (capacity + 1)*sizeof(WCHAR));
633 if (out == NULL)
634 goto done;
636 memcpy(&out[size], path, pathSize * sizeof(WCHAR));
637 size += pathSize;
638 done:
639 HeapFree(GetProcessHeap(), 0, path);
640 if (out == NULL)
641 break;
644 start = end + 2;
645 end = start;
647 if (out && end >= len)
649 DROPFILES *dropFiles;
650 dropFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DROPFILES) + (size + 1)*sizeof(WCHAR));
651 if (dropFiles)
653 dropFiles->pFiles = sizeof(DROPFILES);
654 dropFiles->pt.x = XDNDxy.x;
655 dropFiles->pt.y = XDNDxy.y;
656 dropFiles->fNC = 0;
657 dropFiles->fWide = TRUE;
658 out[size] = '\0';
659 memcpy(((char*)dropFiles) + dropFiles->pFiles, out, (size + 1)*sizeof(WCHAR));
660 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, dropFiles, sizeof(DROPFILES) + (size + 1)*sizeof(WCHAR));
661 count = 1;
664 HeapFree(GetProcessHeap(), 0, out);
665 return count;
669 /**************************************************************************
670 * X11DRV_XDND_DeconstructTextPlain
672 * Interpret text/plain Data and add records to <dndfmt> linked list
674 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len)
676 char* dostext;
678 /* Always supply plain text */
679 X11DRV_XDND_UnixToDos(&dostext, data, len);
680 X11DRV_XDND_InsertXDNDData(property, CF_TEXT, dostext, strlen(dostext));
682 TRACE("CF_TEXT (%d): %s\n", CF_TEXT, dostext);
684 return 1;
688 /**************************************************************************
689 * X11DRV_XDND_DeconstructTextHTML
691 * Interpret text/html data and add records to <dndfmt> linked list
693 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len)
695 char* dostext;
697 X11DRV_XDND_UnixToDos(&dostext, data, len);
699 X11DRV_XDND_InsertXDNDData(property,
700 RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
702 TRACE("UniformResourceLocator: %s\n", dostext);
704 return 1;
708 /**************************************************************************
709 * X11DRV_XDND_SendDropFiles
711 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
713 LPXDNDDATA current = NULL;
714 BOOL found = FALSE;
716 EnterCriticalSection(&xdnd_cs);
718 /* Find CF_HDROP type if any */
719 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
721 if (current->cf_win == CF_HDROP)
723 found = TRUE;
724 break;
728 if (found)
730 DROPFILES *lpDrop = current->data;
732 if (lpDrop)
734 lpDrop->pt.x = XDNDxy.x;
735 lpDrop->pt.y = XDNDxy.y;
737 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd,
738 ((char*)lpDrop) + lpDrop->pFiles, debugstr_w((WCHAR*)(((char*)lpDrop) + lpDrop->pFiles)));
740 PostMessageW(hwnd, WM_DROPFILES, (WPARAM)lpDrop, 0L);
744 LeaveCriticalSection(&xdnd_cs);
748 /**************************************************************************
749 * X11DRV_XDND_FreeDragDropOp
751 static void X11DRV_XDND_FreeDragDropOp(void)
753 LPXDNDDATA next;
754 LPXDNDDATA current;
756 TRACE("\n");
758 EnterCriticalSection(&xdnd_cs);
760 /** Free data cache */
761 LIST_FOR_EACH_ENTRY_SAFE(current, next, &xdndData, XDNDDATA, entry)
763 list_remove(&current->entry);
764 HeapFree(GetProcessHeap(), 0, current);
767 XDNDxy.x = XDNDxy.y = 0;
768 XDNDLastTargetWnd = NULL;
769 XDNDLastDropTargetWnd = NULL;
770 XDNDAccepted = FALSE;
772 LeaveCriticalSection(&xdnd_cs);
777 /**************************************************************************
778 * X11DRV_XDND_UnixToDos
780 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
782 int i;
783 unsigned int destlen, lines;
785 for (i = 0, lines = 0; i <= len; i++)
787 if (lpsrc[i] == '\n')
788 lines++;
791 destlen = len + lines + 1;
793 if (lpdest)
795 char* lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, destlen);
796 for (i = 0, lines = 0; i <= len; i++)
798 if (lpsrc[i] == '\n')
799 lpstr[++lines + i] = '\r';
800 lpstr[lines + i] = lpsrc[i];
803 *lpdest = lpstr;
806 return lines;
810 /**************************************************************************
811 * X11DRV_XDND_URIToDOS
813 static WCHAR* X11DRV_XDND_URIToDOS(char *encodedURI)
815 WCHAR *ret = NULL;
816 int i;
817 int j = 0;
818 char *uri = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlen(encodedURI) + 1);
819 if (uri == NULL)
820 return NULL;
821 for (i = 0; encodedURI[i]; ++i)
823 if (encodedURI[i] == '%')
825 if (encodedURI[i+1] && encodedURI[i+2])
827 char buffer[3];
828 int number;
829 buffer[0] = encodedURI[i+1];
830 buffer[1] = encodedURI[i+2];
831 buffer[2] = '\0';
832 sscanf(buffer, "%x", &number);
833 uri[j++] = number;
834 i += 2;
836 else
838 WARN("invalid URI encoding in %s\n", debugstr_a(encodedURI));
839 HeapFree(GetProcessHeap(), 0, uri);
840 return NULL;
843 else
844 uri[j++] = encodedURI[i];
847 /* Read http://www.freedesktop.org/wiki/Draganddropwarts and cry... */
848 if (strncmp(uri, "file:/", 6) == 0)
850 if (uri[6] == '/')
852 if (uri[7] == '/')
854 /* file:///path/to/file (nautilus, thunar) */
855 ret = wine_get_dos_file_name(&uri[7]);
857 else if (uri[7])
859 /* file://hostname/path/to/file (X file drag spec) */
860 char hostname[256];
861 char *path = strchr(&uri[7], '/');
862 if (path)
864 *path = '\0';
865 if (strcmp(&uri[7], "localhost") == 0)
867 *path = '/';
868 ret = wine_get_dos_file_name(path);
870 else if (gethostname(hostname, sizeof(hostname)) == 0)
872 if (strcmp(hostname, &uri[7]) == 0)
874 *path = '/';
875 ret = wine_get_dos_file_name(path);
881 else if (uri[6])
883 /* file:/path/to/file (konqueror) */
884 ret = wine_get_dos_file_name(&uri[5]);
887 HeapFree(GetProcessHeap(), 0, uri);
888 return ret;
892 /**************************************************************************
893 * X11DRV_XDND_DescribeClipboardFormat
895 static void X11DRV_XDND_DescribeClipboardFormat(int cfFormat, char *buffer, int size)
897 #define D(x) case x: lstrcpynA(buffer, #x, size); return;
898 switch (cfFormat)
900 D(CF_TEXT)
901 D(CF_BITMAP)
902 D(CF_METAFILEPICT)
903 D(CF_SYLK)
904 D(CF_DIF)
905 D(CF_TIFF)
906 D(CF_OEMTEXT)
907 D(CF_DIB)
908 D(CF_PALETTE)
909 D(CF_PENDATA)
910 D(CF_RIFF)
911 D(CF_WAVE)
912 D(CF_UNICODETEXT)
913 D(CF_ENHMETAFILE)
914 D(CF_HDROP)
915 D(CF_LOCALE)
916 D(CF_DIBV5)
918 #undef D
920 if (CF_PRIVATEFIRST <= cfFormat && cfFormat <= CF_PRIVATELAST)
922 lstrcpynA(buffer, "some private object", size);
923 return;
925 if (CF_GDIOBJFIRST <= cfFormat && cfFormat <= CF_GDIOBJLAST)
927 lstrcpynA(buffer, "some GDI object", size);
928 return;
931 GetClipboardFormatNameA(cfFormat, buffer, size);
935 /* The IDataObject singleton we feed to OLE follows */
937 static HRESULT WINAPI XDNDDATAOBJECT_QueryInterface(IDataObject *dataObject,
938 REFIID riid, void **ppvObject)
940 TRACE("(%p, %s, %p)\n", dataObject, debugstr_guid(riid), ppvObject);
941 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDataObject))
943 *ppvObject = dataObject;
944 IDataObject_AddRef(dataObject);
945 return S_OK;
947 *ppvObject = NULL;
948 return E_NOINTERFACE;
951 static ULONG WINAPI XDNDDATAOBJECT_AddRef(IDataObject *dataObject)
953 TRACE("(%p)\n", dataObject);
954 return 2;
957 static ULONG WINAPI XDNDDATAOBJECT_Release(IDataObject *dataObject)
959 TRACE("(%p)\n", dataObject);
960 return 1;
963 static HRESULT WINAPI XDNDDATAOBJECT_GetData(IDataObject *dataObject,
964 FORMATETC *formatEtc,
965 STGMEDIUM *pMedium)
967 HRESULT hr;
968 char formatDesc[1024];
970 TRACE("(%p, %p, %p)\n", dataObject, formatEtc, pMedium);
971 X11DRV_XDND_DescribeClipboardFormat(formatEtc->cfFormat,
972 formatDesc, sizeof(formatDesc));
973 TRACE("application is looking for %s\n", formatDesc);
975 hr = IDataObject_QueryGetData(dataObject, formatEtc);
976 if (SUCCEEDED(hr))
978 XDNDDATA *current;
979 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
981 if (current->cf_win == formatEtc->cfFormat)
983 pMedium->tymed = TYMED_HGLOBAL;
984 pMedium->u.hGlobal = HeapAlloc(GetProcessHeap(), 0, current->size);
985 if (pMedium->u.hGlobal == NULL)
986 return E_OUTOFMEMORY;
987 memcpy(pMedium->u.hGlobal, current->data, current->size);
988 pMedium->pUnkForRelease = 0;
989 return S_OK;
993 return hr;
996 static HRESULT WINAPI XDNDDATAOBJECT_GetDataHere(IDataObject *dataObject,
997 FORMATETC *formatEtc,
998 STGMEDIUM *pMedium)
1000 FIXME("(%p, %p, %p): stub\n", dataObject, formatEtc, pMedium);
1001 return DATA_E_FORMATETC;
1004 static HRESULT WINAPI XDNDDATAOBJECT_QueryGetData(IDataObject *dataObject,
1005 FORMATETC *formatEtc)
1007 char formatDesc[1024];
1008 XDNDDATA *current;
1010 TRACE("(%p, %p={.tymed=0x%x, .dwAspect=%d, .cfFormat=%d}\n",
1011 dataObject, formatEtc, formatEtc->tymed, formatEtc->dwAspect, formatEtc->cfFormat);
1012 X11DRV_XDND_DescribeClipboardFormat(formatEtc->cfFormat, formatDesc, sizeof(formatDesc));
1014 if (formatEtc->tymed && !(formatEtc->tymed & TYMED_HGLOBAL))
1016 FIXME("only HGLOBAL medium types supported right now\n");
1017 return DV_E_TYMED;
1019 if (formatEtc->dwAspect != DVASPECT_CONTENT)
1021 FIXME("only the content aspect is supported right now\n");
1022 return E_NOTIMPL;
1025 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
1027 if (current->cf_win == formatEtc->cfFormat)
1029 TRACE("application found %s\n", formatDesc);
1030 return S_OK;
1033 TRACE("application didn't find %s\n", formatDesc);
1034 return DV_E_FORMATETC;
1037 static HRESULT WINAPI XDNDDATAOBJECT_GetCanonicalFormatEtc(IDataObject *dataObject,
1038 FORMATETC *formatEtc,
1039 FORMATETC *formatEtcOut)
1041 FIXME("(%p, %p, %p): stub\n", dataObject, formatEtc, formatEtcOut);
1042 formatEtcOut->ptd = NULL;
1043 return E_NOTIMPL;
1046 static HRESULT WINAPI XDNDDATAOBJECT_SetData(IDataObject *dataObject,
1047 FORMATETC *formatEtc,
1048 STGMEDIUM *pMedium, BOOL fRelease)
1050 FIXME("(%p, %p, %p, %s): stub\n", dataObject, formatEtc,
1051 pMedium, fRelease?"TRUE":"FALSE");
1052 return E_NOTIMPL;
1055 static HRESULT WINAPI XDNDDATAOBJECT_EnumFormatEtc(IDataObject *dataObject,
1056 DWORD dwDirection,
1057 IEnumFORMATETC **ppEnumFormatEtc)
1059 DWORD count;
1060 FORMATETC *formats;
1062 TRACE("(%p, %u, %p)\n", dataObject, dwDirection, ppEnumFormatEtc);
1064 if (dwDirection != DATADIR_GET)
1066 FIXME("only the get direction is implemented\n");
1067 return E_NOTIMPL;
1070 count = list_count(&xdndData);
1071 formats = HeapAlloc(GetProcessHeap(), 0, count * sizeof(FORMATETC));
1072 if (formats)
1074 XDNDDATA *current;
1075 DWORD i = 0;
1076 HRESULT hr;
1077 LIST_FOR_EACH_ENTRY(current, &xdndData, XDNDDATA, entry)
1079 formats[i].cfFormat = current->cf_win;
1080 formats[i].ptd = NULL;
1081 formats[i].dwAspect = DVASPECT_CONTENT;
1082 formats[i].lindex = -1;
1083 formats[i].tymed = TYMED_HGLOBAL;
1084 i++;
1086 hr = SHCreateStdEnumFmtEtc(count, formats, ppEnumFormatEtc);
1087 HeapFree(GetProcessHeap(), 0, formats);
1088 return hr;
1090 else
1091 return E_OUTOFMEMORY;
1094 static HRESULT WINAPI XDNDDATAOBJECT_DAdvise(IDataObject *dataObject,
1095 FORMATETC *formatEtc, DWORD advf,
1096 IAdviseSink *adviseSink,
1097 DWORD *pdwConnection)
1099 FIXME("(%p, %p, %u, %p, %p): stub\n", dataObject, formatEtc, advf,
1100 adviseSink, pdwConnection);
1101 return OLE_E_ADVISENOTSUPPORTED;
1104 static HRESULT WINAPI XDNDDATAOBJECT_DUnadvise(IDataObject *dataObject,
1105 DWORD dwConnection)
1107 FIXME("(%p, %u): stub\n", dataObject, dwConnection);
1108 return OLE_E_ADVISENOTSUPPORTED;
1111 static HRESULT WINAPI XDNDDATAOBJECT_EnumDAdvise(IDataObject *dataObject,
1112 IEnumSTATDATA **pEnumAdvise)
1114 FIXME("(%p, %p): stub\n", dataObject, pEnumAdvise);
1115 return OLE_E_ADVISENOTSUPPORTED;
1118 static IDataObjectVtbl xdndDataObjectVtbl =
1120 XDNDDATAOBJECT_QueryInterface,
1121 XDNDDATAOBJECT_AddRef,
1122 XDNDDATAOBJECT_Release,
1123 XDNDDATAOBJECT_GetData,
1124 XDNDDATAOBJECT_GetDataHere,
1125 XDNDDATAOBJECT_QueryGetData,
1126 XDNDDATAOBJECT_GetCanonicalFormatEtc,
1127 XDNDDATAOBJECT_SetData,
1128 XDNDDATAOBJECT_EnumFormatEtc,
1129 XDNDDATAOBJECT_DAdvise,
1130 XDNDDATAOBJECT_DUnadvise,
1131 XDNDDATAOBJECT_EnumDAdvise
1134 static IDataObject XDNDDataObject = { &xdndDataObjectVtbl };