4 * Copyright 2003 Ulrich Czekalla
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
38 #include "shlobj.h" /* DROPFILES */
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(xdnd
);
44 /* Maximum wait time for selection notify */
45 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
46 #define SELECTION_WAIT 1000 /* us */
48 typedef struct tagXDNDDATA
54 struct tagXDNDDATA
*next
;
55 } XDNDDATA
, *LPXDNDDATA
;
57 static LPXDNDDATA XDNDData
= NULL
;
58 static POINT XDNDxy
= { 0, 0 };
60 static void X11DRV_XDND_InsertXDNDData(int property
, int format
, void* data
, unsigned int len
);
61 static int X11DRV_XDND_DeconstructTextPlain(int property
, void* data
, int len
);
62 static int X11DRV_XDND_DeconstructTextHTML(int property
, void* data
, int len
);
63 static int X11DRV_XDND_MapFormat(unsigned int property
, unsigned char *data
, int len
);
64 static void X11DRV_XDND_ResolveProperty(Display
*display
, Window xwin
, Time tm
,
65 Atom
*types
, unsigned long *count
);
66 static void X11DRV_XDND_SendDropFiles(HWND hwnd
);
67 static void X11DRV_XDND_FreeDragDropOp(void);
68 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest
, char* lpsrc
, int len
);
69 static DROPFILES
* X11DRV_XDND_BuildDropFiles(char* filename
, unsigned int len
, POINT pt
);
71 static CRITICAL_SECTION xdnd_cs
;
72 static CRITICAL_SECTION_DEBUG critsect_debug
=
75 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
76 0, 0, { 0, (DWORD
)(__FILE__
": xdnd_cs") }
78 static CRITICAL_SECTION xdnd_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
80 /**************************************************************************
83 * Entry point for X11 XDND events. Returns FALSE if event is not handled.
85 int X11DRV_XDND_Event(HWND hWnd
, XClientMessageEvent
*event
)
89 TRACE("0x%p\n", hWnd
);
91 if (event
->message_type
== x11drv_atom(XdndEnter
))
94 unsigned long count
= 0;
96 TRACE("XDNDEnter: ver(%ld) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
97 (event
->data
.l
[1] & 0xFF000000) >> 24, (event
->data
.l
[1] & 1),
98 event
->data
.l
[0], event
->data
.l
[1], event
->data
.l
[2],
99 event
->data
.l
[3], event
->data
.l
[4]);
101 /* If the source supports more than 3 data types we retrieve
102 * the entire list. */
103 if (event
->data
.l
[1] & 1)
107 unsigned long bytesret
;
109 /* Request supported formats from source window */
111 XGetWindowProperty(event
->display
, event
->data
.l
[0], x11drv_atom(XdndTypeList
),
112 0, 65535, FALSE
, AnyPropertyType
, &acttype
, &actfmt
, &count
,
113 &bytesret
, (unsigned char**)&xdndtypes
);
119 xdndtypes
= &event
->data
.l
[2];
127 for (; i
< count
; i
++)
129 if (xdndtypes
[i
] != 0)
131 char * pn
= XGetAtomName(event
->display
, xdndtypes
[i
]);
132 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes
[i
], pn
);
139 /* Do a one-time data read and cache results */
140 X11DRV_XDND_ResolveProperty(event
->display
, event
->window
,
141 event
->data
.l
[1], xdndtypes
, &count
);
143 if (event
->data
.l
[1] & 1)
146 else if (event
->message_type
== x11drv_atom(XdndPosition
))
148 XClientMessageEvent e
;
149 int accept
= 0; /* Assume we're not accepting */
151 XDNDxy
.x
= event
->data
.l
[2] >> 16;
152 XDNDxy
.y
= event
->data
.l
[2] & 0xFFFF;
154 /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
156 if (GetWindowLongW( hWnd
, GWL_EXSTYLE
) & WS_EX_ACCEPTFILES
)
159 TRACE("XDNDPosition. action req: %ld accept(%d) at x(%ld),y(%ld)\n",
160 event
->data
.l
[4], accept
, XDNDxy
.x
, XDNDxy
.y
);
163 * Let source know if we're accepting the drop by
164 * sending a status message.
166 e
.type
= ClientMessage
;
167 e
.display
= event
->display
;
168 e
.window
= event
->data
.l
[0];
169 e
.message_type
= x11drv_atom(XdndStatus
);
171 e
.data
.l
[0] = event
->window
;
172 e
.data
.l
[1] = accept
;
173 e
.data
.l
[2] = 0; /* Empty Rect */
174 e
.data
.l
[3] = 0; /* Empty Rect */
176 e
.data
.l
[4] = event
->data
.l
[4];
180 XSendEvent(event
->display
, event
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&e
);
183 /* FIXME: if drag accepted notify OLE of DragOver */
185 else if (event
->message_type
== x11drv_atom(XdndDrop
))
187 XClientMessageEvent e
;
191 /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
192 if (GetWindowLongW( hWnd
, GWL_EXSTYLE
) & WS_EX_ACCEPTFILES
)
193 X11DRV_XDND_SendDropFiles( hWnd
);
195 /* FIXME: Notify OLE of Drop */
196 X11DRV_XDND_FreeDragDropOp();
198 /* Tell the target we are finished. */
199 memset(&e
, 0, sizeof(e
));
200 e
.type
= ClientMessage
;
201 e
.display
= event
->display
;
202 e
.window
= event
->data
.l
[0];
203 e
.message_type
= x11drv_atom(XdndFinished
);
205 e
.data
.l
[0] = event
->window
;
207 XSendEvent(event
->display
, event
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&e
);
210 else if (event
->message_type
== x11drv_atom(XdndLeave
))
212 TRACE("DND Operation canceled\n");
214 X11DRV_XDND_FreeDragDropOp();
216 /* FIXME: Notify OLE of DragLeave */
218 else /* Not an XDND message */
225 /**************************************************************************
226 * X11DRV_XDND_ResolveProperty
228 * Resolve all MIME types to windows clipboard formats. All data is cached.
230 static void X11DRV_XDND_ResolveProperty(Display
*display
, Window xwin
, Time tm
,
231 Atom
*types
, unsigned long *count
)
238 unsigned long bytesret
, icount
;
240 unsigned char* data
= NULL
;
242 TRACE("count(%ld)\n", *count
);
244 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
246 for (i
= 0; i
< *count
; i
++)
248 TRACE("requesting atom %ld from xwin %ld\n", types
[i
], xwin
);
254 XConvertSelection(display
, x11drv_atom(XdndSelection
), types
[i
],
255 x11drv_atom(XdndTarget
), xwin
, /*tm*/CurrentTime
);
259 * Wait for SelectionNotify
261 for (j
= 0; j
< SELECTION_RETRIES
; j
++)
264 res
= XCheckTypedWindowEvent(display
, xwin
, SelectionNotify
, &xe
);
266 if (res
&& xe
.xselection
.selection
== x11drv_atom(XdndSelection
)) break;
268 usleep(SELECTION_WAIT
);
271 if (xe
.xselection
.property
== None
)
275 XGetWindowProperty(display
, xwin
, x11drv_atom(XdndTarget
), 0, 65535, FALSE
,
276 AnyPropertyType
, &acttype
, &actfmt
, &icount
, &bytesret
, &data
);
279 entries
+= X11DRV_XDND_MapFormat(types
[i
], data
, icount
* (actfmt
/ 8));
289 /**************************************************************************
290 * X11DRV_XDND_InsertXDNDData
292 * Cache available XDND property
294 static void X11DRV_XDND_InsertXDNDData(int property
, int format
, void* data
, unsigned int len
)
296 LPXDNDDATA current
= (LPXDNDDATA
) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(XDNDDATA
));
300 EnterCriticalSection(&xdnd_cs
);
301 current
->next
= XDNDData
;
302 current
->cf_xdnd
= property
;
303 current
->cf_win
= format
;
304 current
->data
= data
;
307 LeaveCriticalSection(&xdnd_cs
);
312 /**************************************************************************
313 * X11DRV_XDND_MapFormat
315 * Map XDND MIME format to windows clipboard format.
317 static int X11DRV_XDND_MapFormat(unsigned int property
, unsigned char *data
, int len
)
322 TRACE("%d: %s\n", property
, data
);
324 /* Always include the raw type */
325 xdata
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
326 memcpy(xdata
, data
, len
);
327 X11DRV_XDND_InsertXDNDData(property
, property
, xdata
, len
);
330 if (property
== x11drv_atom(text_plain
))
331 count
+= X11DRV_XDND_DeconstructTextPlain(property
, data
, len
);
332 else if (property
== x11drv_atom(text_html
))
333 count
+= X11DRV_XDND_DeconstructTextHTML(property
, data
, len
);
339 /**************************************************************************
340 * X11DRV_XDND_DeconstructTextPlain
342 * Interpret text/plain Data and add records to <dndfmt> linked list
344 static int X11DRV_XDND_DeconstructTextPlain(int property
, void* data
, int len
)
346 char *p
= (char*) data
;
350 /* Always suppply plain text */
351 X11DRV_XDND_UnixToDos(&dostext
, (char*)data
, len
);
352 X11DRV_XDND_InsertXDNDData(property
, CF_TEXT
, dostext
, strlen(dostext
));
355 TRACE("CF_TEXT (%d): %s\n", CF_TEXT
, dostext
);
357 /* Check for additional mappings */
358 while (*p
!= '\0' && *p
!= ':') /* Advance to end of protocol */
363 if (!strncasecmp(data
, "http", 4))
365 X11DRV_XDND_InsertXDNDData(property
, RegisterClipboardFormatA("UniformResourceLocator"),
366 strdup(dostext
), strlen(dostext
));
369 TRACE("UniformResourceLocator: %s\n", dostext
);
371 else if (!strncasecmp(data
, "file", 4))
375 pdf
= X11DRV_XDND_BuildDropFiles(p
+1, len
- 5, XDNDxy
);
378 unsigned int size
= HeapSize(GetProcessHeap(), 0, pdf
);
380 X11DRV_XDND_InsertXDNDData(property
, CF_HDROP
, pdf
, size
);
384 TRACE("CF_HDROP: %p\n", pdf
);
392 /**************************************************************************
393 * X11DRV_XDND_DeconstructTextHTML
395 * Interpret text/html data and add records to <dndfmt> linked list
397 static int X11DRV_XDND_DeconstructTextHTML(int property
, void* data
, int len
)
401 X11DRV_XDND_UnixToDos(&dostext
, data
, len
);
403 X11DRV_XDND_InsertXDNDData(property
,
404 RegisterClipboardFormatA("UniformResourceLocator"), dostext
, strlen(dostext
));
406 TRACE("UniformResourceLocator: %s\n", dostext
);
412 /**************************************************************************
413 * X11DRV_XDND_SendDropFiles
415 static void X11DRV_XDND_SendDropFiles(HWND hwnd
)
419 EnterCriticalSection(&xdnd_cs
);
423 /* Find CF_HDROP type if any */
424 while (current
!= NULL
)
426 if (current
->cf_win
== CF_HDROP
)
428 current
= current
->next
;
433 DROPFILES
*lpDrop
= (DROPFILES
*) current
->data
;
437 lpDrop
->pt
.x
= XDNDxy
.x
;
438 lpDrop
->pt
.y
= XDNDxy
.y
;
440 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd
,
441 ((char*)lpDrop
) + lpDrop
->pFiles
, ((char*)lpDrop
) + lpDrop
->pFiles
);
443 PostMessageA(hwnd
, WM_DROPFILES
, (WPARAM
)lpDrop
, 0L);
447 LeaveCriticalSection(&xdnd_cs
);
451 /**************************************************************************
452 * X11DRV_XDND_FreeDragDropOp
454 static void X11DRV_XDND_FreeDragDropOp(void)
461 EnterCriticalSection(&xdnd_cs
);
465 /** Free data cache */
466 while (current
!= NULL
)
468 next
= current
->next
;
469 HeapFree(GetProcessHeap(), 0, current
);
474 XDNDxy
.x
= XDNDxy
.y
= 0;
476 LeaveCriticalSection(&xdnd_cs
);
481 /**************************************************************************
482 * X11DRV_XDND_BuildDropFiles
484 static DROPFILES
* X11DRV_XDND_BuildDropFiles(char* filename
, unsigned int len
, POINT pt
)
489 DROPFILES
*lpDrop
= NULL
;
491 /* Advance to last starting slash */
493 while (*pfn
&& (*pfn
== '\\' || *pfn
=='/'))
499 /* Remove any trailing \r or \n */
502 if (*pfn
== '\r' || *pfn
== '\n')
510 TRACE("%s\n", filename
);
512 pathlen
= GetLongPathNameA(filename
, path
, MAX_PATH
);
515 lpDrop
= (DROPFILES
*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
516 sizeof(DROPFILES
) + pathlen
+ 1);
518 lpDrop
->pFiles
= sizeof(DROPFILES
);
522 lpDrop
->fWide
= FALSE
;
524 strcpy(((char*)lpDrop
)+lpDrop
->pFiles
, path
);
527 TRACE("resolved %s\n", lpDrop
? filename
: NULL
);
533 /**************************************************************************
534 * X11DRV_XDND_UnixToDos
536 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest
, char* lpsrc
, int len
)
539 unsigned int destlen
, lines
;
541 for (i
= 0, lines
= 0; i
<= len
; i
++)
543 if (lpsrc
[i
] == '\n')
547 destlen
= len
+ lines
+ 1;
551 char* lpstr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, destlen
);
552 for (i
= 0, lines
= 0; i
<= len
; i
++)
554 if (lpsrc
[i
] == '\n')
555 lpstr
[++lines
+ i
] = '\r';
556 lpstr
[lines
+ i
] = lpsrc
[i
];