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
23 #include "wine/port.h"
38 #include "shlobj.h" /* DROPFILES */
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(xdnd
);
45 /* Maximum wait time for selection notify */
46 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
47 #define SELECTION_WAIT 1000 /* us */
49 typedef struct tagXDNDDATA
55 struct tagXDNDDATA
*next
;
56 } XDNDDATA
, *LPXDNDDATA
;
58 static LPXDNDDATA XDNDData
= NULL
;
59 static POINT XDNDxy
= { 0, 0 };
61 static void X11DRV_XDND_InsertXDNDData(int property
, int format
, void* data
, unsigned int len
);
62 static int X11DRV_XDND_DeconstructTextURIList(int property
, void* data
, int len
);
63 static int X11DRV_XDND_DeconstructTextPlain(int property
, void* data
, int len
);
64 static int X11DRV_XDND_DeconstructTextHTML(int property
, void* data
, int len
);
65 static int X11DRV_XDND_MapFormat(unsigned int property
, unsigned char *data
, int len
);
66 static void X11DRV_XDND_ResolveProperty(Display
*display
, Window xwin
, Time tm
,
67 Atom
*types
, unsigned long *count
);
68 static void X11DRV_XDND_SendDropFiles(HWND hwnd
);
69 static void X11DRV_XDND_FreeDragDropOp(void);
70 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest
, char* lpsrc
, int len
);
71 static WCHAR
* X11DRV_XDND_URIToDOS(char *encodedURI
);
73 static CRITICAL_SECTION xdnd_cs
;
74 static CRITICAL_SECTION_DEBUG critsect_debug
=
77 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
78 0, 0, { (DWORD_PTR
)(__FILE__
": xdnd_cs") }
80 static CRITICAL_SECTION xdnd_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
83 /**************************************************************************
84 * X11DRV_XDND_EnterEvent
86 * Handle an XdndEnter event.
88 void X11DRV_XDND_EnterEvent( HWND hWnd
, XClientMessageEvent
*event
)
91 unsigned long count
= 0;
93 TRACE("ver(%ld) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
94 (event
->data
.l
[1] & 0xFF000000) >> 24, (event
->data
.l
[1] & 1),
95 event
->data
.l
[0], event
->data
.l
[1], event
->data
.l
[2],
96 event
->data
.l
[3], event
->data
.l
[4]);
98 /* If the source supports more than 3 data types we retrieve
100 if (event
->data
.l
[1] & 1)
104 unsigned long bytesret
;
106 /* Request supported formats from source window */
108 XGetWindowProperty(event
->display
, event
->data
.l
[0], x11drv_atom(XdndTypeList
),
109 0, 65535, FALSE
, AnyPropertyType
, &acttype
, &actfmt
, &count
,
110 &bytesret
, (unsigned char**)&xdndtypes
);
116 xdndtypes
= (Atom
*) &event
->data
.l
[2];
124 for (; i
< count
; i
++)
126 if (xdndtypes
[i
] != 0)
128 char * pn
= XGetAtomName(event
->display
, xdndtypes
[i
]);
129 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes
[i
], pn
);
136 /* Do a one-time data read and cache results */
137 X11DRV_XDND_ResolveProperty(event
->display
, event
->window
,
138 event
->data
.l
[1], xdndtypes
, &count
);
140 if (event
->data
.l
[1] & 1)
144 /**************************************************************************
145 * X11DRV_XDND_PositionEvent
147 * Handle an XdndPosition event.
149 void X11DRV_XDND_PositionEvent( HWND hWnd
, XClientMessageEvent
*event
)
151 XClientMessageEvent e
;
152 int accept
= 0; /* Assume we're not accepting */
154 XDNDxy
.x
= event
->data
.l
[2] >> 16;
155 XDNDxy
.y
= event
->data
.l
[2] & 0xFFFF;
157 /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
159 if (GetWindowLongW( hWnd
, GWL_EXSTYLE
) & WS_EX_ACCEPTFILES
)
162 TRACE("action req: %ld accept(%d) at x(%d),y(%d)\n",
163 event
->data
.l
[4], accept
, XDNDxy
.x
, XDNDxy
.y
);
166 * Let source know if we're accepting the drop by
167 * sending a status message.
169 e
.type
= ClientMessage
;
170 e
.display
= event
->display
;
171 e
.window
= event
->data
.l
[0];
172 e
.message_type
= x11drv_atom(XdndStatus
);
174 e
.data
.l
[0] = event
->window
;
175 e
.data
.l
[1] = accept
;
176 e
.data
.l
[2] = 0; /* Empty Rect */
177 e
.data
.l
[3] = 0; /* Empty Rect */
179 e
.data
.l
[4] = event
->data
.l
[4];
183 XSendEvent(event
->display
, event
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&e
);
186 /* FIXME: if drag accepted notify OLE of DragOver */
189 /**************************************************************************
190 * X11DRV_XDND_DropEvent
192 * Handle an XdndDrop event.
194 void X11DRV_XDND_DropEvent( HWND hWnd
, XClientMessageEvent
*event
)
196 XClientMessageEvent e
;
200 /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
201 if (GetWindowLongW( hWnd
, GWL_EXSTYLE
) & WS_EX_ACCEPTFILES
)
202 X11DRV_XDND_SendDropFiles( hWnd
);
204 /* FIXME: Notify OLE of Drop */
205 X11DRV_XDND_FreeDragDropOp();
207 /* Tell the target we are finished. */
208 memset(&e
, 0, sizeof(e
));
209 e
.type
= ClientMessage
;
210 e
.display
= event
->display
;
211 e
.window
= event
->data
.l
[0];
212 e
.message_type
= x11drv_atom(XdndFinished
);
214 e
.data
.l
[0] = event
->window
;
216 XSendEvent(event
->display
, event
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&e
);
220 /**************************************************************************
221 * X11DRV_XDND_LeaveEvent
223 * Handle an XdndLeave event.
225 void X11DRV_XDND_LeaveEvent( HWND hWnd
, XClientMessageEvent
*event
)
227 TRACE("DND Operation canceled\n");
229 X11DRV_XDND_FreeDragDropOp();
231 /* FIXME: Notify OLE of DragLeave */
235 /**************************************************************************
236 * X11DRV_XDND_ResolveProperty
238 * Resolve all MIME types to windows clipboard formats. All data is cached.
240 static void X11DRV_XDND_ResolveProperty(Display
*display
, Window xwin
, Time tm
,
241 Atom
*types
, unsigned long *count
)
248 unsigned long bytesret
, icount
;
250 unsigned char* data
= NULL
;
252 TRACE("count(%ld)\n", *count
);
254 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
256 for (i
= 0; i
< *count
; i
++)
258 TRACE("requesting atom %ld from xwin %ld\n", types
[i
], xwin
);
264 XConvertSelection(display
, x11drv_atom(XdndSelection
), types
[i
],
265 x11drv_atom(XdndTarget
), xwin
, /*tm*/CurrentTime
);
269 * Wait for SelectionNotify
271 for (j
= 0; j
< SELECTION_RETRIES
; j
++)
274 res
= XCheckTypedWindowEvent(display
, xwin
, SelectionNotify
, &xe
);
276 if (res
&& xe
.xselection
.selection
== x11drv_atom(XdndSelection
)) break;
278 usleep(SELECTION_WAIT
);
281 if (xe
.xselection
.property
== None
)
285 XGetWindowProperty(display
, xwin
, x11drv_atom(XdndTarget
), 0, 65535, FALSE
,
286 AnyPropertyType
, &acttype
, &actfmt
, &icount
, &bytesret
, &data
);
289 entries
+= X11DRV_XDND_MapFormat(types
[i
], data
, get_property_size( actfmt
, icount
));
299 /**************************************************************************
300 * X11DRV_XDND_InsertXDNDData
302 * Cache available XDND property
304 static void X11DRV_XDND_InsertXDNDData(int property
, int format
, void* data
, unsigned int len
)
306 LPXDNDDATA current
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(XDNDDATA
));
310 EnterCriticalSection(&xdnd_cs
);
311 current
->next
= XDNDData
;
312 current
->cf_xdnd
= property
;
313 current
->cf_win
= format
;
314 current
->data
= data
;
317 LeaveCriticalSection(&xdnd_cs
);
322 /**************************************************************************
323 * X11DRV_XDND_MapFormat
325 * Map XDND MIME format to windows clipboard format.
327 static int X11DRV_XDND_MapFormat(unsigned int property
, unsigned char *data
, int len
)
332 TRACE("%d: %s\n", property
, data
);
334 /* Always include the raw type */
335 xdata
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
336 memcpy(xdata
, data
, len
);
337 X11DRV_XDND_InsertXDNDData(property
, property
, xdata
, len
);
340 if (property
== x11drv_atom(text_uri_list
))
341 count
+= X11DRV_XDND_DeconstructTextURIList(property
, data
, len
);
342 else if (property
== x11drv_atom(text_plain
))
343 count
+= X11DRV_XDND_DeconstructTextPlain(property
, data
, len
);
344 else if (property
== x11drv_atom(text_html
))
345 count
+= X11DRV_XDND_DeconstructTextHTML(property
, data
, len
);
351 /**************************************************************************
352 * X11DRV_XDND_DeconstructTextURIList
354 * Interpret text/uri-list data and add records to <dndfmt> linked list
356 static int X11DRV_XDND_DeconstructTextURIList(int property
, void* data
, int len
)
358 char *uriList
= data
;
370 out
= HeapAlloc(GetProcessHeap(), 0, capacity
);
376 while (end
< len
&& uriList
[end
] != '\r')
378 if (end
< (len
- 1) && uriList
[end
+1] != '\n')
380 WARN("URI list line doesn't end in \\r\\n\n");
384 uri
= HeapAlloc(GetProcessHeap(), 0, end
- start
+ 1);
387 lstrcpynA(uri
, &uriList
[start
], end
- start
+ 1);
388 path
= X11DRV_XDND_URIToDOS(uri
);
389 TRACE("converted URI %s to DOS path %s\n", debugstr_a(uri
), debugstr_w(path
));
390 HeapFree(GetProcessHeap(), 0, uri
);
394 int pathSize
= strlenW(path
) + 1;
395 if (pathSize
> capacity
-size
)
397 capacity
= 2*capacity
+ pathSize
;
398 out
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, out
, capacity
+ 1);
402 WideCharToMultiByte(CP_ACP
, 0, path
, -1, &out
[size
], pathSize
, 0, 0);
405 HeapFree(GetProcessHeap(), 0, path
);
413 if (out
&& end
>= len
)
415 DROPFILES
*dropFiles
;
416 dropFiles
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(DROPFILES
) + size
+ 1);
419 dropFiles
->pFiles
= sizeof(DROPFILES
);
420 dropFiles
->pt
.x
= XDNDxy
.x
;
421 dropFiles
->pt
.y
= XDNDxy
.y
;
423 dropFiles
->fWide
= FALSE
;
425 memcpy(((char*)dropFiles
) + dropFiles
->pFiles
, out
, size
+ 1);
426 X11DRV_XDND_InsertXDNDData(property
, CF_HDROP
, dropFiles
, sizeof(DROPFILES
) + size
+ 1);
430 HeapFree(GetProcessHeap(), 0, out
);
435 /**************************************************************************
436 * X11DRV_XDND_DeconstructTextPlain
438 * Interpret text/plain Data and add records to <dndfmt> linked list
440 static int X11DRV_XDND_DeconstructTextPlain(int property
, void* data
, int len
)
444 /* Always supply plain text */
445 X11DRV_XDND_UnixToDos(&dostext
, data
, len
);
446 X11DRV_XDND_InsertXDNDData(property
, CF_TEXT
, dostext
, strlen(dostext
));
448 TRACE("CF_TEXT (%d): %s\n", CF_TEXT
, dostext
);
454 /**************************************************************************
455 * X11DRV_XDND_DeconstructTextHTML
457 * Interpret text/html data and add records to <dndfmt> linked list
459 static int X11DRV_XDND_DeconstructTextHTML(int property
, void* data
, int len
)
463 X11DRV_XDND_UnixToDos(&dostext
, data
, len
);
465 X11DRV_XDND_InsertXDNDData(property
,
466 RegisterClipboardFormatA("UniformResourceLocator"), dostext
, strlen(dostext
));
468 TRACE("UniformResourceLocator: %s\n", dostext
);
474 /**************************************************************************
475 * X11DRV_XDND_SendDropFiles
477 static void X11DRV_XDND_SendDropFiles(HWND hwnd
)
481 EnterCriticalSection(&xdnd_cs
);
485 /* Find CF_HDROP type if any */
486 while (current
!= NULL
)
488 if (current
->cf_win
== CF_HDROP
)
490 current
= current
->next
;
495 DROPFILES
*lpDrop
= current
->data
;
499 lpDrop
->pt
.x
= XDNDxy
.x
;
500 lpDrop
->pt
.y
= XDNDxy
.y
;
502 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd
,
503 ((char*)lpDrop
) + lpDrop
->pFiles
, ((char*)lpDrop
) + lpDrop
->pFiles
);
505 PostMessageA(hwnd
, WM_DROPFILES
, (WPARAM
)lpDrop
, 0L);
509 LeaveCriticalSection(&xdnd_cs
);
513 /**************************************************************************
514 * X11DRV_XDND_FreeDragDropOp
516 static void X11DRV_XDND_FreeDragDropOp(void)
523 EnterCriticalSection(&xdnd_cs
);
527 /** Free data cache */
528 while (current
!= NULL
)
530 next
= current
->next
;
531 HeapFree(GetProcessHeap(), 0, current
);
536 XDNDxy
.x
= XDNDxy
.y
= 0;
538 LeaveCriticalSection(&xdnd_cs
);
543 /**************************************************************************
544 * X11DRV_XDND_UnixToDos
546 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest
, char* lpsrc
, int len
)
549 unsigned int destlen
, lines
;
551 for (i
= 0, lines
= 0; i
<= len
; i
++)
553 if (lpsrc
[i
] == '\n')
557 destlen
= len
+ lines
+ 1;
561 char* lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, destlen
);
562 for (i
= 0, lines
= 0; i
<= len
; i
++)
564 if (lpsrc
[i
] == '\n')
565 lpstr
[++lines
+ i
] = '\r';
566 lpstr
[lines
+ i
] = lpsrc
[i
];
576 /**************************************************************************
577 * X11DRV_XDND_URIToDOS
579 static WCHAR
* X11DRV_XDND_URIToDOS(char *encodedURI
)
584 char *uri
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, strlen(encodedURI
) + 1);
587 for (i
= 0; encodedURI
[i
]; ++i
)
589 if (encodedURI
[i
] == '%')
591 if (encodedURI
[i
+1] && encodedURI
[i
+2])
595 buffer
[0] = encodedURI
[i
+1];
596 buffer
[1] = encodedURI
[i
+2];
598 sscanf(buffer
, "%x", &number
);
604 WARN("invalid URI encoding in %s\n", debugstr_a(encodedURI
));
605 HeapFree(GetProcessHeap(), 0, uri
);
610 uri
[j
++] = encodedURI
[i
];
613 /* Read http://www.freedesktop.org/wiki/Draganddropwarts and cry... */
614 if (strncmp(uri
, "file:/", 6) == 0)
620 /* file:///path/to/file (nautilus, thunar) */
621 ret
= wine_get_dos_file_name(&uri
[7]);
625 /* file://hostname/path/to/file (X file drag spec) */
627 char *path
= strchr(&uri
[7], '/');
631 if (strcmp(&uri
[7], "localhost") == 0)
634 ret
= wine_get_dos_file_name(path
);
636 else if (gethostname(hostname
, sizeof(hostname
)) == 0)
638 if (strcmp(hostname
, &uri
[7]) == 0)
641 ret
= wine_get_dos_file_name(path
);
649 /* file:/path/to/file (konqueror) */
650 ret
= wine_get_dos_file_name(&uri
[5]);
653 HeapFree(GetProcessHeap(), 0, uri
);