push 83071add6f9f2e5b8e6b93c0b0e6ee71c20d2ad7
[wine/hacks.git] / dlls / winex11.drv / xdnd.c
blobf1d6d24e164c543e8cf2f1ef0e33b889ffe5aa80
1 /*
2 * XDND handler code
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <stdarg.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
35 #include "x11drv.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
48 int cf_win;
49 Atom cf_xdnd;
50 void *data;
51 unsigned int size;
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 =
72 0, 0, &xdnd_cs,
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 )
86 Atom *xdndtypes;
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
95 * the entire list. */
96 if (event->data.l[1] & 1)
98 Atom acttype;
99 int actfmt;
100 unsigned long bytesret;
102 /* Request supported formats from source window */
103 wine_tsx11_lock();
104 XGetWindowProperty(event->display, event->data.l[0], x11drv_atom(XdndTypeList),
105 0, 65535, FALSE, AnyPropertyType, &acttype, &actfmt, &count,
106 &bytesret, (unsigned char**)&xdndtypes);
107 wine_tsx11_unlock();
109 else
111 count = 3;
112 xdndtypes = (Atom*) &event->data.l[2];
115 if (TRACE_ON(xdnd))
117 unsigned int i = 0;
119 wine_tsx11_lock();
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);
126 XFree(pn);
129 wine_tsx11_unlock();
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)
137 XFree(xdndtypes);
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)
156 accept = 1;
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);
169 e.format = 32;
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 */
174 if (accept)
175 e.data.l[4] = event->data.l[4];
176 else
177 e.data.l[4] = None;
178 wine_tsx11_lock();
179 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
180 wine_tsx11_unlock();
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;
194 TRACE("\n");
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);
209 e.format = 32;
210 e.data.l[0] = event->window;
211 wine_tsx11_lock();
212 XSendEvent(event->display, event->data.l[0], False, NoEventMask, (XEvent*)&e);
213 wine_tsx11_unlock();
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)
239 unsigned int i, j;
240 BOOL res;
241 XEvent xe;
242 Atom acttype;
243 int actfmt;
244 unsigned long bytesret, icount;
245 int entries = 0;
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);
256 if (types[i] == 0)
257 continue;
259 wine_tsx11_lock();
260 XConvertSelection(display, x11drv_atom(XdndSelection), types[i],
261 x11drv_atom(XdndTarget), xwin, /*tm*/CurrentTime);
262 wine_tsx11_unlock();
265 * Wait for SelectionNotify
267 for (j = 0; j < SELECTION_RETRIES; j++)
269 wine_tsx11_lock();
270 res = XCheckTypedWindowEvent(display, xwin, SelectionNotify, &xe);
271 wine_tsx11_unlock();
272 if (res && xe.xselection.selection == x11drv_atom(XdndSelection)) break;
274 usleep(SELECTION_WAIT);
277 if (xe.xselection.property == None)
278 continue;
280 wine_tsx11_lock();
281 XGetWindowProperty(display, xwin, x11drv_atom(XdndTarget), 0, 65535, FALSE,
282 AnyPropertyType, &acttype, &actfmt, &icount, &bytesret, &data);
283 wine_tsx11_unlock();
285 entries += X11DRV_XDND_MapFormat(types[i], data, icount * (actfmt / 8));
286 wine_tsx11_lock();
287 XFree(data);
288 wine_tsx11_unlock();
291 *count = entries;
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));
304 if (current)
306 EnterCriticalSection(&xdnd_cs);
307 current->next = XDNDData;
308 current->cf_xdnd = property;
309 current->cf_win = format;
310 current->data = data;
311 current->size = len;
312 XDNDData = current;
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)
325 void* xdata;
326 int count = 0;
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);
334 count++;
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);
341 return count;
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;
353 char* dostext;
354 int count = 0;
356 /* Always suppply plain text */
357 X11DRV_XDND_UnixToDos(&dostext, (char*)data, len);
358 X11DRV_XDND_InsertXDNDData(property, CF_TEXT, dostext, strlen(dostext));
359 count++;
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 */
365 p++;
367 if (*p == ':')
369 if (!strncasecmp(data, "http", 4))
371 X11DRV_XDND_InsertXDNDData(property, RegisterClipboardFormatA("UniformResourceLocator"),
372 strdup(dostext), strlen(dostext));
373 count++;
375 TRACE("UniformResourceLocator: %s\n", dostext);
377 else if (!strncasecmp(data, "file", 4))
379 DROPFILES* pdf;
381 pdf = X11DRV_XDND_BuildDropFiles(p+1, len - 5, XDNDxy);
382 if (pdf)
384 unsigned int size = HeapSize(GetProcessHeap(), 0, pdf);
386 X11DRV_XDND_InsertXDNDData(property, CF_HDROP, pdf, size);
387 count++;
390 TRACE("CF_HDROP: %p\n", pdf);
394 return count;
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)
405 char* dostext;
407 X11DRV_XDND_UnixToDos(&dostext, data, len);
409 X11DRV_XDND_InsertXDNDData(property,
410 RegisterClipboardFormatA("UniformResourceLocator"), dostext, strlen(dostext));
412 TRACE("UniformResourceLocator: %s\n", dostext);
414 return 1;
418 /**************************************************************************
419 * X11DRV_XDND_SendDropFiles
421 static void X11DRV_XDND_SendDropFiles(HWND hwnd)
423 LPXDNDDATA current;
425 EnterCriticalSection(&xdnd_cs);
427 current = XDNDData;
429 /* Find CF_HDROP type if any */
430 while (current != NULL)
432 if (current->cf_win == CF_HDROP)
433 break;
434 current = current->next;
437 if (current != NULL)
439 DROPFILES *lpDrop = (DROPFILES*) current->data;
441 if (lpDrop)
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)
462 LPXDNDDATA next;
463 LPXDNDDATA current;
465 TRACE("\n");
467 EnterCriticalSection(&xdnd_cs);
469 current = XDNDData;
471 /** Free data cache */
472 while (current != NULL)
474 next = current->next;
475 HeapFree(GetProcessHeap(), 0, current);
476 current = next;
479 XDNDData = NULL;
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)
492 char* pfn;
493 int pathlen;
494 char path[MAX_PATH];
495 DROPFILES *lpDrop = NULL;
497 /* Advance to last starting slash */
498 pfn = filename + 1;
499 while (*pfn && (*pfn == '\\' || *pfn =='/'))
501 pfn++;
502 filename++;
505 /* Remove any trailing \r or \n */
506 while (*pfn)
508 if (*pfn == '\r' || *pfn == '\n')
510 *pfn = 0;
511 break;
513 pfn++;
516 TRACE("%s\n", filename);
518 pathlen = GetLongPathNameA(filename, path, MAX_PATH);
519 if (pathlen)
521 lpDrop = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DROPFILES) + pathlen + 1);
523 lpDrop->pFiles = sizeof(DROPFILES);
524 lpDrop->pt.x = pt.x;
525 lpDrop->pt.y = pt.y;
526 lpDrop->fNC = 0;
527 lpDrop->fWide = FALSE;
529 strcpy(((char*)lpDrop)+lpDrop->pFiles, path);
532 TRACE("resolved %s\n", lpDrop ? filename : NULL);
534 return lpDrop;
538 /**************************************************************************
539 * X11DRV_XDND_UnixToDos
541 static unsigned int X11DRV_XDND_UnixToDos(char** lpdest, char* lpsrc, int len)
543 int i;
544 unsigned int destlen, lines;
546 for (i = 0, lines = 0; i <= len; i++)
548 if (lpsrc[i] == '\n')
549 lines++;
552 destlen = len + lines + 1;
554 if (lpdest)
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];
564 *lpdest = lpstr;
567 return lines;