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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
36 #include "shlobj.h" /* DROPFILES */
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(xdnd);
42 /* Maximum wait time for selection notify */
43 #define SELECTION_RETRIES 500 /* wait for .1 seconds */
44 #define SELECTION_WAIT 1000 /* us */
46 typedef struct tagXDNDDATA
52 struct tagXDNDDATA *next;
53 } XDNDDATA, *LPXDNDDATA;
55 static LPXDNDDATA XDNDData = NULL;
56 static POINT XDNDxy = { 0, 0 };
58 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len);
59 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len);
60 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len);
61 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len);
62 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
63 Atom *types, unsigned long *count);
64 static void X11DRV_XDND_SendDropFiles(HWND hwnd);
65 static void X11DRV_XDND_FreeDragDropOp(void);
66 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len);
67 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt);
69 static CRITICAL_SECTION xdnd_cs;
70 static CRITICAL_SECTION_DEBUG critsect_debug =
73 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
74 0, 0, { (DWORD_PTR)(__FILE__ ": xdnd_cs") }
76 static CRITICAL_SECTION xdnd_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
79 /**************************************************************************
80 * X11DRV_XDND_EnterEvent
82 * Handle an XdndEnter event.
84 void X11DRV_XDND_EnterEvent( HWND hWnd, XClientMessageEvent *event )
87 unsigned long count = 0;
89 TRACE("ver(%ld) check-XdndTypeList(%ld) data=%ld,%ld,%ld,%ld,%ld\n",
90 (event->data.l[1] & 0xFF000000) >> 24, (event->data.l[1] & 1),
91 event->data.l[0], event->data.l[1], event->data.l[2],
92 event->data.l[3], event->data.l[4]);
94 /* If the source supports more than 3 data types we retrieve
96 if (event->data.l[1] & 1)
100 unsigned long bytesret;
102 /* Request supported formats from source window */
104 XGetWindowProperty(event->display, event->data.l[0], x11drv_atom(XdndTypeList),
105 0, 65535, FALSE, AnyPropertyType, &acttype, &actfmt, &count,
106 &bytesret, (unsigned char**)&xdndtypes);
112 xdndtypes = (Atom*) &event->data.l[2];
120 for (; i < count; i++)
122 if (xdndtypes[i] != 0)
124 char * pn = XGetAtomName(event->display, xdndtypes[i]);
125 TRACE("XDNDEnterAtom %ld: %s\n", xdndtypes[i], pn);
132 /* Do a one-time data read and cache results */
133 X11DRV_XDND_ResolveProperty(event->display, event->window,
134 event->data.l[1], xdndtypes, &count);
136 if (event->data.l[1] & 1)
140 /**************************************************************************
141 * X11DRV_XDND_PositionEvent
143 * Handle an XdndPosition event.
145 void X11DRV_XDND_PositionEvent( HWND hWnd, XClientMessageEvent *event )
147 XClientMessageEvent e;
148 int accept = 0; /* Assume we're not accepting */
150 XDNDxy.x = event->data.l[2] >> 16;
151 XDNDxy.y = event->data.l[2] & 0xFFFF;
153 /* FIXME: Notify OLE of DragEnter. Result determines if we accept */
155 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
158 TRACE("action req: %ld accept(%d) at x(%d),y(%d)\n",
159 event->data.l[4], accept, XDNDxy.x, XDNDxy.y);
162 * Let source know if we're accepting the drop by
163 * sending a status message.
165 e.type = ClientMessage;
166 e.display = event->display;
167 e.window = event->data.l[0];
168 e.message_type = x11drv_atom(XdndStatus);
170 e.data.l[0] = event->window;
171 e.data.l[1] = accept;
172 e.data.l[2] = 0; /* Empty Rect */
173 e.data.l[3] = 0; /* Empty Rect */
175 e.data.l[4] = event->data.l[4];
179 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
182 /* FIXME: if drag accepted notify OLE of DragOver */
185 /**************************************************************************
186 * X11DRV_XDND_DropEvent
188 * Handle an XdndDrop event.
190 void X11DRV_XDND_DropEvent( HWND hWnd, XClientMessageEvent *event )
192 XClientMessageEvent e;
196 /* If we have a HDROP type we send a WM_ACCEPTFILES.*/
197 if (GetWindowLongW( hWnd, GWL_EXSTYLE ) & WS_EX_ACCEPTFILES)
198 X11DRV_XDND_SendDropFiles( hWnd );
200 /* FIXME: Notify OLE of Drop */
201 X11DRV_XDND_FreeDragDropOp();
203 /* Tell the target we are finished. */
204 memset(&e, 0, sizeof(e));
205 e.type = ClientMessage;
206 e.display = event->display;
207 e.window = event->data.l[0];
208 e.message_type = x11drv_atom(XdndFinished);
210 e.data.l[0] = event->window;
212 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
216 /**************************************************************************
217 * X11DRV_XDND_LeaveEvent
219 * Handle an XdndLeave event.
221 void X11DRV_XDND_LeaveEvent( HWND hWnd, XClientMessageEvent *event )
223 TRACE("DND Operation canceled\n");
225 X11DRV_XDND_FreeDragDropOp();
227 /* FIXME: Notify OLE of DragLeave */
231 /**************************************************************************
232 * X11DRV_XDND_ResolveProperty
234 * Resolve all MIME types to windows clipboard formats. All data is cached.
236 static void X11DRV_XDND_ResolveProperty(Display *display, Window xwin, Time tm,
237 Atom *types, unsigned long *count)
244 unsigned long bytesret, icount;
246 unsigned char* data = NULL;
248 TRACE("count(%ld)\n", *count);
250 X11DRV_XDND_FreeDragDropOp(); /* Clear previously cached data */
252 for (i = 0; i < *count; i++)
254 TRACE("requesting atom %ld from xwin %ld\n", types[i], xwin);
260 XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
261 x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
265 * Wait for SelectionNotify
267 for (j = 0; j < SELECTION_RETRIES; j++)
270 res = XCheckTypedWindowEvent(display, xwin, SelectionNotify, &xe);
272 if (res && xe.xselection.selection == x11drv_atom(XdndSelection)) break;
274 usleep(SELECTION_WAIT);
277 if (xe.xselection.property == None)
281 XGetWindowProperty(display, xwin, x11drv_atom(XdndTarget), 0, 65535, FALSE,
282 AnyPropertyType, &acttype, &actfmt, &icount, &bytesret, &data);
285 entries += X11DRV_XDND_MapFormat(types[i], data, icount * (actfmt / 8));
295 /**************************************************************************
296 * X11DRV_XDND_InsertXDNDData
298 * Cache available XDND property
300 static void X11DRV_XDND_InsertXDNDData(int property, int format, void* data, unsigned int len)
302 LPXDNDDATA current = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(XDNDDATA));
306 EnterCriticalSection(&xdnd_cs);
307 current->next = XDNDData;
308 current->cf_xdnd = property;
309 current->cf_win = format;
310 current->data = data;
313 LeaveCriticalSection(&xdnd_cs);
318 /**************************************************************************
319 * X11DRV_XDND_MapFormat
321 * Map XDND MIME format to windows clipboard format.
323 static int X11DRV_XDND_MapFormat(unsigned int property, unsigned char *data, int len)
328 TRACE("%d: %s\n", property, data);
330 /* Always include the raw type */
331 xdata = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
332 memcpy(xdata, data, len);
333 X11DRV_XDND_InsertXDNDData(property, property, xdata, len);
336 if (property == x11drv_atom(text_plain))
337 count += X11DRV_XDND_DeconstructTextPlain(property, data, len);
338 else if (property == x11drv_atom(text_html))
339 count += X11DRV_XDND_DeconstructTextHTML(property, data, len);
345 /**************************************************************************
346 * X11DRV_XDND_DeconstructTextPlain
348 * Interpret text/plain Data and add records to <dndfmt> linked list
350 static int X11DRV_XDND_DeconstructTextPlain(int property, void* data, int len)
352 char *p = (char*) data;
356 /* Always suppply plain text */
357 X11DRV_XDND_UnixToDos(&dostext, (char*)data, len);
358 X11DRV_XDND_InsertXDNDData(property, CF_TEXT, dostext, strlen(dostext));
361 TRACE("CF_TEXT (%d): %s\n", CF_TEXT, dostext);
363 /* Check for additional mappings */
364 while (*p != '\0' && *p != ':') /* Advance to end of protocol */
369 if (!strncasecmp(data, "http", 4))
371 X11DRV_XDND_InsertXDNDData(property, RegisterClipboardFormatA("UniformResourceLocator"),
372 strdup(dostext), strlen(dostext));
375 TRACE("UniformResourceLocator: %s\n", dostext);
377 else if (!strncasecmp(data, "file", 4))
381 pdf = X11DRV_XDND_BuildDropFiles(p+1, len - 5, XDNDxy);
384 unsigned int size = HeapSize(GetProcessHeap(), 0, pdf);
386 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, pdf, size);
390 TRACE("CF_HDROP: %p\n", pdf);
398 /**************************************************************************
399 * X11DRV_XDND_DeconstructTextHTML
401 * Interpret text/html data and add records to <dndfmt> linked list
403 static int X11DRV_XDND_DeconstructTextHTML(int property, void* data, int len)
407 X11DRV_XDND_UnixToDos(&dostext, data, len);
409 X11DRV_XDND_InsertXDNDData(property,
410 RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
412 TRACE("UniformResourceLocator: %s\n", dostext);
418 /**************************************************************************
419 * X11DRV_XDND_SendDropFiles
421 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
425 EnterCriticalSection(&xdnd_cs);
429 /* Find CF_HDROP type if any */
430 while (current != NULL)
432 if (current->cf_win == CF_HDROP)
434 current = current->next;
439 DROPFILES *lpDrop = (DROPFILES*) current->data;
443 lpDrop->pt.x = XDNDxy.x;
444 lpDrop->pt.y = XDNDxy.y;
446 TRACE("Sending WM_DROPFILES: hWnd(0x%p) %p(%s)\n", hwnd,
447 ((char*)lpDrop) + lpDrop->pFiles, ((char*)lpDrop) + lpDrop->pFiles);
449 PostMessageA(hwnd, WM_DROPFILES, (WPARAM)lpDrop, 0L);
453 LeaveCriticalSection(&xdnd_cs);
457 /**************************************************************************
458 * X11DRV_XDND_FreeDragDropOp
460 static void X11DRV_XDND_FreeDragDropOp(void)
467 EnterCriticalSection(&xdnd_cs);
471 /** Free data cache */
472 while (current != NULL)
474 next = current->next;
475 HeapFree(GetProcessHeap(), 0, current);
480 XDNDxy.x = XDNDxy.y = 0;
482 LeaveCriticalSection(&xdnd_cs);
487 /**************************************************************************
488 * X11DRV_XDND_BuildDropFiles
490 static DROPFILES* X11DRV_XDND_BuildDropFiles(char* filename, unsigned int len, POINT pt)
495 DROPFILES *lpDrop = NULL;
497 /* Advance to last starting slash */
499 while (*pfn && (*pfn == '\\' || *pfn =='/'))
505 /* Remove any trailing \r or \n */
508 if (*pfn == '\r' || *pfn == '\n')
516 TRACE("%s\n", filename);
518 pathlen = GetLongPathNameA(filename, path, MAX_PATH);
521 lpDrop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DROPFILES) + pathlen + 1);
523 lpDrop->pFiles = sizeof(DROPFILES);
527 lpDrop->fWide = FALSE;
529 strcpy(((char*)lpDrop)+lpDrop->pFiles, path);
532 TRACE("resolved %s\n", lpDrop ? filename : NULL);
538 /**************************************************************************
539 * X11DRV_XDND_UnixToDos
541 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
544 unsigned int destlen, lines;
546 for (i = 0, lines = 0; i <= len; i++)
548 if (lpsrc[i] == '\n')
552 destlen = len + lines + 1;
556 char* lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, destlen);
557 for (i = 0, lines = 0; i <= len; i++)
559 if (lpsrc[i] == '\n')
560 lpstr[++lines + i] = '\r';
561 lpstr[lines + i] = lpsrc[i];